| 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" | 
| Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 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 |  | 
| Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 23 | #include "Threads.h" | 
 | 24 |  | 
 | 25 | #include "Client.h" | 
 | 26 | #include "IAfEffect.h" | 
 | 27 | #include "MelReporter.h" | 
 | 28 | #include "ResamplerBufferProvider.h" | 
 | 29 |  | 
 | 30 | #include <afutils/DumpTryLock.h> | 
 | 31 | #include <afutils/Permission.h> | 
 | 32 | #include <afutils/TypedLogger.h> | 
 | 33 | #include <afutils/Vibrator.h> | 
 | 34 | #include <audio_utils/MelProcessor.h> | 
 | 35 | #include <audio_utils/Metadata.h> | 
 | 36 | #ifdef DEBUG_CPU_USAGE | 
 | 37 | #include <audio_utils/Statistics.h> | 
 | 38 | #include <cpustats/ThreadCpuUsage.h> | 
 | 39 | #endif | 
 | 40 | #include <audio_utils/channels.h> | 
 | 41 | #include <audio_utils/format.h> | 
 | 42 | #include <audio_utils/minifloat.h> | 
 | 43 | #include <audio_utils/mono_blend.h> | 
 | 44 | #include <audio_utils/primitives.h> | 
 | 45 | #include <audio_utils/safe_math.h> | 
 | 46 | #include <audiomanager/AudioManager.h> | 
 | 47 | #include <binder/IPCThreadState.h> | 
 | 48 | #include <binder/IServiceManager.h> | 
 | 49 | #include <binder/PersistableBundle.h> | 
| Eric Laurent | 4eb45d0 | 2023-12-20 12:07:17 +0100 | [diff] [blame] | 50 | #include <com_android_media_audio.h> | 
| Glenn Kasten | c9a2367 | 2020-06-30 12:12:42 -0700 | [diff] [blame] | 51 | #include <cutils/bitops.h> | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 52 | #include <cutils/properties.h> | 
| Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 53 | #include <fastpath/AutoPark.h> | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 54 | #include <media/AudioContainers.h> | 
 | 55 | #include <media/AudioDeviceTypeAddr.h> | 
| Glenn Kasten | 1ab85ec | 2013-05-31 09:18:43 -0700 | [diff] [blame] | 56 | #include <media/AudioParameter.h> | 
| Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 57 | #include <media/AudioResamplerPublic.h> | 
| Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 58 | #ifdef ADD_BATTERY_DATA | 
 | 59 | #include <media/IMediaPlayerService.h> | 
 | 60 | #include <media/IMediaDeathNotifier.h> | 
 | 61 | #endif | 
 | 62 | #include <media/MmapStreamCallback.h> | 
| Andy Hung | 8981605 | 2017-01-11 17:08:23 -0800 | [diff] [blame] | 63 | #include <media/RecordBufferConverter.h> | 
| Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 64 | #include <media/TypeConverter.h> | 
| Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 65 | #include <media/audiohal/EffectsFactoryHalInterface.h> | 
 | 66 | #include <media/audiohal/StreamHalInterface.h> | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 67 | #include <media/nbaio/AudioStreamInSource.h> | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 68 | #include <media/nbaio/AudioStreamOutSink.h> | 
 | 69 | #include <media/nbaio/MonoPipe.h> | 
 | 70 | #include <media/nbaio/MonoPipeReader.h> | 
 | 71 | #include <media/nbaio/Pipe.h> | 
 | 72 | #include <media/nbaio/PipeReader.h> | 
 | 73 | #include <media/nbaio/SourceAudioBufferProvider.h> | 
| Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 74 | #include <mediautils/BatteryNotifier.h> | 
| Andy Hung | afc51db | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 75 | #include <mediautils/Process.h> | 
| Andy Hung | ab7ef30 | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 76 | #include <mediautils/SchedulingPolicyService.h> | 
 | 77 | #include <mediautils/ServiceUtilities.h> | 
| Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 78 | #include <powermanager/PowerManager.h> | 
 | 79 | #include <private/android_filesystem_config.h> | 
 | 80 | #include <private/media/AudioTrackShared.h> | 
 | 81 | #include <system/audio_effects/effect_aec.h> | 
 | 82 | #include <system/audio_effects/effect_downmix.h> | 
 | 83 | #include <system/audio_effects/effect_ns.h> | 
 | 84 | #include <system/audio_effects/effect_spatializer.h> | 
 | 85 | #include <utils/Log.h> | 
 | 86 | #include <utils/Trace.h> | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 87 |  | 
| Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 88 | #include <fcntl.h> | 
 | 89 | #include <linux/futex.h> | 
 | 90 | #include <math.h> | 
 | 91 | #include <memory> | 
| Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 92 | #include <pthread.h> | 
| Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 93 | #include <sstream> | 
 | 94 | #include <string> | 
 | 95 | #include <sys/stat.h> | 
 | 96 | #include <sys/syscall.h> | 
| Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 97 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 98 | // ---------------------------------------------------------------------------- | 
 | 99 |  | 
 | 100 | // Note: the following macro is used for extremely verbose logging message.  In | 
 | 101 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to | 
 | 102 | // 0; but one side effect of this is to turn all LOGV's as well.  Some messages | 
 | 103 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT | 
 | 104 | // turned on.  Do not uncomment the #def below unless you really know what you | 
 | 105 | // are doing and want to see all of the extremely verbose messages. | 
 | 106 | //#define VERY_VERY_VERBOSE_LOGGING | 
 | 107 | #ifdef VERY_VERY_VERBOSE_LOGGING | 
 | 108 | #define ALOGVV ALOGV | 
 | 109 | #else | 
 | 110 | #define ALOGVV(a...) do { } while(0) | 
 | 111 | #endif | 
 | 112 |  | 
| Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 113 | // TODO: Move these macro/inlines to a header file. | 
| Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 114 | #define max(a, b) ((a) > (b) ? (a) : (b)) | 
| Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 115 |  | 
| Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 116 | template <typename T> | 
 | 117 | static inline T min(const T& a, const T& b) | 
 | 118 | { | 
 | 119 |     return a < b ? a : b; | 
 | 120 | } | 
| Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 121 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 122 | namespace android { | 
 | 123 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 124 | using audioflinger::SyncEvent; | 
| Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 125 | using media::IEffectClient; | 
| Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 126 | using content::AttributionSourceState; | 
| Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 127 |  | 
| Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 128 | // Keep in sync with java definition in media/java/android/media/AudioRecord.java | 
 | 129 | static constexpr int32_t kMaxSharedAudioHistoryMs = 5000; | 
 | 130 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 131 | // retry counts for buffer fill timeout | 
 | 132 | // 50 * ~20msecs = 1 second | 
 | 133 | static const int8_t kMaxTrackRetries = 50; | 
 | 134 | static const int8_t kMaxTrackStartupRetries = 50; | 
| Andy Hung | 455982f | 2021-04-27 17:46:12 -0700 | [diff] [blame] | 135 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 136 | // allow less retry attempts on direct output thread. | 
 | 137 | // direct outputs can be a scarce resource in audio hardware and should | 
 | 138 | // be released as quickly as possible. | 
| Andy Hung | 455982f | 2021-04-27 17:46:12 -0700 | [diff] [blame] | 139 | // Notes: | 
 | 140 | // 1) The retry duration kMaxTrackRetriesDirectMs may be increased | 
 | 141 | //    in case the data write is bursty for the AudioTrack.  The application | 
 | 142 | //    should endeavor to write at least once every kMaxTrackRetriesDirectMs | 
 | 143 | //    to prevent an underrun situation.  If the data is bursty, then | 
 | 144 | //    the application can also throttle the data sent to be even. | 
 | 145 | // 2) For compressed audio data, any data present in the AudioTrack buffer | 
 | 146 | //    will be sent and reset the retry count.  This delivers data as | 
 | 147 | //    it arrives, with approximately kDirectMinSleepTimeUs = 10ms checking interval. | 
 | 148 | // 3) For linear PCM or proportional PCM, we wait one period for a period's worth | 
 | 149 | //    of data to be available, then any remaining data is delivered. | 
 | 150 | //    This is required to ensure the last bit of data is delivered before underrun. | 
 | 151 | // | 
 | 152 | // Sleep time per cycle is kDirectMinSleepTimeUs for compressed tracks | 
 | 153 | // or the size of the HAL period for proportional / linear PCM tracks. | 
 | 154 | static const int32_t kMaxTrackRetriesDirectMs = 200; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 155 |  | 
 | 156 | // don't warn about blocked writes or record buffer overflows more often than this | 
 | 157 | static const nsecs_t kWarningThrottleNs = seconds(5); | 
 | 158 |  | 
 | 159 | // RecordThread loop sleep time upon application overrun or audio HAL read error | 
 | 160 | static const int kRecordThreadSleepUs = 5000; | 
 | 161 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 162 | // maximum time to wait in sendConfigEvent_l() for a status to be received | 
 | 163 | static const nsecs_t kConfigEventTimeoutNs = seconds(2); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 164 |  | 
 | 165 | // minimum sleep time for the mixer thread loop when tracks are active but in underrun | 
 | 166 | static const uint32_t kMinThreadSleepTimeUs = 5000; | 
 | 167 | // maximum divider applied to the active sleep time in the mixer thread loop | 
 | 168 | static const uint32_t kMaxThreadSleepTimeShift = 2; | 
 | 169 |  | 
| Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 170 | // minimum normal sink buffer size, expressed in milliseconds rather than frames | 
| Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 171 | // FIXME This should be based on experimentally observed scheduling jitter | 
| Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 172 | static const uint32_t kMinNormalSinkBufferSizeMs = 20; | 
 | 173 | // maximum normal sink buffer size | 
 | 174 | static const uint32_t kMaxNormalSinkBufferSizeMs = 24; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 175 |  | 
| Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 176 | // minimum capture buffer size in milliseconds to _not_ need a fast capture thread | 
 | 177 | // FIXME This should be based on experimentally observed scheduling jitter | 
 | 178 | static const uint32_t kMinNormalCaptureBufferSizeMs = 12; | 
 | 179 |  | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 180 | // Offloaded output thread standby delay: allows track transition without going to standby | 
 | 181 | static const nsecs_t kOffloadStandbyDelayNs = seconds(1); | 
 | 182 |  | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 183 | // Direct output thread minimum sleep time in idle or active(underrun) state | 
 | 184 | static const nsecs_t kDirectMinSleepTimeUs = 10000; | 
 | 185 |  | 
| Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 186 | // Minimum amount of time between checking to see if the timestamp is advancing | 
 | 187 | // for underrun detection. If we check too frequently, we may not detect a | 
 | 188 | // timestamp update and will falsely detect underrun. | 
| Andy Hung | 0ff1429 | 2023-12-20 15:55:16 -0800 | [diff] [blame] | 189 | static constexpr nsecs_t kMinimumTimeBetweenTimestampChecksNs = 150 /* ms */ * 1'000'000; | 
| Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 190 |  | 
| Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 191 | // The universal constant for ubiquitous 20ms value. The value of 20ms seems to provide a good | 
 | 192 | // balance between power consumption and latency, and allows threads to be scheduled reliably | 
 | 193 | // by the CFS scheduler. | 
 | 194 | // FIXME Express other hardcoded references to 20ms with references to this constant and move | 
 | 195 | // it appropriately. | 
 | 196 | #define FMS_20 20 | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 197 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 198 | // Whether to use fast mixer | 
 | 199 | static const enum { | 
 | 200 |     FastMixer_Never,    // never initialize or use: for debugging only | 
 | 201 |     FastMixer_Always,   // always initialize and use, even if not needed: for debugging only | 
 | 202 |                         // normal mixer multiplier is 1 | 
 | 203 |     FastMixer_Static,   // initialize if needed, then use all the time if initialized, | 
 | 204 |                         // multiplier is calculated based on min & max normal mixer buffer size | 
 | 205 |     FastMixer_Dynamic,  // initialize if needed, then use dynamically depending on track load, | 
 | 206 |                         // multiplier is calculated based on min & max normal mixer buffer size | 
 | 207 |     // FIXME for FastMixer_Dynamic: | 
 | 208 |     //  Supporting this option will require fixing HALs that can't handle large writes. | 
 | 209 |     //  For example, one HAL implementation returns an error from a large write, | 
 | 210 |     //  and another HAL implementation corrupts memory, possibly in the sample rate converter. | 
 | 211 |     //  We could either fix the HAL implementations, or provide a wrapper that breaks | 
 | 212 |     //  up large writes into smaller ones, and the wrapper would need to deal with scheduler. | 
 | 213 | } kUseFastMixer = FastMixer_Static; | 
 | 214 |  | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 215 | // Whether to use fast capture | 
 | 216 | static const enum { | 
 | 217 |     FastCapture_Never,  // never initialize or use: for debugging only | 
 | 218 |     FastCapture_Always, // always initialize and use, even if not needed: for debugging only | 
 | 219 |     FastCapture_Static, // initialize if needed, then use all the time if initialized | 
 | 220 | } kUseFastCapture = FastCapture_Static; | 
 | 221 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 222 | // Priorities for requestPriority | 
 | 223 | static const int kPriorityAudioApp = 2; | 
 | 224 | static const int kPriorityFastMixer = 3; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 225 | static const int kPriorityFastCapture = 3; | 
| Pattara Teerapong | 9a332c5 | 2024-01-26 08:18:05 +0000 | [diff] [blame] | 226 | // Request real-time priority for PlaybackThread in ARC | 
 | 227 | static const int kPriorityPlaybackThreadArc = 1; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 228 |  | 
| Glenn Kasten | ea38ee7 | 2016-04-18 11:08:01 -0700 | [diff] [blame] | 229 | // IAudioFlinger::createTrack() has an in/out parameter 'pFrameCount' for the total size of the | 
 | 230 | // track buffer in shared memory.  Zero on input means to use a default value.  For fast tracks, | 
 | 231 | // AudioFlinger derives the default from HAL buffer size and 'fast track multiplier'. | 
| Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 232 |  | 
 | 233 | // This is the default value, if not specified by property. | 
| Glenn Kasten | b5fed68 | 2013-12-03 09:06:43 -0800 | [diff] [blame] | 234 | static const int kFastTrackMultiplier = 2; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 235 |  | 
| Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 236 | // The minimum and maximum allowed values | 
 | 237 | static const int kFastTrackMultiplierMin = 1; | 
 | 238 | static const int kFastTrackMultiplierMax = 2; | 
 | 239 |  | 
 | 240 | // The actual value to use, which can be specified per-device via property af.fast_track_multiplier. | 
 | 241 | static int sFastTrackMultiplier = kFastTrackMultiplier; | 
 | 242 |  | 
| Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 243 | // See Thread::readOnlyHeap(). | 
 | 244 | // Initially this heap is used to allocate client buffers for "fast" AudioRecord. | 
 | 245 | // Eventually it will be the single buffer that FastCapture writes into via HAL read(), | 
 | 246 | // and that all "fast" AudioRecord clients read from.  In either case, the size can be small. | 
| Mikhail Naganov | 79a7782 | 2018-05-10 15:57:25 -0700 | [diff] [blame] | 247 | static const size_t kRecordThreadReadOnlyHeapSize = 0xD000; | 
| Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 248 |  | 
| Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 249 | static const nsecs_t kDefaultStandbyTimeInNsecs = seconds(3); | 
| Andy Hung | d58c473 | 2023-07-20 21:31:38 -0700 | [diff] [blame] | 250 |  | 
 | 251 | static nsecs_t getStandbyTimeInNanos() { | 
 | 252 |     static nsecs_t standbyTimeInNanos = []() { | 
 | 253 |         const int ms = property_get_int32("ro.audio.flinger_standbytime_ms", | 
 | 254 |                     kDefaultStandbyTimeInNsecs / NANOS_PER_MILLISECOND); | 
 | 255 |         ALOGI("%s: Using %d ms as standby time", __func__, ms); | 
 | 256 |         return milliseconds(ms); | 
 | 257 |     }(); | 
 | 258 |     return standbyTimeInNanos; | 
 | 259 | } | 
 | 260 |  | 
| Andy Hung | d21a2ab | 2023-07-20 21:44:14 -0700 | [diff] [blame] | 261 | // Set kEnableExtendedChannels to true to enable greater than stereo output | 
 | 262 | // for the MixerThread and device sink.  Number of channels allowed is | 
 | 263 | // FCC_2 <= channels <= FCC_LIMIT. | 
 | 264 | constexpr bool kEnableExtendedChannels = true; | 
 | 265 |  | 
 | 266 | // Returns true if channel mask is permitted for the PCM sink in the MixerThread | 
 | 267 | /* static */ | 
 | 268 | bool IAfThreadBase::isValidPcmSinkChannelMask(audio_channel_mask_t channelMask) { | 
 | 269 |     switch (audio_channel_mask_get_representation(channelMask)) { | 
 | 270 |     case AUDIO_CHANNEL_REPRESENTATION_POSITION: { | 
 | 271 |         // Haptic channel mask is only applicable for channel position mask. | 
 | 272 |         const uint32_t channelCount = audio_channel_count_from_out_mask( | 
 | 273 |                 static_cast<audio_channel_mask_t>(channelMask & ~AUDIO_CHANNEL_HAPTIC_ALL)); | 
 | 274 |         const uint32_t maxChannelCount = kEnableExtendedChannels | 
 | 275 |                 ? FCC_LIMIT : FCC_2; | 
 | 276 |         if (channelCount < FCC_2 // mono is not supported at this time | 
 | 277 |                 || channelCount > maxChannelCount) { | 
 | 278 |             return false; | 
 | 279 |         } | 
 | 280 |         // check that channelMask is the "canonical" one we expect for the channelCount. | 
 | 281 |         return audio_channel_position_mask_is_out_canonical(channelMask); | 
 | 282 |         } | 
 | 283 |     case AUDIO_CHANNEL_REPRESENTATION_INDEX: | 
 | 284 |         if (kEnableExtendedChannels) { | 
 | 285 |             const uint32_t channelCount = audio_channel_count_from_out_mask(channelMask); | 
 | 286 |             if (channelCount >= FCC_2 // mono is not supported at this time | 
 | 287 |                     && channelCount <= FCC_LIMIT) { | 
 | 288 |                 return true; | 
 | 289 |             } | 
 | 290 |         } | 
 | 291 |         return false; | 
 | 292 |     default: | 
 | 293 |         return false; | 
 | 294 |     } | 
 | 295 | } | 
 | 296 |  | 
 | 297 | // Set kEnableExtendedPrecision to true to use extended precision in MixerThread | 
 | 298 | constexpr bool kEnableExtendedPrecision = true; | 
 | 299 |  | 
 | 300 | // Returns true if format is permitted for the PCM sink in the MixerThread | 
 | 301 | /* static */ | 
 | 302 | bool IAfThreadBase::isValidPcmSinkFormat(audio_format_t format) { | 
 | 303 |     switch (format) { | 
 | 304 |     case AUDIO_FORMAT_PCM_16_BIT: | 
 | 305 |         return true; | 
 | 306 |     case AUDIO_FORMAT_PCM_FLOAT: | 
 | 307 |     case AUDIO_FORMAT_PCM_24_BIT_PACKED: | 
 | 308 |     case AUDIO_FORMAT_PCM_32_BIT: | 
 | 309 |     case AUDIO_FORMAT_PCM_8_24_BIT: | 
 | 310 |         return kEnableExtendedPrecision; | 
 | 311 |     default: | 
 | 312 |         return false; | 
 | 313 |     } | 
 | 314 | } | 
 | 315 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 316 | // ---------------------------------------------------------------------------- | 
 | 317 |  | 
| Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 318 | // formatToString() needs to be exact for MediaMetrics purposes. | 
 | 319 | // Do not use media/TypeConverter.h toString(). | 
 | 320 | /* static */ | 
 | 321 | std::string IAfThreadBase::formatToString(audio_format_t format) { | 
 | 322 |     std::string result; | 
 | 323 |     FormatConverter::toString(format, result); | 
 | 324 |     return result; | 
 | 325 | } | 
 | 326 |  | 
| Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 327 | // TODO: move all toString helpers to audio.h | 
 | 328 | // under  #ifdef __cplusplus #endif | 
 | 329 | static std::string patchSinksToString(const struct audio_patch *patch) | 
 | 330 | { | 
 | 331 |     std::stringstream ss; | 
 | 332 |     for (size_t i = 0; i < patch->num_sinks; ++i) { | 
| Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 333 |         if (i > 0) { | 
 | 334 |             ss << "|"; | 
 | 335 |         } | 
| Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 336 |         ss << "(" << toString(patch->sinks[i].ext.device.type) | 
 | 337 |             << ", " << patch->sinks[i].ext.device.address << ")"; | 
 | 338 |     } | 
 | 339 |     return ss.str(); | 
 | 340 | } | 
 | 341 |  | 
 | 342 | static std::string patchSourcesToString(const struct audio_patch *patch) | 
 | 343 | { | 
 | 344 |     std::stringstream ss; | 
 | 345 |     for (size_t i = 0; i < patch->num_sources; ++i) { | 
| Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 346 |         if (i > 0) { | 
 | 347 |             ss << "|"; | 
 | 348 |         } | 
| Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 349 |         ss << "(" << toString(patch->sources[i].ext.device.type) | 
 | 350 |             << ", " << patch->sources[i].ext.device.address << ")"; | 
 | 351 |     } | 
 | 352 |     return ss.str(); | 
 | 353 | } | 
 | 354 |  | 
| Andy Hung | 4bd53e7 | 2022-11-17 17:21:45 -0800 | [diff] [blame] | 355 | static std::string toString(audio_latency_mode_t mode) { | 
 | 356 |     // We convert to the AIDL type to print (eventually the legacy type will be removed). | 
| Mikhail Naganov | f53e182 | 2022-12-18 02:48:14 +0000 | [diff] [blame] | 357 |     const auto result = legacy2aidl_audio_latency_mode_t_AudioLatencyMode(mode); | 
 | 358 |     return result.has_value() ? media::audio::common::toString(*result) : "UNKNOWN"; | 
| Andy Hung | 4bd53e7 | 2022-11-17 17:21:45 -0800 | [diff] [blame] | 359 | } | 
 | 360 |  | 
 | 361 | // Could be made a template, but other toString overloads for std::vector are confused. | 
 | 362 | static std::string toString(const std::vector<audio_latency_mode_t>& elements) { | 
 | 363 |     std::string s("{ "); | 
 | 364 |     for (const auto& e : elements) { | 
 | 365 |         s.append(toString(e)); | 
 | 366 |         s.append(" "); | 
 | 367 |     } | 
 | 368 |     s.append("}"); | 
 | 369 |     return s; | 
 | 370 | } | 
 | 371 |  | 
| Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 372 | static pthread_once_t sFastTrackMultiplierOnce = PTHREAD_ONCE_INIT; | 
 | 373 |  | 
 | 374 | static void sFastTrackMultiplierInit() | 
 | 375 | { | 
 | 376 |     char value[PROPERTY_VALUE_MAX]; | 
 | 377 |     if (property_get("af.fast_track_multiplier", value, NULL) > 0) { | 
 | 378 |         char *endptr; | 
 | 379 |         unsigned long ul = strtoul(value, &endptr, 0); | 
 | 380 |         if (*endptr == '\0' && kFastTrackMultiplierMin <= ul && ul <= kFastTrackMultiplierMax) { | 
 | 381 |             sFastTrackMultiplier = (int) ul; | 
 | 382 |         } | 
 | 383 |     } | 
 | 384 | } | 
 | 385 |  | 
 | 386 | // ---------------------------------------------------------------------------- | 
 | 387 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 388 | #ifdef ADD_BATTERY_DATA | 
 | 389 | // To collect the amplifier usage | 
 | 390 | static void addBatteryData(uint32_t params) { | 
 | 391 |     sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService(); | 
 | 392 |     if (service == NULL) { | 
 | 393 |         // it already logged | 
 | 394 |         return; | 
 | 395 |     } | 
 | 396 |  | 
 | 397 |     service->addBatteryData(params); | 
 | 398 | } | 
 | 399 | #endif | 
 | 400 |  | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 401 | // Track the CLOCK_BOOTTIME versus CLOCK_MONOTONIC timebase offset | 
 | 402 | struct { | 
 | 403 |     // call when you acquire a partial wakelock | 
 | 404 |     void acquire(const sp<IBinder> &wakeLockToken) { | 
 | 405 |         pthread_mutex_lock(&mLock); | 
 | 406 |         if (wakeLockToken.get() == nullptr) { | 
 | 407 |             adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME); | 
 | 408 |         } else { | 
 | 409 |             if (mCount == 0) { | 
 | 410 |                 adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME); | 
 | 411 |             } | 
 | 412 |             ++mCount; | 
 | 413 |         } | 
 | 414 |         pthread_mutex_unlock(&mLock); | 
 | 415 |     } | 
 | 416 |  | 
 | 417 |     // call when you release a partial wakelock. | 
 | 418 |     void release(const sp<IBinder> &wakeLockToken) { | 
 | 419 |         if (wakeLockToken.get() == nullptr) { | 
 | 420 |             return; | 
 | 421 |         } | 
 | 422 |         pthread_mutex_lock(&mLock); | 
 | 423 |         if (--mCount < 0) { | 
 | 424 |             ALOGE("negative wakelock count"); | 
 | 425 |             mCount = 0; | 
 | 426 |         } | 
 | 427 |         pthread_mutex_unlock(&mLock); | 
 | 428 |     } | 
 | 429 |  | 
 | 430 |     // retrieves the boottime timebase offset from monotonic. | 
 | 431 |     int64_t getBoottimeOffset() { | 
 | 432 |         pthread_mutex_lock(&mLock); | 
 | 433 |         int64_t boottimeOffset = mBoottimeOffset; | 
 | 434 |         pthread_mutex_unlock(&mLock); | 
 | 435 |         return boottimeOffset; | 
 | 436 |     } | 
 | 437 |  | 
 | 438 |     // Adjusts the timebase offset between TIMEBASE_MONOTONIC | 
 | 439 |     // and the selected timebase. | 
 | 440 |     // Currently only TIMEBASE_BOOTTIME is allowed. | 
 | 441 |     // | 
 | 442 |     // This only needs to be called upon acquiring the first partial wakelock | 
 | 443 |     // after all other partial wakelocks are released. | 
 | 444 |     // | 
 | 445 |     // We do an empirical measurement of the offset rather than parsing | 
 | 446 |     // /proc/timer_list since the latter is not a formal kernel ABI. | 
 | 447 |     static void adjustTimebaseOffset(int64_t *offset, ExtendedTimestamp::Timebase timebase) { | 
 | 448 |         int clockbase; | 
 | 449 |         switch (timebase) { | 
 | 450 |         case ExtendedTimestamp::TIMEBASE_BOOTTIME: | 
 | 451 |             clockbase = SYSTEM_TIME_BOOTTIME; | 
 | 452 |             break; | 
 | 453 |         default: | 
 | 454 |             LOG_ALWAYS_FATAL("invalid timebase %d", timebase); | 
 | 455 |             break; | 
 | 456 |         } | 
 | 457 |         // try three times to get the clock offset, choose the one | 
 | 458 |         // with the minimum gap in measurements. | 
 | 459 |         const int tries = 3; | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 460 |         nsecs_t bestGap = 0, measured = 0; // not required, initialized for clang-tidy | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 461 |         for (int i = 0; i < tries; ++i) { | 
 | 462 |             const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC); | 
 | 463 |             const nsecs_t tbase = systemTime(clockbase); | 
 | 464 |             const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC); | 
 | 465 |             const nsecs_t gap = tmono2 - tmono; | 
 | 466 |             if (i == 0 || gap < bestGap) { | 
 | 467 |                 bestGap = gap; | 
 | 468 |                 measured = tbase - ((tmono + tmono2) >> 1); | 
 | 469 |             } | 
 | 470 |         } | 
 | 471 |  | 
 | 472 |         // to avoid micro-adjusting, we don't change the timebase | 
 | 473 |         // unless it is significantly different. | 
 | 474 |         // | 
 | 475 |         // Assumption: It probably takes more than toleranceNs to | 
 | 476 |         // suspend and resume the device. | 
 | 477 |         static int64_t toleranceNs = 10000; // 10 us | 
 | 478 |         if (llabs(*offset - measured) > toleranceNs) { | 
 | 479 |             ALOGV("Adjusting timebase offset old: %lld  new: %lld", | 
 | 480 |                     (long long)*offset, (long long)measured); | 
 | 481 |             *offset = measured; | 
 | 482 |         } | 
 | 483 |     } | 
 | 484 |  | 
 | 485 |     pthread_mutex_t mLock; | 
 | 486 |     int32_t mCount; | 
 | 487 |     int64_t mBoottimeOffset; | 
 | 488 | } gBoottime = { PTHREAD_MUTEX_INITIALIZER, 0, 0 }; // static, so use POD initialization | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 489 |  | 
 | 490 | // ---------------------------------------------------------------------------- | 
 | 491 | //      CPU Stats | 
 | 492 | // ---------------------------------------------------------------------------- | 
 | 493 |  | 
 | 494 | class CpuStats { | 
 | 495 | public: | 
 | 496 |     CpuStats(); | 
 | 497 |     void sample(const String8 &title); | 
 | 498 | #ifdef DEBUG_CPU_USAGE | 
 | 499 | private: | 
 | 500 |     ThreadCpuUsage mCpuUsage;           // instantaneous thread CPU usage in wall clock ns | 
| Andy Hung | 16698b8 | 2018-08-01 10:48:38 -0700 | [diff] [blame] | 501 |     audio_utils::Statistics<double> mWcStats; // statistics on thread CPU usage in wall clock ns | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 502 |  | 
| Andy Hung | 16698b8 | 2018-08-01 10:48:38 -0700 | [diff] [blame] | 503 |     audio_utils::Statistics<double> mHzStats; // statistics on thread CPU usage in cycles | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 504 |  | 
 | 505 |     int mCpuNum;                        // thread's current CPU number | 
 | 506 |     int mCpukHz;                        // frequency of thread's current CPU in kHz | 
 | 507 | #endif | 
 | 508 | }; | 
 | 509 |  | 
 | 510 | CpuStats::CpuStats() | 
 | 511 | #ifdef DEBUG_CPU_USAGE | 
 | 512 |     : mCpuNum(-1), mCpukHz(-1) | 
 | 513 | #endif | 
 | 514 | { | 
 | 515 | } | 
 | 516 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 517 | void CpuStats::sample(const String8 &title | 
 | 518 | #ifndef DEBUG_CPU_USAGE | 
 | 519 |                 __unused | 
 | 520 | #endif | 
 | 521 |         ) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 522 | #ifdef DEBUG_CPU_USAGE | 
 | 523 |     // get current thread's delta CPU time in wall clock ns | 
 | 524 |     double wcNs; | 
 | 525 |     bool valid = mCpuUsage.sampleAndEnable(wcNs); | 
 | 526 |  | 
 | 527 |     // record sample for wall clock statistics | 
 | 528 |     if (valid) { | 
| Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 529 |         mWcStats.add(wcNs); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 530 |     } | 
 | 531 |  | 
 | 532 |     // get the current CPU number | 
 | 533 |     int cpuNum = sched_getcpu(); | 
 | 534 |  | 
 | 535 |     // get the current CPU frequency in kHz | 
 | 536 |     int cpukHz = mCpuUsage.getCpukHz(cpuNum); | 
 | 537 |  | 
 | 538 |     // check if either CPU number or frequency changed | 
 | 539 |     if (cpuNum != mCpuNum || cpukHz != mCpukHz) { | 
 | 540 |         mCpuNum = cpuNum; | 
 | 541 |         mCpukHz = cpukHz; | 
 | 542 |         // ignore sample for purposes of cycles | 
 | 543 |         valid = false; | 
 | 544 |     } | 
 | 545 |  | 
 | 546 |     // if no change in CPU number or frequency, then record sample for cycle statistics | 
 | 547 |     if (valid && mCpukHz > 0) { | 
| Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 548 |         const double cycles = wcNs * cpukHz * 0.000001; | 
 | 549 |         mHzStats.add(cycles); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 550 |     } | 
 | 551 |  | 
| Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 552 |     const unsigned n = mWcStats.getN(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 553 |     // mCpuUsage.elapsed() is expensive, so don't call it every loop | 
 | 554 |     if ((n & 127) == 1) { | 
| Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 555 |         const long long elapsed = mCpuUsage.elapsed(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 556 |         if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { | 
| Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 557 |             const double perLoop = elapsed / (double) n; | 
 | 558 |             const double perLoop100 = perLoop * 0.01; | 
 | 559 |             const double perLoop1k = perLoop * 0.001; | 
 | 560 |             const double mean = mWcStats.getMean(); | 
 | 561 |             const double stddev = mWcStats.getStdDev(); | 
 | 562 |             const double minimum = mWcStats.getMin(); | 
 | 563 |             const double maximum = mWcStats.getMax(); | 
 | 564 |             const double meanCycles = mHzStats.getMean(); | 
 | 565 |             const double stddevCycles = mHzStats.getStdDev(); | 
 | 566 |             const double minCycles = mHzStats.getMin(); | 
 | 567 |             const double maxCycles = mHzStats.getMax(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 568 |             mCpuUsage.resetElapsed(); | 
 | 569 |             mWcStats.reset(); | 
 | 570 |             mHzStats.reset(); | 
 | 571 |             ALOGD("CPU usage for %s over past %.1f secs\n" | 
 | 572 |                 "  (%u mixer loops at %.1f mean ms per loop):\n" | 
 | 573 |                 "  us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n" | 
 | 574 |                 "  %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n" | 
 | 575 |                 "  MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f", | 
| Tomasz Wasilczyk | 5b05437 | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 576 |                     title.c_str(), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 577 |                     elapsed * .000000001, n, perLoop * .000001, | 
 | 578 |                     mean * .001, | 
 | 579 |                     stddev * .001, | 
 | 580 |                     minimum * .001, | 
 | 581 |                     maximum * .001, | 
 | 582 |                     mean / perLoop100, | 
 | 583 |                     stddev / perLoop100, | 
 | 584 |                     minimum / perLoop100, | 
 | 585 |                     maximum / perLoop100, | 
 | 586 |                     meanCycles / perLoop1k, | 
 | 587 |                     stddevCycles / perLoop1k, | 
 | 588 |                     minCycles / perLoop1k, | 
 | 589 |                     maxCycles / perLoop1k); | 
 | 590 |  | 
 | 591 |         } | 
 | 592 |     } | 
 | 593 | #endif | 
 | 594 | }; | 
 | 595 |  | 
 | 596 | // ---------------------------------------------------------------------------- | 
 | 597 | //      ThreadBase | 
 | 598 | // ---------------------------------------------------------------------------- | 
 | 599 |  | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 600 | // static | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 601 | const char* ThreadBase::threadTypeToString(ThreadBase::type_t type) | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 602 | { | 
 | 603 |     switch (type) { | 
 | 604 |     case MIXER: | 
 | 605 |         return "MIXER"; | 
 | 606 |     case DIRECT: | 
 | 607 |         return "DIRECT"; | 
 | 608 |     case DUPLICATING: | 
 | 609 |         return "DUPLICATING"; | 
 | 610 |     case RECORD: | 
 | 611 |         return "RECORD"; | 
 | 612 |     case OFFLOAD: | 
 | 613 |         return "OFFLOAD"; | 
| Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 614 |     case MMAP_PLAYBACK: | 
 | 615 |         return "MMAP_PLAYBACK"; | 
 | 616 |     case MMAP_CAPTURE: | 
 | 617 |         return "MMAP_CAPTURE"; | 
| Eric Laurent | 1c5e2e3 | 2021-08-18 18:50:28 +0200 | [diff] [blame] | 618 |     case SPATIALIZER: | 
 | 619 |         return "SPATIALIZER"; | 
| jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 620 |     case BIT_PERFECT: | 
 | 621 |         return "BIT_PERFECT"; | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 622 |     default: | 
 | 623 |         return "unknown"; | 
 | 624 |     } | 
 | 625 | } | 
 | 626 |  | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 627 | ThreadBase::ThreadBase(const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id, | 
| Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 628 |         type_t type, bool systemReady, bool isOut) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 629 |     :   Thread(false /*canCallJava*/), | 
 | 630 |         mType(type), | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 631 |         mAfThreadCallback(afThreadCallback), | 
| Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 632 |         mThreadMetrics(std::string(AMEDIAMETRICS_KEY_PREFIX_AUDIO_THREAD) + std::to_string(id), | 
 | 633 |                isOut), | 
 | 634 |         mIsOut(isOut), | 
| Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 635 |         // mSampleRate, mFrameCount, mChannelMask, mChannelCount, mFrameSize, mFormat, mBufferSize | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 636 |         // are set by PlaybackThread::readOutputParameters_l() or | 
 | 637 |         // RecordThread::readInputParameters_l() | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 638 |         //FIXME: mStandby should be true here. Is this some kind of hack? | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 639 |         mStandby(false), | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 640 |         mAudioSource(AUDIO_SOURCE_DEFAULT), mId(id), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 641 |         // mName will be set by concrete (non-virtual) subclass | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 642 |         mDeathRecipient(new PMDeathRecipient(this)), | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 643 |         mSystemReady(systemReady), | 
 | 644 |         mSignalPending(false) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 645 | { | 
| Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 646 |     mThreadMetrics.logConstructor(getpid(), threadTypeToString(type), id); | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 647 |     memset(&mPatch, 0, sizeof(struct audio_patch)); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 648 | } | 
 | 649 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 650 | ThreadBase::~ThreadBase() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 651 | { | 
| Glenn Kasten | c6ae3c8 | 2013-07-17 09:08:51 -0700 | [diff] [blame] | 652 |     // 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] | 653 |     mConfigEvents.clear(); | 
 | 654 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 655 |     // do not lock the mutex in destructor | 
 | 656 |     releaseWakeLock_l(); | 
 | 657 |     if (mPowerManager != 0) { | 
| Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 658 |         sp<IBinder> binder = IInterface::asBinder(mPowerManager); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 659 |         binder->unlinkToDeath(mDeathRecipient); | 
 | 660 |     } | 
| Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 661 |  | 
 | 662 |     sendStatistics(true /* force */); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 663 | } | 
 | 664 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 665 | status_t ThreadBase::readyToRun() | 
| Glenn Kasten | cf04c2c | 2013-08-06 07:41:16 -0700 | [diff] [blame] | 666 | { | 
 | 667 |     status_t status = initCheck(); | 
 | 668 |     if (status == NO_ERROR) { | 
| Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 669 |         ALOGI("AudioFlinger's thread %p tid=%d ready to run", this, getTid()); | 
| Glenn Kasten | cf04c2c | 2013-08-06 07:41:16 -0700 | [diff] [blame] | 670 |     } else { | 
 | 671 |         ALOGE("No working audio driver found."); | 
 | 672 |     } | 
 | 673 |     return status; | 
 | 674 | } | 
 | 675 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 676 | void ThreadBase::exit() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 677 | { | 
 | 678 |     ALOGV("ThreadBase::exit"); | 
 | 679 |     // do any cleanup required for exit to succeed | 
 | 680 |     preExit(); | 
 | 681 |     { | 
 | 682 |         // This lock prevents the following race in thread (uniprocessor for illustration): | 
 | 683 |         //  if (!exitPending()) { | 
 | 684 |         //      // context switch from here to exit() | 
 | 685 |         //      // exit() calls requestExit(), what exitPending() observes | 
 | 686 |         //      // exit() calls signal(), which is dropped since no waiters | 
 | 687 |         //      // context switch back from exit() to here | 
 | 688 |         //      mWaitWorkCV.wait(...); | 
 | 689 |         //      // now thread is hung | 
 | 690 |         //  } | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 691 |         audio_utils::lock_guard lock(mutex()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 692 |         requestExit(); | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 693 |         mWaitWorkCV.notify_all(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 694 |     } | 
 | 695 |     // When Thread::requestExitAndWait is made virtual and this method is renamed to | 
 | 696 |     // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();" | 
| Andy Hung | ef2096d | 2024-03-21 19:43:05 -0700 | [diff] [blame] | 697 |  | 
 | 698 |     // For TimeCheck: track waiting on the thread join of getTid(). | 
 | 699 |     audio_utils::mutex::scoped_join_wait_check sjw(getTid()); | 
 | 700 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 701 |     requestExitAndWait(); | 
 | 702 | } | 
 | 703 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 704 | status_t ThreadBase::setParameters(const String8& keyValuePairs) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 705 | { | 
| Tomasz Wasilczyk | 5b05437 | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 706 |     ALOGV("ThreadBase::setParameters() %s", keyValuePairs.c_str()); | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 707 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 708 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 709 |     return sendSetParameterConfigEvent_l(keyValuePairs); | 
 | 710 | } | 
 | 711 |  | 
 | 712 | // sendConfigEvent_l() must be called with ThreadBase::mLock held | 
 | 713 | // Can temporarily release the lock if waiting for a reply from processConfigEvents_l(). | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 714 | status_t ThreadBase::sendConfigEvent_l(sp<ConfigEvent>& event) | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 715 | NO_THREAD_SAFETY_ANALYSIS  // condition variable | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 716 | { | 
 | 717 |     status_t status = NO_ERROR; | 
 | 718 |  | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 719 |     if (event->mRequiresSystemReady && !mSystemReady) { | 
 | 720 |         event->mWaitStatus = false; | 
 | 721 |         mPendingConfigEvents.add(event); | 
 | 722 |         return status; | 
 | 723 |     } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 724 |     mConfigEvents.add(event); | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 725 |     ALOGV("sendConfigEvent_l() num events %zu event %d", mConfigEvents.size(), event->mType); | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 726 |     mWaitWorkCV.notify_one(); | 
 | 727 |     mutex().unlock(); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 728 |     { | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 729 |         audio_utils::unique_lock _l(event->mutex()); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 730 |         while (event->mWaitStatus) { | 
| Andy Hung | 5529c13 | 2024-01-25 17:02:30 -0800 | [diff] [blame] | 731 |             if (event->mCondition.wait_for( | 
 | 732 |                     _l, std::chrono::nanoseconds(kConfigEventTimeoutNs), getTid()) | 
 | 733 |                             == std::cv_status::timeout) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 734 |                 event->mStatus = TIMED_OUT; | 
 | 735 |                 event->mWaitStatus = false; | 
 | 736 |             } | 
 | 737 |         } | 
 | 738 |         status = event->mStatus; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 739 |     } | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 740 |     mutex().lock(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 741 |     return status; | 
 | 742 | } | 
 | 743 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 744 | void ThreadBase::sendIoConfigEvent(audio_io_config_event_t event, pid_t pid, | 
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 745 |                                                  audio_port_handle_t portId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 746 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 747 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 748 |     sendIoConfigEvent_l(event, pid, portId); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 749 | } | 
 | 750 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 751 | // sendIoConfigEvent_l() must be called with ThreadBase::mutex() held | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 752 | void ThreadBase::sendIoConfigEvent_l(audio_io_config_event_t event, pid_t pid, | 
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 753 |                                                    audio_port_handle_t portId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 754 | { | 
| Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 755 |     // The audio statistics history is exponentially weighted to forget events | 
 | 756 |     // about five or more seconds in the past.  In order to have | 
 | 757 |     // crisper statistics for mediametrics, we reset the statistics on | 
 | 758 |     // an IoConfigEvent, to reflect different properties for a new device. | 
 | 759 |     mIoJitterMs.reset(); | 
 | 760 |     mLatencyMs.reset(); | 
 | 761 |     mProcessTimeMs.reset(); | 
| Robert Wu | 06db0a3 | 2021-08-10 19:05:34 +0000 | [diff] [blame] | 762 |     mMonopipePipeDepthStats.reset(); | 
| Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 763 |     mTimestampVerifier.discontinuity(mTimestampVerifier.DISCONTINUITY_MODE_CONTINUOUS); | 
| Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 764 |  | 
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 765 |     sp<ConfigEvent> configEvent = (ConfigEvent *)new IoConfigEvent(event, pid, portId); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 766 |     sendConfigEvent_l(configEvent); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 767 | } | 
 | 768 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 769 | void ThreadBase::sendPrioConfigEvent(pid_t pid, pid_t tid, int32_t prio, bool forApp) | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 770 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 771 |     audio_utils::lock_guard _l(mutex()); | 
| Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 772 |     sendPrioConfigEvent_l(pid, tid, prio, forApp); | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 773 | } | 
 | 774 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 775 | // sendPrioConfigEvent_l() must be called with ThreadBase::mutex() held | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 776 | void ThreadBase::sendPrioConfigEvent_l( | 
| Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 777 |         pid_t pid, pid_t tid, int32_t prio, bool forApp) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 778 | { | 
| Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 779 |     sp<ConfigEvent> configEvent = (ConfigEvent *)new PrioConfigEvent(pid, tid, prio, forApp); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 780 |     sendConfigEvent_l(configEvent); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 781 | } | 
 | 782 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 783 | // sendSetParameterConfigEvent_l() must be called with ThreadBase::mutex() held | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 784 | status_t ThreadBase::sendSetParameterConfigEvent_l(const String8& keyValuePair) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 785 | { | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 786 |     sp<ConfigEvent> configEvent; | 
 | 787 |     AudioParameter param(keyValuePair); | 
 | 788 |     int value; | 
| Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 789 |     if (param.getInt(String8(AudioParameter::keyMonoOutput), value) == NO_ERROR) { | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 790 |         setMasterMono_l(value != 0); | 
 | 791 |         if (param.size() == 1) { | 
 | 792 |             return NO_ERROR; // should be a solo parameter - we don't pass down | 
 | 793 |         } | 
| Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 794 |         param.remove(String8(AudioParameter::keyMonoOutput)); | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 795 |         configEvent = new SetParameterConfigEvent(param.toString()); | 
 | 796 |     } else { | 
 | 797 |         configEvent = new SetParameterConfigEvent(keyValuePair); | 
 | 798 |     } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 799 |     return sendConfigEvent_l(configEvent); | 
| Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 800 | } | 
 | 801 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 802 | status_t ThreadBase::sendCreateAudioPatchConfigEvent( | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 803 |                                                         const struct audio_patch *patch, | 
 | 804 |                                                         audio_patch_handle_t *handle) | 
 | 805 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 806 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 807 |     sp<ConfigEvent> configEvent = (ConfigEvent *)new CreateAudioPatchConfigEvent(*patch, *handle); | 
 | 808 |     status_t status = sendConfigEvent_l(configEvent); | 
 | 809 |     if (status == NO_ERROR) { | 
 | 810 |         CreateAudioPatchConfigEventData *data = | 
 | 811 |                                         (CreateAudioPatchConfigEventData *)configEvent->mData.get(); | 
 | 812 |         *handle = data->mHandle; | 
 | 813 |     } | 
 | 814 |     return status; | 
 | 815 | } | 
 | 816 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 817 | status_t ThreadBase::sendReleaseAudioPatchConfigEvent( | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 818 |                                                                 const audio_patch_handle_t handle) | 
 | 819 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 820 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 821 |     sp<ConfigEvent> configEvent = (ConfigEvent *)new ReleaseAudioPatchConfigEvent(handle); | 
 | 822 |     return sendConfigEvent_l(configEvent); | 
 | 823 | } | 
 | 824 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 825 | status_t ThreadBase::sendUpdateOutDeviceConfigEvent( | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 826 |         const DeviceDescriptorBaseVector& outDevices) | 
 | 827 | { | 
 | 828 |     if (type() != RECORD) { | 
 | 829 |         // The update out device operation is only for record thread. | 
 | 830 |         return INVALID_OPERATION; | 
 | 831 |     } | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 832 |     audio_utils::lock_guard _l(mutex()); | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 833 |     sp<ConfigEvent> configEvent = (ConfigEvent *)new UpdateOutDevicesConfigEvent(outDevices); | 
 | 834 |     return sendConfigEvent_l(configEvent); | 
 | 835 | } | 
 | 836 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 837 | void ThreadBase::sendResizeBufferConfigEvent_l(int32_t maxSharedAudioHistoryMs) | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 838 | { | 
 | 839 |     ALOG_ASSERT(type() == RECORD, "sendResizeBufferConfigEvent_l() called on non record thread"); | 
 | 840 |     sp<ConfigEvent> configEvent = | 
 | 841 |             (ConfigEvent *)new ResizeBufferConfigEvent(maxSharedAudioHistoryMs); | 
 | 842 |     sendConfigEvent_l(configEvent); | 
 | 843 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 844 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 845 | void ThreadBase::sendCheckOutputStageEffectsEvent() | 
| Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 846 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 847 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 848 |     sendCheckOutputStageEffectsEvent_l(); | 
 | 849 | } | 
 | 850 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 851 | void ThreadBase::sendCheckOutputStageEffectsEvent_l() | 
| Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 852 | { | 
 | 853 |     sp<ConfigEvent> configEvent = | 
 | 854 |             (ConfigEvent *)new CheckOutputStageEffectsEvent(); | 
 | 855 |     sendConfigEvent_l(configEvent); | 
 | 856 | } | 
 | 857 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 858 | void ThreadBase::sendHalLatencyModesChangedEvent_l() | 
| Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 859 | { | 
 | 860 |     sp<ConfigEvent> configEvent = sp<HalLatencyModesChangedEvent>::make(); | 
 | 861 |     sendConfigEvent_l(configEvent); | 
 | 862 | } | 
 | 863 |  | 
| Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 864 | // post condition: mConfigEvents.isEmpty() | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 865 | void ThreadBase::processConfigEvents_l() | 
| Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 866 | { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 867 |     bool configChanged = false; | 
 | 868 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 869 |     while (!mConfigEvents.isEmpty()) { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 870 |         ALOGV("processConfigEvents_l() remaining events %zu", mConfigEvents.size()); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 871 |         sp<ConfigEvent> event = mConfigEvents[0]; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 872 |         mConfigEvents.removeAt(0); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 873 |         switch (event->mType) { | 
| Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 874 |         case CFG_EVENT_PRIO: { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 875 |             PrioConfigEventData *data = (PrioConfigEventData *)event->mData.get(); | 
 | 876 |             // FIXME Need to understand why this has to be done asynchronously | 
| Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 877 |             int err = requestPriority(data->mPid, data->mTid, data->mPrio, data->mForApp, | 
| Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 878 |                     true /*asynchronous*/); | 
 | 879 |             if (err != 0) { | 
 | 880 |                 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] | 881 |                       data->mPrio, data->mPid, data->mTid, err); | 
| Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 882 |             } | 
 | 883 |         } break; | 
 | 884 |         case CFG_EVENT_IO: { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 885 |             IoConfigEventData *data = (IoConfigEventData *)event->mData.get(); | 
| Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 886 |             ioConfigChanged_l(data->mEvent, data->mPid, data->mPortId); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 887 |         } break; | 
 | 888 |         case CFG_EVENT_SET_PARAMETER: { | 
 | 889 |             SetParameterConfigEventData *data = (SetParameterConfigEventData *)event->mData.get(); | 
 | 890 |             if (checkForNewParameter_l(data->mKeyValuePairs, event->mStatus)) { | 
 | 891 |                 configChanged = true; | 
| Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 892 |                 mLocalLog.log("CFG_EVENT_SET_PARAMETER: (%s) configuration changed", | 
| Tomasz Wasilczyk | 5b05437 | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 893 |                         data->mKeyValuePairs.c_str()); | 
| Glenn Kasten | d5418eb | 2013-08-14 13:11:06 -0700 | [diff] [blame] | 894 |             } | 
| Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 895 |         } break; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 896 |         case CFG_EVENT_CREATE_AUDIO_PATCH: { | 
| Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 897 |             const DeviceTypeSet oldDevices = getDeviceTypes_l(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 898 |             CreateAudioPatchConfigEventData *data = | 
 | 899 |                                             (CreateAudioPatchConfigEventData *)event->mData.get(); | 
 | 900 |             event->mStatus = createAudioPatch_l(&data->mPatch, &data->mHandle); | 
| Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 901 |             const DeviceTypeSet newDevices = getDeviceTypes_l(); | 
| Eric Laurent | 5256814 | 2022-10-28 11:23:28 +0200 | [diff] [blame] | 902 |             configChanged = oldDevices != newDevices; | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 903 |             mLocalLog.log("CFG_EVENT_CREATE_AUDIO_PATCH: old device %s (%s) new device %s (%s)", | 
 | 904 |                     dumpDeviceTypes(oldDevices).c_str(), toString(oldDevices).c_str(), | 
 | 905 |                     dumpDeviceTypes(newDevices).c_str(), toString(newDevices).c_str()); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 906 |         } break; | 
 | 907 |         case CFG_EVENT_RELEASE_AUDIO_PATCH: { | 
| Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 908 |             const DeviceTypeSet oldDevices = getDeviceTypes_l(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 909 |             ReleaseAudioPatchConfigEventData *data = | 
 | 910 |                                             (ReleaseAudioPatchConfigEventData *)event->mData.get(); | 
 | 911 |             event->mStatus = releaseAudioPatch_l(data->mHandle); | 
| Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 912 |             const DeviceTypeSet newDevices = getDeviceTypes_l(); | 
| Eric Laurent | 5256814 | 2022-10-28 11:23:28 +0200 | [diff] [blame] | 913 |             configChanged = oldDevices != newDevices; | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 914 |             mLocalLog.log("CFG_EVENT_RELEASE_AUDIO_PATCH: old device %s (%s) new device %s (%s)", | 
 | 915 |                     dumpDeviceTypes(oldDevices).c_str(), toString(oldDevices).c_str(), | 
 | 916 |                     dumpDeviceTypes(newDevices).c_str(), toString(newDevices).c_str()); | 
 | 917 |         } break; | 
 | 918 |         case CFG_EVENT_UPDATE_OUT_DEVICE: { | 
 | 919 |             UpdateOutDevicesConfigEventData *data = | 
 | 920 |                     (UpdateOutDevicesConfigEventData *)event->mData.get(); | 
 | 921 |             updateOutDevices(data->mOutDevices); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 922 |         } break; | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 923 |         case CFG_EVENT_RESIZE_BUFFER: { | 
 | 924 |             ResizeBufferConfigEventData *data = | 
 | 925 |                     (ResizeBufferConfigEventData *)event->mData.get(); | 
 | 926 |             resizeInputBuffer_l(data->mMaxSharedAudioHistoryMs); | 
 | 927 |         } break; | 
| Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 928 |  | 
 | 929 |         case CFG_EVENT_CHECK_OUTPUT_STAGE_EFFECTS: { | 
 | 930 |             setCheckOutputStageEffects(); | 
 | 931 |         } break; | 
 | 932 |  | 
| Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 933 |         case CFG_EVENT_HAL_LATENCY_MODES_CHANGED: { | 
 | 934 |             onHalLatencyModesChanged_l(); | 
 | 935 |         } break; | 
 | 936 |  | 
| Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 937 |         default: | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 938 |             ALOG_ASSERT(false, "processConfigEvents_l() unknown event type %d", event->mType); | 
| Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 939 |             break; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 940 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 941 |         { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 942 |             audio_utils::lock_guard _l(event->mutex()); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 943 |             if (event->mWaitStatus) { | 
 | 944 |                 event->mWaitStatus = false; | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 945 |                 event->mCondition.notify_one(); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 946 |             } | 
 | 947 |         } | 
 | 948 |         ALOGV_IF(mConfigEvents.isEmpty(), "processConfigEvents_l() DONE thread %p", this); | 
 | 949 |     } | 
 | 950 |  | 
 | 951 |     if (configChanged) { | 
 | 952 |         cacheParameters_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 953 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 954 | } | 
 | 955 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 956 | String8 channelMaskToString(audio_channel_mask_t mask, bool output) { | 
 | 957 |     String8 s; | 
| Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 958 |     const audio_channel_representation_t representation = | 
 | 959 |             audio_channel_mask_get_representation(mask); | 
| Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 960 |  | 
 | 961 |     switch (representation) { | 
| jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 962 |     // Travel all single bit channel mask to convert channel mask to string. | 
| Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 963 |     case AUDIO_CHANNEL_REPRESENTATION_POSITION: { | 
 | 964 |         if (output) { | 
 | 965 |             if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT) s.append("front-left, "); | 
 | 966 |             if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT) s.append("front-right, "); | 
 | 967 |             if (mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) s.append("front-center, "); | 
| Andy Hung | fba7125 | 2021-05-07 11:58:32 -0700 | [diff] [blame] | 968 |             if (mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) s.append("low-frequency, "); | 
| Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 969 |             if (mask & AUDIO_CHANNEL_OUT_BACK_LEFT) s.append("back-left, "); | 
 | 970 |             if (mask & AUDIO_CHANNEL_OUT_BACK_RIGHT) s.append("back-right, "); | 
 | 971 |             if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER) s.append("front-left-of-center, "); | 
 | 972 |             if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER) s.append("front-right-of-center, "); | 
 | 973 |             if (mask & AUDIO_CHANNEL_OUT_BACK_CENTER) s.append("back-center, "); | 
 | 974 |             if (mask & AUDIO_CHANNEL_OUT_SIDE_LEFT) s.append("side-left, "); | 
 | 975 |             if (mask & AUDIO_CHANNEL_OUT_SIDE_RIGHT) s.append("side-right, "); | 
 | 976 |             if (mask & AUDIO_CHANNEL_OUT_TOP_CENTER) s.append("top-center ,"); | 
 | 977 |             if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT) s.append("top-front-left, "); | 
 | 978 |             if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER) s.append("top-front-center, "); | 
 | 979 |             if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT) s.append("top-front-right, "); | 
 | 980 |             if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_LEFT) s.append("top-back-left, "); | 
| Andy Hung | ae81b4c | 2021-05-07 08:54:28 -0700 | [diff] [blame] | 981 |             if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_CENTER) s.append("top-back-center, "); | 
 | 982 |             if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT) s.append("top-back-right, "); | 
 | 983 |             if (mask & AUDIO_CHANNEL_OUT_TOP_SIDE_LEFT) s.append("top-side-left, "); | 
 | 984 |             if (mask & AUDIO_CHANNEL_OUT_TOP_SIDE_RIGHT) s.append("top-side-right, "); | 
 | 985 |             if (mask & AUDIO_CHANNEL_OUT_BOTTOM_FRONT_LEFT) s.append("bottom-front-left, "); | 
 | 986 |             if (mask & AUDIO_CHANNEL_OUT_BOTTOM_FRONT_CENTER) s.append("bottom-front-center, "); | 
 | 987 |             if (mask & AUDIO_CHANNEL_OUT_BOTTOM_FRONT_RIGHT) s.append("bottom-front-right, "); | 
| Andy Hung | fba7125 | 2021-05-07 11:58:32 -0700 | [diff] [blame] | 988 |             if (mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY_2) s.append("low-frequency-2, "); | 
| Andy Hung | ae81b4c | 2021-05-07 08:54:28 -0700 | [diff] [blame] | 989 |             if (mask & AUDIO_CHANNEL_OUT_HAPTIC_B) s.append("haptic-B, "); | 
 | 990 |             if (mask & AUDIO_CHANNEL_OUT_HAPTIC_A) s.append("haptic-A, "); | 
| Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 991 |             if (mask & ~AUDIO_CHANNEL_OUT_ALL) s.append("unknown,  "); | 
 | 992 |         } else { | 
 | 993 |             if (mask & AUDIO_CHANNEL_IN_LEFT) s.append("left, "); | 
 | 994 |             if (mask & AUDIO_CHANNEL_IN_RIGHT) s.append("right, "); | 
 | 995 |             if (mask & AUDIO_CHANNEL_IN_FRONT) s.append("front, "); | 
 | 996 |             if (mask & AUDIO_CHANNEL_IN_BACK) s.append("back, "); | 
 | 997 |             if (mask & AUDIO_CHANNEL_IN_LEFT_PROCESSED) s.append("left-processed, "); | 
 | 998 |             if (mask & AUDIO_CHANNEL_IN_RIGHT_PROCESSED) s.append("right-processed, "); | 
 | 999 |             if (mask & AUDIO_CHANNEL_IN_FRONT_PROCESSED) s.append("front-processed, "); | 
 | 1000 |             if (mask & AUDIO_CHANNEL_IN_BACK_PROCESSED) s.append("back-processed, "); | 
 | 1001 |             if (mask & AUDIO_CHANNEL_IN_PRESSURE) s.append("pressure, "); | 
 | 1002 |             if (mask & AUDIO_CHANNEL_IN_X_AXIS) s.append("X, "); | 
 | 1003 |             if (mask & AUDIO_CHANNEL_IN_Y_AXIS) s.append("Y, "); | 
 | 1004 |             if (mask & AUDIO_CHANNEL_IN_Z_AXIS) s.append("Z, "); | 
| Mikhail Naganov | c994849 | 2018-05-10 17:25:28 -0700 | [diff] [blame] | 1005 |             if (mask & AUDIO_CHANNEL_IN_BACK_LEFT) s.append("back-left, "); | 
 | 1006 |             if (mask & AUDIO_CHANNEL_IN_BACK_RIGHT) s.append("back-right, "); | 
 | 1007 |             if (mask & AUDIO_CHANNEL_IN_CENTER) s.append("center, "); | 
| Andy Hung | fba7125 | 2021-05-07 11:58:32 -0700 | [diff] [blame] | 1008 |             if (mask & AUDIO_CHANNEL_IN_LOW_FREQUENCY) s.append("low-frequency, "); | 
| Andy Hung | ae81b4c | 2021-05-07 08:54:28 -0700 | [diff] [blame] | 1009 |             if (mask & AUDIO_CHANNEL_IN_TOP_LEFT) s.append("top-left, "); | 
 | 1010 |             if (mask & AUDIO_CHANNEL_IN_TOP_RIGHT) s.append("top-right, "); | 
| Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 1011 |             if (mask & AUDIO_CHANNEL_IN_VOICE_UPLINK) s.append("voice-uplink, "); | 
 | 1012 |             if (mask & AUDIO_CHANNEL_IN_VOICE_DNLINK) s.append("voice-dnlink, "); | 
 | 1013 |             if (mask & ~AUDIO_CHANNEL_IN_ALL) s.append("unknown,  "); | 
 | 1014 |         } | 
 | 1015 |         const int len = s.length(); | 
 | 1016 |         if (len > 2) { | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 1017 |             (void) s.lockBuffer(len);      // needed? | 
| Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 1018 |             s.unlockBuffer(len - 2);       // remove trailing ", " | 
 | 1019 |         } | 
 | 1020 |         return s; | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1021 |     } | 
| Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 1022 |     case AUDIO_CHANNEL_REPRESENTATION_INDEX: | 
 | 1023 |         s.appendFormat("index mask, bits:%#x", audio_channel_mask_get_bits(mask)); | 
 | 1024 |         return s; | 
 | 1025 |     default: | 
 | 1026 |         s.appendFormat("unknown mask, representation:%d  bits:%#x", | 
 | 1027 |                 representation, audio_channel_mask_get_bits(mask)); | 
 | 1028 |         return s; | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1029 |     } | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1030 | } | 
 | 1031 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1032 | void ThreadBase::dump(int fd, const Vector<String16>& args) | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 1033 | NO_THREAD_SAFETY_ANALYSIS  // conditional try lock | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1034 | { | 
| Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 1035 |     dprintf(fd, "\n%s thread %p, name %s, tid %d, type %d (%s):\n", isOutput() ? "Output" : "Input", | 
 | 1036 |             this, mThreadName, getTid(), type(), threadTypeToString(type())); | 
 | 1037 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1038 |     const bool locked = afutils::dumpTryLock(mutex()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1039 |     if (!locked) { | 
| Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 1040 |         dprintf(fd, "  Thread may be deadlocked\n"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1041 |     } | 
 | 1042 |  | 
| Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 1043 |     dumpBase_l(fd, args); | 
 | 1044 |     dumpInternals_l(fd, args); | 
 | 1045 |     dumpTracks_l(fd, args); | 
 | 1046 |     dumpEffectChains_l(fd, args); | 
 | 1047 |  | 
 | 1048 |     if (locked) { | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1049 |         mutex().unlock(); | 
| Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 1050 |     } | 
 | 1051 |  | 
 | 1052 |     dprintf(fd, "  Local log:\n"); | 
 | 1053 |     mLocalLog.dump(fd, "   " /* prefix */, 40 /* lines */); | 
| Andy Hung | afc51db | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 1054 |  | 
 | 1055 |     // --all does the statistics | 
 | 1056 |     bool dumpAll = false; | 
 | 1057 |     for (const auto &arg : args) { | 
 | 1058 |         if (arg == String16("--all")) { | 
 | 1059 |             dumpAll = true; | 
 | 1060 |         } | 
 | 1061 |     } | 
 | 1062 |     if (dumpAll || type() == SPATIALIZER) { | 
| Andy Hung | 44d648b | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 1063 |         const std::string sched = mThreadSnapshot.toString(); | 
| Andy Hung | afc51db | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 1064 |         if (!sched.empty()) { | 
 | 1065 |             (void)write(fd, sched.c_str(), sched.size()); | 
 | 1066 |         } | 
 | 1067 |     } | 
| Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 1068 | } | 
 | 1069 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1070 | void ThreadBase::dumpBase_l(int fd, const Vector<String16>& /* args */) | 
| Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 1071 | { | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1072 |     dprintf(fd, "  I/O handle: %d\n", mId); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1073 |     dprintf(fd, "  Standby: %s\n", mStandby ? "yes" : "no"); | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 1074 |     dprintf(fd, "  Sample rate: %u Hz\n", mSampleRate); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1075 |     dprintf(fd, "  HAL frame count: %zu\n", mFrameCount); | 
| Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 1076 |     dprintf(fd, "  HAL format: 0x%x (%s)\n", mHALFormat, | 
 | 1077 |             IAfThreadBase::formatToString(mHALFormat).c_str()); | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1078 |     dprintf(fd, "  HAL buffer size: %zu bytes\n", mBufferSize); | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 1079 |     dprintf(fd, "  Channel count: %u\n", mChannelCount); | 
 | 1080 |     dprintf(fd, "  Channel mask: 0x%08x (%s)\n", mChannelMask, | 
| Tomasz Wasilczyk | 5b05437 | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 1081 |             channelMaskToString(mChannelMask, mType != RECORD).c_str()); | 
| Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 1082 |     dprintf(fd, "  Processing format: 0x%x (%s)\n", mFormat, | 
 | 1083 |             IAfThreadBase::formatToString(mFormat).c_str()); | 
| Glenn Kasten | f87c2f5 | 2015-08-21 08:03:57 -0700 | [diff] [blame] | 1084 |     dprintf(fd, "  Processing frame size: %zu bytes\n", mFrameSize); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1085 |     dprintf(fd, "  Pending config events:"); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1086 |     size_t numConfig = mConfigEvents.size(); | 
 | 1087 |     if (numConfig) { | 
| Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 1088 |         const size_t SIZE = 256; | 
 | 1089 |         char buffer[SIZE]; | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1090 |         for (size_t i = 0; i < numConfig; i++) { | 
 | 1091 |             mConfigEvents[i]->dump(buffer, SIZE); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1092 |             dprintf(fd, "\n    %s", buffer); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1093 |         } | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1094 |         dprintf(fd, "\n"); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1095 |     } else { | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1096 |         dprintf(fd, " none\n"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1097 |     } | 
| Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 1098 |     // Note: output device may be used by capture threads for effects such as AEC. | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 1099 |     dprintf(fd, "  Output devices: %s (%s)\n", | 
| Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1100 |             dumpDeviceTypes(outDeviceTypes_l()).c_str(), toString(outDeviceTypes_l()).c_str()); | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 1101 |     dprintf(fd, "  Input device: %#x (%s)\n", | 
| Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1102 |             inDeviceType_l(), toString(inDeviceType_l()).c_str()); | 
| Andy Hung | 9b18195 | 2019-02-25 14:53:36 -0800 | [diff] [blame] | 1103 |     dprintf(fd, "  Audio source: %d (%s)\n", mAudioSource, toString(mAudioSource).c_str()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1104 |  | 
| Andy Hung | 2e2c0bb | 2018-06-11 19:13:11 -0700 | [diff] [blame] | 1105 |     // Dump timestamp statistics for the Thread types that support it. | 
 | 1106 |     if (mType == RECORD | 
 | 1107 |             || mType == MIXER | 
 | 1108 |             || mType == DUPLICATING | 
| Andy Hung | f323451 | 2018-07-03 14:51:47 -0700 | [diff] [blame] | 1109 |             || mType == DIRECT | 
| Andy Hung | 4b7f54f | 2022-04-28 17:45:28 -0700 | [diff] [blame] | 1110 |             || mType == OFFLOAD | 
 | 1111 |             || mType == SPATIALIZER) { | 
| Andy Hung | 2e2c0bb | 2018-06-11 19:13:11 -0700 | [diff] [blame] | 1112 |         dprintf(fd, "  Timestamp stats: %s\n", mTimestampVerifier.toString().c_str()); | 
| Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1113 |         dprintf(fd, "  Timestamp corrected: %s\n", | 
 | 1114 |                 isTimestampCorrectionEnabled_l() ? "yes" : "no"); | 
| Andy Hung | 2e2c0bb | 2018-06-11 19:13:11 -0700 | [diff] [blame] | 1115 |     } | 
 | 1116 |  | 
| Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 1117 |     if (mLastIoBeginNs > 0) { // MMAP may not set this | 
 | 1118 |         dprintf(fd, "  Last %s occurred (msecs): %lld\n", | 
 | 1119 |                 isOutput() ? "write" : "read", | 
 | 1120 |                 (long long) (systemTime() - mLastIoBeginNs) / NANOS_PER_MILLISECOND); | 
 | 1121 |     } | 
 | 1122 |  | 
 | 1123 |     if (mProcessTimeMs.getN() > 0) { | 
 | 1124 |         dprintf(fd, "  Process time ms stats: %s\n", mProcessTimeMs.toString().c_str()); | 
 | 1125 |     } | 
 | 1126 |  | 
 | 1127 |     if (mIoJitterMs.getN() > 0) { | 
 | 1128 |         dprintf(fd, "  Hal %s jitter ms stats: %s\n", | 
 | 1129 |                 isOutput() ? "write" : "read", | 
 | 1130 |                 mIoJitterMs.toString().c_str()); | 
 | 1131 |     } | 
 | 1132 |  | 
| Andy Hung | e6c3711 | 2019-02-26 17:38:10 -0800 | [diff] [blame] | 1133 |     if (mLatencyMs.getN() > 0) { | 
 | 1134 |         dprintf(fd, "  Threadloop %s latency stats: %s\n", | 
 | 1135 |                 isOutput() ? "write" : "read", | 
 | 1136 |                 mLatencyMs.toString().c_str()); | 
 | 1137 |     } | 
| Robert Wu | 06db0a3 | 2021-08-10 19:05:34 +0000 | [diff] [blame] | 1138 |  | 
 | 1139 |     if (mMonopipePipeDepthStats.getN() > 0) { | 
 | 1140 |         dprintf(fd, "  Monopipe %s pipe depth stats: %s\n", | 
 | 1141 |             isOutput() ? "write" : "read", | 
 | 1142 |             mMonopipePipeDepthStats.toString().c_str()); | 
 | 1143 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1144 | } | 
 | 1145 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1146 | void ThreadBase::dumpEffectChains_l(int fd, const Vector<String16>& args) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1147 | { | 
 | 1148 |     const size_t SIZE = 256; | 
 | 1149 |     char buffer[SIZE]; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1150 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1151 |     size_t numEffectChains = mEffectChains.size(); | 
| Narayan Kamath | 1d6fa7a | 2014-02-11 13:47:53 +0000 | [diff] [blame] | 1152 |     snprintf(buffer, SIZE, "  %zu Effect Chains\n", numEffectChains); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1153 |     write(fd, buffer, strlen(buffer)); | 
 | 1154 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1155 |     for (size_t i = 0; i < numEffectChains; ++i) { | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1156 |         sp<IAfEffectChain> chain = mEffectChains[i]; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1157 |         if (chain != 0) { | 
 | 1158 |             chain->dump(fd, args); | 
 | 1159 |         } | 
 | 1160 |     } | 
 | 1161 | } | 
 | 1162 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1163 | void ThreadBase::acquireWakeLock() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1164 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1165 |     audio_utils::lock_guard _l(mutex()); | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1166 |     acquireWakeLock_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1167 | } | 
 | 1168 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1169 | String16 ThreadBase::getWakeLockTag() | 
| Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 1170 | { | 
 | 1171 |     switch (mType) { | 
| Glenn Kasten | bcb1486 | 2015-03-05 17:11:21 -0800 | [diff] [blame] | 1172 |     case MIXER: | 
 | 1173 |         return String16("AudioMix"); | 
 | 1174 |     case DIRECT: | 
 | 1175 |         return String16("AudioDirectOut"); | 
 | 1176 |     case DUPLICATING: | 
 | 1177 |         return String16("AudioDup"); | 
 | 1178 |     case RECORD: | 
 | 1179 |         return String16("AudioIn"); | 
 | 1180 |     case OFFLOAD: | 
 | 1181 |         return String16("AudioOffload"); | 
| Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 1182 |     case MMAP_PLAYBACK: | 
 | 1183 |         return String16("MmapPlayback"); | 
 | 1184 |     case MMAP_CAPTURE: | 
 | 1185 |         return String16("MmapCapture"); | 
| Eric Laurent | 1c5e2e3 | 2021-08-18 18:50:28 +0200 | [diff] [blame] | 1186 |     case SPATIALIZER: | 
 | 1187 |         return String16("AudioSpatial"); | 
| Glenn Kasten | bcb1486 | 2015-03-05 17:11:21 -0800 | [diff] [blame] | 1188 |     default: | 
 | 1189 |         ALOG_ASSERT(false); | 
 | 1190 |         return String16("AudioUnknown"); | 
| Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 1191 |     } | 
 | 1192 | } | 
 | 1193 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1194 | void ThreadBase::acquireWakeLock_l() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1195 | { | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1196 |     getPowerManager_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1197 |     if (mPowerManager != 0) { | 
 | 1198 |         sp<IBinder> binder = new BBinder(); | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1199 |         // Uses AID_AUDIOSERVER for wakelock.  updateWakeLockUids_l() updates with client uids. | 
| Chris Ye | 6597d73 | 2020-02-28 22:38:25 -0800 | [diff] [blame] | 1200 |         binder::Status status = mPowerManager->acquireWakeLockAsync(binder, | 
 | 1201 |                     POWERMANAGER_PARTIAL_WAKE_LOCK, | 
| Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 1202 |                     getWakeLockTag(), | 
| Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 1203 |                     String16("audioserver"), | 
| Chris Ye | 6597d73 | 2020-02-28 22:38:25 -0800 | [diff] [blame] | 1204 |                     {} /* workSource */, | 
 | 1205 |                     {} /* historyTag */); | 
 | 1206 |         if (status.isOk()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1207 |             mWakeLockToken = binder; | 
 | 1208 |         } | 
| Chris Ye | 6597d73 | 2020-02-28 22:38:25 -0800 | [diff] [blame] | 1209 |         ALOGV("acquireWakeLock_l() %s status %d", mThreadName, status.exceptionCode()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1210 |     } | 
| Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 1211 |  | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 1212 |     gBoottime.acquire(mWakeLockToken); | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1213 |     mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME] = | 
 | 1214 |             gBoottime.getBoottimeOffset(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1215 | } | 
 | 1216 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1217 | void ThreadBase::releaseWakeLock() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1218 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1219 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1220 |     releaseWakeLock_l(); | 
 | 1221 | } | 
 | 1222 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1223 | void ThreadBase::releaseWakeLock_l() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1224 | { | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 1225 |     gBoottime.release(mWakeLockToken); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1226 |     if (mWakeLockToken != 0) { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1227 |         ALOGV("releaseWakeLock_l() %s", mThreadName); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1228 |         if (mPowerManager != 0) { | 
| Chris Ye | 6597d73 | 2020-02-28 22:38:25 -0800 | [diff] [blame] | 1229 |             mPowerManager->releaseWakeLockAsync(mWakeLockToken, 0); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1230 |         } | 
 | 1231 |         mWakeLockToken.clear(); | 
 | 1232 |     } | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1233 | } | 
 | 1234 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1235 | void ThreadBase::getPowerManager_l() { | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1236 |     if (mSystemReady && mPowerManager == 0) { | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1237 |         // use checkService() to avoid blocking if power service is not up yet | 
 | 1238 |         sp<IBinder> binder = | 
 | 1239 |             defaultServiceManager()->checkService(String16("power")); | 
 | 1240 |         if (binder == 0) { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1241 |             ALOGW("Thread %s cannot connect to the power manager service", mThreadName); | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1242 |         } else { | 
| Chris Ye | 6597d73 | 2020-02-28 22:38:25 -0800 | [diff] [blame] | 1243 |             mPowerManager = interface_cast<os::IPowerManager>(binder); | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1244 |             binder->linkToDeath(mDeathRecipient); | 
 | 1245 |         } | 
 | 1246 |     } | 
 | 1247 | } | 
 | 1248 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1249 | void ThreadBase::updateWakeLockUids_l(const SortedVector<uid_t>& uids) { | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1250 |     getPowerManager_l(); | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1251 |  | 
 | 1252 | #if !LOG_NDEBUG | 
 | 1253 |     std::stringstream s; | 
| Andy Hung | d01b0f1 | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 1254 |     for (uid_t uid : uids) { | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1255 |         s << uid << " "; | 
 | 1256 |     } | 
 | 1257 |     ALOGD("updateWakeLockUids_l %s uids:%s", mThreadName, s.str().c_str()); | 
 | 1258 | #endif | 
 | 1259 |  | 
| Andy Hung | 438e757 | 2015-12-14 15:51:17 -0800 | [diff] [blame] | 1260 |     if (mWakeLockToken == NULL) { // token may be NULL if AudioFlinger::systemReady() not called. | 
 | 1261 |         if (mSystemReady) { | 
 | 1262 |             ALOGE("no wake lock to update, but system ready!"); | 
 | 1263 |         } else { | 
 | 1264 |             ALOGW("no wake lock to update, system not ready yet"); | 
 | 1265 |         } | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1266 |         return; | 
 | 1267 |     } | 
 | 1268 |     if (mPowerManager != 0) { | 
| Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 1269 |         std::vector<int> uidsAsInt(uids.begin(), uids.end()); // powermanager expects uids as ints | 
| Chris Ye | 6597d73 | 2020-02-28 22:38:25 -0800 | [diff] [blame] | 1270 |         binder::Status status = mPowerManager->updateWakeLockUidsAsync( | 
 | 1271 |                 mWakeLockToken, uidsAsInt); | 
 | 1272 |         ALOGV("updateWakeLockUids_l() %s status %d", mThreadName, status.exceptionCode()); | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1273 |     } | 
 | 1274 | } | 
 | 1275 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1276 | void ThreadBase::clearPowerManager() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1277 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1278 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1279 |     releaseWakeLock_l(); | 
 | 1280 |     mPowerManager.clear(); | 
 | 1281 | } | 
 | 1282 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1283 | void ThreadBase::updateOutDevices( | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 1284 |         const DeviceDescriptorBaseVector& outDevices __unused) | 
 | 1285 | { | 
 | 1286 |     ALOGE("%s should only be called in RecordThread", __func__); | 
 | 1287 | } | 
 | 1288 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1289 | void ThreadBase::resizeInputBuffer_l(int32_t /* maxSharedAudioHistoryMs */) | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 1290 | { | 
 | 1291 |     ALOGE("%s should only be called in RecordThread", __func__); | 
 | 1292 | } | 
 | 1293 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1294 | void ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& /* who */) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1295 | { | 
 | 1296 |     sp<ThreadBase> thread = mThread.promote(); | 
 | 1297 |     if (thread != 0) { | 
 | 1298 |         thread->clearPowerManager(); | 
 | 1299 |     } | 
 | 1300 |     ALOGW("power manager service died !!!"); | 
 | 1301 | } | 
 | 1302 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1303 | void ThreadBase::setEffectSuspended_l( | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1304 |         const effect_uuid_t *type, bool suspend, audio_session_t sessionId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1305 | { | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1306 |     sp<IAfEffectChain> chain = getEffectChain_l(sessionId); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1307 |     if (chain != 0) { | 
 | 1308 |         if (type != NULL) { | 
 | 1309 |             chain->setEffectSuspended_l(type, suspend); | 
 | 1310 |         } else { | 
 | 1311 |             chain->setEffectSuspendedAll_l(suspend); | 
 | 1312 |         } | 
 | 1313 |     } | 
 | 1314 |  | 
 | 1315 |     updateSuspendedSessions_l(type, suspend, sessionId); | 
 | 1316 | } | 
 | 1317 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1318 | void ThreadBase::checkSuspendOnAddEffectChain_l(const sp<IAfEffectChain>& chain) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1319 | { | 
 | 1320 |     ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId()); | 
 | 1321 |     if (index < 0) { | 
 | 1322 |         return; | 
 | 1323 |     } | 
 | 1324 |  | 
 | 1325 |     const KeyedVector <int, sp<SuspendedSessionDesc> >& sessionEffects = | 
 | 1326 |             mSuspendedSessions.valueAt(index); | 
 | 1327 |  | 
 | 1328 |     for (size_t i = 0; i < sessionEffects.size(); i++) { | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1329 |         const sp<SuspendedSessionDesc>& desc = sessionEffects.valueAt(i); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1330 |         for (int j = 0; j < desc->mRefCount; j++) { | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1331 |             if (sessionEffects.keyAt(i) == IAfEffectChain::kKeyForSuspendAll) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1332 |                 chain->setEffectSuspendedAll_l(true); | 
 | 1333 |             } else { | 
 | 1334 |                 ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", | 
 | 1335 |                     desc->mType.timeLow); | 
 | 1336 |                 chain->setEffectSuspended_l(&desc->mType, true); | 
 | 1337 |             } | 
 | 1338 |         } | 
 | 1339 |     } | 
 | 1340 | } | 
 | 1341 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1342 | void ThreadBase::updateSuspendedSessions_l(const effect_uuid_t* type, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1343 |                                                          bool suspend, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1344 |                                                          audio_session_t sessionId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1345 | { | 
 | 1346 |     ssize_t index = mSuspendedSessions.indexOfKey(sessionId); | 
 | 1347 |  | 
 | 1348 |     KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; | 
 | 1349 |  | 
 | 1350 |     if (suspend) { | 
 | 1351 |         if (index >= 0) { | 
 | 1352 |             sessionEffects = mSuspendedSessions.valueAt(index); | 
 | 1353 |         } else { | 
 | 1354 |             mSuspendedSessions.add(sessionId, sessionEffects); | 
 | 1355 |         } | 
 | 1356 |     } else { | 
 | 1357 |         if (index < 0) { | 
 | 1358 |             return; | 
 | 1359 |         } | 
 | 1360 |         sessionEffects = mSuspendedSessions.valueAt(index); | 
 | 1361 |     } | 
 | 1362 |  | 
 | 1363 |  | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1364 |     int key = IAfEffectChain::kKeyForSuspendAll; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1365 |     if (type != NULL) { | 
 | 1366 |         key = type->timeLow; | 
 | 1367 |     } | 
 | 1368 |     index = sessionEffects.indexOfKey(key); | 
 | 1369 |  | 
 | 1370 |     sp<SuspendedSessionDesc> desc; | 
 | 1371 |     if (suspend) { | 
 | 1372 |         if (index >= 0) { | 
 | 1373 |             desc = sessionEffects.valueAt(index); | 
 | 1374 |         } else { | 
 | 1375 |             desc = new SuspendedSessionDesc(); | 
 | 1376 |             if (type != NULL) { | 
 | 1377 |                 desc->mType = *type; | 
 | 1378 |             } | 
 | 1379 |             sessionEffects.add(key, desc); | 
 | 1380 |             ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); | 
 | 1381 |         } | 
 | 1382 |         desc->mRefCount++; | 
 | 1383 |     } else { | 
 | 1384 |         if (index < 0) { | 
 | 1385 |             return; | 
 | 1386 |         } | 
 | 1387 |         desc = sessionEffects.valueAt(index); | 
 | 1388 |         if (--desc->mRefCount == 0) { | 
 | 1389 |             ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); | 
 | 1390 |             sessionEffects.removeItemsAt(index); | 
 | 1391 |             if (sessionEffects.isEmpty()) { | 
 | 1392 |                 ALOGV("updateSuspendedSessions_l() restore removing session %d", | 
 | 1393 |                                  sessionId); | 
 | 1394 |                 mSuspendedSessions.removeItem(sessionId); | 
 | 1395 |             } | 
 | 1396 |         } | 
 | 1397 |     } | 
 | 1398 |     if (!sessionEffects.isEmpty()) { | 
 | 1399 |         mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); | 
 | 1400 |     } | 
 | 1401 | } | 
 | 1402 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1403 | void ThreadBase::checkSuspendOnEffectEnabled(bool enabled, | 
| Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1404 |                                                            audio_session_t sessionId, | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 1405 |                                                            bool threadLocked) | 
 | 1406 | NO_THREAD_SAFETY_ANALYSIS  // manual locking | 
 | 1407 | { | 
| Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1408 |     if (!threadLocked) { | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1409 |         mutex().lock(); | 
| Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1410 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1411 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1412 |     if (mType != RECORD) { | 
 | 1413 |         // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on | 
 | 1414 |         // another session. This gives the priority to well behaved effect control panels | 
 | 1415 |         // and applications not using global effects. | 
 | 1416 |         // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect | 
 | 1417 |         // global effects | 
| Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 1418 |         if (!audio_is_global_session(sessionId)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1419 |             setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); | 
 | 1420 |         } | 
 | 1421 |     } | 
 | 1422 |  | 
| Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1423 |     if (!threadLocked) { | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1424 |         mutex().unlock(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1425 |     } | 
 | 1426 | } | 
 | 1427 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1428 | // checkEffectCompatibility_l() must be called with ThreadBase::mutex() held | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1429 | status_t RecordThread::checkEffectCompatibility_l( | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1430 |         const effect_descriptor_t *desc, audio_session_t sessionId) | 
 | 1431 | { | 
| Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 1432 |     // No global output effect sessions on record threads | 
 | 1433 |     if (sessionId == AUDIO_SESSION_OUTPUT_MIX | 
 | 1434 |             || sessionId == AUDIO_SESSION_OUTPUT_STAGE) { | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1435 |         ALOGW("checkEffectCompatibility_l(): global effect %s on record thread %s", | 
 | 1436 |                 desc->name, mThreadName); | 
 | 1437 |         return BAD_VALUE; | 
 | 1438 |     } | 
 | 1439 |     // only pre processing effects on record thread | 
 | 1440 |     if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC) { | 
 | 1441 |         ALOGW("checkEffectCompatibility_l(): non pre processing effect %s on record thread %s", | 
 | 1442 |                 desc->name, mThreadName); | 
 | 1443 |         return BAD_VALUE; | 
 | 1444 |     } | 
| Eric Laurent | 6dd0fd9 | 2016-09-15 12:44:53 -0700 | [diff] [blame] | 1445 |  | 
 | 1446 |     // always allow effects without processing load or latency | 
 | 1447 |     if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) == EFFECT_FLAG_NO_PROCESS) { | 
 | 1448 |         return NO_ERROR; | 
 | 1449 |     } | 
 | 1450 |  | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1451 |     audio_input_flags_t flags = mInput->flags; | 
 | 1452 |     if (hasFastCapture() || (flags & AUDIO_INPUT_FLAG_FAST)) { | 
 | 1453 |         if (flags & AUDIO_INPUT_FLAG_RAW) { | 
 | 1454 |             ALOGW("checkEffectCompatibility_l(): effect %s on record thread %s in raw mode", | 
 | 1455 |                   desc->name, mThreadName); | 
 | 1456 |             return BAD_VALUE; | 
 | 1457 |         } | 
 | 1458 |         if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { | 
 | 1459 |             ALOGW("checkEffectCompatibility_l(): non HW effect %s on record thread %s in fast mode", | 
 | 1460 |                   desc->name, mThreadName); | 
 | 1461 |             return BAD_VALUE; | 
 | 1462 |         } | 
 | 1463 |     } | 
| jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 1464 |  | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1465 |     if (IAfEffectModule::isHapticGenerator(&desc->type)) { | 
| jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 1466 |         ALOGE("%s(): HapticGenerator is not supported in RecordThread", __func__); | 
 | 1467 |         return BAD_VALUE; | 
 | 1468 |     } | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1469 |     return NO_ERROR; | 
 | 1470 | } | 
 | 1471 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1472 | // checkEffectCompatibility_l() must be called with ThreadBase::mutex() held | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1473 | status_t PlaybackThread::checkEffectCompatibility_l( | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1474 |         const effect_descriptor_t *desc, audio_session_t sessionId) | 
 | 1475 | { | 
 | 1476 |     // no preprocessing on playback threads | 
 | 1477 |     if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC) { | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1478 |         ALOGW("%s: pre processing effect %s created on playback" | 
 | 1479 |                 " thread %s", __func__, desc->name, mThreadName); | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1480 |         return BAD_VALUE; | 
 | 1481 |     } | 
 | 1482 |  | 
| Eric Laurent | 3e4de77 | 2017-07-16 16:55:08 -0700 | [diff] [blame] | 1483 |     // always allow effects without processing load or latency | 
 | 1484 |     if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) == EFFECT_FLAG_NO_PROCESS) { | 
 | 1485 |         return NO_ERROR; | 
 | 1486 |     } | 
 | 1487 |  | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1488 |     if (IAfEffectModule::isHapticGenerator(&desc->type) && mHapticChannelCount == 0) { | 
| jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 1489 |         ALOGW("%s: thread doesn't support haptic playback while the effect is HapticGenerator", | 
 | 1490 |                 __func__); | 
 | 1491 |         return BAD_VALUE; | 
 | 1492 |     } | 
 | 1493 |  | 
| Eric Laurent | 4eb45d0 | 2023-12-20 12:07:17 +0100 | [diff] [blame] | 1494 |     if (IAfEffectModule::isSpatializer(&desc->type) | 
| Eric Laurent | f690c46 | 2021-09-17 14:47:03 +0200 | [diff] [blame] | 1495 |             && mType != SPATIALIZER) { | 
 | 1496 |         ALOGW("%s: attempt to create a spatializer effect on a thread of type %d", | 
 | 1497 |                 __func__, mType); | 
 | 1498 |         return BAD_VALUE; | 
 | 1499 |     } | 
 | 1500 |  | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1501 |     switch (mType) { | 
 | 1502 |     case MIXER: { | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1503 |         audio_output_flags_t flags = mOutput->flags; | 
 | 1504 |         if (hasFastMixer() || (flags & AUDIO_OUTPUT_FLAG_FAST)) { | 
 | 1505 |             if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { | 
 | 1506 |                 // global effects are applied only to non fast tracks if they are SW | 
 | 1507 |                 if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { | 
 | 1508 |                     break; | 
 | 1509 |                 } | 
 | 1510 |             } else if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) { | 
 | 1511 |                 // only post processing on output stage session | 
 | 1512 |                 if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) { | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1513 |                     ALOGW("%s: non post processing effect %s not allowed on output stage session", | 
 | 1514 |                             __func__, desc->name); | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1515 |                     return BAD_VALUE; | 
 | 1516 |                 } | 
| Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 1517 |             } else if (sessionId == AUDIO_SESSION_DEVICE) { | 
 | 1518 |                 // only post processing on output stage session | 
 | 1519 |                 if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) { | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1520 |                     ALOGW("%s: non post processing effect %s not allowed on device session", | 
 | 1521 |                             __func__, desc->name); | 
| Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 1522 |                     return BAD_VALUE; | 
 | 1523 |                 } | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1524 |             } else { | 
 | 1525 |                 // no restriction on effects applied on non fast tracks | 
 | 1526 |                 if ((hasAudioSession_l(sessionId) & ThreadBase::FAST_SESSION) == 0) { | 
 | 1527 |                     break; | 
 | 1528 |                 } | 
 | 1529 |             } | 
| Eric Laurent | 6dd0fd9 | 2016-09-15 12:44:53 -0700 | [diff] [blame] | 1530 |  | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1531 |             if (flags & AUDIO_OUTPUT_FLAG_RAW) { | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1532 |                 ALOGW("%s: effect %s on playback thread in raw mode", __func__, desc->name); | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1533 |                 return BAD_VALUE; | 
 | 1534 |             } | 
 | 1535 |             if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1536 |                 ALOGW("%s: non HW effect %s on playback thread in fast mode", | 
 | 1537 |                         __func__, desc->name); | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1538 |                 return BAD_VALUE; | 
 | 1539 |             } | 
 | 1540 |         } | 
 | 1541 |     } break; | 
 | 1542 |     case OFFLOAD: | 
| Jean-Michel Trivi | 773ee95 | 2016-07-11 16:53:18 -0700 | [diff] [blame] | 1543 |         // nothing actionable on offload threads, if the effect: | 
 | 1544 |         //   - is offloadable: the effect can be created | 
 | 1545 |         //   - is NOT offloadable: the effect should still be created, but EffectHandle::enable() | 
 | 1546 |         //     will take care of invalidating the tracks of the thread | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1547 |         break; | 
 | 1548 |     case DIRECT: | 
 | 1549 |         // Reject any effect on Direct output threads for now, since the format of | 
 | 1550 |         // mSinkBuffer is not guaranteed to be compatible with effect processing (PCM 16 stereo). | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1551 |         ALOGW("%s: effect %s on DIRECT output thread %s", | 
 | 1552 |                 __func__, desc->name, mThreadName); | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1553 |         return BAD_VALUE; | 
 | 1554 |     case DUPLICATING: | 
| Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 1555 |         if (audio_is_global_session(sessionId)) { | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1556 |             ALOGW("%s: global effect %s on DUPLICATING thread %s", | 
 | 1557 |                     __func__, desc->name, mThreadName); | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1558 |             return BAD_VALUE; | 
 | 1559 |         } | 
 | 1560 |         if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) { | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1561 |             ALOGW("%s: post processing effect %s on DUPLICATING thread %s", | 
 | 1562 |                 __func__, desc->name, mThreadName); | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1563 |             return BAD_VALUE; | 
 | 1564 |         } | 
 | 1565 |         if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) != 0) { | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1566 |             ALOGW("%s: HW tunneled effect %s on DUPLICATING thread %s", | 
 | 1567 |                     __func__, desc->name, mThreadName); | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1568 |             return BAD_VALUE; | 
 | 1569 |         } | 
 | 1570 |         break; | 
| Eric Laurent | 1c5e2e3 | 2021-08-18 18:50:28 +0200 | [diff] [blame] | 1571 |     case SPATIALIZER: | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1572 |         // Global effects (AUDIO_SESSION_OUTPUT_MIX) are not supported on spatializer mixer | 
 | 1573 |         // as there is no common accumulation buffer for sptialized and non sptialized tracks. | 
 | 1574 |         // Post processing effects (AUDIO_SESSION_OUTPUT_STAGE or AUDIO_SESSION_DEVICE) | 
 | 1575 |         // are supported and added after the spatializer. | 
 | 1576 |         if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { | 
 | 1577 |             ALOGW("%s: global effect %s not supported on spatializer thread %s", | 
 | 1578 |                     __func__, desc->name, mThreadName); | 
| Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 1579 |             return BAD_VALUE; | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1580 |         } else if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) { | 
 | 1581 |             // only post processing , downmixer or spatializer effects on output stage session | 
| Eric Laurent | 4eb45d0 | 2023-12-20 12:07:17 +0100 | [diff] [blame] | 1582 |             if (IAfEffectModule::isSpatializer(&desc->type) | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1583 |                     || memcmp(&desc->type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0) { | 
 | 1584 |                 break; | 
 | 1585 |             } | 
 | 1586 |             if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) { | 
 | 1587 |                 ALOGW("%s: non post processing effect %s not allowed on output stage session", | 
 | 1588 |                         __func__, desc->name); | 
 | 1589 |                 return BAD_VALUE; | 
 | 1590 |             } | 
 | 1591 |         } else if (sessionId == AUDIO_SESSION_DEVICE) { | 
 | 1592 |             // only post processing on output stage session | 
 | 1593 |             if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) { | 
 | 1594 |                 ALOGW("%s: non post processing effect %s not allowed on device session", | 
 | 1595 |                         __func__, desc->name); | 
 | 1596 |                 return BAD_VALUE; | 
 | 1597 |             } | 
| Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 1598 |         } | 
 | 1599 |         break; | 
| jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 1600 |     case BIT_PERFECT: | 
 | 1601 |         if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) != 0) { | 
 | 1602 |             // Allow HW accelerated effects of tunnel type | 
 | 1603 |             break; | 
 | 1604 |         } | 
 | 1605 |         // As bit-perfect tracks will not be allowed to apply audio effect that will touch the audio | 
 | 1606 |         // data, effects will not be allowed on 1) global effects (AUDIO_SESSION_OUTPUT_MIX), | 
 | 1607 |         // 2) post-processing effects (AUDIO_SESSION_OUTPUT_STAGE or AUDIO_SESSION_DEVICE) and | 
 | 1608 |         // 3) there is any bit-perfect track with the given session id. | 
 | 1609 |         if (sessionId == AUDIO_SESSION_OUTPUT_MIX || sessionId == AUDIO_SESSION_OUTPUT_STAGE || | 
 | 1610 |             sessionId == AUDIO_SESSION_DEVICE) { | 
 | 1611 |             ALOGW("%s: effect %s not supported on bit-perfect thread %s", | 
 | 1612 |                   __func__, desc->name, mThreadName); | 
 | 1613 |             return BAD_VALUE; | 
 | 1614 |         } else if ((hasAudioSession_l(sessionId) & ThreadBase::BIT_PERFECT_SESSION) != 0) { | 
 | 1615 |             ALOGW("%s: effect %s not supported as there is a bit-perfect track with session as %d", | 
 | 1616 |                   __func__, desc->name, sessionId); | 
 | 1617 |             return BAD_VALUE; | 
 | 1618 |         } | 
 | 1619 |         break; | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1620 |     default: | 
 | 1621 |         LOG_ALWAYS_FATAL("checkEffectCompatibility_l(): wrong thread type %d", mType); | 
 | 1622 |     } | 
 | 1623 |  | 
 | 1624 |     return NO_ERROR; | 
 | 1625 | } | 
 | 1626 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1627 | // ThreadBase::createEffect_l() must be called with AudioFlinger::mutex() held | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1628 | sp<IAfEffectHandle> ThreadBase::createEffect_l( | 
| Andy Hung | 88035ac | 2023-06-27 17:05:02 -0700 | [diff] [blame] | 1629 |         const sp<Client>& client, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1630 |         const sp<IEffectClient>& effectClient, | 
 | 1631 |         int32_t priority, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1632 |         audio_session_t sessionId, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1633 |         effect_descriptor_t *desc, | 
 | 1634 |         int *enabled, | 
| Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1635 |         status_t *status, | 
| Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 1636 |         bool pinned, | 
| Eric Laurent | de8caf4 | 2021-08-11 17:19:25 +0200 | [diff] [blame] | 1637 |         bool probe, | 
 | 1638 |         bool notifyFramesProcessed) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1639 | { | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1640 |     sp<IAfEffectModule> effect; | 
 | 1641 |     sp<IAfEffectHandle> handle; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1642 |     status_t lStatus; | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1643 |     sp<IAfEffectChain> chain; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1644 |     bool chainCreated = false; | 
 | 1645 |     bool effectCreated = false; | 
| Mikhail Naganov | 2247f7b | 2017-01-13 11:52:54 -0800 | [diff] [blame] | 1646 |     audio_unique_id_t effectId = AUDIO_UNIQUE_ID_USE_UNSPECIFIED; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1647 |  | 
 | 1648 |     lStatus = initCheck(); | 
 | 1649 |     if (lStatus != NO_ERROR) { | 
 | 1650 |         ALOGW("createEffect_l() Audio driver not initialized."); | 
 | 1651 |         goto Exit; | 
 | 1652 |     } | 
 | 1653 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1654 |     ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId); | 
 | 1655 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1656 |     { // scope for mutex() | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1657 |         audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1658 |  | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1659 |         lStatus = checkEffectCompatibility_l(desc, sessionId); | 
| Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 1660 |         if (probe || lStatus != NO_ERROR) { | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1661 |             goto Exit; | 
 | 1662 |         } | 
 | 1663 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1664 |         // check for existing effect chain with the requested audio session | 
 | 1665 |         chain = getEffectChain_l(sessionId); | 
 | 1666 |         if (chain == 0) { | 
 | 1667 |             // create a new chain for this session | 
 | 1668 |             ALOGV("createEffect_l() new effect chain for session %d", sessionId); | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1669 |             chain = IAfEffectChain::create(this, sessionId); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1670 |             addEffectChain_l(chain); | 
 | 1671 |             chain->setStrategy(getStrategyForSession_l(sessionId)); | 
 | 1672 |             chainCreated = true; | 
 | 1673 |         } else { | 
 | 1674 |             effect = chain->getEffectFromDesc_l(desc); | 
 | 1675 |         } | 
 | 1676 |  | 
 | 1677 |         ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get()); | 
 | 1678 |  | 
 | 1679 |         if (effect == 0) { | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 1680 |             effectId = mAfThreadCallback->nextUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1681 |             // create a new effect module if none present in the chain | 
| Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1682 |             lStatus = chain->createEffect_l(effect, desc, effectId, sessionId, pinned); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1683 |             if (lStatus != NO_ERROR) { | 
 | 1684 |                 goto Exit; | 
 | 1685 |             } | 
 | 1686 |             effectCreated = true; | 
 | 1687 |  | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 1688 |             // FIXME: use vector of device and address when effect interface is ready. | 
| jiabin | 8f278ee | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 1689 |             effect->setDevices(outDeviceTypeAddrs()); | 
 | 1690 |             effect->setInputDevice(inDeviceTypeAddr()); | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 1691 |             effect->setMode(mAfThreadCallback->getMode()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1692 |             effect->setAudioSource(mAudioSource); | 
 | 1693 |         } | 
| jiabin | 1319f5a | 2021-03-30 22:21:24 +0000 | [diff] [blame] | 1694 |         if (effect->isHapticGenerator()) { | 
 | 1695 |             // TODO(b/184194057): Use the vibrator information from the vibrator that will be used | 
 | 1696 |             // for the HapticGenerator. | 
| Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 1697 |             const std::optional<media::AudioVibratorInfo> defaultVibratorInfo = | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 1698 |                     std::move(mAfThreadCallback->getDefaultVibratorInfo_l()); | 
| Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 1699 |             if (defaultVibratorInfo) { | 
| jiabin | 1319f5a | 2021-03-30 22:21:24 +0000 | [diff] [blame] | 1700 |                 // Only set the vibrator info when it is a valid one. | 
| Shunkai Yao | d125e40 | 2024-01-20 03:19:06 +0000 | [diff] [blame] | 1701 |                 effect->setVibratorInfo_l(*defaultVibratorInfo); | 
| jiabin | 1319f5a | 2021-03-30 22:21:24 +0000 | [diff] [blame] | 1702 |             } | 
 | 1703 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1704 |         // create effect handle and connect it to effect module | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1705 |         handle = IAfEffectHandle::create( | 
 | 1706 |                 effect, client, effectClient, priority, notifyFramesProcessed); | 
| Glenn Kasten | e75da40 | 2013-11-20 13:54:52 -0800 | [diff] [blame] | 1707 |         lStatus = handle->initCheck(); | 
 | 1708 |         if (lStatus == OK) { | 
 | 1709 |             lStatus = effect->addHandle(handle.get()); | 
| Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 1710 |             sendCheckOutputStageEffectsEvent_l(); | 
| Glenn Kasten | e75da40 | 2013-11-20 13:54:52 -0800 | [diff] [blame] | 1711 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1712 |         if (enabled != NULL) { | 
 | 1713 |             *enabled = (int)effect->isEnabled(); | 
 | 1714 |         } | 
 | 1715 |     } | 
 | 1716 |  | 
 | 1717 | Exit: | 
| Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 1718 |     if (!probe && lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1719 |         audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1720 |         if (effectCreated) { | 
 | 1721 |             chain->removeEffect_l(effect); | 
 | 1722 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1723 |         if (chainCreated) { | 
 | 1724 |             removeEffectChain_l(chain); | 
 | 1725 |         } | 
| haobo10173529 | 5ff7b0d | 2017-03-17 17:44:03 +0800 | [diff] [blame] | 1726 |         // handle must be cleared by caller to avoid deadlock. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1727 |     } | 
 | 1728 |  | 
| Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1729 |     *status = lStatus; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1730 |     return handle; | 
 | 1731 | } | 
 | 1732 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1733 | void ThreadBase::disconnectEffectHandle(IAfEffectHandle* handle, | 
| Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1734 |                                                       bool unpinIfLast) | 
 | 1735 | { | 
 | 1736 |     bool remove = false; | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1737 |     sp<IAfEffectModule> effect; | 
| Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1738 |     { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1739 |         audio_utils::lock_guard _l(mutex()); | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1740 |         sp<IAfEffectBase> effectBase = handle->effect().promote(); | 
| Eric Laurent | 4170955 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 1741 |         if (effectBase == nullptr) { | 
| Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1742 |             return; | 
 | 1743 |         } | 
| Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 1744 |         effect = effectBase->asEffectModule(); | 
 | 1745 |         if (effect == nullptr) { | 
 | 1746 |             return; | 
 | 1747 |         } | 
| Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1748 |         // restore suspended effects if the disconnected handle was enabled and the last one. | 
 | 1749 |         remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast); | 
 | 1750 |         if (remove) { | 
 | 1751 |             removeEffect_l(effect, true); | 
 | 1752 |         } | 
| Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 1753 |         sendCheckOutputStageEffectsEvent_l(); | 
| Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1754 |     } | 
 | 1755 |     if (remove) { | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 1756 |         mAfThreadCallback->updateOrphanEffectChains(effect); | 
| Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1757 |         if (handle->enabled()) { | 
| Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1758 |             effect->checkSuspendOnEffectEnabled(false, false /*threadLocked*/); | 
| Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1759 |         } | 
 | 1760 |     } | 
 | 1761 | } | 
 | 1762 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1763 | void ThreadBase::onEffectEnable(const sp<IAfEffectModule>& effect) { | 
| Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 1764 |     if (isOffloadOrMmap()) { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1765 |         audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1766 |         broadcast_l(); | 
 | 1767 |     } | 
 | 1768 |     if (!effect->isOffloadable()) { | 
 | 1769 |         if (mType == ThreadBase::OFFLOAD) { | 
 | 1770 |             PlaybackThread *t = (PlaybackThread *)this; | 
 | 1771 |             t->invalidateTracks(AUDIO_STREAM_MUSIC); | 
 | 1772 |         } | 
 | 1773 |         if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) { | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 1774 |             mAfThreadCallback->onNonOffloadableGlobalEffectEnable(); | 
| Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1775 |         } | 
 | 1776 |     } | 
 | 1777 | } | 
 | 1778 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1779 | void ThreadBase::onEffectDisable() { | 
| Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 1780 |     if (isOffloadOrMmap()) { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1781 |         audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1782 |         broadcast_l(); | 
 | 1783 |     } | 
 | 1784 | } | 
 | 1785 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1786 | sp<IAfEffectModule> ThreadBase::getEffect(audio_session_t sessionId, | 
| Andy Hung | 3e4c874 | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1787 |         int effectId) const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1788 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1789 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1790 |     return getEffect_l(sessionId, effectId); | 
 | 1791 | } | 
 | 1792 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1793 | sp<IAfEffectModule> ThreadBase::getEffect_l(audio_session_t sessionId, | 
| Andy Hung | 3e4c874 | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1794 |         int effectId) const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1795 | { | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1796 |     sp<IAfEffectChain> chain = getEffectChain_l(sessionId); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1797 |     return chain != 0 ? chain->getEffectFromId_l(effectId) : 0; | 
 | 1798 | } | 
 | 1799 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1800 | std::vector<int> ThreadBase::getEffectIds_l(audio_session_t sessionId) const | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 1801 | { | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1802 |     sp<IAfEffectChain> chain = getEffectChain_l(sessionId); | 
| Shunkai Yao | d125e40 | 2024-01-20 03:19:06 +0000 | [diff] [blame] | 1803 |     return chain != nullptr ? chain->getEffectIds_l() : std::vector<int>{}; | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 1804 | } | 
 | 1805 |  | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1806 | // PlaybackThread::addEffect_ll() must be called with AudioFlinger::mutex() and | 
 | 1807 | // ThreadBase::mutex() held | 
 | 1808 | status_t ThreadBase::addEffect_ll(const sp<IAfEffectModule>& effect) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1809 | { | 
 | 1810 |     // check for existing effect chain with the requested audio session | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1811 |     audio_session_t sessionId = effect->sessionId(); | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1812 |     sp<IAfEffectChain> chain = getEffectChain_l(sessionId); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1813 |     bool chainCreated = false; | 
 | 1814 |  | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1815 |     ALOGD_IF((mType == OFFLOAD) && !effect->isOffloadable(), | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1816 |              "%s: on offloaded thread %p: effect %s does not support offload flags %#x", | 
 | 1817 |              __func__, this, effect->desc().name, effect->desc().flags); | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1818 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1819 |     if (chain == 0) { | 
 | 1820 |         // create a new chain for this session | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1821 |         ALOGV("%s: new effect chain for session %d", __func__, sessionId); | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1822 |         chain = IAfEffectChain::create(this, sessionId); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1823 |         addEffectChain_l(chain); | 
 | 1824 |         chain->setStrategy(getStrategyForSession_l(sessionId)); | 
 | 1825 |         chainCreated = true; | 
 | 1826 |     } | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1827 |     ALOGV("%s: %p chain %p effect %p", __func__, this, chain.get(), effect.get()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1828 |  | 
 | 1829 |     if (chain->getEffectFromId_l(effect->id()) != 0) { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1830 |         ALOGW("%s: %p effect %s already present in chain %p", | 
 | 1831 |                 __func__, this, effect->desc().name, chain.get()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1832 |         return BAD_VALUE; | 
 | 1833 |     } | 
 | 1834 |  | 
| Shunkai Yao | d125e40 | 2024-01-20 03:19:06 +0000 | [diff] [blame] | 1835 |     effect->setOffloaded_l(mType == OFFLOAD, mId); | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1836 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1837 |     status_t status = chain->addEffect_l(effect); | 
 | 1838 |     if (status != NO_ERROR) { | 
 | 1839 |         if (chainCreated) { | 
 | 1840 |             removeEffectChain_l(chain); | 
 | 1841 |         } | 
 | 1842 |         return status; | 
 | 1843 |     } | 
 | 1844 |  | 
| jiabin | 8f278ee | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 1845 |     effect->setDevices(outDeviceTypeAddrs()); | 
 | 1846 |     effect->setInputDevice(inDeviceTypeAddr()); | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 1847 |     effect->setMode(mAfThreadCallback->getMode()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1848 |     effect->setAudioSource(mAudioSource); | 
| Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 1849 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1850 |     return NO_ERROR; | 
 | 1851 | } | 
 | 1852 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1853 | void ThreadBase::removeEffect_l(const sp<IAfEffectModule>& effect, bool release) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1854 |  | 
| Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1855 |     ALOGV("%s %p effect %p", __FUNCTION__, this, effect.get()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1856 |     effect_descriptor_t desc = effect->desc(); | 
 | 1857 |     if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
 | 1858 |         detachAuxEffect_l(effect->id()); | 
 | 1859 |     } | 
 | 1860 |  | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1861 |     sp<IAfEffectChain> chain = effect->getCallback()->chain().promote(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1862 |     if (chain != 0) { | 
 | 1863 |         // remove effect chain if removing last effect | 
| Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1864 |         if (chain->removeEffect_l(effect, release) == 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1865 |             removeEffectChain_l(chain); | 
 | 1866 |         } | 
 | 1867 |     } else { | 
 | 1868 |         ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); | 
 | 1869 |     } | 
 | 1870 | } | 
 | 1871 |  | 
| Shunkai Yao | f484765 | 2024-01-12 00:25:20 +0000 | [diff] [blame] | 1872 | void ThreadBase::lockEffectChains_l(Vector<sp<IAfEffectChain>>& effectChains) | 
 | 1873 |         NO_THREAD_SAFETY_ANALYSIS  // calls EffectChain::lock() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1874 | { | 
 | 1875 |     effectChains = mEffectChains; | 
| Shunkai Yao | f484765 | 2024-01-12 00:25:20 +0000 | [diff] [blame] | 1876 |     for (const auto& effectChain : effectChains) { | 
 | 1877 |         effectChain->mutex().lock(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1878 |     } | 
 | 1879 | } | 
 | 1880 |  | 
| Shunkai Yao | f484765 | 2024-01-12 00:25:20 +0000 | [diff] [blame] | 1881 | void ThreadBase::unlockEffectChains(const Vector<sp<IAfEffectChain>>& effectChains) | 
 | 1882 |         NO_THREAD_SAFETY_ANALYSIS  // calls EffectChain::unlock() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1883 | { | 
| Shunkai Yao | f484765 | 2024-01-12 00:25:20 +0000 | [diff] [blame] | 1884 |     for (const auto& effectChain : effectChains) { | 
 | 1885 |         effectChain->mutex().unlock(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1886 |     } | 
 | 1887 | } | 
 | 1888 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1889 | sp<IAfEffectChain> ThreadBase::getEffectChain(audio_session_t sessionId) const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1890 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1891 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1892 |     return getEffectChain_l(sessionId); | 
 | 1893 | } | 
 | 1894 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1895 | sp<IAfEffectChain> ThreadBase::getEffectChain_l(audio_session_t sessionId) | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1896 |         const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1897 | { | 
 | 1898 |     size_t size = mEffectChains.size(); | 
 | 1899 |     for (size_t i = 0; i < size; i++) { | 
 | 1900 |         if (mEffectChains[i]->sessionId() == sessionId) { | 
 | 1901 |             return mEffectChains[i]; | 
 | 1902 |         } | 
 | 1903 |     } | 
 | 1904 |     return 0; | 
 | 1905 | } | 
 | 1906 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1907 | void ThreadBase::setMode(audio_mode_t mode) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1908 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1909 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1910 |     size_t size = mEffectChains.size(); | 
 | 1911 |     for (size_t i = 0; i < size; i++) { | 
 | 1912 |         mEffectChains[i]->setMode_l(mode); | 
 | 1913 |     } | 
 | 1914 | } | 
 | 1915 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1916 | void ThreadBase::toAudioPortConfig(struct audio_port_config* config) | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1917 | { | 
 | 1918 |     config->type = AUDIO_PORT_TYPE_MIX; | 
 | 1919 |     config->ext.mix.handle = mId; | 
 | 1920 |     config->sample_rate = mSampleRate; | 
| Mikhail Naganov | 88d54da | 2023-09-11 11:53:50 -0700 | [diff] [blame] | 1921 |     config->format = mHALFormat; | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1922 |     config->channel_mask = mChannelMask; | 
 | 1923 |     config->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK| | 
 | 1924 |                             AUDIO_PORT_CONFIG_FORMAT; | 
 | 1925 | } | 
 | 1926 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1927 | void ThreadBase::systemReady() | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1928 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1929 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1930 |     if (mSystemReady) { | 
 | 1931 |         return; | 
 | 1932 |     } | 
 | 1933 |     mSystemReady = true; | 
 | 1934 |  | 
 | 1935 |     for (size_t i = 0; i < mPendingConfigEvents.size(); i++) { | 
 | 1936 |         sendConfigEvent_l(mPendingConfigEvents.editItemAt(i)); | 
 | 1937 |     } | 
 | 1938 |     mPendingConfigEvents.clear(); | 
 | 1939 | } | 
 | 1940 |  | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1941 | template <typename T> | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1942 | ssize_t ThreadBase::ActiveTracks<T>::add(const sp<T>& track) { | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1943 |     ssize_t index = mActiveTracks.indexOf(track); | 
 | 1944 |     if (index >= 0) { | 
 | 1945 |         ALOGW("ActiveTracks<T>::add track %p already there", track.get()); | 
 | 1946 |         return index; | 
 | 1947 |     } | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1948 |     logTrack("add", track); | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1949 |     mActiveTracksGeneration++; | 
 | 1950 |     mLatestActiveTrack = track; | 
| Atneya Nair | 166663a | 2023-06-27 19:16:24 -0700 | [diff] [blame] | 1951 |     track->beginBatteryAttribution(); | 
| Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 1952 |     mHasChanged = true; | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1953 |     return mActiveTracks.add(track); | 
 | 1954 | } | 
 | 1955 |  | 
 | 1956 | template <typename T> | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1957 | ssize_t ThreadBase::ActiveTracks<T>::remove(const sp<T>& track) { | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1958 |     ssize_t index = mActiveTracks.remove(track); | 
 | 1959 |     if (index < 0) { | 
 | 1960 |         ALOGW("ActiveTracks<T>::remove nonexistent track %p", track.get()); | 
 | 1961 |         return index; | 
 | 1962 |     } | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1963 |     logTrack("remove", track); | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1964 |     mActiveTracksGeneration++; | 
| Atneya Nair | 166663a | 2023-06-27 19:16:24 -0700 | [diff] [blame] | 1965 |     track->endBatteryAttribution(); | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1966 |     // mLatestActiveTrack is not cleared even if is the same as track. | 
| Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 1967 |     mHasChanged = true; | 
| Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 1968 | #ifdef TEE_SINK | 
 | 1969 |     track->dumpTee(-1 /* fd */, "_REMOVE"); | 
 | 1970 | #endif | 
| Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 1971 |     track->logEndInterval(); // log to MediaMetrics | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1972 |     return index; | 
 | 1973 | } | 
 | 1974 |  | 
 | 1975 | template <typename T> | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1976 | void ThreadBase::ActiveTracks<T>::clear() { | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1977 |     for (const sp<T> &track : mActiveTracks) { | 
| Atneya Nair | 166663a | 2023-06-27 19:16:24 -0700 | [diff] [blame] | 1978 |         track->endBatteryAttribution(); | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1979 |         logTrack("clear", track); | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1980 |     } | 
 | 1981 |     mLastActiveTracksGeneration = mActiveTracksGeneration; | 
| Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 1982 |     if (!mActiveTracks.empty()) { mHasChanged = true; } | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1983 |     mActiveTracks.clear(); | 
 | 1984 |     mLatestActiveTrack.clear(); | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1985 | } | 
 | 1986 |  | 
 | 1987 | template <typename T> | 
| Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1988 | void ThreadBase::ActiveTracks<T>::updatePowerState_l( | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 1989 |         const sp<ThreadBase>& thread, bool force) { | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1990 |     // Updates ActiveTracks client uids to the thread wakelock. | 
 | 1991 |     if (mActiveTracksGeneration != mLastActiveTracksGeneration || force) { | 
 | 1992 |         thread->updateWakeLockUids_l(getWakeLockUids()); | 
 | 1993 |         mLastActiveTracksGeneration = mActiveTracksGeneration; | 
 | 1994 |     } | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1995 | } | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1996 |  | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1997 | template <typename T> | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1998 | bool ThreadBase::ActiveTracks<T>::readAndClearHasChanged() { | 
| Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 1999 |     bool hasChanged = mHasChanged; | 
| Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 2000 |     mHasChanged = false; | 
| Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 2001 |  | 
 | 2002 |     for (const sp<T> &track : mActiveTracks) { | 
 | 2003 |         // Do not short-circuit as all hasChanged states must be reset | 
 | 2004 |         // as all the metadata are going to be sent | 
 | 2005 |         hasChanged |= track->readAndClearHasChanged(); | 
 | 2006 |     } | 
| Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 2007 |     return hasChanged; | 
 | 2008 | } | 
 | 2009 |  | 
 | 2010 | template <typename T> | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2011 | void ThreadBase::ActiveTracks<T>::logTrack( | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2012 |         const char *funcName, const sp<T> &track) const { | 
 | 2013 |     if (mLocalLog != nullptr) { | 
 | 2014 |         String8 result; | 
 | 2015 |         track->appendDump(result, false /* active */); | 
| Tomasz Wasilczyk | 5b05437 | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 2016 |         mLocalLog->log("AT::%-10s(%p) %s", funcName, track.get(), result.c_str()); | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2017 |     } | 
 | 2018 | } | 
 | 2019 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2020 | void ThreadBase::broadcast_l() | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2021 | { | 
 | 2022 |     // Thread could be blocked waiting for async | 
 | 2023 |     // so signal it to handle state changes immediately | 
 | 2024 |     // If threadLoop is currently unlocked a signal of mWaitWorkCV will | 
 | 2025 |     // be lost so we also flag to prevent it blocking on mWaitWorkCV | 
 | 2026 |     mSignalPending = true; | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2027 |     mWaitWorkCV.notify_all(); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2028 | } | 
 | 2029 |  | 
| Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 2030 | // Call only from threadLoop() or when it is idle. | 
 | 2031 | // Do not call from high performance code as this may do binder rpc to the MediaMetrics service. | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2032 | void ThreadBase::sendStatistics(bool force) | 
| Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2033 | NO_THREAD_SAFETY_ANALYSIS | 
| Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 2034 | { | 
 | 2035 |     // Do not log if we have no stats. | 
 | 2036 |     // We choose the timestamp verifier because it is the most likely item to be present. | 
 | 2037 |     const int64_t nstats = mTimestampVerifier.getN() - mLastRecordedTimestampVerifierN; | 
 | 2038 |     if (nstats == 0) { | 
 | 2039 |         return; | 
 | 2040 |     } | 
 | 2041 |  | 
 | 2042 |     // Don't log more frequently than once per 12 hours. | 
 | 2043 |     // We use BOOTTIME to include suspend time. | 
 | 2044 |     const int64_t timeNs = systemTime(SYSTEM_TIME_BOOTTIME); | 
 | 2045 |     const int64_t sinceNs = timeNs - mLastRecordedTimeNs; // ok if mLastRecordedTimeNs = 0 | 
 | 2046 |     if (!force && sinceNs <= 12 * NANOS_PER_HOUR) { | 
 | 2047 |         return; | 
 | 2048 |     } | 
 | 2049 |  | 
 | 2050 |     mLastRecordedTimestampVerifierN = mTimestampVerifier.getN(); | 
 | 2051 |     mLastRecordedTimeNs = timeNs; | 
 | 2052 |  | 
| Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 2053 |     std::unique_ptr<mediametrics::Item> item(mediametrics::Item::create("audiothread")); | 
| Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 2054 |  | 
 | 2055 | #define MM_PREFIX "android.media.audiothread." // avoid cut-n-paste errors. | 
 | 2056 |  | 
 | 2057 |     // thread configuration | 
 | 2058 |     item->setInt32(MM_PREFIX "id", (int32_t)mId); // IO handle | 
 | 2059 |     // item->setInt32(MM_PREFIX "portId", (int32_t)mPortId); | 
 | 2060 |     item->setCString(MM_PREFIX "type", threadTypeToString(mType)); | 
 | 2061 |     item->setInt32(MM_PREFIX "sampleRate", (int32_t)mSampleRate); | 
 | 2062 |     item->setInt64(MM_PREFIX "channelMask", (int64_t)mChannelMask); | 
 | 2063 |     item->setCString(MM_PREFIX "encoding", toString(mFormat).c_str()); | 
 | 2064 |     item->setInt32(MM_PREFIX "frameCount", (int32_t)mFrameCount); | 
| Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2065 |     item->setCString(MM_PREFIX "outDevice", toString(outDeviceTypes_l()).c_str()); | 
 | 2066 |     item->setCString(MM_PREFIX "inDevice", toString(inDeviceType_l()).c_str()); | 
| Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 2067 |  | 
 | 2068 |     // thread statistics | 
 | 2069 |     if (mIoJitterMs.getN() > 0) { | 
 | 2070 |         item->setDouble(MM_PREFIX "ioJitterMs.mean", mIoJitterMs.getMean()); | 
 | 2071 |         item->setDouble(MM_PREFIX "ioJitterMs.std", mIoJitterMs.getStdDev()); | 
 | 2072 |     } | 
 | 2073 |     if (mProcessTimeMs.getN() > 0) { | 
 | 2074 |         item->setDouble(MM_PREFIX "processTimeMs.mean", mProcessTimeMs.getMean()); | 
 | 2075 |         item->setDouble(MM_PREFIX "processTimeMs.std", mProcessTimeMs.getStdDev()); | 
 | 2076 |     } | 
 | 2077 |     const auto tsjitter = mTimestampVerifier.getJitterMs(); | 
 | 2078 |     if (tsjitter.getN() > 0) { | 
 | 2079 |         item->setDouble(MM_PREFIX "timestampJitterMs.mean", tsjitter.getMean()); | 
 | 2080 |         item->setDouble(MM_PREFIX "timestampJitterMs.std", tsjitter.getStdDev()); | 
 | 2081 |     } | 
 | 2082 |     if (mLatencyMs.getN() > 0) { | 
 | 2083 |         item->setDouble(MM_PREFIX "latencyMs.mean", mLatencyMs.getMean()); | 
 | 2084 |         item->setDouble(MM_PREFIX "latencyMs.std", mLatencyMs.getStdDev()); | 
 | 2085 |     } | 
| Robert Wu | 06db0a3 | 2021-08-10 19:05:34 +0000 | [diff] [blame] | 2086 |     if (mMonopipePipeDepthStats.getN() > 0) { | 
 | 2087 |         item->setDouble(MM_PREFIX "monopipePipeDepthStats.mean", | 
 | 2088 |                         mMonopipePipeDepthStats.getMean()); | 
 | 2089 |         item->setDouble(MM_PREFIX "monopipePipeDepthStats.std", | 
 | 2090 |                         mMonopipePipeDepthStats.getStdDev()); | 
 | 2091 |     } | 
| Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 2092 |  | 
 | 2093 |     item->selfrecord(); | 
 | 2094 | } | 
 | 2095 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2096 | product_strategy_t ThreadBase::getStrategyForStream(audio_stream_type_t stream) const | 
| Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 2097 | { | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 2098 |     if (!mAfThreadCallback->isAudioPolicyReady()) { | 
| Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 2099 |         return PRODUCT_STRATEGY_NONE; | 
 | 2100 |     } | 
 | 2101 |     return AudioSystem::getStrategyForStream(stream); | 
 | 2102 | } | 
 | 2103 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2104 | // startMelComputation_l() must be called with AudioFlinger::mutex() held | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2105 | void ThreadBase::startMelComputation_l( | 
| Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 2106 |         const sp<audio_utils::MelProcessor>& /*processor*/) | 
 | 2107 | { | 
 | 2108 |     // Do nothing | 
 | 2109 |     ALOGW("%s: ThreadBase does not support CSD", __func__); | 
 | 2110 | } | 
 | 2111 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2112 | // stopMelComputation_l() must be called with AudioFlinger::mutex() held | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2113 | void ThreadBase::stopMelComputation_l() | 
| Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 2114 | { | 
 | 2115 |     // Do nothing | 
 | 2116 |     ALOGW("%s: ThreadBase does not support CSD", __func__); | 
 | 2117 | } | 
 | 2118 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2119 | // ---------------------------------------------------------------------------- | 
 | 2120 | //      Playback | 
 | 2121 | // ---------------------------------------------------------------------------- | 
 | 2122 |  | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 2123 | PlaybackThread::PlaybackThread(const sp<IAfThreadCallback>& afThreadCallback, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2124 |                                              AudioStreamOut* output, | 
 | 2125 |                                              audio_io_handle_t id, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 2126 |                                              type_t type, | 
| Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 2127 |                                              bool systemReady, | 
 | 2128 |                                              audio_config_base_t *mixerConfig) | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 2129 |     :   ThreadBase(afThreadCallback, id, type, systemReady, true /* isOut */), | 
| Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 2130 |         mNormalFrameCount(0), mSinkBuffer(NULL), | 
| Andy Hung | d21a2ab | 2023-07-20 21:44:14 -0700 | [diff] [blame] | 2131 |         mMixerBufferEnabled(kEnableExtendedPrecision || type == SPATIALIZER), | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2132 |         mMixerBuffer(NULL), | 
 | 2133 |         mMixerBufferSize(0), | 
 | 2134 |         mMixerBufferFormat(AUDIO_FORMAT_INVALID), | 
 | 2135 |         mMixerBufferValid(false), | 
| Andy Hung | d21a2ab | 2023-07-20 21:44:14 -0700 | [diff] [blame] | 2136 |         mEffectBufferEnabled(kEnableExtendedPrecision || type == SPATIALIZER), | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2137 |         mEffectBuffer(NULL), | 
 | 2138 |         mEffectBufferSize(0), | 
 | 2139 |         mEffectBufferFormat(AUDIO_FORMAT_INVALID), | 
 | 2140 |         mEffectBufferValid(false), | 
| Glenn Kasten | c1fac19 | 2013-08-06 07:41:36 -0700 | [diff] [blame] | 2141 |         mSuspended(0), mBytesWritten(0), | 
| Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 2142 |         mFramesWritten(0), | 
| Andy Hung | 238fa3d | 2016-07-28 10:53:22 -0700 | [diff] [blame] | 2143 |         mSuspendedFrames(0), | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2144 |         mActiveTracks(&this->mLocalLog), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2145 |         // mStreamTypes[] initialized in constructor body | 
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2146 |         mTracks(type == MIXER), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2147 |         mOutput(output), | 
| Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 2148 |         mNumWrites(0), mNumDelayedWrites(0), mInWrite(false), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2149 |         mMixerStatus(MIXER_IDLE), | 
 | 2150 |         mMixerStatusIgnoringFastTracks(MIXER_IDLE), | 
| Andy Hung | d58c473 | 2023-07-20 21:31:38 -0700 | [diff] [blame] | 2151 |         mStandbyDelayNs(getStandbyTimeInNanos()), | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2152 |         mBytesRemaining(0), | 
 | 2153 |         mCurrentWriteLength(0), | 
 | 2154 |         mUseAsyncWrite(false), | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2155 |         mWriteAckSequence(0), | 
 | 2156 |         mDrainSequence(0), | 
| Andy Hung | 1b6d46a | 2023-07-19 16:22:58 -0700 | [diff] [blame] | 2157 |         mScreenState(mAfThreadCallback->getScreenState()), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2158 |         // index 0 is reserved for normal mixer's submix | 
| Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 2159 |         mFastTrackAvailMask(((1 << FastMixerState::sMaxFastTracks) - 1) & ~1), | 
| Eric Laurent | 7c29ec9 | 2017-09-20 17:54:22 -0700 | [diff] [blame] | 2160 |         mHwSupportsPause(false), mHwPaused(false), mFlushPending(false), | 
| Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 2161 |         mLeftVolFloat(-1.0), mRightVolFloat(-1.0), | 
| Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 2162 |         mDownStreamPatch{}, | 
| Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 2163 |         mIsTimestampAdvancing(kMinimumTimeBetweenTimestampChecksNs) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2164 | { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 2165 |     snprintf(mThreadName, kThreadNameLength, "AudioOut_%X", id); | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 2166 |     mNBLogWriter = afThreadCallback->newWriter_l(kLogSize, mThreadName); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2167 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2168 |     // Assumes constructor is called by AudioFlinger with its mutex() held, but | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2169 |     // it would be safer to explicitly pass initial masterVolume/masterMute as | 
 | 2170 |     // parameter. | 
 | 2171 |     // | 
 | 2172 |     // If the HAL we are using has support for master volume or master mute, | 
 | 2173 |     // then do not attenuate or mute during mixing (just leave the volume at 1.0 | 
 | 2174 |     // and the mute set to false). | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 2175 |     mMasterVolume = afThreadCallback->masterVolume_l(); | 
 | 2176 |     mMasterMute = afThreadCallback->masterMute_l(); | 
| George Burgess IV | 5e4d86f | 2020-02-18 12:55:36 -0800 | [diff] [blame] | 2177 |     if (mOutput->audioHwDev) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2178 |         if (mOutput->audioHwDev->canSetMasterVolume()) { | 
 | 2179 |             mMasterVolume = 1.0; | 
 | 2180 |         } | 
 | 2181 |  | 
 | 2182 |         if (mOutput->audioHwDev->canSetMasterMute()) { | 
 | 2183 |             mMasterMute = false; | 
 | 2184 |         } | 
| Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 2185 |         mIsMsdDevice = strcmp( | 
 | 2186 |                 mOutput->audioHwDev->moduleName(), AUDIO_HARDWARE_MODULE_ID_MSD) == 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2187 |     } | 
 | 2188 |  | 
| Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 2189 |     if (mixerConfig != nullptr && mixerConfig->channel_mask != AUDIO_CHANNEL_NONE) { | 
 | 2190 |         mMixerChannelMask = mixerConfig->channel_mask; | 
 | 2191 |     } | 
 | 2192 |  | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 2193 |     readOutputParameters_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2194 |  | 
| Eric Laurent | 1c5e2e3 | 2021-08-18 18:50:28 +0200 | [diff] [blame] | 2195 |     if (mType != SPATIALIZER | 
| Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 2196 |             && mMixerChannelMask != mChannelMask) { | 
 | 2197 |         LOG_ALWAYS_FATAL("HAL channel mask %#x does not match mixer channel mask %#x", | 
 | 2198 |                 mChannelMask, mMixerChannelMask); | 
 | 2199 |     } | 
 | 2200 |  | 
| Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 2201 |     // TODO: We may also match on address as well as device type for | 
 | 2202 |     // AUDIO_DEVICE_OUT_BUS, AUDIO_DEVICE_OUT_ALL_A2DP, AUDIO_DEVICE_OUT_REMOTE_SUBMIX | 
| Dean Wheatley | f8726f0 | 2019-12-02 14:12:01 +1100 | [diff] [blame] | 2203 |     if (type == MIXER || type == DIRECT || type == OFFLOAD) { | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 2204 |         // TODO: This property should be ensure that only contains one single device type. | 
 | 2205 |         mTimestampCorrectedDevice = (audio_devices_t)property_get_int64( | 
 | 2206 |                 "audio.timestamp.corrected_output_device", | 
| Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 2207 |                 (int64_t)(mIsMsdDevice ? AUDIO_DEVICE_OUT_BUS // turn on by default for MSD | 
 | 2208 |                                        : AUDIO_DEVICE_NONE)); | 
 | 2209 |     } | 
 | 2210 |  | 
| Mikhail Naganov | f33115d | 2020-09-25 23:03:05 +0000 | [diff] [blame] | 2211 |     for (int i = AUDIO_STREAM_MIN; i < AUDIO_STREAM_FOR_POLICY_CNT; ++i) { | 
 | 2212 |         const audio_stream_type_t stream{static_cast<audio_stream_type_t>(i)}; | 
| Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 2213 |         mStreamTypes[stream].volume = 0.0f; | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 2214 |         mStreamTypes[stream].mute = mAfThreadCallback->streamMute_l(stream); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2215 |     } | 
| Eric Laurent | 35f8c7c | 2020-02-08 11:42:35 -0800 | [diff] [blame] | 2216 |     // Audio patch and call assistant volume are always max | 
| Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 2217 |     mStreamTypes[AUDIO_STREAM_PATCH].volume = 1.0f; | 
 | 2218 |     mStreamTypes[AUDIO_STREAM_PATCH].mute = false; | 
| Eric Laurent | 35f8c7c | 2020-02-08 11:42:35 -0800 | [diff] [blame] | 2219 |     mStreamTypes[AUDIO_STREAM_CALL_ASSISTANT].volume = 1.0f; | 
 | 2220 |     mStreamTypes[AUDIO_STREAM_CALL_ASSISTANT].mute = false; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2221 | } | 
 | 2222 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2223 | PlaybackThread::~PlaybackThread() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2224 | { | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 2225 |     mAfThreadCallback->unregisterWriter(mNBLogWriter); | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2226 |     free(mSinkBuffer); | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2227 |     free(mMixerBuffer); | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2228 |     free(mEffectBuffer); | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 2229 |     free(mPostSpatializerBuffer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2230 | } | 
 | 2231 |  | 
| Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 2232 | // Thread virtuals | 
 | 2233 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2234 | void PlaybackThread::onFirstRef() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2235 | { | 
| Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 2236 |     if (!isStreamInitialized()) { | 
| jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 2237 |         ALOGE("The stream is not open yet"); // This should not happen. | 
 | 2238 |     } else { | 
| Mikhail Naganov | aec565e | 2023-02-22 16:31:28 -0800 | [diff] [blame] | 2239 |         // Callbacks take strong or weak pointers as a parameter. | 
 | 2240 |         // Since PlaybackThread passes itself as a callback handler, it can only | 
 | 2241 |         // be done outside of the constructor. Creating weak and especially strong | 
 | 2242 |         // pointers to a refcounted object in its own constructor is strongly | 
 | 2243 |         // discouraged, see comments in system/core/libutils/include/utils/RefBase.h. | 
 | 2244 |         // Even if a function takes a weak pointer, it is possible that it will | 
 | 2245 |         // need to convert it to a strong pointer down the line. | 
 | 2246 |         if (mOutput->flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING && | 
 | 2247 |                 mOutput->stream->setCallback(this) == OK) { | 
 | 2248 |             mUseAsyncWrite = true; | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2249 |             mCallbackThread = sp<AsyncCallbackThread>::make(this); | 
| Mikhail Naganov | aec565e | 2023-02-22 16:31:28 -0800 | [diff] [blame] | 2250 |         } | 
 | 2251 |  | 
| jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 2252 |         if (mOutput->stream->setEventCallback(this) != OK) { | 
| jiabin | f1a3605 | 2020-08-19 14:33:31 -0700 | [diff] [blame] | 2253 |             ALOGD("Failed to add event callback"); | 
| jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 2254 |         } | 
 | 2255 |     } | 
| Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 2256 |     run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO); | 
| Andy Hung | 44d648b | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 2257 |     mThreadSnapshot.setTid(getTid()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2258 | } | 
 | 2259 |  | 
| Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 2260 | // ThreadBase virtuals | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2261 | void PlaybackThread::preExit() | 
| Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 2262 | { | 
 | 2263 |     ALOGV("  preExit()"); | 
| Ytai Ben-Tsvi | 7e0183f | 2022-02-04 10:49:54 -0800 | [diff] [blame] | 2264 |     status_t result = mOutput->stream->exit(); | 
 | 2265 |     ALOGE_IF(result != OK, "Error when calling exit(): %d", result); | 
| Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 2266 | } | 
 | 2267 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2268 | void PlaybackThread::dumpTracks_l(int fd, const Vector<String16>& /* args */) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2269 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2270 |     String8 result; | 
 | 2271 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2272 |     result.appendFormat("  Stream volumes in dB: "); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2273 |     for (int i = 0; i < AUDIO_STREAM_CNT; ++i) { | 
 | 2274 |         const stream_type_t *st = &mStreamTypes[i]; | 
 | 2275 |         if (i > 0) { | 
 | 2276 |             result.appendFormat(", "); | 
 | 2277 |         } | 
 | 2278 |         result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume)); | 
 | 2279 |         if (st->mute) { | 
 | 2280 |             result.append("M"); | 
 | 2281 |         } | 
 | 2282 |     } | 
 | 2283 |     result.append("\n"); | 
| Tomasz Wasilczyk | 5b05437 | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 2284 |     write(fd, result.c_str(), result.length()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2285 |     result.clear(); | 
 | 2286 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2287 |     // These values are "raw"; they will wrap around.  See prepareTracks_l() for a better way. | 
 | 2288 |     FastTrackUnderruns underruns = getFastTrackUnderruns(0); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 2289 |     dprintf(fd, "  Normal mixer raw underrun counters: partial=%u empty=%u\n", | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2290 |             underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2291 |  | 
 | 2292 |     size_t numtracks = mTracks.size(); | 
 | 2293 |     size_t numactive = mActiveTracks.size(); | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2294 |     dprintf(fd, "  %zu Tracks", numtracks); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2295 |     size_t numactiveseen = 0; | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2296 |     const char *prefix = "    "; | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2297 |     if (numtracks) { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2298 |         dprintf(fd, " of which %zu are active\n", numactive); | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2299 |         result.append(prefix); | 
| Andy Hung | f6ab58d | 2018-05-25 12:50:39 -0700 | [diff] [blame] | 2300 |         mTracks[0]->appendDumpHeader(result); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2301 |         for (size_t i = 0; i < numtracks; ++i) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2302 |             sp<IAfTrack> track = mTracks[i]; | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2303 |             if (track != 0) { | 
 | 2304 |                 bool active = mActiveTracks.indexOf(track) >= 0; | 
 | 2305 |                 if (active) { | 
 | 2306 |                     numactiveseen++; | 
 | 2307 |                 } | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2308 |                 result.append(prefix); | 
 | 2309 |                 track->appendDump(result, active); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2310 |             } | 
 | 2311 |         } | 
 | 2312 |     } else { | 
 | 2313 |         result.append("\n"); | 
 | 2314 |     } | 
 | 2315 |     if (numactiveseen != numactive) { | 
 | 2316 |         // some tracks in the active list were not in the tracks list | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2317 |         result.append("  The following tracks are in the active list but" | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2318 |                 " not in the track list\n"); | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2319 |         result.append(prefix); | 
| Andy Hung | f6ab58d | 2018-05-25 12:50:39 -0700 | [diff] [blame] | 2320 |         mActiveTracks[0]->appendDumpHeader(result); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2321 |         for (size_t i = 0; i < numactive; ++i) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2322 |             sp<IAfTrack> track = mActiveTracks[i]; | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 2323 |             if (mTracks.indexOf(track) < 0) { | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2324 |                 result.append(prefix); | 
 | 2325 |                 track->appendDump(result, true /* active */); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2326 |             } | 
 | 2327 |         } | 
 | 2328 |     } | 
 | 2329 |  | 
| Tomasz Wasilczyk | 5b05437 | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 2330 |     write(fd, result.c_str(), result.size()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2331 | } | 
 | 2332 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2333 | void PlaybackThread::dumpInternals_l(int fd, const Vector<String16>& args) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2334 | { | 
| Andy Hung | 04cb8f7 | 2020-03-20 13:44:33 -0700 | [diff] [blame] | 2335 |     dprintf(fd, "  Master volume: %f\n", mMasterVolume); | 
| Mikhail Naganov | dfb411f | 2018-09-11 13:39:56 -0700 | [diff] [blame] | 2336 |     dprintf(fd, "  Master mute: %s\n", mMasterMute ? "on" : "off"); | 
| Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 2337 |     dprintf(fd, "  Mixer channel Mask: %#x (%s)\n", | 
 | 2338 |             mMixerChannelMask, channelMaskToString(mMixerChannelMask, true /* output */).c_str()); | 
| jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 2339 |     if (mHapticChannelMask != AUDIO_CHANNEL_NONE) { | 
 | 2340 |         dprintf(fd, "  Haptic channel mask: %#x (%s)\n", mHapticChannelMask, | 
 | 2341 |                 channelMaskToString(mHapticChannelMask, true /* output */).c_str()); | 
 | 2342 |     } | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 2343 |     dprintf(fd, "  Normal frame count: %zu\n", mNormalFrameCount); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 2344 |     dprintf(fd, "  Total writes: %d\n", mNumWrites); | 
 | 2345 |     dprintf(fd, "  Delayed writes: %d\n", mNumDelayedWrites); | 
 | 2346 |     dprintf(fd, "  Blocked in write: %s\n", mInWrite ? "yes" : "no"); | 
| Andy Hung | 160664b | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 2347 |     dprintf(fd, "  Suspend count: %d\n", (int32_t)mSuspended); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 2348 |     dprintf(fd, "  Fast track availMask=%#x\n", mFastTrackAvailMask); | 
| Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 2349 |     dprintf(fd, "  Standby delay ns=%lld\n", (long long)mStandbyDelayNs); | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 2350 |     AudioStreamOut *output = mOutput; | 
 | 2351 |     audio_output_flags_t flags = output != NULL ? output->flags : AUDIO_OUTPUT_FLAG_NONE; | 
| Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 2352 |     dprintf(fd, "  AudioStreamOut: %p flags %#x (%s)\n", | 
| Andy Hung | 9b18195 | 2019-02-25 14:53:36 -0800 | [diff] [blame] | 2353 |             output, flags, toString(flags).c_str()); | 
| Andy Hung | b54c854 | 2016-09-21 12:55:15 -0700 | [diff] [blame] | 2354 |     dprintf(fd, "  Frames written: %lld\n", (long long)mFramesWritten); | 
 | 2355 |     dprintf(fd, "  Suspended frames: %lld\n", (long long)mSuspendedFrames); | 
 | 2356 |     if (mPipeSink.get() != nullptr) { | 
 | 2357 |         dprintf(fd, "  PipeSink frames written: %lld\n", (long long)mPipeSink->framesWritten()); | 
 | 2358 |     } | 
 | 2359 |     if (output != nullptr) { | 
 | 2360 |         dprintf(fd, "  Hal stream dump:\n"); | 
| Andy Hung | 61589a4 | 2021-06-16 09:37:53 -0700 | [diff] [blame] | 2361 |         (void)output->stream->dump(fd, args); | 
| Andy Hung | b54c854 | 2016-09-21 12:55:15 -0700 | [diff] [blame] | 2362 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2363 | } | 
 | 2364 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2365 | // PlaybackThread::createTrack_l() must be called with AudioFlinger::mutex() held | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2366 | sp<IAfTrack> PlaybackThread::createTrack_l( | 
| Andy Hung | 88035ac | 2023-06-27 17:05:02 -0700 | [diff] [blame] | 2367 |         const sp<Client>& client, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2368 |         audio_stream_type_t streamType, | 
| Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 2369 |         const audio_attributes_t& attr, | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2370 |         uint32_t *pSampleRate, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2371 |         audio_format_t format, | 
 | 2372 |         audio_channel_mask_t channelMask, | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 2373 |         size_t *pFrameCount, | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2374 |         size_t *pNotificationFrameCount, | 
 | 2375 |         uint32_t notificationsPerBuffer, | 
 | 2376 |         float speed, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2377 |         const sp<IMemory>& sharedBuffer, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2378 |         audio_session_t sessionId, | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2379 |         audio_output_flags_t *flags, | 
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 2380 |         pid_t creatorPid, | 
| Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 2381 |         const AttributionSourceState& attributionSource, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2382 |         pid_t tid, | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2383 |         status_t *status, | 
| jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 2384 |         audio_port_handle_t portId, | 
| Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 2385 |         const sp<media::IAudioTrackCallback>& callback, | 
| jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 2386 |         bool isSpatialized, | 
| jiabin | 94ed47c | 2023-07-27 23:34:20 +0000 | [diff] [blame] | 2387 |         bool isBitPerfect, | 
 | 2388 |         audio_output_flags_t *afTrackFlags) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2389 | { | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 2390 |     size_t frameCount = *pFrameCount; | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2391 |     size_t notificationFrameCount = *pNotificationFrameCount; | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2392 |     sp<IAfTrack> track; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2393 |     status_t lStatus; | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2394 |     audio_output_flags_t outputFlags = mOutput->flags; | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2395 |     audio_output_flags_t requestedFlags = *flags; | 
| Eric Laurent | 9b11c02 | 2018-06-06 19:19:22 -0700 | [diff] [blame] | 2396 |     uint32_t sampleRate; | 
 | 2397 |  | 
 | 2398 |     if (sharedBuffer != 0 && checkIMemory(sharedBuffer) != NO_ERROR) { | 
 | 2399 |         lStatus = BAD_VALUE; | 
 | 2400 |         goto Exit; | 
 | 2401 |     } | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2402 |  | 
 | 2403 |     if (*pSampleRate == 0) { | 
 | 2404 |         *pSampleRate = mSampleRate; | 
 | 2405 |     } | 
| Eric Laurent | 9b11c02 | 2018-06-06 19:19:22 -0700 | [diff] [blame] | 2406 |     sampleRate = *pSampleRate; | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2407 |  | 
 | 2408 |     // special case for FAST flag considered OK if fast mixer is present | 
 | 2409 |     if (hasFastMixer()) { | 
 | 2410 |         outputFlags = (audio_output_flags_t)(outputFlags | AUDIO_OUTPUT_FLAG_FAST); | 
 | 2411 |     } | 
 | 2412 |  | 
 | 2413 |     // Check if requested flags are compatible with output stream flags | 
 | 2414 |     if ((*flags & outputFlags) != *flags) { | 
 | 2415 |         ALOGW("createTrack_l(): mismatch between requested flags (%08x) and output flags (%08x)", | 
 | 2416 |               *flags, outputFlags); | 
 | 2417 |         *flags = (audio_output_flags_t)(*flags & outputFlags); | 
 | 2418 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2419 |  | 
| jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 2420 |     if (isBitPerfect) { | 
| Andy Hung | 160664b | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 2421 |         audio_utils::lock_guard _l(mutex()); | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 2422 |         sp<IAfEffectChain> chain = getEffectChain_l(sessionId); | 
| jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 2423 |         if (chain.get() != nullptr) { | 
 | 2424 |             // Bit-perfect is required according to the configuration and preferred mixer | 
 | 2425 |             // attributes, but it is not in the output flag from the client's request. Explicitly | 
 | 2426 |             // adding bit-perfect flag to check the compatibility | 
 | 2427 |             audio_output_flags_t flagsToCheck = | 
 | 2428 |                     (audio_output_flags_t)(*flags & AUDIO_OUTPUT_FLAG_BIT_PERFECT); | 
 | 2429 |             chain->checkOutputFlagCompatibility(&flagsToCheck); | 
 | 2430 |             if ((flagsToCheck & AUDIO_OUTPUT_FLAG_BIT_PERFECT) == AUDIO_OUTPUT_FLAG_NONE) { | 
 | 2431 |                 ALOGE("%s cannot create track as there is data-processing effect attached to " | 
 | 2432 |                       "given session id(%d)", __func__, sessionId); | 
 | 2433 |                 lStatus = BAD_VALUE; | 
 | 2434 |                 goto Exit; | 
 | 2435 |             } | 
 | 2436 |             *flags = flagsToCheck; | 
 | 2437 |         } | 
 | 2438 |     } | 
 | 2439 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2440 |     // client expresses a preference for FAST, but we get the final say | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2441 |     if (*flags & AUDIO_OUTPUT_FLAG_FAST) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2442 |       if ( | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2443 |             // PCM data | 
 | 2444 |             audio_is_linear_pcm(format) && | 
| Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 2445 |             // TODO: extract as a data library function that checks that a computationally | 
 | 2446 |             // expensive downmixer is not required: isFastOutputChannelConversion() | 
| jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 2447 |             (channelMask == (mChannelMask | mHapticChannelMask) || | 
| Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 2448 |                     mChannelMask != AUDIO_CHANNEL_OUT_STEREO || | 
 | 2449 |                     (channelMask == AUDIO_CHANNEL_OUT_MONO | 
 | 2450 |                             /* && mChannelMask == AUDIO_CHANNEL_OUT_STEREO */)) && | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2451 |             // hardware sample rate | 
 | 2452 |             (sampleRate == mSampleRate) && | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2453 |             // normal mixer has an associated fast mixer | 
 | 2454 |             hasFastMixer() && | 
 | 2455 |             // there are sufficient fast track slots available | 
 | 2456 |             (mFastTrackAvailMask != 0) | 
 | 2457 |             // FIXME test that MixerThread for this fast track has a capable output HAL | 
 | 2458 |             // FIXME add a permission test also? | 
 | 2459 |         ) { | 
| Andy Hung | e0a269a | 2016-03-23 15:13:42 -0700 | [diff] [blame] | 2460 |         // static tracks can have any nonzero framecount, streaming tracks check against minimum. | 
 | 2461 |         if (sharedBuffer == 0) { | 
| Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 2462 |             // read the fast track multiplier property the first time it is needed | 
 | 2463 |             int ok = pthread_once(&sFastTrackMultiplierOnce, sFastTrackMultiplierInit); | 
 | 2464 |             if (ok != 0) { | 
 | 2465 |                 ALOGE("%s pthread_once failed: %d", __func__, ok); | 
 | 2466 |             } | 
| Andy Hung | e0a269a | 2016-03-23 15:13:42 -0700 | [diff] [blame] | 2467 |             frameCount = max(frameCount, mFrameCount * sFastTrackMultiplier); // incl framecount 0 | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2468 |         } | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2469 |  | 
 | 2470 |         // check compatibility with audio effects. | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2471 |         { // scope for mutex() | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2472 |             audio_utils::lock_guard _l(mutex()); | 
| Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame] | 2473 |             for (audio_session_t session : { | 
| Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 2474 |                     AUDIO_SESSION_DEVICE, | 
| Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame] | 2475 |                     AUDIO_SESSION_OUTPUT_STAGE, | 
 | 2476 |                     AUDIO_SESSION_OUTPUT_MIX, | 
 | 2477 |                     sessionId, | 
 | 2478 |                 }) { | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 2479 |                 sp<IAfEffectChain> chain = getEffectChain_l(session); | 
| Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame] | 2480 |                 if (chain.get() != nullptr) { | 
 | 2481 |                     audio_output_flags_t old = *flags; | 
 | 2482 |                     chain->checkOutputFlagCompatibility(flags); | 
 | 2483 |                     if (old != *flags) { | 
 | 2484 |                         ALOGV("AUDIO_OUTPUT_FLAGS denied by effect, session=%d old=%#x new=%#x", | 
 | 2485 |                                 (int)session, (int)old, (int)*flags); | 
 | 2486 |                     } | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2487 |                 } | 
 | 2488 |             } | 
 | 2489 |         } | 
| Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 2490 |         ALOGV_IF((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0, | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2491 |                  "AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu", | 
 | 2492 |                  frameCount, mFrameCount); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2493 |       } else { | 
| Robert Wu | 4c7bb7d | 2021-08-12 18:50:13 +0000 | [diff] [blame] | 2494 |         ALOGD("AUDIO_OUTPUT_FLAG_FAST denied: sharedBuffer=%p frameCount=%zu " | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2495 |                 "mFrameCount=%zu format=%#x mFormat=%#x isLinear=%d channelMask=%#x " | 
| Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 2496 |                 "sampleRate=%u mSampleRate=%u " | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2497 |                 "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x", | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 2498 |                 sharedBuffer.get(), frameCount, mFrameCount, format, mFormat, | 
| Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 2499 |                 audio_is_linear_pcm(format), channelMask, sampleRate, | 
 | 2500 |                 mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask); | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2501 |         *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST); | 
| Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 2502 |       } | 
 | 2503 |     } | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2504 |  | 
 | 2505 |     if (!audio_has_proportional_frames(format)) { | 
 | 2506 |         if (sharedBuffer != 0) { | 
 | 2507 |             // Same comment as below about ignoring frameCount parameter for set() | 
 | 2508 |             frameCount = sharedBuffer->size(); | 
 | 2509 |         } else if (frameCount == 0) { | 
 | 2510 |             frameCount = mNormalFrameCount; | 
 | 2511 |         } | 
 | 2512 |         if (notificationFrameCount != frameCount) { | 
 | 2513 |             notificationFrameCount = frameCount; | 
 | 2514 |         } | 
 | 2515 |     } else if (sharedBuffer != 0) { | 
 | 2516 |         // FIXME: Ensure client side memory buffers need | 
 | 2517 |         // not have additional alignment beyond sample | 
 | 2518 |         // (e.g. 16 bit stereo accessed as 32 bit frame). | 
 | 2519 |         size_t alignment = audio_bytes_per_sample(format); | 
 | 2520 |         if (alignment & 1) { | 
 | 2521 |             // for AUDIO_FORMAT_PCM_24_BIT_PACKED (not exposed through Java). | 
 | 2522 |             alignment = 1; | 
 | 2523 |         } | 
 | 2524 |         uint32_t channelCount = audio_channel_count_from_out_mask(channelMask); | 
 | 2525 |         size_t frameSize = channelCount * audio_bytes_per_sample(format); | 
 | 2526 |         if (channelCount > 1) { | 
 | 2527 |             // More than 2 channels does not require stronger alignment than stereo | 
 | 2528 |             alignment <<= 1; | 
 | 2529 |         } | 
| Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 2530 |         if (((uintptr_t)sharedBuffer->unsecurePointer() & (alignment - 1)) != 0) { | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2531 |             ALOGE("Invalid buffer alignment: address %p, channel count %u", | 
| Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 2532 |                   sharedBuffer->unsecurePointer(), channelCount); | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2533 |             lStatus = BAD_VALUE; | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2534 |             goto Exit; | 
 | 2535 |         } | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2536 |  | 
 | 2537 |         // When initializing a shared buffer AudioTrack via constructors, | 
 | 2538 |         // there's no frameCount parameter. | 
 | 2539 |         // But when initializing a shared buffer AudioTrack via set(), | 
 | 2540 |         // there _is_ a frameCount parameter.  We silently ignore it. | 
 | 2541 |         frameCount = sharedBuffer->size() / frameSize; | 
 | 2542 |     } else { | 
 | 2543 |         size_t minFrameCount = 0; | 
 | 2544 |         // For fast tracks we try to respect the application's request for notifications per buffer. | 
 | 2545 |         if (*flags & AUDIO_OUTPUT_FLAG_FAST) { | 
 | 2546 |             if (notificationsPerBuffer > 0) { | 
 | 2547 |                 // Avoid possible arithmetic overflow during multiplication. | 
 | 2548 |                 if (notificationsPerBuffer > SIZE_MAX / mFrameCount) { | 
 | 2549 |                     ALOGE("Requested notificationPerBuffer=%u ignored for HAL frameCount=%zu", | 
 | 2550 |                           notificationsPerBuffer, mFrameCount); | 
 | 2551 |                 } else { | 
 | 2552 |                     minFrameCount = mFrameCount * notificationsPerBuffer; | 
 | 2553 |                 } | 
 | 2554 |             } | 
 | 2555 |         } else { | 
 | 2556 |             // For normal PCM streaming tracks, update minimum frame count. | 
 | 2557 |             // Buffer depth is forced to be at least 2 x the normal mixer frame count and | 
 | 2558 |             // cover audio hardware latency. | 
 | 2559 |             // This is probably too conservative, but legacy application code may depend on it. | 
 | 2560 |             // If you change this calculation, also review the start threshold which is related. | 
 | 2561 |             uint32_t latencyMs = latency_l(); | 
 | 2562 |             if (latencyMs == 0) { | 
 | 2563 |                 ALOGE("Error when retrieving output stream latency"); | 
 | 2564 |                 lStatus = UNKNOWN_ERROR; | 
 | 2565 |                 goto Exit; | 
 | 2566 |             } | 
 | 2567 |  | 
 | 2568 |             minFrameCount = AudioSystem::calculateMinFrameCount(latencyMs, mNormalFrameCount, | 
 | 2569 |                                 mSampleRate, sampleRate, speed /*, 0 mNotificationsPerBufferReq*/); | 
 | 2570 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2571 |         } | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2572 |         if (frameCount < minFrameCount) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2573 |             frameCount = minFrameCount; | 
 | 2574 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2575 |     } | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2576 |  | 
 | 2577 |     // Make sure that application is notified with sufficient margin before underrun. | 
 | 2578 |     // The client can divide the AudioTrack buffer into sub-buffers, | 
 | 2579 |     // and expresses its desire to server as the notification frame count. | 
 | 2580 |     if (sharedBuffer == 0 && audio_is_linear_pcm(format)) { | 
 | 2581 |         size_t maxNotificationFrames; | 
 | 2582 |         if (*flags & AUDIO_OUTPUT_FLAG_FAST) { | 
 | 2583 |             // notify every HAL buffer, regardless of the size of the track buffer | 
 | 2584 |             maxNotificationFrames = mFrameCount; | 
 | 2585 |         } else { | 
| Andy Hung | fa11780 | 2019-04-12 13:50:39 -0700 | [diff] [blame] | 2586 |             // Triple buffer the notification period for a triple buffered mixer period; | 
 | 2587 |             // otherwise, double buffering for the notification period is fine. | 
 | 2588 |             // | 
 | 2589 |             // TODO: This should be moved to AudioTrack to modify the notification period | 
 | 2590 |             // on AudioTrack::setBufferSizeInFrames() changes. | 
 | 2591 |             const int nBuffering = | 
 | 2592 |                     (uint64_t{frameCount} * mSampleRate) | 
 | 2593 |                             / (uint64_t{mNormalFrameCount} * sampleRate) == 3 ? 3 : 2; | 
 | 2594 |  | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2595 |             maxNotificationFrames = frameCount / nBuffering; | 
 | 2596 |             // If client requested a fast track but this was denied, then use the smaller maximum. | 
 | 2597 |             if (requestedFlags & AUDIO_OUTPUT_FLAG_FAST) { | 
 | 2598 |                 size_t maxNotificationFramesFastDenied = FMS_20 * sampleRate / 1000; | 
 | 2599 |                 if (maxNotificationFrames > maxNotificationFramesFastDenied) { | 
 | 2600 |                     maxNotificationFrames = maxNotificationFramesFastDenied; | 
 | 2601 |                 } | 
 | 2602 |             } | 
 | 2603 |         } | 
 | 2604 |         if (notificationFrameCount == 0 || notificationFrameCount > maxNotificationFrames) { | 
 | 2605 |             if (notificationFrameCount == 0) { | 
 | 2606 |                 ALOGD("Client defaulted notificationFrames to %zu for frameCount %zu", | 
 | 2607 |                     maxNotificationFrames, frameCount); | 
 | 2608 |             } else { | 
 | 2609 |                 ALOGW("Client adjusted notificationFrames from %zu to %zu for frameCount %zu", | 
 | 2610 |                       notificationFrameCount, maxNotificationFrames, frameCount); | 
 | 2611 |             } | 
 | 2612 |             notificationFrameCount = maxNotificationFrames; | 
 | 2613 |         } | 
 | 2614 |     } | 
 | 2615 |  | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 2616 |     *pFrameCount = frameCount; | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2617 |     *pNotificationFrameCount = notificationFrameCount; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2618 |  | 
| Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2619 |     switch (mType) { | 
| jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 2620 |     case BIT_PERFECT: | 
 | 2621 |         if (isBitPerfect) { | 
 | 2622 |             if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { | 
 | 2623 |                 ALOGE("%s, bad parameter when request streaming bit-perfect, sampleRate=%u, " | 
 | 2624 |                       "format=%#x, channelMask=%#x, mSampleRate=%u, mFormat=%#x, mChannelMask=%#x", | 
 | 2625 |                       __func__, sampleRate, format, channelMask, mSampleRate, mFormat, | 
 | 2626 |                       mChannelMask); | 
 | 2627 |                 lStatus = BAD_VALUE; | 
 | 2628 |                 goto Exit; | 
 | 2629 |             } | 
 | 2630 |         } | 
 | 2631 |         break; | 
| Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2632 |  | 
 | 2633 |     case DIRECT: | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 2634 |         if (audio_is_linear_pcm(format)) { // TODO maybe use audio_has_proportional_frames()? | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2635 |             if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { | 
| Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 2636 |                 ALOGE("createTrack_l() Bad parameter: sampleRate %u format %#x, channelMask 0x%08x " | 
 | 2637 |                         "for output %p with format %#x", | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2638 |                         sampleRate, format, channelMask, mOutput, mFormat); | 
 | 2639 |                 lStatus = BAD_VALUE; | 
 | 2640 |                 goto Exit; | 
 | 2641 |             } | 
 | 2642 |         } | 
| Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2643 |         break; | 
 | 2644 |  | 
 | 2645 |     case OFFLOAD: | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2646 |         if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { | 
| Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 2647 |             ALOGE("createTrack_l() Bad parameter: sampleRate %d format %#x, channelMask 0x%08x \"" | 
 | 2648 |                     "for output %p with format %#x", | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2649 |                     sampleRate, format, channelMask, mOutput, mFormat); | 
 | 2650 |             lStatus = BAD_VALUE; | 
 | 2651 |             goto Exit; | 
 | 2652 |         } | 
| Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2653 |         break; | 
 | 2654 |  | 
 | 2655 |     default: | 
| Glenn Kasten | 993fa06 | 2014-05-02 11:14:34 -0700 | [diff] [blame] | 2656 |         if (!audio_is_linear_pcm(format)) { | 
| Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 2657 |                 ALOGE("createTrack_l() Bad parameter: format %#x \"" | 
 | 2658 |                         "for output %p with format %#x", | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2659 |                         format, mOutput, mFormat); | 
 | 2660 |                 lStatus = BAD_VALUE; | 
 | 2661 |                 goto Exit; | 
 | 2662 |         } | 
| Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 2663 |         if (sampleRate > mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2664 |             ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate); | 
 | 2665 |             lStatus = BAD_VALUE; | 
 | 2666 |             goto Exit; | 
 | 2667 |         } | 
| Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2668 |         break; | 
 | 2669 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2670 |     } | 
 | 2671 |  | 
 | 2672 |     lStatus = initCheck(); | 
 | 2673 |     if (lStatus != NO_ERROR) { | 
| Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 2674 |         ALOGE("createTrack_l() audio driver not initialized"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2675 |         goto Exit; | 
 | 2676 |     } | 
 | 2677 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2678 |     { // scope for mutex() | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2679 |         audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2680 |  | 
 | 2681 |         // all tracks in same audio session must share the same routing strategy otherwise | 
 | 2682 |         // conflicts will happen when tracks are moved from one output to another by audio policy | 
 | 2683 |         // manager | 
| Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 2684 |         product_strategy_t strategy = getStrategyForStream(streamType); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2685 |         for (size_t i = 0; i < mTracks.size(); ++i) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2686 |             sp<IAfTrack> t = mTracks[i]; | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2687 |             if (t != 0 && t->isExternalTrack()) { | 
| Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 2688 |                 product_strategy_t actual = getStrategyForStream(t->streamType()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2689 |                 if (sessionId == t->sessionId() && strategy != actual) { | 
 | 2690 |                     ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", | 
 | 2691 |                             strategy, actual); | 
 | 2692 |                     lStatus = BAD_VALUE; | 
 | 2693 |                     goto Exit; | 
 | 2694 |                 } | 
 | 2695 |             } | 
 | 2696 |         } | 
 | 2697 |  | 
| Deeraj Soman | 2b51523 | 2024-05-14 12:58:24 +0530 | [diff] [blame] | 2698 |         // Set DIRECT/OFFLOAD flag if current thread is DirectOutputThread/OffloadThread. | 
 | 2699 |         // This can happen when the playback is rerouted to direct output/offload thread by | 
| yucliu | c9c49cd | 2020-07-13 16:25:21 -0700 | [diff] [blame] | 2700 |         // dynamic audio policy. | 
 | 2701 |         // Do NOT report the flag changes back to client, since the client | 
| Deeraj Soman | 2b51523 | 2024-05-14 12:58:24 +0530 | [diff] [blame] | 2702 |         // doesn't explicitly request a direct/offload flag. | 
| yucliu | c9c49cd | 2020-07-13 16:25:21 -0700 | [diff] [blame] | 2703 |         audio_output_flags_t trackFlags = *flags; | 
 | 2704 |         if (mType == DIRECT) { | 
 | 2705 |             trackFlags = static_cast<audio_output_flags_t>(trackFlags | AUDIO_OUTPUT_FLAG_DIRECT); | 
| Deeraj Soman | 2b51523 | 2024-05-14 12:58:24 +0530 | [diff] [blame] | 2706 |         } else if (mType == OFFLOAD) { | 
 | 2707 |             trackFlags = static_cast<audio_output_flags_t>(trackFlags | | 
 | 2708 |                                    AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT); | 
| yucliu | c9c49cd | 2020-07-13 16:25:21 -0700 | [diff] [blame] | 2709 |         } | 
| jiabin | 94ed47c | 2023-07-27 23:34:20 +0000 | [diff] [blame] | 2710 |         *afTrackFlags = trackFlags; | 
| yucliu | c9c49cd | 2020-07-13 16:25:21 -0700 | [diff] [blame] | 2711 |  | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2712 |         track = IAfTrack::create(this, client, streamType, attr, sampleRate, format, | 
| Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 2713 |                           channelMask, frameCount, | 
 | 2714 |                           nullptr /* buffer */, (size_t)0 /* bufferSize */, sharedBuffer, | 
| Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 2715 |                           sessionId, creatorPid, attributionSource, trackFlags, | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2716 |                           IAfTrackBase::TYPE_DEFAULT, portId, SIZE_MAX /*frameCountToBeReady*/, | 
| jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 2717 |                           speed, isSpatialized, isBitPerfect); | 
| Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 2718 |  | 
| Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 2719 |         lStatus = track != 0 ? track->initCheck() : (status_t) NO_MEMORY; | 
 | 2720 |         if (lStatus != NO_ERROR) { | 
| Glenn Kasten | 0cde076 | 2014-01-16 15:06:36 -0800 | [diff] [blame] | 2721 |             ALOGE("createTrack_l() initCheck failed %d; no control block?", lStatus); | 
| Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 2722 |             // 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] | 2723 |             goto Exit; | 
 | 2724 |         } | 
 | 2725 |         mTracks.add(track); | 
| jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 2726 |         { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2727 |             audio_utils::lock_guard _atCbL(audioTrackCbMutex()); | 
| jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 2728 |             if (callback.get() != nullptr) { | 
| jiabin | 18a4b1c | 2020-09-17 11:40:42 -0700 | [diff] [blame] | 2729 |                 mAudioTrackCallbacks.emplace(track, callback); | 
| jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 2730 |             } | 
 | 2731 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2732 |  | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 2733 |         sp<IAfEffectChain> chain = getEffectChain_l(sessionId); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2734 |         if (chain != 0) { | 
 | 2735 |             ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); | 
 | 2736 |             track->setMainBuffer(chain->inBuffer()); | 
| Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 2737 |             chain->setStrategy(getStrategyForStream(track->streamType())); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2738 |             chain->incTrackCnt(); | 
 | 2739 |         } | 
 | 2740 |  | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2741 |         if ((*flags & AUDIO_OUTPUT_FLAG_FAST) && (tid != -1)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2742 |             pid_t callingPid = IPCThreadState::self()->getCallingPid(); | 
 | 2743 |             // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, | 
 | 2744 |             // so ask activity manager to do this on our behalf | 
| Glenn Kasten | af9a7b5 | 2017-03-29 11:29:39 -0700 | [diff] [blame] | 2745 |             sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp, true /*forApp*/); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2746 |         } | 
 | 2747 |     } | 
 | 2748 |  | 
 | 2749 |     lStatus = NO_ERROR; | 
 | 2750 |  | 
 | 2751 | Exit: | 
| Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 2752 |     *status = lStatus; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2753 |     return track; | 
 | 2754 | } | 
 | 2755 |  | 
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2756 | template<typename T> | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2757 | ssize_t PlaybackThread::Tracks<T>::remove(const sp<T>& track) | 
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2758 | { | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 2759 |     const int trackId = track->id(); | 
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2760 |     const ssize_t index = mTracks.remove(track); | 
 | 2761 |     if (index >= 0) { | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 2762 |         if (mSaveDeletedTrackIds) { | 
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2763 |             // We can't directly access mAudioMixer since the caller may be outside of threadLoop. | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 2764 |             // Instead, we add to mDeletedTrackIds which is solely used for mAudioMixer update, | 
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2765 |             // to be handled when MixerThread::prepareTracks_l() next changes mAudioMixer. | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 2766 |             mDeletedTrackIds.emplace(trackId); | 
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2767 |         } | 
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2768 |     } | 
 | 2769 |     return index; | 
 | 2770 | } | 
 | 2771 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2772 | uint32_t PlaybackThread::correctLatency_l(uint32_t latency) const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2773 | { | 
 | 2774 |     return latency; | 
 | 2775 | } | 
 | 2776 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2777 | uint32_t PlaybackThread::latency() const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2778 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2779 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2780 |     return latency_l(); | 
 | 2781 | } | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2782 | uint32_t PlaybackThread::latency_l() const | 
| Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2783 | NO_THREAD_SAFETY_ANALYSIS | 
 | 2784 | // Fix later. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2785 | { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2786 |     uint32_t latency; | 
 | 2787 |     if (initCheck() == NO_ERROR && mOutput->stream->getLatency(&latency) == OK) { | 
 | 2788 |         return correctLatency_l(latency); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2789 |     } | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2790 |     return 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2791 | } | 
 | 2792 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2793 | void PlaybackThread::setMasterVolume(float value) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2794 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2795 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2796 |     // Don't apply master volume in SW if our HAL can do it for us. | 
 | 2797 |     if (mOutput && mOutput->audioHwDev && | 
 | 2798 |         mOutput->audioHwDev->canSetMasterVolume()) { | 
 | 2799 |         mMasterVolume = 1.0; | 
 | 2800 |     } else { | 
 | 2801 |         mMasterVolume = value; | 
 | 2802 |     } | 
 | 2803 | } | 
 | 2804 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2805 | void PlaybackThread::setMasterBalance(float balance) | 
| Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 2806 | { | 
 | 2807 |     mMasterBalance.store(balance); | 
 | 2808 | } | 
 | 2809 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2810 | void PlaybackThread::setMasterMute(bool muted) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2811 | { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2812 |     if (isDuplicating()) { | 
 | 2813 |         return; | 
 | 2814 |     } | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2815 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2816 |     // Don't apply master mute in SW if our HAL can do it for us. | 
 | 2817 |     if (mOutput && mOutput->audioHwDev && | 
 | 2818 |         mOutput->audioHwDev->canSetMasterMute()) { | 
 | 2819 |         mMasterMute = false; | 
 | 2820 |     } else { | 
 | 2821 |         mMasterMute = muted; | 
 | 2822 |     } | 
 | 2823 | } | 
 | 2824 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2825 | void PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2826 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2827 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2828 |     mStreamTypes[stream].volume = value; | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2829 |     broadcast_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2830 | } | 
 | 2831 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2832 | void PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2833 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2834 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2835 |     mStreamTypes[stream].mute = muted; | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2836 |     broadcast_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2837 | } | 
 | 2838 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2839 | float PlaybackThread::streamVolume(audio_stream_type_t stream) const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2840 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2841 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2842 |     return mStreamTypes[stream].volume; | 
 | 2843 | } | 
 | 2844 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2845 | void PlaybackThread::setVolumeForOutput_l(float left, float right) const | 
| Tomoharu Kasahara | 1990bd4 | 2014-12-12 14:04:11 +0900 | [diff] [blame] | 2846 | { | 
 | 2847 |     mOutput->stream->setVolume(left, right); | 
 | 2848 | } | 
 | 2849 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2850 | // addTrack_l() must be called with ThreadBase::mutex() held | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2851 | status_t PlaybackThread::addTrack_l(const sp<IAfTrack>& track) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2852 | { | 
 | 2853 |     status_t status = ALREADY_EXISTS; | 
 | 2854 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2855 |     if (mActiveTracks.indexOf(track) < 0) { | 
 | 2856 |         // the track is newly added, make sure it fills up all its | 
 | 2857 |         // buffers before playing. This is to ensure the client will | 
 | 2858 |         // effectively get the latency it requested. | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2859 |         if (track->isExternalTrack()) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2860 |             IAfTrackBase::track_state state = track->state(); | 
| Andy Hung | a718771 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 2861 |             // Because the track is not on the ActiveTracks, | 
 | 2862 |             // at this point, only the TrackHandle will be adding the track. | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2863 |             mutex().unlock(); | 
| Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 2864 |             status = AudioSystem::startOutput(track->portId()); | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2865 |             mutex().lock(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2866 |             // abort track was stopped/paused while we released the lock | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2867 |             if (state != track->state()) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2868 |                 if (status == NO_ERROR) { | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2869 |                     mutex().unlock(); | 
| Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 2870 |                     AudioSystem::stopOutput(track->portId()); | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2871 |                     mutex().lock(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2872 |                 } | 
 | 2873 |                 return INVALID_OPERATION; | 
 | 2874 |             } | 
 | 2875 |             // abort if start is rejected by audio policy manager | 
 | 2876 |             if (status != NO_ERROR) { | 
| jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 2877 |                 // Do not replace the error if it is DEAD_OBJECT. When this happens, it indicates | 
 | 2878 |                 // current playback thread is reopened, which may happen when clients set preferred | 
 | 2879 |                 // mixer configuration. Returning DEAD_OBJECT will make the client restore track | 
 | 2880 |                 // immediately. | 
 | 2881 |                 return status == DEAD_OBJECT ? status : PERMISSION_DENIED; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2882 |             } | 
 | 2883 | #ifdef ADD_BATTERY_DATA | 
 | 2884 |             // to track the speaker usage | 
 | 2885 |             addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); | 
 | 2886 | #endif | 
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 2887 |             sendIoConfigEvent_l(AUDIO_CLIENT_STARTED, track->creatorPid(), track->portId()); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2888 |         } | 
 | 2889 |  | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2890 |         // set retry count for buffer fill | 
 | 2891 |         if (track->isOffloaded()) { | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 2892 |             if (track->isStopping_1()) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2893 |                 track->retryCount() = kMaxTrackStopRetriesOffload; | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 2894 |             } else { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2895 |                 track->retryCount() = kMaxTrackStartupRetriesOffload; | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 2896 |             } | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2897 |             track->fillingStatus() = mStandby ? IAfTrack::FS_FILLING : IAfTrack::FS_FILLED; | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2898 |         } else { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2899 |             track->retryCount() = kMaxTrackStartupRetries; | 
 | 2900 |             track->fillingStatus() = | 
 | 2901 |                     track->sharedBuffer() != 0 ? IAfTrack::FS_FILLED : IAfTrack::FS_FILLING; | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2902 |         } | 
 | 2903 |  | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 2904 |         sp<IAfEffectChain> chain = getEffectChain_l(track->sessionId()); | 
| jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 2905 |         if (mHapticChannelMask != AUDIO_CHANNEL_NONE | 
 | 2906 |                 && ((track->channelMask() & AUDIO_CHANNEL_HAPTIC_ALL) != AUDIO_CHANNEL_NONE | 
 | 2907 |                         || (chain != nullptr && chain->containsHapticGeneratingEffect_l()))) { | 
| jiabin | 57303cc | 2018-12-18 15:45:57 -0800 | [diff] [blame] | 2908 |             // Unlock due to VibratorService will lock for this call and will | 
 | 2909 |             // call Tracks.mute/unmute which also require thread's lock. | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2910 |             mutex().unlock(); | 
| Ahmad Khalil | 229466a | 2024-02-05 12:15:30 +0000 | [diff] [blame] | 2911 |             const os::HapticScale hapticScale = afutils::onExternalVibrationStart( | 
| jiabin | 57303cc | 2018-12-18 15:45:57 -0800 | [diff] [blame] | 2912 |                     track->getExternalVibration()); | 
| Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 2913 |             std::optional<media::AudioVibratorInfo> vibratorInfo; | 
 | 2914 |             { | 
 | 2915 |                 // TODO(b/184194780): Use the vibrator information from the vibrator that will be | 
 | 2916 |                 // used to play this track. | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2917 |                  audio_utils::lock_guard _l(mAfThreadCallback->mutex()); | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 2918 |                 vibratorInfo = std::move(mAfThreadCallback->getDefaultVibratorInfo_l()); | 
| Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 2919 |             } | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2920 |             mutex().lock(); | 
| Ahmad Khalil | 229466a | 2024-02-05 12:15:30 +0000 | [diff] [blame] | 2921 |             track->setHapticScale(hapticScale); | 
| Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 2922 |             if (vibratorInfo) { | 
 | 2923 |                 track->setHapticMaxAmplitude(vibratorInfo->maxAmplitude); | 
 | 2924 |             } | 
 | 2925 |  | 
| jiabin | 57303cc | 2018-12-18 15:45:57 -0800 | [diff] [blame] | 2926 |             // Haptic playback should be enabled by vibrator service. | 
 | 2927 |             if (track->getHapticPlaybackEnabled()) { | 
 | 2928 |                 // Disable haptic playback of all active track to ensure only | 
 | 2929 |                 // one track playing haptic if current track should play haptic. | 
 | 2930 |                 for (const auto &t : mActiveTracks) { | 
 | 2931 |                     t->setHapticPlaybackEnabled(false); | 
 | 2932 |                 } | 
| jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 2933 |             } | 
| jiabin | e70bc7f | 2020-06-30 22:07:55 -0700 | [diff] [blame] | 2934 |  | 
 | 2935 |             // Set haptic intensity for effect | 
 | 2936 |             if (chain != nullptr) { | 
| Ahmad Khalil | 229466a | 2024-02-05 12:15:30 +0000 | [diff] [blame] | 2937 |                 // TODO(b/324559333): Add adaptive haptics scaling support for the HapticGenerator. | 
 | 2938 |                 chain->setHapticScale_l(track->id(), hapticScale); | 
| jiabin | e70bc7f | 2020-06-30 22:07:55 -0700 | [diff] [blame] | 2939 |             } | 
| jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 2940 |         } | 
 | 2941 |  | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2942 |         track->setResetDone(false); | 
| Andy Hung | 59de426 | 2021-06-14 10:53:54 -0700 | [diff] [blame] | 2943 |         track->resetPresentationComplete(); | 
| Andy Hung | a718771 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 2944 |  | 
 | 2945 |         // Do not release the ThreadBase mutex after the track is added to mActiveTracks unless | 
 | 2946 |         // all key changes are complete.  It is possible that the threadLoop will begin | 
 | 2947 |         // processing the added track immediately after the ThreadBase mutex is released. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2948 |         mActiveTracks.add(track); | 
| Andy Hung | a718771 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 2949 |  | 
| Eric Laurent | d0107bc | 2013-06-11 14:38:48 -0700 | [diff] [blame] | 2950 |         if (chain != 0) { | 
 | 2951 |             ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), | 
 | 2952 |                     track->sessionId()); | 
 | 2953 |             chain->incActiveTrackCnt(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2954 |         } | 
 | 2955 |  | 
| Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 2956 |         track->logBeginInterval(patchSinksToString(&mPatch)); // log to MediaMetrics | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2957 |         status = NO_ERROR; | 
 | 2958 |     } | 
 | 2959 |  | 
| Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 2960 |     onAddNewTrack_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2961 |     return status; | 
 | 2962 | } | 
 | 2963 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2964 | bool PlaybackThread::destroyTrack_l(const sp<IAfTrack>& track) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2965 | { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2966 |     track->terminate(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2967 |     // active tracks are removed by threadLoop() | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2968 |     bool trackActive = (mActiveTracks.indexOf(track) >= 0); | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2969 |     track->setState(IAfTrackBase::STOPPED); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2970 |     if (!trackActive) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2971 |         removeTrack_l(track); | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 2972 |     } else if (track->isFastTrack() || track->isOffloaded() || track->isDirect()) { | 
| Andy Hung | 916987e | 2023-03-20 19:08:16 -0700 | [diff] [blame] | 2973 |         if (track->isPausePending()) { | 
 | 2974 |             track->pauseAck(); | 
 | 2975 |         } | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2976 |         track->setState(IAfTrackBase::STOPPING_1); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2977 |     } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2978 |  | 
 | 2979 |     return trackActive; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2980 | } | 
 | 2981 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2982 | void PlaybackThread::removeTrack_l(const sp<IAfTrack>& track) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2983 | { | 
 | 2984 |     track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); | 
| Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 2985 |  | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2986 |     String8 result; | 
 | 2987 |     track->appendDump(result, false /* active */); | 
| Tomasz Wasilczyk | 5b05437 | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 2988 |     mLocalLog.log("removeTrack_l (%p) %s", track.get(), result.c_str()); | 
| Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 2989 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2990 |     mTracks.remove(track); | 
| jiabin | 18a4b1c | 2020-09-17 11:40:42 -0700 | [diff] [blame] | 2991 |     { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2992 |         audio_utils::lock_guard _atCbL(audioTrackCbMutex()); | 
| jiabin | 18a4b1c | 2020-09-17 11:40:42 -0700 | [diff] [blame] | 2993 |         mAudioTrackCallbacks.erase(track); | 
 | 2994 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2995 |     if (track->isFastTrack()) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2996 |         int index = track->fastIndex(); | 
| Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 2997 |         ALOG_ASSERT(0 < index && index < (int)FastMixerState::sMaxFastTracks); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2998 |         ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index))); | 
 | 2999 |         mFastTrackAvailMask |= 1 << index; | 
 | 3000 |         // redundant as track is about to be destroyed, for dumpsys only | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3001 |         track->fastIndex() = -1; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3002 |     } | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 3003 |     sp<IAfEffectChain> chain = getEffectChain_l(track->sessionId()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3004 |     if (chain != 0) { | 
 | 3005 |         chain->decTrackCnt(); | 
 | 3006 |     } | 
 | 3007 | } | 
 | 3008 |  | 
| Mikhail Naganov | f548cd3 | 2024-05-29 17:06:46 +0000 | [diff] [blame] | 3009 | std::set<audio_port_handle_t> PlaybackThread::getTrackPortIds_l() | 
 | 3010 | { | 
 | 3011 |     std::set<int32_t> result; | 
 | 3012 |     for (const auto& t : mTracks) { | 
 | 3013 |         if (t->isExternalTrack()) { | 
 | 3014 |             result.insert(t->portId()); | 
 | 3015 |         } | 
 | 3016 |     } | 
 | 3017 |     return result; | 
 | 3018 | } | 
 | 3019 |  | 
 | 3020 | std::set<audio_port_handle_t> PlaybackThread::getTrackPortIds() | 
 | 3021 | { | 
 | 3022 |     audio_utils::lock_guard _l(mutex()); | 
 | 3023 |     return getTrackPortIds_l(); | 
 | 3024 | } | 
 | 3025 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3026 | String8 PlaybackThread::getParameters(const String8& keys) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3027 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3028 |     audio_utils::lock_guard _l(mutex()); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3029 |     String8 out_s8; | 
 | 3030 |     if (initCheck() == NO_ERROR && mOutput->stream->getParameters(keys, &out_s8) == OK) { | 
 | 3031 |         return out_s8; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3032 |     } | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 3033 |     return {}; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3034 | } | 
 | 3035 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3036 | status_t DirectOutputThread::selectPresentation(int presentationId, int programId) { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3037 |     audio_utils::lock_guard _l(mutex()); | 
| Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 3038 |     if (!isStreamInitialized()) { | 
| Mikhail Naganov | ac917ac | 2018-11-28 14:03:52 -0800 | [diff] [blame] | 3039 |         return NO_INIT; | 
 | 3040 |     } | 
 | 3041 |     return mOutput->stream->selectPresentation(presentationId, programId); | 
 | 3042 | } | 
 | 3043 |  | 
| Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 3044 | void PlaybackThread::ioConfigChanged_l(audio_io_config_event_t event, pid_t pid, | 
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 3045 |                                                    audio_port_handle_t portId) { | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 3046 |     ALOGV("PlaybackThread::ioConfigChanged, thread %p, event %d", this, event); | 
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 3047 |     sp<AudioIoDescriptor> desc; | 
 | 3048 |     const struct audio_patch patch = isMsdDevice() ? mDownStreamPatch : mPatch; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3049 |     switch (event) { | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 3050 |     case AUDIO_OUTPUT_OPENED: | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 3051 |     case AUDIO_OUTPUT_REGISTERED: | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 3052 |     case AUDIO_OUTPUT_CONFIG_CHANGED: | 
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 3053 |         desc = sp<AudioIoDescriptor>::make(mId, patch, false /*isInput*/, | 
 | 3054 |                 mSampleRate, mFormat, mChannelMask, | 
 | 3055 |                 // FIXME AudioFlinger::frameCount(audio_io_handle_t) instead of mNormalFrameCount? | 
 | 3056 |                 mNormalFrameCount, mFrameCount, latency_l()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3057 |         break; | 
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 3058 |     case AUDIO_CLIENT_STARTED: | 
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 3059 |         desc = sp<AudioIoDescriptor>::make(mId, patch, portId); | 
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 3060 |         break; | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 3061 |     case AUDIO_OUTPUT_CLOSED: | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3062 |     default: | 
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 3063 |         desc = sp<AudioIoDescriptor>::make(mId); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3064 |         break; | 
 | 3065 |     } | 
| Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 3066 |     mAfThreadCallback->ioConfigChanged_l(event, desc, pid); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3067 | } | 
 | 3068 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3069 | void PlaybackThread::onWriteReady() | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3070 | { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3071 |     mCallbackThread->resetWriteBlocked(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3072 | } | 
 | 3073 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3074 | void PlaybackThread::onDrainReady() | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3075 | { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3076 |     mCallbackThread->resetDraining(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3077 | } | 
 | 3078 |  | 
| Mikhail Naganov | f548cd3 | 2024-05-29 17:06:46 +0000 | [diff] [blame] | 3079 | void PlaybackThread::onError(bool isHardError) | 
| Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 3080 | { | 
| Mikhail Naganov | f548cd3 | 2024-05-29 17:06:46 +0000 | [diff] [blame] | 3081 |     mCallbackThread->setAsyncError(isHardError); | 
| Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 3082 | } | 
 | 3083 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3084 | void PlaybackThread::onCodecFormatChanged( | 
| Ryan Prichard | 78c5e45 | 2024-02-08 16:16:57 -0800 | [diff] [blame] | 3085 |         const std::vector<uint8_t>& metadataBs) | 
| jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 3086 | { | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3087 |     const auto weakPointerThis = wp<PlaybackThread>::fromExisting(this); | 
| Kuowei Li | 9e2f616 | 2022-11-23 16:25:26 +0800 | [diff] [blame] | 3088 |     std::thread([this, metadataBs, weakPointerThis]() { | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3089 |             const sp<PlaybackThread> playbackThread = weakPointerThis.promote(); | 
| Kuowei Li | 9e2f616 | 2022-11-23 16:25:26 +0800 | [diff] [blame] | 3090 |             if (playbackThread == nullptr) { | 
 | 3091 |                 ALOGW("PlaybackThread was destroyed, skip codec format change event"); | 
 | 3092 |                 return; | 
 | 3093 |             } | 
 | 3094 |  | 
| jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 3095 |             audio_utils::metadata::Data metadata = | 
 | 3096 |                     audio_utils::metadata::dataFromByteString(metadataBs); | 
 | 3097 |             if (metadata.empty()) { | 
 | 3098 |                 ALOGW("Can not transform the buffer to audio metadata, %s, %d", | 
 | 3099 |                       reinterpret_cast<char*>(const_cast<uint8_t*>(metadataBs.data())), | 
 | 3100 |                       (int)metadataBs.size()); | 
 | 3101 |                 return; | 
 | 3102 |             } | 
 | 3103 |  | 
 | 3104 |             audio_utils::metadata::ByteString metaDataStr = | 
 | 3105 |                     audio_utils::metadata::byteStringFromData(metadata); | 
 | 3106 |             std::vector metadataVec(metaDataStr.begin(), metaDataStr.end()); | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3107 |             audio_utils::lock_guard _l(audioTrackCbMutex()); | 
| jiabin | 18a4b1c | 2020-09-17 11:40:42 -0700 | [diff] [blame] | 3108 |             for (const auto& callbackPair : mAudioTrackCallbacks) { | 
 | 3109 |                 callbackPair.second->onCodecFormatChanged(metadataVec); | 
| jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 3110 |             } | 
 | 3111 |     }).detach(); | 
 | 3112 | } | 
 | 3113 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3114 | void PlaybackThread::resetWriteBlocked(uint32_t sequence) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3115 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3116 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3117 |     // reject out of sequence requests | 
 | 3118 |     if ((mWriteAckSequence & 1) && (sequence == mWriteAckSequence)) { | 
 | 3119 |         mWriteAckSequence &= ~1; | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 3120 |         mWaitWorkCV.notify_one(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3121 |     } | 
 | 3122 | } | 
 | 3123 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3124 | void PlaybackThread::resetDraining(uint32_t sequence) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3125 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3126 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3127 |     // reject out of sequence requests | 
 | 3128 |     if ((mDrainSequence & 1) && (sequence == mDrainSequence)) { | 
| Andy Hung | f323451 | 2018-07-03 14:51:47 -0700 | [diff] [blame] | 3129 |         // Register discontinuity when HW drain is completed because that can cause | 
 | 3130 |         // the timestamp frame position to reset to 0 for direct and offload threads. | 
 | 3131 |         // (Out of sequence requests are ignored, since the discontinuity would be handled | 
 | 3132 |         // elsewhere, e.g. in flush). | 
| Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 3133 |         mTimestampVerifier.discontinuity(mTimestampVerifier.DISCONTINUITY_MODE_ZERO); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3134 |         mDrainSequence &= ~1; | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 3135 |         mWaitWorkCV.notify_one(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3136 |     } | 
 | 3137 | } | 
 | 3138 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3139 | void PlaybackThread::readOutputParameters_l() | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3140 | NO_THREAD_SAFETY_ANALYSIS | 
 | 3141 | // 'moveEffectChain_ll' requires holding mutex 'AudioFlinger_Mutex' exclusively | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3142 | { | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 3143 |     // unfortunately we have no way of recovering from errors here, hence the LOG_ALWAYS_FATAL | 
| Mikhail Naganov | 560637e | 2021-03-31 22:40:13 +0000 | [diff] [blame] | 3144 |     const audio_config_base_t audioConfig = mOutput->getAudioProperties(); | 
 | 3145 |     mSampleRate = audioConfig.sample_rate; | 
 | 3146 |     mChannelMask = audioConfig.channel_mask; | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 3147 |     if (!audio_is_output_channel(mChannelMask)) { | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 3148 |         LOG_ALWAYS_FATAL("HAL channel mask %#x not valid for output", mChannelMask); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 3149 |     } | 
| Andy Hung | d21a2ab | 2023-07-20 21:44:14 -0700 | [diff] [blame] | 3150 |     if (hasMixer() && !isValidPcmSinkChannelMask(mChannelMask)) { | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 3151 |         LOG_ALWAYS_FATAL("HAL channel mask %#x not supported for mixed output", | 
 | 3152 |                 mChannelMask); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 3153 |     } | 
| Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 3154 |  | 
 | 3155 |     if (mMixerChannelMask == AUDIO_CHANNEL_NONE) { | 
 | 3156 |         mMixerChannelMask = mChannelMask; | 
 | 3157 |     } | 
 | 3158 |  | 
| Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 3159 |     mChannelCount = audio_channel_count_from_out_mask(mChannelMask); | 
| Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 3160 |     mBalance.setChannelMask(mChannelMask); | 
| Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 3161 |  | 
| Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 3162 |     uint32_t mixerChannelCount = audio_channel_count_from_out_mask(mMixerChannelMask); | 
 | 3163 |  | 
| Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 3164 |     // Get actual HAL format. | 
| Mikhail Naganov | 560637e | 2021-03-31 22:40:13 +0000 | [diff] [blame] | 3165 |     status_t result = mOutput->stream->getAudioProperties(nullptr, nullptr, &mHALFormat); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3166 |     LOG_ALWAYS_FATAL_IF(result != OK, "Error when retrieving output stream format: %d", result); | 
| Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 3167 |     // Get format from the shim, which will be different than the HAL format | 
 | 3168 |     // if playing compressed audio over HDMI passthrough. | 
| Mikhail Naganov | 560637e | 2021-03-31 22:40:13 +0000 | [diff] [blame] | 3169 |     mFormat = audioConfig.format; | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 3170 |     if (!audio_is_valid_format(mFormat)) { | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 3171 |         LOG_ALWAYS_FATAL("HAL format %#x not valid for output", mFormat); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 3172 |     } | 
| Andy Hung | d21a2ab | 2023-07-20 21:44:14 -0700 | [diff] [blame] | 3173 |     if (hasMixer() && !isValidPcmSinkFormat(mFormat)) { | 
| Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 3174 |         LOG_FATAL("HAL format %#x not supported for mixed output", | 
 | 3175 |                 mFormat); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 3176 |     } | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 3177 |     mFrameSize = mOutput->getFrameSize(); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3178 |     result = mOutput->stream->getBufferSize(&mBufferSize); | 
 | 3179 |     LOG_ALWAYS_FATAL_IF(result != OK, | 
 | 3180 |             "Error when retrieving output stream buffer size: %d", result); | 
| Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 3181 |     mFrameCount = mBufferSize / mFrameSize; | 
| Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 3182 |     if (hasMixer() && (mFrameCount & 15)) { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3183 |         ALOGW("HAL output buffer size is %zu frames but AudioMixer requires multiples of 16 frames", | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3184 |                 mFrameCount); | 
 | 3185 |     } | 
 | 3186 |  | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 3187 |     mHwSupportsPause = false; | 
 | 3188 |     if (mOutput->flags & AUDIO_OUTPUT_FLAG_DIRECT) { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3189 |         bool supportsPause = false, supportsResume = false; | 
 | 3190 |         if (mOutput->stream->supportsPauseAndResume(&supportsPause, &supportsResume) == OK) { | 
 | 3191 |             if (supportsPause && supportsResume) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 3192 |                 mHwSupportsPause = true; | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3193 |             } else if (supportsPause) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 3194 |                 ALOGW("direct output implements pause but not resume"); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3195 |             } else if (supportsResume) { | 
 | 3196 |                 ALOGW("direct output implements resume but not pause"); | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 3197 |             } | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 3198 |         } | 
 | 3199 |     } | 
| Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 3200 |     if (!mHwSupportsPause && mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) { | 
 | 3201 |         LOG_ALWAYS_FATAL("HW_AV_SYNC requested but HAL does not implement pause and resume"); | 
 | 3202 |     } | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 3203 |  | 
| Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 3204 |     if (mType == DUPLICATING && mMixerBufferEnabled && mEffectBufferEnabled) { | 
 | 3205 |         // For best precision, we use float instead of the associated output | 
 | 3206 |         // device format (typically PCM 16 bit). | 
 | 3207 |  | 
 | 3208 |         mFormat = AUDIO_FORMAT_PCM_FLOAT; | 
 | 3209 |         mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); | 
 | 3210 |         mBufferSize = mFrameSize * mFrameCount; | 
 | 3211 |  | 
 | 3212 |         // TODO: We currently use the associated output device channel mask and sample rate. | 
 | 3213 |         // (1) Perhaps use the ORed channel mask of all downstream MixerThreads | 
 | 3214 |         // (if a valid mask) to avoid premature downmix. | 
 | 3215 |         // (2) Perhaps use the maximum sample rate of all downstream MixerThreads | 
 | 3216 |         // instead of the output device sample rate to avoid loss of high frequency information. | 
 | 3217 |         // This may need to be updated as MixerThread/OutputTracks are added and not here. | 
 | 3218 |     } | 
 | 3219 |  | 
| Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 3220 |     // 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] | 3221 |     double multiplier = 1.0; | 
| Andy Hung | d363992 | 2022-04-28 18:00:49 -0700 | [diff] [blame] | 3222 |     // Note: mType == SPATIALIZER does not support FastMixer. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3223 |     if (mType == MIXER && (kUseFastMixer == FastMixer_Static || | 
 | 3224 |             kUseFastMixer == FastMixer_Dynamic)) { | 
| Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 3225 |         size_t minNormalFrameCount = (kMinNormalSinkBufferSizeMs * mSampleRate) / 1000; | 
 | 3226 |         size_t maxNormalFrameCount = (kMaxNormalSinkBufferSizeMs * mSampleRate) / 1000; | 
| Haynes Mathew George | 227a14b | 2016-05-09 12:45:48 -0700 | [diff] [blame] | 3227 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3228 |         // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer | 
 | 3229 |         minNormalFrameCount = (minNormalFrameCount + 15) & ~15; | 
 | 3230 |         maxNormalFrameCount = maxNormalFrameCount & ~15; | 
 | 3231 |         if (maxNormalFrameCount < minNormalFrameCount) { | 
 | 3232 |             maxNormalFrameCount = minNormalFrameCount; | 
 | 3233 |         } | 
 | 3234 |         multiplier = (double) minNormalFrameCount / (double) mFrameCount; | 
 | 3235 |         if (multiplier <= 1.0) { | 
 | 3236 |             multiplier = 1.0; | 
 | 3237 |         } else if (multiplier <= 2.0) { | 
 | 3238 |             if (2 * mFrameCount <= maxNormalFrameCount) { | 
 | 3239 |                 multiplier = 2.0; | 
 | 3240 |             } else { | 
 | 3241 |                 multiplier = (double) maxNormalFrameCount / (double) mFrameCount; | 
 | 3242 |             } | 
 | 3243 |         } else { | 
| Haynes Mathew George | 227a14b | 2016-05-09 12:45:48 -0700 | [diff] [blame] | 3244 |             multiplier = floor(multiplier); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3245 |         } | 
 | 3246 |     } | 
 | 3247 |     mNormalFrameCount = multiplier * mFrameCount; | 
 | 3248 |     // round up to nearest 16 frames to satisfy AudioMixer | 
| Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 3249 |     if (hasMixer()) { | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 3250 |         mNormalFrameCount = (mNormalFrameCount + 15) & ~15; | 
 | 3251 |     } | 
| Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 3252 |     ALOGI("HAL output buffer size %zu frames, normal sink buffer size %zu frames", | 
 | 3253 |             (size_t)mFrameCount, mNormalFrameCount); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3254 |  | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3255 |     // Check if we want to throttle the processing to no more than 2x normal rate | 
 | 3256 |     mThreadThrottle = property_get_bool("af.thread.throttle", true /* default_value */); | 
| Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3257 |     mThreadThrottleTimeMs = 0; | 
 | 3258 |     mThreadThrottleEndMs = 0; | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3259 |     mHalfBufferMs = mNormalFrameCount * 1000 / (2 * mSampleRate); | 
 | 3260 |  | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 3261 |     // mSinkBuffer is the sink buffer.  Size is always multiple-of-16 frames. | 
 | 3262 |     // Originally this was int16_t[] array, need to remove legacy implications. | 
 | 3263 |     free(mSinkBuffer); | 
 | 3264 |     mSinkBuffer = NULL; | 
| Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 3265 |  | 
| Andy Hung | 5b10a20 | 2014-03-13 13:59:29 -0700 | [diff] [blame] | 3266 |     // For sink buffer size, we use the frame size from the downstream sink to avoid problems | 
 | 3267 |     // with non PCM formats for compressed music, e.g. AAC, and Offload threads. | 
 | 3268 |     const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 3269 |     (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3270 |  | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3271 |     // We resize the mMixerBuffer according to the requirements of the sink buffer which | 
 | 3272 |     // drives the output. | 
 | 3273 |     free(mMixerBuffer); | 
 | 3274 |     mMixerBuffer = NULL; | 
 | 3275 |     if (mMixerBufferEnabled) { | 
| Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 3276 |         mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT; // no longer valid: AUDIO_FORMAT_PCM_16_BIT. | 
| Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 3277 |         mMixerBufferSize = mNormalFrameCount * mixerChannelCount | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3278 |                 * audio_bytes_per_sample(mMixerBufferFormat); | 
 | 3279 |         (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize); | 
 | 3280 |     } | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3281 |     free(mEffectBuffer); | 
 | 3282 |     mEffectBuffer = NULL; | 
 | 3283 |     if (mEffectBufferEnabled) { | 
| Andy Hung | 319587b | 2023-05-23 14:01:03 -0700 | [diff] [blame] | 3284 |         mEffectBufferFormat = AUDIO_FORMAT_PCM_FLOAT; | 
| Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 3285 |         mEffectBufferSize = mNormalFrameCount * mixerChannelCount | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3286 |                 * audio_bytes_per_sample(mEffectBufferFormat); | 
 | 3287 |         (void)posix_memalign(&mEffectBuffer, 32, mEffectBufferSize); | 
 | 3288 |     } | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3289 |  | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3290 |     if (mType == SPATIALIZER) { | 
 | 3291 |         free(mPostSpatializerBuffer); | 
 | 3292 |         mPostSpatializerBuffer = nullptr; | 
 | 3293 |         mPostSpatializerBufferSize = mNormalFrameCount * mChannelCount | 
 | 3294 |                 * audio_bytes_per_sample(mEffectBufferFormat); | 
 | 3295 |         (void)posix_memalign(&mPostSpatializerBuffer, 32, mPostSpatializerBufferSize); | 
 | 3296 |     } | 
 | 3297 |  | 
| Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 3298 |     mHapticChannelMask = static_cast<audio_channel_mask_t>(mChannelMask & AUDIO_CHANNEL_HAPTIC_ALL); | 
 | 3299 |     mChannelMask = static_cast<audio_channel_mask_t>(mChannelMask & ~mHapticChannelMask); | 
| jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 3300 |     mHapticChannelCount = audio_channel_count_from_out_mask(mHapticChannelMask); | 
 | 3301 |     mChannelCount -= mHapticChannelCount; | 
| Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 3302 |     mMixerChannelMask = static_cast<audio_channel_mask_t>(mMixerChannelMask & ~mHapticChannelMask); | 
| jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 3303 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3304 |     // force reconfiguration of effect chains and engines to take new buffer size and audio | 
 | 3305 |     // parameters into account | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 3306 |     // Note that mutex() is not held when readOutputParameters_l() is called from the constructor | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3307 |     // but in this case nothing is done below as no audio sessions have effect yet so it doesn't | 
 | 3308 |     // matter. | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3309 |     // create a copy of mEffectChains as calling moveEffectChain_ll() | 
 | 3310 |     // can reorder some effect chains | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 3311 |     Vector<sp<IAfEffectChain>> effectChains = mEffectChains; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3312 |     for (size_t i = 0; i < effectChains.size(); i ++) { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3313 |         mAfThreadCallback->moveEffectChain_ll(effectChains[i]->sessionId(), | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3314 |             this/* srcThread */, this/* dstThread */); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3315 |     } | 
| Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 3316 |  | 
| George Burgess IV | 5e4d86f | 2020-02-18 12:55:36 -0800 | [diff] [blame] | 3317 |     audio_output_flags_t flags = mOutput->flags; | 
| Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 3318 |     mediametrics::LogItem item(mThreadMetrics.getMetricsId()); // TODO: method in ThreadMetrics? | 
| Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 3319 |     item.set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_READPARAMETERS) | 
| Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 3320 |         .set(AMEDIAMETRICS_PROP_ENCODING, IAfThreadBase::formatToString(mFormat).c_str()) | 
| Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 3321 |         .set(AMEDIAMETRICS_PROP_SAMPLERATE, (int32_t)mSampleRate) | 
 | 3322 |         .set(AMEDIAMETRICS_PROP_CHANNELMASK, (int32_t)mChannelMask) | 
 | 3323 |         .set(AMEDIAMETRICS_PROP_CHANNELCOUNT, (int32_t)mChannelCount) | 
 | 3324 |         .set(AMEDIAMETRICS_PROP_FRAMECOUNT, (int32_t)mNormalFrameCount) | 
 | 3325 |         .set(AMEDIAMETRICS_PROP_FLAGS, toString(flags).c_str()) | 
 | 3326 |         .set(AMEDIAMETRICS_PROP_PREFIX_HAPTIC AMEDIAMETRICS_PROP_CHANNELMASK, | 
 | 3327 |                 (int32_t)mHapticChannelMask) | 
 | 3328 |         .set(AMEDIAMETRICS_PROP_PREFIX_HAPTIC AMEDIAMETRICS_PROP_CHANNELCOUNT, | 
 | 3329 |                 (int32_t)mHapticChannelCount) | 
 | 3330 |         .set(AMEDIAMETRICS_PROP_PREFIX_HAL    AMEDIAMETRICS_PROP_ENCODING, | 
| Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 3331 |                 IAfThreadBase::formatToString(mHALFormat).c_str()) | 
| Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 3332 |         .set(AMEDIAMETRICS_PROP_PREFIX_HAL    AMEDIAMETRICS_PROP_FRAMECOUNT, | 
 | 3333 |                 (int32_t)mFrameCount) // sic - added HAL | 
 | 3334 |         ; | 
 | 3335 |     uint32_t latencyMs; | 
 | 3336 |     if (mOutput->stream->getLatency(&latencyMs) == NO_ERROR) { | 
 | 3337 |         item.set(AMEDIAMETRICS_PROP_PREFIX_HAL AMEDIAMETRICS_PROP_LATENCYMS, (double)latencyMs); | 
 | 3338 |     } | 
 | 3339 |     item.record(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3340 | } | 
 | 3341 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3342 | ThreadBase::MetadataUpdate PlaybackThread::updateMetadata_l() | 
| Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 3343 | { | 
| Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 3344 |     if (!isStreamInitialized() || !mActiveTracks.readAndClearHasChanged()) { | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 3345 |         return {}; // nothing to do | 
| Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 3346 |     } | 
 | 3347 |     StreamOutHalInterface::SourceMetadata metadata; | 
| Nikhil Bhanu | 8f4ea77 | 2024-01-31 17:15:52 -0800 | [diff] [blame] | 3348 |     static const bool stereo_spatialization_property = | 
 | 3349 |             property_get_bool("ro.audio.stereo_spatialization_enabled", false); | 
 | 3350 |     const bool stereo_spatialization_enabled = | 
 | 3351 |             stereo_spatialization_property && com_android_media_audio_stereo_spatialization(); | 
 | 3352 |     if (stereo_spatialization_enabled) { | 
| Eric Laurent | 4eb45d0 | 2023-12-20 12:07:17 +0100 | [diff] [blame] | 3353 |         std::map<audio_session_t, std::vector<playback_track_metadata_v7_t> >allSessionsMetadata; | 
 | 3354 |         for (const sp<IAfTrack>& track : mActiveTracks) { | 
 | 3355 |             std::vector<playback_track_metadata_v7_t>& sessionMetadata = | 
 | 3356 |                     allSessionsMetadata[track->sessionId()]; | 
 | 3357 |             auto backInserter = std::back_inserter(sessionMetadata); | 
 | 3358 |             // No track is invalid as this is called after prepareTrack_l in the same | 
 | 3359 |             // critical section | 
 | 3360 |             track->copyMetadataTo(backInserter); | 
 | 3361 |         } | 
 | 3362 |         std::vector<playback_track_metadata_v7_t> spatializedTracksMetaData; | 
 | 3363 |         for (const auto& [session, sessionTrackMetadata] : allSessionsMetadata) { | 
 | 3364 |             metadata.tracks.insert(metadata.tracks.end(), | 
 | 3365 |                     sessionTrackMetadata.begin(), sessionTrackMetadata.end()); | 
 | 3366 |             if (auto chain = getEffectChain_l(session) ; chain != nullptr) { | 
 | 3367 |                 chain->sendMetadata_l(sessionTrackMetadata, {}); | 
 | 3368 |             } | 
 | 3369 |             if ((hasAudioSession_l(session) & IAfThreadBase::SPATIALIZED_SESSION) != 0) { | 
 | 3370 |                 spatializedTracksMetaData.insert(spatializedTracksMetaData.end(), | 
 | 3371 |                         sessionTrackMetadata.begin(), sessionTrackMetadata.end()); | 
 | 3372 |             } | 
 | 3373 |         } | 
 | 3374 |         if (auto chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); chain != nullptr) { | 
 | 3375 |             chain->sendMetadata_l(metadata.tracks, {}); | 
 | 3376 |         } | 
 | 3377 |         if (auto chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_STAGE); chain != nullptr) { | 
 | 3378 |             chain->sendMetadata_l(metadata.tracks, spatializedTracksMetaData); | 
 | 3379 |         } | 
 | 3380 |         if (auto chain = getEffectChain_l(AUDIO_SESSION_DEVICE); chain != nullptr) { | 
 | 3381 |             chain->sendMetadata_l(metadata.tracks, {}); | 
 | 3382 |         } | 
 | 3383 |     } else { | 
 | 3384 |         auto backInserter = std::back_inserter(metadata.tracks); | 
 | 3385 |         for (const sp<IAfTrack>& track : mActiveTracks) { | 
 | 3386 |             // No track is invalid as this is called after prepareTrack_l in the same | 
 | 3387 |             // critical section | 
 | 3388 |             track->copyMetadataTo(backInserter); | 
 | 3389 |         } | 
| Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 3390 |     } | 
| Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 3391 |     sendMetadataToBackend_l(metadata); | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 3392 |     MetadataUpdate change; | 
 | 3393 |     change.playbackMetadataUpdate = metadata.tracks; | 
 | 3394 |     return change; | 
| Kevin Rocard | 80ee272 | 2018-04-11 15:53:48 +0000 | [diff] [blame] | 3395 | } | 
| Kevin Rocard | c86a7f7 | 2018-04-03 09:00:09 -0700 | [diff] [blame] | 3396 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3397 | void PlaybackThread::sendMetadataToBackend_l( | 
| Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 3398 |         const StreamOutHalInterface::SourceMetadata& metadata) | 
 | 3399 | { | 
 | 3400 |     mOutput->stream->updateSourceMetadata(metadata); | 
 | 3401 | }; | 
 | 3402 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3403 | status_t PlaybackThread::getRenderPosition( | 
| Andy Hung | 3e4c874 | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 3404 |         uint32_t* halFrames, uint32_t* dspFrames) const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3405 | { | 
 | 3406 |     if (halFrames == NULL || dspFrames == NULL) { | 
 | 3407 |         return BAD_VALUE; | 
 | 3408 |     } | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3409 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3410 |     if (initCheck() != NO_ERROR) { | 
 | 3411 |         return INVALID_OPERATION; | 
 | 3412 |     } | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3413 |     int64_t framesWritten = mBytesWritten / mFrameSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3414 |     *halFrames = framesWritten; | 
 | 3415 |  | 
 | 3416 |     if (isSuspended()) { | 
 | 3417 |         // return an estimation of rendered frames when the output is suspended | 
 | 3418 |         size_t latencyFrames = (latency_l() * mSampleRate) / 1000; | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3419 |         *dspFrames = (uint32_t) | 
 | 3420 |                 (framesWritten >= (int64_t)latencyFrames ? framesWritten - latencyFrames : 0); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3421 |         return NO_ERROR; | 
 | 3422 |     } else { | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 3423 |         status_t status; | 
| Mikhail Naganov | 0ea58fe | 2024-05-10 13:30:40 -0700 | [diff] [blame] | 3424 |         uint64_t frames = 0; | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 3425 |         status = mOutput->getRenderPosition(&frames); | 
| Mikhail Naganov | 0ea58fe | 2024-05-10 13:30:40 -0700 | [diff] [blame] | 3426 |         *dspFrames = (uint32_t)frames; | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 3427 |         return status; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3428 |     } | 
 | 3429 | } | 
 | 3430 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3431 | product_strategy_t PlaybackThread::getStrategyForSession_l(audio_session_t sessionId) const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3432 | { | 
 | 3433 |     // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that | 
 | 3434 |     // it is moved to correct output by audio policy manager when A2DP is connected or disconnected | 
 | 3435 |     if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { | 
| Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 3436 |         return getStrategyForStream(AUDIO_STREAM_MUSIC); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3437 |     } | 
 | 3438 |     for (size_t i = 0; i < mTracks.size(); i++) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3439 |         sp<IAfTrack> track = mTracks[i]; | 
| Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 3440 |         if (sessionId == track->sessionId() && !track->isInvalid()) { | 
| Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 3441 |             return getStrategyForStream(track->streamType()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3442 |         } | 
 | 3443 |     } | 
| Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 3444 |     return getStrategyForStream(AUDIO_STREAM_MUSIC); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3445 | } | 
 | 3446 |  | 
 | 3447 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3448 | AudioStreamOut* PlaybackThread::getOutput() const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3449 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3450 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3451 |     return mOutput; | 
 | 3452 | } | 
 | 3453 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3454 | AudioStreamOut* PlaybackThread::clearOutput() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3455 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3456 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3457 |     AudioStreamOut *output = mOutput; | 
 | 3458 |     mOutput = NULL; | 
 | 3459 |     // FIXME FastMixer might also have a raw ptr to mOutputSink; | 
 | 3460 |     //       must push a NULL and wait for ack | 
 | 3461 |     mOutputSink.clear(); | 
 | 3462 |     mPipeSink.clear(); | 
 | 3463 |     mNormalSink.clear(); | 
 | 3464 |     return output; | 
 | 3465 | } | 
 | 3466 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 3467 | // this method must always be called either with ThreadBase mutex() held or inside the thread loop | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3468 | sp<StreamHalInterface> PlaybackThread::stream() const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3469 | { | 
 | 3470 |     if (mOutput == NULL) { | 
 | 3471 |         return NULL; | 
 | 3472 |     } | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3473 |     return mOutput->stream; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3474 | } | 
 | 3475 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3476 | uint32_t PlaybackThread::activeSleepTimeUs() const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3477 | { | 
 | 3478 |     return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000); | 
 | 3479 | } | 
 | 3480 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3481 | status_t PlaybackThread::setSyncEvent(const sp<SyncEvent>& event) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3482 | { | 
 | 3483 |     if (!isValidSyncEvent(event)) { | 
 | 3484 |         return BAD_VALUE; | 
 | 3485 |     } | 
 | 3486 |  | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3487 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3488 |  | 
 | 3489 |     for (size_t i = 0; i < mTracks.size(); ++i) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3490 |         sp<IAfTrack> track = mTracks[i]; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3491 |         if (event->triggerSession() == track->sessionId()) { | 
 | 3492 |             (void) track->setSyncEvent(event); | 
 | 3493 |             return NO_ERROR; | 
 | 3494 |         } | 
 | 3495 |     } | 
 | 3496 |  | 
 | 3497 |     return NAME_NOT_FOUND; | 
 | 3498 | } | 
 | 3499 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3500 | bool PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3501 | { | 
 | 3502 |     return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE; | 
 | 3503 | } | 
 | 3504 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3505 | void PlaybackThread::threadLoop_removeTracks( | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3506 |         [[maybe_unused]] const Vector<sp<IAfTrack>>& tracksToRemove) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3507 | { | 
| Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 3508 |     // Miscellaneous track cleanup when removed from the active list, | 
 | 3509 |     // called without Thread lock but synchronized with threadLoop processing. | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3510 | #ifdef ADD_BATTERY_DATA | 
| Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 3511 |     for (const auto& track : tracksToRemove) { | 
 | 3512 |         if (track->isExternalTrack()) { | 
 | 3513 |             // to track the speaker usage | 
 | 3514 |             addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3515 |         } | 
 | 3516 |     } | 
| Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 3517 | #endif | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3518 | } | 
 | 3519 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3520 | void PlaybackThread::checkSilentMode_l() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3521 | { | 
 | 3522 |     if (!mMasterMute) { | 
 | 3523 |         char value[PROPERTY_VALUE_MAX]; | 
| jiabin | 0a957d3 | 2020-04-29 10:56:20 -0700 | [diff] [blame] | 3524 |         if (mOutDeviceTypeAddrs.empty()) { | 
 | 3525 |             ALOGD("ro.audio.silent is ignored since no output device is set"); | 
 | 3526 |             return; | 
 | 3527 |         } | 
| Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 3528 |         if (isSingleDeviceType(outDeviceTypes_l(), AUDIO_DEVICE_OUT_REMOTE_SUBMIX)) { | 
| Jean-Michel Trivi | 32f37c2 | 2016-03-31 16:00:32 -0700 | [diff] [blame] | 3529 |             ALOGD("ro.audio.silent will be ignored for threads on AUDIO_DEVICE_OUT_REMOTE_SUBMIX"); | 
 | 3530 |             return; | 
 | 3531 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3532 |         if (property_get("ro.audio.silent", value, "0") > 0) { | 
 | 3533 |             char *endptr; | 
 | 3534 |             unsigned long ul = strtoul(value, &endptr, 0); | 
 | 3535 |             if (*endptr == '\0' && ul != 0) { | 
 | 3536 |                 ALOGD("Silence is golden"); | 
 | 3537 |                 // The setprop command will not allow a property to be changed after | 
 | 3538 |                 // the first time it is set, so we don't have to worry about un-muting. | 
 | 3539 |                 setMasterMute_l(true); | 
 | 3540 |             } | 
 | 3541 |         } | 
 | 3542 |     } | 
 | 3543 | } | 
 | 3544 |  | 
 | 3545 | // shared by MIXER and DIRECT, overridden by DUPLICATING | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3546 | ssize_t PlaybackThread::threadLoop_write() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3547 | { | 
| Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 3548 |     LOG_HIST_TS(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3549 |     mInWrite = true; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3550 |     ssize_t bytesWritten; | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 3551 |     const size_t offset = mCurrentWriteLength - mBytesRemaining; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3552 |  | 
 | 3553 |     // If an NBAIO sink is present, use it to write the normal mixer's submix | 
 | 3554 |     if (mNormalSink != 0) { | 
| Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 3555 |  | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 3556 |         const size_t count = mBytesRemaining / mFrameSize; | 
 | 3557 |  | 
| Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 3558 |         ATRACE_BEGIN("write"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3559 |         // update the setpoint when AudioFlinger::mScreenState changes | 
| Andy Hung | 1b6d46a | 2023-07-19 16:22:58 -0700 | [diff] [blame] | 3560 |         const uint32_t screenState = mAfThreadCallback->getScreenState(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3561 |         if (screenState != mScreenState) { | 
 | 3562 |             mScreenState = screenState; | 
 | 3563 |             MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); | 
 | 3564 |             if (pipe != NULL) { | 
 | 3565 |                 pipe->setAvgFrames((mScreenState & 1) ? | 
 | 3566 |                         (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); | 
 | 3567 |             } | 
 | 3568 |         } | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 3569 |         ssize_t framesWritten = mNormalSink->write((char *)mSinkBuffer + offset, count); | 
| Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 3570 |         ATRACE_END(); | 
| Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 3571 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3572 |         if (framesWritten > 0) { | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 3573 |             bytesWritten = framesWritten * mFrameSize; | 
| Andy Hung | 58e3287 | 2022-11-15 16:12:24 -0800 | [diff] [blame] | 3574 |  | 
| Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 3575 | #ifdef TEE_SINK | 
 | 3576 |             mTee.write((char *)mSinkBuffer + offset, framesWritten); | 
 | 3577 | #endif | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3578 |         } else { | 
 | 3579 |             bytesWritten = framesWritten; | 
 | 3580 |         } | 
 | 3581 |     // otherwise use the HAL / AudioStreamOut directly | 
 | 3582 |     } else { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3583 |         // Direct output and offload threads | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 3584 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3585 |         if (mUseAsyncWrite) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3586 |             ALOGW_IF(mWriteAckSequence & 1, "threadLoop_write(): out of sequence write request"); | 
 | 3587 |             mWriteAckSequence += 2; | 
 | 3588 |             mWriteAckSequence |= 1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3589 |             ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3590 |             mCallbackThread->setWriteBlocked(mWriteAckSequence); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3591 |         } | 
| Mikhail Naganov | ddb07bc | 2019-08-15 20:18:47 -0700 | [diff] [blame] | 3592 |         ATRACE_BEGIN("write"); | 
| Glenn Kasten | 767094d | 2013-08-23 13:51:43 -0700 | [diff] [blame] | 3593 |         // FIXME We should have an implementation of timestamps for direct output threads. | 
 | 3594 |         // They are used e.g for multichannel PCM playback over HDMI. | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 3595 |         bytesWritten = mOutput->write((char *)mSinkBuffer + offset, mBytesRemaining); | 
| Mikhail Naganov | ddb07bc | 2019-08-15 20:18:47 -0700 | [diff] [blame] | 3596 |         ATRACE_END(); | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 3597 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3598 |         if (mUseAsyncWrite && | 
 | 3599 |                 ((bytesWritten < 0) || (bytesWritten == (ssize_t)mBytesRemaining))) { | 
 | 3600 |             // 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] | 3601 |             mWriteAckSequence &= ~1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3602 |             ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3603 |             mCallbackThread->setWriteBlocked(mWriteAckSequence); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3604 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3605 |     } | 
 | 3606 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3607 |     mNumWrites++; | 
 | 3608 |     mInWrite = false; | 
| Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 3609 |     if (mStandby) { | 
 | 3610 |         mThreadMetrics.logBeginInterval(); | 
| Andy Hung | 44d648b | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 3611 |         mThreadSnapshot.onBegin(); | 
| Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 3612 |         mStandby = false; | 
 | 3613 |     } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3614 |     return bytesWritten; | 
 | 3615 | } | 
 | 3616 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 3617 | // startMelComputation_l() must be called with AudioFlinger::mutex() held | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3618 | void PlaybackThread::startMelComputation_l( | 
| Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 3619 |         const sp<audio_utils::MelProcessor>& processor) | 
| Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 3620 | { | 
| Vlad Popa | 3c7a266 | 2023-02-14 20:09:47 +0100 | [diff] [blame] | 3621 |     auto outputSink = static_cast<AudioStreamOutSink*>(mOutputSink.get()); | 
| Vlad Popa | 1a0c3ab | 2023-03-17 01:07:01 +0100 | [diff] [blame] | 3622 |     if (outputSink != nullptr) { | 
 | 3623 |         outputSink->startMelComputation(processor); | 
 | 3624 |     } | 
| Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 3625 | } | 
 | 3626 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 3627 | // stopMelComputation_l() must be called with AudioFlinger::mutex() held | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3628 | void PlaybackThread::stopMelComputation_l() | 
| Vlad Popa | 3c7a266 | 2023-02-14 20:09:47 +0100 | [diff] [blame] | 3629 | { | 
 | 3630 |     auto outputSink = static_cast<AudioStreamOutSink*>(mOutputSink.get()); | 
| Vlad Popa | 1a0c3ab | 2023-03-17 01:07:01 +0100 | [diff] [blame] | 3631 |     if (outputSink != nullptr) { | 
 | 3632 |         outputSink->stopMelComputation(); | 
 | 3633 |     } | 
| Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 3634 | } | 
 | 3635 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3636 | void PlaybackThread::threadLoop_drain() | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3637 | { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3638 |     bool supportsDrain = false; | 
 | 3639 |     if (mOutput->stream->supportsDrain(&supportsDrain) == OK && supportsDrain) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3640 |         ALOGV("draining %s", (mMixerStatus == MIXER_DRAIN_TRACK) ? "early" : "full"); | 
 | 3641 |         if (mUseAsyncWrite) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3642 |             ALOGW_IF(mDrainSequence & 1, "threadLoop_drain(): out of sequence drain request"); | 
 | 3643 |             mDrainSequence |= 1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3644 |             ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3645 |             mCallbackThread->setDraining(mDrainSequence); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3646 |         } | 
| Mikhail Naganov | cbc8f61 | 2016-10-11 18:05:13 -0700 | [diff] [blame] | 3647 |         status_t result = mOutput->stream->drain(mMixerStatus == MIXER_DRAIN_TRACK); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3648 |         ALOGE_IF(result != OK, "Error when draining stream: %d", result); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3649 |     } | 
 | 3650 | } | 
 | 3651 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3652 | void PlaybackThread::threadLoop_exit() | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3653 | { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3654 |     { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3655 |         audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3656 |         for (size_t i = 0; i < mTracks.size(); i++) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3657 |             sp<IAfTrack> track = mTracks[i]; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3658 |             track->invalidate(); | 
 | 3659 |         } | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 3660 |         // Clear ActiveTracks to update BatteryNotifier in case active tracks remain. | 
 | 3661 |         // After we exit there are no more track changes sent to BatteryNotifier | 
 | 3662 |         // because that requires an active threadLoop. | 
 | 3663 |         // TODO: should we decActiveTrackCnt() of the cleared track effect chain? | 
 | 3664 |         mActiveTracks.clear(); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3665 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3666 | } | 
 | 3667 |  | 
 | 3668 | /* | 
 | 3669 | The derived values that are cached: | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3670 |  - mSinkBufferSize from frame count * frame size | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3671 |  - mActiveSleepTimeUs from activeSleepTimeUs() | 
 | 3672 |  - mIdleSleepTimeUs from idleSleepTimeUs() | 
| Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 3673 |  - mStandbyDelayNs from mActiveSleepTimeUs (DIRECT only) or forced to at least | 
 | 3674 |    kDefaultStandbyTimeInNsecs when connected to an A2DP device. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3675 |  - maxPeriod from frame count and sample rate (MIXER only) | 
 | 3676 |  | 
 | 3677 | The parameters that affect these derived values are: | 
 | 3678 |  - frame count | 
 | 3679 |  - frame size | 
 | 3680 |  - sample rate | 
 | 3681 |  - device type: A2DP or not | 
 | 3682 |  - device latency | 
 | 3683 |  - format: PCM or not | 
 | 3684 |  - active sleep time | 
 | 3685 |  - idle sleep time | 
 | 3686 | */ | 
 | 3687 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3688 | void PlaybackThread::cacheParameters_l() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3689 | { | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3690 |     mSinkBufferSize = mNormalFrameCount * mFrameSize; | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3691 |     mActiveSleepTimeUs = activeSleepTimeUs(); | 
 | 3692 |     mIdleSleepTimeUs = idleSleepTimeUs(); | 
| Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 3693 |  | 
| Andy Hung | d58c473 | 2023-07-20 21:31:38 -0700 | [diff] [blame] | 3694 |     mStandbyDelayNs = getStandbyTimeInNanos(); | 
| Carter Hsu | 0ca47c2 | 2023-06-02 18:01:45 +0800 | [diff] [blame] | 3695 |  | 
| Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 3696 |     // make sure standby delay is not too short when connected to an A2DP sink to avoid | 
 | 3697 |     // truncating audio when going to standby. | 
| Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 3698 |     if (!Intersection(outDeviceTypes_l(),  getAudioDeviceOutAllA2dpSet()).empty()) { | 
| Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 3699 |         if (mStandbyDelayNs < kDefaultStandbyTimeInNsecs) { | 
 | 3700 |             mStandbyDelayNs = kDefaultStandbyTimeInNsecs; | 
 | 3701 |         } | 
 | 3702 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3703 | } | 
 | 3704 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3705 | bool PlaybackThread::invalidateTracks_l(audio_stream_type_t streamType) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3706 | { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3707 |     ALOGV("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %zu", | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3708 |             this,  streamType, mTracks.size()); | 
| Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 3709 |     bool trackMatch = false; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3710 |     size_t size = mTracks.size(); | 
 | 3711 |     for (size_t i = 0; i < size; i++) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3712 |         sp<IAfTrack> t = mTracks[i]; | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3713 |         if (t->streamType() == streamType && t->isExternalTrack()) { | 
| Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 3714 |             t->invalidate(); | 
| Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 3715 |             trackMatch = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3716 |         } | 
 | 3717 |     } | 
| Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 3718 |     return trackMatch; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3719 | } | 
 | 3720 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3721 | void PlaybackThread::invalidateTracks(audio_stream_type_t streamType) | 
| Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 3722 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3723 |     audio_utils::lock_guard _l(mutex()); | 
| Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 3724 |     invalidateTracks_l(streamType); | 
 | 3725 | } | 
 | 3726 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3727 | void PlaybackThread::invalidateTracks(std::set<audio_port_handle_t>& portIds) { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3728 |     audio_utils::lock_guard _l(mutex()); | 
| jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 3729 |     invalidateTracks_l(portIds); | 
 | 3730 | } | 
 | 3731 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3732 | bool PlaybackThread::invalidateTracks_l(std::set<audio_port_handle_t>& portIds) { | 
| jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 3733 |     bool trackMatch = false; | 
 | 3734 |     const size_t size = mTracks.size(); | 
 | 3735 |     for (size_t i = 0; i < size; i++) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3736 |         sp<IAfTrack> t = mTracks[i]; | 
| jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 3737 |         if (t->isExternalTrack() && portIds.find(t->portId()) != portIds.end()) { | 
 | 3738 |             t->invalidate(); | 
 | 3739 |             portIds.erase(t->portId()); | 
 | 3740 |             trackMatch = true; | 
 | 3741 |         } | 
 | 3742 |         if (portIds.empty()) { | 
 | 3743 |             break; | 
 | 3744 |         } | 
 | 3745 |     } | 
 | 3746 |     return trackMatch; | 
 | 3747 | } | 
 | 3748 |  | 
| jiabin | f042b9b | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 3749 | // getTrackById_l must be called with holding thread lock | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3750 | IAfTrack* PlaybackThread::getTrackById_l( | 
| jiabin | f042b9b | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 3751 |         audio_port_handle_t trackPortId) { | 
 | 3752 |     for (size_t i = 0; i < mTracks.size(); i++) { | 
 | 3753 |         if (mTracks[i]->portId() == trackPortId) { | 
 | 3754 |             return mTracks[i].get(); | 
 | 3755 |         } | 
 | 3756 |     } | 
 | 3757 |     return nullptr; | 
 | 3758 | } | 
 | 3759 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3760 | status_t PlaybackThread::addEffectChain_l(const sp<IAfEffectChain>& chain) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3761 | { | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3762 |     audio_session_t session = chain->sessionId(); | 
| Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 3763 |     sp<EffectBufferHalInterface> halInBuffer, halOutBuffer; | 
| Andy Hung | 319587b | 2023-05-23 14:01:03 -0700 | [diff] [blame] | 3764 |     float *buffer = nullptr; // only used for non global sessions | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3765 |  | 
| Andy Hung | d363992 | 2022-04-28 18:00:49 -0700 | [diff] [blame] | 3766 |     if (mType == SPATIALIZER) { | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3767 |         if (!audio_is_global_session(session)) { | 
 | 3768 |             // player sessions on a spatializer output will use a dedicated input buffer and | 
 | 3769 |             // will either output multi channel to mEffectBuffer if the track is spatilaized | 
 | 3770 |             // or stereo to mPostSpatializerBuffer if not spatialized. | 
 | 3771 |             uint32_t channelMask; | 
 | 3772 |             bool isSessionSpatialized = | 
 | 3773 |                 (hasAudioSession_l(session) & ThreadBase::SPATIALIZED_SESSION) != 0; | 
 | 3774 |             if (isSessionSpatialized) { | 
 | 3775 |                 channelMask = mMixerChannelMask; | 
 | 3776 |             } else { | 
 | 3777 |                 channelMask = mChannelMask; | 
 | 3778 |             } | 
| Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 3779 |             size_t numSamples = mNormalFrameCount | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3780 |                     * (audio_channel_count_from_out_mask(channelMask) + mHapticChannelCount); | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 3781 |             status_t result = mAfThreadCallback->getEffectsFactoryHal()->allocateBuffer( | 
| Andy Hung | 319587b | 2023-05-23 14:01:03 -0700 | [diff] [blame] | 3782 |                     numSamples * sizeof(float), | 
| Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 3783 |                     &halInBuffer); | 
 | 3784 |             if (result != OK) return result; | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3785 |  | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 3786 |             result = mAfThreadCallback->getEffectsFactoryHal()->mirrorBuffer( | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3787 |                     isSessionSpatialized ? mEffectBuffer : mPostSpatializerBuffer, | 
 | 3788 |                     isSessionSpatialized ? mEffectBufferSize : mPostSpatializerBufferSize, | 
 | 3789 |                     &halOutBuffer); | 
 | 3790 |             if (result != OK) return result; | 
 | 3791 |  | 
| Shunkai Yao | 44bdbad | 2023-01-14 05:11:58 +0000 | [diff] [blame] | 3792 |             buffer = halInBuffer ? halInBuffer->audioBuffer()->f32 : buffer; | 
| Andy Hung | 2683692 | 2023-05-22 17:31:57 -0700 | [diff] [blame] | 3793 |  | 
| Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 3794 |             ALOGV("addEffectChain_l() creating new input buffer %p session %d", | 
 | 3795 |                     buffer, session); | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3796 |         } else { | 
 | 3797 |             // A global session on a SPATIALIZER thread is either OUTPUT_STAGE or DEVICE | 
 | 3798 |             // - OUTPUT_STAGE session uses the mEffectBuffer as input buffer and | 
 | 3799 |             // mPostSpatializerBuffer as output buffer | 
 | 3800 |             // - DEVICE session uses the mPostSpatializerBuffer as input and output buffer. | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 3801 |             status_t result = mAfThreadCallback->getEffectsFactoryHal()->mirrorBuffer( | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3802 |                     mEffectBuffer, mEffectBufferSize, &halInBuffer); | 
 | 3803 |             if (result != OK) return result; | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 3804 |             result = mAfThreadCallback->getEffectsFactoryHal()->mirrorBuffer( | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3805 |                     mPostSpatializerBuffer, mPostSpatializerBufferSize, &halOutBuffer); | 
 | 3806 |             if (result != OK) return result; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3807 |  | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3808 |             if (session == AUDIO_SESSION_DEVICE) { | 
 | 3809 |                 halInBuffer = halOutBuffer; | 
 | 3810 |             } | 
 | 3811 |         } | 
 | 3812 |     } else { | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 3813 |         status_t result = mAfThreadCallback->getEffectsFactoryHal()->mirrorBuffer( | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3814 |                 mEffectBufferEnabled ? mEffectBuffer : mSinkBuffer, | 
 | 3815 |                 mEffectBufferEnabled ? mEffectBufferSize : mSinkBufferSize, | 
 | 3816 |                 &halInBuffer); | 
 | 3817 |         if (result != OK) return result; | 
 | 3818 |         halOutBuffer = halInBuffer; | 
 | 3819 |         ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); | 
 | 3820 |         if (!audio_is_global_session(session)) { | 
| Andy Hung | 319587b | 2023-05-23 14:01:03 -0700 | [diff] [blame] | 3821 |             buffer = halInBuffer ? reinterpret_cast<float*>(halInBuffer->externalData()) | 
| Shunkai Yao | 44bdbad | 2023-01-14 05:11:58 +0000 | [diff] [blame] | 3822 |                                  : buffer; | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3823 |             // Only one effect chain can be present in direct output thread and it uses | 
 | 3824 |             // the sink buffer as input | 
 | 3825 |             if (mType != DIRECT) { | 
 | 3826 |                 size_t numSamples = mNormalFrameCount | 
 | 3827 |                         * (audio_channel_count_from_out_mask(mMixerChannelMask) | 
 | 3828 |                                                              + mHapticChannelCount); | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 3829 |                 const status_t allocateStatus = | 
 | 3830 |                         mAfThreadCallback->getEffectsFactoryHal()->allocateBuffer( | 
| Andy Hung | 319587b | 2023-05-23 14:01:03 -0700 | [diff] [blame] | 3831 |                         numSamples * sizeof(float), | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3832 |                         &halInBuffer); | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 3833 |                 if (allocateStatus != OK) return allocateStatus; | 
| Andy Hung | 2683692 | 2023-05-22 17:31:57 -0700 | [diff] [blame] | 3834 |  | 
| Shunkai Yao | 44bdbad | 2023-01-14 05:11:58 +0000 | [diff] [blame] | 3835 |                 buffer = halInBuffer ? halInBuffer->audioBuffer()->f32 : buffer; | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3836 |                 ALOGV("addEffectChain_l() creating new input buffer %p session %d", | 
 | 3837 |                         buffer, session); | 
 | 3838 |             } | 
 | 3839 |         } | 
 | 3840 |     } | 
 | 3841 |  | 
 | 3842 |     if (!audio_is_global_session(session)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3843 |         // Attach all tracks with same session ID to this chain. | 
 | 3844 |         for (size_t i = 0; i < mTracks.size(); ++i) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3845 |             sp<IAfTrack> track = mTracks[i]; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3846 |             if (session == track->sessionId()) { | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3847 |                 ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", | 
 | 3848 |                         track.get(), buffer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3849 |                 track->setMainBuffer(buffer); | 
 | 3850 |                 chain->incTrackCnt(); | 
 | 3851 |             } | 
 | 3852 |         } | 
 | 3853 |  | 
 | 3854 |         // indicate all active tracks in the chain | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3855 |         for (const sp<IAfTrack>& track : mActiveTracks) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3856 |             if (session == track->sessionId()) { | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3857 |                 ALOGV("addEffectChain_l() activating track %p on session %d", | 
 | 3858 |                         track.get(), session); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3859 |                 chain->incActiveTrackCnt(); | 
 | 3860 |             } | 
 | 3861 |         } | 
 | 3862 |     } | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3863 |  | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 3864 |     chain->setThread(this); | 
| Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 3865 |     chain->setInBuffer(halInBuffer); | 
 | 3866 |     chain->setOutBuffer(halOutBuffer); | 
| Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 3867 |     // Effect chain for session AUDIO_SESSION_DEVICE is inserted at end of effect | 
 | 3868 |     // chains list in order to be processed last as it contains output device effects. | 
 | 3869 |     // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted just before to apply post | 
 | 3870 |     // processing effects specific to an output stream before effects applied to all streams | 
 | 3871 |     // routed to a given device. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3872 |     // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before | 
 | 3873 |     // session AUDIO_SESSION_OUTPUT_STAGE to be processed | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3874 |     // after track specific effects and before output stage. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3875 |     // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3876 |     // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3877 |     // Effect chain for other sessions are inserted at beginning of effect | 
 | 3878 |     // chains list to be processed before output mix effects. Relative order between other | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3879 |     // sessions is not important. | 
 | 3880 |     static_assert(AUDIO_SESSION_OUTPUT_MIX == 0 && | 
| Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 3881 |             AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX && | 
 | 3882 |             AUDIO_SESSION_DEVICE < AUDIO_SESSION_OUTPUT_STAGE, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3883 |             "audio_session_t constants misdefined"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3884 |     size_t size = mEffectChains.size(); | 
 | 3885 |     size_t i = 0; | 
 | 3886 |     for (i = 0; i < size; i++) { | 
 | 3887 |         if (mEffectChains[i]->sessionId() < session) { | 
 | 3888 |             break; | 
 | 3889 |         } | 
 | 3890 |     } | 
 | 3891 |     mEffectChains.insertAt(chain, i); | 
 | 3892 |     checkSuspendOnAddEffectChain_l(chain); | 
 | 3893 |  | 
 | 3894 |     return NO_ERROR; | 
 | 3895 | } | 
 | 3896 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3897 | size_t PlaybackThread::removeEffectChain_l(const sp<IAfEffectChain>& chain) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3898 | { | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3899 |     audio_session_t session = chain->sessionId(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3900 |  | 
 | 3901 |     ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); | 
 | 3902 |  | 
 | 3903 |     for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 3904 |         if (chain == mEffectChains[i]) { | 
 | 3905 |             mEffectChains.removeAt(i); | 
 | 3906 |             // detach all active tracks from the chain | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3907 |             for (const sp<IAfTrack>& track : mActiveTracks) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3908 |                 if (session == track->sessionId()) { | 
 | 3909 |                     ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", | 
 | 3910 |                             chain.get(), session); | 
 | 3911 |                     chain->decActiveTrackCnt(); | 
 | 3912 |                 } | 
 | 3913 |             } | 
 | 3914 |  | 
 | 3915 |             // detach all tracks with same session ID from this chain | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 3916 |             for (size_t j = 0; j < mTracks.size(); ++j) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3917 |                 sp<IAfTrack> track = mTracks[j]; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3918 |                 if (session == track->sessionId()) { | 
| Andy Hung | 319587b | 2023-05-23 14:01:03 -0700 | [diff] [blame] | 3919 |                     track->setMainBuffer(reinterpret_cast<float*>(mSinkBuffer)); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3920 |                     chain->decTrackCnt(); | 
 | 3921 |                 } | 
 | 3922 |             } | 
 | 3923 |             break; | 
 | 3924 |         } | 
 | 3925 |     } | 
 | 3926 |     return mEffectChains.size(); | 
 | 3927 | } | 
 | 3928 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3929 | status_t PlaybackThread::attachAuxEffect( | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3930 |         const sp<IAfTrack>& track, int EffectId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3931 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3932 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3933 |     return attachAuxEffect_l(track, EffectId); | 
 | 3934 | } | 
 | 3935 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3936 | status_t PlaybackThread::attachAuxEffect_l( | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3937 |         const sp<IAfTrack>& track, int EffectId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3938 | { | 
 | 3939 |     status_t status = NO_ERROR; | 
 | 3940 |  | 
 | 3941 |     if (EffectId == 0) { | 
 | 3942 |         track->setAuxBuffer(0, NULL); | 
 | 3943 |     } else { | 
 | 3944 |         // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 3945 |         sp<IAfEffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3946 |         if (effect != 0) { | 
 | 3947 |             if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
 | 3948 |                 track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); | 
 | 3949 |             } else { | 
 | 3950 |                 status = INVALID_OPERATION; | 
 | 3951 |             } | 
 | 3952 |         } else { | 
 | 3953 |             status = BAD_VALUE; | 
 | 3954 |         } | 
 | 3955 |     } | 
 | 3956 |     return status; | 
 | 3957 | } | 
 | 3958 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3959 | void PlaybackThread::detachAuxEffect_l(int effectId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3960 | { | 
 | 3961 |     for (size_t i = 0; i < mTracks.size(); ++i) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3962 |         sp<IAfTrack> track = mTracks[i]; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3963 |         if (track->auxEffectId() == effectId) { | 
 | 3964 |             attachAuxEffect_l(track, 0); | 
 | 3965 |         } | 
 | 3966 |     } | 
 | 3967 | } | 
 | 3968 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3969 | bool PlaybackThread::threadLoop() | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 3970 | NO_THREAD_SAFETY_ANALYSIS  // manual locking of AudioFlinger | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3971 | { | 
| Andy Hung | 78d8d95 | 2023-05-30 18:10:23 -0700 | [diff] [blame] | 3972 |     aflog::setThreadWriter(mNBLogWriter.get()); | 
| Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 3973 |  | 
| Andy Hung | 45a38f2 | 2023-10-03 10:49:34 -0700 | [diff] [blame] | 3974 |     if (mType == SPATIALIZER) { | 
 | 3975 |         const pid_t tid = getTid(); | 
 | 3976 |         if (tid == -1) {  // odd: we are here, we must be a running thread. | 
 | 3977 |             ALOGW("%s: Cannot update Spatializer mixer thread priority, no tid", __func__); | 
 | 3978 |         } else { | 
 | 3979 |             const int priorityBoost = requestSpatializerPriority(getpid(), tid); | 
 | 3980 |             if (priorityBoost > 0) { | 
 | 3981 |                 stream()->setHalThreadPriority(priorityBoost); | 
 | 3982 |             } | 
 | 3983 |         } | 
| Pattara Teerapong | 9a332c5 | 2024-01-26 08:18:05 +0000 | [diff] [blame] | 3984 |     } else if (property_get_bool("ro.boot.container", false /* default_value */)) { | 
 | 3985 |         // In ARC experiments (b/73091832), the latency under using CFS scheduler with any priority | 
 | 3986 |         // is not enough for PlaybackThread to process audio data in time. We request the lowest | 
 | 3987 |         // real-time priority, SCHED_FIFO=1, for PlaybackThread in ARC. ro.boot.container is true | 
 | 3988 |         // only on ARC. | 
 | 3989 |         const pid_t tid = getTid(); | 
 | 3990 |         if (tid == -1) { | 
 | 3991 |             ALOGW("%s: Cannot update PlaybackThread priority for ARC, no tid", __func__); | 
 | 3992 |         } else { | 
 | 3993 |             const status_t status = requestPriority(getpid(), | 
 | 3994 |                                                     tid, | 
 | 3995 |                                                     kPriorityPlaybackThreadArc, | 
 | 3996 |                                                     false /* isForApp */, | 
 | 3997 |                                                     true /* asynchronous */); | 
 | 3998 |             if (status != OK) { | 
 | 3999 |                 ALOGW("%s: Cannot update PlaybackThread priority for ARC, status %d", __func__, | 
 | 4000 |                         status); | 
 | 4001 |             } else { | 
 | 4002 |                 stream()->setHalThreadPriority(kPriorityPlaybackThreadArc); | 
 | 4003 |             } | 
 | 4004 |         } | 
| Andy Hung | 45a38f2 | 2023-10-03 10:49:34 -0700 | [diff] [blame] | 4005 |     } | 
 | 4006 |  | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 4007 |     Vector<sp<IAfTrack>> tracksToRemove; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4008 |  | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4009 |     mStandbyTimeNs = systemTime(); | 
| Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4010 |     int64_t lastLoopCountWritten = -2; // never matches "previous" loop, when loopCount = 0. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4011 |  | 
 | 4012 |     // MIXER | 
 | 4013 |     nsecs_t lastWarning = 0; | 
 | 4014 |  | 
 | 4015 |     // DUPLICATING | 
 | 4016 |     // FIXME could this be made local to while loop? | 
 | 4017 |     writeFrames = 0; | 
 | 4018 |  | 
 | 4019 |     cacheParameters_l(); | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4020 |     mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4021 |  | 
| Andy Hung | d363992 | 2022-04-28 18:00:49 -0700 | [diff] [blame] | 4022 |     if (mType == MIXER || mType == SPATIALIZER) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4023 |         sleepTimeShift = 0; | 
 | 4024 |     } | 
 | 4025 |  | 
 | 4026 |     CpuStats cpuStats; | 
 | 4027 |     const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); | 
 | 4028 |  | 
 | 4029 |     acquireWakeLock(); | 
 | 4030 |  | 
| Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 4031 |     // mNBLogWriter logging APIs can only be called by a single thread, typically the | 
 | 4032 |     // thread associated with this PlaybackThread. | 
 | 4033 |     // If you want to share the mNBLogWriter with other threads (for example, binder threads) | 
 | 4034 |     // then all such threads must agree to hold a common mutex before logging. | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 4035 |     // So if you need to log when mutex is unlocked, set logString to a non-NULL string, | 
 | 4036 |     // and then that string will be logged at the next convenient opportunity. | 
| Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 4037 |     // See reference to logString below. | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 4038 |     const char *logString = NULL; | 
 | 4039 |  | 
| rago | 1bb9082 | 2017-05-02 18:31:48 -0700 | [diff] [blame] | 4040 |     // Estimated time for next buffer to be written to hal. This is used only on | 
 | 4041 |     // suspended mode (for now) to help schedule the wait time until next iteration. | 
 | 4042 |     nsecs_t timeLoopNextNs = 0; | 
 | 4043 |  | 
| Eric Laurent | 664539d | 2013-09-23 18:24:31 -0700 | [diff] [blame] | 4044 |     checkSilentMode_l(); | 
| Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 4045 |  | 
| Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4046 |     audio_patch_handle_t lastDownstreamPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
| Andy Hung | f323451 | 2018-07-03 14:51:47 -0700 | [diff] [blame] | 4047 |  | 
| Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 4048 |     sendCheckOutputStageEffectsEvent(); | 
 | 4049 |  | 
| Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4050 |     // loopCount is used for statistics and diagnostics. | 
 | 4051 |     for (int64_t loopCount = 0; !exitPending(); ++loopCount) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4052 |     { | 
| Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 4053 |         // Log merge requests are performed during AudioFlinger binder transactions, but | 
 | 4054 |         // that does not cover audio playback. It's requested here for that reason. | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 4055 |         mAfThreadCallback->requestLogMerge(); | 
| Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 4056 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4057 |         cpuStats.sample(myName); | 
 | 4058 |  | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 4059 |         Vector<sp<IAfEffectChain>> effectChains; | 
| Andy Hung | 6e6a2e6 | 2019-04-30 16:38:41 -0700 | [diff] [blame] | 4060 |         audio_session_t activeHapticSessionId = AUDIO_SESSION_NONE; | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4061 |         bool isHapticSessionSpatialized = false; | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 4062 |         std::vector<sp<IAfTrack>> activeTracks; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4063 |  | 
| Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4064 |         // If the device is AUDIO_DEVICE_OUT_BUS, check for downstream latency. | 
 | 4065 |         // | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4066 |         // Note: we access outDeviceTypes() outside of mutex(). | 
| Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 4067 |         if (isMsdDevice() && outDeviceTypes_l().count(AUDIO_DEVICE_OUT_BUS) != 0) { | 
| Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4068 |             // Here, we try for the AF lock, but do not block on it as the latency | 
 | 4069 |             // is more informational. | 
| Andy Hung | 85a0745 | 2023-08-28 18:36:53 -0700 | [diff] [blame] | 4070 |             if (mAfThreadCallback->mutex().try_lock()) { | 
| Andy Hung | d25fe39 | 2023-07-13 16:52:46 -0700 | [diff] [blame] | 4071 |                 std::vector<SoftwarePatch> swPatches; | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 4072 |                 double latencyMs = 0.; // not required; initialized for clang-tidy | 
| Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4073 |                 status_t status = INVALID_OPERATION; | 
 | 4074 |                 audio_patch_handle_t downstreamPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 4075 |                 if (mAfThreadCallback->getPatchPanel()->getDownstreamSoftwarePatches( | 
| Andy Hung | d25fe39 | 2023-07-13 16:52:46 -0700 | [diff] [blame] | 4076 |                                 id(), &swPatches) == OK | 
| Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4077 |                         && swPatches.size() > 0) { | 
 | 4078 |                         status = swPatches[0].getLatencyMs_l(&latencyMs); | 
 | 4079 |                         downstreamPatchHandle = swPatches[0].getPatchHandle(); | 
 | 4080 |                 } | 
 | 4081 |                 if (downstreamPatchHandle != lastDownstreamPatchHandle) { | 
| Dean Wheatley | 30d2842 | 2018-11-06 10:27:40 +1100 | [diff] [blame] | 4082 |                     mDownstreamLatencyStatMs.reset(); | 
| Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4083 |                     lastDownstreamPatchHandle = downstreamPatchHandle; | 
 | 4084 |                 } | 
 | 4085 |                 if (status == OK) { | 
 | 4086 |                     // verify downstream latency (we assume a max reasonable | 
| Mikhail Naganov | 6aa0a31 | 2018-11-09 11:00:01 -0800 | [diff] [blame] | 4087 |                     // latency of 5 seconds). | 
 | 4088 |                     const double minLatency = 0., maxLatency = 5000.; | 
 | 4089 |                     if (latencyMs >= minLatency && latencyMs <= maxLatency) { | 
| Dean Wheatley | 56a583e | 2020-05-08 15:12:17 +1000 | [diff] [blame] | 4090 |                         ALOGVV("new downstream latency %lf ms", latencyMs); | 
| Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4091 |                     } else { | 
 | 4092 |                         ALOGD("out of range downstream latency %lf ms", latencyMs); | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 4093 |                         latencyMs = std::clamp(latencyMs, minLatency, maxLatency); | 
| Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4094 |                     } | 
| Dean Wheatley | 30d2842 | 2018-11-06 10:27:40 +1100 | [diff] [blame] | 4095 |                     mDownstreamLatencyStatMs.add(latencyMs); | 
| Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4096 |                 } | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 4097 |                 mAfThreadCallback->mutex().unlock(); | 
| Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4098 |             } | 
 | 4099 |         } else { | 
 | 4100 |             if (lastDownstreamPatchHandle != AUDIO_PATCH_HANDLE_NONE) { | 
 | 4101 |                 // our device is no longer AUDIO_DEVICE_OUT_BUS, reset patch handle and stats. | 
| Dean Wheatley | 30d2842 | 2018-11-06 10:27:40 +1100 | [diff] [blame] | 4102 |                 mDownstreamLatencyStatMs.reset(); | 
| Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4103 |                 lastDownstreamPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
 | 4104 |             } | 
 | 4105 |         } | 
 | 4106 |  | 
| Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 4107 |         if (mCheckOutputStageEffects.exchange(false)) { | 
 | 4108 |             checkOutputStageEffects(); | 
 | 4109 |         } | 
 | 4110 |  | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 4111 |         MetadataUpdate metadataUpdate; | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4112 |         { // scope for mutex() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4113 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4114 |             audio_utils::unique_lock _l(mutex()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4115 |  | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 4116 |             processConfigEvents_l(); | 
| Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 4117 |             if (mCheckOutputStageEffects.load()) { | 
 | 4118 |                 continue; | 
 | 4119 |             } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4120 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4121 |             // See comment at declaration of logString for why this is done under mutex() | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 4122 |             if (logString != NULL) { | 
 | 4123 |                 mNBLogWriter->logTimestamp(); | 
 | 4124 |                 mNBLogWriter->log(logString); | 
 | 4125 |                 logString = NULL; | 
 | 4126 |             } | 
 | 4127 |  | 
| Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 4128 |             collectTimestamps_l(); | 
| Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 4129 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4130 |             saveOutputTracks(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4131 |             if (mSignalPending) { | 
 | 4132 |                 // A signal was raised while we were unlocked | 
 | 4133 |                 mSignalPending = false; | 
 | 4134 |             } else if (waitingAsyncCallback_l()) { | 
 | 4135 |                 if (exitPending()) { | 
 | 4136 |                     break; | 
 | 4137 |                 } | 
| Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 4138 |                 bool released = false; | 
| Eric Laurent | 6466797 | 2016-03-30 18:19:46 -0700 | [diff] [blame] | 4139 |                 if (!keepWakeLock()) { | 
| Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 4140 |                     releaseWakeLock_l(); | 
 | 4141 |                     released = true; | 
 | 4142 |                 } | 
| Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 4143 |  | 
 | 4144 |                 const int64_t waitNs = computeWaitTimeNs_l(); | 
 | 4145 |                 ALOGV("wait async completion (wait time: %lld)", (long long)waitNs); | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4146 |                 std::cv_status cvstatus = | 
 | 4147 |                         mWaitWorkCV.wait_for(_l, std::chrono::nanoseconds(waitNs)); | 
 | 4148 |                 if (cvstatus == std::cv_status::timeout) { | 
| Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 4149 |                     mSignalPending = true; // if timeout recheck everything | 
 | 4150 |                 } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4151 |                 ALOGV("async completion/wake"); | 
| Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 4152 |                 if (released) { | 
 | 4153 |                     acquireWakeLock_l(); | 
 | 4154 |                 } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4155 |                 mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
 | 4156 |                 mSleepTimeUs = 0; | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 4157 |  | 
 | 4158 |                 continue; | 
 | 4159 |             } | 
| Eric Tan | 39ec8d6 | 2018-07-24 09:49:29 -0700 | [diff] [blame] | 4160 |             if ((mActiveTracks.isEmpty() && systemTime() > mStandbyTimeNs) || | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4161 |                                    isSuspended()) { | 
 | 4162 |                 // put audio hardware into standby after short delay | 
 | 4163 |                 if (shouldStandby_l()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4164 |  | 
 | 4165 |                     threadLoop_standby(); | 
 | 4166 |  | 
| Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 4167 |                     // This is where we go into standby | 
 | 4168 |                     if (!mStandby) { | 
 | 4169 |                         LOG_AUDIO_STATE(); | 
| Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 4170 |                         mThreadMetrics.logEndInterval(); | 
| Andy Hung | 44d648b | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 4171 |                         mThreadSnapshot.onEnd(); | 
| Eric Laurent | 19952e1 | 2023-04-20 10:08:29 +0200 | [diff] [blame] | 4172 |                         setStandby_l(); | 
| Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 4173 |                     } | 
| Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 4174 |                     sendStatistics(false /* force */); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4175 |                 } | 
 | 4176 |  | 
| Eric Tan | 39ec8d6 | 2018-07-24 09:49:29 -0700 | [diff] [blame] | 4177 |                 if (mActiveTracks.isEmpty() && mConfigEvents.isEmpty()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4178 |                     // we're about to wait, flush the binder command buffer | 
 | 4179 |                     IPCThreadState::self()->flushCommands(); | 
 | 4180 |  | 
 | 4181 |                     clearOutputTracks(); | 
 | 4182 |  | 
 | 4183 |                     if (exitPending()) { | 
 | 4184 |                         break; | 
 | 4185 |                     } | 
 | 4186 |  | 
 | 4187 |                     releaseWakeLock_l(); | 
 | 4188 |                     // wait until we have something to do... | 
| Tomasz Wasilczyk | 5b05437 | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 4189 |                     ALOGV("%s going to sleep", myName.c_str()); | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4190 |                     mWaitWorkCV.wait(_l); | 
| Tomasz Wasilczyk | 5b05437 | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 4191 |                     ALOGV("%s waking up", myName.c_str()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4192 |                     acquireWakeLock_l(); | 
 | 4193 |  | 
 | 4194 |                     mMixerStatus = MIXER_IDLE; | 
 | 4195 |                     mMixerStatusIgnoringFastTracks = MIXER_IDLE; | 
 | 4196 |                     mBytesWritten = 0; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4197 |                     mBytesRemaining = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4198 |                     checkSilentMode_l(); | 
 | 4199 |  | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4200 |                     mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
 | 4201 |                     mSleepTimeUs = mIdleSleepTimeUs; | 
| Andy Hung | d363992 | 2022-04-28 18:00:49 -0700 | [diff] [blame] | 4202 |                     if (mType == MIXER || mType == SPATIALIZER) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4203 |                         sleepTimeShift = 0; | 
 | 4204 |                     } | 
 | 4205 |  | 
 | 4206 |                     continue; | 
 | 4207 |                 } | 
 | 4208 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4209 |             // mMixerStatusIgnoringFastTracks is also updated internally | 
 | 4210 |             mMixerStatus = prepareTracks_l(&tracksToRemove); | 
 | 4211 |  | 
| Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 4212 |             mActiveTracks.updatePowerState_l(this); | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 4213 |  | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 4214 |             metadataUpdate = updateMetadata_l(); | 
| Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 4215 |  | 
| Andy Hung | f302e81 | 2024-01-26 11:55:15 -0800 | [diff] [blame] | 4216 |             // Acquire a local copy of active tracks with lock (release w/o lock). | 
 | 4217 |             // | 
 | 4218 |             // Control methods on the track acquire the ThreadBase lock (e.g. start() | 
 | 4219 |             // stop(), pause(), etc.), but the threadLoop is entitled to call audio | 
 | 4220 |             // data / buffer methods on tracks from activeTracks without the ThreadBase lock. | 
 | 4221 |             activeTracks.insert(activeTracks.end(), mActiveTracks.begin(), mActiveTracks.end()); | 
 | 4222 |  | 
 | 4223 |             setHalLatencyMode_l(); | 
 | 4224 |  | 
 | 4225 |             // updateTeePatches_l will acquire the ThreadBase_Mutex of other threads, | 
 | 4226 |             // so this is done before we lock our effect chains. | 
 | 4227 |             for (const auto& track : mActiveTracks) { | 
 | 4228 |                 track->updateTeePatches_l(); | 
 | 4229 |             } | 
 | 4230 |  | 
 | 4231 |             // signal actual start of output stream when the render position reported by | 
 | 4232 |             // the kernel starts moving. | 
 | 4233 |             if (!mHalStarted && ((isSuspended() && (mBytesWritten != 0)) || (!mStandby | 
 | 4234 |                     && (mKernelPositionOnStandby | 
 | 4235 |                             != mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL])))) { | 
 | 4236 |                 mHalStarted = true; | 
 | 4237 |                 mWaitHalStartCV.notify_all(); | 
 | 4238 |             } | 
 | 4239 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4240 |             // prevent any changes in effect chain list and in each effect chain | 
 | 4241 |             // during mixing and effect process as the audio buffers could be deleted | 
 | 4242 |             // or modified if an effect is created or deleted | 
 | 4243 |             lockEffectChains_l(effectChains); | 
| Andy Hung | 6e6a2e6 | 2019-04-30 16:38:41 -0700 | [diff] [blame] | 4244 |  | 
 | 4245 |             // Determine which session to pick up haptic data. | 
 | 4246 |             // This must be done under the same lock as prepareTracks_l(). | 
| jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 4247 |             // The haptic data from the effect is at a higher priority than the one from track. | 
| Andy Hung | 6e6a2e6 | 2019-04-30 16:38:41 -0700 | [diff] [blame] | 4248 |             // TODO: Write haptic data directly to sink buffer when mixing. | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4249 |             if (mHapticChannelCount > 0) { | 
| Andy Hung | 6e6a2e6 | 2019-04-30 16:38:41 -0700 | [diff] [blame] | 4250 |                 for (const auto& track : mActiveTracks) { | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 4251 |                     sp<IAfEffectChain> effectChain = getEffectChain_l(track->sessionId()); | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4252 |                     if (effectChain != nullptr | 
 | 4253 |                             && effectChain->containsHapticGeneratingEffect_l()) { | 
| jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 4254 |                         activeHapticSessionId = track->sessionId(); | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4255 |                         isHapticSessionSpatialized = | 
| Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 4256 |                                 mType == SPATIALIZER && track->isSpatialized(); | 
| jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 4257 |                         break; | 
 | 4258 |                     } | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4259 |                     if (activeHapticSessionId == AUDIO_SESSION_NONE | 
 | 4260 |                             && track->getHapticPlaybackEnabled()) { | 
| Andy Hung | 6e6a2e6 | 2019-04-30 16:38:41 -0700 | [diff] [blame] | 4261 |                         activeHapticSessionId = track->sessionId(); | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4262 |                         isHapticSessionSpatialized = | 
| Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 4263 |                                 mType == SPATIALIZER && track->isSpatialized(); | 
| Andy Hung | 6e6a2e6 | 2019-04-30 16:38:41 -0700 | [diff] [blame] | 4264 |                     } | 
 | 4265 |                 } | 
 | 4266 |             } | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4267 |         } // mutex() scope ends | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4268 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4269 |         if (mBytesRemaining == 0) { | 
 | 4270 |             mCurrentWriteLength = 0; | 
 | 4271 |             if (mMixerStatus == MIXER_TRACKS_READY) { | 
 | 4272 |                 // threadLoop_mix() sets mCurrentWriteLength | 
 | 4273 |                 threadLoop_mix(); | 
 | 4274 |             } else if ((mMixerStatus != MIXER_DRAIN_TRACK) | 
 | 4275 |                         && (mMixerStatus != MIXER_DRAIN_ALL)) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4276 |                 // threadLoop_sleepTime sets mSleepTimeUs to 0 if data | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4277 |                 // must be written to HAL | 
 | 4278 |                 threadLoop_sleepTime(); | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4279 |                 if (mSleepTimeUs == 0) { | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 4280 |                     mCurrentWriteLength = mSinkBufferSize; | 
| Andy Hung | c164638 | 2019-04-30 16:12:10 -0700 | [diff] [blame] | 4281 |  | 
 | 4282 |                     // Tally underrun frames as we are inserting 0s here. | 
 | 4283 |                     for (const auto& track : activeTracks) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 4284 |                         if (track->fillingStatus() == IAfTrack::FS_ACTIVE | 
| Andy Hung | e2e830f | 2019-12-03 12:54:46 -0800 | [diff] [blame] | 4285 |                                 && !track->isStopped() | 
 | 4286 |                                 && !track->isPaused() | 
 | 4287 |                                 && !track->isTerminated()) { | 
 | 4288 |                             ALOGV("%s: track(%d) %s underrun due to thread sleep of %zu frames", | 
 | 4289 |                                     __func__, track->id(), track->getTrackStateAsString(), | 
 | 4290 |                                     mNormalFrameCount); | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 4291 |                             track->audioTrackServerProxy()->tallyUnderrunFrames( | 
 | 4292 |                                     mNormalFrameCount); | 
| Andy Hung | c164638 | 2019-04-30 16:12:10 -0700 | [diff] [blame] | 4293 |                         } | 
 | 4294 |                     } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4295 |                 } | 
 | 4296 |             } | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4297 |             // Either threadLoop_mix() or threadLoop_sleepTime() should have set | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4298 |             // mMixerBuffer with data if mMixerBufferValid is true and mSleepTimeUs == 0. | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4299 |             // Merge mMixerBuffer data into mEffectBuffer (if any effects are valid) | 
| jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 4300 |             // or mSinkBuffer (if there are no effects and there is no data already copied to | 
 | 4301 |             // mSinkBuffer). | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4302 |             // | 
 | 4303 |             // This is done pre-effects computation; if effects change to | 
 | 4304 |             // support higher precision, this needs to move. | 
 | 4305 |             // | 
 | 4306 |             // mMixerBufferValid is only set true by MixerThread::prepareTracks_l(). | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4307 |             // TODO use mSleepTimeUs == 0 as an additional condition. | 
| Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 4308 |             uint32_t mixerChannelCount = mEffectBufferValid ? | 
 | 4309 |                         audio_channel_count_from_out_mask(mMixerChannelMask) : mChannelCount; | 
| jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 4310 |             if (mMixerBufferValid && (mEffectBufferValid || !mHasDataCopiedToSinkBuffer)) { | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4311 |                 void *buffer = mEffectBufferValid ? mEffectBuffer : mSinkBuffer; | 
 | 4312 |                 audio_format_t format = mEffectBufferValid ? mEffectBufferFormat : mFormat; | 
 | 4313 |  | 
| David Li | 88ee090 | 2022-06-22 10:01:21 +0800 | [diff] [blame] | 4314 |                 // Apply mono blending and balancing if the effect buffer is not valid. Otherwise, | 
 | 4315 |                 // do these processes after effects are applied. | 
 | 4316 |                 if (!mEffectBufferValid) { | 
 | 4317 |                     // mono blend occurs for mixer threads only (not direct or offloaded) | 
 | 4318 |                     // and is handled here if we're going directly to the sink. | 
 | 4319 |                     if (requireMonoBlend()) { | 
 | 4320 |                         mono_blend(mMixerBuffer, mMixerBufferFormat, mChannelCount, | 
 | 4321 |                                 mNormalFrameCount, true /*limit*/); | 
 | 4322 |                     } | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4323 |  | 
| David Li | 88ee090 | 2022-06-22 10:01:21 +0800 | [diff] [blame] | 4324 |                     if (!hasFastMixer()) { | 
 | 4325 |                         // Balance must take effect after mono conversion. | 
 | 4326 |                         // We do it here if there is no FastMixer. | 
 | 4327 |                         // mBalance detects zero balance within the class for speed | 
 | 4328 |                         // (not needed here). | 
 | 4329 |                         mBalance.setBalance(mMasterBalance.load()); | 
 | 4330 |                         mBalance.process((float *)mMixerBuffer, mNormalFrameCount); | 
 | 4331 |                     } | 
| Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 4332 |                 } | 
 | 4333 |  | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4334 |                 memcpy_by_audio_format(buffer, format, mMixerBuffer, mMixerBufferFormat, | 
| Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 4335 |                         mNormalFrameCount * (mixerChannelCount + mHapticChannelCount)); | 
| jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 4336 |  | 
 | 4337 |                 // If we're going directly to the sink and there are haptic channels, | 
 | 4338 |                 // we should adjust channels as the sample data is partially interleaved | 
 | 4339 |                 // in this case. | 
 | 4340 |                 if (!mEffectBufferValid && mHapticChannelCount > 0) { | 
 | 4341 |                     adjust_channels_non_destructive(buffer, mChannelCount, buffer, | 
 | 4342 |                             mChannelCount + mHapticChannelCount, | 
 | 4343 |                             audio_bytes_per_sample(format), | 
 | 4344 |                             audio_bytes_per_frame(mChannelCount, format) * mNormalFrameCount); | 
 | 4345 |                 } | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4346 |             } | 
 | 4347 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4348 |             mBytesRemaining = mCurrentWriteLength; | 
 | 4349 |             if (isSuspended()) { | 
| Andy Hung | 238fa3d | 2016-07-28 10:53:22 -0700 | [diff] [blame] | 4350 |                 // Simulate write to HAL when suspended (e.g. BT SCO phone call). | 
 | 4351 |                 mSleepTimeUs = suspendSleepTimeUs(); // assumes full buffer. | 
 | 4352 |                 const size_t framesRemaining = mBytesRemaining / mFrameSize; | 
 | 4353 |                 mBytesWritten += mBytesRemaining; | 
 | 4354 |                 mFramesWritten += framesRemaining; | 
 | 4355 |                 mSuspendedFrames += framesRemaining; // to adjust kernel HAL position | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4356 |                 mBytesRemaining = 0; | 
 | 4357 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4358 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4359 |             // only process effects if we're going to write | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4360 |             if (mSleepTimeUs == 0 && mType != OFFLOAD) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4361 |                 for (size_t i = 0; i < effectChains.size(); i ++) { | 
 | 4362 |                     effectChains[i]->process_l(); | 
| jiabin | 47affe5 | 2019-04-04 18:02:07 -0700 | [diff] [blame] | 4363 |                     // TODO: Write haptic data directly to sink buffer when mixing. | 
| Andy Hung | 6e6a2e6 | 2019-04-30 16:38:41 -0700 | [diff] [blame] | 4364 |                     if (activeHapticSessionId != AUDIO_SESSION_NONE | 
 | 4365 |                             && activeHapticSessionId == effectChains[i]->sessionId()) { | 
| jiabin | 47affe5 | 2019-04-04 18:02:07 -0700 | [diff] [blame] | 4366 |                         // Haptic data is active in this case, copy it directly from | 
 | 4367 |                         // in buffer to out buffer. | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4368 |                         uint32_t hapticSessionChannelCount = mEffectBufferValid ? | 
 | 4369 |                                             audio_channel_count_from_out_mask(mMixerChannelMask) : | 
 | 4370 |                                             mChannelCount; | 
 | 4371 |                         if (mType == SPATIALIZER && !isHapticSessionSpatialized) { | 
 | 4372 |                             hapticSessionChannelCount = mChannelCount; | 
 | 4373 |                         } | 
 | 4374 |  | 
| jiabin | 47affe5 | 2019-04-04 18:02:07 -0700 | [diff] [blame] | 4375 |                         const size_t audioBufferSize = mNormalFrameCount | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4376 |                             * audio_bytes_per_frame(hapticSessionChannelCount, | 
| Andy Hung | 319587b | 2023-05-23 14:01:03 -0700 | [diff] [blame] | 4377 |                                                     AUDIO_FORMAT_PCM_FLOAT); | 
| jiabin | 47affe5 | 2019-04-04 18:02:07 -0700 | [diff] [blame] | 4378 |                         memcpy_by_audio_format( | 
 | 4379 |                                 (uint8_t*)effectChains[i]->outBuffer() + audioBufferSize, | 
| Andy Hung | 319587b | 2023-05-23 14:01:03 -0700 | [diff] [blame] | 4380 |                                 AUDIO_FORMAT_PCM_FLOAT, | 
| jiabin | 47affe5 | 2019-04-04 18:02:07 -0700 | [diff] [blame] | 4381 |                                 (const uint8_t*)effectChains[i]->inBuffer() + audioBufferSize, | 
| Andy Hung | 319587b | 2023-05-23 14:01:03 -0700 | [diff] [blame] | 4382 |                                 AUDIO_FORMAT_PCM_FLOAT, mNormalFrameCount * mHapticChannelCount); | 
| jiabin | 47affe5 | 2019-04-04 18:02:07 -0700 | [diff] [blame] | 4383 |                     } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4384 |                 } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4385 |             } | 
 | 4386 |         } | 
| Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 4387 |         // Process effect chains for offloaded thread even if no audio | 
 | 4388 |         // was read from audio track: process only updates effect state | 
 | 4389 |         // and thus does have to be synchronized with audio writes but may have | 
 | 4390 |         // to be called while waiting for async write callback | 
 | 4391 |         if (mType == OFFLOAD) { | 
 | 4392 |             for (size_t i = 0; i < effectChains.size(); i ++) { | 
 | 4393 |                 effectChains[i]->process_l(); | 
 | 4394 |             } | 
 | 4395 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4396 |  | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4397 |         // Only if the Effects buffer is enabled and there is data in the | 
 | 4398 |         // Effects buffer (buffer valid), we need to | 
 | 4399 |         // copy into the sink buffer. | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4400 |         // TODO use mSleepTimeUs == 0 as an additional condition. | 
| jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 4401 |         if (mEffectBufferValid && !mHasDataCopiedToSinkBuffer) { | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4402 |             //ALOGV("writing effect buffer to sink buffer format %#x", mFormat); | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4403 |             void *effectBuffer = (mType == SPATIALIZER) ? mPostSpatializerBuffer : mEffectBuffer; | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4404 |             if (requireMonoBlend()) { | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4405 |                 mono_blend(effectBuffer, mEffectBufferFormat, mChannelCount, mNormalFrameCount, | 
| Glenn Kasten | 03c48d5 | 2016-01-27 17:25:17 -0800 | [diff] [blame] | 4406 |                            true /*limit*/); | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4407 |             } | 
 | 4408 |  | 
| Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 4409 |             if (!hasFastMixer()) { | 
 | 4410 |                 // Balance must take effect after mono conversion. | 
 | 4411 |                 // We do it here if there is no FastMixer. | 
 | 4412 |                 // mBalance detects zero balance within the class for speed (not needed here). | 
 | 4413 |                 mBalance.setBalance(mMasterBalance.load()); | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4414 |                 mBalance.process((float *)effectBuffer, mNormalFrameCount); | 
| Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 4415 |             } | 
 | 4416 |  | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4417 |             // for SPATIALIZER thread, Move haptics channels from mEffectBuffer to | 
 | 4418 |             // mPostSpatializerBuffer if the haptics track is spatialized. | 
 | 4419 |             // Otherwise, the haptics channels are already in mPostSpatializerBuffer. | 
 | 4420 |             // For other thread types, the haptics channels are already in mEffectBuffer. | 
 | 4421 |             if (mType == SPATIALIZER && isHapticSessionSpatialized) { | 
 | 4422 |                 const size_t srcBufferSize = mNormalFrameCount * | 
 | 4423 |                         audio_bytes_per_frame(audio_channel_count_from_out_mask(mMixerChannelMask), | 
 | 4424 |                                               mEffectBufferFormat); | 
 | 4425 |                 const size_t dstBufferSize = mNormalFrameCount | 
 | 4426 |                         * audio_bytes_per_frame(mChannelCount, mEffectBufferFormat); | 
 | 4427 |  | 
 | 4428 |                 memcpy_by_audio_format((uint8_t*)mPostSpatializerBuffer + dstBufferSize, | 
 | 4429 |                                        mEffectBufferFormat, | 
 | 4430 |                                        (uint8_t*)mEffectBuffer + srcBufferSize, | 
 | 4431 |                                        mEffectBufferFormat, | 
 | 4432 |                                        mNormalFrameCount * mHapticChannelCount); | 
| Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 4433 |             } | 
| Atneya Nair | ba9a107 | 2022-09-19 17:51:37 -0700 | [diff] [blame] | 4434 |             const size_t framesToCopy = mNormalFrameCount * (mChannelCount + mHapticChannelCount); | 
 | 4435 |             if (mFormat == AUDIO_FORMAT_PCM_FLOAT && | 
 | 4436 |                     mEffectBufferFormat == AUDIO_FORMAT_PCM_FLOAT) { | 
 | 4437 |                 // Clamp PCM float values more than this distance from 0 to insulate | 
 | 4438 |                 // a HAL which doesn't handle NaN correctly. | 
 | 4439 |                 static constexpr float HAL_FLOAT_SAMPLE_LIMIT = 2.0f; | 
 | 4440 |                 memcpy_to_float_from_float_with_clamping(static_cast<float*>(mSinkBuffer), | 
 | 4441 |                         static_cast<const float*>(effectBuffer), | 
 | 4442 |                         framesToCopy, HAL_FLOAT_SAMPLE_LIMIT /* absMax */); | 
 | 4443 |             } else { | 
 | 4444 |                 memcpy_by_audio_format(mSinkBuffer, mFormat, | 
 | 4445 |                         effectBuffer, mEffectBufferFormat, framesToCopy); | 
 | 4446 |             } | 
| jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 4447 |             // The sample data is partially interleaved when haptic channels exist, | 
 | 4448 |             // we need to adjust channels here. | 
 | 4449 |             if (mHapticChannelCount > 0) { | 
 | 4450 |                 adjust_channels_non_destructive(mSinkBuffer, mChannelCount, mSinkBuffer, | 
 | 4451 |                         mChannelCount + mHapticChannelCount, | 
 | 4452 |                         audio_bytes_per_sample(mFormat), | 
 | 4453 |                         audio_bytes_per_frame(mChannelCount, mFormat) * mNormalFrameCount); | 
 | 4454 |             } | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4455 |         } | 
 | 4456 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4457 |         // enable changes in effect chain | 
 | 4458 |         unlockEffectChains(effectChains); | 
 | 4459 |  | 
| Vlad Popa | fce1086 | 2023-02-03 10:37:07 +0100 | [diff] [blame] | 4460 |         if (!metadataUpdate.playbackMetadataUpdate.empty()) { | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 4461 |             mAfThreadCallback->getMelReporter()->updateMetadataForCsd(id(), | 
| Vlad Popa | fce1086 | 2023-02-03 10:37:07 +0100 | [diff] [blame] | 4462 |                     metadataUpdate.playbackMetadataUpdate); | 
 | 4463 |         } | 
 | 4464 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4465 |         if (!waitingAsyncCallback()) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4466 |             // mSleepTimeUs == 0 means we must write to audio hardware | 
 | 4467 |             if (mSleepTimeUs == 0) { | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4468 |                 ssize_t ret = 0; | 
| Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4469 |                 // writePeriodNs is updated >= 0 when ret > 0. | 
 | 4470 |                 int64_t writePeriodNs = -1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4471 |                 if (mBytesRemaining) { | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 4472 |                     // FIXME rewrite to reduce number of system calls | 
| Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4473 |                     const int64_t lastIoBeginNs = systemTime(); | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4474 |                     ret = threadLoop_write(); | 
| Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4475 |                     const int64_t lastIoEndNs = systemTime(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4476 |                     if (ret < 0) { | 
 | 4477 |                         mBytesRemaining = 0; | 
| Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4478 |                     } else if (ret > 0) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4479 |                         mBytesWritten += ret; | 
 | 4480 |                         mBytesRemaining -= ret; | 
| Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4481 |                         const int64_t frames = ret / mFrameSize; | 
 | 4482 |                         mFramesWritten += frames; | 
 | 4483 |  | 
 | 4484 |                         writePeriodNs = lastIoEndNs - mLastIoEndNs; | 
 | 4485 |                         // process information relating to write time. | 
 | 4486 |                         if (audio_has_proportional_frames(mFormat)) { | 
 | 4487 |                             // we are in a continuous mixing cycle | 
 | 4488 |                             if (mMixerStatus == MIXER_TRACKS_READY && | 
 | 4489 |                                     loopCount == lastLoopCountWritten + 1) { | 
 | 4490 |  | 
 | 4491 |                                 const double jitterMs = | 
 | 4492 |                                         TimestampVerifier<int64_t, int64_t>::computeJitterMs( | 
 | 4493 |                                                 {frames, writePeriodNs}, | 
 | 4494 |                                                 {0, 0} /* lastTimestamp */, mSampleRate); | 
 | 4495 |                                 const double processMs = | 
 | 4496 |                                        (lastIoBeginNs - mLastIoEndNs) * 1e-6; | 
 | 4497 |  | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 4498 |                                 audio_utils::lock_guard _l(mutex()); | 
| Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4499 |                                 mIoJitterMs.add(jitterMs); | 
 | 4500 |                                 mProcessTimeMs.add(processMs); | 
| Robert Wu | 06db0a3 | 2021-08-10 19:05:34 +0000 | [diff] [blame] | 4501 |  | 
 | 4502 |                                 if (mPipeSink.get() != nullptr) { | 
 | 4503 |                                     // Using the Monopipe availableToWrite, we estimate the current | 
 | 4504 |                                     // buffer size. | 
 | 4505 |                                     MonoPipe* monoPipe = static_cast<MonoPipe*>(mPipeSink.get()); | 
 | 4506 |                                     const ssize_t | 
 | 4507 |                                             availableToWrite = mPipeSink->availableToWrite(); | 
 | 4508 |                                     const size_t pipeFrames = monoPipe->maxFrames(); | 
 | 4509 |                                     const size_t | 
 | 4510 |                                             remainingFrames = pipeFrames - max(availableToWrite, 0); | 
 | 4511 |                                     mMonopipePipeDepthStats.add(remainingFrames); | 
 | 4512 |                                 } | 
| Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4513 |                             } | 
 | 4514 |  | 
 | 4515 |                             // write blocked detection | 
 | 4516 |                             const int64_t deltaWriteNs = lastIoEndNs - lastIoBeginNs; | 
| Andy Hung | d363992 | 2022-04-28 18:00:49 -0700 | [diff] [blame] | 4517 |                             if ((mType == MIXER || mType == SPATIALIZER) | 
 | 4518 |                                     && deltaWriteNs > maxPeriod) { | 
| Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4519 |                                 mNumDelayedWrites++; | 
 | 4520 |                                 if ((lastIoEndNs - lastWarning) > kWarningThrottleNs) { | 
 | 4521 |                                     ATRACE_NAME("underrun"); | 
 | 4522 |                                     ALOGW("write blocked for %lld msecs, " | 
 | 4523 |                                             "%d delayed writes, thread %d", | 
 | 4524 |                                             (long long)deltaWriteNs / NANOS_PER_MILLISECOND, | 
 | 4525 |                                             mNumDelayedWrites, mId); | 
 | 4526 |                                     lastWarning = lastIoEndNs; | 
 | 4527 |                                 } | 
 | 4528 |                             } | 
 | 4529 |                         } | 
 | 4530 |                         // update timing info. | 
 | 4531 |                         mLastIoBeginNs = lastIoBeginNs; | 
 | 4532 |                         mLastIoEndNs = lastIoEndNs; | 
 | 4533 |                         lastLoopCountWritten = loopCount; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4534 |                     } | 
 | 4535 |                 } else if ((mMixerStatus == MIXER_DRAIN_TRACK) || | 
 | 4536 |                         (mMixerStatus == MIXER_DRAIN_ALL)) { | 
 | 4537 |                     threadLoop_drain(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4538 |                 } | 
| Andy Hung | d363992 | 2022-04-28 18:00:49 -0700 | [diff] [blame] | 4539 |                 if ((mType == MIXER || mType == SPATIALIZER) && !mStandby) { | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4540 |  | 
 | 4541 |                     if (mThreadThrottle | 
 | 4542 |                             && mMixerStatus == MIXER_TRACKS_READY // we are mixing (active tracks) | 
| Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4543 |                             && writePeriodNs > 0) {               // we have write period info | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4544 |                         // Limit MixerThread data processing to no more than twice the | 
 | 4545 |                         // expected processing rate. | 
 | 4546 |                         // | 
 | 4547 |                         // This helps prevent underruns with NuPlayer and other applications | 
 | 4548 |                         // which may set up buffers that are close to the minimum size, or use | 
 | 4549 |                         // deep buffers, and rely on a double-buffering sleep strategy to fill. | 
 | 4550 |                         // | 
 | 4551 |                         // The throttle smooths out sudden large data drains from the device, | 
 | 4552 |                         // e.g. when it comes out of standby, which often causes problems with | 
 | 4553 |                         // (1) mixer threads without a fast mixer (which has its own warm-up) | 
 | 4554 |                         // (2) minimum buffer sized tracks (even if the track is full, | 
 | 4555 |                         //     the app won't fill fast enough to handle the sudden draw). | 
| Haynes Mathew George | f92b217 | 2016-05-09 11:34:15 -0700 | [diff] [blame] | 4556 |                         // | 
 | 4557 |                         // Total time spent in last processing cycle equals time spent in | 
 | 4558 |                         // 1. threadLoop_write, as well as time spent in | 
 | 4559 |                         // 2. threadLoop_mix (significant for heavy mixing, especially | 
 | 4560 |                         //                    on low tier processors) | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4561 |  | 
| Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4562 |                         // it's OK if deltaMs is an overestimate. | 
 | 4563 |  | 
 | 4564 |                         const int32_t deltaMs = writePeriodNs / NANOS_PER_MILLISECOND; | 
| Ivan Lozano | e02bc54 | 2017-10-26 09:51:54 -0700 | [diff] [blame] | 4565 |  | 
| Ivan Lozano | ea04d39 | 2017-11-07 14:37:07 -0800 | [diff] [blame] | 4566 |                         const int32_t throttleMs = (int32_t)mHalfBufferMs - deltaMs; | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4567 |                         if ((signed)mHalfBufferMs >= throttleMs && throttleMs > 0) { | 
| Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 4568 |                             mThreadMetrics.logThrottleMs((double)throttleMs); | 
| Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 4569 |  | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4570 |                             usleep(throttleMs * 1000); | 
| Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 4571 |                             // notify of throttle start on verbose log | 
 | 4572 |                             ALOGV_IF(mThreadThrottleEndMs == mThreadThrottleTimeMs, | 
 | 4573 |                                     "mixer(%p) throttle begin:" | 
 | 4574 |                                     " ret(%zd) deltaMs(%d) requires sleep %d ms", | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4575 |                                     this, ret, deltaMs, throttleMs); | 
| Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 4576 |                             mThreadThrottleTimeMs += throttleMs; | 
| Andy Hung | 0a31ddd | 2016-07-06 19:10:29 -0700 | [diff] [blame] | 4577 |                             // Throttle must be attributed to the previous mixer loop's write time | 
 | 4578 |                             // to allow back-to-back throttling. | 
| Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4579 |                             // This also ensures proper timing statistics. | 
 | 4580 |                             mLastIoEndNs = systemTime();  // we fetch the write end time again. | 
| Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 4581 |                         } else { | 
 | 4582 |                             uint32_t diff = mThreadThrottleTimeMs - mThreadThrottleEndMs; | 
 | 4583 |                             if (diff > 0) { | 
 | 4584 |                                 // notify of throttle end on debug log | 
| Andy Hung | 3ea004d | 2016-05-05 16:48:37 -0700 | [diff] [blame] | 4585 |                                 // but prevent spamming for bluetooth | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4586 |                                 ALOGD_IF(!isSingleDeviceType( | 
| Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 4587 |                                                  outDeviceTypes_l(), audio_is_a2dp_out_device) && | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4588 |                                          !isSingleDeviceType( | 
| Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 4589 |                                                  outDeviceTypes_l(), | 
 | 4590 |                                                  audio_is_hearing_aid_out_device), | 
| Andy Hung | 3ea004d | 2016-05-05 16:48:37 -0700 | [diff] [blame] | 4591 |                                         "mixer(%p) throttle end: throttle time(%u)", this, diff); | 
| Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 4592 |                                 mThreadThrottleEndMs = mThreadThrottleTimeMs; | 
 | 4593 |                             } | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4594 |                         } | 
 | 4595 |                     } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4596 |                 } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4597 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4598 |             } else { | 
| Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 4599 |                 ATRACE_BEGIN("sleep"); | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4600 |                 audio_utils::unique_lock _l(mutex()); | 
| rago | 1bb9082 | 2017-05-02 18:31:48 -0700 | [diff] [blame] | 4601 |                 // suspended requires accurate metering of sleep time. | 
 | 4602 |                 if (isSuspended()) { | 
 | 4603 |                     // advance by expected sleepTime | 
 | 4604 |                     timeLoopNextNs += microseconds((nsecs_t)mSleepTimeUs); | 
 | 4605 |                     const nsecs_t nowNs = systemTime(); | 
 | 4606 |  | 
 | 4607 |                     // compute expected next time vs current time. | 
 | 4608 |                     // (negative deltas are treated as delays). | 
 | 4609 |                     nsecs_t deltaNs = timeLoopNextNs - nowNs; | 
 | 4610 |                     if (deltaNs < -kMaxNextBufferDelayNs) { | 
 | 4611 |                         // Delays longer than the max allowed trigger a reset. | 
 | 4612 |                         ALOGV("DelayNs: %lld, resetting timeLoopNextNs", (long long) deltaNs); | 
 | 4613 |                         deltaNs = microseconds((nsecs_t)mSleepTimeUs); | 
 | 4614 |                         timeLoopNextNs = nowNs + deltaNs; | 
 | 4615 |                     } else if (deltaNs < 0) { | 
 | 4616 |                         // Delays within the max delay allowed: zero the delta/sleepTime | 
 | 4617 |                         // to help the system catch up in the next iteration(s) | 
 | 4618 |                         ALOGV("DelayNs: %lld, catching-up", (long long) deltaNs); | 
 | 4619 |                         deltaNs = 0; | 
 | 4620 |                     } | 
 | 4621 |                     // update sleep time (which is >= 0) | 
 | 4622 |                     mSleepTimeUs = deltaNs / 1000; | 
 | 4623 |                 } | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 4624 |                 if (!mSignalPending && mConfigEvents.isEmpty() && !exitPending()) { | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4625 |                     mWaitWorkCV.wait_for(_l, std::chrono::microseconds(mSleepTimeUs)); | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 4626 |                 } | 
| Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 4627 |                 ATRACE_END(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4628 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4629 |         } | 
 | 4630 |  | 
 | 4631 |         // Finally let go of removed track(s), without the lock held | 
 | 4632 |         // since we can't guarantee the destructors won't acquire that | 
 | 4633 |         // same lock.  This will also mutate and push a new fast mixer state. | 
 | 4634 |         threadLoop_removeTracks(tracksToRemove); | 
 | 4635 |         tracksToRemove.clear(); | 
 | 4636 |  | 
 | 4637 |         // FIXME I don't understand the need for this here; | 
 | 4638 |         //       it was in the original code but maybe the | 
 | 4639 |         //       assignment in saveOutputTracks() makes this unnecessary? | 
 | 4640 |         clearOutputTracks(); | 
 | 4641 |  | 
 | 4642 |         // Effect chains will be actually deleted here if they were removed from | 
 | 4643 |         // mEffectChains list during mixing or effects processing | 
 | 4644 |         effectChains.clear(); | 
 | 4645 |  | 
 | 4646 |         // FIXME Note that the above .clear() is no longer necessary since effectChains | 
 | 4647 |         // is now local to this block, but will keep it for now (at least until merge done). | 
| Andy Hung | 56ce2ed | 2024-06-12 16:03:16 -0700 | [diff] [blame] | 4648 |  | 
 | 4649 |         mThreadloopExecutor.process(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4650 |     } | 
| Andy Hung | 56ce2ed | 2024-06-12 16:03:16 -0700 | [diff] [blame] | 4651 |     mThreadloopExecutor.process(); // process any remaining deferred actions. | 
 | 4652 |     // deferred actions after this point are ignored. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4653 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4654 |     threadLoop_exit(); | 
 | 4655 |  | 
| Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 4656 |     if (!mStandby) { | 
 | 4657 |         threadLoop_standby(); | 
| Eric Laurent | 19952e1 | 2023-04-20 10:08:29 +0200 | [diff] [blame] | 4658 |         setStandby(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4659 |     } | 
 | 4660 |  | 
 | 4661 |     releaseWakeLock(); | 
 | 4662 |  | 
 | 4663 |     ALOGV("Thread %p type %d exiting", this, mType); | 
 | 4664 |     return false; | 
 | 4665 | } | 
 | 4666 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 4667 | void PlaybackThread::collectTimestamps_l() | 
| Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 4668 | { | 
| Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 4669 |     if (mStandby) { | 
 | 4670 |         mTimestampVerifier.discontinuity(discontinuityForStandbyOrFlush()); | 
 | 4671 |         return; | 
 | 4672 |     } else if (mHwPaused) { | 
 | 4673 |         mTimestampVerifier.discontinuity(mTimestampVerifier.DISCONTINUITY_MODE_CONTINUOUS); | 
 | 4674 |         return; | 
 | 4675 |     } | 
 | 4676 |  | 
 | 4677 |     // Gather the framesReleased counters for all active tracks, | 
 | 4678 |     // and associate with the sink frames written out.  We need | 
 | 4679 |     // this to convert the sink timestamp to the track timestamp. | 
 | 4680 |     bool kernelLocationUpdate = false; | 
 | 4681 |     ExtendedTimestamp timestamp; // use private copy to fetch | 
 | 4682 |  | 
 | 4683 |     // Always query HAL timestamp and update timestamp verifier. In standby or pause, | 
 | 4684 |     // HAL may be draining some small duration buffered data for fade out. | 
 | 4685 |     if (threadloop_getHalTimestamp_l(×tamp) == OK) { | 
 | 4686 |         mTimestampVerifier.add(timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL], | 
 | 4687 |                 timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL], | 
 | 4688 |                 mSampleRate); | 
 | 4689 |  | 
| Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 4690 |         if (isTimestampCorrectionEnabled_l()) { | 
| Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 4691 |             ALOGVV("TS_BEFORE: %d %lld %lld", id(), | 
 | 4692 |                     (long long)timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL], | 
 | 4693 |                     (long long)timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]); | 
 | 4694 |             auto correctedTimestamp = mTimestampVerifier.getLastCorrectedTimestamp(); | 
 | 4695 |             timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] | 
 | 4696 |                     = correctedTimestamp.mFrames; | 
 | 4697 |             timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] | 
 | 4698 |                     = correctedTimestamp.mTimeNs; | 
 | 4699 |             ALOGVV("TS_AFTER: %d %lld %lld", id(), | 
 | 4700 |                     (long long)timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL], | 
 | 4701 |                     (long long)timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]); | 
 | 4702 |  | 
 | 4703 |             // Note: Downstream latency only added if timestamp correction enabled. | 
 | 4704 |             if (mDownstreamLatencyStatMs.getN() > 0) { // we have latency info. | 
 | 4705 |                 const int64_t newPosition = | 
 | 4706 |                         timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] | 
 | 4707 |                         - int64_t(mDownstreamLatencyStatMs.getMean() * mSampleRate * 1e-3); | 
 | 4708 |                 // prevent retrograde | 
 | 4709 |                 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = max( | 
 | 4710 |                         newPosition, | 
 | 4711 |                         (mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] | 
 | 4712 |                                 - mSuspendedFrames)); | 
 | 4713 |             } | 
 | 4714 |         } | 
 | 4715 |  | 
 | 4716 |         // We always fetch the timestamp here because often the downstream | 
 | 4717 |         // sink will block while writing. | 
 | 4718 |  | 
 | 4719 |         // We keep track of the last valid kernel position in case we are in underrun | 
 | 4720 |         // and the normal mixer period is the same as the fast mixer period, or there | 
 | 4721 |         // is some error from the HAL. | 
 | 4722 |         if (mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) { | 
 | 4723 |             mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] = | 
 | 4724 |                     mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]; | 
 | 4725 |             mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] = | 
 | 4726 |                     mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; | 
 | 4727 |  | 
 | 4728 |             mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] = | 
 | 4729 |                     mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER]; | 
 | 4730 |             mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] = | 
 | 4731 |                     mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER]; | 
 | 4732 |         } | 
 | 4733 |  | 
 | 4734 |         if (timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) { | 
 | 4735 |             kernelLocationUpdate = true; | 
 | 4736 |         } else { | 
 | 4737 |             ALOGVV("getTimestamp error - no valid kernel position"); | 
 | 4738 |         } | 
 | 4739 |  | 
 | 4740 |         // copy over kernel info | 
 | 4741 |         mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = | 
 | 4742 |                 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] | 
 | 4743 |                 + mSuspendedFrames; // add frames discarded when suspended | 
 | 4744 |         mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = | 
 | 4745 |                 timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; | 
 | 4746 |     } else { | 
 | 4747 |         mTimestampVerifier.error(); | 
 | 4748 |     } | 
 | 4749 |  | 
 | 4750 |     // mFramesWritten for non-offloaded tracks are contiguous | 
 | 4751 |     // even after standby() is called. This is useful for the track frame | 
 | 4752 |     // to sink frame mapping. | 
 | 4753 |     bool serverLocationUpdate = false; | 
 | 4754 |     if (mFramesWritten != mLastFramesWritten) { | 
 | 4755 |         serverLocationUpdate = true; | 
 | 4756 |         mLastFramesWritten = mFramesWritten; | 
 | 4757 |     } | 
 | 4758 |     // Only update timestamps if there is a meaningful change. | 
 | 4759 |     // Either the kernel timestamp must be valid or we have written something. | 
 | 4760 |     if (kernelLocationUpdate || serverLocationUpdate) { | 
 | 4761 |         if (serverLocationUpdate) { | 
 | 4762 |             // use the time before we called the HAL write - it is a bit more accurate | 
 | 4763 |             // to when the server last read data than the current time here. | 
 | 4764 |             // | 
 | 4765 |             // If we haven't written anything, mLastIoBeginNs will be -1 | 
 | 4766 |             // and we use systemTime(). | 
 | 4767 |             mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] = mFramesWritten; | 
 | 4768 |             mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = mLastIoBeginNs == -1 | 
| Andy Hung | 160664b | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 4769 |                     ? systemTime() : (int64_t)mLastIoBeginNs; | 
| Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 4770 |         } | 
 | 4771 |  | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 4772 |         for (const sp<IAfTrack>& t : mActiveTracks) { | 
| Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 4773 |             if (!t->isFastTrack()) { | 
 | 4774 |                 t->updateTrackFrameInfo( | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 4775 |                         t->audioTrackServerProxy()->framesReleased(), | 
| Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 4776 |                         mFramesWritten, | 
 | 4777 |                         mSampleRate, | 
 | 4778 |                         mTimestamp); | 
 | 4779 |             } | 
 | 4780 |         } | 
 | 4781 |     } | 
 | 4782 |  | 
 | 4783 |     if (audio_has_proportional_frames(mFormat)) { | 
 | 4784 |         const double latencyMs = mTimestamp.getOutputServerLatencyMs(mSampleRate); | 
 | 4785 |         if (latencyMs != 0.) { // note 0. means timestamp is empty. | 
 | 4786 |             mLatencyMs.add(latencyMs); | 
 | 4787 |         } | 
 | 4788 |     } | 
 | 4789 | #if 0 | 
 | 4790 |     // logFormat example | 
 | 4791 |     if (z % 100 == 0) { | 
 | 4792 |         timespec ts; | 
 | 4793 |         clock_gettime(CLOCK_MONOTONIC, &ts); | 
 | 4794 |         LOGT("This is an integer %d, this is a float %f, this is my " | 
 | 4795 |             "pid %p %% %s %t", 42, 3.14, "and this is a timestamp", ts); | 
 | 4796 |         LOGT("A deceptive null-terminated string %\0"); | 
 | 4797 |     } | 
 | 4798 |     ++z; | 
 | 4799 | #endif | 
 | 4800 | } | 
 | 4801 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4802 | // removeTracks_l() must be called with ThreadBase::mutex() held | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 4803 | void PlaybackThread::removeTracks_l(const Vector<sp<IAfTrack>>& tracksToRemove) | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4804 | NO_THREAD_SAFETY_ANALYSIS  // release and re-acquire mutex() | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4805 | { | 
| Andy Hung | a718771 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 4806 |     if (tracksToRemove.empty()) return; | 
 | 4807 |  | 
 | 4808 |     // Block all incoming TrackHandle requests until we are finished with the release. | 
 | 4809 |     setThreadBusy_l(true); | 
 | 4810 |  | 
| Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 4811 |     for (const auto& track : tracksToRemove) { | 
| Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 4812 |         ALOGV("%s(%d): removing track on session %d", __func__, track->id(), track->sessionId()); | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 4813 |         sp<IAfEffectChain> chain = getEffectChain_l(track->sessionId()); | 
| Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 4814 |         if (chain != 0) { | 
 | 4815 |             ALOGV("%s(%d): stopping track on chain %p for session Id: %d", | 
 | 4816 |                     __func__, track->id(), chain.get(), track->sessionId()); | 
 | 4817 |             chain->decActiveTrackCnt(); | 
 | 4818 |         } | 
| Andy Hung | a718771 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 4819 |  | 
| Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 4820 |         // If an external client track, inform APM we're no longer active, and remove if needed. | 
| Andy Hung | a718771 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 4821 |         // Since the track is active, we do it here instead of TrackBase::destroy(). | 
| Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 4822 |         if (track->isExternalTrack()) { | 
| Andy Hung | a718771 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 4823 |             mutex().unlock(); | 
| Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 4824 |             AudioSystem::stopOutput(track->portId()); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4825 |             if (track->isTerminated()) { | 
| Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 4826 |                 AudioSystem::releaseOutput(track->portId()); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4827 |             } | 
| Andy Hung | a718771 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 4828 |             mutex().lock(); | 
| Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 4829 |         } | 
| jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 4830 |         if (mHapticChannelCount > 0 && | 
 | 4831 |                 ((track->channelMask() & AUDIO_CHANNEL_HAPTIC_ALL) != AUDIO_CHANNEL_NONE | 
 | 4832 |                         || (chain != nullptr && chain->containsHapticGeneratingEffect_l()))) { | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4833 |             mutex().unlock(); | 
| jiabin | 57303cc | 2018-12-18 15:45:57 -0800 | [diff] [blame] | 4834 |             // Unlock due to VibratorService will lock for this call and will | 
 | 4835 |             // call Tracks.mute/unmute which also require thread's lock. | 
| Andy Hung | 76cb915 | 2023-07-20 21:23:42 -0700 | [diff] [blame] | 4836 |             afutils::onExternalVibrationStop(track->getExternalVibration()); | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4837 |             mutex().lock(); | 
| jiabin | e70bc7f | 2020-06-30 22:07:55 -0700 | [diff] [blame] | 4838 |  | 
 | 4839 |             // When the track is stop, set the haptic intensity as MUTE | 
 | 4840 |             // for the HapticGenerator effect. | 
 | 4841 |             if (chain != nullptr) { | 
| Ahmad Khalil | 229466a | 2024-02-05 12:15:30 +0000 | [diff] [blame] | 4842 |                 chain->setHapticScale_l(track->id(), os::HapticScale::mute()); | 
| jiabin | e70bc7f | 2020-06-30 22:07:55 -0700 | [diff] [blame] | 4843 |             } | 
| jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 4844 |         } | 
| Andy Hung | a718771 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 4845 |  | 
 | 4846 |         // Under lock, the track is removed from the active tracks list. | 
 | 4847 |         // | 
 | 4848 |         // Once the track is no longer active, the TrackHandle may directly | 
 | 4849 |         // modify it as the threadLoop() is no longer responsible for its maintenance. | 
 | 4850 |         // Do not modify the track from threadLoop after the mutex is unlocked | 
 | 4851 |         // if it is not active. | 
 | 4852 |         mActiveTracks.remove(track); | 
 | 4853 |  | 
 | 4854 |         if (track->isTerminated()) { | 
 | 4855 |             // remove from our tracks vector | 
 | 4856 |             removeTrack_l(track); | 
 | 4857 |         } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4858 |     } | 
| Andy Hung | a718771 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 4859 |  | 
 | 4860 |     // Allow incoming TrackHandle requests.  We still hold the mutex, | 
 | 4861 |     // so pending TrackHandle requests will occur after we unlock it. | 
 | 4862 |     setThreadBusy_l(false); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4863 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4864 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 4865 | status_t PlaybackThread::getTimestamp_l(AudioTimestamp& timestamp) | 
| Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 4866 | { | 
 | 4867 |     if (mNormalSink != 0) { | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 4868 |         ExtendedTimestamp ets; | 
 | 4869 |         status_t status = mNormalSink->getTimestamp(ets); | 
 | 4870 |         if (status == NO_ERROR) { | 
 | 4871 |             status = ets.getBestTimestamp(×tamp); | 
 | 4872 |         } | 
 | 4873 |         return status; | 
| Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 4874 |     } | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 4875 |     if ((mType == OFFLOAD || mType == DIRECT) && mOutput != NULL) { | 
| Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 4876 |         collectTimestamps_l(); | 
 | 4877 |         if (mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] <= 0) { | 
 | 4878 |             return INVALID_OPERATION; | 
| Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 4879 |         } | 
| Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 4880 |         timestamp.mPosition = mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]; | 
 | 4881 |         const int64_t timeNs = mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; | 
 | 4882 |         timestamp.mTime.tv_sec = timeNs / NANOS_PER_SECOND; | 
 | 4883 |         timestamp.mTime.tv_nsec = timeNs - (timestamp.mTime.tv_sec * NANOS_PER_SECOND); | 
 | 4884 |         return NO_ERROR; | 
| Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 4885 |     } | 
 | 4886 |     return INVALID_OPERATION; | 
 | 4887 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4888 |  | 
| Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 4889 | // For dedicated VoIP outputs, let the HAL apply the stream volume. Track volume is | 
 | 4890 | // still applied by the mixer. | 
 | 4891 | // All tracks attached to a mixer with flag VOIP_RX are tied to the same | 
 | 4892 | // stream type STREAM_VOICE_CALL so this will only change the HAL volume once even | 
 | 4893 | // if more than one track are active | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 4894 | status_t PlaybackThread::handleVoipVolume_l(float* volume) | 
| Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 4895 | { | 
 | 4896 |     status_t result = NO_ERROR; | 
 | 4897 |     if ((mOutput->flags & AUDIO_OUTPUT_FLAG_VOIP_RX) != 0) { | 
 | 4898 |         if (*volume != mLeftVolFloat) { | 
 | 4899 |             result = mOutput->stream->setVolume(*volume, *volume); | 
| Alex Leung | c5dedb2 | 2023-05-10 08:00:50 -0700 | [diff] [blame] | 4900 |             // HAL can return INVALID_OPERATION if operation is not supported. | 
 | 4901 |             ALOGE_IF(result != OK && result != INVALID_OPERATION, | 
| Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 4902 |                      "Error when setting output stream volume: %d", result); | 
 | 4903 |             if (result == NO_ERROR) { | 
 | 4904 |                 mLeftVolFloat = *volume; | 
 | 4905 |             } | 
 | 4906 |         } | 
 | 4907 |         // if stream volume was successfully sent to the HAL, mLeftVolFloat == v here and we | 
 | 4908 |         // remove stream volume contribution from software volume. | 
 | 4909 |         if (mLeftVolFloat == *volume) { | 
 | 4910 |             *volume = 1.0f; | 
 | 4911 |         } | 
 | 4912 |     } | 
 | 4913 |     return result; | 
 | 4914 | } | 
 | 4915 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 4916 | status_t MixerThread::createAudioPatch_l(const struct audio_patch* patch, | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4917 |                                                           audio_patch_handle_t *handle) | 
 | 4918 | { | 
| Andy Hung | f60abce | 2016-08-26 11:37:54 -0700 | [diff] [blame] | 4919 |     status_t status; | 
 | 4920 |     if (property_get_bool("af.patch_park", false /* default_value */)) { | 
 | 4921 |         // Park FastMixer to avoid potential DOS issues with writing to the HAL | 
 | 4922 |         // or if HAL does not properly lock against access. | 
 | 4923 |         AutoPark<FastMixer> park(mFastMixer); | 
 | 4924 |         status = PlaybackThread::createAudioPatch_l(patch, handle); | 
 | 4925 |     } else { | 
 | 4926 |         status = PlaybackThread::createAudioPatch_l(patch, handle); | 
 | 4927 |     } | 
| Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 4928 |  | 
 | 4929 |     updateHalSupportedLatencyModes_l(); | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4930 |     return status; | 
 | 4931 | } | 
 | 4932 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 4933 | status_t PlaybackThread::createAudioPatch_l(const struct audio_patch *patch, | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4934 |                                                           audio_patch_handle_t *handle) | 
 | 4935 | { | 
 | 4936 |     status_t status = NO_ERROR; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4937 |  | 
 | 4938 |     // store new device and send to effects | 
 | 4939 |     audio_devices_t type = AUDIO_DEVICE_NONE; | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4940 |     AudioDeviceTypeAddrVector deviceTypeAddrs; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4941 |     for (unsigned int i = 0; i < patch->num_sinks; i++) { | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4942 |         LOG_ALWAYS_FATAL_IF(popcount(patch->sinks[i].ext.device.type) > 1 | 
 | 4943 |                             && !mOutput->audioHwDev->supportsAudioPatches(), | 
 | 4944 |                             "Enumerated device type(%#x) must not be used " | 
 | 4945 |                             "as it does not support audio patches", | 
 | 4946 |                             patch->sinks[i].ext.device.type); | 
| Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 4947 |         type = static_cast<audio_devices_t>(type | patch->sinks[i].ext.device.type); | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 4948 |         deviceTypeAddrs.emplace_back(patch->sinks[i].ext.device.type, | 
 | 4949 |                 patch->sinks[i].ext.device.address); | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4950 |     } | 
 | 4951 |  | 
| François Gaffie | 0c280aa | 2018-07-25 10:02:15 +0200 | [diff] [blame] | 4952 |     audio_port_handle_t sinkPortId = patch->sinks[0].id; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4953 | #ifdef ADD_BATTERY_DATA | 
 | 4954 |     // when changing the audio output device, call addBatteryData to notify | 
 | 4955 |     // the change | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4956 |     if (outDeviceTypes() != deviceTypes) { | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4957 |         uint32_t params = 0; | 
 | 4958 |         // check whether speaker is on | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4959 |         if (deviceTypes.count(AUDIO_DEVICE_OUT_SPEAKER) > 0) { | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4960 |             params |= IMediaPlayerService::kBatteryDataSpeakerOn; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4961 |         } | 
 | 4962 |  | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4963 |         // check if any other device (except speaker) is on | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4964 |         if (!isSingleDeviceType(deviceTypes, AUDIO_DEVICE_OUT_SPEAKER)) { | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4965 |             params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; | 
 | 4966 |         } | 
 | 4967 |  | 
 | 4968 |         if (params != 0) { | 
 | 4969 |             addBatteryData(params); | 
 | 4970 |         } | 
 | 4971 |     } | 
 | 4972 | #endif | 
 | 4973 |  | 
 | 4974 |     for (size_t i = 0; i < mEffectChains.size(); i++) { | 
| jiabin | 8f278ee | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 4975 |         mEffectChains[i]->setDevices_l(deviceTypeAddrs); | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4976 |     } | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 4977 |  | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4978 |     // mPatch.num_sinks is not set when the thread is created so that | 
 | 4979 |     // the first patch creation triggers an ioConfigChanged callback | 
 | 4980 |     bool configChanged = (mPatch.num_sinks == 0) || | 
 | 4981 |                          (mPatch.sinks[0].id != sinkPortId); | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 4982 |     mPatch = *patch; | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4983 |     mOutDeviceTypeAddrs = deviceTypeAddrs; | 
| jiabin | 0a957d3 | 2020-04-29 10:56:20 -0700 | [diff] [blame] | 4984 |     checkSilentMode_l(); | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4985 |  | 
| Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 4986 |     if (mOutput->audioHwDev->supportsAudioPatches()) { | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 4987 |         sp<DeviceHalInterface> hwDevice = mOutput->audioHwDev->hwDevice(); | 
 | 4988 |         status = hwDevice->createAudioPatch(patch->num_sources, | 
 | 4989 |                                             patch->sources, | 
 | 4990 |                                             patch->num_sinks, | 
 | 4991 |                                             patch->sinks, | 
 | 4992 |                                             handle); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4993 |     } else { | 
| Ytai Ben-Tsvi | f997ffe | 2022-02-03 16:38:16 -0800 | [diff] [blame] | 4994 |         status = mOutput->stream->legacyCreateAudioPatch(patch->sinks[0], std::nullopt, type); | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4995 |         *handle = AUDIO_PATCH_HANDLE_NONE; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4996 |     } | 
| Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 4997 |     const std::string patchSinksAsString = patchSinksToString(patch); | 
| Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 4998 |  | 
 | 4999 |     mThreadMetrics.logEndInterval(); | 
| Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 5000 |     mThreadMetrics.logCreatePatch(/* inDevices */ {}, patchSinksAsString); | 
| Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 5001 |     mThreadMetrics.logBeginInterval(); | 
| Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 5002 |     // also dispatch to active AudioTracks for MediaMetrics | 
 | 5003 |     for (const auto &track : mActiveTracks) { | 
 | 5004 |         track->logEndInterval(); | 
 | 5005 |         track->logBeginInterval(patchSinksAsString); | 
 | 5006 |     } | 
| Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 5007 |  | 
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 5008 |     if (configChanged) { | 
 | 5009 |         sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); | 
 | 5010 |     } | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 5011 |     // Force metadata update after a route change | 
| Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 5012 |     mActiveTracks.setHasChanged(); | 
 | 5013 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5014 |     return status; | 
 | 5015 | } | 
 | 5016 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5017 | status_t MixerThread::releaseAudioPatch_l(const audio_patch_handle_t handle) | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 5018 | { | 
| Andy Hung | f60abce | 2016-08-26 11:37:54 -0700 | [diff] [blame] | 5019 |     status_t status; | 
 | 5020 |     if (property_get_bool("af.patch_park", false /* default_value */)) { | 
 | 5021 |         // Park FastMixer to avoid potential DOS issues with writing to the HAL | 
 | 5022 |         // or if HAL does not properly lock against access. | 
 | 5023 |         AutoPark<FastMixer> park(mFastMixer); | 
 | 5024 |         status = PlaybackThread::releaseAudioPatch_l(handle); | 
 | 5025 |     } else { | 
 | 5026 |         status = PlaybackThread::releaseAudioPatch_l(handle); | 
 | 5027 |     } | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 5028 |     return status; | 
 | 5029 | } | 
 | 5030 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5031 | status_t PlaybackThread::releaseAudioPatch_l(const audio_patch_handle_t handle) | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5032 | { | 
 | 5033 |     status_t status = NO_ERROR; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 5034 |  | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 5035 |     mPatch = audio_patch{}; | 
 | 5036 |     mOutDeviceTypeAddrs.clear(); | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 5037 |  | 
| Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 5038 |     if (mOutput->audioHwDev->supportsAudioPatches()) { | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 5039 |         sp<DeviceHalInterface> hwDevice = mOutput->audioHwDev->hwDevice(); | 
 | 5040 |         status = hwDevice->releaseAudioPatch(handle); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5041 |     } else { | 
| Ytai Ben-Tsvi | f997ffe | 2022-02-03 16:38:16 -0800 | [diff] [blame] | 5042 |         status = mOutput->stream->legacyReleaseAudioPatch(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5043 |     } | 
| Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 5044 |     // Force meteadata update after a route change | 
 | 5045 |     mActiveTracks.setHasChanged(); | 
 | 5046 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5047 |     return status; | 
 | 5048 | } | 
 | 5049 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5050 | void PlaybackThread::addPatchTrack(const sp<IAfPatchTrack>& track) | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5051 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 5052 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5053 |     mTracks.add(track); | 
 | 5054 | } | 
 | 5055 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5056 | void PlaybackThread::deletePatchTrack(const sp<IAfPatchTrack>& track) | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5057 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 5058 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5059 |     destroyTrack_l(track); | 
 | 5060 | } | 
 | 5061 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5062 | void PlaybackThread::toAudioPortConfig(struct audio_port_config* config) | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5063 | { | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5064 |     ThreadBase::toAudioPortConfig(config); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5065 |     config->role = AUDIO_PORT_ROLE_SOURCE; | 
 | 5066 |     config->ext.mix.hw_module = mOutput->audioHwDev->handle(); | 
 | 5067 |     config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; | 
| Mikhail Naganov | 32abc2b | 2018-05-24 12:57:11 -0700 | [diff] [blame] | 5068 |     if (mOutput && mOutput->flags != AUDIO_OUTPUT_FLAG_NONE) { | 
 | 5069 |         config->config_mask |= AUDIO_PORT_CONFIG_FLAGS; | 
 | 5070 |         config->flags.output = mOutput->flags; | 
 | 5071 |     } | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5072 | } | 
 | 5073 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5074 | // ---------------------------------------------------------------------------- | 
 | 5075 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5076 | /* static */ | 
 | 5077 | sp<IAfPlaybackThread> IAfPlaybackThread::createMixerThread( | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 5078 |         const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut* output, | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5079 |         audio_io_handle_t id, bool systemReady, type_t type, audio_config_base_t* mixerConfig) { | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 5080 |     return sp<MixerThread>::make(afThreadCallback, output, id, systemReady, type, mixerConfig); | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5081 | } | 
 | 5082 |  | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 5083 | MixerThread::MixerThread(const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut* output, | 
| Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 5084 |         audio_io_handle_t id, bool systemReady, type_t type, audio_config_base_t *mixerConfig) | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 5085 |     :   PlaybackThread(afThreadCallback, output, id, type, systemReady, mixerConfig), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5086 |         // mAudioMixer below | 
 | 5087 |         // mFastMixer below | 
| Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 5088 |         mBluetoothLatencyModesEnabled(false), | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 5089 |         mFastMixerFutex(0), | 
 | 5090 |         mMasterMono(false) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5091 |         // mOutputSink below | 
 | 5092 |         // mPipeSink below | 
 | 5093 |         // mNormalSink below | 
 | 5094 | { | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 5095 |     setMasterBalance(afThreadCallback->getMasterBalance_l()); | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 5096 |     ALOGV("MixerThread() id=%d type=%d", id, type); | 
| Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 5097 |     ALOGV("mSampleRate=%u, mChannelMask=%#x, mChannelCount=%u, mFormat=%#x, mFrameSize=%zu, " | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 5098 |             "mFrameCount=%zu, mNormalFrameCount=%zu", | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5099 |             mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount, | 
 | 5100 |             mNormalFrameCount); | 
 | 5101 |     mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); | 
 | 5102 |  | 
| Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 5103 |     if (type == DUPLICATING) { | 
 | 5104 |         // The Duplicating thread uses the AudioMixer and delivers data to OutputTracks | 
 | 5105 |         // (downstream MixerThreads) in DuplicatingThread::threadLoop_write(). | 
 | 5106 |         // Do not create or use mFastMixer, mOutputSink, mPipeSink, or mNormalSink. | 
 | 5107 |         return; | 
 | 5108 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5109 |     // create an NBAIO sink for the HAL output stream, and negotiate | 
| Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 5110 |     mOutputSink = new AudioStreamOutSink(output->stream); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5111 |     size_t numCounterOffers = 0; | 
| jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 5112 |     const NBAIO_Format offers[1] = {Format_from_SR_C( | 
 | 5113 |             mSampleRate, mChannelCount + mHapticChannelCount, mFormat)}; | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5114 | #if !LOG_NDEBUG | 
 | 5115 |     ssize_t index = | 
 | 5116 | #else | 
 | 5117 |     (void) | 
 | 5118 | #endif | 
 | 5119 |             mOutputSink->negotiate(offers, 1, NULL, numCounterOffers); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5120 |     ALOG_ASSERT(index == 0); | 
 | 5121 |  | 
 | 5122 |     // initialize fast mixer depending on configuration | 
 | 5123 |     bool initFastMixer; | 
| jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 5124 |     if (mType == SPATIALIZER || mType == BIT_PERFECT) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5125 |         initFastMixer = false; | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 5126 |     } else { | 
 | 5127 |         switch (kUseFastMixer) { | 
 | 5128 |         case FastMixer_Never: | 
 | 5129 |             initFastMixer = false; | 
 | 5130 |             break; | 
 | 5131 |         case FastMixer_Always: | 
 | 5132 |             initFastMixer = true; | 
 | 5133 |             break; | 
 | 5134 |         case FastMixer_Static: | 
 | 5135 |         case FastMixer_Dynamic: | 
 | 5136 |             initFastMixer = mFrameCount < mNormalFrameCount; | 
 | 5137 |             break; | 
 | 5138 |         } | 
 | 5139 |         ALOGW_IF(initFastMixer == false && mFrameCount < mNormalFrameCount, | 
 | 5140 |                 "FastMixer is preferred for this sink as frameCount %zu is less than threshold %zu", | 
 | 5141 |                 mFrameCount, mNormalFrameCount); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5142 |     } | 
 | 5143 |     if (initFastMixer) { | 
| Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 5144 |         audio_format_t fastMixerFormat; | 
 | 5145 |         if (mMixerBufferEnabled && mEffectBufferEnabled) { | 
 | 5146 |             fastMixerFormat = AUDIO_FORMAT_PCM_FLOAT; | 
 | 5147 |         } else { | 
 | 5148 |             fastMixerFormat = AUDIO_FORMAT_PCM_16_BIT; | 
 | 5149 |         } | 
 | 5150 |         if (mFormat != fastMixerFormat) { | 
 | 5151 |             // change our Sink format to accept our intermediate precision | 
 | 5152 |             mFormat = fastMixerFormat; | 
 | 5153 |             free(mSinkBuffer); | 
| jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 5154 |             mFrameSize = audio_bytes_per_frame(mChannelCount + mHapticChannelCount, mFormat); | 
| Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 5155 |             const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; | 
 | 5156 |             (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); | 
 | 5157 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5158 |  | 
 | 5159 |         // create a MonoPipe to connect our submix to FastMixer | 
 | 5160 |         NBAIO_Format format = mOutputSink->format(); | 
| Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 5161 |  | 
| Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 5162 |         // adjust format to match that of the Fast Mixer | 
| Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 5163 |         ALOGV("format changed from %#x to %#x", format.mFormat, fastMixerFormat); | 
| Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 5164 |         format.mFormat = fastMixerFormat; | 
 | 5165 |         format.mFrameSize = audio_bytes_per_sample(format.mFormat) * format.mChannelCount; | 
 | 5166 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5167 |         // This pipe depth compensates for scheduling latency of the normal mixer thread. | 
 | 5168 |         // When it wakes up after a maximum latency, it runs a few cycles quickly before | 
 | 5169 |         // finally blocking.  Note the pipe implementation rounds up the request to a power of 2. | 
 | 5170 |         MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/); | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 5171 |         const NBAIO_Format offersFast[1] = {format}; | 
 | 5172 |         size_t numCounterOffersFast = 0; | 
| Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 5173 | #if !LOG_NDEBUG | 
| Eric Laurent | 1e28aaa | 2023-04-16 19:34:23 +0200 | [diff] [blame] | 5174 |         index = | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5175 | #else | 
 | 5176 |         (void) | 
 | 5177 | #endif | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 5178 |                 monoPipe->negotiate(offersFast, std::size(offersFast), | 
 | 5179 |                         nullptr /* counterOffers */, numCounterOffersFast); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5180 |         ALOG_ASSERT(index == 0); | 
 | 5181 |         monoPipe->setAvgFrames((mScreenState & 1) ? | 
 | 5182 |                 (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); | 
 | 5183 |         mPipeSink = monoPipe; | 
 | 5184 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5185 |         // create fast mixer and configure it initially with just one fast track for our submix | 
| Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 5186 |         mFastMixer = new FastMixer(mId); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5187 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 5188 | #ifdef STATE_QUEUE_DUMP | 
 | 5189 |         sq->setObserverDump(&mStateQueueObserverDump); | 
 | 5190 |         sq->setMutatorDump(&mStateQueueMutatorDump); | 
 | 5191 | #endif | 
 | 5192 |         FastMixerState *state = sq->begin(); | 
 | 5193 |         FastTrack *fastTrack = &state->mFastTracks[0]; | 
 | 5194 |         // wrap the source side of the MonoPipe to make it an AudioBufferProvider | 
 | 5195 |         fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe)); | 
 | 5196 |         fastTrack->mVolumeProvider = NULL; | 
| Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 5197 |         fastTrack->mChannelMask = static_cast<audio_channel_mask_t>( | 
 | 5198 |                 mChannelMask | mHapticChannelMask); // mPipeSink channel mask for | 
 | 5199 |                                                     // audio to FastMixer | 
| Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 5200 |         fastTrack->mFormat = mFormat; // mPipeSink format for audio to FastMixer | 
| jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 5201 |         fastTrack->mHapticPlaybackEnabled = mHapticChannelMask != AUDIO_CHANNEL_NONE; | 
| Ahmad Khalil | 229466a | 2024-02-05 12:15:30 +0000 | [diff] [blame] | 5202 |         fastTrack->mHapticScale = {/*level=*/os::HapticLevel::NONE }; | 
| Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 5203 |         fastTrack->mHapticMaxAmplitude = NAN; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5204 |         fastTrack->mGeneration++; | 
 | 5205 |         state->mFastTracksGen++; | 
 | 5206 |         state->mTrackMask = 1; | 
 | 5207 |         // fast mixer will use the HAL output sink | 
 | 5208 |         state->mOutputSink = mOutputSink.get(); | 
 | 5209 |         state->mOutputSinkGen++; | 
 | 5210 |         state->mFrameCount = mFrameCount; | 
| jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 5211 |         // specify sink channel mask when haptic channel mask present as it can not | 
 | 5212 |         // be calculated directly from channel count | 
 | 5213 |         state->mSinkChannelMask = mHapticChannelMask == AUDIO_CHANNEL_NONE | 
| Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 5214 |                 ? AUDIO_CHANNEL_NONE | 
 | 5215 |                 : static_cast<audio_channel_mask_t>(mChannelMask | mHapticChannelMask); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5216 |         state->mCommand = FastMixerState::COLD_IDLE; | 
 | 5217 |         // already done in constructor initialization list | 
 | 5218 |         //mFastMixerFutex = 0; | 
 | 5219 |         state->mColdFutexAddr = &mFastMixerFutex; | 
 | 5220 |         state->mColdGen++; | 
 | 5221 |         state->mDumpState = &mFastMixerDumpState; | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 5222 |         mFastMixerNBLogWriter = afThreadCallback->newWriter_l(kFastMixerLogSize, "FastMixer"); | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 5223 |         state->mNBLogWriter = mFastMixerNBLogWriter.get(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5224 |         sq->end(); | 
 | 5225 |         sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); | 
 | 5226 |  | 
| Eric Tan | 0513b5d | 2018-09-17 10:32:48 -0700 | [diff] [blame] | 5227 |         NBLog::thread_info_t info; | 
 | 5228 |         info.id = mId; | 
 | 5229 |         info.type = NBLog::FASTMIXER; | 
 | 5230 |         mFastMixerNBLogWriter->log<NBLog::EVENT_THREAD_INFO>(info); | 
 | 5231 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5232 |         // start the fast mixer | 
 | 5233 |         mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO); | 
 | 5234 |         pid_t tid = mFastMixer->getTid(); | 
| Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 5235 |         sendPrioConfigEvent(getpid(), tid, kPriorityFastMixer, false /*forApp*/); | 
| Mikhail Naganov | e1c4b5d | 2016-12-22 09:22:45 -0800 | [diff] [blame] | 5236 |         stream()->setHalThreadPriority(kPriorityFastMixer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5237 |  | 
 | 5238 | #ifdef AUDIO_WATCHDOG | 
 | 5239 |         // create and start the watchdog | 
 | 5240 |         mAudioWatchdog = new AudioWatchdog(); | 
 | 5241 |         mAudioWatchdog->setDump(&mAudioWatchdogDump); | 
 | 5242 |         mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO); | 
 | 5243 |         tid = mAudioWatchdog->getTid(); | 
| Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 5244 |         sendPrioConfigEvent(getpid(), tid, kPriorityFastMixer, false /*forApp*/); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5245 | #endif | 
| Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 5246 |     } else { | 
 | 5247 | #ifdef TEE_SINK | 
 | 5248 |         // Only use the MixerThread tee if there is no FastMixer. | 
 | 5249 |         mTee.set(mOutputSink->format(), NBAIO_Tee::TEE_FLAG_OUTPUT_THREAD); | 
 | 5250 |         mTee.setId(std::string("_") + std::to_string(mId) + "_M"); | 
 | 5251 | #endif | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5252 |     } | 
 | 5253 |  | 
 | 5254 |     switch (kUseFastMixer) { | 
 | 5255 |     case FastMixer_Never: | 
 | 5256 |     case FastMixer_Dynamic: | 
 | 5257 |         mNormalSink = mOutputSink; | 
 | 5258 |         break; | 
 | 5259 |     case FastMixer_Always: | 
 | 5260 |         mNormalSink = mPipeSink; | 
 | 5261 |         break; | 
 | 5262 |     case FastMixer_Static: | 
 | 5263 |         mNormalSink = initFastMixer ? mPipeSink : mOutputSink; | 
 | 5264 |         break; | 
 | 5265 |     } | 
 | 5266 | } | 
 | 5267 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5268 | MixerThread::~MixerThread() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5269 | { | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 5270 |     if (mFastMixer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5271 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 5272 |         FastMixerState *state = sq->begin(); | 
 | 5273 |         if (state->mCommand == FastMixerState::COLD_IDLE) { | 
 | 5274 |             int32_t old = android_atomic_inc(&mFastMixerFutex); | 
 | 5275 |             if (old == -1) { | 
| Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 5276 |                 (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5277 |             } | 
 | 5278 |         } | 
 | 5279 |         state->mCommand = FastMixerState::EXIT; | 
 | 5280 |         sq->end(); | 
 | 5281 |         sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); | 
 | 5282 |         mFastMixer->join(); | 
 | 5283 |         // Though the fast mixer thread has exited, it's state queue is still valid. | 
 | 5284 |         // We'll use that extract the final state which contains one remaining fast track | 
 | 5285 |         // corresponding to our sub-mix. | 
 | 5286 |         state = sq->begin(); | 
 | 5287 |         ALOG_ASSERT(state->mTrackMask == 1); | 
 | 5288 |         FastTrack *fastTrack = &state->mFastTracks[0]; | 
 | 5289 |         ALOG_ASSERT(fastTrack->mBufferProvider != NULL); | 
 | 5290 |         delete fastTrack->mBufferProvider; | 
 | 5291 |         sq->end(false /*didModify*/); | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 5292 |         mFastMixer.clear(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5293 | #ifdef AUDIO_WATCHDOG | 
 | 5294 |         if (mAudioWatchdog != 0) { | 
 | 5295 |             mAudioWatchdog->requestExit(); | 
 | 5296 |             mAudioWatchdog->requestExitAndWait(); | 
 | 5297 |             mAudioWatchdog.clear(); | 
 | 5298 |         } | 
 | 5299 | #endif | 
 | 5300 |     } | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 5301 |     mAfThreadCallback->unregisterWriter(mFastMixerNBLogWriter); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5302 |     delete mAudioMixer; | 
 | 5303 | } | 
 | 5304 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5305 | void MixerThread::onFirstRef() { | 
| Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 5306 |     PlaybackThread::onFirstRef(); | 
 | 5307 |  | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 5308 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 5309 |     if (mOutput != nullptr && mOutput->stream != nullptr) { | 
 | 5310 |         status_t status = mOutput->stream->setLatencyModeCallback(this); | 
 | 5311 |         if (status != INVALID_OPERATION) { | 
 | 5312 |             updateHalSupportedLatencyModes_l(); | 
 | 5313 |         } | 
 | 5314 |         // Default to enabled if the HAL supports it. This can be changed by Audioflinger after | 
 | 5315 |         // the thread construction according to AudioFlinger::mBluetoothLatencyModesEnabled | 
 | 5316 |         mBluetoothLatencyModesEnabled.store( | 
 | 5317 |                 mOutput->audioHwDev->supportsBluetoothVariableLatency()); | 
 | 5318 |     } | 
 | 5319 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5320 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5321 | uint32_t MixerThread::correctLatency_l(uint32_t latency) const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5322 | { | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 5323 |     if (mFastMixer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5324 |         MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); | 
 | 5325 |         latency += (pipe->getAvgFrames() * 1000) / mSampleRate; | 
 | 5326 |     } | 
 | 5327 |     return latency; | 
 | 5328 | } | 
 | 5329 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5330 | ssize_t MixerThread::threadLoop_write() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5331 | { | 
 | 5332 |     // FIXME we should only do one push per cycle; confirm this is true | 
 | 5333 |     // Start the fast mixer if it's not already running | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 5334 |     if (mFastMixer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5335 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 5336 |         FastMixerState *state = sq->begin(); | 
 | 5337 |         if (state->mCommand != FastMixerState::MIX_WRITE && | 
 | 5338 |                 (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) { | 
 | 5339 |             if (state->mCommand == FastMixerState::COLD_IDLE) { | 
| Eric Laurent | a2ab450 | 2015-09-09 12:25:51 -0700 | [diff] [blame] | 5340 |  | 
 | 5341 |                 // FIXME workaround for first HAL write being CPU bound on some devices | 
 | 5342 |                 ATRACE_BEGIN("write"); | 
 | 5343 |                 mOutput->write((char *)mSinkBuffer, 0); | 
 | 5344 |                 ATRACE_END(); | 
 | 5345 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5346 |                 int32_t old = android_atomic_inc(&mFastMixerFutex); | 
 | 5347 |                 if (old == -1) { | 
| Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 5348 |                     (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5349 |                 } | 
 | 5350 | #ifdef AUDIO_WATCHDOG | 
 | 5351 |                 if (mAudioWatchdog != 0) { | 
 | 5352 |                     mAudioWatchdog->resume(); | 
 | 5353 |                 } | 
 | 5354 | #endif | 
 | 5355 |             } | 
 | 5356 |             state->mCommand = FastMixerState::MIX_WRITE; | 
| Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 5357 | #ifdef FAST_THREAD_STATISTICS | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 5358 |             mFastMixerDumpState.increaseSamplingN(mAfThreadCallback->isLowRamDevice() ? | 
| Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 5359 |                 FastThreadDumpState::kSamplingNforLowRamDevice : FastThreadDumpState::kSamplingN); | 
| Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 5360 | #endif | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5361 |             sq->end(); | 
 | 5362 |             sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); | 
 | 5363 |             if (kUseFastMixer == FastMixer_Dynamic) { | 
 | 5364 |                 mNormalSink = mPipeSink; | 
 | 5365 |             } | 
 | 5366 |         } else { | 
 | 5367 |             sq->end(false /*didModify*/); | 
 | 5368 |         } | 
 | 5369 |     } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5370 |     return PlaybackThread::threadLoop_write(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5371 | } | 
 | 5372 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5373 | void MixerThread::threadLoop_standby() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5374 | { | 
 | 5375 |     // Idle the fast mixer if it's currently running | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 5376 |     if (mFastMixer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5377 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 5378 |         FastMixerState *state = sq->begin(); | 
 | 5379 |         if (!(state->mCommand & FastMixerState::IDLE)) { | 
| Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 5380 |             // Report any frames trapped in the Monopipe | 
 | 5381 |             MonoPipe *monoPipe = (MonoPipe *)mPipeSink.get(); | 
 | 5382 |             const long long pipeFrames = monoPipe->maxFrames() - monoPipe->availableToWrite(); | 
 | 5383 |             mLocalLog.log("threadLoop_standby: framesWritten:%lld  suspendedFrames:%lld  " | 
 | 5384 |                     "monoPipeWritten:%lld  monoPipeLeft:%lld", | 
 | 5385 |                     (long long)mFramesWritten, (long long)mSuspendedFrames, | 
 | 5386 |                     (long long)mPipeSink->framesWritten(), pipeFrames); | 
 | 5387 |             mLocalLog.log("threadLoop_standby: %s", mTimestamp.toString().c_str()); | 
 | 5388 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5389 |             state->mCommand = FastMixerState::COLD_IDLE; | 
 | 5390 |             state->mColdFutexAddr = &mFastMixerFutex; | 
 | 5391 |             state->mColdGen++; | 
 | 5392 |             mFastMixerFutex = 0; | 
 | 5393 |             sq->end(); | 
 | 5394 |             // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now | 
 | 5395 |             sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); | 
 | 5396 |             if (kUseFastMixer == FastMixer_Dynamic) { | 
 | 5397 |                 mNormalSink = mOutputSink; | 
 | 5398 |             } | 
 | 5399 | #ifdef AUDIO_WATCHDOG | 
 | 5400 |             if (mAudioWatchdog != 0) { | 
 | 5401 |                 mAudioWatchdog->pause(); | 
 | 5402 |             } | 
 | 5403 | #endif | 
 | 5404 |         } else { | 
 | 5405 |             sq->end(false /*didModify*/); | 
 | 5406 |         } | 
 | 5407 |     } | 
 | 5408 |     PlaybackThread::threadLoop_standby(); | 
 | 5409 | } | 
 | 5410 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5411 | bool PlaybackThread::waitingAsyncCallback_l() | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5412 | { | 
 | 5413 |     return false; | 
 | 5414 | } | 
 | 5415 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5416 | bool PlaybackThread::shouldStandby_l() | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5417 | { | 
 | 5418 |     return !mStandby; | 
 | 5419 | } | 
 | 5420 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5421 | bool PlaybackThread::waitingAsyncCallback() | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5422 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 5423 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5424 |     return waitingAsyncCallback_l(); | 
 | 5425 | } | 
 | 5426 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5427 | // shared by MIXER and DIRECT, overridden by DUPLICATING | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5428 | void PlaybackThread::threadLoop_standby() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5429 | { | 
| Andy Hung | 160664b | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 5430 |     ALOGV("%s: audio hardware entering standby, mixer %p, suspend count %d", | 
 | 5431 |             __func__, this, (int32_t)mSuspended); | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5432 |     mOutput->standby(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5433 |     if (mUseAsyncWrite != 0) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5434 |         // discard any pending drain or write ack by incrementing sequence | 
 | 5435 |         mWriteAckSequence = (mWriteAckSequence + 2) & ~1; | 
 | 5436 |         mDrainSequence = (mDrainSequence + 2) & ~1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5437 |         ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5438 |         mCallbackThread->setWriteBlocked(mWriteAckSequence); | 
 | 5439 |         mCallbackThread->setDraining(mDrainSequence); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5440 |     } | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5441 |     mHwPaused = false; | 
| Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 5442 |     setHalLatencyMode_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5443 | } | 
 | 5444 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5445 | void PlaybackThread::onAddNewTrack_l() | 
| Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 5446 | { | 
 | 5447 |     ALOGV("signal playback thread"); | 
 | 5448 |     broadcast_l(); | 
 | 5449 | } | 
 | 5450 |  | 
| Mikhail Naganov | f548cd3 | 2024-05-29 17:06:46 +0000 | [diff] [blame] | 5451 | void PlaybackThread::onAsyncError(bool isHardError) | 
| Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5452 | { | 
| Mikhail Naganov | f548cd3 | 2024-05-29 17:06:46 +0000 | [diff] [blame] | 5453 |     auto allTrackPortIds = getTrackPortIds(); | 
| Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5454 |     for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) { | 
 | 5455 |         invalidateTracks((audio_stream_type_t)i); | 
 | 5456 |     } | 
| Mikhail Naganov | f548cd3 | 2024-05-29 17:06:46 +0000 | [diff] [blame] | 5457 |     if (isHardError) { | 
 | 5458 |         mAfThreadCallback->onHardError(allTrackPortIds); | 
 | 5459 |     } | 
| Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5460 | } | 
 | 5461 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5462 | void MixerThread::threadLoop_mix() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5463 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5464 |     // mix buffers... | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 5465 |     mAudioMixer->process(); | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5466 |     mCurrentWriteLength = mSinkBufferSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5467 |     // increase sleep time progressively when application underrun condition clears. | 
 | 5468 |     // Only increase sleep time if the mixer is ready for two consecutive times to avoid | 
 | 5469 |     // that a steady state of alternating ready/not ready conditions keeps the sleep time | 
 | 5470 |     // such that we would underrun the audio HAL. | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5471 |     if ((mSleepTimeUs == 0) && (sleepTimeShift > 0)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5472 |         sleepTimeShift--; | 
 | 5473 |     } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5474 |     mSleepTimeUs = 0; | 
 | 5475 |     mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5476 |     //TODO: delay standby when effects have a tail | 
| Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 5477 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5478 | } | 
 | 5479 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5480 | void MixerThread::threadLoop_sleepTime() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5481 | { | 
 | 5482 |     // If no tracks are ready, sleep once for the duration of an output | 
 | 5483 |     // buffer size, then write 0s to the output | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5484 |     if (mSleepTimeUs == 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5485 |         if (mMixerStatus == MIXER_TRACKS_ENABLED) { | 
| Andy Hung | 06d1322 | 2018-05-03 20:13:31 -0700 | [diff] [blame] | 5486 |             if (mPipeSink.get() != nullptr && mPipeSink == mNormalSink) { | 
 | 5487 |                 // Using the Monopipe availableToWrite, we estimate the | 
 | 5488 |                 // sleep time to retry for more data (before we underrun). | 
 | 5489 |                 MonoPipe *monoPipe = static_cast<MonoPipe *>(mPipeSink.get()); | 
 | 5490 |                 const ssize_t availableToWrite = mPipeSink->availableToWrite(); | 
 | 5491 |                 const size_t pipeFrames = monoPipe->maxFrames(); | 
 | 5492 |                 const size_t framesLeft = pipeFrames - max(availableToWrite, 0); | 
 | 5493 |                 // HAL_framecount <= framesDelay ~ framesLeft / 2 <= Normal_Mixer_framecount | 
 | 5494 |                 const size_t framesDelay = std::min( | 
 | 5495 |                         mNormalFrameCount, max(framesLeft / 2, mFrameCount)); | 
 | 5496 |                 ALOGV("pipeFrames:%zu framesLeft:%zu framesDelay:%zu", | 
 | 5497 |                         pipeFrames, framesLeft, framesDelay); | 
 | 5498 |                 mSleepTimeUs = framesDelay * MICROS_PER_SECOND / mSampleRate; | 
 | 5499 |             } else { | 
 | 5500 |                 mSleepTimeUs = mActiveSleepTimeUs >> sleepTimeShift; | 
 | 5501 |                 if (mSleepTimeUs < kMinThreadSleepTimeUs) { | 
 | 5502 |                     mSleepTimeUs = kMinThreadSleepTimeUs; | 
 | 5503 |                 } | 
 | 5504 |                 // reduce sleep time in case of consecutive application underruns to avoid | 
 | 5505 |                 // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer | 
 | 5506 |                 // duration we would end up writing less data than needed by the audio HAL if | 
 | 5507 |                 // the condition persists. | 
 | 5508 |                 if (sleepTimeShift < kMaxThreadSleepTimeShift) { | 
 | 5509 |                     sleepTimeShift++; | 
 | 5510 |                 } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5511 |             } | 
 | 5512 |         } else { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5513 |             mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5514 |         } | 
 | 5515 |     } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) { | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 5516 |         // clear out mMixerBuffer or mSinkBuffer, to ensure buffers are cleared | 
 | 5517 |         // before effects processing or output. | 
 | 5518 |         if (mMixerBufferValid) { | 
 | 5519 |             memset(mMixerBuffer, 0, mMixerBufferSize); | 
| Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5520 |             if (mType == SPATIALIZER) { | 
 | 5521 |                 memset(mSinkBuffer, 0, mSinkBufferSize); | 
 | 5522 |             } | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 5523 |         } else { | 
 | 5524 |             memset(mSinkBuffer, 0, mSinkBufferSize); | 
 | 5525 |         } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5526 |         mSleepTimeUs = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5527 |         ALOGV_IF(mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED), | 
 | 5528 |                 "anticipated start"); | 
 | 5529 |     } | 
 | 5530 |     // TODO add standby time extension fct of effect tail | 
 | 5531 | } | 
 | 5532 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 5533 | // prepareTracks_l() must be called with ThreadBase::mutex() held | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5534 | PlaybackThread::mixer_state MixerThread::prepareTracks_l( | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5535 |         Vector<sp<IAfTrack>>* tracksToRemove) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5536 | { | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5537 |     // clean up deleted track ids in AudioMixer before allocating new tracks | 
 | 5538 |     (void)mTracks.processDeletedTrackIds([this](int trackId) { | 
 | 5539 |         // for each trackId, destroy it in the AudioMixer | 
 | 5540 |         if (mAudioMixer->exists(trackId)) { | 
 | 5541 |             mAudioMixer->destroy(trackId); | 
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5542 |         } | 
 | 5543 |     }); | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5544 |     mTracks.clearDeletedTrackIds(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5545 |  | 
 | 5546 |     mixer_state mixerStatus = MIXER_IDLE; | 
 | 5547 |     // find out which tracks need to be processed | 
 | 5548 |     size_t count = mActiveTracks.size(); | 
 | 5549 |     size_t mixedTracks = 0; | 
 | 5550 |     size_t tracksWithEffect = 0; | 
 | 5551 |     // counts only _active_ fast tracks | 
 | 5552 |     size_t fastTracks = 0; | 
 | 5553 |     uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset | 
 | 5554 |  | 
 | 5555 |     float masterVolume = mMasterVolume; | 
 | 5556 |     bool masterMute = mMasterMute; | 
 | 5557 |  | 
 | 5558 |     if (masterMute) { | 
 | 5559 |         masterVolume = 0; | 
 | 5560 |     } | 
 | 5561 |     // Delegate master volume control to effect in output mix effect chain if needed | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 5562 |     sp<IAfEffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5563 |     if (chain != 0) { | 
 | 5564 |         uint32_t v = (uint32_t)(masterVolume * (1 << 24)); | 
| Shunkai Yao | f484765 | 2024-01-12 00:25:20 +0000 | [diff] [blame] | 5565 |         chain->setVolume(&v, &v); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5566 |         masterVolume = (float)((v + (1 << 23)) >> 24); | 
 | 5567 |         chain.clear(); | 
 | 5568 |     } | 
 | 5569 |  | 
 | 5570 |     // prepare a new state to push | 
 | 5571 |     FastMixerStateQueue *sq = NULL; | 
 | 5572 |     FastMixerState *state = NULL; | 
 | 5573 |     bool didModify = false; | 
 | 5574 |     FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED; | 
| Andy Hung | f228531 | 2017-02-14 18:31:59 -0800 | [diff] [blame] | 5575 |     bool coldIdle = false; | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 5576 |     if (mFastMixer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5577 |         sq = mFastMixer->sq(); | 
 | 5578 |         state = sq->begin(); | 
| Andy Hung | f228531 | 2017-02-14 18:31:59 -0800 | [diff] [blame] | 5579 |         coldIdle = state->mCommand == FastMixerState::COLD_IDLE; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5580 |     } | 
 | 5581 |  | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 5582 |     mMixerBufferValid = false;  // mMixerBuffer has no valid data until appropriate tracks found. | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 5583 |     mEffectBufferValid = false; // mEffectBuffer has no valid data until tracks found. | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 5584 |  | 
| Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 5585 |     // DeferredOperations handles statistics after setting mixerStatus. | 
 | 5586 |     class DeferredOperations { | 
 | 5587 |     public: | 
| Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 5588 |         DeferredOperations(mixer_state *mixerStatus, ThreadMetrics *threadMetrics) | 
 | 5589 |             : mMixerStatus(mixerStatus) | 
 | 5590 |             , mThreadMetrics(threadMetrics) {} | 
| Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 5591 |  | 
 | 5592 |         // when leaving scope, tally frames properly. | 
 | 5593 |         ~DeferredOperations() { | 
 | 5594 |             // Tally underrun frames only if we are actually mixing (MIXER_TRACKS_READY) | 
 | 5595 |             // because that is when the underrun occurs. | 
 | 5596 |             // We do not distinguish between FastTracks and NormalTracks here. | 
| Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 5597 |             size_t maxUnderrunFrames = 0; | 
| Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 5598 |             if (*mMixerStatus == MIXER_TRACKS_READY && mUnderrunFrames.size() > 0) { | 
| Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 5599 |                 for (const auto &underrun : mUnderrunFrames) { | 
| Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 5600 |                     underrun.first->tallyUnderrunFrames(underrun.second); | 
| Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 5601 |                     maxUnderrunFrames = max(underrun.second, maxUnderrunFrames); | 
| Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 5602 |                 } | 
 | 5603 |             } | 
| Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 5604 |             // send the max underrun frames for this mixer period | 
 | 5605 |             mThreadMetrics->logUnderrunFrames(maxUnderrunFrames); | 
| Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 5606 |         } | 
 | 5607 |  | 
 | 5608 |         // tallyUnderrunFrames() is called to update the track counters | 
 | 5609 |         // with the number of underrun frames for a particular mixer period. | 
 | 5610 |         // We defer tallying until we know the final mixer status. | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5611 |         void tallyUnderrunFrames(const sp<IAfTrack>& track, size_t underrunFrames) { | 
| Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 5612 |             mUnderrunFrames.emplace_back(track, underrunFrames); | 
 | 5613 |         } | 
 | 5614 |  | 
 | 5615 |     private: | 
 | 5616 |         const mixer_state * const mMixerStatus; | 
| Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 5617 |         ThreadMetrics * const mThreadMetrics; | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5618 |         std::vector<std::pair<sp<IAfTrack>, size_t>> mUnderrunFrames; | 
| Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 5619 |     } deferredOperations(&mixerStatus, &mThreadMetrics); | 
| Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 5620 |     // implicit nested scope for variable capture | 
| Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 5621 |  | 
| jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 5622 |     bool noFastHapticTrack = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5623 |     for (size_t i=0 ; i<count ; i++) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5624 |         const sp<IAfTrack> t = mActiveTracks[i]; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5625 |  | 
 | 5626 |         // this const just means the local variable doesn't change | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5627 |         IAfTrack* const track = t.get(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5628 |  | 
 | 5629 |         // process fast tracks | 
 | 5630 |         if (track->isFastTrack()) { | 
| Andy Hung | ae22b48 | 2019-05-09 15:38:55 -0700 | [diff] [blame] | 5631 |             LOG_ALWAYS_FATAL_IF(mFastMixer.get() == nullptr, | 
 | 5632 |                     "%s(%d): FastTrack(%d) present without FastMixer", | 
 | 5633 |                      __func__, id(), track->id()); | 
 | 5634 |  | 
| jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 5635 |             if (track->getHapticPlaybackEnabled()) { | 
 | 5636 |                 noFastHapticTrack = false; | 
 | 5637 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5638 |  | 
 | 5639 |             // It's theoretically possible (though unlikely) for a fast track to be created | 
 | 5640 |             // and then removed within the same normal mix cycle.  This is not a problem, as | 
 | 5641 |             // the track never becomes active so it's fast mixer slot is never touched. | 
 | 5642 |             // The converse, of removing an (active) track and then creating a new track | 
 | 5643 |             // at the identical fast mixer slot within the same normal mix cycle, | 
 | 5644 |             // is impossible because the slot isn't marked available until the end of each cycle. | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5645 |             int j = track->fastIndex(); | 
| Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 5646 |             ALOG_ASSERT(0 < j && j < (int)FastMixerState::sMaxFastTracks); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5647 |             ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j))); | 
 | 5648 |             FastTrack *fastTrack = &state->mFastTracks[j]; | 
 | 5649 |  | 
 | 5650 |             // Determine whether the track is currently in underrun condition, | 
 | 5651 |             // and whether it had a recent underrun. | 
 | 5652 |             FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j]; | 
 | 5653 |             FastTrackUnderruns underruns = ftDump->mUnderruns; | 
 | 5654 |             uint32_t recentFull = (underruns.mBitFields.mFull - | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5655 |                     track->fastTrackUnderruns().mBitFields.mFull) & UNDERRUN_MASK; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5656 |             uint32_t recentPartial = (underruns.mBitFields.mPartial - | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5657 |                     track->fastTrackUnderruns().mBitFields.mPartial) & UNDERRUN_MASK; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5658 |             uint32_t recentEmpty = (underruns.mBitFields.mEmpty - | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5659 |                     track->fastTrackUnderruns().mBitFields.mEmpty) & UNDERRUN_MASK; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5660 |             uint32_t recentUnderruns = recentPartial + recentEmpty; | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5661 |             track->fastTrackUnderruns() = underruns; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5662 |             // don't count underruns that occur while stopping or pausing | 
 | 5663 |             // or stopped which can occur when flush() is called while active | 
| Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 5664 |             size_t underrunFrames = 0; | 
| Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 5665 |             if (!(track->isStopping() || track->isPausing() || track->isStopped()) && | 
 | 5666 |                     recentUnderruns > 0) { | 
 | 5667 |                 // FIXME fast mixer will pull & mix partial buffers, but we count as a full underrun | 
| Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 5668 |                 underrunFrames = recentUnderruns * mFrameCount; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5669 |             } | 
| Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 5670 |             // Immediately account for FastTrack underruns. | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5671 |             track->audioTrackServerProxy()->tallyUnderrunFrames(underrunFrames); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5672 |  | 
 | 5673 |             // This is similar to the state machine for normal tracks, | 
 | 5674 |             // with a few modifications for fast tracks. | 
 | 5675 |             bool isActive = true; | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5676 |             switch (track->state()) { | 
 | 5677 |             case IAfTrackBase::STOPPING_1: | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5678 |                 // track stays active in STOPPING_1 state until first underrun | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5679 |                 if (recentUnderruns > 0 || track->isTerminated()) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5680 |                     track->setState(IAfTrackBase::STOPPING_2); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5681 |                 } | 
 | 5682 |                 break; | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5683 |             case IAfTrackBase::PAUSING: | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5684 |                 // ramp down is not yet implemented | 
 | 5685 |                 track->setPaused(); | 
 | 5686 |                 break; | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5687 |             case IAfTrackBase::RESUMING: | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5688 |                 // ramp up is not yet implemented | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5689 |                 track->setState(IAfTrackBase::ACTIVE); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5690 |                 break; | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5691 |             case IAfTrackBase::ACTIVE: | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5692 |                 if (recentFull > 0 || recentPartial > 0) { | 
 | 5693 |                     // track has provided at least some frames recently: reset retry count | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5694 |                     track->retryCount() = kMaxTrackRetries; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5695 |                 } | 
 | 5696 |                 if (recentUnderruns == 0) { | 
 | 5697 |                     // no recent underruns: stay active | 
 | 5698 |                     break; | 
 | 5699 |                 } | 
 | 5700 |                 // there has recently been an underrun of some kind | 
 | 5701 |                 if (track->sharedBuffer() == 0) { | 
 | 5702 |                     // were any of the recent underruns "empty" (no frames available)? | 
 | 5703 |                     if (recentEmpty == 0) { | 
 | 5704 |                         // no, then ignore the partial underruns as they are allowed indefinitely | 
 | 5705 |                         break; | 
 | 5706 |                     } | 
 | 5707 |                     // there has recently been an "empty" underrun: decrement the retry counter | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5708 |                     if (--(track->retryCount()) > 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5709 |                         break; | 
 | 5710 |                     } | 
 | 5711 |                     // indicate to client process that the track was disabled because of underrun; | 
 | 5712 |                     // it will then automatically call start() when data is available | 
| Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 5713 |                     track->disable(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5714 |                     // remove from active list, but state remains ACTIVE [confusing but true] | 
 | 5715 |                     isActive = false; | 
 | 5716 |                     break; | 
 | 5717 |                 } | 
| Chih-Hung Hsieh | 2b48703 | 2018-09-13 14:16:02 -0700 | [diff] [blame] | 5718 |                 FALLTHROUGH_INTENDED; | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5719 |             case IAfTrackBase::STOPPING_2: | 
 | 5720 |             case IAfTrackBase::PAUSED: | 
 | 5721 |             case IAfTrackBase::STOPPED: | 
 | 5722 |             case IAfTrackBase::FLUSHED:   // flush() while active | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5723 |                 // Check for presentation complete if track is inactive | 
 | 5724 |                 // We have consumed all the buffers of this track. | 
 | 5725 |                 // This would be incomplete if we auto-paused on underrun | 
 | 5726 |                 { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5727 |                     uint32_t latency = 0; | 
 | 5728 |                     status_t result = mOutput->stream->getLatency(&latency); | 
 | 5729 |                     ALOGE_IF(result != OK, | 
 | 5730 |                             "Error when retrieving output stream latency: %d", result); | 
 | 5731 |                     size_t audioHALFrames = (latency * mSampleRate) / 1000; | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 5732 |                     int64_t framesWritten = mBytesWritten / mFrameSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5733 |                     if (!(mStandby || track->presentationComplete(framesWritten, audioHALFrames))) { | 
 | 5734 |                         // track stays in active list until presentation is complete | 
 | 5735 |                         break; | 
 | 5736 |                     } | 
 | 5737 |                 } | 
 | 5738 |                 if (track->isStopping_2()) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5739 |                     track->setState(IAfTrackBase::STOPPED); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5740 |                 } | 
 | 5741 |                 if (track->isStopped()) { | 
 | 5742 |                     // Can't reset directly, as fast mixer is still polling this track | 
 | 5743 |                     //   track->reset(); | 
 | 5744 |                     // So instead mark this track as needing to be reset after push with ack | 
 | 5745 |                     resetMask |= 1 << i; | 
 | 5746 |                 } | 
 | 5747 |                 isActive = false; | 
 | 5748 |                 break; | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5749 |             case IAfTrackBase::IDLE: | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5750 |             default: | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5751 |                 LOG_ALWAYS_FATAL("unexpected track state %d", (int)track->state()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5752 |             } | 
 | 5753 |  | 
 | 5754 |             if (isActive) { | 
 | 5755 |                 // was it previously inactive? | 
 | 5756 |                 if (!(state->mTrackMask & (1 << j))) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5757 |                     ExtendedAudioBufferProvider *eabp = track->asExtendedAudioBufferProvider(); | 
 | 5758 |                     VolumeProvider *vp = track->asVolumeProvider(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5759 |                     fastTrack->mBufferProvider = eabp; | 
 | 5760 |                     fastTrack->mVolumeProvider = vp; | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5761 |                     fastTrack->mChannelMask = track->channelMask(); | 
 | 5762 |                     fastTrack->mFormat = track->format(); | 
| jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 5763 |                     fastTrack->mHapticPlaybackEnabled = track->getHapticPlaybackEnabled(); | 
| Ahmad Khalil | 229466a | 2024-02-05 12:15:30 +0000 | [diff] [blame] | 5764 |                     fastTrack->mHapticScale = track->getHapticScale(); | 
| Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 5765 |                     fastTrack->mHapticMaxAmplitude = track->getHapticMaxAmplitude(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5766 |                     fastTrack->mGeneration++; | 
 | 5767 |                     state->mTrackMask |= 1 << j; | 
 | 5768 |                     didModify = true; | 
 | 5769 |                     // no acknowledgement required for newly active tracks | 
 | 5770 |                 } | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5771 |                 sp<AudioTrackServerProxy> proxy = track->audioTrackServerProxy(); | 
| Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 5772 |                 float volume; | 
 | 5773 |                 if (track->isPlaybackRestricted() || mStreamTypes[track->streamType()].mute) { | 
 | 5774 |                     volume = 0.f; | 
 | 5775 |                 } else { | 
 | 5776 |                     volume = masterVolume * mStreamTypes[track->streamType()].volume; | 
 | 5777 |                 } | 
 | 5778 |  | 
 | 5779 |                 handleVoipVolume_l(&volume); | 
 | 5780 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5781 |                 // cache the combined master volume and stream type volume for fast mixer; this | 
 | 5782 |                 // lacks any synchronization or barrier so VolumeProvider may read a stale value | 
| Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 5783 |                 const float vh = track->getVolumeHandler()->getVolume( | 
| Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 5784 |                     proxy->framesReleased()).first; | 
 | 5785 |                 volume *= vh; | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5786 |                 track->setCachedVolume(volume); | 
| Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 5787 |                 gain_minifloat_packed_t vlr = proxy->getVolumeLR(); | 
| Vlad Popa | e2f5aef | 2022-07-25 16:00:20 +0200 | [diff] [blame] | 5788 |                 float vlf = float_from_gain(gain_minifloat_unpack_left(vlr)); | 
 | 5789 |                 float vrf = float_from_gain(gain_minifloat_unpack_right(vlr)); | 
 | 5790 |  | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 5791 |                 track->processMuteEvent_l(mAfThreadCallback->getOrCreateAudioManager(), | 
| Vlad Popa | e2f5aef | 2022-07-25 16:00:20 +0200 | [diff] [blame] | 5792 |                     /*muteState=*/{masterVolume == 0.f, | 
 | 5793 |                                    mStreamTypes[track->streamType()].volume == 0.f, | 
 | 5794 |                                    mStreamTypes[track->streamType()].mute, | 
 | 5795 |                                    track->isPlaybackRestricted(), | 
| Vlad Popa | 436c917 | 2022-08-02 15:25:08 +0200 | [diff] [blame] | 5796 |                                    vlf == 0.f && vrf == 0.f, | 
 | 5797 |                                    vh == 0.f}); | 
| Vlad Popa | e2f5aef | 2022-07-25 16:00:20 +0200 | [diff] [blame] | 5798 |  | 
 | 5799 |                 vlf *= volume; | 
 | 5800 |                 vrf *= volume; | 
| Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 5801 |  | 
| jiabin | 76d9469 | 2022-12-15 21:51:21 +0000 | [diff] [blame] | 5802 |                 track->setFinalVolume(vlf, vrf); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5803 |                 ++fastTracks; | 
 | 5804 |             } else { | 
 | 5805 |                 // was it previously active? | 
 | 5806 |                 if (state->mTrackMask & (1 << j)) { | 
 | 5807 |                     fastTrack->mBufferProvider = NULL; | 
 | 5808 |                     fastTrack->mGeneration++; | 
 | 5809 |                     state->mTrackMask &= ~(1 << j); | 
 | 5810 |                     didModify = true; | 
 | 5811 |                     // If any fast tracks were removed, we must wait for acknowledgement | 
 | 5812 |                     // because we're about to decrement the last sp<> on those tracks. | 
 | 5813 |                     block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; | 
 | 5814 |                 } else { | 
| Glenn Kasten | bf848af | 2017-12-22 11:55:01 -0800 | [diff] [blame] | 5815 |                     // ALOGW rather than LOG_ALWAYS_FATAL because it seems there are cases where an | 
 | 5816 |                     // AudioTrack may start (which may not be with a start() but with a write() | 
 | 5817 |                     // after underrun) and immediately paused or released.  In that case the | 
 | 5818 |                     // FastTrack state hasn't had time to update. | 
 | 5819 |                     // TODO Remove the ALOGW when this theory is confirmed. | 
 | 5820 |                     ALOGW("fast track %d should have been active; " | 
| Glenn Kasten | f7d65ee | 2015-12-02 13:45:01 -0800 | [diff] [blame] | 5821 |                             "mState=%d, mTrackMask=%#x, recentUnderruns=%u, isShared=%d", | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5822 |                             j, (int)track->state(), state->mTrackMask, recentUnderruns, | 
| Glenn Kasten | f7d65ee | 2015-12-02 13:45:01 -0800 | [diff] [blame] | 5823 |                             track->sharedBuffer() != 0); | 
| Glenn Kasten | bf848af | 2017-12-22 11:55:01 -0800 | [diff] [blame] | 5824 |                     // Since the FastMixer state already has the track inactive, do nothing here. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5825 |                 } | 
 | 5826 |                 tracksToRemove->add(track); | 
 | 5827 |                 // Avoids a misleading display in dumpsys | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5828 |                 track->fastTrackUnderruns().mBitFields.mMostRecent = UNDERRUN_FULL; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5829 |             } | 
| jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 5830 |             if (fastTrack->mHapticPlaybackEnabled != track->getHapticPlaybackEnabled()) { | 
 | 5831 |                 fastTrack->mHapticPlaybackEnabled = track->getHapticPlaybackEnabled(); | 
 | 5832 |                 didModify = true; | 
 | 5833 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5834 |             continue; | 
 | 5835 |         } | 
 | 5836 |  | 
 | 5837 |         {   // local variable scope to avoid goto warning | 
 | 5838 |  | 
 | 5839 |         audio_track_cblk_t* cblk = track->cblk(); | 
 | 5840 |  | 
 | 5841 |         // The first time a track is added we wait | 
 | 5842 |         // for all its buffers to be filled before processing it | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5843 |         const int trackId = track->id(); | 
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5844 |  | 
 | 5845 |         // if an active track doesn't exist in the AudioMixer, create it. | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5846 |         // use the trackId as the AudioMixer name. | 
 | 5847 |         if (!mAudioMixer->exists(trackId)) { | 
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5848 |             status_t status = mAudioMixer->create( | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5849 |                     trackId, | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5850 |                     track->channelMask(), | 
 | 5851 |                     track->format(), | 
 | 5852 |                     track->sessionId()); | 
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5853 |             if (status != OK) { | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5854 |                 ALOGW("%s(): AudioMixer cannot create track(%d)" | 
 | 5855 |                         " mask %#x, format %#x, sessionId %d", | 
 | 5856 |                         __func__, trackId, | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5857 |                         track->channelMask(), track->format(), track->sessionId()); | 
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5858 |                 tracksToRemove->add(track); | 
 | 5859 |                 track->invalidate(); // consider it dead. | 
 | 5860 |                 continue; | 
 | 5861 |             } | 
 | 5862 |         } | 
 | 5863 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5864 |         // make sure that we have enough frames to mix one full buffer. | 
 | 5865 |         // enforce this condition only once to enable draining the buffer in case the client | 
 | 5866 |         // app does not call stop() and relies on underrun to stop: | 
 | 5867 |         // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed | 
 | 5868 |         // during last round | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 5869 |         size_t desiredFrames; | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5870 |         const uint32_t sampleRate = track->audioTrackServerProxy()->getSampleRate(); | 
 | 5871 |         const AudioPlaybackRate playbackRate = track->audioTrackServerProxy()->getPlaybackRate(); | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 5872 |  | 
 | 5873 |         desiredFrames = sourceFramesNeededWithTimestretch( | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 5874 |                 sampleRate, mNormalFrameCount, mSampleRate, playbackRate.mSpeed); | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 5875 |         // TODO: ONLY USED FOR LEGACY RESAMPLERS, remove when they are removed. | 
 | 5876 |         // add frames already consumed but not yet released by the resampler | 
 | 5877 |         // because mAudioTrackServerProxy->framesReady() will include these frames | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5878 |         desiredFrames += mAudioMixer->getUnreleasedFrames(trackId); | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 5879 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5880 |         uint32_t minFrames = 1; | 
 | 5881 |         if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() && | 
 | 5882 |                 (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) { | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 5883 |             minFrames = desiredFrames; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5884 |         } | 
| Eric Laurent | 13e4c96 | 2013-12-20 17:36:01 -0800 | [diff] [blame] | 5885 |  | 
 | 5886 |         size_t framesReady = track->framesReady(); | 
| Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 5887 |         if (ATRACE_ENABLED()) { | 
 | 5888 |             // I wish we had formatted trace names | 
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5889 |             std::string traceName("nRdy"); | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5890 |             traceName += std::to_string(trackId); | 
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5891 |             ATRACE_INT(traceName.c_str(), framesReady); | 
| Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 5892 |         } | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 5893 |         if ((framesReady >= minFrames) && track->isReady() && | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5894 |                 !track->isPaused() && !track->isTerminated()) | 
 | 5895 |         { | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5896 |             ALOGVV("track(%d) s=%08x [OK] on thread %p", trackId, cblk->mServer, this); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5897 |  | 
 | 5898 |             mixedTracks++; | 
 | 5899 |  | 
| Shunkai Yao | f484765 | 2024-01-12 00:25:20 +0000 | [diff] [blame] | 5900 |             // track->mainBuffer() != mSinkBuffer and mMixerBuffer means | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 5901 |             // there is an effect chain connected to the track | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5902 |             chain.clear(); | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 5903 |             if (track->mainBuffer() != mSinkBuffer && | 
 | 5904 |                     track->mainBuffer() != mMixerBuffer) { | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 5905 |                 if (mEffectBufferEnabled) { | 
 | 5906 |                     mEffectBufferValid = true; // Later can set directly. | 
 | 5907 |                 } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5908 |                 chain = getEffectChain_l(track->sessionId()); | 
 | 5909 |                 // Delegate volume control to effect in track effect chain if needed | 
 | 5910 |                 if (chain != 0) { | 
 | 5911 |                     tracksWithEffect++; | 
 | 5912 |                 } else { | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5913 |                     ALOGW("prepareTracks_l(): track(%d) attached to effect but no chain found on " | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5914 |                             "session %d", | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5915 |                             trackId, track->sessionId()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5916 |                 } | 
 | 5917 |             } | 
 | 5918 |  | 
 | 5919 |  | 
 | 5920 |             int param = AudioMixer::VOLUME; | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5921 |             if (track->fillingStatus() == IAfTrack::FS_FILLED) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5922 |                 // no ramp for the first volume setting | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5923 |                 track->fillingStatus() = IAfTrack::FS_ACTIVE; | 
 | 5924 |                 if (track->state() == IAfTrackBase::RESUMING) { | 
 | 5925 |                     track->setState(IAfTrackBase::ACTIVE); | 
| Revathi Uddaraju | 453bcb5 | 2017-08-14 14:19:40 +0800 | [diff] [blame] | 5926 |                     // If a new track is paused immediately after start, do not ramp on resume. | 
 | 5927 |                     if (cblk->mServer != 0) { | 
 | 5928 |                         param = AudioMixer::RAMP_VOLUME; | 
 | 5929 |                     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5930 |                 } | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5931 |                 mAudioMixer->setParameter(trackId, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); | 
| Eric Laurent | 7c29ec9 | 2017-09-20 17:54:22 -0700 | [diff] [blame] | 5932 |                 mLeftVolFloat = -1.0; | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 5933 |             // FIXME should not make a decision based on mServer | 
 | 5934 |             } else if (cblk->mServer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5935 |                 // If the track is stopped before the first frame was mixed, | 
 | 5936 |                 // do not apply ramp | 
 | 5937 |                 param = AudioMixer::RAMP_VOLUME; | 
 | 5938 |             } | 
 | 5939 |  | 
 | 5940 |             // compute volume for this track | 
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 5941 |             uint32_t vl, vr;       // in U8.24 integer format | 
 | 5942 |             float vlf, vrf, vaf;   // in [0.0, 1.0] float format | 
| Eric Laurent | 7c29ec9 | 2017-09-20 17:54:22 -0700 | [diff] [blame] | 5943 |             // read original volumes with volume control | 
| Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 5944 |             float v = masterVolume * mStreamTypes[track->streamType()].volume; | 
| Andy Hung | 333ab96 | 2019-05-28 20:23:35 -0700 | [diff] [blame] | 5945 |             // Always fetch volumeshaper volume to ensure state is updated. | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5946 |             const sp<AudioTrackServerProxy> proxy = track->audioTrackServerProxy(); | 
| Andy Hung | 333ab96 | 2019-05-28 20:23:35 -0700 | [diff] [blame] | 5947 |             const float vh = track->getVolumeHandler()->getVolume( | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5948 |                     track->audioTrackServerProxy()->framesReleased()).first; | 
| Eric Laurent | 7c29ec9 | 2017-09-20 17:54:22 -0700 | [diff] [blame] | 5949 |  | 
| Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 5950 |             if (mStreamTypes[track->streamType()].mute || track->isPlaybackRestricted()) { | 
 | 5951 |                 v = 0; | 
 | 5952 |             } | 
 | 5953 |  | 
 | 5954 |             handleVoipVolume_l(&v); | 
 | 5955 |  | 
 | 5956 |             if (track->isPausing()) { | 
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 5957 |                 vl = vr = 0; | 
 | 5958 |                 vlf = vrf = vaf = 0.; | 
| Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 5959 |                 track->setPaused(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5960 |             } else { | 
| Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 5961 |                 gain_minifloat_packed_t vlr = proxy->getVolumeLR(); | 
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 5962 |                 vlf = float_from_gain(gain_minifloat_unpack_left(vlr)); | 
 | 5963 |                 vrf = float_from_gain(gain_minifloat_unpack_right(vlr)); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5964 |                 // 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] | 5965 |                 if (vlf > GAIN_FLOAT_UNITY) { | 
 | 5966 |                     ALOGV("Track left volume out of range: %.3g", vlf); | 
 | 5967 |                     vlf = GAIN_FLOAT_UNITY; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5968 |                 } | 
| Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 5969 |                 if (vrf > GAIN_FLOAT_UNITY) { | 
 | 5970 |                     ALOGV("Track right volume out of range: %.3g", vrf); | 
 | 5971 |                     vrf = GAIN_FLOAT_UNITY; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5972 |                 } | 
| Vlad Popa | e2f5aef | 2022-07-25 16:00:20 +0200 | [diff] [blame] | 5973 |  | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 5974 |                 track->processMuteEvent_l(mAfThreadCallback->getOrCreateAudioManager(), | 
| Vlad Popa | e2f5aef | 2022-07-25 16:00:20 +0200 | [diff] [blame] | 5975 |                     /*muteState=*/{masterVolume == 0.f, | 
 | 5976 |                                    mStreamTypes[track->streamType()].volume == 0.f, | 
 | 5977 |                                    mStreamTypes[track->streamType()].mute, | 
 | 5978 |                                    track->isPlaybackRestricted(), | 
| Vlad Popa | 436c917 | 2022-08-02 15:25:08 +0200 | [diff] [blame] | 5979 |                                    vlf == 0.f && vrf == 0.f, | 
 | 5980 |                                    vh == 0.f}); | 
| Vlad Popa | e2f5aef | 2022-07-25 16:00:20 +0200 | [diff] [blame] | 5981 |  | 
| Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 5982 |                 // now apply the master volume and stream type volume and shaper volume | 
 | 5983 |                 vlf *= v * vh; | 
 | 5984 |                 vrf *= v * vh; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5985 |                 // 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] | 5986 |                 // then derive vl and vr as U8.24 versions for the effect chain | 
 | 5987 |                 const float scaleto8_24 = MAX_GAIN_INT * MAX_GAIN_INT; | 
 | 5988 |                 vl = (uint32_t) (scaleto8_24 * vlf); | 
 | 5989 |                 vr = (uint32_t) (scaleto8_24 * vrf); | 
 | 5990 |                 // vl and vr are now in U8.24 format | 
| Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 5991 |                 uint16_t sendLevel = proxy->getSendLevel_U4_12(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5992 |                 // send level comes from shared memory and so may be corrupt | 
 | 5993 |                 if (sendLevel > MAX_GAIN_INT) { | 
 | 5994 |                     ALOGV("Track send level out of range: %04X", sendLevel); | 
 | 5995 |                     sendLevel = MAX_GAIN_INT; | 
 | 5996 |                 } | 
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 5997 |                 // vaf is represented as [0.0, 1.0] float by rescaling sendLevel | 
 | 5998 |                 vaf = v * sendLevel * (1. / MAX_GAIN_INT); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5999 |             } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6000 |  | 
| Jiabin Huang | 66aa1e3 | 2024-05-13 20:33:29 +0000 | [diff] [blame] | 6001 |             track->setFinalVolume(vlf, vrf); | 
| Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 6002 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6003 |             // Delegate volume control to effect in track effect chain if needed | 
| Shunkai Yao | f484765 | 2024-01-12 00:25:20 +0000 | [diff] [blame] | 6004 |             if (chain != 0 && chain->setVolume(&vl, &vr)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6005 |                 // Do not ramp volume if volume is controlled by effect | 
 | 6006 |                 param = AudioMixer::VOLUME; | 
| Bryant Liu | b6be7f2 | 2014-06-12 22:02:41 +0800 | [diff] [blame] | 6007 |                 // Update remaining floating point volume levels | 
 | 6008 |                 vlf = (float)vl / (1 << 24); | 
 | 6009 |                 vrf = (float)vr / (1 << 24); | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6010 |                 track->setHasVolumeController(true); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6011 |             } else { | 
 | 6012 |                 // force no volume ramp when volume controller was just disabled or removed | 
 | 6013 |                 // from effect chain to avoid volume spike | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6014 |                 if (track->hasVolumeController()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6015 |                     param = AudioMixer::VOLUME; | 
 | 6016 |                 } | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6017 |                 track->setHasVolumeController(false); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6018 |             } | 
 | 6019 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6020 |             // XXX: these things DON'T need to be done each time | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6021 |             mAudioMixer->setBufferProvider(trackId, track->asExtendedAudioBufferProvider()); | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6022 |             mAudioMixer->enable(trackId); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6023 |  | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6024 |             mAudioMixer->setParameter(trackId, param, AudioMixer::VOLUME0, &vlf); | 
 | 6025 |             mAudioMixer->setParameter(trackId, param, AudioMixer::VOLUME1, &vrf); | 
 | 6026 |             mAudioMixer->setParameter(trackId, param, AudioMixer::AUXLEVEL, &vaf); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6027 |             mAudioMixer->setParameter( | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6028 |                 trackId, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6029 |                 AudioMixer::TRACK, | 
 | 6030 |                 AudioMixer::FORMAT, (void *)track->format()); | 
 | 6031 |             mAudioMixer->setParameter( | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6032 |                 trackId, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6033 |                 AudioMixer::TRACK, | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 6034 |                 AudioMixer::CHANNEL_MASK, (void *)(uintptr_t)track->channelMask()); | 
| Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6035 |  | 
| Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 6036 |             if (mType == SPATIALIZER && !track->isSpatialized()) { | 
| Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6037 |                 mAudioMixer->setParameter( | 
 | 6038 |                     trackId, | 
 | 6039 |                     AudioMixer::TRACK, | 
 | 6040 |                     AudioMixer::MIXER_CHANNEL_MASK, | 
 | 6041 |                     (void *)(uintptr_t)(mChannelMask | mHapticChannelMask)); | 
 | 6042 |             } else { | 
 | 6043 |                 mAudioMixer->setParameter( | 
 | 6044 |                     trackId, | 
 | 6045 |                     AudioMixer::TRACK, | 
 | 6046 |                     AudioMixer::MIXER_CHANNEL_MASK, | 
 | 6047 |                     (void *)(uintptr_t)(mMixerChannelMask | mHapticChannelMask)); | 
 | 6048 |             } | 
 | 6049 |  | 
| Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 6050 |             // 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] | 6051 |             uint32_t maxSampleRate = mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX; | 
| Andy Hung | 333ab96 | 2019-05-28 20:23:35 -0700 | [diff] [blame] | 6052 |             uint32_t reqSampleRate = proxy->getSampleRate(); | 
| Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 6053 |             if (reqSampleRate == 0) { | 
 | 6054 |                 reqSampleRate = mSampleRate; | 
 | 6055 |             } else if (reqSampleRate > maxSampleRate) { | 
 | 6056 |                 reqSampleRate = maxSampleRate; | 
 | 6057 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6058 |             mAudioMixer->setParameter( | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6059 |                 trackId, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6060 |                 AudioMixer::RESAMPLE, | 
 | 6061 |                 AudioMixer::SAMPLE_RATE, | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 6062 |                 (void *)(uintptr_t)reqSampleRate); | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 6063 |  | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 6064 |             mAudioMixer->setParameter( | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6065 |                 trackId, | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 6066 |                 AudioMixer::TIMESTRETCH, | 
 | 6067 |                 AudioMixer::PLAYBACK_RATE, | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 6068 |                 // cast away constness for this generic API. | 
 | 6069 |                 const_cast<void *>(reinterpret_cast<const void *>(&playbackRate))); | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 6070 |  | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 6071 |             /* | 
 | 6072 |              * Select the appropriate output buffer for the track. | 
 | 6073 |              * | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 6074 |              * Tracks with effects go into their own effects chain buffer | 
 | 6075 |              * and from there into either mEffectBuffer or mSinkBuffer. | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 6076 |              * | 
 | 6077 |              * Other tracks can use mMixerBuffer for higher precision | 
 | 6078 |              * channel accumulation.  If this buffer is enabled | 
 | 6079 |              * (mMixerBufferEnabled true), then selected tracks will accumulate | 
 | 6080 |              * into it. | 
 | 6081 |              * | 
 | 6082 |              */ | 
 | 6083 |             if (mMixerBufferEnabled | 
 | 6084 |                     && (track->mainBuffer() == mSinkBuffer | 
 | 6085 |                             || track->mainBuffer() == mMixerBuffer)) { | 
| Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 6086 |                 if (mType == SPATIALIZER && !track->isSpatialized()) { | 
| Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6087 |                     mAudioMixer->setParameter( | 
 | 6088 |                             trackId, | 
 | 6089 |                             AudioMixer::TRACK, | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 6090 |                             AudioMixer::MIXER_FORMAT, (void *)mEffectBufferFormat); | 
| Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6091 |                     mAudioMixer->setParameter( | 
 | 6092 |                             trackId, | 
 | 6093 |                             AudioMixer::TRACK, | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 6094 |                             AudioMixer::MAIN_BUFFER, (void *)mPostSpatializerBuffer); | 
| Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6095 |                 } else { | 
 | 6096 |                     mAudioMixer->setParameter( | 
 | 6097 |                             trackId, | 
 | 6098 |                             AudioMixer::TRACK, | 
 | 6099 |                             AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat); | 
 | 6100 |                     mAudioMixer->setParameter( | 
 | 6101 |                             trackId, | 
 | 6102 |                             AudioMixer::TRACK, | 
 | 6103 |                             AudioMixer::MAIN_BUFFER, (void *)mMixerBuffer); | 
 | 6104 |                     // TODO: override track->mainBuffer()? | 
 | 6105 |                     mMixerBufferValid = true; | 
 | 6106 |                 } | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 6107 |             } else { | 
 | 6108 |                 mAudioMixer->setParameter( | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6109 |                         trackId, | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 6110 |                         AudioMixer::TRACK, | 
| Andy Hung | 319587b | 2023-05-23 14:01:03 -0700 | [diff] [blame] | 6111 |                         AudioMixer::MIXER_FORMAT, (void *)AUDIO_FORMAT_PCM_FLOAT); | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 6112 |                 mAudioMixer->setParameter( | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6113 |                         trackId, | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 6114 |                         AudioMixer::TRACK, | 
 | 6115 |                         AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); | 
 | 6116 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6117 |             mAudioMixer->setParameter( | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6118 |                 trackId, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6119 |                 AudioMixer::TRACK, | 
 | 6120 |                 AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); | 
| jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 6121 |             mAudioMixer->setParameter( | 
 | 6122 |                 trackId, | 
 | 6123 |                 AudioMixer::TRACK, | 
 | 6124 |                 AudioMixer::HAPTIC_ENABLED, (void *)(uintptr_t)track->getHapticPlaybackEnabled()); | 
| Ahmad Khalil | 229466a | 2024-02-05 12:15:30 +0000 | [diff] [blame] | 6125 |             const os::HapticScale hapticScale = track->getHapticScale(); | 
| jiabin | 77270b8 | 2018-12-18 15:41:29 -0800 | [diff] [blame] | 6126 |             mAudioMixer->setParameter( | 
| Ahmad Khalil | 229466a | 2024-02-05 12:15:30 +0000 | [diff] [blame] | 6127 |                     trackId, | 
 | 6128 |                     AudioMixer::TRACK, | 
 | 6129 |                     AudioMixer::HAPTIC_SCALE, (void *)&hapticScale); | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6130 |             const float hapticMaxAmplitude = track->getHapticMaxAmplitude(); | 
| Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 6131 |             mAudioMixer->setParameter( | 
 | 6132 |                 trackId, | 
 | 6133 |                 AudioMixer::TRACK, | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6134 |                 AudioMixer::HAPTIC_MAX_AMPLITUDE, (void *)&hapticMaxAmplitude); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6135 |  | 
 | 6136 |             // reset retry count | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6137 |             track->retryCount() = kMaxTrackRetries; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6138 |  | 
 | 6139 |             // If one track is ready, set the mixer ready if: | 
 | 6140 |             //  - the mixer was not ready during previous round OR | 
 | 6141 |             //  - no other track is not ready | 
 | 6142 |             if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY || | 
 | 6143 |                     mixerStatus != MIXER_TRACKS_ENABLED) { | 
 | 6144 |                 mixerStatus = MIXER_TRACKS_READY; | 
 | 6145 |             } | 
| Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 6146 |  | 
 | 6147 |             // Enable the next few lines to instrument a test for underrun log handling. | 
 | 6148 |             // TODO: Remove when we have a better way of testing the underrun log. | 
 | 6149 | #if 0 | 
 | 6150 |             static int i; | 
 | 6151 |             if ((++i & 0xf) == 0) { | 
 | 6152 |                 deferredOperations.tallyUnderrunFrames(track, 10 /* underrunFrames */); | 
 | 6153 |             } | 
 | 6154 | #endif | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6155 |         } else { | 
| Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 6156 |             size_t underrunFrames = 0; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 6157 |             if (framesReady < desiredFrames && !track->isStopped() && !track->isPaused()) { | 
| Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 6158 |                 ALOGV("track(%d) underrun, track state %s  framesReady(%zu) < framesDesired(%zd)", | 
 | 6159 |                         trackId, track->getTrackStateAsString(), framesReady, desiredFrames); | 
| Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 6160 |                 underrunFrames = desiredFrames; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 6161 |             } | 
| Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 6162 |             deferredOperations.tallyUnderrunFrames(track, underrunFrames); | 
| Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 6163 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6164 |             // clear effect chain input buffer if an active track underruns to avoid sending | 
 | 6165 |             // previous audio buffer again to effects | 
 | 6166 |             chain = getEffectChain_l(track->sessionId()); | 
 | 6167 |             if (chain != 0) { | 
 | 6168 |                 chain->clearInputBuffer(); | 
 | 6169 |             } | 
 | 6170 |  | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6171 |             ALOGVV("track(%d) s=%08x [NOT READY] on thread %p", trackId, cblk->mServer, this); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6172 |             if ((track->sharedBuffer() != 0) || track->isTerminated() || | 
 | 6173 |                     track->isStopped() || track->isPaused()) { | 
 | 6174 |                 // We have consumed all the buffers of this track. | 
 | 6175 |                 // Remove it from the list of active tracks. | 
 | 6176 |                 // TODO: use actual buffer filling status instead of latency when available from | 
 | 6177 |                 // audio HAL | 
 | 6178 |                 size_t audioHALFrames = (latency_l() * mSampleRate) / 1000; | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 6179 |                 int64_t framesWritten = mBytesWritten / mFrameSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6180 |                 if (mStandby || track->presentationComplete(framesWritten, audioHALFrames)) { | 
 | 6181 |                     if (track->isStopped()) { | 
 | 6182 |                         track->reset(); | 
 | 6183 |                     } | 
 | 6184 |                     tracksToRemove->add(track); | 
 | 6185 |                 } | 
 | 6186 |             } else { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6187 |                 // No buffers for this track. Give it a few chances to | 
 | 6188 |                 // fill a buffer, then remove it from active list. | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6189 |                 if (--(track->retryCount()) <= 0) { | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6190 |                     ALOGI("BUFFER TIMEOUT: remove(%d) from active list on thread %p", | 
 | 6191 |                             trackId, this); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6192 |                     tracksToRemove->add(track); | 
 | 6193 |                     // indicate to client process that the track was disabled because of underrun; | 
 | 6194 |                     // it will then automatically call start() when data is available | 
| Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 6195 |                     track->disable(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6196 |                 // If one track is not ready, mark the mixer also not ready if: | 
 | 6197 |                 //  - the mixer was ready during previous round OR | 
 | 6198 |                 //  - no other track is ready | 
 | 6199 |                 } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY || | 
 | 6200 |                                 mixerStatus != MIXER_TRACKS_READY) { | 
 | 6201 |                     mixerStatus = MIXER_TRACKS_ENABLED; | 
 | 6202 |                 } | 
 | 6203 |             } | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6204 |             mAudioMixer->disable(trackId); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6205 |         } | 
 | 6206 |  | 
 | 6207 |         }   // local variable scope to avoid goto warning | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6208 |  | 
 | 6209 |     } | 
 | 6210 |  | 
| jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 6211 |     if (mHapticChannelMask != AUDIO_CHANNEL_NONE && sq != NULL) { | 
 | 6212 |         // When there is no fast track playing haptic and FastMixer exists, | 
 | 6213 |         // enabling the first FastTrack, which provides mixed data from normal | 
 | 6214 |         // tracks, to play haptic data. | 
 | 6215 |         FastTrack *fastTrack = &state->mFastTracks[0]; | 
 | 6216 |         if (fastTrack->mHapticPlaybackEnabled != noFastHapticTrack) { | 
 | 6217 |             fastTrack->mHapticPlaybackEnabled = noFastHapticTrack; | 
 | 6218 |             didModify = true; | 
 | 6219 |         } | 
 | 6220 |     } | 
 | 6221 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6222 |     // Push the new FastMixer state if necessary | 
| Jing Mike | 537412f | 2023-03-12 11:01:47 +0800 | [diff] [blame] | 6223 |     [[maybe_unused]] bool pauseAudioWatchdog = false; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6224 |     if (didModify) { | 
 | 6225 |         state->mFastTracksGen++; | 
 | 6226 |         // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle | 
 | 6227 |         if (kUseFastMixer == FastMixer_Dynamic && | 
 | 6228 |                 state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) { | 
 | 6229 |             state->mCommand = FastMixerState::COLD_IDLE; | 
 | 6230 |             state->mColdFutexAddr = &mFastMixerFutex; | 
 | 6231 |             state->mColdGen++; | 
 | 6232 |             mFastMixerFutex = 0; | 
 | 6233 |             if (kUseFastMixer == FastMixer_Dynamic) { | 
 | 6234 |                 mNormalSink = mOutputSink; | 
 | 6235 |             } | 
 | 6236 |             // If we go into cold idle, need to wait for acknowledgement | 
 | 6237 |             // so that fast mixer stops doing I/O. | 
 | 6238 |             block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; | 
 | 6239 |             pauseAudioWatchdog = true; | 
 | 6240 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6241 |     } | 
 | 6242 |     if (sq != NULL) { | 
 | 6243 |         sq->end(didModify); | 
| Andy Hung | f228531 | 2017-02-14 18:31:59 -0800 | [diff] [blame] | 6244 |         // No need to block if the FastMixer is in COLD_IDLE as the FastThread | 
 | 6245 |         // is not active. (We BLOCK_UNTIL_ACKED when entering COLD_IDLE | 
 | 6246 |         // when bringing the output sink into standby.) | 
 | 6247 |         // | 
 | 6248 |         // We will get the latest FastMixer state when we come out of COLD_IDLE. | 
 | 6249 |         // | 
 | 6250 |         // This occurs with BT suspend when we idle the FastMixer with | 
 | 6251 |         // active tracks, which may be added or removed. | 
 | 6252 |         sq->push(coldIdle ? FastMixerStateQueue::BLOCK_NEVER : block); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6253 |     } | 
 | 6254 | #ifdef AUDIO_WATCHDOG | 
 | 6255 |     if (pauseAudioWatchdog && mAudioWatchdog != 0) { | 
 | 6256 |         mAudioWatchdog->pause(); | 
 | 6257 |     } | 
 | 6258 | #endif | 
 | 6259 |  | 
 | 6260 |     // Now perform the deferred reset on fast tracks that have stopped | 
 | 6261 |     while (resetMask != 0) { | 
 | 6262 |         size_t i = __builtin_ctz(resetMask); | 
 | 6263 |         ALOG_ASSERT(i < count); | 
 | 6264 |         resetMask &= ~(1 << i); | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6265 |         sp<IAfTrack> track = mActiveTracks[i]; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6266 |         ALOG_ASSERT(track->isFastTrack() && track->isStopped()); | 
 | 6267 |         track->reset(); | 
 | 6268 |     } | 
 | 6269 |  | 
| Andy Hung | 80d03d2 | 2018-04-10 10:32:11 -0700 | [diff] [blame] | 6270 |     // Track destruction may occur outside of threadLoop once it is removed from active tracks. | 
 | 6271 |     // Ensure the AudioMixer doesn't have a raw "buffer provider" pointer to the track if | 
 | 6272 |     // it ceases to be active, to allow safe removal from the AudioMixer at the start | 
 | 6273 |     // of prepareTracks_l(); this releases any outstanding buffer back to the track. | 
 | 6274 |     // See also the implementation of destroyTrack_l(). | 
 | 6275 |     for (const auto &track : *tracksToRemove) { | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6276 |         const int trackId = track->id(); | 
 | 6277 |         if (mAudioMixer->exists(trackId)) { // Normal tracks here, fast tracks in FastMixer. | 
 | 6278 |             mAudioMixer->setBufferProvider(trackId, nullptr /* bufferProvider */); | 
| Andy Hung | 80d03d2 | 2018-04-10 10:32:11 -0700 | [diff] [blame] | 6279 |         } | 
 | 6280 |     } | 
 | 6281 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6282 |     // remove all the tracks that need to be... | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6283 |     removeTracks_l(*tracksToRemove); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6284 |  | 
| Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 6285 |     if (getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX) != 0 || | 
 | 6286 |             getEffectChain_l(AUDIO_SESSION_OUTPUT_STAGE) != 0) { | 
| Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 6287 |         mEffectBufferValid = true; | 
| Marco Nelissen | ac30214 | 2014-10-20 13:15:38 -0700 | [diff] [blame] | 6288 |     } | 
 | 6289 |  | 
 | 6290 |     if (mEffectBufferValid) { | 
| Marco Nelissen | 57088b5 | 2014-10-17 16:39:39 -0700 | [diff] [blame] | 6291 |         // as long as there are effects we should clear the effects buffer, to avoid | 
 | 6292 |         // passing a non-clean buffer to the effect chain | 
 | 6293 |         memset(mEffectBuffer, 0, mEffectBufferSize); | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 6294 |         if (mType == SPATIALIZER) { | 
 | 6295 |             memset(mPostSpatializerBuffer, 0, mPostSpatializerBufferSize); | 
 | 6296 |         } | 
| Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 6297 |     } | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 6298 |     // sink or mix buffer must be cleared if all tracks are connected to an | 
 | 6299 |     // effect chain as in this case the mixer will not write to the sink or mix buffer | 
 | 6300 |     // and track effects will accumulate into it | 
| Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6301 |     // always clear sink buffer for spatializer output as the output of the spatializer | 
 | 6302 |     // effect will be accumulated into it | 
 | 6303 |     if ((mBytesRemaining == 0) && (((mixedTracks != 0 && mixedTracks == tracksWithEffect) || | 
 | 6304 |             (mixedTracks == 0 && fastTracks > 0)) || (mType == SPATIALIZER))) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6305 |         // FIXME as a performance optimization, should remember previous zero status | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 6306 |         if (mMixerBufferValid) { | 
 | 6307 |             memset(mMixerBuffer, 0, mMixerBufferSize); | 
 | 6308 |             // TODO: In testing, mSinkBuffer below need not be cleared because | 
 | 6309 |             // the PlaybackThread::threadLoop() copies mMixerBuffer into mSinkBuffer | 
 | 6310 |             // after mixing. | 
 | 6311 |             // | 
 | 6312 |             // To enforce this guarantee: | 
 | 6313 |             // ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || | 
 | 6314 |             // (mixedTracks == 0 && fastTracks > 0)) | 
 | 6315 |             // must imply MIXER_TRACKS_READY. | 
 | 6316 |             // Later, we may clear buffers regardless, and skip much of this logic. | 
 | 6317 |         } | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 6318 |         // FIXME as a performance optimization, should remember previous zero status | 
| Andy Hung | 5567aaf | 2014-07-17 14:00:07 -0700 | [diff] [blame] | 6319 |         memset(mSinkBuffer, 0, mNormalFrameCount * mFrameSize); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6320 |     } | 
 | 6321 |  | 
 | 6322 |     // if any fast tracks, then status is ready | 
 | 6323 |     mMixerStatusIgnoringFastTracks = mixerStatus; | 
 | 6324 |     if (fastTracks > 0) { | 
 | 6325 |         mixerStatus = MIXER_TRACKS_READY; | 
 | 6326 |     } | 
 | 6327 |     return mixerStatus; | 
 | 6328 | } | 
 | 6329 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 6330 | // trackCountForUid_l() must be called with ThreadBase::mutex() held | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6331 | uint32_t PlaybackThread::trackCountForUid_l(uid_t uid) const | 
| Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 6332 | { | 
 | 6333 |     uint32_t trackCount = 0; | 
 | 6334 |     for (size_t i = 0; i < mTracks.size() ; i++) { | 
| Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 6335 |         if (mTracks[i]->uid() == uid) { | 
| Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 6336 |             trackCount++; | 
 | 6337 |         } | 
 | 6338 |     } | 
 | 6339 |     return trackCount; | 
 | 6340 | } | 
 | 6341 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6342 | bool PlaybackThread::IsTimestampAdvancing::check(AudioStreamOut* output) | 
| ziyangch | 8f194f1 | 2021-12-01 13:48:04 -0800 | [diff] [blame] | 6343 | { | 
| Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 6344 |     // Check the timestamp to see if it's advancing once every 150ms. If we check too frequently, we | 
 | 6345 |     // could falsely detect that the frame position has stalled due to underrun because we haven't | 
 | 6346 |     // given the Audio HAL enough time to update. | 
 | 6347 |     const nsecs_t nowNs = systemTime(); | 
 | 6348 |     if (nowNs - mPreviousNs < mMinimumTimeBetweenChecksNs) { | 
 | 6349 |         return mLatchedValue; | 
 | 6350 |     } | 
 | 6351 |     mPreviousNs = nowNs; | 
 | 6352 |     mLatchedValue = false; | 
 | 6353 |     // Determine if the presentation position is still advancing. | 
| ziyangch | 8f194f1 | 2021-12-01 13:48:04 -0800 | [diff] [blame] | 6354 |     uint64_t position = 0; | 
 | 6355 |     struct timespec unused; | 
| Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 6356 |     const status_t ret = output->getPresentationPosition(&position, &unused); | 
| ziyangch | 8f194f1 | 2021-12-01 13:48:04 -0800 | [diff] [blame] | 6357 |     if (ret == NO_ERROR) { | 
| Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 6358 |         if (position != mPreviousPosition) { | 
 | 6359 |             mPreviousPosition = position; | 
 | 6360 |             mLatchedValue = true; | 
| ziyangch | 8f194f1 | 2021-12-01 13:48:04 -0800 | [diff] [blame] | 6361 |         } | 
 | 6362 |     } | 
| Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 6363 |     return mLatchedValue; | 
 | 6364 | } | 
 | 6365 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6366 | void PlaybackThread::IsTimestampAdvancing::clear() | 
| Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 6367 | { | 
 | 6368 |     mLatchedValue = true; | 
 | 6369 |     mPreviousPosition = 0; | 
 | 6370 |     mPreviousNs = 0; | 
| ziyangch | 8f194f1 | 2021-12-01 13:48:04 -0800 | [diff] [blame] | 6371 | } | 
 | 6372 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 6373 | // isTrackAllowed_l() must be called with ThreadBase::mutex() held | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6374 | bool MixerThread::isTrackAllowed_l( | 
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 6375 |         audio_channel_mask_t channelMask, audio_format_t format, | 
 | 6376 |         audio_session_t sessionId, uid_t uid) const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6377 | { | 
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 6378 |     if (!PlaybackThread::isTrackAllowed_l(channelMask, format, sessionId, uid)) { | 
 | 6379 |         return false; | 
| Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 6380 |     } | 
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 6381 |     // Check validity as we don't call AudioMixer::create() here. | 
| Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 6382 |     if (!mAudioMixer->isValidFormat(format)) { | 
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 6383 |         ALOGW("%s: invalid format: %#x", __func__, format); | 
 | 6384 |         return false; | 
 | 6385 |     } | 
| Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 6386 |     if (!mAudioMixer->isValidChannelMask(channelMask)) { | 
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 6387 |         ALOGW("%s: invalid channelMask: %#x", __func__, channelMask); | 
 | 6388 |         return false; | 
 | 6389 |     } | 
 | 6390 |     return true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6391 | } | 
 | 6392 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 6393 | // checkForNewParameter_l() must be called with ThreadBase::mutex() held | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6394 | bool MixerThread::checkForNewParameter_l(const String8& keyValuePair, | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6395 |                                                        status_t& status) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6396 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6397 |     bool reconfig = false; | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6398 |     status = NO_ERROR; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6399 |  | 
| Glenn Kasten | c05b8d7 | 2016-03-24 09:48:17 -0700 | [diff] [blame] | 6400 |     AutoPark<FastMixer> park(mFastMixer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6401 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6402 |     AudioParameter param = AudioParameter(keyValuePair); | 
 | 6403 |     int value; | 
 | 6404 |     if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { | 
 | 6405 |         reconfig = true; | 
 | 6406 |     } | 
 | 6407 |     if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { | 
| Andy Hung | d21a2ab | 2023-07-20 21:44:14 -0700 | [diff] [blame] | 6408 |         if (!isValidPcmSinkFormat(static_cast<audio_format_t>(value))) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6409 |             status = BAD_VALUE; | 
 | 6410 |         } else { | 
 | 6411 |             // no need to save value, since it's constant | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6412 |             reconfig = true; | 
 | 6413 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6414 |     } | 
 | 6415 |     if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { | 
| Andy Hung | d21a2ab | 2023-07-20 21:44:14 -0700 | [diff] [blame] | 6416 |         if (!isValidPcmSinkChannelMask(static_cast<audio_channel_mask_t>(value))) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6417 |             status = BAD_VALUE; | 
 | 6418 |         } else { | 
 | 6419 |             // no need to save value, since it's constant | 
 | 6420 |             reconfig = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6421 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6422 |     } | 
 | 6423 |     if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { | 
 | 6424 |         // do not accept frame count changes if tracks are open as the track buffer | 
 | 6425 |         // size depends on frame count and correct behavior would not be guaranteed | 
 | 6426 |         // if frame count is changed after track creation | 
 | 6427 |         if (!mTracks.isEmpty()) { | 
 | 6428 |             status = INVALID_OPERATION; | 
 | 6429 |         } else { | 
 | 6430 |             reconfig = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6431 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6432 |     } | 
 | 6433 |     if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 6434 |         LOG_FATAL("Should not set routing device in MixerThread"); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6435 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6436 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6437 |     if (status == NO_ERROR) { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6438 |         status = mOutput->stream->setParameters(keyValuePair); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6439 |         if (!mStandby && status == INVALID_OPERATION) { | 
| Andy Hung | dda7aed | 2023-03-27 15:53:06 -0700 | [diff] [blame] | 6440 |             ALOGW("%s: setParameters failed with keyValuePair %s, entering standby", | 
 | 6441 |                     __func__, keyValuePair.c_str()); | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 6442 |             mOutput->standby(); | 
| Andy Hung | dda7aed | 2023-03-27 15:53:06 -0700 | [diff] [blame] | 6443 |             mThreadMetrics.logEndInterval(); | 
 | 6444 |             mThreadSnapshot.onEnd(); | 
 | 6445 |             setStandby_l(); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6446 |             mBytesWritten = 0; | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6447 |             status = mOutput->stream->setParameters(keyValuePair); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6448 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6449 |         if (status == NO_ERROR && reconfig) { | 
 | 6450 |             readOutputParameters_l(); | 
 | 6451 |             delete mAudioMixer; | 
 | 6452 |             mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); | 
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 6453 |             for (const auto &track : mTracks) { | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6454 |                 const int trackId = track->id(); | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 6455 |                 const status_t createStatus = mAudioMixer->create( | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6456 |                         trackId, | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6457 |                         track->channelMask(), | 
 | 6458 |                         track->format(), | 
 | 6459 |                         track->sessionId()); | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 6460 |                 ALOGW_IF(createStatus != NO_ERROR, | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6461 |                         "%s(): AudioMixer cannot create track(%d)" | 
 | 6462 |                         " mask %#x, format %#x, sessionId %d", | 
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 6463 |                         __func__, | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6464 |                         trackId, track->channelMask(), track->format(), track->sessionId()); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6465 |             } | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 6466 |             sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6467 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6468 |     } | 
 | 6469 |  | 
| Dean Wheatley | 6891810 | 2021-03-19 22:09:19 +1100 | [diff] [blame] | 6470 |     return reconfig; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6471 | } | 
 | 6472 |  | 
 | 6473 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6474 | void MixerThread::dumpInternals_l(int fd, const Vector<String16>& args) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6475 | { | 
| Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 6476 |     PlaybackThread::dumpInternals_l(fd, args); | 
| Andy Hung | 160664b | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 6477 |     dprintf(fd, "  Thread throttle time (msecs): %u\n", (uint32_t)mThreadThrottleTimeMs); | 
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 6478 |     dprintf(fd, "  AudioMixer tracks: %s\n", mAudioMixer->trackNames().c_str()); | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 6479 |     dprintf(fd, "  Master mono: %s\n", mMasterMono ? "on" : "off"); | 
| Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 6480 |     dprintf(fd, "  Master balance: %f (%s)\n", mMasterBalance.load(), | 
 | 6481 |             (hasFastMixer() ? std::to_string(mFastMixer->getMasterBalance()) | 
 | 6482 |                             : mBalance.toString()).c_str()); | 
| Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 6483 |     if (hasFastMixer()) { | 
 | 6484 |         dprintf(fd, "  FastMixer thread %p tid=%d", mFastMixer.get(), mFastMixer->getTid()); | 
 | 6485 |  | 
 | 6486 |         // Make a non-atomic copy of fast mixer dump state so it won't change underneath us | 
 | 6487 |         // while we are dumping it.  It may be inconsistent, but it won't mutate! | 
 | 6488 |         // This is a large object so we place it on the heap. | 
 | 6489 |         // FIXME 25972958: Need an intelligent copy constructor that does not touch unused pages. | 
| Eric Tan | 2942a4e | 2018-09-12 17:41:27 -0700 | [diff] [blame] | 6490 |         const std::unique_ptr<FastMixerDumpState> copy = | 
 | 6491 |                 std::make_unique<FastMixerDumpState>(mFastMixerDumpState); | 
| Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 6492 |         copy->dump(fd); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6493 |  | 
 | 6494 | #ifdef STATE_QUEUE_DUMP | 
| Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 6495 |         // Similar for state queue | 
 | 6496 |         StateQueueObserverDump observerCopy = mStateQueueObserverDump; | 
 | 6497 |         observerCopy.dump(fd); | 
 | 6498 |         StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump; | 
 | 6499 |         mutatorCopy.dump(fd); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6500 | #endif | 
 | 6501 |  | 
| Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 6502 | #ifdef AUDIO_WATCHDOG | 
 | 6503 |         if (mAudioWatchdog != 0) { | 
 | 6504 |             // Make a non-atomic copy of audio watchdog dump so it won't change underneath us | 
 | 6505 |             AudioWatchdogDump wdCopy = mAudioWatchdogDump; | 
 | 6506 |             wdCopy.dump(fd); | 
 | 6507 |         } | 
 | 6508 | #endif | 
 | 6509 |  | 
 | 6510 |     } else { | 
 | 6511 |         dprintf(fd, "  No FastMixer\n"); | 
 | 6512 |     } | 
| Eric Laurent | 90cea10 | 2023-05-15 15:08:27 +0200 | [diff] [blame] | 6513 |  | 
 | 6514 |      dprintf(fd, "Bluetooth latency modes are %senabled\n", | 
 | 6515 |             mBluetoothLatencyModesEnabled ? "" : "not "); | 
 | 6516 |      dprintf(fd, "HAL does %ssupport Bluetooth latency modes\n", mOutput != nullptr && | 
 | 6517 |              mOutput->audioHwDev->supportsBluetoothVariableLatency() ? "" : "not "); | 
 | 6518 |      dprintf(fd, "Supported latency modes: %s\n", toString(mSupportedLatencyModes).c_str()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6519 | } | 
 | 6520 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6521 | uint32_t MixerThread::idleSleepTimeUs() const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6522 | { | 
 | 6523 |     return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2; | 
 | 6524 | } | 
 | 6525 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6526 | uint32_t MixerThread::suspendSleepTimeUs() const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6527 | { | 
 | 6528 |     return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000); | 
 | 6529 | } | 
 | 6530 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6531 | void MixerThread::cacheParameters_l() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6532 | { | 
 | 6533 |     PlaybackThread::cacheParameters_l(); | 
 | 6534 |  | 
 | 6535 |     // FIXME: Relaxed timing because of a certain device that can't meet latency | 
 | 6536 |     // Should be reduced to 2x after the vendor fixes the driver issue | 
 | 6537 |     // increase threshold again due to low power audio mode. The way this warning | 
 | 6538 |     // threshold is calculated and its usefulness should be reconsidered anyway. | 
 | 6539 |     maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15; | 
 | 6540 | } | 
 | 6541 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6542 | void MixerThread::onHalLatencyModesChanged_l() { | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 6543 |     mAfThreadCallback->onSupportedLatencyModesChanged(mId, mSupportedLatencyModes); | 
| Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 6544 | } | 
 | 6545 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6546 | void MixerThread::setHalLatencyMode_l() { | 
| Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 6547 |     // Only handle latency mode if: | 
 | 6548 |     // - mBluetoothLatencyModesEnabled is true | 
 | 6549 |     // - the HAL supports latency modes | 
 | 6550 |     // - the selected device is Bluetooth LE or A2DP | 
 | 6551 |     if (!mBluetoothLatencyModesEnabled.load() || mSupportedLatencyModes.empty()) { | 
 | 6552 |         return; | 
 | 6553 |     } | 
 | 6554 |     if (mOutDeviceTypeAddrs.size() != 1 | 
 | 6555 |             || !(audio_is_a2dp_out_device(mOutDeviceTypeAddrs[0].mType) | 
 | 6556 |                  || audio_is_ble_out_device(mOutDeviceTypeAddrs[0].mType))) { | 
 | 6557 |         return; | 
 | 6558 |     } | 
 | 6559 |  | 
 | 6560 |     audio_latency_mode_t latencyMode = AUDIO_LATENCY_MODE_FREE; | 
 | 6561 |     if (mSupportedLatencyModes.size() == 1) { | 
 | 6562 |         // If the HAL only support one latency mode currently, confirm the choice | 
 | 6563 |         latencyMode = mSupportedLatencyModes[0]; | 
 | 6564 |     } else if (mSupportedLatencyModes.size() > 1) { | 
 | 6565 |         // Request low latency if: | 
 | 6566 |         // - At least one active track is either: | 
 | 6567 |         //   - a fast track with gaming usage or | 
 | 6568 |         //   - a track with acessibility usage | 
 | 6569 |         for (const auto& track : mActiveTracks) { | 
 | 6570 |             if ((track->isFastTrack() && track->attributes().usage == AUDIO_USAGE_GAME) | 
 | 6571 |                     || track->attributes().usage == AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY) { | 
 | 6572 |                 latencyMode = AUDIO_LATENCY_MODE_LOW; | 
 | 6573 |                 break; | 
 | 6574 |             } | 
 | 6575 |         } | 
 | 6576 |     } | 
 | 6577 |  | 
 | 6578 |     if (latencyMode != mSetLatencyMode) { | 
 | 6579 |         status_t status = mOutput->stream->setLatencyMode(latencyMode); | 
 | 6580 |         ALOGD("%s: thread(%d) setLatencyMode(%s) returned %d", | 
 | 6581 |                 __func__, mId, toString(latencyMode).c_str(), status); | 
 | 6582 |         if (status == NO_ERROR) { | 
 | 6583 |             mSetLatencyMode = latencyMode; | 
 | 6584 |         } | 
 | 6585 |     } | 
 | 6586 | } | 
 | 6587 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6588 | void MixerThread::updateHalSupportedLatencyModes_l() { | 
| Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 6589 |  | 
 | 6590 |     if (mOutput == nullptr || mOutput->stream == nullptr) { | 
 | 6591 |         return; | 
 | 6592 |     } | 
 | 6593 |     std::vector<audio_latency_mode_t> latencyModes; | 
 | 6594 |     const status_t status = mOutput->stream->getRecommendedLatencyModes(&latencyModes); | 
 | 6595 |     if (status != NO_ERROR) { | 
 | 6596 |         latencyModes.clear(); | 
 | 6597 |     } | 
 | 6598 |     if (latencyModes != mSupportedLatencyModes) { | 
 | 6599 |         ALOGD("%s: thread(%d) status %d supported latency modes: %s", | 
 | 6600 |             __func__, mId, status, toString(latencyModes).c_str()); | 
 | 6601 |         mSupportedLatencyModes.swap(latencyModes); | 
 | 6602 |         sendHalLatencyModesChangedEvent_l(); | 
 | 6603 |     } | 
 | 6604 | } | 
 | 6605 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6606 | status_t MixerThread::getSupportedLatencyModes( | 
| Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 6607 |         std::vector<audio_latency_mode_t>* modes) { | 
 | 6608 |     if (modes == nullptr) { | 
 | 6609 |         return BAD_VALUE; | 
 | 6610 |     } | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 6611 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 6612 |     *modes = mSupportedLatencyModes; | 
 | 6613 |     return NO_ERROR; | 
 | 6614 | } | 
 | 6615 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6616 | void MixerThread::onRecommendedLatencyModeChanged( | 
| Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 6617 |         std::vector<audio_latency_mode_t> modes) { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 6618 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 6619 |     if (modes != mSupportedLatencyModes) { | 
 | 6620 |         ALOGD("%s: thread(%d) supported latency modes: %s", | 
 | 6621 |             __func__, mId, toString(modes).c_str()); | 
 | 6622 |         mSupportedLatencyModes.swap(modes); | 
 | 6623 |         sendHalLatencyModesChangedEvent_l(); | 
 | 6624 |     } | 
 | 6625 | } | 
 | 6626 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6627 | status_t MixerThread::setBluetoothVariableLatencyEnabled(bool enabled) { | 
| Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 6628 |     if (mOutput == nullptr || mOutput->audioHwDev == nullptr | 
 | 6629 |             || !mOutput->audioHwDev->supportsBluetoothVariableLatency()) { | 
 | 6630 |         return INVALID_OPERATION; | 
 | 6631 |     } | 
 | 6632 |     mBluetoothLatencyModesEnabled.store(enabled); | 
 | 6633 |     return NO_ERROR; | 
 | 6634 | } | 
 | 6635 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6636 | // ---------------------------------------------------------------------------- | 
 | 6637 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6638 | /* static */ | 
 | 6639 | sp<IAfPlaybackThread> IAfPlaybackThread::createDirectOutputThread( | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 6640 |         const sp<IAfThreadCallback>& afThreadCallback, | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6641 |         AudioStreamOut* output, audio_io_handle_t id, bool systemReady, | 
 | 6642 |         const audio_offload_info_t& offloadInfo) { | 
 | 6643 |     return sp<DirectOutputThread>::make( | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 6644 |             afThreadCallback, output, id, systemReady, offloadInfo); | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6645 | } | 
 | 6646 |  | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 6647 | DirectOutputThread::DirectOutputThread(const sp<IAfThreadCallback>& afThreadCallback, | 
| Gareth Fenn | b18c1a3 | 2022-10-05 13:42:36 -0700 | [diff] [blame] | 6648 |         AudioStreamOut* output, audio_io_handle_t id, ThreadBase::type_t type, bool systemReady, | 
 | 6649 |         const audio_offload_info_t& offloadInfo) | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 6650 |     :   PlaybackThread(afThreadCallback, output, id, type, systemReady) | 
| Gareth Fenn | b18c1a3 | 2022-10-05 13:42:36 -0700 | [diff] [blame] | 6651 |     , mOffloadInfo(offloadInfo) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6652 | { | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 6653 |     setMasterBalance(afThreadCallback->getMasterBalance_l()); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6654 | } | 
 | 6655 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6656 | DirectOutputThread::~DirectOutputThread() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6657 | { | 
 | 6658 | } | 
 | 6659 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6660 | void DirectOutputThread::dumpInternals_l(int fd, const Vector<String16>& args) | 
| Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 6661 | { | 
| Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 6662 |     PlaybackThread::dumpInternals_l(fd, args); | 
| Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 6663 |     dprintf(fd, "  Master balance: %f  Left: %f  Right: %f\n", | 
 | 6664 |             mMasterBalance.load(), mMasterBalanceLeft, mMasterBalanceRight); | 
 | 6665 | } | 
 | 6666 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6667 | void DirectOutputThread::setMasterBalance(float balance) | 
| Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 6668 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 6669 |     audio_utils::lock_guard _l(mutex()); | 
| Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 6670 |     if (mMasterBalance != balance) { | 
 | 6671 |         mMasterBalance.store(balance); | 
 | 6672 |         mBalance.computeStereoBalance(balance, &mMasterBalanceLeft, &mMasterBalanceRight); | 
 | 6673 |         broadcast_l(); | 
 | 6674 |     } | 
 | 6675 | } | 
 | 6676 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6677 | void DirectOutputThread::processVolume_l(IAfTrack* track, bool lastTrack) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6678 | { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6679 |     float left, right; | 
 | 6680 |  | 
| Andy Hung | 333ab96 | 2019-05-28 20:23:35 -0700 | [diff] [blame] | 6681 |     // Ensure volumeshaper state always advances even when muted. | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6682 |     const sp<AudioTrackServerProxy> proxy = track->audioTrackServerProxy(); | 
| Andy Hung | 398ffa2 | 2022-12-13 19:19:53 -0800 | [diff] [blame] | 6683 |  | 
| Andy Hung | 398ffa2 | 2022-12-13 19:19:53 -0800 | [diff] [blame] | 6684 |     const int64_t frames = mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]; | 
 | 6685 |     const int64_t time = mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; | 
 | 6686 |  | 
| Dean Wheatley | b11b002 | 2023-09-12 04:07:35 +1000 | [diff] [blame] | 6687 |     ALOGVV("%s: Direct/Offload bufferConsumed:%zu  timestamp frames:%lld  time:%lld", | 
 | 6688 |             __func__, proxy->framesReleased(), (long long)frames, (long long)time); | 
| Andy Hung | 398ffa2 | 2022-12-13 19:19:53 -0800 | [diff] [blame] | 6689 |  | 
 | 6690 |     const int64_t volumeShaperFrames = | 
 | 6691 |             mMonotonicFrameCounter.updateAndGetMonotonicFrameCount(frames, time); | 
 | 6692 |     const auto [shaperVolume, shaperActive] = | 
 | 6693 |             track->getVolumeHandler()->getVolume(volumeShaperFrames); | 
| Andy Hung | 333ab96 | 2019-05-28 20:23:35 -0700 | [diff] [blame] | 6694 |     mVolumeShaperActive = shaperActive; | 
 | 6695 |  | 
| Vlad Popa | e2f5aef | 2022-07-25 16:00:20 +0200 | [diff] [blame] | 6696 |     gain_minifloat_packed_t vlr = proxy->getVolumeLR(); | 
 | 6697 |     left = float_from_gain(gain_minifloat_unpack_left(vlr)); | 
 | 6698 |     right = float_from_gain(gain_minifloat_unpack_right(vlr)); | 
 | 6699 |  | 
 | 6700 |     const bool clientVolumeMute = (left == 0.f && right == 0.f); | 
 | 6701 |  | 
| Jean-Michel Trivi | 74e01fa | 2019-02-25 12:18:09 -0800 | [diff] [blame] | 6702 |     if (mMasterMute || mStreamTypes[track->streamType()].mute || track->isPlaybackRestricted()) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6703 |         left = right = 0; | 
 | 6704 |     } else { | 
 | 6705 |         float typeVolume = mStreamTypes[track->streamType()].volume; | 
| Andy Hung | 333ab96 | 2019-05-28 20:23:35 -0700 | [diff] [blame] | 6706 |         const float v = mMasterVolume * typeVolume * shaperVolume; | 
| Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 6707 |  | 
| Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 6708 |         if (left > GAIN_FLOAT_UNITY) { | 
 | 6709 |             left = GAIN_FLOAT_UNITY; | 
 | 6710 |         } | 
| Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 6711 |         if (right > GAIN_FLOAT_UNITY) { | 
 | 6712 |             right = GAIN_FLOAT_UNITY; | 
 | 6713 |         } | 
| zhangjincheng | 86cb3bc | 2023-01-16 17:17:38 +0800 | [diff] [blame] | 6714 |         left *= v; | 
 | 6715 |         right *= v; | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 6716 |         if (mAfThreadCallback->getMode() != AUDIO_MODE_IN_COMMUNICATION | 
| zhangjincheng | 86cb3bc | 2023-01-16 17:17:38 +0800 | [diff] [blame] | 6717 |                 || audio_channel_count_from_out_mask(mChannelMask) > 1) { | 
 | 6718 |             left *= mMasterBalanceLeft; // DirectOutputThread balance applied as track volume | 
 | 6719 |             right *= mMasterBalanceRight; | 
 | 6720 |         } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6721 |     } | 
 | 6722 |  | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 6723 |     track->processMuteEvent_l(mAfThreadCallback->getOrCreateAudioManager(), | 
| Vlad Popa | e8d9947 | 2022-06-30 16:02:48 +0200 | [diff] [blame] | 6724 |         /*muteState=*/{mMasterMute, | 
 | 6725 |                        mStreamTypes[track->streamType()].volume == 0.f, | 
 | 6726 |                        mStreamTypes[track->streamType()].mute, | 
| Vlad Popa | e2f5aef | 2022-07-25 16:00:20 +0200 | [diff] [blame] | 6727 |                        track->isPlaybackRestricted(), | 
| Vlad Popa | 436c917 | 2022-08-02 15:25:08 +0200 | [diff] [blame] | 6728 |                        clientVolumeMute, | 
 | 6729 |                        shaperVolume == 0.f}); | 
| Vlad Popa | e8d9947 | 2022-06-30 16:02:48 +0200 | [diff] [blame] | 6730 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6731 |     if (lastTrack) { | 
| jiabin | 76d9469 | 2022-12-15 21:51:21 +0000 | [diff] [blame] | 6732 |         track->setFinalVolume(left, right); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6733 |         if (left != mLeftVolFloat || right != mRightVolFloat) { | 
 | 6734 |             mLeftVolFloat = left; | 
 | 6735 |             mRightVolFloat = right; | 
 | 6736 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6737 |             // Delegate volume control to effect in track effect chain if needed | 
 | 6738 |             // only one effect chain can be present on DirectOutputThread, so if | 
 | 6739 |             // there is one, the track is connected to it | 
 | 6740 |             if (!mEffectChains.isEmpty()) { | 
| Tomoharu Kasahara | 1990bd4 | 2014-12-12 14:04:11 +0900 | [diff] [blame] | 6741 |                 // if effect chain exists, volume is handled by it. | 
 | 6742 |                 // Convert volumes from float to 8.24 | 
 | 6743 |                 uint32_t vl = (uint32_t)(left * (1 << 24)); | 
 | 6744 |                 uint32_t vr = (uint32_t)(right * (1 << 24)); | 
| Shunkai Yao | f484765 | 2024-01-12 00:25:20 +0000 | [diff] [blame] | 6745 |                 // Direct/Offload effect chains set output volume in setVolume(). | 
 | 6746 |                 (void)mEffectChains[0]->setVolume(&vl, &vr); | 
| Tomoharu Kasahara | 1990bd4 | 2014-12-12 14:04:11 +0900 | [diff] [blame] | 6747 |             } else { | 
 | 6748 |                 // otherwise we directly set the volume. | 
 | 6749 |                 setVolumeForOutput_l(left, right); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6750 |             } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6751 |         } | 
 | 6752 |     } | 
 | 6753 | } | 
 | 6754 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6755 | void DirectOutputThread::onAddNewTrack_l() | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 6756 | { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6757 |     sp<IAfTrack> previousTrack = mPreviousTrack.promote(); | 
 | 6758 |     sp<IAfTrack> latestTrack = mActiveTracks.getLatest(); | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 6759 |  | 
| Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 6760 |     if (previousTrack != 0 && latestTrack != 0) { | 
 | 6761 |         if (mType == DIRECT) { | 
 | 6762 |             if (previousTrack.get() != latestTrack.get()) { | 
 | 6763 |                 mFlushPending = true; | 
 | 6764 |             } | 
 | 6765 |         } else /* mType == OFFLOAD */ { | 
| Dean Wheatley | 0f51fd0 | 2022-08-31 16:41:40 +1000 | [diff] [blame] | 6766 |             if (previousTrack->sessionId() != latestTrack->sessionId() || | 
 | 6767 |                 previousTrack->isFlushPending()) { | 
| Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 6768 |                 mFlushPending = true; | 
 | 6769 |             } | 
 | 6770 |         } | 
| Revathi Uddaraju | 20413a9 | 2016-10-13 17:16:14 +0800 | [diff] [blame] | 6771 |     } else if (previousTrack == 0) { | 
 | 6772 |         // there could be an old track added back during track transition for direct | 
 | 6773 |         // output, so always issues flush to flush data of the previous track if it | 
 | 6774 |         // was already destroyed with HAL paused, then flush can resume the playback | 
 | 6775 |         mFlushPending = true; | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 6776 |     } | 
 | 6777 |     PlaybackThread::onAddNewTrack_l(); | 
 | 6778 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6779 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6780 | PlaybackThread::mixer_state DirectOutputThread::prepareTracks_l( | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6781 |     Vector<sp<IAfTrack>>* tracksToRemove | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6782 | ) | 
 | 6783 | { | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 6784 |     size_t count = mActiveTracks.size(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6785 |     mixer_state mixerStatus = MIXER_IDLE; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6786 |     bool doHwPause = false; | 
 | 6787 |     bool doHwResume = false; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6788 |  | 
 | 6789 |     // find out which tracks need to be processed | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6790 |     for (const sp<IAfTrack>& t : mActiveTracks) { | 
| Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 6791 |         if (t->isInvalid()) { | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 6792 |             ALOGW("An invalidated track shouldn't be in active list"); | 
| Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 6793 |             tracksToRemove->add(t); | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 6794 |             continue; | 
 | 6795 |         } | 
 | 6796 |  | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6797 |         IAfTrack* const track = t.get(); | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 6798 | #ifdef VERY_VERY_VERBOSE_LOGGING | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6799 |         audio_track_cblk_t* cblk = track->cblk(); | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 6800 | #endif | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 6801 |         // Only consider last track started for volume and mixer state control. | 
 | 6802 |         // In theory an older track could underrun and restart after the new one starts | 
 | 6803 |         // but as we only care about the transition phase between two tracks on a | 
 | 6804 |         // direct output, it is not a problem to ignore the underrun case. | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6805 |         sp<IAfTrack> l = mActiveTracks.getLatest(); | 
| Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 6806 |         bool last = l.get() == track; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6807 |  | 
| Kuowei Li | 2366647 | 2021-01-20 10:23:25 +0800 | [diff] [blame] | 6808 |         if (track->isPausePending()) { | 
 | 6809 |             track->pauseAck(); | 
 | 6810 |             // It is possible a track might have been flushed or stopped. | 
 | 6811 |             // Other operations such as flush pending might occur on the next prepare. | 
 | 6812 |             if (track->isPausing()) { | 
 | 6813 |                 track->setPaused(); | 
 | 6814 |             } | 
 | 6815 |             // Always perform pause, as an immediate flush will change | 
 | 6816 |             // the pause state to be no longer isPausing(). | 
| Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 6817 |             if (mHwSupportsPause && last && !mHwPaused) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6818 |                 doHwPause = true; | 
 | 6819 |                 mHwPaused = true; | 
 | 6820 |             } | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6821 |         } else if (track->isFlushPending()) { | 
 | 6822 |             track->flushAck(); | 
 | 6823 |             if (last) { | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 6824 |                 mFlushPending = true; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6825 |             } | 
| Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 6826 |         } else if (track->isResumePending()) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6827 |             track->resumeAck(); | 
| Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 6828 |             if (last) { | 
 | 6829 |                 mLeftVolFloat = mRightVolFloat = -1.0; | 
 | 6830 |                 if (mHwPaused) { | 
 | 6831 |                     doHwResume = true; | 
 | 6832 |                     mHwPaused = false; | 
 | 6833 |                 } | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6834 |             } | 
 | 6835 |         } | 
 | 6836 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6837 |         // The first time a track is added we wait | 
| Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 6838 |         // for all its buffers to be filled before processing it. | 
 | 6839 |         // Allow draining the buffer in case the client | 
 | 6840 |         // app does not call stop() and relies on underrun to stop: | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6841 |         // hence the test on (track->retryCount() > 1). | 
 | 6842 |         // If track->retryCount() <= 1 then track is about to be disabled, paused, removed, | 
| Andy Hung | 455982f | 2021-04-27 17:46:12 -0700 | [diff] [blame] | 6843 |         // so we accept any nonzero amount of data delivered by the AudioTrack (which will | 
 | 6844 |         // reset the retry counter). | 
| Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 6845 |         // Do not use a high threshold for compressed audio. | 
| Andy Hung | 455982f | 2021-04-27 17:46:12 -0700 | [diff] [blame] | 6846 |  | 
 | 6847 |         // target retry count that we will use is based on the time we wait for retries. | 
 | 6848 |         const int32_t targetRetryCount = kMaxTrackRetriesDirectMs * 1000 / mActiveSleepTimeUs; | 
 | 6849 |         // the retry threshold is when we accept any size for PCM data.  This is slightly | 
 | 6850 |         // smaller than the retry count so we can push small bits of data without a glitch. | 
 | 6851 |         const int32_t retryThreshold = targetRetryCount > 2 ? targetRetryCount - 1 : 1; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6852 |         uint32_t minFrames; | 
| Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 6853 |         if ((track->sharedBuffer() == 0) && !track->isStopping_1() && !track->isPausing() | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6854 |             && (track->retryCount() > retryThreshold) && audio_has_proportional_frames(mFormat)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6855 |             minFrames = mNormalFrameCount; | 
 | 6856 |         } else { | 
 | 6857 |             minFrames = 1; | 
 | 6858 |         } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6859 |  | 
| Mikhail Naganov | ddb07bc | 2019-08-15 20:18:47 -0700 | [diff] [blame] | 6860 |         const size_t framesReady = track->framesReady(); | 
 | 6861 |         const int trackId = track->id(); | 
 | 6862 |         if (ATRACE_ENABLED()) { | 
 | 6863 |             std::string traceName("nRdy"); | 
 | 6864 |             traceName += std::to_string(trackId); | 
 | 6865 |             ATRACE_INT(traceName.c_str(), framesReady); | 
 | 6866 |         } | 
 | 6867 |         if ((framesReady >= minFrames) && track->isReady() && !track->isPaused() && | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 6868 |                 !track->isStopping_2() && !track->isStopped()) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6869 |         { | 
| Mikhail Naganov | ddb07bc | 2019-08-15 20:18:47 -0700 | [diff] [blame] | 6870 |             ALOGVV("track(%d) s=%08x [OK]", trackId, cblk->mServer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6871 |  | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6872 |             if (track->fillingStatus() == IAfTrack::FS_FILLED) { | 
 | 6873 |                 track->fillingStatus() = IAfTrack::FS_ACTIVE; | 
| Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 6874 |                 if (last) { | 
 | 6875 |                     // make sure processVolume_l() will apply new volume even if 0 | 
 | 6876 |                     mLeftVolFloat = mRightVolFloat = -1.0; | 
 | 6877 |                 } | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6878 |                 if (!mHwSupportsPause) { | 
 | 6879 |                     track->resumeAck(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6880 |                 } | 
 | 6881 |             } | 
 | 6882 |  | 
 | 6883 |             // compute volume for this track | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6884 |             processVolume_l(track, last); | 
 | 6885 |             if (last) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6886 |                 sp<IAfTrack> previousTrack = mPreviousTrack.promote(); | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 6887 |                 if (previousTrack != 0) { | 
 | 6888 |                     if (track != previousTrack.get()) { | 
 | 6889 |                         // Flush any data still being written from last track | 
 | 6890 |                         mBytesRemaining = 0; | 
| Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 6891 |                         // Invalidate previous track to force a seek when resuming. | 
 | 6892 |                         previousTrack->invalidate(); | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 6893 |                     } | 
 | 6894 |                 } | 
 | 6895 |                 mPreviousTrack = track; | 
 | 6896 |  | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 6897 |                 // reset retry count | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6898 |                 track->retryCount() = targetRetryCount; | 
| Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 6899 |                 mActiveTrack = t; | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 6900 |                 mixerStatus = MIXER_TRACKS_READY; | 
| Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 6901 |                 if (mHwPaused) { | 
| Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 6902 |                     doHwResume = true; | 
 | 6903 |                     mHwPaused = false; | 
 | 6904 |                 } | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 6905 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6906 |         } else { | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 6907 |             // clear effect chain input buffer if the last active track started underruns | 
 | 6908 |             // to avoid sending previous audio buffer again to effects | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 6909 |             if (!mEffectChains.isEmpty() && last) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6910 |                 mEffectChains[0]->clearInputBuffer(); | 
 | 6911 |             } | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 6912 |             if (track->isStopping_1()) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6913 |                 track->setState(IAfTrackBase::STOPPING_2); | 
| Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 6914 |                 if (last && mHwPaused) { | 
 | 6915 |                      doHwResume = true; | 
 | 6916 |                      mHwPaused = false; | 
 | 6917 |                  } | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 6918 |             } | 
 | 6919 |             if ((track->sharedBuffer() != 0) || track->isStopped() || | 
 | 6920 |                     track->isStopping_2() || track->isPaused()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6921 |                 // We have consumed all the buffers of this track. | 
 | 6922 |                 // Remove it from the list of active tracks. | 
| Atneya Nair | 0cae043 | 2022-05-10 18:12:12 -0400 | [diff] [blame] | 6923 |                 bool presComplete = false; | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 6924 |                 if (mStandby || !last || | 
| Atneya Nair | 0cae043 | 2022-05-10 18:12:12 -0400 | [diff] [blame] | 6925 |                         (presComplete = track->presentationComplete(latency_l())) || | 
| Jindong | 32dc26e | 2019-11-11 18:10:01 +0800 | [diff] [blame] | 6926 |                         track->isPaused() || mHwPaused) { | 
| Atneya Nair | 0cae043 | 2022-05-10 18:12:12 -0400 | [diff] [blame] | 6927 |                     if (presComplete) { | 
 | 6928 |                         mOutput->presentationComplete(); | 
 | 6929 |                     } | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 6930 |                     if (track->isStopping_2()) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6931 |                         track->setState(IAfTrackBase::STOPPED); | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 6932 |                     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6933 |                     if (track->isStopped()) { | 
 | 6934 |                         track->reset(); | 
 | 6935 |                     } | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 6936 |                     tracksToRemove->add(track); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6937 |                 } | 
 | 6938 |             } else { | 
 | 6939 |                 // No buffers for this track. Give it a few chances to | 
 | 6940 |                 // fill a buffer, then remove it from active list. | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 6941 |                 // Only consider last track started for mixer state control | 
| Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 6942 |                 bool isTimestampAdvancing = mIsTimestampAdvancing.check(mOutput); | 
| Gareth Fenn | b18c1a3 | 2022-10-05 13:42:36 -0700 | [diff] [blame] | 6943 |                 if (!isTunerStream()  // tuner streams remain active in underrun | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6944 |                         && --(track->retryCount()) <= 0) { | 
| Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 6945 |                     if (isTimestampAdvancing) { // HAL is still playing audio, give us more time. | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6946 |                         track->retryCount() = kMaxTrackRetriesOffload; | 
| ziyangch | 8f194f1 | 2021-12-01 13:48:04 -0800 | [diff] [blame] | 6947 |                     } else { | 
 | 6948 |                         ALOGV("BUFFER TIMEOUT: remove track(%d) from active list", trackId); | 
 | 6949 |                         tracksToRemove->add(track); | 
 | 6950 |                         // indicate to client process that the track was disabled because of | 
 | 6951 |                         // underrun; it will then automatically call start() when data is available | 
 | 6952 |                         track->disable(); | 
 | 6953 |                         // only do hw pause when track is going to be removed due to BUFFER TIMEOUT. | 
 | 6954 |                         // unlike mixerthread, HAL can be paused for direct output | 
 | 6955 |                         ALOGW("pause because of UNDERRUN, framesReady = %zu," | 
 | 6956 |                                 "minFrames = %u, mFormat = %#x", | 
 | 6957 |                                 framesReady, minFrames, mFormat); | 
 | 6958 |                         if (last && mHwSupportsPause && !mHwPaused && !mStandby) { | 
 | 6959 |                             doHwPause = true; | 
 | 6960 |                             mHwPaused = true; | 
 | 6961 |                         } | 
| Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 6962 |                     } | 
| Haynes Mathew George | 5c6daae | 2017-01-24 20:06:05 -0800 | [diff] [blame] | 6963 |                 } else if (last) { | 
 | 6964 |                     mixerStatus = MIXER_TRACKS_ENABLED; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6965 |                 } | 
 | 6966 |             } | 
 | 6967 |         } | 
 | 6968 |     } | 
 | 6969 |  | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6970 |     // 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] | 6971 |     if (!mFlushPending) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6972 |         for (size_t i = 0; i < mTracks.size(); i++) { | 
 | 6973 |             if (mTracks[i]->isFlushPending()) { | 
 | 6974 |                 mTracks[i]->flushAck(); | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 6975 |                 mFlushPending = true; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6976 |             } | 
 | 6977 |         } | 
 | 6978 |     } | 
 | 6979 |  | 
 | 6980 |     // make sure the pause/flush/resume sequence is executed in the right order. | 
 | 6981 |     // If a flush is pending and a track is active but the HW is not paused, force a HW pause | 
 | 6982 |     // before flush and then resume HW. This can happen in case of pause/flush/resume | 
 | 6983 |     // if resume is received before pause is executed. | 
 | 6984 |     if (mHwSupportsPause && !mStandby && | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 6985 |             (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6986 |         status_t result = mOutput->stream->pause(); | 
 | 6987 |         ALOGE_IF(result != OK, "Error when pausing output stream: %d", result); | 
| Gareth Fenn | cd1e162 | 2022-10-05 11:45:45 -0700 | [diff] [blame] | 6988 |         doHwResume = !doHwPause;  // resume if pause is due to flush. | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6989 |     } | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 6990 |     if (mFlushPending) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6991 |         flushHw_l(); | 
 | 6992 |     } | 
 | 6993 |     if (mHwSupportsPause && !mStandby && doHwResume) { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6994 |         status_t result = mOutput->stream->resume(); | 
 | 6995 |         ALOGE_IF(result != OK, "Error when resuming output stream: %d", result); | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6996 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6997 |     // remove all the tracks that need to be... | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6998 |     removeTracks_l(*tracksToRemove); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6999 |  | 
 | 7000 |     return mixerStatus; | 
 | 7001 | } | 
 | 7002 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7003 | void DirectOutputThread::threadLoop_mix() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7004 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7005 |     size_t frameCount = mFrameCount; | 
| Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 7006 |     int8_t *curBuf = (int8_t *)mSinkBuffer; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7007 |     // output audio to hardware | 
 | 7008 |     while (frameCount) { | 
| Glenn Kasten | 34542ac | 2013-06-26 11:29:02 -0700 | [diff] [blame] | 7009 |         AudioBufferProvider::Buffer buffer; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7010 |         buffer.frameCount = frameCount; | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 7011 |         status_t status = mActiveTrack->getNextBuffer(&buffer); | 
 | 7012 |         if (status != NO_ERROR || buffer.raw == NULL) { | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 7013 |             // no need to pad with 0 for compressed audio | 
 | 7014 |             if (audio_has_proportional_frames(mFormat)) { | 
 | 7015 |                 memset(curBuf, 0, frameCount * mFrameSize); | 
 | 7016 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7017 |             break; | 
 | 7018 |         } | 
 | 7019 |         memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); | 
 | 7020 |         frameCount -= buffer.frameCount; | 
 | 7021 |         curBuf += buffer.frameCount * mFrameSize; | 
 | 7022 |         mActiveTrack->releaseBuffer(&buffer); | 
 | 7023 |     } | 
| Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 7024 |     mCurrentWriteLength = curBuf - (int8_t *)mSinkBuffer; | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7025 |     mSleepTimeUs = 0; | 
 | 7026 |     mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7027 |     mActiveTrack.clear(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7028 | } | 
 | 7029 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7030 | void DirectOutputThread::threadLoop_sleepTime() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7031 | { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7032 |     // do not write to HAL when paused | 
| Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 7033 |     if (mHwPaused || (usesHwAvSync() && mStandby)) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7034 |         mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7035 |         return; | 
 | 7036 |     } | 
| Andy Hung | 85ba333 | 2021-04-27 17:40:26 -0700 | [diff] [blame] | 7037 |     if (mMixerStatus == MIXER_TRACKS_ENABLED) { | 
 | 7038 |         mSleepTimeUs = mActiveSleepTimeUs; | 
 | 7039 |     } else { | 
 | 7040 |         mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7041 |     } | 
| Andy Hung | 85ba333 | 2021-04-27 17:40:26 -0700 | [diff] [blame] | 7042 |     // Note: In S or later, we do not write zeroes for | 
 | 7043 |     // linear or proportional PCM direct tracks in underrun. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7044 | } | 
 | 7045 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7046 | void DirectOutputThread::threadLoop_exit() | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7047 | { | 
 | 7048 |     { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7049 |         audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7050 |         for (size_t i = 0; i < mTracks.size(); i++) { | 
 | 7051 |             if (mTracks[i]->isFlushPending()) { | 
 | 7052 |                 mTracks[i]->flushAck(); | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 7053 |                 mFlushPending = true; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7054 |             } | 
 | 7055 |         } | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 7056 |         if (mFlushPending) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7057 |             flushHw_l(); | 
 | 7058 |         } | 
 | 7059 |     } | 
 | 7060 |     PlaybackThread::threadLoop_exit(); | 
 | 7061 | } | 
 | 7062 |  | 
 | 7063 | // must be called with thread mutex locked | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7064 | bool DirectOutputThread::shouldStandby_l() | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7065 | { | 
 | 7066 |     bool trackPaused = false; | 
| Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 7067 |     bool trackStopped = false; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7068 |  | 
 | 7069 |     // do not put the HAL in standby when paused. AwesomePlayer clear the offloaded AudioTrack | 
 | 7070 |     // after a timeout and we will enter standby then. | 
 | 7071 |     if (mTracks.size() > 0) { | 
 | 7072 |         trackPaused = mTracks[mTracks.size() - 1]->isPaused(); | 
| Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 7073 |         trackStopped = mTracks[mTracks.size() - 1]->isStopped() || | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7074 |                            mTracks[mTracks.size() - 1]->state() == IAfTrackBase::IDLE; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7075 |     } | 
 | 7076 |  | 
| Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 7077 |     return !mStandby && !(trackPaused || (mHwPaused && !trackStopped)); | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7078 | } | 
 | 7079 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 7080 | // checkForNewParameter_l() must be called with ThreadBase::mutex() held | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7081 | bool DirectOutputThread::checkForNewParameter_l(const String8& keyValuePair, | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7082 |                                                               status_t& status) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7083 | { | 
 | 7084 |     bool reconfig = false; | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7085 |     status = NO_ERROR; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7086 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7087 |     AudioParameter param = AudioParameter(keyValuePair); | 
 | 7088 |     int value; | 
 | 7089 |     if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 7090 |         LOG_FATAL("Should not set routing device in DirectOutputThread"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7091 |     } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7092 |     if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { | 
 | 7093 |         // do not accept frame count changes if tracks are open as the track buffer | 
 | 7094 |         // size depends on frame count and correct behavior would not be garantied | 
 | 7095 |         // if frame count is changed after track creation | 
 | 7096 |         if (!mTracks.isEmpty()) { | 
 | 7097 |             status = INVALID_OPERATION; | 
 | 7098 |         } else { | 
 | 7099 |             reconfig = true; | 
 | 7100 |         } | 
 | 7101 |     } | 
 | 7102 |     if (status == NO_ERROR) { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7103 |         status = mOutput->stream->setParameters(keyValuePair); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7104 |         if (!mStandby && status == INVALID_OPERATION) { | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 7105 |             mOutput->standby(); | 
| Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 7106 |             if (!mStandby) { | 
 | 7107 |                 mThreadMetrics.logEndInterval(); | 
| Andy Hung | 44d648b | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 7108 |                 mThreadSnapshot.onEnd(); | 
| Eric Laurent | 19952e1 | 2023-04-20 10:08:29 +0200 | [diff] [blame] | 7109 |                 setStandby_l(); | 
| Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 7110 |             } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7111 |             mBytesWritten = 0; | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7112 |             status = mOutput->stream->setParameters(keyValuePair); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7113 |         } | 
 | 7114 |         if (status == NO_ERROR && reconfig) { | 
 | 7115 |             readOutputParameters_l(); | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7116 |             sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7117 |         } | 
 | 7118 |     } | 
 | 7119 |  | 
| Dean Wheatley | 6891810 | 2021-03-19 22:09:19 +1100 | [diff] [blame] | 7120 |     return reconfig; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7121 | } | 
 | 7122 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7123 | uint32_t DirectOutputThread::activeSleepTimeUs() const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7124 | { | 
 | 7125 |     uint32_t time; | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 7126 |     if (audio_has_proportional_frames(mFormat)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7127 |         time = PlaybackThread::activeSleepTimeUs(); | 
 | 7128 |     } else { | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 7129 |         time = kDirectMinSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7130 |     } | 
 | 7131 |     return time; | 
 | 7132 | } | 
 | 7133 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7134 | uint32_t DirectOutputThread::idleSleepTimeUs() const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7135 | { | 
 | 7136 |     uint32_t time; | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 7137 |     if (audio_has_proportional_frames(mFormat)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7138 |         time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; | 
 | 7139 |     } else { | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 7140 |         time = kDirectMinSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7141 |     } | 
 | 7142 |     return time; | 
 | 7143 | } | 
 | 7144 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7145 | uint32_t DirectOutputThread::suspendSleepTimeUs() const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7146 | { | 
 | 7147 |     uint32_t time; | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 7148 |     if (audio_has_proportional_frames(mFormat)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7149 |         time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); | 
 | 7150 |     } else { | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 7151 |         time = kDirectMinSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7152 |     } | 
 | 7153 |     return time; | 
 | 7154 | } | 
 | 7155 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7156 | void DirectOutputThread::cacheParameters_l() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7157 | { | 
 | 7158 |     PlaybackThread::cacheParameters_l(); | 
 | 7159 |  | 
 | 7160 |     // use shorter standby delay as on normal output to release | 
 | 7161 |     // hardware resources as soon as possible | 
| Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 7162 |     // no delay on outputs with HW A/V sync | 
 | 7163 |     if (usesHwAvSync()) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7164 |         mStandbyDelayNs = 0; | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 7165 |     } else if ((mType == OFFLOAD) && !audio_has_proportional_frames(mFormat)) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7166 |         mStandbyDelayNs = kOffloadStandbyDelayNs; | 
| Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 7167 |     } else { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7168 |         mStandbyDelayNs = microseconds(mActiveSleepTimeUs*2); | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 7169 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7170 | } | 
 | 7171 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7172 | void DirectOutputThread::flushHw_l() | 
| Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 7173 | { | 
| ziyangch | 8f194f1 | 2021-12-01 13:48:04 -0800 | [diff] [blame] | 7174 |     PlaybackThread::flushHw_l(); | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 7175 |     mOutput->flush(); | 
| Haofan Wang | 3987e9d | 2024-06-17 21:22:00 +0000 | [diff] [blame] | 7176 |     mHwPaused = false; | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 7177 |     mFlushPending = false; | 
| Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 7178 |     mTimestampVerifier.discontinuity(discontinuityForStandbyOrFlush()); | 
| Sampath Shetty | 999f0e8 | 2020-01-15 10:19:06 +1100 | [diff] [blame] | 7179 |     mTimestamp.clear(); | 
| Andy Hung | 398ffa2 | 2022-12-13 19:19:53 -0800 | [diff] [blame] | 7180 |     mMonotonicFrameCounter.onFlush(); | 
| Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 7181 | } | 
 | 7182 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7183 | int64_t DirectOutputThread::computeWaitTimeNs_l() const { | 
| Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 7184 |     // If a VolumeShaper is active, we must wake up periodically to update volume. | 
 | 7185 |     const int64_t NS_PER_MS = 1000000; | 
 | 7186 |     return mVolumeShaperActive ? | 
 | 7187 |             kMinNormalSinkBufferSizeMs * NS_PER_MS : PlaybackThread::computeWaitTimeNs_l(); | 
 | 7188 | } | 
 | 7189 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7190 | // ---------------------------------------------------------------------------- | 
 | 7191 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7192 | AsyncCallbackThread::AsyncCallbackThread( | 
 | 7193 |         const wp<PlaybackThread>& playbackThread) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7194 |     :   Thread(false /*canCallJava*/), | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 7195 |         mPlaybackThread(playbackThread), | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7196 |         mWriteAckSequence(0), | 
| Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 7197 |         mDrainSequence(0), | 
| Mikhail Naganov | f548cd3 | 2024-05-29 17:06:46 +0000 | [diff] [blame] | 7198 |         mAsyncError(ASYNC_ERROR_NONE) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7199 | { | 
 | 7200 | } | 
 | 7201 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7202 | void AsyncCallbackThread::onFirstRef() | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7203 | { | 
 | 7204 |     run("Offload Cbk", ANDROID_PRIORITY_URGENT_AUDIO); | 
 | 7205 | } | 
 | 7206 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7207 | bool AsyncCallbackThread::threadLoop() | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7208 | { | 
 | 7209 |     while (!exitPending()) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7210 |         uint32_t writeAckSequence; | 
 | 7211 |         uint32_t drainSequence; | 
| Mikhail Naganov | f548cd3 | 2024-05-29 17:06:46 +0000 | [diff] [blame] | 7212 |         AsyncError asyncError; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7213 |  | 
 | 7214 |         { | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 7215 |             audio_utils::unique_lock _l(mutex()); | 
| Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 7216 |             while (!((mWriteAckSequence & 1) || | 
 | 7217 |                      (mDrainSequence & 1) || | 
| Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 7218 |                      mAsyncError || | 
| Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 7219 |                      exitPending())) { | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 7220 |                 mWaitWorkCV.wait(_l); | 
| Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 7221 |             } | 
 | 7222 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7223 |             if (exitPending()) { | 
 | 7224 |                 break; | 
 | 7225 |             } | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7226 |             ALOGV("AsyncCallbackThread mWriteAckSequence %d mDrainSequence %d", | 
 | 7227 |                   mWriteAckSequence, mDrainSequence); | 
 | 7228 |             writeAckSequence = mWriteAckSequence; | 
 | 7229 |             mWriteAckSequence &= ~1; | 
 | 7230 |             drainSequence = mDrainSequence; | 
 | 7231 |             mDrainSequence &= ~1; | 
| Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 7232 |             asyncError = mAsyncError; | 
| Mikhail Naganov | f548cd3 | 2024-05-29 17:06:46 +0000 | [diff] [blame] | 7233 |             mAsyncError = ASYNC_ERROR_NONE; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7234 |         } | 
 | 7235 |         { | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7236 |             const sp<PlaybackThread> playbackThread = mPlaybackThread.promote(); | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 7237 |             if (playbackThread != 0) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7238 |                 if (writeAckSequence & 1) { | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 7239 |                     playbackThread->resetWriteBlocked(writeAckSequence >> 1); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7240 |                 } | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7241 |                 if (drainSequence & 1) { | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 7242 |                     playbackThread->resetDraining(drainSequence >> 1); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7243 |                 } | 
| Mikhail Naganov | f548cd3 | 2024-05-29 17:06:46 +0000 | [diff] [blame] | 7244 |                 if (asyncError != ASYNC_ERROR_NONE) { | 
 | 7245 |                     playbackThread->onAsyncError(asyncError == ASYNC_ERROR_HARD); | 
| Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 7246 |                 } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7247 |             } | 
 | 7248 |         } | 
 | 7249 |     } | 
 | 7250 |     return false; | 
 | 7251 | } | 
 | 7252 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7253 | void AsyncCallbackThread::exit() | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7254 | { | 
 | 7255 |     ALOGV("AsyncCallbackThread::exit"); | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7256 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7257 |     requestExit(); | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 7258 |     mWaitWorkCV.notify_all(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7259 | } | 
 | 7260 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7261 | void AsyncCallbackThread::setWriteBlocked(uint32_t sequence) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7262 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7263 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7264 |     // bit 0 is cleared | 
 | 7265 |     mWriteAckSequence = sequence << 1; | 
 | 7266 | } | 
 | 7267 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7268 | void AsyncCallbackThread::resetWriteBlocked() | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7269 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7270 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7271 |     // ignore unexpected callbacks | 
 | 7272 |     if (mWriteAckSequence & 2) { | 
 | 7273 |         mWriteAckSequence |= 1; | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 7274 |         mWaitWorkCV.notify_one(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7275 |     } | 
 | 7276 | } | 
 | 7277 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7278 | void AsyncCallbackThread::setDraining(uint32_t sequence) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7279 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7280 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7281 |     // bit 0 is cleared | 
 | 7282 |     mDrainSequence = sequence << 1; | 
 | 7283 | } | 
 | 7284 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7285 | void AsyncCallbackThread::resetDraining() | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7286 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7287 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7288 |     // ignore unexpected callbacks | 
 | 7289 |     if (mDrainSequence & 2) { | 
 | 7290 |         mDrainSequence |= 1; | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 7291 |         mWaitWorkCV.notify_one(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7292 |     } | 
 | 7293 | } | 
 | 7294 |  | 
| Mikhail Naganov | f548cd3 | 2024-05-29 17:06:46 +0000 | [diff] [blame] | 7295 | void AsyncCallbackThread::setAsyncError(bool isHardError) | 
| Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 7296 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7297 |     audio_utils::lock_guard _l(mutex()); | 
| Mikhail Naganov | f548cd3 | 2024-05-29 17:06:46 +0000 | [diff] [blame] | 7298 |     mAsyncError = isHardError ? ASYNC_ERROR_HARD : ASYNC_ERROR_SOFT; | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 7299 |     mWaitWorkCV.notify_one(); | 
| Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 7300 | } | 
 | 7301 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7302 |  | 
 | 7303 | // ---------------------------------------------------------------------------- | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7304 |  | 
 | 7305 | /* static */ | 
 | 7306 | sp<IAfPlaybackThread> IAfPlaybackThread::createOffloadThread( | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7307 |         const sp<IAfThreadCallback>& afThreadCallback, | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7308 |         AudioStreamOut* output, audio_io_handle_t id, bool systemReady, | 
 | 7309 |         const audio_offload_info_t& offloadInfo) { | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7310 |     return sp<OffloadThread>::make(afThreadCallback, output, id, systemReady, offloadInfo); | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7311 | } | 
 | 7312 |  | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7313 | OffloadThread::OffloadThread(const sp<IAfThreadCallback>& afThreadCallback, | 
| Gareth Fenn | b18c1a3 | 2022-10-05 13:42:36 -0700 | [diff] [blame] | 7314 |         AudioStreamOut* output, audio_io_handle_t id, bool systemReady, | 
 | 7315 |         const audio_offload_info_t& offloadInfo) | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7316 |     :   DirectOutputThread(afThreadCallback, output, id, OFFLOAD, systemReady, offloadInfo), | 
| ziyangch | 8f194f1 | 2021-12-01 13:48:04 -0800 | [diff] [blame] | 7317 |         mPausedWriteLength(0), mPausedBytesRemaining(0), mKeepWakeLock(true) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7318 | { | 
| Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 7319 |     //FIXME: mStandby should be set to true by ThreadBase constructo | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 7320 |     mStandby = true; | 
| Eric Laurent | 6466797 | 2016-03-30 18:19:46 -0700 | [diff] [blame] | 7321 |     mKeepWakeLock = property_get_bool("ro.audio.offload_wakelock", true /* default_value */); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7322 | } | 
 | 7323 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7324 | void OffloadThread::threadLoop_exit() | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7325 | { | 
 | 7326 |     if (mFlushPending || mHwPaused) { | 
 | 7327 |         // If a flush is pending or track was paused, just discard buffered data | 
| Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 7328 |         audio_utils::lock_guard l(mutex()); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7329 |         flushHw_l(); | 
 | 7330 |     } else { | 
 | 7331 |         mMixerStatus = MIXER_DRAIN_ALL; | 
 | 7332 |         threadLoop_drain(); | 
 | 7333 |     } | 
| Uday Gupta | 56604aa | 2014-05-13 11:19:17 -0700 | [diff] [blame] | 7334 |     if (mUseAsyncWrite) { | 
 | 7335 |         ALOG_ASSERT(mCallbackThread != 0); | 
 | 7336 |         mCallbackThread->exit(); | 
 | 7337 |     } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7338 |     PlaybackThread::threadLoop_exit(); | 
 | 7339 | } | 
 | 7340 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7341 | PlaybackThread::mixer_state OffloadThread::prepareTracks_l( | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7342 |     Vector<sp<IAfTrack>>* tracksToRemove | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7343 | ) | 
 | 7344 | { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7345 |     size_t count = mActiveTracks.size(); | 
 | 7346 |  | 
 | 7347 |     mixer_state mixerStatus = MIXER_IDLE; | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 7348 |     bool doHwPause = false; | 
 | 7349 |     bool doHwResume = false; | 
 | 7350 |  | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 7351 |     ALOGV("OffloadThread::prepareTracks_l active tracks %zu", count); | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 7352 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7353 |     // find out which tracks need to be processed | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7354 |     for (const sp<IAfTrack>& t : mActiveTracks) { | 
 | 7355 |         IAfTrack* const track = t.get(); | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 7356 | #ifdef VERY_VERY_VERBOSE_LOGGING | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7357 |         audio_track_cblk_t* cblk = track->cblk(); | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 7358 | #endif | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 7359 |         // Only consider last track started for volume and mixer state control. | 
 | 7360 |         // In theory an older track could underrun and restart after the new one starts | 
 | 7361 |         // but as we only care about the transition phase between two tracks on a | 
 | 7362 |         // direct output, it is not a problem to ignore the underrun case. | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7363 |         sp<IAfTrack> l = mActiveTracks.getLatest(); | 
| Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 7364 |         bool last = l.get() == track; | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 7365 |  | 
| Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 7366 |         if (track->isInvalid()) { | 
 | 7367 |             ALOGW("An invalidated track shouldn't be in active list"); | 
 | 7368 |             tracksToRemove->add(track); | 
 | 7369 |             continue; | 
 | 7370 |         } | 
 | 7371 |  | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7372 |         if (track->state() == IAfTrackBase::IDLE) { | 
| Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 7373 |             ALOGW("An idle track shouldn't be in active list"); | 
 | 7374 |             continue; | 
 | 7375 |         } | 
 | 7376 |  | 
| Kuowei Li | 2366647 | 2021-01-20 10:23:25 +0800 | [diff] [blame] | 7377 |         if (track->isPausePending()) { | 
 | 7378 |             track->pauseAck(); | 
 | 7379 |             // It is possible a track might have been flushed or stopped. | 
 | 7380 |             // Other operations such as flush pending might occur on the next prepare. | 
 | 7381 |             if (track->isPausing()) { | 
 | 7382 |                 track->setPaused(); | 
 | 7383 |             } | 
 | 7384 |             // Always perform pause if last, as an immediate flush will change | 
 | 7385 |             // the pause state to be no longer isPausing(). | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7386 |             if (last) { | 
| Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 7387 |                 if (mHwSupportsPause && !mHwPaused) { | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 7388 |                     doHwPause = true; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7389 |                     mHwPaused = true; | 
 | 7390 |                 } | 
 | 7391 |                 // If we were part way through writing the mixbuffer to | 
 | 7392 |                 // the HAL we must save this until we resume | 
 | 7393 |                 // BUG - this will be wrong if a different track is made active, | 
 | 7394 |                 // in that case we want to discard the pending data in the | 
 | 7395 |                 // mixbuffer and tell the client to present it again when the | 
 | 7396 |                 // track is resumed | 
 | 7397 |                 mPausedWriteLength = mCurrentWriteLength; | 
 | 7398 |                 mPausedBytesRemaining = mBytesRemaining; | 
 | 7399 |                 mBytesRemaining = 0;    // stop writing | 
 | 7400 |             } | 
 | 7401 |             tracksToRemove->add(track); | 
| Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 7402 |         } else if (track->isFlushPending()) { | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 7403 |             if (track->isStopping_1()) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7404 |                 track->retryCount() = kMaxTrackStopRetriesOffload; | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 7405 |             } else { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7406 |                 track->retryCount() = kMaxTrackRetriesOffload; | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 7407 |             } | 
| Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 7408 |             track->flushAck(); | 
 | 7409 |             if (last) { | 
 | 7410 |                 mFlushPending = true; | 
 | 7411 |             } | 
| Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 7412 |         } else if (track->isResumePending()){ | 
 | 7413 |             track->resumeAck(); | 
 | 7414 |             if (last) { | 
 | 7415 |                 if (mPausedBytesRemaining) { | 
 | 7416 |                     // Need to continue write that was interrupted | 
 | 7417 |                     mCurrentWriteLength = mPausedWriteLength; | 
 | 7418 |                     mBytesRemaining = mPausedBytesRemaining; | 
 | 7419 |                     mPausedBytesRemaining = 0; | 
 | 7420 |                 } | 
 | 7421 |                 if (mHwPaused) { | 
 | 7422 |                     doHwResume = true; | 
 | 7423 |                     mHwPaused = false; | 
 | 7424 |                     // threadLoop_mix() will handle the case that we need to | 
 | 7425 |                     // resume an interrupted write | 
 | 7426 |                 } | 
 | 7427 |                 // enable write to audio HAL | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7428 |                 mSleepTimeUs = 0; | 
| Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 7429 |  | 
| Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 7430 |                 mLeftVolFloat = mRightVolFloat = -1.0; | 
 | 7431 |  | 
| Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 7432 |                 // Do not handle new data in this iteration even if track->framesReady() | 
 | 7433 |                 mixerStatus = MIXER_TRACKS_ENABLED; | 
 | 7434 |             } | 
 | 7435 |         }  else if (track->framesReady() && track->isReady() && | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7436 |                 !track->isPaused() && !track->isTerminated() && !track->isStopping_2()) { | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 7437 |             ALOGVV("OffloadThread: track(%d) s=%08x [OK]", track->id(), cblk->mServer); | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7438 |             if (track->fillingStatus() == IAfTrack::FS_FILLED) { | 
 | 7439 |                 track->fillingStatus() = IAfTrack::FS_ACTIVE; | 
| Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 7440 |                 if (last) { | 
 | 7441 |                     // make sure processVolume_l() will apply new volume even if 0 | 
 | 7442 |                     mLeftVolFloat = mRightVolFloat = -1.0; | 
 | 7443 |                 } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7444 |             } | 
 | 7445 |  | 
 | 7446 |             if (last) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7447 |                 sp<IAfTrack> previousTrack = mPreviousTrack.promote(); | 
| Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 7448 |                 if (previousTrack != 0) { | 
 | 7449 |                     if (track != previousTrack.get()) { | 
| Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 7450 |                         // Flush any data still being written from last track | 
 | 7451 |                         mBytesRemaining = 0; | 
 | 7452 |                         if (mPausedBytesRemaining) { | 
 | 7453 |                             // Last track was paused so we also need to flush saved | 
 | 7454 |                             // mixbuffer state and invalidate track so that it will | 
 | 7455 |                             // re-submit that unwritten data when it is next resumed | 
 | 7456 |                             mPausedBytesRemaining = 0; | 
 | 7457 |                             // Invalidate is a bit drastic - would be more efficient | 
 | 7458 |                             // to have a flag to tell client that some of the | 
 | 7459 |                             // previously written data was lost | 
| Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 7460 |                             previousTrack->invalidate(); | 
| Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 7461 |                         } | 
 | 7462 |                         // flush data already sent to the DSP if changing audio session as audio | 
 | 7463 |                         // comes from a different source. Also invalidate previous track to force a | 
 | 7464 |                         // seek when resuming. | 
| Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 7465 |                         if (previousTrack->sessionId() != track->sessionId()) { | 
 | 7466 |                             previousTrack->invalidate(); | 
| Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 7467 |                         } | 
 | 7468 |                     } | 
 | 7469 |                 } | 
 | 7470 |                 mPreviousTrack = track; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7471 |                 // reset retry count | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 7472 |                 if (track->isStopping_1()) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7473 |                     track->retryCount() = kMaxTrackStopRetriesOffload; | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 7474 |                 } else { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7475 |                     track->retryCount() = kMaxTrackRetriesOffload; | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 7476 |                 } | 
| Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 7477 |                 mActiveTrack = t; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7478 |                 mixerStatus = MIXER_TRACKS_READY; | 
 | 7479 |             } | 
 | 7480 |         } else { | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 7481 |             ALOGVV("OffloadThread: track(%d) s=%08x [NOT READY]", track->id(), cblk->mServer); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7482 |             if (track->isStopping_1()) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7483 |                 if (--(track->retryCount()) <= 0) { | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 7484 |                     // Hardware buffer can hold a large amount of audio so we must | 
 | 7485 |                     // wait for all current track's data to drain before we say | 
 | 7486 |                     // that the track is stopped. | 
 | 7487 |                     if (mBytesRemaining == 0) { | 
 | 7488 |                         // Only start draining when all data in mixbuffer | 
 | 7489 |                         // has been written | 
 | 7490 |                         ALOGV("OffloadThread: underrun and STOPPING_1 -> draining, STOPPING_2"); | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7491 |                         track->setState(IAfTrackBase::STOPPING_2); | 
 | 7492 |                         // so presentation completes after | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 7493 |                         // drain do not drain if no data was ever sent to HAL (mStandby == true) | 
 | 7494 |                         if (last && !mStandby) { | 
 | 7495 |                             // do not modify drain sequence if we are already draining. This happens | 
 | 7496 |                             // when resuming from pause after drain. | 
 | 7497 |                             if ((mDrainSequence & 1) == 0) { | 
 | 7498 |                                 mSleepTimeUs = 0; | 
 | 7499 |                                 mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
 | 7500 |                                 mixerStatus = MIXER_DRAIN_TRACK; | 
 | 7501 |                                 mDrainSequence += 2; | 
 | 7502 |                             } | 
 | 7503 |                             if (mHwPaused) { | 
 | 7504 |                                 // It is possible to move from PAUSED to STOPPING_1 without | 
 | 7505 |                                 // a resume so we must ensure hardware is running | 
 | 7506 |                                 doHwResume = true; | 
 | 7507 |                                 mHwPaused = false; | 
 | 7508 |                             } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7509 |                         } | 
 | 7510 |                     } | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 7511 |                 } else if (last) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7512 |                     ALOGV("stopping1 underrun retries left %d", track->retryCount()); | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 7513 |                     mixerStatus = MIXER_TRACKS_ENABLED; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7514 |                 } | 
 | 7515 |             } else if (track->isStopping_2()) { | 
| Eric Laurent | 6a51d7e | 2013-10-17 18:59:26 -0700 | [diff] [blame] | 7516 |                 // Drain has completed or we are in standby, signal presentation complete | 
 | 7517 |                 if (!(mDrainSequence & 1) || !last || mStandby) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7518 |                     track->setState(IAfTrackBase::STOPPED); | 
| Atneya Nair | 0cae043 | 2022-05-10 18:12:12 -0400 | [diff] [blame] | 7519 |                     mOutput->presentationComplete(); | 
 | 7520 |                     track->presentationComplete(latency_l()); // always returns true | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7521 |                     track->reset(); | 
 | 7522 |                     tracksToRemove->add(track); | 
| Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 7523 |                     // OFFLOADED stop resets frame counts. | 
| Andy Hung | f323451 | 2018-07-03 14:51:47 -0700 | [diff] [blame] | 7524 |                     if (!mUseAsyncWrite) { | 
 | 7525 |                         // If we don't get explicit drain notification we must | 
 | 7526 |                         // register discontinuity regardless of whether this is | 
 | 7527 |                         // the previous (!last) or the upcoming (last) track | 
 | 7528 |                         // to avoid skipping the discontinuity. | 
| Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 7529 |                         mTimestampVerifier.discontinuity( | 
 | 7530 |                                 mTimestampVerifier.DISCONTINUITY_MODE_ZERO); | 
| Andy Hung | f323451 | 2018-07-03 14:51:47 -0700 | [diff] [blame] | 7531 |                     } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7532 |                 } | 
 | 7533 |             } else { | 
 | 7534 |                 // No buffers for this track. Give it a few chances to | 
 | 7535 |                 // fill a buffer, then remove it from active list. | 
| Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 7536 |                 bool isTimestampAdvancing = mIsTimestampAdvancing.check(mOutput); | 
| Gareth Fenn | b18c1a3 | 2022-10-05 13:42:36 -0700 | [diff] [blame] | 7537 |                 if (!isTunerStream()  // tuner streams remain active in underrun | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7538 |                         && --(track->retryCount()) <= 0) { | 
| Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 7539 |                     if (isTimestampAdvancing) { // HAL is still playing audio, give us more time. | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7540 |                         track->retryCount() = kMaxTrackRetriesOffload; | 
| Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 7541 |                     } else { | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 7542 |                         ALOGV("OffloadThread: BUFFER TIMEOUT: remove track(%d) from active list", | 
 | 7543 |                                 track->id()); | 
| Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 7544 |                         tracksToRemove->add(track); | 
| Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 7545 |                         // tell client process that the track was disabled because of underrun; | 
| Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 7546 |                         // it will then automatically call start() when data is available | 
 | 7547 |                         track->disable(); | 
 | 7548 |                     } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7549 |                 } else if (last){ | 
 | 7550 |                     mixerStatus = MIXER_TRACKS_ENABLED; | 
 | 7551 |                 } | 
 | 7552 |             } | 
 | 7553 |         } | 
 | 7554 |         // compute volume for this track | 
| Wenfeng Sun | 7945833 | 2019-05-29 20:12:43 +0800 | [diff] [blame] | 7555 |         if (track->isReady()) {  // check ready to prevent premature start. | 
 | 7556 |             processVolume_l(track, last); | 
 | 7557 |         } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7558 |     } | 
| Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 7559 |  | 
| Eric Laurent | ea0fade | 2013-10-04 16:23:48 -0700 | [diff] [blame] | 7560 |     // make sure the pause/flush/resume sequence is executed in the right order. | 
 | 7561 |     // If a flush is pending and a track is active but the HW is not paused, force a HW pause | 
 | 7562 |     // before flush and then resume HW. This can happen in case of pause/flush/resume | 
 | 7563 |     // if resume is received before pause is executed. | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 7564 |     if (!mStandby && (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7565 |         status_t result = mOutput->stream->pause(); | 
 | 7566 |         ALOGE_IF(result != OK, "Error when pausing output stream: %d", result); | 
| Gareth Fenn | cd1e162 | 2022-10-05 11:45:45 -0700 | [diff] [blame] | 7567 |         doHwResume = !doHwPause;  // resume if pause is due to flush. | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 7568 |     } | 
| Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 7569 |     if (mFlushPending) { | 
 | 7570 |         flushHw_l(); | 
| Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 7571 |     } | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 7572 |     if (!mStandby && doHwResume) { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7573 |         status_t result = mOutput->stream->resume(); | 
 | 7574 |         ALOGE_IF(result != OK, "Error when resuming output stream: %d", result); | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 7575 |     } | 
| Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 7576 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7577 |     // remove all the tracks that need to be... | 
 | 7578 |     removeTracks_l(*tracksToRemove); | 
 | 7579 |  | 
 | 7580 |     return mixerStatus; | 
 | 7581 | } | 
 | 7582 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7583 | // must be called with thread mutex locked | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7584 | bool OffloadThread::waitingAsyncCallback_l() | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7585 | { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7586 |     ALOGVV("waitingAsyncCallback_l mWriteAckSequence %d mDrainSequence %d", | 
 | 7587 |           mWriteAckSequence, mDrainSequence); | 
 | 7588 |     if (mUseAsyncWrite && ((mWriteAckSequence & 1) || (mDrainSequence & 1))) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7589 |         return true; | 
 | 7590 |     } | 
 | 7591 |     return false; | 
 | 7592 | } | 
 | 7593 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7594 | bool OffloadThread::waitingAsyncCallback() | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7595 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7596 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7597 |     return waitingAsyncCallback_l(); | 
 | 7598 | } | 
 | 7599 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7600 | void OffloadThread::flushHw_l() | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7601 | { | 
| Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 7602 |     DirectOutputThread::flushHw_l(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7603 |     // Flush anything still waiting in the mixbuffer | 
 | 7604 |     mCurrentWriteLength = 0; | 
 | 7605 |     mBytesRemaining = 0; | 
 | 7606 |     mPausedWriteLength = 0; | 
 | 7607 |     mPausedBytesRemaining = 0; | 
| Eric Laurent | 3eaf66b | 2016-04-01 14:44:17 -0700 | [diff] [blame] | 7608 |     // reset bytes written count to reflect that DSP buffers are empty after flush. | 
 | 7609 |     mBytesWritten = 0; | 
| Haynes Mathew George | 0f02f26 | 2014-01-11 13:03:57 -0800 | [diff] [blame] | 7610 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7611 |     if (mUseAsyncWrite) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7612 |         // discard any pending drain or write ack by incrementing sequence | 
 | 7613 |         mWriteAckSequence = (mWriteAckSequence + 2) & ~1; | 
 | 7614 |         mDrainSequence = (mDrainSequence + 2) & ~1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7615 |         ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7616 |         mCallbackThread->setWriteBlocked(mWriteAckSequence); | 
 | 7617 |         mCallbackThread->setDraining(mDrainSequence); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7618 |     } | 
 | 7619 | } | 
 | 7620 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7621 | void OffloadThread::invalidateTracks(audio_stream_type_t streamType) | 
| Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 7622 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7623 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 7624 |     if (PlaybackThread::invalidateTracks_l(streamType)) { | 
 | 7625 |         mFlushPending = true; | 
 | 7626 |     } | 
| Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 7627 | } | 
 | 7628 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7629 | void OffloadThread::invalidateTracks(std::set<audio_port_handle_t>& portIds) { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7630 |     audio_utils::lock_guard _l(mutex()); | 
| jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 7631 |     if (PlaybackThread::invalidateTracks_l(portIds)) { | 
 | 7632 |         mFlushPending = true; | 
 | 7633 |     } | 
 | 7634 | } | 
 | 7635 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7636 | // ---------------------------------------------------------------------------- | 
 | 7637 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7638 | /* static */ | 
 | 7639 | sp<IAfDuplicatingThread> IAfDuplicatingThread::create( | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7640 |         const sp<IAfThreadCallback>& afThreadCallback, | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7641 |         IAfPlaybackThread* mainThread, audio_io_handle_t id, bool systemReady) { | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7642 |     return sp<DuplicatingThread>::make(afThreadCallback, mainThread, id, systemReady); | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7643 | } | 
 | 7644 |  | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7645 | DuplicatingThread::DuplicatingThread(const sp<IAfThreadCallback>& afThreadCallback, | 
| Andy Hung | 0c1e11e | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 7646 |        IAfPlaybackThread* mainThread, audio_io_handle_t id, bool systemReady) | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7647 |     :   MixerThread(afThreadCallback, mainThread->getOutput(), id, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 7648 |                     systemReady, DUPLICATING), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7649 |         mWaitTimeMs(UINT_MAX) | 
 | 7650 | { | 
 | 7651 |     addOutputTrack(mainThread); | 
 | 7652 | } | 
 | 7653 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7654 | DuplicatingThread::~DuplicatingThread() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7655 | { | 
 | 7656 |     for (size_t i = 0; i < mOutputTracks.size(); i++) { | 
 | 7657 |         mOutputTracks[i]->destroy(); | 
 | 7658 |     } | 
 | 7659 | } | 
 | 7660 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7661 | void DuplicatingThread::threadLoop_mix() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7662 | { | 
 | 7663 |     // mix buffers... | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 7664 |     if (outputsReady()) { | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 7665 |         mAudioMixer->process(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7666 |     } else { | 
| Eric Laurent | 02b5708 | 2014-11-07 17:28:28 -0800 | [diff] [blame] | 7667 |         if (mMixerBufferValid) { | 
 | 7668 |             memset(mMixerBuffer, 0, mMixerBufferSize); | 
 | 7669 |         } else { | 
 | 7670 |             memset(mSinkBuffer, 0, mSinkBufferSize); | 
 | 7671 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7672 |     } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7673 |     mSleepTimeUs = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7674 |     writeFrames = mNormalFrameCount; | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 7675 |     mCurrentWriteLength = mSinkBufferSize; | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7676 |     mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7677 | } | 
 | 7678 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7679 | void DuplicatingThread::threadLoop_sleepTime() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7680 | { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7681 |     if (mSleepTimeUs == 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7682 |         if (mMixerStatus == MIXER_TRACKS_ENABLED) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7683 |             mSleepTimeUs = mActiveSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7684 |         } else { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7685 |             mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7686 |         } | 
 | 7687 |     } else if (mBytesWritten != 0) { | 
 | 7688 |         if (mMixerStatus == MIXER_TRACKS_ENABLED) { | 
 | 7689 |             writeFrames = mNormalFrameCount; | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 7690 |             memset(mSinkBuffer, 0, mSinkBufferSize); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7691 |         } else { | 
 | 7692 |             // flush remaining overflow buffers in output tracks | 
 | 7693 |             writeFrames = 0; | 
 | 7694 |         } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7695 |         mSleepTimeUs = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7696 |     } | 
 | 7697 | } | 
 | 7698 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7699 | ssize_t DuplicatingThread::threadLoop_write() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7700 | { | 
 | 7701 |     for (size_t i = 0; i < outputTracks.size(); i++) { | 
| Andy Hung | 1c86ebe | 2018-05-29 20:29:08 -0700 | [diff] [blame] | 7702 |         const ssize_t actualWritten = outputTracks[i]->write(mSinkBuffer, writeFrames); | 
 | 7703 |  | 
 | 7704 |         // Consider the first OutputTrack for timestamp and frame counting. | 
 | 7705 |  | 
 | 7706 |         // The threadLoop() generally assumes writing a full sink buffer size at a time. | 
 | 7707 |         // Here, we correct for writeFrames of 0 (a stop) or underruns because | 
 | 7708 |         // we always claim success. | 
 | 7709 |         if (i == 0) { | 
 | 7710 |             const ssize_t correction = mSinkBufferSize / mFrameSize - actualWritten; | 
 | 7711 |             ALOGD_IF(correction != 0 && writeFrames != 0, | 
 | 7712 |                     "%s: writeFrames:%u  actualWritten:%zd  correction:%zd  mFramesWritten:%lld", | 
 | 7713 |                     __func__, writeFrames, actualWritten, correction, (long long)mFramesWritten); | 
 | 7714 |             mFramesWritten -= correction; | 
 | 7715 |         } | 
 | 7716 |  | 
 | 7717 |         // TODO: Report correction for the other output tracks and show in the dump. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7718 |     } | 
| Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 7719 |     if (mStandby) { | 
 | 7720 |         mThreadMetrics.logBeginInterval(); | 
| Andy Hung | 44d648b | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 7721 |         mThreadSnapshot.onBegin(); | 
| Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 7722 |         mStandby = false; | 
 | 7723 |     } | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 7724 |     return (ssize_t)mSinkBufferSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7725 | } | 
 | 7726 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7727 | void DuplicatingThread::threadLoop_standby() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7728 | { | 
 | 7729 |     // DuplicatingThread implements standby by stopping all tracks | 
 | 7730 |     for (size_t i = 0; i < outputTracks.size(); i++) { | 
 | 7731 |         outputTracks[i]->stop(); | 
 | 7732 |     } | 
 | 7733 | } | 
 | 7734 |  | 
| Andy Hung | 8a5abfd | 2023-12-07 19:35:12 -0800 | [diff] [blame] | 7735 | void DuplicatingThread::threadLoop_exit() | 
 | 7736 | { | 
 | 7737 |     // Prevent calling the OutputTrack dtor in the DuplicatingThread dtor | 
 | 7738 |     // where other mutexes (i.e. AudioPolicyService_Mutex) may be held. | 
 | 7739 |     // Do so here in the threadLoop_exit(). | 
 | 7740 |  | 
 | 7741 |     SortedVector <sp<IAfOutputTrack>> localTracks; | 
 | 7742 |     { | 
 | 7743 |         audio_utils::lock_guard l(mutex()); | 
 | 7744 |         localTracks = std::move(mOutputTracks); | 
 | 7745 |         mOutputTracks.clear(); | 
 | 7746 |     } | 
 | 7747 |     localTracks.clear(); | 
 | 7748 |     outputTracks.clear(); | 
 | 7749 |     PlaybackThread::threadLoop_exit(); | 
 | 7750 | } | 
 | 7751 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7752 | void DuplicatingThread::dumpInternals_l(int fd, const Vector<String16>& args) | 
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 7753 | { | 
| Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 7754 |     MixerThread::dumpInternals_l(fd, args); | 
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 7755 |  | 
 | 7756 |     std::stringstream ss; | 
 | 7757 |     const size_t numTracks = mOutputTracks.size(); | 
 | 7758 |     ss << "  " << numTracks << " OutputTracks"; | 
 | 7759 |     if (numTracks > 0) { | 
 | 7760 |         ss << ":"; | 
 | 7761 |         for (const auto &track : mOutputTracks) { | 
| Andy Hung | 0c1e11e | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 7762 |             const auto thread = track->thread().promote(); | 
| Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 7763 |             ss << " (" << track->id() << " : "; | 
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 7764 |             if (thread.get() != nullptr) { | 
 | 7765 |                 ss << thread.get() << ", " << thread->id(); | 
 | 7766 |             } else { | 
 | 7767 |                 ss << "null"; | 
 | 7768 |             } | 
 | 7769 |             ss << ")"; | 
 | 7770 |         } | 
 | 7771 |     } | 
 | 7772 |     ss << "\n"; | 
 | 7773 |     std::string result = ss.str(); | 
 | 7774 |     write(fd, result.c_str(), result.size()); | 
 | 7775 | } | 
 | 7776 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7777 | void DuplicatingThread::saveOutputTracks() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7778 | { | 
 | 7779 |     outputTracks = mOutputTracks; | 
 | 7780 | } | 
 | 7781 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7782 | void DuplicatingThread::clearOutputTracks() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7783 | { | 
 | 7784 |     outputTracks.clear(); | 
 | 7785 | } | 
 | 7786 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7787 | void DuplicatingThread::addOutputTrack(IAfPlaybackThread* thread) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7788 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7789 |     audio_utils::lock_guard _l(mutex()); | 
| Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 7790 |     // The downstream MixerThread consumes thread->frameCount() amount of frames per mix pass. | 
 | 7791 |     // Adjust for thread->sampleRate() to determine minimum buffer frame count. | 
 | 7792 |     // Then triple buffer because Threads do not run synchronously and may not be clock locked. | 
 | 7793 |     const size_t frameCount = | 
 | 7794 |             3 * sourceFramesNeeded(mSampleRate, thread->frameCount(), thread->sampleRate()); | 
 | 7795 |     // TODO: Consider asynchronous sample rate conversion to handle clock disparity | 
 | 7796 |     // from different OutputTracks and their associated MixerThreads (e.g. one may | 
 | 7797 |     // nearly empty and the other may be dropping data). | 
 | 7798 |  | 
| Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 7799 |     // TODO b/182392769: use attribution source util, move to server edge | 
 | 7800 |     AttributionSourceState attributionSource = AttributionSourceState(); | 
 | 7801 |     attributionSource.uid = VALUE_OR_FATAL(legacy2aidl_uid_t_int32_t( | 
| Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 7802 |         IPCThreadState::self()->getCallingUid())); | 
| Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 7803 |     attributionSource.pid = VALUE_OR_FATAL(legacy2aidl_pid_t_int32_t( | 
| Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 7804 |       IPCThreadState::self()->getCallingPid())); | 
| Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 7805 |     attributionSource.token = sp<BBinder>::make(); | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7806 |     sp<IAfOutputTrack> outputTrack = IAfOutputTrack::create(thread, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7807 |                                             this, | 
 | 7808 |                                             mSampleRate, | 
| Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 7809 |                                             mFormat, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7810 |                                             mChannelMask, | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 7811 |                                             frameCount, | 
| Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 7812 |                                             attributionSource); | 
| Eric Laurent | af3ec7c | 2016-08-01 11:25:19 -0700 | [diff] [blame] | 7813 |     status_t status = outputTrack != 0 ? outputTrack->initCheck() : (status_t) NO_MEMORY; | 
 | 7814 |     if (status != NO_ERROR) { | 
 | 7815 |         ALOGE("addOutputTrack() initCheck failed %d", status); | 
 | 7816 |         return; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7817 |     } | 
| Eric Laurent | af3ec7c | 2016-08-01 11:25:19 -0700 | [diff] [blame] | 7818 |     thread->setStreamVolume(AUDIO_STREAM_PATCH, 1.0f); | 
 | 7819 |     mOutputTracks.add(outputTrack); | 
 | 7820 |     ALOGV("addOutputTrack() track %p, on thread %p", outputTrack.get(), thread); | 
 | 7821 |     updateWaitTime_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7822 | } | 
 | 7823 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7824 | void DuplicatingThread::removeOutputTrack(IAfPlaybackThread* thread) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7825 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7826 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7827 |     for (size_t i = 0; i < mOutputTracks.size(); i++) { | 
 | 7828 |         if (mOutputTracks[i]->thread() == thread) { | 
 | 7829 |             mOutputTracks[i]->destroy(); | 
 | 7830 |             mOutputTracks.removeAt(i); | 
 | 7831 |             updateWaitTime_l(); | 
| Andy Hung | 160664b | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 7832 |             // NO_THREAD_SAFETY_ANALYSIS | 
 | 7833 |             // Lambda workaround: as thread != this | 
 | 7834 |             // we can safely call the remote thread getOutput. | 
 | 7835 |             const bool equalOutput = | 
 | 7836 |                     [&](){ return thread->getOutput() == mOutput; }(); | 
 | 7837 |             if (equalOutput) { | 
 | 7838 |                 mOutput = nullptr; | 
| Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 7839 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7840 |             return; | 
 | 7841 |         } | 
 | 7842 |     } | 
| Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 7843 |     ALOGV("removeOutputTrack(): unknown thread: %p", thread); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7844 | } | 
 | 7845 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 7846 | // caller must hold mutex() | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7847 | void DuplicatingThread::updateWaitTime_l() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7848 | { | 
 | 7849 |     mWaitTimeMs = UINT_MAX; | 
 | 7850 |     for (size_t i = 0; i < mOutputTracks.size(); i++) { | 
| Andy Hung | 0c1e11e | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 7851 |         const auto strong = mOutputTracks[i]->thread().promote(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7852 |         if (strong != 0) { | 
 | 7853 |             uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); | 
 | 7854 |             if (waitTimeMs < mWaitTimeMs) { | 
 | 7855 |                 mWaitTimeMs = waitTimeMs; | 
 | 7856 |             } | 
 | 7857 |         } | 
 | 7858 |     } | 
 | 7859 | } | 
 | 7860 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7861 | bool DuplicatingThread::outputsReady() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7862 | { | 
 | 7863 |     for (size_t i = 0; i < outputTracks.size(); i++) { | 
| Andy Hung | 0c1e11e | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 7864 |         const auto thread = outputTracks[i]->thread().promote(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7865 |         if (thread == 0) { | 
 | 7866 |             ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", | 
 | 7867 |                     outputTracks[i].get()); | 
 | 7868 |             return false; | 
 | 7869 |         } | 
| Andy Hung | 0c1e11e | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 7870 |         IAfPlaybackThread* const playbackThread = thread->asIAfPlaybackThread().get(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7871 |         // see note at standby() declaration | 
| Andy Hung | 3e4c874 | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 7872 |         if (playbackThread->inStandby() && !playbackThread->isSuspended()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7873 |             ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), | 
 | 7874 |                     thread.get()); | 
 | 7875 |             return false; | 
 | 7876 |         } | 
 | 7877 |     } | 
 | 7878 |     return true; | 
 | 7879 | } | 
 | 7880 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7881 | void DuplicatingThread::sendMetadataToBackend_l( | 
| Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 7882 |         const StreamOutHalInterface::SourceMetadata& metadata) | 
| Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 7883 | { | 
| Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 7884 |     for (auto& outputTrack : outputTracks) { // not mOutputTracks | 
 | 7885 |         outputTrack->setMetadatas(metadata.tracks); | 
 | 7886 |     } | 
| Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 7887 | } | 
 | 7888 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7889 | uint32_t DuplicatingThread::activeSleepTimeUs() const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7890 | { | 
| Andy Hung | 7a6a0f0 | 2023-11-29 13:42:08 -0800 | [diff] [blame] | 7891 |     // return half the wait time in microseconds. | 
 | 7892 |     return std::min(mWaitTimeMs * 500ULL, (unsigned long long)UINT32_MAX);  // prevent overflow. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7893 | } | 
 | 7894 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7895 | void DuplicatingThread::cacheParameters_l() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7896 | { | 
 | 7897 |     // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first | 
 | 7898 |     updateWaitTime_l(); | 
 | 7899 |  | 
 | 7900 |     MixerThread::cacheParameters_l(); | 
 | 7901 | } | 
 | 7902 |  | 
| Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 7903 | // ---------------------------------------------------------------------------- | 
 | 7904 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7905 | /* static */ | 
 | 7906 | sp<IAfPlaybackThread> IAfPlaybackThread::createSpatializerThread( | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7907 |         const sp<IAfThreadCallback>& afThreadCallback, | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7908 |         AudioStreamOut* output, | 
 | 7909 |         audio_io_handle_t id, | 
 | 7910 |         bool systemReady, | 
 | 7911 |         audio_config_base_t* mixerConfig) { | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7912 |     return sp<SpatializerThread>::make(afThreadCallback, output, id, systemReady, mixerConfig); | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7913 | } | 
 | 7914 |  | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7915 | SpatializerThread::SpatializerThread(const sp<IAfThreadCallback>& afThreadCallback, | 
| Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 7916 |                                                              AudioStreamOut* output, | 
 | 7917 |                                                              audio_io_handle_t id, | 
 | 7918 |                                                              bool systemReady, | 
 | 7919 |                                                              audio_config_base_t *mixerConfig) | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7920 |     : MixerThread(afThreadCallback, output, id, systemReady, SPATIALIZER, mixerConfig) | 
| Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 7921 | { | 
 | 7922 | } | 
 | 7923 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7924 | void SpatializerThread::setHalLatencyMode_l() { | 
| Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 7925 |     // if mSupportedLatencyModes is empty, the HAL stream does not support | 
 | 7926 |     // latency mode control and we can exit. | 
 | 7927 |     if (mSupportedLatencyModes.empty()) { | 
 | 7928 |         return; | 
 | 7929 |     } | 
 | 7930 |     audio_latency_mode_t latencyMode = AUDIO_LATENCY_MODE_FREE; | 
 | 7931 |     if (mSupportedLatencyModes.size() == 1) { | 
 | 7932 |         // If the HAL only support one latency mode currently, confirm the choice | 
 | 7933 |         latencyMode = mSupportedLatencyModes[0]; | 
 | 7934 |     } else if (mSupportedLatencyModes.size() > 1) { | 
 | 7935 |         // Request low latency if: | 
 | 7936 |         // - The low latency mode is requested by the spatializer controller | 
 | 7937 |         //   (mRequestedLatencyMode = AUDIO_LATENCY_MODE_LOW) | 
 | 7938 |         //      AND | 
 | 7939 |         // - At least one active track is spatialized | 
| Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 7940 |         for (const auto& track : mActiveTracks) { | 
 | 7941 |             if (track->isSpatialized()) { | 
| Eric Laurent | b024157 | 2024-02-01 21:03:49 +0100 | [diff] [blame] | 7942 |                 latencyMode = mRequestedLatencyMode; | 
| Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 7943 |                 break; | 
 | 7944 |             } | 
 | 7945 |         } | 
| Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 7946 |     } | 
 | 7947 |  | 
 | 7948 |     if (latencyMode != mSetLatencyMode) { | 
 | 7949 |         status_t status = mOutput->stream->setLatencyMode(latencyMode); | 
| Andy Hung | 4bd53e7 | 2022-11-17 17:21:45 -0800 | [diff] [blame] | 7950 |         ALOGD("%s: thread(%d) setLatencyMode(%s) returned %d", | 
 | 7951 |                 __func__, mId, toString(latencyMode).c_str(), status); | 
| Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 7952 |         if (status == NO_ERROR) { | 
 | 7953 |             mSetLatencyMode = latencyMode; | 
 | 7954 |         } | 
 | 7955 |     } | 
 | 7956 | } | 
 | 7957 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7958 | status_t SpatializerThread::setRequestedLatencyMode(audio_latency_mode_t mode) { | 
| Eric Laurent | b024157 | 2024-02-01 21:03:49 +0100 | [diff] [blame] | 7959 |     if (mode < 0 || mode >= AUDIO_LATENCY_MODE_CNT) { | 
| Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 7960 |         return BAD_VALUE; | 
 | 7961 |     } | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7962 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 7963 |     mRequestedLatencyMode = mode; | 
 | 7964 |     return NO_ERROR; | 
 | 7965 | } | 
 | 7966 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7967 | void SpatializerThread::checkOutputStageEffects() | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7968 | NO_THREAD_SAFETY_ANALYSIS | 
 | 7969 | //  'createEffect_l' requires holding mutex 'AudioFlinger_Mutex' exclusively | 
| Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 7970 | { | 
 | 7971 |     bool hasVirtualizer = false; | 
 | 7972 |     bool hasDownMixer = false; | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 7973 |     sp<IAfEffectHandle> finalDownMixer; | 
| Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 7974 |     { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7975 |         audio_utils::lock_guard _l(mutex()); | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 7976 |         sp<IAfEffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_STAGE); | 
| Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 7977 |         if (chain != 0) { | 
| Eric Laurent | 1c5e2e3 | 2021-08-18 18:50:28 +0200 | [diff] [blame] | 7978 |             hasVirtualizer = chain->getEffectFromType_l(FX_IID_SPATIALIZER) != nullptr; | 
| Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 7979 |             hasDownMixer = chain->getEffectFromType_l(EFFECT_UIID_DOWNMIX) != nullptr; | 
 | 7980 |         } | 
 | 7981 |  | 
 | 7982 |         finalDownMixer = mFinalDownMixer; | 
 | 7983 |         mFinalDownMixer.clear(); | 
 | 7984 |     } | 
 | 7985 |  | 
 | 7986 |     if (hasVirtualizer) { | 
 | 7987 |         if (finalDownMixer != nullptr) { | 
 | 7988 |             int32_t ret; | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 7989 |             finalDownMixer->asIEffect()->disable(&ret); | 
| Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 7990 |         } | 
 | 7991 |         finalDownMixer.clear(); | 
 | 7992 |     } else if (!hasDownMixer) { | 
 | 7993 |         std::vector<effect_descriptor_t> descriptors; | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7994 |         status_t status = mAfThreadCallback->getEffectsFactoryHal()->getDescriptors( | 
| Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 7995 |                                                         EFFECT_UIID_DOWNMIX, &descriptors); | 
 | 7996 |         if (status != NO_ERROR) { | 
 | 7997 |             return; | 
 | 7998 |         } | 
 | 7999 |         ALOG_ASSERT(!descriptors.empty(), | 
 | 8000 |                 "%s getDescriptors() returned no error but empty list", __func__); | 
 | 8001 |  | 
 | 8002 |         finalDownMixer = createEffect_l(nullptr /*client*/, nullptr /*effectClient*/, | 
 | 8003 |                 0 /*priority*/, AUDIO_SESSION_OUTPUT_STAGE, &descriptors[0], nullptr /*enabled*/, | 
| Eric Laurent | de8caf4 | 2021-08-11 17:19:25 +0200 | [diff] [blame] | 8004 |                 &status, false /*pinned*/, false /*probe*/, false /*notifyFramesProcessed*/); | 
| Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 8005 |  | 
 | 8006 |         if (finalDownMixer == nullptr || (status != NO_ERROR && status != ALREADY_EXISTS)) { | 
 | 8007 |             ALOGW("%s error creating downmixer %d", __func__, status); | 
 | 8008 |             finalDownMixer.clear(); | 
 | 8009 |         } else { | 
 | 8010 |             int32_t ret; | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 8011 |             finalDownMixer->asIEffect()->enable(&ret); | 
| Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 8012 |         } | 
 | 8013 |     } | 
 | 8014 |  | 
 | 8015 |     { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 8016 |         audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 8017 |         mFinalDownMixer = finalDownMixer; | 
 | 8018 |     } | 
 | 8019 | } | 
 | 8020 |  | 
| Andy Hung | e251446 | 2023-12-06 14:59:24 -0800 | [diff] [blame] | 8021 | void SpatializerThread::threadLoop_exit() | 
 | 8022 | { | 
 | 8023 |     // The Spatializer EffectHandle must be released on the PlaybackThread | 
 | 8024 |     // threadLoop() to prevent lock inversion in the SpatializerThread dtor. | 
 | 8025 |     mFinalDownMixer.clear(); | 
 | 8026 |  | 
 | 8027 |     PlaybackThread::threadLoop_exit(); | 
 | 8028 | } | 
 | 8029 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8030 | // ---------------------------------------------------------------------------- | 
 | 8031 | //      Record | 
 | 8032 | // ---------------------------------------------------------------------------- | 
 | 8033 |  | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 8034 | sp<IAfRecordThread> IAfRecordThread::create(const sp<IAfThreadCallback>& afThreadCallback, | 
| Andy Hung | 0c1e11e | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 8035 |         AudioStreamIn* input, | 
 | 8036 |         audio_io_handle_t id, | 
 | 8037 |         bool systemReady) { | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 8038 |     return sp<RecordThread>::make(afThreadCallback, input, id, systemReady); | 
| Andy Hung | 0c1e11e | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 8039 | } | 
 | 8040 |  | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 8041 | RecordThread::RecordThread(const sp<IAfThreadCallback>& afThreadCallback, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8042 |                                          AudioStreamIn *input, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8043 |                                          audio_io_handle_t id, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 8044 |                                          bool systemReady | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 8045 |                                          ) : | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 8046 |     ThreadBase(afThreadCallback, id, RECORD, systemReady, false /* isOut */), | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 8047 |     mInput(input), | 
| Mikhail Naganov | 2534b38 | 2019-09-25 13:05:02 -0700 | [diff] [blame] | 8048 |     mSource(mInput), | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 8049 |     mActiveTracks(&this->mLocalLog), | 
 | 8050 |     mRsmpInBuffer(NULL), | 
| Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 8051 |     // mRsmpInFrames, mRsmpInFramesP2, and mRsmpInFramesOA are set by readInputParameters_l() | 
| Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 8052 |     mRsmpInRear(0) | 
| Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 8053 |     , mReadOnlyHeap(new MemoryDealer(kRecordThreadReadOnlyHeapSize, | 
 | 8054 |             "RecordThreadRO", MemoryHeapBase::READ_ONLY)) | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8055 |     // mFastCapture below | 
 | 8056 |     , mFastCaptureFutex(0) | 
 | 8057 |     // mInputSource | 
 | 8058 |     // mPipeSink | 
 | 8059 |     // mPipeSource | 
 | 8060 |     , mPipeFramesP2(0) | 
 | 8061 |     // mPipeMemory | 
 | 8062 |     // mFastCaptureNBLogWriter | 
| Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 8063 |     , mFastTrackAvail(false) | 
| Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 8064 |     , mBtNrecSuspended(false) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8065 | { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 8066 |     snprintf(mThreadName, kThreadNameLength, "AudioIn_%X", id); | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 8067 |     mNBLogWriter = afThreadCallback->newWriter_l(kLogSize, mThreadName); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8068 |  | 
| George Burgess IV | a8f90c1 | 2020-05-14 11:27:19 -0700 | [diff] [blame] | 8069 |     if (mInput->audioHwDev != nullptr) { | 
| Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 8070 |         mIsMsdDevice = strcmp( | 
 | 8071 |                 mInput->audioHwDev->moduleName(), AUDIO_HARDWARE_MODULE_ID_MSD) == 0; | 
 | 8072 |     } | 
 | 8073 |  | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 8074 |     readInputParameters_l(); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8075 |  | 
| Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 8076 |     // TODO: We may also match on address as well as device type for | 
 | 8077 |     // AUDIO_DEVICE_IN_BUS, AUDIO_DEVICE_IN_BLUETOOTH_A2DP, AUDIO_DEVICE_IN_REMOTE_SUBMIX | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 8078 |     // TODO: This property should be ensure that only contains one single device type. | 
 | 8079 |     mTimestampCorrectedDevice = (audio_devices_t)property_get_int64( | 
 | 8080 |             "audio.timestamp.corrected_input_device", | 
| Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 8081 |             (int64_t)(mIsMsdDevice ? AUDIO_DEVICE_IN_BUS // turn on by default for MSD | 
 | 8082 |                                    : AUDIO_DEVICE_NONE)); | 
 | 8083 |  | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8084 |     // create an NBAIO source for the HAL input stream, and negotiate | 
| Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 8085 |     mInputSource = new AudioStreamInSource(input->stream); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8086 |     size_t numCounterOffers = 0; | 
 | 8087 |     const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 8088 | #if !LOG_NDEBUG | 
| Jing Mike | 537412f | 2023-03-12 11:01:47 +0800 | [diff] [blame] | 8089 |     [[maybe_unused]] ssize_t index = | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 8090 | #else | 
 | 8091 |     (void) | 
 | 8092 | #endif | 
 | 8093 |             mInputSource->negotiate(offers, 1, NULL, numCounterOffers); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8094 |     ALOG_ASSERT(index == 0); | 
 | 8095 |  | 
 | 8096 |     // initialize fast capture depending on configuration | 
 | 8097 |     bool initFastCapture; | 
 | 8098 |     switch (kUseFastCapture) { | 
 | 8099 |     case FastCapture_Never: | 
 | 8100 |         initFastCapture = false; | 
| Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 8101 |         ALOGV("%p kUseFastCapture = Never, initFastCapture = false", this); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8102 |         break; | 
 | 8103 |     case FastCapture_Always: | 
 | 8104 |         initFastCapture = true; | 
| Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 8105 |         ALOGV("%p kUseFastCapture = Always, initFastCapture = true", this); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8106 |         break; | 
 | 8107 |     case FastCapture_Static: | 
| Sampath | 6fac233 | 2022-12-16 17:34:37 +1100 | [diff] [blame] | 8108 |         initFastCapture = !mIsMsdDevice // Disable fast capture for MSD BUS devices. | 
| Dean Wheatley | 8e5d9e4 | 2023-11-03 12:37:27 +1100 | [diff] [blame] | 8109 |                 && audio_is_linear_pcm(mFormat) | 
| Sampath | 6fac233 | 2022-12-16 17:34:37 +1100 | [diff] [blame] | 8110 |                 && (mFrameCount * 1000) / mSampleRate < kMinNormalCaptureBufferSizeMs; | 
| Dean Wheatley | 8e5d9e4 | 2023-11-03 12:37:27 +1100 | [diff] [blame] | 8111 |         ALOGV("%p kUseFastCapture = Static, format = 0x%x, (%lld * 1000) / %u vs %u, " | 
 | 8112 |                 "initFastCapture = %d, mIsMsdDevice = %d", this, mFormat, (long long)mFrameCount, | 
 | 8113 |                 mSampleRate, kMinNormalCaptureBufferSizeMs, initFastCapture, mIsMsdDevice); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8114 |         break; | 
 | 8115 |     // case FastCapture_Dynamic: | 
 | 8116 |     } | 
 | 8117 |  | 
 | 8118 |     if (initFastCapture) { | 
| Glenn Kasten | d198b85 | 2015-03-16 14:55:53 -0700 | [diff] [blame] | 8119 |         // 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] | 8120 |         NBAIO_Format format = mInputSource->format(); | 
| Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 8121 |         // quadruple-buffering of 20 ms each; this ensures we can sleep for 20ms in RecordThread | 
 | 8122 |         size_t pipeFramesP2 = roundup(4 * FMS_20 * mSampleRate / 1000); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8123 |         size_t pipeSize = pipeFramesP2 * Format_frameSize(format); | 
| Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 8124 |         void *pipeBuffer = nullptr; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8125 |         const sp<MemoryDealer> roHeap(readOnlyHeap()); | 
 | 8126 |         sp<IMemory> pipeMemory; | 
 | 8127 |         if ((roHeap == 0) || | 
 | 8128 |                 (pipeMemory = roHeap->allocate(pipeSize)) == 0 || | 
| Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 8129 |                 (pipeBuffer = pipeMemory->unsecurePointer()) == nullptr) { | 
| Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 8130 |             ALOGE("not enough memory for pipe buffer size=%zu; " | 
 | 8131 |                     "roHeap=%p, pipeMemory=%p, pipeBuffer=%p; roHeapSize: %lld", | 
 | 8132 |                     pipeSize, roHeap.get(), pipeMemory.get(), pipeBuffer, | 
 | 8133 |                     (long long)kRecordThreadReadOnlyHeapSize); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8134 |             goto failed; | 
 | 8135 |         } | 
 | 8136 |         // pipe will be shared directly with fast clients, so clear to avoid leaking old information | 
 | 8137 |         memset(pipeBuffer, 0, pipeSize); | 
 | 8138 |         Pipe *pipe = new Pipe(pipeFramesP2, format, pipeBuffer); | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 8139 |         const NBAIO_Format offersFast[1] = {format}; | 
 | 8140 |         size_t numCounterOffersFast = 0; | 
| Eric Laurent | 1e28aaa | 2023-04-16 19:34:23 +0200 | [diff] [blame] | 8141 |         [[maybe_unused]] ssize_t index2 = pipe->negotiate(offersFast, std::size(offersFast), | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 8142 |                 nullptr /* counterOffers */, numCounterOffersFast); | 
| Eric Laurent | 1e28aaa | 2023-04-16 19:34:23 +0200 | [diff] [blame] | 8143 |         ALOG_ASSERT(index2 == 0); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8144 |         mPipeSink = pipe; | 
 | 8145 |         PipeReader *pipeReader = new PipeReader(*pipe); | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 8146 |         numCounterOffersFast = 0; | 
| Eric Laurent | 1e28aaa | 2023-04-16 19:34:23 +0200 | [diff] [blame] | 8147 |         index2 = pipeReader->negotiate(offersFast, std::size(offersFast), | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 8148 |                 nullptr /* counterOffers */, numCounterOffersFast); | 
| Eric Laurent | 1e28aaa | 2023-04-16 19:34:23 +0200 | [diff] [blame] | 8149 |         ALOG_ASSERT(index2 == 0); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8150 |         mPipeSource = pipeReader; | 
 | 8151 |         mPipeFramesP2 = pipeFramesP2; | 
 | 8152 |         mPipeMemory = pipeMemory; | 
 | 8153 |  | 
 | 8154 |         // create fast capture | 
 | 8155 |         mFastCapture = new FastCapture(); | 
 | 8156 |         FastCaptureStateQueue *sq = mFastCapture->sq(); | 
 | 8157 | #ifdef STATE_QUEUE_DUMP | 
 | 8158 |         // FIXME | 
 | 8159 | #endif | 
 | 8160 |         FastCaptureState *state = sq->begin(); | 
 | 8161 |         state->mCblk = NULL; | 
 | 8162 |         state->mInputSource = mInputSource.get(); | 
 | 8163 |         state->mInputSourceGen++; | 
 | 8164 |         state->mPipeSink = pipe; | 
 | 8165 |         state->mPipeSinkGen++; | 
 | 8166 |         state->mFrameCount = mFrameCount; | 
 | 8167 |         state->mCommand = FastCaptureState::COLD_IDLE; | 
 | 8168 |         // already done in constructor initialization list | 
 | 8169 |         //mFastCaptureFutex = 0; | 
 | 8170 |         state->mColdFutexAddr = &mFastCaptureFutex; | 
 | 8171 |         state->mColdGen++; | 
 | 8172 |         state->mDumpState = &mFastCaptureDumpState; | 
 | 8173 | #ifdef TEE_SINK | 
 | 8174 |         // FIXME | 
 | 8175 | #endif | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 8176 |         mFastCaptureNBLogWriter = | 
 | 8177 |                 afThreadCallback->newWriter_l(kFastCaptureLogSize, "FastCapture"); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8178 |         state->mNBLogWriter = mFastCaptureNBLogWriter.get(); | 
 | 8179 |         sq->end(); | 
 | 8180 |         sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); | 
 | 8181 |  | 
 | 8182 |         // start the fast capture | 
 | 8183 |         mFastCapture->run("FastCapture", ANDROID_PRIORITY_URGENT_AUDIO); | 
 | 8184 |         pid_t tid = mFastCapture->getTid(); | 
| Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 8185 |         sendPrioConfigEvent(getpid(), tid, kPriorityFastCapture, false /*forApp*/); | 
| Mikhail Naganov | e1c4b5d | 2016-12-22 09:22:45 -0800 | [diff] [blame] | 8186 |         stream()->setHalThreadPriority(kPriorityFastCapture); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8187 | #ifdef AUDIO_WATCHDOG | 
 | 8188 |         // FIXME | 
 | 8189 | #endif | 
 | 8190 |  | 
| Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 8191 |         mFastTrackAvail = true; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8192 |     } | 
| Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 8193 | #ifdef TEE_SINK | 
 | 8194 |     mTee.set(mInputSource->format(), NBAIO_Tee::TEE_FLAG_INPUT_THREAD); | 
 | 8195 |     mTee.setId(std::string("_") + std::to_string(mId) + "_C"); | 
 | 8196 | #endif | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8197 | failed: ; | 
 | 8198 |  | 
 | 8199 |     // FIXME mNormalSource | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8200 | } | 
 | 8201 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 8202 | RecordThread::~RecordThread() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8203 | { | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8204 |     if (mFastCapture != 0) { | 
 | 8205 |         FastCaptureStateQueue *sq = mFastCapture->sq(); | 
 | 8206 |         FastCaptureState *state = sq->begin(); | 
 | 8207 |         if (state->mCommand == FastCaptureState::COLD_IDLE) { | 
 | 8208 |             int32_t old = android_atomic_inc(&mFastCaptureFutex); | 
 | 8209 |             if (old == -1) { | 
 | 8210 |                 (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); | 
 | 8211 |             } | 
 | 8212 |         } | 
 | 8213 |         state->mCommand = FastCaptureState::EXIT; | 
 | 8214 |         sq->end(); | 
 | 8215 |         sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); | 
 | 8216 |         mFastCapture->join(); | 
 | 8217 |         mFastCapture.clear(); | 
 | 8218 |     } | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 8219 |     mAfThreadCallback->unregisterWriter(mFastCaptureNBLogWriter); | 
 | 8220 |     mAfThreadCallback->unregisterWriter(mNBLogWriter); | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 8221 |     free(mRsmpInBuffer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8222 | } | 
 | 8223 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 8224 | void RecordThread::onFirstRef() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8225 | { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 8226 |     run(mThreadName, PRIORITY_URGENT_AUDIO); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8227 | } | 
 | 8228 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 8229 | void RecordThread::preExit() | 
| Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 8230 | { | 
 | 8231 |     ALOGV("  preExit()"); | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 8232 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 8233 |     for (size_t i = 0; i < mTracks.size(); i++) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8234 |         sp<IAfRecordTrack> track = mTracks[i]; | 
| Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 8235 |         track->invalidate(); | 
 | 8236 |     } | 
 | 8237 |     mActiveTracks.clear(); | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 8238 |     mStartStopCV.notify_all(); | 
| Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 8239 | } | 
 | 8240 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 8241 | bool RecordThread::threadLoop() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8242 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8243 |     nsecs_t lastWarning = 0; | 
 | 8244 |  | 
 | 8245 |     inputStandBy(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8246 |  | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 8247 | reacquire_wakelock: | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 8248 |     { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 8249 |         audio_utils::lock_guard _l(mutex()); | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 8250 |         acquireWakeLock_l(); | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 8251 |     } | 
 | 8252 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8253 |     // used to request a deferred sleep, to be executed later while mutex is unlocked | 
 | 8254 |     uint32_t sleepUs = 0; | 
 | 8255 |  | 
| Andy Hung | 1381a07 | 2023-10-20 16:41:18 -0700 | [diff] [blame] | 8256 |     // timestamp correction enable is determined under lock, used in processing step. | 
 | 8257 |     bool timestampCorrectionEnabled = false; | 
 | 8258 |  | 
| Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 8259 |     int64_t lastLoopCountRead = -2;  // never matches "previous" loop, when loopCount = 0. | 
 | 8260 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8261 |     // loop while there is work to do | 
| Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 8262 |     for (int64_t loopCount = 0;; ++loopCount) {  // loopCount used for statistics tracking | 
| Andy Hung | fdb84b9 | 2024-03-15 10:15:10 -0700 | [diff] [blame] | 8263 |         // Note: these sp<> are released at the end of the for loop outside of the mutex() lock. | 
 | 8264 |         sp<IAfRecordTrack> activeTrack; | 
| Andy Hung | b2b01c6 | 2024-04-23 13:56:19 -0700 | [diff] [blame] | 8265 |         std::vector<sp<IAfRecordTrack>> oldActiveTracks; | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 8266 |         Vector<sp<IAfEffectChain>> effectChains; | 
| Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 8267 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8268 |         // activeTracks accumulates a copy of a subset of mActiveTracks | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8269 |         Vector<sp<IAfRecordTrack>> activeTracks; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8270 |  | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 8271 |         // reference to the (first and only) active fast track | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8272 |         sp<IAfRecordTrack> fastTrack; | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 8273 |  | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 8274 |         // reference to a fast track which is about to be removed | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8275 |         sp<IAfRecordTrack> fastTrackToRemove; | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 8276 |  | 
| Eric Laurent | 33403f0 | 2020-05-29 18:35:06 -0700 | [diff] [blame] | 8277 |         bool silenceFastCapture = false; | 
 | 8278 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 8279 |         { // scope for mutex() | 
 | 8280 |             audio_utils::unique_lock _l(mutex()); | 
| Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 8281 |  | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 8282 |             processConfigEvents_l(); | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 8283 |  | 
| Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 8284 |             // check exitPending here because checkForNewParameters_l() and | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 8285 |             // checkForNewParameters_l() can temporarily release mutex() | 
| Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 8286 |             if (exitPending()) { | 
 | 8287 |                 break; | 
 | 8288 |             } | 
 | 8289 |  | 
| Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 8290 |             // sleep with mutex unlocked | 
 | 8291 |             if (sleepUs > 0) { | 
| Glenn Kasten | f9715e4 | 2016-07-13 14:02:03 -0700 | [diff] [blame] | 8292 |                 ATRACE_BEGIN("sleepC"); | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 8293 |                 (void)mWaitWorkCV.wait_for(_l, std::chrono::microseconds(sleepUs)); | 
| Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 8294 |                 ATRACE_END(); | 
 | 8295 |                 sleepUs = 0; | 
 | 8296 |                 continue; | 
 | 8297 |             } | 
 | 8298 |  | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 8299 |             // if no active track(s), then standby and release wakelock | 
 | 8300 |             size_t size = mActiveTracks.size(); | 
 | 8301 |             if (size == 0) { | 
| Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 8302 |                 standbyIfNotAlreadyInStandby(); | 
| Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 8303 |                 // exitPending() can't become true here | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8304 |                 releaseWakeLock_l(); | 
 | 8305 |                 ALOGV("RecordThread: loop stopping"); | 
 | 8306 |                 // go to sleep | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 8307 |                 mWaitWorkCV.wait(_l); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8308 |                 ALOGV("RecordThread: loop starting"); | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 8309 |                 goto reacquire_wakelock; | 
 | 8310 |             } | 
 | 8311 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8312 |             bool doBroadcast = false; | 
| Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 8313 |             bool allStopped = true; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8314 |             for (size_t i = 0; i < size; ) { | 
| Andy Hung | b2b01c6 | 2024-04-23 13:56:19 -0700 | [diff] [blame] | 8315 |                 if (activeTrack) {  // ensure track release is outside lock. | 
 | 8316 |                     oldActiveTracks.emplace_back(std::move(activeTrack)); | 
 | 8317 |                 } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8318 |                 activeTrack = mActiveTracks[i]; | 
 | 8319 |                 if (activeTrack->isTerminated()) { | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 8320 |                     if (activeTrack->isFastTrack()) { | 
 | 8321 |                         ALOG_ASSERT(fastTrackToRemove == 0); | 
 | 8322 |                         fastTrackToRemove = activeTrack; | 
 | 8323 |                     } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8324 |                     removeTrack_l(activeTrack); | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 8325 |                     mActiveTracks.remove(activeTrack); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8326 |                     size--; | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 8327 |                     continue; | 
 | 8328 |                 } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8329 |  | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8330 |                 IAfTrackBase::track_state activeTrackState = activeTrack->state(); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8331 |                 switch (activeTrackState) { | 
 | 8332 |  | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8333 |                 case IAfTrackBase::PAUSING: | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8334 |                     mActiveTracks.remove(activeTrack); | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8335 |                     activeTrack->setState(IAfTrackBase::PAUSED); | 
| François Gaffie | 39634e4 | 2023-10-17 12:13:32 +0200 | [diff] [blame] | 8336 |                     if (activeTrack->isFastTrack()) { | 
 | 8337 |                         ALOGV("%s fast track is paused, thus removed from active list", __func__); | 
 | 8338 |                         // Keep a ref on fast track to wait for FastCapture thread to get updated | 
 | 8339 |                         // state before potential track removal | 
 | 8340 |                         fastTrackToRemove = activeTrack; | 
 | 8341 |                     } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8342 |                     doBroadcast = true; | 
 | 8343 |                     size--; | 
 | 8344 |                     continue; | 
 | 8345 |  | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8346 |                 case IAfTrackBase::STARTING_1: | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8347 |                     sleepUs = 10000; | 
 | 8348 |                     i++; | 
| Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 8349 |                     allStopped = false; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8350 |                     continue; | 
 | 8351 |  | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8352 |                 case IAfTrackBase::STARTING_2: | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8353 |                     doBroadcast = true; | 
| Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 8354 |                     if (mStandby) { | 
 | 8355 |                         mThreadMetrics.logBeginInterval(); | 
| Andy Hung | 44d648b | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 8356 |                         mThreadSnapshot.onBegin(); | 
| Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 8357 |                         mStandby = false; | 
 | 8358 |                     } | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8359 |                     activeTrack->setState(IAfTrackBase::ACTIVE); | 
| Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 8360 |                     allStopped = false; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8361 |                     break; | 
 | 8362 |  | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8363 |                 case IAfTrackBase::ACTIVE: | 
| Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 8364 |                     allStopped = false; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8365 |                     break; | 
 | 8366 |  | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8367 |                 case IAfTrackBase::IDLE:    // cannot be on ActiveTracks if idle | 
 | 8368 |                 case IAfTrackBase::PAUSED:  // cannot be on ActiveTracks if paused | 
 | 8369 |                 case IAfTrackBase::STOPPED: // cannot be on ActiveTracks if destroyed/terminated | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8370 |                 default: | 
| Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 8371 |                     LOG_ALWAYS_FATAL("%s: Unexpected active track state:%d, id:%d, tracks:%zu", | 
 | 8372 |                             __func__, activeTrackState, activeTrack->id(), size); | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 8373 |                 } | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 8374 |  | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8375 |                 if (activeTrack->isFastTrack()) { | 
 | 8376 |                     ALOG_ASSERT(!mFastTrackAvail); | 
 | 8377 |                     ALOG_ASSERT(fastTrack == 0); | 
| Eric Laurent | 33403f0 | 2020-05-29 18:35:06 -0700 | [diff] [blame] | 8378 |                     // if the active fast track is silenced either: | 
 | 8379 |                     // 1) silence the whole capture from fast capture buffer if this is | 
 | 8380 |                     //    the only active track | 
 | 8381 |                     // 2) invalidate this track: this will cause the client to reconnect and possibly | 
 | 8382 |                     //    be invalidated again until unsilenced | 
| Eric Laurent | 5f0fd7b | 2021-05-07 16:33:26 +0200 | [diff] [blame] | 8383 |                     bool invalidate = false; | 
| Eric Laurent | 33403f0 | 2020-05-29 18:35:06 -0700 | [diff] [blame] | 8384 |                     if (activeTrack->isSilenced()) { | 
 | 8385 |                         if (size > 1) { | 
| Eric Laurent | 5f0fd7b | 2021-05-07 16:33:26 +0200 | [diff] [blame] | 8386 |                             invalidate = true; | 
| Eric Laurent | 33403f0 | 2020-05-29 18:35:06 -0700 | [diff] [blame] | 8387 |                         } else { | 
 | 8388 |                             silenceFastCapture = true; | 
 | 8389 |                         } | 
 | 8390 |                     } | 
| Eric Laurent | 5f0fd7b | 2021-05-07 16:33:26 +0200 | [diff] [blame] | 8391 |                     // Invalidate fast tracks if access to audio history is required as this is not | 
 | 8392 |                     // possible with fast tracks. Once the fast track has been invalidated, no new | 
 | 8393 |                     // fast track will be created until mMaxSharedAudioHistoryMs is cleared. | 
 | 8394 |                     if (mMaxSharedAudioHistoryMs != 0) { | 
 | 8395 |                         invalidate = true; | 
 | 8396 |                     } | 
 | 8397 |                     if (invalidate) { | 
 | 8398 |                         activeTrack->invalidate(); | 
 | 8399 |                         ALOG_ASSERT(fastTrackToRemove == 0); | 
 | 8400 |                         fastTrackToRemove = activeTrack; | 
 | 8401 |                         removeTrack_l(activeTrack); | 
 | 8402 |                         mActiveTracks.remove(activeTrack); | 
 | 8403 |                         size--; | 
 | 8404 |                         continue; | 
 | 8405 |                     } | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8406 |                     fastTrack = activeTrack; | 
 | 8407 |                 } | 
| Eric Laurent | 33403f0 | 2020-05-29 18:35:06 -0700 | [diff] [blame] | 8408 |  | 
 | 8409 |                 activeTracks.add(activeTrack); | 
 | 8410 |                 i++; | 
 | 8411 |  | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 8412 |             } | 
| Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 8413 |  | 
| Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 8414 |             mActiveTracks.updatePowerState_l(this); | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 8415 |  | 
| Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 8416 |             updateMetadata_l(); | 
 | 8417 |  | 
| Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 8418 |             if (allStopped) { | 
 | 8419 |                 standbyIfNotAlreadyInStandby(); | 
 | 8420 |             } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8421 |             if (doBroadcast) { | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 8422 |                 mStartStopCV.notify_all(); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8423 |             } | 
 | 8424 |  | 
 | 8425 |             // sleep if there are no active tracks to process | 
| Eric Tan | 39ec8d6 | 2018-07-24 09:49:29 -0700 | [diff] [blame] | 8426 |             if (activeTracks.isEmpty()) { | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8427 |                 if (sleepUs == 0) { | 
 | 8428 |                     sleepUs = kRecordThreadSleepUs; | 
 | 8429 |                 } | 
 | 8430 |                 continue; | 
 | 8431 |             } | 
 | 8432 |             sleepUs = 0; | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 8433 |  | 
| Andy Hung | 1381a07 | 2023-10-20 16:41:18 -0700 | [diff] [blame] | 8434 |             timestampCorrectionEnabled = isTimestampCorrectionEnabled_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8435 |             lockEffectChains_l(effectChains); | 
 | 8436 |         } | 
 | 8437 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8438 |         // thread mutex is now unlocked, mActiveTracks unknown, activeTracks.size() > 0 | 
| Glenn Kasten | 7165268 | 2013-08-14 15:17:55 -0700 | [diff] [blame] | 8439 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8440 |         size_t size = effectChains.size(); | 
 | 8441 |         for (size_t i = 0; i < size; i++) { | 
| Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 8442 |             // thread mutex is not locked, but effect chain is locked | 
 | 8443 |             effectChains[i]->process_l(); | 
 | 8444 |         } | 
 | 8445 |  | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 8446 |         // 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] | 8447 |         if (mFastCapture != 0) { | 
 | 8448 |             FastCaptureStateQueue *sq = mFastCapture->sq(); | 
 | 8449 |             FastCaptureState *state = sq->begin(); | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 8450 |             bool didModify = false; | 
 | 8451 |             FastCaptureStateQueue::block_t block = FastCaptureStateQueue::BLOCK_UNTIL_PUSHED; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8452 |             if (state->mCommand != FastCaptureState::READ_WRITE /* FIXME && | 
 | 8453 |                     (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)*/) { | 
 | 8454 |                 if (state->mCommand == FastCaptureState::COLD_IDLE) { | 
 | 8455 |                     int32_t old = android_atomic_inc(&mFastCaptureFutex); | 
 | 8456 |                     if (old == -1) { | 
 | 8457 |                         (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); | 
 | 8458 |                     } | 
 | 8459 |                 } | 
 | 8460 |                 state->mCommand = FastCaptureState::READ_WRITE; | 
 | 8461 | #if 0   // FIXME | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 8462 |                 mFastCaptureDumpState.increaseSamplingN(mAfThreadCallback->isLowRamDevice() ? | 
| Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 8463 |                         FastThreadDumpState::kSamplingNforLowRamDevice : | 
 | 8464 |                         FastThreadDumpState::kSamplingN); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8465 | #endif | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 8466 |                 didModify = true; | 
 | 8467 |             } | 
 | 8468 |             audio_track_cblk_t *cblkOld = state->mCblk; | 
 | 8469 |             audio_track_cblk_t *cblkNew = fastTrack != 0 ? fastTrack->cblk() : NULL; | 
 | 8470 |             if (cblkNew != cblkOld) { | 
 | 8471 |                 state->mCblk = cblkNew; | 
 | 8472 |                 // block until acked if removing a fast track | 
 | 8473 |                 if (cblkOld != NULL) { | 
 | 8474 |                     block = FastCaptureStateQueue::BLOCK_UNTIL_ACKED; | 
 | 8475 |                 } | 
 | 8476 |                 didModify = true; | 
 | 8477 |             } | 
| jiabin | 01c8f56 | 2018-07-19 17:47:28 -0700 | [diff] [blame] | 8478 |             AudioBufferProvider* abp = (fastTrack != 0 && fastTrack->isPatchTrack()) ? | 
 | 8479 |                     reinterpret_cast<AudioBufferProvider*>(fastTrack.get()) : nullptr; | 
 | 8480 |             if (state->mFastPatchRecordBufferProvider != abp) { | 
 | 8481 |                 state->mFastPatchRecordBufferProvider = abp; | 
 | 8482 |                 state->mFastPatchRecordFormat = fastTrack == 0 ? | 
 | 8483 |                         AUDIO_FORMAT_INVALID : fastTrack->format(); | 
 | 8484 |                 didModify = true; | 
 | 8485 |             } | 
| Eric Laurent | 33403f0 | 2020-05-29 18:35:06 -0700 | [diff] [blame] | 8486 |             if (state->mSilenceCapture != silenceFastCapture) { | 
 | 8487 |                 state->mSilenceCapture = silenceFastCapture; | 
 | 8488 |                 didModify = true; | 
 | 8489 |             } | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 8490 |             sq->end(didModify); | 
 | 8491 |             if (didModify) { | 
 | 8492 |                 sq->push(block); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8493 | #if 0 | 
 | 8494 |                 if (kUseFastCapture == FastCapture_Dynamic) { | 
 | 8495 |                     mNormalSource = mPipeSource; | 
 | 8496 |                 } | 
 | 8497 | #endif | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8498 |             } | 
 | 8499 |         } | 
 | 8500 |  | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 8501 |         // now run the fast track destructor with thread mutex unlocked | 
 | 8502 |         fastTrackToRemove.clear(); | 
 | 8503 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8504 |         // Read from HAL to keep up with fastest client if multiple active tracks, not slowest one. | 
 | 8505 |         // Only the client(s) that are too slow will overrun. But if even the fastest client is too | 
 | 8506 |         // slow, then this RecordThread will overrun by not calling HAL read often enough. | 
 | 8507 |         // If destination is non-contiguous, first read past the nominal end of buffer, then | 
 | 8508 |         // copy to the right place.  Permitted because mRsmpInBuffer was over-allocated. | 
| Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 8509 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8510 |         int32_t rear = mRsmpInRear & (mRsmpInFramesP2 - 1); | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 8511 |         ssize_t framesRead = 0; // not needed, remove clang-tidy warning. | 
| Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 8512 |         const int64_t lastIoBeginNs = systemTime(); // start IO timing | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8513 |  | 
 | 8514 |         // If an NBAIO source is present, use it to read the normal capture's data | 
 | 8515 |         if (mPipeSource != 0) { | 
| Andy Hung | 156317a | 2018-08-02 11:49:29 -0700 | [diff] [blame] | 8516 |             size_t framesToRead = min(mRsmpInFramesOA - rear, mRsmpInFramesP2 / 2); | 
| Andy Hung | d5b638f | 2018-04-30 13:56:10 -0700 | [diff] [blame] | 8517 |  | 
 | 8518 |             // The audio fifo read() returns OVERRUN on overflow, and advances the read pointer | 
 | 8519 |             // to the full buffer point (clearing the overflow condition).  Upon OVERRUN error, | 
 | 8520 |             // we immediately retry the read() to get data and prevent another overflow. | 
 | 8521 |             for (int retries = 0; retries <= 2; ++retries) { | 
 | 8522 |                 ALOGW_IF(retries > 0, "overrun on read from pipe, retry #%d", retries); | 
 | 8523 |                 framesRead = mPipeSource->read((uint8_t*)mRsmpInBuffer + rear * mFrameSize, | 
 | 8524 |                         framesToRead); | 
 | 8525 |                 if (framesRead != OVERRUN) break; | 
 | 8526 |             } | 
 | 8527 |  | 
| Andy Hung | 7a3dc6b | 2018-05-01 16:39:51 -0700 | [diff] [blame] | 8528 |             const ssize_t availableToRead = mPipeSource->availableToRead(); | 
 | 8529 |             if (availableToRead >= 0) { | 
| Robert Wu | 06db0a3 | 2021-08-10 19:05:34 +0000 | [diff] [blame] | 8530 |                 mMonopipePipeDepthStats.add(availableToRead); | 
| Glenn Kasten | a2f59b3 | 2020-08-03 16:37:24 -0700 | [diff] [blame] | 8531 |                 // PipeSource is the primary clock.  It is up to the AudioRecord client to keep up. | 
| Andy Hung | 7a3dc6b | 2018-05-01 16:39:51 -0700 | [diff] [blame] | 8532 |                 LOG_ALWAYS_FATAL_IF((size_t)availableToRead > mPipeFramesP2, | 
 | 8533 |                         "more frames to read than fifo size, %zd > %zu", | 
 | 8534 |                         availableToRead, mPipeFramesP2); | 
 | 8535 |                 const size_t pipeFramesFree = mPipeFramesP2 - availableToRead; | 
 | 8536 |                 const size_t sleepFrames = min(pipeFramesFree, mRsmpInFramesP2) / 2; | 
 | 8537 |                 ALOGVV("mPipeFramesP2:%zu mRsmpInFramesP2:%zu sleepFrames:%zu availableToRead:%zd", | 
 | 8538 |                         mPipeFramesP2, mRsmpInFramesP2, sleepFrames, availableToRead); | 
| Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 8539 |                 sleepUs = (sleepFrames * 1000000LL) / mSampleRate; | 
 | 8540 |             } | 
 | 8541 |             if (framesRead < 0) { | 
 | 8542 |                 status_t status = (status_t) framesRead; | 
 | 8543 |                 switch (status) { | 
 | 8544 |                 case OVERRUN: | 
 | 8545 |                     ALOGW("overrun on read from pipe"); | 
 | 8546 |                     framesRead = 0; | 
 | 8547 |                     break; | 
 | 8548 |                 case NEGOTIATE: | 
 | 8549 |                     ALOGE("re-negotiation is needed"); | 
 | 8550 |                     framesRead = -1;  // Will cause an attempt to recover. | 
 | 8551 |                     break; | 
 | 8552 |                 default: | 
 | 8553 |                     ALOGE("unknown error %d on read from pipe", status); | 
 | 8554 |                     break; | 
 | 8555 |                 } | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8556 |             } | 
 | 8557 |         // otherwise use the HAL / AudioStreamIn directly | 
 | 8558 |         } else { | 
| Glenn Kasten | ec6a703 | 2016-03-14 07:40:23 -0700 | [diff] [blame] | 8559 |             ATRACE_BEGIN("read"); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 8560 |             size_t bytesRead; | 
| Mikhail Naganov | 2534b38 | 2019-09-25 13:05:02 -0700 | [diff] [blame] | 8561 |             status_t result = mSource->read( | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 8562 |                     (uint8_t*)mRsmpInBuffer + rear * mFrameSize, mBufferSize, &bytesRead); | 
| Glenn Kasten | ec6a703 | 2016-03-14 07:40:23 -0700 | [diff] [blame] | 8563 |             ATRACE_END(); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 8564 |             if (result < 0) { | 
 | 8565 |                 framesRead = result; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8566 |             } else { | 
 | 8567 |                 framesRead = bytesRead / mFrameSize; | 
 | 8568 |             } | 
 | 8569 |         } | 
 | 8570 |  | 
| Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 8571 |         const int64_t lastIoEndNs = systemTime(); // end IO timing | 
 | 8572 |  | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 8573 |         // Update server timestamp with server stats | 
 | 8574 |         // systemTime() is optional if the hardware supports timestamps. | 
| Andy Hung | 743f640 | 2020-06-03 13:17:04 -0700 | [diff] [blame] | 8575 |         if (framesRead >= 0) { | 
 | 8576 |             mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] += framesRead; | 
 | 8577 |             mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = lastIoEndNs; | 
 | 8578 |         } | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 8579 |  | 
 | 8580 |         // Update server timestamp with kernel stats | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 8581 |         if (mPipeSource.get() == nullptr /* don't obtain for FastCapture, could block */) { | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 8582 |             int64_t position, time; | 
| Andy Hung | 2e2c0bb | 2018-06-11 19:13:11 -0700 | [diff] [blame] | 8583 |             if (mStandby) { | 
| Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 8584 |                 mTimestampVerifier.discontinuity(audio_is_linear_pcm(mFormat) ? | 
 | 8585 |                     mTimestampVerifier.DISCONTINUITY_MODE_CONTINUOUS : | 
 | 8586 |                     mTimestampVerifier.DISCONTINUITY_MODE_ZERO); | 
| Mikhail Naganov | 2534b38 | 2019-09-25 13:05:02 -0700 | [diff] [blame] | 8587 |             } else if (mSource->getCapturePosition(&position, &time) == NO_ERROR | 
| Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 8588 |                     && time > mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]) { | 
 | 8589 |  | 
 | 8590 |                 mTimestampVerifier.add(position, time, mSampleRate); | 
| Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 8591 |                 if (timestampCorrectionEnabled) { | 
| Dean Wheatley | 56a583e | 2020-05-08 15:12:17 +1000 | [diff] [blame] | 8592 |                     ALOGVV("TS_BEFORE: %d %lld %lld", | 
| Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 8593 |                             id(), (long long)time, (long long)position); | 
 | 8594 |                     auto correctedTimestamp = mTimestampVerifier.getLastCorrectedTimestamp(); | 
 | 8595 |                     position = correctedTimestamp.mFrames; | 
 | 8596 |                     time = correctedTimestamp.mTimeNs; | 
| Dean Wheatley | 56a583e | 2020-05-08 15:12:17 +1000 | [diff] [blame] | 8597 |                     ALOGVV("TS_AFTER: %d %lld %lld", | 
| Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 8598 |                             id(), (long long)time, (long long)position); | 
 | 8599 |                 } | 
 | 8600 |  | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 8601 |                 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = position; | 
 | 8602 |                 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = time; | 
 | 8603 |                 // Note: In general record buffers should tend to be empty in | 
 | 8604 |                 // a properly running pipeline. | 
 | 8605 |                 // | 
 | 8606 |                 // Also, it is not advantageous to call get_presentation_position during the read | 
 | 8607 |                 // as the read obtains a lock, preventing the timestamp call from executing. | 
| Andy Hung | 2e2c0bb | 2018-06-11 19:13:11 -0700 | [diff] [blame] | 8608 |             } else { | 
 | 8609 |                 mTimestampVerifier.error(); | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 8610 |             } | 
 | 8611 |         } | 
| Andy Hung | e6c3711 | 2019-02-26 17:38:10 -0800 | [diff] [blame] | 8612 |  | 
 | 8613 |         // From the timestamp, input read latency is negative output write latency. | 
 | 8614 |         const audio_input_flags_t flags = mInput != NULL ? mInput->flags : AUDIO_INPUT_FLAG_NONE; | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8615 |         const double latencyMs = IAfRecordTrack::checkServerLatencySupported(mFormat, flags) | 
| Andy Hung | e6c3711 | 2019-02-26 17:38:10 -0800 | [diff] [blame] | 8616 |                 ? - mTimestamp.getOutputServerLatencyMs(mSampleRate) : 0.; | 
 | 8617 |         if (latencyMs != 0.) { // note 0. means timestamp is empty. | 
 | 8618 |             mLatencyMs.add(latencyMs); | 
 | 8619 |         } | 
 | 8620 |  | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 8621 |         // Use this to track timestamp information | 
 | 8622 |         // ALOGD("%s", mTimestamp.toString().c_str()); | 
 | 8623 |  | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8624 |         if (framesRead < 0 || (framesRead == 0 && mPipeSource == 0)) { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 8625 |             ALOGE("read failed: framesRead=%zd", framesRead); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8626 |             // Force input into standby so that it tries to recover at next read attempt | 
 | 8627 |             inputStandBy(); | 
 | 8628 |             sleepUs = kRecordThreadSleepUs; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8629 |         } | 
 | 8630 |         if (framesRead <= 0) { | 
| Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 8631 |             goto unlock; | 
| Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 8632 |         } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8633 |         ALOG_ASSERT(framesRead > 0); | 
| Andy Hung | 6427e44 | 2018-08-09 12:51:02 -0700 | [diff] [blame] | 8634 |         mFramesRead += framesRead; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8635 |  | 
| Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 8636 | #ifdef TEE_SINK | 
 | 8637 |         (void)mTee.write((uint8_t*)mRsmpInBuffer + rear * mFrameSize, framesRead); | 
 | 8638 | #endif | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8639 |         // 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] | 8640 |         { | 
 | 8641 |             size_t part1 = mRsmpInFramesP2 - rear; | 
 | 8642 |             if ((size_t) framesRead > part1) { | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 8643 |                 memcpy(mRsmpInBuffer, (uint8_t*)mRsmpInBuffer + mRsmpInFramesP2 * mFrameSize, | 
| Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 8644 |                         (framesRead - part1) * mFrameSize); | 
 | 8645 |             } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8646 |         } | 
| Dean Wheatley | fd56e81 | 2020-11-06 22:32:21 +1100 | [diff] [blame] | 8647 |         mRsmpInRear = audio_utils::safe_add_overflow(mRsmpInRear, (int32_t)framesRead); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8648 |  | 
 | 8649 |         size = activeTracks.size(); | 
| Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 8650 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8651 |         // loop over each active track | 
 | 8652 |         for (size_t i = 0; i < size; i++) { | 
| Andy Hung | 460e10f | 2024-06-17 15:42:48 -0700 | [diff] [blame] | 8653 |             if (activeTrack) {  // ensure track release is outside lock. | 
 | 8654 |                 oldActiveTracks.emplace_back(std::move(activeTrack)); | 
 | 8655 |             } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8656 |             activeTrack = activeTracks[i]; | 
 | 8657 |  | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8658 |             // skip fast tracks, as those are handled directly by FastCapture | 
 | 8659 |             if (activeTrack->isFastTrack()) { | 
 | 8660 |                 continue; | 
 | 8661 |             } | 
 | 8662 |  | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 8663 |             // TODO: This code probably should be moved to RecordTrack. | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 8664 |             // TODO: Update the activeTrack buffer converter in case of reconfigure. | 
 | 8665 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8666 |             enum { | 
 | 8667 |                 OVERRUN_UNKNOWN, | 
 | 8668 |                 OVERRUN_TRUE, | 
 | 8669 |                 OVERRUN_FALSE | 
 | 8670 |             } overrun = OVERRUN_UNKNOWN; | 
 | 8671 |  | 
 | 8672 |             // loop over getNextBuffer to handle circular sink | 
 | 8673 |             for (;;) { | 
 | 8674 |  | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8675 |                 activeTrack->sinkBuffer().frameCount = ~0; | 
 | 8676 |                 status_t status = activeTrack->getNextBuffer(&activeTrack->sinkBuffer()); | 
 | 8677 |                 size_t framesOut = activeTrack->sinkBuffer().frameCount; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8678 |                 LOG_ALWAYS_FATAL_IF((status == OK) != (framesOut > 0)); | 
 | 8679 |  | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 8680 |                 // check available frames and handle overrun conditions | 
 | 8681 |                 // if the record track isn't draining fast enough. | 
 | 8682 |                 bool hasOverrun; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8683 |                 size_t framesIn; | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8684 |                 activeTrack->resamplerBufferProvider()->sync(&framesIn, &hasOverrun); | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 8685 |                 if (hasOverrun) { | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8686 |                     overrun = OVERRUN_TRUE; | 
 | 8687 |                 } | 
| Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 8688 |                 if (framesOut == 0 || framesIn == 0) { | 
 | 8689 |                     break; | 
 | 8690 |                 } | 
 | 8691 |  | 
| Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 8692 |                 // Don't allow framesOut to be larger than what is possible with resampling | 
 | 8693 |                 // from framesIn. | 
 | 8694 |                 // This isn't strictly necessary but helps limit buffer resizing in | 
 | 8695 |                 // RecordBufferConverter.  TODO: remove when no longer needed. | 
| Dean Wheatley | dea650c | 2023-11-01 22:49:01 +1100 | [diff] [blame] | 8696 |                 if (audio_is_linear_pcm(activeTrack->format())) { | 
 | 8697 |                     framesOut = min(framesOut, | 
 | 8698 |                             destinationFramesPossible( | 
 | 8699 |                                     framesIn, mSampleRate, activeTrack->sampleRate())); | 
 | 8700 |                 } | 
| Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 8701 |  | 
 | 8702 |                 if (activeTrack->isDirect()) { | 
| Daniel Van Veen | 9e2376e | 2018-09-27 14:37:58 +1000 | [diff] [blame] | 8703 |                     // No RecordBufferConverter used for direct streams. Pass | 
| Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 8704 |                     // straight from RecordThread buffer to RecordTrack buffer. | 
 | 8705 |                     AudioBufferProvider::Buffer buffer; | 
 | 8706 |                     buffer.frameCount = framesOut; | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 8707 |                     const status_t getNextBufferStatus = | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8708 |                             activeTrack->resamplerBufferProvider()->getNextBuffer(&buffer); | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 8709 |                     if (getNextBufferStatus == OK && buffer.frameCount != 0) { | 
| Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 8710 |                         ALOGV_IF(buffer.frameCount != framesOut, | 
 | 8711 |                                 "%s() read less than expected (%zu vs %zu)", | 
 | 8712 |                                 __func__, buffer.frameCount, framesOut); | 
 | 8713 |                         framesOut = buffer.frameCount; | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8714 |                         memcpy(activeTrack->sinkBuffer().raw, | 
 | 8715 |                                 buffer.raw, buffer.frameCount * mFrameSize); | 
 | 8716 |                         activeTrack->resamplerBufferProvider()->releaseBuffer(&buffer); | 
| Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 8717 |                     } else { | 
 | 8718 |                         framesOut = 0; | 
 | 8719 |                         ALOGE("%s() cannot fill request, status: %d, frameCount: %zu", | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 8720 |                             __func__, getNextBufferStatus, buffer.frameCount); | 
| Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 8721 |                     } | 
 | 8722 |                 } else { | 
 | 8723 |                     // process frames from the RecordThread buffer provider to the RecordTrack | 
 | 8724 |                     // buffer | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8725 |                     framesOut = activeTrack->recordBufferConverter()->convert( | 
 | 8726 |                             activeTrack->sinkBuffer().raw, | 
 | 8727 |                             activeTrack->resamplerBufferProvider(), | 
| Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 8728 |                             framesOut); | 
 | 8729 |                 } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8730 |  | 
 | 8731 |                 if (framesOut > 0 && (overrun == OVERRUN_UNKNOWN)) { | 
 | 8732 |                     overrun = OVERRUN_FALSE; | 
 | 8733 |                 } | 
 | 8734 |  | 
| Andy Hung | 93bb573 | 2023-05-04 21:16:34 -0700 | [diff] [blame] | 8735 |                 // MediaSyncEvent handling: Synchronize AudioRecord to AudioTrack completion. | 
 | 8736 |                 const ssize_t framesToDrop = | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8737 |                         activeTrack->synchronizedRecordState().updateRecordFrames(framesOut); | 
| Andy Hung | 93bb573 | 2023-05-04 21:16:34 -0700 | [diff] [blame] | 8738 |                 if (framesToDrop == 0) { | 
 | 8739 |                     // no sync event, process normally, otherwise ignore. | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8740 |                     if (framesOut > 0) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8741 |                         activeTrack->sinkBuffer().frameCount = framesOut; | 
| Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 8742 |                         // Sanitize before releasing if the track has no access to the source data | 
 | 8743 |                         // An idle UID receives silence from non virtual devices until active | 
 | 8744 |                         if (activeTrack->isSilenced()) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8745 |                             memset(activeTrack->sinkBuffer().raw, | 
 | 8746 |                                     0, framesOut * activeTrack->frameSize()); | 
| Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 8747 |                         } | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8748 |                         activeTrack->releaseBuffer(&activeTrack->sinkBuffer()); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8749 |                     } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8750 |                 } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8751 |                 if (framesOut == 0) { | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8752 |                     break; | 
| Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 8753 |                 } | 
 | 8754 |             } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8755 |  | 
 | 8756 |             switch (overrun) { | 
 | 8757 |             case OVERRUN_TRUE: | 
 | 8758 |                 // client isn't retrieving buffers fast enough | 
 | 8759 |                 if (!activeTrack->setOverflow()) { | 
 | 8760 |                     nsecs_t now = systemTime(); | 
 | 8761 |                     // FIXME should lastWarning per track? | 
 | 8762 |                     if ((now - lastWarning) > kWarningThrottleNs) { | 
 | 8763 |                         ALOGW("RecordThread: buffer overflow"); | 
 | 8764 |                         lastWarning = now; | 
 | 8765 |                     } | 
 | 8766 |                 } | 
 | 8767 |                 break; | 
 | 8768 |             case OVERRUN_FALSE: | 
 | 8769 |                 activeTrack->clearOverflow(); | 
 | 8770 |                 break; | 
 | 8771 |             case OVERRUN_UNKNOWN: | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8772 |                 break; | 
 | 8773 |             } | 
 | 8774 |  | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 8775 |             // update frame information and push timestamp out | 
 | 8776 |             activeTrack->updateTrackFrameInfo( | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8777 |                     activeTrack->serverProxy()->framesReleased(), | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 8778 |                     mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER], | 
 | 8779 |                     mSampleRate, mTimestamp); | 
| Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 8780 |         } | 
 | 8781 |  | 
| Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 8782 | unlock: | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8783 |         // enable changes in effect chain | 
 | 8784 |         unlockEffectChains(effectChains); | 
| Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 8785 |         // effectChains doesn't need to be cleared, since it is cleared by destructor at scope end | 
| Eric Laurent | cccbc76 | 2019-04-05 14:20:05 -0700 | [diff] [blame] | 8786 |         if (audio_has_proportional_frames(mFormat) | 
 | 8787 |             && loopCount == lastLoopCountRead + 1) { | 
 | 8788 |             const int64_t readPeriodNs = lastIoEndNs - mLastIoEndNs; | 
 | 8789 |             const double jitterMs = | 
 | 8790 |                 TimestampVerifier<int64_t, int64_t>::computeJitterMs( | 
 | 8791 |                     {framesRead, readPeriodNs}, | 
 | 8792 |                     {0, 0} /* lastTimestamp */, mSampleRate); | 
 | 8793 |             const double processMs = (lastIoBeginNs - mLastIoEndNs) * 1e-6; | 
 | 8794 |  | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 8795 |             audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | cccbc76 | 2019-04-05 14:20:05 -0700 | [diff] [blame] | 8796 |             mIoJitterMs.add(jitterMs); | 
 | 8797 |             mProcessTimeMs.add(processMs); | 
 | 8798 |         } | 
| Andy Hung | 56ce2ed | 2024-06-12 16:03:16 -0700 | [diff] [blame] | 8799 |        mThreadloopExecutor.process(); | 
| Eric Laurent | cccbc76 | 2019-04-05 14:20:05 -0700 | [diff] [blame] | 8800 |         // update timing info. | 
 | 8801 |         mLastIoBeginNs = lastIoBeginNs; | 
 | 8802 |         mLastIoEndNs = lastIoEndNs; | 
 | 8803 |         lastLoopCountRead = loopCount; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8804 |     } | 
| Andy Hung | 56ce2ed | 2024-06-12 16:03:16 -0700 | [diff] [blame] | 8805 |     mThreadloopExecutor.process(); // process any remaining deferred actions. | 
 | 8806 |     // deferred actions after this point are ignored. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8807 |  | 
| Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 8808 |     standbyIfNotAlreadyInStandby(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8809 |  | 
 | 8810 |     { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 8811 |         audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 8812 |         for (size_t i = 0; i < mTracks.size(); i++) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8813 |             sp<IAfRecordTrack> track = mTracks[i]; | 
| Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 8814 |             track->invalidate(); | 
 | 8815 |         } | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 8816 |         mActiveTracks.clear(); | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 8817 |         mStartStopCV.notify_all(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8818 |     } | 
 | 8819 |  | 
 | 8820 |     releaseWakeLock(); | 
 | 8821 |  | 
 | 8822 |     ALOGV("RecordThread %p exiting", this); | 
 | 8823 |     return false; | 
 | 8824 | } | 
 | 8825 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 8826 | void RecordThread::standbyIfNotAlreadyInStandby() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8827 | { | 
 | 8828 |     if (!mStandby) { | 
 | 8829 |         inputStandBy(); | 
| Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 8830 |         mThreadMetrics.logEndInterval(); | 
| Andy Hung | 44d648b | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 8831 |         mThreadSnapshot.onEnd(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8832 |         mStandby = true; | 
 | 8833 |     } | 
 | 8834 | } | 
 | 8835 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 8836 | void RecordThread::inputStandBy() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8837 | { | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8838 |     // Idle the fast capture if it's currently running | 
 | 8839 |     if (mFastCapture != 0) { | 
 | 8840 |         FastCaptureStateQueue *sq = mFastCapture->sq(); | 
 | 8841 |         FastCaptureState *state = sq->begin(); | 
 | 8842 |         if (!(state->mCommand & FastCaptureState::IDLE)) { | 
 | 8843 |             state->mCommand = FastCaptureState::COLD_IDLE; | 
 | 8844 |             state->mColdFutexAddr = &mFastCaptureFutex; | 
 | 8845 |             state->mColdGen++; | 
 | 8846 |             mFastCaptureFutex = 0; | 
 | 8847 |             sq->end(); | 
 | 8848 |             // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now | 
 | 8849 |             sq->push(FastCaptureStateQueue::BLOCK_UNTIL_ACKED); | 
 | 8850 | #if 0 | 
 | 8851 |             if (kUseFastCapture == FastCapture_Dynamic) { | 
 | 8852 |                 // FIXME | 
 | 8853 |             } | 
 | 8854 | #endif | 
 | 8855 | #ifdef AUDIO_WATCHDOG | 
 | 8856 |             // FIXME | 
 | 8857 | #endif | 
 | 8858 |         } else { | 
 | 8859 |             sq->end(false /*didModify*/); | 
 | 8860 |         } | 
 | 8861 |     } | 
| Mikhail Naganov | 2534b38 | 2019-09-25 13:05:02 -0700 | [diff] [blame] | 8862 |     status_t result = mSource->standby(); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 8863 |     ALOGE_IF(result != OK, "Error when putting input stream into standby: %d", result); | 
| Andy Hung | ad6d52d | 2016-07-18 13:42:03 -0700 | [diff] [blame] | 8864 |  | 
 | 8865 |     // If going into standby, flush the pipe source. | 
 | 8866 |     if (mPipeSource.get() != nullptr) { | 
 | 8867 |         const ssize_t flushed = mPipeSource->flush(); | 
 | 8868 |         if (flushed > 0) { | 
 | 8869 |             ALOGV("Input standby flushed PipeSource %zd frames", flushed); | 
 | 8870 |             mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] += flushed; | 
 | 8871 |             mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = systemTime(); | 
 | 8872 |         } | 
 | 8873 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8874 | } | 
 | 8875 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 8876 | // RecordThread::createRecordTrack_l() must be called with AudioFlinger::mutex() held | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 8877 | sp<IAfRecordTrack> RecordThread::createRecordTrack_l( | 
| Andy Hung | 88035ac | 2023-06-27 17:05:02 -0700 | [diff] [blame] | 8878 |         const sp<Client>& client, | 
| Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 8879 |         const audio_attributes_t& attr, | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 8880 |         uint32_t *pSampleRate, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8881 |         audio_format_t format, | 
 | 8882 |         audio_channel_mask_t channelMask, | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 8883 |         size_t *pFrameCount, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 8884 |         audio_session_t sessionId, | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 8885 |         size_t *pNotificationFrameCount, | 
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 8886 |         pid_t creatorPid, | 
| Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 8887 |         const AttributionSourceState& attributionSource, | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 8888 |         audio_input_flags_t *flags, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8889 |         pid_t tid, | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 8890 |         status_t *status, | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 8891 |         audio_port_handle_t portId, | 
 | 8892 |         int32_t maxSharedAudioHistoryMs) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8893 | { | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 8894 |     size_t frameCount = *pFrameCount; | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 8895 |     size_t notificationFrameCount = *pNotificationFrameCount; | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8896 |     sp<IAfRecordTrack> track; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8897 |     status_t lStatus; | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 8898 |     audio_input_flags_t inputFlags = mInput->flags; | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 8899 |     audio_input_flags_t requestedFlags = *flags; | 
 | 8900 |     uint32_t sampleRate; | 
 | 8901 |  | 
 | 8902 |     lStatus = initCheck(); | 
 | 8903 |     if (lStatus != NO_ERROR) { | 
 | 8904 |         ALOGE("createRecordTrack_l() audio driver not initialized"); | 
 | 8905 |         goto Exit; | 
 | 8906 |     } | 
 | 8907 |  | 
| Mikhail Naganov | ce9f265 | 2018-07-16 11:09:24 -0700 | [diff] [blame] | 8908 |     if (!audio_is_linear_pcm(mFormat) && (*flags & AUDIO_INPUT_FLAG_DIRECT) == 0) { | 
 | 8909 |         ALOGE("createRecordTrack_l() on an encoded stream requires AUDIO_INPUT_FLAG_DIRECT"); | 
 | 8910 |         lStatus = BAD_VALUE; | 
 | 8911 |         goto Exit; | 
 | 8912 |     } | 
 | 8913 |  | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 8914 |     if (maxSharedAudioHistoryMs != 0) { | 
| Eric Laurent | 9ff3e53 | 2022-11-10 16:04:44 +0100 | [diff] [blame] | 8915 |         if (!captureHotwordAllowed(attributionSource)) { | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 8916 |             lStatus = PERMISSION_DENIED; | 
 | 8917 |             goto Exit; | 
 | 8918 |         } | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 8919 |         if (maxSharedAudioHistoryMs < 0 | 
| Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 8920 |                 || maxSharedAudioHistoryMs > kMaxSharedAudioHistoryMs) { | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 8921 |             lStatus = BAD_VALUE; | 
 | 8922 |             goto Exit; | 
 | 8923 |         } | 
 | 8924 |     } | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 8925 |     if (*pSampleRate == 0) { | 
 | 8926 |         *pSampleRate = mSampleRate; | 
 | 8927 |     } | 
 | 8928 |     sampleRate = *pSampleRate; | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 8929 |  | 
| Eric Laurent | 5f0fd7b | 2021-05-07 16:33:26 +0200 | [diff] [blame] | 8930 |     // special case for FAST flag considered OK if fast capture is present and access to | 
 | 8931 |     // audio history is not required | 
 | 8932 |     if (hasFastCapture() && mMaxSharedAudioHistoryMs == 0) { | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 8933 |         inputFlags = (audio_input_flags_t)(inputFlags | AUDIO_INPUT_FLAG_FAST); | 
 | 8934 |     } | 
 | 8935 |  | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 8936 |     // Check if requested flags are compatible with input stream flags | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 8937 |     if ((*flags & inputFlags) != *flags) { | 
 | 8938 |         ALOGW("createRecordTrack_l(): mismatch between requested flags (%08x) and" | 
 | 8939 |                 " input flags (%08x)", | 
 | 8940 |               *flags, inputFlags); | 
 | 8941 |         *flags = (audio_input_flags_t)(*flags & inputFlags); | 
 | 8942 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8943 |  | 
| Eric Laurent | 5f0fd7b | 2021-05-07 16:33:26 +0200 | [diff] [blame] | 8944 |     // client expresses a preference for FAST and no access to audio history, | 
 | 8945 |     // but we get the final say | 
 | 8946 |     if (*flags & AUDIO_INPUT_FLAG_FAST && maxSharedAudioHistoryMs == 0) { | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 8947 |       if ( | 
| Glenn Kasten | b7fbf7e | 2015-03-18 12:57:28 -0700 | [diff] [blame] | 8948 |             // we formerly checked for a callback handler (non-0 tid), | 
 | 8949 |             // but that is no longer required for TRANSFER_OBTAIN mode | 
| jiabin | b00edc3 | 2021-08-16 16:27:54 +0000 | [diff] [blame] | 8950 |             // No need to match hardware format, format conversion will be done in client side. | 
| Glenn Kasten | b7fbf7e | 2015-03-18 12:57:28 -0700 | [diff] [blame] | 8951 |             // | 
| Phil Burk | 7ed66a1 | 2019-04-18 13:20:30 -0700 | [diff] [blame] | 8952 |             // Frame count is not specified (0), or is less than or equal the pipe depth. | 
 | 8953 |             // It is OK to provide a higher capacity than requested. | 
 | 8954 |             // We will force it to mPipeFramesP2 below. | 
 | 8955 |             (frameCount <= mPipeFramesP2) && | 
| Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 8956 |             // PCM data | 
 | 8957 |             audio_is_linear_pcm(format) && | 
| Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 8958 |             // hardware channel mask | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8959 |             (channelMask == mChannelMask) && | 
| Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 8960 |             // hardware sample rate | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 8961 |             (sampleRate == mSampleRate) && | 
| Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 8962 |             // record thread has an associated fast capture | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8963 |             hasFastCapture() && | 
 | 8964 |             // there are sufficient fast track slots available | 
 | 8965 |             mFastTrackAvail | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 8966 |         ) { | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 8967 |           // check compatibility with audio effects. | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 8968 |           audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 8969 |           // Do not accept FAST flag if the session has software effects | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 8970 |           sp<IAfEffectChain> chain = getEffectChain_l(sessionId); | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 8971 |           if (chain != 0) { | 
| Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame] | 8972 |               audio_input_flags_t old = *flags; | 
 | 8973 |               chain->checkInputFlagCompatibility(flags); | 
 | 8974 |               if (old != *flags) { | 
| Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 8975 |                   ALOGV("%p AUDIO_INPUT_FLAGS denied by effect old=%#x new=%#x", | 
 | 8976 |                           this, (int)old, (int)*flags); | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 8977 |               } | 
 | 8978 |           } | 
| Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 8979 |           ALOGV_IF((*flags & AUDIO_INPUT_FLAG_FAST) != 0, | 
| Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 8980 |                    "%p AUDIO_INPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu", | 
 | 8981 |                    this, frameCount, mFrameCount); | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 8982 |       } else { | 
| Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 8983 |         ALOGV("%p AUDIO_INPUT_FLAG_FAST denied: frameCount=%zu mFrameCount=%zu mPipeFramesP2=%zu " | 
 | 8984 |                 "format=%#x isLinear=%d mFormat=%#x channelMask=%#x sampleRate=%u mSampleRate=%u " | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8985 |                 "hasFastCapture=%d tid=%d mFastTrackAvail=%d", | 
| Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 8986 |                 this, frameCount, mFrameCount, mPipeFramesP2, | 
 | 8987 |                 format, audio_is_linear_pcm(format), mFormat, channelMask, sampleRate, mSampleRate, | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 8988 |                 hasFastCapture(), tid, mFastTrackAvail); | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 8989 |         *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST); | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 8990 |       } | 
 | 8991 |     } | 
 | 8992 |  | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 8993 |     // If FAST or RAW flags were corrected, ask caller to request new input from audio policy | 
 | 8994 |     if ((*flags & AUDIO_INPUT_FLAG_FAST) != | 
 | 8995 |             (requestedFlags & AUDIO_INPUT_FLAG_FAST)) { | 
 | 8996 |         *flags = (audio_input_flags_t) (*flags & ~(AUDIO_INPUT_FLAG_FAST | AUDIO_INPUT_FLAG_RAW)); | 
 | 8997 |         lStatus = BAD_TYPE; | 
 | 8998 |         goto Exit; | 
 | 8999 |     } | 
 | 9000 |  | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 9001 |     // compute track buffer size in frames, and suggest the notification frame count | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 9002 |     if (*flags & AUDIO_INPUT_FLAG_FAST) { | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 9003 |         // fast track: frame count is exactly the pipe depth | 
 | 9004 |         frameCount = mPipeFramesP2; | 
 | 9005 |         // ignore requested notificationFrames, and always notify exactly once every HAL buffer | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 9006 |         notificationFrameCount = mFrameCount; | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 9007 |     } else { | 
| Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 9008 |         // not fast track: max notification period is resampled equivalent of one HAL buffer time | 
 | 9009 |         //                 or 20 ms if there is a fast capture | 
 | 9010 |         // TODO This could be a roundupRatio inline, and const | 
 | 9011 |         size_t maxNotificationFrames = ((int64_t) (hasFastCapture() ? mSampleRate/50 : mFrameCount) | 
 | 9012 |                 * sampleRate + mSampleRate - 1) / mSampleRate; | 
 | 9013 |         // minimum number of notification periods is at least kMinNotifications, | 
 | 9014 |         // and at least kMinMs rounded up to a whole notification period (minNotificationsByMs) | 
 | 9015 |         static const size_t kMinNotifications = 3; | 
 | 9016 |         static const uint32_t kMinMs = 30; | 
 | 9017 |         // TODO This could be a roundupRatio inline | 
 | 9018 |         const size_t minFramesByMs = (sampleRate * kMinMs + 1000 - 1) / 1000; | 
 | 9019 |         // TODO This could be a roundupRatio inline | 
 | 9020 |         const size_t minNotificationsByMs = (minFramesByMs + maxNotificationFrames - 1) / | 
 | 9021 |                 maxNotificationFrames; | 
 | 9022 |         const size_t minFrameCount = maxNotificationFrames * | 
 | 9023 |                 max(kMinNotifications, minNotificationsByMs); | 
 | 9024 |         frameCount = max(frameCount, minFrameCount); | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 9025 |         if (notificationFrameCount == 0 || notificationFrameCount > maxNotificationFrames) { | 
 | 9026 |             notificationFrameCount = maxNotificationFrames; | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 9027 |         } | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 9028 |     } | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 9029 |     *pFrameCount = frameCount; | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 9030 |     *pNotificationFrameCount = notificationFrameCount; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9031 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 9032 |     { // scope for mutex() | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9033 |         audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 9034 |         int32_t startFrames = -1; | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9035 |         if (!mSharedAudioPackageName.empty() | 
| Eric Laurent | 9ff3e53 | 2022-11-10 16:04:44 +0100 | [diff] [blame] | 9036 |                 && mSharedAudioPackageName == attributionSource.packageName | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9037 |                 && mSharedAudioSessionId == sessionId | 
| Eric Laurent | 9ff3e53 | 2022-11-10 16:04:44 +0100 | [diff] [blame] | 9038 |                 && captureHotwordAllowed(attributionSource)) { | 
| Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 9039 |             startFrames = mSharedAudioStartFrames; | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9040 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9041 |  | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9042 |         track = IAfRecordTrack::create(this, client, attr, sampleRate, | 
| Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 9043 |                       format, channelMask, frameCount, | 
| Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 9044 |                       nullptr /* buffer */, (size_t)0 /* bufferSize */, sessionId, creatorPid, | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9045 |                       attributionSource, *flags, IAfTrackBase::TYPE_DEFAULT, portId, | 
| Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 9046 |                       startFrames); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9047 |  | 
| Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 9048 |         lStatus = track->initCheck(); | 
 | 9049 |         if (lStatus != NO_ERROR) { | 
| Glenn Kasten | 3529507 | 2013-10-07 09:27:06 -0700 | [diff] [blame] | 9050 |             ALOGE("createRecordTrack_l() initCheck failed %d; no control block?", lStatus); | 
| Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 9051 |             // 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] | 9052 |             goto Exit; | 
 | 9053 |         } | 
 | 9054 |         mTracks.add(track); | 
 | 9055 |  | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 9056 |         if ((*flags & AUDIO_INPUT_FLAG_FAST) && (tid != -1)) { | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 9057 |             pid_t callingPid = IPCThreadState::self()->getCallingPid(); | 
 | 9058 |             // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, | 
 | 9059 |             // so ask activity manager to do this on our behalf | 
| Glenn Kasten | af9a7b5 | 2017-03-29 11:29:39 -0700 | [diff] [blame] | 9060 |             sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp, true /*forApp*/); | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 9061 |         } | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9062 |  | 
 | 9063 |         if (maxSharedAudioHistoryMs != 0) { | 
 | 9064 |             sendResizeBufferConfigEvent_l(maxSharedAudioHistoryMs); | 
 | 9065 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9066 |     } | 
| Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 9067 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9068 |     lStatus = NO_ERROR; | 
 | 9069 |  | 
 | 9070 | Exit: | 
| Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 9071 |     *status = lStatus; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9072 |     return track; | 
 | 9073 | } | 
 | 9074 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9075 | status_t RecordThread::start(IAfRecordTrack* recordTrack, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9076 |                                            AudioSystem::sync_event_t event, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 9077 |                                            audio_session_t triggerSession) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9078 | { | 
 | 9079 |     ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession); | 
 | 9080 |     sp<ThreadBase> strongMe = this; | 
 | 9081 |     status_t status = NO_ERROR; | 
 | 9082 |  | 
 | 9083 |     if (event == AudioSystem::SYNC_EVENT_NONE) { | 
| Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 9084 |         recordTrack->clearSyncStartEvent(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9085 |     } else if (event != AudioSystem::SYNC_EVENT_SAME) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9086 |         recordTrack->synchronizedRecordState().startRecording( | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 9087 |                 mAfThreadCallback->createSyncEvent( | 
| Andy Hung | 93bb573 | 2023-05-04 21:16:34 -0700 | [diff] [blame] | 9088 |                         event, triggerSession, | 
 | 9089 |                         recordTrack->sessionId(), syncStartEventCallback, recordTrack)); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9090 |     } | 
 | 9091 |  | 
 | 9092 |     { | 
| Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 9093 |         // This section is a rendezvous between binder thread executing start() and RecordThread | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9094 |          audio_utils::lock_guard lock(mutex()); | 
| Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9095 |         if (recordTrack->isInvalid()) { | 
 | 9096 |             recordTrack->clearSyncStartEvent(); | 
| Eric Laurent | d52a28c | 2020-08-21 17:10:39 -0700 | [diff] [blame] | 9097 |             ALOGW("%s track %d: invalidated before startInput", __func__, recordTrack->portId()); | 
 | 9098 |             return DEAD_OBJECT; | 
| Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9099 |         } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 9100 |         if (mActiveTracks.indexOf(recordTrack) >= 0) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9101 |             if (recordTrack->state() == IAfTrackBase::PAUSING) { | 
| Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9102 |                 // We haven't stopped yet (moved to PAUSED and not in mActiveTracks) | 
 | 9103 |                 // so no need to startInput(). | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 9104 |                 ALOGV("active record track PAUSING -> ACTIVE"); | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9105 |                 recordTrack->setState(IAfTrackBase::ACTIVE); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 9106 |             } else { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9107 |                 ALOGV("active record track state %d", (int)recordTrack->state()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9108 |             } | 
 | 9109 |             return status; | 
 | 9110 |         } | 
 | 9111 |  | 
| Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 9112 |         // TODO consider other ways of handling this, such as changing the state to :STARTING and | 
 | 9113 |         //      adding the track to mActiveTracks after returning from AudioSystem::startInput(), | 
 | 9114 |         //      or using a separate command thread | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9115 |         recordTrack->setState(IAfTrackBase::STARTING_1); | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 9116 |         mActiveTracks.add(recordTrack); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 9117 |         if (recordTrack->isExternalTrack()) { | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 9118 |             mutex().unlock(); | 
| Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 9119 |             status = AudioSystem::startInput(recordTrack->portId()); | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 9120 |             mutex().lock(); | 
| Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9121 |             if (recordTrack->isInvalid()) { | 
 | 9122 |                 recordTrack->clearSyncStartEvent(); | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9123 |                 if (status == NO_ERROR && recordTrack->state() == IAfTrackBase::STARTING_1) { | 
 | 9124 |                     recordTrack->setState(IAfTrackBase::STARTING_2); | 
| Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9125 |                     // STARTING_2 forces destroy to call stopInput. | 
 | 9126 |                 } | 
| Eric Laurent | d52a28c | 2020-08-21 17:10:39 -0700 | [diff] [blame] | 9127 |                 ALOGW("%s track %d: invalidated after startInput", __func__, recordTrack->portId()); | 
 | 9128 |                 return DEAD_OBJECT; | 
| Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9129 |             } | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9130 |             if (recordTrack->state() != IAfTrackBase::STARTING_1) { | 
| Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9131 |                 ALOGW("%s(%d): unsynchronized mState:%d change", | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9132 |                     __func__, recordTrack->id(), (int)recordTrack->state()); | 
| Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9133 |                 // Someone else has changed state, let them take over, | 
 | 9134 |                 // leave mState in the new state. | 
 | 9135 |                 recordTrack->clearSyncStartEvent(); | 
 | 9136 |                 return INVALID_OPERATION; | 
 | 9137 |             } | 
 | 9138 |             // we're ok, but perhaps startInput has failed | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 9139 |             if (status != NO_ERROR) { | 
| Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9140 |                 ALOGW("%s(%d): startInput failed, status %d", | 
 | 9141 |                     __func__, recordTrack->id(), status); | 
 | 9142 |                 // We are in ActiveTracks if STARTING_1 and valid, so remove from ActiveTracks, | 
 | 9143 |                 // leave in STARTING_1, so destroy() will not call stopInput. | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 9144 |                 mActiveTracks.remove(recordTrack); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 9145 |                 recordTrack->clearSyncStartEvent(); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 9146 |                 return status; | 
 | 9147 |             } | 
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 9148 |             sendIoConfigEvent_l( | 
 | 9149 |                 AUDIO_CLIENT_STARTED, recordTrack->creatorPid(), recordTrack->portId()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9150 |         } | 
| Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 9151 |  | 
 | 9152 |         recordTrack->logBeginInterval(patchSourcesToString(&mPatch)); // log to MediaMetrics | 
 | 9153 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 9154 |         // Catch up with current buffer indices if thread is already running. | 
 | 9155 |         // This is what makes a new client discard all buffered data.  If the track's mRsmpInFront | 
 | 9156 |         // was initialized to some value closer to the thread's mRsmpInFront, then the track could | 
 | 9157 |         // see previously buffered data before it called start(), but with greater risk of overrun. | 
 | 9158 |  | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9159 |         recordTrack->resamplerBufferProvider()->reset(); | 
| Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 9160 |         if (!recordTrack->isDirect()) { | 
 | 9161 |             // clear any converter state as new data will be discontinuous | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9162 |             recordTrack->recordBufferConverter()->reset(); | 
| Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 9163 |         } | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9164 |         recordTrack->setState(IAfTrackBase::STARTING_2); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9165 |         // signal thread to start | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 9166 |         mWaitWorkCV.notify_all(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9167 |         return status; | 
 | 9168 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9169 | } | 
 | 9170 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9171 | void RecordThread::syncStartEventCallback(const wp<SyncEvent>& event) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9172 | { | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9173 |     const sp<SyncEvent> strongEvent = event.promote(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9174 |  | 
 | 9175 |     if (strongEvent != 0) { | 
| Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 9176 |         sp<IAfTrackBase> ptr = | 
 | 9177 |                 std::any_cast<const wp<IAfTrackBase>>(strongEvent->cookie()).promote(); | 
 | 9178 |         if (ptr != nullptr) { | 
| Andy Hung | eb6b5f8 | 2023-07-14 11:00:08 -0700 | [diff] [blame] | 9179 |             // TODO(b/291317898) handleSyncStartEvent is in IAfTrackBase not IAfRecordTrack. | 
| Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 9180 |             ptr->handleSyncStartEvent(strongEvent); | 
| Eric Laurent | 8ea16e4 | 2014-02-20 16:26:11 -0800 | [diff] [blame] | 9181 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9182 |     } | 
 | 9183 | } | 
 | 9184 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9185 | bool RecordThread::stop(IAfRecordTrack* recordTrack) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9186 |     ALOGV("RecordThread::stop"); | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 9187 |     audio_utils::unique_lock _l(mutex()); | 
| Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9188 |     // if we're invalid, we can't be on the ActiveTracks. | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9189 |     if (mActiveTracks.indexOf(recordTrack) < 0 || recordTrack->state() == IAfTrackBase::PAUSING) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9190 |         return false; | 
 | 9191 |     } | 
| Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 9192 |     // note that threadLoop may still be processing the track at this point [without lock] | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9193 |     recordTrack->setState(IAfTrackBase::PAUSING); | 
| Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9194 |  | 
| Andy Hung | abfab20 | 2019-03-07 19:45:54 -0800 | [diff] [blame] | 9195 |     // NOTE: Waiting here is important to keep stop synchronous. | 
 | 9196 |     // This is needed for proper patchRecord peer release. | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9197 |     while (recordTrack->state() == IAfTrackBase::PAUSING && !recordTrack->isInvalid()) { | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 9198 |         mWaitWorkCV.notify_all(); // signal thread to stop | 
 | 9199 |         mStartStopCV.wait(_l); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9200 |     } | 
| Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9201 |  | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9202 |     if (recordTrack->state() == IAfTrackBase::PAUSED) { // successful stop | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9203 |         ALOGV("Record stopped OK"); | 
 | 9204 |         return true; | 
 | 9205 |     } | 
| Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9206 |  | 
 | 9207 |     // don't handle anything - we've been invalidated or restarted and in a different state | 
 | 9208 |     ALOGW_IF("%s(%d): unsynchronized stop, state: %d", | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9209 |             __func__, recordTrack->id(), recordTrack->state()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9210 |     return false; | 
 | 9211 | } | 
 | 9212 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9213 | bool RecordThread::isValidSyncEvent(const sp<SyncEvent>& /* event */) const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9214 | { | 
 | 9215 |     return false; | 
 | 9216 | } | 
 | 9217 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9218 | status_t RecordThread::setSyncEvent(const sp<SyncEvent>& /* event */) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9219 | { | 
 | 9220 | #if 0   // This branch is currently dead code, but is preserved in case it will be needed in future | 
 | 9221 |     if (!isValidSyncEvent(event)) { | 
 | 9222 |         return BAD_VALUE; | 
 | 9223 |     } | 
 | 9224 |  | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 9225 |     audio_session_t eventSession = event->triggerSession(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9226 |     status_t ret = NAME_NOT_FOUND; | 
 | 9227 |  | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9228 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9229 |  | 
 | 9230 |     for (size_t i = 0; i < mTracks.size(); i++) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9231 |         sp<IAfRecordTrack> track = mTracks[i]; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9232 |         if (eventSession == track->sessionId()) { | 
 | 9233 |             (void) track->setSyncEvent(event); | 
 | 9234 |             ret = NO_ERROR; | 
 | 9235 |         } | 
 | 9236 |     } | 
 | 9237 |     return ret; | 
 | 9238 | #else | 
 | 9239 |     return BAD_VALUE; | 
 | 9240 | #endif | 
 | 9241 | } | 
 | 9242 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9243 | status_t RecordThread::getActiveMicrophones( | 
| Andy Hung | 0c1e11e | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 9244 |         std::vector<media::MicrophoneInfoFw>* activeMicrophones) const | 
| jiabin | 653cc0a | 2018-01-17 17:54:10 -0800 | [diff] [blame] | 9245 | { | 
 | 9246 |     ALOGV("RecordThread::getActiveMicrophones"); | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9247 |      audio_utils::lock_guard _l(mutex()); | 
| Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 9248 |     if (!isStreamInitialized()) { | 
| Paul McLean | 8a661a3 | 2021-04-12 10:21:42 -0600 | [diff] [blame] | 9249 |         return NO_INIT; | 
 | 9250 |     } | 
| jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 9251 |     status_t status = mInput->stream->getActiveMicrophones(activeMicrophones); | 
 | 9252 |     return status; | 
| jiabin | 653cc0a | 2018-01-17 17:54:10 -0800 | [diff] [blame] | 9253 | } | 
 | 9254 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9255 | status_t RecordThread::setPreferredMicrophoneDirection( | 
| Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 9256 |             audio_microphone_direction_t direction) | 
| Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 9257 | { | 
| Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 9258 |     ALOGV("setPreferredMicrophoneDirection(%d)", direction); | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9259 |      audio_utils::lock_guard _l(mutex()); | 
| Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 9260 |     if (!isStreamInitialized()) { | 
| Paul McLean | 8a661a3 | 2021-04-12 10:21:42 -0600 | [diff] [blame] | 9261 |         return NO_INIT; | 
 | 9262 |     } | 
| Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 9263 |     return mInput->stream->setPreferredMicrophoneDirection(direction); | 
| Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 9264 | } | 
 | 9265 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9266 | status_t RecordThread::setPreferredMicrophoneFieldDimension(float zoom) | 
| Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 9267 | { | 
| Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 9268 |     ALOGV("setPreferredMicrophoneFieldDimension(%f)", zoom); | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9269 |      audio_utils::lock_guard _l(mutex()); | 
| Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 9270 |     if (!isStreamInitialized()) { | 
| Paul McLean | 8a661a3 | 2021-04-12 10:21:42 -0600 | [diff] [blame] | 9271 |         return NO_INIT; | 
 | 9272 |     } | 
| Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 9273 |     return mInput->stream->setPreferredMicrophoneFieldDimension(zoom); | 
| Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 9274 | } | 
 | 9275 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9276 | status_t RecordThread::shareAudioHistory( | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9277 |         const std::string& sharedAudioPackageName, audio_session_t sharedSessionId, | 
 | 9278 |         int64_t sharedAudioStartMs) { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9279 |      audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9280 |     return shareAudioHistory_l(sharedAudioPackageName, sharedSessionId, sharedAudioStartMs); | 
 | 9281 | } | 
 | 9282 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9283 | status_t RecordThread::shareAudioHistory_l( | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9284 |         const std::string& sharedAudioPackageName, audio_session_t sharedSessionId, | 
 | 9285 |         int64_t sharedAudioStartMs) { | 
| Eric Laurent | 92d0a32 | 2021-07-16 15:32:33 +0200 | [diff] [blame] | 9286 |  | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9287 |     if ((hasAudioSession_l(sharedSessionId) & ThreadBase::TRACK_SESSION) == 0) { | 
 | 9288 |         return BAD_VALUE; | 
 | 9289 |     } | 
| Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 9290 |  | 
 | 9291 |     if (sharedAudioStartMs < 0 | 
 | 9292 |         || sharedAudioStartMs > INT64_MAX / mSampleRate) { | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9293 |         return BAD_VALUE; | 
 | 9294 |     } | 
 | 9295 |  | 
| Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 9296 |     // Current implementation of the input resampling buffer wraps around indexes at 32 bit. | 
 | 9297 |     // As we cannot detect more than one wraparound, only accept values up current write position | 
 | 9298 |     // after one wraparound | 
 | 9299 |     // We assume recent wraparounds on mRsmpInRear only given it is unlikely that the requesting | 
 | 9300 |     // app waits several hours after the start time was computed. | 
| Eric Laurent | 92d0a32 | 2021-07-16 15:32:33 +0200 | [diff] [blame] | 9301 |     int64_t sharedAudioStartFrames = sharedAudioStartMs * mSampleRate / 1000; | 
| Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 9302 |     const int32_t sharedOffset = audio_utils::safe_sub_overflow(mRsmpInRear, | 
 | 9303 |           (int32_t)sharedAudioStartFrames); | 
| Eric Laurent | 92d0a32 | 2021-07-16 15:32:33 +0200 | [diff] [blame] | 9304 |     // Bring the start frame position within the input buffer to match the documented | 
 | 9305 |     // "best effort" behavior of the API. | 
 | 9306 |     if (sharedOffset < 0) { | 
 | 9307 |         sharedAudioStartFrames = mRsmpInRear; | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 9308 |     } else if (sharedOffset > static_cast<signed>(mRsmpInFrames)) { | 
| Eric Laurent | 92d0a32 | 2021-07-16 15:32:33 +0200 | [diff] [blame] | 9309 |         sharedAudioStartFrames = | 
 | 9310 |                 audio_utils::safe_sub_overflow(mRsmpInRear, (int32_t)mRsmpInFrames); | 
| Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 9311 |     } | 
 | 9312 |  | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9313 |     mSharedAudioPackageName = sharedAudioPackageName; | 
 | 9314 |     if (mSharedAudioPackageName.empty()) { | 
| Eric Laurent | 92d0a32 | 2021-07-16 15:32:33 +0200 | [diff] [blame] | 9315 |         resetAudioHistory_l(); | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9316 |     } else { | 
 | 9317 |         mSharedAudioSessionId = sharedSessionId; | 
| Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 9318 |         mSharedAudioStartFrames = (int32_t)sharedAudioStartFrames; | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9319 |     } | 
 | 9320 |     return NO_ERROR; | 
 | 9321 | } | 
 | 9322 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9323 | void RecordThread::resetAudioHistory_l() { | 
| Eric Laurent | 92d0a32 | 2021-07-16 15:32:33 +0200 | [diff] [blame] | 9324 |     mSharedAudioSessionId = AUDIO_SESSION_NONE; | 
 | 9325 |     mSharedAudioStartFrames = -1; | 
 | 9326 |     mSharedAudioPackageName = ""; | 
 | 9327 | } | 
 | 9328 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9329 | ThreadBase::MetadataUpdate RecordThread::updateMetadata_l() | 
| Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 9330 | { | 
| Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 9331 |     if (!isStreamInitialized() || !mActiveTracks.readAndClearHasChanged()) { | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 9332 |         return {}; // nothing to do | 
| Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 9333 |     } | 
 | 9334 |     StreamInHalInterface::SinkMetadata metadata; | 
| Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 9335 |     auto backInserter = std::back_inserter(metadata.tracks); | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9336 |     for (const sp<IAfRecordTrack>& track : mActiveTracks) { | 
| Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 9337 |         track->copyMetadataTo(backInserter); | 
| Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 9338 |     } | 
 | 9339 |     mInput->stream->updateSinkMetadata(metadata); | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 9340 |     MetadataUpdate change; | 
 | 9341 |     change.recordMetadataUpdate = metadata.tracks; | 
 | 9342 |     return change; | 
| Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 9343 | } | 
 | 9344 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 9345 | // destroyTrack_l() must be called with ThreadBase::mutex() held | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9346 | void RecordThread::destroyTrack_l(const sp<IAfRecordTrack>& track) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9347 | { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 9348 |     track->terminate(); | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9349 |     track->setState(IAfTrackBase::STOPPED); | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9350 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9351 |     // active tracks are removed by threadLoop() | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 9352 |     if (mActiveTracks.indexOf(track) < 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9353 |         removeTrack_l(track); | 
 | 9354 |     } | 
 | 9355 | } | 
 | 9356 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9357 | void RecordThread::removeTrack_l(const sp<IAfRecordTrack>& track) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9358 | { | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 9359 |     String8 result; | 
 | 9360 |     track->appendDump(result, false /* active */); | 
| Tomasz Wasilczyk | 5b05437 | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 9361 |     mLocalLog.log("removeTrack_l (%p) %s", track.get(), result.c_str()); | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 9362 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9363 |     mTracks.remove(track); | 
 | 9364 |     // need anything related to effects here? | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 9365 |     if (track->isFastTrack()) { | 
 | 9366 |         ALOG_ASSERT(!mFastTrackAvail); | 
 | 9367 |         mFastTrackAvail = true; | 
 | 9368 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9369 | } | 
 | 9370 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9371 | void RecordThread::dumpInternals_l(int fd, const Vector<String16>& /* args */) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9372 | { | 
| Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 9373 |     AudioStreamIn *input = mInput; | 
 | 9374 |     audio_input_flags_t flags = input != NULL ? input->flags : AUDIO_INPUT_FLAG_NONE; | 
 | 9375 |     dprintf(fd, "  AudioStreamIn: %p flags %#x (%s)\n", | 
| Andy Hung | 9b18195 | 2019-02-25 14:53:36 -0800 | [diff] [blame] | 9376 |             input, flags, toString(flags).c_str()); | 
| Andy Hung | 6427e44 | 2018-08-09 12:51:02 -0700 | [diff] [blame] | 9377 |     dprintf(fd, "  Frames read: %lld\n", (long long)mFramesRead); | 
| Eric Tan | 39ec8d6 | 2018-07-24 09:49:29 -0700 | [diff] [blame] | 9378 |     if (mActiveTracks.isEmpty()) { | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 9379 |         dprintf(fd, "  No active record clients\n"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9380 |     } | 
| Andy Hung | bfa6496 | 2017-06-12 14:43:19 -0700 | [diff] [blame] | 9381 |  | 
 | 9382 |     if (input != nullptr) { | 
 | 9383 |         dprintf(fd, "  Hal stream dump:\n"); | 
 | 9384 |         (void)input->stream->dump(fd); | 
 | 9385 |     } | 
 | 9386 |  | 
| Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 9387 |     dprintf(fd, "  Fast capture thread: %s\n", hasFastCapture() ? "yes" : "no"); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 9388 |     dprintf(fd, "  Fast track available: %s\n", mFastTrackAvail ? "yes" : "no"); | 
| Glenn Kasten | 17c9c99 | 2015-03-02 15:53:01 -0800 | [diff] [blame] | 9389 |  | 
| Glenn Kasten | 2f90c51 | 2015-12-02 11:40:09 -0800 | [diff] [blame] | 9390 |     // Make a non-atomic copy of fast capture dump state so it won't change underneath us | 
 | 9391 |     // while we are dumping it.  It may be inconsistent, but it won't mutate! | 
 | 9392 |     // This is a large object so we place it on the heap. | 
 | 9393 |     // FIXME 25972958: Need an intelligent copy constructor that does not touch unused pages. | 
| Eric Tan | 2942a4e | 2018-09-12 17:41:27 -0700 | [diff] [blame] | 9394 |     const std::unique_ptr<FastCaptureDumpState> copy = | 
 | 9395 |             std::make_unique<FastCaptureDumpState>(mFastCaptureDumpState); | 
| Glenn Kasten | 2f90c51 | 2015-12-02 11:40:09 -0800 | [diff] [blame] | 9396 |     copy->dump(fd); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9397 | } | 
 | 9398 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9399 | void RecordThread::dumpTracks_l(int fd, const Vector<String16>& /* args */) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9400 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9401 |     String8 result; | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9402 |     size_t numtracks = mTracks.size(); | 
 | 9403 |     size_t numactive = mActiveTracks.size(); | 
 | 9404 |     size_t numactiveseen = 0; | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 9405 |     dprintf(fd, "  %zu Tracks", numtracks); | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 9406 |     const char *prefix = "    "; | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9407 |     if (numtracks) { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 9408 |         dprintf(fd, " of which %zu are active\n", numactive); | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 9409 |         result.append(prefix); | 
| Andy Hung | 000adb5 | 2018-06-01 15:43:26 -0700 | [diff] [blame] | 9410 |         mTracks[0]->appendDumpHeader(result); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9411 |         for (size_t i = 0; i < numtracks ; ++i) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9412 |             sp<IAfRecordTrack> track = mTracks[i]; | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9413 |             if (track != 0) { | 
 | 9414 |                 bool active = mActiveTracks.indexOf(track) >= 0; | 
 | 9415 |                 if (active) { | 
 | 9416 |                     numactiveseen++; | 
 | 9417 |                 } | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 9418 |                 result.append(prefix); | 
 | 9419 |                 track->appendDump(result, active); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9420 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9421 |         } | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9422 |     } else { | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 9423 |         dprintf(fd, "\n"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9424 |     } | 
 | 9425 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9426 |     if (numactiveseen != numactive) { | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 9427 |         result.append("  The following tracks are in the active list but" | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9428 |                 " not in the track list\n"); | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 9429 |         result.append(prefix); | 
| Andy Hung | 000adb5 | 2018-06-01 15:43:26 -0700 | [diff] [blame] | 9430 |         mActiveTracks[0]->appendDumpHeader(result); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9431 |         for (size_t i = 0; i < numactive; ++i) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9432 |             sp<IAfRecordTrack> track = mActiveTracks[i]; | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9433 |             if (mTracks.indexOf(track) < 0) { | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 9434 |                 result.append(prefix); | 
 | 9435 |                 track->appendDump(result, true /* active */); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9436 |             } | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 9437 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9438 |  | 
 | 9439 |     } | 
| Tomasz Wasilczyk | 5b05437 | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 9440 |     write(fd, result.c_str(), result.size()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9441 | } | 
 | 9442 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9443 | void RecordThread::setRecordSilenced(audio_port_handle_t portId, bool silenced) | 
| Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 9444 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9445 |     audio_utils::lock_guard _l(mutex()); | 
| Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 9446 |     for (size_t i = 0; i < mTracks.size() ; i++) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9447 |         sp<IAfRecordTrack> track = mTracks[i]; | 
| Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 9448 |         if (track != 0 && track->portId() == portId) { | 
| Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 9449 |             track->setSilenced(silenced); | 
 | 9450 |         } | 
 | 9451 |     } | 
 | 9452 | } | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9453 |  | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9454 | void ResamplerBufferProvider::reset() | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9455 | { | 
| Andy Hung | 0c1e11e | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 9456 |     const auto threadBase = mRecordTrack->thread().promote(); | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9457 |     auto* const recordThread = static_cast<RecordThread *>(threadBase->asIAfRecordThread().get()); | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9458 |     mRsmpInUnrel = 0; | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9459 |     const int32_t rear = recordThread->mRsmpInRear; | 
 | 9460 |     ssize_t deltaFrames = 0; | 
| Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 9461 |     if (mRecordTrack->startFrames() >= 0) { | 
 | 9462 |         int32_t startFrames = mRecordTrack->startFrames(); | 
 | 9463 |         // Accept a recent wraparound of mRsmpInRear | 
 | 9464 |         if (startFrames <= rear) { | 
 | 9465 |             deltaFrames = rear - startFrames; | 
 | 9466 |         } else { | 
 | 9467 |             deltaFrames = (int32_t)((int64_t)rear + UINT32_MAX + 1 - startFrames); | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9468 |         } | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9469 |         // start frame cannot be further in the past than start of resampling buffer | 
 | 9470 |         if ((size_t) deltaFrames > recordThread->mRsmpInFrames) { | 
 | 9471 |             deltaFrames = recordThread->mRsmpInFrames; | 
 | 9472 |         } | 
 | 9473 |     } | 
 | 9474 |     mRsmpInFront = audio_utils::safe_sub_overflow(rear, static_cast<int32_t>(deltaFrames)); | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9475 | } | 
 | 9476 |  | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9477 | void ResamplerBufferProvider::sync( | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9478 |         size_t *framesAvailable, bool *hasOverrun) | 
 | 9479 | { | 
| Andy Hung | 0c1e11e | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 9480 |     const auto threadBase = mRecordTrack->thread().promote(); | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9481 |     auto* const recordThread = static_cast<RecordThread *>(threadBase->asIAfRecordThread().get()); | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9482 |     const int32_t rear = recordThread->mRsmpInRear; | 
 | 9483 |     const int32_t front = mRsmpInFront; | 
| Hongwei Wang | 95e3768 | 2019-04-12 11:13:36 -0700 | [diff] [blame] | 9484 |     const ssize_t filled = audio_utils::safe_sub_overflow(rear, front); | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9485 |  | 
 | 9486 |     size_t framesIn; | 
 | 9487 |     bool overrun = false; | 
 | 9488 |     if (filled < 0) { | 
 | 9489 |         // should not happen, but treat like a massive overrun and re-sync | 
 | 9490 |         framesIn = 0; | 
 | 9491 |         mRsmpInFront = rear; | 
 | 9492 |         overrun = true; | 
 | 9493 |     } else if ((size_t) filled <= recordThread->mRsmpInFrames) { | 
 | 9494 |         framesIn = (size_t) filled; | 
 | 9495 |     } else { | 
 | 9496 |         // client is not keeping up with server, but give it latest data | 
 | 9497 |         framesIn = recordThread->mRsmpInFrames; | 
| Hongwei Wang | 95e3768 | 2019-04-12 11:13:36 -0700 | [diff] [blame] | 9498 |         mRsmpInFront = /* front = */ audio_utils::safe_sub_overflow( | 
 | 9499 |                 rear, static_cast<int32_t>(framesIn)); | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9500 |         overrun = true; | 
 | 9501 |     } | 
 | 9502 |     if (framesAvailable != NULL) { | 
 | 9503 |         *framesAvailable = framesIn; | 
 | 9504 |     } | 
 | 9505 |     if (hasOverrun != NULL) { | 
 | 9506 |         *hasOverrun = overrun; | 
 | 9507 |     } | 
 | 9508 | } | 
 | 9509 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9510 | // AudioBufferProvider interface | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9511 | status_t ResamplerBufferProvider::getNextBuffer( | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 9512 |         AudioBufferProvider::Buffer* buffer) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9513 | { | 
| Andy Hung | 0c1e11e | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 9514 |     const auto threadBase = mRecordTrack->thread().promote(); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 9515 |     if (threadBase == 0) { | 
 | 9516 |         buffer->frameCount = 0; | 
| Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 9517 |         buffer->raw = NULL; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 9518 |         return NOT_ENOUGH_DATA; | 
 | 9519 |     } | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9520 |     auto* const recordThread = static_cast<RecordThread *>(threadBase->asIAfRecordThread().get()); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 9521 |     int32_t rear = recordThread->mRsmpInRear; | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9522 |     int32_t front = mRsmpInFront; | 
| Hongwei Wang | 95e3768 | 2019-04-12 11:13:36 -0700 | [diff] [blame] | 9523 |     ssize_t filled = audio_utils::safe_sub_overflow(rear, front); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 9524 |     // FIXME should not be P2 (don't want to increase latency) | 
 | 9525 |     // FIXME if client not keeping up, discard | 
| Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 9526 |     LOG_ALWAYS_FATAL_IF(!(0 <= filled && (size_t) filled <= recordThread->mRsmpInFrames)); | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 9527 |     // 'filled' may be non-contiguous, so return only the first contiguous chunk | 
| Eric Laurent | 5f0fd7b | 2021-05-07 16:33:26 +0200 | [diff] [blame] | 9528 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 9529 |     front &= recordThread->mRsmpInFramesP2 - 1; | 
 | 9530 |     size_t part1 = recordThread->mRsmpInFramesP2 - front; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 9531 |     if (part1 > (size_t) filled) { | 
 | 9532 |         part1 = filled; | 
 | 9533 |     } | 
 | 9534 |     size_t ask = buffer->frameCount; | 
 | 9535 |     ALOG_ASSERT(ask > 0); | 
 | 9536 |     if (part1 > ask) { | 
 | 9537 |         part1 = ask; | 
 | 9538 |     } | 
 | 9539 |     if (part1 == 0) { | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9540 |         // 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] | 9541 |         buffer->raw = NULL; | 
 | 9542 |         buffer->frameCount = 0; | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9543 |         mRsmpInUnrel = 0; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 9544 |         return NOT_ENOUGH_DATA; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9545 |     } | 
 | 9546 |  | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 9547 |     buffer->raw = (uint8_t*)recordThread->mRsmpInBuffer + front * recordThread->mFrameSize; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 9548 |     buffer->frameCount = part1; | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9549 |     mRsmpInUnrel = part1; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9550 |     return NO_ERROR; | 
 | 9551 | } | 
 | 9552 |  | 
 | 9553 | // AudioBufferProvider interface | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9554 | void ResamplerBufferProvider::releaseBuffer( | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 9555 |         AudioBufferProvider::Buffer* buffer) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9556 | { | 
| Hongwei Wang | 95e3768 | 2019-04-12 11:13:36 -0700 | [diff] [blame] | 9557 |     int32_t stepCount = static_cast<int32_t>(buffer->frameCount); | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 9558 |     if (stepCount == 0) { | 
 | 9559 |         return; | 
 | 9560 |     } | 
| Eric Laurent | 1e28aaa | 2023-04-16 19:34:23 +0200 | [diff] [blame] | 9561 |     ALOG_ASSERT(stepCount <= (int32_t)mRsmpInUnrel); | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9562 |     mRsmpInUnrel -= stepCount; | 
| Hongwei Wang | 95e3768 | 2019-04-12 11:13:36 -0700 | [diff] [blame] | 9563 |     mRsmpInFront = audio_utils::safe_add_overflow(mRsmpInFront, stepCount); | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 9564 |     buffer->raw = NULL; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9565 |     buffer->frameCount = 0; | 
 | 9566 | } | 
 | 9567 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9568 | void RecordThread::checkBtNrec() | 
| Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 9569 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9570 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 9571 |     checkBtNrec_l(); | 
 | 9572 | } | 
 | 9573 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9574 | void RecordThread::checkBtNrec_l() | 
| Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 9575 | { | 
 | 9576 |     // disable AEC and NS if the device is a BT SCO headset supporting those | 
 | 9577 |     // pre processings | 
| Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 9578 |     bool suspend = audio_is_bluetooth_sco_device(inDeviceType_l()) && | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 9579 |                         mAfThreadCallback->btNrecIsOff(); | 
| Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 9580 |     if (mBtNrecSuspended.exchange(suspend) != suspend) { | 
 | 9581 |         for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 9582 |             setEffectSuspended_l(FX_IID_AEC, suspend, mEffectChains[i]->sessionId()); | 
 | 9583 |             setEffectSuspended_l(FX_IID_NS, suspend, mEffectChains[i]->sessionId()); | 
 | 9584 |         } | 
 | 9585 |     } | 
 | 9586 | } | 
 | 9587 |  | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 9588 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9589 | bool RecordThread::checkForNewParameter_l(const String8& keyValuePair, | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9590 |                                                         status_t& status) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9591 | { | 
 | 9592 |     bool reconfig = false; | 
 | 9593 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9594 |     status = NO_ERROR; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9595 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9596 |     audio_format_t reqFormat = mFormat; | 
 | 9597 |     uint32_t samplingRate = mSampleRate; | 
| Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 9598 |     // TODO this may change if we want to support capture from HDMI PCM multi channel (e.g on TVs). | 
| Jing Mike | 537412f | 2023-03-12 11:01:47 +0800 | [diff] [blame] | 9599 |     [[maybe_unused]] audio_channel_mask_t channelMask = | 
 | 9600 |                                 audio_channel_in_mask_from_count(mChannelCount); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9601 |  | 
 | 9602 |     AudioParameter param = AudioParameter(keyValuePair); | 
 | 9603 |     int value; | 
| Haynes Mathew George | 9ce67b5 | 2015-09-30 11:40:47 -0700 | [diff] [blame] | 9604 |  | 
 | 9605 |     // scope for AutoPark extends to end of method | 
 | 9606 |     AutoPark<FastCapture> park(mFastCapture); | 
 | 9607 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9608 |     // TODO Investigate when this code runs. Check with audio policy when a sample rate and | 
 | 9609 |     //      channel count change can be requested. Do we mandate the first client defines the | 
 | 9610 |     //      HAL sampling rate and channel count or do we allow changes on the fly? | 
 | 9611 |     if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { | 
 | 9612 |         samplingRate = value; | 
 | 9613 |         reconfig = true; | 
 | 9614 |     } | 
 | 9615 |     if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 9616 |         if (!audio_is_linear_pcm((audio_format_t) value)) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9617 |             status = BAD_VALUE; | 
 | 9618 |         } else { | 
 | 9619 |             reqFormat = (audio_format_t) value; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9620 |             reconfig = true; | 
 | 9621 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9622 |     } | 
 | 9623 |     if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { | 
 | 9624 |         audio_channel_mask_t mask = (audio_channel_mask_t) value; | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 9625 |         if (!audio_is_input_channel(mask) || | 
| Andy Hung | 936845a | 2021-06-08 00:09:06 -0700 | [diff] [blame] | 9626 |                 audio_channel_count_from_in_mask(mask) > FCC_LIMIT) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9627 |             status = BAD_VALUE; | 
 | 9628 |         } else { | 
 | 9629 |             channelMask = mask; | 
 | 9630 |             reconfig = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9631 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9632 |     } | 
 | 9633 |     if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { | 
 | 9634 |         // do not accept frame count changes if tracks are open as the track buffer | 
 | 9635 |         // size depends on frame count and correct behavior would not be guaranteed | 
 | 9636 |         // if frame count is changed after track creation | 
 | 9637 |         if (mActiveTracks.size() > 0) { | 
 | 9638 |             status = INVALID_OPERATION; | 
 | 9639 |         } else { | 
 | 9640 |             reconfig = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9641 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9642 |     } | 
 | 9643 |     if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 9644 |         LOG_FATAL("Should not set routing device in RecordThread"); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9645 |     } | 
 | 9646 |     if (param.getInt(String8(AudioParameter::keyInputSource), value) == NO_ERROR && | 
 | 9647 |             mAudioSource != (audio_source_t)value) { | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 9648 |         LOG_FATAL("Should not set audio source in RecordThread"); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9649 |     } | 
| Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 9650 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9651 |     if (status == NO_ERROR) { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 9652 |         status = mInput->stream->setParameters(keyValuePair); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9653 |         if (status == INVALID_OPERATION) { | 
 | 9654 |             inputStandBy(); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 9655 |             status = mInput->stream->setParameters(keyValuePair); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9656 |         } | 
 | 9657 |         if (reconfig) { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 9658 |             if (status == BAD_VALUE) { | 
| Mikhail Naganov | 560637e | 2021-03-31 22:40:13 +0000 | [diff] [blame] | 9659 |                 audio_config_base_t config = AUDIO_CONFIG_BASE_INITIALIZER; | 
 | 9660 |                 if (mInput->stream->getAudioProperties(&config) == OK && | 
 | 9661 |                         audio_is_linear_pcm(config.format) && audio_is_linear_pcm(reqFormat) && | 
 | 9662 |                         config.sample_rate <= (AUDIO_RESAMPLER_DOWN_RATIO_MAX * samplingRate) && | 
| Andy Hung | 936845a | 2021-06-08 00:09:06 -0700 | [diff] [blame] | 9663 |                         audio_channel_count_from_in_mask(config.channel_mask) <= FCC_LIMIT) { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 9664 |                     status = NO_ERROR; | 
 | 9665 |                 } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9666 |             } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9667 |             if (status == NO_ERROR) { | 
 | 9668 |                 readInputParameters_l(); | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 9669 |                 sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9670 |             } | 
 | 9671 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9672 |     } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9673 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9674 |     return reconfig; | 
 | 9675 | } | 
 | 9676 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9677 | String8 RecordThread::getParameters(const String8& keys) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9678 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9679 |     audio_utils::lock_guard _l(mutex()); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 9680 |     if (initCheck() == NO_ERROR) { | 
 | 9681 |         String8 out_s8; | 
 | 9682 |         if (mInput->stream->getParameters(keys, &out_s8) == OK) { | 
 | 9683 |             return out_s8; | 
 | 9684 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9685 |     } | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 9686 |     return {}; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9687 | } | 
 | 9688 |  | 
| Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 9689 | void RecordThread::ioConfigChanged_l(audio_io_config_event_t event, pid_t pid, | 
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 9690 |                                                  audio_port_handle_t portId) { | 
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 9691 |     sp<AudioIoDescriptor> desc; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9692 |     switch (event) { | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 9693 |     case AUDIO_INPUT_OPENED: | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 9694 |     case AUDIO_INPUT_REGISTERED: | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 9695 |     case AUDIO_INPUT_CONFIG_CHANGED: | 
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 9696 |         desc = sp<AudioIoDescriptor>::make(mId, mPatch, true /*isInput*/, | 
 | 9697 |                 mSampleRate, mFormat, mChannelMask, mFrameCount, mFrameCount); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9698 |         break; | 
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 9699 |     case AUDIO_CLIENT_STARTED: | 
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 9700 |         desc = sp<AudioIoDescriptor>::make(mId, mPatch, portId); | 
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 9701 |         break; | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 9702 |     case AUDIO_INPUT_CLOSED: | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9703 |     default: | 
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 9704 |         desc = sp<AudioIoDescriptor>::make(mId); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9705 |         break; | 
 | 9706 |     } | 
| Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 9707 |     mAfThreadCallback->ioConfigChanged_l(event, desc, pid); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9708 | } | 
 | 9709 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9710 | void RecordThread::readInputParameters_l() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9711 | { | 
| Dean Wheatley | 6c00951 | 2023-10-23 09:34:14 +1100 | [diff] [blame] | 9712 |     const audio_config_base_t audioConfig = mInput->getAudioProperties(); | 
 | 9713 |     mSampleRate = audioConfig.sample_rate; | 
 | 9714 |     mChannelMask = audioConfig.channel_mask; | 
 | 9715 |     if (!audio_is_input_channel(mChannelMask)) { | 
 | 9716 |         LOG_ALWAYS_FATAL("Channel mask %#x not valid for input", mChannelMask); | 
 | 9717 |     } | 
 | 9718 |  | 
| Mikhail Naganov | ce9f265 | 2018-07-16 11:09:24 -0700 | [diff] [blame] | 9719 |     mChannelCount = audio_channel_count_from_in_mask(mChannelMask); | 
| Dean Wheatley | 6c00951 | 2023-10-23 09:34:14 +1100 | [diff] [blame] | 9720 |  | 
 | 9721 |     // Get actual HAL format. | 
 | 9722 |     status_t result = mInput->stream->getAudioProperties(nullptr, nullptr, &mHALFormat); | 
 | 9723 |     LOG_ALWAYS_FATAL_IF(result != OK, "Error when retrieving input stream format: %d", result); | 
 | 9724 |     // Get format from the shim, which will be different than the HAL format | 
 | 9725 |     // if recording compressed audio from IEC61937 wrapped sources. | 
 | 9726 |     mFormat = audioConfig.format; | 
 | 9727 |     if (!audio_is_valid_format(mFormat)) { | 
 | 9728 |         LOG_ALWAYS_FATAL("Format %#x not valid for input", mFormat); | 
 | 9729 |     } | 
| Mikhail Naganov | ce9f265 | 2018-07-16 11:09:24 -0700 | [diff] [blame] | 9730 |     if (audio_is_linear_pcm(mFormat)) { | 
| Andy Hung | 936845a | 2021-06-08 00:09:06 -0700 | [diff] [blame] | 9731 |         LOG_ALWAYS_FATAL_IF(mChannelCount > FCC_LIMIT, "HAL channel count %d > %d", | 
 | 9732 |                 mChannelCount, FCC_LIMIT); | 
| Mikhail Naganov | ce9f265 | 2018-07-16 11:09:24 -0700 | [diff] [blame] | 9733 |     } else { | 
| Andy Hung | 936845a | 2021-06-08 00:09:06 -0700 | [diff] [blame] | 9734 |         // Can have more that FCC_LIMIT channels in encoded streams. | 
| Mikhail Naganov | ce9f265 | 2018-07-16 11:09:24 -0700 | [diff] [blame] | 9735 |         ALOGI("HAL format %#x is not linear pcm", mFormat); | 
 | 9736 |     } | 
| Dean Wheatley | 6c00951 | 2023-10-23 09:34:14 +1100 | [diff] [blame] | 9737 |     mFrameSize = mInput->getFrameSize(); | 
| Hayden Gomes | 1a89ab3 | 2020-06-12 11:05:47 -0700 | [diff] [blame] | 9738 |     LOG_ALWAYS_FATAL_IF(mFrameSize <= 0, "Error frame size was %zu but must be greater than zero", | 
 | 9739 |             mFrameSize); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 9740 |     result = mInput->stream->getBufferSize(&mBufferSize); | 
 | 9741 |     LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving buffer size from HAL: %d", result); | 
| Glenn Kasten | 548efc9 | 2012-11-29 08:48:51 -0800 | [diff] [blame] | 9742 |     mFrameCount = mBufferSize / mFrameSize; | 
| Hayden Gomes | 1a89ab3 | 2020-06-12 11:05:47 -0700 | [diff] [blame] | 9743 |     ALOGV("%p RecordThread params: mChannelCount=%u, mFormat=%#x, mFrameSize=%zu, " | 
 | 9744 |             "mBufferSize=%zu, mFrameCount=%zu", | 
 | 9745 |             this, mChannelCount, mFormat, mFrameSize, mBufferSize, mFrameCount); | 
| Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 9746 |  | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9747 |     // mRsmpInFrames must be 0 before calling resizeInputBuffer_l for the first time | 
 | 9748 |     mRsmpInFrames = 0; | 
| Eric Laurent | 5f0fd7b | 2021-05-07 16:33:26 +0200 | [diff] [blame] | 9749 |     resizeInputBuffer_l(0 /*maxSharedAudioHistoryMs*/); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9750 |  | 
| Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 9751 |     // AudioRecord mSampleRate and mChannelCount are constant due to AudioRecord API constraints. | 
 | 9752 |     // But if thread's mSampleRate or mChannelCount changes, how will that affect active tracks? | 
| Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 9753 |  | 
 | 9754 |     audio_input_flags_t flags = mInput->flags; | 
 | 9755 |     mediametrics::LogItem item(mThreadMetrics.getMetricsId()); | 
 | 9756 |     item.set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_READPARAMETERS) | 
| Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 9757 |         .set(AMEDIAMETRICS_PROP_ENCODING, IAfThreadBase::formatToString(mFormat).c_str()) | 
| Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 9758 |         .set(AMEDIAMETRICS_PROP_FLAGS, toString(flags).c_str()) | 
 | 9759 |         .set(AMEDIAMETRICS_PROP_SAMPLERATE, (int32_t)mSampleRate) | 
 | 9760 |         .set(AMEDIAMETRICS_PROP_CHANNELMASK, (int32_t)mChannelMask) | 
 | 9761 |         .set(AMEDIAMETRICS_PROP_CHANNELCOUNT, (int32_t)mChannelCount) | 
 | 9762 |         .set(AMEDIAMETRICS_PROP_FRAMECOUNT, (int32_t)mFrameCount) | 
 | 9763 |         .record(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9764 | } | 
 | 9765 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9766 | uint32_t RecordThread::getInputFramesLost() const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9767 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9768 |     audio_utils::lock_guard _l(mutex()); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 9769 |     uint32_t result; | 
 | 9770 |     if (initCheck() == NO_ERROR && mInput->stream->getInputFramesLost(&result) == OK) { | 
 | 9771 |         return result; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9772 |     } | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 9773 |     return 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9774 | } | 
 | 9775 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9776 | KeyedVector<audio_session_t, bool> RecordThread::sessionIds() const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9777 | { | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 9778 |     KeyedVector<audio_session_t, bool> ids; | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9779 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9780 |     for (size_t j = 0; j < mTracks.size(); ++j) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9781 |         sp<IAfRecordTrack> track = mTracks[j]; | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 9782 |         audio_session_t sessionId = track->sessionId(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9783 |         if (ids.indexOfKey(sessionId) < 0) { | 
 | 9784 |             ids.add(sessionId, true); | 
 | 9785 |         } | 
 | 9786 |     } | 
 | 9787 |     return ids; | 
 | 9788 | } | 
 | 9789 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9790 | AudioStreamIn* RecordThread::clearInput() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9791 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9792 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9793 |     AudioStreamIn *input = mInput; | 
 | 9794 |     mInput = NULL; | 
| Mikhail Naganov | 49aecf0 | 2023-09-08 15:14:34 -0700 | [diff] [blame] | 9795 |     mInputSource.clear(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9796 |     return input; | 
 | 9797 | } | 
 | 9798 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 9799 | // this method must always be called either with ThreadBase mutex() held or inside the thread loop | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9800 | sp<StreamHalInterface> RecordThread::stream() const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9801 | { | 
 | 9802 |     if (mInput == NULL) { | 
 | 9803 |         return NULL; | 
 | 9804 |     } | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 9805 |     return mInput->stream; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9806 | } | 
 | 9807 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9808 | status_t RecordThread::addEffectChain_l(const sp<IAfEffectChain>& chain) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9809 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9810 |     ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 9811 |     chain->setThread(this); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9812 |     chain->setInBuffer(NULL); | 
 | 9813 |     chain->setOutBuffer(NULL); | 
 | 9814 |  | 
 | 9815 |     checkSuspendOnAddEffectChain_l(chain); | 
 | 9816 |  | 
| Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 9817 |     // make sure enabled pre processing effects state is communicated to the HAL as we | 
 | 9818 |     // just moved them to a new input stream. | 
| Shunkai Yao | d125e40 | 2024-01-20 03:19:06 +0000 | [diff] [blame] | 9819 |     chain->syncHalEffectsState_l(); | 
| Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 9820 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9821 |     mEffectChains.add(chain); | 
 | 9822 |  | 
 | 9823 |     return NO_ERROR; | 
 | 9824 | } | 
 | 9825 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9826 | size_t RecordThread::removeEffectChain_l(const sp<IAfEffectChain>& chain) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9827 | { | 
 | 9828 |     ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); | 
| Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 9829 |  | 
 | 9830 |     for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 9831 |         if (chain == mEffectChains[i]) { | 
 | 9832 |             mEffectChains.removeAt(i); | 
 | 9833 |             break; | 
 | 9834 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9835 |     } | 
| Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 9836 |     return mEffectChains.size(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9837 | } | 
 | 9838 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9839 | status_t RecordThread::createAudioPatch_l(const struct audio_patch* patch, | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 9840 |                                                           audio_patch_handle_t *handle) | 
 | 9841 | { | 
 | 9842 |     status_t status = NO_ERROR; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 9843 |  | 
 | 9844 |     // store new device and send to effects | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 9845 |     mInDeviceTypeAddr.mType = patch->sources[0].ext.device.type; | 
| jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 9846 |     mInDeviceTypeAddr.setAddress(patch->sources[0].ext.device.address); | 
| François Gaffie | 0c280aa | 2018-07-25 10:02:15 +0200 | [diff] [blame] | 9847 |     audio_port_handle_t deviceId = patch->sources[0].id; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 9848 |     for (size_t i = 0; i < mEffectChains.size(); i++) { | 
| jiabin | 8f278ee | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 9849 |         mEffectChains[i]->setInputDevice_l(inDeviceTypeAddr()); | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 9850 |     } | 
 | 9851 |  | 
| Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 9852 |     checkBtNrec_l(); | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 9853 |  | 
 | 9854 |     // store new source and send to effects | 
 | 9855 |     if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) { | 
 | 9856 |         mAudioSource = patch->sinks[0].ext.mix.usecase.source; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 9857 |         for (size_t i = 0; i < mEffectChains.size(); i++) { | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 9858 |             mEffectChains[i]->setAudioSource_l(mAudioSource); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 9859 |         } | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 9860 |     } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 9861 |  | 
| Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 9862 |     if (mInput->audioHwDev->supportsAudioPatches()) { | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 9863 |         sp<DeviceHalInterface> hwDevice = mInput->audioHwDev->hwDevice(); | 
 | 9864 |         status = hwDevice->createAudioPatch(patch->num_sources, | 
 | 9865 |                                             patch->sources, | 
 | 9866 |                                             patch->num_sinks, | 
 | 9867 |                                             patch->sinks, | 
 | 9868 |                                             handle); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 9869 |     } else { | 
| Ytai Ben-Tsvi | f997ffe | 2022-02-03 16:38:16 -0800 | [diff] [blame] | 9870 |         status = mInput->stream->legacyCreateAudioPatch(patch->sources[0], | 
 | 9871 |                                                         patch->sinks[0].ext.mix.usecase.source, | 
 | 9872 |                                                         patch->sources[0].ext.device.type); | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 9873 |         *handle = AUDIO_PATCH_HANDLE_NONE; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 9874 |     } | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 9875 |  | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 9876 |     if ((mPatch.num_sources == 0) || (mPatch.sources[0].id != deviceId)) { | 
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 9877 |         sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 9878 |         mPatch = *patch; | 
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 9879 |     } | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 9880 |  | 
| Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 9881 |     const std::string pathSourcesAsString = patchSourcesToString(patch); | 
| Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 9882 |     mThreadMetrics.logEndInterval(); | 
| Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 9883 |     mThreadMetrics.logCreatePatch(pathSourcesAsString, /* outDevices */ {}); | 
| Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 9884 |     mThreadMetrics.logBeginInterval(); | 
| Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 9885 |     // also dispatch to active AudioRecords | 
 | 9886 |     for (const auto &track : mActiveTracks) { | 
 | 9887 |         track->logEndInterval(); | 
 | 9888 |         track->logBeginInterval(pathSourcesAsString); | 
 | 9889 |     } | 
| Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 9890 |     // Force meteadata update after a route change | 
 | 9891 |     mActiveTracks.setHasChanged(); | 
 | 9892 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 9893 |     return status; | 
 | 9894 | } | 
 | 9895 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9896 | status_t RecordThread::releaseAudioPatch_l(const audio_patch_handle_t handle) | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 9897 | { | 
 | 9898 |     status_t status = NO_ERROR; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 9899 |  | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 9900 |     mPatch = audio_patch{}; | 
 | 9901 |     mInDeviceTypeAddr.reset(); | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 9902 |  | 
| Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 9903 |     if (mInput->audioHwDev->supportsAudioPatches()) { | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 9904 |         sp<DeviceHalInterface> hwDevice = mInput->audioHwDev->hwDevice(); | 
 | 9905 |         status = hwDevice->releaseAudioPatch(handle); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 9906 |     } else { | 
| Ytai Ben-Tsvi | f997ffe | 2022-02-03 16:38:16 -0800 | [diff] [blame] | 9907 |         status = mInput->stream->legacyReleaseAudioPatch(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 9908 |     } | 
| Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 9909 |     // Force meteadata update after a route change | 
 | 9910 |     mActiveTracks.setHasChanged(); | 
 | 9911 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 9912 |     return status; | 
 | 9913 | } | 
 | 9914 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9915 | void RecordThread::updateOutDevices(const DeviceDescriptorBaseVector& outDevices) | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 9916 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9917 |     audio_utils::lock_guard _l(mutex()); | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 9918 |     mOutDevices = outDevices; | 
 | 9919 |     mOutDeviceTypeAddrs = deviceTypeAddrsFromDescriptors(mOutDevices); | 
 | 9920 |     for (size_t i = 0; i < mEffectChains.size(); i++) { | 
| jiabin | 8f278ee | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 9921 |         mEffectChains[i]->setDevices_l(outDeviceTypeAddrs()); | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 9922 |     } | 
 | 9923 | } | 
 | 9924 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9925 | int32_t RecordThread::getOldestFront_l() | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9926 | { | 
 | 9927 |     if (mTracks.size() == 0) { | 
| Eric Laurent | 92d0a32 | 2021-07-16 15:32:33 +0200 | [diff] [blame] | 9928 |         return mRsmpInRear; | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9929 |     } | 
| Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 9930 |     int32_t oldestFront = mRsmpInRear; | 
 | 9931 |     int32_t maxFilled = 0; | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9932 |     for (size_t i = 0; i < mTracks.size(); i++) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9933 |         int32_t front = mTracks[i]->resamplerBufferProvider()->getFront(); | 
| Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 9934 |         int32_t filled; | 
| Eric Laurent | 92d0a32 | 2021-07-16 15:32:33 +0200 | [diff] [blame] | 9935 |         (void)__builtin_sub_overflow(mRsmpInRear, front, &filled); | 
| Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 9936 |         if (filled > maxFilled) { | 
 | 9937 |             oldestFront = front; | 
 | 9938 |             maxFilled = filled; | 
 | 9939 |         } | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9940 |     } | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 9941 |     if (maxFilled > static_cast<signed>(mRsmpInFrames)) { | 
| Eric Laurent | 92d0a32 | 2021-07-16 15:32:33 +0200 | [diff] [blame] | 9942 |         (void)__builtin_sub_overflow(mRsmpInRear, mRsmpInFrames, &oldestFront); | 
 | 9943 |     } | 
| Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 9944 |     return oldestFront; | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9945 | } | 
 | 9946 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9947 | void RecordThread::updateFronts_l(int32_t offset) | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9948 | { | 
 | 9949 |     if (offset == 0) { | 
 | 9950 |         return; | 
 | 9951 |     } | 
 | 9952 |     for (size_t i = 0; i < mTracks.size(); i++) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9953 |         int32_t front = mTracks[i]->resamplerBufferProvider()->getFront(); | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9954 |         front = audio_utils::safe_sub_overflow(front, offset); | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9955 |         mTracks[i]->resamplerBufferProvider()->setFront(front); | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9956 |     } | 
 | 9957 | } | 
 | 9958 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9959 | void RecordThread::resizeInputBuffer_l(int32_t maxSharedAudioHistoryMs) | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9960 | { | 
 | 9961 |     // This is the formula for calculating the temporary buffer size. | 
 | 9962 |     // With 7 HAL buffers, we can guarantee ability to down-sample the input by ratio of 6:1 to | 
 | 9963 |     // 1 full output buffer, regardless of the alignment of the available input. | 
 | 9964 |     // The value is somewhat arbitrary, and could probably be even larger. | 
 | 9965 |     // A larger value should allow more old data to be read after a track calls start(), | 
 | 9966 |     // without increasing latency. | 
 | 9967 |     // | 
 | 9968 |     // Note this is independent of the maximum downsampling ratio permitted for capture. | 
 | 9969 |     size_t minRsmpInFrames = mFrameCount * 7; | 
 | 9970 |  | 
 | 9971 |     // maxSharedAudioHistoryMs != 0 indicates a request to possibly make some part of the audio | 
 | 9972 |     // capture history available to another client using the same session ID: | 
 | 9973 |     // dimension the resampler input buffer accordingly. | 
 | 9974 |  | 
 | 9975 |     // Get oldest client read position:  getOldestFront_l() must be called before altering | 
 | 9976 |     // mRsmpInRear, or mRsmpInFrames | 
 | 9977 |     int32_t previousFront = getOldestFront_l(); | 
 | 9978 |     size_t previousRsmpInFramesP2 = mRsmpInFramesP2; | 
 | 9979 |     int32_t previousRear = mRsmpInRear; | 
 | 9980 |     mRsmpInRear = 0; | 
 | 9981 |  | 
| Eric Laurent | 5f0fd7b | 2021-05-07 16:33:26 +0200 | [diff] [blame] | 9982 |     ALOG_ASSERT(maxSharedAudioHistoryMs >= 0 | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9983 |             && maxSharedAudioHistoryMs <= kMaxSharedAudioHistoryMs, | 
| Eric Laurent | 5f0fd7b | 2021-05-07 16:33:26 +0200 | [diff] [blame] | 9984 |             "resizeInputBuffer_l() called with invalid max shared history %d", | 
 | 9985 |             maxSharedAudioHistoryMs); | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9986 |     if (maxSharedAudioHistoryMs != 0) { | 
 | 9987 |         // resizeInputBuffer_l should never be called with a non zero shared history if the | 
 | 9988 |         // buffer was not already allocated | 
 | 9989 |         ALOG_ASSERT(mRsmpInBuffer != nullptr && mRsmpInFrames != 0, | 
 | 9990 |                 "resizeInputBuffer_l() called with shared history and unallocated buffer"); | 
 | 9991 |         size_t rsmpInFrames = (size_t)maxSharedAudioHistoryMs * mSampleRate / 1000; | 
 | 9992 |         // never reduce resampler input buffer size | 
| Eric Laurent | 92d0a32 | 2021-07-16 15:32:33 +0200 | [diff] [blame] | 9993 |         if (rsmpInFrames <= mRsmpInFrames) { | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9994 |             return; | 
 | 9995 |         } | 
 | 9996 |         mRsmpInFrames = rsmpInFrames; | 
 | 9997 |     } | 
| Eric Laurent | 5f0fd7b | 2021-05-07 16:33:26 +0200 | [diff] [blame] | 9998 |     mMaxSharedAudioHistoryMs = maxSharedAudioHistoryMs; | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9999 |     // Note: mRsmpInFrames is 0 when called with maxSharedAudioHistoryMs equals to 0 so it is always | 
 | 10000 |     // initialized | 
 | 10001 |     if (mRsmpInFrames < minRsmpInFrames) { | 
 | 10002 |         mRsmpInFrames = minRsmpInFrames; | 
 | 10003 |     } | 
 | 10004 |     mRsmpInFramesP2 = roundup(mRsmpInFrames); | 
 | 10005 |  | 
 | 10006 |     // TODO optimize audio capture buffer sizes ... | 
 | 10007 |     // Here we calculate the size of the sliding buffer used as a source | 
 | 10008 |     // for resampling.  mRsmpInFramesP2 is currently roundup(mFrameCount * 7). | 
 | 10009 |     // For current HAL frame counts, this is usually 2048 = 40 ms.  It would | 
 | 10010 |     // be better to have it derived from the pipe depth in the long term. | 
 | 10011 |     // The current value is higher than necessary.  However it should not add to latency. | 
 | 10012 |  | 
 | 10013 |     // Over-allocate beyond mRsmpInFramesP2 to permit a HAL read past end of buffer | 
 | 10014 |     mRsmpInFramesOA = mRsmpInFramesP2 + mFrameCount - 1; | 
 | 10015 |  | 
 | 10016 |     void *rsmpInBuffer; | 
 | 10017 |     (void)posix_memalign(&rsmpInBuffer, 32, mRsmpInFramesOA * mFrameSize); | 
 | 10018 |     // if posix_memalign fails, will segv here. | 
 | 10019 |     memset(rsmpInBuffer, 0, mRsmpInFramesOA * mFrameSize); | 
 | 10020 |  | 
 | 10021 |     // Copy audio history if any from old buffer before freeing it | 
 | 10022 |     if (previousRear != 0) { | 
 | 10023 |         ALOG_ASSERT(mRsmpInBuffer != nullptr, | 
 | 10024 |                 "resizeInputBuffer_l() called with null buffer but frames already read from HAL"); | 
 | 10025 |  | 
 | 10026 |         ssize_t unread = audio_utils::safe_sub_overflow(previousRear, previousFront); | 
 | 10027 |         previousFront &= previousRsmpInFramesP2 - 1; | 
 | 10028 |         size_t part1 = previousRsmpInFramesP2 - previousFront; | 
 | 10029 |         if (part1 > (size_t) unread) { | 
 | 10030 |             part1 = unread; | 
 | 10031 |         } | 
 | 10032 |         if (part1 != 0) { | 
 | 10033 |             memcpy(rsmpInBuffer, (const uint8_t*)mRsmpInBuffer + previousFront * mFrameSize, | 
 | 10034 |                    part1 * mFrameSize); | 
 | 10035 |             mRsmpInRear = part1; | 
 | 10036 |             part1 = unread - part1; | 
 | 10037 |             if (part1 != 0) { | 
 | 10038 |                 memcpy((uint8_t*)rsmpInBuffer + mRsmpInRear * mFrameSize, | 
 | 10039 |                        (const uint8_t*)mRsmpInBuffer, part1 * mFrameSize); | 
 | 10040 |                 mRsmpInRear += part1; | 
 | 10041 |             } | 
 | 10042 |         } | 
 | 10043 |         // Update front for all clients according to new rear | 
 | 10044 |         updateFronts_l(audio_utils::safe_sub_overflow(previousRear, mRsmpInRear)); | 
 | 10045 |     } else { | 
 | 10046 |         mRsmpInRear = 0; | 
 | 10047 |     } | 
 | 10048 |     free(mRsmpInBuffer); | 
 | 10049 |     mRsmpInBuffer = rsmpInBuffer; | 
 | 10050 | } | 
 | 10051 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10052 | void RecordThread::addPatchTrack(const sp<IAfPatchRecord>& record) | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 10053 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 10054 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 10055 |     mTracks.add(record); | 
| Mikhail Naganov | 2534b38 | 2019-09-25 13:05:02 -0700 | [diff] [blame] | 10056 |     if (record->getSource()) { | 
 | 10057 |         mSource = record->getSource(); | 
 | 10058 |     } | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 10059 | } | 
 | 10060 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10061 | void RecordThread::deletePatchTrack(const sp<IAfPatchRecord>& record) | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 10062 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 10063 |     audio_utils::lock_guard _l(mutex()); | 
| Mikhail Naganov | 2534b38 | 2019-09-25 13:05:02 -0700 | [diff] [blame] | 10064 |     if (mSource == record->getSource()) { | 
 | 10065 |         mSource = mInput; | 
 | 10066 |     } | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 10067 |     destroyTrack_l(record); | 
 | 10068 | } | 
 | 10069 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10070 | void RecordThread::toAudioPortConfig(struct audio_port_config* config) | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 10071 | { | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 10072 |     ThreadBase::toAudioPortConfig(config); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 10073 |     config->role = AUDIO_PORT_ROLE_SINK; | 
 | 10074 |     config->ext.mix.hw_module = mInput->audioHwDev->handle(); | 
 | 10075 |     config->ext.mix.usecase.source = mAudioSource; | 
| Mikhail Naganov | 32abc2b | 2018-05-24 12:57:11 -0700 | [diff] [blame] | 10076 |     if (mInput && mInput->flags != AUDIO_INPUT_FLAG_NONE) { | 
 | 10077 |         config->config_mask |= AUDIO_PORT_CONFIG_FLAGS; | 
 | 10078 |         config->flags.input = mInput->flags; | 
 | 10079 |     } | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 10080 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 10081 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10082 | // ---------------------------------------------------------------------------- | 
 | 10083 | //      Mmap | 
 | 10084 | // ---------------------------------------------------------------------------- | 
 | 10085 |  | 
| Andy Hung | 765de28 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 10086 | // Mmap stream control interface implementation. Each MmapThreadHandle controls one | 
 | 10087 | // MmapPlaybackThread or MmapCaptureThread instance. | 
 | 10088 | class MmapThreadHandle : public MmapStreamInterface { | 
 | 10089 | public: | 
 | 10090 |     explicit MmapThreadHandle(const sp<IAfMmapThread>& thread); | 
 | 10091 |     ~MmapThreadHandle() override; | 
 | 10092 |  | 
 | 10093 |     // MmapStreamInterface virtuals | 
 | 10094 |     status_t createMmapBuffer(int32_t minSizeFrames, | 
 | 10095 |         struct audio_mmap_buffer_info* info) final; | 
 | 10096 |     status_t getMmapPosition(struct audio_mmap_position* position) final; | 
 | 10097 |     status_t getExternalPosition(uint64_t* position, int64_t* timeNanos) final; | 
 | 10098 |     status_t start(const AudioClient& client, | 
 | 10099 |            const audio_attributes_t* attr, audio_port_handle_t* handle) final; | 
 | 10100 |     status_t stop(audio_port_handle_t handle) final; | 
 | 10101 |     status_t standby() final; | 
 | 10102 |     status_t reportData(const void* buffer, size_t frameCount) final; | 
 | 10103 | private: | 
 | 10104 |     const sp<IAfMmapThread> mThread; | 
 | 10105 | }; | 
 | 10106 |  | 
 | 10107 | /* static */ | 
 | 10108 | sp<MmapStreamInterface> IAfMmapThread::createMmapStreamInterfaceAdapter( | 
 | 10109 |         const sp<IAfMmapThread>& mmapThread) { | 
 | 10110 |     return sp<MmapThreadHandle>::make(mmapThread); | 
 | 10111 | } | 
 | 10112 |  | 
 | 10113 | MmapThreadHandle::MmapThreadHandle(const sp<IAfMmapThread>& thread) | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10114 |     : mThread(thread) | 
 | 10115 | { | 
| Phil Burk | 9fabbf8 | 2017-08-03 12:02:00 -0700 | [diff] [blame] | 10116 |     assert(thread != 0); // thread must start non-null and stay non-null | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10117 | } | 
 | 10118 |  | 
| Andy Hung | 765de28 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 10119 | // MmapStreamInterface could be directly implemented by MmapThread excepting this | 
 | 10120 | // special handling on adapter dtor. | 
 | 10121 | MmapThreadHandle::~MmapThreadHandle() | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10122 | { | 
| Phil Burk | 9fabbf8 | 2017-08-03 12:02:00 -0700 | [diff] [blame] | 10123 |     mThread->disconnect(); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10124 | } | 
 | 10125 |  | 
| Andy Hung | 765de28 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 10126 | status_t MmapThreadHandle::createMmapBuffer(int32_t minSizeFrames, | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10127 |                                   struct audio_mmap_buffer_info *info) | 
 | 10128 | { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10129 |     return mThread->createMmapBuffer(minSizeFrames, info); | 
 | 10130 | } | 
 | 10131 |  | 
| Andy Hung | 765de28 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 10132 | status_t MmapThreadHandle::getMmapPosition(struct audio_mmap_position* position) | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10133 | { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10134 |     return mThread->getMmapPosition(position); | 
 | 10135 | } | 
 | 10136 |  | 
| Andy Hung | 765de28 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 10137 | status_t MmapThreadHandle::getExternalPosition(uint64_t* position, | 
| jiabin | b7d8c5a | 2020-08-26 17:24:52 -0700 | [diff] [blame] | 10138 |                                                              int64_t *timeNanos) { | 
 | 10139 |     return mThread->getExternalPosition(position, timeNanos); | 
 | 10140 | } | 
 | 10141 |  | 
| Andy Hung | 765de28 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 10142 | status_t MmapThreadHandle::start(const AudioClient& client, | 
| jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 10143 |         const audio_attributes_t *attr, audio_port_handle_t *handle) | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10144 | { | 
| jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 10145 |     return mThread->start(client, attr, handle); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10146 | } | 
 | 10147 |  | 
| Andy Hung | 765de28 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 10148 | status_t MmapThreadHandle::stop(audio_port_handle_t handle) | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10149 | { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10150 |     return mThread->stop(handle); | 
 | 10151 | } | 
 | 10152 |  | 
| Andy Hung | 765de28 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 10153 | status_t MmapThreadHandle::standby() | 
| Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 10154 | { | 
| Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 10155 |     return mThread->standby(); | 
 | 10156 | } | 
 | 10157 |  | 
| Andy Hung | 765de28 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 10158 | status_t MmapThreadHandle::reportData(const void* buffer, size_t frameCount) | 
 | 10159 | { | 
| jiabin | fc791ee | 2023-02-15 19:43:40 +0000 | [diff] [blame] | 10160 |     return mThread->reportData(buffer, frameCount); | 
 | 10161 | } | 
 | 10162 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10163 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10164 | MmapThread::MmapThread( | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 10165 |         const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id, | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 10166 |         AudioHwDevice *hwDev, const sp<StreamHalInterface>& stream, bool systemReady, bool isOut) | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 10167 |     : ThreadBase(afThreadCallback, id, (isOut ? MMAP_PLAYBACK : MMAP_CAPTURE), systemReady, isOut), | 
| Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 10168 |       mSessionId(AUDIO_SESSION_NONE), | 
| François Gaffie | 0c280aa | 2018-07-25 10:02:15 +0200 | [diff] [blame] | 10169 |       mPortId(AUDIO_PORT_HANDLE_NONE), | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 10170 |       mHalStream(stream), mHalDevice(hwDev->hwDevice()), mAudioHwDev(hwDev), | 
| Eric Laurent | 67f9729 | 2018-04-20 18:05:41 -0700 | [diff] [blame] | 10171 |       mActiveTracks(&this->mLocalLog), | 
 | 10172 |       mHalVolFloat(-1.0f), // Initialize to illegal value so it always gets set properly later. | 
 | 10173 |       mNoCallbackWarningCount(0) | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10174 | { | 
| Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 10175 |     mStandby = true; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10176 |     readHalParameters_l(); | 
 | 10177 | } | 
 | 10178 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10179 | void MmapThread::onFirstRef() | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10180 | { | 
 | 10181 |     run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO); | 
 | 10182 | } | 
 | 10183 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10184 | void MmapThread::disconnect() | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10185 | { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 10186 |     ActiveTracks<IAfMmapTrack> activeTracks; | 
| Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10187 |     audio_port_handle_t localPortId; | 
| Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 10188 |     { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 10189 |         audio_utils::lock_guard _l(mutex()); | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 10190 |         for (const sp<IAfMmapTrack>& t : mActiveTracks) { | 
| Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 10191 |             activeTracks.add(t); | 
 | 10192 |         } | 
| Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10193 |         localPortId = mPortId; | 
| Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 10194 |     } | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 10195 |     for (const sp<IAfMmapTrack>& t : activeTracks) { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10196 |         stop(t->portId()); | 
 | 10197 |     } | 
| Phil Burk | 9fabbf8 | 2017-08-03 12:02:00 -0700 | [diff] [blame] | 10198 |     // This will decrement references and may cause the destruction of this thread. | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10199 |     if (isOutput()) { | 
| Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10200 |         AudioSystem::releaseOutput(localPortId); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10201 |     } else { | 
| Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10202 |         AudioSystem::releaseInput(localPortId); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10203 |     } | 
 | 10204 | } | 
 | 10205 |  | 
 | 10206 |  | 
| Andy Hung | 160664b | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 10207 | void MmapThread::configure_l(const audio_attributes_t* attr, | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10208 |                                                 audio_stream_type_t streamType __unused, | 
 | 10209 |                                                 audio_session_t sessionId, | 
 | 10210 |                                                 const sp<MmapStreamCallback>& callback, | 
| Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 10211 |                                                 audio_port_handle_t deviceId, | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10212 |                                                 audio_port_handle_t portId) | 
 | 10213 | { | 
 | 10214 |     mAttr = *attr; | 
 | 10215 |     mSessionId = sessionId; | 
 | 10216 |     mCallback = callback; | 
| Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 10217 |     mDeviceId = deviceId; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10218 |     mPortId = portId; | 
 | 10219 | } | 
 | 10220 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10221 | status_t MmapThread::createMmapBuffer(int32_t minSizeFrames, | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10222 |                                   struct audio_mmap_buffer_info *info) | 
 | 10223 | { | 
| Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10224 |     audio_utils::lock_guard l(mutex()); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10225 |     if (mHalStream == 0) { | 
 | 10226 |         return NO_INIT; | 
 | 10227 |     } | 
| Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 10228 |     mStandby = true; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10229 |     return mHalStream->createMmapBuffer(minSizeFrames, info); | 
 | 10230 | } | 
 | 10231 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10232 | status_t MmapThread::getMmapPosition(struct audio_mmap_position* position) const | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10233 | { | 
| Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10234 |     audio_utils::lock_guard l(mutex()); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10235 |     if (mHalStream == 0) { | 
 | 10236 |         return NO_INIT; | 
 | 10237 |     } | 
 | 10238 |     return mHalStream->getMmapPosition(position); | 
 | 10239 | } | 
 | 10240 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10241 | status_t MmapThread::exitStandby_l() | 
| Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 10242 | { | 
| Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 10243 |     // The HAL must receive track metadata before starting the stream | 
 | 10244 |     updateMetadata_l(); | 
| Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 10245 |     status_t ret = mHalStream->start(); | 
 | 10246 |     if (ret != NO_ERROR) { | 
 | 10247 |         ALOGE("%s: error mHalStream->start() = %d for first track", __FUNCTION__, ret); | 
 | 10248 |         return ret; | 
 | 10249 |     } | 
| Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 10250 |     if (mStandby) { | 
 | 10251 |         mThreadMetrics.logBeginInterval(); | 
| Andy Hung | 44d648b | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 10252 |         mThreadSnapshot.onBegin(); | 
| Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 10253 |         mStandby = false; | 
 | 10254 |     } | 
| Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 10255 |     return NO_ERROR; | 
 | 10256 | } | 
 | 10257 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10258 | status_t MmapThread::start(const AudioClient& client, | 
| jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 10259 |                                          const audio_attributes_t *attr, | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10260 |                                          audio_port_handle_t *handle) | 
 | 10261 | { | 
| Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10262 |     audio_utils::lock_guard l(mutex()); | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10263 |     ALOGV("%s clientUid %d mStandby %d mPortId %d *handle %d", __FUNCTION__, | 
| Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 10264 |           client.attributionSource.uid, mStandby, mPortId, *handle); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10265 |     if (mHalStream == 0) { | 
 | 10266 |         return NO_INIT; | 
 | 10267 |     } | 
 | 10268 |  | 
 | 10269 |     status_t ret; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10270 |  | 
| Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 10271 |     // For the first track, reuse portId and session allocated when the stream was opened. | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10272 |     if (*handle == mPortId) { | 
| Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10273 |         acquireWakeLock_l(); | 
| Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 10274 |         return NO_ERROR; | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10275 |     } | 
 | 10276 |  | 
 | 10277 |     audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; | 
 | 10278 |  | 
 | 10279 |     audio_io_handle_t io = mId; | 
| Andy Hung | c3af011 | 2023-07-19 16:56:19 -0700 | [diff] [blame] | 10280 |     const AttributionSourceState adjAttributionSource = afutils::checkAttributionSourcePackage( | 
| Atneya Nair | f59db5c | 2023-05-10 21:37:41 -0700 | [diff] [blame] | 10281 |             client.attributionSource); | 
 | 10282 |  | 
| Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10283 |     const auto localSessionId = mSessionId; | 
 | 10284 |     auto localAttr = mAttr; | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10285 |     if (isOutput()) { | 
 | 10286 |         audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
 | 10287 |         config.sample_rate = mSampleRate; | 
 | 10288 |         config.channel_mask = mChannelMask; | 
 | 10289 |         config.format = mFormat; | 
| Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10290 |         audio_stream_type_t stream = streamType_l(); | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10291 |         audio_output_flags_t flags = | 
 | 10292 |                 (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_MMAP_NOIRQ | AUDIO_OUTPUT_FLAG_DIRECT); | 
| Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 10293 |         audio_port_handle_t deviceId = mDeviceId; | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 10294 |         std::vector<audio_io_handle_t> secondaryOutputs; | 
| Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 10295 |         bool isSpatialized; | 
| jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 10296 |         bool isBitPerfect; | 
| Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10297 |         mutex().unlock(); | 
 | 10298 |         ret = AudioSystem::getOutputForAttr(&localAttr, &io, | 
 | 10299 |                                             localSessionId, | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10300 |                                             &stream, | 
| Atneya Nair | f59db5c | 2023-05-10 21:37:41 -0700 | [diff] [blame] | 10301 |                                             adjAttributionSource, | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10302 |                                             &config, | 
 | 10303 |                                             flags, | 
 | 10304 |                                             &deviceId, | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 10305 |                                             &portId, | 
| Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 10306 |                                             &secondaryOutputs, | 
| jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 10307 |                                             &isSpatialized, | 
 | 10308 |                                             &isBitPerfect); | 
| Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10309 |         mutex().lock(); | 
 | 10310 |         mAttr = localAttr; | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 10311 |         ALOGD_IF(!secondaryOutputs.empty(), | 
 | 10312 |                  "MmapThread::start does not support secondary outputs, ignoring them"); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10313 |     } else { | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10314 |         audio_config_base_t config; | 
 | 10315 |         config.sample_rate = mSampleRate; | 
 | 10316 |         config.channel_mask = mChannelMask; | 
 | 10317 |         config.format = mFormat; | 
| Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 10318 |         audio_port_handle_t deviceId = mDeviceId; | 
| Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10319 |         mutex().unlock(); | 
 | 10320 |         ret = AudioSystem::getInputForAttr(&localAttr, &io, | 
| Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 10321 |                                               RECORD_RIID_INVALID, | 
| Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10322 |                                               localSessionId, | 
| Atneya Nair | f59db5c | 2023-05-10 21:37:41 -0700 | [diff] [blame] | 10323 |                                               adjAttributionSource, | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10324 |                                               &config, | 
 | 10325 |                                               AUDIO_INPUT_FLAG_MMAP_NOIRQ, | 
 | 10326 |                                               &deviceId, | 
 | 10327 |                                               &portId); | 
| Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10328 |         mutex().lock(); | 
 | 10329 |         // localAttr is const for getInputForAttr. | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10330 |     } | 
 | 10331 |     // APM should not chose a different input or output stream for the same set of attributes | 
 | 10332 |     // and audo configuration | 
 | 10333 |     if (ret != NO_ERROR || io != mId) { | 
 | 10334 |         ALOGE("%s: error getting output or input from APM (error %d, io %d expected io %d)", | 
 | 10335 |               __FUNCTION__, ret, io, mId); | 
 | 10336 |         return BAD_VALUE; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10337 |     } | 
 | 10338 |  | 
 | 10339 |     if (isOutput()) { | 
| Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10340 |         mutex().unlock(); | 
| Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 10341 |         ret = AudioSystem::startOutput(portId); | 
| Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10342 |         mutex().lock(); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10343 |     } else { | 
| jiabin | 0960903 | 2022-06-15 19:26:01 +0000 | [diff] [blame] | 10344 |         { | 
 | 10345 |             // Add the track record before starting input so that the silent status for the | 
 | 10346 |             // client can be cached. | 
| jiabin | 0960903 | 2022-06-15 19:26:01 +0000 | [diff] [blame] | 10347 |             setClientSilencedState_l(portId, false /*silenced*/); | 
 | 10348 |         } | 
| Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10349 |         mutex().unlock(); | 
| Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 10350 |         ret = AudioSystem::startInput(portId); | 
| Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10351 |         mutex().lock(); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10352 |     } | 
 | 10353 |  | 
 | 10354 |     // abort if start is rejected by audio policy manager | 
 | 10355 |     if (ret != NO_ERROR) { | 
| Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 10356 |         ALOGE("%s: error start rejected by AudioPolicyManager = %d", __FUNCTION__, ret); | 
| Eric Tan | 39ec8d6 | 2018-07-24 09:49:29 -0700 | [diff] [blame] | 10357 |         if (!mActiveTracks.isEmpty()) { | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 10358 |             mutex().unlock(); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10359 |             if (isOutput()) { | 
| Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 10360 |                 AudioSystem::releaseOutput(portId); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10361 |             } else { | 
| Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 10362 |                 AudioSystem::releaseInput(portId); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10363 |             } | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 10364 |             mutex().lock(); | 
| Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 10365 |         } else { | 
 | 10366 |             mHalStream->stop(); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10367 |         } | 
| jiabin | 0960903 | 2022-06-15 19:26:01 +0000 | [diff] [blame] | 10368 |         eraseClientSilencedState_l(portId); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10369 |         return PERMISSION_DENIED; | 
 | 10370 |     } | 
 | 10371 |  | 
| Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 10372 |     // Given that MmapThread::mAttr is mutable, should a MmapTrack have attributes ? | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 10373 |     sp<IAfMmapTrack> track = IAfMmapTrack::create( | 
 | 10374 |             this, attr == nullptr ? mAttr : *attr, mSampleRate, mFormat, | 
| Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 10375 |                                         mChannelMask, mSessionId, isOutput(), | 
 | 10376 |                                         client.attributionSource, | 
| Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 10377 |                                         IPCThreadState::self()->getCallingPid(), portId); | 
| jiabin | 0960903 | 2022-06-15 19:26:01 +0000 | [diff] [blame] | 10378 |     if (!isOutput()) { | 
 | 10379 |         track->setSilenced_l(isClientSilenced_l(portId)); | 
 | 10380 |     } | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10381 |  | 
| Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 10382 |     if (isOutput()) { | 
 | 10383 |         // force volume update when a new track is added | 
 | 10384 |         mHalVolFloat = -1.0f; | 
 | 10385 |     } else if (!track->isSilenced_l()) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 10386 |         for (const sp<IAfMmapTrack>& t : mActiveTracks) { | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 10387 |             if (t->isSilenced_l() | 
 | 10388 |                     && t->uid() != static_cast<uid_t>(client.attributionSource.uid)) { | 
| Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 10389 |                 t->invalidate(); | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 10390 |             } | 
| Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 10391 |         } | 
 | 10392 |     } | 
 | 10393 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10394 |     mActiveTracks.add(track); | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 10395 |     sp<IAfEffectChain> chain = getEffectChain_l(mSessionId); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10396 |     if (chain != 0) { | 
| Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10397 |         chain->setStrategy(getStrategyForStream(streamType_l())); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10398 |         chain->incTrackCnt(); | 
 | 10399 |         chain->incActiveTrackCnt(); | 
 | 10400 |     } | 
 | 10401 |  | 
| Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 10402 |     track->logBeginInterval(patchSinksToString(&mPatch)); // log to MediaMetrics | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10403 |     *handle = portId; | 
| Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 10404 |  | 
 | 10405 |     if (mActiveTracks.size() == 1) { | 
 | 10406 |         ret = exitStandby_l(); | 
 | 10407 |     } | 
 | 10408 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10409 |     broadcast_l(); | 
 | 10410 |  | 
| Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 10411 |     ALOGV("%s DONE status %d handle %d stream %p", __FUNCTION__, ret, *handle, mHalStream.get()); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10412 |  | 
| Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 10413 |     return ret; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10414 | } | 
 | 10415 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10416 | status_t MmapThread::stop(audio_port_handle_t handle) | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10417 | { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10418 |     ALOGV("%s handle %d", __FUNCTION__, handle); | 
| Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10419 |     audio_utils::lock_guard l(mutex()); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10420 |  | 
 | 10421 |     if (mHalStream == 0) { | 
 | 10422 |         return NO_INIT; | 
 | 10423 |     } | 
 | 10424 |  | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10425 |     if (handle == mPortId) { | 
| Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10426 |         releaseWakeLock_l(); | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10427 |         return NO_ERROR; | 
 | 10428 |     } | 
 | 10429 |  | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 10430 |     sp<IAfMmapTrack> track; | 
 | 10431 |     for (const sp<IAfMmapTrack>& t : mActiveTracks) { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10432 |         if (handle == t->portId()) { | 
 | 10433 |             track = t; | 
 | 10434 |             break; | 
 | 10435 |         } | 
 | 10436 |     } | 
 | 10437 |     if (track == 0) { | 
 | 10438 |         return BAD_VALUE; | 
 | 10439 |     } | 
 | 10440 |  | 
 | 10441 |     mActiveTracks.remove(track); | 
| jiabin | 0960903 | 2022-06-15 19:26:01 +0000 | [diff] [blame] | 10442 |     eraseClientSilencedState_l(track->portId()); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10443 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 10444 |     mutex().unlock(); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10445 |     if (isOutput()) { | 
| Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 10446 |         AudioSystem::stopOutput(track->portId()); | 
 | 10447 |         AudioSystem::releaseOutput(track->portId()); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10448 |     } else { | 
| Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 10449 |         AudioSystem::stopInput(track->portId()); | 
 | 10450 |         AudioSystem::releaseInput(track->portId()); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10451 |     } | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 10452 |     mutex().lock(); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10453 |  | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 10454 |     sp<IAfEffectChain> chain = getEffectChain_l(track->sessionId()); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10455 |     if (chain != 0) { | 
 | 10456 |         chain->decActiveTrackCnt(); | 
 | 10457 |         chain->decTrackCnt(); | 
 | 10458 |     } | 
 | 10459 |  | 
| Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 10460 |     if (mActiveTracks.isEmpty()) { | 
 | 10461 |         mHalStream->stop(); | 
 | 10462 |     } | 
 | 10463 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10464 |     broadcast_l(); | 
 | 10465 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10466 |     return NO_ERROR; | 
 | 10467 | } | 
 | 10468 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10469 | status_t MmapThread::standby() | 
| Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10470 | NO_THREAD_SAFETY_ANALYSIS  // clang bug | 
| Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 10471 | { | 
 | 10472 |     ALOGV("%s", __FUNCTION__); | 
| Atneya Nair | 97a7388 | 2023-10-30 20:26:21 -0700 | [diff] [blame] | 10473 |     audio_utils::lock_guard l_{mutex()}; | 
| Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 10474 |  | 
 | 10475 |     if (mHalStream == 0) { | 
 | 10476 |         return NO_INIT; | 
 | 10477 |     } | 
| Eric Tan | 39ec8d6 | 2018-07-24 09:49:29 -0700 | [diff] [blame] | 10478 |     if (!mActiveTracks.isEmpty()) { | 
| Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 10479 |         return INVALID_OPERATION; | 
 | 10480 |     } | 
 | 10481 |     mHalStream->standby(); | 
| Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 10482 |     if (!mStandby) { | 
 | 10483 |         mThreadMetrics.logEndInterval(); | 
| Andy Hung | 44d648b | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 10484 |         mThreadSnapshot.onEnd(); | 
| Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 10485 |         mStandby = true; | 
 | 10486 |     } | 
| Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10487 |     releaseWakeLock_l(); | 
| Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 10488 |     return NO_ERROR; | 
 | 10489 | } | 
 | 10490 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10491 | status_t MmapThread::reportData(const void* /*buffer*/, size_t /*frameCount*/) { | 
| jiabin | fc791ee | 2023-02-15 19:43:40 +0000 | [diff] [blame] | 10492 |     // This is a stub implementation. The MmapPlaybackThread overrides this function. | 
 | 10493 |     return INVALID_OPERATION; | 
 | 10494 | } | 
 | 10495 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10496 | void MmapThread::readHalParameters_l() | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10497 | { | 
 | 10498 |     status_t result = mHalStream->getAudioProperties(&mSampleRate, &mChannelMask, &mHALFormat); | 
 | 10499 |     LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving audio properties from HAL: %d", result); | 
 | 10500 |     mFormat = mHALFormat; | 
 | 10501 |     LOG_ALWAYS_FATAL_IF(!audio_is_linear_pcm(mFormat), "HAL format %#x is not linear pcm", mFormat); | 
 | 10502 |     result = mHalStream->getFrameSize(&mFrameSize); | 
 | 10503 |     LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving frame size from HAL: %d", result); | 
| Hayden Gomes | 1a89ab3 | 2020-06-12 11:05:47 -0700 | [diff] [blame] | 10504 |     LOG_ALWAYS_FATAL_IF(mFrameSize <= 0, "Error frame size was %zu but must be greater than zero", | 
 | 10505 |             mFrameSize); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10506 |     result = mHalStream->getBufferSize(&mBufferSize); | 
 | 10507 |     LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving buffer size from HAL: %d", result); | 
 | 10508 |     mFrameCount = mBufferSize / mFrameSize; | 
| Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 10509 |  | 
| Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 10510 |     // TODO: make a readHalParameters call? | 
 | 10511 |     mediametrics::LogItem item(mThreadMetrics.getMetricsId()); | 
| Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 10512 |     item.set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_READPARAMETERS) | 
| Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 10513 |         .set(AMEDIAMETRICS_PROP_ENCODING, IAfThreadBase::formatToString(mFormat).c_str()) | 
| Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 10514 |         .set(AMEDIAMETRICS_PROP_SAMPLERATE, (int32_t)mSampleRate) | 
 | 10515 |         .set(AMEDIAMETRICS_PROP_CHANNELMASK, (int32_t)mChannelMask) | 
 | 10516 |         .set(AMEDIAMETRICS_PROP_CHANNELCOUNT, (int32_t)mChannelCount) | 
 | 10517 |         .set(AMEDIAMETRICS_PROP_FRAMECOUNT, (int32_t)mFrameCount) | 
 | 10518 |         /* | 
 | 10519 |         .set(AMEDIAMETRICS_PROP_FLAGS, toString(flags).c_str()) | 
 | 10520 |         .set(AMEDIAMETRICS_PROP_PREFIX_HAPTIC AMEDIAMETRICS_PROP_CHANNELMASK, | 
 | 10521 |                 (int32_t)mHapticChannelMask) | 
 | 10522 |         .set(AMEDIAMETRICS_PROP_PREFIX_HAPTIC AMEDIAMETRICS_PROP_CHANNELCOUNT, | 
 | 10523 |                 (int32_t)mHapticChannelCount) | 
 | 10524 |         */ | 
 | 10525 |         .set(AMEDIAMETRICS_PROP_PREFIX_HAL    AMEDIAMETRICS_PROP_ENCODING, | 
| Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 10526 |                 IAfThreadBase::formatToString(mHALFormat).c_str()) | 
| Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 10527 |         .set(AMEDIAMETRICS_PROP_PREFIX_HAL    AMEDIAMETRICS_PROP_FRAMECOUNT, | 
 | 10528 |                 (int32_t)mFrameCount) // sic - added HAL | 
 | 10529 |         .record(); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10530 | } | 
 | 10531 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10532 | bool MmapThread::threadLoop() | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10533 | { | 
| Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 10534 |     { | 
 | 10535 |         audio_utils::unique_lock _l(mutex()); | 
 | 10536 |         checkSilentMode_l(); | 
 | 10537 |     } | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10538 |  | 
 | 10539 |     const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); | 
 | 10540 |  | 
 | 10541 |     while (!exitPending()) | 
 | 10542 |     { | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 10543 |         Vector<sp<IAfEffectChain>> effectChains; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10544 |  | 
| Andy Hung | 13850be | 2019-03-14 11:33:09 -0700 | [diff] [blame] | 10545 |         { // under Thread lock | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 10546 |         audio_utils::unique_lock _l(mutex()); | 
| Andy Hung | 13850be | 2019-03-14 11:33:09 -0700 | [diff] [blame] | 10547 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10548 |         if (mSignalPending) { | 
 | 10549 |             // A signal was raised while we were unlocked | 
 | 10550 |             mSignalPending = false; | 
 | 10551 |         } else { | 
 | 10552 |             if (mConfigEvents.isEmpty()) { | 
 | 10553 |                 // we're about to wait, flush the binder command buffer | 
 | 10554 |                 IPCThreadState::self()->flushCommands(); | 
 | 10555 |  | 
 | 10556 |                 if (exitPending()) { | 
 | 10557 |                     break; | 
 | 10558 |                 } | 
 | 10559 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10560 |                 // wait until we have something to do... | 
| Tomasz Wasilczyk | 5b05437 | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 10561 |                 ALOGV("%s going to sleep", myName.c_str()); | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 10562 |                 mWaitWorkCV.wait(_l); | 
| Tomasz Wasilczyk | 5b05437 | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 10563 |                 ALOGV("%s waking up", myName.c_str()); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10564 |  | 
 | 10565 |                 checkSilentMode_l(); | 
 | 10566 |  | 
 | 10567 |                 continue; | 
 | 10568 |             } | 
 | 10569 |         } | 
 | 10570 |  | 
 | 10571 |         processConfigEvents_l(); | 
 | 10572 |  | 
 | 10573 |         processVolume_l(); | 
 | 10574 |  | 
 | 10575 |         checkInvalidTracks_l(); | 
 | 10576 |  | 
| Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 10577 |         mActiveTracks.updatePowerState_l(this); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10578 |  | 
| Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 10579 |         updateMetadata_l(); | 
 | 10580 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10581 |         lockEffectChains_l(effectChains); | 
| Andy Hung | 13850be | 2019-03-14 11:33:09 -0700 | [diff] [blame] | 10582 |         } // release Thread lock | 
 | 10583 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10584 |         for (size_t i = 0; i < effectChains.size(); i ++) { | 
| Andy Hung | 13850be | 2019-03-14 11:33:09 -0700 | [diff] [blame] | 10585 |             effectChains[i]->process_l(); // Thread is not locked, but effect chain is locked | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10586 |         } | 
| Andy Hung | 13850be | 2019-03-14 11:33:09 -0700 | [diff] [blame] | 10587 |  | 
 | 10588 |         // enable changes in effect chain, including moving to another thread. | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10589 |         unlockEffectChains(effectChains); | 
 | 10590 |         // Effect chains will be actually deleted here if they were removed from | 
 | 10591 |         // mEffectChains list during mixing or effects processing | 
| Andy Hung | 56ce2ed | 2024-06-12 16:03:16 -0700 | [diff] [blame] | 10592 |         mThreadloopExecutor.process(); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10593 |     } | 
| Andy Hung | 56ce2ed | 2024-06-12 16:03:16 -0700 | [diff] [blame] | 10594 |     mThreadloopExecutor.process(); // process any remaining deferred actions. | 
 | 10595 |     // deferred actions after this point are ignored. | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10596 |  | 
 | 10597 |     threadLoop_exit(); | 
 | 10598 |  | 
 | 10599 |     if (!mStandby) { | 
 | 10600 |         threadLoop_standby(); | 
 | 10601 |         mStandby = true; | 
 | 10602 |     } | 
 | 10603 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10604 |     ALOGV("Thread %p type %d exiting", this, mType); | 
 | 10605 |     return false; | 
 | 10606 | } | 
 | 10607 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 10608 | // checkForNewParameter_l() must be called with ThreadBase::mutex() held | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10609 | bool MmapThread::checkForNewParameter_l(const String8& keyValuePair, | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10610 |                                                               status_t& status) | 
 | 10611 | { | 
 | 10612 |     AudioParameter param = AudioParameter(keyValuePair); | 
 | 10613 |     int value; | 
| Eric Laurent | e6e9a48 | 2017-07-25 19:26:02 -0700 | [diff] [blame] | 10614 |     bool sendToHal = true; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10615 |     if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10616 |         LOG_FATAL("Should not happen set routing device in MmapThread"); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10617 |     } | 
| Eric Laurent | e6e9a48 | 2017-07-25 19:26:02 -0700 | [diff] [blame] | 10618 |     if (sendToHal) { | 
 | 10619 |         status = mHalStream->setParameters(keyValuePair); | 
 | 10620 |     } else { | 
 | 10621 |         status = NO_ERROR; | 
 | 10622 |     } | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10623 |  | 
 | 10624 |     return false; | 
 | 10625 | } | 
 | 10626 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10627 | String8 MmapThread::getParameters(const String8& keys) | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10628 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 10629 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10630 |     String8 out_s8; | 
 | 10631 |     if (initCheck() == NO_ERROR && mHalStream->getParameters(keys, &out_s8) == OK) { | 
 | 10632 |         return out_s8; | 
 | 10633 |     } | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 10634 |     return {}; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10635 | } | 
 | 10636 |  | 
| Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 10637 | void MmapThread::ioConfigChanged_l(audio_io_config_event_t event, pid_t pid, | 
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 10638 |                                                audio_port_handle_t portId __unused) { | 
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 10639 |     sp<AudioIoDescriptor> desc; | 
 | 10640 |     bool isInput = false; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10641 |     switch (event) { | 
 | 10642 |     case AUDIO_INPUT_OPENED: | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 10643 |     case AUDIO_INPUT_REGISTERED: | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10644 |     case AUDIO_INPUT_CONFIG_CHANGED: | 
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 10645 |         isInput = true; | 
 | 10646 |         FALLTHROUGH_INTENDED; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10647 |     case AUDIO_OUTPUT_OPENED: | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 10648 |     case AUDIO_OUTPUT_REGISTERED: | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10649 |     case AUDIO_OUTPUT_CONFIG_CHANGED: | 
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 10650 |         desc = sp<AudioIoDescriptor>::make(mId, mPatch, isInput, | 
 | 10651 |                 mSampleRate, mFormat, mChannelMask, mFrameCount, mFrameCount); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10652 |         break; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10653 |     case AUDIO_INPUT_CLOSED: | 
 | 10654 |     case AUDIO_OUTPUT_CLOSED: | 
 | 10655 |     default: | 
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 10656 |         desc = sp<AudioIoDescriptor>::make(mId); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10657 |         break; | 
 | 10658 |     } | 
| Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 10659 |     mAfThreadCallback->ioConfigChanged_l(event, desc, pid); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10660 | } | 
 | 10661 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10662 | status_t MmapThread::createAudioPatch_l(const struct audio_patch* patch, | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10663 |                                                           audio_patch_handle_t *handle) | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 10664 | NO_THREAD_SAFETY_ANALYSIS  // elease and re-acquire mutex() | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10665 | { | 
 | 10666 |     status_t status = NO_ERROR; | 
 | 10667 |  | 
 | 10668 |     // store new device and send to effects | 
 | 10669 |     audio_devices_t type = AUDIO_DEVICE_NONE; | 
 | 10670 |     audio_port_handle_t deviceId; | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10671 |     AudioDeviceTypeAddrVector sinkDeviceTypeAddrs; | 
 | 10672 |     AudioDeviceTypeAddr sourceDeviceTypeAddr; | 
 | 10673 |     uint32_t numDevices = 0; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10674 |     if (isOutput()) { | 
 | 10675 |         for (unsigned int i = 0; i < patch->num_sinks; i++) { | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10676 |             LOG_ALWAYS_FATAL_IF(popcount(patch->sinks[i].ext.device.type) > 1 | 
 | 10677 |                                 && !mAudioHwDev->supportsAudioPatches(), | 
 | 10678 |                                 "Enumerated device type(%#x) must not be used " | 
 | 10679 |                                 "as it does not support audio patches", | 
 | 10680 |                                 patch->sinks[i].ext.device.type); | 
| Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 10681 |             type = static_cast<audio_devices_t>(type | patch->sinks[i].ext.device.type); | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 10682 |             sinkDeviceTypeAddrs.emplace_back(patch->sinks[i].ext.device.type, | 
 | 10683 |                     patch->sinks[i].ext.device.address); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10684 |         } | 
 | 10685 |         deviceId = patch->sinks[0].id; | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10686 |         numDevices = mPatch.num_sinks; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10687 |     } else { | 
 | 10688 |         type = patch->sources[0].ext.device.type; | 
 | 10689 |         deviceId = patch->sources[0].id; | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10690 |         numDevices = mPatch.num_sources; | 
 | 10691 |         sourceDeviceTypeAddr.mType = patch->sources[0].ext.device.type; | 
| jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 10692 |         sourceDeviceTypeAddr.setAddress(patch->sources[0].ext.device.address); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10693 |     } | 
 | 10694 |  | 
 | 10695 |     for (size_t i = 0; i < mEffectChains.size(); i++) { | 
| jiabin | 8f278ee | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 10696 |         if (isOutput()) { | 
 | 10697 |             mEffectChains[i]->setDevices_l(sinkDeviceTypeAddrs); | 
 | 10698 |         } else { | 
 | 10699 |             mEffectChains[i]->setInputDevice_l(sourceDeviceTypeAddr); | 
 | 10700 |         } | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10701 |     } | 
 | 10702 |  | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10703 |     if (!isOutput()) { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10704 |         // store new source and send to effects | 
 | 10705 |         if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) { | 
 | 10706 |             mAudioSource = patch->sinks[0].ext.mix.usecase.source; | 
 | 10707 |             for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 10708 |                 mEffectChains[i]->setAudioSource_l(mAudioSource); | 
 | 10709 |             } | 
 | 10710 |         } | 
 | 10711 |     } | 
 | 10712 |  | 
| jiabin | 78b86f2 | 2024-02-22 00:39:29 +0000 | [diff] [blame] | 10713 |     // For mmap streams, once the routing has changed, they will be disconnected. It should be | 
 | 10714 |     // okay to notify the client earlier before the new patch creation. | 
 | 10715 |     if (mDeviceId != deviceId) { | 
 | 10716 |         if (const sp<MmapStreamCallback> callback = mCallback.promote()) { | 
 | 10717 |             // The aaudioservice handle the routing changed event asynchronously. In that case, | 
 | 10718 |             // it is safe to hold the lock here. | 
 | 10719 |             callback->onRoutingChanged(deviceId); | 
 | 10720 |         } | 
 | 10721 |     } | 
 | 10722 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10723 |     if (mAudioHwDev->supportsAudioPatches()) { | 
| Ytai Ben-Tsvi | f997ffe | 2022-02-03 16:38:16 -0800 | [diff] [blame] | 10724 |         status = mHalDevice->createAudioPatch(patch->num_sources, patch->sources, patch->num_sinks, | 
 | 10725 |                                               patch->sinks, handle); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10726 |     } else { | 
| Ytai Ben-Tsvi | f997ffe | 2022-02-03 16:38:16 -0800 | [diff] [blame] | 10727 |         audio_port_config port; | 
 | 10728 |         std::optional<audio_source_t> source; | 
 | 10729 |         if (isOutput()) { | 
 | 10730 |             port = patch->sinks[0]; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10731 |         } else { | 
| Ytai Ben-Tsvi | f997ffe | 2022-02-03 16:38:16 -0800 | [diff] [blame] | 10732 |             port = patch->sources[0]; | 
 | 10733 |             source = patch->sinks[0].ext.mix.usecase.source; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10734 |         } | 
| Ytai Ben-Tsvi | f997ffe | 2022-02-03 16:38:16 -0800 | [diff] [blame] | 10735 |         status = mHalStream->legacyCreateAudioPatch(port, source, type); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10736 |         *handle = AUDIO_PATCH_HANDLE_NONE; | 
 | 10737 |     } | 
 | 10738 |  | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10739 |     if (numDevices == 0 || mDeviceId != deviceId) { | 
 | 10740 |         if (isOutput()) { | 
 | 10741 |             sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); | 
 | 10742 |             mOutDeviceTypeAddrs = sinkDeviceTypeAddrs; | 
| jiabin | 0a957d3 | 2020-04-29 10:56:20 -0700 | [diff] [blame] | 10743 |             checkSilentMode_l(); | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10744 |         } else { | 
 | 10745 |             sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); | 
 | 10746 |             mInDeviceTypeAddr = sourceDeviceTypeAddr; | 
 | 10747 |         } | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10748 |         mPatch = *patch; | 
| Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 10749 |         mDeviceId = deviceId; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10750 |     } | 
| Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 10751 |     // Force meteadata update after a route change | 
 | 10752 |     mActiveTracks.setHasChanged(); | 
 | 10753 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10754 |     return status; | 
 | 10755 | } | 
 | 10756 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10757 | status_t MmapThread::releaseAudioPatch_l(const audio_patch_handle_t handle) | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10758 | { | 
 | 10759 |     status_t status = NO_ERROR; | 
 | 10760 |  | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10761 |     mPatch = audio_patch{}; | 
 | 10762 |     mOutDeviceTypeAddrs.clear(); | 
 | 10763 |     mInDeviceTypeAddr.reset(); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10764 |  | 
 | 10765 |     bool supportsAudioPatches = mHalDevice->supportsAudioPatches(&supportsAudioPatches) == OK ? | 
 | 10766 |                                         supportsAudioPatches : false; | 
 | 10767 |  | 
 | 10768 |     if (supportsAudioPatches) { | 
 | 10769 |         status = mHalDevice->releaseAudioPatch(handle); | 
 | 10770 |     } else { | 
| Ytai Ben-Tsvi | f997ffe | 2022-02-03 16:38:16 -0800 | [diff] [blame] | 10771 |         status = mHalStream->legacyReleaseAudioPatch(); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10772 |     } | 
| Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 10773 |     // Force meteadata update after a route change | 
 | 10774 |     mActiveTracks.setHasChanged(); | 
 | 10775 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10776 |     return status; | 
 | 10777 | } | 
 | 10778 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10779 | void MmapThread::toAudioPortConfig(struct audio_port_config* config) | 
| Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10780 | NO_THREAD_SAFETY_ANALYSIS // mAudioHwDev handle access | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10781 | { | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 10782 |     ThreadBase::toAudioPortConfig(config); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10783 |     if (isOutput()) { | 
 | 10784 |         config->role = AUDIO_PORT_ROLE_SOURCE; | 
 | 10785 |         config->ext.mix.hw_module = mAudioHwDev->handle(); | 
 | 10786 |         config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; | 
 | 10787 |     } else { | 
 | 10788 |         config->role = AUDIO_PORT_ROLE_SINK; | 
 | 10789 |         config->ext.mix.hw_module = mAudioHwDev->handle(); | 
 | 10790 |         config->ext.mix.usecase.source = mAudioSource; | 
 | 10791 |     } | 
 | 10792 | } | 
 | 10793 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10794 | status_t MmapThread::addEffectChain_l(const sp<IAfEffectChain>& chain) | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10795 | { | 
 | 10796 |     audio_session_t session = chain->sessionId(); | 
 | 10797 |  | 
 | 10798 |     ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); | 
 | 10799 |     // Attach all tracks with same session ID to this chain. | 
 | 10800 |     // indicate all active tracks in the chain | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 10801 |     for (const sp<IAfMmapTrack>& track : mActiveTracks) { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10802 |         if (session == track->sessionId()) { | 
 | 10803 |             chain->incTrackCnt(); | 
 | 10804 |             chain->incActiveTrackCnt(); | 
 | 10805 |         } | 
 | 10806 |     } | 
 | 10807 |  | 
 | 10808 |     chain->setThread(this); | 
 | 10809 |     chain->setInBuffer(nullptr); | 
 | 10810 |     chain->setOutBuffer(nullptr); | 
| Shunkai Yao | d125e40 | 2024-01-20 03:19:06 +0000 | [diff] [blame] | 10811 |     chain->syncHalEffectsState_l(); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10812 |  | 
 | 10813 |     mEffectChains.add(chain); | 
 | 10814 |     checkSuspendOnAddEffectChain_l(chain); | 
 | 10815 |     return NO_ERROR; | 
 | 10816 | } | 
 | 10817 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10818 | size_t MmapThread::removeEffectChain_l(const sp<IAfEffectChain>& chain) | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10819 | { | 
 | 10820 |     audio_session_t session = chain->sessionId(); | 
 | 10821 |  | 
 | 10822 |     ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); | 
 | 10823 |  | 
 | 10824 |     for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 10825 |         if (chain == mEffectChains[i]) { | 
 | 10826 |             mEffectChains.removeAt(i); | 
 | 10827 |             // detach all active tracks from the chain | 
 | 10828 |             // detach all tracks with same session ID from this chain | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 10829 |             for (const sp<IAfMmapTrack>& track : mActiveTracks) { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10830 |                 if (session == track->sessionId()) { | 
 | 10831 |                     chain->decActiveTrackCnt(); | 
 | 10832 |                     chain->decTrackCnt(); | 
 | 10833 |                 } | 
 | 10834 |             } | 
 | 10835 |             break; | 
 | 10836 |         } | 
 | 10837 |     } | 
 | 10838 |     return mEffectChains.size(); | 
 | 10839 | } | 
 | 10840 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10841 | void MmapThread::threadLoop_standby() | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10842 | { | 
 | 10843 |     mHalStream->standby(); | 
 | 10844 | } | 
 | 10845 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10846 | void MmapThread::threadLoop_exit() | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10847 | { | 
| Phil Burk | 7dce728 | 2017-09-27 13:51:41 -0700 | [diff] [blame] | 10848 |     // Do not call callback->onTearDown() because it is redundant for thread exit | 
 | 10849 |     // and because it can cause a recursive mutex lock on stop(). | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10850 | } | 
 | 10851 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10852 | status_t MmapThread::setSyncEvent(const sp<SyncEvent>& /* event */) | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10853 | { | 
 | 10854 |     return BAD_VALUE; | 
 | 10855 | } | 
 | 10856 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10857 | bool MmapThread::isValidSyncEvent( | 
 | 10858 |         const sp<SyncEvent>& /* event */) const | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10859 | { | 
 | 10860 |     return false; | 
 | 10861 | } | 
 | 10862 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10863 | status_t MmapThread::checkEffectCompatibility_l( | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10864 |         const effect_descriptor_t *desc, audio_session_t sessionId) | 
 | 10865 | { | 
 | 10866 |     // No global effect sessions on mmap threads | 
| Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 10867 |     if (audio_is_global_session(sessionId)) { | 
 | 10868 |         ALOGW("checkEffectCompatibility_l(): global effect %s on MMAP thread %s", | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10869 |                 desc->name, mThreadName); | 
 | 10870 |         return BAD_VALUE; | 
 | 10871 |     } | 
 | 10872 |  | 
 | 10873 |     if (!isOutput() && ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC)) { | 
 | 10874 |         ALOGW("checkEffectCompatibility_l(): non pre processing effect %s on capture mmap thread", | 
 | 10875 |                 desc->name); | 
 | 10876 |         return BAD_VALUE; | 
 | 10877 |     } | 
 | 10878 |     if (isOutput() && ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) { | 
| Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 10879 |         ALOGW("checkEffectCompatibility_l(): pre processing effect %s created on playback mmap " | 
 | 10880 |               "thread", desc->name); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10881 |         return BAD_VALUE; | 
 | 10882 |     } | 
 | 10883 |  | 
 | 10884 |     // Only allow effects without processing load or latency | 
 | 10885 |     if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) != EFFECT_FLAG_NO_PROCESS) { | 
 | 10886 |         return BAD_VALUE; | 
 | 10887 |     } | 
 | 10888 |  | 
| Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 10889 |     if (IAfEffectModule::isHapticGenerator(&desc->type)) { | 
| jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 10890 |         ALOGE("%s(): HapticGenerator is not supported for MmapThread", __func__); | 
 | 10891 |         return BAD_VALUE; | 
 | 10892 |     } | 
 | 10893 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10894 |     return NO_ERROR; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10895 | } | 
 | 10896 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10897 | void MmapThread::checkInvalidTracks_l() | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10898 | { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 10899 |     for (const sp<IAfMmapTrack>& track : mActiveTracks) { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10900 |         if (track->isInvalid()) { | 
| jiabin | 78b86f2 | 2024-02-22 00:39:29 +0000 | [diff] [blame] | 10901 |             if (const sp<MmapStreamCallback> callback = mCallback.promote()) { | 
 | 10902 |                 // The aaudioservice handle the routing changed event asynchronously. In that case, | 
 | 10903 |                 // it is safe to hold the lock here. | 
 | 10904 |                 callback->onRoutingChanged(AUDIO_PORT_HANDLE_NONE); | 
 | 10905 |             } else if (mNoCallbackWarningCount < kMaxNoCallbackWarnings) { | 
| Eric Laurent | 039c24a | 2022-10-07 14:01:59 +0200 | [diff] [blame] | 10906 |                 ALOGW("Could not notify MMAP stream tear down: no onRoutingChanged callback!"); | 
 | 10907 |                 mNoCallbackWarningCount++; | 
 | 10908 |             } | 
 | 10909 |             break; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10910 |         } | 
 | 10911 |     } | 
 | 10912 | } | 
 | 10913 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10914 | void MmapThread::dumpInternals_l(int fd, const Vector<String16>& /* args */) | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10915 | { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10916 |     dprintf(fd, "  Attributes: content type %d usage %d source %d\n", | 
 | 10917 |             mAttr.content_type, mAttr.usage, mAttr.source); | 
 | 10918 |     dprintf(fd, "  Session: %d port Id: %d\n", mSessionId, mPortId); | 
| Eric Tan | 39ec8d6 | 2018-07-24 09:49:29 -0700 | [diff] [blame] | 10919 |     if (mActiveTracks.isEmpty()) { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10920 |         dprintf(fd, "  No active clients\n"); | 
 | 10921 |     } | 
 | 10922 | } | 
 | 10923 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10924 | void MmapThread::dumpTracks_l(int fd, const Vector<String16>& /* args */) | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10925 | { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10926 |     String8 result; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10927 |     size_t numtracks = mActiveTracks.size(); | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 10928 |     dprintf(fd, "  %zu Tracks\n", numtracks); | 
 | 10929 |     const char *prefix = "    "; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10930 |     if (numtracks) { | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 10931 |         result.append(prefix); | 
| Kevin Rocard | 5f2136e | 2018-05-11 22:03:00 -0700 | [diff] [blame] | 10932 |         mActiveTracks[0]->appendDumpHeader(result); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10933 |         for (size_t i = 0; i < numtracks ; ++i) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 10934 |             sp<IAfMmapTrack> track = mActiveTracks[i]; | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 10935 |             result.append(prefix); | 
 | 10936 |             track->appendDump(result, true /* active */); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10937 |         } | 
 | 10938 |     } else { | 
 | 10939 |         dprintf(fd, "\n"); | 
 | 10940 |     } | 
| Tomasz Wasilczyk | 5b05437 | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 10941 |     write(fd, result.c_str(), result.size()); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10942 | } | 
 | 10943 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10944 | /* static */ | 
 | 10945 | sp<IAfMmapPlaybackThread> IAfMmapPlaybackThread::create( | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 10946 |         const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id, | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10947 |         AudioHwDevice* hwDev,  AudioStreamOut* output, bool systemReady) { | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 10948 |     return sp<MmapPlaybackThread>::make(afThreadCallback, id, hwDev, output, systemReady); | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10949 | } | 
 | 10950 |  | 
 | 10951 | MmapPlaybackThread::MmapPlaybackThread( | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 10952 |         const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id, | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10953 |         AudioHwDevice *hwDev,  AudioStreamOut *output, bool systemReady) | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 10954 |     : MmapThread(afThreadCallback, id, hwDev, output->stream, systemReady, true /* isOut */), | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10955 |       mStreamType(AUDIO_STREAM_MUSIC), | 
| Phil Burk | 56ecf3e | 2018-03-12 15:38:17 -0700 | [diff] [blame] | 10956 |       mOutput(output) | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10957 | { | 
 | 10958 |     snprintf(mThreadName, kThreadNameLength, "AudioMmapOut_%X", id); | 
 | 10959 |     mChannelCount = audio_channel_count_from_out_mask(mChannelMask); | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 10960 |     mMasterVolume = afThreadCallback->masterVolume_l(); | 
 | 10961 |     mMasterMute = afThreadCallback->masterMute_l(); | 
| Eric Laurent | 1961151 | 2023-07-03 18:14:07 +0200 | [diff] [blame] | 10962 |  | 
 | 10963 |     for (int i = AUDIO_STREAM_MIN; i < AUDIO_STREAM_FOR_POLICY_CNT; ++i) { | 
 | 10964 |         const audio_stream_type_t stream{static_cast<audio_stream_type_t>(i)}; | 
 | 10965 |         mStreamTypes[stream].volume = 0.0f; | 
 | 10966 |         mStreamTypes[stream].mute = mAfThreadCallback->streamMute_l(stream); | 
 | 10967 |     } | 
 | 10968 |     // Audio patch and call assistant volume are always max | 
 | 10969 |     mStreamTypes[AUDIO_STREAM_PATCH].volume = 1.0f; | 
 | 10970 |     mStreamTypes[AUDIO_STREAM_PATCH].mute = false; | 
 | 10971 |     mStreamTypes[AUDIO_STREAM_CALL_ASSISTANT].volume = 1.0f; | 
 | 10972 |     mStreamTypes[AUDIO_STREAM_CALL_ASSISTANT].mute = false; | 
 | 10973 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10974 |     if (mAudioHwDev) { | 
 | 10975 |         if (mAudioHwDev->canSetMasterVolume()) { | 
 | 10976 |             mMasterVolume = 1.0; | 
 | 10977 |         } | 
 | 10978 |  | 
 | 10979 |         if (mAudioHwDev->canSetMasterMute()) { | 
 | 10980 |             mMasterMute = false; | 
 | 10981 |         } | 
 | 10982 |     } | 
 | 10983 | } | 
 | 10984 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10985 | void MmapPlaybackThread::configure(const audio_attributes_t* attr, | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10986 |                                                 audio_stream_type_t streamType, | 
 | 10987 |                                                 audio_session_t sessionId, | 
 | 10988 |                                                 const sp<MmapStreamCallback>& callback, | 
| Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 10989 |                                                 audio_port_handle_t deviceId, | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10990 |                                                 audio_port_handle_t portId) | 
 | 10991 | { | 
| Andy Hung | 160664b | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 10992 |     audio_utils::lock_guard l(mutex()); | 
 | 10993 |     MmapThread::configure_l(attr, streamType, sessionId, callback, deviceId, portId); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10994 |     mStreamType = streamType; | 
 | 10995 | } | 
 | 10996 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10997 | AudioStreamOut* MmapPlaybackThread::clearOutput() | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10998 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 10999 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11000 |     AudioStreamOut *output = mOutput; | 
 | 11001 |     mOutput = NULL; | 
 | 11002 |     return output; | 
 | 11003 | } | 
 | 11004 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11005 | void MmapPlaybackThread::setMasterVolume(float value) | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11006 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 11007 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11008 |     // Don't apply master volume in SW if our HAL can do it for us. | 
 | 11009 |     if (mAudioHwDev && | 
 | 11010 |             mAudioHwDev->canSetMasterVolume()) { | 
 | 11011 |         mMasterVolume = 1.0; | 
 | 11012 |     } else { | 
 | 11013 |         mMasterVolume = value; | 
 | 11014 |     } | 
 | 11015 | } | 
 | 11016 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11017 | void MmapPlaybackThread::setMasterMute(bool muted) | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11018 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 11019 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11020 |     // Don't apply master mute in SW if our HAL can do it for us. | 
 | 11021 |     if (mAudioHwDev && mAudioHwDev->canSetMasterMute()) { | 
 | 11022 |         mMasterMute = false; | 
 | 11023 |     } else { | 
 | 11024 |         mMasterMute = muted; | 
 | 11025 |     } | 
 | 11026 | } | 
 | 11027 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11028 | void MmapPlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11029 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 11030 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 1961151 | 2023-07-03 18:14:07 +0200 | [diff] [blame] | 11031 |     mStreamTypes[stream].volume = value; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11032 |     if (stream == mStreamType) { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11033 |         broadcast_l(); | 
 | 11034 |     } | 
 | 11035 | } | 
 | 11036 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11037 | float MmapPlaybackThread::streamVolume(audio_stream_type_t stream) const | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11038 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 11039 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 1961151 | 2023-07-03 18:14:07 +0200 | [diff] [blame] | 11040 |     return mStreamTypes[stream].volume; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11041 | } | 
 | 11042 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11043 | void MmapPlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11044 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 11045 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 1961151 | 2023-07-03 18:14:07 +0200 | [diff] [blame] | 11046 |     mStreamTypes[stream].mute = muted; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11047 |     if (stream == mStreamType) { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11048 |         broadcast_l(); | 
 | 11049 |     } | 
 | 11050 | } | 
 | 11051 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11052 | void MmapPlaybackThread::invalidateTracks(audio_stream_type_t streamType) | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11053 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 11054 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11055 |     if (streamType == mStreamType) { | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 11056 |         for (const sp<IAfMmapTrack>& track : mActiveTracks) { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11057 |             track->invalidate(); | 
 | 11058 |         } | 
 | 11059 |         broadcast_l(); | 
 | 11060 |     } | 
 | 11061 | } | 
 | 11062 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11063 | void MmapPlaybackThread::invalidateTracks(std::set<audio_port_handle_t>& portIds) | 
| jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 11064 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 11065 |     audio_utils::lock_guard _l(mutex()); | 
| jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 11066 |     bool trackMatch = false; | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 11067 |     for (const sp<IAfMmapTrack>& track : mActiveTracks) { | 
| jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 11068 |         if (portIds.find(track->portId()) != portIds.end()) { | 
 | 11069 |             track->invalidate(); | 
 | 11070 |             trackMatch = true; | 
 | 11071 |             portIds.erase(track->portId()); | 
 | 11072 |         } | 
 | 11073 |         if (portIds.empty()) { | 
 | 11074 |             break; | 
 | 11075 |         } | 
 | 11076 |     } | 
 | 11077 |     if (trackMatch) { | 
 | 11078 |         broadcast_l(); | 
 | 11079 |     } | 
 | 11080 | } | 
 | 11081 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11082 | void MmapPlaybackThread::processVolume_l() | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 11083 | NO_THREAD_SAFETY_ANALYSIS // access of track->processMuteEvent_l | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11084 | { | 
 | 11085 |     float volume; | 
 | 11086 |  | 
| Eric Laurent | 1961151 | 2023-07-03 18:14:07 +0200 | [diff] [blame] | 11087 |     if (mMasterMute || streamMuted_l()) { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11088 |         volume = 0; | 
 | 11089 |     } else { | 
| Eric Laurent | 1961151 | 2023-07-03 18:14:07 +0200 | [diff] [blame] | 11090 |         volume = mMasterVolume * streamVolume_l(); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11091 |     } | 
 | 11092 |  | 
 | 11093 |     if (volume != mHalVolFloat) { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11094 |         // Convert volumes from float to 8.24 | 
 | 11095 |         uint32_t vol = (uint32_t)(volume * (1 << 24)); | 
 | 11096 |  | 
 | 11097 |         // Delegate volume control to effect in track effect chain if needed | 
 | 11098 |         // only one effect chain can be present on DirectOutputThread, so if | 
 | 11099 |         // there is one, the track is connected to it | 
 | 11100 |         if (!mEffectChains.isEmpty()) { | 
| Shunkai Yao | f484765 | 2024-01-12 00:25:20 +0000 | [diff] [blame] | 11101 |             mEffectChains[0]->setVolume(&vol, &vol); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11102 |             volume = (float)vol / (1 << 24); | 
 | 11103 |         } | 
| Eric Laurent | dff774a | 2017-04-21 15:29:38 -0700 | [diff] [blame] | 11104 |         // Try to use HW volume control and fall back to SW control if not implemented | 
| Phil Burk | 56ecf3e | 2018-03-12 15:38:17 -0700 | [diff] [blame] | 11105 |         if (mOutput->stream->setVolume(volume, volume) == NO_ERROR) { | 
 | 11106 |             mHalVolFloat = volume; // HW volume control worked, so update value. | 
 | 11107 |             mNoCallbackWarningCount = 0; | 
 | 11108 |         } else { | 
| Eric Laurent | dff774a | 2017-04-21 15:29:38 -0700 | [diff] [blame] | 11109 |             sp<MmapStreamCallback> callback = mCallback.promote(); | 
 | 11110 |             if (callback != 0) { | 
| Phil Burk | 56ecf3e | 2018-03-12 15:38:17 -0700 | [diff] [blame] | 11111 |                 mHalVolFloat = volume; // SW volume control worked, so update value. | 
 | 11112 |                 mNoCallbackWarningCount = 0; | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 11113 |                 mutex().unlock(); | 
| Robert Wu | 4389ae6 | 2022-02-17 18:39:41 +0000 | [diff] [blame] | 11114 |                 callback->onVolumeChanged(volume); | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 11115 |                 mutex().lock(); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11116 |             } else { | 
| Phil Burk | 56ecf3e | 2018-03-12 15:38:17 -0700 | [diff] [blame] | 11117 |                 if (mNoCallbackWarningCount < kMaxNoCallbackWarnings) { | 
 | 11118 |                     ALOGW("Could not set MMAP stream volume: no volume callback!"); | 
 | 11119 |                     mNoCallbackWarningCount++; | 
 | 11120 |                 } | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11121 |             } | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11122 |         } | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 11123 |         for (const sp<IAfMmapTrack>& track : mActiveTracks) { | 
| Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 11124 |             track->setMetadataHasChanged(); | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 11125 |             track->processMuteEvent_l(mAfThreadCallback->getOrCreateAudioManager(), | 
| Vlad Popa | ec1788e | 2022-08-04 11:23:30 +0200 | [diff] [blame] | 11126 |                 /*muteState=*/{mMasterMute, | 
| Eric Laurent | 1961151 | 2023-07-03 18:14:07 +0200 | [diff] [blame] | 11127 |                                streamVolume_l() == 0.f, | 
 | 11128 |                                streamMuted_l(), | 
| Vlad Popa | ec1788e | 2022-08-04 11:23:30 +0200 | [diff] [blame] | 11129 |                                // TODO(b/241533526): adjust logic to include mute from AppOps | 
 | 11130 |                                false /*muteFromPlaybackRestricted*/, | 
 | 11131 |                                false /*muteFromClientVolume*/, | 
 | 11132 |                                false /*muteFromVolumeShaper*/}); | 
| Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 11133 |         } | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11134 |     } | 
 | 11135 | } | 
 | 11136 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11137 | ThreadBase::MetadataUpdate MmapPlaybackThread::updateMetadata_l() | 
| Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11138 | { | 
| Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 11139 |     if (!isStreamInitialized() || !mActiveTracks.readAndClearHasChanged()) { | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 11140 |         return {}; // nothing to do | 
| Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11141 |     } | 
 | 11142 |     StreamOutHalInterface::SourceMetadata metadata; | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 11143 |     for (const sp<IAfMmapTrack>& track : mActiveTracks) { | 
| Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11144 |         // No track is invalid as this is called after prepareTrack_l in the same critical section | 
| Eric Laurent | 9457917 | 2020-11-20 18:41:04 +0100 | [diff] [blame] | 11145 |         playback_track_metadata_v7_t trackMetadata; | 
 | 11146 |         trackMetadata.base = { | 
| Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11147 |                 .usage = track->attributes().usage, | 
 | 11148 |                 .content_type = track->attributes().content_type, | 
 | 11149 |                 .gain = mHalVolFloat, // TODO: propagate from aaudio pre-mix volume | 
| Eric Laurent | 9457917 | 2020-11-20 18:41:04 +0100 | [diff] [blame] | 11150 |         }; | 
 | 11151 |         trackMetadata.channel_mask = track->channelMask(), | 
 | 11152 |         strncpy(trackMetadata.tags, track->attributes().tags, AUDIO_ATTRIBUTES_TAGS_MAX_SIZE); | 
 | 11153 |         metadata.tracks.push_back(trackMetadata); | 
| Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11154 |     } | 
 | 11155 |     mOutput->stream->updateSourceMetadata(metadata); | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 11156 |  | 
 | 11157 |     MetadataUpdate change; | 
 | 11158 |     change.playbackMetadataUpdate = metadata.tracks; | 
 | 11159 |     return change; | 
 | 11160 | }; | 
| Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11161 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11162 | void MmapPlaybackThread::checkSilentMode_l() | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11163 | { | 
 | 11164 |     if (!mMasterMute) { | 
 | 11165 |         char value[PROPERTY_VALUE_MAX]; | 
 | 11166 |         if (property_get("ro.audio.silent", value, "0") > 0) { | 
 | 11167 |             char *endptr; | 
 | 11168 |             unsigned long ul = strtoul(value, &endptr, 0); | 
 | 11169 |             if (*endptr == '\0' && ul != 0) { | 
| Andy Hung | 6fb2689 | 2024-02-20 16:32:57 -0800 | [diff] [blame] | 11170 |                 ALOGW("%s: mute from ro.audio.silent. Silence is golden", __func__); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11171 |                 // The setprop command will not allow a property to be changed after | 
 | 11172 |                 // the first time it is set, so we don't have to worry about un-muting. | 
 | 11173 |                 setMasterMute_l(true); | 
 | 11174 |             } | 
 | 11175 |         } | 
 | 11176 |     } | 
 | 11177 | } | 
 | 11178 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11179 | void MmapPlaybackThread::toAudioPortConfig(struct audio_port_config* config) | 
| Mikhail Naganov | 32abc2b | 2018-05-24 12:57:11 -0700 | [diff] [blame] | 11180 | { | 
 | 11181 |     MmapThread::toAudioPortConfig(config); | 
 | 11182 |     if (mOutput && mOutput->flags != AUDIO_OUTPUT_FLAG_NONE) { | 
 | 11183 |         config->config_mask |= AUDIO_PORT_CONFIG_FLAGS; | 
 | 11184 |         config->flags.output = mOutput->flags; | 
 | 11185 |     } | 
 | 11186 | } | 
 | 11187 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11188 | status_t MmapPlaybackThread::getExternalPosition(uint64_t* position, | 
| Andy Hung | 3e4c874 | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 11189 |         int64_t* timeNanos) const | 
| jiabin | b7d8c5a | 2020-08-26 17:24:52 -0700 | [diff] [blame] | 11190 | { | 
 | 11191 |     if (mOutput == nullptr) { | 
 | 11192 |         return NO_INIT; | 
 | 11193 |     } | 
 | 11194 |     struct timespec timestamp; | 
 | 11195 |     status_t status = mOutput->getPresentationPosition(position, ×tamp); | 
 | 11196 |     if (status == NO_ERROR) { | 
 | 11197 |         *timeNanos = timestamp.tv_sec * NANOS_PER_SECOND + timestamp.tv_nsec; | 
 | 11198 |     } | 
 | 11199 |     return status; | 
 | 11200 | } | 
 | 11201 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11202 | status_t MmapPlaybackThread::reportData(const void* buffer, size_t frameCount) { | 
| Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 11203 |     // Send to MelProcessor for sound dose measurement. | 
 | 11204 |     auto processor = mMelProcessor.load(); | 
 | 11205 |     if (processor) { | 
 | 11206 |         processor->process(buffer, frameCount * mFrameSize); | 
 | 11207 |     } | 
 | 11208 |  | 
| jiabin | fc791ee | 2023-02-15 19:43:40 +0000 | [diff] [blame] | 11209 |     return NO_ERROR; | 
 | 11210 | } | 
 | 11211 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 11212 | // startMelComputation_l() must be called with AudioFlinger::mutex() held | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11213 | void MmapPlaybackThread::startMelComputation_l( | 
| Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 11214 |         const sp<audio_utils::MelProcessor>& processor) | 
 | 11215 | { | 
 | 11216 |     ALOGV("%s: starting mel processor for thread %d", __func__, id()); | 
| Vlad Popa | 1c2f7e1 | 2023-03-28 02:08:56 +0200 | [diff] [blame] | 11217 |     mMelProcessor.store(processor); | 
 | 11218 |     if (processor) { | 
 | 11219 |         processor->resume(); | 
| Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 11220 |     } | 
| Vlad Popa | 1c2f7e1 | 2023-03-28 02:08:56 +0200 | [diff] [blame] | 11221 |  | 
 | 11222 |     // no need to update output format for MMapPlaybackThread since it is | 
 | 11223 |     // assigned constant for each thread | 
| Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 11224 | } | 
 | 11225 |  | 
| Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 11226 | // stopMelComputation_l() must be called with AudioFlinger::mutex() held | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11227 | void MmapPlaybackThread::stopMelComputation_l() | 
| Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 11228 | { | 
| Vlad Popa | 1c2f7e1 | 2023-03-28 02:08:56 +0200 | [diff] [blame] | 11229 |     ALOGV("%s: pausing mel processor for thread %d", __func__, id()); | 
 | 11230 |     auto melProcessor = mMelProcessor.load(); | 
 | 11231 |     if (melProcessor != nullptr) { | 
 | 11232 |         melProcessor->pause(); | 
 | 11233 |     } | 
| Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 11234 | } | 
 | 11235 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11236 | void MmapPlaybackThread::dumpInternals_l(int fd, const Vector<String16>& args) | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11237 | { | 
| Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 11238 |     MmapThread::dumpInternals_l(fd, args); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11239 |  | 
| Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 11240 |     dprintf(fd, "  Stream type: %d Stream volume: %f HAL volume: %f Stream mute %d\n", | 
| Eric Laurent | 1961151 | 2023-07-03 18:14:07 +0200 | [diff] [blame] | 11241 |             mStreamType, streamVolume_l(), mHalVolFloat, streamMuted_l()); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11242 |     dprintf(fd, "  Master volume: %f Master mute %d\n", mMasterVolume, mMasterMute); | 
 | 11243 | } | 
 | 11244 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11245 | /* static */ | 
 | 11246 | sp<IAfMmapCaptureThread> IAfMmapCaptureThread::create( | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 11247 |         const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id, | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11248 |         AudioHwDevice* hwDev,  AudioStreamIn* input, bool systemReady) { | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 11249 |     return sp<MmapCaptureThread>::make(afThreadCallback, id, hwDev, input, systemReady); | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11250 | } | 
 | 11251 |  | 
 | 11252 | MmapCaptureThread::MmapCaptureThread( | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 11253 |         const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id, | 
| jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 11254 |         AudioHwDevice *hwDev,  AudioStreamIn *input, bool systemReady) | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 11255 |     : MmapThread(afThreadCallback, id, hwDev, input->stream, systemReady, false /* isOut */), | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11256 |       mInput(input) | 
 | 11257 | { | 
 | 11258 |     snprintf(mThreadName, kThreadNameLength, "AudioMmapIn_%X", id); | 
 | 11259 |     mChannelCount = audio_channel_count_from_in_mask(mChannelMask); | 
 | 11260 | } | 
 | 11261 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11262 | status_t MmapCaptureThread::exitStandby_l() | 
| Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 11263 | { | 
| Phil Burk | f054fc3 | 2018-12-06 09:45:59 -0800 | [diff] [blame] | 11264 |     { | 
 | 11265 |         // mInput might have been cleared by clearInput() | 
| Phil Burk | f054fc3 | 2018-12-06 09:45:59 -0800 | [diff] [blame] | 11266 |         if (mInput != nullptr && mInput->stream != nullptr) { | 
 | 11267 |             mInput->stream->setGain(1.0f); | 
 | 11268 |         } | 
 | 11269 |     } | 
| Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 11270 |     return MmapThread::exitStandby_l(); | 
| Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 11271 | } | 
 | 11272 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11273 | AudioStreamIn* MmapCaptureThread::clearInput() | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11274 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 11275 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11276 |     AudioStreamIn *input = mInput; | 
 | 11277 |     mInput = NULL; | 
 | 11278 |     return input; | 
 | 11279 | } | 
| Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11280 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11281 | void MmapCaptureThread::processVolume_l() | 
| Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 11282 | { | 
 | 11283 |     bool changed = false; | 
 | 11284 |     bool silenced = false; | 
 | 11285 |  | 
 | 11286 |     sp<MmapStreamCallback> callback = mCallback.promote(); | 
 | 11287 |     if (callback == 0) { | 
 | 11288 |         if (mNoCallbackWarningCount < kMaxNoCallbackWarnings) { | 
 | 11289 |             ALOGW("Could not set MMAP stream silenced: no onStreamSilenced callback!"); | 
 | 11290 |             mNoCallbackWarningCount++; | 
 | 11291 |         } | 
 | 11292 |     } | 
 | 11293 |  | 
 | 11294 |     // After a change occurred in track silenced state, mute capture in audio DSP if at least one | 
 | 11295 |     // track is silenced and unmute otherwise | 
 | 11296 |     for (size_t i = 0; i < mActiveTracks.size() && !silenced; i++) { | 
 | 11297 |         if (!mActiveTracks[i]->getAndSetSilencedNotified_l()) { | 
 | 11298 |             changed = true; | 
 | 11299 |             silenced = mActiveTracks[i]->isSilenced_l(); | 
 | 11300 |         } | 
 | 11301 |     } | 
 | 11302 |  | 
 | 11303 |     if (changed) { | 
 | 11304 |         mInput->stream->setGain(silenced ? 0.0f: 1.0f); | 
 | 11305 |     } | 
 | 11306 | } | 
 | 11307 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11308 | ThreadBase::MetadataUpdate MmapCaptureThread::updateMetadata_l() | 
| Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11309 | { | 
| Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 11310 |     if (!isStreamInitialized() || !mActiveTracks.readAndClearHasChanged()) { | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 11311 |         return {}; // nothing to do | 
| Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11312 |     } | 
 | 11313 |     StreamInHalInterface::SinkMetadata metadata; | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 11314 |     for (const sp<IAfMmapTrack>& track : mActiveTracks) { | 
| Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11315 |         // No track is invalid as this is called after prepareTrack_l in the same critical section | 
| Eric Laurent | 9457917 | 2020-11-20 18:41:04 +0100 | [diff] [blame] | 11316 |         record_track_metadata_v7_t trackMetadata; | 
 | 11317 |         trackMetadata.base = { | 
| Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11318 |                 .source = track->attributes().source, | 
 | 11319 |                 .gain = 1, // capture tracks do not have volumes | 
| Eric Laurent | 9457917 | 2020-11-20 18:41:04 +0100 | [diff] [blame] | 11320 |         }; | 
 | 11321 |         trackMetadata.channel_mask = track->channelMask(), | 
 | 11322 |         strncpy(trackMetadata.tags, track->attributes().tags, AUDIO_ATTRIBUTES_TAGS_MAX_SIZE); | 
 | 11323 |         metadata.tracks.push_back(trackMetadata); | 
| Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11324 |     } | 
 | 11325 |     mInput->stream->updateSinkMetadata(metadata); | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 11326 |     MetadataUpdate change; | 
 | 11327 |     change.recordMetadataUpdate = metadata.tracks; | 
 | 11328 |     return change; | 
| Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11329 | } | 
 | 11330 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11331 | void MmapCaptureThread::setRecordSilenced(audio_port_handle_t portId, bool silenced) | 
| Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 11332 | { | 
| Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 11333 |     audio_utils::lock_guard _l(mutex()); | 
| Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 11334 |     for (size_t i = 0; i < mActiveTracks.size() ; i++) { | 
| Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 11335 |         if (mActiveTracks[i]->portId() == portId) { | 
| Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 11336 |             mActiveTracks[i]->setSilenced_l(silenced); | 
 | 11337 |             broadcast_l(); | 
 | 11338 |         } | 
 | 11339 |     } | 
| jiabin | 0960903 | 2022-06-15 19:26:01 +0000 | [diff] [blame] | 11340 |     setClientSilencedIfExists_l(portId, silenced); | 
| Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 11341 | } | 
 | 11342 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11343 | void MmapCaptureThread::toAudioPortConfig(struct audio_port_config* config) | 
| Mikhail Naganov | 32abc2b | 2018-05-24 12:57:11 -0700 | [diff] [blame] | 11344 | { | 
 | 11345 |     MmapThread::toAudioPortConfig(config); | 
 | 11346 |     if (mInput && mInput->flags != AUDIO_INPUT_FLAG_NONE) { | 
 | 11347 |         config->config_mask |= AUDIO_PORT_CONFIG_FLAGS; | 
 | 11348 |         config->flags.input = mInput->flags; | 
 | 11349 |     } | 
 | 11350 | } | 
 | 11351 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11352 | status_t MmapCaptureThread::getExternalPosition( | 
| Andy Hung | 3e4c874 | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 11353 |         uint64_t* position, int64_t* timeNanos) const | 
| jiabin | b7d8c5a | 2020-08-26 17:24:52 -0700 | [diff] [blame] | 11354 | { | 
 | 11355 |     if (mInput == nullptr) { | 
 | 11356 |         return NO_INIT; | 
 | 11357 |     } | 
 | 11358 |     return mInput->getCapturePosition((int64_t*)position, timeNanos); | 
 | 11359 | } | 
 | 11360 |  | 
| jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 11361 | // ---------------------------------------------------------------------------- | 
 | 11362 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11363 | /* static */ | 
 | 11364 | sp<IAfPlaybackThread> IAfPlaybackThread::createBitPerfectThread( | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 11365 |         const sp<IAfThreadCallback>& afThreadCallback, | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11366 |         AudioStreamOut* output, audio_io_handle_t id, bool systemReady) { | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 11367 |     return sp<BitPerfectThread>::make(afThreadCallback, output, id, systemReady); | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11368 | } | 
 | 11369 |  | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 11370 | BitPerfectThread::BitPerfectThread(const sp<IAfThreadCallback> &afThreadCallback, | 
| jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 11371 |         AudioStreamOut *output, audio_io_handle_t id, bool systemReady) | 
| Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 11372 |         : MixerThread(afThreadCallback, output, id, systemReady, BIT_PERFECT) {} | 
| jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 11373 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11374 | PlaybackThread::mixer_state BitPerfectThread::prepareTracks_l( | 
| Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 11375 |         Vector<sp<IAfTrack>>* tracksToRemove) { | 
| jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 11376 |     mixer_state result = MixerThread::prepareTracks_l(tracksToRemove); | 
 | 11377 |     // If there is only one active track and it is bit-perfect, enable tee buffer. | 
| jiabin | 76d9469 | 2022-12-15 21:51:21 +0000 | [diff] [blame] | 11378 |     float volumeLeft = 1.0f; | 
 | 11379 |     float volumeRight = 1.0f; | 
| jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 11380 |     if (mActiveTracks.size() == 1 && mActiveTracks[0]->isBitPerfect()) { | 
 | 11381 |         const int trackId = mActiveTracks[0]->id(); | 
 | 11382 |         mAudioMixer->setParameter( | 
 | 11383 |                     trackId, AudioMixer::TRACK, AudioMixer::TEE_BUFFER, (void *)mSinkBuffer); | 
 | 11384 |         mAudioMixer->setParameter( | 
 | 11385 |                     trackId, AudioMixer::TRACK, AudioMixer::TEE_BUFFER_FRAME_COUNT, | 
 | 11386 |                     (void *)(uintptr_t)mNormalFrameCount); | 
| jiabin | 76d9469 | 2022-12-15 21:51:21 +0000 | [diff] [blame] | 11387 |         mActiveTracks[0]->getFinalVolume(&volumeLeft, &volumeRight); | 
| jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 11388 |         mIsBitPerfect = true; | 
 | 11389 |     } else { | 
 | 11390 |         mIsBitPerfect = false; | 
 | 11391 |         // No need to copy bit-perfect data directly to sink buffer given there are multiple tracks | 
 | 11392 |         // active. | 
 | 11393 |         for (const auto& track : mActiveTracks) { | 
 | 11394 |             const int trackId = track->id(); | 
 | 11395 |             mAudioMixer->setParameter( | 
 | 11396 |                         trackId, AudioMixer::TRACK, AudioMixer::TEE_BUFFER, nullptr); | 
 | 11397 |         } | 
 | 11398 |     } | 
| jiabin | 76d9469 | 2022-12-15 21:51:21 +0000 | [diff] [blame] | 11399 |     if (mVolumeLeft != volumeLeft || mVolumeRight != volumeRight) { | 
 | 11400 |         mVolumeLeft = volumeLeft; | 
 | 11401 |         mVolumeRight = volumeRight; | 
 | 11402 |         setVolumeForOutput_l(volumeLeft, volumeRight); | 
 | 11403 |     } | 
| jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 11404 |     return result; | 
 | 11405 | } | 
 | 11406 |  | 
| Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11407 | void BitPerfectThread::threadLoop_mix() { | 
| jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 11408 |     MixerThread::threadLoop_mix(); | 
 | 11409 |     mHasDataCopiedToSinkBuffer = mIsBitPerfect; | 
 | 11410 | } | 
 | 11411 |  | 
| Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 11412 | } // namespace android |