| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1 | /* | 
 | 2 | ** | 
 | 3 | ** Copyright 2012, The Android Open Source Project | 
 | 4 | ** | 
 | 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); | 
 | 6 | ** you may not use this file except in compliance with the License. | 
 | 7 | ** You may obtain a copy of the License at | 
 | 8 | ** | 
 | 9 | **     http://www.apache.org/licenses/LICENSE-2.0 | 
 | 10 | ** | 
 | 11 | ** Unless required by applicable law or agreed to in writing, software | 
 | 12 | ** distributed under the License is distributed on an "AS IS" BASIS, | 
 | 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
 | 14 | ** See the License for the specific language governing permissions and | 
 | 15 | ** limitations under the License. | 
 | 16 | */ | 
 | 17 |  | 
 | 18 |  | 
 | 19 | #define LOG_TAG "AudioFlinger" | 
 | 20 | //#define LOG_NDEBUG 0 | 
| Alex Ray | 371eb97 | 2012-11-30 11:11:54 -0800 | [diff] [blame] | 21 | #define ATRACE_TAG ATRACE_TAG_AUDIO | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 22 |  | 
| Glenn Kasten | 153b9fe | 2013-07-15 11:23:36 -0700 | [diff] [blame] | 23 | #include "Configuration.h" | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 24 | #include <math.h> | 
 | 25 | #include <fcntl.h> | 
| Glenn Kasten | ad8510a | 2015-02-17 16:24:07 -0800 | [diff] [blame] | 26 | #include <linux/futex.h> | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 27 | #include <sys/stat.h> | 
| Glenn Kasten | ad8510a | 2015-02-17 16:24:07 -0800 | [diff] [blame] | 28 | #include <sys/syscall.h> | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 29 | #include <cutils/properties.h> | 
| Glenn Kasten | 1ab85ec | 2013-05-31 09:18:43 -0700 | [diff] [blame] | 30 | #include <media/AudioParameter.h> | 
| Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 31 | #include <media/AudioResamplerPublic.h> | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 32 | #include <utils/Log.h> | 
| Alex Ray | 371eb97 | 2012-11-30 11:11:54 -0800 | [diff] [blame] | 33 | #include <utils/Trace.h> | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 34 |  | 
 | 35 | #include <private/media/AudioTrackShared.h> | 
 | 36 | #include <hardware/audio.h> | 
 | 37 | #include <audio_effects/effect_ns.h> | 
 | 38 | #include <audio_effects/effect_aec.h> | 
 | 39 | #include <audio_utils/primitives.h> | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 40 | #include <audio_utils/format.h> | 
| Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 41 | #include <audio_utils/minifloat.h> | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 42 |  | 
 | 43 | // NBAIO implementations | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 44 | #include <media/nbaio/AudioStreamInSource.h> | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 45 | #include <media/nbaio/AudioStreamOutSink.h> | 
 | 46 | #include <media/nbaio/MonoPipe.h> | 
 | 47 | #include <media/nbaio/MonoPipeReader.h> | 
 | 48 | #include <media/nbaio/Pipe.h> | 
 | 49 | #include <media/nbaio/PipeReader.h> | 
 | 50 | #include <media/nbaio/SourceAudioBufferProvider.h> | 
 | 51 |  | 
 | 52 | #include <powermanager/PowerManager.h> | 
 | 53 |  | 
 | 54 | #include <common_time/cc_helper.h> | 
 | 55 | #include <common_time/local_clock.h> | 
 | 56 |  | 
 | 57 | #include "AudioFlinger.h" | 
 | 58 | #include "AudioMixer.h" | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 59 | #include "BufferProviders.h" | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 60 | #include "FastMixer.h" | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 61 | #include "FastCapture.h" | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 62 | #include "ServiceUtilities.h" | 
 | 63 | #include "SchedulingPolicyService.h" | 
 | 64 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 65 | #ifdef ADD_BATTERY_DATA | 
 | 66 | #include <media/IMediaPlayerService.h> | 
 | 67 | #include <media/IMediaDeathNotifier.h> | 
 | 68 | #endif | 
 | 69 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 70 | #ifdef DEBUG_CPU_USAGE | 
 | 71 | #include <cpustats/CentralTendencyStatistics.h> | 
 | 72 | #include <cpustats/ThreadCpuUsage.h> | 
 | 73 | #endif | 
 | 74 |  | 
 | 75 | // ---------------------------------------------------------------------------- | 
 | 76 |  | 
 | 77 | // Note: the following macro is used for extremely verbose logging message.  In | 
 | 78 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to | 
 | 79 | // 0; but one side effect of this is to turn all LOGV's as well.  Some messages | 
 | 80 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT | 
 | 81 | // turned on.  Do not uncomment the #def below unless you really know what you | 
 | 82 | // are doing and want to see all of the extremely verbose messages. | 
 | 83 | //#define VERY_VERY_VERBOSE_LOGGING | 
 | 84 | #ifdef VERY_VERY_VERBOSE_LOGGING | 
 | 85 | #define ALOGVV ALOGV | 
 | 86 | #else | 
 | 87 | #define ALOGVV(a...) do { } while(0) | 
 | 88 | #endif | 
 | 89 |  | 
| Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 90 | // TODO: Move these macro/inlines to a header file. | 
| Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 91 | #define max(a, b) ((a) > (b) ? (a) : (b)) | 
| Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 92 | template <typename T> | 
 | 93 | static inline T min(const T& a, const T& b) | 
 | 94 | { | 
 | 95 |     return a < b ? a : b; | 
 | 96 | } | 
| Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 97 |  | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 98 | #ifndef ARRAY_SIZE | 
 | 99 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) | 
 | 100 | #endif | 
 | 101 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 102 | namespace android { | 
 | 103 |  | 
 | 104 | // retry counts for buffer fill timeout | 
 | 105 | // 50 * ~20msecs = 1 second | 
 | 106 | static const int8_t kMaxTrackRetries = 50; | 
 | 107 | static const int8_t kMaxTrackStartupRetries = 50; | 
 | 108 | // allow less retry attempts on direct output thread. | 
 | 109 | // direct outputs can be a scarce resource in audio hardware and should | 
 | 110 | // be released as quickly as possible. | 
 | 111 | static const int8_t kMaxTrackRetriesDirect = 2; | 
 | 112 |  | 
 | 113 | // don't warn about blocked writes or record buffer overflows more often than this | 
 | 114 | static const nsecs_t kWarningThrottleNs = seconds(5); | 
 | 115 |  | 
 | 116 | // RecordThread loop sleep time upon application overrun or audio HAL read error | 
 | 117 | static const int kRecordThreadSleepUs = 5000; | 
 | 118 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 119 | // maximum time to wait in sendConfigEvent_l() for a status to be received | 
 | 120 | static const nsecs_t kConfigEventTimeoutNs = seconds(2); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 121 |  | 
 | 122 | // minimum sleep time for the mixer thread loop when tracks are active but in underrun | 
 | 123 | static const uint32_t kMinThreadSleepTimeUs = 5000; | 
 | 124 | // maximum divider applied to the active sleep time in the mixer thread loop | 
 | 125 | static const uint32_t kMaxThreadSleepTimeShift = 2; | 
 | 126 |  | 
| Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 127 | // minimum normal sink buffer size, expressed in milliseconds rather than frames | 
 | 128 | static const uint32_t kMinNormalSinkBufferSizeMs = 20; | 
 | 129 | // maximum normal sink buffer size | 
 | 130 | static const uint32_t kMaxNormalSinkBufferSizeMs = 24; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 131 |  | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 132 | // Offloaded output thread standby delay: allows track transition without going to standby | 
 | 133 | static const nsecs_t kOffloadStandbyDelayNs = seconds(1); | 
 | 134 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 135 | // Whether to use fast mixer | 
 | 136 | static const enum { | 
 | 137 |     FastMixer_Never,    // never initialize or use: for debugging only | 
 | 138 |     FastMixer_Always,   // always initialize and use, even if not needed: for debugging only | 
 | 139 |                         // normal mixer multiplier is 1 | 
 | 140 |     FastMixer_Static,   // initialize if needed, then use all the time if initialized, | 
 | 141 |                         // multiplier is calculated based on min & max normal mixer buffer size | 
 | 142 |     FastMixer_Dynamic,  // initialize if needed, then use dynamically depending on track load, | 
 | 143 |                         // multiplier is calculated based on min & max normal mixer buffer size | 
 | 144 |     // FIXME for FastMixer_Dynamic: | 
 | 145 |     //  Supporting this option will require fixing HALs that can't handle large writes. | 
 | 146 |     //  For example, one HAL implementation returns an error from a large write, | 
 | 147 |     //  and another HAL implementation corrupts memory, possibly in the sample rate converter. | 
 | 148 |     //  We could either fix the HAL implementations, or provide a wrapper that breaks | 
 | 149 |     //  up large writes into smaller ones, and the wrapper would need to deal with scheduler. | 
 | 150 | } kUseFastMixer = FastMixer_Static; | 
 | 151 |  | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 152 | // Whether to use fast capture | 
 | 153 | static const enum { | 
 | 154 |     FastCapture_Never,  // never initialize or use: for debugging only | 
 | 155 |     FastCapture_Always, // always initialize and use, even if not needed: for debugging only | 
 | 156 |     FastCapture_Static, // initialize if needed, then use all the time if initialized | 
 | 157 | } kUseFastCapture = FastCapture_Static; | 
 | 158 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 159 | // Priorities for requestPriority | 
 | 160 | static const int kPriorityAudioApp = 2; | 
 | 161 | static const int kPriorityFastMixer = 3; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 162 | static const int kPriorityFastCapture = 3; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 163 |  | 
 | 164 | // IAudioFlinger::createTrack() reports back to client the total size of shared memory area | 
 | 165 | // for the track.  The client then sub-divides this into smaller buffers for its use. | 
| Glenn Kasten | b5fed68 | 2013-12-03 09:06:43 -0800 | [diff] [blame] | 166 | // Currently the client uses N-buffering by default, but doesn't tell us about the value of N. | 
 | 167 | // So for now we just assume that client is double-buffered for fast tracks. | 
 | 168 | // FIXME It would be better for client to tell AudioFlinger the value of N, | 
 | 169 | // so AudioFlinger could allocate the right amount of memory. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 170 | // See the client's minBufCount and mNotificationFramesAct calculations for details. | 
| Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 171 |  | 
 | 172 | // This is the default value, if not specified by property. | 
| Glenn Kasten | b5fed68 | 2013-12-03 09:06:43 -0800 | [diff] [blame] | 173 | static const int kFastTrackMultiplier = 2; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 174 |  | 
| Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 175 | // The minimum and maximum allowed values | 
 | 176 | static const int kFastTrackMultiplierMin = 1; | 
 | 177 | static const int kFastTrackMultiplierMax = 2; | 
 | 178 |  | 
 | 179 | // The actual value to use, which can be specified per-device via property af.fast_track_multiplier. | 
 | 180 | static int sFastTrackMultiplier = kFastTrackMultiplier; | 
 | 181 |  | 
| Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 182 | // See Thread::readOnlyHeap(). | 
 | 183 | // Initially this heap is used to allocate client buffers for "fast" AudioRecord. | 
 | 184 | // Eventually it will be the single buffer that FastCapture writes into via HAL read(), | 
 | 185 | // and that all "fast" AudioRecord clients read from.  In either case, the size can be small. | 
| Glenn Kasten | 9f81de3 | 2014-07-27 15:02:23 -0700 | [diff] [blame] | 186 | static const size_t kRecordThreadReadOnlyHeapSize = 0x2000; | 
| Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 187 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 188 | // ---------------------------------------------------------------------------- | 
 | 189 |  | 
| Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 190 | static pthread_once_t sFastTrackMultiplierOnce = PTHREAD_ONCE_INIT; | 
 | 191 |  | 
 | 192 | static void sFastTrackMultiplierInit() | 
 | 193 | { | 
 | 194 |     char value[PROPERTY_VALUE_MAX]; | 
 | 195 |     if (property_get("af.fast_track_multiplier", value, NULL) > 0) { | 
 | 196 |         char *endptr; | 
 | 197 |         unsigned long ul = strtoul(value, &endptr, 0); | 
 | 198 |         if (*endptr == '\0' && kFastTrackMultiplierMin <= ul && ul <= kFastTrackMultiplierMax) { | 
 | 199 |             sFastTrackMultiplier = (int) ul; | 
 | 200 |         } | 
 | 201 |     } | 
 | 202 | } | 
 | 203 |  | 
 | 204 | // ---------------------------------------------------------------------------- | 
 | 205 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 206 | #ifdef ADD_BATTERY_DATA | 
 | 207 | // To collect the amplifier usage | 
 | 208 | static void addBatteryData(uint32_t params) { | 
 | 209 |     sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService(); | 
 | 210 |     if (service == NULL) { | 
 | 211 |         // it already logged | 
 | 212 |         return; | 
 | 213 |     } | 
 | 214 |  | 
 | 215 |     service->addBatteryData(params); | 
 | 216 | } | 
 | 217 | #endif | 
 | 218 |  | 
 | 219 |  | 
 | 220 | // ---------------------------------------------------------------------------- | 
 | 221 | //      CPU Stats | 
 | 222 | // ---------------------------------------------------------------------------- | 
 | 223 |  | 
 | 224 | class CpuStats { | 
 | 225 | public: | 
 | 226 |     CpuStats(); | 
 | 227 |     void sample(const String8 &title); | 
 | 228 | #ifdef DEBUG_CPU_USAGE | 
 | 229 | private: | 
 | 230 |     ThreadCpuUsage mCpuUsage;           // instantaneous thread CPU usage in wall clock ns | 
 | 231 |     CentralTendencyStatistics mWcStats; // statistics on thread CPU usage in wall clock ns | 
 | 232 |  | 
 | 233 |     CentralTendencyStatistics mHzStats; // statistics on thread CPU usage in cycles | 
 | 234 |  | 
 | 235 |     int mCpuNum;                        // thread's current CPU number | 
 | 236 |     int mCpukHz;                        // frequency of thread's current CPU in kHz | 
 | 237 | #endif | 
 | 238 | }; | 
 | 239 |  | 
 | 240 | CpuStats::CpuStats() | 
 | 241 | #ifdef DEBUG_CPU_USAGE | 
 | 242 |     : mCpuNum(-1), mCpukHz(-1) | 
 | 243 | #endif | 
 | 244 | { | 
 | 245 | } | 
 | 246 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 247 | void CpuStats::sample(const String8 &title | 
 | 248 | #ifndef DEBUG_CPU_USAGE | 
 | 249 |                 __unused | 
 | 250 | #endif | 
 | 251 |         ) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 252 | #ifdef DEBUG_CPU_USAGE | 
 | 253 |     // get current thread's delta CPU time in wall clock ns | 
 | 254 |     double wcNs; | 
 | 255 |     bool valid = mCpuUsage.sampleAndEnable(wcNs); | 
 | 256 |  | 
 | 257 |     // record sample for wall clock statistics | 
 | 258 |     if (valid) { | 
 | 259 |         mWcStats.sample(wcNs); | 
 | 260 |     } | 
 | 261 |  | 
 | 262 |     // get the current CPU number | 
 | 263 |     int cpuNum = sched_getcpu(); | 
 | 264 |  | 
 | 265 |     // get the current CPU frequency in kHz | 
 | 266 |     int cpukHz = mCpuUsage.getCpukHz(cpuNum); | 
 | 267 |  | 
 | 268 |     // check if either CPU number or frequency changed | 
 | 269 |     if (cpuNum != mCpuNum || cpukHz != mCpukHz) { | 
 | 270 |         mCpuNum = cpuNum; | 
 | 271 |         mCpukHz = cpukHz; | 
 | 272 |         // ignore sample for purposes of cycles | 
 | 273 |         valid = false; | 
 | 274 |     } | 
 | 275 |  | 
 | 276 |     // if no change in CPU number or frequency, then record sample for cycle statistics | 
 | 277 |     if (valid && mCpukHz > 0) { | 
 | 278 |         double cycles = wcNs * cpukHz * 0.000001; | 
 | 279 |         mHzStats.sample(cycles); | 
 | 280 |     } | 
 | 281 |  | 
 | 282 |     unsigned n = mWcStats.n(); | 
 | 283 |     // mCpuUsage.elapsed() is expensive, so don't call it every loop | 
 | 284 |     if ((n & 127) == 1) { | 
 | 285 |         long long elapsed = mCpuUsage.elapsed(); | 
 | 286 |         if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { | 
 | 287 |             double perLoop = elapsed / (double) n; | 
 | 288 |             double perLoop100 = perLoop * 0.01; | 
 | 289 |             double perLoop1k = perLoop * 0.001; | 
 | 290 |             double mean = mWcStats.mean(); | 
 | 291 |             double stddev = mWcStats.stddev(); | 
 | 292 |             double minimum = mWcStats.minimum(); | 
 | 293 |             double maximum = mWcStats.maximum(); | 
 | 294 |             double meanCycles = mHzStats.mean(); | 
 | 295 |             double stddevCycles = mHzStats.stddev(); | 
 | 296 |             double minCycles = mHzStats.minimum(); | 
 | 297 |             double maxCycles = mHzStats.maximum(); | 
 | 298 |             mCpuUsage.resetElapsed(); | 
 | 299 |             mWcStats.reset(); | 
 | 300 |             mHzStats.reset(); | 
 | 301 |             ALOGD("CPU usage for %s over past %.1f secs\n" | 
 | 302 |                 "  (%u mixer loops at %.1f mean ms per loop):\n" | 
 | 303 |                 "  us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n" | 
 | 304 |                 "  %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n" | 
 | 305 |                 "  MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f", | 
 | 306 |                     title.string(), | 
 | 307 |                     elapsed * .000000001, n, perLoop * .000001, | 
 | 308 |                     mean * .001, | 
 | 309 |                     stddev * .001, | 
 | 310 |                     minimum * .001, | 
 | 311 |                     maximum * .001, | 
 | 312 |                     mean / perLoop100, | 
 | 313 |                     stddev / perLoop100, | 
 | 314 |                     minimum / perLoop100, | 
 | 315 |                     maximum / perLoop100, | 
 | 316 |                     meanCycles / perLoop1k, | 
 | 317 |                     stddevCycles / perLoop1k, | 
 | 318 |                     minCycles / perLoop1k, | 
 | 319 |                     maxCycles / perLoop1k); | 
 | 320 |  | 
 | 321 |         } | 
 | 322 |     } | 
 | 323 | #endif | 
 | 324 | }; | 
 | 325 |  | 
 | 326 | // ---------------------------------------------------------------------------- | 
 | 327 | //      ThreadBase | 
 | 328 | // ---------------------------------------------------------------------------- | 
 | 329 |  | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 330 | // static | 
 | 331 | const char *AudioFlinger::ThreadBase::threadTypeToString(AudioFlinger::ThreadBase::type_t type) | 
 | 332 | { | 
 | 333 |     switch (type) { | 
 | 334 |     case MIXER: | 
 | 335 |         return "MIXER"; | 
 | 336 |     case DIRECT: | 
 | 337 |         return "DIRECT"; | 
 | 338 |     case DUPLICATING: | 
 | 339 |         return "DUPLICATING"; | 
 | 340 |     case RECORD: | 
 | 341 |         return "RECORD"; | 
 | 342 |     case OFFLOAD: | 
 | 343 |         return "OFFLOAD"; | 
 | 344 |     default: | 
 | 345 |         return "unknown"; | 
 | 346 |     } | 
 | 347 | } | 
 | 348 |  | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 349 | String8 devicesToString(audio_devices_t devices) | 
 | 350 | { | 
 | 351 |     static const struct mapping { | 
 | 352 |         audio_devices_t mDevices; | 
 | 353 |         const char *    mString; | 
 | 354 |     } mappingsOut[] = { | 
 | 355 |         AUDIO_DEVICE_OUT_EARPIECE,          "EARPIECE", | 
 | 356 |         AUDIO_DEVICE_OUT_SPEAKER,           "SPEAKER", | 
 | 357 |         AUDIO_DEVICE_OUT_WIRED_HEADSET,     "WIRED_HEADSET", | 
 | 358 |         AUDIO_DEVICE_OUT_WIRED_HEADPHONE,   "WIRED_HEADPHONE", | 
| Glenn Kasten | 84d61ca | 2015-05-06 18:32:13 -0700 | [diff] [blame] | 359 |         AUDIO_DEVICE_OUT_BLUETOOTH_SCO,     "BLUETOOTH_SCO", | 
 | 360 |         AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET,     "BLUETOOTH_SCO_HEADSET", | 
 | 361 |         AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT,      "BLUETOOTH_SCO_CARKIT", | 
 | 362 |         AUDIO_DEVICE_OUT_BLUETOOTH_A2DP,            "BLUETOOTH_A2DP", | 
 | 363 |         AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES, "BLUETOOTH_A2DP_HEADPHONES", | 
 | 364 |         AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER,    "BLUETOOTH_A2DP_SPEAKER", | 
 | 365 |         AUDIO_DEVICE_OUT_AUX_DIGITAL,       "AUX_DIGITAL", | 
 | 366 |         AUDIO_DEVICE_OUT_HDMI,              "HDMI", | 
 | 367 |         AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET, "ANLG_DOCK_HEADSET", | 
 | 368 |         AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, "DGTL_DOCK_HEADSET", | 
 | 369 |         AUDIO_DEVICE_OUT_USB_ACCESSORY,     "USB_ACCESSORY", | 
 | 370 |         AUDIO_DEVICE_OUT_USB_DEVICE,        "USB_DEVICE", | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 371 |         AUDIO_DEVICE_OUT_TELEPHONY_TX,      "TELEPHONY_TX", | 
| Glenn Kasten | 84d61ca | 2015-05-06 18:32:13 -0700 | [diff] [blame] | 372 |         AUDIO_DEVICE_OUT_LINE,              "LINE", | 
 | 373 |         AUDIO_DEVICE_OUT_HDMI_ARC,          "HDMI_ARC", | 
 | 374 |         AUDIO_DEVICE_OUT_SPDIF,             "SPDIF", | 
 | 375 |         AUDIO_DEVICE_OUT_FM,                "FM", | 
 | 376 |         AUDIO_DEVICE_OUT_AUX_LINE,          "AUX_LINE", | 
 | 377 |         AUDIO_DEVICE_OUT_SPEAKER_SAFE,      "SPEAKER_SAFE", | 
| Eric Laurent | b9d7333 | 2015-06-30 17:09:20 -0700 | [diff] [blame^] | 378 |         AUDIO_DEVICE_OUT_IP,                "IP", | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 379 |         AUDIO_DEVICE_NONE,                  "NONE",         // must be last | 
 | 380 |     }, mappingsIn[] = { | 
| Glenn Kasten | 84d61ca | 2015-05-06 18:32:13 -0700 | [diff] [blame] | 381 |         AUDIO_DEVICE_IN_COMMUNICATION,      "COMMUNICATION", | 
 | 382 |         AUDIO_DEVICE_IN_AMBIENT,            "AMBIENT", | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 383 |         AUDIO_DEVICE_IN_BUILTIN_MIC,        "BUILTIN_MIC", | 
| Glenn Kasten | 84d61ca | 2015-05-06 18:32:13 -0700 | [diff] [blame] | 384 |         AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET,  "BLUETOOTH_SCO_HEADSET", | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 385 |         AUDIO_DEVICE_IN_WIRED_HEADSET,      "WIRED_HEADSET", | 
| Glenn Kasten | 84d61ca | 2015-05-06 18:32:13 -0700 | [diff] [blame] | 386 |         AUDIO_DEVICE_IN_AUX_DIGITAL,        "AUX_DIGITAL", | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 387 |         AUDIO_DEVICE_IN_VOICE_CALL,         "VOICE_CALL", | 
| Glenn Kasten | 84d61ca | 2015-05-06 18:32:13 -0700 | [diff] [blame] | 388 |         AUDIO_DEVICE_IN_TELEPHONY_RX,       "TELEPHONY_RX", | 
 | 389 |         AUDIO_DEVICE_IN_BACK_MIC,           "BACK_MIC", | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 390 |         AUDIO_DEVICE_IN_REMOTE_SUBMIX,      "REMOTE_SUBMIX", | 
| Glenn Kasten | 84d61ca | 2015-05-06 18:32:13 -0700 | [diff] [blame] | 391 |         AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET,  "ANLG_DOCK_HEADSET", | 
 | 392 |         AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET,  "DGTL_DOCK_HEADSET", | 
 | 393 |         AUDIO_DEVICE_IN_USB_ACCESSORY,      "USB_ACCESSORY", | 
 | 394 |         AUDIO_DEVICE_IN_USB_DEVICE,         "USB_DEVICE", | 
 | 395 |         AUDIO_DEVICE_IN_FM_TUNER,           "FM_TUNER", | 
 | 396 |         AUDIO_DEVICE_IN_TV_TUNER,           "TV_TUNER", | 
 | 397 |         AUDIO_DEVICE_IN_LINE,               "LINE", | 
 | 398 |         AUDIO_DEVICE_IN_SPDIF,              "SPDIF", | 
 | 399 |         AUDIO_DEVICE_IN_BLUETOOTH_A2DP,     "BLUETOOTH_A2DP", | 
 | 400 |         AUDIO_DEVICE_IN_LOOPBACK,           "LOOPBACK", | 
| Eric Laurent | b9d7333 | 2015-06-30 17:09:20 -0700 | [diff] [blame^] | 401 |         AUDIO_DEVICE_IN_IP,                 "IP", | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 402 |         AUDIO_DEVICE_NONE,                  "NONE",         // must be last | 
 | 403 |     }; | 
 | 404 |     String8 result; | 
 | 405 |     audio_devices_t allDevices = AUDIO_DEVICE_NONE; | 
 | 406 |     const mapping *entry; | 
 | 407 |     if (devices & AUDIO_DEVICE_BIT_IN) { | 
 | 408 |         devices &= ~AUDIO_DEVICE_BIT_IN; | 
 | 409 |         entry = mappingsIn; | 
 | 410 |     } else { | 
 | 411 |         entry = mappingsOut; | 
 | 412 |     } | 
 | 413 |     for ( ; entry->mDevices != AUDIO_DEVICE_NONE; entry++) { | 
 | 414 |         allDevices = (audio_devices_t) (allDevices | entry->mDevices); | 
 | 415 |         if (devices & entry->mDevices) { | 
 | 416 |             if (!result.isEmpty()) { | 
 | 417 |                 result.append("|"); | 
 | 418 |             } | 
 | 419 |             result.append(entry->mString); | 
 | 420 |         } | 
 | 421 |     } | 
 | 422 |     if (devices & ~allDevices) { | 
 | 423 |         if (!result.isEmpty()) { | 
 | 424 |             result.append("|"); | 
 | 425 |         } | 
 | 426 |         result.appendFormat("0x%X", devices & ~allDevices); | 
 | 427 |     } | 
 | 428 |     if (result.isEmpty()) { | 
 | 429 |         result.append(entry->mString); | 
 | 430 |     } | 
 | 431 |     return result; | 
 | 432 | } | 
 | 433 |  | 
 | 434 | String8 inputFlagsToString(audio_input_flags_t flags) | 
 | 435 | { | 
 | 436 |     static const struct mapping { | 
 | 437 |         audio_input_flags_t     mFlag; | 
 | 438 |         const char *            mString; | 
 | 439 |     } mappings[] = { | 
 | 440 |         AUDIO_INPUT_FLAG_FAST,              "FAST", | 
 | 441 |         AUDIO_INPUT_FLAG_HW_HOTWORD,        "HW_HOTWORD", | 
 | 442 |         AUDIO_INPUT_FLAG_NONE,              "NONE",         // must be last | 
 | 443 |     }; | 
 | 444 |     String8 result; | 
 | 445 |     audio_input_flags_t allFlags = AUDIO_INPUT_FLAG_NONE; | 
 | 446 |     const mapping *entry; | 
 | 447 |     for (entry = mappings; entry->mFlag != AUDIO_INPUT_FLAG_NONE; entry++) { | 
 | 448 |         allFlags = (audio_input_flags_t) (allFlags | entry->mFlag); | 
 | 449 |         if (flags & entry->mFlag) { | 
 | 450 |             if (!result.isEmpty()) { | 
 | 451 |                 result.append("|"); | 
 | 452 |             } | 
 | 453 |             result.append(entry->mString); | 
 | 454 |         } | 
 | 455 |     } | 
 | 456 |     if (flags & ~allFlags) { | 
 | 457 |         if (!result.isEmpty()) { | 
 | 458 |             result.append("|"); | 
 | 459 |         } | 
 | 460 |         result.appendFormat("0x%X", flags & ~allFlags); | 
 | 461 |     } | 
 | 462 |     if (result.isEmpty()) { | 
 | 463 |         result.append(entry->mString); | 
 | 464 |     } | 
 | 465 |     return result; | 
 | 466 | } | 
 | 467 |  | 
 | 468 | String8 outputFlagsToString(audio_output_flags_t flags) | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 469 | { | 
 | 470 |     static const struct mapping { | 
 | 471 |         audio_output_flags_t    mFlag; | 
 | 472 |         const char *            mString; | 
 | 473 |     } mappings[] = { | 
 | 474 |         AUDIO_OUTPUT_FLAG_DIRECT,           "DIRECT", | 
 | 475 |         AUDIO_OUTPUT_FLAG_PRIMARY,          "PRIMARY", | 
 | 476 |         AUDIO_OUTPUT_FLAG_FAST,             "FAST", | 
 | 477 |         AUDIO_OUTPUT_FLAG_DEEP_BUFFER,      "DEEP_BUFFER", | 
| Glenn Kasten | dfb0e11 | 2015-02-18 14:33:39 -0800 | [diff] [blame] | 478 |         AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD, "COMPRESS_OFFLOAD", | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 479 |         AUDIO_OUTPUT_FLAG_NON_BLOCKING,     "NON_BLOCKING", | 
 | 480 |         AUDIO_OUTPUT_FLAG_HW_AV_SYNC,       "HW_AV_SYNC", | 
 | 481 |         AUDIO_OUTPUT_FLAG_NONE,             "NONE",         // must be last | 
 | 482 |     }; | 
 | 483 |     String8 result; | 
 | 484 |     audio_output_flags_t allFlags = AUDIO_OUTPUT_FLAG_NONE; | 
 | 485 |     const mapping *entry; | 
 | 486 |     for (entry = mappings; entry->mFlag != AUDIO_OUTPUT_FLAG_NONE; entry++) { | 
 | 487 |         allFlags = (audio_output_flags_t) (allFlags | entry->mFlag); | 
 | 488 |         if (flags & entry->mFlag) { | 
 | 489 |             if (!result.isEmpty()) { | 
 | 490 |                 result.append("|"); | 
 | 491 |             } | 
 | 492 |             result.append(entry->mString); | 
 | 493 |         } | 
 | 494 |     } | 
 | 495 |     if (flags & ~allFlags) { | 
 | 496 |         if (!result.isEmpty()) { | 
 | 497 |             result.append("|"); | 
 | 498 |         } | 
 | 499 |         result.appendFormat("0x%X", flags & ~allFlags); | 
 | 500 |     } | 
 | 501 |     if (result.isEmpty()) { | 
 | 502 |         result.append(entry->mString); | 
 | 503 |     } | 
 | 504 |     return result; | 
 | 505 | } | 
 | 506 |  | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 507 | const char *sourceToString(audio_source_t source) | 
 | 508 | { | 
 | 509 |     switch (source) { | 
 | 510 |     case AUDIO_SOURCE_DEFAULT:              return "default"; | 
 | 511 |     case AUDIO_SOURCE_MIC:                  return "mic"; | 
 | 512 |     case AUDIO_SOURCE_VOICE_UPLINK:         return "voice uplink"; | 
 | 513 |     case AUDIO_SOURCE_VOICE_DOWNLINK:       return "voice downlink"; | 
 | 514 |     case AUDIO_SOURCE_VOICE_CALL:           return "voice call"; | 
 | 515 |     case AUDIO_SOURCE_CAMCORDER:            return "camcorder"; | 
 | 516 |     case AUDIO_SOURCE_VOICE_RECOGNITION:    return "voice recognition"; | 
 | 517 |     case AUDIO_SOURCE_VOICE_COMMUNICATION:  return "voice communication"; | 
 | 518 |     case AUDIO_SOURCE_REMOTE_SUBMIX:        return "remote submix"; | 
 | 519 |     case AUDIO_SOURCE_FM_TUNER:             return "FM tuner"; | 
 | 520 |     case AUDIO_SOURCE_HOTWORD:              return "hotword"; | 
 | 521 |     default:                                return "unknown"; | 
 | 522 |     } | 
 | 523 | } | 
 | 524 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 525 | AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 526 |         audio_devices_t outDevice, audio_devices_t inDevice, type_t type, bool systemReady) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 527 |     :   Thread(false /*canCallJava*/), | 
 | 528 |         mType(type), | 
| Glenn Kasten | 9b58f63 | 2013-07-16 11:37:48 -0700 | [diff] [blame] | 529 |         mAudioFlinger(audioFlinger), | 
| Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 530 |         // mSampleRate, mFrameCount, mChannelMask, mChannelCount, mFrameSize, mFormat, mBufferSize | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 531 |         // are set by PlaybackThread::readOutputParameters_l() or | 
 | 532 |         // RecordThread::readInputParameters_l() | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 533 |         //FIXME: mStandby should be true here. Is this some kind of hack? | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 534 |         mStandby(false), mOutDevice(outDevice), mInDevice(inDevice), | 
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 535 |         mPrevInDevice(AUDIO_DEVICE_NONE), mAudioSource(AUDIO_SOURCE_DEFAULT), mId(id), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 536 |         // mName will be set by concrete (non-virtual) subclass | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 537 |         mDeathRecipient(new PMDeathRecipient(this)), | 
 | 538 |         mSystemReady(systemReady) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 539 | { | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 540 |     memset(&mPatch, 0, sizeof(struct audio_patch)); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 541 | } | 
 | 542 |  | 
 | 543 | AudioFlinger::ThreadBase::~ThreadBase() | 
 | 544 | { | 
| Glenn Kasten | c6ae3c8 | 2013-07-17 09:08:51 -0700 | [diff] [blame] | 545 |     // 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] | 546 |     mConfigEvents.clear(); | 
 | 547 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 548 |     // do not lock the mutex in destructor | 
 | 549 |     releaseWakeLock_l(); | 
 | 550 |     if (mPowerManager != 0) { | 
| Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 551 |         sp<IBinder> binder = IInterface::asBinder(mPowerManager); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 552 |         binder->unlinkToDeath(mDeathRecipient); | 
 | 553 |     } | 
 | 554 | } | 
 | 555 |  | 
| Glenn Kasten | cf04c2c | 2013-08-06 07:41:16 -0700 | [diff] [blame] | 556 | status_t AudioFlinger::ThreadBase::readyToRun() | 
 | 557 | { | 
 | 558 |     status_t status = initCheck(); | 
 | 559 |     if (status == NO_ERROR) { | 
 | 560 |         ALOGI("AudioFlinger's thread %p ready to run", this); | 
 | 561 |     } else { | 
 | 562 |         ALOGE("No working audio driver found."); | 
 | 563 |     } | 
 | 564 |     return status; | 
 | 565 | } | 
 | 566 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 567 | void AudioFlinger::ThreadBase::exit() | 
 | 568 | { | 
 | 569 |     ALOGV("ThreadBase::exit"); | 
 | 570 |     // do any cleanup required for exit to succeed | 
 | 571 |     preExit(); | 
 | 572 |     { | 
 | 573 |         // This lock prevents the following race in thread (uniprocessor for illustration): | 
 | 574 |         //  if (!exitPending()) { | 
 | 575 |         //      // context switch from here to exit() | 
 | 576 |         //      // exit() calls requestExit(), what exitPending() observes | 
 | 577 |         //      // exit() calls signal(), which is dropped since no waiters | 
 | 578 |         //      // context switch back from exit() to here | 
 | 579 |         //      mWaitWorkCV.wait(...); | 
 | 580 |         //      // now thread is hung | 
 | 581 |         //  } | 
 | 582 |         AutoMutex lock(mLock); | 
 | 583 |         requestExit(); | 
 | 584 |         mWaitWorkCV.broadcast(); | 
 | 585 |     } | 
 | 586 |     // When Thread::requestExitAndWait is made virtual and this method is renamed to | 
 | 587 |     // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();" | 
 | 588 |     requestExitAndWait(); | 
 | 589 | } | 
 | 590 |  | 
 | 591 | status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs) | 
 | 592 | { | 
 | 593 |     status_t status; | 
 | 594 |  | 
 | 595 |     ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); | 
 | 596 |     Mutex::Autolock _l(mLock); | 
 | 597 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 598 |     return sendSetParameterConfigEvent_l(keyValuePairs); | 
 | 599 | } | 
 | 600 |  | 
 | 601 | // sendConfigEvent_l() must be called with ThreadBase::mLock held | 
 | 602 | // Can temporarily release the lock if waiting for a reply from processConfigEvents_l(). | 
 | 603 | status_t AudioFlinger::ThreadBase::sendConfigEvent_l(sp<ConfigEvent>& event) | 
 | 604 | { | 
 | 605 |     status_t status = NO_ERROR; | 
 | 606 |  | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 607 |     if (event->mRequiresSystemReady && !mSystemReady) { | 
 | 608 |         event->mWaitStatus = false; | 
 | 609 |         mPendingConfigEvents.add(event); | 
 | 610 |         return status; | 
 | 611 |     } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 612 |     mConfigEvents.add(event); | 
 | 613 |     ALOGV("sendConfigEvent_l() num events %d event %d", mConfigEvents.size(), event->mType); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 614 |     mWaitWorkCV.signal(); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 615 |     mLock.unlock(); | 
 | 616 |     { | 
 | 617 |         Mutex::Autolock _l(event->mLock); | 
 | 618 |         while (event->mWaitStatus) { | 
 | 619 |             if (event->mCond.waitRelative(event->mLock, kConfigEventTimeoutNs) != NO_ERROR) { | 
 | 620 |                 event->mStatus = TIMED_OUT; | 
 | 621 |                 event->mWaitStatus = false; | 
 | 622 |             } | 
 | 623 |         } | 
 | 624 |         status = event->mStatus; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 625 |     } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 626 |     mLock.lock(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 627 |     return status; | 
 | 628 | } | 
 | 629 |  | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 630 | void AudioFlinger::ThreadBase::sendIoConfigEvent(audio_io_config_event event) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 631 | { | 
 | 632 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 633 |     sendIoConfigEvent_l(event); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 634 | } | 
 | 635 |  | 
 | 636 | // sendIoConfigEvent_l() must be called with ThreadBase::mLock held | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 637 | void AudioFlinger::ThreadBase::sendIoConfigEvent_l(audio_io_config_event event) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 638 | { | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 639 |     sp<ConfigEvent> configEvent = (ConfigEvent *)new IoConfigEvent(event); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 640 |     sendConfigEvent_l(configEvent); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 641 | } | 
 | 642 |  | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 643 | void AudioFlinger::ThreadBase::sendPrioConfigEvent(pid_t pid, pid_t tid, int32_t prio) | 
 | 644 | { | 
 | 645 |     Mutex::Autolock _l(mLock); | 
 | 646 |     sendPrioConfigEvent_l(pid, tid, prio); | 
 | 647 | } | 
 | 648 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 649 | // sendPrioConfigEvent_l() must be called with ThreadBase::mLock held | 
 | 650 | void AudioFlinger::ThreadBase::sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio) | 
 | 651 | { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 652 |     sp<ConfigEvent> configEvent = (ConfigEvent *)new PrioConfigEvent(pid, tid, prio); | 
 | 653 |     sendConfigEvent_l(configEvent); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 654 | } | 
 | 655 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 656 | // sendSetParameterConfigEvent_l() must be called with ThreadBase::mLock held | 
 | 657 | status_t AudioFlinger::ThreadBase::sendSetParameterConfigEvent_l(const String8& keyValuePair) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 658 | { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 659 |     sp<ConfigEvent> configEvent = (ConfigEvent *)new SetParameterConfigEvent(keyValuePair); | 
 | 660 |     return sendConfigEvent_l(configEvent); | 
| Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 661 | } | 
 | 662 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 663 | status_t AudioFlinger::ThreadBase::sendCreateAudioPatchConfigEvent( | 
 | 664 |                                                         const struct audio_patch *patch, | 
 | 665 |                                                         audio_patch_handle_t *handle) | 
 | 666 | { | 
 | 667 |     Mutex::Autolock _l(mLock); | 
 | 668 |     sp<ConfigEvent> configEvent = (ConfigEvent *)new CreateAudioPatchConfigEvent(*patch, *handle); | 
 | 669 |     status_t status = sendConfigEvent_l(configEvent); | 
 | 670 |     if (status == NO_ERROR) { | 
 | 671 |         CreateAudioPatchConfigEventData *data = | 
 | 672 |                                         (CreateAudioPatchConfigEventData *)configEvent->mData.get(); | 
 | 673 |         *handle = data->mHandle; | 
 | 674 |     } | 
 | 675 |     return status; | 
 | 676 | } | 
 | 677 |  | 
 | 678 | status_t AudioFlinger::ThreadBase::sendReleaseAudioPatchConfigEvent( | 
 | 679 |                                                                 const audio_patch_handle_t handle) | 
 | 680 | { | 
 | 681 |     Mutex::Autolock _l(mLock); | 
 | 682 |     sp<ConfigEvent> configEvent = (ConfigEvent *)new ReleaseAudioPatchConfigEvent(handle); | 
 | 683 |     return sendConfigEvent_l(configEvent); | 
 | 684 | } | 
 | 685 |  | 
 | 686 |  | 
| Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 687 | // post condition: mConfigEvents.isEmpty() | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 688 | void AudioFlinger::ThreadBase::processConfigEvents_l() | 
| Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 689 | { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 690 |     bool configChanged = false; | 
 | 691 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 692 |     while (!mConfigEvents.isEmpty()) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 693 |         ALOGV("processConfigEvents_l() remaining events %d", mConfigEvents.size()); | 
 | 694 |         sp<ConfigEvent> event = mConfigEvents[0]; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 695 |         mConfigEvents.removeAt(0); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 696 |         switch (event->mType) { | 
| Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 697 |         case CFG_EVENT_PRIO: { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 698 |             PrioConfigEventData *data = (PrioConfigEventData *)event->mData.get(); | 
 | 699 |             // FIXME Need to understand why this has to be done asynchronously | 
 | 700 |             int err = requestPriority(data->mPid, data->mTid, data->mPrio, | 
| Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 701 |                     true /*asynchronous*/); | 
 | 702 |             if (err != 0) { | 
 | 703 |                 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] | 704 |                       data->mPrio, data->mPid, data->mTid, err); | 
| Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 705 |             } | 
 | 706 |         } break; | 
 | 707 |         case CFG_EVENT_IO: { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 708 |             IoConfigEventData *data = (IoConfigEventData *)event->mData.get(); | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 709 |             ioConfigChanged(data->mEvent); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 710 |         } break; | 
 | 711 |         case CFG_EVENT_SET_PARAMETER: { | 
 | 712 |             SetParameterConfigEventData *data = (SetParameterConfigEventData *)event->mData.get(); | 
 | 713 |             if (checkForNewParameter_l(data->mKeyValuePairs, event->mStatus)) { | 
 | 714 |                 configChanged = true; | 
| Glenn Kasten | d5418eb | 2013-08-14 13:11:06 -0700 | [diff] [blame] | 715 |             } | 
| Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 716 |         } break; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 717 |         case CFG_EVENT_CREATE_AUDIO_PATCH: { | 
 | 718 |             CreateAudioPatchConfigEventData *data = | 
 | 719 |                                             (CreateAudioPatchConfigEventData *)event->mData.get(); | 
 | 720 |             event->mStatus = createAudioPatch_l(&data->mPatch, &data->mHandle); | 
 | 721 |         } break; | 
 | 722 |         case CFG_EVENT_RELEASE_AUDIO_PATCH: { | 
 | 723 |             ReleaseAudioPatchConfigEventData *data = | 
 | 724 |                                             (ReleaseAudioPatchConfigEventData *)event->mData.get(); | 
 | 725 |             event->mStatus = releaseAudioPatch_l(data->mHandle); | 
 | 726 |         } break; | 
| Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 727 |         default: | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 728 |             ALOG_ASSERT(false, "processConfigEvents_l() unknown event type %d", event->mType); | 
| Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 729 |             break; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 730 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 731 |         { | 
 | 732 |             Mutex::Autolock _l(event->mLock); | 
 | 733 |             if (event->mWaitStatus) { | 
 | 734 |                 event->mWaitStatus = false; | 
 | 735 |                 event->mCond.signal(); | 
 | 736 |             } | 
 | 737 |         } | 
 | 738 |         ALOGV_IF(mConfigEvents.isEmpty(), "processConfigEvents_l() DONE thread %p", this); | 
 | 739 |     } | 
 | 740 |  | 
 | 741 |     if (configChanged) { | 
 | 742 |         cacheParameters_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 743 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 744 | } | 
 | 745 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 746 | String8 channelMaskToString(audio_channel_mask_t mask, bool output) { | 
 | 747 |     String8 s; | 
| Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 748 |     const audio_channel_representation_t representation = | 
 | 749 |             audio_channel_mask_get_representation(mask); | 
| Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 750 |  | 
 | 751 |     switch (representation) { | 
 | 752 |     case AUDIO_CHANNEL_REPRESENTATION_POSITION: { | 
 | 753 |         if (output) { | 
 | 754 |             if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT) s.append("front-left, "); | 
 | 755 |             if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT) s.append("front-right, "); | 
 | 756 |             if (mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) s.append("front-center, "); | 
 | 757 |             if (mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) s.append("low freq, "); | 
 | 758 |             if (mask & AUDIO_CHANNEL_OUT_BACK_LEFT) s.append("back-left, "); | 
 | 759 |             if (mask & AUDIO_CHANNEL_OUT_BACK_RIGHT) s.append("back-right, "); | 
 | 760 |             if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER) s.append("front-left-of-center, "); | 
 | 761 |             if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER) s.append("front-right-of-center, "); | 
 | 762 |             if (mask & AUDIO_CHANNEL_OUT_BACK_CENTER) s.append("back-center, "); | 
 | 763 |             if (mask & AUDIO_CHANNEL_OUT_SIDE_LEFT) s.append("side-left, "); | 
 | 764 |             if (mask & AUDIO_CHANNEL_OUT_SIDE_RIGHT) s.append("side-right, "); | 
 | 765 |             if (mask & AUDIO_CHANNEL_OUT_TOP_CENTER) s.append("top-center ,"); | 
 | 766 |             if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT) s.append("top-front-left, "); | 
 | 767 |             if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER) s.append("top-front-center, "); | 
 | 768 |             if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT) s.append("top-front-right, "); | 
 | 769 |             if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_LEFT) s.append("top-back-left, "); | 
 | 770 |             if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_CENTER) s.append("top-back-center, " ); | 
 | 771 |             if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT) s.append("top-back-right, " ); | 
 | 772 |             if (mask & ~AUDIO_CHANNEL_OUT_ALL) s.append("unknown,  "); | 
 | 773 |         } else { | 
 | 774 |             if (mask & AUDIO_CHANNEL_IN_LEFT) s.append("left, "); | 
 | 775 |             if (mask & AUDIO_CHANNEL_IN_RIGHT) s.append("right, "); | 
 | 776 |             if (mask & AUDIO_CHANNEL_IN_FRONT) s.append("front, "); | 
 | 777 |             if (mask & AUDIO_CHANNEL_IN_BACK) s.append("back, "); | 
 | 778 |             if (mask & AUDIO_CHANNEL_IN_LEFT_PROCESSED) s.append("left-processed, "); | 
 | 779 |             if (mask & AUDIO_CHANNEL_IN_RIGHT_PROCESSED) s.append("right-processed, "); | 
 | 780 |             if (mask & AUDIO_CHANNEL_IN_FRONT_PROCESSED) s.append("front-processed, "); | 
 | 781 |             if (mask & AUDIO_CHANNEL_IN_BACK_PROCESSED) s.append("back-processed, "); | 
 | 782 |             if (mask & AUDIO_CHANNEL_IN_PRESSURE) s.append("pressure, "); | 
 | 783 |             if (mask & AUDIO_CHANNEL_IN_X_AXIS) s.append("X, "); | 
 | 784 |             if (mask & AUDIO_CHANNEL_IN_Y_AXIS) s.append("Y, "); | 
 | 785 |             if (mask & AUDIO_CHANNEL_IN_Z_AXIS) s.append("Z, "); | 
 | 786 |             if (mask & AUDIO_CHANNEL_IN_VOICE_UPLINK) s.append("voice-uplink, "); | 
 | 787 |             if (mask & AUDIO_CHANNEL_IN_VOICE_DNLINK) s.append("voice-dnlink, "); | 
 | 788 |             if (mask & ~AUDIO_CHANNEL_IN_ALL) s.append("unknown,  "); | 
 | 789 |         } | 
 | 790 |         const int len = s.length(); | 
 | 791 |         if (len > 2) { | 
 | 792 |             char *str = s.lockBuffer(len); // needed? | 
 | 793 |             s.unlockBuffer(len - 2);       // remove trailing ", " | 
 | 794 |         } | 
 | 795 |         return s; | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 796 |     } | 
| Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 797 |     case AUDIO_CHANNEL_REPRESENTATION_INDEX: | 
 | 798 |         s.appendFormat("index mask, bits:%#x", audio_channel_mask_get_bits(mask)); | 
 | 799 |         return s; | 
 | 800 |     default: | 
 | 801 |         s.appendFormat("unknown mask, representation:%d  bits:%#x", | 
 | 802 |                 representation, audio_channel_mask_get_bits(mask)); | 
 | 803 |         return s; | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 804 |     } | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 805 | } | 
 | 806 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 807 | void AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 808 | { | 
 | 809 |     const size_t SIZE = 256; | 
 | 810 |     char buffer[SIZE]; | 
 | 811 |     String8 result; | 
 | 812 |  | 
 | 813 |     bool locked = AudioFlinger::dumpTryLock(mLock); | 
 | 814 |     if (!locked) { | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 815 |         dprintf(fd, "thread %p may be deadlocked\n", this); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 816 |     } | 
 | 817 |  | 
| Glenn Kasten | 0b89bc0 | 2015-03-05 16:37:47 -0800 | [diff] [blame] | 818 |     dprintf(fd, "  Thread name: %s\n", mThreadName); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 819 |     dprintf(fd, "  I/O handle: %d\n", mId); | 
 | 820 |     dprintf(fd, "  TID: %d\n", getTid()); | 
 | 821 |     dprintf(fd, "  Standby: %s\n", mStandby ? "yes" : "no"); | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 822 |     dprintf(fd, "  Sample rate: %u Hz\n", mSampleRate); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 823 |     dprintf(fd, "  HAL frame count: %zu\n", mFrameCount); | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 824 |     dprintf(fd, "  HAL format: 0x%x (%s)\n", mHALFormat, formatToString(mHALFormat)); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 825 |     dprintf(fd, "  HAL buffer size: %u bytes\n", mBufferSize); | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 826 |     dprintf(fd, "  Channel count: %u\n", mChannelCount); | 
 | 827 |     dprintf(fd, "  Channel mask: 0x%08x (%s)\n", mChannelMask, | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 828 |             channelMaskToString(mChannelMask, mType != RECORD).string()); | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 829 |     dprintf(fd, "  Format: 0x%x (%s)\n", mFormat, formatToString(mFormat)); | 
 | 830 |     dprintf(fd, "  Frame size: %zu bytes\n", mFrameSize); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 831 |     dprintf(fd, "  Pending config events:"); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 832 |     size_t numConfig = mConfigEvents.size(); | 
 | 833 |     if (numConfig) { | 
 | 834 |         for (size_t i = 0; i < numConfig; i++) { | 
 | 835 |             mConfigEvents[i]->dump(buffer, SIZE); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 836 |             dprintf(fd, "\n    %s", buffer); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 837 |         } | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 838 |         dprintf(fd, "\n"); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 839 |     } else { | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 840 |         dprintf(fd, " none\n"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 841 |     } | 
| Glenn Kasten | 0b89bc0 | 2015-03-05 16:37:47 -0800 | [diff] [blame] | 842 |     dprintf(fd, "  Output device: %#x (%s)\n", mOutDevice, devicesToString(mOutDevice).string()); | 
 | 843 |     dprintf(fd, "  Input device: %#x (%s)\n", mInDevice, devicesToString(mInDevice).string()); | 
 | 844 |     dprintf(fd, "  Audio source: %d (%s)\n", mAudioSource, sourceToString(mAudioSource)); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 845 |  | 
 | 846 |     if (locked) { | 
 | 847 |         mLock.unlock(); | 
 | 848 |     } | 
 | 849 | } | 
 | 850 |  | 
 | 851 | void AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args) | 
 | 852 | { | 
 | 853 |     const size_t SIZE = 256; | 
 | 854 |     char buffer[SIZE]; | 
 | 855 |     String8 result; | 
 | 856 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 857 |     size_t numEffectChains = mEffectChains.size(); | 
| Narayan Kamath | 1d6fa7a | 2014-02-11 13:47:53 +0000 | [diff] [blame] | 858 |     snprintf(buffer, SIZE, "  %zu Effect Chains\n", numEffectChains); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 859 |     write(fd, buffer, strlen(buffer)); | 
 | 860 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 861 |     for (size_t i = 0; i < numEffectChains; ++i) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 862 |         sp<EffectChain> chain = mEffectChains[i]; | 
 | 863 |         if (chain != 0) { | 
 | 864 |             chain->dump(fd, args); | 
 | 865 |         } | 
 | 866 |     } | 
 | 867 | } | 
 | 868 |  | 
| Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 869 | void AudioFlinger::ThreadBase::acquireWakeLock(int uid) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 870 | { | 
 | 871 |     Mutex::Autolock _l(mLock); | 
| Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 872 |     acquireWakeLock_l(uid); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 873 | } | 
 | 874 |  | 
| Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 875 | String16 AudioFlinger::ThreadBase::getWakeLockTag() | 
 | 876 | { | 
 | 877 |     switch (mType) { | 
| Glenn Kasten | bcb1486 | 2015-03-05 17:11:21 -0800 | [diff] [blame] | 878 |     case MIXER: | 
 | 879 |         return String16("AudioMix"); | 
 | 880 |     case DIRECT: | 
 | 881 |         return String16("AudioDirectOut"); | 
 | 882 |     case DUPLICATING: | 
 | 883 |         return String16("AudioDup"); | 
 | 884 |     case RECORD: | 
 | 885 |         return String16("AudioIn"); | 
 | 886 |     case OFFLOAD: | 
 | 887 |         return String16("AudioOffload"); | 
 | 888 |     default: | 
 | 889 |         ALOG_ASSERT(false); | 
 | 890 |         return String16("AudioUnknown"); | 
| Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 891 |     } | 
 | 892 | } | 
 | 893 |  | 
| Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 894 | void AudioFlinger::ThreadBase::acquireWakeLock_l(int uid) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 895 | { | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 896 |     getPowerManager_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 897 |     if (mPowerManager != 0) { | 
 | 898 |         sp<IBinder> binder = new BBinder(); | 
| Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 899 |         status_t status; | 
 | 900 |         if (uid >= 0) { | 
| Eric Laurent | 547789d | 2013-10-04 11:46:55 -0700 | [diff] [blame] | 901 |             status = mPowerManager->acquireWakeLockWithUid(POWERMANAGER_PARTIAL_WAKE_LOCK, | 
| Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 902 |                     binder, | 
| Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 903 |                     getWakeLockTag(), | 
| Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 904 |                     String16("media"), | 
| Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 905 |                     uid, | 
 | 906 |                     true /* FIXME force oneway contrary to .aidl */); | 
| Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 907 |         } else { | 
| Eric Laurent | 547789d | 2013-10-04 11:46:55 -0700 | [diff] [blame] | 908 |             status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK, | 
| Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 909 |                     binder, | 
| Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 910 |                     getWakeLockTag(), | 
| Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 911 |                     String16("media"), | 
 | 912 |                     true /* FIXME force oneway contrary to .aidl */); | 
| Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 913 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 914 |         if (status == NO_ERROR) { | 
 | 915 |             mWakeLockToken = binder; | 
 | 916 |         } | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 917 |         ALOGV("acquireWakeLock_l() %s status %d", mThreadName, status); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 918 |     } | 
 | 919 | } | 
 | 920 |  | 
 | 921 | void AudioFlinger::ThreadBase::releaseWakeLock() | 
 | 922 | { | 
 | 923 |     Mutex::Autolock _l(mLock); | 
 | 924 |     releaseWakeLock_l(); | 
 | 925 | } | 
 | 926 |  | 
 | 927 | void AudioFlinger::ThreadBase::releaseWakeLock_l() | 
 | 928 | { | 
 | 929 |     if (mWakeLockToken != 0) { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 930 |         ALOGV("releaseWakeLock_l() %s", mThreadName); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 931 |         if (mPowerManager != 0) { | 
| Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 932 |             mPowerManager->releaseWakeLock(mWakeLockToken, 0, | 
 | 933 |                     true /* FIXME force oneway contrary to .aidl */); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 934 |         } | 
 | 935 |         mWakeLockToken.clear(); | 
 | 936 |     } | 
 | 937 | } | 
 | 938 |  | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 939 | void AudioFlinger::ThreadBase::updateWakeLockUids(const SortedVector<int> &uids) { | 
 | 940 |     Mutex::Autolock _l(mLock); | 
 | 941 |     updateWakeLockUids_l(uids); | 
 | 942 | } | 
 | 943 |  | 
 | 944 | void AudioFlinger::ThreadBase::getPowerManager_l() { | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 945 |     if (mSystemReady && mPowerManager == 0) { | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 946 |         // use checkService() to avoid blocking if power service is not up yet | 
 | 947 |         sp<IBinder> binder = | 
 | 948 |             defaultServiceManager()->checkService(String16("power")); | 
 | 949 |         if (binder == 0) { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 950 |             ALOGW("Thread %s cannot connect to the power manager service", mThreadName); | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 951 |         } else { | 
 | 952 |             mPowerManager = interface_cast<IPowerManager>(binder); | 
 | 953 |             binder->linkToDeath(mDeathRecipient); | 
 | 954 |         } | 
 | 955 |     } | 
 | 956 | } | 
 | 957 |  | 
 | 958 | void AudioFlinger::ThreadBase::updateWakeLockUids_l(const SortedVector<int> &uids) { | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 959 |     getPowerManager_l(); | 
 | 960 |     if (mWakeLockToken == NULL) { | 
 | 961 |         ALOGE("no wake lock to update!"); | 
 | 962 |         return; | 
 | 963 |     } | 
 | 964 |     if (mPowerManager != 0) { | 
 | 965 |         sp<IBinder> binder = new BBinder(); | 
 | 966 |         status_t status; | 
| Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 967 |         status = mPowerManager->updateWakeLockUids(mWakeLockToken, uids.size(), uids.array(), | 
 | 968 |                     true /* FIXME force oneway contrary to .aidl */); | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 969 |         ALOGV("acquireWakeLock_l() %s status %d", mThreadName, status); | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 970 |     } | 
 | 971 | } | 
 | 972 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 973 | void AudioFlinger::ThreadBase::clearPowerManager() | 
 | 974 | { | 
 | 975 |     Mutex::Autolock _l(mLock); | 
 | 976 |     releaseWakeLock_l(); | 
 | 977 |     mPowerManager.clear(); | 
 | 978 | } | 
 | 979 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 980 | void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 981 | { | 
 | 982 |     sp<ThreadBase> thread = mThread.promote(); | 
 | 983 |     if (thread != 0) { | 
 | 984 |         thread->clearPowerManager(); | 
 | 985 |     } | 
 | 986 |     ALOGW("power manager service died !!!"); | 
 | 987 | } | 
 | 988 |  | 
 | 989 | void AudioFlinger::ThreadBase::setEffectSuspended( | 
 | 990 |         const effect_uuid_t *type, bool suspend, int sessionId) | 
 | 991 | { | 
 | 992 |     Mutex::Autolock _l(mLock); | 
 | 993 |     setEffectSuspended_l(type, suspend, sessionId); | 
 | 994 | } | 
 | 995 |  | 
 | 996 | void AudioFlinger::ThreadBase::setEffectSuspended_l( | 
 | 997 |         const effect_uuid_t *type, bool suspend, int sessionId) | 
 | 998 | { | 
 | 999 |     sp<EffectChain> chain = getEffectChain_l(sessionId); | 
 | 1000 |     if (chain != 0) { | 
 | 1001 |         if (type != NULL) { | 
 | 1002 |             chain->setEffectSuspended_l(type, suspend); | 
 | 1003 |         } else { | 
 | 1004 |             chain->setEffectSuspendedAll_l(suspend); | 
 | 1005 |         } | 
 | 1006 |     } | 
 | 1007 |  | 
 | 1008 |     updateSuspendedSessions_l(type, suspend, sessionId); | 
 | 1009 | } | 
 | 1010 |  | 
 | 1011 | void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain) | 
 | 1012 | { | 
 | 1013 |     ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId()); | 
 | 1014 |     if (index < 0) { | 
 | 1015 |         return; | 
 | 1016 |     } | 
 | 1017 |  | 
 | 1018 |     const KeyedVector <int, sp<SuspendedSessionDesc> >& sessionEffects = | 
 | 1019 |             mSuspendedSessions.valueAt(index); | 
 | 1020 |  | 
 | 1021 |     for (size_t i = 0; i < sessionEffects.size(); i++) { | 
 | 1022 |         sp<SuspendedSessionDesc> desc = sessionEffects.valueAt(i); | 
 | 1023 |         for (int j = 0; j < desc->mRefCount; j++) { | 
 | 1024 |             if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) { | 
 | 1025 |                 chain->setEffectSuspendedAll_l(true); | 
 | 1026 |             } else { | 
 | 1027 |                 ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", | 
 | 1028 |                     desc->mType.timeLow); | 
 | 1029 |                 chain->setEffectSuspended_l(&desc->mType, true); | 
 | 1030 |             } | 
 | 1031 |         } | 
 | 1032 |     } | 
 | 1033 | } | 
 | 1034 |  | 
 | 1035 | void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type, | 
 | 1036 |                                                          bool suspend, | 
 | 1037 |                                                          int sessionId) | 
 | 1038 | { | 
 | 1039 |     ssize_t index = mSuspendedSessions.indexOfKey(sessionId); | 
 | 1040 |  | 
 | 1041 |     KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; | 
 | 1042 |  | 
 | 1043 |     if (suspend) { | 
 | 1044 |         if (index >= 0) { | 
 | 1045 |             sessionEffects = mSuspendedSessions.valueAt(index); | 
 | 1046 |         } else { | 
 | 1047 |             mSuspendedSessions.add(sessionId, sessionEffects); | 
 | 1048 |         } | 
 | 1049 |     } else { | 
 | 1050 |         if (index < 0) { | 
 | 1051 |             return; | 
 | 1052 |         } | 
 | 1053 |         sessionEffects = mSuspendedSessions.valueAt(index); | 
 | 1054 |     } | 
 | 1055 |  | 
 | 1056 |  | 
 | 1057 |     int key = EffectChain::kKeyForSuspendAll; | 
 | 1058 |     if (type != NULL) { | 
 | 1059 |         key = type->timeLow; | 
 | 1060 |     } | 
 | 1061 |     index = sessionEffects.indexOfKey(key); | 
 | 1062 |  | 
 | 1063 |     sp<SuspendedSessionDesc> desc; | 
 | 1064 |     if (suspend) { | 
 | 1065 |         if (index >= 0) { | 
 | 1066 |             desc = sessionEffects.valueAt(index); | 
 | 1067 |         } else { | 
 | 1068 |             desc = new SuspendedSessionDesc(); | 
 | 1069 |             if (type != NULL) { | 
 | 1070 |                 desc->mType = *type; | 
 | 1071 |             } | 
 | 1072 |             sessionEffects.add(key, desc); | 
 | 1073 |             ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); | 
 | 1074 |         } | 
 | 1075 |         desc->mRefCount++; | 
 | 1076 |     } else { | 
 | 1077 |         if (index < 0) { | 
 | 1078 |             return; | 
 | 1079 |         } | 
 | 1080 |         desc = sessionEffects.valueAt(index); | 
 | 1081 |         if (--desc->mRefCount == 0) { | 
 | 1082 |             ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); | 
 | 1083 |             sessionEffects.removeItemsAt(index); | 
 | 1084 |             if (sessionEffects.isEmpty()) { | 
 | 1085 |                 ALOGV("updateSuspendedSessions_l() restore removing session %d", | 
 | 1086 |                                  sessionId); | 
 | 1087 |                 mSuspendedSessions.removeItem(sessionId); | 
 | 1088 |             } | 
 | 1089 |         } | 
 | 1090 |     } | 
 | 1091 |     if (!sessionEffects.isEmpty()) { | 
 | 1092 |         mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); | 
 | 1093 |     } | 
 | 1094 | } | 
 | 1095 |  | 
 | 1096 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, | 
 | 1097 |                                                             bool enabled, | 
 | 1098 |                                                             int sessionId) | 
 | 1099 | { | 
 | 1100 |     Mutex::Autolock _l(mLock); | 
 | 1101 |     checkSuspendOnEffectEnabled_l(effect, enabled, sessionId); | 
 | 1102 | } | 
 | 1103 |  | 
 | 1104 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect, | 
 | 1105 |                                                             bool enabled, | 
 | 1106 |                                                             int sessionId) | 
 | 1107 | { | 
 | 1108 |     if (mType != RECORD) { | 
 | 1109 |         // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on | 
 | 1110 |         // another session. This gives the priority to well behaved effect control panels | 
 | 1111 |         // and applications not using global effects. | 
 | 1112 |         // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect | 
 | 1113 |         // global effects | 
 | 1114 |         if ((sessionId != AUDIO_SESSION_OUTPUT_MIX) && (sessionId != AUDIO_SESSION_OUTPUT_STAGE)) { | 
 | 1115 |             setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); | 
 | 1116 |         } | 
 | 1117 |     } | 
 | 1118 |  | 
 | 1119 |     sp<EffectChain> chain = getEffectChain_l(sessionId); | 
 | 1120 |     if (chain != 0) { | 
 | 1121 |         chain->checkSuspendOnEffectEnabled(effect, enabled); | 
 | 1122 |     } | 
 | 1123 | } | 
 | 1124 |  | 
 | 1125 | // ThreadBase::createEffect_l() must be called with AudioFlinger::mLock held | 
 | 1126 | sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l( | 
 | 1127 |         const sp<AudioFlinger::Client>& client, | 
 | 1128 |         const sp<IEffectClient>& effectClient, | 
 | 1129 |         int32_t priority, | 
 | 1130 |         int sessionId, | 
 | 1131 |         effect_descriptor_t *desc, | 
 | 1132 |         int *enabled, | 
| Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1133 |         status_t *status) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1134 | { | 
 | 1135 |     sp<EffectModule> effect; | 
 | 1136 |     sp<EffectHandle> handle; | 
 | 1137 |     status_t lStatus; | 
 | 1138 |     sp<EffectChain> chain; | 
 | 1139 |     bool chainCreated = false; | 
 | 1140 |     bool effectCreated = false; | 
 | 1141 |     bool effectRegistered = false; | 
 | 1142 |  | 
 | 1143 |     lStatus = initCheck(); | 
 | 1144 |     if (lStatus != NO_ERROR) { | 
 | 1145 |         ALOGW("createEffect_l() Audio driver not initialized."); | 
 | 1146 |         goto Exit; | 
 | 1147 |     } | 
 | 1148 |  | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1149 |     // Reject any effect on Direct output threads for now, since the format of | 
 | 1150 |     // mSinkBuffer is not guaranteed to be compatible with effect processing (PCM 16 stereo). | 
 | 1151 |     if (mType == DIRECT) { | 
 | 1152 |         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] | 1153 |                 desc->name, mThreadName); | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1154 |         lStatus = BAD_VALUE; | 
 | 1155 |         goto Exit; | 
 | 1156 |     } | 
 | 1157 |  | 
| Andy Hung | 389cfdb | 2014-08-07 17:49:53 -0700 | [diff] [blame] | 1158 |     // Reject any effect on mixer or duplicating multichannel sinks. | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1159 |     // TODO: fix both format and multichannel issues with effects. | 
| Andy Hung | 389cfdb | 2014-08-07 17:49:53 -0700 | [diff] [blame] | 1160 |     if ((mType == MIXER || mType == DUPLICATING) && mChannelCount != FCC_2) { | 
 | 1161 |         ALOGW("createEffect_l() Cannot add effect %s for multichannel(%d) %s threads", | 
 | 1162 |                 desc->name, mChannelCount, mType == MIXER ? "MIXER" : "DUPLICATING"); | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1163 |         lStatus = BAD_VALUE; | 
 | 1164 |         goto Exit; | 
 | 1165 |     } | 
 | 1166 |  | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1167 |     // Allow global effects only on offloaded and mixer threads | 
 | 1168 |     if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { | 
 | 1169 |         switch (mType) { | 
 | 1170 |         case MIXER: | 
 | 1171 |         case OFFLOAD: | 
 | 1172 |             break; | 
 | 1173 |         case DIRECT: | 
 | 1174 |         case DUPLICATING: | 
 | 1175 |         case RECORD: | 
 | 1176 |         default: | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1177 |             ALOGW("createEffect_l() Cannot add global effect %s on thread %s", | 
 | 1178 |                     desc->name, mThreadName); | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1179 |             lStatus = BAD_VALUE; | 
 | 1180 |             goto Exit; | 
 | 1181 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1182 |     } | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1183 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1184 |     // Only Pre processor effects are allowed on input threads and only on input threads | 
 | 1185 |     if ((mType == RECORD) != ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) { | 
 | 1186 |         ALOGW("createEffect_l() effect %s (flags %08x) created on wrong thread type %d", | 
 | 1187 |                 desc->name, desc->flags, mType); | 
 | 1188 |         lStatus = BAD_VALUE; | 
 | 1189 |         goto Exit; | 
 | 1190 |     } | 
 | 1191 |  | 
 | 1192 |     ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId); | 
 | 1193 |  | 
 | 1194 |     { // scope for mLock | 
 | 1195 |         Mutex::Autolock _l(mLock); | 
 | 1196 |  | 
 | 1197 |         // check for existing effect chain with the requested audio session | 
 | 1198 |         chain = getEffectChain_l(sessionId); | 
 | 1199 |         if (chain == 0) { | 
 | 1200 |             // create a new chain for this session | 
 | 1201 |             ALOGV("createEffect_l() new effect chain for session %d", sessionId); | 
 | 1202 |             chain = new EffectChain(this, sessionId); | 
 | 1203 |             addEffectChain_l(chain); | 
 | 1204 |             chain->setStrategy(getStrategyForSession_l(sessionId)); | 
 | 1205 |             chainCreated = true; | 
 | 1206 |         } else { | 
 | 1207 |             effect = chain->getEffectFromDesc_l(desc); | 
 | 1208 |         } | 
 | 1209 |  | 
 | 1210 |         ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get()); | 
 | 1211 |  | 
 | 1212 |         if (effect == 0) { | 
 | 1213 |             int id = mAudioFlinger->nextUniqueId(); | 
 | 1214 |             // Check CPU and memory usage | 
 | 1215 |             lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id); | 
 | 1216 |             if (lStatus != NO_ERROR) { | 
 | 1217 |                 goto Exit; | 
 | 1218 |             } | 
 | 1219 |             effectRegistered = true; | 
 | 1220 |             // create a new effect module if none present in the chain | 
 | 1221 |             effect = new EffectModule(this, chain, desc, id, sessionId); | 
 | 1222 |             lStatus = effect->status(); | 
 | 1223 |             if (lStatus != NO_ERROR) { | 
 | 1224 |                 goto Exit; | 
 | 1225 |             } | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1226 |             effect->setOffloaded(mType == OFFLOAD, mId); | 
 | 1227 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1228 |             lStatus = chain->addEffect_l(effect); | 
 | 1229 |             if (lStatus != NO_ERROR) { | 
 | 1230 |                 goto Exit; | 
 | 1231 |             } | 
 | 1232 |             effectCreated = true; | 
 | 1233 |  | 
 | 1234 |             effect->setDevice(mOutDevice); | 
 | 1235 |             effect->setDevice(mInDevice); | 
 | 1236 |             effect->setMode(mAudioFlinger->getMode()); | 
 | 1237 |             effect->setAudioSource(mAudioSource); | 
 | 1238 |         } | 
 | 1239 |         // create effect handle and connect it to effect module | 
 | 1240 |         handle = new EffectHandle(effect, client, effectClient, priority); | 
| Glenn Kasten | e75da40 | 2013-11-20 13:54:52 -0800 | [diff] [blame] | 1241 |         lStatus = handle->initCheck(); | 
 | 1242 |         if (lStatus == OK) { | 
 | 1243 |             lStatus = effect->addHandle(handle.get()); | 
 | 1244 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1245 |         if (enabled != NULL) { | 
 | 1246 |             *enabled = (int)effect->isEnabled(); | 
 | 1247 |         } | 
 | 1248 |     } | 
 | 1249 |  | 
 | 1250 | Exit: | 
 | 1251 |     if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { | 
 | 1252 |         Mutex::Autolock _l(mLock); | 
 | 1253 |         if (effectCreated) { | 
 | 1254 |             chain->removeEffect_l(effect); | 
 | 1255 |         } | 
 | 1256 |         if (effectRegistered) { | 
 | 1257 |             AudioSystem::unregisterEffect(effect->id()); | 
 | 1258 |         } | 
 | 1259 |         if (chainCreated) { | 
 | 1260 |             removeEffectChain_l(chain); | 
 | 1261 |         } | 
 | 1262 |         handle.clear(); | 
 | 1263 |     } | 
 | 1264 |  | 
| Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1265 |     *status = lStatus; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1266 |     return handle; | 
 | 1267 | } | 
 | 1268 |  | 
 | 1269 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect(int sessionId, int effectId) | 
 | 1270 | { | 
 | 1271 |     Mutex::Autolock _l(mLock); | 
 | 1272 |     return getEffect_l(sessionId, effectId); | 
 | 1273 | } | 
 | 1274 |  | 
 | 1275 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(int sessionId, int effectId) | 
 | 1276 | { | 
 | 1277 |     sp<EffectChain> chain = getEffectChain_l(sessionId); | 
 | 1278 |     return chain != 0 ? chain->getEffectFromId_l(effectId) : 0; | 
 | 1279 | } | 
 | 1280 |  | 
 | 1281 | // PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and | 
 | 1282 | // PlaybackThread::mLock held | 
 | 1283 | status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect) | 
 | 1284 | { | 
 | 1285 |     // check for existing effect chain with the requested audio session | 
 | 1286 |     int sessionId = effect->sessionId(); | 
 | 1287 |     sp<EffectChain> chain = getEffectChain_l(sessionId); | 
 | 1288 |     bool chainCreated = false; | 
 | 1289 |  | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1290 |     ALOGD_IF((mType == OFFLOAD) && !effect->isOffloadable(), | 
 | 1291 |              "addEffect_l() on offloaded thread %p: effect %s does not support offload flags %x", | 
 | 1292 |                     this, effect->desc().name, effect->desc().flags); | 
 | 1293 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1294 |     if (chain == 0) { | 
 | 1295 |         // create a new chain for this session | 
 | 1296 |         ALOGV("addEffect_l() new effect chain for session %d", sessionId); | 
 | 1297 |         chain = new EffectChain(this, sessionId); | 
 | 1298 |         addEffectChain_l(chain); | 
 | 1299 |         chain->setStrategy(getStrategyForSession_l(sessionId)); | 
 | 1300 |         chainCreated = true; | 
 | 1301 |     } | 
 | 1302 |     ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get()); | 
 | 1303 |  | 
 | 1304 |     if (chain->getEffectFromId_l(effect->id()) != 0) { | 
 | 1305 |         ALOGW("addEffect_l() %p effect %s already present in chain %p", | 
 | 1306 |                 this, effect->desc().name, chain.get()); | 
 | 1307 |         return BAD_VALUE; | 
 | 1308 |     } | 
 | 1309 |  | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1310 |     effect->setOffloaded(mType == OFFLOAD, mId); | 
 | 1311 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1312 |     status_t status = chain->addEffect_l(effect); | 
 | 1313 |     if (status != NO_ERROR) { | 
 | 1314 |         if (chainCreated) { | 
 | 1315 |             removeEffectChain_l(chain); | 
 | 1316 |         } | 
 | 1317 |         return status; | 
 | 1318 |     } | 
 | 1319 |  | 
 | 1320 |     effect->setDevice(mOutDevice); | 
 | 1321 |     effect->setDevice(mInDevice); | 
 | 1322 |     effect->setMode(mAudioFlinger->getMode()); | 
 | 1323 |     effect->setAudioSource(mAudioSource); | 
 | 1324 |     return NO_ERROR; | 
 | 1325 | } | 
 | 1326 |  | 
 | 1327 | void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) { | 
 | 1328 |  | 
 | 1329 |     ALOGV("removeEffect_l() %p effect %p", this, effect.get()); | 
 | 1330 |     effect_descriptor_t desc = effect->desc(); | 
 | 1331 |     if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
 | 1332 |         detachAuxEffect_l(effect->id()); | 
 | 1333 |     } | 
 | 1334 |  | 
 | 1335 |     sp<EffectChain> chain = effect->chain().promote(); | 
 | 1336 |     if (chain != 0) { | 
 | 1337 |         // remove effect chain if removing last effect | 
 | 1338 |         if (chain->removeEffect_l(effect) == 0) { | 
 | 1339 |             removeEffectChain_l(chain); | 
 | 1340 |         } | 
 | 1341 |     } else { | 
 | 1342 |         ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); | 
 | 1343 |     } | 
 | 1344 | } | 
 | 1345 |  | 
 | 1346 | void AudioFlinger::ThreadBase::lockEffectChains_l( | 
 | 1347 |         Vector< sp<AudioFlinger::EffectChain> >& effectChains) | 
 | 1348 | { | 
 | 1349 |     effectChains = mEffectChains; | 
 | 1350 |     for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 1351 |         mEffectChains[i]->lock(); | 
 | 1352 |     } | 
 | 1353 | } | 
 | 1354 |  | 
 | 1355 | void AudioFlinger::ThreadBase::unlockEffectChains( | 
 | 1356 |         const Vector< sp<AudioFlinger::EffectChain> >& effectChains) | 
 | 1357 | { | 
 | 1358 |     for (size_t i = 0; i < effectChains.size(); i++) { | 
 | 1359 |         effectChains[i]->unlock(); | 
 | 1360 |     } | 
 | 1361 | } | 
 | 1362 |  | 
 | 1363 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(int sessionId) | 
 | 1364 | { | 
 | 1365 |     Mutex::Autolock _l(mLock); | 
 | 1366 |     return getEffectChain_l(sessionId); | 
 | 1367 | } | 
 | 1368 |  | 
 | 1369 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(int sessionId) const | 
 | 1370 | { | 
 | 1371 |     size_t size = mEffectChains.size(); | 
 | 1372 |     for (size_t i = 0; i < size; i++) { | 
 | 1373 |         if (mEffectChains[i]->sessionId() == sessionId) { | 
 | 1374 |             return mEffectChains[i]; | 
 | 1375 |         } | 
 | 1376 |     } | 
 | 1377 |     return 0; | 
 | 1378 | } | 
 | 1379 |  | 
 | 1380 | void AudioFlinger::ThreadBase::setMode(audio_mode_t mode) | 
 | 1381 | { | 
 | 1382 |     Mutex::Autolock _l(mLock); | 
 | 1383 |     size_t size = mEffectChains.size(); | 
 | 1384 |     for (size_t i = 0; i < size; i++) { | 
 | 1385 |         mEffectChains[i]->setMode_l(mode); | 
 | 1386 |     } | 
 | 1387 | } | 
 | 1388 |  | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1389 | void AudioFlinger::ThreadBase::getAudioPortConfig(struct audio_port_config *config) | 
 | 1390 | { | 
 | 1391 |     config->type = AUDIO_PORT_TYPE_MIX; | 
 | 1392 |     config->ext.mix.handle = mId; | 
 | 1393 |     config->sample_rate = mSampleRate; | 
 | 1394 |     config->format = mFormat; | 
 | 1395 |     config->channel_mask = mChannelMask; | 
 | 1396 |     config->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK| | 
 | 1397 |                             AUDIO_PORT_CONFIG_FORMAT; | 
 | 1398 | } | 
 | 1399 |  | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1400 | void AudioFlinger::ThreadBase::systemReady() | 
 | 1401 | { | 
 | 1402 |     Mutex::Autolock _l(mLock); | 
 | 1403 |     if (mSystemReady) { | 
 | 1404 |         return; | 
 | 1405 |     } | 
 | 1406 |     mSystemReady = true; | 
 | 1407 |  | 
 | 1408 |     for (size_t i = 0; i < mPendingConfigEvents.size(); i++) { | 
 | 1409 |         sendConfigEvent_l(mPendingConfigEvents.editItemAt(i)); | 
 | 1410 |     } | 
 | 1411 |     mPendingConfigEvents.clear(); | 
 | 1412 | } | 
 | 1413 |  | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1414 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1415 | // ---------------------------------------------------------------------------- | 
 | 1416 | //      Playback | 
 | 1417 | // ---------------------------------------------------------------------------- | 
 | 1418 |  | 
 | 1419 | AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, | 
 | 1420 |                                              AudioStreamOut* output, | 
 | 1421 |                                              audio_io_handle_t id, | 
 | 1422 |                                              audio_devices_t device, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1423 |                                              type_t type, | 
 | 1424 |                                              bool systemReady) | 
 | 1425 |     :   ThreadBase(audioFlinger, id, device, AUDIO_DEVICE_NONE, type, systemReady), | 
| Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 1426 |         mNormalFrameCount(0), mSinkBuffer(NULL), | 
| Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1427 |         mMixerBufferEnabled(AudioFlinger::kEnableExtendedPrecision), | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1428 |         mMixerBuffer(NULL), | 
 | 1429 |         mMixerBufferSize(0), | 
 | 1430 |         mMixerBufferFormat(AUDIO_FORMAT_INVALID), | 
 | 1431 |         mMixerBufferValid(false), | 
| Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1432 |         mEffectBufferEnabled(AudioFlinger::kEnableExtendedPrecision), | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1433 |         mEffectBuffer(NULL), | 
 | 1434 |         mEffectBufferSize(0), | 
 | 1435 |         mEffectBufferFormat(AUDIO_FORMAT_INVALID), | 
 | 1436 |         mEffectBufferValid(false), | 
| Glenn Kasten | c1fac19 | 2013-08-06 07:41:36 -0700 | [diff] [blame] | 1437 |         mSuspended(0), mBytesWritten(0), | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1438 |         mActiveTracksGeneration(0), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1439 |         // mStreamTypes[] initialized in constructor body | 
 | 1440 |         mOutput(output), | 
 | 1441 |         mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false), | 
 | 1442 |         mMixerStatus(MIXER_IDLE), | 
 | 1443 |         mMixerStatusIgnoringFastTracks(MIXER_IDLE), | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 1444 |         mStandbyDelayNs(AudioFlinger::mStandbyTimeInNsecs), | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1445 |         mBytesRemaining(0), | 
 | 1446 |         mCurrentWriteLength(0), | 
 | 1447 |         mUseAsyncWrite(false), | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1448 |         mWriteAckSequence(0), | 
 | 1449 |         mDrainSequence(0), | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1450 |         mSignalPending(false), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1451 |         mScreenState(AudioFlinger::mScreenState), | 
 | 1452 |         // index 0 is reserved for normal mixer's submix | 
| Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 1453 |         mFastTrackAvailMask(((1 << FastMixerState::kMaxFastTracks) - 1) & ~1), | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 1454 |         mHwSupportsPause(false), mHwPaused(false), mFlushPending(false), | 
| Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 1455 |         // mLatchD, mLatchQ, | 
 | 1456 |         mLatchDValid(false), mLatchQValid(false) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1457 | { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1458 |     snprintf(mThreadName, kThreadNameLength, "AudioOut_%X", id); | 
 | 1459 |     mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1460 |  | 
 | 1461 |     // Assumes constructor is called by AudioFlinger with it's mLock held, but | 
 | 1462 |     // it would be safer to explicitly pass initial masterVolume/masterMute as | 
 | 1463 |     // parameter. | 
 | 1464 |     // | 
 | 1465 |     // If the HAL we are using has support for master volume or master mute, | 
 | 1466 |     // then do not attenuate or mute during mixing (just leave the volume at 1.0 | 
 | 1467 |     // and the mute set to false). | 
 | 1468 |     mMasterVolume = audioFlinger->masterVolume_l(); | 
 | 1469 |     mMasterMute = audioFlinger->masterMute_l(); | 
 | 1470 |     if (mOutput && mOutput->audioHwDev) { | 
 | 1471 |         if (mOutput->audioHwDev->canSetMasterVolume()) { | 
 | 1472 |             mMasterVolume = 1.0; | 
 | 1473 |         } | 
 | 1474 |  | 
 | 1475 |         if (mOutput->audioHwDev->canSetMasterMute()) { | 
 | 1476 |             mMasterMute = false; | 
 | 1477 |         } | 
 | 1478 |     } | 
 | 1479 |  | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 1480 |     readOutputParameters_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1481 |  | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1482 |     // ++ operator does not compile | 
| Glenn Kasten | 66e4635 | 2014-01-16 17:44:23 -0800 | [diff] [blame] | 1483 |     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] | 1484 |             stream = (audio_stream_type_t) (stream + 1)) { | 
 | 1485 |         mStreamTypes[stream].volume = mAudioFlinger->streamVolume_l(stream); | 
 | 1486 |         mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream); | 
 | 1487 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1488 | } | 
 | 1489 |  | 
 | 1490 | AudioFlinger::PlaybackThread::~PlaybackThread() | 
 | 1491 | { | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 1492 |     mAudioFlinger->unregisterWriter(mNBLogWriter); | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 1493 |     free(mSinkBuffer); | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1494 |     free(mMixerBuffer); | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1495 |     free(mEffectBuffer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1496 | } | 
 | 1497 |  | 
 | 1498 | void AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) | 
 | 1499 | { | 
 | 1500 |     dumpInternals(fd, args); | 
 | 1501 |     dumpTracks(fd, args); | 
 | 1502 |     dumpEffectChains(fd, args); | 
 | 1503 | } | 
 | 1504 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 1505 | void AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1506 | { | 
 | 1507 |     const size_t SIZE = 256; | 
 | 1508 |     char buffer[SIZE]; | 
 | 1509 |     String8 result; | 
 | 1510 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1511 |     result.appendFormat("  Stream volumes in dB: "); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1512 |     for (int i = 0; i < AUDIO_STREAM_CNT; ++i) { | 
 | 1513 |         const stream_type_t *st = &mStreamTypes[i]; | 
 | 1514 |         if (i > 0) { | 
 | 1515 |             result.appendFormat(", "); | 
 | 1516 |         } | 
 | 1517 |         result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume)); | 
 | 1518 |         if (st->mute) { | 
 | 1519 |             result.append("M"); | 
 | 1520 |         } | 
 | 1521 |     } | 
 | 1522 |     result.append("\n"); | 
 | 1523 |     write(fd, result.string(), result.length()); | 
 | 1524 |     result.clear(); | 
 | 1525 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1526 |     // These values are "raw"; they will wrap around.  See prepareTracks_l() for a better way. | 
 | 1527 |     FastTrackUnderruns underruns = getFastTrackUnderruns(0); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1528 |     dprintf(fd, "  Normal mixer raw underrun counters: partial=%u empty=%u\n", | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1529 |             underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1530 |  | 
 | 1531 |     size_t numtracks = mTracks.size(); | 
 | 1532 |     size_t numactive = mActiveTracks.size(); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1533 |     dprintf(fd, "  %d Tracks", numtracks); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1534 |     size_t numactiveseen = 0; | 
 | 1535 |     if (numtracks) { | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1536 |         dprintf(fd, " of which %d are active\n", numactive); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1537 |         Track::appendDumpHeader(result); | 
 | 1538 |         for (size_t i = 0; i < numtracks; ++i) { | 
 | 1539 |             sp<Track> track = mTracks[i]; | 
 | 1540 |             if (track != 0) { | 
 | 1541 |                 bool active = mActiveTracks.indexOf(track) >= 0; | 
 | 1542 |                 if (active) { | 
 | 1543 |                     numactiveseen++; | 
 | 1544 |                 } | 
 | 1545 |                 track->dump(buffer, SIZE, active); | 
 | 1546 |                 result.append(buffer); | 
 | 1547 |             } | 
 | 1548 |         } | 
 | 1549 |     } else { | 
 | 1550 |         result.append("\n"); | 
 | 1551 |     } | 
 | 1552 |     if (numactiveseen != numactive) { | 
 | 1553 |         // some tracks in the active list were not in the tracks list | 
 | 1554 |         snprintf(buffer, SIZE, "  The following tracks are in the active list but" | 
 | 1555 |                 " not in the track list\n"); | 
 | 1556 |         result.append(buffer); | 
 | 1557 |         Track::appendDumpHeader(result); | 
 | 1558 |         for (size_t i = 0; i < numactive; ++i) { | 
 | 1559 |             sp<Track> track = mActiveTracks[i].promote(); | 
 | 1560 |             if (track != 0 && mTracks.indexOf(track) < 0) { | 
 | 1561 |                 track->dump(buffer, SIZE, true); | 
 | 1562 |                 result.append(buffer); | 
 | 1563 |             } | 
 | 1564 |         } | 
 | 1565 |     } | 
 | 1566 |  | 
 | 1567 |     write(fd, result.string(), result.size()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1568 | } | 
 | 1569 |  | 
 | 1570 | void AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) | 
 | 1571 | { | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 1572 |     dprintf(fd, "\nOutput thread %p type %d (%s):\n", this, type(), threadTypeToString(type())); | 
| Glenn Kasten | 44182c2 | 2015-03-05 17:12:23 -0800 | [diff] [blame] | 1573 |  | 
 | 1574 |     dumpBase(fd, args); | 
 | 1575 |  | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1576 |     dprintf(fd, "  Normal frame count: %zu\n", mNormalFrameCount); | 
 | 1577 |     dprintf(fd, "  Last write occurred (msecs): %llu\n", ns2ms(systemTime() - mLastWriteTime)); | 
 | 1578 |     dprintf(fd, "  Total writes: %d\n", mNumWrites); | 
 | 1579 |     dprintf(fd, "  Delayed writes: %d\n", mNumDelayedWrites); | 
 | 1580 |     dprintf(fd, "  Blocked in write: %s\n", mInWrite ? "yes" : "no"); | 
 | 1581 |     dprintf(fd, "  Suspend count: %d\n", mSuspended); | 
 | 1582 |     dprintf(fd, "  Sink buffer : %p\n", mSinkBuffer); | 
 | 1583 |     dprintf(fd, "  Mixer buffer: %p\n", mMixerBuffer); | 
 | 1584 |     dprintf(fd, "  Effect buffer: %p\n", mEffectBuffer); | 
 | 1585 |     dprintf(fd, "  Fast track availMask=%#x\n", mFastTrackAvailMask); | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 1586 |     AudioStreamOut *output = mOutput; | 
 | 1587 |     audio_output_flags_t flags = output != NULL ? output->flags : AUDIO_OUTPUT_FLAG_NONE; | 
 | 1588 |     String8 flagsAsString = outputFlagsToString(flags); | 
 | 1589 |     dprintf(fd, "  AudioStreamOut: %p flags %#x (%s)\n", output, flags, flagsAsString.string()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1590 | } | 
 | 1591 |  | 
 | 1592 | // Thread virtuals | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1593 |  | 
 | 1594 | void AudioFlinger::PlaybackThread::onFirstRef() | 
 | 1595 | { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1596 |     run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1597 | } | 
 | 1598 |  | 
 | 1599 | // ThreadBase virtuals | 
 | 1600 | void AudioFlinger::PlaybackThread::preExit() | 
 | 1601 | { | 
 | 1602 |     ALOGV("  preExit()"); | 
 | 1603 |     // FIXME this is using hard-coded strings but in the future, this functionality will be | 
 | 1604 |     //       converted to use audio HAL extensions required to support tunneling | 
 | 1605 |     mOutput->stream->common.set_parameters(&mOutput->stream->common, "exiting=1"); | 
 | 1606 | } | 
 | 1607 |  | 
 | 1608 | // PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held | 
 | 1609 | sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l( | 
 | 1610 |         const sp<AudioFlinger::Client>& client, | 
 | 1611 |         audio_stream_type_t streamType, | 
 | 1612 |         uint32_t sampleRate, | 
 | 1613 |         audio_format_t format, | 
 | 1614 |         audio_channel_mask_t channelMask, | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1615 |         size_t *pFrameCount, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1616 |         const sp<IMemory>& sharedBuffer, | 
 | 1617 |         int sessionId, | 
 | 1618 |         IAudioFlinger::track_flags_t *flags, | 
 | 1619 |         pid_t tid, | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1620 |         int uid, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1621 |         status_t *status) | 
 | 1622 | { | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1623 |     size_t frameCount = *pFrameCount; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1624 |     sp<Track> track; | 
 | 1625 |     status_t lStatus; | 
 | 1626 |  | 
 | 1627 |     bool isTimed = (*flags & IAudioFlinger::TRACK_TIMED) != 0; | 
 | 1628 |  | 
 | 1629 |     // client expresses a preference for FAST, but we get the final say | 
 | 1630 |     if (*flags & IAudioFlinger::TRACK_FAST) { | 
 | 1631 |       if ( | 
 | 1632 |             // not timed | 
 | 1633 |             (!isTimed) && | 
 | 1634 |             // either of these use cases: | 
 | 1635 |             ( | 
 | 1636 |               // use case 1: shared buffer with any frame count | 
 | 1637 |               ( | 
 | 1638 |                 (sharedBuffer != 0) | 
 | 1639 |               ) || | 
| Glenn Kasten | 1dfe2f9 | 2015-03-09 12:03:14 -0700 | [diff] [blame] | 1640 |               // use case 2: frame count is default or at least as large as HAL | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1641 |               ( | 
| Glenn Kasten | 1dfe2f9 | 2015-03-09 12:03:14 -0700 | [diff] [blame] | 1642 |                 // we formerly checked for a callback handler (non-0 tid), | 
 | 1643 |                 // but that is no longer required for TRANSFER_OBTAIN mode | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1644 |                 ((frameCount == 0) || | 
| Glenn Kasten | b5fed68 | 2013-12-03 09:06:43 -0800 | [diff] [blame] | 1645 |                 (frameCount >= mFrameCount)) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1646 |               ) | 
 | 1647 |             ) && | 
 | 1648 |             // PCM data | 
 | 1649 |             audio_is_linear_pcm(format) && | 
| Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 1650 |             // TODO: extract as a data library function that checks that a computationally | 
 | 1651 |             // expensive downmixer is not required: isFastOutputChannelConversion() | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1652 |             (channelMask == mChannelMask || | 
| Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 1653 |                     mChannelMask != AUDIO_CHANNEL_OUT_STEREO || | 
 | 1654 |                     (channelMask == AUDIO_CHANNEL_OUT_MONO | 
 | 1655 |                             /* && mChannelMask == AUDIO_CHANNEL_OUT_STEREO */)) && | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1656 |             // hardware sample rate | 
 | 1657 |             (sampleRate == mSampleRate) && | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1658 |             // normal mixer has an associated fast mixer | 
 | 1659 |             hasFastMixer() && | 
 | 1660 |             // there are sufficient fast track slots available | 
 | 1661 |             (mFastTrackAvailMask != 0) | 
 | 1662 |             // FIXME test that MixerThread for this fast track has a capable output HAL | 
 | 1663 |             // FIXME add a permission test also? | 
 | 1664 |         ) { | 
 | 1665 |         // if frameCount not specified, then it defaults to fast mixer (HAL) frame count | 
 | 1666 |         if (frameCount == 0) { | 
| Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 1667 |             // read the fast track multiplier property the first time it is needed | 
 | 1668 |             int ok = pthread_once(&sFastTrackMultiplierOnce, sFastTrackMultiplierInit); | 
 | 1669 |             if (ok != 0) { | 
 | 1670 |                 ALOGE("%s pthread_once failed: %d", __func__, ok); | 
 | 1671 |             } | 
 | 1672 |             frameCount = mFrameCount * sFastTrackMultiplier; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1673 |         } | 
 | 1674 |         ALOGV("AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%d mFrameCount=%d", | 
 | 1675 |                 frameCount, mFrameCount); | 
 | 1676 |       } else { | 
 | 1677 |         ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: isTimed=%d sharedBuffer=%p frameCount=%d " | 
| Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1678 |                 "mFrameCount=%d format=%#x mFormat=%#x isLinear=%d channelMask=%#x " | 
 | 1679 |                 "sampleRate=%u mSampleRate=%u " | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1680 |                 "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x", | 
| Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1681 |                 isTimed, sharedBuffer.get(), frameCount, mFrameCount, format, mFormat, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1682 |                 audio_is_linear_pcm(format), | 
 | 1683 |                 channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask); | 
 | 1684 |         *flags &= ~IAudioFlinger::TRACK_FAST; | 
| Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1685 |       } | 
 | 1686 |     } | 
 | 1687 |     // For normal PCM streaming tracks, update minimum frame count. | 
 | 1688 |     // For compatibility with AudioTrack calculation, buffer depth is forced | 
 | 1689 |     // to be at least 2 x the normal mixer frame count and cover audio hardware latency. | 
 | 1690 |     // This is probably too conservative, but legacy application code may depend on it. | 
 | 1691 |     // If you change this calculation, also review the start threshold which is related. | 
 | 1692 |     if (!(*flags & IAudioFlinger::TRACK_FAST) | 
 | 1693 |             && audio_is_linear_pcm(format) && sharedBuffer == 0) { | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 1694 |         // this must match AudioTrack.cpp calculateMinFrameCount(). | 
 | 1695 |         // TODO: Move to a common library | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1696 |         uint32_t latencyMs = mOutput->stream->get_latency(mOutput->stream); | 
 | 1697 |         uint32_t minBufCount = latencyMs / ((1000 * mNormalFrameCount) / mSampleRate); | 
 | 1698 |         if (minBufCount < 2) { | 
 | 1699 |             minBufCount = 2; | 
 | 1700 |         } | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 1701 |         // For normal mixing tracks, if speed is > 1.0f (normal), AudioTrack | 
 | 1702 |         // or the client should compute and pass in a larger buffer request. | 
| Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1703 |         size_t minFrameCount = | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 1704 |                 minBufCount * sourceFramesNeededWithTimestretch( | 
 | 1705 |                         sampleRate, mNormalFrameCount, | 
 | 1706 |                         mSampleRate, AUDIO_TIMESTRETCH_SPEED_NORMAL /*speed*/); | 
| Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1707 |         if (frameCount < minFrameCount) { // including frameCount == 0 | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1708 |             frameCount = minFrameCount; | 
 | 1709 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1710 |     } | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1711 |     *pFrameCount = frameCount; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1712 |  | 
| Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1713 |     switch (mType) { | 
 | 1714 |  | 
 | 1715 |     case DIRECT: | 
| Glenn Kasten | 993fa06 | 2014-05-02 11:14:34 -0700 | [diff] [blame] | 1716 |         if (audio_is_linear_pcm(format)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1717 |             if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { | 
| Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1718 |                 ALOGE("createTrack_l() Bad parameter: sampleRate %u format %#x, channelMask 0x%08x " | 
 | 1719 |                         "for output %p with format %#x", | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1720 |                         sampleRate, format, channelMask, mOutput, mFormat); | 
 | 1721 |                 lStatus = BAD_VALUE; | 
 | 1722 |                 goto Exit; | 
 | 1723 |             } | 
 | 1724 |         } | 
| Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1725 |         break; | 
 | 1726 |  | 
 | 1727 |     case OFFLOAD: | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1728 |         if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { | 
| Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1729 |             ALOGE("createTrack_l() Bad parameter: sampleRate %d format %#x, channelMask 0x%08x \"" | 
 | 1730 |                     "for output %p with format %#x", | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1731 |                     sampleRate, format, channelMask, mOutput, mFormat); | 
 | 1732 |             lStatus = BAD_VALUE; | 
 | 1733 |             goto Exit; | 
 | 1734 |         } | 
| Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1735 |         break; | 
 | 1736 |  | 
 | 1737 |     default: | 
| Glenn Kasten | 993fa06 | 2014-05-02 11:14:34 -0700 | [diff] [blame] | 1738 |         if (!audio_is_linear_pcm(format)) { | 
| Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1739 |                 ALOGE("createTrack_l() Bad parameter: format %#x \"" | 
 | 1740 |                         "for output %p with format %#x", | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1741 |                         format, mOutput, mFormat); | 
 | 1742 |                 lStatus = BAD_VALUE; | 
 | 1743 |                 goto Exit; | 
 | 1744 |         } | 
| Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 1745 |         if (sampleRate > mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1746 |             ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate); | 
 | 1747 |             lStatus = BAD_VALUE; | 
 | 1748 |             goto Exit; | 
 | 1749 |         } | 
| Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1750 |         break; | 
 | 1751 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1752 |     } | 
 | 1753 |  | 
 | 1754 |     lStatus = initCheck(); | 
 | 1755 |     if (lStatus != NO_ERROR) { | 
| Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 1756 |         ALOGE("createTrack_l() audio driver not initialized"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1757 |         goto Exit; | 
 | 1758 |     } | 
 | 1759 |  | 
 | 1760 |     { // scope for mLock | 
 | 1761 |         Mutex::Autolock _l(mLock); | 
 | 1762 |  | 
 | 1763 |         // all tracks in same audio session must share the same routing strategy otherwise | 
 | 1764 |         // conflicts will happen when tracks are moved from one output to another by audio policy | 
 | 1765 |         // manager | 
 | 1766 |         uint32_t strategy = AudioSystem::getStrategyForStream(streamType); | 
 | 1767 |         for (size_t i = 0; i < mTracks.size(); ++i) { | 
 | 1768 |             sp<Track> t = mTracks[i]; | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1769 |             if (t != 0 && t->isExternalTrack()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1770 |                 uint32_t actual = AudioSystem::getStrategyForStream(t->streamType()); | 
 | 1771 |                 if (sessionId == t->sessionId() && strategy != actual) { | 
 | 1772 |                     ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", | 
 | 1773 |                             strategy, actual); | 
 | 1774 |                     lStatus = BAD_VALUE; | 
 | 1775 |                     goto Exit; | 
 | 1776 |                 } | 
 | 1777 |             } | 
 | 1778 |         } | 
 | 1779 |  | 
 | 1780 |         if (!isTimed) { | 
 | 1781 |             track = new Track(this, client, streamType, sampleRate, format, | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1782 |                               channelMask, frameCount, NULL, sharedBuffer, | 
 | 1783 |                               sessionId, uid, *flags, TrackBase::TYPE_DEFAULT); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1784 |         } else { | 
 | 1785 |             track = TimedTrack::create(this, client, streamType, sampleRate, format, | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1786 |                     channelMask, frameCount, sharedBuffer, sessionId, uid); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1787 |         } | 
| Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 1788 |  | 
 | 1789 |         // new Track always returns non-NULL, | 
 | 1790 |         // but TimedTrack::create() is a factory that could fail by returning NULL | 
 | 1791 |         lStatus = track != 0 ? track->initCheck() : (status_t) NO_MEMORY; | 
 | 1792 |         if (lStatus != NO_ERROR) { | 
| Glenn Kasten | 0cde076 | 2014-01-16 15:06:36 -0800 | [diff] [blame] | 1793 |             ALOGE("createTrack_l() initCheck failed %d; no control block?", lStatus); | 
| Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 1794 |             // 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] | 1795 |             goto Exit; | 
 | 1796 |         } | 
 | 1797 |         mTracks.add(track); | 
 | 1798 |  | 
 | 1799 |         sp<EffectChain> chain = getEffectChain_l(sessionId); | 
 | 1800 |         if (chain != 0) { | 
 | 1801 |             ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); | 
 | 1802 |             track->setMainBuffer(chain->inBuffer()); | 
 | 1803 |             chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType())); | 
 | 1804 |             chain->incTrackCnt(); | 
 | 1805 |         } | 
 | 1806 |  | 
 | 1807 |         if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) { | 
 | 1808 |             pid_t callingPid = IPCThreadState::self()->getCallingPid(); | 
 | 1809 |             // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, | 
 | 1810 |             // so ask activity manager to do this on our behalf | 
 | 1811 |             sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp); | 
 | 1812 |         } | 
 | 1813 |     } | 
 | 1814 |  | 
 | 1815 |     lStatus = NO_ERROR; | 
 | 1816 |  | 
 | 1817 | Exit: | 
| Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1818 |     *status = lStatus; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1819 |     return track; | 
 | 1820 | } | 
 | 1821 |  | 
 | 1822 | uint32_t AudioFlinger::PlaybackThread::correctLatency_l(uint32_t latency) const | 
 | 1823 | { | 
 | 1824 |     return latency; | 
 | 1825 | } | 
 | 1826 |  | 
 | 1827 | uint32_t AudioFlinger::PlaybackThread::latency() const | 
 | 1828 | { | 
 | 1829 |     Mutex::Autolock _l(mLock); | 
 | 1830 |     return latency_l(); | 
 | 1831 | } | 
 | 1832 | uint32_t AudioFlinger::PlaybackThread::latency_l() const | 
 | 1833 | { | 
 | 1834 |     if (initCheck() == NO_ERROR) { | 
 | 1835 |         return correctLatency_l(mOutput->stream->get_latency(mOutput->stream)); | 
 | 1836 |     } else { | 
 | 1837 |         return 0; | 
 | 1838 |     } | 
 | 1839 | } | 
 | 1840 |  | 
 | 1841 | void AudioFlinger::PlaybackThread::setMasterVolume(float value) | 
 | 1842 | { | 
 | 1843 |     Mutex::Autolock _l(mLock); | 
 | 1844 |     // Don't apply master volume in SW if our HAL can do it for us. | 
 | 1845 |     if (mOutput && mOutput->audioHwDev && | 
 | 1846 |         mOutput->audioHwDev->canSetMasterVolume()) { | 
 | 1847 |         mMasterVolume = 1.0; | 
 | 1848 |     } else { | 
 | 1849 |         mMasterVolume = value; | 
 | 1850 |     } | 
 | 1851 | } | 
 | 1852 |  | 
 | 1853 | void AudioFlinger::PlaybackThread::setMasterMute(bool muted) | 
 | 1854 | { | 
 | 1855 |     Mutex::Autolock _l(mLock); | 
 | 1856 |     // Don't apply master mute in SW if our HAL can do it for us. | 
 | 1857 |     if (mOutput && mOutput->audioHwDev && | 
 | 1858 |         mOutput->audioHwDev->canSetMasterMute()) { | 
 | 1859 |         mMasterMute = false; | 
 | 1860 |     } else { | 
 | 1861 |         mMasterMute = muted; | 
 | 1862 |     } | 
 | 1863 | } | 
 | 1864 |  | 
 | 1865 | void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) | 
 | 1866 | { | 
 | 1867 |     Mutex::Autolock _l(mLock); | 
 | 1868 |     mStreamTypes[stream].volume = value; | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1869 |     broadcast_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1870 | } | 
 | 1871 |  | 
 | 1872 | void AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) | 
 | 1873 | { | 
 | 1874 |     Mutex::Autolock _l(mLock); | 
 | 1875 |     mStreamTypes[stream].mute = muted; | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1876 |     broadcast_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1877 | } | 
 | 1878 |  | 
 | 1879 | float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const | 
 | 1880 | { | 
 | 1881 |     Mutex::Autolock _l(mLock); | 
 | 1882 |     return mStreamTypes[stream].volume; | 
 | 1883 | } | 
 | 1884 |  | 
 | 1885 | // addTrack_l() must be called with ThreadBase::mLock held | 
 | 1886 | status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) | 
 | 1887 | { | 
 | 1888 |     status_t status = ALREADY_EXISTS; | 
 | 1889 |  | 
 | 1890 |     // set retry count for buffer fill | 
 | 1891 |     track->mRetryCount = kMaxTrackStartupRetries; | 
 | 1892 |     if (mActiveTracks.indexOf(track) < 0) { | 
 | 1893 |         // the track is newly added, make sure it fills up all its | 
 | 1894 |         // buffers before playing. This is to ensure the client will | 
 | 1895 |         // effectively get the latency it requested. | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1896 |         if (track->isExternalTrack()) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1897 |             TrackBase::track_state state = track->mState; | 
 | 1898 |             mLock.unlock(); | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1899 |             status = AudioSystem::startOutput(mId, track->streamType(), | 
 | 1900 |                                               (audio_session_t)track->sessionId()); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1901 |             mLock.lock(); | 
 | 1902 |             // abort track was stopped/paused while we released the lock | 
 | 1903 |             if (state != track->mState) { | 
 | 1904 |                 if (status == NO_ERROR) { | 
 | 1905 |                     mLock.unlock(); | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1906 |                     AudioSystem::stopOutput(mId, track->streamType(), | 
 | 1907 |                                             (audio_session_t)track->sessionId()); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1908 |                     mLock.lock(); | 
 | 1909 |                 } | 
 | 1910 |                 return INVALID_OPERATION; | 
 | 1911 |             } | 
 | 1912 |             // abort if start is rejected by audio policy manager | 
 | 1913 |             if (status != NO_ERROR) { | 
 | 1914 |                 return PERMISSION_DENIED; | 
 | 1915 |             } | 
 | 1916 | #ifdef ADD_BATTERY_DATA | 
 | 1917 |             // to track the speaker usage | 
 | 1918 |             addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); | 
 | 1919 | #endif | 
 | 1920 |         } | 
 | 1921 |  | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1922 |         track->mFillingUpStatus = track->sharedBuffer() != 0 ? Track::FS_FILLED : Track::FS_FILLING; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1923 |         track->mResetDone = false; | 
 | 1924 |         track->mPresentationCompleteFrames = 0; | 
 | 1925 |         mActiveTracks.add(track); | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1926 |         mWakeLockUids.add(track->uid()); | 
 | 1927 |         mActiveTracksGeneration++; | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 1928 |         mLatestActiveTrack = track; | 
| Eric Laurent | d0107bc | 2013-06-11 14:38:48 -0700 | [diff] [blame] | 1929 |         sp<EffectChain> chain = getEffectChain_l(track->sessionId()); | 
 | 1930 |         if (chain != 0) { | 
 | 1931 |             ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), | 
 | 1932 |                     track->sessionId()); | 
 | 1933 |             chain->incActiveTrackCnt(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1934 |         } | 
 | 1935 |  | 
 | 1936 |         status = NO_ERROR; | 
 | 1937 |     } | 
 | 1938 |  | 
| Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 1939 |     onAddNewTrack_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1940 |     return status; | 
 | 1941 | } | 
 | 1942 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1943 | bool AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1944 | { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1945 |     track->terminate(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1946 |     // active tracks are removed by threadLoop() | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1947 |     bool trackActive = (mActiveTracks.indexOf(track) >= 0); | 
 | 1948 |     track->mState = TrackBase::STOPPED; | 
 | 1949 |     if (!trackActive) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1950 |         removeTrack_l(track); | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 1951 |     } else if (track->isFastTrack() || track->isOffloaded() || track->isDirect()) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1952 |         track->mState = TrackBase::STOPPING_1; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1953 |     } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1954 |  | 
 | 1955 |     return trackActive; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1956 | } | 
 | 1957 |  | 
 | 1958 | void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track) | 
 | 1959 | { | 
 | 1960 |     track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); | 
 | 1961 |     mTracks.remove(track); | 
 | 1962 |     deleteTrackName_l(track->name()); | 
 | 1963 |     // redundant as track is about to be destroyed, for dumpsys only | 
 | 1964 |     track->mName = -1; | 
 | 1965 |     if (track->isFastTrack()) { | 
 | 1966 |         int index = track->mFastIndex; | 
 | 1967 |         ALOG_ASSERT(0 < index && index < (int)FastMixerState::kMaxFastTracks); | 
 | 1968 |         ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index))); | 
 | 1969 |         mFastTrackAvailMask |= 1 << index; | 
 | 1970 |         // redundant as track is about to be destroyed, for dumpsys only | 
 | 1971 |         track->mFastIndex = -1; | 
 | 1972 |     } | 
 | 1973 |     sp<EffectChain> chain = getEffectChain_l(track->sessionId()); | 
 | 1974 |     if (chain != 0) { | 
 | 1975 |         chain->decTrackCnt(); | 
 | 1976 |     } | 
 | 1977 | } | 
 | 1978 |  | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1979 | void AudioFlinger::PlaybackThread::broadcast_l() | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1980 | { | 
 | 1981 |     // Thread could be blocked waiting for async | 
 | 1982 |     // so signal it to handle state changes immediately | 
 | 1983 |     // If threadLoop is currently unlocked a signal of mWaitWorkCV will | 
 | 1984 |     // be lost so we also flag to prevent it blocking on mWaitWorkCV | 
 | 1985 |     mSignalPending = true; | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1986 |     mWaitWorkCV.broadcast(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1987 | } | 
 | 1988 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1989 | String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) | 
 | 1990 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1991 |     Mutex::Autolock _l(mLock); | 
 | 1992 |     if (initCheck() != NO_ERROR) { | 
| Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 1993 |         return String8(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1994 |     } | 
 | 1995 |  | 
| Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 1996 |     char *s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string()); | 
 | 1997 |     const String8 out_s8(s); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1998 |     free(s); | 
 | 1999 |     return out_s8; | 
 | 2000 | } | 
 | 2001 |  | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2002 | void AudioFlinger::PlaybackThread::ioConfigChanged(audio_io_config_event event) { | 
 | 2003 |     sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); | 
 | 2004 |     ALOGV("PlaybackThread::ioConfigChanged, thread %p, event %d", this, event); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2005 |  | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2006 |     desc->mIoHandle = mId; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2007 |  | 
 | 2008 |     switch (event) { | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2009 |     case AUDIO_OUTPUT_OPENED: | 
 | 2010 |     case AUDIO_OUTPUT_CONFIG_CHANGED: | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 2011 |         desc->mPatch = mPatch; | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2012 |         desc->mChannelMask = mChannelMask; | 
 | 2013 |         desc->mSamplingRate = mSampleRate; | 
 | 2014 |         desc->mFormat = mFormat; | 
 | 2015 |         desc->mFrameCount = mNormalFrameCount; // FIXME see | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2016 |                                              // AudioFlinger::frameCount(audio_io_handle_t) | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2017 |         desc->mLatency = latency_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2018 |         break; | 
 | 2019 |  | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2020 |     case AUDIO_OUTPUT_CLOSED: | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2021 |     default: | 
 | 2022 |         break; | 
 | 2023 |     } | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2024 |     mAudioFlinger->ioConfigChanged(event, desc); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2025 | } | 
 | 2026 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2027 | void AudioFlinger::PlaybackThread::writeCallback() | 
 | 2028 | { | 
 | 2029 |     ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2030 |     mCallbackThread->resetWriteBlocked(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2031 | } | 
 | 2032 |  | 
 | 2033 | void AudioFlinger::PlaybackThread::drainCallback() | 
 | 2034 | { | 
 | 2035 |     ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2036 |     mCallbackThread->resetDraining(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2037 | } | 
 | 2038 |  | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2039 | void AudioFlinger::PlaybackThread::resetWriteBlocked(uint32_t sequence) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2040 | { | 
 | 2041 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2042 |     // reject out of sequence requests | 
 | 2043 |     if ((mWriteAckSequence & 1) && (sequence == mWriteAckSequence)) { | 
 | 2044 |         mWriteAckSequence &= ~1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2045 |         mWaitWorkCV.signal(); | 
 | 2046 |     } | 
 | 2047 | } | 
 | 2048 |  | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2049 | void AudioFlinger::PlaybackThread::resetDraining(uint32_t sequence) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2050 | { | 
 | 2051 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2052 |     // reject out of sequence requests | 
 | 2053 |     if ((mDrainSequence & 1) && (sequence == mDrainSequence)) { | 
 | 2054 |         mDrainSequence &= ~1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2055 |         mWaitWorkCV.signal(); | 
 | 2056 |     } | 
 | 2057 | } | 
 | 2058 |  | 
 | 2059 | // static | 
 | 2060 | int AudioFlinger::PlaybackThread::asyncCallback(stream_callback_event_t event, | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 2061 |                                                 void *param __unused, | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2062 |                                                 void *cookie) | 
 | 2063 | { | 
 | 2064 |     AudioFlinger::PlaybackThread *me = (AudioFlinger::PlaybackThread *)cookie; | 
 | 2065 |     ALOGV("asyncCallback() event %d", event); | 
 | 2066 |     switch (event) { | 
 | 2067 |     case STREAM_CBK_EVENT_WRITE_READY: | 
 | 2068 |         me->writeCallback(); | 
 | 2069 |         break; | 
 | 2070 |     case STREAM_CBK_EVENT_DRAIN_READY: | 
 | 2071 |         me->drainCallback(); | 
 | 2072 |         break; | 
 | 2073 |     default: | 
 | 2074 |         ALOGW("asyncCallback() unknown event %d", event); | 
 | 2075 |         break; | 
 | 2076 |     } | 
 | 2077 |     return 0; | 
 | 2078 | } | 
 | 2079 |  | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 2080 | void AudioFlinger::PlaybackThread::readOutputParameters_l() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2081 | { | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2082 |     // 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] | 2083 |     mSampleRate = mOutput->stream->common.get_sample_rate(&mOutput->stream->common); | 
 | 2084 |     mChannelMask = mOutput->stream->common.get_channels(&mOutput->stream->common); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2085 |     if (!audio_is_output_channel(mChannelMask)) { | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2086 |         LOG_ALWAYS_FATAL("HAL channel mask %#x not valid for output", mChannelMask); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2087 |     } | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 2088 |     if ((mType == MIXER || mType == DUPLICATING) | 
 | 2089 |             && !isValidPcmSinkChannelMask(mChannelMask)) { | 
 | 2090 |         LOG_ALWAYS_FATAL("HAL channel mask %#x not supported for mixed output", | 
 | 2091 |                 mChannelMask); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2092 |     } | 
| Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 2093 |     mChannelCount = audio_channel_count_from_out_mask(mChannelMask); | 
| Andy Hung | 463be25 | 2014-07-10 16:56:07 -0700 | [diff] [blame] | 2094 |     mHALFormat = mOutput->stream->common.get_format(&mOutput->stream->common); | 
 | 2095 |     mFormat = mHALFormat; | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2096 |     if (!audio_is_valid_format(mFormat)) { | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2097 |         LOG_ALWAYS_FATAL("HAL format %#x not valid for output", mFormat); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2098 |     } | 
| Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 2099 |     if ((mType == MIXER || mType == DUPLICATING) | 
 | 2100 |             && !isValidPcmSinkFormat(mFormat)) { | 
 | 2101 |         LOG_FATAL("HAL format %#x not supported for mixed output", | 
 | 2102 |                 mFormat); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2103 |     } | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2104 |     mFrameSize = mOutput->getFrameSize(); | 
| Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 2105 |     mBufferSize = mOutput->stream->common.get_buffer_size(&mOutput->stream->common); | 
 | 2106 |     mFrameCount = mBufferSize / mFrameSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2107 |     if (mFrameCount & 15) { | 
 | 2108 |         ALOGW("HAL output buffer size is %u frames but AudioMixer requires multiples of 16 frames", | 
 | 2109 |                 mFrameCount); | 
 | 2110 |     } | 
 | 2111 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2112 |     if ((mOutput->flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) && | 
 | 2113 |             (mOutput->stream->set_callback != NULL)) { | 
 | 2114 |         if (mOutput->stream->set_callback(mOutput->stream, | 
 | 2115 |                                       AudioFlinger::PlaybackThread::asyncCallback, this) == 0) { | 
 | 2116 |             mUseAsyncWrite = true; | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 2117 |             mCallbackThread = new AudioFlinger::AsyncCallbackThread(this); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2118 |         } | 
 | 2119 |     } | 
 | 2120 |  | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2121 |     mHwSupportsPause = false; | 
 | 2122 |     if (mOutput->flags & AUDIO_OUTPUT_FLAG_DIRECT) { | 
 | 2123 |         if (mOutput->stream->pause != NULL) { | 
 | 2124 |             if (mOutput->stream->resume != NULL) { | 
 | 2125 |                 mHwSupportsPause = true; | 
 | 2126 |             } else { | 
 | 2127 |                 ALOGW("direct output implements pause but not resume"); | 
 | 2128 |             } | 
 | 2129 |         } else if (mOutput->stream->resume != NULL) { | 
 | 2130 |             ALOGW("direct output implements resume but not pause"); | 
 | 2131 |         } | 
 | 2132 |     } | 
| Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 2133 |     if (!mHwSupportsPause && mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) { | 
 | 2134 |         LOG_ALWAYS_FATAL("HW_AV_SYNC requested but HAL does not implement pause and resume"); | 
 | 2135 |     } | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2136 |  | 
| Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 2137 |     if (mType == DUPLICATING && mMixerBufferEnabled && mEffectBufferEnabled) { | 
 | 2138 |         // For best precision, we use float instead of the associated output | 
 | 2139 |         // device format (typically PCM 16 bit). | 
 | 2140 |  | 
 | 2141 |         mFormat = AUDIO_FORMAT_PCM_FLOAT; | 
 | 2142 |         mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); | 
 | 2143 |         mBufferSize = mFrameSize * mFrameCount; | 
 | 2144 |  | 
 | 2145 |         // TODO: We currently use the associated output device channel mask and sample rate. | 
 | 2146 |         // (1) Perhaps use the ORed channel mask of all downstream MixerThreads | 
 | 2147 |         // (if a valid mask) to avoid premature downmix. | 
 | 2148 |         // (2) Perhaps use the maximum sample rate of all downstream MixerThreads | 
 | 2149 |         // instead of the output device sample rate to avoid loss of high frequency information. | 
 | 2150 |         // This may need to be updated as MixerThread/OutputTracks are added and not here. | 
 | 2151 |     } | 
 | 2152 |  | 
| Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2153 |     // 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] | 2154 |     double multiplier = 1.0; | 
 | 2155 |     if (mType == MIXER && (kUseFastMixer == FastMixer_Static || | 
 | 2156 |             kUseFastMixer == FastMixer_Dynamic)) { | 
| Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2157 |         size_t minNormalFrameCount = (kMinNormalSinkBufferSizeMs * mSampleRate) / 1000; | 
 | 2158 |         size_t maxNormalFrameCount = (kMaxNormalSinkBufferSizeMs * mSampleRate) / 1000; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2159 |         // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer | 
 | 2160 |         minNormalFrameCount = (minNormalFrameCount + 15) & ~15; | 
 | 2161 |         maxNormalFrameCount = maxNormalFrameCount & ~15; | 
 | 2162 |         if (maxNormalFrameCount < minNormalFrameCount) { | 
 | 2163 |             maxNormalFrameCount = minNormalFrameCount; | 
 | 2164 |         } | 
 | 2165 |         multiplier = (double) minNormalFrameCount / (double) mFrameCount; | 
 | 2166 |         if (multiplier <= 1.0) { | 
 | 2167 |             multiplier = 1.0; | 
 | 2168 |         } else if (multiplier <= 2.0) { | 
 | 2169 |             if (2 * mFrameCount <= maxNormalFrameCount) { | 
 | 2170 |                 multiplier = 2.0; | 
 | 2171 |             } else { | 
 | 2172 |                 multiplier = (double) maxNormalFrameCount / (double) mFrameCount; | 
 | 2173 |             } | 
 | 2174 |         } else { | 
 | 2175 |             // 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] | 2176 |             // 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] | 2177 |             // track, but we sometimes have to do this to satisfy the maximum frame count | 
 | 2178 |             // constraint) | 
 | 2179 |             // FIXME this rounding up should not be done if no HAL SRC | 
 | 2180 |             uint32_t truncMult = (uint32_t) multiplier; | 
 | 2181 |             if ((truncMult & 1)) { | 
 | 2182 |                 if ((truncMult + 1) * mFrameCount <= maxNormalFrameCount) { | 
 | 2183 |                     ++truncMult; | 
 | 2184 |                 } | 
 | 2185 |             } | 
 | 2186 |             multiplier = (double) truncMult; | 
 | 2187 |         } | 
 | 2188 |     } | 
 | 2189 |     mNormalFrameCount = multiplier * mFrameCount; | 
 | 2190 |     // round up to nearest 16 frames to satisfy AudioMixer | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 2191 |     if (mType == MIXER || mType == DUPLICATING) { | 
 | 2192 |         mNormalFrameCount = (mNormalFrameCount + 15) & ~15; | 
 | 2193 |     } | 
| Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2194 |     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] | 2195 |             mNormalFrameCount); | 
 | 2196 |  | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2197 |     // Check if we want to throttle the processing to no more than 2x normal rate | 
 | 2198 |     mThreadThrottle = property_get_bool("af.thread.throttle", true /* default_value */); | 
| Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 2199 |     mThreadThrottleTimeMs = 0; | 
 | 2200 |     mThreadThrottleEndMs = 0; | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2201 |     mHalfBufferMs = mNormalFrameCount * 1000 / (2 * mSampleRate); | 
 | 2202 |  | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2203 |     // mSinkBuffer is the sink buffer.  Size is always multiple-of-16 frames. | 
 | 2204 |     // Originally this was int16_t[] array, need to remove legacy implications. | 
 | 2205 |     free(mSinkBuffer); | 
 | 2206 |     mSinkBuffer = NULL; | 
| Andy Hung | 5b10a20 | 2014-03-13 13:59:29 -0700 | [diff] [blame] | 2207 |     // For sink buffer size, we use the frame size from the downstream sink to avoid problems | 
 | 2208 |     // with non PCM formats for compressed music, e.g. AAC, and Offload threads. | 
 | 2209 |     const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2210 |     (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2211 |  | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2212 |     // We resize the mMixerBuffer according to the requirements of the sink buffer which | 
 | 2213 |     // drives the output. | 
 | 2214 |     free(mMixerBuffer); | 
 | 2215 |     mMixerBuffer = NULL; | 
 | 2216 |     if (mMixerBufferEnabled) { | 
 | 2217 |         mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT; // also valid: AUDIO_FORMAT_PCM_16_BIT. | 
 | 2218 |         mMixerBufferSize = mNormalFrameCount * mChannelCount | 
 | 2219 |                 * audio_bytes_per_sample(mMixerBufferFormat); | 
 | 2220 |         (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize); | 
 | 2221 |     } | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2222 |     free(mEffectBuffer); | 
 | 2223 |     mEffectBuffer = NULL; | 
 | 2224 |     if (mEffectBufferEnabled) { | 
 | 2225 |         mEffectBufferFormat = AUDIO_FORMAT_PCM_16_BIT; // Note: Effects support 16b only | 
 | 2226 |         mEffectBufferSize = mNormalFrameCount * mChannelCount | 
 | 2227 |                 * audio_bytes_per_sample(mEffectBufferFormat); | 
 | 2228 |         (void)posix_memalign(&mEffectBuffer, 32, mEffectBufferSize); | 
 | 2229 |     } | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2230 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2231 |     // force reconfiguration of effect chains and engines to take new buffer size and audio | 
 | 2232 |     // parameters into account | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 2233 |     // 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] | 2234 |     // but in this case nothing is done below as no audio sessions have effect yet so it doesn't | 
 | 2235 |     // matter. | 
 | 2236 |     // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains | 
 | 2237 |     Vector< sp<EffectChain> > effectChains = mEffectChains; | 
 | 2238 |     for (size_t i = 0; i < effectChains.size(); i ++) { | 
 | 2239 |         mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); | 
 | 2240 |     } | 
 | 2241 | } | 
 | 2242 |  | 
 | 2243 |  | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2244 | status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2245 | { | 
 | 2246 |     if (halFrames == NULL || dspFrames == NULL) { | 
 | 2247 |         return BAD_VALUE; | 
 | 2248 |     } | 
 | 2249 |     Mutex::Autolock _l(mLock); | 
 | 2250 |     if (initCheck() != NO_ERROR) { | 
 | 2251 |         return INVALID_OPERATION; | 
 | 2252 |     } | 
 | 2253 |     size_t framesWritten = mBytesWritten / mFrameSize; | 
 | 2254 |     *halFrames = framesWritten; | 
 | 2255 |  | 
 | 2256 |     if (isSuspended()) { | 
 | 2257 |         // return an estimation of rendered frames when the output is suspended | 
 | 2258 |         size_t latencyFrames = (latency_l() * mSampleRate) / 1000; | 
 | 2259 |         *dspFrames = framesWritten >= latencyFrames ? framesWritten - latencyFrames : 0; | 
 | 2260 |         return NO_ERROR; | 
 | 2261 |     } else { | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2262 |         status_t status; | 
 | 2263 |         uint32_t frames; | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2264 |         status = mOutput->getRenderPosition(&frames); | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2265 |         *dspFrames = (size_t)frames; | 
 | 2266 |         return status; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2267 |     } | 
 | 2268 | } | 
 | 2269 |  | 
 | 2270 | uint32_t AudioFlinger::PlaybackThread::hasAudioSession(int sessionId) const | 
 | 2271 | { | 
 | 2272 |     Mutex::Autolock _l(mLock); | 
 | 2273 |     uint32_t result = 0; | 
 | 2274 |     if (getEffectChain_l(sessionId) != 0) { | 
 | 2275 |         result = EFFECT_SESSION; | 
 | 2276 |     } | 
 | 2277 |  | 
 | 2278 |     for (size_t i = 0; i < mTracks.size(); ++i) { | 
 | 2279 |         sp<Track> track = mTracks[i]; | 
| Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2280 |         if (sessionId == track->sessionId() && !track->isInvalid()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2281 |             result |= TRACK_SESSION; | 
 | 2282 |             break; | 
 | 2283 |         } | 
 | 2284 |     } | 
 | 2285 |  | 
 | 2286 |     return result; | 
 | 2287 | } | 
 | 2288 |  | 
 | 2289 | uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(int sessionId) | 
 | 2290 | { | 
 | 2291 |     // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that | 
 | 2292 |     // it is moved to correct output by audio policy manager when A2DP is connected or disconnected | 
 | 2293 |     if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { | 
 | 2294 |         return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); | 
 | 2295 |     } | 
 | 2296 |     for (size_t i = 0; i < mTracks.size(); i++) { | 
 | 2297 |         sp<Track> track = mTracks[i]; | 
| Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2298 |         if (sessionId == track->sessionId() && !track->isInvalid()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2299 |             return AudioSystem::getStrategyForStream(track->streamType()); | 
 | 2300 |         } | 
 | 2301 |     } | 
 | 2302 |     return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); | 
 | 2303 | } | 
 | 2304 |  | 
 | 2305 |  | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2306 | AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2307 | { | 
 | 2308 |     Mutex::Autolock _l(mLock); | 
 | 2309 |     return mOutput; | 
 | 2310 | } | 
 | 2311 |  | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2312 | AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2313 | { | 
 | 2314 |     Mutex::Autolock _l(mLock); | 
 | 2315 |     AudioStreamOut *output = mOutput; | 
 | 2316 |     mOutput = NULL; | 
 | 2317 |     // FIXME FastMixer might also have a raw ptr to mOutputSink; | 
 | 2318 |     //       must push a NULL and wait for ack | 
 | 2319 |     mOutputSink.clear(); | 
 | 2320 |     mPipeSink.clear(); | 
 | 2321 |     mNormalSink.clear(); | 
 | 2322 |     return output; | 
 | 2323 | } | 
 | 2324 |  | 
 | 2325 | // this method must always be called either with ThreadBase mLock held or inside the thread loop | 
 | 2326 | audio_stream_t* AudioFlinger::PlaybackThread::stream() const | 
 | 2327 | { | 
 | 2328 |     if (mOutput == NULL) { | 
 | 2329 |         return NULL; | 
 | 2330 |     } | 
 | 2331 |     return &mOutput->stream->common; | 
 | 2332 | } | 
 | 2333 |  | 
 | 2334 | uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const | 
 | 2335 | { | 
 | 2336 |     return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000); | 
 | 2337 | } | 
 | 2338 |  | 
 | 2339 | status_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event) | 
 | 2340 | { | 
 | 2341 |     if (!isValidSyncEvent(event)) { | 
 | 2342 |         return BAD_VALUE; | 
 | 2343 |     } | 
 | 2344 |  | 
 | 2345 |     Mutex::Autolock _l(mLock); | 
 | 2346 |  | 
 | 2347 |     for (size_t i = 0; i < mTracks.size(); ++i) { | 
 | 2348 |         sp<Track> track = mTracks[i]; | 
 | 2349 |         if (event->triggerSession() == track->sessionId()) { | 
 | 2350 |             (void) track->setSyncEvent(event); | 
 | 2351 |             return NO_ERROR; | 
 | 2352 |         } | 
 | 2353 |     } | 
 | 2354 |  | 
 | 2355 |     return NAME_NOT_FOUND; | 
 | 2356 | } | 
 | 2357 |  | 
 | 2358 | bool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) const | 
 | 2359 | { | 
 | 2360 |     return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE; | 
 | 2361 | } | 
 | 2362 |  | 
 | 2363 | void AudioFlinger::PlaybackThread::threadLoop_removeTracks( | 
 | 2364 |         const Vector< sp<Track> >& tracksToRemove) | 
 | 2365 | { | 
 | 2366 |     size_t count = tracksToRemove.size(); | 
| Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 2367 |     if (count > 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2368 |         for (size_t i = 0 ; i < count ; i++) { | 
 | 2369 |             const sp<Track>& track = tracksToRemove.itemAt(i); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2370 |             if (track->isExternalTrack()) { | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2371 |                 AudioSystem::stopOutput(mId, track->streamType(), | 
 | 2372 |                                         (audio_session_t)track->sessionId()); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2373 | #ifdef ADD_BATTERY_DATA | 
 | 2374 |                 // to track the speaker usage | 
 | 2375 |                 addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); | 
 | 2376 | #endif | 
 | 2377 |                 if (track->isTerminated()) { | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2378 |                     AudioSystem::releaseOutput(mId, track->streamType(), | 
 | 2379 |                                                (audio_session_t)track->sessionId()); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2380 |                 } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2381 |             } | 
 | 2382 |         } | 
 | 2383 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2384 | } | 
 | 2385 |  | 
 | 2386 | void AudioFlinger::PlaybackThread::checkSilentMode_l() | 
 | 2387 | { | 
 | 2388 |     if (!mMasterMute) { | 
 | 2389 |         char value[PROPERTY_VALUE_MAX]; | 
 | 2390 |         if (property_get("ro.audio.silent", value, "0") > 0) { | 
 | 2391 |             char *endptr; | 
 | 2392 |             unsigned long ul = strtoul(value, &endptr, 0); | 
 | 2393 |             if (*endptr == '\0' && ul != 0) { | 
 | 2394 |                 ALOGD("Silence is golden"); | 
 | 2395 |                 // The setprop command will not allow a property to be changed after | 
 | 2396 |                 // the first time it is set, so we don't have to worry about un-muting. | 
 | 2397 |                 setMasterMute_l(true); | 
 | 2398 |             } | 
 | 2399 |         } | 
 | 2400 |     } | 
 | 2401 | } | 
 | 2402 |  | 
 | 2403 | // shared by MIXER and DIRECT, overridden by DUPLICATING | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2404 | ssize_t AudioFlinger::PlaybackThread::threadLoop_write() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2405 | { | 
 | 2406 |     // FIXME rewrite to reduce number of system calls | 
 | 2407 |     mLastWriteTime = systemTime(); | 
 | 2408 |     mInWrite = true; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2409 |     ssize_t bytesWritten; | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2410 |     const size_t offset = mCurrentWriteLength - mBytesRemaining; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2411 |  | 
 | 2412 |     // If an NBAIO sink is present, use it to write the normal mixer's submix | 
 | 2413 |     if (mNormalSink != 0) { | 
| Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 2414 |  | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2415 |         const size_t count = mBytesRemaining / mFrameSize; | 
 | 2416 |  | 
| Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 2417 |         ATRACE_BEGIN("write"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2418 |         // update the setpoint when AudioFlinger::mScreenState changes | 
 | 2419 |         uint32_t screenState = AudioFlinger::mScreenState; | 
 | 2420 |         if (screenState != mScreenState) { | 
 | 2421 |             mScreenState = screenState; | 
 | 2422 |             MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); | 
 | 2423 |             if (pipe != NULL) { | 
 | 2424 |                 pipe->setAvgFrames((mScreenState & 1) ? | 
 | 2425 |                         (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); | 
 | 2426 |             } | 
 | 2427 |         } | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2428 |         ssize_t framesWritten = mNormalSink->write((char *)mSinkBuffer + offset, count); | 
| Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 2429 |         ATRACE_END(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2430 |         if (framesWritten > 0) { | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2431 |             bytesWritten = framesWritten * mFrameSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2432 |         } else { | 
 | 2433 |             bytesWritten = framesWritten; | 
 | 2434 |         } | 
| Glenn Kasten | efaa7ab | 2014-08-20 08:48:54 -0700 | [diff] [blame] | 2435 |         mLatchDValid = false; | 
| Glenn Kasten | 767094d | 2013-08-23 13:51:43 -0700 | [diff] [blame] | 2436 |         status_t status = mNormalSink->getTimestamp(mLatchD.mTimestamp); | 
| Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 2437 |         if (status == NO_ERROR) { | 
 | 2438 |             size_t totalFramesWritten = mNormalSink->framesWritten(); | 
 | 2439 |             if (totalFramesWritten >= mLatchD.mTimestamp.mPosition) { | 
 | 2440 |                 mLatchD.mUnpresentedFrames = totalFramesWritten - mLatchD.mTimestamp.mPosition; | 
| Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 2441 |                 // mLatchD.mFramesReleased is set immediately before D is clocked into Q | 
| Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 2442 |                 mLatchDValid = true; | 
 | 2443 |             } | 
 | 2444 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2445 |     // otherwise use the HAL / AudioStreamOut directly | 
 | 2446 |     } else { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2447 |         // Direct output and offload threads | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2448 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2449 |         if (mUseAsyncWrite) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2450 |             ALOGW_IF(mWriteAckSequence & 1, "threadLoop_write(): out of sequence write request"); | 
 | 2451 |             mWriteAckSequence += 2; | 
 | 2452 |             mWriteAckSequence |= 1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2453 |             ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2454 |             mCallbackThread->setWriteBlocked(mWriteAckSequence); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2455 |         } | 
| Glenn Kasten | 767094d | 2013-08-23 13:51:43 -0700 | [diff] [blame] | 2456 |         // FIXME We should have an implementation of timestamps for direct output threads. | 
 | 2457 |         // They are used e.g for multichannel PCM playback over HDMI. | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2458 |         bytesWritten = mOutput->write((char *)mSinkBuffer + offset, mBytesRemaining); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2459 |         if (mUseAsyncWrite && | 
 | 2460 |                 ((bytesWritten < 0) || (bytesWritten == (ssize_t)mBytesRemaining))) { | 
 | 2461 |             // 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] | 2462 |             mWriteAckSequence &= ~1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2463 |             ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2464 |             mCallbackThread->setWriteBlocked(mWriteAckSequence); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2465 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2466 |     } | 
 | 2467 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2468 |     mNumWrites++; | 
 | 2469 |     mInWrite = false; | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 2470 |     mStandby = false; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2471 |     return bytesWritten; | 
 | 2472 | } | 
 | 2473 |  | 
 | 2474 | void AudioFlinger::PlaybackThread::threadLoop_drain() | 
 | 2475 | { | 
 | 2476 |     if (mOutput->stream->drain) { | 
 | 2477 |         ALOGV("draining %s", (mMixerStatus == MIXER_DRAIN_TRACK) ? "early" : "full"); | 
 | 2478 |         if (mUseAsyncWrite) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2479 |             ALOGW_IF(mDrainSequence & 1, "threadLoop_drain(): out of sequence drain request"); | 
 | 2480 |             mDrainSequence |= 1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2481 |             ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2482 |             mCallbackThread->setDraining(mDrainSequence); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2483 |         } | 
 | 2484 |         mOutput->stream->drain(mOutput->stream, | 
 | 2485 |             (mMixerStatus == MIXER_DRAIN_TRACK) ? AUDIO_DRAIN_EARLY_NOTIFY | 
 | 2486 |                                                 : AUDIO_DRAIN_ALL); | 
 | 2487 |     } | 
 | 2488 | } | 
 | 2489 |  | 
 | 2490 | void AudioFlinger::PlaybackThread::threadLoop_exit() | 
 | 2491 | { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2492 |     { | 
 | 2493 |         Mutex::Autolock _l(mLock); | 
 | 2494 |         for (size_t i = 0; i < mTracks.size(); i++) { | 
 | 2495 |             sp<Track> track = mTracks[i]; | 
 | 2496 |             track->invalidate(); | 
 | 2497 |         } | 
 | 2498 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2499 | } | 
 | 2500 |  | 
 | 2501 | /* | 
 | 2502 | The derived values that are cached: | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2503 |  - mSinkBufferSize from frame count * frame size | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2504 |  - mActiveSleepTimeUs from activeSleepTimeUs() | 
 | 2505 |  - mIdleSleepTimeUs from idleSleepTimeUs() | 
 | 2506 |  - mStandbyDelayNs from mActiveSleepTimeUs (DIRECT only) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2507 |  - maxPeriod from frame count and sample rate (MIXER only) | 
 | 2508 |  | 
 | 2509 | The parameters that affect these derived values are: | 
 | 2510 |  - frame count | 
 | 2511 |  - frame size | 
 | 2512 |  - sample rate | 
 | 2513 |  - device type: A2DP or not | 
 | 2514 |  - device latency | 
 | 2515 |  - format: PCM or not | 
 | 2516 |  - active sleep time | 
 | 2517 |  - idle sleep time | 
 | 2518 | */ | 
 | 2519 |  | 
 | 2520 | void AudioFlinger::PlaybackThread::cacheParameters_l() | 
 | 2521 | { | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2522 |     mSinkBufferSize = mNormalFrameCount * mFrameSize; | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2523 |     mActiveSleepTimeUs = activeSleepTimeUs(); | 
 | 2524 |     mIdleSleepTimeUs = idleSleepTimeUs(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2525 | } | 
 | 2526 |  | 
 | 2527 | void AudioFlinger::PlaybackThread::invalidateTracks(audio_stream_type_t streamType) | 
 | 2528 | { | 
| Glenn Kasten | 7c02724 | 2012-12-26 14:43:16 -0800 | [diff] [blame] | 2529 |     ALOGV("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %d", | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2530 |             this,  streamType, mTracks.size()); | 
 | 2531 |     Mutex::Autolock _l(mLock); | 
 | 2532 |  | 
 | 2533 |     size_t size = mTracks.size(); | 
 | 2534 |     for (size_t i = 0; i < size; i++) { | 
 | 2535 |         sp<Track> t = mTracks[i]; | 
 | 2536 |         if (t->streamType() == streamType) { | 
| Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2537 |             t->invalidate(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2538 |         } | 
 | 2539 |     } | 
 | 2540 | } | 
 | 2541 |  | 
 | 2542 | status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) | 
 | 2543 | { | 
 | 2544 |     int session = chain->sessionId(); | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2545 |     int16_t* buffer = reinterpret_cast<int16_t*>(mEffectBufferEnabled | 
 | 2546 |             ? mEffectBuffer : mSinkBuffer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2547 |     bool ownsBuffer = false; | 
 | 2548 |  | 
 | 2549 |     ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); | 
 | 2550 |     if (session > 0) { | 
 | 2551 |         // 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] | 2552 |         // the sink buffer as input | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2553 |         if (mType != DIRECT) { | 
 | 2554 |             size_t numSamples = mNormalFrameCount * mChannelCount; | 
 | 2555 |             buffer = new int16_t[numSamples]; | 
 | 2556 |             memset(buffer, 0, numSamples * sizeof(int16_t)); | 
 | 2557 |             ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session); | 
 | 2558 |             ownsBuffer = true; | 
 | 2559 |         } | 
 | 2560 |  | 
 | 2561 |         // Attach all tracks with same session ID to this chain. | 
 | 2562 |         for (size_t i = 0; i < mTracks.size(); ++i) { | 
 | 2563 |             sp<Track> track = mTracks[i]; | 
 | 2564 |             if (session == track->sessionId()) { | 
 | 2565 |                 ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), | 
 | 2566 |                         buffer); | 
 | 2567 |                 track->setMainBuffer(buffer); | 
 | 2568 |                 chain->incTrackCnt(); | 
 | 2569 |             } | 
 | 2570 |         } | 
 | 2571 |  | 
 | 2572 |         // indicate all active tracks in the chain | 
 | 2573 |         for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { | 
 | 2574 |             sp<Track> track = mActiveTracks[i].promote(); | 
 | 2575 |             if (track == 0) { | 
 | 2576 |                 continue; | 
 | 2577 |             } | 
 | 2578 |             if (session == track->sessionId()) { | 
 | 2579 |                 ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); | 
 | 2580 |                 chain->incActiveTrackCnt(); | 
 | 2581 |             } | 
 | 2582 |         } | 
 | 2583 |     } | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2584 |     chain->setThread(this); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2585 |     chain->setInBuffer(buffer, ownsBuffer); | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2586 |     chain->setOutBuffer(reinterpret_cast<int16_t*>(mEffectBufferEnabled | 
 | 2587 |             ? mEffectBuffer : mSinkBuffer)); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2588 |     // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted at end of effect | 
 | 2589 |     // chains list in order to be processed last as it contains output stage effects | 
 | 2590 |     // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before | 
 | 2591 |     // session AUDIO_SESSION_OUTPUT_STAGE to be processed | 
 | 2592 |     // after track specific effects and before output stage | 
 | 2593 |     // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and | 
 | 2594 |     // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX | 
 | 2595 |     // Effect chain for other sessions are inserted at beginning of effect | 
 | 2596 |     // chains list to be processed before output mix effects. Relative order between other | 
 | 2597 |     // sessions is not important | 
 | 2598 |     size_t size = mEffectChains.size(); | 
 | 2599 |     size_t i = 0; | 
 | 2600 |     for (i = 0; i < size; i++) { | 
 | 2601 |         if (mEffectChains[i]->sessionId() < session) { | 
 | 2602 |             break; | 
 | 2603 |         } | 
 | 2604 |     } | 
 | 2605 |     mEffectChains.insertAt(chain, i); | 
 | 2606 |     checkSuspendOnAddEffectChain_l(chain); | 
 | 2607 |  | 
 | 2608 |     return NO_ERROR; | 
 | 2609 | } | 
 | 2610 |  | 
 | 2611 | size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) | 
 | 2612 | { | 
 | 2613 |     int session = chain->sessionId(); | 
 | 2614 |  | 
 | 2615 |     ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); | 
 | 2616 |  | 
 | 2617 |     for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 2618 |         if (chain == mEffectChains[i]) { | 
 | 2619 |             mEffectChains.removeAt(i); | 
 | 2620 |             // detach all active tracks from the chain | 
 | 2621 |             for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { | 
 | 2622 |                 sp<Track> track = mActiveTracks[i].promote(); | 
 | 2623 |                 if (track == 0) { | 
 | 2624 |                     continue; | 
 | 2625 |                 } | 
 | 2626 |                 if (session == track->sessionId()) { | 
 | 2627 |                     ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", | 
 | 2628 |                             chain.get(), session); | 
 | 2629 |                     chain->decActiveTrackCnt(); | 
 | 2630 |                 } | 
 | 2631 |             } | 
 | 2632 |  | 
 | 2633 |             // detach all tracks with same session ID from this chain | 
 | 2634 |             for (size_t i = 0; i < mTracks.size(); ++i) { | 
 | 2635 |                 sp<Track> track = mTracks[i]; | 
 | 2636 |                 if (session == track->sessionId()) { | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2637 |                     track->setMainBuffer(reinterpret_cast<int16_t*>(mSinkBuffer)); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2638 |                     chain->decTrackCnt(); | 
 | 2639 |                 } | 
 | 2640 |             } | 
 | 2641 |             break; | 
 | 2642 |         } | 
 | 2643 |     } | 
 | 2644 |     return mEffectChains.size(); | 
 | 2645 | } | 
 | 2646 |  | 
 | 2647 | status_t AudioFlinger::PlaybackThread::attachAuxEffect( | 
 | 2648 |         const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) | 
 | 2649 | { | 
 | 2650 |     Mutex::Autolock _l(mLock); | 
 | 2651 |     return attachAuxEffect_l(track, EffectId); | 
 | 2652 | } | 
 | 2653 |  | 
 | 2654 | status_t AudioFlinger::PlaybackThread::attachAuxEffect_l( | 
 | 2655 |         const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) | 
 | 2656 | { | 
 | 2657 |     status_t status = NO_ERROR; | 
 | 2658 |  | 
 | 2659 |     if (EffectId == 0) { | 
 | 2660 |         track->setAuxBuffer(0, NULL); | 
 | 2661 |     } else { | 
 | 2662 |         // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX | 
 | 2663 |         sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); | 
 | 2664 |         if (effect != 0) { | 
 | 2665 |             if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
 | 2666 |                 track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); | 
 | 2667 |             } else { | 
 | 2668 |                 status = INVALID_OPERATION; | 
 | 2669 |             } | 
 | 2670 |         } else { | 
 | 2671 |             status = BAD_VALUE; | 
 | 2672 |         } | 
 | 2673 |     } | 
 | 2674 |     return status; | 
 | 2675 | } | 
 | 2676 |  | 
 | 2677 | void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) | 
 | 2678 | { | 
 | 2679 |     for (size_t i = 0; i < mTracks.size(); ++i) { | 
 | 2680 |         sp<Track> track = mTracks[i]; | 
 | 2681 |         if (track->auxEffectId() == effectId) { | 
 | 2682 |             attachAuxEffect_l(track, 0); | 
 | 2683 |         } | 
 | 2684 |     } | 
 | 2685 | } | 
 | 2686 |  | 
 | 2687 | bool AudioFlinger::PlaybackThread::threadLoop() | 
 | 2688 | { | 
 | 2689 |     Vector< sp<Track> > tracksToRemove; | 
 | 2690 |  | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2691 |     mStandbyTimeNs = systemTime(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2692 |  | 
 | 2693 |     // MIXER | 
 | 2694 |     nsecs_t lastWarning = 0; | 
 | 2695 |  | 
 | 2696 |     // DUPLICATING | 
 | 2697 |     // FIXME could this be made local to while loop? | 
 | 2698 |     writeFrames = 0; | 
 | 2699 |  | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2700 |     int lastGeneration = 0; | 
 | 2701 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2702 |     cacheParameters_l(); | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2703 |     mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2704 |  | 
 | 2705 |     if (mType == MIXER) { | 
 | 2706 |         sleepTimeShift = 0; | 
 | 2707 |     } | 
 | 2708 |  | 
 | 2709 |     CpuStats cpuStats; | 
 | 2710 |     const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); | 
 | 2711 |  | 
 | 2712 |     acquireWakeLock(); | 
 | 2713 |  | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 2714 |     // mNBLogWriter->log can only be called while thread mutex mLock is held. | 
 | 2715 |     // So if you need to log when mutex is unlocked, set logString to a non-NULL string, | 
 | 2716 |     // and then that string will be logged at the next convenient opportunity. | 
 | 2717 |     const char *logString = NULL; | 
 | 2718 |  | 
| Eric Laurent | 664539d | 2013-09-23 18:24:31 -0700 | [diff] [blame] | 2719 |     checkSilentMode_l(); | 
 | 2720 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2721 |     while (!exitPending()) | 
 | 2722 |     { | 
 | 2723 |         cpuStats.sample(myName); | 
 | 2724 |  | 
 | 2725 |         Vector< sp<EffectChain> > effectChains; | 
 | 2726 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2727 |         { // scope for mLock | 
 | 2728 |  | 
 | 2729 |             Mutex::Autolock _l(mLock); | 
 | 2730 |  | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 2731 |             processConfigEvents_l(); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 2732 |  | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 2733 |             if (logString != NULL) { | 
 | 2734 |                 mNBLogWriter->logTimestamp(); | 
 | 2735 |                 mNBLogWriter->log(logString); | 
 | 2736 |                 logString = NULL; | 
 | 2737 |             } | 
 | 2738 |  | 
| Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 2739 |             // Gather the framesReleased counters for all active tracks, | 
 | 2740 |             // and latch them atomically with the timestamp. | 
 | 2741 |             // FIXME We're using raw pointers as indices. A unique track ID would be a better index. | 
 | 2742 |             mLatchD.mFramesReleased.clear(); | 
 | 2743 |             size_t size = mActiveTracks.size(); | 
 | 2744 |             for (size_t i = 0; i < size; i++) { | 
 | 2745 |                 sp<Track> t = mActiveTracks[i].promote(); | 
 | 2746 |                 if (t != 0) { | 
 | 2747 |                     mLatchD.mFramesReleased.add(t.get(), | 
 | 2748 |                             t->mAudioTrackServerProxy->framesReleased()); | 
 | 2749 |                 } | 
 | 2750 |             } | 
| Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 2751 |             if (mLatchDValid) { | 
 | 2752 |                 mLatchQ = mLatchD; | 
 | 2753 |                 mLatchDValid = false; | 
 | 2754 |                 mLatchQValid = true; | 
 | 2755 |             } | 
 | 2756 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2757 |             saveOutputTracks(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2758 |             if (mSignalPending) { | 
 | 2759 |                 // A signal was raised while we were unlocked | 
 | 2760 |                 mSignalPending = false; | 
 | 2761 |             } else if (waitingAsyncCallback_l()) { | 
 | 2762 |                 if (exitPending()) { | 
 | 2763 |                     break; | 
 | 2764 |                 } | 
| Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 2765 |                 bool released = false; | 
 | 2766 |                 // The following works around a bug in the offload driver. Ideally we would release | 
 | 2767 |                 // the wake lock every time, but that causes the last offload buffer(s) to be | 
 | 2768 |                 // dropped while the device is on battery, so we need to hold a wake lock during | 
 | 2769 |                 // the drain phase. | 
 | 2770 |                 if (mBytesRemaining && !(mDrainSequence & 1)) { | 
 | 2771 |                     releaseWakeLock_l(); | 
 | 2772 |                     released = true; | 
 | 2773 |                 } | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2774 |                 mWakeLockUids.clear(); | 
 | 2775 |                 mActiveTracksGeneration++; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2776 |                 ALOGV("wait async completion"); | 
 | 2777 |                 mWaitWorkCV.wait(mLock); | 
 | 2778 |                 ALOGV("async completion/wake"); | 
| Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 2779 |                 if (released) { | 
 | 2780 |                     acquireWakeLock_l(); | 
 | 2781 |                 } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2782 |                 mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
 | 2783 |                 mSleepTimeUs = 0; | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2784 |  | 
 | 2785 |                 continue; | 
 | 2786 |             } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2787 |             if ((!mActiveTracks.size() && systemTime() > mStandbyTimeNs) || | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2788 |                                    isSuspended()) { | 
 | 2789 |                 // put audio hardware into standby after short delay | 
 | 2790 |                 if (shouldStandby_l()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2791 |  | 
 | 2792 |                     threadLoop_standby(); | 
 | 2793 |  | 
 | 2794 |                     mStandby = true; | 
 | 2795 |                 } | 
 | 2796 |  | 
 | 2797 |                 if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { | 
 | 2798 |                     // we're about to wait, flush the binder command buffer | 
 | 2799 |                     IPCThreadState::self()->flushCommands(); | 
 | 2800 |  | 
 | 2801 |                     clearOutputTracks(); | 
 | 2802 |  | 
 | 2803 |                     if (exitPending()) { | 
 | 2804 |                         break; | 
 | 2805 |                     } | 
 | 2806 |  | 
 | 2807 |                     releaseWakeLock_l(); | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2808 |                     mWakeLockUids.clear(); | 
 | 2809 |                     mActiveTracksGeneration++; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2810 |                     // wait until we have something to do... | 
 | 2811 |                     ALOGV("%s going to sleep", myName.string()); | 
 | 2812 |                     mWaitWorkCV.wait(mLock); | 
 | 2813 |                     ALOGV("%s waking up", myName.string()); | 
 | 2814 |                     acquireWakeLock_l(); | 
 | 2815 |  | 
 | 2816 |                     mMixerStatus = MIXER_IDLE; | 
 | 2817 |                     mMixerStatusIgnoringFastTracks = MIXER_IDLE; | 
 | 2818 |                     mBytesWritten = 0; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2819 |                     mBytesRemaining = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2820 |                     checkSilentMode_l(); | 
 | 2821 |  | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2822 |                     mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
 | 2823 |                     mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2824 |                     if (mType == MIXER) { | 
 | 2825 |                         sleepTimeShift = 0; | 
 | 2826 |                     } | 
 | 2827 |  | 
 | 2828 |                     continue; | 
 | 2829 |                 } | 
 | 2830 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2831 |             // mMixerStatusIgnoringFastTracks is also updated internally | 
 | 2832 |             mMixerStatus = prepareTracks_l(&tracksToRemove); | 
 | 2833 |  | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2834 |             // compare with previously applied list | 
 | 2835 |             if (lastGeneration != mActiveTracksGeneration) { | 
 | 2836 |                 // update wakelock | 
 | 2837 |                 updateWakeLockUids_l(mWakeLockUids); | 
 | 2838 |                 lastGeneration = mActiveTracksGeneration; | 
 | 2839 |             } | 
 | 2840 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2841 |             // prevent any changes in effect chain list and in each effect chain | 
 | 2842 |             // during mixing and effect process as the audio buffers could be deleted | 
 | 2843 |             // or modified if an effect is created or deleted | 
 | 2844 |             lockEffectChains_l(effectChains); | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2845 |         } // mLock scope ends | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2846 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2847 |         if (mBytesRemaining == 0) { | 
 | 2848 |             mCurrentWriteLength = 0; | 
 | 2849 |             if (mMixerStatus == MIXER_TRACKS_READY) { | 
 | 2850 |                 // threadLoop_mix() sets mCurrentWriteLength | 
 | 2851 |                 threadLoop_mix(); | 
 | 2852 |             } else if ((mMixerStatus != MIXER_DRAIN_TRACK) | 
 | 2853 |                         && (mMixerStatus != MIXER_DRAIN_ALL)) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2854 |                 // threadLoop_sleepTime sets mSleepTimeUs to 0 if data | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2855 |                 // must be written to HAL | 
 | 2856 |                 threadLoop_sleepTime(); | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2857 |                 if (mSleepTimeUs == 0) { | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2858 |                     mCurrentWriteLength = mSinkBufferSize; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2859 |                 } | 
 | 2860 |             } | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2861 |             // Either threadLoop_mix() or threadLoop_sleepTime() should have set | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2862 |             // mMixerBuffer with data if mMixerBufferValid is true and mSleepTimeUs == 0. | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2863 |             // Merge mMixerBuffer data into mEffectBuffer (if any effects are valid) | 
 | 2864 |             // or mSinkBuffer (if there are no effects). | 
 | 2865 |             // | 
 | 2866 |             // This is done pre-effects computation; if effects change to | 
 | 2867 |             // support higher precision, this needs to move. | 
 | 2868 |             // | 
 | 2869 |             // mMixerBufferValid is only set true by MixerThread::prepareTracks_l(). | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2870 |             // TODO use mSleepTimeUs == 0 as an additional condition. | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2871 |             if (mMixerBufferValid) { | 
 | 2872 |                 void *buffer = mEffectBufferValid ? mEffectBuffer : mSinkBuffer; | 
 | 2873 |                 audio_format_t format = mEffectBufferValid ? mEffectBufferFormat : mFormat; | 
 | 2874 |  | 
 | 2875 |                 memcpy_by_audio_format(buffer, format, mMixerBuffer, mMixerBufferFormat, | 
 | 2876 |                         mNormalFrameCount * mChannelCount); | 
 | 2877 |             } | 
 | 2878 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2879 |             mBytesRemaining = mCurrentWriteLength; | 
 | 2880 |             if (isSuspended()) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2881 |                 mSleepTimeUs = suspendSleepTimeUs(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2882 |                 // simulate write to HAL when suspended | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2883 |                 mBytesWritten += mSinkBufferSize; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2884 |                 mBytesRemaining = 0; | 
 | 2885 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2886 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2887 |             // only process effects if we're going to write | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2888 |             if (mSleepTimeUs == 0 && mType != OFFLOAD) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2889 |                 for (size_t i = 0; i < effectChains.size(); i ++) { | 
 | 2890 |                     effectChains[i]->process_l(); | 
 | 2891 |                 } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2892 |             } | 
 | 2893 |         } | 
| Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 2894 |         // Process effect chains for offloaded thread even if no audio | 
 | 2895 |         // was read from audio track: process only updates effect state | 
 | 2896 |         // and thus does have to be synchronized with audio writes but may have | 
 | 2897 |         // to be called while waiting for async write callback | 
 | 2898 |         if (mType == OFFLOAD) { | 
 | 2899 |             for (size_t i = 0; i < effectChains.size(); i ++) { | 
 | 2900 |                 effectChains[i]->process_l(); | 
 | 2901 |             } | 
 | 2902 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2903 |  | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2904 |         // Only if the Effects buffer is enabled and there is data in the | 
 | 2905 |         // Effects buffer (buffer valid), we need to | 
 | 2906 |         // copy into the sink buffer. | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2907 |         // TODO use mSleepTimeUs == 0 as an additional condition. | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2908 |         if (mEffectBufferValid) { | 
 | 2909 |             //ALOGV("writing effect buffer to sink buffer format %#x", mFormat); | 
 | 2910 |             memcpy_by_audio_format(mSinkBuffer, mFormat, mEffectBuffer, mEffectBufferFormat, | 
 | 2911 |                     mNormalFrameCount * mChannelCount); | 
 | 2912 |         } | 
 | 2913 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2914 |         // enable changes in effect chain | 
 | 2915 |         unlockEffectChains(effectChains); | 
 | 2916 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2917 |         if (!waitingAsyncCallback()) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2918 |             // mSleepTimeUs == 0 means we must write to audio hardware | 
 | 2919 |             if (mSleepTimeUs == 0) { | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2920 |                 ssize_t ret = 0; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2921 |                 if (mBytesRemaining) { | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2922 |                     ret = threadLoop_write(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2923 |                     if (ret < 0) { | 
 | 2924 |                         mBytesRemaining = 0; | 
 | 2925 |                     } else { | 
 | 2926 |                         mBytesWritten += ret; | 
 | 2927 |                         mBytesRemaining -= ret; | 
 | 2928 |                     } | 
 | 2929 |                 } else if ((mMixerStatus == MIXER_DRAIN_TRACK) || | 
 | 2930 |                         (mMixerStatus == MIXER_DRAIN_ALL)) { | 
 | 2931 |                     threadLoop_drain(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2932 |                 } | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2933 |                 if (mType == MIXER && !mStandby) { | 
| Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 2934 |                     // write blocked detection | 
 | 2935 |                     nsecs_t now = systemTime(); | 
 | 2936 |                     nsecs_t delta = now - mLastWriteTime; | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2937 |                     if (delta > maxPeriod) { | 
| Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 2938 |                         mNumDelayedWrites++; | 
 | 2939 |                         if ((now - lastWarning) > kWarningThrottleNs) { | 
 | 2940 |                             ATRACE_NAME("underrun"); | 
 | 2941 |                             ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p", | 
 | 2942 |                                     ns2ms(delta), mNumDelayedWrites, this); | 
 | 2943 |                             lastWarning = now; | 
 | 2944 |                         } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2945 |                     } | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2946 |  | 
 | 2947 |                     if (mThreadThrottle | 
 | 2948 |                             && mMixerStatus == MIXER_TRACKS_READY // we are mixing (active tracks) | 
 | 2949 |                             && ret > 0) {                         // we wrote something | 
 | 2950 |                         // Limit MixerThread data processing to no more than twice the | 
 | 2951 |                         // expected processing rate. | 
 | 2952 |                         // | 
 | 2953 |                         // This helps prevent underruns with NuPlayer and other applications | 
 | 2954 |                         // which may set up buffers that are close to the minimum size, or use | 
 | 2955 |                         // deep buffers, and rely on a double-buffering sleep strategy to fill. | 
 | 2956 |                         // | 
 | 2957 |                         // The throttle smooths out sudden large data drains from the device, | 
 | 2958 |                         // e.g. when it comes out of standby, which often causes problems with | 
 | 2959 |                         // (1) mixer threads without a fast mixer (which has its own warm-up) | 
 | 2960 |                         // (2) minimum buffer sized tracks (even if the track is full, | 
 | 2961 |                         //     the app won't fill fast enough to handle the sudden draw). | 
 | 2962 |  | 
 | 2963 |                         const int32_t deltaMs = delta / 1000000; | 
 | 2964 |                         const int32_t throttleMs = mHalfBufferMs - deltaMs; | 
 | 2965 |                         if ((signed)mHalfBufferMs >= throttleMs && throttleMs > 0) { | 
 | 2966 |                             usleep(throttleMs * 1000); | 
| Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 2967 |                             // notify of throttle start on verbose log | 
 | 2968 |                             ALOGV_IF(mThreadThrottleEndMs == mThreadThrottleTimeMs, | 
 | 2969 |                                     "mixer(%p) throttle begin:" | 
 | 2970 |                                     " ret(%zd) deltaMs(%d) requires sleep %d ms", | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2971 |                                     this, ret, deltaMs, throttleMs); | 
| Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 2972 |                             mThreadThrottleTimeMs += throttleMs; | 
 | 2973 |                         } else { | 
 | 2974 |                             uint32_t diff = mThreadThrottleTimeMs - mThreadThrottleEndMs; | 
 | 2975 |                             if (diff > 0) { | 
 | 2976 |                                 // notify of throttle end on debug log | 
 | 2977 |                                 ALOGD("mixer(%p) throttle end: throttle time(%u)", this, diff); | 
 | 2978 |                                 mThreadThrottleEndMs = mThreadThrottleTimeMs; | 
 | 2979 |                             } | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2980 |                         } | 
 | 2981 |                     } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2982 |                 } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2983 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2984 |             } else { | 
| Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 2985 |                 ATRACE_BEGIN("sleep"); | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2986 |                 usleep(mSleepTimeUs); | 
| Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 2987 |                 ATRACE_END(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2988 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2989 |         } | 
 | 2990 |  | 
 | 2991 |         // Finally let go of removed track(s), without the lock held | 
 | 2992 |         // since we can't guarantee the destructors won't acquire that | 
 | 2993 |         // same lock.  This will also mutate and push a new fast mixer state. | 
 | 2994 |         threadLoop_removeTracks(tracksToRemove); | 
 | 2995 |         tracksToRemove.clear(); | 
 | 2996 |  | 
 | 2997 |         // FIXME I don't understand the need for this here; | 
 | 2998 |         //       it was in the original code but maybe the | 
 | 2999 |         //       assignment in saveOutputTracks() makes this unnecessary? | 
 | 3000 |         clearOutputTracks(); | 
 | 3001 |  | 
 | 3002 |         // Effect chains will be actually deleted here if they were removed from | 
 | 3003 |         // mEffectChains list during mixing or effects processing | 
 | 3004 |         effectChains.clear(); | 
 | 3005 |  | 
 | 3006 |         // FIXME Note that the above .clear() is no longer necessary since effectChains | 
 | 3007 |         // is now local to this block, but will keep it for now (at least until merge done). | 
 | 3008 |     } | 
 | 3009 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3010 |     threadLoop_exit(); | 
 | 3011 |  | 
| Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 3012 |     if (!mStandby) { | 
 | 3013 |         threadLoop_standby(); | 
 | 3014 |         mStandby = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3015 |     } | 
 | 3016 |  | 
 | 3017 |     releaseWakeLock(); | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3018 |     mWakeLockUids.clear(); | 
 | 3019 |     mActiveTracksGeneration++; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3020 |  | 
 | 3021 |     ALOGV("Thread %p type %d exiting", this, mType); | 
 | 3022 |     return false; | 
 | 3023 | } | 
 | 3024 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3025 | // removeTracks_l() must be called with ThreadBase::mLock held | 
 | 3026 | void AudioFlinger::PlaybackThread::removeTracks_l(const Vector< sp<Track> >& tracksToRemove) | 
 | 3027 | { | 
 | 3028 |     size_t count = tracksToRemove.size(); | 
| Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 3029 |     if (count > 0) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3030 |         for (size_t i=0 ; i<count ; i++) { | 
 | 3031 |             const sp<Track>& track = tracksToRemove.itemAt(i); | 
 | 3032 |             mActiveTracks.remove(track); | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3033 |             mWakeLockUids.remove(track->uid()); | 
 | 3034 |             mActiveTracksGeneration++; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3035 |             ALOGV("removeTracks_l removing track on session %d", track->sessionId()); | 
 | 3036 |             sp<EffectChain> chain = getEffectChain_l(track->sessionId()); | 
 | 3037 |             if (chain != 0) { | 
 | 3038 |                 ALOGV("stopping track on chain %p for session Id: %d", chain.get(), | 
 | 3039 |                         track->sessionId()); | 
 | 3040 |                 chain->decActiveTrackCnt(); | 
 | 3041 |             } | 
 | 3042 |             if (track->isTerminated()) { | 
 | 3043 |                 removeTrack_l(track); | 
 | 3044 |             } | 
 | 3045 |         } | 
 | 3046 |     } | 
 | 3047 |  | 
 | 3048 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3049 |  | 
| Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3050 | status_t AudioFlinger::PlaybackThread::getTimestamp_l(AudioTimestamp& timestamp) | 
 | 3051 | { | 
 | 3052 |     if (mNormalSink != 0) { | 
 | 3053 |         return mNormalSink->getTimestamp(timestamp); | 
 | 3054 |     } | 
| Andy Hung | 9a1c889 | 2014-12-03 11:37:42 -0800 | [diff] [blame] | 3055 |     if ((mType == OFFLOAD || mType == DIRECT) | 
 | 3056 |             && mOutput != NULL && mOutput->stream->get_presentation_position) { | 
| Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3057 |         uint64_t position64; | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 3058 |         int ret = mOutput->getPresentationPosition(&position64, ×tamp.mTime); | 
| Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3059 |         if (ret == 0) { | 
 | 3060 |             timestamp.mPosition = (uint32_t)position64; | 
 | 3061 |             return NO_ERROR; | 
 | 3062 |         } | 
 | 3063 |     } | 
 | 3064 |     return INVALID_OPERATION; | 
 | 3065 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3066 |  | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3067 | status_t AudioFlinger::MixerThread::createAudioPatch_l(const struct audio_patch *patch, | 
 | 3068 |                                                           audio_patch_handle_t *handle) | 
 | 3069 | { | 
 | 3070 |     // if !&IDLE, holds the FastMixer state to restore after new parameters processed | 
 | 3071 |     FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE; | 
 | 3072 |     if (mFastMixer != 0) { | 
 | 3073 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 3074 |         FastMixerState *state = sq->begin(); | 
 | 3075 |         if (!(state->mCommand & FastMixerState::IDLE)) { | 
 | 3076 |             previousCommand = state->mCommand; | 
 | 3077 |             state->mCommand = FastMixerState::HOT_IDLE; | 
 | 3078 |             sq->end(); | 
 | 3079 |             sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); | 
 | 3080 |         } else { | 
 | 3081 |             sq->end(false /*didModify*/); | 
 | 3082 |         } | 
 | 3083 |     } | 
 | 3084 |     status_t status = PlaybackThread::createAudioPatch_l(patch, handle); | 
 | 3085 |  | 
 | 3086 |     if (!(previousCommand & FastMixerState::IDLE)) { | 
 | 3087 |         ALOG_ASSERT(mFastMixer != 0); | 
 | 3088 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 3089 |         FastMixerState *state = sq->begin(); | 
 | 3090 |         ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE); | 
 | 3091 |         state->mCommand = previousCommand; | 
 | 3092 |         sq->end(); | 
 | 3093 |         sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); | 
 | 3094 |     } | 
 | 3095 |  | 
 | 3096 |     return status; | 
 | 3097 | } | 
 | 3098 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3099 | status_t AudioFlinger::PlaybackThread::createAudioPatch_l(const struct audio_patch *patch, | 
 | 3100 |                                                           audio_patch_handle_t *handle) | 
 | 3101 | { | 
 | 3102 |     status_t status = NO_ERROR; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3103 |  | 
 | 3104 |     // store new device and send to effects | 
 | 3105 |     audio_devices_t type = AUDIO_DEVICE_NONE; | 
 | 3106 |     for (unsigned int i = 0; i < patch->num_sinks; i++) { | 
 | 3107 |         type |= patch->sinks[i].ext.device.type; | 
 | 3108 |     } | 
 | 3109 |  | 
 | 3110 | #ifdef ADD_BATTERY_DATA | 
 | 3111 |     // when changing the audio output device, call addBatteryData to notify | 
 | 3112 |     // the change | 
 | 3113 |     if (mOutDevice != type) { | 
 | 3114 |         uint32_t params = 0; | 
 | 3115 |         // check whether speaker is on | 
 | 3116 |         if (type & AUDIO_DEVICE_OUT_SPEAKER) { | 
 | 3117 |             params |= IMediaPlayerService::kBatteryDataSpeakerOn; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3118 |         } | 
 | 3119 |  | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3120 |         audio_devices_t deviceWithoutSpeaker | 
 | 3121 |             = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; | 
 | 3122 |         // check if any other device (except speaker) is on | 
 | 3123 |         if (type & deviceWithoutSpeaker) { | 
 | 3124 |             params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; | 
 | 3125 |         } | 
 | 3126 |  | 
 | 3127 |         if (params != 0) { | 
 | 3128 |             addBatteryData(params); | 
 | 3129 |         } | 
 | 3130 |     } | 
 | 3131 | #endif | 
 | 3132 |  | 
 | 3133 |     for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 3134 |         mEffectChains[i]->setDevice_l(type); | 
 | 3135 |     } | 
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 3136 |     bool configChanged = mOutDevice != type; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3137 |     mOutDevice = type; | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 3138 |     mPatch = *patch; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3139 |  | 
 | 3140 |     if (mOutput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3141 |         audio_hw_device_t *hwDevice = mOutput->audioHwDev->hwDevice(); | 
 | 3142 |         status = hwDevice->create_audio_patch(hwDevice, | 
 | 3143 |                                                patch->num_sources, | 
 | 3144 |                                                patch->sources, | 
 | 3145 |                                                patch->num_sinks, | 
 | 3146 |                                                patch->sinks, | 
 | 3147 |                                                handle); | 
 | 3148 |     } else { | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3149 |         char *address; | 
 | 3150 |         if (strcmp(patch->sinks[0].ext.device.address, "") != 0) { | 
 | 3151 |             //FIXME: we only support address on first sink with HAL version < 3.0 | 
 | 3152 |             address = audio_device_address_to_parameter( | 
 | 3153 |                                                         patch->sinks[0].ext.device.type, | 
 | 3154 |                                                         patch->sinks[0].ext.device.address); | 
 | 3155 |         } else { | 
 | 3156 |             address = (char *)calloc(1, 1); | 
 | 3157 |         } | 
 | 3158 |         AudioParameter param = AudioParameter(String8(address)); | 
 | 3159 |         free(address); | 
 | 3160 |         param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), (int)type); | 
 | 3161 |         status = mOutput->stream->common.set_parameters(&mOutput->stream->common, | 
 | 3162 |                 param.toString().string()); | 
 | 3163 |         *handle = AUDIO_PATCH_HANDLE_NONE; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3164 |     } | 
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 3165 |     if (configChanged) { | 
 | 3166 |         sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); | 
 | 3167 |     } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3168 |     return status; | 
 | 3169 | } | 
 | 3170 |  | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3171 | status_t AudioFlinger::MixerThread::releaseAudioPatch_l(const audio_patch_handle_t handle) | 
 | 3172 | { | 
 | 3173 |     // if !&IDLE, holds the FastMixer state to restore after new parameters processed | 
 | 3174 |     FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE; | 
 | 3175 |     if (mFastMixer != 0) { | 
 | 3176 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 3177 |         FastMixerState *state = sq->begin(); | 
 | 3178 |         if (!(state->mCommand & FastMixerState::IDLE)) { | 
 | 3179 |             previousCommand = state->mCommand; | 
 | 3180 |             state->mCommand = FastMixerState::HOT_IDLE; | 
 | 3181 |             sq->end(); | 
 | 3182 |             sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); | 
 | 3183 |         } else { | 
 | 3184 |             sq->end(false /*didModify*/); | 
 | 3185 |         } | 
 | 3186 |     } | 
 | 3187 |  | 
 | 3188 |     status_t status = PlaybackThread::releaseAudioPatch_l(handle); | 
 | 3189 |  | 
 | 3190 |     if (!(previousCommand & FastMixerState::IDLE)) { | 
 | 3191 |         ALOG_ASSERT(mFastMixer != 0); | 
 | 3192 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 3193 |         FastMixerState *state = sq->begin(); | 
 | 3194 |         ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE); | 
 | 3195 |         state->mCommand = previousCommand; | 
 | 3196 |         sq->end(); | 
 | 3197 |         sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); | 
 | 3198 |     } | 
 | 3199 |  | 
 | 3200 |     return status; | 
 | 3201 | } | 
 | 3202 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3203 | status_t AudioFlinger::PlaybackThread::releaseAudioPatch_l(const audio_patch_handle_t handle) | 
 | 3204 | { | 
 | 3205 |     status_t status = NO_ERROR; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3206 |  | 
 | 3207 |     mOutDevice = AUDIO_DEVICE_NONE; | 
 | 3208 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3209 |     if (mOutput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { | 
 | 3210 |         audio_hw_device_t *hwDevice = mOutput->audioHwDev->hwDevice(); | 
 | 3211 |         status = hwDevice->release_audio_patch(hwDevice, handle); | 
 | 3212 |     } else { | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3213 |         AudioParameter param; | 
 | 3214 |         param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), 0); | 
 | 3215 |         status = mOutput->stream->common.set_parameters(&mOutput->stream->common, | 
 | 3216 |                 param.toString().string()); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3217 |     } | 
 | 3218 |     return status; | 
 | 3219 | } | 
 | 3220 |  | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3221 | void AudioFlinger::PlaybackThread::addPatchTrack(const sp<PatchTrack>& track) | 
 | 3222 | { | 
 | 3223 |     Mutex::Autolock _l(mLock); | 
 | 3224 |     mTracks.add(track); | 
 | 3225 | } | 
 | 3226 |  | 
 | 3227 | void AudioFlinger::PlaybackThread::deletePatchTrack(const sp<PatchTrack>& track) | 
 | 3228 | { | 
 | 3229 |     Mutex::Autolock _l(mLock); | 
 | 3230 |     destroyTrack_l(track); | 
 | 3231 | } | 
 | 3232 |  | 
 | 3233 | void AudioFlinger::PlaybackThread::getAudioPortConfig(struct audio_port_config *config) | 
 | 3234 | { | 
 | 3235 |     ThreadBase::getAudioPortConfig(config); | 
 | 3236 |     config->role = AUDIO_PORT_ROLE_SOURCE; | 
 | 3237 |     config->ext.mix.hw_module = mOutput->audioHwDev->handle(); | 
 | 3238 |     config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; | 
 | 3239 | } | 
 | 3240 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3241 | // ---------------------------------------------------------------------------- | 
 | 3242 |  | 
 | 3243 | AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 3244 |         audio_io_handle_t id, audio_devices_t device, bool systemReady, type_t type) | 
 | 3245 |     :   PlaybackThread(audioFlinger, output, id, device, type, systemReady), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3246 |         // mAudioMixer below | 
 | 3247 |         // mFastMixer below | 
 | 3248 |         mFastMixerFutex(0) | 
 | 3249 |         // mOutputSink below | 
 | 3250 |         // mPipeSink below | 
 | 3251 |         // mNormalSink below | 
 | 3252 | { | 
 | 3253 |     ALOGV("MixerThread() id=%d device=%#x type=%d", id, device, type); | 
| Glenn Kasten | f6ed423 | 2013-07-16 11:16:27 -0700 | [diff] [blame] | 3254 |     ALOGV("mSampleRate=%u, mChannelMask=%#x, mChannelCount=%u, mFormat=%d, mFrameSize=%u, " | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3255 |             "mFrameCount=%d, mNormalFrameCount=%d", | 
 | 3256 |             mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount, | 
 | 3257 |             mNormalFrameCount); | 
 | 3258 |     mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); | 
 | 3259 |  | 
| Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 3260 |     if (type == DUPLICATING) { | 
 | 3261 |         // The Duplicating thread uses the AudioMixer and delivers data to OutputTracks | 
 | 3262 |         // (downstream MixerThreads) in DuplicatingThread::threadLoop_write(). | 
 | 3263 |         // Do not create or use mFastMixer, mOutputSink, mPipeSink, or mNormalSink. | 
 | 3264 |         return; | 
 | 3265 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3266 |     // create an NBAIO sink for the HAL output stream, and negotiate | 
 | 3267 |     mOutputSink = new AudioStreamOutSink(output->stream); | 
 | 3268 |     size_t numCounterOffers = 0; | 
| Glenn Kasten | f69f986 | 2014-03-07 08:37:57 -0800 | [diff] [blame] | 3269 |     const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3270 |     ssize_t index = mOutputSink->negotiate(offers, 1, NULL, numCounterOffers); | 
 | 3271 |     ALOG_ASSERT(index == 0); | 
 | 3272 |  | 
 | 3273 |     // initialize fast mixer depending on configuration | 
 | 3274 |     bool initFastMixer; | 
 | 3275 |     switch (kUseFastMixer) { | 
 | 3276 |     case FastMixer_Never: | 
 | 3277 |         initFastMixer = false; | 
 | 3278 |         break; | 
 | 3279 |     case FastMixer_Always: | 
 | 3280 |         initFastMixer = true; | 
 | 3281 |         break; | 
 | 3282 |     case FastMixer_Static: | 
 | 3283 |     case FastMixer_Dynamic: | 
 | 3284 |         initFastMixer = mFrameCount < mNormalFrameCount; | 
 | 3285 |         break; | 
 | 3286 |     } | 
 | 3287 |     if (initFastMixer) { | 
| Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3288 |         audio_format_t fastMixerFormat; | 
 | 3289 |         if (mMixerBufferEnabled && mEffectBufferEnabled) { | 
 | 3290 |             fastMixerFormat = AUDIO_FORMAT_PCM_FLOAT; | 
 | 3291 |         } else { | 
 | 3292 |             fastMixerFormat = AUDIO_FORMAT_PCM_16_BIT; | 
 | 3293 |         } | 
 | 3294 |         if (mFormat != fastMixerFormat) { | 
 | 3295 |             // change our Sink format to accept our intermediate precision | 
 | 3296 |             mFormat = fastMixerFormat; | 
 | 3297 |             free(mSinkBuffer); | 
 | 3298 |             mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); | 
 | 3299 |             const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; | 
 | 3300 |             (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); | 
 | 3301 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3302 |  | 
 | 3303 |         // create a MonoPipe to connect our submix to FastMixer | 
 | 3304 |         NBAIO_Format format = mOutputSink->format(); | 
| Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3305 |         NBAIO_Format origformat = format; | 
| Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3306 |         // adjust format to match that of the Fast Mixer | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 3307 |         ALOGV("format changed from %d to %d", format.mFormat, fastMixerFormat); | 
| Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3308 |         format.mFormat = fastMixerFormat; | 
 | 3309 |         format.mFrameSize = audio_bytes_per_sample(format.mFormat) * format.mChannelCount; | 
 | 3310 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3311 |         // This pipe depth compensates for scheduling latency of the normal mixer thread. | 
 | 3312 |         // When it wakes up after a maximum latency, it runs a few cycles quickly before | 
 | 3313 |         // finally blocking.  Note the pipe implementation rounds up the request to a power of 2. | 
 | 3314 |         MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/); | 
 | 3315 |         const NBAIO_Format offers[1] = {format}; | 
 | 3316 |         size_t numCounterOffers = 0; | 
 | 3317 |         ssize_t index = monoPipe->negotiate(offers, 1, NULL, numCounterOffers); | 
 | 3318 |         ALOG_ASSERT(index == 0); | 
 | 3319 |         monoPipe->setAvgFrames((mScreenState & 1) ? | 
 | 3320 |                 (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); | 
 | 3321 |         mPipeSink = monoPipe; | 
 | 3322 |  | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3323 | #ifdef TEE_SINK | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3324 |         if (mTeeSinkOutputEnabled) { | 
 | 3325 |             // 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] | 3326 |             Pipe *teeSink = new Pipe(mTeeSinkOutputFrames, origformat); | 
 | 3327 |             const NBAIO_Format offers2[1] = {origformat}; | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3328 |             numCounterOffers = 0; | 
| Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3329 |             index = teeSink->negotiate(offers2, 1, NULL, numCounterOffers); | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3330 |             ALOG_ASSERT(index == 0); | 
 | 3331 |             mTeeSink = teeSink; | 
 | 3332 |             PipeReader *teeSource = new PipeReader(*teeSink); | 
 | 3333 |             numCounterOffers = 0; | 
| Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3334 |             index = teeSource->negotiate(offers2, 1, NULL, numCounterOffers); | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3335 |             ALOG_ASSERT(index == 0); | 
 | 3336 |             mTeeSource = teeSource; | 
 | 3337 |         } | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3338 | #endif | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3339 |  | 
 | 3340 |         // create fast mixer and configure it initially with just one fast track for our submix | 
 | 3341 |         mFastMixer = new FastMixer(); | 
 | 3342 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 3343 | #ifdef STATE_QUEUE_DUMP | 
 | 3344 |         sq->setObserverDump(&mStateQueueObserverDump); | 
 | 3345 |         sq->setMutatorDump(&mStateQueueMutatorDump); | 
 | 3346 | #endif | 
 | 3347 |         FastMixerState *state = sq->begin(); | 
 | 3348 |         FastTrack *fastTrack = &state->mFastTracks[0]; | 
 | 3349 |         // wrap the source side of the MonoPipe to make it an AudioBufferProvider | 
 | 3350 |         fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe)); | 
 | 3351 |         fastTrack->mVolumeProvider = NULL; | 
| Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 3352 |         fastTrack->mChannelMask = mChannelMask; // mPipeSink channel mask for audio to FastMixer | 
 | 3353 |         fastTrack->mFormat = mFormat; // mPipeSink format for audio to FastMixer | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3354 |         fastTrack->mGeneration++; | 
 | 3355 |         state->mFastTracksGen++; | 
 | 3356 |         state->mTrackMask = 1; | 
 | 3357 |         // fast mixer will use the HAL output sink | 
 | 3358 |         state->mOutputSink = mOutputSink.get(); | 
 | 3359 |         state->mOutputSinkGen++; | 
 | 3360 |         state->mFrameCount = mFrameCount; | 
 | 3361 |         state->mCommand = FastMixerState::COLD_IDLE; | 
 | 3362 |         // already done in constructor initialization list | 
 | 3363 |         //mFastMixerFutex = 0; | 
 | 3364 |         state->mColdFutexAddr = &mFastMixerFutex; | 
 | 3365 |         state->mColdGen++; | 
 | 3366 |         state->mDumpState = &mFastMixerDumpState; | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3367 | #ifdef TEE_SINK | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3368 |         state->mTeeSink = mTeeSink.get(); | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3369 | #endif | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3370 |         mFastMixerNBLogWriter = audioFlinger->newWriter_l(kFastMixerLogSize, "FastMixer"); | 
 | 3371 |         state->mNBLogWriter = mFastMixerNBLogWriter.get(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3372 |         sq->end(); | 
 | 3373 |         sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); | 
 | 3374 |  | 
 | 3375 |         // start the fast mixer | 
 | 3376 |         mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO); | 
 | 3377 |         pid_t tid = mFastMixer->getTid(); | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 3378 |         sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3379 |  | 
 | 3380 | #ifdef AUDIO_WATCHDOG | 
 | 3381 |         // create and start the watchdog | 
 | 3382 |         mAudioWatchdog = new AudioWatchdog(); | 
 | 3383 |         mAudioWatchdog->setDump(&mAudioWatchdogDump); | 
 | 3384 |         mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO); | 
 | 3385 |         tid = mAudioWatchdog->getTid(); | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 3386 |         sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3387 | #endif | 
 | 3388 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3389 |     } | 
 | 3390 |  | 
 | 3391 |     switch (kUseFastMixer) { | 
 | 3392 |     case FastMixer_Never: | 
 | 3393 |     case FastMixer_Dynamic: | 
 | 3394 |         mNormalSink = mOutputSink; | 
 | 3395 |         break; | 
 | 3396 |     case FastMixer_Always: | 
 | 3397 |         mNormalSink = mPipeSink; | 
 | 3398 |         break; | 
 | 3399 |     case FastMixer_Static: | 
 | 3400 |         mNormalSink = initFastMixer ? mPipeSink : mOutputSink; | 
 | 3401 |         break; | 
 | 3402 |     } | 
 | 3403 | } | 
 | 3404 |  | 
 | 3405 | AudioFlinger::MixerThread::~MixerThread() | 
 | 3406 | { | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3407 |     if (mFastMixer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3408 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 3409 |         FastMixerState *state = sq->begin(); | 
 | 3410 |         if (state->mCommand == FastMixerState::COLD_IDLE) { | 
 | 3411 |             int32_t old = android_atomic_inc(&mFastMixerFutex); | 
 | 3412 |             if (old == -1) { | 
| Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 3413 |                 (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3414 |             } | 
 | 3415 |         } | 
 | 3416 |         state->mCommand = FastMixerState::EXIT; | 
 | 3417 |         sq->end(); | 
 | 3418 |         sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); | 
 | 3419 |         mFastMixer->join(); | 
 | 3420 |         // Though the fast mixer thread has exited, it's state queue is still valid. | 
 | 3421 |         // We'll use that extract the final state which contains one remaining fast track | 
 | 3422 |         // corresponding to our sub-mix. | 
 | 3423 |         state = sq->begin(); | 
 | 3424 |         ALOG_ASSERT(state->mTrackMask == 1); | 
 | 3425 |         FastTrack *fastTrack = &state->mFastTracks[0]; | 
 | 3426 |         ALOG_ASSERT(fastTrack->mBufferProvider != NULL); | 
 | 3427 |         delete fastTrack->mBufferProvider; | 
 | 3428 |         sq->end(false /*didModify*/); | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3429 |         mFastMixer.clear(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3430 | #ifdef AUDIO_WATCHDOG | 
 | 3431 |         if (mAudioWatchdog != 0) { | 
 | 3432 |             mAudioWatchdog->requestExit(); | 
 | 3433 |             mAudioWatchdog->requestExitAndWait(); | 
 | 3434 |             mAudioWatchdog.clear(); | 
 | 3435 |         } | 
 | 3436 | #endif | 
 | 3437 |     } | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3438 |     mAudioFlinger->unregisterWriter(mFastMixerNBLogWriter); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3439 |     delete mAudioMixer; | 
 | 3440 | } | 
 | 3441 |  | 
 | 3442 |  | 
 | 3443 | uint32_t AudioFlinger::MixerThread::correctLatency_l(uint32_t latency) const | 
 | 3444 | { | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3445 |     if (mFastMixer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3446 |         MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); | 
 | 3447 |         latency += (pipe->getAvgFrames() * 1000) / mSampleRate; | 
 | 3448 |     } | 
 | 3449 |     return latency; | 
 | 3450 | } | 
 | 3451 |  | 
 | 3452 |  | 
 | 3453 | void AudioFlinger::MixerThread::threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove) | 
 | 3454 | { | 
 | 3455 |     PlaybackThread::threadLoop_removeTracks(tracksToRemove); | 
 | 3456 | } | 
 | 3457 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3458 | ssize_t AudioFlinger::MixerThread::threadLoop_write() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3459 | { | 
 | 3460 |     // FIXME we should only do one push per cycle; confirm this is true | 
 | 3461 |     // Start the fast mixer if it's not already running | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3462 |     if (mFastMixer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3463 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 3464 |         FastMixerState *state = sq->begin(); | 
 | 3465 |         if (state->mCommand != FastMixerState::MIX_WRITE && | 
 | 3466 |                 (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) { | 
 | 3467 |             if (state->mCommand == FastMixerState::COLD_IDLE) { | 
 | 3468 |                 int32_t old = android_atomic_inc(&mFastMixerFutex); | 
 | 3469 |                 if (old == -1) { | 
| Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 3470 |                     (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3471 |                 } | 
 | 3472 | #ifdef AUDIO_WATCHDOG | 
 | 3473 |                 if (mAudioWatchdog != 0) { | 
 | 3474 |                     mAudioWatchdog->resume(); | 
 | 3475 |                 } | 
 | 3476 | #endif | 
 | 3477 |             } | 
 | 3478 |             state->mCommand = FastMixerState::MIX_WRITE; | 
| Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 3479 | #ifdef FAST_THREAD_STATISTICS | 
| Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 3480 |             mFastMixerDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? | 
| Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 3481 |                 FastThreadDumpState::kSamplingNforLowRamDevice : FastThreadDumpState::kSamplingN); | 
| Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 3482 | #endif | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3483 |             sq->end(); | 
 | 3484 |             sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); | 
 | 3485 |             if (kUseFastMixer == FastMixer_Dynamic) { | 
 | 3486 |                 mNormalSink = mPipeSink; | 
 | 3487 |             } | 
 | 3488 |         } else { | 
 | 3489 |             sq->end(false /*didModify*/); | 
 | 3490 |         } | 
 | 3491 |     } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3492 |     return PlaybackThread::threadLoop_write(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3493 | } | 
 | 3494 |  | 
 | 3495 | void AudioFlinger::MixerThread::threadLoop_standby() | 
 | 3496 | { | 
 | 3497 |     // Idle the fast mixer if it's currently running | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3498 |     if (mFastMixer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3499 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 3500 |         FastMixerState *state = sq->begin(); | 
 | 3501 |         if (!(state->mCommand & FastMixerState::IDLE)) { | 
 | 3502 |             state->mCommand = FastMixerState::COLD_IDLE; | 
 | 3503 |             state->mColdFutexAddr = &mFastMixerFutex; | 
 | 3504 |             state->mColdGen++; | 
 | 3505 |             mFastMixerFutex = 0; | 
 | 3506 |             sq->end(); | 
 | 3507 |             // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now | 
 | 3508 |             sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); | 
 | 3509 |             if (kUseFastMixer == FastMixer_Dynamic) { | 
 | 3510 |                 mNormalSink = mOutputSink; | 
 | 3511 |             } | 
 | 3512 | #ifdef AUDIO_WATCHDOG | 
 | 3513 |             if (mAudioWatchdog != 0) { | 
 | 3514 |                 mAudioWatchdog->pause(); | 
 | 3515 |             } | 
 | 3516 | #endif | 
 | 3517 |         } else { | 
 | 3518 |             sq->end(false /*didModify*/); | 
 | 3519 |         } | 
 | 3520 |     } | 
 | 3521 |     PlaybackThread::threadLoop_standby(); | 
 | 3522 | } | 
 | 3523 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3524 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback_l() | 
 | 3525 | { | 
 | 3526 |     return false; | 
 | 3527 | } | 
 | 3528 |  | 
 | 3529 | bool AudioFlinger::PlaybackThread::shouldStandby_l() | 
 | 3530 | { | 
 | 3531 |     return !mStandby; | 
 | 3532 | } | 
 | 3533 |  | 
 | 3534 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback() | 
 | 3535 | { | 
 | 3536 |     Mutex::Autolock _l(mLock); | 
 | 3537 |     return waitingAsyncCallback_l(); | 
 | 3538 | } | 
 | 3539 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3540 | // shared by MIXER and DIRECT, overridden by DUPLICATING | 
 | 3541 | void AudioFlinger::PlaybackThread::threadLoop_standby() | 
 | 3542 | { | 
 | 3543 |     ALOGV("Audio hardware entering standby, mixer %p, suspend count %d", this, mSuspended); | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 3544 |     mOutput->standby(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3545 |     if (mUseAsyncWrite != 0) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3546 |         // discard any pending drain or write ack by incrementing sequence | 
 | 3547 |         mWriteAckSequence = (mWriteAckSequence + 2) & ~1; | 
 | 3548 |         mDrainSequence = (mDrainSequence + 2) & ~1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3549 |         ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3550 |         mCallbackThread->setWriteBlocked(mWriteAckSequence); | 
 | 3551 |         mCallbackThread->setDraining(mDrainSequence); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3552 |     } | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 3553 |     mHwPaused = false; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3554 | } | 
 | 3555 |  | 
| Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 3556 | void AudioFlinger::PlaybackThread::onAddNewTrack_l() | 
 | 3557 | { | 
 | 3558 |     ALOGV("signal playback thread"); | 
 | 3559 |     broadcast_l(); | 
 | 3560 | } | 
 | 3561 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3562 | void AudioFlinger::MixerThread::threadLoop_mix() | 
 | 3563 | { | 
 | 3564 |     // obtain the presentation timestamp of the next output buffer | 
 | 3565 |     int64_t pts; | 
 | 3566 |     status_t status = INVALID_OPERATION; | 
 | 3567 |  | 
 | 3568 |     if (mNormalSink != 0) { | 
 | 3569 |         status = mNormalSink->getNextWriteTimestamp(&pts); | 
 | 3570 |     } else { | 
 | 3571 |         status = mOutputSink->getNextWriteTimestamp(&pts); | 
 | 3572 |     } | 
 | 3573 |  | 
 | 3574 |     if (status != NO_ERROR) { | 
 | 3575 |         pts = AudioBufferProvider::kInvalidPTS; | 
 | 3576 |     } | 
 | 3577 |  | 
 | 3578 |     // mix buffers... | 
 | 3579 |     mAudioMixer->process(pts); | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3580 |     mCurrentWriteLength = mSinkBufferSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3581 |     // increase sleep time progressively when application underrun condition clears. | 
 | 3582 |     // Only increase sleep time if the mixer is ready for two consecutive times to avoid | 
 | 3583 |     // that a steady state of alternating ready/not ready conditions keeps the sleep time | 
 | 3584 |     // such that we would underrun the audio HAL. | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3585 |     if ((mSleepTimeUs == 0) && (sleepTimeShift > 0)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3586 |         sleepTimeShift--; | 
 | 3587 |     } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3588 |     mSleepTimeUs = 0; | 
 | 3589 |     mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3590 |     //TODO: delay standby when effects have a tail | 
| Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 3591 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3592 | } | 
 | 3593 |  | 
 | 3594 | void AudioFlinger::MixerThread::threadLoop_sleepTime() | 
 | 3595 | { | 
 | 3596 |     // If no tracks are ready, sleep once for the duration of an output | 
 | 3597 |     // buffer size, then write 0s to the output | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3598 |     if (mSleepTimeUs == 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3599 |         if (mMixerStatus == MIXER_TRACKS_ENABLED) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3600 |             mSleepTimeUs = mActiveSleepTimeUs >> sleepTimeShift; | 
 | 3601 |             if (mSleepTimeUs < kMinThreadSleepTimeUs) { | 
 | 3602 |                 mSleepTimeUs = kMinThreadSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3603 |             } | 
 | 3604 |             // reduce sleep time in case of consecutive application underruns to avoid | 
 | 3605 |             // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer | 
 | 3606 |             // duration we would end up writing less data than needed by the audio HAL if | 
 | 3607 |             // the condition persists. | 
 | 3608 |             if (sleepTimeShift < kMaxThreadSleepTimeShift) { | 
 | 3609 |                 sleepTimeShift++; | 
 | 3610 |             } | 
 | 3611 |         } else { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3612 |             mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3613 |         } | 
 | 3614 |     } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) { | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3615 |         // clear out mMixerBuffer or mSinkBuffer, to ensure buffers are cleared | 
 | 3616 |         // before effects processing or output. | 
 | 3617 |         if (mMixerBufferValid) { | 
 | 3618 |             memset(mMixerBuffer, 0, mMixerBufferSize); | 
 | 3619 |         } else { | 
 | 3620 |             memset(mSinkBuffer, 0, mSinkBufferSize); | 
 | 3621 |         } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3622 |         mSleepTimeUs = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3623 |         ALOGV_IF(mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED), | 
 | 3624 |                 "anticipated start"); | 
 | 3625 |     } | 
 | 3626 |     // TODO add standby time extension fct of effect tail | 
 | 3627 | } | 
 | 3628 |  | 
 | 3629 | // prepareTracks_l() must be called with ThreadBase::mLock held | 
 | 3630 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l( | 
 | 3631 |         Vector< sp<Track> > *tracksToRemove) | 
 | 3632 | { | 
 | 3633 |  | 
 | 3634 |     mixer_state mixerStatus = MIXER_IDLE; | 
 | 3635 |     // find out which tracks need to be processed | 
 | 3636 |     size_t count = mActiveTracks.size(); | 
 | 3637 |     size_t mixedTracks = 0; | 
 | 3638 |     size_t tracksWithEffect = 0; | 
 | 3639 |     // counts only _active_ fast tracks | 
 | 3640 |     size_t fastTracks = 0; | 
 | 3641 |     uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset | 
 | 3642 |  | 
 | 3643 |     float masterVolume = mMasterVolume; | 
 | 3644 |     bool masterMute = mMasterMute; | 
 | 3645 |  | 
 | 3646 |     if (masterMute) { | 
 | 3647 |         masterVolume = 0; | 
 | 3648 |     } | 
 | 3649 |     // Delegate master volume control to effect in output mix effect chain if needed | 
 | 3650 |     sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); | 
 | 3651 |     if (chain != 0) { | 
 | 3652 |         uint32_t v = (uint32_t)(masterVolume * (1 << 24)); | 
 | 3653 |         chain->setVolume_l(&v, &v); | 
 | 3654 |         masterVolume = (float)((v + (1 << 23)) >> 24); | 
 | 3655 |         chain.clear(); | 
 | 3656 |     } | 
 | 3657 |  | 
 | 3658 |     // prepare a new state to push | 
 | 3659 |     FastMixerStateQueue *sq = NULL; | 
 | 3660 |     FastMixerState *state = NULL; | 
 | 3661 |     bool didModify = false; | 
 | 3662 |     FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED; | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3663 |     if (mFastMixer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3664 |         sq = mFastMixer->sq(); | 
 | 3665 |         state = sq->begin(); | 
 | 3666 |     } | 
 | 3667 |  | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3668 |     mMixerBufferValid = false;  // mMixerBuffer has no valid data until appropriate tracks found. | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3669 |     mEffectBufferValid = false; // mEffectBuffer has no valid data until tracks found. | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3670 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3671 |     for (size_t i=0 ; i<count ; i++) { | 
| Glenn Kasten | 9fdcb0a | 2013-06-26 16:11:36 -0700 | [diff] [blame] | 3672 |         const sp<Track> t = mActiveTracks[i].promote(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3673 |         if (t == 0) { | 
 | 3674 |             continue; | 
 | 3675 |         } | 
 | 3676 |  | 
 | 3677 |         // this const just means the local variable doesn't change | 
 | 3678 |         Track* const track = t.get(); | 
 | 3679 |  | 
 | 3680 |         // process fast tracks | 
 | 3681 |         if (track->isFastTrack()) { | 
 | 3682 |  | 
 | 3683 |             // It's theoretically possible (though unlikely) for a fast track to be created | 
 | 3684 |             // and then removed within the same normal mix cycle.  This is not a problem, as | 
 | 3685 |             // the track never becomes active so it's fast mixer slot is never touched. | 
 | 3686 |             // The converse, of removing an (active) track and then creating a new track | 
 | 3687 |             // at the identical fast mixer slot within the same normal mix cycle, | 
 | 3688 |             // is impossible because the slot isn't marked available until the end of each cycle. | 
 | 3689 |             int j = track->mFastIndex; | 
 | 3690 |             ALOG_ASSERT(0 < j && j < (int)FastMixerState::kMaxFastTracks); | 
 | 3691 |             ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j))); | 
 | 3692 |             FastTrack *fastTrack = &state->mFastTracks[j]; | 
 | 3693 |  | 
 | 3694 |             // Determine whether the track is currently in underrun condition, | 
 | 3695 |             // and whether it had a recent underrun. | 
 | 3696 |             FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j]; | 
 | 3697 |             FastTrackUnderruns underruns = ftDump->mUnderruns; | 
 | 3698 |             uint32_t recentFull = (underruns.mBitFields.mFull - | 
 | 3699 |                     track->mObservedUnderruns.mBitFields.mFull) & UNDERRUN_MASK; | 
 | 3700 |             uint32_t recentPartial = (underruns.mBitFields.mPartial - | 
 | 3701 |                     track->mObservedUnderruns.mBitFields.mPartial) & UNDERRUN_MASK; | 
 | 3702 |             uint32_t recentEmpty = (underruns.mBitFields.mEmpty - | 
 | 3703 |                     track->mObservedUnderruns.mBitFields.mEmpty) & UNDERRUN_MASK; | 
 | 3704 |             uint32_t recentUnderruns = recentPartial + recentEmpty; | 
 | 3705 |             track->mObservedUnderruns = underruns; | 
 | 3706 |             // don't count underruns that occur while stopping or pausing | 
 | 3707 |             // or stopped which can occur when flush() is called while active | 
| Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 3708 |             if (!(track->isStopping() || track->isPausing() || track->isStopped()) && | 
 | 3709 |                     recentUnderruns > 0) { | 
 | 3710 |                 // FIXME fast mixer will pull & mix partial buffers, but we count as a full underrun | 
 | 3711 |                 track->mAudioTrackServerProxy->tallyUnderrunFrames(recentUnderruns * mFrameCount); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3712 |             } | 
 | 3713 |  | 
 | 3714 |             // This is similar to the state machine for normal tracks, | 
 | 3715 |             // with a few modifications for fast tracks. | 
 | 3716 |             bool isActive = true; | 
 | 3717 |             switch (track->mState) { | 
 | 3718 |             case TrackBase::STOPPING_1: | 
 | 3719 |                 // track stays active in STOPPING_1 state until first underrun | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3720 |                 if (recentUnderruns > 0 || track->isTerminated()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3721 |                     track->mState = TrackBase::STOPPING_2; | 
 | 3722 |                 } | 
 | 3723 |                 break; | 
 | 3724 |             case TrackBase::PAUSING: | 
 | 3725 |                 // ramp down is not yet implemented | 
 | 3726 |                 track->setPaused(); | 
 | 3727 |                 break; | 
 | 3728 |             case TrackBase::RESUMING: | 
 | 3729 |                 // ramp up is not yet implemented | 
 | 3730 |                 track->mState = TrackBase::ACTIVE; | 
 | 3731 |                 break; | 
 | 3732 |             case TrackBase::ACTIVE: | 
 | 3733 |                 if (recentFull > 0 || recentPartial > 0) { | 
 | 3734 |                     // track has provided at least some frames recently: reset retry count | 
 | 3735 |                     track->mRetryCount = kMaxTrackRetries; | 
 | 3736 |                 } | 
 | 3737 |                 if (recentUnderruns == 0) { | 
 | 3738 |                     // no recent underruns: stay active | 
 | 3739 |                     break; | 
 | 3740 |                 } | 
 | 3741 |                 // there has recently been an underrun of some kind | 
 | 3742 |                 if (track->sharedBuffer() == 0) { | 
 | 3743 |                     // were any of the recent underruns "empty" (no frames available)? | 
 | 3744 |                     if (recentEmpty == 0) { | 
 | 3745 |                         // no, then ignore the partial underruns as they are allowed indefinitely | 
 | 3746 |                         break; | 
 | 3747 |                     } | 
 | 3748 |                     // there has recently been an "empty" underrun: decrement the retry counter | 
 | 3749 |                     if (--(track->mRetryCount) > 0) { | 
 | 3750 |                         break; | 
 | 3751 |                     } | 
 | 3752 |                     // indicate to client process that the track was disabled because of underrun; | 
 | 3753 |                     // it will then automatically call start() when data is available | 
| Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 3754 |                     android_atomic_or(CBLK_DISABLED, &track->mCblk->mFlags); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3755 |                     // remove from active list, but state remains ACTIVE [confusing but true] | 
 | 3756 |                     isActive = false; | 
 | 3757 |                     break; | 
 | 3758 |                 } | 
 | 3759 |                 // fall through | 
 | 3760 |             case TrackBase::STOPPING_2: | 
 | 3761 |             case TrackBase::PAUSED: | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3762 |             case TrackBase::STOPPED: | 
 | 3763 |             case TrackBase::FLUSHED:   // flush() while active | 
 | 3764 |                 // Check for presentation complete if track is inactive | 
 | 3765 |                 // We have consumed all the buffers of this track. | 
 | 3766 |                 // This would be incomplete if we auto-paused on underrun | 
 | 3767 |                 { | 
 | 3768 |                     size_t audioHALFrames = | 
 | 3769 |                             (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; | 
 | 3770 |                     size_t framesWritten = mBytesWritten / mFrameSize; | 
 | 3771 |                     if (!(mStandby || track->presentationComplete(framesWritten, audioHALFrames))) { | 
 | 3772 |                         // track stays in active list until presentation is complete | 
 | 3773 |                         break; | 
 | 3774 |                     } | 
 | 3775 |                 } | 
 | 3776 |                 if (track->isStopping_2()) { | 
 | 3777 |                     track->mState = TrackBase::STOPPED; | 
 | 3778 |                 } | 
 | 3779 |                 if (track->isStopped()) { | 
 | 3780 |                     // Can't reset directly, as fast mixer is still polling this track | 
 | 3781 |                     //   track->reset(); | 
 | 3782 |                     // So instead mark this track as needing to be reset after push with ack | 
 | 3783 |                     resetMask |= 1 << i; | 
 | 3784 |                 } | 
 | 3785 |                 isActive = false; | 
 | 3786 |                 break; | 
 | 3787 |             case TrackBase::IDLE: | 
 | 3788 |             default: | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 3789 |                 LOG_ALWAYS_FATAL("unexpected track state %d", track->mState); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3790 |             } | 
 | 3791 |  | 
 | 3792 |             if (isActive) { | 
 | 3793 |                 // was it previously inactive? | 
 | 3794 |                 if (!(state->mTrackMask & (1 << j))) { | 
 | 3795 |                     ExtendedAudioBufferProvider *eabp = track; | 
 | 3796 |                     VolumeProvider *vp = track; | 
 | 3797 |                     fastTrack->mBufferProvider = eabp; | 
 | 3798 |                     fastTrack->mVolumeProvider = vp; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3799 |                     fastTrack->mChannelMask = track->mChannelMask; | 
| Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 3800 |                     fastTrack->mFormat = track->mFormat; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3801 |                     fastTrack->mGeneration++; | 
 | 3802 |                     state->mTrackMask |= 1 << j; | 
 | 3803 |                     didModify = true; | 
 | 3804 |                     // no acknowledgement required for newly active tracks | 
 | 3805 |                 } | 
 | 3806 |                 // cache the combined master volume and stream type volume for fast mixer; this | 
 | 3807 |                 // lacks any synchronization or barrier so VolumeProvider may read a stale value | 
| Glenn Kasten | e4756fe | 2012-11-29 13:38:14 -0800 | [diff] [blame] | 3808 |                 track->mCachedVolume = masterVolume * mStreamTypes[track->streamType()].volume; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3809 |                 ++fastTracks; | 
 | 3810 |             } else { | 
 | 3811 |                 // was it previously active? | 
 | 3812 |                 if (state->mTrackMask & (1 << j)) { | 
 | 3813 |                     fastTrack->mBufferProvider = NULL; | 
 | 3814 |                     fastTrack->mGeneration++; | 
 | 3815 |                     state->mTrackMask &= ~(1 << j); | 
 | 3816 |                     didModify = true; | 
 | 3817 |                     // If any fast tracks were removed, we must wait for acknowledgement | 
 | 3818 |                     // because we're about to decrement the last sp<> on those tracks. | 
 | 3819 |                     block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; | 
 | 3820 |                 } else { | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 3821 |                     LOG_ALWAYS_FATAL("fast track %d should have been active", j); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3822 |                 } | 
 | 3823 |                 tracksToRemove->add(track); | 
 | 3824 |                 // Avoids a misleading display in dumpsys | 
 | 3825 |                 track->mObservedUnderruns.mBitFields.mMostRecent = UNDERRUN_FULL; | 
 | 3826 |             } | 
 | 3827 |             continue; | 
 | 3828 |         } | 
 | 3829 |  | 
 | 3830 |         {   // local variable scope to avoid goto warning | 
 | 3831 |  | 
 | 3832 |         audio_track_cblk_t* cblk = track->cblk(); | 
 | 3833 |  | 
 | 3834 |         // The first time a track is added we wait | 
 | 3835 |         // for all its buffers to be filled before processing it | 
 | 3836 |         int name = track->name(); | 
 | 3837 |         // make sure that we have enough frames to mix one full buffer. | 
 | 3838 |         // enforce this condition only once to enable draining the buffer in case the client | 
 | 3839 |         // app does not call stop() and relies on underrun to stop: | 
 | 3840 |         // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed | 
 | 3841 |         // during last round | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3842 |         size_t desiredFrames; | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 3843 |         const uint32_t sampleRate = track->mAudioTrackServerProxy->getSampleRate(); | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 3844 |         AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate(); | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 3845 |  | 
 | 3846 |         desiredFrames = sourceFramesNeededWithTimestretch( | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 3847 |                 sampleRate, mNormalFrameCount, mSampleRate, playbackRate.mSpeed); | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 3848 |         // TODO: ONLY USED FOR LEGACY RESAMPLERS, remove when they are removed. | 
 | 3849 |         // add frames already consumed but not yet released by the resampler | 
 | 3850 |         // because mAudioTrackServerProxy->framesReady() will include these frames | 
 | 3851 |         desiredFrames += mAudioMixer->getUnreleasedFrames(track->name()); | 
 | 3852 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3853 |         uint32_t minFrames = 1; | 
 | 3854 |         if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() && | 
 | 3855 |                 (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) { | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3856 |             minFrames = desiredFrames; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3857 |         } | 
| Eric Laurent | 13e4c96 | 2013-12-20 17:36:01 -0800 | [diff] [blame] | 3858 |  | 
 | 3859 |         size_t framesReady = track->framesReady(); | 
| Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 3860 |         if (ATRACE_ENABLED()) { | 
 | 3861 |             // I wish we had formatted trace names | 
 | 3862 |             char traceName[16]; | 
 | 3863 |             strcpy(traceName, "nRdy"); | 
 | 3864 |             int name = track->name(); | 
 | 3865 |             if (AudioMixer::TRACK0 <= name && | 
 | 3866 |                     name < (int) (AudioMixer::TRACK0 + AudioMixer::MAX_NUM_TRACKS)) { | 
 | 3867 |                 name -= AudioMixer::TRACK0; | 
 | 3868 |                 traceName[4] = (name / 10) + '0'; | 
 | 3869 |                 traceName[5] = (name % 10) + '0'; | 
 | 3870 |             } else { | 
 | 3871 |                 traceName[4] = '?'; | 
 | 3872 |                 traceName[5] = '?'; | 
 | 3873 |             } | 
 | 3874 |             traceName[6] = '\0'; | 
 | 3875 |             ATRACE_INT(traceName, framesReady); | 
 | 3876 |         } | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3877 |         if ((framesReady >= minFrames) && track->isReady() && | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3878 |                 !track->isPaused() && !track->isTerminated()) | 
 | 3879 |         { | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 3880 |             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] | 3881 |  | 
 | 3882 |             mixedTracks++; | 
 | 3883 |  | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3884 |             // track->mainBuffer() != mSinkBuffer or mMixerBuffer means | 
 | 3885 |             // there is an effect chain connected to the track | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3886 |             chain.clear(); | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3887 |             if (track->mainBuffer() != mSinkBuffer && | 
 | 3888 |                     track->mainBuffer() != mMixerBuffer) { | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3889 |                 if (mEffectBufferEnabled) { | 
 | 3890 |                     mEffectBufferValid = true; // Later can set directly. | 
 | 3891 |                 } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3892 |                 chain = getEffectChain_l(track->sessionId()); | 
 | 3893 |                 // Delegate volume control to effect in track effect chain if needed | 
 | 3894 |                 if (chain != 0) { | 
 | 3895 |                     tracksWithEffect++; | 
 | 3896 |                 } else { | 
 | 3897 |                     ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on " | 
 | 3898 |                             "session %d", | 
 | 3899 |                             name, track->sessionId()); | 
 | 3900 |                 } | 
 | 3901 |             } | 
 | 3902 |  | 
 | 3903 |  | 
 | 3904 |             int param = AudioMixer::VOLUME; | 
 | 3905 |             if (track->mFillingUpStatus == Track::FS_FILLED) { | 
 | 3906 |                 // no ramp for the first volume setting | 
 | 3907 |                 track->mFillingUpStatus = Track::FS_ACTIVE; | 
 | 3908 |                 if (track->mState == TrackBase::RESUMING) { | 
 | 3909 |                     track->mState = TrackBase::ACTIVE; | 
 | 3910 |                     param = AudioMixer::RAMP_VOLUME; | 
 | 3911 |                 } | 
 | 3912 |                 mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 3913 |             // FIXME should not make a decision based on mServer | 
 | 3914 |             } else if (cblk->mServer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3915 |                 // If the track is stopped before the first frame was mixed, | 
 | 3916 |                 // do not apply ramp | 
 | 3917 |                 param = AudioMixer::RAMP_VOLUME; | 
 | 3918 |             } | 
 | 3919 |  | 
 | 3920 |             // compute volume for this track | 
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3921 |             uint32_t vl, vr;       // in U8.24 integer format | 
 | 3922 |             float vlf, vrf, vaf;   // in [0.0, 1.0] float format | 
| Glenn Kasten | e4756fe | 2012-11-29 13:38:14 -0800 | [diff] [blame] | 3923 |             if (track->isPausing() || mStreamTypes[track->streamType()].mute) { | 
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3924 |                 vl = vr = 0; | 
 | 3925 |                 vlf = vrf = vaf = 0.; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3926 |                 if (track->isPausing()) { | 
 | 3927 |                     track->setPaused(); | 
 | 3928 |                 } | 
 | 3929 |             } else { | 
 | 3930 |  | 
 | 3931 |                 // read original volumes with volume control | 
 | 3932 |                 float typeVolume = mStreamTypes[track->streamType()].volume; | 
 | 3933 |                 float v = masterVolume * typeVolume; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3934 |                 AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy; | 
| Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 3935 |                 gain_minifloat_packed_t vlr = proxy->getVolumeLR(); | 
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3936 |                 vlf = float_from_gain(gain_minifloat_unpack_left(vlr)); | 
 | 3937 |                 vrf = float_from_gain(gain_minifloat_unpack_right(vlr)); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3938 |                 // 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] | 3939 |                 if (vlf > GAIN_FLOAT_UNITY) { | 
 | 3940 |                     ALOGV("Track left volume out of range: %.3g", vlf); | 
 | 3941 |                     vlf = GAIN_FLOAT_UNITY; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3942 |                 } | 
| Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 3943 |                 if (vrf > GAIN_FLOAT_UNITY) { | 
 | 3944 |                     ALOGV("Track right volume out of range: %.3g", vrf); | 
 | 3945 |                     vrf = GAIN_FLOAT_UNITY; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3946 |                 } | 
 | 3947 |                 // now apply the master volume and stream type volume | 
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3948 |                 vlf *= v; | 
 | 3949 |                 vrf *= v; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3950 |                 // 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] | 3951 |                 // then derive vl and vr as U8.24 versions for the effect chain | 
 | 3952 |                 const float scaleto8_24 = MAX_GAIN_INT * MAX_GAIN_INT; | 
 | 3953 |                 vl = (uint32_t) (scaleto8_24 * vlf); | 
 | 3954 |                 vr = (uint32_t) (scaleto8_24 * vrf); | 
 | 3955 |                 // vl and vr are now in U8.24 format | 
| Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 3956 |                 uint16_t sendLevel = proxy->getSendLevel_U4_12(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3957 |                 // send level comes from shared memory and so may be corrupt | 
 | 3958 |                 if (sendLevel > MAX_GAIN_INT) { | 
 | 3959 |                     ALOGV("Track send level out of range: %04X", sendLevel); | 
 | 3960 |                     sendLevel = MAX_GAIN_INT; | 
 | 3961 |                 } | 
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3962 |                 // vaf is represented as [0.0, 1.0] float by rescaling sendLevel | 
 | 3963 |                 vaf = v * sendLevel * (1. / MAX_GAIN_INT); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3964 |             } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3965 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3966 |             // Delegate volume control to effect in track effect chain if needed | 
 | 3967 |             if (chain != 0 && chain->setVolume_l(&vl, &vr)) { | 
 | 3968 |                 // Do not ramp volume if volume is controlled by effect | 
 | 3969 |                 param = AudioMixer::VOLUME; | 
| Bryant Liu | b6be7f2 | 2014-06-12 22:02:41 +0800 | [diff] [blame] | 3970 |                 // Update remaining floating point volume levels | 
 | 3971 |                 vlf = (float)vl / (1 << 24); | 
 | 3972 |                 vrf = (float)vr / (1 << 24); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3973 |                 track->mHasVolumeController = true; | 
 | 3974 |             } else { | 
 | 3975 |                 // force no volume ramp when volume controller was just disabled or removed | 
 | 3976 |                 // from effect chain to avoid volume spike | 
 | 3977 |                 if (track->mHasVolumeController) { | 
 | 3978 |                     param = AudioMixer::VOLUME; | 
 | 3979 |                 } | 
 | 3980 |                 track->mHasVolumeController = false; | 
 | 3981 |             } | 
 | 3982 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3983 |             // XXX: these things DON'T need to be done each time | 
 | 3984 |             mAudioMixer->setBufferProvider(name, track); | 
 | 3985 |             mAudioMixer->enable(name); | 
 | 3986 |  | 
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3987 |             mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, &vlf); | 
 | 3988 |             mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, &vrf); | 
 | 3989 |             mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, &vaf); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3990 |             mAudioMixer->setParameter( | 
 | 3991 |                 name, | 
 | 3992 |                 AudioMixer::TRACK, | 
 | 3993 |                 AudioMixer::FORMAT, (void *)track->format()); | 
 | 3994 |             mAudioMixer->setParameter( | 
 | 3995 |                 name, | 
 | 3996 |                 AudioMixer::TRACK, | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 3997 |                 AudioMixer::CHANNEL_MASK, (void *)(uintptr_t)track->channelMask()); | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 3998 |             mAudioMixer->setParameter( | 
 | 3999 |                 name, | 
 | 4000 |                 AudioMixer::TRACK, | 
 | 4001 |                 AudioMixer::MIXER_CHANNEL_MASK, (void *)(uintptr_t)mChannelMask); | 
| Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4002 |             // 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] | 4003 |             uint32_t maxSampleRate = mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4004 |             uint32_t reqSampleRate = track->mAudioTrackServerProxy->getSampleRate(); | 
| Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4005 |             if (reqSampleRate == 0) { | 
 | 4006 |                 reqSampleRate = mSampleRate; | 
 | 4007 |             } else if (reqSampleRate > maxSampleRate) { | 
 | 4008 |                 reqSampleRate = maxSampleRate; | 
 | 4009 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4010 |             mAudioMixer->setParameter( | 
 | 4011 |                 name, | 
 | 4012 |                 AudioMixer::RESAMPLE, | 
 | 4013 |                 AudioMixer::SAMPLE_RATE, | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 4014 |                 (void *)(uintptr_t)reqSampleRate); | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4015 |  | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4016 |             AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate(); | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4017 |             mAudioMixer->setParameter( | 
 | 4018 |                 name, | 
 | 4019 |                 AudioMixer::TIMESTRETCH, | 
 | 4020 |                 AudioMixer::PLAYBACK_RATE, | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4021 |                 &playbackRate); | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4022 |  | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4023 |             /* | 
 | 4024 |              * Select the appropriate output buffer for the track. | 
 | 4025 |              * | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4026 |              * Tracks with effects go into their own effects chain buffer | 
 | 4027 |              * and from there into either mEffectBuffer or mSinkBuffer. | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4028 |              * | 
 | 4029 |              * Other tracks can use mMixerBuffer for higher precision | 
 | 4030 |              * channel accumulation.  If this buffer is enabled | 
 | 4031 |              * (mMixerBufferEnabled true), then selected tracks will accumulate | 
 | 4032 |              * into it. | 
 | 4033 |              * | 
 | 4034 |              */ | 
 | 4035 |             if (mMixerBufferEnabled | 
 | 4036 |                     && (track->mainBuffer() == mSinkBuffer | 
 | 4037 |                             || track->mainBuffer() == mMixerBuffer)) { | 
 | 4038 |                 mAudioMixer->setParameter( | 
 | 4039 |                         name, | 
 | 4040 |                         AudioMixer::TRACK, | 
| Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 4041 |                         AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat); | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4042 |                 mAudioMixer->setParameter( | 
 | 4043 |                         name, | 
 | 4044 |                         AudioMixer::TRACK, | 
 | 4045 |                         AudioMixer::MAIN_BUFFER, (void *)mMixerBuffer); | 
 | 4046 |                 // TODO: override track->mainBuffer()? | 
 | 4047 |                 mMixerBufferValid = true; | 
 | 4048 |             } else { | 
 | 4049 |                 mAudioMixer->setParameter( | 
 | 4050 |                         name, | 
 | 4051 |                         AudioMixer::TRACK, | 
| Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 4052 |                         AudioMixer::MIXER_FORMAT, (void *)AUDIO_FORMAT_PCM_16_BIT); | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4053 |                 mAudioMixer->setParameter( | 
 | 4054 |                         name, | 
 | 4055 |                         AudioMixer::TRACK, | 
 | 4056 |                         AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); | 
 | 4057 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4058 |             mAudioMixer->setParameter( | 
 | 4059 |                 name, | 
 | 4060 |                 AudioMixer::TRACK, | 
 | 4061 |                 AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); | 
 | 4062 |  | 
 | 4063 |             // reset retry count | 
 | 4064 |             track->mRetryCount = kMaxTrackRetries; | 
 | 4065 |  | 
 | 4066 |             // If one track is ready, set the mixer ready if: | 
 | 4067 |             //  - the mixer was not ready during previous round OR | 
 | 4068 |             //  - no other track is not ready | 
 | 4069 |             if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY || | 
 | 4070 |                     mixerStatus != MIXER_TRACKS_ENABLED) { | 
 | 4071 |                 mixerStatus = MIXER_TRACKS_READY; | 
 | 4072 |             } | 
 | 4073 |         } else { | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4074 |             if (framesReady < desiredFrames && !track->isStopped() && !track->isPaused()) { | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4075 |                 ALOGV("track(%p) underrun,  framesReady(%zu) < framesDesired(%zd)", | 
 | 4076 |                         track, framesReady, desiredFrames); | 
| Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 4077 |                 track->mAudioTrackServerProxy->tallyUnderrunFrames(desiredFrames); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4078 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4079 |             // clear effect chain input buffer if an active track underruns to avoid sending | 
 | 4080 |             // previous audio buffer again to effects | 
 | 4081 |             chain = getEffectChain_l(track->sessionId()); | 
 | 4082 |             if (chain != 0) { | 
 | 4083 |                 chain->clearInputBuffer(); | 
 | 4084 |             } | 
 | 4085 |  | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4086 |             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] | 4087 |             if ((track->sharedBuffer() != 0) || track->isTerminated() || | 
 | 4088 |                     track->isStopped() || track->isPaused()) { | 
 | 4089 |                 // We have consumed all the buffers of this track. | 
 | 4090 |                 // Remove it from the list of active tracks. | 
 | 4091 |                 // TODO: use actual buffer filling status instead of latency when available from | 
 | 4092 |                 // audio HAL | 
 | 4093 |                 size_t audioHALFrames = (latency_l() * mSampleRate) / 1000; | 
 | 4094 |                 size_t framesWritten = mBytesWritten / mFrameSize; | 
 | 4095 |                 if (mStandby || track->presentationComplete(framesWritten, audioHALFrames)) { | 
 | 4096 |                     if (track->isStopped()) { | 
 | 4097 |                         track->reset(); | 
 | 4098 |                     } | 
 | 4099 |                     tracksToRemove->add(track); | 
 | 4100 |                 } | 
 | 4101 |             } else { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4102 |                 // No buffers for this track. Give it a few chances to | 
 | 4103 |                 // fill a buffer, then remove it from active list. | 
 | 4104 |                 if (--(track->mRetryCount) <= 0) { | 
| Glenn Kasten | c9b2e20 | 2013-02-26 11:32:32 -0800 | [diff] [blame] | 4105 |                     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] | 4106 |                     tracksToRemove->add(track); | 
 | 4107 |                     // indicate to client process that the track was disabled because of underrun; | 
 | 4108 |                     // it will then automatically call start() when data is available | 
| Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 4109 |                     android_atomic_or(CBLK_DISABLED, &cblk->mFlags); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4110 |                 // If one track is not ready, mark the mixer also not ready if: | 
 | 4111 |                 //  - the mixer was ready during previous round OR | 
 | 4112 |                 //  - no other track is ready | 
 | 4113 |                 } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY || | 
 | 4114 |                                 mixerStatus != MIXER_TRACKS_READY) { | 
 | 4115 |                     mixerStatus = MIXER_TRACKS_ENABLED; | 
 | 4116 |                 } | 
 | 4117 |             } | 
 | 4118 |             mAudioMixer->disable(name); | 
 | 4119 |         } | 
 | 4120 |  | 
 | 4121 |         }   // local variable scope to avoid goto warning | 
 | 4122 | track_is_ready: ; | 
 | 4123 |  | 
 | 4124 |     } | 
 | 4125 |  | 
 | 4126 |     // Push the new FastMixer state if necessary | 
 | 4127 |     bool pauseAudioWatchdog = false; | 
 | 4128 |     if (didModify) { | 
 | 4129 |         state->mFastTracksGen++; | 
 | 4130 |         // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle | 
 | 4131 |         if (kUseFastMixer == FastMixer_Dynamic && | 
 | 4132 |                 state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) { | 
 | 4133 |             state->mCommand = FastMixerState::COLD_IDLE; | 
 | 4134 |             state->mColdFutexAddr = &mFastMixerFutex; | 
 | 4135 |             state->mColdGen++; | 
 | 4136 |             mFastMixerFutex = 0; | 
 | 4137 |             if (kUseFastMixer == FastMixer_Dynamic) { | 
 | 4138 |                 mNormalSink = mOutputSink; | 
 | 4139 |             } | 
 | 4140 |             // If we go into cold idle, need to wait for acknowledgement | 
 | 4141 |             // so that fast mixer stops doing I/O. | 
 | 4142 |             block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; | 
 | 4143 |             pauseAudioWatchdog = true; | 
 | 4144 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4145 |     } | 
 | 4146 |     if (sq != NULL) { | 
 | 4147 |         sq->end(didModify); | 
 | 4148 |         sq->push(block); | 
 | 4149 |     } | 
 | 4150 | #ifdef AUDIO_WATCHDOG | 
 | 4151 |     if (pauseAudioWatchdog && mAudioWatchdog != 0) { | 
 | 4152 |         mAudioWatchdog->pause(); | 
 | 4153 |     } | 
 | 4154 | #endif | 
 | 4155 |  | 
 | 4156 |     // Now perform the deferred reset on fast tracks that have stopped | 
 | 4157 |     while (resetMask != 0) { | 
 | 4158 |         size_t i = __builtin_ctz(resetMask); | 
 | 4159 |         ALOG_ASSERT(i < count); | 
 | 4160 |         resetMask &= ~(1 << i); | 
 | 4161 |         sp<Track> t = mActiveTracks[i].promote(); | 
 | 4162 |         if (t == 0) { | 
 | 4163 |             continue; | 
 | 4164 |         } | 
 | 4165 |         Track* track = t.get(); | 
 | 4166 |         ALOG_ASSERT(track->isFastTrack() && track->isStopped()); | 
 | 4167 |         track->reset(); | 
 | 4168 |     } | 
 | 4169 |  | 
 | 4170 |     // remove all the tracks that need to be... | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4171 |     removeTracks_l(*tracksToRemove); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4172 |  | 
| Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 4173 |     if (getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX) != 0) { | 
 | 4174 |         mEffectBufferValid = true; | 
| Marco Nelissen | ac30214 | 2014-10-20 13:15:38 -0700 | [diff] [blame] | 4175 |     } | 
 | 4176 |  | 
 | 4177 |     if (mEffectBufferValid) { | 
| Marco Nelissen | 57088b5 | 2014-10-17 16:39:39 -0700 | [diff] [blame] | 4178 |         // as long as there are effects we should clear the effects buffer, to avoid | 
 | 4179 |         // passing a non-clean buffer to the effect chain | 
 | 4180 |         memset(mEffectBuffer, 0, mEffectBufferSize); | 
| Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 4181 |     } | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4182 |     // sink or mix buffer must be cleared if all tracks are connected to an | 
 | 4183 |     // effect chain as in this case the mixer will not write to the sink or mix buffer | 
 | 4184 |     // and track effects will accumulate into it | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4185 |     if ((mBytesRemaining == 0) && ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || | 
 | 4186 |             (mixedTracks == 0 && fastTracks > 0))) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4187 |         // FIXME as a performance optimization, should remember previous zero status | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4188 |         if (mMixerBufferValid) { | 
 | 4189 |             memset(mMixerBuffer, 0, mMixerBufferSize); | 
 | 4190 |             // TODO: In testing, mSinkBuffer below need not be cleared because | 
 | 4191 |             // the PlaybackThread::threadLoop() copies mMixerBuffer into mSinkBuffer | 
 | 4192 |             // after mixing. | 
 | 4193 |             // | 
 | 4194 |             // To enforce this guarantee: | 
 | 4195 |             // ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || | 
 | 4196 |             // (mixedTracks == 0 && fastTracks > 0)) | 
 | 4197 |             // must imply MIXER_TRACKS_READY. | 
 | 4198 |             // Later, we may clear buffers regardless, and skip much of this logic. | 
 | 4199 |         } | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4200 |         // FIXME as a performance optimization, should remember previous zero status | 
| Andy Hung | 5567aaf | 2014-07-17 14:00:07 -0700 | [diff] [blame] | 4201 |         memset(mSinkBuffer, 0, mNormalFrameCount * mFrameSize); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4202 |     } | 
 | 4203 |  | 
 | 4204 |     // if any fast tracks, then status is ready | 
 | 4205 |     mMixerStatusIgnoringFastTracks = mixerStatus; | 
 | 4206 |     if (fastTracks > 0) { | 
 | 4207 |         mixerStatus = MIXER_TRACKS_READY; | 
 | 4208 |     } | 
 | 4209 |     return mixerStatus; | 
 | 4210 | } | 
 | 4211 |  | 
 | 4212 | // getTrackName_l() must be called with ThreadBase::mLock held | 
| Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4213 | int AudioFlinger::MixerThread::getTrackName_l(audio_channel_mask_t channelMask, | 
 | 4214 |         audio_format_t format, int sessionId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4215 | { | 
| Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4216 |     return mAudioMixer->getTrackName(channelMask, format, sessionId); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4217 | } | 
 | 4218 |  | 
 | 4219 | // deleteTrackName_l() must be called with ThreadBase::mLock held | 
 | 4220 | void AudioFlinger::MixerThread::deleteTrackName_l(int name) | 
 | 4221 | { | 
 | 4222 |     ALOGV("remove track (%d) and delete from mixer", name); | 
 | 4223 |     mAudioMixer->deleteTrackName(name); | 
 | 4224 | } | 
 | 4225 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4226 | // checkForNewParameter_l() must be called with ThreadBase::mLock held | 
 | 4227 | bool AudioFlinger::MixerThread::checkForNewParameter_l(const String8& keyValuePair, | 
 | 4228 |                                                        status_t& status) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4229 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4230 |     bool reconfig = false; | 
 | 4231 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4232 |     status = NO_ERROR; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4233 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4234 |     // if !&IDLE, holds the FastMixer state to restore after new parameters processed | 
 | 4235 |     FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE; | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 4236 |     if (mFastMixer != 0) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4237 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 4238 |         FastMixerState *state = sq->begin(); | 
 | 4239 |         if (!(state->mCommand & FastMixerState::IDLE)) { | 
 | 4240 |             previousCommand = state->mCommand; | 
 | 4241 |             state->mCommand = FastMixerState::HOT_IDLE; | 
 | 4242 |             sq->end(); | 
 | 4243 |             sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); | 
 | 4244 |         } else { | 
 | 4245 |             sq->end(false /*didModify*/); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4246 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4247 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4248 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4249 |     AudioParameter param = AudioParameter(keyValuePair); | 
 | 4250 |     int value; | 
 | 4251 |     if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { | 
 | 4252 |         reconfig = true; | 
 | 4253 |     } | 
 | 4254 |     if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4255 |         if (!isValidPcmSinkFormat((audio_format_t) value)) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4256 |             status = BAD_VALUE; | 
 | 4257 |         } else { | 
 | 4258 |             // no need to save value, since it's constant | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4259 |             reconfig = true; | 
 | 4260 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4261 |     } | 
 | 4262 |     if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4263 |         if (!isValidPcmSinkChannelMask((audio_channel_mask_t) value)) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4264 |             status = BAD_VALUE; | 
 | 4265 |         } else { | 
 | 4266 |             // no need to save value, since it's constant | 
 | 4267 |             reconfig = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4268 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4269 |     } | 
 | 4270 |     if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { | 
 | 4271 |         // do not accept frame count changes if tracks are open as the track buffer | 
 | 4272 |         // size depends on frame count and correct behavior would not be guaranteed | 
 | 4273 |         // if frame count is changed after track creation | 
 | 4274 |         if (!mTracks.isEmpty()) { | 
 | 4275 |             status = INVALID_OPERATION; | 
 | 4276 |         } else { | 
 | 4277 |             reconfig = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4278 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4279 |     } | 
 | 4280 |     if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4281 | #ifdef ADD_BATTERY_DATA | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4282 |         // when changing the audio output device, call addBatteryData to notify | 
 | 4283 |         // the change | 
 | 4284 |         if (mOutDevice != value) { | 
 | 4285 |             uint32_t params = 0; | 
 | 4286 |             // check whether speaker is on | 
 | 4287 |             if (value & AUDIO_DEVICE_OUT_SPEAKER) { | 
 | 4288 |                 params |= IMediaPlayerService::kBatteryDataSpeakerOn; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4289 |             } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4290 |  | 
 | 4291 |             audio_devices_t deviceWithoutSpeaker | 
 | 4292 |                 = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; | 
 | 4293 |             // check if any other device (except speaker) is on | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4294 |             if (value & deviceWithoutSpeaker) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4295 |                 params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; | 
 | 4296 |             } | 
 | 4297 |  | 
 | 4298 |             if (params != 0) { | 
 | 4299 |                 addBatteryData(params); | 
 | 4300 |             } | 
 | 4301 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4302 | #endif | 
 | 4303 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4304 |         // forward device change to effects that have requested to be | 
 | 4305 |         // aware of attached audio device. | 
 | 4306 |         if (value != AUDIO_DEVICE_NONE) { | 
 | 4307 |             mOutDevice = value; | 
 | 4308 |             for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 4309 |                 mEffectChains[i]->setDevice_l(mOutDevice); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4310 |             } | 
 | 4311 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4312 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4313 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4314 |     if (status == NO_ERROR) { | 
 | 4315 |         status = mOutput->stream->common.set_parameters(&mOutput->stream->common, | 
 | 4316 |                                                 keyValuePair.string()); | 
 | 4317 |         if (!mStandby && status == INVALID_OPERATION) { | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 4318 |             mOutput->standby(); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4319 |             mStandby = true; | 
 | 4320 |             mBytesWritten = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4321 |             status = mOutput->stream->common.set_parameters(&mOutput->stream->common, | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4322 |                                                    keyValuePair.string()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4323 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4324 |         if (status == NO_ERROR && reconfig) { | 
 | 4325 |             readOutputParameters_l(); | 
 | 4326 |             delete mAudioMixer; | 
 | 4327 |             mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); | 
 | 4328 |             for (size_t i = 0; i < mTracks.size() ; i++) { | 
| Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4329 |                 int name = getTrackName_l(mTracks[i]->mChannelMask, | 
 | 4330 |                         mTracks[i]->mFormat, mTracks[i]->mSessionId); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4331 |                 if (name < 0) { | 
 | 4332 |                     break; | 
 | 4333 |                 } | 
 | 4334 |                 mTracks[i]->mName = name; | 
 | 4335 |             } | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 4336 |             sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4337 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4338 |     } | 
 | 4339 |  | 
 | 4340 |     if (!(previousCommand & FastMixerState::IDLE)) { | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 4341 |         ALOG_ASSERT(mFastMixer != 0); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4342 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 4343 |         FastMixerState *state = sq->begin(); | 
 | 4344 |         ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE); | 
 | 4345 |         state->mCommand = previousCommand; | 
 | 4346 |         sq->end(); | 
 | 4347 |         sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); | 
 | 4348 |     } | 
 | 4349 |  | 
 | 4350 |     return reconfig; | 
 | 4351 | } | 
 | 4352 |  | 
 | 4353 |  | 
 | 4354 | void AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) | 
 | 4355 | { | 
 | 4356 |     const size_t SIZE = 256; | 
 | 4357 |     char buffer[SIZE]; | 
 | 4358 |     String8 result; | 
 | 4359 |  | 
 | 4360 |     PlaybackThread::dumpInternals(fd, args); | 
| Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 4361 |     dprintf(fd, "  Thread throttle time (msecs): %u\n", mThreadThrottleTimeMs); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 4362 |     dprintf(fd, "  AudioMixer tracks: 0x%08x\n", mAudioMixer->trackNames()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4363 |  | 
 | 4364 |     // 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] | 4365 |     const FastMixerDumpState copy(mFastMixerDumpState); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4366 |     copy.dump(fd); | 
 | 4367 |  | 
 | 4368 | #ifdef STATE_QUEUE_DUMP | 
 | 4369 |     // Similar for state queue | 
 | 4370 |     StateQueueObserverDump observerCopy = mStateQueueObserverDump; | 
 | 4371 |     observerCopy.dump(fd); | 
 | 4372 |     StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump; | 
 | 4373 |     mutatorCopy.dump(fd); | 
 | 4374 | #endif | 
 | 4375 |  | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 4376 | #ifdef TEE_SINK | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4377 |     // Write the tee output to a .wav file | 
 | 4378 |     dumpTee(fd, mTeeSource, mId); | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 4379 | #endif | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4380 |  | 
 | 4381 | #ifdef AUDIO_WATCHDOG | 
 | 4382 |     if (mAudioWatchdog != 0) { | 
 | 4383 |         // Make a non-atomic copy of audio watchdog dump so it won't change underneath us | 
 | 4384 |         AudioWatchdogDump wdCopy = mAudioWatchdogDump; | 
 | 4385 |         wdCopy.dump(fd); | 
 | 4386 |     } | 
 | 4387 | #endif | 
 | 4388 | } | 
 | 4389 |  | 
 | 4390 | uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() const | 
 | 4391 | { | 
 | 4392 |     return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2; | 
 | 4393 | } | 
 | 4394 |  | 
 | 4395 | uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() const | 
 | 4396 | { | 
 | 4397 |     return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000); | 
 | 4398 | } | 
 | 4399 |  | 
 | 4400 | void AudioFlinger::MixerThread::cacheParameters_l() | 
 | 4401 | { | 
 | 4402 |     PlaybackThread::cacheParameters_l(); | 
 | 4403 |  | 
 | 4404 |     // FIXME: Relaxed timing because of a certain device that can't meet latency | 
 | 4405 |     // Should be reduced to 2x after the vendor fixes the driver issue | 
 | 4406 |     // increase threshold again due to low power audio mode. The way this warning | 
 | 4407 |     // threshold is calculated and its usefulness should be reconsidered anyway. | 
 | 4408 |     maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15; | 
 | 4409 | } | 
 | 4410 |  | 
 | 4411 | // ---------------------------------------------------------------------------- | 
 | 4412 |  | 
 | 4413 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 4414 |         AudioStreamOut* output, audio_io_handle_t id, audio_devices_t device, bool systemReady) | 
 | 4415 |     :   PlaybackThread(audioFlinger, output, id, device, DIRECT, systemReady) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4416 |         // mLeftVolFloat, mRightVolFloat | 
 | 4417 | { | 
 | 4418 | } | 
 | 4419 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4420 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, | 
 | 4421 |         AudioStreamOut* output, audio_io_handle_t id, uint32_t device, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 4422 |         ThreadBase::type_t type, bool systemReady) | 
 | 4423 |     :   PlaybackThread(audioFlinger, output, id, device, type, systemReady) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4424 |         // mLeftVolFloat, mRightVolFloat | 
 | 4425 | { | 
 | 4426 | } | 
 | 4427 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4428 | AudioFlinger::DirectOutputThread::~DirectOutputThread() | 
 | 4429 | { | 
 | 4430 | } | 
 | 4431 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4432 | void AudioFlinger::DirectOutputThread::processVolume_l(Track *track, bool lastTrack) | 
 | 4433 | { | 
 | 4434 |     audio_track_cblk_t* cblk = track->cblk(); | 
 | 4435 |     float left, right; | 
 | 4436 |  | 
 | 4437 |     if (mMasterMute || mStreamTypes[track->streamType()].mute) { | 
 | 4438 |         left = right = 0; | 
 | 4439 |     } else { | 
 | 4440 |         float typeVolume = mStreamTypes[track->streamType()].volume; | 
 | 4441 |         float v = mMasterVolume * typeVolume; | 
 | 4442 |         AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy; | 
| Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4443 |         gain_minifloat_packed_t vlr = proxy->getVolumeLR(); | 
 | 4444 |         left = float_from_gain(gain_minifloat_unpack_left(vlr)); | 
 | 4445 |         if (left > GAIN_FLOAT_UNITY) { | 
 | 4446 |             left = GAIN_FLOAT_UNITY; | 
 | 4447 |         } | 
 | 4448 |         left *= v; | 
 | 4449 |         right = float_from_gain(gain_minifloat_unpack_right(vlr)); | 
 | 4450 |         if (right > GAIN_FLOAT_UNITY) { | 
 | 4451 |             right = GAIN_FLOAT_UNITY; | 
 | 4452 |         } | 
 | 4453 |         right *= v; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4454 |     } | 
 | 4455 |  | 
 | 4456 |     if (lastTrack) { | 
 | 4457 |         if (left != mLeftVolFloat || right != mRightVolFloat) { | 
 | 4458 |             mLeftVolFloat = left; | 
 | 4459 |             mRightVolFloat = right; | 
 | 4460 |  | 
 | 4461 |             // Convert volumes from float to 8.24 | 
 | 4462 |             uint32_t vl = (uint32_t)(left * (1 << 24)); | 
 | 4463 |             uint32_t vr = (uint32_t)(right * (1 << 24)); | 
 | 4464 |  | 
 | 4465 |             // Delegate volume control to effect in track effect chain if needed | 
 | 4466 |             // only one effect chain can be present on DirectOutputThread, so if | 
 | 4467 |             // there is one, the track is connected to it | 
 | 4468 |             if (!mEffectChains.isEmpty()) { | 
 | 4469 |                 mEffectChains[0]->setVolume_l(&vl, &vr); | 
 | 4470 |                 left = (float)vl / (1 << 24); | 
 | 4471 |                 right = (float)vr / (1 << 24); | 
 | 4472 |             } | 
 | 4473 |             if (mOutput->stream->set_volume) { | 
 | 4474 |                 mOutput->stream->set_volume(mOutput->stream, left, right); | 
 | 4475 |             } | 
 | 4476 |         } | 
 | 4477 |     } | 
 | 4478 | } | 
 | 4479 |  | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4480 | void AudioFlinger::DirectOutputThread::onAddNewTrack_l() | 
 | 4481 | { | 
 | 4482 |     sp<Track> previousTrack = mPreviousTrack.promote(); | 
 | 4483 |     sp<Track> latestTrack = mLatestActiveTrack.promote(); | 
 | 4484 |  | 
 | 4485 |     if (previousTrack != 0 && latestTrack != 0 && | 
 | 4486 |         (previousTrack->sessionId() != latestTrack->sessionId())) { | 
 | 4487 |         mFlushPending = true; | 
 | 4488 |     } | 
 | 4489 |     PlaybackThread::onAddNewTrack_l(); | 
 | 4490 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4491 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4492 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l( | 
 | 4493 |     Vector< sp<Track> > *tracksToRemove | 
 | 4494 | ) | 
 | 4495 | { | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4496 |     size_t count = mActiveTracks.size(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4497 |     mixer_state mixerStatus = MIXER_IDLE; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4498 |     bool doHwPause = false; | 
 | 4499 |     bool doHwResume = false; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4500 |  | 
 | 4501 |     // find out which tracks need to be processed | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4502 |     for (size_t i = 0; i < count; i++) { | 
 | 4503 |         sp<Track> t = mActiveTracks[i].promote(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4504 |         // The track died recently | 
 | 4505 |         if (t == 0) { | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4506 |             continue; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4507 |         } | 
 | 4508 |  | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4509 |         if (t->isInvalid()) { | 
 | 4510 |             ALOGW("An invalidated track shouldn't be in active list"); | 
 | 4511 |             tracksToRemove->add(t); | 
 | 4512 |             continue; | 
 | 4513 |         } | 
 | 4514 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4515 |         Track* const track = t.get(); | 
 | 4516 |         audio_track_cblk_t* cblk = track->cblk(); | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4517 |         // Only consider last track started for volume and mixer state control. | 
 | 4518 |         // In theory an older track could underrun and restart after the new one starts | 
 | 4519 |         // but as we only care about the transition phase between two tracks on a | 
 | 4520 |         // direct output, it is not a problem to ignore the underrun case. | 
 | 4521 |         sp<Track> l = mLatestActiveTrack.promote(); | 
 | 4522 |         bool last = l.get() == track; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4523 |  | 
| Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4524 |         if (track->isPausing()) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4525 |             track->setPaused(); | 
| Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4526 |             if (mHwSupportsPause && last && !mHwPaused) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4527 |                 doHwPause = true; | 
 | 4528 |                 mHwPaused = true; | 
 | 4529 |             } | 
 | 4530 |             tracksToRemove->add(track); | 
 | 4531 |         } else if (track->isFlushPending()) { | 
 | 4532 |             track->flushAck(); | 
 | 4533 |             if (last) { | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4534 |                 mFlushPending = true; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4535 |             } | 
| Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4536 |         } else if (track->isResumePending()) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4537 |             track->resumeAck(); | 
| Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4538 |             if (last && mHwPaused) { | 
 | 4539 |                 doHwResume = true; | 
 | 4540 |                 mHwPaused = false; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4541 |             } | 
 | 4542 |         } | 
 | 4543 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4544 |         // The first time a track is added we wait | 
| Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 4545 |         // for all its buffers to be filled before processing it. | 
 | 4546 |         // Allow draining the buffer in case the client | 
 | 4547 |         // app does not call stop() and relies on underrun to stop: | 
 | 4548 |         // hence the test on (track->mRetryCount > 1). | 
 | 4549 |         // If retryCount<=1 then track is about to underrun and be removed. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4550 |         uint32_t minFrames; | 
| Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 4551 |         if ((track->sharedBuffer() == 0) && !track->isStopping_1() && !track->isPausing() | 
 | 4552 |             && (track->mRetryCount > 1)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4553 |             minFrames = mNormalFrameCount; | 
 | 4554 |         } else { | 
 | 4555 |             minFrames = 1; | 
 | 4556 |         } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4557 |  | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4558 |         if ((track->framesReady() >= minFrames) && track->isReady() && !track->isPaused() && | 
 | 4559 |                 !track->isStopping_2() && !track->isStopped()) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4560 |         { | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4561 |             ALOGVV("track %d s=%08x [OK]", track->name(), cblk->mServer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4562 |  | 
 | 4563 |             if (track->mFillingUpStatus == Track::FS_FILLED) { | 
 | 4564 |                 track->mFillingUpStatus = Track::FS_ACTIVE; | 
| Eric Laurent | 1abbdb4 | 2013-09-13 17:00:08 -0700 | [diff] [blame] | 4565 |                 // make sure processVolume_l() will apply new volume even if 0 | 
 | 4566 |                 mLeftVolFloat = mRightVolFloat = -1.0; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4567 |                 if (!mHwSupportsPause) { | 
 | 4568 |                     track->resumeAck(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4569 |                 } | 
 | 4570 |             } | 
 | 4571 |  | 
 | 4572 |             // compute volume for this track | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4573 |             processVolume_l(track, last); | 
 | 4574 |             if (last) { | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4575 |                 sp<Track> previousTrack = mPreviousTrack.promote(); | 
 | 4576 |                 if (previousTrack != 0) { | 
 | 4577 |                     if (track != previousTrack.get()) { | 
 | 4578 |                         // Flush any data still being written from last track | 
 | 4579 |                         mBytesRemaining = 0; | 
 | 4580 |                         // flush data already sent if changing audio session as audio | 
 | 4581 |                         // comes from a different source. Also invalidate previous track to force a | 
 | 4582 |                         // seek when resuming. | 
 | 4583 |                         if (previousTrack->sessionId() != track->sessionId()) { | 
 | 4584 |                             previousTrack->invalidate(); | 
 | 4585 |                         } | 
 | 4586 |                     } | 
 | 4587 |                 } | 
 | 4588 |                 mPreviousTrack = track; | 
 | 4589 |  | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4590 |                 // reset retry count | 
 | 4591 |                 track->mRetryCount = kMaxTrackRetriesDirect; | 
 | 4592 |                 mActiveTrack = t; | 
 | 4593 |                 mixerStatus = MIXER_TRACKS_READY; | 
| Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 4594 |                 if (mHwPaused) { | 
| Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 4595 |                     doHwResume = true; | 
 | 4596 |                     mHwPaused = false; | 
 | 4597 |                 } | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4598 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4599 |         } else { | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4600 |             // clear effect chain input buffer if the last active track started underruns | 
 | 4601 |             // to avoid sending previous audio buffer again to effects | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4602 |             if (!mEffectChains.isEmpty() && last) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4603 |                 mEffectChains[0]->clearInputBuffer(); | 
 | 4604 |             } | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4605 |             if (track->isStopping_1()) { | 
 | 4606 |                 track->mState = TrackBase::STOPPING_2; | 
| Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 4607 |                 if (last && mHwPaused) { | 
 | 4608 |                      doHwResume = true; | 
 | 4609 |                      mHwPaused = false; | 
 | 4610 |                  } | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4611 |             } | 
 | 4612 |             if ((track->sharedBuffer() != 0) || track->isStopped() || | 
 | 4613 |                     track->isStopping_2() || track->isPaused()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4614 |                 // We have consumed all the buffers of this track. | 
 | 4615 |                 // Remove it from the list of active tracks. | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4616 |                 size_t audioHALFrames; | 
 | 4617 |                 if (audio_is_linear_pcm(mFormat)) { | 
 | 4618 |                     audioHALFrames = (latency_l() * mSampleRate) / 1000; | 
 | 4619 |                 } else { | 
 | 4620 |                     audioHALFrames = 0; | 
 | 4621 |                 } | 
 | 4622 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4623 |                 size_t framesWritten = mBytesWritten / mFrameSize; | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4624 |                 if (mStandby || !last || | 
 | 4625 |                         track->presentationComplete(framesWritten, audioHALFrames)) { | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4626 |                     if (track->isStopping_2()) { | 
 | 4627 |                         track->mState = TrackBase::STOPPED; | 
 | 4628 |                     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4629 |                     if (track->isStopped()) { | 
 | 4630 |                         track->reset(); | 
 | 4631 |                     } | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4632 |                     tracksToRemove->add(track); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4633 |                 } | 
 | 4634 |             } else { | 
 | 4635 |                 // No buffers for this track. Give it a few chances to | 
 | 4636 |                 // fill a buffer, then remove it from active list. | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4637 |                 // Only consider last track started for mixer state control | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4638 |                 if (--(track->mRetryCount) <= 0) { | 
 | 4639 |                     ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name()); | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4640 |                     tracksToRemove->add(track); | 
| Eric Laurent | a23f17a | 2013-11-05 18:22:08 -0800 | [diff] [blame] | 4641 |                     // indicate to client process that the track was disabled because of underrun; | 
 | 4642 |                     // it will then automatically call start() when data is available | 
 | 4643 |                     android_atomic_or(CBLK_DISABLED, &cblk->mFlags); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4644 |                 } else if (last) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4645 |                     mixerStatus = MIXER_TRACKS_ENABLED; | 
| Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 4646 |                     if (mHwSupportsPause && !mHwPaused && !mStandby) { | 
| Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 4647 |                         doHwPause = true; | 
 | 4648 |                         mHwPaused = true; | 
 | 4649 |                     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4650 |                 } | 
 | 4651 |             } | 
 | 4652 |         } | 
 | 4653 |     } | 
 | 4654 |  | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4655 |     // if an active track did not command a flush, check for pending flush on stopped tracks | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4656 |     if (!mFlushPending) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4657 |         for (size_t i = 0; i < mTracks.size(); i++) { | 
 | 4658 |             if (mTracks[i]->isFlushPending()) { | 
 | 4659 |                 mTracks[i]->flushAck(); | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4660 |                 mFlushPending = true; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4661 |             } | 
 | 4662 |         } | 
 | 4663 |     } | 
 | 4664 |  | 
 | 4665 |     // make sure the pause/flush/resume sequence is executed in the right order. | 
 | 4666 |     // If a flush is pending and a track is active but the HW is not paused, force a HW pause | 
 | 4667 |     // before flush and then resume HW. This can happen in case of pause/flush/resume | 
 | 4668 |     // if resume is received before pause is executed. | 
 | 4669 |     if (mHwSupportsPause && !mStandby && | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4670 |             (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4671 |         mOutput->stream->pause(mOutput->stream); | 
 | 4672 |     } | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4673 |     if (mFlushPending) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4674 |         flushHw_l(); | 
 | 4675 |     } | 
 | 4676 |     if (mHwSupportsPause && !mStandby && doHwResume) { | 
 | 4677 |         mOutput->stream->resume(mOutput->stream); | 
 | 4678 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4679 |     // remove all the tracks that need to be... | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4680 |     removeTracks_l(*tracksToRemove); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4681 |  | 
 | 4682 |     return mixerStatus; | 
 | 4683 | } | 
 | 4684 |  | 
 | 4685 | void AudioFlinger::DirectOutputThread::threadLoop_mix() | 
 | 4686 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4687 |     size_t frameCount = mFrameCount; | 
| Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 4688 |     int8_t *curBuf = (int8_t *)mSinkBuffer; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4689 |     // output audio to hardware | 
 | 4690 |     while (frameCount) { | 
| Glenn Kasten | 34542ac | 2013-06-26 11:29:02 -0700 | [diff] [blame] | 4691 |         AudioBufferProvider::Buffer buffer; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4692 |         buffer.frameCount = frameCount; | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 4693 |         status_t status = mActiveTrack->getNextBuffer(&buffer); | 
 | 4694 |         if (status != NO_ERROR || buffer.raw == NULL) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4695 |             memset(curBuf, 0, frameCount * mFrameSize); | 
 | 4696 |             break; | 
 | 4697 |         } | 
 | 4698 |         memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); | 
 | 4699 |         frameCount -= buffer.frameCount; | 
 | 4700 |         curBuf += buffer.frameCount * mFrameSize; | 
 | 4701 |         mActiveTrack->releaseBuffer(&buffer); | 
 | 4702 |     } | 
| Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 4703 |     mCurrentWriteLength = curBuf - (int8_t *)mSinkBuffer; | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4704 |     mSleepTimeUs = 0; | 
 | 4705 |     mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4706 |     mActiveTrack.clear(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4707 | } | 
 | 4708 |  | 
 | 4709 | void AudioFlinger::DirectOutputThread::threadLoop_sleepTime() | 
 | 4710 | { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4711 |     // do not write to HAL when paused | 
| Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 4712 |     if (mHwPaused || (usesHwAvSync() && mStandby)) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4713 |         mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4714 |         return; | 
 | 4715 |     } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4716 |     if (mSleepTimeUs == 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4717 |         if (mMixerStatus == MIXER_TRACKS_ENABLED) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4718 |             mSleepTimeUs = mActiveSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4719 |         } else { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4720 |             mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4721 |         } | 
 | 4722 |     } else if (mBytesWritten != 0 && audio_is_linear_pcm(mFormat)) { | 
| Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 4723 |         memset(mSinkBuffer, 0, mFrameCount * mFrameSize); | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4724 |         mSleepTimeUs = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4725 |     } | 
 | 4726 | } | 
 | 4727 |  | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4728 | void AudioFlinger::DirectOutputThread::threadLoop_exit() | 
 | 4729 | { | 
 | 4730 |     { | 
 | 4731 |         Mutex::Autolock _l(mLock); | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4732 |         for (size_t i = 0; i < mTracks.size(); i++) { | 
 | 4733 |             if (mTracks[i]->isFlushPending()) { | 
 | 4734 |                 mTracks[i]->flushAck(); | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4735 |                 mFlushPending = true; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4736 |             } | 
 | 4737 |         } | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4738 |         if (mFlushPending) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4739 |             flushHw_l(); | 
 | 4740 |         } | 
 | 4741 |     } | 
 | 4742 |     PlaybackThread::threadLoop_exit(); | 
 | 4743 | } | 
 | 4744 |  | 
 | 4745 | // must be called with thread mutex locked | 
 | 4746 | bool AudioFlinger::DirectOutputThread::shouldStandby_l() | 
 | 4747 | { | 
 | 4748 |     bool trackPaused = false; | 
| Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 4749 |     bool trackStopped = false; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4750 |  | 
 | 4751 |     // do not put the HAL in standby when paused. AwesomePlayer clear the offloaded AudioTrack | 
 | 4752 |     // after a timeout and we will enter standby then. | 
 | 4753 |     if (mTracks.size() > 0) { | 
 | 4754 |         trackPaused = mTracks[mTracks.size() - 1]->isPaused(); | 
| Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 4755 |         trackStopped = mTracks[mTracks.size() - 1]->isStopped() || | 
 | 4756 |                            mTracks[mTracks.size() - 1]->mState == TrackBase::IDLE; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4757 |     } | 
 | 4758 |  | 
| Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 4759 |     return !mStandby && !(trackPaused || (mHwPaused && !trackStopped)); | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4760 | } | 
 | 4761 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4762 | // getTrackName_l() must be called with ThreadBase::mLock held | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 4763 | int AudioFlinger::DirectOutputThread::getTrackName_l(audio_channel_mask_t channelMask __unused, | 
| Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4764 |         audio_format_t format __unused, int sessionId __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4765 | { | 
 | 4766 |     return 0; | 
 | 4767 | } | 
 | 4768 |  | 
 | 4769 | // deleteTrackName_l() must be called with ThreadBase::mLock held | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 4770 | void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4771 | { | 
 | 4772 | } | 
 | 4773 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4774 | // checkForNewParameter_l() must be called with ThreadBase::mLock held | 
 | 4775 | bool AudioFlinger::DirectOutputThread::checkForNewParameter_l(const String8& keyValuePair, | 
 | 4776 |                                                               status_t& status) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4777 | { | 
 | 4778 |     bool reconfig = false; | 
 | 4779 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4780 |     status = NO_ERROR; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4781 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4782 |     AudioParameter param = AudioParameter(keyValuePair); | 
 | 4783 |     int value; | 
 | 4784 |     if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { | 
 | 4785 |         // forward device change to effects that have requested to be | 
 | 4786 |         // aware of attached audio device. | 
 | 4787 |         if (value != AUDIO_DEVICE_NONE) { | 
 | 4788 |             mOutDevice = value; | 
 | 4789 |             for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 4790 |                 mEffectChains[i]->setDevice_l(mOutDevice); | 
| Glenn Kasten | c125f38 | 2014-04-11 18:37:33 -0700 | [diff] [blame] | 4791 |             } | 
 | 4792 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4793 |     } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4794 |     if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { | 
 | 4795 |         // do not accept frame count changes if tracks are open as the track buffer | 
 | 4796 |         // size depends on frame count and correct behavior would not be garantied | 
 | 4797 |         // if frame count is changed after track creation | 
 | 4798 |         if (!mTracks.isEmpty()) { | 
 | 4799 |             status = INVALID_OPERATION; | 
 | 4800 |         } else { | 
 | 4801 |             reconfig = true; | 
 | 4802 |         } | 
 | 4803 |     } | 
 | 4804 |     if (status == NO_ERROR) { | 
 | 4805 |         status = mOutput->stream->common.set_parameters(&mOutput->stream->common, | 
 | 4806 |                                                 keyValuePair.string()); | 
 | 4807 |         if (!mStandby && status == INVALID_OPERATION) { | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 4808 |             mOutput->standby(); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4809 |             mStandby = true; | 
 | 4810 |             mBytesWritten = 0; | 
 | 4811 |             status = mOutput->stream->common.set_parameters(&mOutput->stream->common, | 
 | 4812 |                                                    keyValuePair.string()); | 
 | 4813 |         } | 
 | 4814 |         if (status == NO_ERROR && reconfig) { | 
 | 4815 |             readOutputParameters_l(); | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 4816 |             sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4817 |         } | 
 | 4818 |     } | 
 | 4819 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4820 |     return reconfig; | 
 | 4821 | } | 
 | 4822 |  | 
 | 4823 | uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() const | 
 | 4824 | { | 
 | 4825 |     uint32_t time; | 
 | 4826 |     if (audio_is_linear_pcm(mFormat)) { | 
 | 4827 |         time = PlaybackThread::activeSleepTimeUs(); | 
 | 4828 |     } else { | 
 | 4829 |         time = 10000; | 
 | 4830 |     } | 
 | 4831 |     return time; | 
 | 4832 | } | 
 | 4833 |  | 
 | 4834 | uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() const | 
 | 4835 | { | 
 | 4836 |     uint32_t time; | 
 | 4837 |     if (audio_is_linear_pcm(mFormat)) { | 
 | 4838 |         time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; | 
 | 4839 |     } else { | 
 | 4840 |         time = 10000; | 
 | 4841 |     } | 
 | 4842 |     return time; | 
 | 4843 | } | 
 | 4844 |  | 
 | 4845 | uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() const | 
 | 4846 | { | 
 | 4847 |     uint32_t time; | 
 | 4848 |     if (audio_is_linear_pcm(mFormat)) { | 
 | 4849 |         time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); | 
 | 4850 |     } else { | 
 | 4851 |         time = 10000; | 
 | 4852 |     } | 
 | 4853 |     return time; | 
 | 4854 | } | 
 | 4855 |  | 
 | 4856 | void AudioFlinger::DirectOutputThread::cacheParameters_l() | 
 | 4857 | { | 
 | 4858 |     PlaybackThread::cacheParameters_l(); | 
 | 4859 |  | 
 | 4860 |     // use shorter standby delay as on normal output to release | 
 | 4861 |     // hardware resources as soon as possible | 
| Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 4862 |     // no delay on outputs with HW A/V sync | 
 | 4863 |     if (usesHwAvSync()) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4864 |         mStandbyDelayNs = 0; | 
| Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 4865 |     } else if ((mType == OFFLOAD) && !audio_is_linear_pcm(mFormat)) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4866 |         mStandbyDelayNs = kOffloadStandbyDelayNs; | 
| Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 4867 |     } else { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4868 |         mStandbyDelayNs = microseconds(mActiveSleepTimeUs*2); | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 4869 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4870 | } | 
 | 4871 |  | 
| Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 4872 | void AudioFlinger::DirectOutputThread::flushHw_l() | 
 | 4873 | { | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 4874 |     mOutput->flush(); | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4875 |     mHwPaused = false; | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4876 |     mFlushPending = false; | 
| Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 4877 | } | 
 | 4878 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4879 | // ---------------------------------------------------------------------------- | 
 | 4880 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4881 | AudioFlinger::AsyncCallbackThread::AsyncCallbackThread( | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 4882 |         const wp<AudioFlinger::PlaybackThread>& playbackThread) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4883 |     :   Thread(false /*canCallJava*/), | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 4884 |         mPlaybackThread(playbackThread), | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4885 |         mWriteAckSequence(0), | 
 | 4886 |         mDrainSequence(0) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4887 | { | 
 | 4888 | } | 
 | 4889 |  | 
 | 4890 | AudioFlinger::AsyncCallbackThread::~AsyncCallbackThread() | 
 | 4891 | { | 
 | 4892 | } | 
 | 4893 |  | 
 | 4894 | void AudioFlinger::AsyncCallbackThread::onFirstRef() | 
 | 4895 | { | 
 | 4896 |     run("Offload Cbk", ANDROID_PRIORITY_URGENT_AUDIO); | 
 | 4897 | } | 
 | 4898 |  | 
 | 4899 | bool AudioFlinger::AsyncCallbackThread::threadLoop() | 
 | 4900 | { | 
 | 4901 |     while (!exitPending()) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4902 |         uint32_t writeAckSequence; | 
 | 4903 |         uint32_t drainSequence; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4904 |  | 
 | 4905 |         { | 
 | 4906 |             Mutex::Autolock _l(mLock); | 
| Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 4907 |             while (!((mWriteAckSequence & 1) || | 
 | 4908 |                      (mDrainSequence & 1) || | 
 | 4909 |                      exitPending())) { | 
 | 4910 |                 mWaitWorkCV.wait(mLock); | 
 | 4911 |             } | 
 | 4912 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4913 |             if (exitPending()) { | 
 | 4914 |                 break; | 
 | 4915 |             } | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4916 |             ALOGV("AsyncCallbackThread mWriteAckSequence %d mDrainSequence %d", | 
 | 4917 |                   mWriteAckSequence, mDrainSequence); | 
 | 4918 |             writeAckSequence = mWriteAckSequence; | 
 | 4919 |             mWriteAckSequence &= ~1; | 
 | 4920 |             drainSequence = mDrainSequence; | 
 | 4921 |             mDrainSequence &= ~1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4922 |         } | 
 | 4923 |         { | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 4924 |             sp<AudioFlinger::PlaybackThread> playbackThread = mPlaybackThread.promote(); | 
 | 4925 |             if (playbackThread != 0) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4926 |                 if (writeAckSequence & 1) { | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 4927 |                     playbackThread->resetWriteBlocked(writeAckSequence >> 1); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4928 |                 } | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4929 |                 if (drainSequence & 1) { | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 4930 |                     playbackThread->resetDraining(drainSequence >> 1); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4931 |                 } | 
 | 4932 |             } | 
 | 4933 |         } | 
 | 4934 |     } | 
 | 4935 |     return false; | 
 | 4936 | } | 
 | 4937 |  | 
 | 4938 | void AudioFlinger::AsyncCallbackThread::exit() | 
 | 4939 | { | 
 | 4940 |     ALOGV("AsyncCallbackThread::exit"); | 
 | 4941 |     Mutex::Autolock _l(mLock); | 
 | 4942 |     requestExit(); | 
 | 4943 |     mWaitWorkCV.broadcast(); | 
 | 4944 | } | 
 | 4945 |  | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4946 | void AudioFlinger::AsyncCallbackThread::setWriteBlocked(uint32_t sequence) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4947 | { | 
 | 4948 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4949 |     // bit 0 is cleared | 
 | 4950 |     mWriteAckSequence = sequence << 1; | 
 | 4951 | } | 
 | 4952 |  | 
 | 4953 | void AudioFlinger::AsyncCallbackThread::resetWriteBlocked() | 
 | 4954 | { | 
 | 4955 |     Mutex::Autolock _l(mLock); | 
 | 4956 |     // ignore unexpected callbacks | 
 | 4957 |     if (mWriteAckSequence & 2) { | 
 | 4958 |         mWriteAckSequence |= 1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4959 |         mWaitWorkCV.signal(); | 
 | 4960 |     } | 
 | 4961 | } | 
 | 4962 |  | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4963 | void AudioFlinger::AsyncCallbackThread::setDraining(uint32_t sequence) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4964 | { | 
 | 4965 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4966 |     // bit 0 is cleared | 
 | 4967 |     mDrainSequence = sequence << 1; | 
 | 4968 | } | 
 | 4969 |  | 
 | 4970 | void AudioFlinger::AsyncCallbackThread::resetDraining() | 
 | 4971 | { | 
 | 4972 |     Mutex::Autolock _l(mLock); | 
 | 4973 |     // ignore unexpected callbacks | 
 | 4974 |     if (mDrainSequence & 2) { | 
 | 4975 |         mDrainSequence |= 1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4976 |         mWaitWorkCV.signal(); | 
 | 4977 |     } | 
 | 4978 | } | 
 | 4979 |  | 
 | 4980 |  | 
 | 4981 | // ---------------------------------------------------------------------------- | 
 | 4982 | AudioFlinger::OffloadThread::OffloadThread(const sp<AudioFlinger>& audioFlinger, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 4983 |         AudioStreamOut* output, audio_io_handle_t id, uint32_t device, bool systemReady) | 
 | 4984 |     :   DirectOutputThread(audioFlinger, output, id, device, OFFLOAD, systemReady), | 
| Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 4985 |         mPausedBytesRemaining(0) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4986 | { | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4987 |     //FIXME: mStandby should be set to true by ThreadBase constructor | 
 | 4988 |     mStandby = true; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4989 | } | 
 | 4990 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4991 | void AudioFlinger::OffloadThread::threadLoop_exit() | 
 | 4992 | { | 
 | 4993 |     if (mFlushPending || mHwPaused) { | 
 | 4994 |         // If a flush is pending or track was paused, just discard buffered data | 
 | 4995 |         flushHw_l(); | 
 | 4996 |     } else { | 
 | 4997 |         mMixerStatus = MIXER_DRAIN_ALL; | 
 | 4998 |         threadLoop_drain(); | 
 | 4999 |     } | 
| Uday Gupta | 56604aa | 2014-05-13 11:19:17 -0700 | [diff] [blame] | 5000 |     if (mUseAsyncWrite) { | 
 | 5001 |         ALOG_ASSERT(mCallbackThread != 0); | 
 | 5002 |         mCallbackThread->exit(); | 
 | 5003 |     } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5004 |     PlaybackThread::threadLoop_exit(); | 
 | 5005 | } | 
 | 5006 |  | 
 | 5007 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::OffloadThread::prepareTracks_l( | 
 | 5008 |     Vector< sp<Track> > *tracksToRemove | 
 | 5009 | ) | 
 | 5010 | { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5011 |     size_t count = mActiveTracks.size(); | 
 | 5012 |  | 
 | 5013 |     mixer_state mixerStatus = MIXER_IDLE; | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5014 |     bool doHwPause = false; | 
 | 5015 |     bool doHwResume = false; | 
 | 5016 |  | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 5017 |     ALOGV("OffloadThread::prepareTracks_l active tracks %d", count); | 
 | 5018 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5019 |     // find out which tracks need to be processed | 
 | 5020 |     for (size_t i = 0; i < count; i++) { | 
 | 5021 |         sp<Track> t = mActiveTracks[i].promote(); | 
 | 5022 |         // The track died recently | 
 | 5023 |         if (t == 0) { | 
 | 5024 |             continue; | 
 | 5025 |         } | 
 | 5026 |         Track* const track = t.get(); | 
 | 5027 |         audio_track_cblk_t* cblk = track->cblk(); | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5028 |         // Only consider last track started for volume and mixer state control. | 
 | 5029 |         // In theory an older track could underrun and restart after the new one starts | 
 | 5030 |         // but as we only care about the transition phase between two tracks on a | 
 | 5031 |         // direct output, it is not a problem to ignore the underrun case. | 
 | 5032 |         sp<Track> l = mLatestActiveTrack.promote(); | 
 | 5033 |         bool last = l.get() == track; | 
 | 5034 |  | 
| Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5035 |         if (track->isInvalid()) { | 
 | 5036 |             ALOGW("An invalidated track shouldn't be in active list"); | 
 | 5037 |             tracksToRemove->add(track); | 
 | 5038 |             continue; | 
 | 5039 |         } | 
 | 5040 |  | 
 | 5041 |         if (track->mState == TrackBase::IDLE) { | 
 | 5042 |             ALOGW("An idle track shouldn't be in active list"); | 
 | 5043 |             continue; | 
 | 5044 |         } | 
 | 5045 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5046 |         if (track->isPausing()) { | 
 | 5047 |             track->setPaused(); | 
 | 5048 |             if (last) { | 
| Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5049 |                 if (mHwSupportsPause && !mHwPaused) { | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5050 |                     doHwPause = true; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5051 |                     mHwPaused = true; | 
 | 5052 |                 } | 
 | 5053 |                 // If we were part way through writing the mixbuffer to | 
 | 5054 |                 // the HAL we must save this until we resume | 
 | 5055 |                 // BUG - this will be wrong if a different track is made active, | 
 | 5056 |                 // in that case we want to discard the pending data in the | 
 | 5057 |                 // mixbuffer and tell the client to present it again when the | 
 | 5058 |                 // track is resumed | 
 | 5059 |                 mPausedWriteLength = mCurrentWriteLength; | 
 | 5060 |                 mPausedBytesRemaining = mBytesRemaining; | 
 | 5061 |                 mBytesRemaining = 0;    // stop writing | 
 | 5062 |             } | 
 | 5063 |             tracksToRemove->add(track); | 
| Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5064 |         } else if (track->isFlushPending()) { | 
 | 5065 |             track->flushAck(); | 
 | 5066 |             if (last) { | 
 | 5067 |                 mFlushPending = true; | 
 | 5068 |             } | 
| Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 5069 |         } else if (track->isResumePending()){ | 
 | 5070 |             track->resumeAck(); | 
 | 5071 |             if (last) { | 
 | 5072 |                 if (mPausedBytesRemaining) { | 
 | 5073 |                     // Need to continue write that was interrupted | 
 | 5074 |                     mCurrentWriteLength = mPausedWriteLength; | 
 | 5075 |                     mBytesRemaining = mPausedBytesRemaining; | 
 | 5076 |                     mPausedBytesRemaining = 0; | 
 | 5077 |                 } | 
 | 5078 |                 if (mHwPaused) { | 
 | 5079 |                     doHwResume = true; | 
 | 5080 |                     mHwPaused = false; | 
 | 5081 |                     // threadLoop_mix() will handle the case that we need to | 
 | 5082 |                     // resume an interrupted write | 
 | 5083 |                 } | 
 | 5084 |                 // enable write to audio HAL | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5085 |                 mSleepTimeUs = 0; | 
| Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 5086 |  | 
 | 5087 |                 // Do not handle new data in this iteration even if track->framesReady() | 
 | 5088 |                 mixerStatus = MIXER_TRACKS_ENABLED; | 
 | 5089 |             } | 
 | 5090 |         }  else if (track->framesReady() && track->isReady() && | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5091 |                 !track->isPaused() && !track->isTerminated() && !track->isStopping_2()) { | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 5092 |             ALOGVV("OffloadThread: track %d s=%08x [OK]", track->name(), cblk->mServer); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5093 |             if (track->mFillingUpStatus == Track::FS_FILLED) { | 
 | 5094 |                 track->mFillingUpStatus = Track::FS_ACTIVE; | 
| Eric Laurent | 1abbdb4 | 2013-09-13 17:00:08 -0700 | [diff] [blame] | 5095 |                 // make sure processVolume_l() will apply new volume even if 0 | 
 | 5096 |                 mLeftVolFloat = mRightVolFloat = -1.0; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5097 |             } | 
 | 5098 |  | 
 | 5099 |             if (last) { | 
| Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5100 |                 sp<Track> previousTrack = mPreviousTrack.promote(); | 
 | 5101 |                 if (previousTrack != 0) { | 
 | 5102 |                     if (track != previousTrack.get()) { | 
| Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5103 |                         // Flush any data still being written from last track | 
 | 5104 |                         mBytesRemaining = 0; | 
 | 5105 |                         if (mPausedBytesRemaining) { | 
 | 5106 |                             // Last track was paused so we also need to flush saved | 
 | 5107 |                             // mixbuffer state and invalidate track so that it will | 
 | 5108 |                             // re-submit that unwritten data when it is next resumed | 
 | 5109 |                             mPausedBytesRemaining = 0; | 
 | 5110 |                             // Invalidate is a bit drastic - would be more efficient | 
 | 5111 |                             // to have a flag to tell client that some of the | 
 | 5112 |                             // previously written data was lost | 
| Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5113 |                             previousTrack->invalidate(); | 
| Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5114 |                         } | 
 | 5115 |                         // flush data already sent to the DSP if changing audio session as audio | 
 | 5116 |                         // comes from a different source. Also invalidate previous track to force a | 
 | 5117 |                         // seek when resuming. | 
| Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5118 |                         if (previousTrack->sessionId() != track->sessionId()) { | 
 | 5119 |                             previousTrack->invalidate(); | 
| Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5120 |                         } | 
 | 5121 |                     } | 
 | 5122 |                 } | 
 | 5123 |                 mPreviousTrack = track; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5124 |                 // reset retry count | 
 | 5125 |                 track->mRetryCount = kMaxTrackRetriesOffload; | 
 | 5126 |                 mActiveTrack = t; | 
 | 5127 |                 mixerStatus = MIXER_TRACKS_READY; | 
 | 5128 |             } | 
 | 5129 |         } else { | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 5130 |             ALOGVV("OffloadThread: track %d s=%08x [NOT READY]", track->name(), cblk->mServer); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5131 |             if (track->isStopping_1()) { | 
 | 5132 |                 // Hardware buffer can hold a large amount of audio so we must | 
 | 5133 |                 // wait for all current track's data to drain before we say | 
 | 5134 |                 // that the track is stopped. | 
 | 5135 |                 if (mBytesRemaining == 0) { | 
 | 5136 |                     // Only start draining when all data in mixbuffer | 
 | 5137 |                     // has been written | 
 | 5138 |                     ALOGV("OffloadThread: underrun and STOPPING_1 -> draining, STOPPING_2"); | 
 | 5139 |                     track->mState = TrackBase::STOPPING_2; // so presentation completes after drain | 
| Eric Laurent | 6a51d7e | 2013-10-17 18:59:26 -0700 | [diff] [blame] | 5140 |                     // do not drain if no data was ever sent to HAL (mStandby == true) | 
 | 5141 |                     if (last && !mStandby) { | 
| Eric Laurent | 1b9f9b1 | 2013-11-12 19:10:17 -0800 | [diff] [blame] | 5142 |                         // do not modify drain sequence if we are already draining. This happens | 
 | 5143 |                         // when resuming from pause after drain. | 
 | 5144 |                         if ((mDrainSequence & 1) == 0) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5145 |                             mSleepTimeUs = 0; | 
 | 5146 |                             mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
| Eric Laurent | 1b9f9b1 | 2013-11-12 19:10:17 -0800 | [diff] [blame] | 5147 |                             mixerStatus = MIXER_DRAIN_TRACK; | 
 | 5148 |                             mDrainSequence += 2; | 
 | 5149 |                         } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5150 |                         if (mHwPaused) { | 
 | 5151 |                             // It is possible to move from PAUSED to STOPPING_1 without | 
 | 5152 |                             // a resume so we must ensure hardware is running | 
| Eric Laurent | 1b9f9b1 | 2013-11-12 19:10:17 -0800 | [diff] [blame] | 5153 |                             doHwResume = true; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5154 |                             mHwPaused = false; | 
 | 5155 |                         } | 
 | 5156 |                     } | 
 | 5157 |                 } | 
 | 5158 |             } else if (track->isStopping_2()) { | 
| Eric Laurent | 6a51d7e | 2013-10-17 18:59:26 -0700 | [diff] [blame] | 5159 |                 // Drain has completed or we are in standby, signal presentation complete | 
 | 5160 |                 if (!(mDrainSequence & 1) || !last || mStandby) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5161 |                     track->mState = TrackBase::STOPPED; | 
 | 5162 |                     size_t audioHALFrames = | 
 | 5163 |                             (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; | 
 | 5164 |                     size_t framesWritten = | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5165 |                             mBytesWritten / mOutput->getFrameSize(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5166 |                     track->presentationComplete(framesWritten, audioHALFrames); | 
 | 5167 |                     track->reset(); | 
 | 5168 |                     tracksToRemove->add(track); | 
 | 5169 |                 } | 
 | 5170 |             } else { | 
 | 5171 |                 // No buffers for this track. Give it a few chances to | 
 | 5172 |                 // fill a buffer, then remove it from active list. | 
 | 5173 |                 if (--(track->mRetryCount) <= 0) { | 
 | 5174 |                     ALOGV("OffloadThread: BUFFER TIMEOUT: remove(%d) from active list", | 
 | 5175 |                           track->name()); | 
 | 5176 |                     tracksToRemove->add(track); | 
| Eric Laurent | a23f17a | 2013-11-05 18:22:08 -0800 | [diff] [blame] | 5177 |                     // indicate to client process that the track was disabled because of underrun; | 
 | 5178 |                     // it will then automatically call start() when data is available | 
 | 5179 |                     android_atomic_or(CBLK_DISABLED, &cblk->mFlags); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5180 |                 } else if (last){ | 
 | 5181 |                     mixerStatus = MIXER_TRACKS_ENABLED; | 
 | 5182 |                 } | 
 | 5183 |             } | 
 | 5184 |         } | 
 | 5185 |         // compute volume for this track | 
 | 5186 |         processVolume_l(track, last); | 
 | 5187 |     } | 
| Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5188 |  | 
| Eric Laurent | ea0fade | 2013-10-04 16:23:48 -0700 | [diff] [blame] | 5189 |     // make sure the pause/flush/resume sequence is executed in the right order. | 
 | 5190 |     // If a flush is pending and a track is active but the HW is not paused, force a HW pause | 
 | 5191 |     // before flush and then resume HW. This can happen in case of pause/flush/resume | 
 | 5192 |     // if resume is received before pause is executed. | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5193 |     if (!mStandby && (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5194 |         mOutput->stream->pause(mOutput->stream); | 
 | 5195 |     } | 
| Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5196 |     if (mFlushPending) { | 
 | 5197 |         flushHw_l(); | 
| Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5198 |     } | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5199 |     if (!mStandby && doHwResume) { | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5200 |         mOutput->stream->resume(mOutput->stream); | 
 | 5201 |     } | 
| Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5202 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5203 |     // remove all the tracks that need to be... | 
 | 5204 |     removeTracks_l(*tracksToRemove); | 
 | 5205 |  | 
 | 5206 |     return mixerStatus; | 
 | 5207 | } | 
 | 5208 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5209 | // must be called with thread mutex locked | 
 | 5210 | bool AudioFlinger::OffloadThread::waitingAsyncCallback_l() | 
 | 5211 | { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5212 |     ALOGVV("waitingAsyncCallback_l mWriteAckSequence %d mDrainSequence %d", | 
 | 5213 |           mWriteAckSequence, mDrainSequence); | 
 | 5214 |     if (mUseAsyncWrite && ((mWriteAckSequence & 1) || (mDrainSequence & 1))) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5215 |         return true; | 
 | 5216 |     } | 
 | 5217 |     return false; | 
 | 5218 | } | 
 | 5219 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5220 | bool AudioFlinger::OffloadThread::waitingAsyncCallback() | 
 | 5221 | { | 
 | 5222 |     Mutex::Autolock _l(mLock); | 
 | 5223 |     return waitingAsyncCallback_l(); | 
 | 5224 | } | 
 | 5225 |  | 
 | 5226 | void AudioFlinger::OffloadThread::flushHw_l() | 
 | 5227 | { | 
| Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5228 |     DirectOutputThread::flushHw_l(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5229 |     // Flush anything still waiting in the mixbuffer | 
 | 5230 |     mCurrentWriteLength = 0; | 
 | 5231 |     mBytesRemaining = 0; | 
 | 5232 |     mPausedWriteLength = 0; | 
 | 5233 |     mPausedBytesRemaining = 0; | 
| Haynes Mathew George | 0f02f26 | 2014-01-11 13:03:57 -0800 | [diff] [blame] | 5234 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5235 |     if (mUseAsyncWrite) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5236 |         // discard any pending drain or write ack by incrementing sequence | 
 | 5237 |         mWriteAckSequence = (mWriteAckSequence + 2) & ~1; | 
 | 5238 |         mDrainSequence = (mDrainSequence + 2) & ~1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5239 |         ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5240 |         mCallbackThread->setWriteBlocked(mWriteAckSequence); | 
 | 5241 |         mCallbackThread->setDraining(mDrainSequence); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5242 |     } | 
 | 5243 | } | 
 | 5244 |  | 
 | 5245 | // ---------------------------------------------------------------------------- | 
 | 5246 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5247 | AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5248 |         AudioFlinger::MixerThread* mainThread, audio_io_handle_t id, bool systemReady) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5249 |     :   MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->outDevice(), | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5250 |                     systemReady, DUPLICATING), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5251 |         mWaitTimeMs(UINT_MAX) | 
 | 5252 | { | 
 | 5253 |     addOutputTrack(mainThread); | 
 | 5254 | } | 
 | 5255 |  | 
 | 5256 | AudioFlinger::DuplicatingThread::~DuplicatingThread() | 
 | 5257 | { | 
 | 5258 |     for (size_t i = 0; i < mOutputTracks.size(); i++) { | 
 | 5259 |         mOutputTracks[i]->destroy(); | 
 | 5260 |     } | 
 | 5261 | } | 
 | 5262 |  | 
 | 5263 | void AudioFlinger::DuplicatingThread::threadLoop_mix() | 
 | 5264 | { | 
 | 5265 |     // mix buffers... | 
 | 5266 |     if (outputsReady(outputTracks)) { | 
 | 5267 |         mAudioMixer->process(AudioBufferProvider::kInvalidPTS); | 
 | 5268 |     } else { | 
| Eric Laurent | 02b5708 | 2014-11-07 17:28:28 -0800 | [diff] [blame] | 5269 |         if (mMixerBufferValid) { | 
 | 5270 |             memset(mMixerBuffer, 0, mMixerBufferSize); | 
 | 5271 |         } else { | 
 | 5272 |             memset(mSinkBuffer, 0, mSinkBufferSize); | 
 | 5273 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5274 |     } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5275 |     mSleepTimeUs = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5276 |     writeFrames = mNormalFrameCount; | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5277 |     mCurrentWriteLength = mSinkBufferSize; | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5278 |     mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5279 | } | 
 | 5280 |  | 
 | 5281 | void AudioFlinger::DuplicatingThread::threadLoop_sleepTime() | 
 | 5282 | { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5283 |     if (mSleepTimeUs == 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5284 |         if (mMixerStatus == MIXER_TRACKS_ENABLED) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5285 |             mSleepTimeUs = mActiveSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5286 |         } else { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5287 |             mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5288 |         } | 
 | 5289 |     } else if (mBytesWritten != 0) { | 
 | 5290 |         if (mMixerStatus == MIXER_TRACKS_ENABLED) { | 
 | 5291 |             writeFrames = mNormalFrameCount; | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5292 |             memset(mSinkBuffer, 0, mSinkBufferSize); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5293 |         } else { | 
 | 5294 |             // flush remaining overflow buffers in output tracks | 
 | 5295 |             writeFrames = 0; | 
 | 5296 |         } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5297 |         mSleepTimeUs = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5298 |     } | 
 | 5299 | } | 
 | 5300 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5301 | ssize_t AudioFlinger::DuplicatingThread::threadLoop_write() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5302 | { | 
 | 5303 |     for (size_t i = 0; i < outputTracks.size(); i++) { | 
| Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5304 |         outputTracks[i]->write(mSinkBuffer, writeFrames); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5305 |     } | 
| Eric Laurent | 2c3740f | 2013-10-30 16:57:06 -0700 | [diff] [blame] | 5306 |     mStandby = false; | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5307 |     return (ssize_t)mSinkBufferSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5308 | } | 
 | 5309 |  | 
 | 5310 | void AudioFlinger::DuplicatingThread::threadLoop_standby() | 
 | 5311 | { | 
 | 5312 |     // DuplicatingThread implements standby by stopping all tracks | 
 | 5313 |     for (size_t i = 0; i < outputTracks.size(); i++) { | 
 | 5314 |         outputTracks[i]->stop(); | 
 | 5315 |     } | 
 | 5316 | } | 
 | 5317 |  | 
 | 5318 | void AudioFlinger::DuplicatingThread::saveOutputTracks() | 
 | 5319 | { | 
 | 5320 |     outputTracks = mOutputTracks; | 
 | 5321 | } | 
 | 5322 |  | 
 | 5323 | void AudioFlinger::DuplicatingThread::clearOutputTracks() | 
 | 5324 | { | 
 | 5325 |     outputTracks.clear(); | 
 | 5326 | } | 
 | 5327 |  | 
 | 5328 | void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) | 
 | 5329 | { | 
 | 5330 |     Mutex::Autolock _l(mLock); | 
| Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5331 |     // The downstream MixerThread consumes thread->frameCount() amount of frames per mix pass. | 
 | 5332 |     // Adjust for thread->sampleRate() to determine minimum buffer frame count. | 
 | 5333 |     // Then triple buffer because Threads do not run synchronously and may not be clock locked. | 
 | 5334 |     const size_t frameCount = | 
 | 5335 |             3 * sourceFramesNeeded(mSampleRate, thread->frameCount(), thread->sampleRate()); | 
 | 5336 |     // TODO: Consider asynchronous sample rate conversion to handle clock disparity | 
 | 5337 |     // from different OutputTracks and their associated MixerThreads (e.g. one may | 
 | 5338 |     // nearly empty and the other may be dropping data). | 
 | 5339 |  | 
 | 5340 |     sp<OutputTrack> outputTrack = new OutputTrack(thread, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5341 |                                             this, | 
 | 5342 |                                             mSampleRate, | 
| Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5343 |                                             mFormat, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5344 |                                             mChannelMask, | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 5345 |                                             frameCount, | 
 | 5346 |                                             IPCThreadState::self()->getCallingUid()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5347 |     if (outputTrack->cblk() != NULL) { | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5348 |         thread->setStreamVolume(AUDIO_STREAM_PATCH, 1.0f); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5349 |         mOutputTracks.add(outputTrack); | 
| Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5350 |         ALOGV("addOutputTrack() track %p, on thread %p", outputTrack.get(), thread); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5351 |         updateWaitTime_l(); | 
 | 5352 |     } | 
 | 5353 | } | 
 | 5354 |  | 
 | 5355 | void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) | 
 | 5356 | { | 
 | 5357 |     Mutex::Autolock _l(mLock); | 
 | 5358 |     for (size_t i = 0; i < mOutputTracks.size(); i++) { | 
 | 5359 |         if (mOutputTracks[i]->thread() == thread) { | 
 | 5360 |             mOutputTracks[i]->destroy(); | 
 | 5361 |             mOutputTracks.removeAt(i); | 
 | 5362 |             updateWaitTime_l(); | 
| Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 5363 |             if (thread->getOutput() == mOutput) { | 
 | 5364 |                 mOutput = NULL; | 
 | 5365 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5366 |             return; | 
 | 5367 |         } | 
 | 5368 |     } | 
| Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 5369 |     ALOGV("removeOutputTrack(): unknown thread: %p", thread); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5370 | } | 
 | 5371 |  | 
 | 5372 | // caller must hold mLock | 
 | 5373 | void AudioFlinger::DuplicatingThread::updateWaitTime_l() | 
 | 5374 | { | 
 | 5375 |     mWaitTimeMs = UINT_MAX; | 
 | 5376 |     for (size_t i = 0; i < mOutputTracks.size(); i++) { | 
 | 5377 |         sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); | 
 | 5378 |         if (strong != 0) { | 
 | 5379 |             uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); | 
 | 5380 |             if (waitTimeMs < mWaitTimeMs) { | 
 | 5381 |                 mWaitTimeMs = waitTimeMs; | 
 | 5382 |             } | 
 | 5383 |         } | 
 | 5384 |     } | 
 | 5385 | } | 
 | 5386 |  | 
 | 5387 |  | 
 | 5388 | bool AudioFlinger::DuplicatingThread::outputsReady( | 
 | 5389 |         const SortedVector< sp<OutputTrack> > &outputTracks) | 
 | 5390 | { | 
 | 5391 |     for (size_t i = 0; i < outputTracks.size(); i++) { | 
 | 5392 |         sp<ThreadBase> thread = outputTracks[i]->thread().promote(); | 
 | 5393 |         if (thread == 0) { | 
 | 5394 |             ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", | 
 | 5395 |                     outputTracks[i].get()); | 
 | 5396 |             return false; | 
 | 5397 |         } | 
 | 5398 |         PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); | 
 | 5399 |         // see note at standby() declaration | 
 | 5400 |         if (playbackThread->standby() && !playbackThread->isSuspended()) { | 
 | 5401 |             ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), | 
 | 5402 |                     thread.get()); | 
 | 5403 |             return false; | 
 | 5404 |         } | 
 | 5405 |     } | 
 | 5406 |     return true; | 
 | 5407 | } | 
 | 5408 |  | 
 | 5409 | uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() const | 
 | 5410 | { | 
 | 5411 |     return (mWaitTimeMs * 1000) / 2; | 
 | 5412 | } | 
 | 5413 |  | 
 | 5414 | void AudioFlinger::DuplicatingThread::cacheParameters_l() | 
 | 5415 | { | 
 | 5416 |     // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first | 
 | 5417 |     updateWaitTime_l(); | 
 | 5418 |  | 
 | 5419 |     MixerThread::cacheParameters_l(); | 
 | 5420 | } | 
 | 5421 |  | 
 | 5422 | // ---------------------------------------------------------------------------- | 
 | 5423 | //      Record | 
 | 5424 | // ---------------------------------------------------------------------------- | 
 | 5425 |  | 
 | 5426 | AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, | 
 | 5427 |                                          AudioStreamIn *input, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5428 |                                          audio_io_handle_t id, | 
| Eric Laurent | d3922f7 | 2013-02-01 17:57:04 -0800 | [diff] [blame] | 5429 |                                          audio_devices_t outDevice, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5430 |                                          audio_devices_t inDevice, | 
 | 5431 |                                          bool systemReady | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 5432 | #ifdef TEE_SINK | 
 | 5433 |                                          , const sp<NBAIO_Sink>& teeSink | 
 | 5434 | #endif | 
 | 5435 |                                          ) : | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5436 |     ThreadBase(audioFlinger, id, outDevice, inDevice, RECORD, systemReady), | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5437 |     mInput(input), mActiveTracksGen(0), mRsmpInBuffer(NULL), | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 5438 |     // mRsmpInFrames and mRsmpInFramesP2 are set by readInputParameters_l() | 
| Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5439 |     mRsmpInRear(0) | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 5440 | #ifdef TEE_SINK | 
 | 5441 |     , mTeeSink(teeSink) | 
 | 5442 | #endif | 
| Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 5443 |     , mReadOnlyHeap(new MemoryDealer(kRecordThreadReadOnlyHeapSize, | 
 | 5444 |             "RecordThreadRO", MemoryHeapBase::READ_ONLY)) | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5445 |     // mFastCapture below | 
 | 5446 |     , mFastCaptureFutex(0) | 
 | 5447 |     // mInputSource | 
 | 5448 |     // mPipeSink | 
 | 5449 |     // mPipeSource | 
 | 5450 |     , mPipeFramesP2(0) | 
 | 5451 |     // mPipeMemory | 
 | 5452 |     // mFastCaptureNBLogWriter | 
| Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 5453 |     , mFastTrackAvail(false) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5454 | { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 5455 |     snprintf(mThreadName, kThreadNameLength, "AudioIn_%X", id); | 
 | 5456 |     mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5457 |  | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 5458 |     readInputParameters_l(); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5459 |  | 
 | 5460 |     // create an NBAIO source for the HAL input stream, and negotiate | 
 | 5461 |     mInputSource = new AudioStreamInSource(input->stream); | 
 | 5462 |     size_t numCounterOffers = 0; | 
 | 5463 |     const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; | 
 | 5464 |     ssize_t index = mInputSource->negotiate(offers, 1, NULL, numCounterOffers); | 
 | 5465 |     ALOG_ASSERT(index == 0); | 
 | 5466 |  | 
 | 5467 |     // initialize fast capture depending on configuration | 
 | 5468 |     bool initFastCapture; | 
 | 5469 |     switch (kUseFastCapture) { | 
 | 5470 |     case FastCapture_Never: | 
 | 5471 |         initFastCapture = false; | 
 | 5472 |         break; | 
 | 5473 |     case FastCapture_Always: | 
 | 5474 |         initFastCapture = true; | 
 | 5475 |         break; | 
 | 5476 |     case FastCapture_Static: | 
 | 5477 |         uint32_t primaryOutputSampleRate; | 
 | 5478 |         { | 
 | 5479 |             AutoMutex _l(audioFlinger->mHardwareLock); | 
 | 5480 |             primaryOutputSampleRate = audioFlinger->mPrimaryOutputSampleRate; | 
 | 5481 |         } | 
 | 5482 |         initFastCapture = | 
 | 5483 |                 // either capture sample rate is same as (a reasonable) primary output sample rate | 
| Andy Hung | db4c031 | 2015-05-06 08:46:52 -0700 | [diff] [blame] | 5484 |                 ((isMusicRate(primaryOutputSampleRate) && | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5485 |                     (mSampleRate == primaryOutputSampleRate)) || | 
 | 5486 |                 // or primary output sample rate is unknown, and capture sample rate is reasonable | 
 | 5487 |                 ((primaryOutputSampleRate == 0) && | 
| Andy Hung | db4c031 | 2015-05-06 08:46:52 -0700 | [diff] [blame] | 5488 |                         isMusicRate(mSampleRate))) && | 
| Glenn Kasten | 9f81de3 | 2014-07-27 15:02:23 -0700 | [diff] [blame] | 5489 |                 // and the buffer size is < 12 ms | 
 | 5490 |                 (mFrameCount * 1000) / mSampleRate < 12; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5491 |         break; | 
 | 5492 |     // case FastCapture_Dynamic: | 
 | 5493 |     } | 
 | 5494 |  | 
 | 5495 |     if (initFastCapture) { | 
| Glenn Kasten | d198b85 | 2015-03-16 14:55:53 -0700 | [diff] [blame] | 5496 |         // create a Pipe for FastCapture to write to, and for us and fast tracks to read from | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5497 |         NBAIO_Format format = mInputSource->format(); | 
| Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 5498 |         size_t pipeFramesP2 = roundup(mSampleRate / 25);    // double-buffering of 20 ms each | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5499 |         size_t pipeSize = pipeFramesP2 * Format_frameSize(format); | 
 | 5500 |         void *pipeBuffer; | 
 | 5501 |         const sp<MemoryDealer> roHeap(readOnlyHeap()); | 
 | 5502 |         sp<IMemory> pipeMemory; | 
 | 5503 |         if ((roHeap == 0) || | 
 | 5504 |                 (pipeMemory = roHeap->allocate(pipeSize)) == 0 || | 
 | 5505 |                 (pipeBuffer = pipeMemory->pointer()) == NULL) { | 
 | 5506 |             ALOGE("not enough memory for pipe buffer size=%zu", pipeSize); | 
 | 5507 |             goto failed; | 
 | 5508 |         } | 
 | 5509 |         // pipe will be shared directly with fast clients, so clear to avoid leaking old information | 
 | 5510 |         memset(pipeBuffer, 0, pipeSize); | 
 | 5511 |         Pipe *pipe = new Pipe(pipeFramesP2, format, pipeBuffer); | 
 | 5512 |         const NBAIO_Format offers[1] = {format}; | 
 | 5513 |         size_t numCounterOffers = 0; | 
 | 5514 |         ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers); | 
 | 5515 |         ALOG_ASSERT(index == 0); | 
 | 5516 |         mPipeSink = pipe; | 
 | 5517 |         PipeReader *pipeReader = new PipeReader(*pipe); | 
 | 5518 |         numCounterOffers = 0; | 
 | 5519 |         index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers); | 
 | 5520 |         ALOG_ASSERT(index == 0); | 
 | 5521 |         mPipeSource = pipeReader; | 
 | 5522 |         mPipeFramesP2 = pipeFramesP2; | 
 | 5523 |         mPipeMemory = pipeMemory; | 
 | 5524 |  | 
 | 5525 |         // create fast capture | 
 | 5526 |         mFastCapture = new FastCapture(); | 
 | 5527 |         FastCaptureStateQueue *sq = mFastCapture->sq(); | 
 | 5528 | #ifdef STATE_QUEUE_DUMP | 
 | 5529 |         // FIXME | 
 | 5530 | #endif | 
 | 5531 |         FastCaptureState *state = sq->begin(); | 
 | 5532 |         state->mCblk = NULL; | 
 | 5533 |         state->mInputSource = mInputSource.get(); | 
 | 5534 |         state->mInputSourceGen++; | 
 | 5535 |         state->mPipeSink = pipe; | 
 | 5536 |         state->mPipeSinkGen++; | 
 | 5537 |         state->mFrameCount = mFrameCount; | 
 | 5538 |         state->mCommand = FastCaptureState::COLD_IDLE; | 
 | 5539 |         // already done in constructor initialization list | 
 | 5540 |         //mFastCaptureFutex = 0; | 
 | 5541 |         state->mColdFutexAddr = &mFastCaptureFutex; | 
 | 5542 |         state->mColdGen++; | 
 | 5543 |         state->mDumpState = &mFastCaptureDumpState; | 
 | 5544 | #ifdef TEE_SINK | 
 | 5545 |         // FIXME | 
 | 5546 | #endif | 
 | 5547 |         mFastCaptureNBLogWriter = audioFlinger->newWriter_l(kFastCaptureLogSize, "FastCapture"); | 
 | 5548 |         state->mNBLogWriter = mFastCaptureNBLogWriter.get(); | 
 | 5549 |         sq->end(); | 
 | 5550 |         sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); | 
 | 5551 |  | 
 | 5552 |         // start the fast capture | 
 | 5553 |         mFastCapture->run("FastCapture", ANDROID_PRIORITY_URGENT_AUDIO); | 
 | 5554 |         pid_t tid = mFastCapture->getTid(); | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5555 |         sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5556 | #ifdef AUDIO_WATCHDOG | 
 | 5557 |         // FIXME | 
 | 5558 | #endif | 
 | 5559 |  | 
| Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 5560 |         mFastTrackAvail = true; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5561 |     } | 
 | 5562 | failed: ; | 
 | 5563 |  | 
 | 5564 |     // FIXME mNormalSource | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5565 | } | 
 | 5566 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5567 | AudioFlinger::RecordThread::~RecordThread() | 
 | 5568 | { | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5569 |     if (mFastCapture != 0) { | 
 | 5570 |         FastCaptureStateQueue *sq = mFastCapture->sq(); | 
 | 5571 |         FastCaptureState *state = sq->begin(); | 
 | 5572 |         if (state->mCommand == FastCaptureState::COLD_IDLE) { | 
 | 5573 |             int32_t old = android_atomic_inc(&mFastCaptureFutex); | 
 | 5574 |             if (old == -1) { | 
 | 5575 |                 (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); | 
 | 5576 |             } | 
 | 5577 |         } | 
 | 5578 |         state->mCommand = FastCaptureState::EXIT; | 
 | 5579 |         sq->end(); | 
 | 5580 |         sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); | 
 | 5581 |         mFastCapture->join(); | 
 | 5582 |         mFastCapture.clear(); | 
 | 5583 |     } | 
 | 5584 |     mAudioFlinger->unregisterWriter(mFastCaptureNBLogWriter); | 
| Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 5585 |     mAudioFlinger->unregisterWriter(mNBLogWriter); | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 5586 |     free(mRsmpInBuffer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5587 | } | 
 | 5588 |  | 
 | 5589 | void AudioFlinger::RecordThread::onFirstRef() | 
 | 5590 | { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 5591 |     run(mThreadName, PRIORITY_URGENT_AUDIO); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5592 | } | 
 | 5593 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5594 | bool AudioFlinger::RecordThread::threadLoop() | 
 | 5595 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5596 |     nsecs_t lastWarning = 0; | 
 | 5597 |  | 
 | 5598 |     inputStandBy(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5599 |  | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5600 | reacquire_wakelock: | 
 | 5601 |     sp<RecordTrack> activeTrack; | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5602 |     int activeTracksGen; | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5603 |     { | 
 | 5604 |         Mutex::Autolock _l(mLock); | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5605 |         size_t size = mActiveTracks.size(); | 
 | 5606 |         activeTracksGen = mActiveTracksGen; | 
 | 5607 |         if (size > 0) { | 
 | 5608 |             // FIXME an arbitrary choice | 
 | 5609 |             activeTrack = mActiveTracks[0]; | 
 | 5610 |             acquireWakeLock_l(activeTrack->uid()); | 
 | 5611 |             if (size > 1) { | 
 | 5612 |                 SortedVector<int> tmp; | 
 | 5613 |                 for (size_t i = 0; i < size; i++) { | 
 | 5614 |                     tmp.add(mActiveTracks[i]->uid()); | 
 | 5615 |                 } | 
 | 5616 |                 updateWakeLockUids_l(tmp); | 
 | 5617 |             } | 
 | 5618 |         } else { | 
 | 5619 |             acquireWakeLock_l(-1); | 
 | 5620 |         } | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5621 |     } | 
 | 5622 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5623 |     // used to request a deferred sleep, to be executed later while mutex is unlocked | 
 | 5624 |     uint32_t sleepUs = 0; | 
 | 5625 |  | 
 | 5626 |     // loop while there is work to do | 
| Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 5627 |     for (;;) { | 
| Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 5628 |         Vector< sp<EffectChain> > effectChains; | 
| Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 5629 |  | 
| Glenn Kasten | 5edadd4 | 2013-08-14 16:30:49 -0700 | [diff] [blame] | 5630 |         // sleep with mutex unlocked | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5631 |         if (sleepUs > 0) { | 
| Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 5632 |             ATRACE_BEGIN("sleep"); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5633 |             usleep(sleepUs); | 
| Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 5634 |             ATRACE_END(); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5635 |             sleepUs = 0; | 
| Glenn Kasten | 5edadd4 | 2013-08-14 16:30:49 -0700 | [diff] [blame] | 5636 |         } | 
 | 5637 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5638 |         // activeTracks accumulates a copy of a subset of mActiveTracks | 
 | 5639 |         Vector< sp<RecordTrack> > activeTracks; | 
 | 5640 |  | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5641 |         // reference to the (first and only) active fast track | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5642 |         sp<RecordTrack> fastTrack; | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5643 |  | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5644 |         // reference to a fast track which is about to be removed | 
 | 5645 |         sp<RecordTrack> fastTrackToRemove; | 
 | 5646 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5647 |         { // scope for mLock | 
 | 5648 |             Mutex::Autolock _l(mLock); | 
| Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 5649 |  | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 5650 |             processConfigEvents_l(); | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5651 |  | 
| Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 5652 |             // check exitPending here because checkForNewParameters_l() and | 
 | 5653 |             // checkForNewParameters_l() can temporarily release mLock | 
 | 5654 |             if (exitPending()) { | 
 | 5655 |                 break; | 
 | 5656 |             } | 
 | 5657 |  | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5658 |             // if no active track(s), then standby and release wakelock | 
 | 5659 |             size_t size = mActiveTracks.size(); | 
 | 5660 |             if (size == 0) { | 
| Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 5661 |                 standbyIfNotAlreadyInStandby(); | 
| Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 5662 |                 // exitPending() can't become true here | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5663 |                 releaseWakeLock_l(); | 
 | 5664 |                 ALOGV("RecordThread: loop stopping"); | 
 | 5665 |                 // go to sleep | 
 | 5666 |                 mWaitWorkCV.wait(mLock); | 
 | 5667 |                 ALOGV("RecordThread: loop starting"); | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5668 |                 goto reacquire_wakelock; | 
 | 5669 |             } | 
 | 5670 |  | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5671 |             if (mActiveTracksGen != activeTracksGen) { | 
 | 5672 |                 activeTracksGen = mActiveTracksGen; | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5673 |                 SortedVector<int> tmp; | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5674 |                 for (size_t i = 0; i < size; i++) { | 
 | 5675 |                     tmp.add(mActiveTracks[i]->uid()); | 
 | 5676 |                 } | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5677 |                 updateWakeLockUids_l(tmp); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5678 |             } | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5679 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5680 |             bool doBroadcast = false; | 
 | 5681 |             for (size_t i = 0; i < size; ) { | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5682 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5683 |                 activeTrack = mActiveTracks[i]; | 
 | 5684 |                 if (activeTrack->isTerminated()) { | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5685 |                     if (activeTrack->isFastTrack()) { | 
 | 5686 |                         ALOG_ASSERT(fastTrackToRemove == 0); | 
 | 5687 |                         fastTrackToRemove = activeTrack; | 
 | 5688 |                     } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5689 |                     removeTrack_l(activeTrack); | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5690 |                     mActiveTracks.remove(activeTrack); | 
 | 5691 |                     mActiveTracksGen++; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5692 |                     size--; | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5693 |                     continue; | 
 | 5694 |                 } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5695 |  | 
 | 5696 |                 TrackBase::track_state activeTrackState = activeTrack->mState; | 
 | 5697 |                 switch (activeTrackState) { | 
 | 5698 |  | 
 | 5699 |                 case TrackBase::PAUSING: | 
 | 5700 |                     mActiveTracks.remove(activeTrack); | 
 | 5701 |                     mActiveTracksGen++; | 
 | 5702 |                     doBroadcast = true; | 
 | 5703 |                     size--; | 
 | 5704 |                     continue; | 
 | 5705 |  | 
 | 5706 |                 case TrackBase::STARTING_1: | 
 | 5707 |                     sleepUs = 10000; | 
 | 5708 |                     i++; | 
 | 5709 |                     continue; | 
 | 5710 |  | 
 | 5711 |                 case TrackBase::STARTING_2: | 
 | 5712 |                     doBroadcast = true; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5713 |                     mStandby = false; | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5714 |                     activeTrack->mState = TrackBase::ACTIVE; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5715 |                     break; | 
 | 5716 |  | 
 | 5717 |                 case TrackBase::ACTIVE: | 
 | 5718 |                     break; | 
 | 5719 |  | 
 | 5720 |                 case TrackBase::IDLE: | 
 | 5721 |                     i++; | 
 | 5722 |                     continue; | 
 | 5723 |  | 
 | 5724 |                 default: | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 5725 |                     LOG_ALWAYS_FATAL("Unexpected activeTrackState %d", activeTrackState); | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5726 |                 } | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5727 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5728 |                 activeTracks.add(activeTrack); | 
 | 5729 |                 i++; | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5730 |  | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5731 |                 if (activeTrack->isFastTrack()) { | 
 | 5732 |                     ALOG_ASSERT(!mFastTrackAvail); | 
 | 5733 |                     ALOG_ASSERT(fastTrack == 0); | 
 | 5734 |                     fastTrack = activeTrack; | 
 | 5735 |                 } | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5736 |             } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5737 |             if (doBroadcast) { | 
 | 5738 |                 mStartStopCond.broadcast(); | 
 | 5739 |             } | 
 | 5740 |  | 
 | 5741 |             // sleep if there are no active tracks to process | 
 | 5742 |             if (activeTracks.size() == 0) { | 
 | 5743 |                 if (sleepUs == 0) { | 
 | 5744 |                     sleepUs = kRecordThreadSleepUs; | 
 | 5745 |                 } | 
 | 5746 |                 continue; | 
 | 5747 |             } | 
 | 5748 |             sleepUs = 0; | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5749 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5750 |             lockEffectChains_l(effectChains); | 
 | 5751 |         } | 
 | 5752 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5753 |         // thread mutex is now unlocked, mActiveTracks unknown, activeTracks.size() > 0 | 
| Glenn Kasten | 7165268 | 2013-08-14 15:17:55 -0700 | [diff] [blame] | 5754 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5755 |         size_t size = effectChains.size(); | 
 | 5756 |         for (size_t i = 0; i < size; i++) { | 
| Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5757 |             // thread mutex is not locked, but effect chain is locked | 
 | 5758 |             effectChains[i]->process_l(); | 
 | 5759 |         } | 
 | 5760 |  | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5761 |         // 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] | 5762 |         if (mFastCapture != 0) { | 
 | 5763 |             FastCaptureStateQueue *sq = mFastCapture->sq(); | 
 | 5764 |             FastCaptureState *state = sq->begin(); | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5765 |             bool didModify = false; | 
 | 5766 |             FastCaptureStateQueue::block_t block = FastCaptureStateQueue::BLOCK_UNTIL_PUSHED; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5767 |             if (state->mCommand != FastCaptureState::READ_WRITE /* FIXME && | 
 | 5768 |                     (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)*/) { | 
 | 5769 |                 if (state->mCommand == FastCaptureState::COLD_IDLE) { | 
 | 5770 |                     int32_t old = android_atomic_inc(&mFastCaptureFutex); | 
 | 5771 |                     if (old == -1) { | 
 | 5772 |                         (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); | 
 | 5773 |                     } | 
 | 5774 |                 } | 
 | 5775 |                 state->mCommand = FastCaptureState::READ_WRITE; | 
 | 5776 | #if 0   // FIXME | 
 | 5777 |                 mFastCaptureDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? | 
| Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 5778 |                         FastThreadDumpState::kSamplingNforLowRamDevice : | 
 | 5779 |                         FastThreadDumpState::kSamplingN); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5780 | #endif | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5781 |                 didModify = true; | 
 | 5782 |             } | 
 | 5783 |             audio_track_cblk_t *cblkOld = state->mCblk; | 
 | 5784 |             audio_track_cblk_t *cblkNew = fastTrack != 0 ? fastTrack->cblk() : NULL; | 
 | 5785 |             if (cblkNew != cblkOld) { | 
 | 5786 |                 state->mCblk = cblkNew; | 
 | 5787 |                 // block until acked if removing a fast track | 
 | 5788 |                 if (cblkOld != NULL) { | 
 | 5789 |                     block = FastCaptureStateQueue::BLOCK_UNTIL_ACKED; | 
 | 5790 |                 } | 
 | 5791 |                 didModify = true; | 
 | 5792 |             } | 
 | 5793 |             sq->end(didModify); | 
 | 5794 |             if (didModify) { | 
 | 5795 |                 sq->push(block); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5796 | #if 0 | 
 | 5797 |                 if (kUseFastCapture == FastCapture_Dynamic) { | 
 | 5798 |                     mNormalSource = mPipeSource; | 
 | 5799 |                 } | 
 | 5800 | #endif | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5801 |             } | 
 | 5802 |         } | 
 | 5803 |  | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5804 |         // now run the fast track destructor with thread mutex unlocked | 
 | 5805 |         fastTrackToRemove.clear(); | 
 | 5806 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5807 |         // Read from HAL to keep up with fastest client if multiple active tracks, not slowest one. | 
 | 5808 |         // Only the client(s) that are too slow will overrun. But if even the fastest client is too | 
 | 5809 |         // slow, then this RecordThread will overrun by not calling HAL read often enough. | 
 | 5810 |         // If destination is non-contiguous, first read past the nominal end of buffer, then | 
 | 5811 |         // copy to the right place.  Permitted because mRsmpInBuffer was over-allocated. | 
| Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5812 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5813 |         int32_t rear = mRsmpInRear & (mRsmpInFramesP2 - 1); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5814 |         ssize_t framesRead; | 
 | 5815 |  | 
 | 5816 |         // If an NBAIO source is present, use it to read the normal capture's data | 
 | 5817 |         if (mPipeSource != 0) { | 
 | 5818 |             size_t framesToRead = mBufferSize / mFrameSize; | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 5819 |             framesRead = mPipeSource->read((uint8_t*)mRsmpInBuffer + rear * mFrameSize, | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5820 |                     framesToRead, AudioBufferProvider::kInvalidPTS); | 
 | 5821 |             if (framesRead == 0) { | 
 | 5822 |                 // since pipe is non-blocking, simulate blocking input | 
 | 5823 |                 sleepUs = (framesToRead * 1000000LL) / mSampleRate; | 
 | 5824 |             } | 
 | 5825 |         // otherwise use the HAL / AudioStreamIn directly | 
 | 5826 |         } else { | 
 | 5827 |             ssize_t bytesRead = mInput->stream->read(mInput->stream, | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 5828 |                     (uint8_t*)mRsmpInBuffer + rear * mFrameSize, mBufferSize); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5829 |             if (bytesRead < 0) { | 
 | 5830 |                 framesRead = bytesRead; | 
 | 5831 |             } else { | 
 | 5832 |                 framesRead = bytesRead / mFrameSize; | 
 | 5833 |             } | 
 | 5834 |         } | 
 | 5835 |  | 
 | 5836 |         if (framesRead < 0 || (framesRead == 0 && mPipeSource == 0)) { | 
 | 5837 |             ALOGE("read failed: framesRead=%d", framesRead); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5838 |             // Force input into standby so that it tries to recover at next read attempt | 
 | 5839 |             inputStandBy(); | 
 | 5840 |             sleepUs = kRecordThreadSleepUs; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5841 |         } | 
 | 5842 |         if (framesRead <= 0) { | 
| Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 5843 |             goto unlock; | 
| Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5844 |         } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5845 |         ALOG_ASSERT(framesRead > 0); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5846 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5847 |         if (mTeeSink != 0) { | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 5848 |             (void) mTeeSink->write((uint8_t*)mRsmpInBuffer + rear * mFrameSize, framesRead); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5849 |         } | 
 | 5850 |         // 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] | 5851 |         { | 
 | 5852 |             size_t part1 = mRsmpInFramesP2 - rear; | 
 | 5853 |             if ((size_t) framesRead > part1) { | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 5854 |                 memcpy(mRsmpInBuffer, (uint8_t*)mRsmpInBuffer + mRsmpInFramesP2 * mFrameSize, | 
| Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 5855 |                         (framesRead - part1) * mFrameSize); | 
 | 5856 |             } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5857 |         } | 
 | 5858 |         rear = mRsmpInRear += framesRead; | 
 | 5859 |  | 
 | 5860 |         size = activeTracks.size(); | 
 | 5861 |         // loop over each active track | 
 | 5862 |         for (size_t i = 0; i < size; i++) { | 
 | 5863 |             activeTrack = activeTracks[i]; | 
 | 5864 |  | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5865 |             // skip fast tracks, as those are handled directly by FastCapture | 
 | 5866 |             if (activeTrack->isFastTrack()) { | 
 | 5867 |                 continue; | 
 | 5868 |             } | 
 | 5869 |  | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 5870 |             // TODO: This code probably should be moved to RecordTrack. | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 5871 |             // TODO: Update the activeTrack buffer converter in case of reconfigure. | 
 | 5872 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5873 |             enum { | 
 | 5874 |                 OVERRUN_UNKNOWN, | 
 | 5875 |                 OVERRUN_TRUE, | 
 | 5876 |                 OVERRUN_FALSE | 
 | 5877 |             } overrun = OVERRUN_UNKNOWN; | 
 | 5878 |  | 
 | 5879 |             // loop over getNextBuffer to handle circular sink | 
 | 5880 |             for (;;) { | 
 | 5881 |  | 
 | 5882 |                 activeTrack->mSink.frameCount = ~0; | 
 | 5883 |                 status_t status = activeTrack->getNextBuffer(&activeTrack->mSink); | 
 | 5884 |                 size_t framesOut = activeTrack->mSink.frameCount; | 
 | 5885 |                 LOG_ALWAYS_FATAL_IF((status == OK) != (framesOut > 0)); | 
 | 5886 |  | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 5887 |                 // check available frames and handle overrun conditions | 
 | 5888 |                 // if the record track isn't draining fast enough. | 
 | 5889 |                 bool hasOverrun; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5890 |                 size_t framesIn; | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 5891 |                 activeTrack->mResamplerBufferProvider->sync(&framesIn, &hasOverrun); | 
 | 5892 |                 if (hasOverrun) { | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5893 |                     overrun = OVERRUN_TRUE; | 
 | 5894 |                 } | 
| Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5895 |                 if (framesOut == 0 || framesIn == 0) { | 
 | 5896 |                     break; | 
 | 5897 |                 } | 
 | 5898 |  | 
| Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 5899 |                 // Don't allow framesOut to be larger than what is possible with resampling | 
 | 5900 |                 // from framesIn. | 
 | 5901 |                 // This isn't strictly necessary but helps limit buffer resizing in | 
 | 5902 |                 // RecordBufferConverter.  TODO: remove when no longer needed. | 
 | 5903 |                 framesOut = min(framesOut, | 
 | 5904 |                         destinationFramesPossible( | 
 | 5905 |                                 framesIn, mSampleRate, activeTrack->mSampleRate)); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 5906 |                 // process frames from the RecordThread buffer provider to the RecordTrack buffer | 
 | 5907 |                 framesOut = activeTrack->mRecordBufferConverter->convert( | 
 | 5908 |                         activeTrack->mSink.raw, activeTrack->mResamplerBufferProvider, framesOut); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5909 |  | 
 | 5910 |                 if (framesOut > 0 && (overrun == OVERRUN_UNKNOWN)) { | 
 | 5911 |                     overrun = OVERRUN_FALSE; | 
 | 5912 |                 } | 
 | 5913 |  | 
 | 5914 |                 if (activeTrack->mFramesToDrop == 0) { | 
 | 5915 |                     if (framesOut > 0) { | 
 | 5916 |                         activeTrack->mSink.frameCount = framesOut; | 
 | 5917 |                         activeTrack->releaseBuffer(&activeTrack->mSink); | 
 | 5918 |                     } | 
 | 5919 |                 } else { | 
 | 5920 |                     // FIXME could do a partial drop of framesOut | 
 | 5921 |                     if (activeTrack->mFramesToDrop > 0) { | 
 | 5922 |                         activeTrack->mFramesToDrop -= framesOut; | 
 | 5923 |                         if (activeTrack->mFramesToDrop <= 0) { | 
| Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 5924 |                             activeTrack->clearSyncStartEvent(); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5925 |                         } | 
 | 5926 |                     } else { | 
 | 5927 |                         activeTrack->mFramesToDrop += framesOut; | 
 | 5928 |                         if (activeTrack->mFramesToDrop >= 0 || activeTrack->mSyncStartEvent == 0 || | 
 | 5929 |                                 activeTrack->mSyncStartEvent->isCancelled()) { | 
 | 5930 |                             ALOGW("Synced record %s, session %d, trigger session %d", | 
 | 5931 |                                   (activeTrack->mFramesToDrop >= 0) ? "timed out" : "cancelled", | 
 | 5932 |                                   activeTrack->sessionId(), | 
 | 5933 |                                   (activeTrack->mSyncStartEvent != 0) ? | 
 | 5934 |                                           activeTrack->mSyncStartEvent->triggerSession() : 0); | 
| Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 5935 |                             activeTrack->clearSyncStartEvent(); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5936 |                         } | 
 | 5937 |                     } | 
 | 5938 |                 } | 
 | 5939 |  | 
 | 5940 |                 if (framesOut == 0) { | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5941 |                     break; | 
| Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5942 |                 } | 
 | 5943 |             } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5944 |  | 
 | 5945 |             switch (overrun) { | 
 | 5946 |             case OVERRUN_TRUE: | 
 | 5947 |                 // client isn't retrieving buffers fast enough | 
 | 5948 |                 if (!activeTrack->setOverflow()) { | 
 | 5949 |                     nsecs_t now = systemTime(); | 
 | 5950 |                     // FIXME should lastWarning per track? | 
 | 5951 |                     if ((now - lastWarning) > kWarningThrottleNs) { | 
 | 5952 |                         ALOGW("RecordThread: buffer overflow"); | 
 | 5953 |                         lastWarning = now; | 
 | 5954 |                     } | 
 | 5955 |                 } | 
 | 5956 |                 break; | 
 | 5957 |             case OVERRUN_FALSE: | 
 | 5958 |                 activeTrack->clearOverflow(); | 
 | 5959 |                 break; | 
 | 5960 |             case OVERRUN_UNKNOWN: | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5961 |                 break; | 
 | 5962 |             } | 
 | 5963 |  | 
| Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5964 |         } | 
 | 5965 |  | 
| Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 5966 | unlock: | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5967 |         // enable changes in effect chain | 
 | 5968 |         unlockEffectChains(effectChains); | 
| Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 5969 |         // 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] | 5970 |     } | 
 | 5971 |  | 
| Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 5972 |     standbyIfNotAlreadyInStandby(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5973 |  | 
 | 5974 |     { | 
 | 5975 |         Mutex::Autolock _l(mLock); | 
| Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 5976 |         for (size_t i = 0; i < mTracks.size(); i++) { | 
 | 5977 |             sp<RecordTrack> track = mTracks[i]; | 
 | 5978 |             track->invalidate(); | 
 | 5979 |         } | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5980 |         mActiveTracks.clear(); | 
 | 5981 |         mActiveTracksGen++; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5982 |         mStartStopCond.broadcast(); | 
 | 5983 |     } | 
 | 5984 |  | 
 | 5985 |     releaseWakeLock(); | 
 | 5986 |  | 
 | 5987 |     ALOGV("RecordThread %p exiting", this); | 
 | 5988 |     return false; | 
 | 5989 | } | 
 | 5990 |  | 
| Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 5991 | void AudioFlinger::RecordThread::standbyIfNotAlreadyInStandby() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5992 | { | 
 | 5993 |     if (!mStandby) { | 
 | 5994 |         inputStandBy(); | 
 | 5995 |         mStandby = true; | 
 | 5996 |     } | 
 | 5997 | } | 
 | 5998 |  | 
 | 5999 | void AudioFlinger::RecordThread::inputStandBy() | 
 | 6000 | { | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6001 |     // Idle the fast capture if it's currently running | 
 | 6002 |     if (mFastCapture != 0) { | 
 | 6003 |         FastCaptureStateQueue *sq = mFastCapture->sq(); | 
 | 6004 |         FastCaptureState *state = sq->begin(); | 
 | 6005 |         if (!(state->mCommand & FastCaptureState::IDLE)) { | 
 | 6006 |             state->mCommand = FastCaptureState::COLD_IDLE; | 
 | 6007 |             state->mColdFutexAddr = &mFastCaptureFutex; | 
 | 6008 |             state->mColdGen++; | 
 | 6009 |             mFastCaptureFutex = 0; | 
 | 6010 |             sq->end(); | 
 | 6011 |             // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now | 
 | 6012 |             sq->push(FastCaptureStateQueue::BLOCK_UNTIL_ACKED); | 
 | 6013 | #if 0 | 
 | 6014 |             if (kUseFastCapture == FastCapture_Dynamic) { | 
 | 6015 |                 // FIXME | 
 | 6016 |             } | 
 | 6017 | #endif | 
 | 6018 | #ifdef AUDIO_WATCHDOG | 
 | 6019 |             // FIXME | 
 | 6020 | #endif | 
 | 6021 |         } else { | 
 | 6022 |             sq->end(false /*didModify*/); | 
 | 6023 |         } | 
 | 6024 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6025 |     mInput->stream->common.standby(&mInput->stream->common); | 
 | 6026 | } | 
 | 6027 |  | 
| Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 6028 | // RecordThread::createRecordTrack_l() must be called with AudioFlinger::mLock held | 
| Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 6029 | sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l( | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6030 |         const sp<AudioFlinger::Client>& client, | 
 | 6031 |         uint32_t sampleRate, | 
 | 6032 |         audio_format_t format, | 
 | 6033 |         audio_channel_mask_t channelMask, | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6034 |         size_t *pFrameCount, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6035 |         int sessionId, | 
| Glenn Kasten | 7df8c0b | 2014-07-03 12:23:29 -0700 | [diff] [blame] | 6036 |         size_t *notificationFrames, | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 6037 |         int uid, | 
| Glenn Kasten | ddb0ccf | 2013-07-31 16:14:50 -0700 | [diff] [blame] | 6038 |         IAudioFlinger::track_flags_t *flags, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6039 |         pid_t tid, | 
 | 6040 |         status_t *status) | 
 | 6041 | { | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6042 |     size_t frameCount = *pFrameCount; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6043 |     sp<RecordTrack> track; | 
 | 6044 |     status_t lStatus; | 
 | 6045 |  | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6046 |     // client expresses a preference for FAST, but we get the final say | 
 | 6047 |     if (*flags & IAudioFlinger::TRACK_FAST) { | 
 | 6048 |       if ( | 
| Glenn Kasten | b7fbf7e | 2015-03-18 12:57:28 -0700 | [diff] [blame] | 6049 |             // we formerly checked for a callback handler (non-0 tid), | 
 | 6050 |             // but that is no longer required for TRANSFER_OBTAIN mode | 
 | 6051 |             // | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6052 |             // frame count is not specified, or is exactly the pipe depth | 
 | 6053 |             ((frameCount == 0) || (frameCount == mPipeFramesP2)) && | 
| Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 6054 |             // PCM data | 
 | 6055 |             audio_is_linear_pcm(format) && | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6056 |             // native format | 
 | 6057 |             (format == mFormat) && | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6058 |             // native channel mask | 
 | 6059 |             (channelMask == mChannelMask) && | 
 | 6060 |             // native hardware sample rate | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6061 |             (sampleRate == mSampleRate) && | 
| Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 6062 |             // record thread has an associated fast capture | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6063 |             hasFastCapture() && | 
 | 6064 |             // there are sufficient fast track slots available | 
 | 6065 |             mFastTrackAvail | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6066 |         ) { | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6067 |         ALOGV("AUDIO_INPUT_FLAG_FAST accepted: frameCount=%u mFrameCount=%u", | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6068 |                 frameCount, mFrameCount); | 
 | 6069 |       } else { | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6070 |         ALOGV("AUDIO_INPUT_FLAG_FAST denied: frameCount=%u mFrameCount=%u mPipeFramesP2=%u " | 
 | 6071 |                 "format=%#x isLinear=%d channelMask=%#x sampleRate=%u mSampleRate=%u " | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6072 |                 "hasFastCapture=%d tid=%d mFastTrackAvail=%d", | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6073 |                 frameCount, mFrameCount, mPipeFramesP2, | 
 | 6074 |                 format, audio_is_linear_pcm(format), channelMask, sampleRate, mSampleRate, | 
 | 6075 |                 hasFastCapture(), tid, mFastTrackAvail); | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6076 |         *flags &= ~IAudioFlinger::TRACK_FAST; | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6077 |       } | 
 | 6078 |     } | 
 | 6079 |  | 
 | 6080 |     // compute track buffer size in frames, and suggest the notification frame count | 
 | 6081 |     if (*flags & IAudioFlinger::TRACK_FAST) { | 
 | 6082 |         // fast track: frame count is exactly the pipe depth | 
 | 6083 |         frameCount = mPipeFramesP2; | 
 | 6084 |         // ignore requested notificationFrames, and always notify exactly once every HAL buffer | 
 | 6085 |         *notificationFrames = mFrameCount; | 
 | 6086 |     } else { | 
| Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 6087 |         // not fast track: max notification period is resampled equivalent of one HAL buffer time | 
 | 6088 |         //                 or 20 ms if there is a fast capture | 
 | 6089 |         // TODO This could be a roundupRatio inline, and const | 
 | 6090 |         size_t maxNotificationFrames = ((int64_t) (hasFastCapture() ? mSampleRate/50 : mFrameCount) | 
 | 6091 |                 * sampleRate + mSampleRate - 1) / mSampleRate; | 
 | 6092 |         // minimum number of notification periods is at least kMinNotifications, | 
 | 6093 |         // and at least kMinMs rounded up to a whole notification period (minNotificationsByMs) | 
 | 6094 |         static const size_t kMinNotifications = 3; | 
 | 6095 |         static const uint32_t kMinMs = 30; | 
 | 6096 |         // TODO This could be a roundupRatio inline | 
 | 6097 |         const size_t minFramesByMs = (sampleRate * kMinMs + 1000 - 1) / 1000; | 
 | 6098 |         // TODO This could be a roundupRatio inline | 
 | 6099 |         const size_t minNotificationsByMs = (minFramesByMs + maxNotificationFrames - 1) / | 
 | 6100 |                 maxNotificationFrames; | 
 | 6101 |         const size_t minFrameCount = maxNotificationFrames * | 
 | 6102 |                 max(kMinNotifications, minNotificationsByMs); | 
 | 6103 |         frameCount = max(frameCount, minFrameCount); | 
 | 6104 |         if (*notificationFrames == 0 || *notificationFrames > maxNotificationFrames) { | 
 | 6105 |             *notificationFrames = maxNotificationFrames; | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6106 |         } | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6107 |     } | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6108 |     *pFrameCount = frameCount; | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6109 |  | 
| Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 6110 |     lStatus = initCheck(); | 
 | 6111 |     if (lStatus != NO_ERROR) { | 
 | 6112 |         ALOGE("createRecordTrack_l() audio driver not initialized"); | 
 | 6113 |         goto Exit; | 
 | 6114 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6115 |  | 
 | 6116 |     { // scope for mLock | 
 | 6117 |         Mutex::Autolock _l(mLock); | 
 | 6118 |  | 
 | 6119 |         track = new RecordTrack(this, client, sampleRate, | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6120 |                       format, channelMask, frameCount, NULL, sessionId, uid, | 
 | 6121 |                       *flags, TrackBase::TYPE_DEFAULT); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6122 |  | 
| Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 6123 |         lStatus = track->initCheck(); | 
 | 6124 |         if (lStatus != NO_ERROR) { | 
| Glenn Kasten | 3529507 | 2013-10-07 09:27:06 -0700 | [diff] [blame] | 6125 |             ALOGE("createRecordTrack_l() initCheck failed %d; no control block?", lStatus); | 
| Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 6126 |             // 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] | 6127 |             goto Exit; | 
 | 6128 |         } | 
 | 6129 |         mTracks.add(track); | 
 | 6130 |  | 
 | 6131 |         // disable AEC and NS if the device is a BT SCO headset supporting those pre processings | 
 | 6132 |         bool suspend = audio_is_bluetooth_sco_device(mInDevice) && | 
 | 6133 |                         mAudioFlinger->btNrecIsOff(); | 
 | 6134 |         setEffectSuspended_l(FX_IID_AEC, suspend, sessionId); | 
 | 6135 |         setEffectSuspended_l(FX_IID_NS, suspend, sessionId); | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6136 |  | 
 | 6137 |         if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) { | 
 | 6138 |             pid_t callingPid = IPCThreadState::self()->getCallingPid(); | 
 | 6139 |             // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, | 
 | 6140 |             // so ask activity manager to do this on our behalf | 
 | 6141 |             sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp); | 
 | 6142 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6143 |     } | 
| Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 6144 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6145 |     lStatus = NO_ERROR; | 
 | 6146 |  | 
 | 6147 | Exit: | 
| Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 6148 |     *status = lStatus; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6149 |     return track; | 
 | 6150 | } | 
 | 6151 |  | 
 | 6152 | status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack, | 
 | 6153 |                                            AudioSystem::sync_event_t event, | 
 | 6154 |                                            int triggerSession) | 
 | 6155 | { | 
 | 6156 |     ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession); | 
 | 6157 |     sp<ThreadBase> strongMe = this; | 
 | 6158 |     status_t status = NO_ERROR; | 
 | 6159 |  | 
 | 6160 |     if (event == AudioSystem::SYNC_EVENT_NONE) { | 
| Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6161 |         recordTrack->clearSyncStartEvent(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6162 |     } else if (event != AudioSystem::SYNC_EVENT_SAME) { | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6163 |         recordTrack->mSyncStartEvent = mAudioFlinger->createSyncEvent(event, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6164 |                                        triggerSession, | 
 | 6165 |                                        recordTrack->sessionId(), | 
 | 6166 |                                        syncStartEventCallback, | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6167 |                                        recordTrack); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6168 |         // Sync event can be cancelled by the trigger session if the track is not in a | 
 | 6169 |         // compatible state in which case we start record immediately | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6170 |         if (recordTrack->mSyncStartEvent->isCancelled()) { | 
| Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6171 |             recordTrack->clearSyncStartEvent(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6172 |         } else { | 
 | 6173 |             // do not wait for the event for more than AudioSystem::kSyncRecordStartTimeOutMs | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6174 |             recordTrack->mFramesToDrop = - | 
| Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6175 |                     ((AudioSystem::kSyncRecordStartTimeOutMs * recordTrack->mSampleRate) / 1000); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6176 |         } | 
 | 6177 |     } | 
 | 6178 |  | 
 | 6179 |     { | 
| Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6180 |         // This section is a rendezvous between binder thread executing start() and RecordThread | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6181 |         AutoMutex lock(mLock); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6182 |         if (mActiveTracks.indexOf(recordTrack) >= 0) { | 
 | 6183 |             if (recordTrack->mState == TrackBase::PAUSING) { | 
 | 6184 |                 ALOGV("active record track PAUSING -> ACTIVE"); | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6185 |                 recordTrack->mState = TrackBase::ACTIVE; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6186 |             } else { | 
 | 6187 |                 ALOGV("active record track state %d", recordTrack->mState); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6188 |             } | 
 | 6189 |             return status; | 
 | 6190 |         } | 
 | 6191 |  | 
| Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6192 |         // TODO consider other ways of handling this, such as changing the state to :STARTING and | 
 | 6193 |         //      adding the track to mActiveTracks after returning from AudioSystem::startInput(), | 
 | 6194 |         //      or using a separate command thread | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6195 |         recordTrack->mState = TrackBase::STARTING_1; | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6196 |         mActiveTracks.add(recordTrack); | 
 | 6197 |         mActiveTracksGen++; | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6198 |         status_t status = NO_ERROR; | 
 | 6199 |         if (recordTrack->isExternalTrack()) { | 
 | 6200 |             mLock.unlock(); | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 6201 |             status = AudioSystem::startInput(mId, (audio_session_t)recordTrack->sessionId()); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6202 |             mLock.lock(); | 
 | 6203 |             // FIXME should verify that recordTrack is still in mActiveTracks | 
 | 6204 |             if (status != NO_ERROR) { | 
 | 6205 |                 mActiveTracks.remove(recordTrack); | 
 | 6206 |                 mActiveTracksGen++; | 
 | 6207 |                 recordTrack->clearSyncStartEvent(); | 
 | 6208 |                 ALOGV("RecordThread::start error %d", status); | 
 | 6209 |                 return status; | 
 | 6210 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6211 |         } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6212 |         // Catch up with current buffer indices if thread is already running. | 
 | 6213 |         // This is what makes a new client discard all buffered data.  If the track's mRsmpInFront | 
 | 6214 |         // was initialized to some value closer to the thread's mRsmpInFront, then the track could | 
 | 6215 |         // see previously buffered data before it called start(), but with greater risk of overrun. | 
 | 6216 |  | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6217 |         recordTrack->mResamplerBufferProvider->reset(); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6218 |         // clear any converter state as new data will be discontinuous | 
 | 6219 |         recordTrack->mRecordBufferConverter->reset(); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6220 |         recordTrack->mState = TrackBase::STARTING_2; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6221 |         // signal thread to start | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6222 |         mWaitWorkCV.broadcast(); | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6223 |         if (mActiveTracks.indexOf(recordTrack) < 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6224 |             ALOGV("Record failed to start"); | 
 | 6225 |             status = BAD_VALUE; | 
 | 6226 |             goto startError; | 
 | 6227 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6228 |         return status; | 
 | 6229 |     } | 
| Glenn Kasten | 7c02724 | 2012-12-26 14:43:16 -0800 | [diff] [blame] | 6230 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6231 | startError: | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6232 |     if (recordTrack->isExternalTrack()) { | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 6233 |         AudioSystem::stopInput(mId, (audio_session_t)recordTrack->sessionId()); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6234 |     } | 
| Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6235 |     recordTrack->clearSyncStartEvent(); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6236 |     // FIXME I wonder why we do not reset the state here? | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6237 |     return status; | 
 | 6238 | } | 
 | 6239 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6240 | void AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event) | 
 | 6241 | { | 
 | 6242 |     sp<SyncEvent> strongEvent = event.promote(); | 
 | 6243 |  | 
 | 6244 |     if (strongEvent != 0) { | 
| Eric Laurent | 8ea16e4 | 2014-02-20 16:26:11 -0800 | [diff] [blame] | 6245 |         sp<RefBase> ptr = strongEvent->cookie().promote(); | 
 | 6246 |         if (ptr != 0) { | 
 | 6247 |             RecordTrack *recordTrack = (RecordTrack *)ptr.get(); | 
 | 6248 |             recordTrack->handleSyncStartEvent(strongEvent); | 
 | 6249 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6250 |     } | 
 | 6251 | } | 
 | 6252 |  | 
| Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 6253 | bool AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6254 |     ALOGV("RecordThread::stop"); | 
| Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 6255 |     AutoMutex _l(mLock); | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6256 |     if (mActiveTracks.indexOf(recordTrack) != 0 || recordTrack->mState == TrackBase::PAUSING) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6257 |         return false; | 
 | 6258 |     } | 
| Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6259 |     // 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] | 6260 |     recordTrack->mState = TrackBase::PAUSING; | 
 | 6261 |     // do not wait for mStartStopCond if exiting | 
 | 6262 |     if (exitPending()) { | 
 | 6263 |         return true; | 
 | 6264 |     } | 
| Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6265 |     // FIXME incorrect usage of wait: no explicit predicate or loop | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6266 |     mStartStopCond.wait(mLock); | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6267 |     // if we have been restarted, recordTrack is in mActiveTracks here | 
 | 6268 |     if (exitPending() || mActiveTracks.indexOf(recordTrack) != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6269 |         ALOGV("Record stopped OK"); | 
 | 6270 |         return true; | 
 | 6271 |     } | 
 | 6272 |     return false; | 
 | 6273 | } | 
 | 6274 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6275 | bool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6276 | { | 
 | 6277 |     return false; | 
 | 6278 | } | 
 | 6279 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6280 | status_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6281 | { | 
 | 6282 | #if 0   // This branch is currently dead code, but is preserved in case it will be needed in future | 
 | 6283 |     if (!isValidSyncEvent(event)) { | 
 | 6284 |         return BAD_VALUE; | 
 | 6285 |     } | 
 | 6286 |  | 
 | 6287 |     int eventSession = event->triggerSession(); | 
 | 6288 |     status_t ret = NAME_NOT_FOUND; | 
 | 6289 |  | 
 | 6290 |     Mutex::Autolock _l(mLock); | 
 | 6291 |  | 
 | 6292 |     for (size_t i = 0; i < mTracks.size(); i++) { | 
 | 6293 |         sp<RecordTrack> track = mTracks[i]; | 
 | 6294 |         if (eventSession == track->sessionId()) { | 
 | 6295 |             (void) track->setSyncEvent(event); | 
 | 6296 |             ret = NO_ERROR; | 
 | 6297 |         } | 
 | 6298 |     } | 
 | 6299 |     return ret; | 
 | 6300 | #else | 
 | 6301 |     return BAD_VALUE; | 
 | 6302 | #endif | 
 | 6303 | } | 
 | 6304 |  | 
 | 6305 | // destroyTrack_l() must be called with ThreadBase::mLock held | 
 | 6306 | void AudioFlinger::RecordThread::destroyTrack_l(const sp<RecordTrack>& track) | 
 | 6307 | { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6308 |     track->terminate(); | 
 | 6309 |     track->mState = TrackBase::STOPPED; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6310 |     // active tracks are removed by threadLoop() | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6311 |     if (mActiveTracks.indexOf(track) < 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6312 |         removeTrack_l(track); | 
 | 6313 |     } | 
 | 6314 | } | 
 | 6315 |  | 
 | 6316 | void AudioFlinger::RecordThread::removeTrack_l(const sp<RecordTrack>& track) | 
 | 6317 | { | 
 | 6318 |     mTracks.remove(track); | 
 | 6319 |     // need anything related to effects here? | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6320 |     if (track->isFastTrack()) { | 
 | 6321 |         ALOG_ASSERT(!mFastTrackAvail); | 
 | 6322 |         mFastTrackAvail = true; | 
 | 6323 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6324 | } | 
 | 6325 |  | 
 | 6326 | void AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) | 
 | 6327 | { | 
 | 6328 |     dumpInternals(fd, args); | 
 | 6329 |     dumpTracks(fd, args); | 
 | 6330 |     dumpEffectChains(fd, args); | 
 | 6331 | } | 
 | 6332 |  | 
 | 6333 | void AudioFlinger::RecordThread::dumpInternals(int fd, const Vector<String16>& args) | 
 | 6334 | { | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6335 |     dprintf(fd, "\nInput thread %p:\n", this); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6336 |  | 
| Glenn Kasten | 44182c2 | 2015-03-05 17:12:23 -0800 | [diff] [blame] | 6337 |     dumpBase(fd, args); | 
 | 6338 |  | 
 | 6339 |     if (mActiveTracks.size() == 0) { | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6340 |         dprintf(fd, "  No active record clients\n"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6341 |     } | 
| Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 6342 |     dprintf(fd, "  Fast capture thread: %s\n", hasFastCapture() ? "yes" : "no"); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6343 |     dprintf(fd, "  Fast track available: %s\n", mFastTrackAvail ? "yes" : "no"); | 
| Glenn Kasten | 17c9c99 | 2015-03-02 15:53:01 -0800 | [diff] [blame] | 6344 |  | 
 | 6345 |     //  Make a non-atomic copy of fast capture dump state so it won't change underneath us | 
 | 6346 |     const FastCaptureDumpState copy(mFastCaptureDumpState); | 
 | 6347 |     copy.dump(fd); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6348 | } | 
 | 6349 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6350 | void AudioFlinger::RecordThread::dumpTracks(int fd, const Vector<String16>& args __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6351 | { | 
 | 6352 |     const size_t SIZE = 256; | 
 | 6353 |     char buffer[SIZE]; | 
 | 6354 |     String8 result; | 
 | 6355 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6356 |     size_t numtracks = mTracks.size(); | 
 | 6357 |     size_t numactive = mActiveTracks.size(); | 
 | 6358 |     size_t numactiveseen = 0; | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6359 |     dprintf(fd, "  %d Tracks", numtracks); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6360 |     if (numtracks) { | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6361 |         dprintf(fd, " of which %d are active\n", numactive); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6362 |         RecordTrack::appendDumpHeader(result); | 
 | 6363 |         for (size_t i = 0; i < numtracks ; ++i) { | 
 | 6364 |             sp<RecordTrack> track = mTracks[i]; | 
 | 6365 |             if (track != 0) { | 
 | 6366 |                 bool active = mActiveTracks.indexOf(track) >= 0; | 
 | 6367 |                 if (active) { | 
 | 6368 |                     numactiveseen++; | 
 | 6369 |                 } | 
 | 6370 |                 track->dump(buffer, SIZE, active); | 
 | 6371 |                 result.append(buffer); | 
 | 6372 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6373 |         } | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6374 |     } else { | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6375 |         dprintf(fd, "\n"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6376 |     } | 
 | 6377 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6378 |     if (numactiveseen != numactive) { | 
 | 6379 |         snprintf(buffer, SIZE, "  The following tracks are in the active list but" | 
 | 6380 |                 " not in the track list\n"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6381 |         result.append(buffer); | 
 | 6382 |         RecordTrack::appendDumpHeader(result); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6383 |         for (size_t i = 0; i < numactive; ++i) { | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6384 |             sp<RecordTrack> track = mActiveTracks[i]; | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6385 |             if (mTracks.indexOf(track) < 0) { | 
 | 6386 |                 track->dump(buffer, SIZE, true); | 
 | 6387 |                 result.append(buffer); | 
 | 6388 |             } | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6389 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6390 |  | 
 | 6391 |     } | 
 | 6392 |     write(fd, result.string(), result.size()); | 
 | 6393 | } | 
 | 6394 |  | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6395 |  | 
 | 6396 | void AudioFlinger::RecordThread::ResamplerBufferProvider::reset() | 
 | 6397 | { | 
 | 6398 |     sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); | 
 | 6399 |     RecordThread *recordThread = (RecordThread *) threadBase.get(); | 
 | 6400 |     mRsmpInFront = recordThread->mRsmpInRear; | 
 | 6401 |     mRsmpInUnrel = 0; | 
 | 6402 | } | 
 | 6403 |  | 
 | 6404 | void AudioFlinger::RecordThread::ResamplerBufferProvider::sync( | 
 | 6405 |         size_t *framesAvailable, bool *hasOverrun) | 
 | 6406 | { | 
 | 6407 |     sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); | 
 | 6408 |     RecordThread *recordThread = (RecordThread *) threadBase.get(); | 
 | 6409 |     const int32_t rear = recordThread->mRsmpInRear; | 
 | 6410 |     const int32_t front = mRsmpInFront; | 
 | 6411 |     const ssize_t filled = rear - front; | 
 | 6412 |  | 
 | 6413 |     size_t framesIn; | 
 | 6414 |     bool overrun = false; | 
 | 6415 |     if (filled < 0) { | 
 | 6416 |         // should not happen, but treat like a massive overrun and re-sync | 
 | 6417 |         framesIn = 0; | 
 | 6418 |         mRsmpInFront = rear; | 
 | 6419 |         overrun = true; | 
 | 6420 |     } else if ((size_t) filled <= recordThread->mRsmpInFrames) { | 
 | 6421 |         framesIn = (size_t) filled; | 
 | 6422 |     } else { | 
 | 6423 |         // client is not keeping up with server, but give it latest data | 
 | 6424 |         framesIn = recordThread->mRsmpInFrames; | 
 | 6425 |         mRsmpInFront = /* front = */ rear - framesIn; | 
 | 6426 |         overrun = true; | 
 | 6427 |     } | 
 | 6428 |     if (framesAvailable != NULL) { | 
 | 6429 |         *framesAvailable = framesIn; | 
 | 6430 |     } | 
 | 6431 |     if (hasOverrun != NULL) { | 
 | 6432 |         *hasOverrun = overrun; | 
 | 6433 |     } | 
 | 6434 | } | 
 | 6435 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6436 | // AudioBufferProvider interface | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6437 | status_t AudioFlinger::RecordThread::ResamplerBufferProvider::getNextBuffer( | 
 | 6438 |         AudioBufferProvider::Buffer* buffer, int64_t pts __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6439 | { | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6440 |     sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6441 |     if (threadBase == 0) { | 
 | 6442 |         buffer->frameCount = 0; | 
| Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 6443 |         buffer->raw = NULL; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6444 |         return NOT_ENOUGH_DATA; | 
 | 6445 |     } | 
 | 6446 |     RecordThread *recordThread = (RecordThread *) threadBase.get(); | 
 | 6447 |     int32_t rear = recordThread->mRsmpInRear; | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6448 |     int32_t front = mRsmpInFront; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6449 |     ssize_t filled = rear - front; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6450 |     // FIXME should not be P2 (don't want to increase latency) | 
 | 6451 |     // FIXME if client not keeping up, discard | 
| Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 6452 |     LOG_ALWAYS_FATAL_IF(!(0 <= filled && (size_t) filled <= recordThread->mRsmpInFrames)); | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6453 |     // 'filled' may be non-contiguous, so return only the first contiguous chunk | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6454 |     front &= recordThread->mRsmpInFramesP2 - 1; | 
 | 6455 |     size_t part1 = recordThread->mRsmpInFramesP2 - front; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6456 |     if (part1 > (size_t) filled) { | 
 | 6457 |         part1 = filled; | 
 | 6458 |     } | 
 | 6459 |     size_t ask = buffer->frameCount; | 
 | 6460 |     ALOG_ASSERT(ask > 0); | 
 | 6461 |     if (part1 > ask) { | 
 | 6462 |         part1 = ask; | 
 | 6463 |     } | 
 | 6464 |     if (part1 == 0) { | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6465 |         // out of data is fine since the resampler will return a short-count. | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6466 |         buffer->raw = NULL; | 
 | 6467 |         buffer->frameCount = 0; | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6468 |         mRsmpInUnrel = 0; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6469 |         return NOT_ENOUGH_DATA; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6470 |     } | 
 | 6471 |  | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6472 |     buffer->raw = (uint8_t*)recordThread->mRsmpInBuffer + front * recordThread->mFrameSize; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6473 |     buffer->frameCount = part1; | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6474 |     mRsmpInUnrel = part1; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6475 |     return NO_ERROR; | 
 | 6476 | } | 
 | 6477 |  | 
 | 6478 | // AudioBufferProvider interface | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6479 | void AudioFlinger::RecordThread::ResamplerBufferProvider::releaseBuffer( | 
 | 6480 |         AudioBufferProvider::Buffer* buffer) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6481 | { | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6482 |     size_t stepCount = buffer->frameCount; | 
 | 6483 |     if (stepCount == 0) { | 
 | 6484 |         return; | 
 | 6485 |     } | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6486 |     ALOG_ASSERT(stepCount <= mRsmpInUnrel); | 
 | 6487 |     mRsmpInUnrel -= stepCount; | 
 | 6488 |     mRsmpInFront += stepCount; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6489 |     buffer->raw = NULL; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6490 |     buffer->frameCount = 0; | 
 | 6491 | } | 
 | 6492 |  | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6493 | AudioFlinger::RecordThread::RecordBufferConverter::RecordBufferConverter( | 
 | 6494 |         audio_channel_mask_t srcChannelMask, audio_format_t srcFormat, | 
 | 6495 |         uint32_t srcSampleRate, | 
 | 6496 |         audio_channel_mask_t dstChannelMask, audio_format_t dstFormat, | 
 | 6497 |         uint32_t dstSampleRate) : | 
 | 6498 |             mSrcChannelMask(AUDIO_CHANNEL_INVALID), // updateParameters will set following vars | 
 | 6499 |             // mSrcFormat | 
 | 6500 |             // mSrcSampleRate | 
 | 6501 |             // mDstChannelMask | 
 | 6502 |             // mDstFormat | 
 | 6503 |             // mDstSampleRate | 
 | 6504 |             // mSrcChannelCount | 
 | 6505 |             // mDstChannelCount | 
 | 6506 |             // mDstFrameSize | 
 | 6507 |             mBuf(NULL), mBufFrames(0), mBufFrameSize(0), | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6508 |             mResampler(NULL), | 
 | 6509 |             mIsLegacyDownmix(false), | 
 | 6510 |             mIsLegacyUpmix(false), | 
 | 6511 |             mRequiresFloat(false), | 
 | 6512 |             mInputConverterProvider(NULL) | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6513 | { | 
 | 6514 |     (void)updateParameters(srcChannelMask, srcFormat, srcSampleRate, | 
 | 6515 |             dstChannelMask, dstFormat, dstSampleRate); | 
 | 6516 | } | 
 | 6517 |  | 
 | 6518 | AudioFlinger::RecordThread::RecordBufferConverter::~RecordBufferConverter() { | 
 | 6519 |     free(mBuf); | 
 | 6520 |     delete mResampler; | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6521 |     delete mInputConverterProvider; | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6522 | } | 
 | 6523 |  | 
 | 6524 | size_t AudioFlinger::RecordThread::RecordBufferConverter::convert(void *dst, | 
 | 6525 |         AudioBufferProvider *provider, size_t frames) | 
 | 6526 | { | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6527 |     if (mInputConverterProvider != NULL) { | 
 | 6528 |         mInputConverterProvider->setBufferProvider(provider); | 
 | 6529 |         provider = mInputConverterProvider; | 
 | 6530 |     } | 
 | 6531 |  | 
 | 6532 |     if (mResampler == NULL) { | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6533 |         ALOGVV("NO RESAMPLING sampleRate:%u mSrcFormat:%#x mDstFormat:%#x", | 
 | 6534 |                 mSrcSampleRate, mSrcFormat, mDstFormat); | 
 | 6535 |  | 
 | 6536 |         AudioBufferProvider::Buffer buffer; | 
 | 6537 |         for (size_t i = frames; i > 0; ) { | 
 | 6538 |             buffer.frameCount = i; | 
 | 6539 |             status_t status = provider->getNextBuffer(&buffer, 0); | 
 | 6540 |             if (status != OK || buffer.frameCount == 0) { | 
 | 6541 |                 frames -= i; // cannot fill request. | 
 | 6542 |                 break; | 
 | 6543 |             } | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6544 |             // format convert to destination buffer | 
 | 6545 |             convertNoResampler(dst, buffer.raw, buffer.frameCount); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6546 |  | 
 | 6547 |             dst = (int8_t*)dst + buffer.frameCount * mDstFrameSize; | 
 | 6548 |             i -= buffer.frameCount; | 
 | 6549 |             provider->releaseBuffer(&buffer); | 
 | 6550 |         } | 
 | 6551 |     } else { | 
 | 6552 |          ALOGVV("RESAMPLING mSrcSampleRate:%u mDstSampleRate:%u mSrcFormat:%#x mDstFormat:%#x", | 
 | 6553 |                  mSrcSampleRate, mDstSampleRate, mSrcFormat, mDstFormat); | 
 | 6554 |  | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6555 |          // reallocate buffer if needed | 
 | 6556 |          if (mBufFrameSize != 0 && mBufFrames < frames) { | 
 | 6557 |              free(mBuf); | 
 | 6558 |              mBufFrames = frames; | 
 | 6559 |              (void)posix_memalign(&mBuf, 32, mBufFrames * mBufFrameSize); | 
 | 6560 |          } | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6561 |         // resampler accumulates, but we only have one source track | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6562 |         memset(mBuf, 0, frames * mBufFrameSize); | 
 | 6563 |         frames = mResampler->resample((int32_t*)mBuf, frames, provider); | 
 | 6564 |         // format convert to destination buffer | 
 | 6565 |         convertResampler(dst, mBuf, frames); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6566 |     } | 
 | 6567 |     return frames; | 
 | 6568 | } | 
 | 6569 |  | 
 | 6570 | status_t AudioFlinger::RecordThread::RecordBufferConverter::updateParameters( | 
 | 6571 |         audio_channel_mask_t srcChannelMask, audio_format_t srcFormat, | 
 | 6572 |         uint32_t srcSampleRate, | 
 | 6573 |         audio_channel_mask_t dstChannelMask, audio_format_t dstFormat, | 
 | 6574 |         uint32_t dstSampleRate) | 
 | 6575 | { | 
 | 6576 |     // quick evaluation if there is any change. | 
 | 6577 |     if (mSrcFormat == srcFormat | 
 | 6578 |             && mSrcChannelMask == srcChannelMask | 
 | 6579 |             && mSrcSampleRate == srcSampleRate | 
 | 6580 |             && mDstFormat == dstFormat | 
 | 6581 |             && mDstChannelMask == dstChannelMask | 
 | 6582 |             && mDstSampleRate == dstSampleRate) { | 
 | 6583 |         return NO_ERROR; | 
 | 6584 |     } | 
 | 6585 |  | 
| Andy Hung | db4c031 | 2015-05-06 08:46:52 -0700 | [diff] [blame] | 6586 |     ALOGV("RecordBufferConverter updateParameters srcMask:%#x dstMask:%#x" | 
 | 6587 |             "  srcFormat:%#x dstFormat:%#x  srcRate:%u dstRate:%u", | 
 | 6588 |             srcChannelMask, dstChannelMask, srcFormat, dstFormat, srcSampleRate, dstSampleRate); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6589 |     const bool valid = | 
 | 6590 |             audio_is_input_channel(srcChannelMask) | 
 | 6591 |             && audio_is_input_channel(dstChannelMask) | 
 | 6592 |             && audio_is_valid_format(srcFormat) && audio_is_linear_pcm(srcFormat) | 
 | 6593 |             && audio_is_valid_format(dstFormat) && audio_is_linear_pcm(dstFormat) | 
 | 6594 |             && (srcSampleRate <= dstSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) | 
 | 6595 |             ; // no upsampling checks for now | 
 | 6596 |     if (!valid) { | 
 | 6597 |         return BAD_VALUE; | 
 | 6598 |     } | 
 | 6599 |  | 
 | 6600 |     mSrcFormat = srcFormat; | 
 | 6601 |     mSrcChannelMask = srcChannelMask; | 
 | 6602 |     mSrcSampleRate = srcSampleRate; | 
 | 6603 |     mDstFormat = dstFormat; | 
 | 6604 |     mDstChannelMask = dstChannelMask; | 
 | 6605 |     mDstSampleRate = dstSampleRate; | 
 | 6606 |  | 
 | 6607 |     // compute derived parameters | 
 | 6608 |     mSrcChannelCount = audio_channel_count_from_in_mask(srcChannelMask); | 
 | 6609 |     mDstChannelCount = audio_channel_count_from_in_mask(dstChannelMask); | 
 | 6610 |     mDstFrameSize = mDstChannelCount * audio_bytes_per_sample(mDstFormat); | 
 | 6611 |  | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6612 |     // do we need to resample? | 
 | 6613 |     delete mResampler; | 
 | 6614 |     mResampler = NULL; | 
 | 6615 |     if (mSrcSampleRate != mDstSampleRate) { | 
 | 6616 |         mResampler = AudioResampler::create(AUDIO_FORMAT_PCM_FLOAT, | 
 | 6617 |                 mSrcChannelCount, mDstSampleRate); | 
 | 6618 |         mResampler->setSampleRate(mSrcSampleRate); | 
 | 6619 |         mResampler->setVolume(AudioMixer::UNITY_GAIN_FLOAT, AudioMixer::UNITY_GAIN_FLOAT); | 
 | 6620 |     } | 
 | 6621 |  | 
 | 6622 |     // are we running legacy channel conversion modes? | 
 | 6623 |     mIsLegacyDownmix = (mSrcChannelMask == AUDIO_CHANNEL_IN_STEREO | 
 | 6624 |                             || mSrcChannelMask == AUDIO_CHANNEL_IN_FRONT_BACK) | 
 | 6625 |                    && mDstChannelMask == AUDIO_CHANNEL_IN_MONO; | 
 | 6626 |     mIsLegacyUpmix = mSrcChannelMask == AUDIO_CHANNEL_IN_MONO | 
 | 6627 |                    && (mDstChannelMask == AUDIO_CHANNEL_IN_STEREO | 
 | 6628 |                             || mDstChannelMask == AUDIO_CHANNEL_IN_FRONT_BACK); | 
 | 6629 |  | 
 | 6630 |     // do we need to process in float? | 
 | 6631 |     mRequiresFloat = mResampler != NULL || mIsLegacyDownmix || mIsLegacyUpmix; | 
 | 6632 |  | 
 | 6633 |     // do we need a staging buffer to convert for destination (we can still optimize this)? | 
 | 6634 |     // we use mBufFrameSize > 0 to indicate both frame size as well as buffer necessity | 
 | 6635 |     if (mResampler != NULL) { | 
 | 6636 |         mBufFrameSize = max(mSrcChannelCount, FCC_2) | 
 | 6637 |                 * audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT); | 
 | 6638 |     } else if ((mIsLegacyUpmix || mIsLegacyDownmix) && mDstFormat != AUDIO_FORMAT_PCM_FLOAT) { | 
 | 6639 |         mBufFrameSize = mDstChannelCount * audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT); | 
 | 6640 |     } else if (mSrcChannelMask != mDstChannelMask && mDstFormat != mSrcFormat) { | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6641 |         mBufFrameSize = mDstChannelCount * audio_bytes_per_sample(mSrcFormat); | 
 | 6642 |     } else { | 
 | 6643 |         mBufFrameSize = 0; | 
 | 6644 |     } | 
 | 6645 |     mBufFrames = 0; // force the buffer to be resized. | 
 | 6646 |  | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6647 |     // do we need an input converter buffer provider to give us float? | 
 | 6648 |     delete mInputConverterProvider; | 
 | 6649 |     mInputConverterProvider = NULL; | 
 | 6650 |     if (mRequiresFloat && mSrcFormat != AUDIO_FORMAT_PCM_FLOAT) { | 
 | 6651 |         mInputConverterProvider = new ReformatBufferProvider( | 
 | 6652 |                 audio_channel_count_from_in_mask(mSrcChannelMask), | 
 | 6653 |                 mSrcFormat, | 
 | 6654 |                 AUDIO_FORMAT_PCM_FLOAT, | 
 | 6655 |                 256 /* provider buffer frame count */); | 
 | 6656 |     } | 
 | 6657 |  | 
 | 6658 |     // do we need a remixer to do channel mask conversion | 
 | 6659 |     if (!mIsLegacyDownmix && !mIsLegacyUpmix && mSrcChannelMask != mDstChannelMask) { | 
 | 6660 |         (void) memcpy_by_index_array_initialization_from_channel_mask( | 
 | 6661 |                 mIdxAry, ARRAY_SIZE(mIdxAry), mDstChannelMask, mSrcChannelMask); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6662 |     } | 
 | 6663 |     return NO_ERROR; | 
 | 6664 | } | 
 | 6665 |  | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6666 | void AudioFlinger::RecordThread::RecordBufferConverter::convertNoResampler( | 
 | 6667 |         void *dst, const void *src, size_t frames) | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6668 | { | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6669 |     // src is native type unless there is legacy upmix or downmix, whereupon it is float. | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6670 |     if (mBufFrameSize != 0 && mBufFrames < frames) { | 
 | 6671 |         free(mBuf); | 
 | 6672 |         mBufFrames = frames; | 
 | 6673 |         (void)posix_memalign(&mBuf, 32, mBufFrames * mBufFrameSize); | 
 | 6674 |     } | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6675 |     // do we need to do legacy upmix and downmix? | 
 | 6676 |     if (mIsLegacyUpmix || mIsLegacyDownmix) { | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6677 |         void *dstBuf = mBuf != NULL ? mBuf : dst; | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6678 |         if (mIsLegacyUpmix) { | 
 | 6679 |             upmix_to_stereo_float_from_mono_float((float *)dstBuf, | 
 | 6680 |                     (const float *)src, frames); | 
 | 6681 |         } else /*mIsLegacyDownmix */ { | 
 | 6682 |             downmix_to_mono_float_from_stereo_float((float *)dstBuf, | 
 | 6683 |                     (const float *)src, frames); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6684 |         } | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6685 |         if (mBuf != NULL) { | 
 | 6686 |             memcpy_by_audio_format(dst, mDstFormat, mBuf, AUDIO_FORMAT_PCM_FLOAT, | 
 | 6687 |                     frames * mDstChannelCount); | 
 | 6688 |         } | 
 | 6689 |         return; | 
 | 6690 |     } | 
 | 6691 |     // do we need to do channel mask conversion? | 
 | 6692 |     if (mSrcChannelMask != mDstChannelMask) { | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6693 |         void *dstBuf = mBuf != NULL ? mBuf : dst; | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6694 |         memcpy_by_index_array(dstBuf, mDstChannelCount, | 
 | 6695 |                 src, mSrcChannelCount, mIdxAry, audio_bytes_per_sample(mSrcFormat), frames); | 
 | 6696 |         if (dstBuf == dst) { | 
 | 6697 |             return; // format is the same | 
 | 6698 |         } | 
 | 6699 |     } | 
 | 6700 |     // convert to destination buffer | 
 | 6701 |     const void *convertBuf = mBuf != NULL ? mBuf : src; | 
 | 6702 |     memcpy_by_audio_format(dst, mDstFormat, convertBuf, mSrcFormat, | 
 | 6703 |             frames * mDstChannelCount); | 
 | 6704 | } | 
 | 6705 |  | 
 | 6706 | void AudioFlinger::RecordThread::RecordBufferConverter::convertResampler( | 
 | 6707 |         void *dst, /*not-a-const*/ void *src, size_t frames) | 
 | 6708 | { | 
 | 6709 |     // src buffer format is ALWAYS float when entering this routine | 
 | 6710 |     if (mIsLegacyUpmix) { | 
 | 6711 |         ; // mono to stereo already handled by resampler | 
 | 6712 |     } else if (mIsLegacyDownmix | 
 | 6713 |             || (mSrcChannelMask == mDstChannelMask && mSrcChannelCount == 1)) { | 
 | 6714 |         // the resampler outputs stereo for mono input channel (a feature?) | 
 | 6715 |         // must convert to mono | 
 | 6716 |         downmix_to_mono_float_from_stereo_float((float *)src, | 
 | 6717 |                 (const float *)src, frames); | 
 | 6718 |     } else if (mSrcChannelMask != mDstChannelMask) { | 
 | 6719 |         // convert to mono channel again for channel mask conversion (could be skipped | 
 | 6720 |         // with further optimization). | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6721 |         if (mSrcChannelCount == 1) { | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6722 |             downmix_to_mono_float_from_stereo_float((float *)src, | 
 | 6723 |                 (const float *)src, frames); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6724 |         } | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6725 |         // convert to destination format (in place, OK as float is larger than other types) | 
 | 6726 |         if (mDstFormat != AUDIO_FORMAT_PCM_FLOAT) { | 
 | 6727 |             memcpy_by_audio_format(src, mDstFormat, src, AUDIO_FORMAT_PCM_FLOAT, | 
 | 6728 |                     frames * mSrcChannelCount); | 
 | 6729 |         } | 
 | 6730 |         // channel convert and save to dst | 
 | 6731 |         memcpy_by_index_array(dst, mDstChannelCount, | 
 | 6732 |                 src, mSrcChannelCount, mIdxAry, audio_bytes_per_sample(mDstFormat), frames); | 
 | 6733 |         return; | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6734 |     } | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6735 |     // convert to destination format and save to dst | 
 | 6736 |     memcpy_by_audio_format(dst, mDstFormat, src, AUDIO_FORMAT_PCM_FLOAT, | 
 | 6737 |             frames * mDstChannelCount); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6738 | } | 
 | 6739 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6740 | bool AudioFlinger::RecordThread::checkForNewParameter_l(const String8& keyValuePair, | 
 | 6741 |                                                         status_t& status) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6742 | { | 
 | 6743 |     bool reconfig = false; | 
 | 6744 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6745 |     status = NO_ERROR; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6746 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6747 |     audio_format_t reqFormat = mFormat; | 
 | 6748 |     uint32_t samplingRate = mSampleRate; | 
| Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 6749 |     // TODO this may change if we want to support capture from HDMI PCM multi channel (e.g on TVs). | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6750 |     audio_channel_mask_t channelMask = audio_channel_in_mask_from_count(mChannelCount); | 
 | 6751 |  | 
 | 6752 |     AudioParameter param = AudioParameter(keyValuePair); | 
 | 6753 |     int value; | 
 | 6754 |     // TODO Investigate when this code runs. Check with audio policy when a sample rate and | 
 | 6755 |     //      channel count change can be requested. Do we mandate the first client defines the | 
 | 6756 |     //      HAL sampling rate and channel count or do we allow changes on the fly? | 
 | 6757 |     if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { | 
 | 6758 |         samplingRate = value; | 
 | 6759 |         reconfig = true; | 
 | 6760 |     } | 
 | 6761 |     if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6762 |         if (!audio_is_linear_pcm((audio_format_t) value)) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6763 |             status = BAD_VALUE; | 
 | 6764 |         } else { | 
 | 6765 |             reqFormat = (audio_format_t) value; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6766 |             reconfig = true; | 
 | 6767 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6768 |     } | 
 | 6769 |     if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { | 
 | 6770 |         audio_channel_mask_t mask = (audio_channel_mask_t) value; | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6771 |         if (!audio_is_input_channel(mask) || | 
 | 6772 |                 audio_channel_count_from_in_mask(mask) > FCC_8) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6773 |             status = BAD_VALUE; | 
 | 6774 |         } else { | 
 | 6775 |             channelMask = mask; | 
 | 6776 |             reconfig = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6777 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6778 |     } | 
 | 6779 |     if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { | 
 | 6780 |         // do not accept frame count changes if tracks are open as the track buffer | 
 | 6781 |         // size depends on frame count and correct behavior would not be guaranteed | 
 | 6782 |         // if frame count is changed after track creation | 
 | 6783 |         if (mActiveTracks.size() > 0) { | 
 | 6784 |             status = INVALID_OPERATION; | 
 | 6785 |         } else { | 
 | 6786 |             reconfig = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6787 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6788 |     } | 
 | 6789 |     if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { | 
 | 6790 |         // forward device change to effects that have requested to be | 
 | 6791 |         // aware of attached audio device. | 
 | 6792 |         for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 6793 |             mEffectChains[i]->setDevice_l(value); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6794 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6795 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6796 |         // store input device and output device but do not forward output device to audio HAL. | 
 | 6797 |         // Note that status is ignored by the caller for output device | 
 | 6798 |         // (see AudioFlinger::setParameters() | 
 | 6799 |         if (audio_is_output_devices(value)) { | 
 | 6800 |             mOutDevice = value; | 
 | 6801 |             status = BAD_VALUE; | 
 | 6802 |         } else { | 
 | 6803 |             mInDevice = value; | 
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 6804 |             if (value != AUDIO_DEVICE_NONE) { | 
 | 6805 |                 mPrevInDevice = value; | 
 | 6806 |             } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6807 |             // disable AEC and NS if the device is a BT SCO headset supporting those | 
 | 6808 |             // pre processings | 
 | 6809 |             if (mTracks.size() > 0) { | 
 | 6810 |                 bool suspend = audio_is_bluetooth_sco_device(mInDevice) && | 
 | 6811 |                                     mAudioFlinger->btNrecIsOff(); | 
 | 6812 |                 for (size_t i = 0; i < mTracks.size(); i++) { | 
 | 6813 |                     sp<RecordTrack> track = mTracks[i]; | 
 | 6814 |                     setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); | 
 | 6815 |                     setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6816 |                 } | 
 | 6817 |             } | 
 | 6818 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6819 |     } | 
 | 6820 |     if (param.getInt(String8(AudioParameter::keyInputSource), value) == NO_ERROR && | 
 | 6821 |             mAudioSource != (audio_source_t)value) { | 
 | 6822 |         // forward device change to effects that have requested to be | 
 | 6823 |         // aware of attached audio device. | 
 | 6824 |         for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 6825 |             mEffectChains[i]->setAudioSource_l((audio_source_t)value); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6826 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6827 |         mAudioSource = (audio_source_t)value; | 
 | 6828 |     } | 
| Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 6829 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6830 |     if (status == NO_ERROR) { | 
 | 6831 |         status = mInput->stream->common.set_parameters(&mInput->stream->common, | 
 | 6832 |                 keyValuePair.string()); | 
 | 6833 |         if (status == INVALID_OPERATION) { | 
 | 6834 |             inputStandBy(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6835 |             status = mInput->stream->common.set_parameters(&mInput->stream->common, | 
 | 6836 |                     keyValuePair.string()); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6837 |         } | 
 | 6838 |         if (reconfig) { | 
 | 6839 |             if (status == BAD_VALUE && | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6840 |                 audio_is_linear_pcm(mInput->stream->common.get_format(&mInput->stream->common)) && | 
 | 6841 |                 audio_is_linear_pcm(reqFormat) && | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6842 |                 (mInput->stream->common.get_sample_rate(&mInput->stream->common) | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6843 |                         <= (AUDIO_RESAMPLER_DOWN_RATIO_MAX * samplingRate)) && | 
| Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 6844 |                 audio_channel_count_from_in_mask( | 
| Andy Hung | d1abb8f | 2015-05-05 23:42:34 -0700 | [diff] [blame] | 6845 |                         mInput->stream->common.get_channels(&mInput->stream->common)) <= FCC_8) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6846 |                 status = NO_ERROR; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6847 |             } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6848 |             if (status == NO_ERROR) { | 
 | 6849 |                 readInputParameters_l(); | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 6850 |                 sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6851 |             } | 
 | 6852 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6853 |     } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6854 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6855 |     return reconfig; | 
 | 6856 | } | 
 | 6857 |  | 
 | 6858 | String8 AudioFlinger::RecordThread::getParameters(const String8& keys) | 
 | 6859 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6860 |     Mutex::Autolock _l(mLock); | 
 | 6861 |     if (initCheck() != NO_ERROR) { | 
| Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 6862 |         return String8(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6863 |     } | 
 | 6864 |  | 
| Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 6865 |     char *s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string()); | 
 | 6866 |     const String8 out_s8(s); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6867 |     free(s); | 
 | 6868 |     return out_s8; | 
 | 6869 | } | 
 | 6870 |  | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 6871 | void AudioFlinger::RecordThread::ioConfigChanged(audio_io_config_event event) { | 
 | 6872 |     sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); | 
 | 6873 |  | 
 | 6874 |     desc->mIoHandle = mId; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6875 |  | 
 | 6876 |     switch (event) { | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 6877 |     case AUDIO_INPUT_OPENED: | 
 | 6878 |     case AUDIO_INPUT_CONFIG_CHANGED: | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 6879 |         desc->mPatch = mPatch; | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 6880 |         desc->mChannelMask = mChannelMask; | 
 | 6881 |         desc->mSamplingRate = mSampleRate; | 
 | 6882 |         desc->mFormat = mFormat; | 
 | 6883 |         desc->mFrameCount = mFrameCount; | 
 | 6884 |         desc->mLatency = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6885 |         break; | 
 | 6886 |  | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 6887 |     case AUDIO_INPUT_CLOSED: | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6888 |     default: | 
 | 6889 |         break; | 
 | 6890 |     } | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 6891 |     mAudioFlinger->ioConfigChanged(event, desc); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6892 | } | 
 | 6893 |  | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 6894 | void AudioFlinger::RecordThread::readInputParameters_l() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6895 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6896 |     mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common); | 
 | 6897 |     mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common); | 
| Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 6898 |     mChannelCount = audio_channel_count_from_in_mask(mChannelMask); | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6899 |     if (mChannelCount > FCC_8) { | 
 | 6900 |         ALOGE("HAL channel count %d > %d", mChannelCount, FCC_8); | 
 | 6901 |     } | 
| Andy Hung | 463be25 | 2014-07-10 16:56:07 -0700 | [diff] [blame] | 6902 |     mHALFormat = mInput->stream->common.get_format(&mInput->stream->common); | 
 | 6903 |     mFormat = mHALFormat; | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6904 |     if (!audio_is_linear_pcm(mFormat)) { | 
 | 6905 |         ALOGE("HAL format %#x is not linear pcm", mFormat); | 
| Glenn Kasten | 291bb6d | 2013-07-16 17:23:39 -0700 | [diff] [blame] | 6906 |     } | 
| Eric Laurent | 665470b | 2014-07-03 16:37:08 -0700 | [diff] [blame] | 6907 |     mFrameSize = audio_stream_in_frame_size(mInput->stream); | 
| Glenn Kasten | 548efc9 | 2012-11-29 08:48:51 -0800 | [diff] [blame] | 6908 |     mBufferSize = mInput->stream->common.get_buffer_size(&mInput->stream->common); | 
 | 6909 |     mFrameCount = mBufferSize / mFrameSize; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6910 |     // This is the formula for calculating the temporary buffer size. | 
| Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 6911 |     // 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] | 6912 |     // 1 full output buffer, regardless of the alignment of the available input. | 
| Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 6913 |     // The value is somewhat arbitrary, and could probably be even larger. | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6914 |     // A larger value should allow more old data to be read after a track calls start(), | 
 | 6915 |     // without increasing latency. | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6916 |     // | 
 | 6917 |     // Note this is independent of the maximum downsampling ratio permitted for capture. | 
| Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 6918 |     mRsmpInFrames = mFrameCount * 7; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6919 |     mRsmpInFramesP2 = roundup(mRsmpInFrames); | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6920 |     free(mRsmpInBuffer); | 
| Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 6921 |  | 
 | 6922 |     // TODO optimize audio capture buffer sizes ... | 
 | 6923 |     // Here we calculate the size of the sliding buffer used as a source | 
 | 6924 |     // for resampling.  mRsmpInFramesP2 is currently roundup(mFrameCount * 7). | 
 | 6925 |     // For current HAL frame counts, this is usually 2048 = 40 ms.  It would | 
 | 6926 |     // be better to have it derived from the pipe depth in the long term. | 
 | 6927 |     // The current value is higher than necessary.  However it should not add to latency. | 
 | 6928 |  | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6929 |     // Over-allocate beyond mRsmpInFramesP2 to permit a HAL read past end of buffer | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6930 |     (void)posix_memalign(&mRsmpInBuffer, 32, (mRsmpInFramesP2 + mFrameCount - 1) * mFrameSize); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6931 |  | 
| Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6932 |     // AudioRecord mSampleRate and mChannelCount are constant due to AudioRecord API constraints. | 
 | 6933 |     // 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] | 6934 | } | 
 | 6935 |  | 
| Glenn Kasten | 5f972c0 | 2014-01-13 09:59:31 -0800 | [diff] [blame] | 6936 | uint32_t AudioFlinger::RecordThread::getInputFramesLost() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6937 | { | 
 | 6938 |     Mutex::Autolock _l(mLock); | 
 | 6939 |     if (initCheck() != NO_ERROR) { | 
 | 6940 |         return 0; | 
 | 6941 |     } | 
 | 6942 |  | 
 | 6943 |     return mInput->stream->get_input_frames_lost(mInput->stream); | 
 | 6944 | } | 
 | 6945 |  | 
 | 6946 | uint32_t AudioFlinger::RecordThread::hasAudioSession(int sessionId) const | 
 | 6947 | { | 
 | 6948 |     Mutex::Autolock _l(mLock); | 
 | 6949 |     uint32_t result = 0; | 
 | 6950 |     if (getEffectChain_l(sessionId) != 0) { | 
 | 6951 |         result = EFFECT_SESSION; | 
 | 6952 |     } | 
 | 6953 |  | 
 | 6954 |     for (size_t i = 0; i < mTracks.size(); ++i) { | 
 | 6955 |         if (sessionId == mTracks[i]->sessionId()) { | 
 | 6956 |             result |= TRACK_SESSION; | 
 | 6957 |             break; | 
 | 6958 |         } | 
 | 6959 |     } | 
 | 6960 |  | 
 | 6961 |     return result; | 
 | 6962 | } | 
 | 6963 |  | 
 | 6964 | KeyedVector<int, bool> AudioFlinger::RecordThread::sessionIds() const | 
 | 6965 | { | 
 | 6966 |     KeyedVector<int, bool> ids; | 
 | 6967 |     Mutex::Autolock _l(mLock); | 
 | 6968 |     for (size_t j = 0; j < mTracks.size(); ++j) { | 
 | 6969 |         sp<RecordThread::RecordTrack> track = mTracks[j]; | 
 | 6970 |         int sessionId = track->sessionId(); | 
 | 6971 |         if (ids.indexOfKey(sessionId) < 0) { | 
 | 6972 |             ids.add(sessionId, true); | 
 | 6973 |         } | 
 | 6974 |     } | 
 | 6975 |     return ids; | 
 | 6976 | } | 
 | 6977 |  | 
 | 6978 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() | 
 | 6979 | { | 
 | 6980 |     Mutex::Autolock _l(mLock); | 
 | 6981 |     AudioStreamIn *input = mInput; | 
 | 6982 |     mInput = NULL; | 
 | 6983 |     return input; | 
 | 6984 | } | 
 | 6985 |  | 
 | 6986 | // this method must always be called either with ThreadBase mLock held or inside the thread loop | 
 | 6987 | audio_stream_t* AudioFlinger::RecordThread::stream() const | 
 | 6988 | { | 
 | 6989 |     if (mInput == NULL) { | 
 | 6990 |         return NULL; | 
 | 6991 |     } | 
 | 6992 |     return &mInput->stream->common; | 
 | 6993 | } | 
 | 6994 |  | 
 | 6995 | status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) | 
 | 6996 | { | 
 | 6997 |     // only one chain per input thread | 
 | 6998 |     if (mEffectChains.size() != 0) { | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 6999 |         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] | 7000 |         return INVALID_OPERATION; | 
 | 7001 |     } | 
 | 7002 |     ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 7003 |     chain->setThread(this); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7004 |     chain->setInBuffer(NULL); | 
 | 7005 |     chain->setOutBuffer(NULL); | 
 | 7006 |  | 
 | 7007 |     checkSuspendOnAddEffectChain_l(chain); | 
 | 7008 |  | 
| Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 7009 |     // make sure enabled pre processing effects state is communicated to the HAL as we | 
 | 7010 |     // just moved them to a new input stream. | 
 | 7011 |     chain->syncHalEffectsState(); | 
 | 7012 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7013 |     mEffectChains.add(chain); | 
 | 7014 |  | 
 | 7015 |     return NO_ERROR; | 
 | 7016 | } | 
 | 7017 |  | 
 | 7018 | size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) | 
 | 7019 | { | 
 | 7020 |     ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); | 
 | 7021 |     ALOGW_IF(mEffectChains.size() != 1, | 
 | 7022 |             "removeEffectChain_l() %p invalid chain size %d on thread %p", | 
 | 7023 |             chain.get(), mEffectChains.size(), this); | 
 | 7024 |     if (mEffectChains.size() == 1) { | 
 | 7025 |         mEffectChains.removeAt(0); | 
 | 7026 |     } | 
 | 7027 |     return 0; | 
 | 7028 | } | 
 | 7029 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7030 | status_t AudioFlinger::RecordThread::createAudioPatch_l(const struct audio_patch *patch, | 
 | 7031 |                                                           audio_patch_handle_t *handle) | 
 | 7032 | { | 
 | 7033 |     status_t status = NO_ERROR; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7034 |  | 
 | 7035 |     // store new device and send to effects | 
 | 7036 |     mInDevice = patch->sources[0].ext.device.type; | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7037 |     mPatch = *patch; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7038 |     for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 7039 |         mEffectChains[i]->setDevice_l(mInDevice); | 
 | 7040 |     } | 
 | 7041 |  | 
 | 7042 |     // disable AEC and NS if the device is a BT SCO headset supporting those | 
 | 7043 |     // pre processings | 
 | 7044 |     if (mTracks.size() > 0) { | 
 | 7045 |         bool suspend = audio_is_bluetooth_sco_device(mInDevice) && | 
 | 7046 |                             mAudioFlinger->btNrecIsOff(); | 
 | 7047 |         for (size_t i = 0; i < mTracks.size(); i++) { | 
 | 7048 |             sp<RecordTrack> track = mTracks[i]; | 
 | 7049 |             setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); | 
 | 7050 |             setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); | 
 | 7051 |         } | 
 | 7052 |     } | 
 | 7053 |  | 
 | 7054 |     // store new source and send to effects | 
 | 7055 |     if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) { | 
 | 7056 |         mAudioSource = patch->sinks[0].ext.mix.usecase.source; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7057 |         for (size_t i = 0; i < mEffectChains.size(); i++) { | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7058 |             mEffectChains[i]->setAudioSource_l(mAudioSource); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7059 |         } | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7060 |     } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7061 |  | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7062 |     if (mInput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7063 |         audio_hw_device_t *hwDevice = mInput->audioHwDev->hwDevice(); | 
 | 7064 |         status = hwDevice->create_audio_patch(hwDevice, | 
 | 7065 |                                                patch->num_sources, | 
 | 7066 |                                                patch->sources, | 
 | 7067 |                                                patch->num_sinks, | 
 | 7068 |                                                patch->sinks, | 
 | 7069 |                                                handle); | 
 | 7070 |     } else { | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7071 |         char *address; | 
 | 7072 |         if (strcmp(patch->sources[0].ext.device.address, "") != 0) { | 
 | 7073 |             address = audio_device_address_to_parameter( | 
 | 7074 |                                                 patch->sources[0].ext.device.type, | 
 | 7075 |                                                 patch->sources[0].ext.device.address); | 
 | 7076 |         } else { | 
 | 7077 |             address = (char *)calloc(1, 1); | 
 | 7078 |         } | 
 | 7079 |         AudioParameter param = AudioParameter(String8(address)); | 
 | 7080 |         free(address); | 
 | 7081 |         param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), | 
 | 7082 |                      (int)patch->sources[0].ext.device.type); | 
 | 7083 |         param.addInt(String8(AUDIO_PARAMETER_STREAM_INPUT_SOURCE), | 
 | 7084 |                                          (int)patch->sinks[0].ext.mix.usecase.source); | 
 | 7085 |         status = mInput->stream->common.set_parameters(&mInput->stream->common, | 
 | 7086 |                 param.toString().string()); | 
 | 7087 |         *handle = AUDIO_PATCH_HANDLE_NONE; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7088 |     } | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7089 |  | 
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 7090 |     if (mInDevice != mPrevInDevice) { | 
 | 7091 |         sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); | 
 | 7092 |         mPrevInDevice = mInDevice; | 
 | 7093 |     } | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7094 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7095 |     return status; | 
 | 7096 | } | 
 | 7097 |  | 
 | 7098 | status_t AudioFlinger::RecordThread::releaseAudioPatch_l(const audio_patch_handle_t handle) | 
 | 7099 | { | 
 | 7100 |     status_t status = NO_ERROR; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7101 |  | 
 | 7102 |     mInDevice = AUDIO_DEVICE_NONE; | 
 | 7103 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7104 |     if (mInput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { | 
 | 7105 |         audio_hw_device_t *hwDevice = mInput->audioHwDev->hwDevice(); | 
 | 7106 |         status = hwDevice->release_audio_patch(hwDevice, handle); | 
 | 7107 |     } else { | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7108 |         AudioParameter param; | 
 | 7109 |         param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), 0); | 
 | 7110 |         status = mInput->stream->common.set_parameters(&mInput->stream->common, | 
 | 7111 |                 param.toString().string()); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7112 |     } | 
 | 7113 |     return status; | 
 | 7114 | } | 
 | 7115 |  | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 7116 | void AudioFlinger::RecordThread::addPatchRecord(const sp<PatchRecord>& record) | 
 | 7117 | { | 
 | 7118 |     Mutex::Autolock _l(mLock); | 
 | 7119 |     mTracks.add(record); | 
 | 7120 | } | 
 | 7121 |  | 
 | 7122 | void AudioFlinger::RecordThread::deletePatchRecord(const sp<PatchRecord>& record) | 
 | 7123 | { | 
 | 7124 |     Mutex::Autolock _l(mLock); | 
 | 7125 |     destroyTrack_l(record); | 
 | 7126 | } | 
 | 7127 |  | 
 | 7128 | void AudioFlinger::RecordThread::getAudioPortConfig(struct audio_port_config *config) | 
 | 7129 | { | 
 | 7130 |     ThreadBase::getAudioPortConfig(config); | 
 | 7131 |     config->role = AUDIO_PORT_ROLE_SINK; | 
 | 7132 |     config->ext.mix.hw_module = mInput->audioHwDev->handle(); | 
 | 7133 |     config->ext.mix.usecase.source = mAudioSource; | 
 | 7134 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7135 |  | 
| Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 7136 | } // namespace android |