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 | 25a80ac | 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 | |
Atneya Nair | f94040f | 2024-10-07 16:00:49 -0700 | [diff] [blame] | 30 | #include <afutils/FallibleLockGuard.h> |
Andy Hung | 25a80ac | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 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> |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 36 | #include <com_android_media_audioserver.h> |
Andy Hung | 25a80ac | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 37 | #ifdef DEBUG_CPU_USAGE |
| 38 | #include <audio_utils/Statistics.h> |
| 39 | #include <cpustats/ThreadCpuUsage.h> |
| 40 | #endif |
| 41 | #include <audio_utils/channels.h> |
| 42 | #include <audio_utils/format.h> |
| 43 | #include <audio_utils/minifloat.h> |
| 44 | #include <audio_utils/mono_blend.h> |
| 45 | #include <audio_utils/primitives.h> |
| 46 | #include <audio_utils/safe_math.h> |
| 47 | #include <audiomanager/AudioManager.h> |
| 48 | #include <binder/IPCThreadState.h> |
| 49 | #include <binder/IServiceManager.h> |
| 50 | #include <binder/PersistableBundle.h> |
Eric Laurent | 4eb45d0 | 2023-12-20 12:07:17 +0100 | [diff] [blame] | 51 | #include <com_android_media_audio.h> |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 52 | #include <com_android_media_audioserver.h> |
Glenn Kasten | c9a2367 | 2020-06-30 12:12:42 -0700 | [diff] [blame] | 53 | #include <cutils/bitops.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 54 | #include <cutils/properties.h> |
Andy Hung | 25a80ac | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 55 | #include <fastpath/AutoPark.h> |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 56 | #include <media/AudioContainers.h> |
| 57 | #include <media/AudioDeviceTypeAddr.h> |
Glenn Kasten | 1ab85ec | 2013-05-31 09:18:43 -0700 | [diff] [blame] | 58 | #include <media/AudioParameter.h> |
Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 59 | #include <media/AudioResamplerPublic.h> |
Andy Hung | 25a80ac | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 60 | #ifdef ADD_BATTERY_DATA |
| 61 | #include <media/IMediaPlayerService.h> |
| 62 | #include <media/IMediaDeathNotifier.h> |
| 63 | #endif |
| 64 | #include <media/MmapStreamCallback.h> |
Andy Hung | 8981605 | 2017-01-11 17:08:23 -0800 | [diff] [blame] | 65 | #include <media/RecordBufferConverter.h> |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 66 | #include <media/TypeConverter.h> |
Andy Hung | 25a80ac | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 67 | #include <media/audiohal/EffectsFactoryHalInterface.h> |
| 68 | #include <media/audiohal/StreamHalInterface.h> |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 69 | #include <media/nbaio/AudioStreamInSource.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 70 | #include <media/nbaio/AudioStreamOutSink.h> |
| 71 | #include <media/nbaio/MonoPipe.h> |
| 72 | #include <media/nbaio/MonoPipeReader.h> |
| 73 | #include <media/nbaio/Pipe.h> |
| 74 | #include <media/nbaio/PipeReader.h> |
| 75 | #include <media/nbaio/SourceAudioBufferProvider.h> |
Atneya Nair | 5997a65 | 2024-06-14 17:24:45 -0700 | [diff] [blame] | 76 | #include <media/ValidatedAttributionSourceState.h> |
Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 77 | #include <mediautils/BatteryNotifier.h> |
Andy Hung | afc51db | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 78 | #include <mediautils/Process.h> |
Andy Hung | ab7ef30 | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 79 | #include <mediautils/SchedulingPolicyService.h> |
| 80 | #include <mediautils/ServiceUtilities.h> |
Andy Hung | 25a80ac | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 81 | #include <powermanager/PowerManager.h> |
| 82 | #include <private/android_filesystem_config.h> |
| 83 | #include <private/media/AudioTrackShared.h> |
Andy Hung | 88a7afe | 2024-08-12 20:00:46 -0700 | [diff] [blame] | 84 | #include <psh_utils/AudioPowerManager.h> |
Andy Hung | 25a80ac | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 85 | #include <system/audio_effects/effect_aec.h> |
| 86 | #include <system/audio_effects/effect_downmix.h> |
| 87 | #include <system/audio_effects/effect_ns.h> |
| 88 | #include <system/audio_effects/effect_spatializer.h> |
| 89 | #include <utils/Log.h> |
| 90 | #include <utils/Trace.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 91 | |
Andy Hung | 25a80ac | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 92 | #include <fcntl.h> |
| 93 | #include <linux/futex.h> |
| 94 | #include <math.h> |
| 95 | #include <memory> |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 96 | #include <pthread.h> |
Andy Hung | 25a80ac | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 97 | #include <sstream> |
| 98 | #include <string> |
| 99 | #include <sys/stat.h> |
| 100 | #include <sys/syscall.h> |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 101 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 102 | // ---------------------------------------------------------------------------- |
| 103 | |
| 104 | // Note: the following macro is used for extremely verbose logging message. In |
| 105 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to |
| 106 | // 0; but one side effect of this is to turn all LOGV's as well. Some messages |
| 107 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT |
| 108 | // turned on. Do not uncomment the #def below unless you really know what you |
| 109 | // are doing and want to see all of the extremely verbose messages. |
| 110 | //#define VERY_VERY_VERBOSE_LOGGING |
| 111 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 112 | #define ALOGVV ALOGV |
| 113 | #else |
| 114 | #define ALOGVV(a...) do { } while(0) |
| 115 | #endif |
| 116 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 117 | // TODO: Move these macro/inlines to a header file. |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 118 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 119 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 120 | template <typename T> |
| 121 | static inline T min(const T& a, const T& b) |
| 122 | { |
| 123 | return a < b ? a : b; |
| 124 | } |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 125 | |
Atneya Nair | 5997a65 | 2024-06-14 17:24:45 -0700 | [diff] [blame] | 126 | using com::android::media::permission::ValidatedAttributionSourceState; |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 127 | namespace audioserver_flags = com::android::media::audioserver; |
Atneya Nair | 5997a65 | 2024-06-14 17:24:45 -0700 | [diff] [blame] | 128 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 129 | namespace android { |
| 130 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 131 | using audioflinger::SyncEvent; |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 132 | using media::IEffectClient; |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 133 | using content::AttributionSourceState; |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 134 | |
Andy Hung | 25a80ac | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 135 | // Keep in sync with java definition in media/java/android/media/AudioRecord.java |
| 136 | static constexpr int32_t kMaxSharedAudioHistoryMs = 5000; |
| 137 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 138 | // retry counts for buffer fill timeout |
| 139 | // 50 * ~20msecs = 1 second |
| 140 | static const int8_t kMaxTrackRetries = 50; |
| 141 | static const int8_t kMaxTrackStartupRetries = 50; |
Andy Hung | 455982f | 2021-04-27 17:46:12 -0700 | [diff] [blame] | 142 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 143 | // allow less retry attempts on direct output thread. |
| 144 | // direct outputs can be a scarce resource in audio hardware and should |
| 145 | // be released as quickly as possible. |
Andy Hung | 455982f | 2021-04-27 17:46:12 -0700 | [diff] [blame] | 146 | // Notes: |
| 147 | // 1) The retry duration kMaxTrackRetriesDirectMs may be increased |
| 148 | // in case the data write is bursty for the AudioTrack. The application |
| 149 | // should endeavor to write at least once every kMaxTrackRetriesDirectMs |
| 150 | // to prevent an underrun situation. If the data is bursty, then |
| 151 | // the application can also throttle the data sent to be even. |
| 152 | // 2) For compressed audio data, any data present in the AudioTrack buffer |
| 153 | // will be sent and reset the retry count. This delivers data as |
| 154 | // it arrives, with approximately kDirectMinSleepTimeUs = 10ms checking interval. |
| 155 | // 3) For linear PCM or proportional PCM, we wait one period for a period's worth |
| 156 | // of data to be available, then any remaining data is delivered. |
| 157 | // This is required to ensure the last bit of data is delivered before underrun. |
| 158 | // |
| 159 | // Sleep time per cycle is kDirectMinSleepTimeUs for compressed tracks |
| 160 | // or the size of the HAL period for proportional / linear PCM tracks. |
| 161 | static const int32_t kMaxTrackRetriesDirectMs = 200; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 162 | |
| 163 | // don't warn about blocked writes or record buffer overflows more often than this |
| 164 | static const nsecs_t kWarningThrottleNs = seconds(5); |
| 165 | |
| 166 | // RecordThread loop sleep time upon application overrun or audio HAL read error |
| 167 | static const int kRecordThreadSleepUs = 5000; |
| 168 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 169 | // maximum time to wait in sendConfigEvent_l() for a status to be received |
| 170 | static const nsecs_t kConfigEventTimeoutNs = seconds(2); |
Eric Laurent | 3fddffe | 2024-07-31 14:18:34 +0000 | [diff] [blame] | 171 | // longer timeout for create audio patch to account for specific scenarii |
| 172 | // with Bluetooth devices |
| 173 | static const nsecs_t kCreatePatchEventTimeoutNs = seconds(4); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 174 | |
| 175 | // minimum sleep time for the mixer thread loop when tracks are active but in underrun |
| 176 | static const uint32_t kMinThreadSleepTimeUs = 5000; |
| 177 | // maximum divider applied to the active sleep time in the mixer thread loop |
| 178 | static const uint32_t kMaxThreadSleepTimeShift = 2; |
| 179 | |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 180 | // minimum normal sink buffer size, expressed in milliseconds rather than frames |
Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 181 | // FIXME This should be based on experimentally observed scheduling jitter |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 182 | static const uint32_t kMinNormalSinkBufferSizeMs = 20; |
| 183 | // maximum normal sink buffer size |
| 184 | static const uint32_t kMaxNormalSinkBufferSizeMs = 24; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 185 | |
Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 186 | // minimum capture buffer size in milliseconds to _not_ need a fast capture thread |
| 187 | // FIXME This should be based on experimentally observed scheduling jitter |
| 188 | static const uint32_t kMinNormalCaptureBufferSizeMs = 12; |
| 189 | |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 190 | // Offloaded output thread standby delay: allows track transition without going to standby |
| 191 | static const nsecs_t kOffloadStandbyDelayNs = seconds(1); |
| 192 | |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 193 | // Direct output thread minimum sleep time in idle or active(underrun) state |
| 194 | static const nsecs_t kDirectMinSleepTimeUs = 10000; |
| 195 | |
Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 196 | // Minimum amount of time between checking to see if the timestamp is advancing |
| 197 | // for underrun detection. If we check too frequently, we may not detect a |
| 198 | // timestamp update and will falsely detect underrun. |
Andy Hung | 0ff1429 | 2023-12-20 15:55:16 -0800 | [diff] [blame] | 199 | static constexpr nsecs_t kMinimumTimeBetweenTimestampChecksNs = 150 /* ms */ * 1'000'000; |
Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 200 | |
Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 201 | // The universal constant for ubiquitous 20ms value. The value of 20ms seems to provide a good |
| 202 | // balance between power consumption and latency, and allows threads to be scheduled reliably |
| 203 | // by the CFS scheduler. |
| 204 | // FIXME Express other hardcoded references to 20ms with references to this constant and move |
| 205 | // it appropriately. |
| 206 | #define FMS_20 20 |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 207 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 208 | // Whether to use fast mixer |
| 209 | static const enum { |
| 210 | FastMixer_Never, // never initialize or use: for debugging only |
| 211 | FastMixer_Always, // always initialize and use, even if not needed: for debugging only |
| 212 | // normal mixer multiplier is 1 |
| 213 | FastMixer_Static, // initialize if needed, then use all the time if initialized, |
| 214 | // multiplier is calculated based on min & max normal mixer buffer size |
| 215 | FastMixer_Dynamic, // initialize if needed, then use dynamically depending on track load, |
| 216 | // multiplier is calculated based on min & max normal mixer buffer size |
| 217 | // FIXME for FastMixer_Dynamic: |
| 218 | // Supporting this option will require fixing HALs that can't handle large writes. |
| 219 | // For example, one HAL implementation returns an error from a large write, |
| 220 | // and another HAL implementation corrupts memory, possibly in the sample rate converter. |
| 221 | // We could either fix the HAL implementations, or provide a wrapper that breaks |
| 222 | // up large writes into smaller ones, and the wrapper would need to deal with scheduler. |
| 223 | } kUseFastMixer = FastMixer_Static; |
| 224 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 225 | // Whether to use fast capture |
| 226 | static const enum { |
| 227 | FastCapture_Never, // never initialize or use: for debugging only |
| 228 | FastCapture_Always, // always initialize and use, even if not needed: for debugging only |
| 229 | FastCapture_Static, // initialize if needed, then use all the time if initialized |
| 230 | } kUseFastCapture = FastCapture_Static; |
| 231 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 232 | // Priorities for requestPriority |
| 233 | static const int kPriorityAudioApp = 2; |
| 234 | static const int kPriorityFastMixer = 3; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 235 | static const int kPriorityFastCapture = 3; |
Pattara Teerapong | 9a332c5 | 2024-01-26 08:18:05 +0000 | [diff] [blame] | 236 | // Request real-time priority for PlaybackThread in ARC |
| 237 | static const int kPriorityPlaybackThreadArc = 1; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 238 | |
Glenn Kasten | ea38ee7 | 2016-04-18 11:08:01 -0700 | [diff] [blame] | 239 | // IAudioFlinger::createTrack() has an in/out parameter 'pFrameCount' for the total size of the |
| 240 | // track buffer in shared memory. Zero on input means to use a default value. For fast tracks, |
| 241 | // AudioFlinger derives the default from HAL buffer size and 'fast track multiplier'. |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 242 | |
| 243 | // This is the default value, if not specified by property. |
Glenn Kasten | b5fed68 | 2013-12-03 09:06:43 -0800 | [diff] [blame] | 244 | static const int kFastTrackMultiplier = 2; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 245 | |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 246 | // The minimum and maximum allowed values |
| 247 | static const int kFastTrackMultiplierMin = 1; |
| 248 | static const int kFastTrackMultiplierMax = 2; |
| 249 | |
| 250 | // The actual value to use, which can be specified per-device via property af.fast_track_multiplier. |
| 251 | static int sFastTrackMultiplier = kFastTrackMultiplier; |
| 252 | |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 253 | // See Thread::readOnlyHeap(). |
| 254 | // Initially this heap is used to allocate client buffers for "fast" AudioRecord. |
| 255 | // Eventually it will be the single buffer that FastCapture writes into via HAL read(), |
| 256 | // 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] | 257 | static const size_t kRecordThreadReadOnlyHeapSize = 0xD000; |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 258 | |
Andy Hung | 25a80ac | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 259 | static const nsecs_t kDefaultStandbyTimeInNsecs = seconds(3); |
Andy Hung | 8fe87eb | 2023-07-20 21:31:38 -0700 | [diff] [blame] | 260 | |
| 261 | static nsecs_t getStandbyTimeInNanos() { |
| 262 | static nsecs_t standbyTimeInNanos = []() { |
| 263 | const int ms = property_get_int32("ro.audio.flinger_standbytime_ms", |
| 264 | kDefaultStandbyTimeInNsecs / NANOS_PER_MILLISECOND); |
| 265 | ALOGI("%s: Using %d ms as standby time", __func__, ms); |
| 266 | return milliseconds(ms); |
| 267 | }(); |
| 268 | return standbyTimeInNanos; |
| 269 | } |
| 270 | |
Andy Hung | 81994d6 | 2023-07-20 21:44:14 -0700 | [diff] [blame] | 271 | // Set kEnableExtendedChannels to true to enable greater than stereo output |
| 272 | // for the MixerThread and device sink. Number of channels allowed is |
| 273 | // FCC_2 <= channels <= FCC_LIMIT. |
| 274 | constexpr bool kEnableExtendedChannels = true; |
| 275 | |
| 276 | // Returns true if channel mask is permitted for the PCM sink in the MixerThread |
| 277 | /* static */ |
| 278 | bool IAfThreadBase::isValidPcmSinkChannelMask(audio_channel_mask_t channelMask) { |
| 279 | switch (audio_channel_mask_get_representation(channelMask)) { |
| 280 | case AUDIO_CHANNEL_REPRESENTATION_POSITION: { |
| 281 | // Haptic channel mask is only applicable for channel position mask. |
| 282 | const uint32_t channelCount = audio_channel_count_from_out_mask( |
| 283 | static_cast<audio_channel_mask_t>(channelMask & ~AUDIO_CHANNEL_HAPTIC_ALL)); |
| 284 | const uint32_t maxChannelCount = kEnableExtendedChannels |
| 285 | ? FCC_LIMIT : FCC_2; |
| 286 | if (channelCount < FCC_2 // mono is not supported at this time |
| 287 | || channelCount > maxChannelCount) { |
| 288 | return false; |
| 289 | } |
| 290 | // check that channelMask is the "canonical" one we expect for the channelCount. |
| 291 | return audio_channel_position_mask_is_out_canonical(channelMask); |
| 292 | } |
| 293 | case AUDIO_CHANNEL_REPRESENTATION_INDEX: |
| 294 | if (kEnableExtendedChannels) { |
| 295 | const uint32_t channelCount = audio_channel_count_from_out_mask(channelMask); |
| 296 | if (channelCount >= FCC_2 // mono is not supported at this time |
| 297 | && channelCount <= FCC_LIMIT) { |
| 298 | return true; |
| 299 | } |
| 300 | } |
| 301 | return false; |
| 302 | default: |
| 303 | return false; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | // Set kEnableExtendedPrecision to true to use extended precision in MixerThread |
| 308 | constexpr bool kEnableExtendedPrecision = true; |
| 309 | |
| 310 | // Returns true if format is permitted for the PCM sink in the MixerThread |
| 311 | /* static */ |
| 312 | bool IAfThreadBase::isValidPcmSinkFormat(audio_format_t format) { |
| 313 | switch (format) { |
| 314 | case AUDIO_FORMAT_PCM_16_BIT: |
| 315 | return true; |
| 316 | case AUDIO_FORMAT_PCM_FLOAT: |
| 317 | case AUDIO_FORMAT_PCM_24_BIT_PACKED: |
| 318 | case AUDIO_FORMAT_PCM_32_BIT: |
| 319 | case AUDIO_FORMAT_PCM_8_24_BIT: |
| 320 | return kEnableExtendedPrecision; |
| 321 | default: |
| 322 | return false; |
| 323 | } |
| 324 | } |
| 325 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 326 | // ---------------------------------------------------------------------------- |
| 327 | |
Andy Hung | 25a80ac | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 328 | // formatToString() needs to be exact for MediaMetrics purposes. |
| 329 | // Do not use media/TypeConverter.h toString(). |
| 330 | /* static */ |
| 331 | std::string IAfThreadBase::formatToString(audio_format_t format) { |
| 332 | std::string result; |
| 333 | FormatConverter::toString(format, result); |
| 334 | return result; |
| 335 | } |
| 336 | |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 337 | // TODO: move all toString helpers to audio.h |
| 338 | // under #ifdef __cplusplus #endif |
| 339 | static std::string patchSinksToString(const struct audio_patch *patch) |
| 340 | { |
| 341 | std::stringstream ss; |
| 342 | for (size_t i = 0; i < patch->num_sinks; ++i) { |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 343 | if (i > 0) { |
| 344 | ss << "|"; |
| 345 | } |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 346 | ss << "(" << toString(patch->sinks[i].ext.device.type) |
| 347 | << ", " << patch->sinks[i].ext.device.address << ")"; |
| 348 | } |
| 349 | return ss.str(); |
| 350 | } |
| 351 | |
| 352 | static std::string patchSourcesToString(const struct audio_patch *patch) |
| 353 | { |
| 354 | std::stringstream ss; |
| 355 | for (size_t i = 0; i < patch->num_sources; ++i) { |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 356 | if (i > 0) { |
| 357 | ss << "|"; |
| 358 | } |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 359 | ss << "(" << toString(patch->sources[i].ext.device.type) |
| 360 | << ", " << patch->sources[i].ext.device.address << ")"; |
| 361 | } |
| 362 | return ss.str(); |
| 363 | } |
| 364 | |
Andy Hung | 4bd53e7 | 2022-11-17 17:21:45 -0800 | [diff] [blame] | 365 | static std::string toString(audio_latency_mode_t mode) { |
| 366 | // 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] | 367 | const auto result = legacy2aidl_audio_latency_mode_t_AudioLatencyMode(mode); |
| 368 | return result.has_value() ? media::audio::common::toString(*result) : "UNKNOWN"; |
Andy Hung | 4bd53e7 | 2022-11-17 17:21:45 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | // Could be made a template, but other toString overloads for std::vector are confused. |
| 372 | static std::string toString(const std::vector<audio_latency_mode_t>& elements) { |
| 373 | std::string s("{ "); |
| 374 | for (const auto& e : elements) { |
| 375 | s.append(toString(e)); |
| 376 | s.append(" "); |
| 377 | } |
| 378 | s.append("}"); |
| 379 | return s; |
| 380 | } |
| 381 | |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 382 | static pthread_once_t sFastTrackMultiplierOnce = PTHREAD_ONCE_INIT; |
| 383 | |
| 384 | static void sFastTrackMultiplierInit() |
| 385 | { |
| 386 | char value[PROPERTY_VALUE_MAX]; |
| 387 | if (property_get("af.fast_track_multiplier", value, NULL) > 0) { |
| 388 | char *endptr; |
| 389 | unsigned long ul = strtoul(value, &endptr, 0); |
| 390 | if (*endptr == '\0' && kFastTrackMultiplierMin <= ul && ul <= kFastTrackMultiplierMax) { |
| 391 | sFastTrackMultiplier = (int) ul; |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | // ---------------------------------------------------------------------------- |
| 397 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 398 | #ifdef ADD_BATTERY_DATA |
| 399 | // To collect the amplifier usage |
| 400 | static void addBatteryData(uint32_t params) { |
| 401 | sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService(); |
| 402 | if (service == NULL) { |
| 403 | // it already logged |
| 404 | return; |
| 405 | } |
| 406 | |
| 407 | service->addBatteryData(params); |
| 408 | } |
| 409 | #endif |
| 410 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 411 | // Track the CLOCK_BOOTTIME versus CLOCK_MONOTONIC timebase offset |
| 412 | struct { |
| 413 | // call when you acquire a partial wakelock |
| 414 | void acquire(const sp<IBinder> &wakeLockToken) { |
| 415 | pthread_mutex_lock(&mLock); |
| 416 | if (wakeLockToken.get() == nullptr) { |
| 417 | adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME); |
| 418 | } else { |
| 419 | if (mCount == 0) { |
| 420 | adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME); |
| 421 | } |
| 422 | ++mCount; |
| 423 | } |
| 424 | pthread_mutex_unlock(&mLock); |
| 425 | } |
| 426 | |
| 427 | // call when you release a partial wakelock. |
| 428 | void release(const sp<IBinder> &wakeLockToken) { |
| 429 | if (wakeLockToken.get() == nullptr) { |
| 430 | return; |
| 431 | } |
| 432 | pthread_mutex_lock(&mLock); |
| 433 | if (--mCount < 0) { |
| 434 | ALOGE("negative wakelock count"); |
| 435 | mCount = 0; |
| 436 | } |
| 437 | pthread_mutex_unlock(&mLock); |
| 438 | } |
| 439 | |
| 440 | // retrieves the boottime timebase offset from monotonic. |
| 441 | int64_t getBoottimeOffset() { |
| 442 | pthread_mutex_lock(&mLock); |
| 443 | int64_t boottimeOffset = mBoottimeOffset; |
| 444 | pthread_mutex_unlock(&mLock); |
| 445 | return boottimeOffset; |
| 446 | } |
| 447 | |
| 448 | // Adjusts the timebase offset between TIMEBASE_MONOTONIC |
| 449 | // and the selected timebase. |
| 450 | // Currently only TIMEBASE_BOOTTIME is allowed. |
| 451 | // |
| 452 | // This only needs to be called upon acquiring the first partial wakelock |
| 453 | // after all other partial wakelocks are released. |
| 454 | // |
| 455 | // We do an empirical measurement of the offset rather than parsing |
| 456 | // /proc/timer_list since the latter is not a formal kernel ABI. |
| 457 | static void adjustTimebaseOffset(int64_t *offset, ExtendedTimestamp::Timebase timebase) { |
| 458 | int clockbase; |
| 459 | switch (timebase) { |
| 460 | case ExtendedTimestamp::TIMEBASE_BOOTTIME: |
| 461 | clockbase = SYSTEM_TIME_BOOTTIME; |
| 462 | break; |
| 463 | default: |
| 464 | LOG_ALWAYS_FATAL("invalid timebase %d", timebase); |
| 465 | break; |
| 466 | } |
| 467 | // try three times to get the clock offset, choose the one |
| 468 | // with the minimum gap in measurements. |
| 469 | const int tries = 3; |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 470 | nsecs_t bestGap = 0, measured = 0; // not required, initialized for clang-tidy |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 471 | for (int i = 0; i < tries; ++i) { |
| 472 | const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC); |
| 473 | const nsecs_t tbase = systemTime(clockbase); |
| 474 | const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC); |
| 475 | const nsecs_t gap = tmono2 - tmono; |
| 476 | if (i == 0 || gap < bestGap) { |
| 477 | bestGap = gap; |
| 478 | measured = tbase - ((tmono + tmono2) >> 1); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | // to avoid micro-adjusting, we don't change the timebase |
| 483 | // unless it is significantly different. |
| 484 | // |
| 485 | // Assumption: It probably takes more than toleranceNs to |
| 486 | // suspend and resume the device. |
| 487 | static int64_t toleranceNs = 10000; // 10 us |
| 488 | if (llabs(*offset - measured) > toleranceNs) { |
| 489 | ALOGV("Adjusting timebase offset old: %lld new: %lld", |
| 490 | (long long)*offset, (long long)measured); |
| 491 | *offset = measured; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | pthread_mutex_t mLock; |
| 496 | int32_t mCount; |
| 497 | int64_t mBoottimeOffset; |
| 498 | } gBoottime = { PTHREAD_MUTEX_INITIALIZER, 0, 0 }; // static, so use POD initialization |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 499 | |
| 500 | // ---------------------------------------------------------------------------- |
| 501 | // CPU Stats |
| 502 | // ---------------------------------------------------------------------------- |
| 503 | |
| 504 | class CpuStats { |
| 505 | public: |
| 506 | CpuStats(); |
| 507 | void sample(const String8 &title); |
| 508 | #ifdef DEBUG_CPU_USAGE |
| 509 | private: |
| 510 | ThreadCpuUsage mCpuUsage; // instantaneous thread CPU usage in wall clock ns |
Andy Hung | 16698b8 | 2018-08-01 10:48:38 -0700 | [diff] [blame] | 511 | 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] | 512 | |
Andy Hung | 16698b8 | 2018-08-01 10:48:38 -0700 | [diff] [blame] | 513 | audio_utils::Statistics<double> mHzStats; // statistics on thread CPU usage in cycles |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 514 | |
| 515 | int mCpuNum; // thread's current CPU number |
| 516 | int mCpukHz; // frequency of thread's current CPU in kHz |
| 517 | #endif |
| 518 | }; |
| 519 | |
| 520 | CpuStats::CpuStats() |
| 521 | #ifdef DEBUG_CPU_USAGE |
| 522 | : mCpuNum(-1), mCpukHz(-1) |
| 523 | #endif |
| 524 | { |
| 525 | } |
| 526 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 527 | void CpuStats::sample(const String8 &title |
| 528 | #ifndef DEBUG_CPU_USAGE |
| 529 | __unused |
| 530 | #endif |
| 531 | ) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 532 | #ifdef DEBUG_CPU_USAGE |
| 533 | // get current thread's delta CPU time in wall clock ns |
| 534 | double wcNs; |
| 535 | bool valid = mCpuUsage.sampleAndEnable(wcNs); |
| 536 | |
| 537 | // record sample for wall clock statistics |
| 538 | if (valid) { |
Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 539 | mWcStats.add(wcNs); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | // get the current CPU number |
| 543 | int cpuNum = sched_getcpu(); |
| 544 | |
| 545 | // get the current CPU frequency in kHz |
| 546 | int cpukHz = mCpuUsage.getCpukHz(cpuNum); |
| 547 | |
| 548 | // check if either CPU number or frequency changed |
| 549 | if (cpuNum != mCpuNum || cpukHz != mCpukHz) { |
| 550 | mCpuNum = cpuNum; |
| 551 | mCpukHz = cpukHz; |
| 552 | // ignore sample for purposes of cycles |
| 553 | valid = false; |
| 554 | } |
| 555 | |
| 556 | // if no change in CPU number or frequency, then record sample for cycle statistics |
| 557 | if (valid && mCpukHz > 0) { |
Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 558 | const double cycles = wcNs * cpukHz * 0.000001; |
| 559 | mHzStats.add(cycles); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 560 | } |
| 561 | |
Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 562 | const unsigned n = mWcStats.getN(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 563 | // mCpuUsage.elapsed() is expensive, so don't call it every loop |
| 564 | if ((n & 127) == 1) { |
Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 565 | const long long elapsed = mCpuUsage.elapsed(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 566 | if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { |
Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 567 | const double perLoop = elapsed / (double) n; |
| 568 | const double perLoop100 = perLoop * 0.01; |
| 569 | const double perLoop1k = perLoop * 0.001; |
| 570 | const double mean = mWcStats.getMean(); |
| 571 | const double stddev = mWcStats.getStdDev(); |
| 572 | const double minimum = mWcStats.getMin(); |
| 573 | const double maximum = mWcStats.getMax(); |
| 574 | const double meanCycles = mHzStats.getMean(); |
| 575 | const double stddevCycles = mHzStats.getStdDev(); |
| 576 | const double minCycles = mHzStats.getMin(); |
| 577 | const double maxCycles = mHzStats.getMax(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 578 | mCpuUsage.resetElapsed(); |
| 579 | mWcStats.reset(); |
| 580 | mHzStats.reset(); |
| 581 | ALOGD("CPU usage for %s over past %.1f secs\n" |
| 582 | " (%u mixer loops at %.1f mean ms per loop):\n" |
| 583 | " us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n" |
| 584 | " %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n" |
| 585 | " MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f", |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 586 | title.c_str(), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 587 | elapsed * .000000001, n, perLoop * .000001, |
| 588 | mean * .001, |
| 589 | stddev * .001, |
| 590 | minimum * .001, |
| 591 | maximum * .001, |
| 592 | mean / perLoop100, |
| 593 | stddev / perLoop100, |
| 594 | minimum / perLoop100, |
| 595 | maximum / perLoop100, |
| 596 | meanCycles / perLoop1k, |
| 597 | stddevCycles / perLoop1k, |
| 598 | minCycles / perLoop1k, |
| 599 | maxCycles / perLoop1k); |
| 600 | |
| 601 | } |
| 602 | } |
| 603 | #endif |
| 604 | }; |
| 605 | |
| 606 | // ---------------------------------------------------------------------------- |
| 607 | // ThreadBase |
| 608 | // ---------------------------------------------------------------------------- |
| 609 | |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 610 | // static |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 611 | const char* ThreadBase::threadTypeToString(ThreadBase::type_t type) |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 612 | { |
| 613 | switch (type) { |
| 614 | case MIXER: |
| 615 | return "MIXER"; |
| 616 | case DIRECT: |
| 617 | return "DIRECT"; |
| 618 | case DUPLICATING: |
| 619 | return "DUPLICATING"; |
| 620 | case RECORD: |
| 621 | return "RECORD"; |
| 622 | case OFFLOAD: |
| 623 | return "OFFLOAD"; |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 624 | case MMAP_PLAYBACK: |
| 625 | return "MMAP_PLAYBACK"; |
| 626 | case MMAP_CAPTURE: |
| 627 | return "MMAP_CAPTURE"; |
Eric Laurent | 1c5e2e3 | 2021-08-18 18:50:28 +0200 | [diff] [blame] | 628 | case SPATIALIZER: |
| 629 | return "SPATIALIZER"; |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 630 | case BIT_PERFECT: |
| 631 | return "BIT_PERFECT"; |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 632 | default: |
| 633 | return "unknown"; |
| 634 | } |
| 635 | } |
| 636 | |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 637 | ThreadBase::ThreadBase(const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id, |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 638 | type_t type, bool systemReady, bool isOut) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 639 | : Thread(false /*canCallJava*/), |
| 640 | mType(type), |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 641 | mAfThreadCallback(afThreadCallback), |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 642 | mThreadMetrics(std::string(AMEDIAMETRICS_KEY_PREFIX_AUDIO_THREAD) + std::to_string(id), |
| 643 | isOut), |
| 644 | mIsOut(isOut), |
Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 645 | // mSampleRate, mFrameCount, mChannelMask, mChannelCount, mFrameSize, mFormat, mBufferSize |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 646 | // are set by PlaybackThread::readOutputParameters_l() or |
| 647 | // RecordThread::readInputParameters_l() |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 648 | //FIXME: mStandby should be true here. Is this some kind of hack? |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 649 | mStandby(false), |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 650 | mAudioSource(AUDIO_SOURCE_DEFAULT), mId(id), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 651 | // mName will be set by concrete (non-virtual) subclass |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 652 | mDeathRecipient(new PMDeathRecipient(this)), |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 653 | mSystemReady(systemReady), |
| 654 | mSignalPending(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 655 | { |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 656 | mThreadMetrics.logConstructor(getpid(), threadTypeToString(type), id); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 657 | memset(&mPatch, 0, sizeof(struct audio_patch)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 658 | } |
| 659 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 660 | ThreadBase::~ThreadBase() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 661 | { |
Glenn Kasten | c6ae3c8 | 2013-07-17 09:08:51 -0700 | [diff] [blame] | 662 | // 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] | 663 | mConfigEvents.clear(); |
| 664 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 665 | // do not lock the mutex in destructor |
| 666 | releaseWakeLock_l(); |
| 667 | if (mPowerManager != 0) { |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 668 | sp<IBinder> binder = IInterface::asBinder(mPowerManager); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 669 | binder->unlinkToDeath(mDeathRecipient); |
| 670 | } |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 671 | |
| 672 | sendStatistics(true /* force */); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 673 | } |
| 674 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 675 | status_t ThreadBase::readyToRun() |
Glenn Kasten | cf04c2c | 2013-08-06 07:41:16 -0700 | [diff] [blame] | 676 | { |
| 677 | status_t status = initCheck(); |
| 678 | if (status == NO_ERROR) { |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 679 | ALOGI("AudioFlinger's thread %p tid=%d ready to run", this, getTid()); |
Glenn Kasten | cf04c2c | 2013-08-06 07:41:16 -0700 | [diff] [blame] | 680 | } else { |
| 681 | ALOGE("No working audio driver found."); |
| 682 | } |
| 683 | return status; |
| 684 | } |
| 685 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 686 | void ThreadBase::exit() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 687 | { |
| 688 | ALOGV("ThreadBase::exit"); |
| 689 | // do any cleanup required for exit to succeed |
| 690 | preExit(); |
| 691 | { |
| 692 | // This lock prevents the following race in thread (uniprocessor for illustration): |
| 693 | // if (!exitPending()) { |
| 694 | // // context switch from here to exit() |
| 695 | // // exit() calls requestExit(), what exitPending() observes |
| 696 | // // exit() calls signal(), which is dropped since no waiters |
| 697 | // // context switch back from exit() to here |
| 698 | // mWaitWorkCV.wait(...); |
| 699 | // // now thread is hung |
| 700 | // } |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 701 | audio_utils::lock_guard lock(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 702 | requestExit(); |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 703 | mWaitWorkCV.notify_all(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 704 | } |
| 705 | // When Thread::requestExitAndWait is made virtual and this method is renamed to |
| 706 | // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();" |
Andy Hung | 51e73d3 | 2024-03-21 19:43:05 -0700 | [diff] [blame] | 707 | |
| 708 | // For TimeCheck: track waiting on the thread join of getTid(). |
| 709 | audio_utils::mutex::scoped_join_wait_check sjw(getTid()); |
| 710 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 711 | requestExitAndWait(); |
| 712 | } |
| 713 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 714 | status_t ThreadBase::setParameters(const String8& keyValuePairs) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 715 | { |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 716 | ALOGV("ThreadBase::setParameters() %s", keyValuePairs.c_str()); |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 717 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 718 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 719 | return sendSetParameterConfigEvent_l(keyValuePairs); |
| 720 | } |
| 721 | |
| 722 | // sendConfigEvent_l() must be called with ThreadBase::mLock held |
| 723 | // Can temporarily release the lock if waiting for a reply from processConfigEvents_l(). |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 724 | status_t ThreadBase::sendConfigEvent_l(sp<ConfigEvent>& event) |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 725 | NO_THREAD_SAFETY_ANALYSIS // condition variable |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 726 | { |
| 727 | status_t status = NO_ERROR; |
| 728 | |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 729 | if (event->mRequiresSystemReady && !mSystemReady) { |
| 730 | event->mWaitStatus = false; |
| 731 | mPendingConfigEvents.add(event); |
| 732 | return status; |
| 733 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 734 | mConfigEvents.add(event); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 735 | ALOGV("sendConfigEvent_l() num events %zu event %d", mConfigEvents.size(), event->mType); |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 736 | mWaitWorkCV.notify_one(); |
| 737 | mutex().unlock(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 738 | { |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 739 | audio_utils::unique_lock _l(event->mutex()); |
Eric Laurent | 3fddffe | 2024-07-31 14:18:34 +0000 | [diff] [blame] | 740 | nsecs_t timeoutNs = event->mType == CFG_EVENT_CREATE_AUDIO_PATCH ? |
| 741 | kCreatePatchEventTimeoutNs : kConfigEventTimeoutNs; |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 742 | while (event->mWaitStatus) { |
Andy Hung | 02ea2a0 | 2024-01-25 17:02:30 -0800 | [diff] [blame] | 743 | if (event->mCondition.wait_for( |
Eric Laurent | 3fddffe | 2024-07-31 14:18:34 +0000 | [diff] [blame] | 744 | _l, std::chrono::nanoseconds(timeoutNs), getTid()) |
Andy Hung | 02ea2a0 | 2024-01-25 17:02:30 -0800 | [diff] [blame] | 745 | == std::cv_status::timeout) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 746 | event->mStatus = TIMED_OUT; |
| 747 | event->mWaitStatus = false; |
| 748 | } |
| 749 | } |
| 750 | status = event->mStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 751 | } |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 752 | mutex().lock(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 753 | return status; |
| 754 | } |
| 755 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 756 | void ThreadBase::sendIoConfigEvent(audio_io_config_event_t event, pid_t pid, |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 757 | audio_port_handle_t portId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 758 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 759 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 760 | sendIoConfigEvent_l(event, pid, portId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 761 | } |
| 762 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 763 | // sendIoConfigEvent_l() must be called with ThreadBase::mutex() held |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 764 | 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] | 765 | audio_port_handle_t portId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 766 | { |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 767 | // The audio statistics history is exponentially weighted to forget events |
| 768 | // about five or more seconds in the past. In order to have |
| 769 | // crisper statistics for mediametrics, we reset the statistics on |
| 770 | // an IoConfigEvent, to reflect different properties for a new device. |
| 771 | mIoJitterMs.reset(); |
| 772 | mLatencyMs.reset(); |
| 773 | mProcessTimeMs.reset(); |
Robert Wu | 06db0a3 | 2021-08-10 19:05:34 +0000 | [diff] [blame] | 774 | mMonopipePipeDepthStats.reset(); |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 775 | mTimestampVerifier.discontinuity(mTimestampVerifier.DISCONTINUITY_MODE_CONTINUOUS); |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 776 | |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 777 | sp<ConfigEvent> configEvent = (ConfigEvent *)new IoConfigEvent(event, pid, portId); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 778 | sendConfigEvent_l(configEvent); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 779 | } |
| 780 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 781 | 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] | 782 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 783 | audio_utils::lock_guard _l(mutex()); |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 784 | sendPrioConfigEvent_l(pid, tid, prio, forApp); |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 787 | // sendPrioConfigEvent_l() must be called with ThreadBase::mutex() held |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 788 | void ThreadBase::sendPrioConfigEvent_l( |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 789 | pid_t pid, pid_t tid, int32_t prio, bool forApp) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 790 | { |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 791 | sp<ConfigEvent> configEvent = (ConfigEvent *)new PrioConfigEvent(pid, tid, prio, forApp); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 792 | sendConfigEvent_l(configEvent); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 793 | } |
| 794 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 795 | // sendSetParameterConfigEvent_l() must be called with ThreadBase::mutex() held |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 796 | status_t ThreadBase::sendSetParameterConfigEvent_l(const String8& keyValuePair) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 797 | { |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 798 | sp<ConfigEvent> configEvent; |
| 799 | AudioParameter param(keyValuePair); |
| 800 | int value; |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 801 | if (param.getInt(String8(AudioParameter::keyMonoOutput), value) == NO_ERROR) { |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 802 | setMasterMono_l(value != 0); |
| 803 | if (param.size() == 1) { |
| 804 | return NO_ERROR; // should be a solo parameter - we don't pass down |
| 805 | } |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 806 | param.remove(String8(AudioParameter::keyMonoOutput)); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 807 | configEvent = new SetParameterConfigEvent(param.toString()); |
| 808 | } else { |
| 809 | configEvent = new SetParameterConfigEvent(keyValuePair); |
| 810 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 811 | return sendConfigEvent_l(configEvent); |
Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 812 | } |
| 813 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 814 | status_t ThreadBase::sendCreateAudioPatchConfigEvent( |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 815 | const struct audio_patch *patch, |
| 816 | audio_patch_handle_t *handle) |
| 817 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 818 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 819 | sp<ConfigEvent> configEvent = (ConfigEvent *)new CreateAudioPatchConfigEvent(*patch, *handle); |
| 820 | status_t status = sendConfigEvent_l(configEvent); |
| 821 | if (status == NO_ERROR) { |
| 822 | CreateAudioPatchConfigEventData *data = |
| 823 | (CreateAudioPatchConfigEventData *)configEvent->mData.get(); |
| 824 | *handle = data->mHandle; |
| 825 | } |
| 826 | return status; |
| 827 | } |
| 828 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 829 | status_t ThreadBase::sendReleaseAudioPatchConfigEvent( |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 830 | const audio_patch_handle_t handle) |
| 831 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 832 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 833 | sp<ConfigEvent> configEvent = (ConfigEvent *)new ReleaseAudioPatchConfigEvent(handle); |
| 834 | return sendConfigEvent_l(configEvent); |
| 835 | } |
| 836 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 837 | status_t ThreadBase::sendUpdateOutDeviceConfigEvent( |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 838 | const DeviceDescriptorBaseVector& outDevices) |
| 839 | { |
| 840 | if (type() != RECORD) { |
| 841 | // The update out device operation is only for record thread. |
| 842 | return INVALID_OPERATION; |
| 843 | } |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 844 | audio_utils::lock_guard _l(mutex()); |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 845 | sp<ConfigEvent> configEvent = (ConfigEvent *)new UpdateOutDevicesConfigEvent(outDevices); |
| 846 | return sendConfigEvent_l(configEvent); |
| 847 | } |
| 848 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 849 | void ThreadBase::sendResizeBufferConfigEvent_l(int32_t maxSharedAudioHistoryMs) |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 850 | { |
| 851 | ALOG_ASSERT(type() == RECORD, "sendResizeBufferConfigEvent_l() called on non record thread"); |
| 852 | sp<ConfigEvent> configEvent = |
| 853 | (ConfigEvent *)new ResizeBufferConfigEvent(maxSharedAudioHistoryMs); |
| 854 | sendConfigEvent_l(configEvent); |
| 855 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 856 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 857 | void ThreadBase::sendCheckOutputStageEffectsEvent() |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 858 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 859 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 860 | sendCheckOutputStageEffectsEvent_l(); |
| 861 | } |
| 862 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 863 | void ThreadBase::sendCheckOutputStageEffectsEvent_l() |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 864 | { |
| 865 | sp<ConfigEvent> configEvent = |
| 866 | (ConfigEvent *)new CheckOutputStageEffectsEvent(); |
| 867 | sendConfigEvent_l(configEvent); |
| 868 | } |
| 869 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 870 | void ThreadBase::sendHalLatencyModesChangedEvent_l() |
Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 871 | { |
| 872 | sp<ConfigEvent> configEvent = sp<HalLatencyModesChangedEvent>::make(); |
| 873 | sendConfigEvent_l(configEvent); |
| 874 | } |
| 875 | |
Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 876 | // post condition: mConfigEvents.isEmpty() |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 877 | void ThreadBase::processConfigEvents_l() |
Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 878 | { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 879 | bool configChanged = false; |
| 880 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 881 | while (!mConfigEvents.isEmpty()) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 882 | ALOGV("processConfigEvents_l() remaining events %zu", mConfigEvents.size()); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 883 | sp<ConfigEvent> event = mConfigEvents[0]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 884 | mConfigEvents.removeAt(0); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 885 | switch (event->mType) { |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 886 | case CFG_EVENT_PRIO: { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 887 | PrioConfigEventData *data = (PrioConfigEventData *)event->mData.get(); |
| 888 | // FIXME Need to understand why this has to be done asynchronously |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 889 | int err = requestPriority(data->mPid, data->mTid, data->mPrio, data->mForApp, |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 890 | true /*asynchronous*/); |
| 891 | if (err != 0) { |
| 892 | 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] | 893 | data->mPrio, data->mPid, data->mTid, err); |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 894 | } |
| 895 | } break; |
| 896 | case CFG_EVENT_IO: { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 897 | IoConfigEventData *data = (IoConfigEventData *)event->mData.get(); |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 898 | ioConfigChanged_l(data->mEvent, data->mPid, data->mPortId); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 899 | } break; |
| 900 | case CFG_EVENT_SET_PARAMETER: { |
| 901 | SetParameterConfigEventData *data = (SetParameterConfigEventData *)event->mData.get(); |
| 902 | if (checkForNewParameter_l(data->mKeyValuePairs, event->mStatus)) { |
| 903 | configChanged = true; |
Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 904 | mLocalLog.log("CFG_EVENT_SET_PARAMETER: (%s) configuration changed", |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 905 | data->mKeyValuePairs.c_str()); |
Glenn Kasten | d5418eb | 2013-08-14 13:11:06 -0700 | [diff] [blame] | 906 | } |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 907 | } break; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 908 | case CFG_EVENT_CREATE_AUDIO_PATCH: { |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 909 | const DeviceTypeSet oldDevices = getDeviceTypes_l(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 910 | CreateAudioPatchConfigEventData *data = |
| 911 | (CreateAudioPatchConfigEventData *)event->mData.get(); |
| 912 | event->mStatus = createAudioPatch_l(&data->mPatch, &data->mHandle); |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 913 | const DeviceTypeSet newDevices = getDeviceTypes_l(); |
Eric Laurent | 5256814 | 2022-10-28 11:23:28 +0200 | [diff] [blame] | 914 | configChanged = oldDevices != newDevices; |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 915 | mLocalLog.log("CFG_EVENT_CREATE_AUDIO_PATCH: old device %s (%s) new device %s (%s)", |
| 916 | dumpDeviceTypes(oldDevices).c_str(), toString(oldDevices).c_str(), |
| 917 | dumpDeviceTypes(newDevices).c_str(), toString(newDevices).c_str()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 918 | } break; |
| 919 | case CFG_EVENT_RELEASE_AUDIO_PATCH: { |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 920 | const DeviceTypeSet oldDevices = getDeviceTypes_l(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 921 | ReleaseAudioPatchConfigEventData *data = |
| 922 | (ReleaseAudioPatchConfigEventData *)event->mData.get(); |
| 923 | event->mStatus = releaseAudioPatch_l(data->mHandle); |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 924 | const DeviceTypeSet newDevices = getDeviceTypes_l(); |
Eric Laurent | 5256814 | 2022-10-28 11:23:28 +0200 | [diff] [blame] | 925 | configChanged = oldDevices != newDevices; |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 926 | mLocalLog.log("CFG_EVENT_RELEASE_AUDIO_PATCH: old device %s (%s) new device %s (%s)", |
| 927 | dumpDeviceTypes(oldDevices).c_str(), toString(oldDevices).c_str(), |
| 928 | dumpDeviceTypes(newDevices).c_str(), toString(newDevices).c_str()); |
| 929 | } break; |
| 930 | case CFG_EVENT_UPDATE_OUT_DEVICE: { |
| 931 | UpdateOutDevicesConfigEventData *data = |
| 932 | (UpdateOutDevicesConfigEventData *)event->mData.get(); |
| 933 | updateOutDevices(data->mOutDevices); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 934 | } break; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 935 | case CFG_EVENT_RESIZE_BUFFER: { |
| 936 | ResizeBufferConfigEventData *data = |
| 937 | (ResizeBufferConfigEventData *)event->mData.get(); |
| 938 | resizeInputBuffer_l(data->mMaxSharedAudioHistoryMs); |
| 939 | } break; |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 940 | |
| 941 | case CFG_EVENT_CHECK_OUTPUT_STAGE_EFFECTS: { |
| 942 | setCheckOutputStageEffects(); |
| 943 | } break; |
| 944 | |
Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 945 | case CFG_EVENT_HAL_LATENCY_MODES_CHANGED: { |
| 946 | onHalLatencyModesChanged_l(); |
| 947 | } break; |
| 948 | |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 949 | default: |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 950 | ALOG_ASSERT(false, "processConfigEvents_l() unknown event type %d", event->mType); |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 951 | break; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 952 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 953 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 954 | audio_utils::lock_guard _l(event->mutex()); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 955 | if (event->mWaitStatus) { |
| 956 | event->mWaitStatus = false; |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 957 | event->mCondition.notify_one(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 958 | } |
| 959 | } |
| 960 | ALOGV_IF(mConfigEvents.isEmpty(), "processConfigEvents_l() DONE thread %p", this); |
| 961 | } |
| 962 | |
| 963 | if (configChanged) { |
| 964 | cacheParameters_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 965 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 966 | } |
| 967 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 968 | String8 channelMaskToString(audio_channel_mask_t mask, bool output) { |
| 969 | String8 s; |
Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 970 | const audio_channel_representation_t representation = |
| 971 | audio_channel_mask_get_representation(mask); |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 972 | |
| 973 | switch (representation) { |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 974 | // Travel all single bit channel mask to convert channel mask to string. |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 975 | case AUDIO_CHANNEL_REPRESENTATION_POSITION: { |
| 976 | if (output) { |
| 977 | if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT) s.append("front-left, "); |
| 978 | if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT) s.append("front-right, "); |
| 979 | if (mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) s.append("front-center, "); |
Andy Hung | fba7125 | 2021-05-07 11:58:32 -0700 | [diff] [blame] | 980 | if (mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) s.append("low-frequency, "); |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 981 | if (mask & AUDIO_CHANNEL_OUT_BACK_LEFT) s.append("back-left, "); |
| 982 | if (mask & AUDIO_CHANNEL_OUT_BACK_RIGHT) s.append("back-right, "); |
| 983 | if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER) s.append("front-left-of-center, "); |
| 984 | if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER) s.append("front-right-of-center, "); |
| 985 | if (mask & AUDIO_CHANNEL_OUT_BACK_CENTER) s.append("back-center, "); |
| 986 | if (mask & AUDIO_CHANNEL_OUT_SIDE_LEFT) s.append("side-left, "); |
| 987 | if (mask & AUDIO_CHANNEL_OUT_SIDE_RIGHT) s.append("side-right, "); |
| 988 | if (mask & AUDIO_CHANNEL_OUT_TOP_CENTER) s.append("top-center ,"); |
| 989 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT) s.append("top-front-left, "); |
| 990 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER) s.append("top-front-center, "); |
| 991 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT) s.append("top-front-right, "); |
| 992 | 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] | 993 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_CENTER) s.append("top-back-center, "); |
| 994 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT) s.append("top-back-right, "); |
| 995 | if (mask & AUDIO_CHANNEL_OUT_TOP_SIDE_LEFT) s.append("top-side-left, "); |
| 996 | if (mask & AUDIO_CHANNEL_OUT_TOP_SIDE_RIGHT) s.append("top-side-right, "); |
| 997 | if (mask & AUDIO_CHANNEL_OUT_BOTTOM_FRONT_LEFT) s.append("bottom-front-left, "); |
| 998 | if (mask & AUDIO_CHANNEL_OUT_BOTTOM_FRONT_CENTER) s.append("bottom-front-center, "); |
| 999 | 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] | 1000 | 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] | 1001 | if (mask & AUDIO_CHANNEL_OUT_HAPTIC_B) s.append("haptic-B, "); |
| 1002 | if (mask & AUDIO_CHANNEL_OUT_HAPTIC_A) s.append("haptic-A, "); |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 1003 | if (mask & ~AUDIO_CHANNEL_OUT_ALL) s.append("unknown, "); |
| 1004 | } else { |
| 1005 | if (mask & AUDIO_CHANNEL_IN_LEFT) s.append("left, "); |
| 1006 | if (mask & AUDIO_CHANNEL_IN_RIGHT) s.append("right, "); |
| 1007 | if (mask & AUDIO_CHANNEL_IN_FRONT) s.append("front, "); |
| 1008 | if (mask & AUDIO_CHANNEL_IN_BACK) s.append("back, "); |
| 1009 | if (mask & AUDIO_CHANNEL_IN_LEFT_PROCESSED) s.append("left-processed, "); |
| 1010 | if (mask & AUDIO_CHANNEL_IN_RIGHT_PROCESSED) s.append("right-processed, "); |
| 1011 | if (mask & AUDIO_CHANNEL_IN_FRONT_PROCESSED) s.append("front-processed, "); |
| 1012 | if (mask & AUDIO_CHANNEL_IN_BACK_PROCESSED) s.append("back-processed, "); |
| 1013 | if (mask & AUDIO_CHANNEL_IN_PRESSURE) s.append("pressure, "); |
| 1014 | if (mask & AUDIO_CHANNEL_IN_X_AXIS) s.append("X, "); |
| 1015 | if (mask & AUDIO_CHANNEL_IN_Y_AXIS) s.append("Y, "); |
| 1016 | if (mask & AUDIO_CHANNEL_IN_Z_AXIS) s.append("Z, "); |
Mikhail Naganov | c994849 | 2018-05-10 17:25:28 -0700 | [diff] [blame] | 1017 | if (mask & AUDIO_CHANNEL_IN_BACK_LEFT) s.append("back-left, "); |
| 1018 | if (mask & AUDIO_CHANNEL_IN_BACK_RIGHT) s.append("back-right, "); |
| 1019 | if (mask & AUDIO_CHANNEL_IN_CENTER) s.append("center, "); |
Andy Hung | fba7125 | 2021-05-07 11:58:32 -0700 | [diff] [blame] | 1020 | if (mask & AUDIO_CHANNEL_IN_LOW_FREQUENCY) s.append("low-frequency, "); |
Andy Hung | ae81b4c | 2021-05-07 08:54:28 -0700 | [diff] [blame] | 1021 | if (mask & AUDIO_CHANNEL_IN_TOP_LEFT) s.append("top-left, "); |
| 1022 | if (mask & AUDIO_CHANNEL_IN_TOP_RIGHT) s.append("top-right, "); |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 1023 | if (mask & AUDIO_CHANNEL_IN_VOICE_UPLINK) s.append("voice-uplink, "); |
| 1024 | if (mask & AUDIO_CHANNEL_IN_VOICE_DNLINK) s.append("voice-dnlink, "); |
| 1025 | if (mask & ~AUDIO_CHANNEL_IN_ALL) s.append("unknown, "); |
| 1026 | } |
| 1027 | const int len = s.length(); |
| 1028 | if (len > 2) { |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 1029 | (void) s.lockBuffer(len); // needed? |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 1030 | s.unlockBuffer(len - 2); // remove trailing ", " |
| 1031 | } |
| 1032 | return s; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1033 | } |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 1034 | case AUDIO_CHANNEL_REPRESENTATION_INDEX: |
| 1035 | s.appendFormat("index mask, bits:%#x", audio_channel_mask_get_bits(mask)); |
| 1036 | return s; |
| 1037 | default: |
| 1038 | s.appendFormat("unknown mask, representation:%d bits:%#x", |
| 1039 | representation, audio_channel_mask_get_bits(mask)); |
| 1040 | return s; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1041 | } |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1042 | } |
| 1043 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1044 | void ThreadBase::dump(int fd, const Vector<String16>& args) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1045 | { |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 1046 | dprintf(fd, "\n%s thread %p, name %s, tid %d, type %d (%s):\n", isOutput() ? "Output" : "Input", |
| 1047 | this, mThreadName, getTid(), type(), threadTypeToString(type())); |
| 1048 | |
Atneya Nair | f94040f | 2024-10-07 16:00:49 -0700 | [diff] [blame] | 1049 | { |
| 1050 | afutils::FallibleLockGuard l{mutex()}; |
| 1051 | if (!l) { |
| 1052 | dprintf(fd, " Thread may be deadlocked\n"); |
| 1053 | } |
| 1054 | dumpBase_l(fd, args); |
| 1055 | dumpInternals_l(fd, args); |
| 1056 | dumpTracks_l(fd, args); |
| 1057 | dumpEffectChains_l(fd, args); |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | dprintf(fd, " Local log:\n"); |
Atneya Nair | aa3afcb | 2024-10-08 16:36:19 -0700 | [diff] [blame] | 1061 | const auto logHeader = this->getLocalLogHeader(); |
| 1062 | write(fd, logHeader.data(), logHeader.length()); |
Atneya Nair | 0423af9 | 2024-10-07 21:23:29 -0700 | [diff] [blame] | 1063 | mLocalLog.dump(fd, " " /* prefix */); |
Andy Hung | afc51db | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 1064 | |
| 1065 | // --all does the statistics |
| 1066 | bool dumpAll = false; |
| 1067 | for (const auto &arg : args) { |
| 1068 | if (arg == String16("--all")) { |
| 1069 | dumpAll = true; |
| 1070 | } |
| 1071 | } |
| 1072 | if (dumpAll || type() == SPATIALIZER) { |
Andy Hung | 44d648b | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 1073 | const std::string sched = mThreadSnapshot.toString(); |
Andy Hung | afc51db | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 1074 | if (!sched.empty()) { |
| 1075 | (void)write(fd, sched.c_str(), sched.size()); |
| 1076 | } |
| 1077 | } |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 1078 | } |
| 1079 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1080 | void ThreadBase::dumpBase_l(int fd, const Vector<String16>& /* args */) |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 1081 | { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1082 | dprintf(fd, " I/O handle: %d\n", mId); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1083 | dprintf(fd, " Standby: %s\n", mStandby ? "yes" : "no"); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 1084 | dprintf(fd, " Sample rate: %u Hz\n", mSampleRate); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1085 | dprintf(fd, " HAL frame count: %zu\n", mFrameCount); |
Andy Hung | 25a80ac | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 1086 | dprintf(fd, " HAL format: 0x%x (%s)\n", mHALFormat, |
| 1087 | IAfThreadBase::formatToString(mHALFormat).c_str()); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1088 | dprintf(fd, " HAL buffer size: %zu bytes\n", mBufferSize); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 1089 | dprintf(fd, " Channel count: %u\n", mChannelCount); |
| 1090 | dprintf(fd, " Channel mask: 0x%08x (%s)\n", mChannelMask, |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 1091 | channelMaskToString(mChannelMask, mType != RECORD).c_str()); |
Andy Hung | 25a80ac | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 1092 | dprintf(fd, " Processing format: 0x%x (%s)\n", mFormat, |
| 1093 | IAfThreadBase::formatToString(mFormat).c_str()); |
Glenn Kasten | f87c2f5 | 2015-08-21 08:03:57 -0700 | [diff] [blame] | 1094 | dprintf(fd, " Processing frame size: %zu bytes\n", mFrameSize); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1095 | dprintf(fd, " Pending config events:"); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1096 | size_t numConfig = mConfigEvents.size(); |
| 1097 | if (numConfig) { |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 1098 | const size_t SIZE = 256; |
| 1099 | char buffer[SIZE]; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1100 | for (size_t i = 0; i < numConfig; i++) { |
| 1101 | mConfigEvents[i]->dump(buffer, SIZE); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1102 | dprintf(fd, "\n %s", buffer); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1103 | } |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1104 | dprintf(fd, "\n"); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1105 | } else { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1106 | dprintf(fd, " none\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1107 | } |
Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 1108 | // 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] | 1109 | dprintf(fd, " Output devices: %s (%s)\n", |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1110 | dumpDeviceTypes(outDeviceTypes_l()).c_str(), toString(outDeviceTypes_l()).c_str()); |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 1111 | dprintf(fd, " Input device: %#x (%s)\n", |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1112 | inDeviceType_l(), toString(inDeviceType_l()).c_str()); |
Andy Hung | 9b18195 | 2019-02-25 14:53:36 -0800 | [diff] [blame] | 1113 | dprintf(fd, " Audio source: %d (%s)\n", mAudioSource, toString(mAudioSource).c_str()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1114 | |
Andy Hung | 2e2c0bb | 2018-06-11 19:13:11 -0700 | [diff] [blame] | 1115 | // Dump timestamp statistics for the Thread types that support it. |
| 1116 | if (mType == RECORD |
| 1117 | || mType == MIXER |
| 1118 | || mType == DUPLICATING |
Andy Hung | f323451 | 2018-07-03 14:51:47 -0700 | [diff] [blame] | 1119 | || mType == DIRECT |
Andy Hung | 4b7f54f | 2022-04-28 17:45:28 -0700 | [diff] [blame] | 1120 | || mType == OFFLOAD |
| 1121 | || mType == SPATIALIZER) { |
Andy Hung | 2e2c0bb | 2018-06-11 19:13:11 -0700 | [diff] [blame] | 1122 | dprintf(fd, " Timestamp stats: %s\n", mTimestampVerifier.toString().c_str()); |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1123 | dprintf(fd, " Timestamp corrected: %s\n", |
| 1124 | isTimestampCorrectionEnabled_l() ? "yes" : "no"); |
Andy Hung | 2e2c0bb | 2018-06-11 19:13:11 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 1127 | if (mLastIoBeginNs > 0) { // MMAP may not set this |
| 1128 | dprintf(fd, " Last %s occurred (msecs): %lld\n", |
| 1129 | isOutput() ? "write" : "read", |
| 1130 | (long long) (systemTime() - mLastIoBeginNs) / NANOS_PER_MILLISECOND); |
| 1131 | } |
| 1132 | |
| 1133 | if (mProcessTimeMs.getN() > 0) { |
| 1134 | dprintf(fd, " Process time ms stats: %s\n", mProcessTimeMs.toString().c_str()); |
| 1135 | } |
| 1136 | |
| 1137 | if (mIoJitterMs.getN() > 0) { |
| 1138 | dprintf(fd, " Hal %s jitter ms stats: %s\n", |
| 1139 | isOutput() ? "write" : "read", |
| 1140 | mIoJitterMs.toString().c_str()); |
| 1141 | } |
| 1142 | |
Andy Hung | e6c3711 | 2019-02-26 17:38:10 -0800 | [diff] [blame] | 1143 | if (mLatencyMs.getN() > 0) { |
| 1144 | dprintf(fd, " Threadloop %s latency stats: %s\n", |
| 1145 | isOutput() ? "write" : "read", |
| 1146 | mLatencyMs.toString().c_str()); |
| 1147 | } |
Robert Wu | 06db0a3 | 2021-08-10 19:05:34 +0000 | [diff] [blame] | 1148 | |
| 1149 | if (mMonopipePipeDepthStats.getN() > 0) { |
| 1150 | dprintf(fd, " Monopipe %s pipe depth stats: %s\n", |
| 1151 | isOutput() ? "write" : "read", |
| 1152 | mMonopipePipeDepthStats.toString().c_str()); |
| 1153 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1154 | } |
| 1155 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1156 | void ThreadBase::dumpEffectChains_l(int fd, const Vector<String16>& args) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1157 | { |
| 1158 | const size_t SIZE = 256; |
| 1159 | char buffer[SIZE]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1160 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1161 | size_t numEffectChains = mEffectChains.size(); |
Narayan Kamath | 1d6fa7a | 2014-02-11 13:47:53 +0000 | [diff] [blame] | 1162 | snprintf(buffer, SIZE, " %zu Effect Chains\n", numEffectChains); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1163 | write(fd, buffer, strlen(buffer)); |
| 1164 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1165 | for (size_t i = 0; i < numEffectChains; ++i) { |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1166 | sp<IAfEffectChain> chain = mEffectChains[i]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1167 | if (chain != 0) { |
| 1168 | chain->dump(fd, args); |
| 1169 | } |
| 1170 | } |
| 1171 | } |
| 1172 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1173 | void ThreadBase::acquireWakeLock() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1174 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1175 | audio_utils::lock_guard _l(mutex()); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1176 | acquireWakeLock_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1177 | } |
| 1178 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1179 | String16 ThreadBase::getWakeLockTag() |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 1180 | { |
| 1181 | switch (mType) { |
Glenn Kasten | bcb1486 | 2015-03-05 17:11:21 -0800 | [diff] [blame] | 1182 | case MIXER: |
| 1183 | return String16("AudioMix"); |
| 1184 | case DIRECT: |
| 1185 | return String16("AudioDirectOut"); |
| 1186 | case DUPLICATING: |
| 1187 | return String16("AudioDup"); |
| 1188 | case RECORD: |
| 1189 | return String16("AudioIn"); |
| 1190 | case OFFLOAD: |
| 1191 | return String16("AudioOffload"); |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 1192 | case MMAP_PLAYBACK: |
| 1193 | return String16("MmapPlayback"); |
| 1194 | case MMAP_CAPTURE: |
| 1195 | return String16("MmapCapture"); |
Eric Laurent | 1c5e2e3 | 2021-08-18 18:50:28 +0200 | [diff] [blame] | 1196 | case SPATIALIZER: |
| 1197 | return String16("AudioSpatial"); |
jiabin | 10b2fb8 | 2024-09-03 17:51:35 +0000 | [diff] [blame] | 1198 | case BIT_PERFECT: |
| 1199 | return String16("AudioBitPerfect"); |
Glenn Kasten | bcb1486 | 2015-03-05 17:11:21 -0800 | [diff] [blame] | 1200 | default: |
| 1201 | ALOG_ASSERT(false); |
| 1202 | return String16("AudioUnknown"); |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 1203 | } |
| 1204 | } |
| 1205 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1206 | void ThreadBase::acquireWakeLock_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1207 | { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1208 | getPowerManager_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1209 | if (mPowerManager != 0) { |
| 1210 | sp<IBinder> binder = new BBinder(); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1211 | // Uses AID_AUDIOSERVER for wakelock. updateWakeLockUids_l() updates with client uids. |
Chris Ye | 6597d73 | 2020-02-28 22:38:25 -0800 | [diff] [blame] | 1212 | binder::Status status = mPowerManager->acquireWakeLockAsync(binder, |
| 1213 | POWERMANAGER_PARTIAL_WAKE_LOCK, |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 1214 | getWakeLockTag(), |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 1215 | String16("audioserver"), |
Chris Ye | 6597d73 | 2020-02-28 22:38:25 -0800 | [diff] [blame] | 1216 | {} /* workSource */, |
| 1217 | {} /* historyTag */); |
| 1218 | if (status.isOk()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1219 | mWakeLockToken = binder; |
Andy Hung | 88a7afe | 2024-08-12 20:00:46 -0700 | [diff] [blame] | 1220 | if (media::psh_utils::AudioPowerManager::enabled()) { |
| 1221 | mThreadToken = media::psh_utils::createAudioThreadToken( |
| 1222 | getTid(), String8(getWakeLockTag()).c_str()); |
| 1223 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1224 | } |
Chris Ye | 6597d73 | 2020-02-28 22:38:25 -0800 | [diff] [blame] | 1225 | ALOGV("acquireWakeLock_l() %s status %d", mThreadName, status.exceptionCode()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1226 | } |
Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 1227 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 1228 | gBoottime.acquire(mWakeLockToken); |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1229 | mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME] = |
| 1230 | gBoottime.getBoottimeOffset(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1231 | } |
| 1232 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1233 | void ThreadBase::releaseWakeLock() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1234 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1235 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1236 | releaseWakeLock_l(); |
| 1237 | } |
| 1238 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1239 | void ThreadBase::releaseWakeLock_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1240 | { |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 1241 | gBoottime.release(mWakeLockToken); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1242 | if (mWakeLockToken != 0) { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1243 | ALOGV("releaseWakeLock_l() %s", mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1244 | if (mPowerManager != 0) { |
Chris Ye | 6597d73 | 2020-02-28 22:38:25 -0800 | [diff] [blame] | 1245 | mPowerManager->releaseWakeLockAsync(mWakeLockToken, 0); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1246 | } |
| 1247 | mWakeLockToken.clear(); |
| 1248 | } |
Andy Hung | 88a7afe | 2024-08-12 20:00:46 -0700 | [diff] [blame] | 1249 | mThreadToken.reset(); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1250 | } |
| 1251 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1252 | void ThreadBase::getPowerManager_l() { |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1253 | if (mSystemReady && mPowerManager == 0) { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1254 | // use checkService() to avoid blocking if power service is not up yet |
| 1255 | sp<IBinder> binder = |
| 1256 | defaultServiceManager()->checkService(String16("power")); |
| 1257 | if (binder == 0) { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1258 | ALOGW("Thread %s cannot connect to the power manager service", mThreadName); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1259 | } else { |
Chris Ye | 6597d73 | 2020-02-28 22:38:25 -0800 | [diff] [blame] | 1260 | mPowerManager = interface_cast<os::IPowerManager>(binder); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1261 | binder->linkToDeath(mDeathRecipient); |
| 1262 | } |
| 1263 | } |
| 1264 | } |
| 1265 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1266 | void ThreadBase::updateWakeLockUids_l(const SortedVector<uid_t>& uids) { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1267 | getPowerManager_l(); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1268 | |
| 1269 | #if !LOG_NDEBUG |
| 1270 | std::stringstream s; |
Andy Hung | d01b0f1 | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 1271 | for (uid_t uid : uids) { |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1272 | s << uid << " "; |
| 1273 | } |
| 1274 | ALOGD("updateWakeLockUids_l %s uids:%s", mThreadName, s.str().c_str()); |
| 1275 | #endif |
| 1276 | |
Andy Hung | 438e757 | 2015-12-14 15:51:17 -0800 | [diff] [blame] | 1277 | if (mWakeLockToken == NULL) { // token may be NULL if AudioFlinger::systemReady() not called. |
| 1278 | if (mSystemReady) { |
| 1279 | ALOGE("no wake lock to update, but system ready!"); |
| 1280 | } else { |
| 1281 | ALOGW("no wake lock to update, system not ready yet"); |
| 1282 | } |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1283 | return; |
| 1284 | } |
| 1285 | if (mPowerManager != 0) { |
Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 1286 | 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] | 1287 | binder::Status status = mPowerManager->updateWakeLockUidsAsync( |
| 1288 | mWakeLockToken, uidsAsInt); |
| 1289 | ALOGV("updateWakeLockUids_l() %s status %d", mThreadName, status.exceptionCode()); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1290 | } |
| 1291 | } |
| 1292 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1293 | void ThreadBase::clearPowerManager() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1294 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1295 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1296 | releaseWakeLock_l(); |
| 1297 | mPowerManager.clear(); |
| 1298 | } |
| 1299 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1300 | void ThreadBase::updateOutDevices( |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 1301 | const DeviceDescriptorBaseVector& outDevices __unused) |
| 1302 | { |
| 1303 | ALOGE("%s should only be called in RecordThread", __func__); |
| 1304 | } |
| 1305 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1306 | void ThreadBase::resizeInputBuffer_l(int32_t /* maxSharedAudioHistoryMs */) |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 1307 | { |
| 1308 | ALOGE("%s should only be called in RecordThread", __func__); |
| 1309 | } |
| 1310 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1311 | void ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& /* who */) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1312 | { |
| 1313 | sp<ThreadBase> thread = mThread.promote(); |
| 1314 | if (thread != 0) { |
| 1315 | thread->clearPowerManager(); |
| 1316 | } |
| 1317 | ALOGW("power manager service died !!!"); |
| 1318 | } |
| 1319 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1320 | void ThreadBase::setEffectSuspended_l( |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1321 | const effect_uuid_t *type, bool suspend, audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1322 | { |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1323 | sp<IAfEffectChain> chain = getEffectChain_l(sessionId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1324 | if (chain != 0) { |
| 1325 | if (type != NULL) { |
| 1326 | chain->setEffectSuspended_l(type, suspend); |
| 1327 | } else { |
| 1328 | chain->setEffectSuspendedAll_l(suspend); |
| 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | updateSuspendedSessions_l(type, suspend, sessionId); |
| 1333 | } |
| 1334 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1335 | void ThreadBase::checkSuspendOnAddEffectChain_l(const sp<IAfEffectChain>& chain) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1336 | { |
| 1337 | ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId()); |
| 1338 | if (index < 0) { |
| 1339 | return; |
| 1340 | } |
| 1341 | |
| 1342 | const KeyedVector <int, sp<SuspendedSessionDesc> >& sessionEffects = |
| 1343 | mSuspendedSessions.valueAt(index); |
| 1344 | |
| 1345 | for (size_t i = 0; i < sessionEffects.size(); i++) { |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1346 | const sp<SuspendedSessionDesc>& desc = sessionEffects.valueAt(i); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1347 | for (int j = 0; j < desc->mRefCount; j++) { |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1348 | if (sessionEffects.keyAt(i) == IAfEffectChain::kKeyForSuspendAll) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1349 | chain->setEffectSuspendedAll_l(true); |
| 1350 | } else { |
| 1351 | ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", |
| 1352 | desc->mType.timeLow); |
| 1353 | chain->setEffectSuspended_l(&desc->mType, true); |
| 1354 | } |
| 1355 | } |
| 1356 | } |
| 1357 | } |
| 1358 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1359 | void ThreadBase::updateSuspendedSessions_l(const effect_uuid_t* type, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1360 | bool suspend, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1361 | audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1362 | { |
| 1363 | ssize_t index = mSuspendedSessions.indexOfKey(sessionId); |
| 1364 | |
| 1365 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; |
| 1366 | |
| 1367 | if (suspend) { |
| 1368 | if (index >= 0) { |
| 1369 | sessionEffects = mSuspendedSessions.valueAt(index); |
| 1370 | } else { |
| 1371 | mSuspendedSessions.add(sessionId, sessionEffects); |
| 1372 | } |
| 1373 | } else { |
| 1374 | if (index < 0) { |
| 1375 | return; |
| 1376 | } |
| 1377 | sessionEffects = mSuspendedSessions.valueAt(index); |
| 1378 | } |
| 1379 | |
| 1380 | |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1381 | int key = IAfEffectChain::kKeyForSuspendAll; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1382 | if (type != NULL) { |
| 1383 | key = type->timeLow; |
| 1384 | } |
| 1385 | index = sessionEffects.indexOfKey(key); |
| 1386 | |
| 1387 | sp<SuspendedSessionDesc> desc; |
| 1388 | if (suspend) { |
| 1389 | if (index >= 0) { |
| 1390 | desc = sessionEffects.valueAt(index); |
| 1391 | } else { |
| 1392 | desc = new SuspendedSessionDesc(); |
| 1393 | if (type != NULL) { |
| 1394 | desc->mType = *type; |
| 1395 | } |
| 1396 | sessionEffects.add(key, desc); |
| 1397 | ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); |
| 1398 | } |
| 1399 | desc->mRefCount++; |
| 1400 | } else { |
| 1401 | if (index < 0) { |
| 1402 | return; |
| 1403 | } |
| 1404 | desc = sessionEffects.valueAt(index); |
| 1405 | if (--desc->mRefCount == 0) { |
| 1406 | ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); |
| 1407 | sessionEffects.removeItemsAt(index); |
| 1408 | if (sessionEffects.isEmpty()) { |
| 1409 | ALOGV("updateSuspendedSessions_l() restore removing session %d", |
| 1410 | sessionId); |
| 1411 | mSuspendedSessions.removeItem(sessionId); |
| 1412 | } |
| 1413 | } |
| 1414 | } |
| 1415 | if (!sessionEffects.isEmpty()) { |
| 1416 | mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); |
| 1417 | } |
| 1418 | } |
| 1419 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1420 | void ThreadBase::checkSuspendOnEffectEnabled(bool enabled, |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1421 | audio_session_t sessionId, |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 1422 | bool threadLocked) |
| 1423 | NO_THREAD_SAFETY_ANALYSIS // manual locking |
| 1424 | { |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1425 | if (!threadLocked) { |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1426 | mutex().lock(); |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1427 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1428 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1429 | if (mType != RECORD) { |
| 1430 | // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on |
| 1431 | // another session. This gives the priority to well behaved effect control panels |
| 1432 | // and applications not using global effects. |
| 1433 | // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect |
| 1434 | // global effects |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 1435 | if (!audio_is_global_session(sessionId)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1436 | setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); |
| 1437 | } |
| 1438 | } |
| 1439 | |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1440 | if (!threadLocked) { |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1441 | mutex().unlock(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1442 | } |
| 1443 | } |
| 1444 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1445 | // checkEffectCompatibility_l() must be called with ThreadBase::mutex() held |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1446 | status_t RecordThread::checkEffectCompatibility_l( |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1447 | const effect_descriptor_t *desc, audio_session_t sessionId) |
| 1448 | { |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 1449 | // No global output effect sessions on record threads |
| 1450 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX |
| 1451 | || sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1452 | ALOGW("checkEffectCompatibility_l(): global effect %s on record thread %s", |
| 1453 | desc->name, mThreadName); |
| 1454 | return BAD_VALUE; |
| 1455 | } |
| 1456 | // only pre processing effects on record thread |
| 1457 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC) { |
| 1458 | ALOGW("checkEffectCompatibility_l(): non pre processing effect %s on record thread %s", |
| 1459 | desc->name, mThreadName); |
| 1460 | return BAD_VALUE; |
| 1461 | } |
Eric Laurent | 6dd0fd9 | 2016-09-15 12:44:53 -0700 | [diff] [blame] | 1462 | |
| 1463 | // always allow effects without processing load or latency |
| 1464 | if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) == EFFECT_FLAG_NO_PROCESS) { |
| 1465 | return NO_ERROR; |
| 1466 | } |
| 1467 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1468 | audio_input_flags_t flags = mInput->flags; |
| 1469 | if (hasFastCapture() || (flags & AUDIO_INPUT_FLAG_FAST)) { |
| 1470 | if (flags & AUDIO_INPUT_FLAG_RAW) { |
| 1471 | ALOGW("checkEffectCompatibility_l(): effect %s on record thread %s in raw mode", |
| 1472 | desc->name, mThreadName); |
| 1473 | return BAD_VALUE; |
| 1474 | } |
| 1475 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { |
| 1476 | ALOGW("checkEffectCompatibility_l(): non HW effect %s on record thread %s in fast mode", |
| 1477 | desc->name, mThreadName); |
| 1478 | return BAD_VALUE; |
| 1479 | } |
| 1480 | } |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 1481 | |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1482 | if (IAfEffectModule::isHapticGenerator(&desc->type)) { |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 1483 | ALOGE("%s(): HapticGenerator is not supported in RecordThread", __func__); |
| 1484 | return BAD_VALUE; |
| 1485 | } |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1486 | return NO_ERROR; |
| 1487 | } |
| 1488 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1489 | // checkEffectCompatibility_l() must be called with ThreadBase::mutex() held |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1490 | status_t PlaybackThread::checkEffectCompatibility_l( |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1491 | const effect_descriptor_t *desc, audio_session_t sessionId) |
| 1492 | { |
| 1493 | // no preprocessing on playback threads |
| 1494 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC) { |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1495 | ALOGW("%s: pre processing effect %s created on playback" |
| 1496 | " thread %s", __func__, desc->name, mThreadName); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1497 | return BAD_VALUE; |
| 1498 | } |
| 1499 | |
Eric Laurent | 3e4de77 | 2017-07-16 16:55:08 -0700 | [diff] [blame] | 1500 | // always allow effects without processing load or latency |
| 1501 | if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) == EFFECT_FLAG_NO_PROCESS) { |
| 1502 | return NO_ERROR; |
| 1503 | } |
| 1504 | |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1505 | if (IAfEffectModule::isHapticGenerator(&desc->type) && mHapticChannelCount == 0) { |
Shunkai Yao | 4c3af93 | 2024-04-26 04:12:21 +0000 | [diff] [blame] | 1506 | ALOGW("%s: thread (%s) doesn't support haptic playback while the effect is HapticGenerator", |
| 1507 | __func__, threadTypeToString(mType)); |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 1508 | return BAD_VALUE; |
| 1509 | } |
| 1510 | |
Eric Laurent | 4eb45d0 | 2023-12-20 12:07:17 +0100 | [diff] [blame] | 1511 | if (IAfEffectModule::isSpatializer(&desc->type) |
Eric Laurent | f690c46 | 2021-09-17 14:47:03 +0200 | [diff] [blame] | 1512 | && mType != SPATIALIZER) { |
| 1513 | ALOGW("%s: attempt to create a spatializer effect on a thread of type %d", |
| 1514 | __func__, mType); |
| 1515 | return BAD_VALUE; |
| 1516 | } |
| 1517 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1518 | switch (mType) { |
| 1519 | case MIXER: { |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1520 | audio_output_flags_t flags = mOutput->flags; |
| 1521 | if (hasFastMixer() || (flags & AUDIO_OUTPUT_FLAG_FAST)) { |
| 1522 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 1523 | // global effects are applied only to non fast tracks if they are SW |
| 1524 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { |
| 1525 | break; |
| 1526 | } |
| 1527 | } else if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
| 1528 | // only post processing on output stage session |
| 1529 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) { |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1530 | ALOGW("%s: non post processing effect %s not allowed on output stage session", |
| 1531 | __func__, desc->name); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1532 | return BAD_VALUE; |
| 1533 | } |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 1534 | } else if (sessionId == AUDIO_SESSION_DEVICE) { |
| 1535 | // only post processing on output stage session |
| 1536 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) { |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1537 | ALOGW("%s: non post processing effect %s not allowed on device session", |
| 1538 | __func__, desc->name); |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 1539 | return BAD_VALUE; |
| 1540 | } |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1541 | } else { |
| 1542 | // no restriction on effects applied on non fast tracks |
| 1543 | if ((hasAudioSession_l(sessionId) & ThreadBase::FAST_SESSION) == 0) { |
| 1544 | break; |
| 1545 | } |
| 1546 | } |
Eric Laurent | 6dd0fd9 | 2016-09-15 12:44:53 -0700 | [diff] [blame] | 1547 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1548 | if (flags & AUDIO_OUTPUT_FLAG_RAW) { |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1549 | 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] | 1550 | return BAD_VALUE; |
| 1551 | } |
| 1552 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1553 | ALOGW("%s: non HW effect %s on playback thread in fast mode", |
| 1554 | __func__, desc->name); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1555 | return BAD_VALUE; |
| 1556 | } |
| 1557 | } |
| 1558 | } break; |
| 1559 | case OFFLOAD: |
Jean-Michel Trivi | 773ee95 | 2016-07-11 16:53:18 -0700 | [diff] [blame] | 1560 | // nothing actionable on offload threads, if the effect: |
| 1561 | // - is offloadable: the effect can be created |
| 1562 | // - is NOT offloadable: the effect should still be created, but EffectHandle::enable() |
| 1563 | // will take care of invalidating the tracks of the thread |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1564 | break; |
| 1565 | case DIRECT: |
| 1566 | // Reject any effect on Direct output threads for now, since the format of |
| 1567 | // 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] | 1568 | ALOGW("%s: effect %s on DIRECT output thread %s", |
| 1569 | __func__, desc->name, mThreadName); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1570 | return BAD_VALUE; |
| 1571 | case DUPLICATING: |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 1572 | if (audio_is_global_session(sessionId)) { |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1573 | ALOGW("%s: global effect %s on DUPLICATING thread %s", |
| 1574 | __func__, desc->name, mThreadName); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1575 | return BAD_VALUE; |
| 1576 | } |
| 1577 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) { |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1578 | ALOGW("%s: post processing effect %s on DUPLICATING thread %s", |
| 1579 | __func__, desc->name, mThreadName); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1580 | return BAD_VALUE; |
| 1581 | } |
| 1582 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) != 0) { |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1583 | ALOGW("%s: HW tunneled effect %s on DUPLICATING thread %s", |
| 1584 | __func__, desc->name, mThreadName); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1585 | return BAD_VALUE; |
| 1586 | } |
| 1587 | break; |
Eric Laurent | 1c5e2e3 | 2021-08-18 18:50:28 +0200 | [diff] [blame] | 1588 | case SPATIALIZER: |
Shunkai Yao | 2dcd60c | 2024-08-27 21:08:53 +0000 | [diff] [blame] | 1589 | // Global effects (AUDIO_SESSION_OUTPUT_MIX) are supported on spatializer mixer, but only |
| 1590 | // the spatialized track have global effects applied for now. |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1591 | // Post processing effects (AUDIO_SESSION_OUTPUT_STAGE or AUDIO_SESSION_DEVICE) |
| 1592 | // are supported and added after the spatializer. |
| 1593 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
Shunkai Yao | 2dcd60c | 2024-08-27 21:08:53 +0000 | [diff] [blame] | 1594 | ALOGD("%s: global effect %s on spatializer thread %s", __func__, desc->name, |
| 1595 | mThreadName); |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1596 | } else if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
| 1597 | // only post processing , downmixer or spatializer effects on output stage session |
Eric Laurent | 4eb45d0 | 2023-12-20 12:07:17 +0100 | [diff] [blame] | 1598 | if (IAfEffectModule::isSpatializer(&desc->type) |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1599 | || memcmp(&desc->type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0) { |
| 1600 | break; |
| 1601 | } |
| 1602 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) { |
| 1603 | ALOGW("%s: non post processing effect %s not allowed on output stage session", |
| 1604 | __func__, desc->name); |
| 1605 | return BAD_VALUE; |
| 1606 | } |
| 1607 | } else if (sessionId == AUDIO_SESSION_DEVICE) { |
| 1608 | // only post processing on output stage session |
| 1609 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) { |
| 1610 | ALOGW("%s: non post processing effect %s not allowed on device session", |
| 1611 | __func__, desc->name); |
| 1612 | return BAD_VALUE; |
| 1613 | } |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 1614 | } |
| 1615 | break; |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 1616 | case BIT_PERFECT: |
| 1617 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) != 0) { |
| 1618 | // Allow HW accelerated effects of tunnel type |
| 1619 | break; |
| 1620 | } |
| 1621 | // As bit-perfect tracks will not be allowed to apply audio effect that will touch the audio |
| 1622 | // data, effects will not be allowed on 1) global effects (AUDIO_SESSION_OUTPUT_MIX), |
| 1623 | // 2) post-processing effects (AUDIO_SESSION_OUTPUT_STAGE or AUDIO_SESSION_DEVICE) and |
| 1624 | // 3) there is any bit-perfect track with the given session id. |
| 1625 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX || sessionId == AUDIO_SESSION_OUTPUT_STAGE || |
| 1626 | sessionId == AUDIO_SESSION_DEVICE) { |
| 1627 | ALOGW("%s: effect %s not supported on bit-perfect thread %s", |
| 1628 | __func__, desc->name, mThreadName); |
| 1629 | return BAD_VALUE; |
| 1630 | } else if ((hasAudioSession_l(sessionId) & ThreadBase::BIT_PERFECT_SESSION) != 0) { |
| 1631 | ALOGW("%s: effect %s not supported as there is a bit-perfect track with session as %d", |
| 1632 | __func__, desc->name, sessionId); |
| 1633 | return BAD_VALUE; |
| 1634 | } |
| 1635 | break; |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1636 | default: |
| 1637 | LOG_ALWAYS_FATAL("checkEffectCompatibility_l(): wrong thread type %d", mType); |
| 1638 | } |
| 1639 | |
| 1640 | return NO_ERROR; |
| 1641 | } |
| 1642 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1643 | // ThreadBase::createEffect_l() must be called with AudioFlinger::mutex() held |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1644 | sp<IAfEffectHandle> ThreadBase::createEffect_l( |
Andy Hung | 88035ac | 2023-06-27 17:05:02 -0700 | [diff] [blame] | 1645 | const sp<Client>& client, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1646 | const sp<IEffectClient>& effectClient, |
| 1647 | int32_t priority, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1648 | audio_session_t sessionId, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1649 | effect_descriptor_t *desc, |
| 1650 | int *enabled, |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1651 | status_t *status, |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 1652 | bool pinned, |
Eric Laurent | de8caf4 | 2021-08-11 17:19:25 +0200 | [diff] [blame] | 1653 | bool probe, |
| 1654 | bool notifyFramesProcessed) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1655 | { |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1656 | sp<IAfEffectModule> effect; |
| 1657 | sp<IAfEffectHandle> handle; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1658 | status_t lStatus; |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1659 | sp<IAfEffectChain> chain; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1660 | bool chainCreated = false; |
| 1661 | bool effectCreated = false; |
Mikhail Naganov | 2247f7b | 2017-01-13 11:52:54 -0800 | [diff] [blame] | 1662 | audio_unique_id_t effectId = AUDIO_UNIQUE_ID_USE_UNSPECIFIED; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1663 | |
| 1664 | lStatus = initCheck(); |
| 1665 | if (lStatus != NO_ERROR) { |
| 1666 | ALOGW("createEffect_l() Audio driver not initialized."); |
| 1667 | goto Exit; |
| 1668 | } |
| 1669 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1670 | ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId); |
| 1671 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1672 | { // scope for mutex() |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1673 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1674 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1675 | lStatus = checkEffectCompatibility_l(desc, sessionId); |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 1676 | if (probe || lStatus != NO_ERROR) { |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1677 | goto Exit; |
| 1678 | } |
| 1679 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1680 | // check for existing effect chain with the requested audio session |
| 1681 | chain = getEffectChain_l(sessionId); |
| 1682 | if (chain == 0) { |
| 1683 | // create a new chain for this session |
| 1684 | ALOGV("createEffect_l() new effect chain for session %d", sessionId); |
Shunkai Yao | 29d1057 | 2024-03-19 04:31:47 +0000 | [diff] [blame] | 1685 | chain = IAfEffectChain::create(this, sessionId, mAfThreadCallback); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1686 | addEffectChain_l(chain); |
| 1687 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 1688 | chainCreated = true; |
| 1689 | } else { |
Shunkai Yao | 29d1057 | 2024-03-19 04:31:47 +0000 | [diff] [blame] | 1690 | effect = chain->getEffectFromDesc(desc); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1691 | } |
| 1692 | |
| 1693 | ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get()); |
| 1694 | |
| 1695 | if (effect == 0) { |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 1696 | effectId = mAfThreadCallback->nextUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1697 | // create a new effect module if none present in the chain |
Shunkai Yao | 29d1057 | 2024-03-19 04:31:47 +0000 | [diff] [blame] | 1698 | lStatus = chain->createEffect(effect, desc, effectId, sessionId, pinned); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1699 | if (lStatus != NO_ERROR) { |
| 1700 | goto Exit; |
| 1701 | } |
| 1702 | effectCreated = true; |
| 1703 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 1704 | // FIXME: use vector of device and address when effect interface is ready. |
jiabin | 8f278ee | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 1705 | effect->setDevices(outDeviceTypeAddrs()); |
| 1706 | effect->setInputDevice(inDeviceTypeAddr()); |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 1707 | effect->setMode(mAfThreadCallback->getMode()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1708 | effect->setAudioSource(mAudioSource); |
| 1709 | } |
jiabin | 1319f5a | 2021-03-30 22:21:24 +0000 | [diff] [blame] | 1710 | if (effect->isHapticGenerator()) { |
| 1711 | // TODO(b/184194057): Use the vibrator information from the vibrator that will be used |
| 1712 | // for the HapticGenerator. |
Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 1713 | const std::optional<media::AudioVibratorInfo> defaultVibratorInfo = |
Yi Kong | 3ac211f | 2024-08-12 07:31:44 +0800 | [diff] [blame] | 1714 | mAfThreadCallback->getDefaultVibratorInfo_l(); |
Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 1715 | if (defaultVibratorInfo) { |
Shunkai Yao | 29d1057 | 2024-03-19 04:31:47 +0000 | [diff] [blame] | 1716 | audio_utils::lock_guard _cl(chain->mutex()); |
jiabin | 1319f5a | 2021-03-30 22:21:24 +0000 | [diff] [blame] | 1717 | // Only set the vibrator info when it is a valid one. |
Shunkai Yao | d125e40 | 2024-01-20 03:19:06 +0000 | [diff] [blame] | 1718 | effect->setVibratorInfo_l(*defaultVibratorInfo); |
jiabin | 1319f5a | 2021-03-30 22:21:24 +0000 | [diff] [blame] | 1719 | } |
| 1720 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1721 | // create effect handle and connect it to effect module |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1722 | handle = IAfEffectHandle::create( |
| 1723 | effect, client, effectClient, priority, notifyFramesProcessed); |
Glenn Kasten | e75da40 | 2013-11-20 13:54:52 -0800 | [diff] [blame] | 1724 | lStatus = handle->initCheck(); |
| 1725 | if (lStatus == OK) { |
| 1726 | lStatus = effect->addHandle(handle.get()); |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 1727 | sendCheckOutputStageEffectsEvent_l(); |
Glenn Kasten | e75da40 | 2013-11-20 13:54:52 -0800 | [diff] [blame] | 1728 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1729 | if (enabled != NULL) { |
| 1730 | *enabled = (int)effect->isEnabled(); |
| 1731 | } |
| 1732 | } |
| 1733 | |
| 1734 | Exit: |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 1735 | if (!probe && lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1736 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1737 | if (effectCreated) { |
Shunkai Yao | 29d1057 | 2024-03-19 04:31:47 +0000 | [diff] [blame] | 1738 | chain->removeEffect(effect); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1739 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1740 | if (chainCreated) { |
| 1741 | removeEffectChain_l(chain); |
| 1742 | } |
haobo10173529 | 5ff7b0d | 2017-03-17 17:44:03 +0800 | [diff] [blame] | 1743 | // handle must be cleared by caller to avoid deadlock. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1744 | } |
| 1745 | |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1746 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1747 | return handle; |
| 1748 | } |
| 1749 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1750 | void ThreadBase::disconnectEffectHandle(IAfEffectHandle* handle, |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1751 | bool unpinIfLast) |
| 1752 | { |
| 1753 | bool remove = false; |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1754 | sp<IAfEffectModule> effect; |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1755 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1756 | audio_utils::lock_guard _l(mutex()); |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1757 | sp<IAfEffectBase> effectBase = handle->effect().promote(); |
Eric Laurent | 4170955 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 1758 | if (effectBase == nullptr) { |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1759 | return; |
| 1760 | } |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 1761 | effect = effectBase->asEffectModule(); |
| 1762 | if (effect == nullptr) { |
| 1763 | return; |
| 1764 | } |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1765 | // restore suspended effects if the disconnected handle was enabled and the last one. |
| 1766 | remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast); |
| 1767 | if (remove) { |
| 1768 | removeEffect_l(effect, true); |
| 1769 | } |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 1770 | sendCheckOutputStageEffectsEvent_l(); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1771 | } |
| 1772 | if (remove) { |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 1773 | mAfThreadCallback->updateOrphanEffectChains(effect); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1774 | if (handle->enabled()) { |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1775 | effect->checkSuspendOnEffectEnabled(false, false /*threadLocked*/); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1776 | } |
| 1777 | } |
| 1778 | } |
| 1779 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1780 | void ThreadBase::onEffectEnable(const sp<IAfEffectModule>& effect) { |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 1781 | if (isOffloadOrMmap()) { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1782 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1783 | broadcast_l(); |
| 1784 | } |
| 1785 | if (!effect->isOffloadable()) { |
| 1786 | if (mType == ThreadBase::OFFLOAD) { |
| 1787 | PlaybackThread *t = (PlaybackThread *)this; |
| 1788 | t->invalidateTracks(AUDIO_STREAM_MUSIC); |
| 1789 | } |
| 1790 | if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) { |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 1791 | mAfThreadCallback->onNonOffloadableGlobalEffectEnable(); |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1792 | } |
| 1793 | } |
| 1794 | } |
| 1795 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1796 | void ThreadBase::onEffectDisable() { |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 1797 | if (isOffloadOrMmap()) { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1798 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1799 | broadcast_l(); |
| 1800 | } |
| 1801 | } |
| 1802 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1803 | sp<IAfEffectModule> ThreadBase::getEffect(audio_session_t sessionId, |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1804 | int effectId) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1805 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1806 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1807 | return getEffect_l(sessionId, effectId); |
| 1808 | } |
| 1809 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1810 | sp<IAfEffectModule> ThreadBase::getEffect_l(audio_session_t sessionId, |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1811 | int effectId) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1812 | { |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1813 | sp<IAfEffectChain> chain = getEffectChain_l(sessionId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1814 | return chain != 0 ? chain->getEffectFromId_l(effectId) : 0; |
| 1815 | } |
| 1816 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1817 | std::vector<int> ThreadBase::getEffectIds_l(audio_session_t sessionId) const |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 1818 | { |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1819 | sp<IAfEffectChain> chain = getEffectChain_l(sessionId); |
Shunkai Yao | d125e40 | 2024-01-20 03:19:06 +0000 | [diff] [blame] | 1820 | return chain != nullptr ? chain->getEffectIds_l() : std::vector<int>{}; |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 1821 | } |
| 1822 | |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1823 | // PlaybackThread::addEffect_ll() must be called with AudioFlinger::mutex() and |
| 1824 | // ThreadBase::mutex() held |
| 1825 | status_t ThreadBase::addEffect_ll(const sp<IAfEffectModule>& effect) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1826 | { |
| 1827 | // check for existing effect chain with the requested audio session |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1828 | audio_session_t sessionId = effect->sessionId(); |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1829 | sp<IAfEffectChain> chain = getEffectChain_l(sessionId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1830 | bool chainCreated = false; |
| 1831 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1832 | ALOGD_IF((mType == OFFLOAD) && !effect->isOffloadable(), |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1833 | "%s: on offloaded thread %p: effect %s does not support offload flags %#x", |
| 1834 | __func__, this, effect->desc().name, effect->desc().flags); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1835 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1836 | if (chain == 0) { |
| 1837 | // create a new chain for this session |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1838 | ALOGV("%s: new effect chain for session %d", __func__, sessionId); |
Shunkai Yao | 29d1057 | 2024-03-19 04:31:47 +0000 | [diff] [blame] | 1839 | chain = IAfEffectChain::create(this, sessionId, mAfThreadCallback); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1840 | addEffectChain_l(chain); |
| 1841 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 1842 | chainCreated = true; |
| 1843 | } |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1844 | 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] | 1845 | |
| 1846 | if (chain->getEffectFromId_l(effect->id()) != 0) { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1847 | ALOGW("%s: %p effect %s already present in chain %p", |
| 1848 | __func__, this, effect->desc().name, chain.get()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1849 | return BAD_VALUE; |
| 1850 | } |
| 1851 | |
Shunkai Yao | d125e40 | 2024-01-20 03:19:06 +0000 | [diff] [blame] | 1852 | effect->setOffloaded_l(mType == OFFLOAD, mId); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1853 | |
Shunkai Yao | 29d1057 | 2024-03-19 04:31:47 +0000 | [diff] [blame] | 1854 | status_t status = chain->addEffect(effect); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1855 | if (status != NO_ERROR) { |
| 1856 | if (chainCreated) { |
| 1857 | removeEffectChain_l(chain); |
| 1858 | } |
| 1859 | return status; |
| 1860 | } |
| 1861 | |
jiabin | 8f278ee | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 1862 | effect->setDevices(outDeviceTypeAddrs()); |
| 1863 | effect->setInputDevice(inDeviceTypeAddr()); |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 1864 | effect->setMode(mAfThreadCallback->getMode()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1865 | effect->setAudioSource(mAudioSource); |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 1866 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1867 | return NO_ERROR; |
| 1868 | } |
| 1869 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1870 | void ThreadBase::removeEffect_l(const sp<IAfEffectModule>& effect, bool release) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1871 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1872 | ALOGV("%s %p effect %p", __FUNCTION__, this, effect.get()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1873 | effect_descriptor_t desc = effect->desc(); |
| 1874 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 1875 | detachAuxEffect_l(effect->id()); |
| 1876 | } |
| 1877 | |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1878 | sp<IAfEffectChain> chain = effect->getCallback()->chain().promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1879 | if (chain != 0) { |
| 1880 | // remove effect chain if removing last effect |
Shunkai Yao | 29d1057 | 2024-03-19 04:31:47 +0000 | [diff] [blame] | 1881 | if (chain->removeEffect(effect, release) == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1882 | removeEffectChain_l(chain); |
| 1883 | } |
| 1884 | } else { |
| 1885 | ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); |
| 1886 | } |
| 1887 | } |
| 1888 | |
Shunkai Yao | f484765 | 2024-01-12 00:25:20 +0000 | [diff] [blame] | 1889 | void ThreadBase::lockEffectChains_l(Vector<sp<IAfEffectChain>>& effectChains) |
| 1890 | NO_THREAD_SAFETY_ANALYSIS // calls EffectChain::lock() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1891 | { |
| 1892 | effectChains = mEffectChains; |
Shunkai Yao | f484765 | 2024-01-12 00:25:20 +0000 | [diff] [blame] | 1893 | for (const auto& effectChain : effectChains) { |
| 1894 | effectChain->mutex().lock(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1895 | } |
| 1896 | } |
| 1897 | |
Shunkai Yao | f484765 | 2024-01-12 00:25:20 +0000 | [diff] [blame] | 1898 | void ThreadBase::unlockEffectChains(const Vector<sp<IAfEffectChain>>& effectChains) |
| 1899 | NO_THREAD_SAFETY_ANALYSIS // calls EffectChain::unlock() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1900 | { |
Shunkai Yao | f484765 | 2024-01-12 00:25:20 +0000 | [diff] [blame] | 1901 | for (const auto& effectChain : effectChains) { |
| 1902 | effectChain->mutex().unlock(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1903 | } |
| 1904 | } |
| 1905 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1906 | sp<IAfEffectChain> ThreadBase::getEffectChain(audio_session_t sessionId) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1907 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1908 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1909 | return getEffectChain_l(sessionId); |
| 1910 | } |
| 1911 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1912 | sp<IAfEffectChain> ThreadBase::getEffectChain_l(audio_session_t sessionId) |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1913 | const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1914 | { |
| 1915 | size_t size = mEffectChains.size(); |
| 1916 | for (size_t i = 0; i < size; i++) { |
| 1917 | if (mEffectChains[i]->sessionId() == sessionId) { |
| 1918 | return mEffectChains[i]; |
| 1919 | } |
| 1920 | } |
| 1921 | return 0; |
| 1922 | } |
| 1923 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1924 | void ThreadBase::setMode(audio_mode_t mode) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1925 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1926 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1927 | size_t size = mEffectChains.size(); |
| 1928 | for (size_t i = 0; i < size; i++) { |
| 1929 | mEffectChains[i]->setMode_l(mode); |
| 1930 | } |
| 1931 | } |
| 1932 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1933 | void ThreadBase::toAudioPortConfig(struct audio_port_config* config) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1934 | { |
| 1935 | config->type = AUDIO_PORT_TYPE_MIX; |
| 1936 | config->ext.mix.handle = mId; |
| 1937 | config->sample_rate = mSampleRate; |
Mikhail Naganov | 88d54da | 2023-09-11 11:53:50 -0700 | [diff] [blame] | 1938 | config->format = mHALFormat; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1939 | config->channel_mask = mChannelMask; |
| 1940 | config->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK| |
| 1941 | AUDIO_PORT_CONFIG_FORMAT; |
| 1942 | } |
| 1943 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1944 | void ThreadBase::systemReady() |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1945 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1946 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1947 | if (mSystemReady) { |
| 1948 | return; |
| 1949 | } |
| 1950 | mSystemReady = true; |
| 1951 | |
| 1952 | for (size_t i = 0; i < mPendingConfigEvents.size(); i++) { |
| 1953 | sendConfigEvent_l(mPendingConfigEvents.editItemAt(i)); |
| 1954 | } |
| 1955 | mPendingConfigEvents.clear(); |
| 1956 | } |
| 1957 | |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1958 | template <typename T> |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1959 | ssize_t ThreadBase::ActiveTracks<T>::add(const sp<T>& track) { |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1960 | ssize_t index = mActiveTracks.indexOf(track); |
| 1961 | if (index >= 0) { |
| 1962 | ALOGW("ActiveTracks<T>::add track %p already there", track.get()); |
| 1963 | return index; |
| 1964 | } |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1965 | logTrack("add", track); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1966 | mActiveTracksGeneration++; |
| 1967 | mLatestActiveTrack = track; |
Atneya Nair | 166663a | 2023-06-27 19:16:24 -0700 | [diff] [blame] | 1968 | track->beginBatteryAttribution(); |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 1969 | mHasChanged = true; |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1970 | return mActiveTracks.add(track); |
| 1971 | } |
| 1972 | |
| 1973 | template <typename T> |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1974 | ssize_t ThreadBase::ActiveTracks<T>::remove(const sp<T>& track) { |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1975 | ssize_t index = mActiveTracks.remove(track); |
| 1976 | if (index < 0) { |
| 1977 | ALOGW("ActiveTracks<T>::remove nonexistent track %p", track.get()); |
| 1978 | return index; |
| 1979 | } |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1980 | logTrack("remove", track); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1981 | mActiveTracksGeneration++; |
Atneya Nair | 166663a | 2023-06-27 19:16:24 -0700 | [diff] [blame] | 1982 | track->endBatteryAttribution(); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1983 | // mLatestActiveTrack is not cleared even if is the same as track. |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 1984 | mHasChanged = true; |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 1985 | #ifdef TEE_SINK |
| 1986 | track->dumpTee(-1 /* fd */, "_REMOVE"); |
| 1987 | #endif |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 1988 | track->logEndInterval(); // log to MediaMetrics |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1989 | return index; |
| 1990 | } |
| 1991 | |
| 1992 | template <typename T> |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1993 | void ThreadBase::ActiveTracks<T>::clear() { |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1994 | for (const sp<T> &track : mActiveTracks) { |
Atneya Nair | 166663a | 2023-06-27 19:16:24 -0700 | [diff] [blame] | 1995 | track->endBatteryAttribution(); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1996 | logTrack("clear", track); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1997 | } |
| 1998 | mLastActiveTracksGeneration = mActiveTracksGeneration; |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 1999 | if (!mActiveTracks.empty()) { mHasChanged = true; } |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 2000 | mActiveTracks.clear(); |
| 2001 | mLatestActiveTrack.clear(); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 2002 | } |
| 2003 | |
| 2004 | template <typename T> |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2005 | void ThreadBase::ActiveTracks<T>::updatePowerState_l( |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 2006 | const sp<ThreadBase>& thread, bool force) { |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 2007 | // Updates ActiveTracks client uids to the thread wakelock. |
| 2008 | if (mActiveTracksGeneration != mLastActiveTracksGeneration || force) { |
| 2009 | thread->updateWakeLockUids_l(getWakeLockUids()); |
| 2010 | mLastActiveTracksGeneration = mActiveTracksGeneration; |
| 2011 | } |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 2012 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2013 | |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2014 | template <typename T> |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2015 | bool ThreadBase::ActiveTracks<T>::readAndClearHasChanged() { |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 2016 | bool hasChanged = mHasChanged; |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 2017 | mHasChanged = false; |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 2018 | |
| 2019 | for (const sp<T> &track : mActiveTracks) { |
| 2020 | // Do not short-circuit as all hasChanged states must be reset |
| 2021 | // as all the metadata are going to be sent |
| 2022 | hasChanged |= track->readAndClearHasChanged(); |
| 2023 | } |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 2024 | return hasChanged; |
| 2025 | } |
| 2026 | |
| 2027 | template <typename T> |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2028 | void ThreadBase::ActiveTracks<T>::logTrack( |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2029 | const char *funcName, const sp<T> &track) const { |
| 2030 | if (mLocalLog != nullptr) { |
| 2031 | String8 result; |
| 2032 | track->appendDump(result, false /* active */); |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 2033 | mLocalLog->log("AT::%-10s(%p) %s", funcName, track.get(), result.c_str()); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2034 | } |
| 2035 | } |
| 2036 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2037 | void ThreadBase::broadcast_l() |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2038 | { |
| 2039 | // Thread could be blocked waiting for async |
| 2040 | // so signal it to handle state changes immediately |
| 2041 | // If threadLoop is currently unlocked a signal of mWaitWorkCV will |
| 2042 | // be lost so we also flag to prevent it blocking on mWaitWorkCV |
| 2043 | mSignalPending = true; |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2044 | mWaitWorkCV.notify_all(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2045 | } |
| 2046 | |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 2047 | // Call only from threadLoop() or when it is idle. |
| 2048 | // Do not call from high performance code as this may do binder rpc to the MediaMetrics service. |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2049 | void ThreadBase::sendStatistics(bool force) |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2050 | NO_THREAD_SAFETY_ANALYSIS |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 2051 | { |
| 2052 | // Do not log if we have no stats. |
| 2053 | // We choose the timestamp verifier because it is the most likely item to be present. |
| 2054 | const int64_t nstats = mTimestampVerifier.getN() - mLastRecordedTimestampVerifierN; |
| 2055 | if (nstats == 0) { |
| 2056 | return; |
| 2057 | } |
| 2058 | |
| 2059 | // Don't log more frequently than once per 12 hours. |
| 2060 | // We use BOOTTIME to include suspend time. |
| 2061 | const int64_t timeNs = systemTime(SYSTEM_TIME_BOOTTIME); |
| 2062 | const int64_t sinceNs = timeNs - mLastRecordedTimeNs; // ok if mLastRecordedTimeNs = 0 |
| 2063 | if (!force && sinceNs <= 12 * NANOS_PER_HOUR) { |
| 2064 | return; |
| 2065 | } |
| 2066 | |
| 2067 | mLastRecordedTimestampVerifierN = mTimestampVerifier.getN(); |
| 2068 | mLastRecordedTimeNs = timeNs; |
| 2069 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 2070 | std::unique_ptr<mediametrics::Item> item(mediametrics::Item::create("audiothread")); |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 2071 | |
| 2072 | #define MM_PREFIX "android.media.audiothread." // avoid cut-n-paste errors. |
| 2073 | |
| 2074 | // thread configuration |
| 2075 | item->setInt32(MM_PREFIX "id", (int32_t)mId); // IO handle |
| 2076 | // item->setInt32(MM_PREFIX "portId", (int32_t)mPortId); |
| 2077 | item->setCString(MM_PREFIX "type", threadTypeToString(mType)); |
| 2078 | item->setInt32(MM_PREFIX "sampleRate", (int32_t)mSampleRate); |
| 2079 | item->setInt64(MM_PREFIX "channelMask", (int64_t)mChannelMask); |
| 2080 | item->setCString(MM_PREFIX "encoding", toString(mFormat).c_str()); |
| 2081 | item->setInt32(MM_PREFIX "frameCount", (int32_t)mFrameCount); |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2082 | item->setCString(MM_PREFIX "outDevice", toString(outDeviceTypes_l()).c_str()); |
| 2083 | item->setCString(MM_PREFIX "inDevice", toString(inDeviceType_l()).c_str()); |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 2084 | |
| 2085 | // thread statistics |
| 2086 | if (mIoJitterMs.getN() > 0) { |
| 2087 | item->setDouble(MM_PREFIX "ioJitterMs.mean", mIoJitterMs.getMean()); |
| 2088 | item->setDouble(MM_PREFIX "ioJitterMs.std", mIoJitterMs.getStdDev()); |
| 2089 | } |
| 2090 | if (mProcessTimeMs.getN() > 0) { |
| 2091 | item->setDouble(MM_PREFIX "processTimeMs.mean", mProcessTimeMs.getMean()); |
| 2092 | item->setDouble(MM_PREFIX "processTimeMs.std", mProcessTimeMs.getStdDev()); |
| 2093 | } |
| 2094 | const auto tsjitter = mTimestampVerifier.getJitterMs(); |
| 2095 | if (tsjitter.getN() > 0) { |
| 2096 | item->setDouble(MM_PREFIX "timestampJitterMs.mean", tsjitter.getMean()); |
| 2097 | item->setDouble(MM_PREFIX "timestampJitterMs.std", tsjitter.getStdDev()); |
| 2098 | } |
| 2099 | if (mLatencyMs.getN() > 0) { |
| 2100 | item->setDouble(MM_PREFIX "latencyMs.mean", mLatencyMs.getMean()); |
| 2101 | item->setDouble(MM_PREFIX "latencyMs.std", mLatencyMs.getStdDev()); |
| 2102 | } |
Robert Wu | 06db0a3 | 2021-08-10 19:05:34 +0000 | [diff] [blame] | 2103 | if (mMonopipePipeDepthStats.getN() > 0) { |
| 2104 | item->setDouble(MM_PREFIX "monopipePipeDepthStats.mean", |
| 2105 | mMonopipePipeDepthStats.getMean()); |
| 2106 | item->setDouble(MM_PREFIX "monopipePipeDepthStats.std", |
| 2107 | mMonopipePipeDepthStats.getStdDev()); |
| 2108 | } |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 2109 | |
| 2110 | item->selfrecord(); |
| 2111 | } |
| 2112 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2113 | product_strategy_t ThreadBase::getStrategyForStream(audio_stream_type_t stream) const |
Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 2114 | { |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 2115 | if (!mAfThreadCallback->isAudioPolicyReady()) { |
Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 2116 | return PRODUCT_STRATEGY_NONE; |
| 2117 | } |
| 2118 | return AudioSystem::getStrategyForStream(stream); |
| 2119 | } |
| 2120 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2121 | // startMelComputation_l() must be called with AudioFlinger::mutex() held |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2122 | void ThreadBase::startMelComputation_l( |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 2123 | const sp<audio_utils::MelProcessor>& /*processor*/) |
| 2124 | { |
| 2125 | // Do nothing |
| 2126 | ALOGW("%s: ThreadBase does not support CSD", __func__); |
| 2127 | } |
| 2128 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2129 | // stopMelComputation_l() must be called with AudioFlinger::mutex() held |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2130 | void ThreadBase::stopMelComputation_l() |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 2131 | { |
| 2132 | // Do nothing |
| 2133 | ALOGW("%s: ThreadBase does not support CSD", __func__); |
| 2134 | } |
| 2135 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2136 | // ---------------------------------------------------------------------------- |
| 2137 | // Playback |
| 2138 | // ---------------------------------------------------------------------------- |
| 2139 | |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 2140 | PlaybackThread::PlaybackThread(const sp<IAfThreadCallback>& afThreadCallback, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2141 | AudioStreamOut* output, |
| 2142 | audio_io_handle_t id, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 2143 | type_t type, |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 2144 | bool systemReady, |
| 2145 | audio_config_base_t *mixerConfig) |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 2146 | : ThreadBase(afThreadCallback, id, type, systemReady, true /* isOut */), |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 2147 | mNormalFrameCount(0), mSinkBuffer(NULL), |
Andy Hung | 81994d6 | 2023-07-20 21:44:14 -0700 | [diff] [blame] | 2148 | mMixerBufferEnabled(kEnableExtendedPrecision || type == SPATIALIZER), |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2149 | mMixerBuffer(NULL), |
| 2150 | mMixerBufferSize(0), |
| 2151 | mMixerBufferFormat(AUDIO_FORMAT_INVALID), |
| 2152 | mMixerBufferValid(false), |
Andy Hung | 81994d6 | 2023-07-20 21:44:14 -0700 | [diff] [blame] | 2153 | mEffectBufferEnabled(kEnableExtendedPrecision || type == SPATIALIZER), |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2154 | mEffectBuffer(NULL), |
| 2155 | mEffectBufferSize(0), |
| 2156 | mEffectBufferFormat(AUDIO_FORMAT_INVALID), |
| 2157 | mEffectBufferValid(false), |
Glenn Kasten | c1fac19 | 2013-08-06 07:41:36 -0700 | [diff] [blame] | 2158 | mSuspended(0), mBytesWritten(0), |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 2159 | mFramesWritten(0), |
Andy Hung | 238fa3d | 2016-07-28 10:53:22 -0700 | [diff] [blame] | 2160 | mSuspendedFrames(0), |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2161 | mActiveTracks(&this->mLocalLog), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2162 | // mStreamTypes[] initialized in constructor body |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2163 | mTracks(type == MIXER), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2164 | mOutput(output), |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 2165 | mNumWrites(0), mNumDelayedWrites(0), mInWrite(false), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2166 | mMixerStatus(MIXER_IDLE), |
| 2167 | mMixerStatusIgnoringFastTracks(MIXER_IDLE), |
Andy Hung | 8fe87eb | 2023-07-20 21:31:38 -0700 | [diff] [blame] | 2168 | mStandbyDelayNs(getStandbyTimeInNanos()), |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2169 | mBytesRemaining(0), |
| 2170 | mCurrentWriteLength(0), |
| 2171 | mUseAsyncWrite(false), |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2172 | mWriteAckSequence(0), |
| 2173 | mDrainSequence(0), |
Andy Hung | 1d2d2aea | 2023-07-19 16:22:58 -0700 | [diff] [blame] | 2174 | mScreenState(mAfThreadCallback->getScreenState()), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2175 | // index 0 is reserved for normal mixer's submix |
Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 2176 | mFastTrackAvailMask(((1 << FastMixerState::sMaxFastTracks) - 1) & ~1), |
Eric Laurent | 7c29ec9 | 2017-09-20 17:54:22 -0700 | [diff] [blame] | 2177 | mHwSupportsPause(false), mHwPaused(false), mFlushPending(false), |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 2178 | mLeftVolFloat(-1.0), mRightVolFloat(-1.0), |
Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 2179 | mDownStreamPatch{}, |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 2180 | mIsTimestampAdvancing(kMinimumTimeBetweenTimestampChecksNs) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2181 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 2182 | snprintf(mThreadName, kThreadNameLength, "AudioOut_%X", id); |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 2183 | mNBLogWriter = afThreadCallback->newWriter_l(kLogSize, mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2184 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2185 | // Assumes constructor is called by AudioFlinger with its mutex() held, but |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2186 | // it would be safer to explicitly pass initial masterVolume/masterMute as |
| 2187 | // parameter. |
| 2188 | // |
| 2189 | // If the HAL we are using has support for master volume or master mute, |
| 2190 | // then do not attenuate or mute during mixing (just leave the volume at 1.0 |
| 2191 | // and the mute set to false). |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 2192 | mMasterVolume = afThreadCallback->masterVolume_l(); |
| 2193 | mMasterMute = afThreadCallback->masterMute_l(); |
George Burgess IV | 5e4d86f | 2020-02-18 12:55:36 -0800 | [diff] [blame] | 2194 | if (mOutput->audioHwDev) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2195 | if (mOutput->audioHwDev->canSetMasterVolume()) { |
| 2196 | mMasterVolume = 1.0; |
| 2197 | } |
| 2198 | |
| 2199 | if (mOutput->audioHwDev->canSetMasterMute()) { |
| 2200 | mMasterMute = false; |
| 2201 | } |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 2202 | mIsMsdDevice = strcmp( |
| 2203 | mOutput->audioHwDev->moduleName(), AUDIO_HARDWARE_MODULE_ID_MSD) == 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2204 | } |
| 2205 | |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 2206 | if (mixerConfig != nullptr && mixerConfig->channel_mask != AUDIO_CHANNEL_NONE) { |
| 2207 | mMixerChannelMask = mixerConfig->channel_mask; |
| 2208 | } |
| 2209 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 2210 | readOutputParameters_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2211 | |
Eric Laurent | 1c5e2e3 | 2021-08-18 18:50:28 +0200 | [diff] [blame] | 2212 | if (mType != SPATIALIZER |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 2213 | && mMixerChannelMask != mChannelMask) { |
| 2214 | LOG_ALWAYS_FATAL("HAL channel mask %#x does not match mixer channel mask %#x", |
| 2215 | mChannelMask, mMixerChannelMask); |
| 2216 | } |
| 2217 | |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 2218 | // TODO: We may also match on address as well as device type for |
| 2219 | // 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] | 2220 | if (type == MIXER || type == DIRECT || type == OFFLOAD) { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 2221 | // TODO: This property should be ensure that only contains one single device type. |
| 2222 | mTimestampCorrectedDevice = (audio_devices_t)property_get_int64( |
| 2223 | "audio.timestamp.corrected_output_device", |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 2224 | (int64_t)(mIsMsdDevice ? AUDIO_DEVICE_OUT_BUS // turn on by default for MSD |
| 2225 | : AUDIO_DEVICE_NONE)); |
| 2226 | } |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 2227 | if (!audioserver_flags::portid_volume_management()) { |
| 2228 | for (int i = AUDIO_STREAM_MIN; i < AUDIO_STREAM_FOR_POLICY_CNT; ++i) { |
| 2229 | const audio_stream_type_t stream{static_cast<audio_stream_type_t>(i)}; |
| 2230 | mStreamTypes[stream].volume = 0.0f; |
| 2231 | mStreamTypes[stream].mute = mAfThreadCallback->streamMute_l(stream); |
| 2232 | } |
| 2233 | // Audio patch and call assistant volume are always max |
| 2234 | mStreamTypes[AUDIO_STREAM_PATCH].volume = 1.0f; |
| 2235 | mStreamTypes[AUDIO_STREAM_PATCH].mute = false; |
| 2236 | mStreamTypes[AUDIO_STREAM_CALL_ASSISTANT].volume = 1.0f; |
| 2237 | mStreamTypes[AUDIO_STREAM_CALL_ASSISTANT].mute = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2238 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2239 | } |
| 2240 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2241 | PlaybackThread::~PlaybackThread() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2242 | { |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 2243 | mAfThreadCallback->unregisterWriter(mNBLogWriter); |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2244 | free(mSinkBuffer); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2245 | free(mMixerBuffer); |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2246 | free(mEffectBuffer); |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 2247 | free(mPostSpatializerBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2248 | } |
| 2249 | |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 2250 | // Thread virtuals |
| 2251 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2252 | void PlaybackThread::onFirstRef() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2253 | { |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 2254 | if (!isStreamInitialized()) { |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 2255 | ALOGE("The stream is not open yet"); // This should not happen. |
| 2256 | } else { |
Mikhail Naganov | aec565e | 2023-02-22 16:31:28 -0800 | [diff] [blame] | 2257 | // Callbacks take strong or weak pointers as a parameter. |
| 2258 | // Since PlaybackThread passes itself as a callback handler, it can only |
| 2259 | // be done outside of the constructor. Creating weak and especially strong |
| 2260 | // pointers to a refcounted object in its own constructor is strongly |
| 2261 | // discouraged, see comments in system/core/libutils/include/utils/RefBase.h. |
| 2262 | // Even if a function takes a weak pointer, it is possible that it will |
| 2263 | // need to convert it to a strong pointer down the line. |
| 2264 | if (mOutput->flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING && |
| 2265 | mOutput->stream->setCallback(this) == OK) { |
| 2266 | mUseAsyncWrite = true; |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2267 | mCallbackThread = sp<AsyncCallbackThread>::make(this); |
Mikhail Naganov | aec565e | 2023-02-22 16:31:28 -0800 | [diff] [blame] | 2268 | } |
| 2269 | |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 2270 | if (mOutput->stream->setEventCallback(this) != OK) { |
jiabin | f1a3605 | 2020-08-19 14:33:31 -0700 | [diff] [blame] | 2271 | ALOGD("Failed to add event callback"); |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 2272 | } |
| 2273 | } |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 2274 | run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO); |
Andy Hung | 44d648b | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 2275 | mThreadSnapshot.setTid(getTid()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2276 | } |
| 2277 | |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 2278 | // ThreadBase virtuals |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2279 | void PlaybackThread::preExit() |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 2280 | { |
| 2281 | ALOGV(" preExit()"); |
Ytai Ben-Tsvi | 7e0183f | 2022-02-04 10:49:54 -0800 | [diff] [blame] | 2282 | status_t result = mOutput->stream->exit(); |
| 2283 | ALOGE_IF(result != OK, "Error when calling exit(): %d", result); |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 2284 | } |
| 2285 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2286 | void PlaybackThread::dumpTracks_l(int fd, const Vector<String16>& /* args */) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2287 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2288 | String8 result; |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 2289 | if (!audioserver_flags::portid_volume_management()) { |
| 2290 | result.appendFormat(" Stream volumes in dB: "); |
| 2291 | for (int i = 0; i < AUDIO_STREAM_CNT; ++i) { |
| 2292 | const stream_type_t *st = &mStreamTypes[i]; |
| 2293 | if (i > 0) { |
| 2294 | result.appendFormat(", "); |
| 2295 | } |
| 2296 | result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume)); |
| 2297 | if (st->mute) { |
| 2298 | result.append("M"); |
| 2299 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2300 | } |
| 2301 | } |
| 2302 | result.append("\n"); |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 2303 | write(fd, result.c_str(), result.length()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2304 | result.clear(); |
| 2305 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2306 | // These values are "raw"; they will wrap around. See prepareTracks_l() for a better way. |
| 2307 | FastTrackUnderruns underruns = getFastTrackUnderruns(0); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 2308 | dprintf(fd, " Normal mixer raw underrun counters: partial=%u empty=%u\n", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2309 | underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2310 | |
| 2311 | size_t numtracks = mTracks.size(); |
| 2312 | size_t numactive = mActiveTracks.size(); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2313 | dprintf(fd, " %zu Tracks", numtracks); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2314 | size_t numactiveseen = 0; |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2315 | const char *prefix = " "; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2316 | if (numtracks) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2317 | dprintf(fd, " of which %zu are active\n", numactive); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2318 | result.append(prefix); |
Andy Hung | f6ab58d | 2018-05-25 12:50:39 -0700 | [diff] [blame] | 2319 | mTracks[0]->appendDumpHeader(result); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2320 | for (size_t i = 0; i < numtracks; ++i) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2321 | sp<IAfTrack> track = mTracks[i]; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2322 | if (track != 0) { |
| 2323 | bool active = mActiveTracks.indexOf(track) >= 0; |
| 2324 | if (active) { |
| 2325 | numactiveseen++; |
| 2326 | } |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2327 | result.append(prefix); |
| 2328 | track->appendDump(result, active); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2329 | } |
| 2330 | } |
| 2331 | } else { |
| 2332 | result.append("\n"); |
| 2333 | } |
| 2334 | if (numactiveseen != numactive) { |
| 2335 | // some tracks in the active list were not in the tracks list |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2336 | result.append(" The following tracks are in the active list but" |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2337 | " not in the track list\n"); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2338 | result.append(prefix); |
Andy Hung | f6ab58d | 2018-05-25 12:50:39 -0700 | [diff] [blame] | 2339 | mActiveTracks[0]->appendDumpHeader(result); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2340 | for (size_t i = 0; i < numactive; ++i) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2341 | sp<IAfTrack> track = mActiveTracks[i]; |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 2342 | if (mTracks.indexOf(track) < 0) { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2343 | result.append(prefix); |
| 2344 | track->appendDump(result, true /* active */); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2345 | } |
| 2346 | } |
| 2347 | } |
| 2348 | |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 2349 | write(fd, result.c_str(), result.size()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2350 | } |
| 2351 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2352 | void PlaybackThread::dumpInternals_l(int fd, const Vector<String16>& args) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2353 | { |
Andy Hung | 04cb8f7 | 2020-03-20 13:44:33 -0700 | [diff] [blame] | 2354 | dprintf(fd, " Master volume: %f\n", mMasterVolume); |
Mikhail Naganov | dfb411f | 2018-09-11 13:39:56 -0700 | [diff] [blame] | 2355 | dprintf(fd, " Master mute: %s\n", mMasterMute ? "on" : "off"); |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 2356 | dprintf(fd, " Mixer channel Mask: %#x (%s)\n", |
| 2357 | mMixerChannelMask, channelMaskToString(mMixerChannelMask, true /* output */).c_str()); |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 2358 | if (mHapticChannelMask != AUDIO_CHANNEL_NONE) { |
| 2359 | dprintf(fd, " Haptic channel mask: %#x (%s)\n", mHapticChannelMask, |
| 2360 | channelMaskToString(mHapticChannelMask, true /* output */).c_str()); |
| 2361 | } |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 2362 | dprintf(fd, " Normal frame count: %zu\n", mNormalFrameCount); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 2363 | dprintf(fd, " Total writes: %d\n", mNumWrites); |
| 2364 | dprintf(fd, " Delayed writes: %d\n", mNumDelayedWrites); |
| 2365 | dprintf(fd, " Blocked in write: %s\n", mInWrite ? "yes" : "no"); |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 2366 | dprintf(fd, " Suspend count: %d\n", (int32_t)mSuspended); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 2367 | dprintf(fd, " Fast track availMask=%#x\n", mFastTrackAvailMask); |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 2368 | dprintf(fd, " Standby delay ns=%lld\n", (long long)mStandbyDelayNs); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 2369 | AudioStreamOut *output = mOutput; |
| 2370 | 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] | 2371 | dprintf(fd, " AudioStreamOut: %p flags %#x (%s)\n", |
Andy Hung | 9b18195 | 2019-02-25 14:53:36 -0800 | [diff] [blame] | 2372 | output, flags, toString(flags).c_str()); |
Andy Hung | b54c854 | 2016-09-21 12:55:15 -0700 | [diff] [blame] | 2373 | dprintf(fd, " Frames written: %lld\n", (long long)mFramesWritten); |
| 2374 | dprintf(fd, " Suspended frames: %lld\n", (long long)mSuspendedFrames); |
| 2375 | if (mPipeSink.get() != nullptr) { |
| 2376 | dprintf(fd, " PipeSink frames written: %lld\n", (long long)mPipeSink->framesWritten()); |
| 2377 | } |
| 2378 | if (output != nullptr) { |
| 2379 | dprintf(fd, " Hal stream dump:\n"); |
Andy Hung | 61589a4 | 2021-06-16 09:37:53 -0700 | [diff] [blame] | 2380 | (void)output->stream->dump(fd, args); |
Andy Hung | b54c854 | 2016-09-21 12:55:15 -0700 | [diff] [blame] | 2381 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2382 | } |
| 2383 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2384 | // PlaybackThread::createTrack_l() must be called with AudioFlinger::mutex() held |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2385 | sp<IAfTrack> PlaybackThread::createTrack_l( |
Andy Hung | 88035ac | 2023-06-27 17:05:02 -0700 | [diff] [blame] | 2386 | const sp<Client>& client, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2387 | audio_stream_type_t streamType, |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 2388 | const audio_attributes_t& attr, |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2389 | uint32_t *pSampleRate, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2390 | audio_format_t format, |
| 2391 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 2392 | size_t *pFrameCount, |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2393 | size_t *pNotificationFrameCount, |
| 2394 | uint32_t notificationsPerBuffer, |
| 2395 | float speed, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2396 | const sp<IMemory>& sharedBuffer, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2397 | audio_session_t sessionId, |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2398 | audio_output_flags_t *flags, |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 2399 | pid_t creatorPid, |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 2400 | const AttributionSourceState& attributionSource, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2401 | pid_t tid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2402 | status_t *status, |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 2403 | audio_port_handle_t portId, |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 2404 | const sp<media::IAudioTrackCallback>& callback, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 2405 | bool isSpatialized, |
jiabin | 94ed47c | 2023-07-27 23:34:20 +0000 | [diff] [blame] | 2406 | bool isBitPerfect, |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 2407 | audio_output_flags_t *afTrackFlags, |
Vlad Popa | 1e865e6 | 2024-08-15 19:11:42 -0700 | [diff] [blame] | 2408 | float volume, |
| 2409 | bool muted) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2410 | { |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 2411 | size_t frameCount = *pFrameCount; |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2412 | size_t notificationFrameCount = *pNotificationFrameCount; |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2413 | sp<IAfTrack> track; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2414 | status_t lStatus; |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2415 | audio_output_flags_t outputFlags = mOutput->flags; |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2416 | audio_output_flags_t requestedFlags = *flags; |
Eric Laurent | 9b11c02 | 2018-06-06 19:19:22 -0700 | [diff] [blame] | 2417 | uint32_t sampleRate; |
| 2418 | |
| 2419 | if (sharedBuffer != 0 && checkIMemory(sharedBuffer) != NO_ERROR) { |
| 2420 | lStatus = BAD_VALUE; |
| 2421 | goto Exit; |
| 2422 | } |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2423 | |
| 2424 | if (*pSampleRate == 0) { |
| 2425 | *pSampleRate = mSampleRate; |
| 2426 | } |
Eric Laurent | 9b11c02 | 2018-06-06 19:19:22 -0700 | [diff] [blame] | 2427 | sampleRate = *pSampleRate; |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2428 | |
| 2429 | // special case for FAST flag considered OK if fast mixer is present |
| 2430 | if (hasFastMixer()) { |
| 2431 | outputFlags = (audio_output_flags_t)(outputFlags | AUDIO_OUTPUT_FLAG_FAST); |
| 2432 | } |
| 2433 | |
| 2434 | // Check if requested flags are compatible with output stream flags |
| 2435 | if ((*flags & outputFlags) != *flags) { |
| 2436 | ALOGW("createTrack_l(): mismatch between requested flags (%08x) and output flags (%08x)", |
| 2437 | *flags, outputFlags); |
| 2438 | *flags = (audio_output_flags_t)(*flags & outputFlags); |
| 2439 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2440 | |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 2441 | if (isBitPerfect) { |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 2442 | audio_utils::lock_guard _l(mutex()); |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 2443 | sp<IAfEffectChain> chain = getEffectChain_l(sessionId); |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 2444 | if (chain.get() != nullptr) { |
| 2445 | // Bit-perfect is required according to the configuration and preferred mixer |
| 2446 | // attributes, but it is not in the output flag from the client's request. Explicitly |
| 2447 | // adding bit-perfect flag to check the compatibility |
| 2448 | audio_output_flags_t flagsToCheck = |
| 2449 | (audio_output_flags_t)(*flags & AUDIO_OUTPUT_FLAG_BIT_PERFECT); |
| 2450 | chain->checkOutputFlagCompatibility(&flagsToCheck); |
| 2451 | if ((flagsToCheck & AUDIO_OUTPUT_FLAG_BIT_PERFECT) == AUDIO_OUTPUT_FLAG_NONE) { |
| 2452 | ALOGE("%s cannot create track as there is data-processing effect attached to " |
| 2453 | "given session id(%d)", __func__, sessionId); |
| 2454 | lStatus = BAD_VALUE; |
| 2455 | goto Exit; |
| 2456 | } |
| 2457 | *flags = flagsToCheck; |
| 2458 | } |
| 2459 | } |
| 2460 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2461 | // client expresses a preference for FAST, but we get the final say |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2462 | if (*flags & AUDIO_OUTPUT_FLAG_FAST) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2463 | if ( |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2464 | // PCM data |
| 2465 | audio_is_linear_pcm(format) && |
Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 2466 | // TODO: extract as a data library function that checks that a computationally |
| 2467 | // expensive downmixer is not required: isFastOutputChannelConversion() |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 2468 | (channelMask == (mChannelMask | mHapticChannelMask) || |
Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 2469 | mChannelMask != AUDIO_CHANNEL_OUT_STEREO || |
| 2470 | (channelMask == AUDIO_CHANNEL_OUT_MONO |
| 2471 | /* && mChannelMask == AUDIO_CHANNEL_OUT_STEREO */)) && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2472 | // hardware sample rate |
| 2473 | (sampleRate == mSampleRate) && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2474 | // normal mixer has an associated fast mixer |
| 2475 | hasFastMixer() && |
| 2476 | // there are sufficient fast track slots available |
| 2477 | (mFastTrackAvailMask != 0) |
| 2478 | // FIXME test that MixerThread for this fast track has a capable output HAL |
| 2479 | // FIXME add a permission test also? |
| 2480 | ) { |
Andy Hung | e0a269a | 2016-03-23 15:13:42 -0700 | [diff] [blame] | 2481 | // static tracks can have any nonzero framecount, streaming tracks check against minimum. |
| 2482 | if (sharedBuffer == 0) { |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 2483 | // read the fast track multiplier property the first time it is needed |
| 2484 | int ok = pthread_once(&sFastTrackMultiplierOnce, sFastTrackMultiplierInit); |
| 2485 | if (ok != 0) { |
| 2486 | ALOGE("%s pthread_once failed: %d", __func__, ok); |
| 2487 | } |
Andy Hung | e0a269a | 2016-03-23 15:13:42 -0700 | [diff] [blame] | 2488 | frameCount = max(frameCount, mFrameCount * sFastTrackMultiplier); // incl framecount 0 |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2489 | } |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2490 | |
| 2491 | // check compatibility with audio effects. |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2492 | { // scope for mutex() |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2493 | audio_utils::lock_guard _l(mutex()); |
Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame] | 2494 | for (audio_session_t session : { |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 2495 | AUDIO_SESSION_DEVICE, |
Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame] | 2496 | AUDIO_SESSION_OUTPUT_STAGE, |
| 2497 | AUDIO_SESSION_OUTPUT_MIX, |
| 2498 | sessionId, |
| 2499 | }) { |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 2500 | sp<IAfEffectChain> chain = getEffectChain_l(session); |
Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame] | 2501 | if (chain.get() != nullptr) { |
| 2502 | audio_output_flags_t old = *flags; |
| 2503 | chain->checkOutputFlagCompatibility(flags); |
| 2504 | if (old != *flags) { |
| 2505 | ALOGV("AUDIO_OUTPUT_FLAGS denied by effect, session=%d old=%#x new=%#x", |
| 2506 | (int)session, (int)old, (int)*flags); |
| 2507 | } |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2508 | } |
| 2509 | } |
| 2510 | } |
Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 2511 | ALOGV_IF((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0, |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2512 | "AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu", |
| 2513 | frameCount, mFrameCount); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2514 | } else { |
Robert Wu | 4c7bb7d | 2021-08-12 18:50:13 +0000 | [diff] [blame] | 2515 | ALOGD("AUDIO_OUTPUT_FLAG_FAST denied: sharedBuffer=%p frameCount=%zu " |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2516 | "mFrameCount=%zu format=%#x mFormat=%#x isLinear=%d channelMask=%#x " |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 2517 | "sampleRate=%u mSampleRate=%u " |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2518 | "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x", |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 2519 | sharedBuffer.get(), frameCount, mFrameCount, format, mFormat, |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 2520 | audio_is_linear_pcm(format), channelMask, sampleRate, |
| 2521 | mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2522 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST); |
Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 2523 | } |
| 2524 | } |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2525 | |
| 2526 | if (!audio_has_proportional_frames(format)) { |
| 2527 | if (sharedBuffer != 0) { |
| 2528 | // Same comment as below about ignoring frameCount parameter for set() |
| 2529 | frameCount = sharedBuffer->size(); |
| 2530 | } else if (frameCount == 0) { |
| 2531 | frameCount = mNormalFrameCount; |
| 2532 | } |
| 2533 | if (notificationFrameCount != frameCount) { |
| 2534 | notificationFrameCount = frameCount; |
| 2535 | } |
| 2536 | } else if (sharedBuffer != 0) { |
| 2537 | // FIXME: Ensure client side memory buffers need |
| 2538 | // not have additional alignment beyond sample |
| 2539 | // (e.g. 16 bit stereo accessed as 32 bit frame). |
| 2540 | size_t alignment = audio_bytes_per_sample(format); |
| 2541 | if (alignment & 1) { |
| 2542 | // for AUDIO_FORMAT_PCM_24_BIT_PACKED (not exposed through Java). |
| 2543 | alignment = 1; |
| 2544 | } |
| 2545 | uint32_t channelCount = audio_channel_count_from_out_mask(channelMask); |
| 2546 | size_t frameSize = channelCount * audio_bytes_per_sample(format); |
| 2547 | if (channelCount > 1) { |
| 2548 | // More than 2 channels does not require stronger alignment than stereo |
| 2549 | alignment <<= 1; |
| 2550 | } |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 2551 | if (((uintptr_t)sharedBuffer->unsecurePointer() & (alignment - 1)) != 0) { |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2552 | ALOGE("Invalid buffer alignment: address %p, channel count %u", |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 2553 | sharedBuffer->unsecurePointer(), channelCount); |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2554 | lStatus = BAD_VALUE; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2555 | goto Exit; |
| 2556 | } |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2557 | |
| 2558 | // When initializing a shared buffer AudioTrack via constructors, |
| 2559 | // there's no frameCount parameter. |
| 2560 | // But when initializing a shared buffer AudioTrack via set(), |
| 2561 | // there _is_ a frameCount parameter. We silently ignore it. |
| 2562 | frameCount = sharedBuffer->size() / frameSize; |
| 2563 | } else { |
| 2564 | size_t minFrameCount = 0; |
| 2565 | // For fast tracks we try to respect the application's request for notifications per buffer. |
| 2566 | if (*flags & AUDIO_OUTPUT_FLAG_FAST) { |
| 2567 | if (notificationsPerBuffer > 0) { |
| 2568 | // Avoid possible arithmetic overflow during multiplication. |
| 2569 | if (notificationsPerBuffer > SIZE_MAX / mFrameCount) { |
| 2570 | ALOGE("Requested notificationPerBuffer=%u ignored for HAL frameCount=%zu", |
| 2571 | notificationsPerBuffer, mFrameCount); |
| 2572 | } else { |
| 2573 | minFrameCount = mFrameCount * notificationsPerBuffer; |
| 2574 | } |
| 2575 | } |
| 2576 | } else { |
| 2577 | // For normal PCM streaming tracks, update minimum frame count. |
| 2578 | // Buffer depth is forced to be at least 2 x the normal mixer frame count and |
| 2579 | // cover audio hardware latency. |
| 2580 | // This is probably too conservative, but legacy application code may depend on it. |
| 2581 | // If you change this calculation, also review the start threshold which is related. |
| 2582 | uint32_t latencyMs = latency_l(); |
| 2583 | if (latencyMs == 0) { |
| 2584 | ALOGE("Error when retrieving output stream latency"); |
| 2585 | lStatus = UNKNOWN_ERROR; |
| 2586 | goto Exit; |
| 2587 | } |
| 2588 | |
| 2589 | minFrameCount = AudioSystem::calculateMinFrameCount(latencyMs, mNormalFrameCount, |
| 2590 | mSampleRate, sampleRate, speed /*, 0 mNotificationsPerBufferReq*/); |
| 2591 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2592 | } |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2593 | if (frameCount < minFrameCount) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2594 | frameCount = minFrameCount; |
| 2595 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2596 | } |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2597 | |
| 2598 | // Make sure that application is notified with sufficient margin before underrun. |
| 2599 | // The client can divide the AudioTrack buffer into sub-buffers, |
| 2600 | // and expresses its desire to server as the notification frame count. |
| 2601 | if (sharedBuffer == 0 && audio_is_linear_pcm(format)) { |
| 2602 | size_t maxNotificationFrames; |
| 2603 | if (*flags & AUDIO_OUTPUT_FLAG_FAST) { |
| 2604 | // notify every HAL buffer, regardless of the size of the track buffer |
| 2605 | maxNotificationFrames = mFrameCount; |
| 2606 | } else { |
Andy Hung | fa11780 | 2019-04-12 13:50:39 -0700 | [diff] [blame] | 2607 | // Triple buffer the notification period for a triple buffered mixer period; |
| 2608 | // otherwise, double buffering for the notification period is fine. |
| 2609 | // |
| 2610 | // TODO: This should be moved to AudioTrack to modify the notification period |
| 2611 | // on AudioTrack::setBufferSizeInFrames() changes. |
| 2612 | const int nBuffering = |
| 2613 | (uint64_t{frameCount} * mSampleRate) |
| 2614 | / (uint64_t{mNormalFrameCount} * sampleRate) == 3 ? 3 : 2; |
| 2615 | |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2616 | maxNotificationFrames = frameCount / nBuffering; |
| 2617 | // If client requested a fast track but this was denied, then use the smaller maximum. |
| 2618 | if (requestedFlags & AUDIO_OUTPUT_FLAG_FAST) { |
| 2619 | size_t maxNotificationFramesFastDenied = FMS_20 * sampleRate / 1000; |
| 2620 | if (maxNotificationFrames > maxNotificationFramesFastDenied) { |
| 2621 | maxNotificationFrames = maxNotificationFramesFastDenied; |
| 2622 | } |
| 2623 | } |
| 2624 | } |
| 2625 | if (notificationFrameCount == 0 || notificationFrameCount > maxNotificationFrames) { |
| 2626 | if (notificationFrameCount == 0) { |
| 2627 | ALOGD("Client defaulted notificationFrames to %zu for frameCount %zu", |
| 2628 | maxNotificationFrames, frameCount); |
| 2629 | } else { |
| 2630 | ALOGW("Client adjusted notificationFrames from %zu to %zu for frameCount %zu", |
| 2631 | notificationFrameCount, maxNotificationFrames, frameCount); |
| 2632 | } |
| 2633 | notificationFrameCount = maxNotificationFrames; |
| 2634 | } |
| 2635 | } |
| 2636 | |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 2637 | *pFrameCount = frameCount; |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2638 | *pNotificationFrameCount = notificationFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2639 | |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2640 | switch (mType) { |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 2641 | case BIT_PERFECT: |
| 2642 | if (isBitPerfect) { |
| 2643 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
| 2644 | ALOGE("%s, bad parameter when request streaming bit-perfect, sampleRate=%u, " |
| 2645 | "format=%#x, channelMask=%#x, mSampleRate=%u, mFormat=%#x, mChannelMask=%#x", |
| 2646 | __func__, sampleRate, format, channelMask, mSampleRate, mFormat, |
| 2647 | mChannelMask); |
| 2648 | lStatus = BAD_VALUE; |
| 2649 | goto Exit; |
| 2650 | } |
| 2651 | } |
| 2652 | break; |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2653 | |
| 2654 | case DIRECT: |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 2655 | 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] | 2656 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 2657 | ALOGE("createTrack_l() Bad parameter: sampleRate %u format %#x, channelMask 0x%08x " |
| 2658 | "for output %p with format %#x", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2659 | sampleRate, format, channelMask, mOutput, mFormat); |
| 2660 | lStatus = BAD_VALUE; |
| 2661 | goto Exit; |
| 2662 | } |
| 2663 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2664 | break; |
| 2665 | |
| 2666 | case OFFLOAD: |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2667 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 2668 | ALOGE("createTrack_l() Bad parameter: sampleRate %d format %#x, channelMask 0x%08x \"" |
| 2669 | "for output %p with format %#x", |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2670 | sampleRate, format, channelMask, mOutput, mFormat); |
| 2671 | lStatus = BAD_VALUE; |
| 2672 | goto Exit; |
| 2673 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2674 | break; |
| 2675 | |
| 2676 | default: |
Glenn Kasten | 993fa06 | 2014-05-02 11:14:34 -0700 | [diff] [blame] | 2677 | if (!audio_is_linear_pcm(format)) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 2678 | ALOGE("createTrack_l() Bad parameter: format %#x \"" |
| 2679 | "for output %p with format %#x", |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2680 | format, mOutput, mFormat); |
| 2681 | lStatus = BAD_VALUE; |
| 2682 | goto Exit; |
| 2683 | } |
Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 2684 | if (sampleRate > mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2685 | ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate); |
| 2686 | lStatus = BAD_VALUE; |
| 2687 | goto Exit; |
| 2688 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2689 | break; |
| 2690 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2691 | } |
| 2692 | |
| 2693 | lStatus = initCheck(); |
| 2694 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 2695 | ALOGE("createTrack_l() audio driver not initialized"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2696 | goto Exit; |
| 2697 | } |
| 2698 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2699 | { // scope for mutex() |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2700 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2701 | |
| 2702 | // all tracks in same audio session must share the same routing strategy otherwise |
| 2703 | // conflicts will happen when tracks are moved from one output to another by audio policy |
| 2704 | // manager |
Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 2705 | product_strategy_t strategy = getStrategyForStream(streamType); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2706 | for (size_t i = 0; i < mTracks.size(); ++i) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2707 | sp<IAfTrack> t = mTracks[i]; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2708 | if (t != 0 && t->isExternalTrack()) { |
Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 2709 | product_strategy_t actual = getStrategyForStream(t->streamType()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2710 | if (sessionId == t->sessionId() && strategy != actual) { |
| 2711 | ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", |
| 2712 | strategy, actual); |
| 2713 | lStatus = BAD_VALUE; |
| 2714 | goto Exit; |
| 2715 | } |
| 2716 | } |
| 2717 | } |
| 2718 | |
Deeraj Soman | 2b51523 | 2024-05-14 12:58:24 +0530 | [diff] [blame] | 2719 | // Set DIRECT/OFFLOAD flag if current thread is DirectOutputThread/OffloadThread. |
| 2720 | // 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] | 2721 | // dynamic audio policy. |
| 2722 | // Do NOT report the flag changes back to client, since the client |
Deeraj Soman | 2b51523 | 2024-05-14 12:58:24 +0530 | [diff] [blame] | 2723 | // doesn't explicitly request a direct/offload flag. |
yucliu | c9c49cd | 2020-07-13 16:25:21 -0700 | [diff] [blame] | 2724 | audio_output_flags_t trackFlags = *flags; |
| 2725 | if (mType == DIRECT) { |
| 2726 | trackFlags = static_cast<audio_output_flags_t>(trackFlags | AUDIO_OUTPUT_FLAG_DIRECT); |
Deeraj Soman | 2b51523 | 2024-05-14 12:58:24 +0530 | [diff] [blame] | 2727 | } else if (mType == OFFLOAD) { |
| 2728 | trackFlags = static_cast<audio_output_flags_t>(trackFlags | |
| 2729 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT); |
yucliu | c9c49cd | 2020-07-13 16:25:21 -0700 | [diff] [blame] | 2730 | } |
jiabin | 94ed47c | 2023-07-27 23:34:20 +0000 | [diff] [blame] | 2731 | *afTrackFlags = trackFlags; |
yucliu | c9c49cd | 2020-07-13 16:25:21 -0700 | [diff] [blame] | 2732 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2733 | track = IAfTrack::create(this, client, streamType, attr, sampleRate, format, |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 2734 | channelMask, frameCount, |
| 2735 | nullptr /* buffer */, (size_t)0 /* bufferSize */, sharedBuffer, |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 2736 | sessionId, creatorPid, attributionSource, trackFlags, |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2737 | IAfTrackBase::TYPE_DEFAULT, portId, SIZE_MAX /*frameCountToBeReady*/, |
Vlad Popa | 1e865e6 | 2024-08-15 19:11:42 -0700 | [diff] [blame] | 2738 | speed, isSpatialized, isBitPerfect, volume, muted); |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 2739 | |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 2740 | lStatus = track != 0 ? track->initCheck() : (status_t) NO_MEMORY; |
| 2741 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 0cde076 | 2014-01-16 15:06:36 -0800 | [diff] [blame] | 2742 | ALOGE("createTrack_l() initCheck failed %d; no control block?", lStatus); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 2743 | // 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] | 2744 | goto Exit; |
| 2745 | } |
| 2746 | mTracks.add(track); |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 2747 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2748 | audio_utils::lock_guard _atCbL(audioTrackCbMutex()); |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 2749 | if (callback.get() != nullptr) { |
jiabin | 18a4b1c | 2020-09-17 11:40:42 -0700 | [diff] [blame] | 2750 | mAudioTrackCallbacks.emplace(track, callback); |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 2751 | } |
| 2752 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2753 | |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 2754 | sp<IAfEffectChain> chain = getEffectChain_l(sessionId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2755 | if (chain != 0) { |
| 2756 | ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); |
| 2757 | track->setMainBuffer(chain->inBuffer()); |
Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 2758 | chain->setStrategy(getStrategyForStream(track->streamType())); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2759 | chain->incTrackCnt(); |
| 2760 | } |
| 2761 | |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2762 | if ((*flags & AUDIO_OUTPUT_FLAG_FAST) && (tid != -1)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2763 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 2764 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, |
| 2765 | // so ask activity manager to do this on our behalf |
Glenn Kasten | af9a7b5 | 2017-03-29 11:29:39 -0700 | [diff] [blame] | 2766 | sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp, true /*forApp*/); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2767 | } |
| 2768 | } |
| 2769 | |
| 2770 | lStatus = NO_ERROR; |
| 2771 | |
| 2772 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 2773 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2774 | return track; |
| 2775 | } |
| 2776 | |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2777 | template<typename T> |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2778 | ssize_t PlaybackThread::Tracks<T>::remove(const sp<T>& track) |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2779 | { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 2780 | const int trackId = track->id(); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2781 | const ssize_t index = mTracks.remove(track); |
| 2782 | if (index >= 0) { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 2783 | if (mSaveDeletedTrackIds) { |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2784 | // 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] | 2785 | // Instead, we add to mDeletedTrackIds which is solely used for mAudioMixer update, |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2786 | // to be handled when MixerThread::prepareTracks_l() next changes mAudioMixer. |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 2787 | mDeletedTrackIds.emplace(trackId); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2788 | } |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2789 | } |
| 2790 | return index; |
| 2791 | } |
| 2792 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2793 | uint32_t PlaybackThread::correctLatency_l(uint32_t latency) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2794 | { |
| 2795 | return latency; |
| 2796 | } |
| 2797 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2798 | uint32_t PlaybackThread::latency() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2799 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2800 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2801 | return latency_l(); |
| 2802 | } |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2803 | uint32_t PlaybackThread::latency_l() const |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2804 | NO_THREAD_SAFETY_ANALYSIS |
| 2805 | // Fix later. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2806 | { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2807 | uint32_t latency; |
| 2808 | if (initCheck() == NO_ERROR && mOutput->stream->getLatency(&latency) == OK) { |
| 2809 | return correctLatency_l(latency); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2810 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2811 | return 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2812 | } |
| 2813 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2814 | void PlaybackThread::setMasterVolume(float value) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2815 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2816 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2817 | // Don't apply master volume in SW if our HAL can do it for us. |
| 2818 | if (mOutput && mOutput->audioHwDev && |
| 2819 | mOutput->audioHwDev->canSetMasterVolume()) { |
| 2820 | mMasterVolume = 1.0; |
| 2821 | } else { |
| 2822 | mMasterVolume = value; |
| 2823 | } |
| 2824 | } |
| 2825 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2826 | void PlaybackThread::setMasterBalance(float balance) |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 2827 | { |
| 2828 | mMasterBalance.store(balance); |
| 2829 | } |
| 2830 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2831 | void PlaybackThread::setMasterMute(bool muted) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2832 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2833 | if (isDuplicating()) { |
| 2834 | return; |
| 2835 | } |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2836 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2837 | // Don't apply master mute in SW if our HAL can do it for us. |
| 2838 | if (mOutput && mOutput->audioHwDev && |
| 2839 | mOutput->audioHwDev->canSetMasterMute()) { |
| 2840 | mMasterMute = false; |
| 2841 | } else { |
| 2842 | mMasterMute = muted; |
| 2843 | } |
| 2844 | } |
| 2845 | |
Vlad Popa | 1e865e6 | 2024-08-15 19:11:42 -0700 | [diff] [blame] | 2846 | void PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value, bool muted) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2847 | { |
Vlad Popa | 1e865e6 | 2024-08-15 19:11:42 -0700 | [diff] [blame] | 2848 | ALOGV("%s: stream %d value %f muted %d", __func__, stream, value, muted); |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2849 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2850 | mStreamTypes[stream].volume = value; |
Vlad Popa | 1e865e6 | 2024-08-15 19:11:42 -0700 | [diff] [blame] | 2851 | if (com_android_media_audio_ring_my_car()) { |
| 2852 | mStreamTypes[stream].mute = muted; |
| 2853 | } |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2854 | broadcast_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2855 | } |
| 2856 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2857 | void PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2858 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2859 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2860 | mStreamTypes[stream].mute = muted; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2861 | broadcast_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2862 | } |
| 2863 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2864 | float PlaybackThread::streamVolume(audio_stream_type_t stream) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2865 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2866 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2867 | return mStreamTypes[stream].volume; |
| 2868 | } |
| 2869 | |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 2870 | status_t PlaybackThread::setPortsVolume( |
Vlad Popa | 1e865e6 | 2024-08-15 19:11:42 -0700 | [diff] [blame] | 2871 | const std::vector<audio_port_handle_t>& portIds, float volume, bool muted) { |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 2872 | audio_utils::lock_guard _l(mutex()); |
| 2873 | for (const auto& portId : portIds) { |
| 2874 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 2875 | sp<IAfTrack> track = mTracks[i].get(); |
| 2876 | if (portId == track->portId()) { |
| 2877 | track->setPortVolume(volume); |
Vlad Popa | 1e865e6 | 2024-08-15 19:11:42 -0700 | [diff] [blame] | 2878 | track->setPortMute(muted); |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 2879 | break; |
| 2880 | } |
| 2881 | } |
| 2882 | } |
| 2883 | broadcast_l(); |
| 2884 | return NO_ERROR; |
| 2885 | } |
| 2886 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2887 | void PlaybackThread::setVolumeForOutput_l(float left, float right) const |
Tomoharu Kasahara | 1990bd4 | 2014-12-12 14:04:11 +0900 | [diff] [blame] | 2888 | { |
| 2889 | mOutput->stream->setVolume(left, right); |
| 2890 | } |
| 2891 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2892 | // addTrack_l() must be called with ThreadBase::mutex() held |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2893 | status_t PlaybackThread::addTrack_l(const sp<IAfTrack>& track) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2894 | { |
| 2895 | status_t status = ALREADY_EXISTS; |
| 2896 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2897 | if (mActiveTracks.indexOf(track) < 0) { |
| 2898 | // the track is newly added, make sure it fills up all its |
| 2899 | // buffers before playing. This is to ensure the client will |
| 2900 | // effectively get the latency it requested. |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2901 | if (track->isExternalTrack()) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2902 | IAfTrackBase::track_state state = track->state(); |
Andy Hung | 6c498e9 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 2903 | // Because the track is not on the ActiveTracks, |
| 2904 | // at this point, only the TrackHandle will be adding the track. |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2905 | mutex().unlock(); |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 2906 | status = AudioSystem::startOutput(track->portId()); |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2907 | mutex().lock(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2908 | // abort track was stopped/paused while we released the lock |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2909 | if (state != track->state()) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2910 | if (status == NO_ERROR) { |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2911 | mutex().unlock(); |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 2912 | AudioSystem::stopOutput(track->portId()); |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2913 | mutex().lock(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2914 | } |
| 2915 | return INVALID_OPERATION; |
| 2916 | } |
| 2917 | // abort if start is rejected by audio policy manager |
| 2918 | if (status != NO_ERROR) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 2919 | // Do not replace the error if it is DEAD_OBJECT. When this happens, it indicates |
| 2920 | // current playback thread is reopened, which may happen when clients set preferred |
| 2921 | // mixer configuration. Returning DEAD_OBJECT will make the client restore track |
| 2922 | // immediately. |
| 2923 | return status == DEAD_OBJECT ? status : PERMISSION_DENIED; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2924 | } |
| 2925 | #ifdef ADD_BATTERY_DATA |
| 2926 | // to track the speaker usage |
| 2927 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); |
| 2928 | #endif |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 2929 | sendIoConfigEvent_l(AUDIO_CLIENT_STARTED, track->creatorPid(), track->portId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2930 | } |
| 2931 | |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2932 | // set retry count for buffer fill |
| 2933 | if (track->isOffloaded()) { |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 2934 | if (track->isStopping_1()) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2935 | track->retryCount() = kMaxTrackStopRetriesOffload; |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 2936 | } else { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2937 | track->retryCount() = kMaxTrackStartupRetriesOffload; |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 2938 | } |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2939 | track->fillingStatus() = mStandby ? IAfTrack::FS_FILLING : IAfTrack::FS_FILLED; |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2940 | } else { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2941 | track->retryCount() = kMaxTrackStartupRetries; |
| 2942 | track->fillingStatus() = |
| 2943 | track->sharedBuffer() != 0 ? IAfTrack::FS_FILLED : IAfTrack::FS_FILLING; |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2944 | } |
| 2945 | |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 2946 | sp<IAfEffectChain> chain = getEffectChain_l(track->sessionId()); |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 2947 | if (mHapticChannelMask != AUDIO_CHANNEL_NONE |
| 2948 | && ((track->channelMask() & AUDIO_CHANNEL_HAPTIC_ALL) != AUDIO_CHANNEL_NONE |
Shunkai Yao | 29d1057 | 2024-03-19 04:31:47 +0000 | [diff] [blame] | 2949 | || (chain != nullptr && chain->containsHapticGeneratingEffect()))) { |
jiabin | 57303cc | 2018-12-18 15:45:57 -0800 | [diff] [blame] | 2950 | // Unlock due to VibratorService will lock for this call and will |
| 2951 | // call Tracks.mute/unmute which also require thread's lock. |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2952 | mutex().unlock(); |
Ahmad Khalil | 229466a | 2024-02-05 12:15:30 +0000 | [diff] [blame] | 2953 | const os::HapticScale hapticScale = afutils::onExternalVibrationStart( |
jiabin | 57303cc | 2018-12-18 15:45:57 -0800 | [diff] [blame] | 2954 | track->getExternalVibration()); |
Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 2955 | std::optional<media::AudioVibratorInfo> vibratorInfo; |
| 2956 | { |
| 2957 | // TODO(b/184194780): Use the vibrator information from the vibrator that will be |
| 2958 | // used to play this track. |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2959 | audio_utils::lock_guard _l(mAfThreadCallback->mutex()); |
Yi Kong | 3ac211f | 2024-08-12 07:31:44 +0800 | [diff] [blame] | 2960 | vibratorInfo = mAfThreadCallback->getDefaultVibratorInfo_l(); |
Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 2961 | } |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2962 | mutex().lock(); |
Ahmad Khalil | 229466a | 2024-02-05 12:15:30 +0000 | [diff] [blame] | 2963 | track->setHapticScale(hapticScale); |
Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 2964 | if (vibratorInfo) { |
| 2965 | track->setHapticMaxAmplitude(vibratorInfo->maxAmplitude); |
| 2966 | } |
| 2967 | |
jiabin | 57303cc | 2018-12-18 15:45:57 -0800 | [diff] [blame] | 2968 | // Haptic playback should be enabled by vibrator service. |
| 2969 | if (track->getHapticPlaybackEnabled()) { |
| 2970 | // Disable haptic playback of all active track to ensure only |
| 2971 | // one track playing haptic if current track should play haptic. |
| 2972 | for (const auto &t : mActiveTracks) { |
| 2973 | t->setHapticPlaybackEnabled(false); |
| 2974 | } |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 2975 | } |
jiabin | e70bc7f | 2020-06-30 22:07:55 -0700 | [diff] [blame] | 2976 | |
| 2977 | // Set haptic intensity for effect |
| 2978 | if (chain != nullptr) { |
Ahmad Khalil | 229466a | 2024-02-05 12:15:30 +0000 | [diff] [blame] | 2979 | chain->setHapticScale_l(track->id(), hapticScale); |
jiabin | e70bc7f | 2020-06-30 22:07:55 -0700 | [diff] [blame] | 2980 | } |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 2981 | } |
| 2982 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2983 | track->setResetDone(false); |
Andy Hung | 59de426 | 2021-06-14 10:53:54 -0700 | [diff] [blame] | 2984 | track->resetPresentationComplete(); |
Andy Hung | 6c498e9 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 2985 | |
| 2986 | // Do not release the ThreadBase mutex after the track is added to mActiveTracks unless |
| 2987 | // all key changes are complete. It is possible that the threadLoop will begin |
| 2988 | // processing the added track immediately after the ThreadBase mutex is released. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2989 | mActiveTracks.add(track); |
Andy Hung | 6c498e9 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 2990 | |
Eric Laurent | d0107bc | 2013-06-11 14:38:48 -0700 | [diff] [blame] | 2991 | if (chain != 0) { |
| 2992 | ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), |
| 2993 | track->sessionId()); |
| 2994 | chain->incActiveTrackCnt(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2995 | } |
| 2996 | |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 2997 | track->logBeginInterval(patchSinksToString(&mPatch)); // log to MediaMetrics |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2998 | status = NO_ERROR; |
| 2999 | } |
| 3000 | |
Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 3001 | onAddNewTrack_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3002 | return status; |
| 3003 | } |
| 3004 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3005 | bool PlaybackThread::destroyTrack_l(const sp<IAfTrack>& track) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3006 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3007 | track->terminate(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3008 | // active tracks are removed by threadLoop() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3009 | bool trackActive = (mActiveTracks.indexOf(track) >= 0); |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3010 | track->setState(IAfTrackBase::STOPPED); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3011 | if (!trackActive) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3012 | removeTrack_l(track); |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 3013 | } else if (track->isFastTrack() || track->isOffloaded() || track->isDirect()) { |
Andy Hung | 916987e | 2023-03-20 19:08:16 -0700 | [diff] [blame] | 3014 | if (track->isPausePending()) { |
| 3015 | track->pauseAck(); |
| 3016 | } |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3017 | track->setState(IAfTrackBase::STOPPING_1); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3018 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3019 | |
| 3020 | return trackActive; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3021 | } |
| 3022 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3023 | void PlaybackThread::removeTrack_l(const sp<IAfTrack>& track) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3024 | { |
| 3025 | track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); |
Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 3026 | |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 3027 | String8 result; |
| 3028 | track->appendDump(result, false /* active */); |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 3029 | mLocalLog.log("removeTrack_l (%p) %s", track.get(), result.c_str()); |
Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 3030 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3031 | mTracks.remove(track); |
jiabin | 18a4b1c | 2020-09-17 11:40:42 -0700 | [diff] [blame] | 3032 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3033 | audio_utils::lock_guard _atCbL(audioTrackCbMutex()); |
jiabin | 18a4b1c | 2020-09-17 11:40:42 -0700 | [diff] [blame] | 3034 | mAudioTrackCallbacks.erase(track); |
| 3035 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3036 | if (track->isFastTrack()) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3037 | int index = track->fastIndex(); |
Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 3038 | ALOG_ASSERT(0 < index && index < (int)FastMixerState::sMaxFastTracks); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3039 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index))); |
| 3040 | mFastTrackAvailMask |= 1 << index; |
| 3041 | // redundant as track is about to be destroyed, for dumpsys only |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3042 | track->fastIndex() = -1; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3043 | } |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 3044 | sp<IAfEffectChain> chain = getEffectChain_l(track->sessionId()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3045 | if (chain != 0) { |
| 3046 | chain->decTrackCnt(); |
| 3047 | } |
| 3048 | } |
| 3049 | |
Mikhail Naganov | bf203ce | 2024-05-23 16:27:59 -0700 | [diff] [blame] | 3050 | std::set<audio_port_handle_t> PlaybackThread::getTrackPortIds_l() |
| 3051 | { |
| 3052 | std::set<int32_t> result; |
| 3053 | for (const auto& t : mTracks) { |
| 3054 | if (t->isExternalTrack()) { |
| 3055 | result.insert(t->portId()); |
| 3056 | } |
| 3057 | } |
| 3058 | return result; |
| 3059 | } |
| 3060 | |
| 3061 | std::set<audio_port_handle_t> PlaybackThread::getTrackPortIds() |
| 3062 | { |
| 3063 | audio_utils::lock_guard _l(mutex()); |
| 3064 | return getTrackPortIds_l(); |
| 3065 | } |
| 3066 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3067 | String8 PlaybackThread::getParameters(const String8& keys) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3068 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3069 | audio_utils::lock_guard _l(mutex()); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3070 | String8 out_s8; |
| 3071 | if (initCheck() == NO_ERROR && mOutput->stream->getParameters(keys, &out_s8) == OK) { |
| 3072 | return out_s8; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3073 | } |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 3074 | return {}; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3075 | } |
| 3076 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3077 | status_t DirectOutputThread::selectPresentation(int presentationId, int programId) { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3078 | audio_utils::lock_guard _l(mutex()); |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 3079 | if (!isStreamInitialized()) { |
Mikhail Naganov | ac917ac | 2018-11-28 14:03:52 -0800 | [diff] [blame] | 3080 | return NO_INIT; |
| 3081 | } |
| 3082 | return mOutput->stream->selectPresentation(presentationId, programId); |
| 3083 | } |
| 3084 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 3085 | 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] | 3086 | audio_port_handle_t portId) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 3087 | ALOGV("PlaybackThread::ioConfigChanged, thread %p, event %d", this, event); |
Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 3088 | sp<AudioIoDescriptor> desc; |
| 3089 | const struct audio_patch patch = isMsdDevice() ? mDownStreamPatch : mPatch; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3090 | switch (event) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 3091 | case AUDIO_OUTPUT_OPENED: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 3092 | case AUDIO_OUTPUT_REGISTERED: |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 3093 | case AUDIO_OUTPUT_CONFIG_CHANGED: |
Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 3094 | desc = sp<AudioIoDescriptor>::make(mId, patch, false /*isInput*/, |
| 3095 | mSampleRate, mFormat, mChannelMask, |
| 3096 | // FIXME AudioFlinger::frameCount(audio_io_handle_t) instead of mNormalFrameCount? |
| 3097 | mNormalFrameCount, mFrameCount, latency_l()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3098 | break; |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 3099 | case AUDIO_CLIENT_STARTED: |
Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 3100 | desc = sp<AudioIoDescriptor>::make(mId, patch, portId); |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 3101 | break; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 3102 | case AUDIO_OUTPUT_CLOSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3103 | default: |
Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 3104 | desc = sp<AudioIoDescriptor>::make(mId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3105 | break; |
| 3106 | } |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 3107 | mAfThreadCallback->ioConfigChanged_l(event, desc, pid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3108 | } |
| 3109 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3110 | void PlaybackThread::onWriteReady() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3111 | { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3112 | mCallbackThread->resetWriteBlocked(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3113 | } |
| 3114 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3115 | void PlaybackThread::onDrainReady() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3116 | { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3117 | mCallbackThread->resetDraining(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3118 | } |
| 3119 | |
Mikhail Naganov | bf203ce | 2024-05-23 16:27:59 -0700 | [diff] [blame] | 3120 | void PlaybackThread::onError(bool isHardError) |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 3121 | { |
Mikhail Naganov | bf203ce | 2024-05-23 16:27:59 -0700 | [diff] [blame] | 3122 | mCallbackThread->setAsyncError(isHardError); |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 3123 | } |
| 3124 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3125 | void PlaybackThread::onCodecFormatChanged( |
Ryan Prichard | 78c5e45 | 2024-02-08 16:16:57 -0800 | [diff] [blame] | 3126 | const std::vector<uint8_t>& metadataBs) |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 3127 | { |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3128 | const auto weakPointerThis = wp<PlaybackThread>::fromExisting(this); |
Kuowei Li | 9e2f616 | 2022-11-23 16:25:26 +0800 | [diff] [blame] | 3129 | std::thread([this, metadataBs, weakPointerThis]() { |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3130 | const sp<PlaybackThread> playbackThread = weakPointerThis.promote(); |
Kuowei Li | 9e2f616 | 2022-11-23 16:25:26 +0800 | [diff] [blame] | 3131 | if (playbackThread == nullptr) { |
| 3132 | ALOGW("PlaybackThread was destroyed, skip codec format change event"); |
| 3133 | return; |
| 3134 | } |
| 3135 | |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 3136 | audio_utils::metadata::Data metadata = |
| 3137 | audio_utils::metadata::dataFromByteString(metadataBs); |
| 3138 | if (metadata.empty()) { |
| 3139 | ALOGW("Can not transform the buffer to audio metadata, %s, %d", |
| 3140 | reinterpret_cast<char*>(const_cast<uint8_t*>(metadataBs.data())), |
| 3141 | (int)metadataBs.size()); |
| 3142 | return; |
| 3143 | } |
| 3144 | |
| 3145 | audio_utils::metadata::ByteString metaDataStr = |
| 3146 | audio_utils::metadata::byteStringFromData(metadata); |
| 3147 | std::vector metadataVec(metaDataStr.begin(), metaDataStr.end()); |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3148 | audio_utils::lock_guard _l(audioTrackCbMutex()); |
jiabin | 18a4b1c | 2020-09-17 11:40:42 -0700 | [diff] [blame] | 3149 | for (const auto& callbackPair : mAudioTrackCallbacks) { |
| 3150 | callbackPair.second->onCodecFormatChanged(metadataVec); |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 3151 | } |
| 3152 | }).detach(); |
| 3153 | } |
| 3154 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3155 | void PlaybackThread::resetWriteBlocked(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3156 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3157 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3158 | // reject out of sequence requests |
| 3159 | if ((mWriteAckSequence & 1) && (sequence == mWriteAckSequence)) { |
| 3160 | mWriteAckSequence &= ~1; |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 3161 | mWaitWorkCV.notify_one(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3162 | } |
| 3163 | } |
| 3164 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3165 | void PlaybackThread::resetDraining(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3166 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3167 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3168 | // reject out of sequence requests |
| 3169 | if ((mDrainSequence & 1) && (sequence == mDrainSequence)) { |
Andy Hung | f323451 | 2018-07-03 14:51:47 -0700 | [diff] [blame] | 3170 | // Register discontinuity when HW drain is completed because that can cause |
| 3171 | // the timestamp frame position to reset to 0 for direct and offload threads. |
| 3172 | // (Out of sequence requests are ignored, since the discontinuity would be handled |
| 3173 | // elsewhere, e.g. in flush). |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 3174 | mTimestampVerifier.discontinuity(mTimestampVerifier.DISCONTINUITY_MODE_ZERO); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3175 | mDrainSequence &= ~1; |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 3176 | mWaitWorkCV.notify_one(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3177 | } |
| 3178 | } |
| 3179 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3180 | void PlaybackThread::readOutputParameters_l() |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3181 | NO_THREAD_SAFETY_ANALYSIS |
| 3182 | // 'moveEffectChain_ll' requires holding mutex 'AudioFlinger_Mutex' exclusively |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3183 | { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 3184 | // 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] | 3185 | const audio_config_base_t audioConfig = mOutput->getAudioProperties(); |
| 3186 | mSampleRate = audioConfig.sample_rate; |
| 3187 | mChannelMask = audioConfig.channel_mask; |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 3188 | if (!audio_is_output_channel(mChannelMask)) { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 3189 | LOG_ALWAYS_FATAL("HAL channel mask %#x not valid for output", mChannelMask); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 3190 | } |
Andy Hung | 81994d6 | 2023-07-20 21:44:14 -0700 | [diff] [blame] | 3191 | if (hasMixer() && !isValidPcmSinkChannelMask(mChannelMask)) { |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 3192 | LOG_ALWAYS_FATAL("HAL channel mask %#x not supported for mixed output", |
| 3193 | mChannelMask); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 3194 | } |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 3195 | |
| 3196 | if (mMixerChannelMask == AUDIO_CHANNEL_NONE) { |
| 3197 | mMixerChannelMask = mChannelMask; |
| 3198 | } |
| 3199 | |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 3200 | mChannelCount = audio_channel_count_from_out_mask(mChannelMask); |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 3201 | mBalance.setChannelMask(mChannelMask); |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 3202 | |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 3203 | uint32_t mixerChannelCount = audio_channel_count_from_out_mask(mMixerChannelMask); |
| 3204 | |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 3205 | // Get actual HAL format. |
Mikhail Naganov | 560637e | 2021-03-31 22:40:13 +0000 | [diff] [blame] | 3206 | status_t result = mOutput->stream->getAudioProperties(nullptr, nullptr, &mHALFormat); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3207 | 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] | 3208 | // Get format from the shim, which will be different than the HAL format |
| 3209 | // if playing compressed audio over HDMI passthrough. |
Mikhail Naganov | 560637e | 2021-03-31 22:40:13 +0000 | [diff] [blame] | 3210 | mFormat = audioConfig.format; |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 3211 | if (!audio_is_valid_format(mFormat)) { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 3212 | LOG_ALWAYS_FATAL("HAL format %#x not valid for output", mFormat); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 3213 | } |
Andy Hung | 81994d6 | 2023-07-20 21:44:14 -0700 | [diff] [blame] | 3214 | if (hasMixer() && !isValidPcmSinkFormat(mFormat)) { |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 3215 | LOG_FATAL("HAL format %#x not supported for mixed output", |
| 3216 | mFormat); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 3217 | } |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 3218 | mFrameSize = mOutput->getFrameSize(); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3219 | result = mOutput->stream->getBufferSize(&mBufferSize); |
| 3220 | LOG_ALWAYS_FATAL_IF(result != OK, |
| 3221 | "Error when retrieving output stream buffer size: %d", result); |
Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 3222 | mFrameCount = mBufferSize / mFrameSize; |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 3223 | if (hasMixer() && (mFrameCount & 15)) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3224 | 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] | 3225 | mFrameCount); |
| 3226 | } |
| 3227 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 3228 | mHwSupportsPause = false; |
| 3229 | if (mOutput->flags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3230 | bool supportsPause = false, supportsResume = false; |
| 3231 | if (mOutput->stream->supportsPauseAndResume(&supportsPause, &supportsResume) == OK) { |
| 3232 | if (supportsPause && supportsResume) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 3233 | mHwSupportsPause = true; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3234 | } else if (supportsPause) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 3235 | ALOGW("direct output implements pause but not resume"); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3236 | } else if (supportsResume) { |
| 3237 | ALOGW("direct output implements resume but not pause"); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 3238 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 3239 | } |
| 3240 | } |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 3241 | if (!mHwSupportsPause && mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) { |
| 3242 | LOG_ALWAYS_FATAL("HW_AV_SYNC requested but HAL does not implement pause and resume"); |
| 3243 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 3244 | |
Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 3245 | if (mType == DUPLICATING && mMixerBufferEnabled && mEffectBufferEnabled) { |
| 3246 | // For best precision, we use float instead of the associated output |
| 3247 | // device format (typically PCM 16 bit). |
| 3248 | |
| 3249 | mFormat = AUDIO_FORMAT_PCM_FLOAT; |
| 3250 | mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); |
| 3251 | mBufferSize = mFrameSize * mFrameCount; |
| 3252 | |
| 3253 | // TODO: We currently use the associated output device channel mask and sample rate. |
| 3254 | // (1) Perhaps use the ORed channel mask of all downstream MixerThreads |
| 3255 | // (if a valid mask) to avoid premature downmix. |
| 3256 | // (2) Perhaps use the maximum sample rate of all downstream MixerThreads |
| 3257 | // instead of the output device sample rate to avoid loss of high frequency information. |
| 3258 | // This may need to be updated as MixerThread/OutputTracks are added and not here. |
| 3259 | } |
| 3260 | |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 3261 | // 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] | 3262 | double multiplier = 1.0; |
Henrik Tillman | 470b399 | 2024-10-08 12:49:28 +0200 | [diff] [blame] | 3263 | // Note: mType == SPATIALIZER does not support FastMixer and DEEP is by definition not "fast" |
| 3264 | if ((mType == MIXER && !(mOutput->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER)) && |
| 3265 | (kUseFastMixer == FastMixer_Static || kUseFastMixer == FastMixer_Dynamic)) { |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 3266 | size_t minNormalFrameCount = (kMinNormalSinkBufferSizeMs * mSampleRate) / 1000; |
| 3267 | size_t maxNormalFrameCount = (kMaxNormalSinkBufferSizeMs * mSampleRate) / 1000; |
Haynes Mathew George | 227a14b | 2016-05-09 12:45:48 -0700 | [diff] [blame] | 3268 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3269 | // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer |
| 3270 | minNormalFrameCount = (minNormalFrameCount + 15) & ~15; |
| 3271 | maxNormalFrameCount = maxNormalFrameCount & ~15; |
| 3272 | if (maxNormalFrameCount < minNormalFrameCount) { |
| 3273 | maxNormalFrameCount = minNormalFrameCount; |
| 3274 | } |
| 3275 | multiplier = (double) minNormalFrameCount / (double) mFrameCount; |
| 3276 | if (multiplier <= 1.0) { |
| 3277 | multiplier = 1.0; |
| 3278 | } else if (multiplier <= 2.0) { |
| 3279 | if (2 * mFrameCount <= maxNormalFrameCount) { |
| 3280 | multiplier = 2.0; |
| 3281 | } else { |
| 3282 | multiplier = (double) maxNormalFrameCount / (double) mFrameCount; |
| 3283 | } |
| 3284 | } else { |
Haynes Mathew George | 227a14b | 2016-05-09 12:45:48 -0700 | [diff] [blame] | 3285 | multiplier = floor(multiplier); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3286 | } |
| 3287 | } |
| 3288 | mNormalFrameCount = multiplier * mFrameCount; |
| 3289 | // round up to nearest 16 frames to satisfy AudioMixer |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 3290 | if (hasMixer()) { |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 3291 | mNormalFrameCount = (mNormalFrameCount + 15) & ~15; |
| 3292 | } |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 3293 | ALOGI("HAL output buffer size %zu frames, normal sink buffer size %zu frames", |
| 3294 | (size_t)mFrameCount, mNormalFrameCount); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3295 | |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3296 | // Check if we want to throttle the processing to no more than 2x normal rate |
| 3297 | mThreadThrottle = property_get_bool("af.thread.throttle", true /* default_value */); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3298 | mThreadThrottleTimeMs = 0; |
| 3299 | mThreadThrottleEndMs = 0; |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3300 | mHalfBufferMs = mNormalFrameCount * 1000 / (2 * mSampleRate); |
| 3301 | |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 3302 | // mSinkBuffer is the sink buffer. Size is always multiple-of-16 frames. |
| 3303 | // Originally this was int16_t[] array, need to remove legacy implications. |
| 3304 | free(mSinkBuffer); |
| 3305 | mSinkBuffer = NULL; |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 3306 | |
Andy Hung | 5b10a20 | 2014-03-13 13:59:29 -0700 | [diff] [blame] | 3307 | // For sink buffer size, we use the frame size from the downstream sink to avoid problems |
| 3308 | // with non PCM formats for compressed music, e.g. AAC, and Offload threads. |
| 3309 | const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 3310 | (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3311 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3312 | // We resize the mMixerBuffer according to the requirements of the sink buffer which |
| 3313 | // drives the output. |
| 3314 | free(mMixerBuffer); |
| 3315 | mMixerBuffer = NULL; |
| 3316 | if (mMixerBufferEnabled) { |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 3317 | 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] | 3318 | mMixerBufferSize = mNormalFrameCount * mixerChannelCount |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3319 | * audio_bytes_per_sample(mMixerBufferFormat); |
| 3320 | (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize); |
| 3321 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3322 | free(mEffectBuffer); |
| 3323 | mEffectBuffer = NULL; |
| 3324 | if (mEffectBufferEnabled) { |
Andy Hung | 319587b | 2023-05-23 14:01:03 -0700 | [diff] [blame] | 3325 | mEffectBufferFormat = AUDIO_FORMAT_PCM_FLOAT; |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 3326 | mEffectBufferSize = mNormalFrameCount * mixerChannelCount |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3327 | * audio_bytes_per_sample(mEffectBufferFormat); |
| 3328 | (void)posix_memalign(&mEffectBuffer, 32, mEffectBufferSize); |
| 3329 | } |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3330 | |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3331 | if (mType == SPATIALIZER) { |
| 3332 | free(mPostSpatializerBuffer); |
| 3333 | mPostSpatializerBuffer = nullptr; |
| 3334 | mPostSpatializerBufferSize = mNormalFrameCount * mChannelCount |
| 3335 | * audio_bytes_per_sample(mEffectBufferFormat); |
| 3336 | (void)posix_memalign(&mPostSpatializerBuffer, 32, mPostSpatializerBufferSize); |
| 3337 | } |
| 3338 | |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 3339 | mHapticChannelMask = static_cast<audio_channel_mask_t>(mChannelMask & AUDIO_CHANNEL_HAPTIC_ALL); |
| 3340 | mChannelMask = static_cast<audio_channel_mask_t>(mChannelMask & ~mHapticChannelMask); |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 3341 | mHapticChannelCount = audio_channel_count_from_out_mask(mHapticChannelMask); |
| 3342 | mChannelCount -= mHapticChannelCount; |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 3343 | mMixerChannelMask = static_cast<audio_channel_mask_t>(mMixerChannelMask & ~mHapticChannelMask); |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 3344 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3345 | // force reconfiguration of effect chains and engines to take new buffer size and audio |
| 3346 | // parameters into account |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 3347 | // 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] | 3348 | // but in this case nothing is done below as no audio sessions have effect yet so it doesn't |
| 3349 | // matter. |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3350 | // create a copy of mEffectChains as calling moveEffectChain_ll() |
| 3351 | // can reorder some effect chains |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 3352 | Vector<sp<IAfEffectChain>> effectChains = mEffectChains; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3353 | for (size_t i = 0; i < effectChains.size(); i ++) { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3354 | mAfThreadCallback->moveEffectChain_ll(effectChains[i]->sessionId(), |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3355 | this/* srcThread */, this/* dstThread */); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3356 | } |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 3357 | |
George Burgess IV | 5e4d86f | 2020-02-18 12:55:36 -0800 | [diff] [blame] | 3358 | audio_output_flags_t flags = mOutput->flags; |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 3359 | mediametrics::LogItem item(mThreadMetrics.getMetricsId()); // TODO: method in ThreadMetrics? |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 3360 | item.set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_READPARAMETERS) |
Andy Hung | 25a80ac | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 3361 | .set(AMEDIAMETRICS_PROP_ENCODING, IAfThreadBase::formatToString(mFormat).c_str()) |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 3362 | .set(AMEDIAMETRICS_PROP_SAMPLERATE, (int32_t)mSampleRate) |
| 3363 | .set(AMEDIAMETRICS_PROP_CHANNELMASK, (int32_t)mChannelMask) |
| 3364 | .set(AMEDIAMETRICS_PROP_CHANNELCOUNT, (int32_t)mChannelCount) |
| 3365 | .set(AMEDIAMETRICS_PROP_FRAMECOUNT, (int32_t)mNormalFrameCount) |
| 3366 | .set(AMEDIAMETRICS_PROP_FLAGS, toString(flags).c_str()) |
| 3367 | .set(AMEDIAMETRICS_PROP_PREFIX_HAPTIC AMEDIAMETRICS_PROP_CHANNELMASK, |
| 3368 | (int32_t)mHapticChannelMask) |
| 3369 | .set(AMEDIAMETRICS_PROP_PREFIX_HAPTIC AMEDIAMETRICS_PROP_CHANNELCOUNT, |
| 3370 | (int32_t)mHapticChannelCount) |
| 3371 | .set(AMEDIAMETRICS_PROP_PREFIX_HAL AMEDIAMETRICS_PROP_ENCODING, |
Andy Hung | 25a80ac | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 3372 | IAfThreadBase::formatToString(mHALFormat).c_str()) |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 3373 | .set(AMEDIAMETRICS_PROP_PREFIX_HAL AMEDIAMETRICS_PROP_FRAMECOUNT, |
| 3374 | (int32_t)mFrameCount) // sic - added HAL |
| 3375 | ; |
| 3376 | uint32_t latencyMs; |
| 3377 | if (mOutput->stream->getLatency(&latencyMs) == NO_ERROR) { |
| 3378 | item.set(AMEDIAMETRICS_PROP_PREFIX_HAL AMEDIAMETRICS_PROP_LATENCYMS, (double)latencyMs); |
| 3379 | } |
| 3380 | item.record(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3381 | } |
| 3382 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3383 | ThreadBase::MetadataUpdate PlaybackThread::updateMetadata_l() |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 3384 | { |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 3385 | if (!isStreamInitialized() || !mActiveTracks.readAndClearHasChanged()) { |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 3386 | return {}; // nothing to do |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 3387 | } |
| 3388 | StreamOutHalInterface::SourceMetadata metadata; |
Nikhil Bhanu | 8f4ea77 | 2024-01-31 17:15:52 -0800 | [diff] [blame] | 3389 | static const bool stereo_spatialization_property = |
| 3390 | property_get_bool("ro.audio.stereo_spatialization_enabled", false); |
| 3391 | const bool stereo_spatialization_enabled = |
| 3392 | stereo_spatialization_property && com_android_media_audio_stereo_spatialization(); |
| 3393 | if (stereo_spatialization_enabled) { |
Eric Laurent | 4eb45d0 | 2023-12-20 12:07:17 +0100 | [diff] [blame] | 3394 | std::map<audio_session_t, std::vector<playback_track_metadata_v7_t> >allSessionsMetadata; |
| 3395 | for (const sp<IAfTrack>& track : mActiveTracks) { |
| 3396 | std::vector<playback_track_metadata_v7_t>& sessionMetadata = |
| 3397 | allSessionsMetadata[track->sessionId()]; |
| 3398 | auto backInserter = std::back_inserter(sessionMetadata); |
| 3399 | // No track is invalid as this is called after prepareTrack_l in the same |
| 3400 | // critical section |
| 3401 | track->copyMetadataTo(backInserter); |
| 3402 | } |
| 3403 | std::vector<playback_track_metadata_v7_t> spatializedTracksMetaData; |
| 3404 | for (const auto& [session, sessionTrackMetadata] : allSessionsMetadata) { |
| 3405 | metadata.tracks.insert(metadata.tracks.end(), |
| 3406 | sessionTrackMetadata.begin(), sessionTrackMetadata.end()); |
| 3407 | if (auto chain = getEffectChain_l(session) ; chain != nullptr) { |
| 3408 | chain->sendMetadata_l(sessionTrackMetadata, {}); |
| 3409 | } |
| 3410 | if ((hasAudioSession_l(session) & IAfThreadBase::SPATIALIZED_SESSION) != 0) { |
| 3411 | spatializedTracksMetaData.insert(spatializedTracksMetaData.end(), |
| 3412 | sessionTrackMetadata.begin(), sessionTrackMetadata.end()); |
| 3413 | } |
| 3414 | } |
| 3415 | if (auto chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); chain != nullptr) { |
| 3416 | chain->sendMetadata_l(metadata.tracks, {}); |
| 3417 | } |
| 3418 | if (auto chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_STAGE); chain != nullptr) { |
| 3419 | chain->sendMetadata_l(metadata.tracks, spatializedTracksMetaData); |
| 3420 | } |
| 3421 | if (auto chain = getEffectChain_l(AUDIO_SESSION_DEVICE); chain != nullptr) { |
| 3422 | chain->sendMetadata_l(metadata.tracks, {}); |
| 3423 | } |
| 3424 | } else { |
| 3425 | auto backInserter = std::back_inserter(metadata.tracks); |
| 3426 | for (const sp<IAfTrack>& track : mActiveTracks) { |
| 3427 | // No track is invalid as this is called after prepareTrack_l in the same |
| 3428 | // critical section |
| 3429 | track->copyMetadataTo(backInserter); |
| 3430 | } |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 3431 | } |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 3432 | sendMetadataToBackend_l(metadata); |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 3433 | MetadataUpdate change; |
| 3434 | change.playbackMetadataUpdate = metadata.tracks; |
| 3435 | return change; |
Kevin Rocard | 80ee272 | 2018-04-11 15:53:48 +0000 | [diff] [blame] | 3436 | } |
Kevin Rocard | c86a7f7 | 2018-04-03 09:00:09 -0700 | [diff] [blame] | 3437 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3438 | void PlaybackThread::sendMetadataToBackend_l( |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 3439 | const StreamOutHalInterface::SourceMetadata& metadata) |
| 3440 | { |
| 3441 | mOutput->stream->updateSourceMetadata(metadata); |
| 3442 | }; |
| 3443 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3444 | status_t PlaybackThread::getRenderPosition( |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 3445 | uint32_t* halFrames, uint32_t* dspFrames) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3446 | { |
| 3447 | if (halFrames == NULL || dspFrames == NULL) { |
| 3448 | return BAD_VALUE; |
| 3449 | } |
Andy Hung | 972bec1 | 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 | if (initCheck() != NO_ERROR) { |
| 3452 | return INVALID_OPERATION; |
| 3453 | } |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3454 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3455 | *halFrames = framesWritten; |
| 3456 | |
| 3457 | if (isSuspended()) { |
| 3458 | // return an estimation of rendered frames when the output is suspended |
| 3459 | size_t latencyFrames = (latency_l() * mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3460 | *dspFrames = (uint32_t) |
| 3461 | (framesWritten >= (int64_t)latencyFrames ? framesWritten - latencyFrames : 0); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3462 | return NO_ERROR; |
| 3463 | } else { |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 3464 | status_t status; |
Mikhail Naganov | 0ea58fe | 2024-05-10 13:30:40 -0700 | [diff] [blame] | 3465 | uint64_t frames = 0; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 3466 | status = mOutput->getRenderPosition(&frames); |
Mikhail Naganov | 0ea58fe | 2024-05-10 13:30:40 -0700 | [diff] [blame] | 3467 | *dspFrames = (uint32_t)frames; |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 3468 | return status; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3469 | } |
| 3470 | } |
| 3471 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3472 | product_strategy_t PlaybackThread::getStrategyForSession_l(audio_session_t sessionId) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3473 | { |
| 3474 | // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that |
| 3475 | // it is moved to correct output by audio policy manager when A2DP is connected or disconnected |
| 3476 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 3477 | return getStrategyForStream(AUDIO_STREAM_MUSIC); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3478 | } |
| 3479 | for (size_t i = 0; i < mTracks.size(); i++) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3480 | sp<IAfTrack> track = mTracks[i]; |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 3481 | if (sessionId == track->sessionId() && !track->isInvalid()) { |
Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 3482 | return getStrategyForStream(track->streamType()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3483 | } |
| 3484 | } |
Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 3485 | return getStrategyForStream(AUDIO_STREAM_MUSIC); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3486 | } |
| 3487 | |
| 3488 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3489 | AudioStreamOut* PlaybackThread::getOutput() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3490 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3491 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3492 | return mOutput; |
| 3493 | } |
| 3494 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3495 | AudioStreamOut* PlaybackThread::clearOutput() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3496 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3497 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3498 | AudioStreamOut *output = mOutput; |
| 3499 | mOutput = NULL; |
| 3500 | // FIXME FastMixer might also have a raw ptr to mOutputSink; |
| 3501 | // must push a NULL and wait for ack |
| 3502 | mOutputSink.clear(); |
| 3503 | mPipeSink.clear(); |
| 3504 | mNormalSink.clear(); |
| 3505 | return output; |
| 3506 | } |
| 3507 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 3508 | // this method must always be called either with ThreadBase mutex() held or inside the thread loop |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3509 | sp<StreamHalInterface> PlaybackThread::stream() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3510 | { |
| 3511 | if (mOutput == NULL) { |
| 3512 | return NULL; |
| 3513 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3514 | return mOutput->stream; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3515 | } |
| 3516 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3517 | uint32_t PlaybackThread::activeSleepTimeUs() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3518 | { |
| 3519 | return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
| 3520 | } |
| 3521 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3522 | status_t PlaybackThread::setSyncEvent(const sp<SyncEvent>& event) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3523 | { |
| 3524 | if (!isValidSyncEvent(event)) { |
| 3525 | return BAD_VALUE; |
| 3526 | } |
| 3527 | |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3528 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3529 | |
| 3530 | for (size_t i = 0; i < mTracks.size(); ++i) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3531 | sp<IAfTrack> track = mTracks[i]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3532 | if (event->triggerSession() == track->sessionId()) { |
| 3533 | (void) track->setSyncEvent(event); |
| 3534 | return NO_ERROR; |
| 3535 | } |
| 3536 | } |
| 3537 | |
| 3538 | return NAME_NOT_FOUND; |
| 3539 | } |
| 3540 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3541 | bool PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3542 | { |
| 3543 | return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE; |
| 3544 | } |
| 3545 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3546 | void PlaybackThread::threadLoop_removeTracks( |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3547 | [[maybe_unused]] const Vector<sp<IAfTrack>>& tracksToRemove) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3548 | { |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 3549 | // Miscellaneous track cleanup when removed from the active list, |
| 3550 | // called without Thread lock but synchronized with threadLoop processing. |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3551 | #ifdef ADD_BATTERY_DATA |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 3552 | for (const auto& track : tracksToRemove) { |
| 3553 | if (track->isExternalTrack()) { |
| 3554 | // to track the speaker usage |
| 3555 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3556 | } |
| 3557 | } |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 3558 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3559 | } |
| 3560 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3561 | void PlaybackThread::checkSilentMode_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3562 | { |
Atneya Nair | 967c85f | 2024-10-27 16:09:50 -0700 | [diff] [blame] | 3563 | if (property_get_bool("ro.audio.silent", false)) { |
| 3564 | ALOGW("ro.audio.silent is now ignored"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3565 | } |
| 3566 | } |
| 3567 | |
| 3568 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3569 | ssize_t PlaybackThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3570 | { |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 3571 | LOG_HIST_TS(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3572 | mInWrite = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3573 | ssize_t bytesWritten; |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 3574 | const size_t offset = mCurrentWriteLength - mBytesRemaining; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3575 | |
| 3576 | // If an NBAIO sink is present, use it to write the normal mixer's submix |
| 3577 | if (mNormalSink != 0) { |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 3578 | |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 3579 | const size_t count = mBytesRemaining / mFrameSize; |
| 3580 | |
Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 3581 | ATRACE_BEGIN("write"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3582 | // update the setpoint when AudioFlinger::mScreenState changes |
Andy Hung | 1d2d2aea | 2023-07-19 16:22:58 -0700 | [diff] [blame] | 3583 | const uint32_t screenState = mAfThreadCallback->getScreenState(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3584 | if (screenState != mScreenState) { |
| 3585 | mScreenState = screenState; |
| 3586 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); |
| 3587 | if (pipe != NULL) { |
| 3588 | pipe->setAvgFrames((mScreenState & 1) ? |
| 3589 | (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); |
| 3590 | } |
| 3591 | } |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 3592 | ssize_t framesWritten = mNormalSink->write((char *)mSinkBuffer + offset, count); |
Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 3593 | ATRACE_END(); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 3594 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3595 | if (framesWritten > 0) { |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 3596 | bytesWritten = framesWritten * mFrameSize; |
Andy Hung | 58e3287 | 2022-11-15 16:12:24 -0800 | [diff] [blame] | 3597 | |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 3598 | #ifdef TEE_SINK |
| 3599 | mTee.write((char *)mSinkBuffer + offset, framesWritten); |
| 3600 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3601 | } else { |
| 3602 | bytesWritten = framesWritten; |
| 3603 | } |
| 3604 | // otherwise use the HAL / AudioStreamOut directly |
| 3605 | } else { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3606 | // Direct output and offload threads |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 3607 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3608 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3609 | ALOGW_IF(mWriteAckSequence & 1, "threadLoop_write(): out of sequence write request"); |
| 3610 | mWriteAckSequence += 2; |
| 3611 | mWriteAckSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3612 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3613 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3614 | } |
Mikhail Naganov | ddb07bc | 2019-08-15 20:18:47 -0700 | [diff] [blame] | 3615 | ATRACE_BEGIN("write"); |
Glenn Kasten | 767094d | 2013-08-23 13:51:43 -0700 | [diff] [blame] | 3616 | // FIXME We should have an implementation of timestamps for direct output threads. |
| 3617 | // They are used e.g for multichannel PCM playback over HDMI. |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 3618 | bytesWritten = mOutput->write((char *)mSinkBuffer + offset, mBytesRemaining); |
Mikhail Naganov | ddb07bc | 2019-08-15 20:18:47 -0700 | [diff] [blame] | 3619 | ATRACE_END(); |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 3620 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3621 | if (mUseAsyncWrite && |
| 3622 | ((bytesWritten < 0) || (bytesWritten == (ssize_t)mBytesRemaining))) { |
| 3623 | // 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] | 3624 | mWriteAckSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3625 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3626 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3627 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3628 | } |
| 3629 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3630 | mNumWrites++; |
| 3631 | mInWrite = false; |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 3632 | if (mStandby) { |
| 3633 | mThreadMetrics.logBeginInterval(); |
Andy Hung | 44d648b | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 3634 | mThreadSnapshot.onBegin(); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 3635 | mStandby = false; |
| 3636 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3637 | return bytesWritten; |
| 3638 | } |
| 3639 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 3640 | // startMelComputation_l() must be called with AudioFlinger::mutex() held |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3641 | void PlaybackThread::startMelComputation_l( |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 3642 | const sp<audio_utils::MelProcessor>& processor) |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 3643 | { |
Vlad Popa | 3c7a266 | 2023-02-14 20:09:47 +0100 | [diff] [blame] | 3644 | auto outputSink = static_cast<AudioStreamOutSink*>(mOutputSink.get()); |
Vlad Popa | 1a0c3ab | 2023-03-17 01:07:01 +0100 | [diff] [blame] | 3645 | if (outputSink != nullptr) { |
| 3646 | outputSink->startMelComputation(processor); |
| 3647 | } |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 3648 | } |
| 3649 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 3650 | // stopMelComputation_l() must be called with AudioFlinger::mutex() held |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3651 | void PlaybackThread::stopMelComputation_l() |
Vlad Popa | 3c7a266 | 2023-02-14 20:09:47 +0100 | [diff] [blame] | 3652 | { |
| 3653 | auto outputSink = static_cast<AudioStreamOutSink*>(mOutputSink.get()); |
Vlad Popa | 1a0c3ab | 2023-03-17 01:07:01 +0100 | [diff] [blame] | 3654 | if (outputSink != nullptr) { |
| 3655 | outputSink->stopMelComputation(); |
| 3656 | } |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 3657 | } |
| 3658 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3659 | void PlaybackThread::threadLoop_drain() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3660 | { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3661 | bool supportsDrain = false; |
| 3662 | if (mOutput->stream->supportsDrain(&supportsDrain) == OK && supportsDrain) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3663 | ALOGV("draining %s", (mMixerStatus == MIXER_DRAIN_TRACK) ? "early" : "full"); |
| 3664 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3665 | ALOGW_IF(mDrainSequence & 1, "threadLoop_drain(): out of sequence drain request"); |
| 3666 | mDrainSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3667 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3668 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3669 | } |
Mikhail Naganov | cbc8f61 | 2016-10-11 18:05:13 -0700 | [diff] [blame] | 3670 | status_t result = mOutput->stream->drain(mMixerStatus == MIXER_DRAIN_TRACK); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3671 | ALOGE_IF(result != OK, "Error when draining stream: %d", result); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3672 | } |
| 3673 | } |
| 3674 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3675 | void PlaybackThread::threadLoop_exit() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3676 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3677 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3678 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3679 | for (size_t i = 0; i < mTracks.size(); i++) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3680 | sp<IAfTrack> track = mTracks[i]; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3681 | track->invalidate(); |
| 3682 | } |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 3683 | // Clear ActiveTracks to update BatteryNotifier in case active tracks remain. |
| 3684 | // After we exit there are no more track changes sent to BatteryNotifier |
| 3685 | // because that requires an active threadLoop. |
| 3686 | // TODO: should we decActiveTrackCnt() of the cleared track effect chain? |
| 3687 | mActiveTracks.clear(); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3688 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3689 | } |
| 3690 | |
| 3691 | /* |
| 3692 | The derived values that are cached: |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3693 | - mSinkBufferSize from frame count * frame size |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3694 | - mActiveSleepTimeUs from activeSleepTimeUs() |
| 3695 | - mIdleSleepTimeUs from idleSleepTimeUs() |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 3696 | - mStandbyDelayNs from mActiveSleepTimeUs (DIRECT only) or forced to at least |
| 3697 | kDefaultStandbyTimeInNsecs when connected to an A2DP device. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3698 | - maxPeriod from frame count and sample rate (MIXER only) |
| 3699 | |
| 3700 | The parameters that affect these derived values are: |
| 3701 | - frame count |
| 3702 | - frame size |
| 3703 | - sample rate |
| 3704 | - device type: A2DP or not |
| 3705 | - device latency |
| 3706 | - format: PCM or not |
| 3707 | - active sleep time |
| 3708 | - idle sleep time |
| 3709 | */ |
| 3710 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3711 | void PlaybackThread::cacheParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3712 | { |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3713 | mSinkBufferSize = mNormalFrameCount * mFrameSize; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3714 | mActiveSleepTimeUs = activeSleepTimeUs(); |
| 3715 | mIdleSleepTimeUs = idleSleepTimeUs(); |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 3716 | |
Andy Hung | 8fe87eb | 2023-07-20 21:31:38 -0700 | [diff] [blame] | 3717 | mStandbyDelayNs = getStandbyTimeInNanos(); |
Carter Hsu | 0ca47c2 | 2023-06-02 18:01:45 +0800 | [diff] [blame] | 3718 | |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 3719 | // make sure standby delay is not too short when connected to an A2DP sink to avoid |
| 3720 | // truncating audio when going to standby. |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 3721 | if (!Intersection(outDeviceTypes_l(), getAudioDeviceOutAllA2dpSet()).empty()) { |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 3722 | if (mStandbyDelayNs < kDefaultStandbyTimeInNsecs) { |
| 3723 | mStandbyDelayNs = kDefaultStandbyTimeInNsecs; |
| 3724 | } |
| 3725 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3726 | } |
| 3727 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3728 | bool PlaybackThread::invalidateTracks_l(audio_stream_type_t streamType) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3729 | { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3730 | ALOGV("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %zu", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3731 | this, streamType, mTracks.size()); |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 3732 | bool trackMatch = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3733 | size_t size = mTracks.size(); |
| 3734 | for (size_t i = 0; i < size; i++) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3735 | sp<IAfTrack> t = mTracks[i]; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3736 | if (t->streamType() == streamType && t->isExternalTrack()) { |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 3737 | t->invalidate(); |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 3738 | trackMatch = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3739 | } |
| 3740 | } |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 3741 | return trackMatch; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3742 | } |
| 3743 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3744 | void PlaybackThread::invalidateTracks(audio_stream_type_t streamType) |
Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 3745 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3746 | audio_utils::lock_guard _l(mutex()); |
Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 3747 | invalidateTracks_l(streamType); |
| 3748 | } |
| 3749 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3750 | void PlaybackThread::invalidateTracks(std::set<audio_port_handle_t>& portIds) { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3751 | audio_utils::lock_guard _l(mutex()); |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 3752 | invalidateTracks_l(portIds); |
| 3753 | } |
| 3754 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3755 | bool PlaybackThread::invalidateTracks_l(std::set<audio_port_handle_t>& portIds) { |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 3756 | bool trackMatch = false; |
| 3757 | const size_t size = mTracks.size(); |
| 3758 | for (size_t i = 0; i < size; i++) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3759 | sp<IAfTrack> t = mTracks[i]; |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 3760 | if (t->isExternalTrack() && portIds.find(t->portId()) != portIds.end()) { |
| 3761 | t->invalidate(); |
| 3762 | portIds.erase(t->portId()); |
| 3763 | trackMatch = true; |
| 3764 | } |
| 3765 | if (portIds.empty()) { |
| 3766 | break; |
| 3767 | } |
| 3768 | } |
| 3769 | return trackMatch; |
| 3770 | } |
| 3771 | |
jiabin | f042b9b | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 3772 | // getTrackById_l must be called with holding thread lock |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3773 | IAfTrack* PlaybackThread::getTrackById_l( |
jiabin | f042b9b | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 3774 | audio_port_handle_t trackPortId) { |
| 3775 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 3776 | if (mTracks[i]->portId() == trackPortId) { |
| 3777 | return mTracks[i].get(); |
| 3778 | } |
| 3779 | } |
| 3780 | return nullptr; |
| 3781 | } |
| 3782 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3783 | status_t PlaybackThread::addEffectChain_l(const sp<IAfEffectChain>& chain) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3784 | { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3785 | audio_session_t session = chain->sessionId(); |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 3786 | sp<EffectBufferHalInterface> halInBuffer, halOutBuffer; |
Andy Hung | 319587b | 2023-05-23 14:01:03 -0700 | [diff] [blame] | 3787 | float *buffer = nullptr; // only used for non global sessions |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3788 | |
Andy Hung | d363992 | 2022-04-28 18:00:49 -0700 | [diff] [blame] | 3789 | if (mType == SPATIALIZER) { |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3790 | if (!audio_is_global_session(session)) { |
| 3791 | // player sessions on a spatializer output will use a dedicated input buffer and |
| 3792 | // will either output multi channel to mEffectBuffer if the track is spatilaized |
| 3793 | // or stereo to mPostSpatializerBuffer if not spatialized. |
| 3794 | uint32_t channelMask; |
| 3795 | bool isSessionSpatialized = |
| 3796 | (hasAudioSession_l(session) & ThreadBase::SPATIALIZED_SESSION) != 0; |
| 3797 | if (isSessionSpatialized) { |
| 3798 | channelMask = mMixerChannelMask; |
| 3799 | } else { |
| 3800 | channelMask = mChannelMask; |
| 3801 | } |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 3802 | size_t numSamples = mNormalFrameCount |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3803 | * (audio_channel_count_from_out_mask(channelMask) + mHapticChannelCount); |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 3804 | status_t result = mAfThreadCallback->getEffectsFactoryHal()->allocateBuffer( |
Andy Hung | 319587b | 2023-05-23 14:01:03 -0700 | [diff] [blame] | 3805 | numSamples * sizeof(float), |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 3806 | &halInBuffer); |
| 3807 | if (result != OK) return result; |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3808 | |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 3809 | result = mAfThreadCallback->getEffectsFactoryHal()->mirrorBuffer( |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3810 | isSessionSpatialized ? mEffectBuffer : mPostSpatializerBuffer, |
| 3811 | isSessionSpatialized ? mEffectBufferSize : mPostSpatializerBufferSize, |
| 3812 | &halOutBuffer); |
| 3813 | if (result != OK) return result; |
| 3814 | |
Shunkai Yao | 44bdbad | 2023-01-14 05:11:58 +0000 | [diff] [blame] | 3815 | buffer = halInBuffer ? halInBuffer->audioBuffer()->f32 : buffer; |
Andy Hung | 2683692 | 2023-05-22 17:31:57 -0700 | [diff] [blame] | 3816 | |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 3817 | ALOGV("addEffectChain_l() creating new input buffer %p session %d", |
| 3818 | buffer, session); |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3819 | } else { |
Shunkai Yao | 2dcd60c | 2024-08-27 21:08:53 +0000 | [diff] [blame] | 3820 | status_t result = INVALID_OPERATION; |
| 3821 | // Buffer configuration for global sessions on a SPATIALIZER thread: |
| 3822 | // - AUDIO_SESSION_OUTPUT_MIX session uses the mEffectBuffer as input and output buffer |
| 3823 | // - AUDIO_SESSION_OUTPUT_STAGE session uses the mEffectBuffer as input buffer and |
| 3824 | // mPostSpatializerBuffer as output buffer |
| 3825 | // - AUDIO_SESSION_DEVICE session uses the mPostSpatializerBuffer as input and output |
| 3826 | // buffer |
| 3827 | if (session == AUDIO_SESSION_OUTPUT_MIX || session == AUDIO_SESSION_OUTPUT_STAGE) { |
| 3828 | result = mAfThreadCallback->getEffectsFactoryHal()->mirrorBuffer( |
| 3829 | mEffectBuffer, mEffectBufferSize, &halInBuffer); |
| 3830 | if (result != OK) return result; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3831 | |
Shunkai Yao | 2dcd60c | 2024-08-27 21:08:53 +0000 | [diff] [blame] | 3832 | if (session == AUDIO_SESSION_OUTPUT_MIX) { |
| 3833 | halOutBuffer = halInBuffer; |
| 3834 | } |
| 3835 | } |
| 3836 | |
| 3837 | if (session == AUDIO_SESSION_OUTPUT_STAGE || session == AUDIO_SESSION_DEVICE) { |
| 3838 | result = mAfThreadCallback->getEffectsFactoryHal()->mirrorBuffer( |
| 3839 | mPostSpatializerBuffer, mPostSpatializerBufferSize, &halOutBuffer); |
| 3840 | if (result != OK) return result; |
| 3841 | |
| 3842 | if (session == AUDIO_SESSION_DEVICE) { |
| 3843 | halInBuffer = halOutBuffer; |
| 3844 | } |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3845 | } |
| 3846 | } |
| 3847 | } else { |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 3848 | status_t result = mAfThreadCallback->getEffectsFactoryHal()->mirrorBuffer( |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3849 | mEffectBufferEnabled ? mEffectBuffer : mSinkBuffer, |
| 3850 | mEffectBufferEnabled ? mEffectBufferSize : mSinkBufferSize, |
| 3851 | &halInBuffer); |
| 3852 | if (result != OK) return result; |
| 3853 | halOutBuffer = halInBuffer; |
| 3854 | ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); |
| 3855 | if (!audio_is_global_session(session)) { |
Andy Hung | 319587b | 2023-05-23 14:01:03 -0700 | [diff] [blame] | 3856 | buffer = halInBuffer ? reinterpret_cast<float*>(halInBuffer->externalData()) |
Shunkai Yao | 44bdbad | 2023-01-14 05:11:58 +0000 | [diff] [blame] | 3857 | : buffer; |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3858 | // Only one effect chain can be present in direct output thread and it uses |
| 3859 | // the sink buffer as input |
| 3860 | if (mType != DIRECT) { |
| 3861 | size_t numSamples = mNormalFrameCount |
| 3862 | * (audio_channel_count_from_out_mask(mMixerChannelMask) |
| 3863 | + mHapticChannelCount); |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 3864 | const status_t allocateStatus = |
| 3865 | mAfThreadCallback->getEffectsFactoryHal()->allocateBuffer( |
Andy Hung | 319587b | 2023-05-23 14:01:03 -0700 | [diff] [blame] | 3866 | numSamples * sizeof(float), |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3867 | &halInBuffer); |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 3868 | if (allocateStatus != OK) return allocateStatus; |
Andy Hung | 2683692 | 2023-05-22 17:31:57 -0700 | [diff] [blame] | 3869 | |
Shunkai Yao | 44bdbad | 2023-01-14 05:11:58 +0000 | [diff] [blame] | 3870 | buffer = halInBuffer ? halInBuffer->audioBuffer()->f32 : buffer; |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3871 | ALOGV("addEffectChain_l() creating new input buffer %p session %d", |
| 3872 | buffer, session); |
| 3873 | } |
| 3874 | } |
| 3875 | } |
| 3876 | |
| 3877 | if (!audio_is_global_session(session)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3878 | // Attach all tracks with same session ID to this chain. |
| 3879 | for (size_t i = 0; i < mTracks.size(); ++i) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3880 | sp<IAfTrack> track = mTracks[i]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3881 | if (session == track->sessionId()) { |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3882 | ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", |
| 3883 | track.get(), buffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3884 | track->setMainBuffer(buffer); |
| 3885 | chain->incTrackCnt(); |
| 3886 | } |
| 3887 | } |
| 3888 | |
| 3889 | // indicate all active tracks in the chain |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3890 | for (const sp<IAfTrack>& track : mActiveTracks) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3891 | if (session == track->sessionId()) { |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3892 | ALOGV("addEffectChain_l() activating track %p on session %d", |
| 3893 | track.get(), session); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3894 | chain->incActiveTrackCnt(); |
| 3895 | } |
| 3896 | } |
| 3897 | } |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3898 | |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 3899 | chain->setThread(this); |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 3900 | chain->setInBuffer(halInBuffer); |
| 3901 | chain->setOutBuffer(halOutBuffer); |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 3902 | // Effect chain for session AUDIO_SESSION_DEVICE is inserted at end of effect |
| 3903 | // chains list in order to be processed last as it contains output device effects. |
| 3904 | // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted just before to apply post |
| 3905 | // processing effects specific to an output stream before effects applied to all streams |
| 3906 | // routed to a given device. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3907 | // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before |
| 3908 | // session AUDIO_SESSION_OUTPUT_STAGE to be processed |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3909 | // after track specific effects and before output stage. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3910 | // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3911 | // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3912 | // Effect chain for other sessions are inserted at beginning of effect |
| 3913 | // 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] | 3914 | // sessions is not important. |
| 3915 | static_assert(AUDIO_SESSION_OUTPUT_MIX == 0 && |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 3916 | AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX && |
| 3917 | AUDIO_SESSION_DEVICE < AUDIO_SESSION_OUTPUT_STAGE, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3918 | "audio_session_t constants misdefined"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3919 | size_t size = mEffectChains.size(); |
| 3920 | size_t i = 0; |
| 3921 | for (i = 0; i < size; i++) { |
| 3922 | if (mEffectChains[i]->sessionId() < session) { |
| 3923 | break; |
| 3924 | } |
| 3925 | } |
| 3926 | mEffectChains.insertAt(chain, i); |
| 3927 | checkSuspendOnAddEffectChain_l(chain); |
| 3928 | |
| 3929 | return NO_ERROR; |
| 3930 | } |
| 3931 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3932 | size_t PlaybackThread::removeEffectChain_l(const sp<IAfEffectChain>& chain) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3933 | { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3934 | audio_session_t session = chain->sessionId(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3935 | |
| 3936 | ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); |
| 3937 | |
| 3938 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 3939 | if (chain == mEffectChains[i]) { |
| 3940 | mEffectChains.removeAt(i); |
| 3941 | // detach all active tracks from the chain |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3942 | for (const sp<IAfTrack>& track : mActiveTracks) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3943 | if (session == track->sessionId()) { |
| 3944 | ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", |
| 3945 | chain.get(), session); |
| 3946 | chain->decActiveTrackCnt(); |
| 3947 | } |
| 3948 | } |
| 3949 | |
| 3950 | // detach all tracks with same session ID from this chain |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 3951 | for (size_t j = 0; j < mTracks.size(); ++j) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3952 | sp<IAfTrack> track = mTracks[j]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3953 | if (session == track->sessionId()) { |
Andy Hung | 319587b | 2023-05-23 14:01:03 -0700 | [diff] [blame] | 3954 | track->setMainBuffer(reinterpret_cast<float*>(mSinkBuffer)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3955 | chain->decTrackCnt(); |
| 3956 | } |
| 3957 | } |
| 3958 | break; |
| 3959 | } |
| 3960 | } |
| 3961 | return mEffectChains.size(); |
| 3962 | } |
| 3963 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3964 | status_t PlaybackThread::attachAuxEffect( |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3965 | const sp<IAfTrack>& track, int EffectId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3966 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3967 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3968 | return attachAuxEffect_l(track, EffectId); |
| 3969 | } |
| 3970 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3971 | status_t PlaybackThread::attachAuxEffect_l( |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3972 | const sp<IAfTrack>& track, int EffectId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3973 | { |
| 3974 | status_t status = NO_ERROR; |
| 3975 | |
| 3976 | if (EffectId == 0) { |
| 3977 | track->setAuxBuffer(0, NULL); |
| 3978 | } else { |
| 3979 | // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 3980 | sp<IAfEffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3981 | if (effect != 0) { |
| 3982 | if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 3983 | track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); |
| 3984 | } else { |
| 3985 | status = INVALID_OPERATION; |
| 3986 | } |
| 3987 | } else { |
| 3988 | status = BAD_VALUE; |
| 3989 | } |
| 3990 | } |
| 3991 | return status; |
| 3992 | } |
| 3993 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3994 | void PlaybackThread::detachAuxEffect_l(int effectId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3995 | { |
| 3996 | for (size_t i = 0; i < mTracks.size(); ++i) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3997 | sp<IAfTrack> track = mTracks[i]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3998 | if (track->auxEffectId() == effectId) { |
| 3999 | attachAuxEffect_l(track, 0); |
| 4000 | } |
| 4001 | } |
| 4002 | } |
| 4003 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 4004 | bool PlaybackThread::threadLoop() |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 4005 | NO_THREAD_SAFETY_ANALYSIS // manual locking of AudioFlinger |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4006 | { |
Andy Hung | 78d8d95 | 2023-05-30 18:10:23 -0700 | [diff] [blame] | 4007 | aflog::setThreadWriter(mNBLogWriter.get()); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 4008 | |
Andy Hung | 077d62e | 2023-10-03 10:49:34 -0700 | [diff] [blame] | 4009 | if (mType == SPATIALIZER) { |
| 4010 | const pid_t tid = getTid(); |
| 4011 | if (tid == -1) { // odd: we are here, we must be a running thread. |
| 4012 | ALOGW("%s: Cannot update Spatializer mixer thread priority, no tid", __func__); |
| 4013 | } else { |
Andy Hung | 639dbc9 | 2023-11-28 18:21:55 +0000 | [diff] [blame] | 4014 | const int priorityBoost = requestSpatializerPriority(getpid(), tid); |
| 4015 | if (priorityBoost > 0) { |
| 4016 | stream()->setHalThreadPriority(priorityBoost); |
| 4017 | } |
Andy Hung | 077d62e | 2023-10-03 10:49:34 -0700 | [diff] [blame] | 4018 | } |
Pattara Teerapong | 9a332c5 | 2024-01-26 08:18:05 +0000 | [diff] [blame] | 4019 | } else if (property_get_bool("ro.boot.container", false /* default_value */)) { |
| 4020 | // In ARC experiments (b/73091832), the latency under using CFS scheduler with any priority |
| 4021 | // is not enough for PlaybackThread to process audio data in time. We request the lowest |
| 4022 | // real-time priority, SCHED_FIFO=1, for PlaybackThread in ARC. ro.boot.container is true |
| 4023 | // only on ARC. |
| 4024 | const pid_t tid = getTid(); |
| 4025 | if (tid == -1) { |
| 4026 | ALOGW("%s: Cannot update PlaybackThread priority for ARC, no tid", __func__); |
| 4027 | } else { |
| 4028 | const status_t status = requestPriority(getpid(), |
| 4029 | tid, |
| 4030 | kPriorityPlaybackThreadArc, |
| 4031 | false /* isForApp */, |
| 4032 | true /* asynchronous */); |
| 4033 | if (status != OK) { |
| 4034 | ALOGW("%s: Cannot update PlaybackThread priority for ARC, status %d", __func__, |
| 4035 | status); |
| 4036 | } else { |
| 4037 | stream()->setHalThreadPriority(kPriorityPlaybackThreadArc); |
| 4038 | } |
| 4039 | } |
Andy Hung | 077d62e | 2023-10-03 10:49:34 -0700 | [diff] [blame] | 4040 | } |
| 4041 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 4042 | Vector<sp<IAfTrack>> tracksToRemove; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4043 | |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4044 | mStandbyTimeNs = systemTime(); |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4045 | int64_t lastLoopCountWritten = -2; // never matches "previous" loop, when loopCount = 0. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4046 | |
| 4047 | // MIXER |
| 4048 | nsecs_t lastWarning = 0; |
| 4049 | |
| 4050 | // DUPLICATING |
| 4051 | // FIXME could this be made local to while loop? |
| 4052 | writeFrames = 0; |
| 4053 | |
Andy Hung | 3f2cee6 | 2024-09-17 14:17:15 -0700 | [diff] [blame] | 4054 | { |
| 4055 | audio_utils::lock_guard l(mutex()); |
| 4056 | |
| 4057 | cacheParameters_l(); |
| 4058 | checkSilentMode_l(); |
| 4059 | } |
| 4060 | |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4061 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4062 | |
Andy Hung | d363992 | 2022-04-28 18:00:49 -0700 | [diff] [blame] | 4063 | if (mType == MIXER || mType == SPATIALIZER) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4064 | sleepTimeShift = 0; |
| 4065 | } |
| 4066 | |
| 4067 | CpuStats cpuStats; |
| 4068 | const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); |
| 4069 | |
| 4070 | acquireWakeLock(); |
| 4071 | |
Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 4072 | // mNBLogWriter logging APIs can only be called by a single thread, typically the |
| 4073 | // thread associated with this PlaybackThread. |
| 4074 | // If you want to share the mNBLogWriter with other threads (for example, binder threads) |
| 4075 | // 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] | 4076 | // So if you need to log when mutex is unlocked, set logString to a non-NULL string, |
| 4077 | // and then that string will be logged at the next convenient opportunity. |
Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 4078 | // See reference to logString below. |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 4079 | const char *logString = NULL; |
| 4080 | |
rago | 1bb9082 | 2017-05-02 18:31:48 -0700 | [diff] [blame] | 4081 | // Estimated time for next buffer to be written to hal. This is used only on |
| 4082 | // suspended mode (for now) to help schedule the wait time until next iteration. |
| 4083 | nsecs_t timeLoopNextNs = 0; |
| 4084 | |
Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4085 | audio_patch_handle_t lastDownstreamPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Andy Hung | f323451 | 2018-07-03 14:51:47 -0700 | [diff] [blame] | 4086 | |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 4087 | sendCheckOutputStageEffectsEvent(); |
| 4088 | |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4089 | // loopCount is used for statistics and diagnostics. |
| 4090 | for (int64_t loopCount = 0; !exitPending(); ++loopCount) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4091 | { |
Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 4092 | // Log merge requests are performed during AudioFlinger binder transactions, but |
| 4093 | // that does not cover audio playback. It's requested here for that reason. |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 4094 | mAfThreadCallback->requestLogMerge(); |
Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 4095 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4096 | cpuStats.sample(myName); |
| 4097 | |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 4098 | Vector<sp<IAfEffectChain>> effectChains; |
Andy Hung | 6e6a2e6 | 2019-04-30 16:38:41 -0700 | [diff] [blame] | 4099 | audio_session_t activeHapticSessionId = AUDIO_SESSION_NONE; |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4100 | bool isHapticSessionSpatialized = false; |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 4101 | std::vector<sp<IAfTrack>> activeTracks; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4102 | |
Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4103 | // If the device is AUDIO_DEVICE_OUT_BUS, check for downstream latency. |
| 4104 | // |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4105 | // Note: we access outDeviceTypes() outside of mutex(). |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 4106 | if (isMsdDevice() && outDeviceTypes_l().count(AUDIO_DEVICE_OUT_BUS) != 0) { |
Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4107 | // Here, we try for the AF lock, but do not block on it as the latency |
| 4108 | // is more informational. |
Andy Hung | 954b971 | 2023-08-28 18:36:53 -0700 | [diff] [blame] | 4109 | if (mAfThreadCallback->mutex().try_lock()) { |
Andy Hung | b6692eb | 2023-07-13 16:52:46 -0700 | [diff] [blame] | 4110 | std::vector<SoftwarePatch> swPatches; |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 4111 | double latencyMs = 0.; // not required; initialized for clang-tidy |
Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4112 | status_t status = INVALID_OPERATION; |
| 4113 | audio_patch_handle_t downstreamPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 4114 | if (mAfThreadCallback->getPatchPanel()->getDownstreamSoftwarePatches( |
Andy Hung | b6692eb | 2023-07-13 16:52:46 -0700 | [diff] [blame] | 4115 | id(), &swPatches) == OK |
Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4116 | && swPatches.size() > 0) { |
| 4117 | status = swPatches[0].getLatencyMs_l(&latencyMs); |
| 4118 | downstreamPatchHandle = swPatches[0].getPatchHandle(); |
| 4119 | } |
| 4120 | if (downstreamPatchHandle != lastDownstreamPatchHandle) { |
Dean Wheatley | 30d2842 | 2018-11-06 10:27:40 +1100 | [diff] [blame] | 4121 | mDownstreamLatencyStatMs.reset(); |
Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4122 | lastDownstreamPatchHandle = downstreamPatchHandle; |
| 4123 | } |
| 4124 | if (status == OK) { |
| 4125 | // verify downstream latency (we assume a max reasonable |
Mikhail Naganov | 6aa0a31 | 2018-11-09 11:00:01 -0800 | [diff] [blame] | 4126 | // latency of 5 seconds). |
| 4127 | const double minLatency = 0., maxLatency = 5000.; |
| 4128 | if (latencyMs >= minLatency && latencyMs <= maxLatency) { |
Dean Wheatley | 56a583e | 2020-05-08 15:12:17 +1000 | [diff] [blame] | 4129 | ALOGVV("new downstream latency %lf ms", latencyMs); |
Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4130 | } else { |
| 4131 | ALOGD("out of range downstream latency %lf ms", latencyMs); |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 4132 | latencyMs = std::clamp(latencyMs, minLatency, maxLatency); |
Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4133 | } |
Dean Wheatley | 30d2842 | 2018-11-06 10:27:40 +1100 | [diff] [blame] | 4134 | mDownstreamLatencyStatMs.add(latencyMs); |
Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4135 | } |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 4136 | mAfThreadCallback->mutex().unlock(); |
Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4137 | } |
| 4138 | } else { |
| 4139 | if (lastDownstreamPatchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 4140 | // 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] | 4141 | mDownstreamLatencyStatMs.reset(); |
Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4142 | lastDownstreamPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 4143 | } |
| 4144 | } |
| 4145 | |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 4146 | if (mCheckOutputStageEffects.exchange(false)) { |
| 4147 | checkOutputStageEffects(); |
| 4148 | } |
| 4149 | |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 4150 | MetadataUpdate metadataUpdate; |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4151 | { // scope for mutex() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4152 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4153 | audio_utils::unique_lock _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4154 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 4155 | processConfigEvents_l(); |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 4156 | if (mCheckOutputStageEffects.load()) { |
| 4157 | continue; |
| 4158 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4159 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4160 | // 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] | 4161 | if (logString != NULL) { |
| 4162 | mNBLogWriter->logTimestamp(); |
| 4163 | mNBLogWriter->log(logString); |
| 4164 | logString = NULL; |
| 4165 | } |
| 4166 | |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 4167 | collectTimestamps_l(); |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 4168 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4169 | saveOutputTracks(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4170 | if (mSignalPending) { |
| 4171 | // A signal was raised while we were unlocked |
| 4172 | mSignalPending = false; |
| 4173 | } else if (waitingAsyncCallback_l()) { |
| 4174 | if (exitPending()) { |
| 4175 | break; |
| 4176 | } |
Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 4177 | bool released = false; |
Eric Laurent | 6466797 | 2016-03-30 18:19:46 -0700 | [diff] [blame] | 4178 | if (!keepWakeLock()) { |
Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 4179 | releaseWakeLock_l(); |
| 4180 | released = true; |
| 4181 | } |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 4182 | |
| 4183 | const int64_t waitNs = computeWaitTimeNs_l(); |
| 4184 | ALOGV("wait async completion (wait time: %lld)", (long long)waitNs); |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4185 | std::cv_status cvstatus = |
| 4186 | mWaitWorkCV.wait_for(_l, std::chrono::nanoseconds(waitNs)); |
| 4187 | if (cvstatus == std::cv_status::timeout) { |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 4188 | mSignalPending = true; // if timeout recheck everything |
| 4189 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4190 | ALOGV("async completion/wake"); |
Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 4191 | if (released) { |
| 4192 | acquireWakeLock_l(); |
| 4193 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4194 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
| 4195 | mSleepTimeUs = 0; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 4196 | |
| 4197 | continue; |
| 4198 | } |
Eric Tan | 39ec8d6 | 2018-07-24 09:49:29 -0700 | [diff] [blame] | 4199 | if ((mActiveTracks.isEmpty() && systemTime() > mStandbyTimeNs) || |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4200 | isSuspended()) { |
| 4201 | // put audio hardware into standby after short delay |
| 4202 | if (shouldStandby_l()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4203 | |
| 4204 | threadLoop_standby(); |
| 4205 | |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 4206 | // This is where we go into standby |
| 4207 | if (!mStandby) { |
| 4208 | LOG_AUDIO_STATE(); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 4209 | mThreadMetrics.logEndInterval(); |
Andy Hung | 44d648b | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 4210 | mThreadSnapshot.onEnd(); |
Eric Laurent | 19952e1 | 2023-04-20 10:08:29 +0200 | [diff] [blame] | 4211 | setStandby_l(); |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 4212 | } |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 4213 | sendStatistics(false /* force */); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4214 | } |
| 4215 | |
Eric Tan | 39ec8d6 | 2018-07-24 09:49:29 -0700 | [diff] [blame] | 4216 | if (mActiveTracks.isEmpty() && mConfigEvents.isEmpty()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4217 | // we're about to wait, flush the binder command buffer |
| 4218 | IPCThreadState::self()->flushCommands(); |
| 4219 | |
| 4220 | clearOutputTracks(); |
| 4221 | |
| 4222 | if (exitPending()) { |
| 4223 | break; |
| 4224 | } |
| 4225 | |
| 4226 | releaseWakeLock_l(); |
| 4227 | // wait until we have something to do... |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 4228 | ALOGV("%s going to sleep", myName.c_str()); |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4229 | mWaitWorkCV.wait(_l); |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 4230 | ALOGV("%s waking up", myName.c_str()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4231 | acquireWakeLock_l(); |
| 4232 | |
| 4233 | mMixerStatus = MIXER_IDLE; |
| 4234 | mMixerStatusIgnoringFastTracks = MIXER_IDLE; |
| 4235 | mBytesWritten = 0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4236 | mBytesRemaining = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4237 | checkSilentMode_l(); |
| 4238 | |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4239 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
| 4240 | mSleepTimeUs = mIdleSleepTimeUs; |
Andy Hung | d363992 | 2022-04-28 18:00:49 -0700 | [diff] [blame] | 4241 | if (mType == MIXER || mType == SPATIALIZER) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4242 | sleepTimeShift = 0; |
| 4243 | } |
| 4244 | |
| 4245 | continue; |
| 4246 | } |
| 4247 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4248 | // mMixerStatusIgnoringFastTracks is also updated internally |
| 4249 | mMixerStatus = prepareTracks_l(&tracksToRemove); |
| 4250 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 4251 | mActiveTracks.updatePowerState_l(this); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 4252 | |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 4253 | metadataUpdate = updateMetadata_l(); |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 4254 | |
Andy Hung | f302e81 | 2024-01-26 11:55:15 -0800 | [diff] [blame] | 4255 | // Acquire a local copy of active tracks with lock (release w/o lock). |
| 4256 | // |
| 4257 | // Control methods on the track acquire the ThreadBase lock (e.g. start() |
| 4258 | // stop(), pause(), etc.), but the threadLoop is entitled to call audio |
| 4259 | // data / buffer methods on tracks from activeTracks without the ThreadBase lock. |
| 4260 | activeTracks.insert(activeTracks.end(), mActiveTracks.begin(), mActiveTracks.end()); |
| 4261 | |
| 4262 | setHalLatencyMode_l(); |
| 4263 | |
| 4264 | // updateTeePatches_l will acquire the ThreadBase_Mutex of other threads, |
| 4265 | // so this is done before we lock our effect chains. |
| 4266 | for (const auto& track : mActiveTracks) { |
| 4267 | track->updateTeePatches_l(); |
| 4268 | } |
| 4269 | |
| 4270 | // signal actual start of output stream when the render position reported by |
| 4271 | // the kernel starts moving. |
| 4272 | if (!mHalStarted && ((isSuspended() && (mBytesWritten != 0)) || (!mStandby |
| 4273 | && (mKernelPositionOnStandby |
| 4274 | != mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL])))) { |
| 4275 | mHalStarted = true; |
| 4276 | mWaitHalStartCV.notify_all(); |
| 4277 | } |
| 4278 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4279 | // prevent any changes in effect chain list and in each effect chain |
| 4280 | // during mixing and effect process as the audio buffers could be deleted |
| 4281 | // or modified if an effect is created or deleted |
| 4282 | lockEffectChains_l(effectChains); |
Andy Hung | 6e6a2e6 | 2019-04-30 16:38:41 -0700 | [diff] [blame] | 4283 | |
| 4284 | // Determine which session to pick up haptic data. |
| 4285 | // This must be done under the same lock as prepareTracks_l(). |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 4286 | // 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] | 4287 | // TODO: Write haptic data directly to sink buffer when mixing. |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4288 | if (mHapticChannelCount > 0) { |
Andy Hung | 6e6a2e6 | 2019-04-30 16:38:41 -0700 | [diff] [blame] | 4289 | for (const auto& track : mActiveTracks) { |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 4290 | sp<IAfEffectChain> effectChain = getEffectChain_l(track->sessionId()); |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4291 | if (effectChain != nullptr |
| 4292 | && effectChain->containsHapticGeneratingEffect_l()) { |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 4293 | activeHapticSessionId = track->sessionId(); |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4294 | isHapticSessionSpatialized = |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 4295 | mType == SPATIALIZER && track->isSpatialized(); |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 4296 | break; |
| 4297 | } |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4298 | if (activeHapticSessionId == AUDIO_SESSION_NONE |
| 4299 | && track->getHapticPlaybackEnabled()) { |
Andy Hung | 6e6a2e6 | 2019-04-30 16:38:41 -0700 | [diff] [blame] | 4300 | activeHapticSessionId = track->sessionId(); |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4301 | isHapticSessionSpatialized = |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 4302 | mType == SPATIALIZER && track->isSpatialized(); |
Andy Hung | 6e6a2e6 | 2019-04-30 16:38:41 -0700 | [diff] [blame] | 4303 | } |
| 4304 | } |
| 4305 | } |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4306 | } // mutex() scope ends |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4307 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4308 | if (mBytesRemaining == 0) { |
| 4309 | mCurrentWriteLength = 0; |
| 4310 | if (mMixerStatus == MIXER_TRACKS_READY) { |
| 4311 | // threadLoop_mix() sets mCurrentWriteLength |
| 4312 | threadLoop_mix(); |
| 4313 | } else if ((mMixerStatus != MIXER_DRAIN_TRACK) |
| 4314 | && (mMixerStatus != MIXER_DRAIN_ALL)) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4315 | // threadLoop_sleepTime sets mSleepTimeUs to 0 if data |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4316 | // must be written to HAL |
| 4317 | threadLoop_sleepTime(); |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4318 | if (mSleepTimeUs == 0) { |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 4319 | mCurrentWriteLength = mSinkBufferSize; |
Andy Hung | c164638 | 2019-04-30 16:12:10 -0700 | [diff] [blame] | 4320 | |
| 4321 | // Tally underrun frames as we are inserting 0s here. |
| 4322 | for (const auto& track : activeTracks) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 4323 | if (track->fillingStatus() == IAfTrack::FS_ACTIVE |
Andy Hung | e2e830f | 2019-12-03 12:54:46 -0800 | [diff] [blame] | 4324 | && !track->isStopped() |
| 4325 | && !track->isPaused() |
| 4326 | && !track->isTerminated()) { |
| 4327 | ALOGV("%s: track(%d) %s underrun due to thread sleep of %zu frames", |
| 4328 | __func__, track->id(), track->getTrackStateAsString(), |
| 4329 | mNormalFrameCount); |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 4330 | track->audioTrackServerProxy()->tallyUnderrunFrames( |
| 4331 | mNormalFrameCount); |
Andy Hung | c164638 | 2019-04-30 16:12:10 -0700 | [diff] [blame] | 4332 | } |
| 4333 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4334 | } |
| 4335 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4336 | // Either threadLoop_mix() or threadLoop_sleepTime() should have set |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4337 | // mMixerBuffer with data if mMixerBufferValid is true and mSleepTimeUs == 0. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4338 | // Merge mMixerBuffer data into mEffectBuffer (if any effects are valid) |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 4339 | // or mSinkBuffer (if there are no effects and there is no data already copied to |
| 4340 | // mSinkBuffer). |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4341 | // |
| 4342 | // This is done pre-effects computation; if effects change to |
| 4343 | // support higher precision, this needs to move. |
| 4344 | // |
| 4345 | // mMixerBufferValid is only set true by MixerThread::prepareTracks_l(). |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4346 | // TODO use mSleepTimeUs == 0 as an additional condition. |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 4347 | uint32_t mixerChannelCount = mEffectBufferValid ? |
| 4348 | audio_channel_count_from_out_mask(mMixerChannelMask) : mChannelCount; |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 4349 | if (mMixerBufferValid && (mEffectBufferValid || !mHasDataCopiedToSinkBuffer)) { |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4350 | void *buffer = mEffectBufferValid ? mEffectBuffer : mSinkBuffer; |
| 4351 | audio_format_t format = mEffectBufferValid ? mEffectBufferFormat : mFormat; |
| 4352 | |
David Li | 88ee090 | 2022-06-22 10:01:21 +0800 | [diff] [blame] | 4353 | // Apply mono blending and balancing if the effect buffer is not valid. Otherwise, |
| 4354 | // do these processes after effects are applied. |
| 4355 | if (!mEffectBufferValid) { |
| 4356 | // mono blend occurs for mixer threads only (not direct or offloaded) |
| 4357 | // and is handled here if we're going directly to the sink. |
| 4358 | if (requireMonoBlend()) { |
| 4359 | mono_blend(mMixerBuffer, mMixerBufferFormat, mChannelCount, |
| 4360 | mNormalFrameCount, true /*limit*/); |
| 4361 | } |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4362 | |
David Li | 88ee090 | 2022-06-22 10:01:21 +0800 | [diff] [blame] | 4363 | if (!hasFastMixer()) { |
| 4364 | // Balance must take effect after mono conversion. |
| 4365 | // We do it here if there is no FastMixer. |
| 4366 | // mBalance detects zero balance within the class for speed |
| 4367 | // (not needed here). |
| 4368 | mBalance.setBalance(mMasterBalance.load()); |
| 4369 | mBalance.process((float *)mMixerBuffer, mNormalFrameCount); |
| 4370 | } |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 4371 | } |
| 4372 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4373 | memcpy_by_audio_format(buffer, format, mMixerBuffer, mMixerBufferFormat, |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 4374 | mNormalFrameCount * (mixerChannelCount + mHapticChannelCount)); |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 4375 | |
| 4376 | // If we're going directly to the sink and there are haptic channels, |
| 4377 | // we should adjust channels as the sample data is partially interleaved |
| 4378 | // in this case. |
| 4379 | if (!mEffectBufferValid && mHapticChannelCount > 0) { |
| 4380 | adjust_channels_non_destructive(buffer, mChannelCount, buffer, |
| 4381 | mChannelCount + mHapticChannelCount, |
| 4382 | audio_bytes_per_sample(format), |
| 4383 | audio_bytes_per_frame(mChannelCount, format) * mNormalFrameCount); |
| 4384 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4385 | } |
| 4386 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4387 | mBytesRemaining = mCurrentWriteLength; |
| 4388 | if (isSuspended()) { |
Andy Hung | 238fa3d | 2016-07-28 10:53:22 -0700 | [diff] [blame] | 4389 | // Simulate write to HAL when suspended (e.g. BT SCO phone call). |
| 4390 | mSleepTimeUs = suspendSleepTimeUs(); // assumes full buffer. |
| 4391 | const size_t framesRemaining = mBytesRemaining / mFrameSize; |
| 4392 | mBytesWritten += mBytesRemaining; |
| 4393 | mFramesWritten += framesRemaining; |
| 4394 | mSuspendedFrames += framesRemaining; // to adjust kernel HAL position |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4395 | mBytesRemaining = 0; |
| 4396 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4397 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4398 | // only process effects if we're going to write |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4399 | if (mSleepTimeUs == 0 && mType != OFFLOAD) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4400 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 4401 | effectChains[i]->process_l(); |
jiabin | 47affe5 | 2019-04-04 18:02:07 -0700 | [diff] [blame] | 4402 | // TODO: Write haptic data directly to sink buffer when mixing. |
Andy Hung | 6e6a2e6 | 2019-04-30 16:38:41 -0700 | [diff] [blame] | 4403 | if (activeHapticSessionId != AUDIO_SESSION_NONE |
| 4404 | && activeHapticSessionId == effectChains[i]->sessionId()) { |
jiabin | 47affe5 | 2019-04-04 18:02:07 -0700 | [diff] [blame] | 4405 | // Haptic data is active in this case, copy it directly from |
| 4406 | // in buffer to out buffer. |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4407 | uint32_t hapticSessionChannelCount = mEffectBufferValid ? |
| 4408 | audio_channel_count_from_out_mask(mMixerChannelMask) : |
| 4409 | mChannelCount; |
| 4410 | if (mType == SPATIALIZER && !isHapticSessionSpatialized) { |
| 4411 | hapticSessionChannelCount = mChannelCount; |
| 4412 | } |
| 4413 | |
jiabin | 47affe5 | 2019-04-04 18:02:07 -0700 | [diff] [blame] | 4414 | const size_t audioBufferSize = mNormalFrameCount |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4415 | * audio_bytes_per_frame(hapticSessionChannelCount, |
Andy Hung | 319587b | 2023-05-23 14:01:03 -0700 | [diff] [blame] | 4416 | AUDIO_FORMAT_PCM_FLOAT); |
jiabin | 47affe5 | 2019-04-04 18:02:07 -0700 | [diff] [blame] | 4417 | memcpy_by_audio_format( |
| 4418 | (uint8_t*)effectChains[i]->outBuffer() + audioBufferSize, |
Andy Hung | 319587b | 2023-05-23 14:01:03 -0700 | [diff] [blame] | 4419 | AUDIO_FORMAT_PCM_FLOAT, |
jiabin | 47affe5 | 2019-04-04 18:02:07 -0700 | [diff] [blame] | 4420 | (const uint8_t*)effectChains[i]->inBuffer() + audioBufferSize, |
Andy Hung | 319587b | 2023-05-23 14:01:03 -0700 | [diff] [blame] | 4421 | AUDIO_FORMAT_PCM_FLOAT, mNormalFrameCount * mHapticChannelCount); |
jiabin | 47affe5 | 2019-04-04 18:02:07 -0700 | [diff] [blame] | 4422 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4423 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4424 | } |
| 4425 | } |
Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 4426 | // Process effect chains for offloaded thread even if no audio |
| 4427 | // was read from audio track: process only updates effect state |
| 4428 | // and thus does have to be synchronized with audio writes but may have |
| 4429 | // to be called while waiting for async write callback |
| 4430 | if (mType == OFFLOAD) { |
| 4431 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 4432 | effectChains[i]->process_l(); |
| 4433 | } |
| 4434 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4435 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4436 | // Only if the Effects buffer is enabled and there is data in the |
| 4437 | // Effects buffer (buffer valid), we need to |
| 4438 | // copy into the sink buffer. |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4439 | // TODO use mSleepTimeUs == 0 as an additional condition. |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 4440 | if (mEffectBufferValid && !mHasDataCopiedToSinkBuffer) { |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4441 | //ALOGV("writing effect buffer to sink buffer format %#x", mFormat); |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4442 | void *effectBuffer = (mType == SPATIALIZER) ? mPostSpatializerBuffer : mEffectBuffer; |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4443 | if (requireMonoBlend()) { |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4444 | mono_blend(effectBuffer, mEffectBufferFormat, mChannelCount, mNormalFrameCount, |
Glenn Kasten | 03c48d5 | 2016-01-27 17:25:17 -0800 | [diff] [blame] | 4445 | true /*limit*/); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4446 | } |
| 4447 | |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 4448 | if (!hasFastMixer()) { |
| 4449 | // Balance must take effect after mono conversion. |
| 4450 | // We do it here if there is no FastMixer. |
| 4451 | // mBalance detects zero balance within the class for speed (not needed here). |
| 4452 | mBalance.setBalance(mMasterBalance.load()); |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4453 | mBalance.process((float *)effectBuffer, mNormalFrameCount); |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 4454 | } |
| 4455 | |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4456 | // for SPATIALIZER thread, Move haptics channels from mEffectBuffer to |
| 4457 | // mPostSpatializerBuffer if the haptics track is spatialized. |
| 4458 | // Otherwise, the haptics channels are already in mPostSpatializerBuffer. |
| 4459 | // For other thread types, the haptics channels are already in mEffectBuffer. |
| 4460 | if (mType == SPATIALIZER && isHapticSessionSpatialized) { |
| 4461 | const size_t srcBufferSize = mNormalFrameCount * |
| 4462 | audio_bytes_per_frame(audio_channel_count_from_out_mask(mMixerChannelMask), |
| 4463 | mEffectBufferFormat); |
| 4464 | const size_t dstBufferSize = mNormalFrameCount |
| 4465 | * audio_bytes_per_frame(mChannelCount, mEffectBufferFormat); |
| 4466 | |
| 4467 | memcpy_by_audio_format((uint8_t*)mPostSpatializerBuffer + dstBufferSize, |
| 4468 | mEffectBufferFormat, |
| 4469 | (uint8_t*)mEffectBuffer + srcBufferSize, |
| 4470 | mEffectBufferFormat, |
| 4471 | mNormalFrameCount * mHapticChannelCount); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 4472 | } |
Atneya Nair | ba9a107 | 2022-09-19 17:51:37 -0700 | [diff] [blame] | 4473 | const size_t framesToCopy = mNormalFrameCount * (mChannelCount + mHapticChannelCount); |
| 4474 | if (mFormat == AUDIO_FORMAT_PCM_FLOAT && |
| 4475 | mEffectBufferFormat == AUDIO_FORMAT_PCM_FLOAT) { |
| 4476 | // Clamp PCM float values more than this distance from 0 to insulate |
| 4477 | // a HAL which doesn't handle NaN correctly. |
| 4478 | static constexpr float HAL_FLOAT_SAMPLE_LIMIT = 2.0f; |
| 4479 | memcpy_to_float_from_float_with_clamping(static_cast<float*>(mSinkBuffer), |
| 4480 | static_cast<const float*>(effectBuffer), |
| 4481 | framesToCopy, HAL_FLOAT_SAMPLE_LIMIT /* absMax */); |
| 4482 | } else { |
| 4483 | memcpy_by_audio_format(mSinkBuffer, mFormat, |
| 4484 | effectBuffer, mEffectBufferFormat, framesToCopy); |
| 4485 | } |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 4486 | // The sample data is partially interleaved when haptic channels exist, |
| 4487 | // we need to adjust channels here. |
| 4488 | if (mHapticChannelCount > 0) { |
| 4489 | adjust_channels_non_destructive(mSinkBuffer, mChannelCount, mSinkBuffer, |
| 4490 | mChannelCount + mHapticChannelCount, |
| 4491 | audio_bytes_per_sample(mFormat), |
| 4492 | audio_bytes_per_frame(mChannelCount, mFormat) * mNormalFrameCount); |
| 4493 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4494 | } |
| 4495 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4496 | // enable changes in effect chain |
| 4497 | unlockEffectChains(effectChains); |
| 4498 | |
Vlad Popa | fce1086 | 2023-02-03 10:37:07 +0100 | [diff] [blame] | 4499 | if (!metadataUpdate.playbackMetadataUpdate.empty()) { |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 4500 | mAfThreadCallback->getMelReporter()->updateMetadataForCsd(id(), |
Vlad Popa | fce1086 | 2023-02-03 10:37:07 +0100 | [diff] [blame] | 4501 | metadataUpdate.playbackMetadataUpdate); |
| 4502 | } |
| 4503 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4504 | if (!waitingAsyncCallback()) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4505 | // mSleepTimeUs == 0 means we must write to audio hardware |
| 4506 | if (mSleepTimeUs == 0) { |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4507 | ssize_t ret = 0; |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4508 | // writePeriodNs is updated >= 0 when ret > 0. |
| 4509 | int64_t writePeriodNs = -1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4510 | if (mBytesRemaining) { |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 4511 | // FIXME rewrite to reduce number of system calls |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4512 | const int64_t lastIoBeginNs = systemTime(); |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4513 | ret = threadLoop_write(); |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4514 | const int64_t lastIoEndNs = systemTime(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4515 | if (ret < 0) { |
| 4516 | mBytesRemaining = 0; |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4517 | } else if (ret > 0) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4518 | mBytesWritten += ret; |
| 4519 | mBytesRemaining -= ret; |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4520 | const int64_t frames = ret / mFrameSize; |
| 4521 | mFramesWritten += frames; |
| 4522 | |
| 4523 | writePeriodNs = lastIoEndNs - mLastIoEndNs; |
| 4524 | // process information relating to write time. |
| 4525 | if (audio_has_proportional_frames(mFormat)) { |
| 4526 | // we are in a continuous mixing cycle |
| 4527 | if (mMixerStatus == MIXER_TRACKS_READY && |
| 4528 | loopCount == lastLoopCountWritten + 1) { |
| 4529 | |
| 4530 | const double jitterMs = |
| 4531 | TimestampVerifier<int64_t, int64_t>::computeJitterMs( |
| 4532 | {frames, writePeriodNs}, |
| 4533 | {0, 0} /* lastTimestamp */, mSampleRate); |
| 4534 | const double processMs = |
| 4535 | (lastIoBeginNs - mLastIoEndNs) * 1e-6; |
| 4536 | |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 4537 | audio_utils::lock_guard _l(mutex()); |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4538 | mIoJitterMs.add(jitterMs); |
| 4539 | mProcessTimeMs.add(processMs); |
Robert Wu | 06db0a3 | 2021-08-10 19:05:34 +0000 | [diff] [blame] | 4540 | |
| 4541 | if (mPipeSink.get() != nullptr) { |
| 4542 | // Using the Monopipe availableToWrite, we estimate the current |
| 4543 | // buffer size. |
| 4544 | MonoPipe* monoPipe = static_cast<MonoPipe*>(mPipeSink.get()); |
| 4545 | const ssize_t |
| 4546 | availableToWrite = mPipeSink->availableToWrite(); |
| 4547 | const size_t pipeFrames = monoPipe->maxFrames(); |
| 4548 | const size_t |
| 4549 | remainingFrames = pipeFrames - max(availableToWrite, 0); |
| 4550 | mMonopipePipeDepthStats.add(remainingFrames); |
| 4551 | } |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4552 | } |
| 4553 | |
| 4554 | // write blocked detection |
| 4555 | const int64_t deltaWriteNs = lastIoEndNs - lastIoBeginNs; |
Andy Hung | d363992 | 2022-04-28 18:00:49 -0700 | [diff] [blame] | 4556 | if ((mType == MIXER || mType == SPATIALIZER) |
| 4557 | && deltaWriteNs > maxPeriod) { |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4558 | mNumDelayedWrites++; |
| 4559 | if ((lastIoEndNs - lastWarning) > kWarningThrottleNs) { |
| 4560 | ATRACE_NAME("underrun"); |
| 4561 | ALOGW("write blocked for %lld msecs, " |
| 4562 | "%d delayed writes, thread %d", |
| 4563 | (long long)deltaWriteNs / NANOS_PER_MILLISECOND, |
| 4564 | mNumDelayedWrites, mId); |
| 4565 | lastWarning = lastIoEndNs; |
| 4566 | } |
| 4567 | } |
| 4568 | } |
| 4569 | // update timing info. |
| 4570 | mLastIoBeginNs = lastIoBeginNs; |
| 4571 | mLastIoEndNs = lastIoEndNs; |
| 4572 | lastLoopCountWritten = loopCount; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4573 | } |
| 4574 | } else if ((mMixerStatus == MIXER_DRAIN_TRACK) || |
| 4575 | (mMixerStatus == MIXER_DRAIN_ALL)) { |
| 4576 | threadLoop_drain(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4577 | } |
Andy Hung | d363992 | 2022-04-28 18:00:49 -0700 | [diff] [blame] | 4578 | if ((mType == MIXER || mType == SPATIALIZER) && !mStandby) { |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4579 | |
| 4580 | if (mThreadThrottle |
| 4581 | && mMixerStatus == MIXER_TRACKS_READY // we are mixing (active tracks) |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4582 | && writePeriodNs > 0) { // we have write period info |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4583 | // Limit MixerThread data processing to no more than twice the |
| 4584 | // expected processing rate. |
| 4585 | // |
| 4586 | // This helps prevent underruns with NuPlayer and other applications |
| 4587 | // which may set up buffers that are close to the minimum size, or use |
| 4588 | // deep buffers, and rely on a double-buffering sleep strategy to fill. |
| 4589 | // |
| 4590 | // The throttle smooths out sudden large data drains from the device, |
| 4591 | // e.g. when it comes out of standby, which often causes problems with |
| 4592 | // (1) mixer threads without a fast mixer (which has its own warm-up) |
| 4593 | // (2) minimum buffer sized tracks (even if the track is full, |
| 4594 | // 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] | 4595 | // |
| 4596 | // Total time spent in last processing cycle equals time spent in |
| 4597 | // 1. threadLoop_write, as well as time spent in |
| 4598 | // 2. threadLoop_mix (significant for heavy mixing, especially |
| 4599 | // on low tier processors) |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4600 | |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4601 | // it's OK if deltaMs is an overestimate. |
| 4602 | |
| 4603 | const int32_t deltaMs = writePeriodNs / NANOS_PER_MILLISECOND; |
Ivan Lozano | e02bc54 | 2017-10-26 09:51:54 -0700 | [diff] [blame] | 4604 | |
Ivan Lozano | ea04d39 | 2017-11-07 14:37:07 -0800 | [diff] [blame] | 4605 | const int32_t throttleMs = (int32_t)mHalfBufferMs - deltaMs; |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4606 | if ((signed)mHalfBufferMs >= throttleMs && throttleMs > 0) { |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 4607 | mThreadMetrics.logThrottleMs((double)throttleMs); |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 4608 | |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4609 | usleep(throttleMs * 1000); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 4610 | // notify of throttle start on verbose log |
| 4611 | ALOGV_IF(mThreadThrottleEndMs == mThreadThrottleTimeMs, |
| 4612 | "mixer(%p) throttle begin:" |
| 4613 | " ret(%zd) deltaMs(%d) requires sleep %d ms", |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4614 | this, ret, deltaMs, throttleMs); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 4615 | mThreadThrottleTimeMs += throttleMs; |
Andy Hung | 0a31ddd | 2016-07-06 19:10:29 -0700 | [diff] [blame] | 4616 | // Throttle must be attributed to the previous mixer loop's write time |
| 4617 | // to allow back-to-back throttling. |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4618 | // This also ensures proper timing statistics. |
| 4619 | mLastIoEndNs = systemTime(); // we fetch the write end time again. |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 4620 | } else { |
| 4621 | uint32_t diff = mThreadThrottleTimeMs - mThreadThrottleEndMs; |
| 4622 | if (diff > 0) { |
| 4623 | // notify of throttle end on debug log |
Andy Hung | 3ea004d | 2016-05-05 16:48:37 -0700 | [diff] [blame] | 4624 | // but prevent spamming for bluetooth |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4625 | ALOGD_IF(!isSingleDeviceType( |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 4626 | outDeviceTypes_l(), audio_is_a2dp_out_device) && |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4627 | !isSingleDeviceType( |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 4628 | outDeviceTypes_l(), |
| 4629 | audio_is_hearing_aid_out_device), |
Andy Hung | 3ea004d | 2016-05-05 16:48:37 -0700 | [diff] [blame] | 4630 | "mixer(%p) throttle end: throttle time(%u)", this, diff); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 4631 | mThreadThrottleEndMs = mThreadThrottleTimeMs; |
| 4632 | } |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4633 | } |
| 4634 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4635 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4636 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4637 | } else { |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 4638 | ATRACE_BEGIN("sleep"); |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4639 | audio_utils::unique_lock _l(mutex()); |
rago | 1bb9082 | 2017-05-02 18:31:48 -0700 | [diff] [blame] | 4640 | // suspended requires accurate metering of sleep time. |
| 4641 | if (isSuspended()) { |
| 4642 | // advance by expected sleepTime |
| 4643 | timeLoopNextNs += microseconds((nsecs_t)mSleepTimeUs); |
| 4644 | const nsecs_t nowNs = systemTime(); |
| 4645 | |
| 4646 | // compute expected next time vs current time. |
| 4647 | // (negative deltas are treated as delays). |
| 4648 | nsecs_t deltaNs = timeLoopNextNs - nowNs; |
| 4649 | if (deltaNs < -kMaxNextBufferDelayNs) { |
| 4650 | // Delays longer than the max allowed trigger a reset. |
| 4651 | ALOGV("DelayNs: %lld, resetting timeLoopNextNs", (long long) deltaNs); |
| 4652 | deltaNs = microseconds((nsecs_t)mSleepTimeUs); |
| 4653 | timeLoopNextNs = nowNs + deltaNs; |
| 4654 | } else if (deltaNs < 0) { |
| 4655 | // Delays within the max delay allowed: zero the delta/sleepTime |
| 4656 | // to help the system catch up in the next iteration(s) |
| 4657 | ALOGV("DelayNs: %lld, catching-up", (long long) deltaNs); |
| 4658 | deltaNs = 0; |
| 4659 | } |
| 4660 | // update sleep time (which is >= 0) |
| 4661 | mSleepTimeUs = deltaNs / 1000; |
| 4662 | } |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 4663 | if (!mSignalPending && mConfigEvents.isEmpty() && !exitPending()) { |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4664 | mWaitWorkCV.wait_for(_l, std::chrono::microseconds(mSleepTimeUs)); |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 4665 | } |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 4666 | ATRACE_END(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4667 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4668 | } |
| 4669 | |
| 4670 | // Finally let go of removed track(s), without the lock held |
| 4671 | // since we can't guarantee the destructors won't acquire that |
| 4672 | // same lock. This will also mutate and push a new fast mixer state. |
| 4673 | threadLoop_removeTracks(tracksToRemove); |
| 4674 | tracksToRemove.clear(); |
| 4675 | |
| 4676 | // FIXME I don't understand the need for this here; |
| 4677 | // it was in the original code but maybe the |
| 4678 | // assignment in saveOutputTracks() makes this unnecessary? |
| 4679 | clearOutputTracks(); |
| 4680 | |
| 4681 | // Effect chains will be actually deleted here if they were removed from |
| 4682 | // mEffectChains list during mixing or effects processing |
| 4683 | effectChains.clear(); |
| 4684 | |
| 4685 | // FIXME Note that the above .clear() is no longer necessary since effectChains |
| 4686 | // 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] | 4687 | |
| 4688 | mThreadloopExecutor.process(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4689 | } |
Andy Hung | 56ce2ed | 2024-06-12 16:03:16 -0700 | [diff] [blame] | 4690 | mThreadloopExecutor.process(); // process any remaining deferred actions. |
| 4691 | // deferred actions after this point are ignored. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4692 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4693 | threadLoop_exit(); |
| 4694 | |
Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 4695 | if (!mStandby) { |
| 4696 | threadLoop_standby(); |
Eric Laurent | 19952e1 | 2023-04-20 10:08:29 +0200 | [diff] [blame] | 4697 | setStandby(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4698 | } |
| 4699 | |
| 4700 | releaseWakeLock(); |
| 4701 | |
| 4702 | ALOGV("Thread %p type %d exiting", this, mType); |
| 4703 | return false; |
| 4704 | } |
| 4705 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 4706 | void PlaybackThread::collectTimestamps_l() |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 4707 | { |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 4708 | if (mStandby) { |
| 4709 | mTimestampVerifier.discontinuity(discontinuityForStandbyOrFlush()); |
| 4710 | return; |
| 4711 | } else if (mHwPaused) { |
| 4712 | mTimestampVerifier.discontinuity(mTimestampVerifier.DISCONTINUITY_MODE_CONTINUOUS); |
| 4713 | return; |
| 4714 | } |
| 4715 | |
| 4716 | // Gather the framesReleased counters for all active tracks, |
| 4717 | // and associate with the sink frames written out. We need |
| 4718 | // this to convert the sink timestamp to the track timestamp. |
| 4719 | bool kernelLocationUpdate = false; |
| 4720 | ExtendedTimestamp timestamp; // use private copy to fetch |
| 4721 | |
| 4722 | // Always query HAL timestamp and update timestamp verifier. In standby or pause, |
| 4723 | // HAL may be draining some small duration buffered data for fade out. |
| 4724 | if (threadloop_getHalTimestamp_l(×tamp) == OK) { |
| 4725 | mTimestampVerifier.add(timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL], |
| 4726 | timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL], |
| 4727 | mSampleRate); |
| 4728 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 4729 | if (isTimestampCorrectionEnabled_l()) { |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 4730 | ALOGVV("TS_BEFORE: %d %lld %lld", id(), |
| 4731 | (long long)timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL], |
| 4732 | (long long)timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]); |
| 4733 | auto correctedTimestamp = mTimestampVerifier.getLastCorrectedTimestamp(); |
| 4734 | timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] |
| 4735 | = correctedTimestamp.mFrames; |
| 4736 | timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] |
| 4737 | = correctedTimestamp.mTimeNs; |
| 4738 | ALOGVV("TS_AFTER: %d %lld %lld", id(), |
| 4739 | (long long)timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL], |
| 4740 | (long long)timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]); |
| 4741 | |
| 4742 | // Note: Downstream latency only added if timestamp correction enabled. |
| 4743 | if (mDownstreamLatencyStatMs.getN() > 0) { // we have latency info. |
| 4744 | const int64_t newPosition = |
| 4745 | timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] |
| 4746 | - int64_t(mDownstreamLatencyStatMs.getMean() * mSampleRate * 1e-3); |
| 4747 | // prevent retrograde |
| 4748 | timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = max( |
| 4749 | newPosition, |
| 4750 | (mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] |
| 4751 | - mSuspendedFrames)); |
| 4752 | } |
| 4753 | } |
| 4754 | |
| 4755 | // We always fetch the timestamp here because often the downstream |
| 4756 | // sink will block while writing. |
| 4757 | |
| 4758 | // We keep track of the last valid kernel position in case we are in underrun |
| 4759 | // and the normal mixer period is the same as the fast mixer period, or there |
| 4760 | // is some error from the HAL. |
| 4761 | if (mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) { |
| 4762 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] = |
| 4763 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]; |
| 4764 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] = |
| 4765 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; |
| 4766 | |
| 4767 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] = |
| 4768 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER]; |
| 4769 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] = |
| 4770 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER]; |
| 4771 | } |
| 4772 | |
| 4773 | if (timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) { |
| 4774 | kernelLocationUpdate = true; |
| 4775 | } else { |
| 4776 | ALOGVV("getTimestamp error - no valid kernel position"); |
| 4777 | } |
| 4778 | |
| 4779 | // copy over kernel info |
| 4780 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = |
| 4781 | timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] |
| 4782 | + mSuspendedFrames; // add frames discarded when suspended |
| 4783 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = |
| 4784 | timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; |
| 4785 | } else { |
| 4786 | mTimestampVerifier.error(); |
| 4787 | } |
| 4788 | |
| 4789 | // mFramesWritten for non-offloaded tracks are contiguous |
| 4790 | // even after standby() is called. This is useful for the track frame |
| 4791 | // to sink frame mapping. |
| 4792 | bool serverLocationUpdate = false; |
| 4793 | if (mFramesWritten != mLastFramesWritten) { |
| 4794 | serverLocationUpdate = true; |
| 4795 | mLastFramesWritten = mFramesWritten; |
| 4796 | } |
| 4797 | // Only update timestamps if there is a meaningful change. |
| 4798 | // Either the kernel timestamp must be valid or we have written something. |
| 4799 | if (kernelLocationUpdate || serverLocationUpdate) { |
| 4800 | if (serverLocationUpdate) { |
| 4801 | // use the time before we called the HAL write - it is a bit more accurate |
| 4802 | // to when the server last read data than the current time here. |
| 4803 | // |
| 4804 | // If we haven't written anything, mLastIoBeginNs will be -1 |
| 4805 | // and we use systemTime(). |
| 4806 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] = mFramesWritten; |
| 4807 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = mLastIoBeginNs == -1 |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 4808 | ? systemTime() : (int64_t)mLastIoBeginNs; |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 4809 | } |
| 4810 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 4811 | for (const sp<IAfTrack>& t : mActiveTracks) { |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 4812 | if (!t->isFastTrack()) { |
| 4813 | t->updateTrackFrameInfo( |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 4814 | t->audioTrackServerProxy()->framesReleased(), |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 4815 | mFramesWritten, |
| 4816 | mSampleRate, |
| 4817 | mTimestamp); |
| 4818 | } |
| 4819 | } |
| 4820 | } |
| 4821 | |
| 4822 | if (audio_has_proportional_frames(mFormat)) { |
| 4823 | const double latencyMs = mTimestamp.getOutputServerLatencyMs(mSampleRate); |
| 4824 | if (latencyMs != 0.) { // note 0. means timestamp is empty. |
| 4825 | mLatencyMs.add(latencyMs); |
| 4826 | } |
| 4827 | } |
| 4828 | #if 0 |
| 4829 | // logFormat example |
| 4830 | if (z % 100 == 0) { |
| 4831 | timespec ts; |
| 4832 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 4833 | LOGT("This is an integer %d, this is a float %f, this is my " |
| 4834 | "pid %p %% %s %t", 42, 3.14, "and this is a timestamp", ts); |
| 4835 | LOGT("A deceptive null-terminated string %\0"); |
| 4836 | } |
| 4837 | ++z; |
| 4838 | #endif |
| 4839 | } |
| 4840 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4841 | // removeTracks_l() must be called with ThreadBase::mutex() held |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 4842 | void PlaybackThread::removeTracks_l(const Vector<sp<IAfTrack>>& tracksToRemove) |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4843 | NO_THREAD_SAFETY_ANALYSIS // release and re-acquire mutex() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4844 | { |
Andy Hung | 6c498e9 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 4845 | if (tracksToRemove.empty()) return; |
| 4846 | |
| 4847 | // Block all incoming TrackHandle requests until we are finished with the release. |
| 4848 | setThreadBusy_l(true); |
| 4849 | |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 4850 | for (const auto& track : tracksToRemove) { |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 4851 | 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] | 4852 | sp<IAfEffectChain> chain = getEffectChain_l(track->sessionId()); |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 4853 | if (chain != 0) { |
| 4854 | ALOGV("%s(%d): stopping track on chain %p for session Id: %d", |
| 4855 | __func__, track->id(), chain.get(), track->sessionId()); |
| 4856 | chain->decActiveTrackCnt(); |
| 4857 | } |
Andy Hung | 6c498e9 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 4858 | |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 4859 | // If an external client track, inform APM we're no longer active, and remove if needed. |
Andy Hung | 6c498e9 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 4860 | // 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] | 4861 | if (track->isExternalTrack()) { |
Andy Hung | 6c498e9 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 4862 | mutex().unlock(); |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 4863 | AudioSystem::stopOutput(track->portId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4864 | if (track->isTerminated()) { |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 4865 | AudioSystem::releaseOutput(track->portId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4866 | } |
Andy Hung | 6c498e9 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 4867 | mutex().lock(); |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 4868 | } |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 4869 | if (mHapticChannelCount > 0 && |
| 4870 | ((track->channelMask() & AUDIO_CHANNEL_HAPTIC_ALL) != AUDIO_CHANNEL_NONE |
Shunkai Yao | 29d1057 | 2024-03-19 04:31:47 +0000 | [diff] [blame] | 4871 | || (chain != nullptr && chain->containsHapticGeneratingEffect()))) { |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4872 | mutex().unlock(); |
jiabin | 57303cc | 2018-12-18 15:45:57 -0800 | [diff] [blame] | 4873 | // Unlock due to VibratorService will lock for this call and will |
| 4874 | // call Tracks.mute/unmute which also require thread's lock. |
Andy Hung | 7fb97e1 | 2023-07-20 21:23:42 -0700 | [diff] [blame] | 4875 | afutils::onExternalVibrationStop(track->getExternalVibration()); |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4876 | mutex().lock(); |
jiabin | e70bc7f | 2020-06-30 22:07:55 -0700 | [diff] [blame] | 4877 | |
| 4878 | // When the track is stop, set the haptic intensity as MUTE |
| 4879 | // for the HapticGenerator effect. |
| 4880 | if (chain != nullptr) { |
Ahmad Khalil | 229466a | 2024-02-05 12:15:30 +0000 | [diff] [blame] | 4881 | chain->setHapticScale_l(track->id(), os::HapticScale::mute()); |
jiabin | e70bc7f | 2020-06-30 22:07:55 -0700 | [diff] [blame] | 4882 | } |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 4883 | } |
Andy Hung | 6c498e9 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 4884 | |
| 4885 | // Under lock, the track is removed from the active tracks list. |
| 4886 | // |
| 4887 | // Once the track is no longer active, the TrackHandle may directly |
| 4888 | // modify it as the threadLoop() is no longer responsible for its maintenance. |
| 4889 | // Do not modify the track from threadLoop after the mutex is unlocked |
| 4890 | // if it is not active. |
| 4891 | mActiveTracks.remove(track); |
| 4892 | |
| 4893 | if (track->isTerminated()) { |
| 4894 | // remove from our tracks vector |
| 4895 | removeTrack_l(track); |
| 4896 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4897 | } |
Andy Hung | 6c498e9 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 4898 | |
| 4899 | // Allow incoming TrackHandle requests. We still hold the mutex, |
| 4900 | // so pending TrackHandle requests will occur after we unlock it. |
| 4901 | setThreadBusy_l(false); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4902 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4903 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 4904 | status_t PlaybackThread::getTimestamp_l(AudioTimestamp& timestamp) |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 4905 | { |
| 4906 | if (mNormalSink != 0) { |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 4907 | ExtendedTimestamp ets; |
| 4908 | status_t status = mNormalSink->getTimestamp(ets); |
| 4909 | if (status == NO_ERROR) { |
| 4910 | status = ets.getBestTimestamp(×tamp); |
| 4911 | } |
| 4912 | return status; |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 4913 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 4914 | if ((mType == OFFLOAD || mType == DIRECT) && mOutput != NULL) { |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 4915 | collectTimestamps_l(); |
| 4916 | if (mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] <= 0) { |
| 4917 | return INVALID_OPERATION; |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 4918 | } |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 4919 | timestamp.mPosition = mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]; |
| 4920 | const int64_t timeNs = mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; |
| 4921 | timestamp.mTime.tv_sec = timeNs / NANOS_PER_SECOND; |
| 4922 | timestamp.mTime.tv_nsec = timeNs - (timestamp.mTime.tv_sec * NANOS_PER_SECOND); |
| 4923 | return NO_ERROR; |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 4924 | } |
| 4925 | return INVALID_OPERATION; |
| 4926 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4927 | |
Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 4928 | // For dedicated VoIP outputs, let the HAL apply the stream volume. Track volume is |
| 4929 | // still applied by the mixer. |
| 4930 | // All tracks attached to a mixer with flag VOIP_RX are tied to the same |
| 4931 | // stream type STREAM_VOICE_CALL so this will only change the HAL volume once even |
| 4932 | // if more than one track are active |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 4933 | status_t PlaybackThread::handleVoipVolume_l(float* volume) |
Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 4934 | { |
| 4935 | status_t result = NO_ERROR; |
| 4936 | if ((mOutput->flags & AUDIO_OUTPUT_FLAG_VOIP_RX) != 0) { |
| 4937 | if (*volume != mLeftVolFloat) { |
| 4938 | result = mOutput->stream->setVolume(*volume, *volume); |
Alex Leung | c5dedb2 | 2023-05-10 08:00:50 -0700 | [diff] [blame] | 4939 | // HAL can return INVALID_OPERATION if operation is not supported. |
| 4940 | ALOGE_IF(result != OK && result != INVALID_OPERATION, |
Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 4941 | "Error when setting output stream volume: %d", result); |
| 4942 | if (result == NO_ERROR) { |
| 4943 | mLeftVolFloat = *volume; |
| 4944 | } |
| 4945 | } |
| 4946 | // if stream volume was successfully sent to the HAL, mLeftVolFloat == v here and we |
| 4947 | // remove stream volume contribution from software volume. |
| 4948 | if (mLeftVolFloat == *volume) { |
| 4949 | *volume = 1.0f; |
| 4950 | } |
| 4951 | } |
| 4952 | return result; |
| 4953 | } |
| 4954 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 4955 | status_t MixerThread::createAudioPatch_l(const struct audio_patch* patch, |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4956 | audio_patch_handle_t *handle) |
| 4957 | { |
Andy Hung | f60abce | 2016-08-26 11:37:54 -0700 | [diff] [blame] | 4958 | status_t status; |
| 4959 | if (property_get_bool("af.patch_park", false /* default_value */)) { |
| 4960 | // Park FastMixer to avoid potential DOS issues with writing to the HAL |
| 4961 | // or if HAL does not properly lock against access. |
| 4962 | AutoPark<FastMixer> park(mFastMixer); |
| 4963 | status = PlaybackThread::createAudioPatch_l(patch, handle); |
| 4964 | } else { |
| 4965 | status = PlaybackThread::createAudioPatch_l(patch, handle); |
| 4966 | } |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 4967 | |
| 4968 | updateHalSupportedLatencyModes_l(); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4969 | return status; |
| 4970 | } |
| 4971 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 4972 | status_t PlaybackThread::createAudioPatch_l(const struct audio_patch *patch, |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4973 | audio_patch_handle_t *handle) |
| 4974 | { |
| 4975 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4976 | |
| 4977 | // store new device and send to effects |
| 4978 | audio_devices_t type = AUDIO_DEVICE_NONE; |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4979 | AudioDeviceTypeAddrVector deviceTypeAddrs; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4980 | for (unsigned int i = 0; i < patch->num_sinks; i++) { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4981 | LOG_ALWAYS_FATAL_IF(popcount(patch->sinks[i].ext.device.type) > 1 |
| 4982 | && !mOutput->audioHwDev->supportsAudioPatches(), |
| 4983 | "Enumerated device type(%#x) must not be used " |
| 4984 | "as it does not support audio patches", |
| 4985 | patch->sinks[i].ext.device.type); |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 4986 | 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] | 4987 | deviceTypeAddrs.emplace_back(patch->sinks[i].ext.device.type, |
| 4988 | patch->sinks[i].ext.device.address); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4989 | } |
| 4990 | |
François Gaffie | 0c280aa | 2018-07-25 10:02:15 +0200 | [diff] [blame] | 4991 | audio_port_handle_t sinkPortId = patch->sinks[0].id; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4992 | #ifdef ADD_BATTERY_DATA |
| 4993 | // when changing the audio output device, call addBatteryData to notify |
| 4994 | // the change |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4995 | if (outDeviceTypes() != deviceTypes) { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4996 | uint32_t params = 0; |
| 4997 | // check whether speaker is on |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4998 | if (deviceTypes.count(AUDIO_DEVICE_OUT_SPEAKER) > 0) { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4999 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5000 | } |
| 5001 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 5002 | // check if any other device (except speaker) is on |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 5003 | if (!isSingleDeviceType(deviceTypes, AUDIO_DEVICE_OUT_SPEAKER)) { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 5004 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; |
| 5005 | } |
| 5006 | |
| 5007 | if (params != 0) { |
| 5008 | addBatteryData(params); |
| 5009 | } |
| 5010 | } |
| 5011 | #endif |
| 5012 | |
| 5013 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
jiabin | 8f278ee | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 5014 | mEffectChains[i]->setDevices_l(deviceTypeAddrs); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 5015 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 5016 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 5017 | // mPatch.num_sinks is not set when the thread is created so that |
| 5018 | // the first patch creation triggers an ioConfigChanged callback |
| 5019 | bool configChanged = (mPatch.num_sinks == 0) || |
| 5020 | (mPatch.sinks[0].id != sinkPortId); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 5021 | mPatch = *patch; |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 5022 | mOutDeviceTypeAddrs = deviceTypeAddrs; |
jiabin | 0a957d3 | 2020-04-29 10:56:20 -0700 | [diff] [blame] | 5023 | checkSilentMode_l(); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 5024 | |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 5025 | if (mOutput->audioHwDev->supportsAudioPatches()) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 5026 | sp<DeviceHalInterface> hwDevice = mOutput->audioHwDev->hwDevice(); |
| 5027 | status = hwDevice->createAudioPatch(patch->num_sources, |
| 5028 | patch->sources, |
| 5029 | patch->num_sinks, |
| 5030 | patch->sinks, |
| 5031 | handle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5032 | } else { |
Ytai Ben-Tsvi | f997ffe | 2022-02-03 16:38:16 -0800 | [diff] [blame] | 5033 | status = mOutput->stream->legacyCreateAudioPatch(patch->sinks[0], std::nullopt, type); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 5034 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5035 | } |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 5036 | const std::string patchSinksAsString = patchSinksToString(patch); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 5037 | |
| 5038 | mThreadMetrics.logEndInterval(); |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 5039 | mThreadMetrics.logCreatePatch(/* inDevices */ {}, patchSinksAsString); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 5040 | mThreadMetrics.logBeginInterval(); |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 5041 | // also dispatch to active AudioTracks for MediaMetrics |
| 5042 | for (const auto &track : mActiveTracks) { |
| 5043 | track->logEndInterval(); |
| 5044 | track->logBeginInterval(patchSinksAsString); |
| 5045 | } |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 5046 | |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 5047 | if (configChanged) { |
| 5048 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
| 5049 | } |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 5050 | // Force metadata update after a route change |
Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 5051 | mActiveTracks.setHasChanged(); |
| 5052 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5053 | return status; |
| 5054 | } |
| 5055 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5056 | status_t MixerThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 5057 | { |
Andy Hung | f60abce | 2016-08-26 11:37:54 -0700 | [diff] [blame] | 5058 | status_t status; |
| 5059 | if (property_get_bool("af.patch_park", false /* default_value */)) { |
| 5060 | // Park FastMixer to avoid potential DOS issues with writing to the HAL |
| 5061 | // or if HAL does not properly lock against access. |
| 5062 | AutoPark<FastMixer> park(mFastMixer); |
| 5063 | status = PlaybackThread::releaseAudioPatch_l(handle); |
| 5064 | } else { |
| 5065 | status = PlaybackThread::releaseAudioPatch_l(handle); |
| 5066 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 5067 | return status; |
| 5068 | } |
| 5069 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5070 | status_t PlaybackThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5071 | { |
| 5072 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 5073 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 5074 | mPatch = audio_patch{}; |
| 5075 | mOutDeviceTypeAddrs.clear(); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 5076 | |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 5077 | if (mOutput->audioHwDev->supportsAudioPatches()) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 5078 | sp<DeviceHalInterface> hwDevice = mOutput->audioHwDev->hwDevice(); |
| 5079 | status = hwDevice->releaseAudioPatch(handle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5080 | } else { |
Ytai Ben-Tsvi | f997ffe | 2022-02-03 16:38:16 -0800 | [diff] [blame] | 5081 | status = mOutput->stream->legacyReleaseAudioPatch(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5082 | } |
Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 5083 | // Force meteadata update after a route change |
| 5084 | mActiveTracks.setHasChanged(); |
| 5085 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5086 | return status; |
| 5087 | } |
| 5088 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5089 | void PlaybackThread::addPatchTrack(const sp<IAfPatchTrack>& track) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5090 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 5091 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5092 | mTracks.add(track); |
| 5093 | } |
| 5094 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5095 | void PlaybackThread::deletePatchTrack(const sp<IAfPatchTrack>& track) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5096 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 5097 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5098 | destroyTrack_l(track); |
| 5099 | } |
| 5100 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5101 | void PlaybackThread::toAudioPortConfig(struct audio_port_config* config) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5102 | { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5103 | ThreadBase::toAudioPortConfig(config); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5104 | config->role = AUDIO_PORT_ROLE_SOURCE; |
| 5105 | config->ext.mix.hw_module = mOutput->audioHwDev->handle(); |
| 5106 | config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; |
Mikhail Naganov | 32abc2b | 2018-05-24 12:57:11 -0700 | [diff] [blame] | 5107 | if (mOutput && mOutput->flags != AUDIO_OUTPUT_FLAG_NONE) { |
| 5108 | config->config_mask |= AUDIO_PORT_CONFIG_FLAGS; |
| 5109 | config->flags.output = mOutput->flags; |
| 5110 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5111 | } |
| 5112 | |
Atneya Nair | aa3afcb | 2024-10-08 16:36:19 -0700 | [diff] [blame] | 5113 | std::string PlaybackThread::getLocalLogHeader() const { |
| 5114 | using namespace std::literals; |
| 5115 | static constexpr auto indent = " " |
| 5116 | " "sv; |
| 5117 | return std::string{indent}.append(IAfTrack::getLogHeader()); |
| 5118 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5119 | // ---------------------------------------------------------------------------- |
| 5120 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5121 | /* static */ |
| 5122 | sp<IAfPlaybackThread> IAfPlaybackThread::createMixerThread( |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 5123 | const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut* output, |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5124 | audio_io_handle_t id, bool systemReady, type_t type, audio_config_base_t* mixerConfig) { |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 5125 | return sp<MixerThread>::make(afThreadCallback, output, id, systemReady, type, mixerConfig); |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5126 | } |
| 5127 | |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 5128 | MixerThread::MixerThread(const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut* output, |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 5129 | audio_io_handle_t id, bool systemReady, type_t type, audio_config_base_t *mixerConfig) |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 5130 | : PlaybackThread(afThreadCallback, output, id, type, systemReady, mixerConfig), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5131 | // mAudioMixer below |
| 5132 | // mFastMixer below |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 5133 | mBluetoothLatencyModesEnabled(false), |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 5134 | mFastMixerFutex(0), |
| 5135 | mMasterMono(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5136 | // mOutputSink below |
| 5137 | // mPipeSink below |
| 5138 | // mNormalSink below |
| 5139 | { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 5140 | ALOGV("MixerThread() id=%d type=%d", id, type); |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 5141 | ALOGV("mSampleRate=%u, mChannelMask=%#x, mChannelCount=%u, mFormat=%#x, mFrameSize=%zu, " |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 5142 | "mFrameCount=%zu, mNormalFrameCount=%zu", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5143 | mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount, |
| 5144 | mNormalFrameCount); |
| 5145 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
| 5146 | |
Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 5147 | if (type == DUPLICATING) { |
| 5148 | // The Duplicating thread uses the AudioMixer and delivers data to OutputTracks |
| 5149 | // (downstream MixerThreads) in DuplicatingThread::threadLoop_write(). |
| 5150 | // Do not create or use mFastMixer, mOutputSink, mPipeSink, or mNormalSink. |
Andy Hung | 922617c | 2024-06-25 17:07:58 -0700 | [diff] [blame] | 5151 | // Balance is *not* set in the DuplicatingThread here (or from AudioFlinger), |
| 5152 | // as the downstream MixerThreads implement it. |
Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 5153 | return; |
| 5154 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5155 | // create an NBAIO sink for the HAL output stream, and negotiate |
Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 5156 | mOutputSink = new AudioStreamOutSink(output->stream); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5157 | size_t numCounterOffers = 0; |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 5158 | const NBAIO_Format offers[1] = {Format_from_SR_C( |
| 5159 | mSampleRate, mChannelCount + mHapticChannelCount, mFormat)}; |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5160 | #if !LOG_NDEBUG |
| 5161 | ssize_t index = |
| 5162 | #else |
| 5163 | (void) |
| 5164 | #endif |
| 5165 | mOutputSink->negotiate(offers, 1, NULL, numCounterOffers); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5166 | ALOG_ASSERT(index == 0); |
| 5167 | |
| 5168 | // initialize fast mixer depending on configuration |
| 5169 | bool initFastMixer; |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 5170 | if (mType == SPATIALIZER || mType == BIT_PERFECT) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5171 | initFastMixer = false; |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 5172 | } else { |
| 5173 | switch (kUseFastMixer) { |
| 5174 | case FastMixer_Never: |
| 5175 | initFastMixer = false; |
| 5176 | break; |
| 5177 | case FastMixer_Always: |
| 5178 | initFastMixer = true; |
| 5179 | break; |
| 5180 | case FastMixer_Static: |
| 5181 | case FastMixer_Dynamic: |
Henrik Tillman | 470b399 | 2024-10-08 12:49:28 +0200 | [diff] [blame] | 5182 | if (mType == MIXER && (output->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER)) { |
| 5183 | /* Do not init fast mixer on deep buffer, warn if buffers are confed too small */ |
| 5184 | initFastMixer = false; |
| 5185 | ALOGW_IF(mFrameCount * 1000 / mSampleRate < kMinNormalSinkBufferSizeMs, |
| 5186 | "HAL DEEP BUFFER Buffer (%zu ms) is smaller than set minimal buffer " |
| 5187 | "(%u ms), seems like a configuration error", |
| 5188 | mFrameCount * 1000 / mSampleRate, kMinNormalSinkBufferSizeMs); |
| 5189 | } else { |
| 5190 | initFastMixer = mFrameCount < mNormalFrameCount; |
| 5191 | } |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 5192 | break; |
| 5193 | } |
| 5194 | ALOGW_IF(initFastMixer == false && mFrameCount < mNormalFrameCount, |
| 5195 | "FastMixer is preferred for this sink as frameCount %zu is less than threshold %zu", |
| 5196 | mFrameCount, mNormalFrameCount); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5197 | } |
| 5198 | if (initFastMixer) { |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 5199 | audio_format_t fastMixerFormat; |
| 5200 | if (mMixerBufferEnabled && mEffectBufferEnabled) { |
| 5201 | fastMixerFormat = AUDIO_FORMAT_PCM_FLOAT; |
| 5202 | } else { |
| 5203 | fastMixerFormat = AUDIO_FORMAT_PCM_16_BIT; |
| 5204 | } |
| 5205 | if (mFormat != fastMixerFormat) { |
| 5206 | // change our Sink format to accept our intermediate precision |
| 5207 | mFormat = fastMixerFormat; |
| 5208 | free(mSinkBuffer); |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 5209 | mFrameSize = audio_bytes_per_frame(mChannelCount + mHapticChannelCount, mFormat); |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 5210 | const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; |
| 5211 | (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); |
| 5212 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5213 | |
| 5214 | // create a MonoPipe to connect our submix to FastMixer |
| 5215 | NBAIO_Format format = mOutputSink->format(); |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 5216 | |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 5217 | // adjust format to match that of the Fast Mixer |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 5218 | ALOGV("format changed from %#x to %#x", format.mFormat, fastMixerFormat); |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 5219 | format.mFormat = fastMixerFormat; |
| 5220 | format.mFrameSize = audio_bytes_per_sample(format.mFormat) * format.mChannelCount; |
| 5221 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5222 | // This pipe depth compensates for scheduling latency of the normal mixer thread. |
| 5223 | // When it wakes up after a maximum latency, it runs a few cycles quickly before |
| 5224 | // finally blocking. Note the pipe implementation rounds up the request to a power of 2. |
| 5225 | MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/); |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 5226 | const NBAIO_Format offersFast[1] = {format}; |
| 5227 | size_t numCounterOffersFast = 0; |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 5228 | #if !LOG_NDEBUG |
Eric Laurent | 1e28aaa | 2023-04-16 19:34:23 +0200 | [diff] [blame] | 5229 | index = |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5230 | #else |
| 5231 | (void) |
| 5232 | #endif |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 5233 | monoPipe->negotiate(offersFast, std::size(offersFast), |
| 5234 | nullptr /* counterOffers */, numCounterOffersFast); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5235 | ALOG_ASSERT(index == 0); |
| 5236 | monoPipe->setAvgFrames((mScreenState & 1) ? |
| 5237 | (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); |
| 5238 | mPipeSink = monoPipe; |
| 5239 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5240 | // 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] | 5241 | mFastMixer = new FastMixer(mId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5242 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 5243 | #ifdef STATE_QUEUE_DUMP |
| 5244 | sq->setObserverDump(&mStateQueueObserverDump); |
| 5245 | sq->setMutatorDump(&mStateQueueMutatorDump); |
| 5246 | #endif |
| 5247 | FastMixerState *state = sq->begin(); |
| 5248 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 5249 | // wrap the source side of the MonoPipe to make it an AudioBufferProvider |
| 5250 | fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe)); |
| 5251 | fastTrack->mVolumeProvider = NULL; |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 5252 | fastTrack->mChannelMask = static_cast<audio_channel_mask_t>( |
| 5253 | mChannelMask | mHapticChannelMask); // mPipeSink channel mask for |
| 5254 | // audio to FastMixer |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 5255 | fastTrack->mFormat = mFormat; // mPipeSink format for audio to FastMixer |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 5256 | fastTrack->mHapticPlaybackEnabled = mHapticChannelMask != AUDIO_CHANNEL_NONE; |
Lais Andrade | e8995e9 | 2024-07-24 15:00:38 +0100 | [diff] [blame] | 5257 | fastTrack->mHapticScale = os::HapticScale::none(); |
Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 5258 | fastTrack->mHapticMaxAmplitude = NAN; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5259 | fastTrack->mGeneration++; |
| 5260 | state->mFastTracksGen++; |
| 5261 | state->mTrackMask = 1; |
| 5262 | // fast mixer will use the HAL output sink |
| 5263 | state->mOutputSink = mOutputSink.get(); |
| 5264 | state->mOutputSinkGen++; |
| 5265 | state->mFrameCount = mFrameCount; |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 5266 | // specify sink channel mask when haptic channel mask present as it can not |
| 5267 | // be calculated directly from channel count |
| 5268 | state->mSinkChannelMask = mHapticChannelMask == AUDIO_CHANNEL_NONE |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 5269 | ? AUDIO_CHANNEL_NONE |
| 5270 | : static_cast<audio_channel_mask_t>(mChannelMask | mHapticChannelMask); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5271 | state->mCommand = FastMixerState::COLD_IDLE; |
| 5272 | // already done in constructor initialization list |
| 5273 | //mFastMixerFutex = 0; |
| 5274 | state->mColdFutexAddr = &mFastMixerFutex; |
| 5275 | state->mColdGen++; |
| 5276 | state->mDumpState = &mFastMixerDumpState; |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 5277 | mFastMixerNBLogWriter = afThreadCallback->newWriter_l(kFastMixerLogSize, "FastMixer"); |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 5278 | state->mNBLogWriter = mFastMixerNBLogWriter.get(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5279 | sq->end(); |
Andy Hung | 82f39d6 | 2024-09-30 17:19:14 -0700 | [diff] [blame] | 5280 | { |
| 5281 | audio_utils::mutex::scoped_queue_wait_check queueWaitCheck(mFastMixer->getTid()); |
| 5282 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 5283 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5284 | |
Eric Tan | 0513b5d | 2018-09-17 10:32:48 -0700 | [diff] [blame] | 5285 | NBLog::thread_info_t info; |
| 5286 | info.id = mId; |
| 5287 | info.type = NBLog::FASTMIXER; |
| 5288 | mFastMixerNBLogWriter->log<NBLog::EVENT_THREAD_INFO>(info); |
| 5289 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5290 | // start the fast mixer |
| 5291 | mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO); |
| 5292 | pid_t tid = mFastMixer->getTid(); |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 5293 | sendPrioConfigEvent(getpid(), tid, kPriorityFastMixer, false /*forApp*/); |
Mikhail Naganov | e1c4b5d | 2016-12-22 09:22:45 -0800 | [diff] [blame] | 5294 | stream()->setHalThreadPriority(kPriorityFastMixer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5295 | |
| 5296 | #ifdef AUDIO_WATCHDOG |
| 5297 | // create and start the watchdog |
| 5298 | mAudioWatchdog = new AudioWatchdog(); |
| 5299 | mAudioWatchdog->setDump(&mAudioWatchdogDump); |
| 5300 | mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO); |
| 5301 | tid = mAudioWatchdog->getTid(); |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 5302 | sendPrioConfigEvent(getpid(), tid, kPriorityFastMixer, false /*forApp*/); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5303 | #endif |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 5304 | } else { |
| 5305 | #ifdef TEE_SINK |
| 5306 | // Only use the MixerThread tee if there is no FastMixer. |
| 5307 | mTee.set(mOutputSink->format(), NBAIO_Tee::TEE_FLAG_OUTPUT_THREAD); |
| 5308 | mTee.setId(std::string("_") + std::to_string(mId) + "_M"); |
| 5309 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5310 | } |
| 5311 | |
| 5312 | switch (kUseFastMixer) { |
| 5313 | case FastMixer_Never: |
| 5314 | case FastMixer_Dynamic: |
| 5315 | mNormalSink = mOutputSink; |
| 5316 | break; |
| 5317 | case FastMixer_Always: |
| 5318 | mNormalSink = mPipeSink; |
| 5319 | break; |
| 5320 | case FastMixer_Static: |
| 5321 | mNormalSink = initFastMixer ? mPipeSink : mOutputSink; |
| 5322 | break; |
| 5323 | } |
Andy Hung | 922617c | 2024-06-25 17:07:58 -0700 | [diff] [blame] | 5324 | // setMasterBalance needs to be called after the FastMixer |
| 5325 | // (if any) is set up, in order to deliver the balance settings to it. |
| 5326 | setMasterBalance(afThreadCallback->getMasterBalance_l()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5327 | } |
| 5328 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5329 | MixerThread::~MixerThread() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5330 | { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 5331 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5332 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 5333 | FastMixerState *state = sq->begin(); |
| 5334 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
| 5335 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 5336 | if (old == -1) { |
Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 5337 | (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5338 | } |
| 5339 | } |
| 5340 | state->mCommand = FastMixerState::EXIT; |
| 5341 | sq->end(); |
Andy Hung | 82f39d6 | 2024-09-30 17:19:14 -0700 | [diff] [blame] | 5342 | { |
| 5343 | audio_utils::mutex::scoped_join_wait_check queueWaitCheck(mFastMixer->getTid()); |
| 5344 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 5345 | mFastMixer->join(); |
| 5346 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5347 | // Though the fast mixer thread has exited, it's state queue is still valid. |
| 5348 | // We'll use that extract the final state which contains one remaining fast track |
| 5349 | // corresponding to our sub-mix. |
| 5350 | state = sq->begin(); |
| 5351 | ALOG_ASSERT(state->mTrackMask == 1); |
| 5352 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 5353 | ALOG_ASSERT(fastTrack->mBufferProvider != NULL); |
| 5354 | delete fastTrack->mBufferProvider; |
| 5355 | sq->end(false /*didModify*/); |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 5356 | mFastMixer.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5357 | #ifdef AUDIO_WATCHDOG |
| 5358 | if (mAudioWatchdog != 0) { |
| 5359 | mAudioWatchdog->requestExit(); |
| 5360 | mAudioWatchdog->requestExitAndWait(); |
| 5361 | mAudioWatchdog.clear(); |
| 5362 | } |
| 5363 | #endif |
| 5364 | } |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 5365 | mAfThreadCallback->unregisterWriter(mFastMixerNBLogWriter); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5366 | delete mAudioMixer; |
| 5367 | } |
| 5368 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5369 | void MixerThread::onFirstRef() { |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 5370 | PlaybackThread::onFirstRef(); |
| 5371 | |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 5372 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 5373 | if (mOutput != nullptr && mOutput->stream != nullptr) { |
| 5374 | status_t status = mOutput->stream->setLatencyModeCallback(this); |
| 5375 | if (status != INVALID_OPERATION) { |
| 5376 | updateHalSupportedLatencyModes_l(); |
| 5377 | } |
| 5378 | // Default to enabled if the HAL supports it. This can be changed by Audioflinger after |
| 5379 | // the thread construction according to AudioFlinger::mBluetoothLatencyModesEnabled |
| 5380 | mBluetoothLatencyModesEnabled.store( |
| 5381 | mOutput->audioHwDev->supportsBluetoothVariableLatency()); |
| 5382 | } |
| 5383 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5384 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5385 | uint32_t MixerThread::correctLatency_l(uint32_t latency) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5386 | { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 5387 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5388 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); |
| 5389 | latency += (pipe->getAvgFrames() * 1000) / mSampleRate; |
| 5390 | } |
| 5391 | return latency; |
| 5392 | } |
| 5393 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5394 | ssize_t MixerThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5395 | { |
| 5396 | // FIXME we should only do one push per cycle; confirm this is true |
| 5397 | // Start the fast mixer if it's not already running |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 5398 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5399 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 5400 | FastMixerState *state = sq->begin(); |
| 5401 | if (state->mCommand != FastMixerState::MIX_WRITE && |
| 5402 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) { |
| 5403 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
Eric Laurent | a2ab450 | 2015-09-09 12:25:51 -0700 | [diff] [blame] | 5404 | |
| 5405 | // FIXME workaround for first HAL write being CPU bound on some devices |
| 5406 | ATRACE_BEGIN("write"); |
| 5407 | mOutput->write((char *)mSinkBuffer, 0); |
| 5408 | ATRACE_END(); |
| 5409 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5410 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 5411 | if (old == -1) { |
Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 5412 | (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5413 | } |
| 5414 | #ifdef AUDIO_WATCHDOG |
| 5415 | if (mAudioWatchdog != 0) { |
| 5416 | mAudioWatchdog->resume(); |
| 5417 | } |
| 5418 | #endif |
| 5419 | } |
| 5420 | state->mCommand = FastMixerState::MIX_WRITE; |
Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 5421 | #ifdef FAST_THREAD_STATISTICS |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 5422 | mFastMixerDumpState.increaseSamplingN(mAfThreadCallback->isLowRamDevice() ? |
Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 5423 | FastThreadDumpState::kSamplingNforLowRamDevice : FastThreadDumpState::kSamplingN); |
Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 5424 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5425 | sq->end(); |
Andy Hung | 82f39d6 | 2024-09-30 17:19:14 -0700 | [diff] [blame] | 5426 | { |
| 5427 | audio_utils::mutex::scoped_queue_wait_check queueWaitCheck(mFastMixer->getTid()); |
| 5428 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 5429 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5430 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 5431 | mNormalSink = mPipeSink; |
| 5432 | } |
| 5433 | } else { |
| 5434 | sq->end(false /*didModify*/); |
| 5435 | } |
| 5436 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5437 | return PlaybackThread::threadLoop_write(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5438 | } |
| 5439 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5440 | void MixerThread::threadLoop_standby() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5441 | { |
| 5442 | // Idle the fast mixer if it's currently running |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 5443 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5444 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 5445 | FastMixerState *state = sq->begin(); |
| 5446 | if (!(state->mCommand & FastMixerState::IDLE)) { |
Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 5447 | // Report any frames trapped in the Monopipe |
| 5448 | MonoPipe *monoPipe = (MonoPipe *)mPipeSink.get(); |
| 5449 | const long long pipeFrames = monoPipe->maxFrames() - monoPipe->availableToWrite(); |
| 5450 | mLocalLog.log("threadLoop_standby: framesWritten:%lld suspendedFrames:%lld " |
| 5451 | "monoPipeWritten:%lld monoPipeLeft:%lld", |
| 5452 | (long long)mFramesWritten, (long long)mSuspendedFrames, |
| 5453 | (long long)mPipeSink->framesWritten(), pipeFrames); |
| 5454 | mLocalLog.log("threadLoop_standby: %s", mTimestamp.toString().c_str()); |
| 5455 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5456 | state->mCommand = FastMixerState::COLD_IDLE; |
| 5457 | state->mColdFutexAddr = &mFastMixerFutex; |
| 5458 | state->mColdGen++; |
| 5459 | mFastMixerFutex = 0; |
| 5460 | sq->end(); |
| 5461 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now |
Andy Hung | 82f39d6 | 2024-09-30 17:19:14 -0700 | [diff] [blame] | 5462 | { |
| 5463 | audio_utils::mutex::scoped_queue_wait_check queueWaitCheck(mFastMixer->getTid()); |
| 5464 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); |
| 5465 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5466 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 5467 | mNormalSink = mOutputSink; |
| 5468 | } |
| 5469 | #ifdef AUDIO_WATCHDOG |
| 5470 | if (mAudioWatchdog != 0) { |
| 5471 | mAudioWatchdog->pause(); |
| 5472 | } |
| 5473 | #endif |
| 5474 | } else { |
| 5475 | sq->end(false /*didModify*/); |
| 5476 | } |
| 5477 | } |
| 5478 | PlaybackThread::threadLoop_standby(); |
| 5479 | } |
| 5480 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5481 | bool PlaybackThread::waitingAsyncCallback_l() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5482 | { |
| 5483 | return false; |
| 5484 | } |
| 5485 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5486 | bool PlaybackThread::shouldStandby_l() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5487 | { |
| 5488 | return !mStandby; |
| 5489 | } |
| 5490 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5491 | bool PlaybackThread::waitingAsyncCallback() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5492 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 5493 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5494 | return waitingAsyncCallback_l(); |
| 5495 | } |
| 5496 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5497 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5498 | void PlaybackThread::threadLoop_standby() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5499 | { |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 5500 | ALOGV("%s: audio hardware entering standby, mixer %p, suspend count %d", |
| 5501 | __func__, this, (int32_t)mSuspended); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5502 | mOutput->standby(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5503 | if (mUseAsyncWrite != 0) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5504 | // discard any pending drain or write ack by incrementing sequence |
| 5505 | mWriteAckSequence = (mWriteAckSequence + 2) & ~1; |
| 5506 | mDrainSequence = (mDrainSequence + 2) & ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5507 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5508 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
| 5509 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5510 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5511 | mHwPaused = false; |
Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 5512 | setHalLatencyMode_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5513 | } |
| 5514 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5515 | void PlaybackThread::onAddNewTrack_l() |
Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 5516 | { |
| 5517 | ALOGV("signal playback thread"); |
| 5518 | broadcast_l(); |
| 5519 | } |
| 5520 | |
Mikhail Naganov | bf203ce | 2024-05-23 16:27:59 -0700 | [diff] [blame] | 5521 | void PlaybackThread::onAsyncError(bool isHardError) |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5522 | { |
Mikhail Naganov | bf203ce | 2024-05-23 16:27:59 -0700 | [diff] [blame] | 5523 | auto allTrackPortIds = getTrackPortIds(); |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5524 | for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) { |
| 5525 | invalidateTracks((audio_stream_type_t)i); |
| 5526 | } |
Mikhail Naganov | bf203ce | 2024-05-23 16:27:59 -0700 | [diff] [blame] | 5527 | if (isHardError) { |
| 5528 | mAfThreadCallback->onHardError(allTrackPortIds); |
| 5529 | } |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5530 | } |
| 5531 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5532 | void MixerThread::threadLoop_mix() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5533 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5534 | // mix buffers... |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 5535 | mAudioMixer->process(); |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5536 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5537 | // increase sleep time progressively when application underrun condition clears. |
| 5538 | // Only increase sleep time if the mixer is ready for two consecutive times to avoid |
| 5539 | // that a steady state of alternating ready/not ready conditions keeps the sleep time |
| 5540 | // such that we would underrun the audio HAL. |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5541 | if ((mSleepTimeUs == 0) && (sleepTimeShift > 0)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5542 | sleepTimeShift--; |
| 5543 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5544 | mSleepTimeUs = 0; |
| 5545 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5546 | //TODO: delay standby when effects have a tail |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 5547 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5548 | } |
| 5549 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5550 | void MixerThread::threadLoop_sleepTime() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5551 | { |
| 5552 | // If no tracks are ready, sleep once for the duration of an output |
| 5553 | // buffer size, then write 0s to the output |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5554 | if (mSleepTimeUs == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5555 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Andy Hung | 06d1322 | 2018-05-03 20:13:31 -0700 | [diff] [blame] | 5556 | if (mPipeSink.get() != nullptr && mPipeSink == mNormalSink) { |
| 5557 | // Using the Monopipe availableToWrite, we estimate the |
| 5558 | // sleep time to retry for more data (before we underrun). |
| 5559 | MonoPipe *monoPipe = static_cast<MonoPipe *>(mPipeSink.get()); |
| 5560 | const ssize_t availableToWrite = mPipeSink->availableToWrite(); |
| 5561 | const size_t pipeFrames = monoPipe->maxFrames(); |
| 5562 | const size_t framesLeft = pipeFrames - max(availableToWrite, 0); |
| 5563 | // HAL_framecount <= framesDelay ~ framesLeft / 2 <= Normal_Mixer_framecount |
| 5564 | const size_t framesDelay = std::min( |
| 5565 | mNormalFrameCount, max(framesLeft / 2, mFrameCount)); |
| 5566 | ALOGV("pipeFrames:%zu framesLeft:%zu framesDelay:%zu", |
| 5567 | pipeFrames, framesLeft, framesDelay); |
| 5568 | mSleepTimeUs = framesDelay * MICROS_PER_SECOND / mSampleRate; |
| 5569 | } else { |
| 5570 | mSleepTimeUs = mActiveSleepTimeUs >> sleepTimeShift; |
| 5571 | if (mSleepTimeUs < kMinThreadSleepTimeUs) { |
| 5572 | mSleepTimeUs = kMinThreadSleepTimeUs; |
| 5573 | } |
| 5574 | // reduce sleep time in case of consecutive application underruns to avoid |
| 5575 | // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer |
| 5576 | // duration we would end up writing less data than needed by the audio HAL if |
| 5577 | // the condition persists. |
| 5578 | if (sleepTimeShift < kMaxThreadSleepTimeShift) { |
| 5579 | sleepTimeShift++; |
| 5580 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5581 | } |
| 5582 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5583 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5584 | } |
| 5585 | } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) { |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 5586 | // clear out mMixerBuffer or mSinkBuffer, to ensure buffers are cleared |
| 5587 | // before effects processing or output. |
| 5588 | if (mMixerBufferValid) { |
| 5589 | memset(mMixerBuffer, 0, mMixerBufferSize); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5590 | if (mType == SPATIALIZER) { |
| 5591 | memset(mSinkBuffer, 0, mSinkBufferSize); |
| 5592 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 5593 | } else { |
| 5594 | memset(mSinkBuffer, 0, mSinkBufferSize); |
| 5595 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5596 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5597 | ALOGV_IF(mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED), |
| 5598 | "anticipated start"); |
| 5599 | } |
| 5600 | // TODO add standby time extension fct of effect tail |
| 5601 | } |
| 5602 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 5603 | // prepareTracks_l() must be called with ThreadBase::mutex() held |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5604 | PlaybackThread::mixer_state MixerThread::prepareTracks_l( |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5605 | Vector<sp<IAfTrack>>* tracksToRemove) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5606 | { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5607 | // clean up deleted track ids in AudioMixer before allocating new tracks |
| 5608 | (void)mTracks.processDeletedTrackIds([this](int trackId) { |
| 5609 | // for each trackId, destroy it in the AudioMixer |
| 5610 | if (mAudioMixer->exists(trackId)) { |
| 5611 | mAudioMixer->destroy(trackId); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5612 | } |
| 5613 | }); |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5614 | mTracks.clearDeletedTrackIds(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5615 | |
| 5616 | mixer_state mixerStatus = MIXER_IDLE; |
| 5617 | // find out which tracks need to be processed |
| 5618 | size_t count = mActiveTracks.size(); |
| 5619 | size_t mixedTracks = 0; |
| 5620 | size_t tracksWithEffect = 0; |
| 5621 | // counts only _active_ fast tracks |
| 5622 | size_t fastTracks = 0; |
| 5623 | uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset |
| 5624 | |
| 5625 | float masterVolume = mMasterVolume; |
| 5626 | bool masterMute = mMasterMute; |
| 5627 | |
| 5628 | if (masterMute) { |
| 5629 | masterVolume = 0; |
| 5630 | } |
| 5631 | // 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] | 5632 | sp<IAfEffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5633 | if (chain != 0) { |
| 5634 | uint32_t v = (uint32_t)(masterVolume * (1 << 24)); |
Shunkai Yao | f484765 | 2024-01-12 00:25:20 +0000 | [diff] [blame] | 5635 | chain->setVolume(&v, &v); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5636 | masterVolume = (float)((v + (1 << 23)) >> 24); |
| 5637 | chain.clear(); |
| 5638 | } |
| 5639 | |
| 5640 | // prepare a new state to push |
| 5641 | FastMixerStateQueue *sq = NULL; |
| 5642 | FastMixerState *state = NULL; |
| 5643 | bool didModify = false; |
| 5644 | FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED; |
Andy Hung | f228531 | 2017-02-14 18:31:59 -0800 | [diff] [blame] | 5645 | bool coldIdle = false; |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 5646 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5647 | sq = mFastMixer->sq(); |
| 5648 | state = sq->begin(); |
Andy Hung | f228531 | 2017-02-14 18:31:59 -0800 | [diff] [blame] | 5649 | coldIdle = state->mCommand == FastMixerState::COLD_IDLE; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5650 | } |
| 5651 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 5652 | mMixerBufferValid = false; // mMixerBuffer has no valid data until appropriate tracks found. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 5653 | mEffectBufferValid = false; // mEffectBuffer has no valid data until tracks found. |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 5654 | |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 5655 | // DeferredOperations handles statistics after setting mixerStatus. |
| 5656 | class DeferredOperations { |
| 5657 | public: |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 5658 | DeferredOperations(mixer_state *mixerStatus, ThreadMetrics *threadMetrics) |
| 5659 | : mMixerStatus(mixerStatus) |
| 5660 | , mThreadMetrics(threadMetrics) {} |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 5661 | |
| 5662 | // when leaving scope, tally frames properly. |
| 5663 | ~DeferredOperations() { |
| 5664 | // Tally underrun frames only if we are actually mixing (MIXER_TRACKS_READY) |
| 5665 | // because that is when the underrun occurs. |
| 5666 | // We do not distinguish between FastTracks and NormalTracks here. |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 5667 | size_t maxUnderrunFrames = 0; |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 5668 | if (*mMixerStatus == MIXER_TRACKS_READY && mUnderrunFrames.size() > 0) { |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 5669 | for (const auto &underrun : mUnderrunFrames) { |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 5670 | underrun.first->tallyUnderrunFrames(underrun.second); |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 5671 | maxUnderrunFrames = max(underrun.second, maxUnderrunFrames); |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 5672 | } |
| 5673 | } |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 5674 | // send the max underrun frames for this mixer period |
| 5675 | mThreadMetrics->logUnderrunFrames(maxUnderrunFrames); |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 5676 | } |
| 5677 | |
| 5678 | // tallyUnderrunFrames() is called to update the track counters |
| 5679 | // with the number of underrun frames for a particular mixer period. |
| 5680 | // We defer tallying until we know the final mixer status. |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5681 | void tallyUnderrunFrames(const sp<IAfTrack>& track, size_t underrunFrames) { |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 5682 | mUnderrunFrames.emplace_back(track, underrunFrames); |
| 5683 | } |
| 5684 | |
| 5685 | private: |
| 5686 | const mixer_state * const mMixerStatus; |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 5687 | ThreadMetrics * const mThreadMetrics; |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5688 | std::vector<std::pair<sp<IAfTrack>, size_t>> mUnderrunFrames; |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 5689 | } deferredOperations(&mixerStatus, &mThreadMetrics); |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 5690 | // implicit nested scope for variable capture |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 5691 | |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 5692 | bool noFastHapticTrack = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5693 | for (size_t i=0 ; i<count ; i++) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5694 | const sp<IAfTrack> t = mActiveTracks[i]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5695 | |
| 5696 | // this const just means the local variable doesn't change |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5697 | IAfTrack* const track = t.get(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5698 | |
| 5699 | // process fast tracks |
| 5700 | if (track->isFastTrack()) { |
Andy Hung | ae22b48 | 2019-05-09 15:38:55 -0700 | [diff] [blame] | 5701 | LOG_ALWAYS_FATAL_IF(mFastMixer.get() == nullptr, |
| 5702 | "%s(%d): FastTrack(%d) present without FastMixer", |
| 5703 | __func__, id(), track->id()); |
| 5704 | |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 5705 | if (track->getHapticPlaybackEnabled()) { |
| 5706 | noFastHapticTrack = false; |
| 5707 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5708 | |
| 5709 | // It's theoretically possible (though unlikely) for a fast track to be created |
| 5710 | // and then removed within the same normal mix cycle. This is not a problem, as |
| 5711 | // the track never becomes active so it's fast mixer slot is never touched. |
| 5712 | // The converse, of removing an (active) track and then creating a new track |
| 5713 | // at the identical fast mixer slot within the same normal mix cycle, |
| 5714 | // is impossible because the slot isn't marked available until the end of each cycle. |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5715 | int j = track->fastIndex(); |
Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 5716 | ALOG_ASSERT(0 < j && j < (int)FastMixerState::sMaxFastTracks); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5717 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j))); |
| 5718 | FastTrack *fastTrack = &state->mFastTracks[j]; |
| 5719 | |
| 5720 | // Determine whether the track is currently in underrun condition, |
| 5721 | // and whether it had a recent underrun. |
| 5722 | FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j]; |
| 5723 | FastTrackUnderruns underruns = ftDump->mUnderruns; |
| 5724 | uint32_t recentFull = (underruns.mBitFields.mFull - |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5725 | track->fastTrackUnderruns().mBitFields.mFull) & UNDERRUN_MASK; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5726 | uint32_t recentPartial = (underruns.mBitFields.mPartial - |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5727 | track->fastTrackUnderruns().mBitFields.mPartial) & UNDERRUN_MASK; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5728 | uint32_t recentEmpty = (underruns.mBitFields.mEmpty - |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5729 | track->fastTrackUnderruns().mBitFields.mEmpty) & UNDERRUN_MASK; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5730 | uint32_t recentUnderruns = recentPartial + recentEmpty; |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5731 | track->fastTrackUnderruns() = underruns; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5732 | // don't count underruns that occur while stopping or pausing |
| 5733 | // or stopped which can occur when flush() is called while active |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 5734 | size_t underrunFrames = 0; |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 5735 | if (!(track->isStopping() || track->isPausing() || track->isStopped()) && |
| 5736 | recentUnderruns > 0) { |
| 5737 | // 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] | 5738 | underrunFrames = recentUnderruns * mFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5739 | } |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 5740 | // Immediately account for FastTrack underruns. |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5741 | track->audioTrackServerProxy()->tallyUnderrunFrames(underrunFrames); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5742 | |
| 5743 | // This is similar to the state machine for normal tracks, |
| 5744 | // with a few modifications for fast tracks. |
| 5745 | bool isActive = true; |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5746 | switch (track->state()) { |
| 5747 | case IAfTrackBase::STOPPING_1: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5748 | // track stays active in STOPPING_1 state until first underrun |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5749 | if (recentUnderruns > 0 || track->isTerminated()) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5750 | track->setState(IAfTrackBase::STOPPING_2); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5751 | } |
| 5752 | break; |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5753 | case IAfTrackBase::PAUSING: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5754 | // ramp down is not yet implemented |
| 5755 | track->setPaused(); |
| 5756 | break; |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5757 | case IAfTrackBase::RESUMING: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5758 | // ramp up is not yet implemented |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5759 | track->setState(IAfTrackBase::ACTIVE); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5760 | break; |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5761 | case IAfTrackBase::ACTIVE: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5762 | if (recentFull > 0 || recentPartial > 0) { |
| 5763 | // track has provided at least some frames recently: reset retry count |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5764 | track->retryCount() = kMaxTrackRetries; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5765 | } |
| 5766 | if (recentUnderruns == 0) { |
| 5767 | // no recent underruns: stay active |
| 5768 | break; |
| 5769 | } |
| 5770 | // there has recently been an underrun of some kind |
| 5771 | if (track->sharedBuffer() == 0) { |
| 5772 | // were any of the recent underruns "empty" (no frames available)? |
| 5773 | if (recentEmpty == 0) { |
| 5774 | // no, then ignore the partial underruns as they are allowed indefinitely |
| 5775 | break; |
| 5776 | } |
| 5777 | // there has recently been an "empty" underrun: decrement the retry counter |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5778 | if (--(track->retryCount()) > 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5779 | break; |
| 5780 | } |
| 5781 | // indicate to client process that the track was disabled because of underrun; |
| 5782 | // it will then automatically call start() when data is available |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 5783 | track->disable(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5784 | // remove from active list, but state remains ACTIVE [confusing but true] |
| 5785 | isActive = false; |
| 5786 | break; |
| 5787 | } |
Chih-Hung Hsieh | 2b48703 | 2018-09-13 14:16:02 -0700 | [diff] [blame] | 5788 | FALLTHROUGH_INTENDED; |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5789 | case IAfTrackBase::STOPPING_2: |
| 5790 | case IAfTrackBase::PAUSED: |
| 5791 | case IAfTrackBase::STOPPED: |
| 5792 | case IAfTrackBase::FLUSHED: // flush() while active |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5793 | // Check for presentation complete if track is inactive |
| 5794 | // We have consumed all the buffers of this track. |
| 5795 | // This would be incomplete if we auto-paused on underrun |
| 5796 | { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5797 | uint32_t latency = 0; |
| 5798 | status_t result = mOutput->stream->getLatency(&latency); |
| 5799 | ALOGE_IF(result != OK, |
| 5800 | "Error when retrieving output stream latency: %d", result); |
| 5801 | size_t audioHALFrames = (latency * mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 5802 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5803 | if (!(mStandby || track->presentationComplete(framesWritten, audioHALFrames))) { |
| 5804 | // track stays in active list until presentation is complete |
| 5805 | break; |
| 5806 | } |
| 5807 | } |
| 5808 | if (track->isStopping_2()) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5809 | track->setState(IAfTrackBase::STOPPED); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5810 | } |
| 5811 | if (track->isStopped()) { |
| 5812 | // Can't reset directly, as fast mixer is still polling this track |
| 5813 | // track->reset(); |
| 5814 | // So instead mark this track as needing to be reset after push with ack |
| 5815 | resetMask |= 1 << i; |
| 5816 | } |
| 5817 | isActive = false; |
| 5818 | break; |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5819 | case IAfTrackBase::IDLE: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5820 | default: |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5821 | LOG_ALWAYS_FATAL("unexpected track state %d", (int)track->state()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5822 | } |
| 5823 | |
| 5824 | if (isActive) { |
| 5825 | // was it previously inactive? |
| 5826 | if (!(state->mTrackMask & (1 << j))) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5827 | ExtendedAudioBufferProvider *eabp = track->asExtendedAudioBufferProvider(); |
| 5828 | VolumeProvider *vp = track->asVolumeProvider(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5829 | fastTrack->mBufferProvider = eabp; |
| 5830 | fastTrack->mVolumeProvider = vp; |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5831 | fastTrack->mChannelMask = track->channelMask(); |
| 5832 | fastTrack->mFormat = track->format(); |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 5833 | fastTrack->mHapticPlaybackEnabled = track->getHapticPlaybackEnabled(); |
Ahmad Khalil | 229466a | 2024-02-05 12:15:30 +0000 | [diff] [blame] | 5834 | fastTrack->mHapticScale = track->getHapticScale(); |
Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 5835 | fastTrack->mHapticMaxAmplitude = track->getHapticMaxAmplitude(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5836 | fastTrack->mGeneration++; |
| 5837 | state->mTrackMask |= 1 << j; |
| 5838 | didModify = true; |
| 5839 | // no acknowledgement required for newly active tracks |
| 5840 | } |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5841 | sp<AudioTrackServerProxy> proxy = track->audioTrackServerProxy(); |
Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 5842 | float volume; |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 5843 | if (!audioserver_flags::portid_volume_management()) { |
| 5844 | if (track->isPlaybackRestricted() || mStreamTypes[track->streamType()].mute) { |
| 5845 | volume = 0.f; |
| 5846 | } else { |
| 5847 | volume = masterVolume * mStreamTypes[track->streamType()].volume; |
| 5848 | } |
Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 5849 | } else { |
Vlad Popa | 1e865e6 | 2024-08-15 19:11:42 -0700 | [diff] [blame] | 5850 | if (track->isPlaybackRestricted() || track->getPortMute()) { |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 5851 | volume = 0.f; |
| 5852 | } else { |
| 5853 | volume = masterVolume * track->getPortVolume(); |
| 5854 | } |
Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 5855 | } |
Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 5856 | handleVoipVolume_l(&volume); |
| 5857 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5858 | // cache the combined master volume and stream type volume for fast mixer; this |
| 5859 | // lacks any synchronization or barrier so VolumeProvider may read a stale value |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 5860 | const float vh = track->getVolumeHandler()->getVolume( |
Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 5861 | proxy->framesReleased()).first; |
| 5862 | volume *= vh; |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5863 | track->setCachedVolume(volume); |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 5864 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); |
Vlad Popa | e2f5aef | 2022-07-25 16:00:20 +0200 | [diff] [blame] | 5865 | float vlf = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 5866 | float vrf = float_from_gain(gain_minifloat_unpack_right(vlr)); |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 5867 | if (!audioserver_flags::portid_volume_management()) { |
| 5868 | track->processMuteEvent_l(mAfThreadCallback->getOrCreateAudioManager(), |
| 5869 | /*muteState=*/{masterVolume == 0.f, |
| 5870 | mStreamTypes[track->streamType()].volume == 0.f, |
| 5871 | mStreamTypes[track->streamType()].mute, |
| 5872 | track->isPlaybackRestricted(), |
| 5873 | vlf == 0.f && vrf == 0.f, |
Vlad Popa | 1e865e6 | 2024-08-15 19:11:42 -0700 | [diff] [blame] | 5874 | vh == 0.f, |
| 5875 | /*muteFromPortVolume=*/false}); |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 5876 | } else { |
| 5877 | track->processMuteEvent_l(mAfThreadCallback->getOrCreateAudioManager(), |
| 5878 | /*muteState=*/{masterVolume == 0.f, |
| 5879 | track->getPortVolume() == 0.f, |
| 5880 | /* muteFromStreamMuted= */ false, |
| 5881 | track->isPlaybackRestricted(), |
| 5882 | vlf == 0.f && vrf == 0.f, |
Vlad Popa | 1e865e6 | 2024-08-15 19:11:42 -0700 | [diff] [blame] | 5883 | vh == 0.f, |
| 5884 | track->getPortMute()}); |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 5885 | } |
Vlad Popa | e2f5aef | 2022-07-25 16:00:20 +0200 | [diff] [blame] | 5886 | vlf *= volume; |
| 5887 | vrf *= volume; |
Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 5888 | |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 5889 | if (track->getInternalMute()) { |
| 5890 | vlf = 0.f; |
| 5891 | vrf = 0.f; |
| 5892 | } |
| 5893 | |
jiabin | 76d9469 | 2022-12-15 21:51:21 +0000 | [diff] [blame] | 5894 | track->setFinalVolume(vlf, vrf); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5895 | ++fastTracks; |
| 5896 | } else { |
| 5897 | // was it previously active? |
| 5898 | if (state->mTrackMask & (1 << j)) { |
| 5899 | fastTrack->mBufferProvider = NULL; |
| 5900 | fastTrack->mGeneration++; |
| 5901 | state->mTrackMask &= ~(1 << j); |
| 5902 | didModify = true; |
| 5903 | // If any fast tracks were removed, we must wait for acknowledgement |
| 5904 | // because we're about to decrement the last sp<> on those tracks. |
| 5905 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 5906 | } else { |
Glenn Kasten | bf848af | 2017-12-22 11:55:01 -0800 | [diff] [blame] | 5907 | // ALOGW rather than LOG_ALWAYS_FATAL because it seems there are cases where an |
| 5908 | // AudioTrack may start (which may not be with a start() but with a write() |
| 5909 | // after underrun) and immediately paused or released. In that case the |
| 5910 | // FastTrack state hasn't had time to update. |
| 5911 | // TODO Remove the ALOGW when this theory is confirmed. |
| 5912 | ALOGW("fast track %d should have been active; " |
Glenn Kasten | f7d65ee | 2015-12-02 13:45:01 -0800 | [diff] [blame] | 5913 | "mState=%d, mTrackMask=%#x, recentUnderruns=%u, isShared=%d", |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5914 | j, (int)track->state(), state->mTrackMask, recentUnderruns, |
Glenn Kasten | f7d65ee | 2015-12-02 13:45:01 -0800 | [diff] [blame] | 5915 | track->sharedBuffer() != 0); |
Glenn Kasten | bf848af | 2017-12-22 11:55:01 -0800 | [diff] [blame] | 5916 | // Since the FastMixer state already has the track inactive, do nothing here. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5917 | } |
| 5918 | tracksToRemove->add(track); |
| 5919 | // Avoids a misleading display in dumpsys |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5920 | track->fastTrackUnderruns().mBitFields.mMostRecent = UNDERRUN_FULL; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5921 | } |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 5922 | if (fastTrack->mHapticPlaybackEnabled != track->getHapticPlaybackEnabled()) { |
| 5923 | fastTrack->mHapticPlaybackEnabled = track->getHapticPlaybackEnabled(); |
| 5924 | didModify = true; |
| 5925 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5926 | continue; |
| 5927 | } |
| 5928 | |
| 5929 | { // local variable scope to avoid goto warning |
| 5930 | |
| 5931 | audio_track_cblk_t* cblk = track->cblk(); |
| 5932 | |
| 5933 | // The first time a track is added we wait |
| 5934 | // for all its buffers to be filled before processing it |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5935 | const int trackId = track->id(); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5936 | |
| 5937 | // if an active track doesn't exist in the AudioMixer, create it. |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5938 | // use the trackId as the AudioMixer name. |
| 5939 | if (!mAudioMixer->exists(trackId)) { |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5940 | status_t status = mAudioMixer->create( |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5941 | trackId, |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5942 | track->channelMask(), |
| 5943 | track->format(), |
| 5944 | track->sessionId()); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5945 | if (status != OK) { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5946 | ALOGW("%s(): AudioMixer cannot create track(%d)" |
| 5947 | " mask %#x, format %#x, sessionId %d", |
| 5948 | __func__, trackId, |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5949 | track->channelMask(), track->format(), track->sessionId()); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5950 | tracksToRemove->add(track); |
| 5951 | track->invalidate(); // consider it dead. |
| 5952 | continue; |
| 5953 | } |
| 5954 | } |
| 5955 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5956 | // make sure that we have enough frames to mix one full buffer. |
| 5957 | // enforce this condition only once to enable draining the buffer in case the client |
| 5958 | // app does not call stop() and relies on underrun to stop: |
| 5959 | // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed |
| 5960 | // during last round |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 5961 | size_t desiredFrames; |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5962 | const uint32_t sampleRate = track->audioTrackServerProxy()->getSampleRate(); |
| 5963 | const AudioPlaybackRate playbackRate = track->audioTrackServerProxy()->getPlaybackRate(); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 5964 | |
| 5965 | desiredFrames = sourceFramesNeededWithTimestretch( |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 5966 | sampleRate, mNormalFrameCount, mSampleRate, playbackRate.mSpeed); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 5967 | // TODO: ONLY USED FOR LEGACY RESAMPLERS, remove when they are removed. |
| 5968 | // add frames already consumed but not yet released by the resampler |
| 5969 | // because mAudioTrackServerProxy->framesReady() will include these frames |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5970 | desiredFrames += mAudioMixer->getUnreleasedFrames(trackId); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 5971 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5972 | uint32_t minFrames = 1; |
| 5973 | if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() && |
| 5974 | (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 5975 | minFrames = desiredFrames; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5976 | } |
Eric Laurent | 13e4c96 | 2013-12-20 17:36:01 -0800 | [diff] [blame] | 5977 | |
| 5978 | size_t framesReady = track->framesReady(); |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 5979 | if (ATRACE_ENABLED()) { |
| 5980 | // I wish we had formatted trace names |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5981 | std::string traceName("nRdy"); |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5982 | traceName += std::to_string(trackId); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5983 | ATRACE_INT(traceName.c_str(), framesReady); |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 5984 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 5985 | if ((framesReady >= minFrames) && track->isReady() && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5986 | !track->isPaused() && !track->isTerminated()) |
| 5987 | { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5988 | 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] | 5989 | |
| 5990 | mixedTracks++; |
| 5991 | |
Shunkai Yao | f484765 | 2024-01-12 00:25:20 +0000 | [diff] [blame] | 5992 | // track->mainBuffer() != mSinkBuffer and mMixerBuffer means |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 5993 | // there is an effect chain connected to the track |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5994 | chain.clear(); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 5995 | if (track->mainBuffer() != mSinkBuffer && |
| 5996 | track->mainBuffer() != mMixerBuffer) { |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 5997 | if (mEffectBufferEnabled) { |
| 5998 | mEffectBufferValid = true; // Later can set directly. |
| 5999 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6000 | chain = getEffectChain_l(track->sessionId()); |
| 6001 | // Delegate volume control to effect in track effect chain if needed |
| 6002 | if (chain != 0) { |
| 6003 | tracksWithEffect++; |
| 6004 | } else { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6005 | 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] | 6006 | "session %d", |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6007 | trackId, track->sessionId()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6008 | } |
| 6009 | } |
| 6010 | |
| 6011 | |
| 6012 | int param = AudioMixer::VOLUME; |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6013 | if (track->fillingStatus() == IAfTrack::FS_FILLED) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6014 | // no ramp for the first volume setting |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6015 | track->fillingStatus() = IAfTrack::FS_ACTIVE; |
| 6016 | if (track->state() == IAfTrackBase::RESUMING) { |
| 6017 | track->setState(IAfTrackBase::ACTIVE); |
Revathi Uddaraju | 453bcb5 | 2017-08-14 14:19:40 +0800 | [diff] [blame] | 6018 | // If a new track is paused immediately after start, do not ramp on resume. |
| 6019 | if (cblk->mServer != 0) { |
| 6020 | param = AudioMixer::RAMP_VOLUME; |
| 6021 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6022 | } |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6023 | mAudioMixer->setParameter(trackId, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); |
Eric Laurent | 7c29ec9 | 2017-09-20 17:54:22 -0700 | [diff] [blame] | 6024 | mLeftVolFloat = -1.0; |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 6025 | // FIXME should not make a decision based on mServer |
| 6026 | } else if (cblk->mServer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6027 | // If the track is stopped before the first frame was mixed, |
| 6028 | // do not apply ramp |
| 6029 | param = AudioMixer::RAMP_VOLUME; |
| 6030 | } |
| 6031 | |
| 6032 | // compute volume for this track |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 6033 | uint32_t vl, vr; // in U8.24 integer format |
| 6034 | float vlf, vrf, vaf; // in [0.0, 1.0] float format |
Eric Laurent | 7c29ec9 | 2017-09-20 17:54:22 -0700 | [diff] [blame] | 6035 | // read original volumes with volume control |
Andy Hung | 333ab96 | 2019-05-28 20:23:35 -0700 | [diff] [blame] | 6036 | // Always fetch volumeshaper volume to ensure state is updated. |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6037 | const sp<AudioTrackServerProxy> proxy = track->audioTrackServerProxy(); |
Andy Hung | 333ab96 | 2019-05-28 20:23:35 -0700 | [diff] [blame] | 6038 | const float vh = track->getVolumeHandler()->getVolume( |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6039 | track->audioTrackServerProxy()->framesReleased()).first; |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 6040 | float v; |
| 6041 | if (!audioserver_flags::portid_volume_management()) { |
| 6042 | v = masterVolume * mStreamTypes[track->streamType()].volume; |
| 6043 | if (mStreamTypes[track->streamType()].mute || track->isPlaybackRestricted()) { |
| 6044 | v = 0; |
| 6045 | } |
| 6046 | } else { |
| 6047 | v = masterVolume * track->getPortVolume(); |
Vlad Popa | 1e865e6 | 2024-08-15 19:11:42 -0700 | [diff] [blame] | 6048 | if (track->isPlaybackRestricted() || track->getPortMute()) { |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 6049 | v = 0; |
| 6050 | } |
Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 6051 | } |
Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 6052 | handleVoipVolume_l(&v); |
| 6053 | |
| 6054 | if (track->isPausing()) { |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 6055 | vl = vr = 0; |
| 6056 | vlf = vrf = vaf = 0.; |
Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 6057 | track->setPaused(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6058 | } else { |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 6059 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 6060 | vlf = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 6061 | vrf = float_from_gain(gain_minifloat_unpack_right(vlr)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6062 | // 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] | 6063 | if (vlf > GAIN_FLOAT_UNITY) { |
| 6064 | ALOGV("Track left volume out of range: %.3g", vlf); |
| 6065 | vlf = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6066 | } |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 6067 | if (vrf > GAIN_FLOAT_UNITY) { |
| 6068 | ALOGV("Track right volume out of range: %.3g", vrf); |
| 6069 | vrf = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6070 | } |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 6071 | if (!audioserver_flags::portid_volume_management()) { |
| 6072 | track->processMuteEvent_l(mAfThreadCallback->getOrCreateAudioManager(), |
| 6073 | /*muteState=*/{masterVolume == 0.f, |
| 6074 | mStreamTypes[track->streamType()].volume == 0.f, |
| 6075 | mStreamTypes[track->streamType()].mute, |
| 6076 | track->isPlaybackRestricted(), |
| 6077 | vlf == 0.f && vrf == 0.f, |
Vlad Popa | 1e865e6 | 2024-08-15 19:11:42 -0700 | [diff] [blame] | 6078 | vh == 0.f, |
| 6079 | /*muteFromPortVolume=*/false}); |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 6080 | } else { |
| 6081 | track->processMuteEvent_l(mAfThreadCallback->getOrCreateAudioManager(), |
| 6082 | /*muteState=*/{masterVolume == 0.f, |
| 6083 | track->getPortVolume() == 0.f, |
| 6084 | /* muteFromStreamMuted= */ false, |
| 6085 | track->isPlaybackRestricted(), |
| 6086 | vlf == 0.f && vrf == 0.f, |
Vlad Popa | 1e865e6 | 2024-08-15 19:11:42 -0700 | [diff] [blame] | 6087 | vh == 0.f, |
| 6088 | track->getPortMute()}); |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 6089 | } |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 6090 | // now apply the master volume and stream type volume and shaper volume |
| 6091 | vlf *= v * vh; |
| 6092 | vrf *= v * vh; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6093 | // 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] | 6094 | // then derive vl and vr as U8.24 versions for the effect chain |
| 6095 | const float scaleto8_24 = MAX_GAIN_INT * MAX_GAIN_INT; |
| 6096 | vl = (uint32_t) (scaleto8_24 * vlf); |
| 6097 | vr = (uint32_t) (scaleto8_24 * vrf); |
| 6098 | // vl and vr are now in U8.24 format |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 6099 | uint16_t sendLevel = proxy->getSendLevel_U4_12(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6100 | // send level comes from shared memory and so may be corrupt |
| 6101 | if (sendLevel > MAX_GAIN_INT) { |
| 6102 | ALOGV("Track send level out of range: %04X", sendLevel); |
| 6103 | sendLevel = MAX_GAIN_INT; |
| 6104 | } |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 6105 | // vaf is represented as [0.0, 1.0] float by rescaling sendLevel |
| 6106 | vaf = v * sendLevel * (1. / MAX_GAIN_INT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6107 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6108 | |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 6109 | if (track->getInternalMute()) { |
| 6110 | vrf = 0.f; |
| 6111 | vlf = 0.f; |
| 6112 | } |
| 6113 | |
Jiabin Huang | 66aa1e3 | 2024-05-13 20:33:29 +0000 | [diff] [blame] | 6114 | track->setFinalVolume(vlf, vrf); |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 6115 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6116 | // Delegate volume control to effect in track effect chain if needed |
Shunkai Yao | f484765 | 2024-01-12 00:25:20 +0000 | [diff] [blame] | 6117 | if (chain != 0 && chain->setVolume(&vl, &vr)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6118 | // Do not ramp volume if volume is controlled by effect |
| 6119 | param = AudioMixer::VOLUME; |
Bryant Liu | b6be7f2 | 2014-06-12 22:02:41 +0800 | [diff] [blame] | 6120 | // Update remaining floating point volume levels |
| 6121 | vlf = (float)vl / (1 << 24); |
| 6122 | vrf = (float)vr / (1 << 24); |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6123 | track->setHasVolumeController(true); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6124 | } else { |
| 6125 | // force no volume ramp when volume controller was just disabled or removed |
| 6126 | // from effect chain to avoid volume spike |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6127 | if (track->hasVolumeController()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6128 | param = AudioMixer::VOLUME; |
| 6129 | } |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6130 | track->setHasVolumeController(false); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6131 | } |
| 6132 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6133 | // XXX: these things DON'T need to be done each time |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6134 | mAudioMixer->setBufferProvider(trackId, track->asExtendedAudioBufferProvider()); |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6135 | mAudioMixer->enable(trackId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6136 | |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6137 | mAudioMixer->setParameter(trackId, param, AudioMixer::VOLUME0, &vlf); |
| 6138 | mAudioMixer->setParameter(trackId, param, AudioMixer::VOLUME1, &vrf); |
| 6139 | mAudioMixer->setParameter(trackId, param, AudioMixer::AUXLEVEL, &vaf); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6140 | mAudioMixer->setParameter( |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6141 | trackId, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6142 | AudioMixer::TRACK, |
| 6143 | AudioMixer::FORMAT, (void *)track->format()); |
| 6144 | mAudioMixer->setParameter( |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6145 | trackId, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6146 | AudioMixer::TRACK, |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 6147 | AudioMixer::CHANNEL_MASK, (void *)(uintptr_t)track->channelMask()); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6148 | |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 6149 | if (mType == SPATIALIZER && !track->isSpatialized()) { |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6150 | mAudioMixer->setParameter( |
| 6151 | trackId, |
| 6152 | AudioMixer::TRACK, |
| 6153 | AudioMixer::MIXER_CHANNEL_MASK, |
| 6154 | (void *)(uintptr_t)(mChannelMask | mHapticChannelMask)); |
| 6155 | } else { |
| 6156 | mAudioMixer->setParameter( |
| 6157 | trackId, |
| 6158 | AudioMixer::TRACK, |
| 6159 | AudioMixer::MIXER_CHANNEL_MASK, |
| 6160 | (void *)(uintptr_t)(mMixerChannelMask | mHapticChannelMask)); |
| 6161 | } |
| 6162 | |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 6163 | // 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] | 6164 | uint32_t maxSampleRate = mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX; |
Andy Hung | 333ab96 | 2019-05-28 20:23:35 -0700 | [diff] [blame] | 6165 | uint32_t reqSampleRate = proxy->getSampleRate(); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 6166 | if (reqSampleRate == 0) { |
| 6167 | reqSampleRate = mSampleRate; |
| 6168 | } else if (reqSampleRate > maxSampleRate) { |
| 6169 | reqSampleRate = maxSampleRate; |
| 6170 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6171 | mAudioMixer->setParameter( |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6172 | trackId, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6173 | AudioMixer::RESAMPLE, |
| 6174 | AudioMixer::SAMPLE_RATE, |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 6175 | (void *)(uintptr_t)reqSampleRate); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 6176 | |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 6177 | mAudioMixer->setParameter( |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6178 | trackId, |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 6179 | AudioMixer::TIMESTRETCH, |
| 6180 | AudioMixer::PLAYBACK_RATE, |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 6181 | // cast away constness for this generic API. |
| 6182 | const_cast<void *>(reinterpret_cast<const void *>(&playbackRate))); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 6183 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 6184 | /* |
| 6185 | * Select the appropriate output buffer for the track. |
| 6186 | * |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 6187 | * Tracks with effects go into their own effects chain buffer |
| 6188 | * and from there into either mEffectBuffer or mSinkBuffer. |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 6189 | * |
| 6190 | * Other tracks can use mMixerBuffer for higher precision |
| 6191 | * channel accumulation. If this buffer is enabled |
| 6192 | * (mMixerBufferEnabled true), then selected tracks will accumulate |
| 6193 | * into it. |
| 6194 | * |
| 6195 | */ |
| 6196 | if (mMixerBufferEnabled |
| 6197 | && (track->mainBuffer() == mSinkBuffer |
| 6198 | || track->mainBuffer() == mMixerBuffer)) { |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 6199 | if (mType == SPATIALIZER && !track->isSpatialized()) { |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6200 | mAudioMixer->setParameter( |
| 6201 | trackId, |
| 6202 | AudioMixer::TRACK, |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 6203 | AudioMixer::MIXER_FORMAT, (void *)mEffectBufferFormat); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6204 | mAudioMixer->setParameter( |
| 6205 | trackId, |
| 6206 | AudioMixer::TRACK, |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 6207 | AudioMixer::MAIN_BUFFER, (void *)mPostSpatializerBuffer); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6208 | } else { |
| 6209 | mAudioMixer->setParameter( |
| 6210 | trackId, |
| 6211 | AudioMixer::TRACK, |
| 6212 | AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat); |
| 6213 | mAudioMixer->setParameter( |
| 6214 | trackId, |
| 6215 | AudioMixer::TRACK, |
| 6216 | AudioMixer::MAIN_BUFFER, (void *)mMixerBuffer); |
| 6217 | // TODO: override track->mainBuffer()? |
| 6218 | mMixerBufferValid = true; |
| 6219 | } |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 6220 | } else { |
| 6221 | mAudioMixer->setParameter( |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6222 | trackId, |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 6223 | AudioMixer::TRACK, |
Andy Hung | 319587b | 2023-05-23 14:01:03 -0700 | [diff] [blame] | 6224 | AudioMixer::MIXER_FORMAT, (void *)AUDIO_FORMAT_PCM_FLOAT); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 6225 | mAudioMixer->setParameter( |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6226 | trackId, |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 6227 | AudioMixer::TRACK, |
| 6228 | AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); |
| 6229 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6230 | mAudioMixer->setParameter( |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6231 | trackId, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6232 | AudioMixer::TRACK, |
| 6233 | AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 6234 | mAudioMixer->setParameter( |
| 6235 | trackId, |
| 6236 | AudioMixer::TRACK, |
| 6237 | AudioMixer::HAPTIC_ENABLED, (void *)(uintptr_t)track->getHapticPlaybackEnabled()); |
Ahmad Khalil | 229466a | 2024-02-05 12:15:30 +0000 | [diff] [blame] | 6238 | const os::HapticScale hapticScale = track->getHapticScale(); |
jiabin | 77270b8 | 2018-12-18 15:41:29 -0800 | [diff] [blame] | 6239 | mAudioMixer->setParameter( |
Ahmad Khalil | 229466a | 2024-02-05 12:15:30 +0000 | [diff] [blame] | 6240 | trackId, |
| 6241 | AudioMixer::TRACK, |
| 6242 | AudioMixer::HAPTIC_SCALE, (void *)&hapticScale); |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6243 | const float hapticMaxAmplitude = track->getHapticMaxAmplitude(); |
Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 6244 | mAudioMixer->setParameter( |
| 6245 | trackId, |
| 6246 | AudioMixer::TRACK, |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6247 | AudioMixer::HAPTIC_MAX_AMPLITUDE, (void *)&hapticMaxAmplitude); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6248 | |
| 6249 | // reset retry count |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6250 | track->retryCount() = kMaxTrackRetries; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6251 | |
| 6252 | // If one track is ready, set the mixer ready if: |
| 6253 | // - the mixer was not ready during previous round OR |
| 6254 | // - no other track is not ready |
| 6255 | if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY || |
| 6256 | mixerStatus != MIXER_TRACKS_ENABLED) { |
| 6257 | mixerStatus = MIXER_TRACKS_READY; |
| 6258 | } |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 6259 | |
| 6260 | // Enable the next few lines to instrument a test for underrun log handling. |
| 6261 | // TODO: Remove when we have a better way of testing the underrun log. |
| 6262 | #if 0 |
| 6263 | static int i; |
| 6264 | if ((++i & 0xf) == 0) { |
| 6265 | deferredOperations.tallyUnderrunFrames(track, 10 /* underrunFrames */); |
| 6266 | } |
| 6267 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6268 | } else { |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 6269 | size_t underrunFrames = 0; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 6270 | if (framesReady < desiredFrames && !track->isStopped() && !track->isPaused()) { |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 6271 | ALOGV("track(%d) underrun, track state %s framesReady(%zu) < framesDesired(%zd)", |
| 6272 | trackId, track->getTrackStateAsString(), framesReady, desiredFrames); |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 6273 | underrunFrames = desiredFrames; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 6274 | } |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 6275 | deferredOperations.tallyUnderrunFrames(track, underrunFrames); |
Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 6276 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6277 | // clear effect chain input buffer if an active track underruns to avoid sending |
| 6278 | // previous audio buffer again to effects |
| 6279 | chain = getEffectChain_l(track->sessionId()); |
| 6280 | if (chain != 0) { |
| 6281 | chain->clearInputBuffer(); |
| 6282 | } |
| 6283 | |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6284 | 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] | 6285 | if ((track->sharedBuffer() != 0) || track->isTerminated() || |
| 6286 | track->isStopped() || track->isPaused()) { |
| 6287 | // We have consumed all the buffers of this track. |
| 6288 | // Remove it from the list of active tracks. |
| 6289 | // TODO: use actual buffer filling status instead of latency when available from |
| 6290 | // audio HAL |
| 6291 | size_t audioHALFrames = (latency_l() * mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 6292 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6293 | if (mStandby || track->presentationComplete(framesWritten, audioHALFrames)) { |
| 6294 | if (track->isStopped()) { |
| 6295 | track->reset(); |
| 6296 | } |
| 6297 | tracksToRemove->add(track); |
| 6298 | } |
| 6299 | } else { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6300 | // No buffers for this track. Give it a few chances to |
| 6301 | // fill a buffer, then remove it from active list. |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6302 | if (--(track->retryCount()) <= 0) { |
Eric Laurent | 022a513 | 2024-04-12 17:02:51 +0000 | [diff] [blame] | 6303 | ALOGI("%s BUFFER TIMEOUT: remove track(%d) from active list due to underrun" |
| 6304 | " on thread %d", __func__, trackId, mId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6305 | tracksToRemove->add(track); |
| 6306 | // indicate to client process that the track was disabled because of underrun; |
| 6307 | // it will then automatically call start() when data is available |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 6308 | track->disable(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6309 | // If one track is not ready, mark the mixer also not ready if: |
| 6310 | // - the mixer was ready during previous round OR |
| 6311 | // - no other track is ready |
| 6312 | } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY || |
| 6313 | mixerStatus != MIXER_TRACKS_READY) { |
| 6314 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 6315 | } |
| 6316 | } |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6317 | mAudioMixer->disable(trackId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6318 | } |
| 6319 | |
| 6320 | } // local variable scope to avoid goto warning |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6321 | |
| 6322 | } |
| 6323 | |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 6324 | if (mHapticChannelMask != AUDIO_CHANNEL_NONE && sq != NULL) { |
| 6325 | // When there is no fast track playing haptic and FastMixer exists, |
| 6326 | // enabling the first FastTrack, which provides mixed data from normal |
| 6327 | // tracks, to play haptic data. |
| 6328 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 6329 | if (fastTrack->mHapticPlaybackEnabled != noFastHapticTrack) { |
| 6330 | fastTrack->mHapticPlaybackEnabled = noFastHapticTrack; |
| 6331 | didModify = true; |
| 6332 | } |
| 6333 | } |
| 6334 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6335 | // Push the new FastMixer state if necessary |
Jing Mike | 537412f | 2023-03-12 11:01:47 +0800 | [diff] [blame] | 6336 | [[maybe_unused]] bool pauseAudioWatchdog = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6337 | if (didModify) { |
| 6338 | state->mFastTracksGen++; |
| 6339 | // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle |
| 6340 | if (kUseFastMixer == FastMixer_Dynamic && |
| 6341 | state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) { |
| 6342 | state->mCommand = FastMixerState::COLD_IDLE; |
| 6343 | state->mColdFutexAddr = &mFastMixerFutex; |
| 6344 | state->mColdGen++; |
| 6345 | mFastMixerFutex = 0; |
| 6346 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 6347 | mNormalSink = mOutputSink; |
| 6348 | } |
| 6349 | // If we go into cold idle, need to wait for acknowledgement |
| 6350 | // so that fast mixer stops doing I/O. |
| 6351 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 6352 | pauseAudioWatchdog = true; |
| 6353 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6354 | } |
| 6355 | if (sq != NULL) { |
| 6356 | sq->end(didModify); |
Andy Hung | f228531 | 2017-02-14 18:31:59 -0800 | [diff] [blame] | 6357 | // No need to block if the FastMixer is in COLD_IDLE as the FastThread |
| 6358 | // is not active. (We BLOCK_UNTIL_ACKED when entering COLD_IDLE |
| 6359 | // when bringing the output sink into standby.) |
| 6360 | // |
| 6361 | // We will get the latest FastMixer state when we come out of COLD_IDLE. |
| 6362 | // |
| 6363 | // This occurs with BT suspend when we idle the FastMixer with |
| 6364 | // active tracks, which may be added or removed. |
Andy Hung | 82f39d6 | 2024-09-30 17:19:14 -0700 | [diff] [blame] | 6365 | { |
| 6366 | audio_utils::mutex::scoped_queue_wait_check queueWaitCheck(mFastMixer->getTid()); |
| 6367 | sq->push(coldIdle ? FastMixerStateQueue::BLOCK_NEVER : block); |
| 6368 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6369 | } |
| 6370 | #ifdef AUDIO_WATCHDOG |
| 6371 | if (pauseAudioWatchdog && mAudioWatchdog != 0) { |
| 6372 | mAudioWatchdog->pause(); |
| 6373 | } |
| 6374 | #endif |
| 6375 | |
| 6376 | // Now perform the deferred reset on fast tracks that have stopped |
| 6377 | while (resetMask != 0) { |
| 6378 | size_t i = __builtin_ctz(resetMask); |
| 6379 | ALOG_ASSERT(i < count); |
| 6380 | resetMask &= ~(1 << i); |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6381 | sp<IAfTrack> track = mActiveTracks[i]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6382 | ALOG_ASSERT(track->isFastTrack() && track->isStopped()); |
| 6383 | track->reset(); |
| 6384 | } |
| 6385 | |
Andy Hung | 80d03d2 | 2018-04-10 10:32:11 -0700 | [diff] [blame] | 6386 | // Track destruction may occur outside of threadLoop once it is removed from active tracks. |
| 6387 | // Ensure the AudioMixer doesn't have a raw "buffer provider" pointer to the track if |
| 6388 | // it ceases to be active, to allow safe removal from the AudioMixer at the start |
| 6389 | // of prepareTracks_l(); this releases any outstanding buffer back to the track. |
| 6390 | // See also the implementation of destroyTrack_l(). |
| 6391 | for (const auto &track : *tracksToRemove) { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6392 | const int trackId = track->id(); |
| 6393 | if (mAudioMixer->exists(trackId)) { // Normal tracks here, fast tracks in FastMixer. |
| 6394 | mAudioMixer->setBufferProvider(trackId, nullptr /* bufferProvider */); |
Andy Hung | 80d03d2 | 2018-04-10 10:32:11 -0700 | [diff] [blame] | 6395 | } |
| 6396 | } |
| 6397 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6398 | // remove all the tracks that need to be... |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6399 | removeTracks_l(*tracksToRemove); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6400 | |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 6401 | if (getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX) != 0 || |
| 6402 | getEffectChain_l(AUDIO_SESSION_OUTPUT_STAGE) != 0) { |
Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 6403 | mEffectBufferValid = true; |
Marco Nelissen | ac30214 | 2014-10-20 13:15:38 -0700 | [diff] [blame] | 6404 | } |
| 6405 | |
| 6406 | if (mEffectBufferValid) { |
Marco Nelissen | 57088b5 | 2014-10-17 16:39:39 -0700 | [diff] [blame] | 6407 | // as long as there are effects we should clear the effects buffer, to avoid |
| 6408 | // passing a non-clean buffer to the effect chain |
| 6409 | memset(mEffectBuffer, 0, mEffectBufferSize); |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 6410 | if (mType == SPATIALIZER) { |
| 6411 | memset(mPostSpatializerBuffer, 0, mPostSpatializerBufferSize); |
| 6412 | } |
Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 6413 | } |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 6414 | // sink or mix buffer must be cleared if all tracks are connected to an |
| 6415 | // effect chain as in this case the mixer will not write to the sink or mix buffer |
| 6416 | // and track effects will accumulate into it |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6417 | // always clear sink buffer for spatializer output as the output of the spatializer |
| 6418 | // effect will be accumulated into it |
| 6419 | if ((mBytesRemaining == 0) && (((mixedTracks != 0 && mixedTracks == tracksWithEffect) || |
| 6420 | (mixedTracks == 0 && fastTracks > 0)) || (mType == SPATIALIZER))) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6421 | // FIXME as a performance optimization, should remember previous zero status |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 6422 | if (mMixerBufferValid) { |
| 6423 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 6424 | // TODO: In testing, mSinkBuffer below need not be cleared because |
| 6425 | // the PlaybackThread::threadLoop() copies mMixerBuffer into mSinkBuffer |
| 6426 | // after mixing. |
| 6427 | // |
| 6428 | // To enforce this guarantee: |
| 6429 | // ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || |
| 6430 | // (mixedTracks == 0 && fastTracks > 0)) |
| 6431 | // must imply MIXER_TRACKS_READY. |
| 6432 | // Later, we may clear buffers regardless, and skip much of this logic. |
| 6433 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 6434 | // FIXME as a performance optimization, should remember previous zero status |
Andy Hung | 5567aaf | 2014-07-17 14:00:07 -0700 | [diff] [blame] | 6435 | memset(mSinkBuffer, 0, mNormalFrameCount * mFrameSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6436 | } |
| 6437 | |
| 6438 | // if any fast tracks, then status is ready |
| 6439 | mMixerStatusIgnoringFastTracks = mixerStatus; |
| 6440 | if (fastTracks > 0) { |
| 6441 | mixerStatus = MIXER_TRACKS_READY; |
| 6442 | } |
| 6443 | return mixerStatus; |
| 6444 | } |
| 6445 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 6446 | // trackCountForUid_l() must be called with ThreadBase::mutex() held |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6447 | uint32_t PlaybackThread::trackCountForUid_l(uid_t uid) const |
Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 6448 | { |
| 6449 | uint32_t trackCount = 0; |
| 6450 | for (size_t i = 0; i < mTracks.size() ; i++) { |
Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 6451 | if (mTracks[i]->uid() == uid) { |
Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 6452 | trackCount++; |
| 6453 | } |
| 6454 | } |
| 6455 | return trackCount; |
| 6456 | } |
| 6457 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6458 | bool PlaybackThread::IsTimestampAdvancing::check(AudioStreamOut* output) |
ziyangch | 8f194f1 | 2021-12-01 13:48:04 -0800 | [diff] [blame] | 6459 | { |
Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 6460 | // Check the timestamp to see if it's advancing once every 150ms. If we check too frequently, we |
| 6461 | // could falsely detect that the frame position has stalled due to underrun because we haven't |
| 6462 | // given the Audio HAL enough time to update. |
| 6463 | const nsecs_t nowNs = systemTime(); |
| 6464 | if (nowNs - mPreviousNs < mMinimumTimeBetweenChecksNs) { |
| 6465 | return mLatchedValue; |
| 6466 | } |
| 6467 | mPreviousNs = nowNs; |
| 6468 | mLatchedValue = false; |
| 6469 | // Determine if the presentation position is still advancing. |
ziyangch | 8f194f1 | 2021-12-01 13:48:04 -0800 | [diff] [blame] | 6470 | uint64_t position = 0; |
| 6471 | struct timespec unused; |
Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 6472 | const status_t ret = output->getPresentationPosition(&position, &unused); |
ziyangch | 8f194f1 | 2021-12-01 13:48:04 -0800 | [diff] [blame] | 6473 | if (ret == NO_ERROR) { |
Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 6474 | if (position != mPreviousPosition) { |
| 6475 | mPreviousPosition = position; |
| 6476 | mLatchedValue = true; |
ziyangch | 8f194f1 | 2021-12-01 13:48:04 -0800 | [diff] [blame] | 6477 | } |
| 6478 | } |
Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 6479 | return mLatchedValue; |
| 6480 | } |
| 6481 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6482 | void PlaybackThread::IsTimestampAdvancing::clear() |
Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 6483 | { |
| 6484 | mLatchedValue = true; |
| 6485 | mPreviousPosition = 0; |
| 6486 | mPreviousNs = 0; |
ziyangch | 8f194f1 | 2021-12-01 13:48:04 -0800 | [diff] [blame] | 6487 | } |
| 6488 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 6489 | // isTrackAllowed_l() must be called with ThreadBase::mutex() held |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6490 | bool MixerThread::isTrackAllowed_l( |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 6491 | audio_channel_mask_t channelMask, audio_format_t format, |
| 6492 | audio_session_t sessionId, uid_t uid) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6493 | { |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 6494 | if (!PlaybackThread::isTrackAllowed_l(channelMask, format, sessionId, uid)) { |
| 6495 | return false; |
Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 6496 | } |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 6497 | // Check validity as we don't call AudioMixer::create() here. |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 6498 | if (!mAudioMixer->isValidFormat(format)) { |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 6499 | ALOGW("%s: invalid format: %#x", __func__, format); |
| 6500 | return false; |
| 6501 | } |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 6502 | if (!mAudioMixer->isValidChannelMask(channelMask)) { |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 6503 | ALOGW("%s: invalid channelMask: %#x", __func__, channelMask); |
| 6504 | return false; |
| 6505 | } |
| 6506 | return true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6507 | } |
| 6508 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 6509 | // checkForNewParameter_l() must be called with ThreadBase::mutex() held |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6510 | bool MixerThread::checkForNewParameter_l(const String8& keyValuePair, |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6511 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6512 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6513 | bool reconfig = false; |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6514 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6515 | |
Glenn Kasten | c05b8d7 | 2016-03-24 09:48:17 -0700 | [diff] [blame] | 6516 | AutoPark<FastMixer> park(mFastMixer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6517 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6518 | AudioParameter param = AudioParameter(keyValuePair); |
| 6519 | int value; |
| 6520 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 6521 | reconfig = true; |
| 6522 | } |
| 6523 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Andy Hung | 81994d6 | 2023-07-20 21:44:14 -0700 | [diff] [blame] | 6524 | if (!isValidPcmSinkFormat(static_cast<audio_format_t>(value))) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6525 | status = BAD_VALUE; |
| 6526 | } else { |
| 6527 | // no need to save value, since it's constant |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6528 | reconfig = true; |
| 6529 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6530 | } |
| 6531 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
Andy Hung | 81994d6 | 2023-07-20 21:44:14 -0700 | [diff] [blame] | 6532 | if (!isValidPcmSinkChannelMask(static_cast<audio_channel_mask_t>(value))) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6533 | status = BAD_VALUE; |
| 6534 | } else { |
| 6535 | // no need to save value, since it's constant |
| 6536 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6537 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6538 | } |
| 6539 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 6540 | // do not accept frame count changes if tracks are open as the track buffer |
| 6541 | // size depends on frame count and correct behavior would not be guaranteed |
| 6542 | // if frame count is changed after track creation |
| 6543 | if (!mTracks.isEmpty()) { |
| 6544 | status = INVALID_OPERATION; |
| 6545 | } else { |
| 6546 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6547 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6548 | } |
| 6549 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 6550 | LOG_FATAL("Should not set routing device in MixerThread"); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6551 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6552 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6553 | if (status == NO_ERROR) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6554 | status = mOutput->stream->setParameters(keyValuePair); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6555 | if (!mStandby && status == INVALID_OPERATION) { |
Andy Hung | dda7aed | 2023-03-27 15:53:06 -0700 | [diff] [blame] | 6556 | ALOGW("%s: setParameters failed with keyValuePair %s, entering standby", |
| 6557 | __func__, keyValuePair.c_str()); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 6558 | mOutput->standby(); |
Andy Hung | dda7aed | 2023-03-27 15:53:06 -0700 | [diff] [blame] | 6559 | mThreadMetrics.logEndInterval(); |
| 6560 | mThreadSnapshot.onEnd(); |
| 6561 | setStandby_l(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6562 | mBytesWritten = 0; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6563 | status = mOutput->stream->setParameters(keyValuePair); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6564 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6565 | if (status == NO_ERROR && reconfig) { |
| 6566 | readOutputParameters_l(); |
| 6567 | delete mAudioMixer; |
| 6568 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 6569 | for (const auto &track : mTracks) { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6570 | const int trackId = track->id(); |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 6571 | const status_t createStatus = mAudioMixer->create( |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6572 | trackId, |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6573 | track->channelMask(), |
| 6574 | track->format(), |
| 6575 | track->sessionId()); |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 6576 | ALOGW_IF(createStatus != NO_ERROR, |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6577 | "%s(): AudioMixer cannot create track(%d)" |
| 6578 | " mask %#x, format %#x, sessionId %d", |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 6579 | __func__, |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6580 | trackId, track->channelMask(), track->format(), track->sessionId()); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6581 | } |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 6582 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6583 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6584 | } |
| 6585 | |
Dean Wheatley | 6891810 | 2021-03-19 22:09:19 +1100 | [diff] [blame] | 6586 | return reconfig; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6587 | } |
| 6588 | |
| 6589 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6590 | void MixerThread::dumpInternals_l(int fd, const Vector<String16>& args) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6591 | { |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 6592 | PlaybackThread::dumpInternals_l(fd, args); |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 6593 | dprintf(fd, " Thread throttle time (msecs): %u\n", (uint32_t)mThreadThrottleTimeMs); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 6594 | dprintf(fd, " AudioMixer tracks: %s\n", mAudioMixer->trackNames().c_str()); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 6595 | dprintf(fd, " Master mono: %s\n", mMasterMono ? "on" : "off"); |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 6596 | dprintf(fd, " Master balance: %f (%s)\n", mMasterBalance.load(), |
| 6597 | (hasFastMixer() ? std::to_string(mFastMixer->getMasterBalance()) |
| 6598 | : mBalance.toString()).c_str()); |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 6599 | if (hasFastMixer()) { |
| 6600 | dprintf(fd, " FastMixer thread %p tid=%d", mFastMixer.get(), mFastMixer->getTid()); |
| 6601 | |
| 6602 | // Make a non-atomic copy of fast mixer dump state so it won't change underneath us |
| 6603 | // while we are dumping it. It may be inconsistent, but it won't mutate! |
| 6604 | // This is a large object so we place it on the heap. |
| 6605 | // 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] | 6606 | const std::unique_ptr<FastMixerDumpState> copy = |
| 6607 | std::make_unique<FastMixerDumpState>(mFastMixerDumpState); |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 6608 | copy->dump(fd); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6609 | |
| 6610 | #ifdef STATE_QUEUE_DUMP |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 6611 | // Similar for state queue |
| 6612 | StateQueueObserverDump observerCopy = mStateQueueObserverDump; |
| 6613 | observerCopy.dump(fd); |
| 6614 | StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump; |
| 6615 | mutatorCopy.dump(fd); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6616 | #endif |
| 6617 | |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 6618 | #ifdef AUDIO_WATCHDOG |
| 6619 | if (mAudioWatchdog != 0) { |
| 6620 | // Make a non-atomic copy of audio watchdog dump so it won't change underneath us |
| 6621 | AudioWatchdogDump wdCopy = mAudioWatchdogDump; |
| 6622 | wdCopy.dump(fd); |
| 6623 | } |
| 6624 | #endif |
| 6625 | |
| 6626 | } else { |
| 6627 | dprintf(fd, " No FastMixer\n"); |
| 6628 | } |
Eric Laurent | 90cea10 | 2023-05-15 15:08:27 +0200 | [diff] [blame] | 6629 | |
| 6630 | dprintf(fd, "Bluetooth latency modes are %senabled\n", |
| 6631 | mBluetoothLatencyModesEnabled ? "" : "not "); |
| 6632 | dprintf(fd, "HAL does %ssupport Bluetooth latency modes\n", mOutput != nullptr && |
| 6633 | mOutput->audioHwDev->supportsBluetoothVariableLatency() ? "" : "not "); |
| 6634 | dprintf(fd, "Supported latency modes: %s\n", toString(mSupportedLatencyModes).c_str()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6635 | } |
| 6636 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6637 | uint32_t MixerThread::idleSleepTimeUs() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6638 | { |
| 6639 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2; |
| 6640 | } |
| 6641 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6642 | uint32_t MixerThread::suspendSleepTimeUs() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6643 | { |
| 6644 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
| 6645 | } |
| 6646 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6647 | void MixerThread::cacheParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6648 | { |
| 6649 | PlaybackThread::cacheParameters_l(); |
| 6650 | |
| 6651 | // FIXME: Relaxed timing because of a certain device that can't meet latency |
| 6652 | // Should be reduced to 2x after the vendor fixes the driver issue |
| 6653 | // increase threshold again due to low power audio mode. The way this warning |
| 6654 | // threshold is calculated and its usefulness should be reconsidered anyway. |
| 6655 | maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15; |
| 6656 | } |
| 6657 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6658 | void MixerThread::onHalLatencyModesChanged_l() { |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 6659 | mAfThreadCallback->onSupportedLatencyModesChanged(mId, mSupportedLatencyModes); |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 6660 | } |
| 6661 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6662 | void MixerThread::setHalLatencyMode_l() { |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 6663 | // Only handle latency mode if: |
| 6664 | // - mBluetoothLatencyModesEnabled is true |
| 6665 | // - the HAL supports latency modes |
| 6666 | // - the selected device is Bluetooth LE or A2DP |
| 6667 | if (!mBluetoothLatencyModesEnabled.load() || mSupportedLatencyModes.empty()) { |
| 6668 | return; |
| 6669 | } |
| 6670 | if (mOutDeviceTypeAddrs.size() != 1 |
| 6671 | || !(audio_is_a2dp_out_device(mOutDeviceTypeAddrs[0].mType) |
| 6672 | || audio_is_ble_out_device(mOutDeviceTypeAddrs[0].mType))) { |
| 6673 | return; |
| 6674 | } |
| 6675 | |
| 6676 | audio_latency_mode_t latencyMode = AUDIO_LATENCY_MODE_FREE; |
| 6677 | if (mSupportedLatencyModes.size() == 1) { |
| 6678 | // If the HAL only support one latency mode currently, confirm the choice |
| 6679 | latencyMode = mSupportedLatencyModes[0]; |
| 6680 | } else if (mSupportedLatencyModes.size() > 1) { |
| 6681 | // Request low latency if: |
| 6682 | // - At least one active track is either: |
| 6683 | // - a fast track with gaming usage or |
| 6684 | // - a track with acessibility usage |
| 6685 | for (const auto& track : mActiveTracks) { |
| 6686 | if ((track->isFastTrack() && track->attributes().usage == AUDIO_USAGE_GAME) |
| 6687 | || track->attributes().usage == AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY) { |
| 6688 | latencyMode = AUDIO_LATENCY_MODE_LOW; |
| 6689 | break; |
| 6690 | } |
| 6691 | } |
| 6692 | } |
| 6693 | |
| 6694 | if (latencyMode != mSetLatencyMode) { |
| 6695 | status_t status = mOutput->stream->setLatencyMode(latencyMode); |
| 6696 | ALOGD("%s: thread(%d) setLatencyMode(%s) returned %d", |
| 6697 | __func__, mId, toString(latencyMode).c_str(), status); |
| 6698 | if (status == NO_ERROR) { |
| 6699 | mSetLatencyMode = latencyMode; |
| 6700 | } |
| 6701 | } |
| 6702 | } |
| 6703 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6704 | void MixerThread::updateHalSupportedLatencyModes_l() { |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 6705 | |
| 6706 | if (mOutput == nullptr || mOutput->stream == nullptr) { |
| 6707 | return; |
| 6708 | } |
| 6709 | std::vector<audio_latency_mode_t> latencyModes; |
| 6710 | const status_t status = mOutput->stream->getRecommendedLatencyModes(&latencyModes); |
| 6711 | if (status != NO_ERROR) { |
| 6712 | latencyModes.clear(); |
| 6713 | } |
| 6714 | if (latencyModes != mSupportedLatencyModes) { |
| 6715 | ALOGD("%s: thread(%d) status %d supported latency modes: %s", |
| 6716 | __func__, mId, status, toString(latencyModes).c_str()); |
| 6717 | mSupportedLatencyModes.swap(latencyModes); |
| 6718 | sendHalLatencyModesChangedEvent_l(); |
| 6719 | } |
| 6720 | } |
| 6721 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6722 | status_t MixerThread::getSupportedLatencyModes( |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 6723 | std::vector<audio_latency_mode_t>* modes) { |
| 6724 | if (modes == nullptr) { |
| 6725 | return BAD_VALUE; |
| 6726 | } |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 6727 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 6728 | *modes = mSupportedLatencyModes; |
| 6729 | return NO_ERROR; |
| 6730 | } |
| 6731 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6732 | void MixerThread::onRecommendedLatencyModeChanged( |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 6733 | std::vector<audio_latency_mode_t> modes) { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 6734 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 6735 | if (modes != mSupportedLatencyModes) { |
| 6736 | ALOGD("%s: thread(%d) supported latency modes: %s", |
| 6737 | __func__, mId, toString(modes).c_str()); |
| 6738 | mSupportedLatencyModes.swap(modes); |
| 6739 | sendHalLatencyModesChangedEvent_l(); |
| 6740 | } |
| 6741 | } |
| 6742 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6743 | status_t MixerThread::setBluetoothVariableLatencyEnabled(bool enabled) { |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 6744 | if (mOutput == nullptr || mOutput->audioHwDev == nullptr |
| 6745 | || !mOutput->audioHwDev->supportsBluetoothVariableLatency()) { |
| 6746 | return INVALID_OPERATION; |
| 6747 | } |
| 6748 | mBluetoothLatencyModesEnabled.store(enabled); |
| 6749 | return NO_ERROR; |
| 6750 | } |
| 6751 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6752 | // ---------------------------------------------------------------------------- |
| 6753 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6754 | /* static */ |
| 6755 | sp<IAfPlaybackThread> IAfPlaybackThread::createDirectOutputThread( |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 6756 | const sp<IAfThreadCallback>& afThreadCallback, |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6757 | AudioStreamOut* output, audio_io_handle_t id, bool systemReady, |
| 6758 | const audio_offload_info_t& offloadInfo) { |
| 6759 | return sp<DirectOutputThread>::make( |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 6760 | afThreadCallback, output, id, systemReady, offloadInfo); |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6761 | } |
| 6762 | |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 6763 | DirectOutputThread::DirectOutputThread(const sp<IAfThreadCallback>& afThreadCallback, |
Gareth Fenn | b18c1a3 | 2022-10-05 13:42:36 -0700 | [diff] [blame] | 6764 | AudioStreamOut* output, audio_io_handle_t id, ThreadBase::type_t type, bool systemReady, |
| 6765 | const audio_offload_info_t& offloadInfo) |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 6766 | : PlaybackThread(afThreadCallback, output, id, type, systemReady) |
Gareth Fenn | b18c1a3 | 2022-10-05 13:42:36 -0700 | [diff] [blame] | 6767 | , mOffloadInfo(offloadInfo) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6768 | { |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 6769 | setMasterBalance(afThreadCallback->getMasterBalance_l()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6770 | } |
| 6771 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6772 | DirectOutputThread::~DirectOutputThread() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6773 | { |
| 6774 | } |
| 6775 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6776 | void DirectOutputThread::dumpInternals_l(int fd, const Vector<String16>& args) |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 6777 | { |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 6778 | PlaybackThread::dumpInternals_l(fd, args); |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 6779 | dprintf(fd, " Master balance: %f Left: %f Right: %f\n", |
| 6780 | mMasterBalance.load(), mMasterBalanceLeft, mMasterBalanceRight); |
| 6781 | } |
| 6782 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6783 | void DirectOutputThread::setMasterBalance(float balance) |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 6784 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 6785 | audio_utils::lock_guard _l(mutex()); |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 6786 | if (mMasterBalance != balance) { |
| 6787 | mMasterBalance.store(balance); |
| 6788 | mBalance.computeStereoBalance(balance, &mMasterBalanceLeft, &mMasterBalanceRight); |
| 6789 | broadcast_l(); |
| 6790 | } |
| 6791 | } |
| 6792 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6793 | void DirectOutputThread::processVolume_l(IAfTrack* track, bool lastTrack) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6794 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6795 | float left, right; |
| 6796 | |
Andy Hung | 333ab96 | 2019-05-28 20:23:35 -0700 | [diff] [blame] | 6797 | // Ensure volumeshaper state always advances even when muted. |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6798 | const sp<AudioTrackServerProxy> proxy = track->audioTrackServerProxy(); |
Andy Hung | 398ffa2 | 2022-12-13 19:19:53 -0800 | [diff] [blame] | 6799 | |
Andy Hung | 398ffa2 | 2022-12-13 19:19:53 -0800 | [diff] [blame] | 6800 | const int64_t frames = mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]; |
| 6801 | const int64_t time = mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; |
| 6802 | |
Dean Wheatley | b11b002 | 2023-09-12 04:07:35 +1000 | [diff] [blame] | 6803 | ALOGVV("%s: Direct/Offload bufferConsumed:%zu timestamp frames:%lld time:%lld", |
| 6804 | __func__, proxy->framesReleased(), (long long)frames, (long long)time); |
Andy Hung | 398ffa2 | 2022-12-13 19:19:53 -0800 | [diff] [blame] | 6805 | |
| 6806 | const int64_t volumeShaperFrames = |
| 6807 | mMonotonicFrameCounter.updateAndGetMonotonicFrameCount(frames, time); |
| 6808 | const auto [shaperVolume, shaperActive] = |
| 6809 | track->getVolumeHandler()->getVolume(volumeShaperFrames); |
Andy Hung | 333ab96 | 2019-05-28 20:23:35 -0700 | [diff] [blame] | 6810 | mVolumeShaperActive = shaperActive; |
| 6811 | |
Vlad Popa | e2f5aef | 2022-07-25 16:00:20 +0200 | [diff] [blame] | 6812 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); |
| 6813 | left = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 6814 | right = float_from_gain(gain_minifloat_unpack_right(vlr)); |
| 6815 | |
| 6816 | const bool clientVolumeMute = (left == 0.f && right == 0.f); |
| 6817 | |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 6818 | if (!audioserver_flags::portid_volume_management()) { |
| 6819 | if (mMasterMute || mStreamTypes[track->streamType()].mute || |
| 6820 | track->isPlaybackRestricted()) { |
| 6821 | left = right = 0; |
| 6822 | } else { |
| 6823 | float typeVolume = mStreamTypes[track->streamType()].volume; |
| 6824 | const float v = mMasterVolume * typeVolume * shaperVolume; |
Eric Laurent | 277a37e | 2024-07-29 18:37:52 +0000 | [diff] [blame] | 6825 | |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 6826 | if (left > GAIN_FLOAT_UNITY) { |
| 6827 | left = GAIN_FLOAT_UNITY; |
| 6828 | } |
| 6829 | if (right > GAIN_FLOAT_UNITY) { |
| 6830 | right = GAIN_FLOAT_UNITY; |
| 6831 | } |
| 6832 | left *= v; |
| 6833 | right *= v; |
| 6834 | if (mAfThreadCallback->getMode() != AUDIO_MODE_IN_COMMUNICATION |
Pechetty Sravani (xWF) | 2e077f0 | 2024-08-27 01:46:20 +0000 | [diff] [blame] | 6835 | || audio_channel_count_from_out_mask(mChannelMask) > 1) { |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 6836 | left *= mMasterBalanceLeft; // DirectOutputThread balance applied as track volume |
| 6837 | right *= mMasterBalanceRight; |
| 6838 | } |
Pechetty Sravani (xWF) | 2e077f0 | 2024-08-27 01:46:20 +0000 | [diff] [blame] | 6839 | } |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 6840 | track->processMuteEvent_l(mAfThreadCallback->getOrCreateAudioManager(), |
| 6841 | /*muteState=*/{mMasterMute, |
| 6842 | mStreamTypes[track->streamType()].volume == 0.f, |
| 6843 | mStreamTypes[track->streamType()].mute, |
| 6844 | track->isPlaybackRestricted(), |
| 6845 | clientVolumeMute, |
Vlad Popa | 1e865e6 | 2024-08-15 19:11:42 -0700 | [diff] [blame] | 6846 | shaperVolume == 0.f, |
| 6847 | /*muteFromPortVolume=*/false}); |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 6848 | } else { |
| 6849 | if (mMasterMute || track->isPlaybackRestricted()) { |
| 6850 | left = right = 0; |
| 6851 | } else { |
| 6852 | float typeVolume = track->getPortVolume(); |
| 6853 | const float v = mMasterVolume * typeVolume * shaperVolume; |
Liana Kazanova (xWF) | d3e99d2 | 2024-08-23 22:15:51 +0000 | [diff] [blame] | 6854 | |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 6855 | if (left > GAIN_FLOAT_UNITY) { |
| 6856 | left = GAIN_FLOAT_UNITY; |
| 6857 | } |
| 6858 | if (right > GAIN_FLOAT_UNITY) { |
| 6859 | right = GAIN_FLOAT_UNITY; |
| 6860 | } |
| 6861 | left *= v; |
| 6862 | right *= v; |
| 6863 | if (mAfThreadCallback->getMode() != AUDIO_MODE_IN_COMMUNICATION |
| 6864 | || audio_channel_count_from_out_mask(mChannelMask) > 1) { |
| 6865 | left *= mMasterBalanceLeft; // DirectOutputThread balance applied as track volume |
| 6866 | right *= mMasterBalanceRight; |
| 6867 | } |
| 6868 | } |
| 6869 | track->processMuteEvent_l(mAfThreadCallback->getOrCreateAudioManager(), |
| 6870 | /*muteState=*/{mMasterMute, |
| 6871 | track->getPortVolume() == 0.f, |
| 6872 | /* muteFromStreamMuted= */ false, |
| 6873 | track->isPlaybackRestricted(), |
| 6874 | clientVolumeMute, |
Vlad Popa | 1e865e6 | 2024-08-15 19:11:42 -0700 | [diff] [blame] | 6875 | shaperVolume == 0.f, |
| 6876 | track->getPortMute()}); |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 6877 | } |
Pechetty Sravani (xWF) | 2e077f0 | 2024-08-27 01:46:20 +0000 | [diff] [blame] | 6878 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6879 | if (lastTrack) { |
jiabin | 76d9469 | 2022-12-15 21:51:21 +0000 | [diff] [blame] | 6880 | track->setFinalVolume(left, right); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6881 | if (left != mLeftVolFloat || right != mRightVolFloat) { |
| 6882 | mLeftVolFloat = left; |
| 6883 | mRightVolFloat = right; |
| 6884 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6885 | // Delegate volume control to effect in track effect chain if needed |
| 6886 | // only one effect chain can be present on DirectOutputThread, so if |
| 6887 | // there is one, the track is connected to it |
| 6888 | if (!mEffectChains.isEmpty()) { |
Tomoharu Kasahara | 1990bd4 | 2014-12-12 14:04:11 +0900 | [diff] [blame] | 6889 | // if effect chain exists, volume is handled by it. |
| 6890 | // Convert volumes from float to 8.24 |
| 6891 | uint32_t vl = (uint32_t)(left * (1 << 24)); |
| 6892 | uint32_t vr = (uint32_t)(right * (1 << 24)); |
Shunkai Yao | f484765 | 2024-01-12 00:25:20 +0000 | [diff] [blame] | 6893 | // Direct/Offload effect chains set output volume in setVolume(). |
| 6894 | (void)mEffectChains[0]->setVolume(&vl, &vr); |
Tomoharu Kasahara | 1990bd4 | 2014-12-12 14:04:11 +0900 | [diff] [blame] | 6895 | } else { |
| 6896 | // otherwise we directly set the volume. |
| 6897 | setVolumeForOutput_l(left, right); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6898 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6899 | } |
| 6900 | } |
| 6901 | } |
| 6902 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6903 | void DirectOutputThread::onAddNewTrack_l() |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 6904 | { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6905 | sp<IAfTrack> previousTrack = mPreviousTrack.promote(); |
| 6906 | sp<IAfTrack> latestTrack = mActiveTracks.getLatest(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 6907 | |
Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 6908 | if (previousTrack != 0 && latestTrack != 0) { |
| 6909 | if (mType == DIRECT) { |
| 6910 | if (previousTrack.get() != latestTrack.get()) { |
| 6911 | mFlushPending = true; |
| 6912 | } |
| 6913 | } else /* mType == OFFLOAD */ { |
Dean Wheatley | 0f51fd0 | 2022-08-31 16:41:40 +1000 | [diff] [blame] | 6914 | if (previousTrack->sessionId() != latestTrack->sessionId() || |
| 6915 | previousTrack->isFlushPending()) { |
Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 6916 | mFlushPending = true; |
| 6917 | } |
| 6918 | } |
Revathi Uddaraju | 20413a9 | 2016-10-13 17:16:14 +0800 | [diff] [blame] | 6919 | } else if (previousTrack == 0) { |
| 6920 | // there could be an old track added back during track transition for direct |
| 6921 | // output, so always issues flush to flush data of the previous track if it |
| 6922 | // was already destroyed with HAL paused, then flush can resume the playback |
| 6923 | mFlushPending = true; |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 6924 | } |
| 6925 | PlaybackThread::onAddNewTrack_l(); |
| 6926 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6927 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6928 | PlaybackThread::mixer_state DirectOutputThread::prepareTracks_l( |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6929 | Vector<sp<IAfTrack>>* tracksToRemove |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6930 | ) |
| 6931 | { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 6932 | size_t count = mActiveTracks.size(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6933 | mixer_state mixerStatus = MIXER_IDLE; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6934 | bool doHwPause = false; |
| 6935 | bool doHwResume = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6936 | |
| 6937 | // find out which tracks need to be processed |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6938 | for (const sp<IAfTrack>& t : mActiveTracks) { |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 6939 | if (t->isInvalid()) { |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 6940 | ALOGW("An invalidated track shouldn't be in active list"); |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 6941 | tracksToRemove->add(t); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 6942 | continue; |
| 6943 | } |
| 6944 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6945 | IAfTrack* const track = t.get(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 6946 | #ifdef VERY_VERY_VERBOSE_LOGGING |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6947 | audio_track_cblk_t* cblk = track->cblk(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 6948 | #endif |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 6949 | // Only consider last track started for volume and mixer state control. |
| 6950 | // In theory an older track could underrun and restart after the new one starts |
| 6951 | // but as we only care about the transition phase between two tracks on a |
| 6952 | // direct output, it is not a problem to ignore the underrun case. |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6953 | sp<IAfTrack> l = mActiveTracks.getLatest(); |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 6954 | bool last = l.get() == track; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6955 | |
Kuowei Li | 2366647 | 2021-01-20 10:23:25 +0800 | [diff] [blame] | 6956 | if (track->isPausePending()) { |
| 6957 | track->pauseAck(); |
| 6958 | // It is possible a track might have been flushed or stopped. |
| 6959 | // Other operations such as flush pending might occur on the next prepare. |
| 6960 | if (track->isPausing()) { |
| 6961 | track->setPaused(); |
| 6962 | } |
| 6963 | // Always perform pause, as an immediate flush will change |
| 6964 | // the pause state to be no longer isPausing(). |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 6965 | if (mHwSupportsPause && last && !mHwPaused) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6966 | doHwPause = true; |
| 6967 | mHwPaused = true; |
| 6968 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6969 | } else if (track->isFlushPending()) { |
| 6970 | track->flushAck(); |
| 6971 | if (last) { |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 6972 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6973 | } |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 6974 | } else if (track->isResumePending()) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6975 | track->resumeAck(); |
Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 6976 | if (last) { |
| 6977 | mLeftVolFloat = mRightVolFloat = -1.0; |
| 6978 | if (mHwPaused) { |
| 6979 | doHwResume = true; |
| 6980 | mHwPaused = false; |
| 6981 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6982 | } |
| 6983 | } |
| 6984 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6985 | // The first time a track is added we wait |
Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 6986 | // for all its buffers to be filled before processing it. |
| 6987 | // Allow draining the buffer in case the client |
| 6988 | // app does not call stop() and relies on underrun to stop: |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6989 | // hence the test on (track->retryCount() > 1). |
| 6990 | // 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] | 6991 | // so we accept any nonzero amount of data delivered by the AudioTrack (which will |
| 6992 | // reset the retry counter). |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 6993 | // Do not use a high threshold for compressed audio. |
Andy Hung | 455982f | 2021-04-27 17:46:12 -0700 | [diff] [blame] | 6994 | |
| 6995 | // target retry count that we will use is based on the time we wait for retries. |
| 6996 | const int32_t targetRetryCount = kMaxTrackRetriesDirectMs * 1000 / mActiveSleepTimeUs; |
| 6997 | // the retry threshold is when we accept any size for PCM data. This is slightly |
| 6998 | // smaller than the retry count so we can push small bits of data without a glitch. |
| 6999 | const int32_t retryThreshold = targetRetryCount > 2 ? targetRetryCount - 1 : 1; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7000 | uint32_t minFrames; |
Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 7001 | if ((track->sharedBuffer() == 0) && !track->isStopping_1() && !track->isPausing() |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7002 | && (track->retryCount() > retryThreshold) && audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7003 | minFrames = mNormalFrameCount; |
| 7004 | } else { |
| 7005 | minFrames = 1; |
| 7006 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7007 | |
Mikhail Naganov | ddb07bc | 2019-08-15 20:18:47 -0700 | [diff] [blame] | 7008 | const size_t framesReady = track->framesReady(); |
| 7009 | const int trackId = track->id(); |
| 7010 | if (ATRACE_ENABLED()) { |
| 7011 | std::string traceName("nRdy"); |
| 7012 | traceName += std::to_string(trackId); |
| 7013 | ATRACE_INT(traceName.c_str(), framesReady); |
| 7014 | } |
| 7015 | if ((framesReady >= minFrames) && track->isReady() && !track->isPaused() && |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 7016 | !track->isStopping_2() && !track->isStopped()) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7017 | { |
Mikhail Naganov | ddb07bc | 2019-08-15 20:18:47 -0700 | [diff] [blame] | 7018 | ALOGVV("track(%d) s=%08x [OK]", trackId, cblk->mServer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7019 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7020 | if (track->fillingStatus() == IAfTrack::FS_FILLED) { |
| 7021 | track->fillingStatus() = IAfTrack::FS_ACTIVE; |
Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 7022 | if (last) { |
| 7023 | // make sure processVolume_l() will apply new volume even if 0 |
| 7024 | mLeftVolFloat = mRightVolFloat = -1.0; |
| 7025 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7026 | if (!mHwSupportsPause) { |
| 7027 | track->resumeAck(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7028 | } |
| 7029 | } |
| 7030 | |
| 7031 | // compute volume for this track |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7032 | processVolume_l(track, last); |
| 7033 | if (last) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7034 | sp<IAfTrack> previousTrack = mPreviousTrack.promote(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 7035 | if (previousTrack != 0) { |
| 7036 | if (track != previousTrack.get()) { |
| 7037 | // Flush any data still being written from last track |
| 7038 | mBytesRemaining = 0; |
Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 7039 | // Invalidate previous track to force a seek when resuming. |
| 7040 | previousTrack->invalidate(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 7041 | } |
| 7042 | } |
| 7043 | mPreviousTrack = track; |
| 7044 | |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 7045 | // reset retry count |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7046 | track->retryCount() = targetRetryCount; |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 7047 | mActiveTrack = t; |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 7048 | mixerStatus = MIXER_TRACKS_READY; |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 7049 | if (mHwPaused) { |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 7050 | doHwResume = true; |
| 7051 | mHwPaused = false; |
| 7052 | } |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 7053 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7054 | } else { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 7055 | // clear effect chain input buffer if the last active track started underruns |
| 7056 | // to avoid sending previous audio buffer again to effects |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 7057 | if (!mEffectChains.isEmpty() && last) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7058 | mEffectChains[0]->clearInputBuffer(); |
| 7059 | } |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 7060 | if (track->isStopping_1()) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7061 | track->setState(IAfTrackBase::STOPPING_2); |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 7062 | if (last && mHwPaused) { |
| 7063 | doHwResume = true; |
| 7064 | mHwPaused = false; |
| 7065 | } |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 7066 | } |
| 7067 | if ((track->sharedBuffer() != 0) || track->isStopped() || |
| 7068 | track->isStopping_2() || track->isPaused()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7069 | // We have consumed all the buffers of this track. |
| 7070 | // Remove it from the list of active tracks. |
Atneya Nair | 0cae043 | 2022-05-10 18:12:12 -0400 | [diff] [blame] | 7071 | bool presComplete = false; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 7072 | if (mStandby || !last || |
Atneya Nair | 0cae043 | 2022-05-10 18:12:12 -0400 | [diff] [blame] | 7073 | (presComplete = track->presentationComplete(latency_l())) || |
Jindong | 32dc26e | 2019-11-11 18:10:01 +0800 | [diff] [blame] | 7074 | track->isPaused() || mHwPaused) { |
Atneya Nair | 0cae043 | 2022-05-10 18:12:12 -0400 | [diff] [blame] | 7075 | if (presComplete) { |
| 7076 | mOutput->presentationComplete(); |
| 7077 | } |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 7078 | if (track->isStopping_2()) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7079 | track->setState(IAfTrackBase::STOPPED); |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 7080 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7081 | if (track->isStopped()) { |
| 7082 | track->reset(); |
| 7083 | } |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 7084 | tracksToRemove->add(track); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7085 | } |
| 7086 | } else { |
| 7087 | // No buffers for this track. Give it a few chances to |
| 7088 | // fill a buffer, then remove it from active list. |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 7089 | // Only consider last track started for mixer state control |
Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 7090 | bool isTimestampAdvancing = mIsTimestampAdvancing.check(mOutput); |
Gareth Fenn | b18c1a3 | 2022-10-05 13:42:36 -0700 | [diff] [blame] | 7091 | if (!isTunerStream() // tuner streams remain active in underrun |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7092 | && --(track->retryCount()) <= 0) { |
Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 7093 | if (isTimestampAdvancing) { // HAL is still playing audio, give us more time. |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7094 | track->retryCount() = kMaxTrackRetriesOffload; |
ziyangch | 8f194f1 | 2021-12-01 13:48:04 -0800 | [diff] [blame] | 7095 | } else { |
Eric Laurent | 022a513 | 2024-04-12 17:02:51 +0000 | [diff] [blame] | 7096 | ALOGI("%s BUFFER TIMEOUT: remove track(%d) from active list due to" |
| 7097 | " underrun on thread %d", __func__, trackId, mId); |
ziyangch | 8f194f1 | 2021-12-01 13:48:04 -0800 | [diff] [blame] | 7098 | tracksToRemove->add(track); |
| 7099 | // indicate to client process that the track was disabled because of |
| 7100 | // underrun; it will then automatically call start() when data is available |
| 7101 | track->disable(); |
| 7102 | // only do hw pause when track is going to be removed due to BUFFER TIMEOUT. |
| 7103 | // unlike mixerthread, HAL can be paused for direct output |
| 7104 | ALOGW("pause because of UNDERRUN, framesReady = %zu," |
| 7105 | "minFrames = %u, mFormat = %#x", |
| 7106 | framesReady, minFrames, mFormat); |
| 7107 | if (last && mHwSupportsPause && !mHwPaused && !mStandby) { |
| 7108 | doHwPause = true; |
| 7109 | mHwPaused = true; |
| 7110 | } |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 7111 | } |
Haynes Mathew George | 5c6daae | 2017-01-24 20:06:05 -0800 | [diff] [blame] | 7112 | } else if (last) { |
| 7113 | mixerStatus = MIXER_TRACKS_ENABLED; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7114 | } |
| 7115 | } |
| 7116 | } |
| 7117 | } |
| 7118 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7119 | // 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] | 7120 | if (!mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7121 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 7122 | if (mTracks[i]->isFlushPending()) { |
| 7123 | mTracks[i]->flushAck(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 7124 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7125 | } |
| 7126 | } |
| 7127 | } |
| 7128 | |
| 7129 | // make sure the pause/flush/resume sequence is executed in the right order. |
| 7130 | // If a flush is pending and a track is active but the HW is not paused, force a HW pause |
| 7131 | // before flush and then resume HW. This can happen in case of pause/flush/resume |
| 7132 | // if resume is received before pause is executed. |
| 7133 | if (mHwSupportsPause && !mStandby && |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 7134 | (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7135 | status_t result = mOutput->stream->pause(); |
| 7136 | ALOGE_IF(result != OK, "Error when pausing output stream: %d", result); |
Gareth Fenn | cd1e162 | 2022-10-05 11:45:45 -0700 | [diff] [blame] | 7137 | doHwResume = !doHwPause; // resume if pause is due to flush. |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7138 | } |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 7139 | if (mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7140 | flushHw_l(); |
| 7141 | } |
| 7142 | if (mHwSupportsPause && !mStandby && doHwResume) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7143 | status_t result = mOutput->stream->resume(); |
| 7144 | ALOGE_IF(result != OK, "Error when resuming output stream: %d", result); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7145 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7146 | // remove all the tracks that need to be... |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7147 | removeTracks_l(*tracksToRemove); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7148 | |
| 7149 | return mixerStatus; |
| 7150 | } |
| 7151 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7152 | void DirectOutputThread::threadLoop_mix() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7153 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7154 | size_t frameCount = mFrameCount; |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 7155 | int8_t *curBuf = (int8_t *)mSinkBuffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7156 | // output audio to hardware |
| 7157 | while (frameCount) { |
Glenn Kasten | 34542ac | 2013-06-26 11:29:02 -0700 | [diff] [blame] | 7158 | AudioBufferProvider::Buffer buffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7159 | buffer.frameCount = frameCount; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 7160 | status_t status = mActiveTrack->getNextBuffer(&buffer); |
| 7161 | if (status != NO_ERROR || buffer.raw == NULL) { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 7162 | // no need to pad with 0 for compressed audio |
| 7163 | if (audio_has_proportional_frames(mFormat)) { |
| 7164 | memset(curBuf, 0, frameCount * mFrameSize); |
| 7165 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7166 | break; |
| 7167 | } |
| 7168 | memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); |
| 7169 | frameCount -= buffer.frameCount; |
| 7170 | curBuf += buffer.frameCount * mFrameSize; |
| 7171 | mActiveTrack->releaseBuffer(&buffer); |
| 7172 | } |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 7173 | mCurrentWriteLength = curBuf - (int8_t *)mSinkBuffer; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7174 | mSleepTimeUs = 0; |
| 7175 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7176 | mActiveTrack.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7177 | } |
| 7178 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7179 | void DirectOutputThread::threadLoop_sleepTime() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7180 | { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7181 | // do not write to HAL when paused |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 7182 | if (mHwPaused || (usesHwAvSync() && mStandby)) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7183 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7184 | return; |
| 7185 | } |
Andy Hung | 85ba333 | 2021-04-27 17:40:26 -0700 | [diff] [blame] | 7186 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
| 7187 | mSleepTimeUs = mActiveSleepTimeUs; |
| 7188 | } else { |
| 7189 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7190 | } |
Andy Hung | 85ba333 | 2021-04-27 17:40:26 -0700 | [diff] [blame] | 7191 | // Note: In S or later, we do not write zeroes for |
| 7192 | // linear or proportional PCM direct tracks in underrun. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7193 | } |
| 7194 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7195 | void DirectOutputThread::threadLoop_exit() |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7196 | { |
| 7197 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7198 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7199 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 7200 | if (mTracks[i]->isFlushPending()) { |
| 7201 | mTracks[i]->flushAck(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 7202 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7203 | } |
| 7204 | } |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 7205 | if (mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7206 | flushHw_l(); |
| 7207 | } |
| 7208 | } |
| 7209 | PlaybackThread::threadLoop_exit(); |
| 7210 | } |
| 7211 | |
| 7212 | // must be called with thread mutex locked |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7213 | bool DirectOutputThread::shouldStandby_l() |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7214 | { |
| 7215 | bool trackPaused = false; |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 7216 | bool trackStopped = false; |
Eric Laurent | 022a513 | 2024-04-12 17:02:51 +0000 | [diff] [blame] | 7217 | bool trackDisabled = false; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7218 | |
Eric Laurent | 022a513 | 2024-04-12 17:02:51 +0000 | [diff] [blame] | 7219 | // do not put the HAL in standby when paused. NuPlayer clear the offloaded AudioTrack |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7220 | // after a timeout and we will enter standby then. |
Eric Laurent | 022a513 | 2024-04-12 17:02:51 +0000 | [diff] [blame] | 7221 | // On offload threads, do not enter standby if the main track is still underrunning. |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7222 | if (mTracks.size() > 0) { |
Eric Laurent | 022a513 | 2024-04-12 17:02:51 +0000 | [diff] [blame] | 7223 | const auto& mainTrack = mTracks[mTracks.size() - 1]; |
| 7224 | |
| 7225 | trackPaused = mainTrack->isPaused(); |
| 7226 | trackStopped = mainTrack->isStopped() || mainTrack->state() == IAfTrackBase::IDLE; |
| 7227 | trackDisabled = (mType == OFFLOAD) && mainTrack->isDisabled(); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7228 | } |
| 7229 | |
Eric Laurent | 022a513 | 2024-04-12 17:02:51 +0000 | [diff] [blame] | 7230 | return !mStandby && !(trackPaused || (mHwPaused && !trackStopped) || trackDisabled); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7231 | } |
| 7232 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 7233 | // checkForNewParameter_l() must be called with ThreadBase::mutex() held |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7234 | bool DirectOutputThread::checkForNewParameter_l(const String8& keyValuePair, |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7235 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7236 | { |
| 7237 | bool reconfig = false; |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7238 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7239 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7240 | AudioParameter param = AudioParameter(keyValuePair); |
| 7241 | int value; |
| 7242 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 7243 | LOG_FATAL("Should not set routing device in DirectOutputThread"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7244 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7245 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 7246 | // do not accept frame count changes if tracks are open as the track buffer |
| 7247 | // size depends on frame count and correct behavior would not be garantied |
| 7248 | // if frame count is changed after track creation |
| 7249 | if (!mTracks.isEmpty()) { |
| 7250 | status = INVALID_OPERATION; |
| 7251 | } else { |
| 7252 | reconfig = true; |
| 7253 | } |
| 7254 | } |
| 7255 | if (status == NO_ERROR) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7256 | status = mOutput->stream->setParameters(keyValuePair); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7257 | if (!mStandby && status == INVALID_OPERATION) { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 7258 | mOutput->standby(); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 7259 | if (!mStandby) { |
| 7260 | mThreadMetrics.logEndInterval(); |
Andy Hung | 44d648b | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 7261 | mThreadSnapshot.onEnd(); |
Eric Laurent | 19952e1 | 2023-04-20 10:08:29 +0200 | [diff] [blame] | 7262 | setStandby_l(); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 7263 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7264 | mBytesWritten = 0; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7265 | status = mOutput->stream->setParameters(keyValuePair); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7266 | } |
| 7267 | if (status == NO_ERROR && reconfig) { |
| 7268 | readOutputParameters_l(); |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7269 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7270 | } |
| 7271 | } |
| 7272 | |
Dean Wheatley | 6891810 | 2021-03-19 22:09:19 +1100 | [diff] [blame] | 7273 | return reconfig; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7274 | } |
| 7275 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7276 | uint32_t DirectOutputThread::activeSleepTimeUs() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7277 | { |
| 7278 | uint32_t time; |
Andy Hung | e827325 | 2024-08-07 16:42:42 -0700 | [diff] [blame] | 7279 | if (audio_has_proportional_frames(mFormat) && mType != OFFLOAD) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7280 | time = PlaybackThread::activeSleepTimeUs(); |
| 7281 | } else { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 7282 | time = kDirectMinSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7283 | } |
| 7284 | return time; |
| 7285 | } |
| 7286 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7287 | uint32_t DirectOutputThread::idleSleepTimeUs() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7288 | { |
| 7289 | uint32_t time; |
Andy Hung | e827325 | 2024-08-07 16:42:42 -0700 | [diff] [blame] | 7290 | if (audio_has_proportional_frames(mFormat) && mType != OFFLOAD) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7291 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; |
| 7292 | } else { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 7293 | time = kDirectMinSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7294 | } |
| 7295 | return time; |
| 7296 | } |
| 7297 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7298 | uint32_t DirectOutputThread::suspendSleepTimeUs() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7299 | { |
| 7300 | uint32_t time; |
Andy Hung | e827325 | 2024-08-07 16:42:42 -0700 | [diff] [blame] | 7301 | if (audio_has_proportional_frames(mFormat) && mType != OFFLOAD) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7302 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); |
| 7303 | } else { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 7304 | time = kDirectMinSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7305 | } |
| 7306 | return time; |
| 7307 | } |
| 7308 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7309 | void DirectOutputThread::cacheParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7310 | { |
| 7311 | PlaybackThread::cacheParameters_l(); |
| 7312 | |
| 7313 | // use shorter standby delay as on normal output to release |
| 7314 | // hardware resources as soon as possible |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 7315 | // no delay on outputs with HW A/V sync |
| 7316 | if (usesHwAvSync()) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7317 | mStandbyDelayNs = 0; |
Andy Hung | e827325 | 2024-08-07 16:42:42 -0700 | [diff] [blame] | 7318 | } else if (mType == OFFLOAD) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7319 | mStandbyDelayNs = kOffloadStandbyDelayNs; |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 7320 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7321 | mStandbyDelayNs = microseconds(mActiveSleepTimeUs*2); |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 7322 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7323 | } |
| 7324 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7325 | void DirectOutputThread::flushHw_l() |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 7326 | { |
ziyangch | 8f194f1 | 2021-12-01 13:48:04 -0800 | [diff] [blame] | 7327 | PlaybackThread::flushHw_l(); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 7328 | mOutput->flush(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 7329 | mFlushPending = false; |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 7330 | mTimestampVerifier.discontinuity(discontinuityForStandbyOrFlush()); |
Sampath Shetty | 999f0e8 | 2020-01-15 10:19:06 +1100 | [diff] [blame] | 7331 | mTimestamp.clear(); |
Andy Hung | 398ffa2 | 2022-12-13 19:19:53 -0800 | [diff] [blame] | 7332 | mMonotonicFrameCounter.onFlush(); |
Haofan Wang | 0770bc8 | 2024-10-03 17:37:55 +0000 | [diff] [blame] | 7333 | // We do not reset mHwPaused which is hidden from the Track client. |
| 7334 | // Note: the client track in Tracks.cpp and AudioTrack.cpp |
| 7335 | // has a FLUSHED state but the DirectOutputThread does not; |
| 7336 | // those tracks will continue to show isStopped(). |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 7337 | } |
| 7338 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7339 | int64_t DirectOutputThread::computeWaitTimeNs_l() const { |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 7340 | // If a VolumeShaper is active, we must wake up periodically to update volume. |
| 7341 | const int64_t NS_PER_MS = 1000000; |
| 7342 | return mVolumeShaperActive ? |
| 7343 | kMinNormalSinkBufferSizeMs * NS_PER_MS : PlaybackThread::computeWaitTimeNs_l(); |
| 7344 | } |
| 7345 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7346 | // ---------------------------------------------------------------------------- |
| 7347 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7348 | AsyncCallbackThread::AsyncCallbackThread( |
| 7349 | const wp<PlaybackThread>& playbackThread) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7350 | : Thread(false /*canCallJava*/), |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 7351 | mPlaybackThread(playbackThread), |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7352 | mWriteAckSequence(0), |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 7353 | mDrainSequence(0), |
Mikhail Naganov | bf203ce | 2024-05-23 16:27:59 -0700 | [diff] [blame] | 7354 | mAsyncError(ASYNC_ERROR_NONE) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7355 | { |
| 7356 | } |
| 7357 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7358 | void AsyncCallbackThread::onFirstRef() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7359 | { |
| 7360 | run("Offload Cbk", ANDROID_PRIORITY_URGENT_AUDIO); |
| 7361 | } |
| 7362 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7363 | bool AsyncCallbackThread::threadLoop() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7364 | { |
| 7365 | while (!exitPending()) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7366 | uint32_t writeAckSequence; |
| 7367 | uint32_t drainSequence; |
Mikhail Naganov | bf203ce | 2024-05-23 16:27:59 -0700 | [diff] [blame] | 7368 | AsyncError asyncError; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7369 | |
| 7370 | { |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 7371 | audio_utils::unique_lock _l(mutex()); |
Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 7372 | while (!((mWriteAckSequence & 1) || |
| 7373 | (mDrainSequence & 1) || |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 7374 | mAsyncError || |
Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 7375 | exitPending())) { |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 7376 | mWaitWorkCV.wait(_l); |
Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 7377 | } |
| 7378 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7379 | if (exitPending()) { |
| 7380 | break; |
| 7381 | } |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7382 | ALOGV("AsyncCallbackThread mWriteAckSequence %d mDrainSequence %d", |
| 7383 | mWriteAckSequence, mDrainSequence); |
| 7384 | writeAckSequence = mWriteAckSequence; |
| 7385 | mWriteAckSequence &= ~1; |
| 7386 | drainSequence = mDrainSequence; |
| 7387 | mDrainSequence &= ~1; |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 7388 | asyncError = mAsyncError; |
Mikhail Naganov | bf203ce | 2024-05-23 16:27:59 -0700 | [diff] [blame] | 7389 | mAsyncError = ASYNC_ERROR_NONE; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7390 | } |
| 7391 | { |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7392 | const sp<PlaybackThread> playbackThread = mPlaybackThread.promote(); |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 7393 | if (playbackThread != 0) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7394 | if (writeAckSequence & 1) { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 7395 | playbackThread->resetWriteBlocked(writeAckSequence >> 1); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7396 | } |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7397 | if (drainSequence & 1) { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 7398 | playbackThread->resetDraining(drainSequence >> 1); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7399 | } |
Mikhail Naganov | bf203ce | 2024-05-23 16:27:59 -0700 | [diff] [blame] | 7400 | if (asyncError != ASYNC_ERROR_NONE) { |
| 7401 | playbackThread->onAsyncError(asyncError == ASYNC_ERROR_HARD); |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 7402 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7403 | } |
| 7404 | } |
| 7405 | } |
| 7406 | return false; |
| 7407 | } |
| 7408 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7409 | void AsyncCallbackThread::exit() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7410 | { |
| 7411 | ALOGV("AsyncCallbackThread::exit"); |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7412 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7413 | requestExit(); |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 7414 | mWaitWorkCV.notify_all(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7415 | } |
| 7416 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7417 | void AsyncCallbackThread::setWriteBlocked(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7418 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7419 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7420 | // bit 0 is cleared |
| 7421 | mWriteAckSequence = sequence << 1; |
| 7422 | } |
| 7423 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7424 | void AsyncCallbackThread::resetWriteBlocked() |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7425 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7426 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7427 | // ignore unexpected callbacks |
| 7428 | if (mWriteAckSequence & 2) { |
| 7429 | mWriteAckSequence |= 1; |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 7430 | mWaitWorkCV.notify_one(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7431 | } |
| 7432 | } |
| 7433 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7434 | void AsyncCallbackThread::setDraining(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7435 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7436 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7437 | // bit 0 is cleared |
| 7438 | mDrainSequence = sequence << 1; |
| 7439 | } |
| 7440 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7441 | void AsyncCallbackThread::resetDraining() |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7442 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7443 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7444 | // ignore unexpected callbacks |
| 7445 | if (mDrainSequence & 2) { |
| 7446 | mDrainSequence |= 1; |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 7447 | mWaitWorkCV.notify_one(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7448 | } |
| 7449 | } |
| 7450 | |
Mikhail Naganov | bf203ce | 2024-05-23 16:27:59 -0700 | [diff] [blame] | 7451 | void AsyncCallbackThread::setAsyncError(bool isHardError) |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 7452 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7453 | audio_utils::lock_guard _l(mutex()); |
Mikhail Naganov | bf203ce | 2024-05-23 16:27:59 -0700 | [diff] [blame] | 7454 | mAsyncError = isHardError ? ASYNC_ERROR_HARD : ASYNC_ERROR_SOFT; |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 7455 | mWaitWorkCV.notify_one(); |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 7456 | } |
| 7457 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7458 | |
| 7459 | // ---------------------------------------------------------------------------- |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7460 | |
| 7461 | /* static */ |
| 7462 | sp<IAfPlaybackThread> IAfPlaybackThread::createOffloadThread( |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7463 | const sp<IAfThreadCallback>& afThreadCallback, |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7464 | AudioStreamOut* output, audio_io_handle_t id, bool systemReady, |
| 7465 | const audio_offload_info_t& offloadInfo) { |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7466 | return sp<OffloadThread>::make(afThreadCallback, output, id, systemReady, offloadInfo); |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7467 | } |
| 7468 | |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7469 | OffloadThread::OffloadThread(const sp<IAfThreadCallback>& afThreadCallback, |
Gareth Fenn | b18c1a3 | 2022-10-05 13:42:36 -0700 | [diff] [blame] | 7470 | AudioStreamOut* output, audio_io_handle_t id, bool systemReady, |
| 7471 | const audio_offload_info_t& offloadInfo) |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7472 | : DirectOutputThread(afThreadCallback, output, id, OFFLOAD, systemReady, offloadInfo), |
ziyangch | 8f194f1 | 2021-12-01 13:48:04 -0800 | [diff] [blame] | 7473 | mPausedWriteLength(0), mPausedBytesRemaining(0), mKeepWakeLock(true) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7474 | { |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 7475 | //FIXME: mStandby should be set to true by ThreadBase constructo |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 7476 | mStandby = true; |
Eric Laurent | 6466797 | 2016-03-30 18:19:46 -0700 | [diff] [blame] | 7477 | mKeepWakeLock = property_get_bool("ro.audio.offload_wakelock", true /* default_value */); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7478 | } |
| 7479 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7480 | void OffloadThread::threadLoop_exit() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7481 | { |
| 7482 | if (mFlushPending || mHwPaused) { |
| 7483 | // If a flush is pending or track was paused, just discard buffered data |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 7484 | audio_utils::lock_guard l(mutex()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7485 | flushHw_l(); |
| 7486 | } else { |
| 7487 | mMixerStatus = MIXER_DRAIN_ALL; |
| 7488 | threadLoop_drain(); |
| 7489 | } |
Uday Gupta | 56604aa | 2014-05-13 11:19:17 -0700 | [diff] [blame] | 7490 | if (mUseAsyncWrite) { |
| 7491 | ALOG_ASSERT(mCallbackThread != 0); |
| 7492 | mCallbackThread->exit(); |
| 7493 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7494 | PlaybackThread::threadLoop_exit(); |
| 7495 | } |
| 7496 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7497 | PlaybackThread::mixer_state OffloadThread::prepareTracks_l( |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7498 | Vector<sp<IAfTrack>>* tracksToRemove |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7499 | ) |
| 7500 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7501 | size_t count = mActiveTracks.size(); |
| 7502 | |
| 7503 | mixer_state mixerStatus = MIXER_IDLE; |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 7504 | bool doHwPause = false; |
| 7505 | bool doHwResume = false; |
| 7506 | |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 7507 | ALOGV("OffloadThread::prepareTracks_l active tracks %zu", count); |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 7508 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7509 | // find out which tracks need to be processed |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7510 | for (const sp<IAfTrack>& t : mActiveTracks) { |
| 7511 | IAfTrack* const track = t.get(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 7512 | #ifdef VERY_VERY_VERBOSE_LOGGING |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7513 | audio_track_cblk_t* cblk = track->cblk(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 7514 | #endif |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 7515 | // Only consider last track started for volume and mixer state control. |
| 7516 | // In theory an older track could underrun and restart after the new one starts |
| 7517 | // but as we only care about the transition phase between two tracks on a |
| 7518 | // direct output, it is not a problem to ignore the underrun case. |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7519 | sp<IAfTrack> l = mActiveTracks.getLatest(); |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 7520 | bool last = l.get() == track; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 7521 | |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 7522 | if (track->isInvalid()) { |
| 7523 | ALOGW("An invalidated track shouldn't be in active list"); |
| 7524 | tracksToRemove->add(track); |
| 7525 | continue; |
| 7526 | } |
| 7527 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7528 | if (track->state() == IAfTrackBase::IDLE) { |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 7529 | ALOGW("An idle track shouldn't be in active list"); |
| 7530 | continue; |
| 7531 | } |
| 7532 | |
Kuowei Li | 2366647 | 2021-01-20 10:23:25 +0800 | [diff] [blame] | 7533 | if (track->isPausePending()) { |
| 7534 | track->pauseAck(); |
| 7535 | // It is possible a track might have been flushed or stopped. |
| 7536 | // Other operations such as flush pending might occur on the next prepare. |
| 7537 | if (track->isPausing()) { |
| 7538 | track->setPaused(); |
| 7539 | } |
| 7540 | // Always perform pause if last, as an immediate flush will change |
| 7541 | // the pause state to be no longer isPausing(). |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7542 | if (last) { |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 7543 | if (mHwSupportsPause && !mHwPaused) { |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 7544 | doHwPause = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7545 | mHwPaused = true; |
| 7546 | } |
| 7547 | // If we were part way through writing the mixbuffer to |
| 7548 | // the HAL we must save this until we resume |
| 7549 | // BUG - this will be wrong if a different track is made active, |
| 7550 | // in that case we want to discard the pending data in the |
| 7551 | // mixbuffer and tell the client to present it again when the |
| 7552 | // track is resumed |
| 7553 | mPausedWriteLength = mCurrentWriteLength; |
| 7554 | mPausedBytesRemaining = mBytesRemaining; |
| 7555 | mBytesRemaining = 0; // stop writing |
| 7556 | } |
| 7557 | tracksToRemove->add(track); |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 7558 | } else if (track->isFlushPending()) { |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 7559 | if (track->isStopping_1()) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7560 | track->retryCount() = kMaxTrackStopRetriesOffload; |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 7561 | } else { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7562 | track->retryCount() = kMaxTrackRetriesOffload; |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 7563 | } |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 7564 | track->flushAck(); |
| 7565 | if (last) { |
| 7566 | mFlushPending = true; |
| 7567 | } |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 7568 | } else if (track->isResumePending()){ |
| 7569 | track->resumeAck(); |
| 7570 | if (last) { |
| 7571 | if (mPausedBytesRemaining) { |
| 7572 | // Need to continue write that was interrupted |
| 7573 | mCurrentWriteLength = mPausedWriteLength; |
| 7574 | mBytesRemaining = mPausedBytesRemaining; |
| 7575 | mPausedBytesRemaining = 0; |
| 7576 | } |
| 7577 | if (mHwPaused) { |
| 7578 | doHwResume = true; |
| 7579 | mHwPaused = false; |
| 7580 | // threadLoop_mix() will handle the case that we need to |
| 7581 | // resume an interrupted write |
| 7582 | } |
| 7583 | // enable write to audio HAL |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7584 | mSleepTimeUs = 0; |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 7585 | |
Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 7586 | mLeftVolFloat = mRightVolFloat = -1.0; |
| 7587 | |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 7588 | // Do not handle new data in this iteration even if track->framesReady() |
| 7589 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 7590 | } |
| 7591 | } else if (track->framesReady() && track->isReady() && |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7592 | !track->isPaused() && !track->isTerminated() && !track->isStopping_2()) { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 7593 | ALOGVV("OffloadThread: track(%d) s=%08x [OK]", track->id(), cblk->mServer); |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7594 | if (track->fillingStatus() == IAfTrack::FS_FILLED) { |
| 7595 | track->fillingStatus() = IAfTrack::FS_ACTIVE; |
Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 7596 | if (last) { |
| 7597 | // make sure processVolume_l() will apply new volume even if 0 |
| 7598 | mLeftVolFloat = mRightVolFloat = -1.0; |
| 7599 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7600 | } |
| 7601 | |
| 7602 | if (last) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7603 | sp<IAfTrack> previousTrack = mPreviousTrack.promote(); |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 7604 | if (previousTrack != 0) { |
| 7605 | if (track != previousTrack.get()) { |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 7606 | // Flush any data still being written from last track |
| 7607 | mBytesRemaining = 0; |
| 7608 | if (mPausedBytesRemaining) { |
| 7609 | // Last track was paused so we also need to flush saved |
| 7610 | // mixbuffer state and invalidate track so that it will |
| 7611 | // re-submit that unwritten data when it is next resumed |
| 7612 | mPausedBytesRemaining = 0; |
| 7613 | // Invalidate is a bit drastic - would be more efficient |
| 7614 | // to have a flag to tell client that some of the |
| 7615 | // previously written data was lost |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 7616 | previousTrack->invalidate(); |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 7617 | } |
| 7618 | // flush data already sent to the DSP if changing audio session as audio |
| 7619 | // comes from a different source. Also invalidate previous track to force a |
| 7620 | // seek when resuming. |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 7621 | if (previousTrack->sessionId() != track->sessionId()) { |
| 7622 | previousTrack->invalidate(); |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 7623 | } |
| 7624 | } |
| 7625 | } |
| 7626 | mPreviousTrack = track; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7627 | // reset retry count |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 7628 | if (track->isStopping_1()) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7629 | track->retryCount() = kMaxTrackStopRetriesOffload; |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 7630 | } else { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7631 | track->retryCount() = kMaxTrackRetriesOffload; |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 7632 | } |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 7633 | mActiveTrack = t; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7634 | mixerStatus = MIXER_TRACKS_READY; |
| 7635 | } |
| 7636 | } else { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 7637 | ALOGVV("OffloadThread: track(%d) s=%08x [NOT READY]", track->id(), cblk->mServer); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7638 | if (track->isStopping_1()) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7639 | if (--(track->retryCount()) <= 0) { |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 7640 | // Hardware buffer can hold a large amount of audio so we must |
| 7641 | // wait for all current track's data to drain before we say |
| 7642 | // that the track is stopped. |
| 7643 | if (mBytesRemaining == 0) { |
| 7644 | // Only start draining when all data in mixbuffer |
| 7645 | // has been written |
| 7646 | ALOGV("OffloadThread: underrun and STOPPING_1 -> draining, STOPPING_2"); |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7647 | track->setState(IAfTrackBase::STOPPING_2); |
| 7648 | // so presentation completes after |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 7649 | // drain do not drain if no data was ever sent to HAL (mStandby == true) |
| 7650 | if (last && !mStandby) { |
| 7651 | // do not modify drain sequence if we are already draining. This happens |
| 7652 | // when resuming from pause after drain. |
| 7653 | if ((mDrainSequence & 1) == 0) { |
| 7654 | mSleepTimeUs = 0; |
| 7655 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
| 7656 | mixerStatus = MIXER_DRAIN_TRACK; |
| 7657 | mDrainSequence += 2; |
| 7658 | } |
| 7659 | if (mHwPaused) { |
| 7660 | // It is possible to move from PAUSED to STOPPING_1 without |
| 7661 | // a resume so we must ensure hardware is running |
| 7662 | doHwResume = true; |
| 7663 | mHwPaused = false; |
| 7664 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7665 | } |
| 7666 | } |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 7667 | } else if (last) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7668 | ALOGV("stopping1 underrun retries left %d", track->retryCount()); |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 7669 | mixerStatus = MIXER_TRACKS_ENABLED; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7670 | } |
| 7671 | } else if (track->isStopping_2()) { |
Eric Laurent | 6a51d7e | 2013-10-17 18:59:26 -0700 | [diff] [blame] | 7672 | // Drain has completed or we are in standby, signal presentation complete |
| 7673 | if (!(mDrainSequence & 1) || !last || mStandby) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7674 | track->setState(IAfTrackBase::STOPPED); |
Atneya Nair | 0cae043 | 2022-05-10 18:12:12 -0400 | [diff] [blame] | 7675 | mOutput->presentationComplete(); |
| 7676 | track->presentationComplete(latency_l()); // always returns true |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7677 | track->reset(); |
| 7678 | tracksToRemove->add(track); |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 7679 | // OFFLOADED stop resets frame counts. |
Andy Hung | f323451 | 2018-07-03 14:51:47 -0700 | [diff] [blame] | 7680 | if (!mUseAsyncWrite) { |
| 7681 | // If we don't get explicit drain notification we must |
| 7682 | // register discontinuity regardless of whether this is |
| 7683 | // the previous (!last) or the upcoming (last) track |
| 7684 | // to avoid skipping the discontinuity. |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 7685 | mTimestampVerifier.discontinuity( |
| 7686 | mTimestampVerifier.DISCONTINUITY_MODE_ZERO); |
Andy Hung | f323451 | 2018-07-03 14:51:47 -0700 | [diff] [blame] | 7687 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7688 | } |
| 7689 | } else { |
| 7690 | // No buffers for this track. Give it a few chances to |
| 7691 | // fill a buffer, then remove it from active list. |
Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 7692 | bool isTimestampAdvancing = mIsTimestampAdvancing.check(mOutput); |
Gareth Fenn | b18c1a3 | 2022-10-05 13:42:36 -0700 | [diff] [blame] | 7693 | if (!isTunerStream() // tuner streams remain active in underrun |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7694 | && --(track->retryCount()) <= 0) { |
Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 7695 | if (isTimestampAdvancing) { // HAL is still playing audio, give us more time. |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7696 | track->retryCount() = kMaxTrackRetriesOffload; |
Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 7697 | } else { |
Eric Laurent | 022a513 | 2024-04-12 17:02:51 +0000 | [diff] [blame] | 7698 | ALOGI("%s BUFFER TIMEOUT: remove track(%d) from active list due to" |
| 7699 | " underrun on thread %d", __func__, track->id(), mId); |
Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 7700 | tracksToRemove->add(track); |
Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 7701 | // tell client process that the track was disabled because of underrun; |
Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 7702 | // it will then automatically call start() when data is available |
| 7703 | track->disable(); |
| 7704 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7705 | } else if (last){ |
| 7706 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 7707 | } |
| 7708 | } |
| 7709 | } |
| 7710 | // compute volume for this track |
Wenfeng Sun | 7945833 | 2019-05-29 20:12:43 +0800 | [diff] [blame] | 7711 | if (track->isReady()) { // check ready to prevent premature start. |
| 7712 | processVolume_l(track, last); |
| 7713 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7714 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 7715 | |
Eric Laurent | ea0fade | 2013-10-04 16:23:48 -0700 | [diff] [blame] | 7716 | // make sure the pause/flush/resume sequence is executed in the right order. |
| 7717 | // If a flush is pending and a track is active but the HW is not paused, force a HW pause |
| 7718 | // before flush and then resume HW. This can happen in case of pause/flush/resume |
| 7719 | // if resume is received before pause is executed. |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 7720 | if (!mStandby && (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7721 | status_t result = mOutput->stream->pause(); |
| 7722 | ALOGE_IF(result != OK, "Error when pausing output stream: %d", result); |
Gareth Fenn | cd1e162 | 2022-10-05 11:45:45 -0700 | [diff] [blame] | 7723 | doHwResume = !doHwPause; // resume if pause is due to flush. |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 7724 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 7725 | if (mFlushPending) { |
| 7726 | flushHw_l(); |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 7727 | } |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 7728 | if (!mStandby && doHwResume) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7729 | status_t result = mOutput->stream->resume(); |
| 7730 | ALOGE_IF(result != OK, "Error when resuming output stream: %d", result); |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 7731 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 7732 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7733 | // remove all the tracks that need to be... |
| 7734 | removeTracks_l(*tracksToRemove); |
| 7735 | |
| 7736 | return mixerStatus; |
| 7737 | } |
| 7738 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7739 | // must be called with thread mutex locked |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7740 | bool OffloadThread::waitingAsyncCallback_l() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7741 | { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7742 | ALOGVV("waitingAsyncCallback_l mWriteAckSequence %d mDrainSequence %d", |
| 7743 | mWriteAckSequence, mDrainSequence); |
| 7744 | if (mUseAsyncWrite && ((mWriteAckSequence & 1) || (mDrainSequence & 1))) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7745 | return true; |
| 7746 | } |
| 7747 | return false; |
| 7748 | } |
| 7749 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7750 | bool OffloadThread::waitingAsyncCallback() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7751 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7752 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7753 | return waitingAsyncCallback_l(); |
| 7754 | } |
| 7755 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7756 | void OffloadThread::flushHw_l() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7757 | { |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 7758 | DirectOutputThread::flushHw_l(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7759 | // Flush anything still waiting in the mixbuffer |
| 7760 | mCurrentWriteLength = 0; |
| 7761 | mBytesRemaining = 0; |
| 7762 | mPausedWriteLength = 0; |
| 7763 | mPausedBytesRemaining = 0; |
Eric Laurent | 3eaf66b | 2016-04-01 14:44:17 -0700 | [diff] [blame] | 7764 | // reset bytes written count to reflect that DSP buffers are empty after flush. |
| 7765 | mBytesWritten = 0; |
Haynes Mathew George | 0f02f26 | 2014-01-11 13:03:57 -0800 | [diff] [blame] | 7766 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7767 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7768 | // discard any pending drain or write ack by incrementing sequence |
| 7769 | mWriteAckSequence = (mWriteAckSequence + 2) & ~1; |
| 7770 | mDrainSequence = (mDrainSequence + 2) & ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7771 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7772 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
| 7773 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7774 | } |
| 7775 | } |
| 7776 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7777 | void OffloadThread::invalidateTracks(audio_stream_type_t streamType) |
Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 7778 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7779 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 7780 | if (PlaybackThread::invalidateTracks_l(streamType)) { |
| 7781 | mFlushPending = true; |
| 7782 | } |
Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 7783 | } |
| 7784 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7785 | void OffloadThread::invalidateTracks(std::set<audio_port_handle_t>& portIds) { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7786 | audio_utils::lock_guard _l(mutex()); |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 7787 | if (PlaybackThread::invalidateTracks_l(portIds)) { |
| 7788 | mFlushPending = true; |
| 7789 | } |
| 7790 | } |
| 7791 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7792 | // ---------------------------------------------------------------------------- |
| 7793 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7794 | /* static */ |
| 7795 | sp<IAfDuplicatingThread> IAfDuplicatingThread::create( |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7796 | const sp<IAfThreadCallback>& afThreadCallback, |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7797 | IAfPlaybackThread* mainThread, audio_io_handle_t id, bool systemReady) { |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7798 | return sp<DuplicatingThread>::make(afThreadCallback, mainThread, id, systemReady); |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7799 | } |
| 7800 | |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7801 | DuplicatingThread::DuplicatingThread(const sp<IAfThreadCallback>& afThreadCallback, |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 7802 | IAfPlaybackThread* mainThread, audio_io_handle_t id, bool systemReady) |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7803 | : MixerThread(afThreadCallback, mainThread->getOutput(), id, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 7804 | systemReady, DUPLICATING), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7805 | mWaitTimeMs(UINT_MAX) |
| 7806 | { |
| 7807 | addOutputTrack(mainThread); |
| 7808 | } |
| 7809 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7810 | DuplicatingThread::~DuplicatingThread() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7811 | { |
| 7812 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 7813 | mOutputTracks[i]->destroy(); |
| 7814 | } |
| 7815 | } |
| 7816 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7817 | void DuplicatingThread::threadLoop_mix() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7818 | { |
| 7819 | // mix buffers... |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 7820 | if (outputsReady()) { |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 7821 | mAudioMixer->process(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7822 | } else { |
Eric Laurent | 02b5708 | 2014-11-07 17:28:28 -0800 | [diff] [blame] | 7823 | if (mMixerBufferValid) { |
| 7824 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 7825 | } else { |
| 7826 | memset(mSinkBuffer, 0, mSinkBufferSize); |
| 7827 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7828 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7829 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7830 | writeFrames = mNormalFrameCount; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 7831 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7832 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7833 | } |
| 7834 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7835 | void DuplicatingThread::threadLoop_sleepTime() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7836 | { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7837 | if (mSleepTimeUs == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7838 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7839 | mSleepTimeUs = mActiveSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7840 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7841 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7842 | } |
| 7843 | } else if (mBytesWritten != 0) { |
| 7844 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
| 7845 | writeFrames = mNormalFrameCount; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 7846 | memset(mSinkBuffer, 0, mSinkBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7847 | } else { |
| 7848 | // flush remaining overflow buffers in output tracks |
| 7849 | writeFrames = 0; |
| 7850 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7851 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7852 | } |
| 7853 | } |
| 7854 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7855 | ssize_t DuplicatingThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7856 | { |
Jiabin Huang | 4d0c9e8 | 2024-10-28 21:51:36 +0000 | [diff] [blame] | 7857 | ATRACE_BEGIN("write"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7858 | for (size_t i = 0; i < outputTracks.size(); i++) { |
Andy Hung | 1c86ebe | 2018-05-29 20:29:08 -0700 | [diff] [blame] | 7859 | const ssize_t actualWritten = outputTracks[i]->write(mSinkBuffer, writeFrames); |
| 7860 | |
| 7861 | // Consider the first OutputTrack for timestamp and frame counting. |
| 7862 | |
| 7863 | // The threadLoop() generally assumes writing a full sink buffer size at a time. |
| 7864 | // Here, we correct for writeFrames of 0 (a stop) or underruns because |
| 7865 | // we always claim success. |
| 7866 | if (i == 0) { |
| 7867 | const ssize_t correction = mSinkBufferSize / mFrameSize - actualWritten; |
| 7868 | ALOGD_IF(correction != 0 && writeFrames != 0, |
| 7869 | "%s: writeFrames:%u actualWritten:%zd correction:%zd mFramesWritten:%lld", |
| 7870 | __func__, writeFrames, actualWritten, correction, (long long)mFramesWritten); |
| 7871 | mFramesWritten -= correction; |
| 7872 | } |
| 7873 | |
| 7874 | // 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] | 7875 | } |
Jiabin Huang | 4d0c9e8 | 2024-10-28 21:51:36 +0000 | [diff] [blame] | 7876 | ATRACE_END(); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 7877 | if (mStandby) { |
| 7878 | mThreadMetrics.logBeginInterval(); |
Andy Hung | 44d648b | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 7879 | mThreadSnapshot.onBegin(); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 7880 | mStandby = false; |
| 7881 | } |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 7882 | return (ssize_t)mSinkBufferSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7883 | } |
| 7884 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7885 | void DuplicatingThread::threadLoop_standby() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7886 | { |
| 7887 | // DuplicatingThread implements standby by stopping all tracks |
| 7888 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 7889 | outputTracks[i]->stop(); |
| 7890 | } |
| 7891 | } |
| 7892 | |
Andy Hung | 8a5abfd | 2023-12-07 19:35:12 -0800 | [diff] [blame] | 7893 | void DuplicatingThread::threadLoop_exit() |
| 7894 | { |
| 7895 | // Prevent calling the OutputTrack dtor in the DuplicatingThread dtor |
| 7896 | // where other mutexes (i.e. AudioPolicyService_Mutex) may be held. |
| 7897 | // Do so here in the threadLoop_exit(). |
| 7898 | |
| 7899 | SortedVector <sp<IAfOutputTrack>> localTracks; |
| 7900 | { |
| 7901 | audio_utils::lock_guard l(mutex()); |
| 7902 | localTracks = std::move(mOutputTracks); |
| 7903 | mOutputTracks.clear(); |
jiabin | c62d603 | 2024-09-03 23:39:57 +0000 | [diff] [blame] | 7904 | for (size_t i = 0; i < localTracks.size(); ++i) { |
| 7905 | localTracks[i]->destroy(); |
| 7906 | } |
Andy Hung | 8a5abfd | 2023-12-07 19:35:12 -0800 | [diff] [blame] | 7907 | } |
| 7908 | localTracks.clear(); |
| 7909 | outputTracks.clear(); |
| 7910 | PlaybackThread::threadLoop_exit(); |
| 7911 | } |
| 7912 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7913 | void DuplicatingThread::dumpInternals_l(int fd, const Vector<String16>& args) |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 7914 | { |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 7915 | MixerThread::dumpInternals_l(fd, args); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 7916 | |
| 7917 | std::stringstream ss; |
| 7918 | const size_t numTracks = mOutputTracks.size(); |
| 7919 | ss << " " << numTracks << " OutputTracks"; |
| 7920 | if (numTracks > 0) { |
| 7921 | ss << ":"; |
| 7922 | for (const auto &track : mOutputTracks) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 7923 | const auto thread = track->thread().promote(); |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 7924 | ss << " (" << track->id() << " : "; |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 7925 | if (thread.get() != nullptr) { |
| 7926 | ss << thread.get() << ", " << thread->id(); |
| 7927 | } else { |
| 7928 | ss << "null"; |
| 7929 | } |
| 7930 | ss << ")"; |
| 7931 | } |
| 7932 | } |
| 7933 | ss << "\n"; |
| 7934 | std::string result = ss.str(); |
| 7935 | write(fd, result.c_str(), result.size()); |
| 7936 | } |
| 7937 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7938 | void DuplicatingThread::saveOutputTracks() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7939 | { |
| 7940 | outputTracks = mOutputTracks; |
| 7941 | } |
| 7942 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7943 | void DuplicatingThread::clearOutputTracks() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7944 | { |
| 7945 | outputTracks.clear(); |
| 7946 | } |
| 7947 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7948 | void DuplicatingThread::addOutputTrack(IAfPlaybackThread* thread) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7949 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7950 | audio_utils::lock_guard _l(mutex()); |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 7951 | // The downstream MixerThread consumes thread->frameCount() amount of frames per mix pass. |
| 7952 | // Adjust for thread->sampleRate() to determine minimum buffer frame count. |
| 7953 | // Then triple buffer because Threads do not run synchronously and may not be clock locked. |
| 7954 | const size_t frameCount = |
| 7955 | 3 * sourceFramesNeeded(mSampleRate, thread->frameCount(), thread->sampleRate()); |
| 7956 | // TODO: Consider asynchronous sample rate conversion to handle clock disparity |
| 7957 | // from different OutputTracks and their associated MixerThreads (e.g. one may |
| 7958 | // nearly empty and the other may be dropping data). |
| 7959 | |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 7960 | // TODO b/182392769: use attribution source util, move to server edge |
| 7961 | AttributionSourceState attributionSource = AttributionSourceState(); |
| 7962 | attributionSource.uid = VALUE_OR_FATAL(legacy2aidl_uid_t_int32_t( |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 7963 | IPCThreadState::self()->getCallingUid())); |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 7964 | attributionSource.pid = VALUE_OR_FATAL(legacy2aidl_pid_t_int32_t( |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 7965 | IPCThreadState::self()->getCallingPid())); |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 7966 | attributionSource.token = sp<BBinder>::make(); |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7967 | sp<IAfOutputTrack> outputTrack = IAfOutputTrack::create(thread, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7968 | this, |
| 7969 | mSampleRate, |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 7970 | mFormat, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7971 | mChannelMask, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 7972 | frameCount, |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 7973 | attributionSource); |
Eric Laurent | af3ec7c | 2016-08-01 11:25:19 -0700 | [diff] [blame] | 7974 | status_t status = outputTrack != 0 ? outputTrack->initCheck() : (status_t) NO_MEMORY; |
| 7975 | if (status != NO_ERROR) { |
| 7976 | ALOGE("addOutputTrack() initCheck failed %d", status); |
| 7977 | return; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7978 | } |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 7979 | if (!audioserver_flags::portid_volume_management()) { |
Vlad Popa | 1e865e6 | 2024-08-15 19:11:42 -0700 | [diff] [blame] | 7980 | thread->setStreamVolume(AUDIO_STREAM_PATCH, /*volume=*/1.0f, /*muted=*/false); |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 7981 | } |
Vlad Popa | 1e865e6 | 2024-08-15 19:11:42 -0700 | [diff] [blame] | 7982 | |
Eric Laurent | af3ec7c | 2016-08-01 11:25:19 -0700 | [diff] [blame] | 7983 | mOutputTracks.add(outputTrack); |
| 7984 | ALOGV("addOutputTrack() track %p, on thread %p", outputTrack.get(), thread); |
| 7985 | updateWaitTime_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7986 | } |
| 7987 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7988 | void DuplicatingThread::removeOutputTrack(IAfPlaybackThread* thread) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7989 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7990 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7991 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 7992 | if (mOutputTracks[i]->thread() == thread) { |
| 7993 | mOutputTracks[i]->destroy(); |
| 7994 | mOutputTracks.removeAt(i); |
| 7995 | updateWaitTime_l(); |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 7996 | // NO_THREAD_SAFETY_ANALYSIS |
| 7997 | // Lambda workaround: as thread != this |
| 7998 | // we can safely call the remote thread getOutput. |
| 7999 | const bool equalOutput = |
| 8000 | [&](){ return thread->getOutput() == mOutput; }(); |
| 8001 | if (equalOutput) { |
| 8002 | mOutput = nullptr; |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 8003 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8004 | return; |
| 8005 | } |
| 8006 | } |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 8007 | ALOGV("removeOutputTrack(): unknown thread: %p", thread); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8008 | } |
| 8009 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 8010 | // caller must hold mutex() |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 8011 | void DuplicatingThread::updateWaitTime_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8012 | { |
| 8013 | mWaitTimeMs = UINT_MAX; |
| 8014 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 8015 | const auto strong = mOutputTracks[i]->thread().promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8016 | if (strong != 0) { |
| 8017 | uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); |
| 8018 | if (waitTimeMs < mWaitTimeMs) { |
| 8019 | mWaitTimeMs = waitTimeMs; |
| 8020 | } |
| 8021 | } |
| 8022 | } |
| 8023 | } |
| 8024 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 8025 | bool DuplicatingThread::outputsReady() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8026 | { |
| 8027 | for (size_t i = 0; i < outputTracks.size(); i++) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 8028 | const auto thread = outputTracks[i]->thread().promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8029 | if (thread == 0) { |
| 8030 | ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", |
| 8031 | outputTracks[i].get()); |
| 8032 | return false; |
| 8033 | } |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 8034 | IAfPlaybackThread* const playbackThread = thread->asIAfPlaybackThread().get(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8035 | // see note at standby() declaration |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 8036 | if (playbackThread->inStandby() && !playbackThread->isSuspended()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8037 | ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), |
| 8038 | thread.get()); |
| 8039 | return false; |
| 8040 | } |
| 8041 | } |
| 8042 | return true; |
| 8043 | } |
| 8044 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 8045 | void DuplicatingThread::sendMetadataToBackend_l( |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 8046 | const StreamOutHalInterface::SourceMetadata& metadata) |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 8047 | { |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 8048 | for (auto& outputTrack : outputTracks) { // not mOutputTracks |
| 8049 | outputTrack->setMetadatas(metadata.tracks); |
| 8050 | } |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 8051 | } |
| 8052 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 8053 | uint32_t DuplicatingThread::activeSleepTimeUs() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8054 | { |
Andy Hung | 7a6a0f0 | 2023-11-29 13:42:08 -0800 | [diff] [blame] | 8055 | // return half the wait time in microseconds. |
| 8056 | return std::min(mWaitTimeMs * 500ULL, (unsigned long long)UINT32_MAX); // prevent overflow. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8057 | } |
| 8058 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 8059 | void DuplicatingThread::cacheParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8060 | { |
| 8061 | // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first |
| 8062 | updateWaitTime_l(); |
| 8063 | |
| 8064 | MixerThread::cacheParameters_l(); |
| 8065 | } |
| 8066 | |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 8067 | // ---------------------------------------------------------------------------- |
| 8068 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 8069 | /* static */ |
| 8070 | sp<IAfPlaybackThread> IAfPlaybackThread::createSpatializerThread( |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 8071 | const sp<IAfThreadCallback>& afThreadCallback, |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 8072 | AudioStreamOut* output, |
| 8073 | audio_io_handle_t id, |
| 8074 | bool systemReady, |
| 8075 | audio_config_base_t* mixerConfig) { |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 8076 | return sp<SpatializerThread>::make(afThreadCallback, output, id, systemReady, mixerConfig); |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 8077 | } |
| 8078 | |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 8079 | SpatializerThread::SpatializerThread(const sp<IAfThreadCallback>& afThreadCallback, |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 8080 | AudioStreamOut* output, |
| 8081 | audio_io_handle_t id, |
| 8082 | bool systemReady, |
| 8083 | audio_config_base_t *mixerConfig) |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 8084 | : MixerThread(afThreadCallback, output, id, systemReady, SPATIALIZER, mixerConfig) |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 8085 | { |
| 8086 | } |
| 8087 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 8088 | void SpatializerThread::setHalLatencyMode_l() { |
Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 8089 | // if mSupportedLatencyModes is empty, the HAL stream does not support |
| 8090 | // latency mode control and we can exit. |
| 8091 | if (mSupportedLatencyModes.empty()) { |
| 8092 | return; |
| 8093 | } |
Eric Laurent | 4c85e37 | 2024-02-23 16:50:06 +0000 | [diff] [blame] | 8094 | // Do not update the HAL latency mode if no track is active |
| 8095 | if (mActiveTracks.isEmpty()) { |
| 8096 | return; |
| 8097 | } |
| 8098 | |
Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 8099 | audio_latency_mode_t latencyMode = AUDIO_LATENCY_MODE_FREE; |
| 8100 | if (mSupportedLatencyModes.size() == 1) { |
| 8101 | // If the HAL only support one latency mode currently, confirm the choice |
| 8102 | latencyMode = mSupportedLatencyModes[0]; |
| 8103 | } else if (mSupportedLatencyModes.size() > 1) { |
| 8104 | // Request low latency if: |
| 8105 | // - The low latency mode is requested by the spatializer controller |
| 8106 | // (mRequestedLatencyMode = AUDIO_LATENCY_MODE_LOW) |
| 8107 | // AND |
| 8108 | // - At least one active track is spatialized |
Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 8109 | for (const auto& track : mActiveTracks) { |
| 8110 | if (track->isSpatialized()) { |
Eric Laurent | b024157 | 2024-02-01 21:03:49 +0100 | [diff] [blame] | 8111 | latencyMode = mRequestedLatencyMode; |
Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 8112 | break; |
| 8113 | } |
| 8114 | } |
Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 8115 | } |
| 8116 | |
| 8117 | if (latencyMode != mSetLatencyMode) { |
| 8118 | status_t status = mOutput->stream->setLatencyMode(latencyMode); |
Andy Hung | 4bd53e7 | 2022-11-17 17:21:45 -0800 | [diff] [blame] | 8119 | ALOGD("%s: thread(%d) setLatencyMode(%s) returned %d", |
| 8120 | __func__, mId, toString(latencyMode).c_str(), status); |
Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 8121 | if (status == NO_ERROR) { |
| 8122 | mSetLatencyMode = latencyMode; |
| 8123 | } |
| 8124 | } |
| 8125 | } |
| 8126 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 8127 | status_t SpatializerThread::setRequestedLatencyMode(audio_latency_mode_t mode) { |
Eric Laurent | b024157 | 2024-02-01 21:03:49 +0100 | [diff] [blame] | 8128 | if (mode < 0 || mode >= AUDIO_LATENCY_MODE_CNT) { |
Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 8129 | return BAD_VALUE; |
| 8130 | } |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 8131 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 8132 | mRequestedLatencyMode = mode; |
| 8133 | return NO_ERROR; |
| 8134 | } |
| 8135 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 8136 | void SpatializerThread::checkOutputStageEffects() |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 8137 | NO_THREAD_SAFETY_ANALYSIS |
| 8138 | // 'createEffect_l' requires holding mutex 'AudioFlinger_Mutex' exclusively |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 8139 | { |
| 8140 | bool hasVirtualizer = false; |
| 8141 | bool hasDownMixer = false; |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 8142 | sp<IAfEffectHandle> finalDownMixer; |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 8143 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 8144 | audio_utils::lock_guard _l(mutex()); |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 8145 | sp<IAfEffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_STAGE); |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 8146 | if (chain != 0) { |
Eric Laurent | 1c5e2e3 | 2021-08-18 18:50:28 +0200 | [diff] [blame] | 8147 | hasVirtualizer = chain->getEffectFromType_l(FX_IID_SPATIALIZER) != nullptr; |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 8148 | hasDownMixer = chain->getEffectFromType_l(EFFECT_UIID_DOWNMIX) != nullptr; |
| 8149 | } |
| 8150 | |
| 8151 | finalDownMixer = mFinalDownMixer; |
| 8152 | mFinalDownMixer.clear(); |
| 8153 | } |
| 8154 | |
| 8155 | if (hasVirtualizer) { |
| 8156 | if (finalDownMixer != nullptr) { |
| 8157 | int32_t ret; |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 8158 | finalDownMixer->asIEffect()->disable(&ret); |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 8159 | } |
| 8160 | finalDownMixer.clear(); |
| 8161 | } else if (!hasDownMixer) { |
| 8162 | std::vector<effect_descriptor_t> descriptors; |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 8163 | status_t status = mAfThreadCallback->getEffectsFactoryHal()->getDescriptors( |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 8164 | EFFECT_UIID_DOWNMIX, &descriptors); |
| 8165 | if (status != NO_ERROR) { |
| 8166 | return; |
| 8167 | } |
| 8168 | ALOG_ASSERT(!descriptors.empty(), |
| 8169 | "%s getDescriptors() returned no error but empty list", __func__); |
| 8170 | |
| 8171 | finalDownMixer = createEffect_l(nullptr /*client*/, nullptr /*effectClient*/, |
| 8172 | 0 /*priority*/, AUDIO_SESSION_OUTPUT_STAGE, &descriptors[0], nullptr /*enabled*/, |
Eric Laurent | de8caf4 | 2021-08-11 17:19:25 +0200 | [diff] [blame] | 8173 | &status, false /*pinned*/, false /*probe*/, false /*notifyFramesProcessed*/); |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 8174 | |
| 8175 | if (finalDownMixer == nullptr || (status != NO_ERROR && status != ALREADY_EXISTS)) { |
| 8176 | ALOGW("%s error creating downmixer %d", __func__, status); |
| 8177 | finalDownMixer.clear(); |
| 8178 | } else { |
| 8179 | int32_t ret; |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 8180 | finalDownMixer->asIEffect()->enable(&ret); |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 8181 | } |
| 8182 | } |
| 8183 | |
| 8184 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 8185 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 8186 | mFinalDownMixer = finalDownMixer; |
| 8187 | } |
| 8188 | } |
| 8189 | |
Andy Hung | e251446 | 2023-12-06 14:59:24 -0800 | [diff] [blame] | 8190 | void SpatializerThread::threadLoop_exit() |
| 8191 | { |
| 8192 | // The Spatializer EffectHandle must be released on the PlaybackThread |
| 8193 | // threadLoop() to prevent lock inversion in the SpatializerThread dtor. |
| 8194 | mFinalDownMixer.clear(); |
| 8195 | |
| 8196 | PlaybackThread::threadLoop_exit(); |
| 8197 | } |
| 8198 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8199 | // ---------------------------------------------------------------------------- |
| 8200 | // Record |
| 8201 | // ---------------------------------------------------------------------------- |
| 8202 | |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 8203 | sp<IAfRecordThread> IAfRecordThread::create(const sp<IAfThreadCallback>& afThreadCallback, |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 8204 | AudioStreamIn* input, |
| 8205 | audio_io_handle_t id, |
| 8206 | bool systemReady) { |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 8207 | return sp<RecordThread>::make(afThreadCallback, input, id, systemReady); |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 8208 | } |
| 8209 | |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 8210 | RecordThread::RecordThread(const sp<IAfThreadCallback>& afThreadCallback, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8211 | AudioStreamIn *input, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8212 | audio_io_handle_t id, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 8213 | bool systemReady |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 8214 | ) : |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 8215 | ThreadBase(afThreadCallback, id, RECORD, systemReady, false /* isOut */), |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 8216 | mInput(input), |
Mikhail Naganov | 2534b38 | 2019-09-25 13:05:02 -0700 | [diff] [blame] | 8217 | mSource(mInput), |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 8218 | mActiveTracks(&this->mLocalLog), |
| 8219 | mRsmpInBuffer(NULL), |
Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 8220 | // mRsmpInFrames, mRsmpInFramesP2, and mRsmpInFramesOA are set by readInputParameters_l() |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 8221 | mRsmpInRear(0) |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 8222 | , mReadOnlyHeap(new MemoryDealer(kRecordThreadReadOnlyHeapSize, |
| 8223 | "RecordThreadRO", MemoryHeapBase::READ_ONLY)) |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8224 | // mFastCapture below |
| 8225 | , mFastCaptureFutex(0) |
| 8226 | // mInputSource |
| 8227 | // mPipeSink |
| 8228 | // mPipeSource |
| 8229 | , mPipeFramesP2(0) |
| 8230 | // mPipeMemory |
| 8231 | // mFastCaptureNBLogWriter |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 8232 | , mFastTrackAvail(false) |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 8233 | , mBtNrecSuspended(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8234 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 8235 | snprintf(mThreadName, kThreadNameLength, "AudioIn_%X", id); |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 8236 | mNBLogWriter = afThreadCallback->newWriter_l(kLogSize, mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8237 | |
George Burgess IV | a8f90c1 | 2020-05-14 11:27:19 -0700 | [diff] [blame] | 8238 | if (mInput->audioHwDev != nullptr) { |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 8239 | mIsMsdDevice = strcmp( |
| 8240 | mInput->audioHwDev->moduleName(), AUDIO_HARDWARE_MODULE_ID_MSD) == 0; |
| 8241 | } |
| 8242 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 8243 | readInputParameters_l(); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8244 | |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 8245 | // TODO: We may also match on address as well as device type for |
| 8246 | // 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] | 8247 | // TODO: This property should be ensure that only contains one single device type. |
| 8248 | mTimestampCorrectedDevice = (audio_devices_t)property_get_int64( |
| 8249 | "audio.timestamp.corrected_input_device", |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 8250 | (int64_t)(mIsMsdDevice ? AUDIO_DEVICE_IN_BUS // turn on by default for MSD |
| 8251 | : AUDIO_DEVICE_NONE)); |
| 8252 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8253 | // create an NBAIO source for the HAL input stream, and negotiate |
Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 8254 | mInputSource = new AudioStreamInSource(input->stream); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8255 | size_t numCounterOffers = 0; |
| 8256 | const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 8257 | #if !LOG_NDEBUG |
Jing Mike | 537412f | 2023-03-12 11:01:47 +0800 | [diff] [blame] | 8258 | [[maybe_unused]] ssize_t index = |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 8259 | #else |
| 8260 | (void) |
| 8261 | #endif |
| 8262 | mInputSource->negotiate(offers, 1, NULL, numCounterOffers); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8263 | ALOG_ASSERT(index == 0); |
| 8264 | |
| 8265 | // initialize fast capture depending on configuration |
| 8266 | bool initFastCapture; |
| 8267 | switch (kUseFastCapture) { |
| 8268 | case FastCapture_Never: |
| 8269 | initFastCapture = false; |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 8270 | ALOGV("%p kUseFastCapture = Never, initFastCapture = false", this); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8271 | break; |
| 8272 | case FastCapture_Always: |
| 8273 | initFastCapture = true; |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 8274 | ALOGV("%p kUseFastCapture = Always, initFastCapture = true", this); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8275 | break; |
| 8276 | case FastCapture_Static: |
Sampath | 6fac233 | 2022-12-16 17:34:37 +1100 | [diff] [blame] | 8277 | initFastCapture = !mIsMsdDevice // Disable fast capture for MSD BUS devices. |
Dean Wheatley | 8e5d9e4 | 2023-11-03 12:37:27 +1100 | [diff] [blame] | 8278 | && audio_is_linear_pcm(mFormat) |
Sampath | 6fac233 | 2022-12-16 17:34:37 +1100 | [diff] [blame] | 8279 | && (mFrameCount * 1000) / mSampleRate < kMinNormalCaptureBufferSizeMs; |
Dean Wheatley | 8e5d9e4 | 2023-11-03 12:37:27 +1100 | [diff] [blame] | 8280 | ALOGV("%p kUseFastCapture = Static, format = 0x%x, (%lld * 1000) / %u vs %u, " |
| 8281 | "initFastCapture = %d, mIsMsdDevice = %d", this, mFormat, (long long)mFrameCount, |
| 8282 | mSampleRate, kMinNormalCaptureBufferSizeMs, initFastCapture, mIsMsdDevice); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8283 | break; |
| 8284 | // case FastCapture_Dynamic: |
| 8285 | } |
| 8286 | |
| 8287 | if (initFastCapture) { |
Glenn Kasten | d198b85 | 2015-03-16 14:55:53 -0700 | [diff] [blame] | 8288 | // 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] | 8289 | NBAIO_Format format = mInputSource->format(); |
Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 8290 | // quadruple-buffering of 20 ms each; this ensures we can sleep for 20ms in RecordThread |
| 8291 | size_t pipeFramesP2 = roundup(4 * FMS_20 * mSampleRate / 1000); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8292 | size_t pipeSize = pipeFramesP2 * Format_frameSize(format); |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 8293 | void *pipeBuffer = nullptr; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8294 | const sp<MemoryDealer> roHeap(readOnlyHeap()); |
| 8295 | sp<IMemory> pipeMemory; |
| 8296 | if ((roHeap == 0) || |
| 8297 | (pipeMemory = roHeap->allocate(pipeSize)) == 0 || |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 8298 | (pipeBuffer = pipeMemory->unsecurePointer()) == nullptr) { |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 8299 | ALOGE("not enough memory for pipe buffer size=%zu; " |
| 8300 | "roHeap=%p, pipeMemory=%p, pipeBuffer=%p; roHeapSize: %lld", |
| 8301 | pipeSize, roHeap.get(), pipeMemory.get(), pipeBuffer, |
| 8302 | (long long)kRecordThreadReadOnlyHeapSize); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8303 | goto failed; |
| 8304 | } |
| 8305 | // pipe will be shared directly with fast clients, so clear to avoid leaking old information |
| 8306 | memset(pipeBuffer, 0, pipeSize); |
| 8307 | Pipe *pipe = new Pipe(pipeFramesP2, format, pipeBuffer); |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 8308 | const NBAIO_Format offersFast[1] = {format}; |
| 8309 | size_t numCounterOffersFast = 0; |
Eric Laurent | 1e28aaa | 2023-04-16 19:34:23 +0200 | [diff] [blame] | 8310 | [[maybe_unused]] ssize_t index2 = pipe->negotiate(offersFast, std::size(offersFast), |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 8311 | nullptr /* counterOffers */, numCounterOffersFast); |
Eric Laurent | 1e28aaa | 2023-04-16 19:34:23 +0200 | [diff] [blame] | 8312 | ALOG_ASSERT(index2 == 0); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8313 | mPipeSink = pipe; |
| 8314 | PipeReader *pipeReader = new PipeReader(*pipe); |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 8315 | numCounterOffersFast = 0; |
Eric Laurent | 1e28aaa | 2023-04-16 19:34:23 +0200 | [diff] [blame] | 8316 | index2 = pipeReader->negotiate(offersFast, std::size(offersFast), |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 8317 | nullptr /* counterOffers */, numCounterOffersFast); |
Eric Laurent | 1e28aaa | 2023-04-16 19:34:23 +0200 | [diff] [blame] | 8318 | ALOG_ASSERT(index2 == 0); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8319 | mPipeSource = pipeReader; |
| 8320 | mPipeFramesP2 = pipeFramesP2; |
| 8321 | mPipeMemory = pipeMemory; |
| 8322 | |
| 8323 | // create fast capture |
| 8324 | mFastCapture = new FastCapture(); |
| 8325 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 8326 | #ifdef STATE_QUEUE_DUMP |
| 8327 | // FIXME |
| 8328 | #endif |
| 8329 | FastCaptureState *state = sq->begin(); |
| 8330 | state->mCblk = NULL; |
| 8331 | state->mInputSource = mInputSource.get(); |
| 8332 | state->mInputSourceGen++; |
| 8333 | state->mPipeSink = pipe; |
| 8334 | state->mPipeSinkGen++; |
| 8335 | state->mFrameCount = mFrameCount; |
| 8336 | state->mCommand = FastCaptureState::COLD_IDLE; |
| 8337 | // already done in constructor initialization list |
| 8338 | //mFastCaptureFutex = 0; |
| 8339 | state->mColdFutexAddr = &mFastCaptureFutex; |
| 8340 | state->mColdGen++; |
| 8341 | state->mDumpState = &mFastCaptureDumpState; |
| 8342 | #ifdef TEE_SINK |
| 8343 | // FIXME |
| 8344 | #endif |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 8345 | mFastCaptureNBLogWriter = |
| 8346 | afThreadCallback->newWriter_l(kFastCaptureLogSize, "FastCapture"); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8347 | state->mNBLogWriter = mFastCaptureNBLogWriter.get(); |
| 8348 | sq->end(); |
Andy Hung | 82f39d6 | 2024-09-30 17:19:14 -0700 | [diff] [blame] | 8349 | { |
| 8350 | audio_utils::mutex::scoped_queue_wait_check queueWaitCheck(mFastCapture->getTid()); |
| 8351 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); |
| 8352 | } |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8353 | // start the fast capture |
| 8354 | mFastCapture->run("FastCapture", ANDROID_PRIORITY_URGENT_AUDIO); |
| 8355 | pid_t tid = mFastCapture->getTid(); |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 8356 | sendPrioConfigEvent(getpid(), tid, kPriorityFastCapture, false /*forApp*/); |
Mikhail Naganov | e1c4b5d | 2016-12-22 09:22:45 -0800 | [diff] [blame] | 8357 | stream()->setHalThreadPriority(kPriorityFastCapture); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8358 | #ifdef AUDIO_WATCHDOG |
| 8359 | // FIXME |
| 8360 | #endif |
| 8361 | |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 8362 | mFastTrackAvail = true; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8363 | } |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 8364 | #ifdef TEE_SINK |
| 8365 | mTee.set(mInputSource->format(), NBAIO_Tee::TEE_FLAG_INPUT_THREAD); |
| 8366 | mTee.setId(std::string("_") + std::to_string(mId) + "_C"); |
| 8367 | #endif |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8368 | failed: ; |
| 8369 | |
| 8370 | // FIXME mNormalSource |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8371 | } |
| 8372 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 8373 | RecordThread::~RecordThread() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8374 | { |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8375 | if (mFastCapture != 0) { |
| 8376 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 8377 | FastCaptureState *state = sq->begin(); |
| 8378 | if (state->mCommand == FastCaptureState::COLD_IDLE) { |
| 8379 | int32_t old = android_atomic_inc(&mFastCaptureFutex); |
| 8380 | if (old == -1) { |
| 8381 | (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); |
| 8382 | } |
| 8383 | } |
| 8384 | state->mCommand = FastCaptureState::EXIT; |
| 8385 | sq->end(); |
Andy Hung | 82f39d6 | 2024-09-30 17:19:14 -0700 | [diff] [blame] | 8386 | { |
| 8387 | audio_utils::mutex::scoped_join_wait_check queueWaitCheck(mFastCapture->getTid()); |
| 8388 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); |
| 8389 | mFastCapture->join(); |
| 8390 | } |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8391 | mFastCapture.clear(); |
| 8392 | } |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 8393 | mAfThreadCallback->unregisterWriter(mFastCaptureNBLogWriter); |
| 8394 | mAfThreadCallback->unregisterWriter(mNBLogWriter); |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 8395 | free(mRsmpInBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8396 | } |
| 8397 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 8398 | void RecordThread::onFirstRef() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8399 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 8400 | run(mThreadName, PRIORITY_URGENT_AUDIO); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8401 | } |
| 8402 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 8403 | void RecordThread::preExit() |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 8404 | { |
| 8405 | ALOGV(" preExit()"); |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 8406 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 8407 | for (size_t i = 0; i < mTracks.size(); i++) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8408 | sp<IAfRecordTrack> track = mTracks[i]; |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 8409 | track->invalidate(); |
| 8410 | } |
| 8411 | mActiveTracks.clear(); |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 8412 | mStartStopCV.notify_all(); |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 8413 | } |
| 8414 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 8415 | bool RecordThread::threadLoop() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8416 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8417 | nsecs_t lastWarning = 0; |
| 8418 | |
| 8419 | inputStandBy(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8420 | |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 8421 | reacquire_wakelock: |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 8422 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 8423 | audio_utils::lock_guard _l(mutex()); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 8424 | acquireWakeLock_l(); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 8425 | } |
| 8426 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8427 | // used to request a deferred sleep, to be executed later while mutex is unlocked |
| 8428 | uint32_t sleepUs = 0; |
| 8429 | |
Andy Hung | 95c94a2 | 2023-10-20 16:41:18 -0700 | [diff] [blame] | 8430 | // timestamp correction enable is determined under lock, used in processing step. |
| 8431 | bool timestampCorrectionEnabled = false; |
| 8432 | |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 8433 | int64_t lastLoopCountRead = -2; // never matches "previous" loop, when loopCount = 0. |
| 8434 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8435 | // loop while there is work to do |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 8436 | for (int64_t loopCount = 0;; ++loopCount) { // loopCount used for statistics tracking |
Andy Hung | 6e69366 | 2024-03-15 10:15:10 -0700 | [diff] [blame] | 8437 | // Note: these sp<> are released at the end of the for loop outside of the mutex() lock. |
| 8438 | sp<IAfRecordTrack> activeTrack; |
Andy Hung | ef6d8ae | 2024-04-23 13:56:19 -0700 | [diff] [blame] | 8439 | std::vector<sp<IAfRecordTrack>> oldActiveTracks; |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 8440 | Vector<sp<IAfEffectChain>> effectChains; |
Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 8441 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8442 | // activeTracks accumulates a copy of a subset of mActiveTracks |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8443 | Vector<sp<IAfRecordTrack>> activeTracks; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8444 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 8445 | // reference to the (first and only) active fast track |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8446 | sp<IAfRecordTrack> fastTrack; |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 8447 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 8448 | // reference to a fast track which is about to be removed |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8449 | sp<IAfRecordTrack> fastTrackToRemove; |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 8450 | |
Eric Laurent | 33403f0 | 2020-05-29 18:35:06 -0700 | [diff] [blame] | 8451 | bool silenceFastCapture = false; |
| 8452 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 8453 | { // scope for mutex() |
| 8454 | audio_utils::unique_lock _l(mutex()); |
Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 8455 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 8456 | processConfigEvents_l(); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 8457 | |
Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 8458 | // check exitPending here because checkForNewParameters_l() and |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 8459 | // checkForNewParameters_l() can temporarily release mutex() |
Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 8460 | if (exitPending()) { |
| 8461 | break; |
| 8462 | } |
| 8463 | |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 8464 | // sleep with mutex unlocked |
| 8465 | if (sleepUs > 0) { |
Glenn Kasten | f9715e4 | 2016-07-13 14:02:03 -0700 | [diff] [blame] | 8466 | ATRACE_BEGIN("sleepC"); |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 8467 | (void)mWaitWorkCV.wait_for(_l, std::chrono::microseconds(sleepUs)); |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 8468 | ATRACE_END(); |
| 8469 | sleepUs = 0; |
| 8470 | continue; |
| 8471 | } |
| 8472 | |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 8473 | // if no active track(s), then standby and release wakelock |
| 8474 | size_t size = mActiveTracks.size(); |
| 8475 | if (size == 0) { |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 8476 | standbyIfNotAlreadyInStandby(); |
Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 8477 | // exitPending() can't become true here |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8478 | releaseWakeLock_l(); |
| 8479 | ALOGV("RecordThread: loop stopping"); |
| 8480 | // go to sleep |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 8481 | mWaitWorkCV.wait(_l); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8482 | ALOGV("RecordThread: loop starting"); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 8483 | goto reacquire_wakelock; |
| 8484 | } |
| 8485 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8486 | bool doBroadcast = false; |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 8487 | bool allStopped = true; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8488 | for (size_t i = 0; i < size; ) { |
Andy Hung | ef6d8ae | 2024-04-23 13:56:19 -0700 | [diff] [blame] | 8489 | if (activeTrack) { // ensure track release is outside lock. |
| 8490 | oldActiveTracks.emplace_back(std::move(activeTrack)); |
| 8491 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8492 | activeTrack = mActiveTracks[i]; |
| 8493 | if (activeTrack->isTerminated()) { |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 8494 | if (activeTrack->isFastTrack()) { |
| 8495 | ALOG_ASSERT(fastTrackToRemove == 0); |
| 8496 | fastTrackToRemove = activeTrack; |
| 8497 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8498 | removeTrack_l(activeTrack); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 8499 | mActiveTracks.remove(activeTrack); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8500 | size--; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 8501 | continue; |
| 8502 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8503 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8504 | IAfTrackBase::track_state activeTrackState = activeTrack->state(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8505 | switch (activeTrackState) { |
| 8506 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8507 | case IAfTrackBase::PAUSING: |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8508 | mActiveTracks.remove(activeTrack); |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8509 | activeTrack->setState(IAfTrackBase::PAUSED); |
François Gaffie | 39634e4 | 2023-10-17 12:13:32 +0200 | [diff] [blame] | 8510 | if (activeTrack->isFastTrack()) { |
| 8511 | ALOGV("%s fast track is paused, thus removed from active list", __func__); |
| 8512 | // Keep a ref on fast track to wait for FastCapture thread to get updated |
| 8513 | // state before potential track removal |
| 8514 | fastTrackToRemove = activeTrack; |
| 8515 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8516 | doBroadcast = true; |
| 8517 | size--; |
| 8518 | continue; |
| 8519 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8520 | case IAfTrackBase::STARTING_1: |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8521 | sleepUs = 10000; |
| 8522 | i++; |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 8523 | allStopped = false; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8524 | continue; |
| 8525 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8526 | case IAfTrackBase::STARTING_2: |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8527 | doBroadcast = true; |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 8528 | if (mStandby) { |
| 8529 | mThreadMetrics.logBeginInterval(); |
Andy Hung | 44d648b | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 8530 | mThreadSnapshot.onBegin(); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 8531 | mStandby = false; |
| 8532 | } |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8533 | activeTrack->setState(IAfTrackBase::ACTIVE); |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 8534 | allStopped = false; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8535 | break; |
| 8536 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8537 | case IAfTrackBase::ACTIVE: |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 8538 | allStopped = false; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8539 | break; |
| 8540 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8541 | case IAfTrackBase::IDLE: // cannot be on ActiveTracks if idle |
| 8542 | case IAfTrackBase::PAUSED: // cannot be on ActiveTracks if paused |
| 8543 | case IAfTrackBase::STOPPED: // cannot be on ActiveTracks if destroyed/terminated |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8544 | default: |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 8545 | LOG_ALWAYS_FATAL("%s: Unexpected active track state:%d, id:%d, tracks:%zu", |
| 8546 | __func__, activeTrackState, activeTrack->id(), size); |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 8547 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 8548 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8549 | if (activeTrack->isFastTrack()) { |
| 8550 | ALOG_ASSERT(!mFastTrackAvail); |
| 8551 | ALOG_ASSERT(fastTrack == 0); |
Eric Laurent | 33403f0 | 2020-05-29 18:35:06 -0700 | [diff] [blame] | 8552 | // if the active fast track is silenced either: |
| 8553 | // 1) silence the whole capture from fast capture buffer if this is |
| 8554 | // the only active track |
| 8555 | // 2) invalidate this track: this will cause the client to reconnect and possibly |
| 8556 | // be invalidated again until unsilenced |
Eric Laurent | 5f0fd7b | 2021-05-07 16:33:26 +0200 | [diff] [blame] | 8557 | bool invalidate = false; |
Eric Laurent | 33403f0 | 2020-05-29 18:35:06 -0700 | [diff] [blame] | 8558 | if (activeTrack->isSilenced()) { |
| 8559 | if (size > 1) { |
Eric Laurent | 5f0fd7b | 2021-05-07 16:33:26 +0200 | [diff] [blame] | 8560 | invalidate = true; |
Eric Laurent | 33403f0 | 2020-05-29 18:35:06 -0700 | [diff] [blame] | 8561 | } else { |
| 8562 | silenceFastCapture = true; |
| 8563 | } |
| 8564 | } |
Eric Laurent | 5f0fd7b | 2021-05-07 16:33:26 +0200 | [diff] [blame] | 8565 | // Invalidate fast tracks if access to audio history is required as this is not |
| 8566 | // possible with fast tracks. Once the fast track has been invalidated, no new |
| 8567 | // fast track will be created until mMaxSharedAudioHistoryMs is cleared. |
| 8568 | if (mMaxSharedAudioHistoryMs != 0) { |
| 8569 | invalidate = true; |
| 8570 | } |
| 8571 | if (invalidate) { |
| 8572 | activeTrack->invalidate(); |
| 8573 | ALOG_ASSERT(fastTrackToRemove == 0); |
| 8574 | fastTrackToRemove = activeTrack; |
| 8575 | removeTrack_l(activeTrack); |
| 8576 | mActiveTracks.remove(activeTrack); |
| 8577 | size--; |
| 8578 | continue; |
| 8579 | } |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8580 | fastTrack = activeTrack; |
| 8581 | } |
Eric Laurent | 33403f0 | 2020-05-29 18:35:06 -0700 | [diff] [blame] | 8582 | |
| 8583 | activeTracks.add(activeTrack); |
| 8584 | i++; |
| 8585 | |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 8586 | } |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 8587 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 8588 | mActiveTracks.updatePowerState_l(this); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 8589 | |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 8590 | updateMetadata_l(); |
| 8591 | |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 8592 | if (allStopped) { |
| 8593 | standbyIfNotAlreadyInStandby(); |
| 8594 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8595 | if (doBroadcast) { |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 8596 | mStartStopCV.notify_all(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8597 | } |
| 8598 | |
| 8599 | // sleep if there are no active tracks to process |
Eric Tan | 39ec8d6 | 2018-07-24 09:49:29 -0700 | [diff] [blame] | 8600 | if (activeTracks.isEmpty()) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8601 | if (sleepUs == 0) { |
| 8602 | sleepUs = kRecordThreadSleepUs; |
| 8603 | } |
| 8604 | continue; |
| 8605 | } |
| 8606 | sleepUs = 0; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 8607 | |
Andy Hung | 95c94a2 | 2023-10-20 16:41:18 -0700 | [diff] [blame] | 8608 | timestampCorrectionEnabled = isTimestampCorrectionEnabled_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8609 | lockEffectChains_l(effectChains); |
| 8610 | } |
| 8611 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8612 | // thread mutex is now unlocked, mActiveTracks unknown, activeTracks.size() > 0 |
Glenn Kasten | 7165268 | 2013-08-14 15:17:55 -0700 | [diff] [blame] | 8613 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8614 | size_t size = effectChains.size(); |
| 8615 | for (size_t i = 0; i < size; i++) { |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 8616 | // thread mutex is not locked, but effect chain is locked |
| 8617 | effectChains[i]->process_l(); |
| 8618 | } |
| 8619 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 8620 | // 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] | 8621 | if (mFastCapture != 0) { |
| 8622 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 8623 | FastCaptureState *state = sq->begin(); |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 8624 | bool didModify = false; |
| 8625 | FastCaptureStateQueue::block_t block = FastCaptureStateQueue::BLOCK_UNTIL_PUSHED; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8626 | if (state->mCommand != FastCaptureState::READ_WRITE /* FIXME && |
| 8627 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)*/) { |
| 8628 | if (state->mCommand == FastCaptureState::COLD_IDLE) { |
| 8629 | int32_t old = android_atomic_inc(&mFastCaptureFutex); |
| 8630 | if (old == -1) { |
| 8631 | (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); |
| 8632 | } |
| 8633 | } |
| 8634 | state->mCommand = FastCaptureState::READ_WRITE; |
| 8635 | #if 0 // FIXME |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 8636 | mFastCaptureDumpState.increaseSamplingN(mAfThreadCallback->isLowRamDevice() ? |
Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 8637 | FastThreadDumpState::kSamplingNforLowRamDevice : |
| 8638 | FastThreadDumpState::kSamplingN); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8639 | #endif |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 8640 | didModify = true; |
| 8641 | } |
| 8642 | audio_track_cblk_t *cblkOld = state->mCblk; |
| 8643 | audio_track_cblk_t *cblkNew = fastTrack != 0 ? fastTrack->cblk() : NULL; |
| 8644 | if (cblkNew != cblkOld) { |
| 8645 | state->mCblk = cblkNew; |
| 8646 | // block until acked if removing a fast track |
| 8647 | if (cblkOld != NULL) { |
| 8648 | block = FastCaptureStateQueue::BLOCK_UNTIL_ACKED; |
| 8649 | } |
| 8650 | didModify = true; |
| 8651 | } |
jiabin | 01c8f56 | 2018-07-19 17:47:28 -0700 | [diff] [blame] | 8652 | AudioBufferProvider* abp = (fastTrack != 0 && fastTrack->isPatchTrack()) ? |
| 8653 | reinterpret_cast<AudioBufferProvider*>(fastTrack.get()) : nullptr; |
| 8654 | if (state->mFastPatchRecordBufferProvider != abp) { |
| 8655 | state->mFastPatchRecordBufferProvider = abp; |
| 8656 | state->mFastPatchRecordFormat = fastTrack == 0 ? |
| 8657 | AUDIO_FORMAT_INVALID : fastTrack->format(); |
| 8658 | didModify = true; |
| 8659 | } |
Eric Laurent | 33403f0 | 2020-05-29 18:35:06 -0700 | [diff] [blame] | 8660 | if (state->mSilenceCapture != silenceFastCapture) { |
| 8661 | state->mSilenceCapture = silenceFastCapture; |
| 8662 | didModify = true; |
| 8663 | } |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 8664 | sq->end(didModify); |
| 8665 | if (didModify) { |
| 8666 | sq->push(block); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8667 | #if 0 |
| 8668 | if (kUseFastCapture == FastCapture_Dynamic) { |
| 8669 | mNormalSource = mPipeSource; |
| 8670 | } |
| 8671 | #endif |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8672 | } |
| 8673 | } |
| 8674 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 8675 | // now run the fast track destructor with thread mutex unlocked |
| 8676 | fastTrackToRemove.clear(); |
| 8677 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8678 | // Read from HAL to keep up with fastest client if multiple active tracks, not slowest one. |
| 8679 | // Only the client(s) that are too slow will overrun. But if even the fastest client is too |
| 8680 | // slow, then this RecordThread will overrun by not calling HAL read often enough. |
| 8681 | // If destination is non-contiguous, first read past the nominal end of buffer, then |
| 8682 | // copy to the right place. Permitted because mRsmpInBuffer was over-allocated. |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 8683 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8684 | int32_t rear = mRsmpInRear & (mRsmpInFramesP2 - 1); |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 8685 | ssize_t framesRead = 0; // not needed, remove clang-tidy warning. |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 8686 | const int64_t lastIoBeginNs = systemTime(); // start IO timing |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8687 | |
| 8688 | // If an NBAIO source is present, use it to read the normal capture's data |
| 8689 | if (mPipeSource != 0) { |
Andy Hung | 156317a | 2018-08-02 11:49:29 -0700 | [diff] [blame] | 8690 | size_t framesToRead = min(mRsmpInFramesOA - rear, mRsmpInFramesP2 / 2); |
Andy Hung | d5b638f | 2018-04-30 13:56:10 -0700 | [diff] [blame] | 8691 | |
| 8692 | // The audio fifo read() returns OVERRUN on overflow, and advances the read pointer |
| 8693 | // to the full buffer point (clearing the overflow condition). Upon OVERRUN error, |
| 8694 | // we immediately retry the read() to get data and prevent another overflow. |
| 8695 | for (int retries = 0; retries <= 2; ++retries) { |
| 8696 | ALOGW_IF(retries > 0, "overrun on read from pipe, retry #%d", retries); |
| 8697 | framesRead = mPipeSource->read((uint8_t*)mRsmpInBuffer + rear * mFrameSize, |
| 8698 | framesToRead); |
| 8699 | if (framesRead != OVERRUN) break; |
| 8700 | } |
| 8701 | |
Andy Hung | 7a3dc6b | 2018-05-01 16:39:51 -0700 | [diff] [blame] | 8702 | const ssize_t availableToRead = mPipeSource->availableToRead(); |
| 8703 | if (availableToRead >= 0) { |
Robert Wu | 06db0a3 | 2021-08-10 19:05:34 +0000 | [diff] [blame] | 8704 | mMonopipePipeDepthStats.add(availableToRead); |
Glenn Kasten | a2f59b3 | 2020-08-03 16:37:24 -0700 | [diff] [blame] | 8705 | // 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] | 8706 | LOG_ALWAYS_FATAL_IF((size_t)availableToRead > mPipeFramesP2, |
| 8707 | "more frames to read than fifo size, %zd > %zu", |
| 8708 | availableToRead, mPipeFramesP2); |
| 8709 | const size_t pipeFramesFree = mPipeFramesP2 - availableToRead; |
| 8710 | const size_t sleepFrames = min(pipeFramesFree, mRsmpInFramesP2) / 2; |
| 8711 | ALOGVV("mPipeFramesP2:%zu mRsmpInFramesP2:%zu sleepFrames:%zu availableToRead:%zd", |
| 8712 | mPipeFramesP2, mRsmpInFramesP2, sleepFrames, availableToRead); |
Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 8713 | sleepUs = (sleepFrames * 1000000LL) / mSampleRate; |
| 8714 | } |
| 8715 | if (framesRead < 0) { |
| 8716 | status_t status = (status_t) framesRead; |
| 8717 | switch (status) { |
| 8718 | case OVERRUN: |
| 8719 | ALOGW("overrun on read from pipe"); |
| 8720 | framesRead = 0; |
| 8721 | break; |
| 8722 | case NEGOTIATE: |
| 8723 | ALOGE("re-negotiation is needed"); |
| 8724 | framesRead = -1; // Will cause an attempt to recover. |
| 8725 | break; |
| 8726 | default: |
| 8727 | ALOGE("unknown error %d on read from pipe", status); |
| 8728 | break; |
| 8729 | } |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8730 | } |
| 8731 | // otherwise use the HAL / AudioStreamIn directly |
| 8732 | } else { |
Glenn Kasten | ec6a703 | 2016-03-14 07:40:23 -0700 | [diff] [blame] | 8733 | ATRACE_BEGIN("read"); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 8734 | size_t bytesRead; |
Mikhail Naganov | 2534b38 | 2019-09-25 13:05:02 -0700 | [diff] [blame] | 8735 | status_t result = mSource->read( |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 8736 | (uint8_t*)mRsmpInBuffer + rear * mFrameSize, mBufferSize, &bytesRead); |
Glenn Kasten | ec6a703 | 2016-03-14 07:40:23 -0700 | [diff] [blame] | 8737 | ATRACE_END(); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 8738 | if (result < 0) { |
| 8739 | framesRead = result; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8740 | } else { |
| 8741 | framesRead = bytesRead / mFrameSize; |
| 8742 | } |
| 8743 | } |
| 8744 | |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 8745 | const int64_t lastIoEndNs = systemTime(); // end IO timing |
| 8746 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 8747 | // Update server timestamp with server stats |
| 8748 | // systemTime() is optional if the hardware supports timestamps. |
Andy Hung | 743f640 | 2020-06-03 13:17:04 -0700 | [diff] [blame] | 8749 | if (framesRead >= 0) { |
| 8750 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] += framesRead; |
| 8751 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = lastIoEndNs; |
| 8752 | } |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 8753 | |
| 8754 | // Update server timestamp with kernel stats |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 8755 | if (mPipeSource.get() == nullptr /* don't obtain for FastCapture, could block */) { |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 8756 | int64_t position, time; |
Andy Hung | 2e2c0bb | 2018-06-11 19:13:11 -0700 | [diff] [blame] | 8757 | if (mStandby) { |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 8758 | mTimestampVerifier.discontinuity(audio_is_linear_pcm(mFormat) ? |
| 8759 | mTimestampVerifier.DISCONTINUITY_MODE_CONTINUOUS : |
| 8760 | mTimestampVerifier.DISCONTINUITY_MODE_ZERO); |
Mikhail Naganov | 2534b38 | 2019-09-25 13:05:02 -0700 | [diff] [blame] | 8761 | } else if (mSource->getCapturePosition(&position, &time) == NO_ERROR |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 8762 | && time > mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]) { |
| 8763 | |
| 8764 | mTimestampVerifier.add(position, time, mSampleRate); |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 8765 | if (timestampCorrectionEnabled) { |
Dean Wheatley | 56a583e | 2020-05-08 15:12:17 +1000 | [diff] [blame] | 8766 | ALOGVV("TS_BEFORE: %d %lld %lld", |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 8767 | id(), (long long)time, (long long)position); |
| 8768 | auto correctedTimestamp = mTimestampVerifier.getLastCorrectedTimestamp(); |
| 8769 | position = correctedTimestamp.mFrames; |
| 8770 | time = correctedTimestamp.mTimeNs; |
Dean Wheatley | 56a583e | 2020-05-08 15:12:17 +1000 | [diff] [blame] | 8771 | ALOGVV("TS_AFTER: %d %lld %lld", |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 8772 | id(), (long long)time, (long long)position); |
| 8773 | } |
| 8774 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 8775 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = position; |
| 8776 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = time; |
| 8777 | // Note: In general record buffers should tend to be empty in |
| 8778 | // a properly running pipeline. |
| 8779 | // |
| 8780 | // Also, it is not advantageous to call get_presentation_position during the read |
| 8781 | // as the read obtains a lock, preventing the timestamp call from executing. |
Andy Hung | 2e2c0bb | 2018-06-11 19:13:11 -0700 | [diff] [blame] | 8782 | } else { |
| 8783 | mTimestampVerifier.error(); |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 8784 | } |
| 8785 | } |
Andy Hung | e6c3711 | 2019-02-26 17:38:10 -0800 | [diff] [blame] | 8786 | |
| 8787 | // From the timestamp, input read latency is negative output write latency. |
| 8788 | const audio_input_flags_t flags = mInput != NULL ? mInput->flags : AUDIO_INPUT_FLAG_NONE; |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8789 | const double latencyMs = IAfRecordTrack::checkServerLatencySupported(mFormat, flags) |
Andy Hung | e6c3711 | 2019-02-26 17:38:10 -0800 | [diff] [blame] | 8790 | ? - mTimestamp.getOutputServerLatencyMs(mSampleRate) : 0.; |
| 8791 | if (latencyMs != 0.) { // note 0. means timestamp is empty. |
| 8792 | mLatencyMs.add(latencyMs); |
| 8793 | } |
| 8794 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 8795 | // Use this to track timestamp information |
| 8796 | // ALOGD("%s", mTimestamp.toString().c_str()); |
| 8797 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8798 | if (framesRead < 0 || (framesRead == 0 && mPipeSource == 0)) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 8799 | ALOGE("read failed: framesRead=%zd", framesRead); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8800 | // Force input into standby so that it tries to recover at next read attempt |
| 8801 | inputStandBy(); |
| 8802 | sleepUs = kRecordThreadSleepUs; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8803 | } |
| 8804 | if (framesRead <= 0) { |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 8805 | goto unlock; |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 8806 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8807 | ALOG_ASSERT(framesRead > 0); |
Andy Hung | 6427e44 | 2018-08-09 12:51:02 -0700 | [diff] [blame] | 8808 | mFramesRead += framesRead; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8809 | |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 8810 | #ifdef TEE_SINK |
| 8811 | (void)mTee.write((uint8_t*)mRsmpInBuffer + rear * mFrameSize, framesRead); |
| 8812 | #endif |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8813 | // 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] | 8814 | { |
| 8815 | size_t part1 = mRsmpInFramesP2 - rear; |
| 8816 | if ((size_t) framesRead > part1) { |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 8817 | memcpy(mRsmpInBuffer, (uint8_t*)mRsmpInBuffer + mRsmpInFramesP2 * mFrameSize, |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 8818 | (framesRead - part1) * mFrameSize); |
| 8819 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8820 | } |
Dean Wheatley | fd56e81 | 2020-11-06 22:32:21 +1100 | [diff] [blame] | 8821 | mRsmpInRear = audio_utils::safe_add_overflow(mRsmpInRear, (int32_t)framesRead); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8822 | |
| 8823 | size = activeTracks.size(); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 8824 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8825 | // loop over each active track |
| 8826 | for (size_t i = 0; i < size; i++) { |
Andy Hung | e8c6c53 | 2024-06-17 15:42:48 -0700 | [diff] [blame] | 8827 | if (activeTrack) { // ensure track release is outside lock. |
| 8828 | oldActiveTracks.emplace_back(std::move(activeTrack)); |
| 8829 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8830 | activeTrack = activeTracks[i]; |
| 8831 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8832 | // skip fast tracks, as those are handled directly by FastCapture |
| 8833 | if (activeTrack->isFastTrack()) { |
| 8834 | continue; |
| 8835 | } |
| 8836 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 8837 | // TODO: This code probably should be moved to RecordTrack. |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 8838 | // TODO: Update the activeTrack buffer converter in case of reconfigure. |
| 8839 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8840 | enum { |
| 8841 | OVERRUN_UNKNOWN, |
| 8842 | OVERRUN_TRUE, |
| 8843 | OVERRUN_FALSE |
| 8844 | } overrun = OVERRUN_UNKNOWN; |
| 8845 | |
| 8846 | // loop over getNextBuffer to handle circular sink |
| 8847 | for (;;) { |
| 8848 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8849 | activeTrack->sinkBuffer().frameCount = ~0; |
| 8850 | status_t status = activeTrack->getNextBuffer(&activeTrack->sinkBuffer()); |
| 8851 | size_t framesOut = activeTrack->sinkBuffer().frameCount; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8852 | LOG_ALWAYS_FATAL_IF((status == OK) != (framesOut > 0)); |
| 8853 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 8854 | // check available frames and handle overrun conditions |
| 8855 | // if the record track isn't draining fast enough. |
| 8856 | bool hasOverrun; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8857 | size_t framesIn; |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8858 | activeTrack->resamplerBufferProvider()->sync(&framesIn, &hasOverrun); |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 8859 | if (hasOverrun) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8860 | overrun = OVERRUN_TRUE; |
| 8861 | } |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 8862 | if (framesOut == 0 || framesIn == 0) { |
| 8863 | break; |
| 8864 | } |
| 8865 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 8866 | // Don't allow framesOut to be larger than what is possible with resampling |
| 8867 | // from framesIn. |
| 8868 | // This isn't strictly necessary but helps limit buffer resizing in |
| 8869 | // RecordBufferConverter. TODO: remove when no longer needed. |
Dean Wheatley | dea650c | 2023-11-01 22:49:01 +1100 | [diff] [blame] | 8870 | if (audio_is_linear_pcm(activeTrack->format())) { |
| 8871 | framesOut = min(framesOut, |
| 8872 | destinationFramesPossible( |
| 8873 | framesIn, mSampleRate, activeTrack->sampleRate())); |
| 8874 | } |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 8875 | |
| 8876 | if (activeTrack->isDirect()) { |
Daniel Van Veen | 9e2376e | 2018-09-27 14:37:58 +1000 | [diff] [blame] | 8877 | // No RecordBufferConverter used for direct streams. Pass |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 8878 | // straight from RecordThread buffer to RecordTrack buffer. |
| 8879 | AudioBufferProvider::Buffer buffer; |
| 8880 | buffer.frameCount = framesOut; |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 8881 | const status_t getNextBufferStatus = |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8882 | activeTrack->resamplerBufferProvider()->getNextBuffer(&buffer); |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 8883 | if (getNextBufferStatus == OK && buffer.frameCount != 0) { |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 8884 | ALOGV_IF(buffer.frameCount != framesOut, |
| 8885 | "%s() read less than expected (%zu vs %zu)", |
| 8886 | __func__, buffer.frameCount, framesOut); |
| 8887 | framesOut = buffer.frameCount; |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8888 | memcpy(activeTrack->sinkBuffer().raw, |
| 8889 | buffer.raw, buffer.frameCount * mFrameSize); |
| 8890 | activeTrack->resamplerBufferProvider()->releaseBuffer(&buffer); |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 8891 | } else { |
| 8892 | framesOut = 0; |
| 8893 | ALOGE("%s() cannot fill request, status: %d, frameCount: %zu", |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 8894 | __func__, getNextBufferStatus, buffer.frameCount); |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 8895 | } |
| 8896 | } else { |
| 8897 | // process frames from the RecordThread buffer provider to the RecordTrack |
| 8898 | // buffer |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8899 | framesOut = activeTrack->recordBufferConverter()->convert( |
| 8900 | activeTrack->sinkBuffer().raw, |
| 8901 | activeTrack->resamplerBufferProvider(), |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 8902 | framesOut); |
| 8903 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8904 | |
| 8905 | if (framesOut > 0 && (overrun == OVERRUN_UNKNOWN)) { |
| 8906 | overrun = OVERRUN_FALSE; |
| 8907 | } |
| 8908 | |
Andy Hung | 93bb573 | 2023-05-04 21:16:34 -0700 | [diff] [blame] | 8909 | // MediaSyncEvent handling: Synchronize AudioRecord to AudioTrack completion. |
| 8910 | const ssize_t framesToDrop = |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8911 | activeTrack->synchronizedRecordState().updateRecordFrames(framesOut); |
Andy Hung | 93bb573 | 2023-05-04 21:16:34 -0700 | [diff] [blame] | 8912 | if (framesToDrop == 0) { |
| 8913 | // no sync event, process normally, otherwise ignore. |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8914 | if (framesOut > 0) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8915 | activeTrack->sinkBuffer().frameCount = framesOut; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 8916 | // Sanitize before releasing if the track has no access to the source data |
| 8917 | // An idle UID receives silence from non virtual devices until active |
| 8918 | if (activeTrack->isSilenced()) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8919 | memset(activeTrack->sinkBuffer().raw, |
| 8920 | 0, framesOut * activeTrack->frameSize()); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 8921 | } |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8922 | activeTrack->releaseBuffer(&activeTrack->sinkBuffer()); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8923 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8924 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8925 | if (framesOut == 0) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8926 | break; |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 8927 | } |
| 8928 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8929 | |
| 8930 | switch (overrun) { |
| 8931 | case OVERRUN_TRUE: |
| 8932 | // client isn't retrieving buffers fast enough |
| 8933 | if (!activeTrack->setOverflow()) { |
| 8934 | nsecs_t now = systemTime(); |
| 8935 | // FIXME should lastWarning per track? |
| 8936 | if ((now - lastWarning) > kWarningThrottleNs) { |
| 8937 | ALOGW("RecordThread: buffer overflow"); |
| 8938 | lastWarning = now; |
| 8939 | } |
| 8940 | } |
| 8941 | break; |
| 8942 | case OVERRUN_FALSE: |
| 8943 | activeTrack->clearOverflow(); |
| 8944 | break; |
| 8945 | case OVERRUN_UNKNOWN: |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8946 | break; |
| 8947 | } |
| 8948 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 8949 | // update frame information and push timestamp out |
| 8950 | activeTrack->updateTrackFrameInfo( |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8951 | activeTrack->serverProxy()->framesReleased(), |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 8952 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER], |
| 8953 | mSampleRate, mTimestamp); |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 8954 | } |
| 8955 | |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 8956 | unlock: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8957 | // enable changes in effect chain |
| 8958 | unlockEffectChains(effectChains); |
Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 8959 | // 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] | 8960 | if (audio_has_proportional_frames(mFormat) |
| 8961 | && loopCount == lastLoopCountRead + 1) { |
| 8962 | const int64_t readPeriodNs = lastIoEndNs - mLastIoEndNs; |
| 8963 | const double jitterMs = |
| 8964 | TimestampVerifier<int64_t, int64_t>::computeJitterMs( |
| 8965 | {framesRead, readPeriodNs}, |
| 8966 | {0, 0} /* lastTimestamp */, mSampleRate); |
| 8967 | const double processMs = (lastIoBeginNs - mLastIoEndNs) * 1e-6; |
| 8968 | |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 8969 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | cccbc76 | 2019-04-05 14:20:05 -0700 | [diff] [blame] | 8970 | mIoJitterMs.add(jitterMs); |
| 8971 | mProcessTimeMs.add(processMs); |
| 8972 | } |
Andy Hung | 56ce2ed | 2024-06-12 16:03:16 -0700 | [diff] [blame] | 8973 | mThreadloopExecutor.process(); |
Eric Laurent | cccbc76 | 2019-04-05 14:20:05 -0700 | [diff] [blame] | 8974 | // update timing info. |
| 8975 | mLastIoBeginNs = lastIoBeginNs; |
| 8976 | mLastIoEndNs = lastIoEndNs; |
| 8977 | lastLoopCountRead = loopCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8978 | } |
Andy Hung | 56ce2ed | 2024-06-12 16:03:16 -0700 | [diff] [blame] | 8979 | mThreadloopExecutor.process(); // process any remaining deferred actions. |
| 8980 | // deferred actions after this point are ignored. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8981 | |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 8982 | standbyIfNotAlreadyInStandby(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8983 | |
| 8984 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 8985 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 8986 | for (size_t i = 0; i < mTracks.size(); i++) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8987 | sp<IAfRecordTrack> track = mTracks[i]; |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 8988 | track->invalidate(); |
| 8989 | } |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 8990 | mActiveTracks.clear(); |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 8991 | mStartStopCV.notify_all(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8992 | } |
| 8993 | |
| 8994 | releaseWakeLock(); |
| 8995 | |
| 8996 | ALOGV("RecordThread %p exiting", this); |
| 8997 | return false; |
| 8998 | } |
| 8999 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9000 | void RecordThread::standbyIfNotAlreadyInStandby() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9001 | { |
| 9002 | if (!mStandby) { |
| 9003 | inputStandBy(); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 9004 | mThreadMetrics.logEndInterval(); |
Andy Hung | 44d648b | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 9005 | mThreadSnapshot.onEnd(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9006 | mStandby = true; |
| 9007 | } |
| 9008 | } |
| 9009 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9010 | void RecordThread::inputStandBy() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9011 | { |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 9012 | // Idle the fast capture if it's currently running |
| 9013 | if (mFastCapture != 0) { |
| 9014 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 9015 | FastCaptureState *state = sq->begin(); |
| 9016 | if (!(state->mCommand & FastCaptureState::IDLE)) { |
| 9017 | state->mCommand = FastCaptureState::COLD_IDLE; |
| 9018 | state->mColdFutexAddr = &mFastCaptureFutex; |
| 9019 | state->mColdGen++; |
| 9020 | mFastCaptureFutex = 0; |
| 9021 | sq->end(); |
| 9022 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now |
Andy Hung | 82f39d6 | 2024-09-30 17:19:14 -0700 | [diff] [blame] | 9023 | { |
| 9024 | audio_utils::mutex::scoped_queue_wait_check queueWaitCheck(mFastCapture->getTid()); |
| 9025 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_ACKED); |
| 9026 | } |
| 9027 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 9028 | #if 0 |
| 9029 | if (kUseFastCapture == FastCapture_Dynamic) { |
| 9030 | // FIXME |
| 9031 | } |
| 9032 | #endif |
| 9033 | #ifdef AUDIO_WATCHDOG |
| 9034 | // FIXME |
| 9035 | #endif |
| 9036 | } else { |
| 9037 | sq->end(false /*didModify*/); |
| 9038 | } |
| 9039 | } |
Mikhail Naganov | 2534b38 | 2019-09-25 13:05:02 -0700 | [diff] [blame] | 9040 | status_t result = mSource->standby(); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 9041 | 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] | 9042 | |
| 9043 | // If going into standby, flush the pipe source. |
| 9044 | if (mPipeSource.get() != nullptr) { |
| 9045 | const ssize_t flushed = mPipeSource->flush(); |
| 9046 | if (flushed > 0) { |
| 9047 | ALOGV("Input standby flushed PipeSource %zd frames", flushed); |
| 9048 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] += flushed; |
| 9049 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = systemTime(); |
| 9050 | } |
| 9051 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9052 | } |
| 9053 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 9054 | // RecordThread::createRecordTrack_l() must be called with AudioFlinger::mutex() held |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9055 | sp<IAfRecordTrack> RecordThread::createRecordTrack_l( |
Andy Hung | 88035ac | 2023-06-27 17:05:02 -0700 | [diff] [blame] | 9056 | const sp<Client>& client, |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 9057 | const audio_attributes_t& attr, |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 9058 | uint32_t *pSampleRate, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9059 | audio_format_t format, |
| 9060 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 9061 | size_t *pFrameCount, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 9062 | audio_session_t sessionId, |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 9063 | size_t *pNotificationFrameCount, |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 9064 | pid_t creatorPid, |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 9065 | const AttributionSourceState& attributionSource, |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 9066 | audio_input_flags_t *flags, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9067 | pid_t tid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 9068 | status_t *status, |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9069 | audio_port_handle_t portId, |
| 9070 | int32_t maxSharedAudioHistoryMs) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9071 | { |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 9072 | size_t frameCount = *pFrameCount; |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 9073 | size_t notificationFrameCount = *pNotificationFrameCount; |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9074 | sp<IAfRecordTrack> track; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9075 | status_t lStatus; |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 9076 | audio_input_flags_t inputFlags = mInput->flags; |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 9077 | audio_input_flags_t requestedFlags = *flags; |
| 9078 | uint32_t sampleRate; |
| 9079 | |
| 9080 | lStatus = initCheck(); |
| 9081 | if (lStatus != NO_ERROR) { |
| 9082 | ALOGE("createRecordTrack_l() audio driver not initialized"); |
| 9083 | goto Exit; |
| 9084 | } |
| 9085 | |
Mikhail Naganov | ce9f265 | 2018-07-16 11:09:24 -0700 | [diff] [blame] | 9086 | if (!audio_is_linear_pcm(mFormat) && (*flags & AUDIO_INPUT_FLAG_DIRECT) == 0) { |
| 9087 | ALOGE("createRecordTrack_l() on an encoded stream requires AUDIO_INPUT_FLAG_DIRECT"); |
| 9088 | lStatus = BAD_VALUE; |
| 9089 | goto Exit; |
| 9090 | } |
| 9091 | |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9092 | if (maxSharedAudioHistoryMs != 0) { |
Eric Laurent | 9ff3e53 | 2022-11-10 16:04:44 +0100 | [diff] [blame] | 9093 | if (!captureHotwordAllowed(attributionSource)) { |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9094 | lStatus = PERMISSION_DENIED; |
| 9095 | goto Exit; |
| 9096 | } |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9097 | if (maxSharedAudioHistoryMs < 0 |
Andy Hung | 25a80ac | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 9098 | || maxSharedAudioHistoryMs > kMaxSharedAudioHistoryMs) { |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9099 | lStatus = BAD_VALUE; |
| 9100 | goto Exit; |
| 9101 | } |
| 9102 | } |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 9103 | if (*pSampleRate == 0) { |
| 9104 | *pSampleRate = mSampleRate; |
| 9105 | } |
| 9106 | sampleRate = *pSampleRate; |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 9107 | |
Eric Laurent | 5f0fd7b | 2021-05-07 16:33:26 +0200 | [diff] [blame] | 9108 | // special case for FAST flag considered OK if fast capture is present and access to |
| 9109 | // audio history is not required |
| 9110 | if (hasFastCapture() && mMaxSharedAudioHistoryMs == 0) { |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 9111 | inputFlags = (audio_input_flags_t)(inputFlags | AUDIO_INPUT_FLAG_FAST); |
| 9112 | } |
| 9113 | |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 9114 | // Check if requested flags are compatible with input stream flags |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 9115 | if ((*flags & inputFlags) != *flags) { |
| 9116 | ALOGW("createRecordTrack_l(): mismatch between requested flags (%08x) and" |
| 9117 | " input flags (%08x)", |
| 9118 | *flags, inputFlags); |
| 9119 | *flags = (audio_input_flags_t)(*flags & inputFlags); |
| 9120 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9121 | |
Eric Laurent | 5f0fd7b | 2021-05-07 16:33:26 +0200 | [diff] [blame] | 9122 | // client expresses a preference for FAST and no access to audio history, |
| 9123 | // but we get the final say |
| 9124 | if (*flags & AUDIO_INPUT_FLAG_FAST && maxSharedAudioHistoryMs == 0) { |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 9125 | if ( |
Glenn Kasten | b7fbf7e | 2015-03-18 12:57:28 -0700 | [diff] [blame] | 9126 | // we formerly checked for a callback handler (non-0 tid), |
| 9127 | // but that is no longer required for TRANSFER_OBTAIN mode |
jiabin | b00edc3 | 2021-08-16 16:27:54 +0000 | [diff] [blame] | 9128 | // 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] | 9129 | // |
Phil Burk | 7ed66a1 | 2019-04-18 13:20:30 -0700 | [diff] [blame] | 9130 | // Frame count is not specified (0), or is less than or equal the pipe depth. |
| 9131 | // It is OK to provide a higher capacity than requested. |
| 9132 | // We will force it to mPipeFramesP2 below. |
| 9133 | (frameCount <= mPipeFramesP2) && |
Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 9134 | // PCM data |
| 9135 | audio_is_linear_pcm(format) && |
Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 9136 | // hardware channel mask |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 9137 | (channelMask == mChannelMask) && |
Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 9138 | // hardware sample rate |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 9139 | (sampleRate == mSampleRate) && |
Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 9140 | // record thread has an associated fast capture |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 9141 | hasFastCapture() && |
| 9142 | // there are sufficient fast track slots available |
| 9143 | mFastTrackAvail |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 9144 | ) { |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 9145 | // check compatibility with audio effects. |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9146 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 9147 | // Do not accept FAST flag if the session has software effects |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 9148 | sp<IAfEffectChain> chain = getEffectChain_l(sessionId); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 9149 | if (chain != 0) { |
Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame] | 9150 | audio_input_flags_t old = *flags; |
| 9151 | chain->checkInputFlagCompatibility(flags); |
| 9152 | if (old != *flags) { |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 9153 | ALOGV("%p AUDIO_INPUT_FLAGS denied by effect old=%#x new=%#x", |
| 9154 | this, (int)old, (int)*flags); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 9155 | } |
| 9156 | } |
Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 9157 | ALOGV_IF((*flags & AUDIO_INPUT_FLAG_FAST) != 0, |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 9158 | "%p AUDIO_INPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu", |
| 9159 | this, frameCount, mFrameCount); |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 9160 | } else { |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 9161 | ALOGV("%p AUDIO_INPUT_FLAG_FAST denied: frameCount=%zu mFrameCount=%zu mPipeFramesP2=%zu " |
| 9162 | "format=%#x isLinear=%d mFormat=%#x channelMask=%#x sampleRate=%u mSampleRate=%u " |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 9163 | "hasFastCapture=%d tid=%d mFastTrackAvail=%d", |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 9164 | this, frameCount, mFrameCount, mPipeFramesP2, |
| 9165 | format, audio_is_linear_pcm(format), mFormat, channelMask, sampleRate, mSampleRate, |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 9166 | hasFastCapture(), tid, mFastTrackAvail); |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 9167 | *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST); |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 9168 | } |
| 9169 | } |
| 9170 | |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 9171 | // If FAST or RAW flags were corrected, ask caller to request new input from audio policy |
| 9172 | if ((*flags & AUDIO_INPUT_FLAG_FAST) != |
| 9173 | (requestedFlags & AUDIO_INPUT_FLAG_FAST)) { |
| 9174 | *flags = (audio_input_flags_t) (*flags & ~(AUDIO_INPUT_FLAG_FAST | AUDIO_INPUT_FLAG_RAW)); |
| 9175 | lStatus = BAD_TYPE; |
| 9176 | goto Exit; |
| 9177 | } |
| 9178 | |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 9179 | // compute track buffer size in frames, and suggest the notification frame count |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 9180 | if (*flags & AUDIO_INPUT_FLAG_FAST) { |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 9181 | // fast track: frame count is exactly the pipe depth |
| 9182 | frameCount = mPipeFramesP2; |
| 9183 | // ignore requested notificationFrames, and always notify exactly once every HAL buffer |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 9184 | notificationFrameCount = mFrameCount; |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 9185 | } else { |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 9186 | // not fast track: max notification period is resampled equivalent of one HAL buffer time |
| 9187 | // or 20 ms if there is a fast capture |
| 9188 | // TODO This could be a roundupRatio inline, and const |
| 9189 | size_t maxNotificationFrames = ((int64_t) (hasFastCapture() ? mSampleRate/50 : mFrameCount) |
| 9190 | * sampleRate + mSampleRate - 1) / mSampleRate; |
| 9191 | // minimum number of notification periods is at least kMinNotifications, |
| 9192 | // and at least kMinMs rounded up to a whole notification period (minNotificationsByMs) |
| 9193 | static const size_t kMinNotifications = 3; |
| 9194 | static const uint32_t kMinMs = 30; |
| 9195 | // TODO This could be a roundupRatio inline |
| 9196 | const size_t minFramesByMs = (sampleRate * kMinMs + 1000 - 1) / 1000; |
| 9197 | // TODO This could be a roundupRatio inline |
| 9198 | const size_t minNotificationsByMs = (minFramesByMs + maxNotificationFrames - 1) / |
| 9199 | maxNotificationFrames; |
| 9200 | const size_t minFrameCount = maxNotificationFrames * |
| 9201 | max(kMinNotifications, minNotificationsByMs); |
| 9202 | frameCount = max(frameCount, minFrameCount); |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 9203 | if (notificationFrameCount == 0 || notificationFrameCount > maxNotificationFrames) { |
| 9204 | notificationFrameCount = maxNotificationFrames; |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 9205 | } |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 9206 | } |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 9207 | *pFrameCount = frameCount; |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 9208 | *pNotificationFrameCount = notificationFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9209 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 9210 | { // scope for mutex() |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9211 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 9212 | int32_t startFrames = -1; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9213 | if (!mSharedAudioPackageName.empty() |
Eric Laurent | 9ff3e53 | 2022-11-10 16:04:44 +0100 | [diff] [blame] | 9214 | && mSharedAudioPackageName == attributionSource.packageName |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9215 | && mSharedAudioSessionId == sessionId |
Eric Laurent | 9ff3e53 | 2022-11-10 16:04:44 +0100 | [diff] [blame] | 9216 | && captureHotwordAllowed(attributionSource)) { |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 9217 | startFrames = mSharedAudioStartFrames; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9218 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9219 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9220 | track = IAfRecordTrack::create(this, client, attr, sampleRate, |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 9221 | format, channelMask, frameCount, |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 9222 | nullptr /* buffer */, (size_t)0 /* bufferSize */, sessionId, creatorPid, |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9223 | attributionSource, *flags, IAfTrackBase::TYPE_DEFAULT, portId, |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 9224 | startFrames); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9225 | |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 9226 | lStatus = track->initCheck(); |
| 9227 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 3529507 | 2013-10-07 09:27:06 -0700 | [diff] [blame] | 9228 | ALOGE("createRecordTrack_l() initCheck failed %d; no control block?", lStatus); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 9229 | // 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] | 9230 | goto Exit; |
| 9231 | } |
| 9232 | mTracks.add(track); |
| 9233 | |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 9234 | if ((*flags & AUDIO_INPUT_FLAG_FAST) && (tid != -1)) { |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 9235 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 9236 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, |
| 9237 | // so ask activity manager to do this on our behalf |
Glenn Kasten | af9a7b5 | 2017-03-29 11:29:39 -0700 | [diff] [blame] | 9238 | sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp, true /*forApp*/); |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 9239 | } |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9240 | |
| 9241 | if (maxSharedAudioHistoryMs != 0) { |
| 9242 | sendResizeBufferConfigEvent_l(maxSharedAudioHistoryMs); |
| 9243 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9244 | } |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 9245 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9246 | lStatus = NO_ERROR; |
| 9247 | |
| 9248 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 9249 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9250 | return track; |
| 9251 | } |
| 9252 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9253 | status_t RecordThread::start(IAfRecordTrack* recordTrack, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9254 | AudioSystem::sync_event_t event, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 9255 | audio_session_t triggerSession) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9256 | { |
| 9257 | ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession); |
| 9258 | sp<ThreadBase> strongMe = this; |
| 9259 | status_t status = NO_ERROR; |
| 9260 | |
| 9261 | if (event == AudioSystem::SYNC_EVENT_NONE) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 9262 | recordTrack->clearSyncStartEvent(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9263 | } else if (event != AudioSystem::SYNC_EVENT_SAME) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9264 | recordTrack->synchronizedRecordState().startRecording( |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 9265 | mAfThreadCallback->createSyncEvent( |
Andy Hung | 93bb573 | 2023-05-04 21:16:34 -0700 | [diff] [blame] | 9266 | event, triggerSession, |
| 9267 | recordTrack->sessionId(), syncStartEventCallback, recordTrack)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9268 | } |
| 9269 | |
| 9270 | { |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 9271 | // This section is a rendezvous between binder thread executing start() and RecordThread |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9272 | audio_utils::lock_guard lock(mutex()); |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9273 | if (recordTrack->isInvalid()) { |
| 9274 | recordTrack->clearSyncStartEvent(); |
Eric Laurent | d52a28c | 2020-08-21 17:10:39 -0700 | [diff] [blame] | 9275 | ALOGW("%s track %d: invalidated before startInput", __func__, recordTrack->portId()); |
| 9276 | return DEAD_OBJECT; |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9277 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 9278 | if (mActiveTracks.indexOf(recordTrack) >= 0) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9279 | if (recordTrack->state() == IAfTrackBase::PAUSING) { |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9280 | // We haven't stopped yet (moved to PAUSED and not in mActiveTracks) |
| 9281 | // so no need to startInput(). |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 9282 | ALOGV("active record track PAUSING -> ACTIVE"); |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9283 | recordTrack->setState(IAfTrackBase::ACTIVE); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 9284 | } else { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9285 | ALOGV("active record track state %d", (int)recordTrack->state()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9286 | } |
| 9287 | return status; |
| 9288 | } |
| 9289 | |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 9290 | // TODO consider other ways of handling this, such as changing the state to :STARTING and |
| 9291 | // adding the track to mActiveTracks after returning from AudioSystem::startInput(), |
| 9292 | // or using a separate command thread |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9293 | recordTrack->setState(IAfTrackBase::STARTING_1); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 9294 | mActiveTracks.add(recordTrack); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 9295 | if (recordTrack->isExternalTrack()) { |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 9296 | mutex().unlock(); |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 9297 | status = AudioSystem::startInput(recordTrack->portId()); |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 9298 | mutex().lock(); |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9299 | if (recordTrack->isInvalid()) { |
| 9300 | recordTrack->clearSyncStartEvent(); |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9301 | if (status == NO_ERROR && recordTrack->state() == IAfTrackBase::STARTING_1) { |
| 9302 | recordTrack->setState(IAfTrackBase::STARTING_2); |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9303 | // STARTING_2 forces destroy to call stopInput. |
| 9304 | } |
Eric Laurent | d52a28c | 2020-08-21 17:10:39 -0700 | [diff] [blame] | 9305 | ALOGW("%s track %d: invalidated after startInput", __func__, recordTrack->portId()); |
| 9306 | return DEAD_OBJECT; |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9307 | } |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9308 | if (recordTrack->state() != IAfTrackBase::STARTING_1) { |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9309 | ALOGW("%s(%d): unsynchronized mState:%d change", |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9310 | __func__, recordTrack->id(), (int)recordTrack->state()); |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9311 | // Someone else has changed state, let them take over, |
| 9312 | // leave mState in the new state. |
| 9313 | recordTrack->clearSyncStartEvent(); |
| 9314 | return INVALID_OPERATION; |
| 9315 | } |
| 9316 | // we're ok, but perhaps startInput has failed |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 9317 | if (status != NO_ERROR) { |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9318 | ALOGW("%s(%d): startInput failed, status %d", |
| 9319 | __func__, recordTrack->id(), status); |
| 9320 | // We are in ActiveTracks if STARTING_1 and valid, so remove from ActiveTracks, |
| 9321 | // leave in STARTING_1, so destroy() will not call stopInput. |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 9322 | mActiveTracks.remove(recordTrack); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 9323 | recordTrack->clearSyncStartEvent(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 9324 | return status; |
| 9325 | } |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 9326 | sendIoConfigEvent_l( |
| 9327 | AUDIO_CLIENT_STARTED, recordTrack->creatorPid(), recordTrack->portId()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9328 | } |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 9329 | |
| 9330 | recordTrack->logBeginInterval(patchSourcesToString(&mPatch)); // log to MediaMetrics |
| 9331 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 9332 | // Catch up with current buffer indices if thread is already running. |
| 9333 | // This is what makes a new client discard all buffered data. If the track's mRsmpInFront |
| 9334 | // was initialized to some value closer to the thread's mRsmpInFront, then the track could |
| 9335 | // see previously buffered data before it called start(), but with greater risk of overrun. |
| 9336 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9337 | recordTrack->resamplerBufferProvider()->reset(); |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 9338 | if (!recordTrack->isDirect()) { |
| 9339 | // clear any converter state as new data will be discontinuous |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9340 | recordTrack->recordBufferConverter()->reset(); |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 9341 | } |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9342 | recordTrack->setState(IAfTrackBase::STARTING_2); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9343 | // signal thread to start |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 9344 | mWaitWorkCV.notify_all(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9345 | return status; |
| 9346 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9347 | } |
| 9348 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9349 | void RecordThread::syncStartEventCallback(const wp<SyncEvent>& event) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9350 | { |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9351 | const sp<SyncEvent> strongEvent = event.promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9352 | |
| 9353 | if (strongEvent != 0) { |
Andy Hung | d29af63 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 9354 | sp<IAfTrackBase> ptr = |
| 9355 | std::any_cast<const wp<IAfTrackBase>>(strongEvent->cookie()).promote(); |
| 9356 | if (ptr != nullptr) { |
Andy Hung | 99b1ba6 | 2023-07-14 11:00:08 -0700 | [diff] [blame] | 9357 | // TODO(b/291317898) handleSyncStartEvent is in IAfTrackBase not IAfRecordTrack. |
Andy Hung | d29af63 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 9358 | ptr->handleSyncStartEvent(strongEvent); |
Eric Laurent | 8ea16e4 | 2014-02-20 16:26:11 -0800 | [diff] [blame] | 9359 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9360 | } |
| 9361 | } |
| 9362 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9363 | bool RecordThread::stop(IAfRecordTrack* recordTrack) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9364 | ALOGV("RecordThread::stop"); |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 9365 | audio_utils::unique_lock _l(mutex()); |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9366 | // if we're invalid, we can't be on the ActiveTracks. |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9367 | if (mActiveTracks.indexOf(recordTrack) < 0 || recordTrack->state() == IAfTrackBase::PAUSING) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9368 | return false; |
| 9369 | } |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 9370 | // note that threadLoop may still be processing the track at this point [without lock] |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9371 | recordTrack->setState(IAfTrackBase::PAUSING); |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9372 | |
Andy Hung | abfab20 | 2019-03-07 19:45:54 -0800 | [diff] [blame] | 9373 | // NOTE: Waiting here is important to keep stop synchronous. |
| 9374 | // This is needed for proper patchRecord peer release. |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9375 | while (recordTrack->state() == IAfTrackBase::PAUSING && !recordTrack->isInvalid()) { |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 9376 | mWaitWorkCV.notify_all(); // signal thread to stop |
Andy Hung | 77b1bb4 | 2024-05-06 12:16:36 -0700 | [diff] [blame] | 9377 | mStartStopCV.wait(_l, getTid()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9378 | } |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9379 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9380 | if (recordTrack->state() == IAfTrackBase::PAUSED) { // successful stop |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9381 | ALOGV("Record stopped OK"); |
| 9382 | return true; |
| 9383 | } |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9384 | |
| 9385 | // don't handle anything - we've been invalidated or restarted and in a different state |
| 9386 | ALOGW_IF("%s(%d): unsynchronized stop, state: %d", |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9387 | __func__, recordTrack->id(), recordTrack->state()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9388 | return false; |
| 9389 | } |
| 9390 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9391 | bool RecordThread::isValidSyncEvent(const sp<SyncEvent>& /* event */) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9392 | { |
| 9393 | return false; |
| 9394 | } |
| 9395 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9396 | status_t RecordThread::setSyncEvent(const sp<SyncEvent>& /* event */) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9397 | { |
| 9398 | #if 0 // This branch is currently dead code, but is preserved in case it will be needed in future |
| 9399 | if (!isValidSyncEvent(event)) { |
| 9400 | return BAD_VALUE; |
| 9401 | } |
| 9402 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 9403 | audio_session_t eventSession = event->triggerSession(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9404 | status_t ret = NAME_NOT_FOUND; |
| 9405 | |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9406 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9407 | |
| 9408 | for (size_t i = 0; i < mTracks.size(); i++) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9409 | sp<IAfRecordTrack> track = mTracks[i]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9410 | if (eventSession == track->sessionId()) { |
| 9411 | (void) track->setSyncEvent(event); |
| 9412 | ret = NO_ERROR; |
| 9413 | } |
| 9414 | } |
| 9415 | return ret; |
| 9416 | #else |
| 9417 | return BAD_VALUE; |
| 9418 | #endif |
| 9419 | } |
| 9420 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9421 | status_t RecordThread::getActiveMicrophones( |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 9422 | std::vector<media::MicrophoneInfoFw>* activeMicrophones) const |
jiabin | 653cc0a | 2018-01-17 17:54:10 -0800 | [diff] [blame] | 9423 | { |
| 9424 | ALOGV("RecordThread::getActiveMicrophones"); |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9425 | audio_utils::lock_guard _l(mutex()); |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 9426 | if (!isStreamInitialized()) { |
Paul McLean | 8a661a3 | 2021-04-12 10:21:42 -0600 | [diff] [blame] | 9427 | return NO_INIT; |
| 9428 | } |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 9429 | status_t status = mInput->stream->getActiveMicrophones(activeMicrophones); |
| 9430 | return status; |
jiabin | 653cc0a | 2018-01-17 17:54:10 -0800 | [diff] [blame] | 9431 | } |
| 9432 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9433 | status_t RecordThread::setPreferredMicrophoneDirection( |
Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 9434 | audio_microphone_direction_t direction) |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 9435 | { |
Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 9436 | ALOGV("setPreferredMicrophoneDirection(%d)", direction); |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9437 | audio_utils::lock_guard _l(mutex()); |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 9438 | if (!isStreamInitialized()) { |
Paul McLean | 8a661a3 | 2021-04-12 10:21:42 -0600 | [diff] [blame] | 9439 | return NO_INIT; |
| 9440 | } |
Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 9441 | return mInput->stream->setPreferredMicrophoneDirection(direction); |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 9442 | } |
| 9443 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9444 | status_t RecordThread::setPreferredMicrophoneFieldDimension(float zoom) |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 9445 | { |
Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 9446 | ALOGV("setPreferredMicrophoneFieldDimension(%f)", zoom); |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9447 | audio_utils::lock_guard _l(mutex()); |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 9448 | if (!isStreamInitialized()) { |
Paul McLean | 8a661a3 | 2021-04-12 10:21:42 -0600 | [diff] [blame] | 9449 | return NO_INIT; |
| 9450 | } |
Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 9451 | return mInput->stream->setPreferredMicrophoneFieldDimension(zoom); |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 9452 | } |
| 9453 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9454 | status_t RecordThread::shareAudioHistory( |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9455 | const std::string& sharedAudioPackageName, audio_session_t sharedSessionId, |
| 9456 | int64_t sharedAudioStartMs) { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9457 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9458 | return shareAudioHistory_l(sharedAudioPackageName, sharedSessionId, sharedAudioStartMs); |
| 9459 | } |
| 9460 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9461 | status_t RecordThread::shareAudioHistory_l( |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9462 | const std::string& sharedAudioPackageName, audio_session_t sharedSessionId, |
| 9463 | int64_t sharedAudioStartMs) { |
Eric Laurent | 92d0a32 | 2021-07-16 15:32:33 +0200 | [diff] [blame] | 9464 | |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9465 | if ((hasAudioSession_l(sharedSessionId) & ThreadBase::TRACK_SESSION) == 0) { |
| 9466 | return BAD_VALUE; |
| 9467 | } |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 9468 | |
| 9469 | if (sharedAudioStartMs < 0 |
| 9470 | || sharedAudioStartMs > INT64_MAX / mSampleRate) { |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9471 | return BAD_VALUE; |
| 9472 | } |
| 9473 | |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 9474 | // Current implementation of the input resampling buffer wraps around indexes at 32 bit. |
| 9475 | // As we cannot detect more than one wraparound, only accept values up current write position |
| 9476 | // after one wraparound |
| 9477 | // We assume recent wraparounds on mRsmpInRear only given it is unlikely that the requesting |
| 9478 | // app waits several hours after the start time was computed. |
Eric Laurent | 92d0a32 | 2021-07-16 15:32:33 +0200 | [diff] [blame] | 9479 | int64_t sharedAudioStartFrames = sharedAudioStartMs * mSampleRate / 1000; |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 9480 | const int32_t sharedOffset = audio_utils::safe_sub_overflow(mRsmpInRear, |
| 9481 | (int32_t)sharedAudioStartFrames); |
Eric Laurent | 92d0a32 | 2021-07-16 15:32:33 +0200 | [diff] [blame] | 9482 | // Bring the start frame position within the input buffer to match the documented |
| 9483 | // "best effort" behavior of the API. |
| 9484 | if (sharedOffset < 0) { |
| 9485 | sharedAudioStartFrames = mRsmpInRear; |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 9486 | } else if (sharedOffset > static_cast<signed>(mRsmpInFrames)) { |
Eric Laurent | 92d0a32 | 2021-07-16 15:32:33 +0200 | [diff] [blame] | 9487 | sharedAudioStartFrames = |
| 9488 | audio_utils::safe_sub_overflow(mRsmpInRear, (int32_t)mRsmpInFrames); |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 9489 | } |
| 9490 | |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9491 | mSharedAudioPackageName = sharedAudioPackageName; |
| 9492 | if (mSharedAudioPackageName.empty()) { |
Eric Laurent | 92d0a32 | 2021-07-16 15:32:33 +0200 | [diff] [blame] | 9493 | resetAudioHistory_l(); |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9494 | } else { |
| 9495 | mSharedAudioSessionId = sharedSessionId; |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 9496 | mSharedAudioStartFrames = (int32_t)sharedAudioStartFrames; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9497 | } |
| 9498 | return NO_ERROR; |
| 9499 | } |
| 9500 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9501 | void RecordThread::resetAudioHistory_l() { |
Eric Laurent | 92d0a32 | 2021-07-16 15:32:33 +0200 | [diff] [blame] | 9502 | mSharedAudioSessionId = AUDIO_SESSION_NONE; |
| 9503 | mSharedAudioStartFrames = -1; |
| 9504 | mSharedAudioPackageName = ""; |
| 9505 | } |
| 9506 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9507 | ThreadBase::MetadataUpdate RecordThread::updateMetadata_l() |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 9508 | { |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 9509 | if (!isStreamInitialized() || !mActiveTracks.readAndClearHasChanged()) { |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 9510 | return {}; // nothing to do |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 9511 | } |
| 9512 | StreamInHalInterface::SinkMetadata metadata; |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 9513 | auto backInserter = std::back_inserter(metadata.tracks); |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9514 | for (const sp<IAfRecordTrack>& track : mActiveTracks) { |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 9515 | track->copyMetadataTo(backInserter); |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 9516 | } |
| 9517 | mInput->stream->updateSinkMetadata(metadata); |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 9518 | MetadataUpdate change; |
| 9519 | change.recordMetadataUpdate = metadata.tracks; |
| 9520 | return change; |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 9521 | } |
| 9522 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 9523 | // destroyTrack_l() must be called with ThreadBase::mutex() held |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9524 | void RecordThread::destroyTrack_l(const sp<IAfRecordTrack>& track) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9525 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 9526 | track->terminate(); |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9527 | track->setState(IAfTrackBase::STOPPED); |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9528 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9529 | // active tracks are removed by threadLoop() |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 9530 | if (mActiveTracks.indexOf(track) < 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9531 | removeTrack_l(track); |
| 9532 | } |
| 9533 | } |
| 9534 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9535 | void RecordThread::removeTrack_l(const sp<IAfRecordTrack>& track) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9536 | { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 9537 | String8 result; |
| 9538 | track->appendDump(result, false /* active */); |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 9539 | mLocalLog.log("removeTrack_l (%p) %s", track.get(), result.c_str()); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 9540 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9541 | mTracks.remove(track); |
| 9542 | // need anything related to effects here? |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 9543 | if (track->isFastTrack()) { |
| 9544 | ALOG_ASSERT(!mFastTrackAvail); |
| 9545 | mFastTrackAvail = true; |
| 9546 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9547 | } |
| 9548 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9549 | void RecordThread::dumpInternals_l(int fd, const Vector<String16>& /* args */) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9550 | { |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 9551 | AudioStreamIn *input = mInput; |
| 9552 | audio_input_flags_t flags = input != NULL ? input->flags : AUDIO_INPUT_FLAG_NONE; |
| 9553 | dprintf(fd, " AudioStreamIn: %p flags %#x (%s)\n", |
Andy Hung | 9b18195 | 2019-02-25 14:53:36 -0800 | [diff] [blame] | 9554 | input, flags, toString(flags).c_str()); |
Andy Hung | 6427e44 | 2018-08-09 12:51:02 -0700 | [diff] [blame] | 9555 | dprintf(fd, " Frames read: %lld\n", (long long)mFramesRead); |
Eric Tan | 39ec8d6 | 2018-07-24 09:49:29 -0700 | [diff] [blame] | 9556 | if (mActiveTracks.isEmpty()) { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 9557 | dprintf(fd, " No active record clients\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9558 | } |
Andy Hung | bfa6496 | 2017-06-12 14:43:19 -0700 | [diff] [blame] | 9559 | |
| 9560 | if (input != nullptr) { |
| 9561 | dprintf(fd, " Hal stream dump:\n"); |
| 9562 | (void)input->stream->dump(fd); |
| 9563 | } |
| 9564 | |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 9565 | dprintf(fd, " Fast capture thread: %s\n", hasFastCapture() ? "yes" : "no"); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 9566 | dprintf(fd, " Fast track available: %s\n", mFastTrackAvail ? "yes" : "no"); |
Glenn Kasten | 17c9c99 | 2015-03-02 15:53:01 -0800 | [diff] [blame] | 9567 | |
Glenn Kasten | 2f90c51 | 2015-12-02 11:40:09 -0800 | [diff] [blame] | 9568 | // Make a non-atomic copy of fast capture dump state so it won't change underneath us |
| 9569 | // while we are dumping it. It may be inconsistent, but it won't mutate! |
| 9570 | // This is a large object so we place it on the heap. |
| 9571 | // 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] | 9572 | const std::unique_ptr<FastCaptureDumpState> copy = |
| 9573 | std::make_unique<FastCaptureDumpState>(mFastCaptureDumpState); |
Glenn Kasten | 2f90c51 | 2015-12-02 11:40:09 -0800 | [diff] [blame] | 9574 | copy->dump(fd); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9575 | } |
| 9576 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9577 | void RecordThread::dumpTracks_l(int fd, const Vector<String16>& /* args */) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9578 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9579 | String8 result; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9580 | size_t numtracks = mTracks.size(); |
| 9581 | size_t numactive = mActiveTracks.size(); |
| 9582 | size_t numactiveseen = 0; |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 9583 | dprintf(fd, " %zu Tracks", numtracks); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 9584 | const char *prefix = " "; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9585 | if (numtracks) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 9586 | dprintf(fd, " of which %zu are active\n", numactive); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 9587 | result.append(prefix); |
Andy Hung | 000adb5 | 2018-06-01 15:43:26 -0700 | [diff] [blame] | 9588 | mTracks[0]->appendDumpHeader(result); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9589 | for (size_t i = 0; i < numtracks ; ++i) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9590 | sp<IAfRecordTrack> track = mTracks[i]; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9591 | if (track != 0) { |
| 9592 | bool active = mActiveTracks.indexOf(track) >= 0; |
| 9593 | if (active) { |
| 9594 | numactiveseen++; |
| 9595 | } |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 9596 | result.append(prefix); |
| 9597 | track->appendDump(result, active); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9598 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9599 | } |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9600 | } else { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 9601 | dprintf(fd, "\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9602 | } |
| 9603 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9604 | if (numactiveseen != numactive) { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 9605 | result.append(" The following tracks are in the active list but" |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9606 | " not in the track list\n"); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 9607 | result.append(prefix); |
Andy Hung | 000adb5 | 2018-06-01 15:43:26 -0700 | [diff] [blame] | 9608 | mActiveTracks[0]->appendDumpHeader(result); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9609 | for (size_t i = 0; i < numactive; ++i) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9610 | sp<IAfRecordTrack> track = mActiveTracks[i]; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9611 | if (mTracks.indexOf(track) < 0) { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 9612 | result.append(prefix); |
| 9613 | track->appendDump(result, true /* active */); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9614 | } |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 9615 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9616 | |
| 9617 | } |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 9618 | write(fd, result.c_str(), result.size()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9619 | } |
| 9620 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9621 | void RecordThread::setRecordSilenced(audio_port_handle_t portId, bool silenced) |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 9622 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9623 | audio_utils::lock_guard _l(mutex()); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 9624 | for (size_t i = 0; i < mTracks.size() ; i++) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9625 | sp<IAfRecordTrack> track = mTracks[i]; |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 9626 | if (track != 0 && track->portId() == portId) { |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 9627 | track->setSilenced(silenced); |
| 9628 | } |
| 9629 | } |
| 9630 | } |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9631 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9632 | void ResamplerBufferProvider::reset() |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9633 | { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 9634 | const auto threadBase = mRecordTrack->thread().promote(); |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9635 | auto* const recordThread = static_cast<RecordThread *>(threadBase->asIAfRecordThread().get()); |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9636 | mRsmpInUnrel = 0; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9637 | const int32_t rear = recordThread->mRsmpInRear; |
| 9638 | ssize_t deltaFrames = 0; |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 9639 | if (mRecordTrack->startFrames() >= 0) { |
| 9640 | int32_t startFrames = mRecordTrack->startFrames(); |
| 9641 | // Accept a recent wraparound of mRsmpInRear |
| 9642 | if (startFrames <= rear) { |
| 9643 | deltaFrames = rear - startFrames; |
| 9644 | } else { |
| 9645 | deltaFrames = (int32_t)((int64_t)rear + UINT32_MAX + 1 - startFrames); |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9646 | } |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9647 | // start frame cannot be further in the past than start of resampling buffer |
| 9648 | if ((size_t) deltaFrames > recordThread->mRsmpInFrames) { |
| 9649 | deltaFrames = recordThread->mRsmpInFrames; |
| 9650 | } |
| 9651 | } |
| 9652 | mRsmpInFront = audio_utils::safe_sub_overflow(rear, static_cast<int32_t>(deltaFrames)); |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9653 | } |
| 9654 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9655 | void ResamplerBufferProvider::sync( |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9656 | size_t *framesAvailable, bool *hasOverrun) |
| 9657 | { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 9658 | const auto threadBase = mRecordTrack->thread().promote(); |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9659 | auto* const recordThread = static_cast<RecordThread *>(threadBase->asIAfRecordThread().get()); |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9660 | const int32_t rear = recordThread->mRsmpInRear; |
| 9661 | const int32_t front = mRsmpInFront; |
Hongwei Wang | 95e3768 | 2019-04-12 11:13:36 -0700 | [diff] [blame] | 9662 | const ssize_t filled = audio_utils::safe_sub_overflow(rear, front); |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9663 | |
| 9664 | size_t framesIn; |
| 9665 | bool overrun = false; |
| 9666 | if (filled < 0) { |
| 9667 | // should not happen, but treat like a massive overrun and re-sync |
| 9668 | framesIn = 0; |
| 9669 | mRsmpInFront = rear; |
| 9670 | overrun = true; |
| 9671 | } else if ((size_t) filled <= recordThread->mRsmpInFrames) { |
| 9672 | framesIn = (size_t) filled; |
| 9673 | } else { |
| 9674 | // client is not keeping up with server, but give it latest data |
| 9675 | framesIn = recordThread->mRsmpInFrames; |
Hongwei Wang | 95e3768 | 2019-04-12 11:13:36 -0700 | [diff] [blame] | 9676 | mRsmpInFront = /* front = */ audio_utils::safe_sub_overflow( |
| 9677 | rear, static_cast<int32_t>(framesIn)); |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9678 | overrun = true; |
| 9679 | } |
| 9680 | if (framesAvailable != NULL) { |
| 9681 | *framesAvailable = framesIn; |
| 9682 | } |
| 9683 | if (hasOverrun != NULL) { |
| 9684 | *hasOverrun = overrun; |
| 9685 | } |
| 9686 | } |
| 9687 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9688 | // AudioBufferProvider interface |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9689 | status_t ResamplerBufferProvider::getNextBuffer( |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 9690 | AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9691 | { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 9692 | const auto threadBase = mRecordTrack->thread().promote(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 9693 | if (threadBase == 0) { |
| 9694 | buffer->frameCount = 0; |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 9695 | buffer->raw = NULL; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 9696 | return NOT_ENOUGH_DATA; |
| 9697 | } |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9698 | auto* const recordThread = static_cast<RecordThread *>(threadBase->asIAfRecordThread().get()); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 9699 | int32_t rear = recordThread->mRsmpInRear; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9700 | int32_t front = mRsmpInFront; |
Hongwei Wang | 95e3768 | 2019-04-12 11:13:36 -0700 | [diff] [blame] | 9701 | ssize_t filled = audio_utils::safe_sub_overflow(rear, front); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 9702 | // FIXME should not be P2 (don't want to increase latency) |
| 9703 | // FIXME if client not keeping up, discard |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 9704 | LOG_ALWAYS_FATAL_IF(!(0 <= filled && (size_t) filled <= recordThread->mRsmpInFrames)); |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 9705 | // 'filled' may be non-contiguous, so return only the first contiguous chunk |
Eric Laurent | 5f0fd7b | 2021-05-07 16:33:26 +0200 | [diff] [blame] | 9706 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 9707 | front &= recordThread->mRsmpInFramesP2 - 1; |
| 9708 | size_t part1 = recordThread->mRsmpInFramesP2 - front; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 9709 | if (part1 > (size_t) filled) { |
| 9710 | part1 = filled; |
| 9711 | } |
| 9712 | size_t ask = buffer->frameCount; |
| 9713 | ALOG_ASSERT(ask > 0); |
| 9714 | if (part1 > ask) { |
| 9715 | part1 = ask; |
| 9716 | } |
| 9717 | if (part1 == 0) { |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9718 | // 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] | 9719 | buffer->raw = NULL; |
| 9720 | buffer->frameCount = 0; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9721 | mRsmpInUnrel = 0; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 9722 | return NOT_ENOUGH_DATA; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9723 | } |
| 9724 | |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 9725 | buffer->raw = (uint8_t*)recordThread->mRsmpInBuffer + front * recordThread->mFrameSize; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 9726 | buffer->frameCount = part1; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9727 | mRsmpInUnrel = part1; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9728 | return NO_ERROR; |
| 9729 | } |
| 9730 | |
| 9731 | // AudioBufferProvider interface |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9732 | void ResamplerBufferProvider::releaseBuffer( |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 9733 | AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9734 | { |
Hongwei Wang | 95e3768 | 2019-04-12 11:13:36 -0700 | [diff] [blame] | 9735 | int32_t stepCount = static_cast<int32_t>(buffer->frameCount); |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 9736 | if (stepCount == 0) { |
| 9737 | return; |
| 9738 | } |
Eric Laurent | 1e28aaa | 2023-04-16 19:34:23 +0200 | [diff] [blame] | 9739 | ALOG_ASSERT(stepCount <= (int32_t)mRsmpInUnrel); |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9740 | mRsmpInUnrel -= stepCount; |
Hongwei Wang | 95e3768 | 2019-04-12 11:13:36 -0700 | [diff] [blame] | 9741 | mRsmpInFront = audio_utils::safe_add_overflow(mRsmpInFront, stepCount); |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 9742 | buffer->raw = NULL; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9743 | buffer->frameCount = 0; |
| 9744 | } |
| 9745 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9746 | void RecordThread::checkBtNrec() |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 9747 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9748 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 9749 | checkBtNrec_l(); |
| 9750 | } |
| 9751 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9752 | void RecordThread::checkBtNrec_l() |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 9753 | { |
| 9754 | // disable AEC and NS if the device is a BT SCO headset supporting those |
| 9755 | // pre processings |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 9756 | bool suspend = audio_is_bluetooth_sco_device(inDeviceType_l()) && |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 9757 | mAfThreadCallback->btNrecIsOff(); |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 9758 | if (mBtNrecSuspended.exchange(suspend) != suspend) { |
| 9759 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 9760 | setEffectSuspended_l(FX_IID_AEC, suspend, mEffectChains[i]->sessionId()); |
| 9761 | setEffectSuspended_l(FX_IID_NS, suspend, mEffectChains[i]->sessionId()); |
| 9762 | } |
| 9763 | } |
| 9764 | } |
| 9765 | |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 9766 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9767 | bool RecordThread::checkForNewParameter_l(const String8& keyValuePair, |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9768 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9769 | { |
| 9770 | bool reconfig = false; |
| 9771 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9772 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9773 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9774 | audio_format_t reqFormat = mFormat; |
| 9775 | uint32_t samplingRate = mSampleRate; |
Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 9776 | // 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] | 9777 | [[maybe_unused]] audio_channel_mask_t channelMask = |
| 9778 | audio_channel_in_mask_from_count(mChannelCount); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9779 | |
| 9780 | AudioParameter param = AudioParameter(keyValuePair); |
| 9781 | int value; |
Haynes Mathew George | 9ce67b5 | 2015-09-30 11:40:47 -0700 | [diff] [blame] | 9782 | |
| 9783 | // scope for AutoPark extends to end of method |
| 9784 | AutoPark<FastCapture> park(mFastCapture); |
| 9785 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9786 | // TODO Investigate when this code runs. Check with audio policy when a sample rate and |
| 9787 | // channel count change can be requested. Do we mandate the first client defines the |
| 9788 | // HAL sampling rate and channel count or do we allow changes on the fly? |
| 9789 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 9790 | samplingRate = value; |
| 9791 | reconfig = true; |
| 9792 | } |
| 9793 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 9794 | if (!audio_is_linear_pcm((audio_format_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9795 | status = BAD_VALUE; |
| 9796 | } else { |
| 9797 | reqFormat = (audio_format_t) value; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9798 | reconfig = true; |
| 9799 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9800 | } |
| 9801 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
| 9802 | audio_channel_mask_t mask = (audio_channel_mask_t) value; |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 9803 | if (!audio_is_input_channel(mask) || |
Andy Hung | 936845a | 2021-06-08 00:09:06 -0700 | [diff] [blame] | 9804 | audio_channel_count_from_in_mask(mask) > FCC_LIMIT) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9805 | status = BAD_VALUE; |
| 9806 | } else { |
| 9807 | channelMask = mask; |
| 9808 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9809 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9810 | } |
| 9811 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 9812 | // do not accept frame count changes if tracks are open as the track buffer |
| 9813 | // size depends on frame count and correct behavior would not be guaranteed |
| 9814 | // if frame count is changed after track creation |
| 9815 | if (mActiveTracks.size() > 0) { |
| 9816 | status = INVALID_OPERATION; |
| 9817 | } else { |
| 9818 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9819 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9820 | } |
| 9821 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 9822 | LOG_FATAL("Should not set routing device in RecordThread"); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9823 | } |
| 9824 | if (param.getInt(String8(AudioParameter::keyInputSource), value) == NO_ERROR && |
| 9825 | mAudioSource != (audio_source_t)value) { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 9826 | LOG_FATAL("Should not set audio source in RecordThread"); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9827 | } |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 9828 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9829 | if (status == NO_ERROR) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 9830 | status = mInput->stream->setParameters(keyValuePair); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9831 | if (status == INVALID_OPERATION) { |
| 9832 | inputStandBy(); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 9833 | status = mInput->stream->setParameters(keyValuePair); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9834 | } |
| 9835 | if (reconfig) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 9836 | if (status == BAD_VALUE) { |
Mikhail Naganov | 560637e | 2021-03-31 22:40:13 +0000 | [diff] [blame] | 9837 | audio_config_base_t config = AUDIO_CONFIG_BASE_INITIALIZER; |
| 9838 | if (mInput->stream->getAudioProperties(&config) == OK && |
| 9839 | audio_is_linear_pcm(config.format) && audio_is_linear_pcm(reqFormat) && |
| 9840 | config.sample_rate <= (AUDIO_RESAMPLER_DOWN_RATIO_MAX * samplingRate) && |
Andy Hung | 936845a | 2021-06-08 00:09:06 -0700 | [diff] [blame] | 9841 | audio_channel_count_from_in_mask(config.channel_mask) <= FCC_LIMIT) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 9842 | status = NO_ERROR; |
| 9843 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9844 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9845 | if (status == NO_ERROR) { |
| 9846 | readInputParameters_l(); |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 9847 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9848 | } |
| 9849 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9850 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9851 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9852 | return reconfig; |
| 9853 | } |
| 9854 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9855 | String8 RecordThread::getParameters(const String8& keys) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9856 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9857 | audio_utils::lock_guard _l(mutex()); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 9858 | if (initCheck() == NO_ERROR) { |
| 9859 | String8 out_s8; |
| 9860 | if (mInput->stream->getParameters(keys, &out_s8) == OK) { |
| 9861 | return out_s8; |
| 9862 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9863 | } |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 9864 | return {}; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9865 | } |
| 9866 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 9867 | 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] | 9868 | audio_port_handle_t portId) { |
Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 9869 | sp<AudioIoDescriptor> desc; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9870 | switch (event) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 9871 | case AUDIO_INPUT_OPENED: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 9872 | case AUDIO_INPUT_REGISTERED: |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 9873 | case AUDIO_INPUT_CONFIG_CHANGED: |
Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 9874 | desc = sp<AudioIoDescriptor>::make(mId, mPatch, true /*isInput*/, |
| 9875 | mSampleRate, mFormat, mChannelMask, mFrameCount, mFrameCount); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9876 | break; |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 9877 | case AUDIO_CLIENT_STARTED: |
Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 9878 | desc = sp<AudioIoDescriptor>::make(mId, mPatch, portId); |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 9879 | break; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 9880 | case AUDIO_INPUT_CLOSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9881 | default: |
Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 9882 | desc = sp<AudioIoDescriptor>::make(mId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9883 | break; |
| 9884 | } |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 9885 | mAfThreadCallback->ioConfigChanged_l(event, desc, pid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9886 | } |
| 9887 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9888 | void RecordThread::readInputParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9889 | { |
Dean Wheatley | 6c00951 | 2023-10-23 09:34:14 +1100 | [diff] [blame] | 9890 | const audio_config_base_t audioConfig = mInput->getAudioProperties(); |
| 9891 | mSampleRate = audioConfig.sample_rate; |
| 9892 | mChannelMask = audioConfig.channel_mask; |
| 9893 | if (!audio_is_input_channel(mChannelMask)) { |
| 9894 | LOG_ALWAYS_FATAL("Channel mask %#x not valid for input", mChannelMask); |
| 9895 | } |
| 9896 | |
Mikhail Naganov | ce9f265 | 2018-07-16 11:09:24 -0700 | [diff] [blame] | 9897 | mChannelCount = audio_channel_count_from_in_mask(mChannelMask); |
Dean Wheatley | 6c00951 | 2023-10-23 09:34:14 +1100 | [diff] [blame] | 9898 | |
| 9899 | // Get actual HAL format. |
| 9900 | status_t result = mInput->stream->getAudioProperties(nullptr, nullptr, &mHALFormat); |
| 9901 | LOG_ALWAYS_FATAL_IF(result != OK, "Error when retrieving input stream format: %d", result); |
| 9902 | // Get format from the shim, which will be different than the HAL format |
| 9903 | // if recording compressed audio from IEC61937 wrapped sources. |
| 9904 | mFormat = audioConfig.format; |
| 9905 | if (!audio_is_valid_format(mFormat)) { |
| 9906 | LOG_ALWAYS_FATAL("Format %#x not valid for input", mFormat); |
| 9907 | } |
Mikhail Naganov | ce9f265 | 2018-07-16 11:09:24 -0700 | [diff] [blame] | 9908 | if (audio_is_linear_pcm(mFormat)) { |
Andy Hung | 936845a | 2021-06-08 00:09:06 -0700 | [diff] [blame] | 9909 | LOG_ALWAYS_FATAL_IF(mChannelCount > FCC_LIMIT, "HAL channel count %d > %d", |
| 9910 | mChannelCount, FCC_LIMIT); |
Mikhail Naganov | ce9f265 | 2018-07-16 11:09:24 -0700 | [diff] [blame] | 9911 | } else { |
Andy Hung | 936845a | 2021-06-08 00:09:06 -0700 | [diff] [blame] | 9912 | // Can have more that FCC_LIMIT channels in encoded streams. |
Mikhail Naganov | ce9f265 | 2018-07-16 11:09:24 -0700 | [diff] [blame] | 9913 | ALOGI("HAL format %#x is not linear pcm", mFormat); |
| 9914 | } |
Dean Wheatley | 6c00951 | 2023-10-23 09:34:14 +1100 | [diff] [blame] | 9915 | mFrameSize = mInput->getFrameSize(); |
Hayden Gomes | 1a89ab3 | 2020-06-12 11:05:47 -0700 | [diff] [blame] | 9916 | LOG_ALWAYS_FATAL_IF(mFrameSize <= 0, "Error frame size was %zu but must be greater than zero", |
| 9917 | mFrameSize); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 9918 | result = mInput->stream->getBufferSize(&mBufferSize); |
| 9919 | 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] | 9920 | mFrameCount = mBufferSize / mFrameSize; |
Hayden Gomes | 1a89ab3 | 2020-06-12 11:05:47 -0700 | [diff] [blame] | 9921 | ALOGV("%p RecordThread params: mChannelCount=%u, mFormat=%#x, mFrameSize=%zu, " |
| 9922 | "mBufferSize=%zu, mFrameCount=%zu", |
| 9923 | this, mChannelCount, mFormat, mFrameSize, mBufferSize, mFrameCount); |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 9924 | |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9925 | // mRsmpInFrames must be 0 before calling resizeInputBuffer_l for the first time |
| 9926 | mRsmpInFrames = 0; |
Eric Laurent | 5f0fd7b | 2021-05-07 16:33:26 +0200 | [diff] [blame] | 9927 | resizeInputBuffer_l(0 /*maxSharedAudioHistoryMs*/); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9928 | |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 9929 | // AudioRecord mSampleRate and mChannelCount are constant due to AudioRecord API constraints. |
| 9930 | // 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] | 9931 | |
| 9932 | audio_input_flags_t flags = mInput->flags; |
| 9933 | mediametrics::LogItem item(mThreadMetrics.getMetricsId()); |
| 9934 | item.set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_READPARAMETERS) |
Andy Hung | 25a80ac | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 9935 | .set(AMEDIAMETRICS_PROP_ENCODING, IAfThreadBase::formatToString(mFormat).c_str()) |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 9936 | .set(AMEDIAMETRICS_PROP_FLAGS, toString(flags).c_str()) |
| 9937 | .set(AMEDIAMETRICS_PROP_SAMPLERATE, (int32_t)mSampleRate) |
| 9938 | .set(AMEDIAMETRICS_PROP_CHANNELMASK, (int32_t)mChannelMask) |
| 9939 | .set(AMEDIAMETRICS_PROP_CHANNELCOUNT, (int32_t)mChannelCount) |
| 9940 | .set(AMEDIAMETRICS_PROP_FRAMECOUNT, (int32_t)mFrameCount) |
| 9941 | .record(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9942 | } |
| 9943 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9944 | uint32_t RecordThread::getInputFramesLost() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9945 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9946 | audio_utils::lock_guard _l(mutex()); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 9947 | uint32_t result; |
| 9948 | if (initCheck() == NO_ERROR && mInput->stream->getInputFramesLost(&result) == OK) { |
| 9949 | return result; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9950 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 9951 | return 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9952 | } |
| 9953 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9954 | KeyedVector<audio_session_t, bool> RecordThread::sessionIds() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9955 | { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 9956 | KeyedVector<audio_session_t, bool> ids; |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9957 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9958 | for (size_t j = 0; j < mTracks.size(); ++j) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9959 | sp<IAfRecordTrack> track = mTracks[j]; |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 9960 | audio_session_t sessionId = track->sessionId(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9961 | if (ids.indexOfKey(sessionId) < 0) { |
| 9962 | ids.add(sessionId, true); |
| 9963 | } |
| 9964 | } |
| 9965 | return ids; |
| 9966 | } |
| 9967 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9968 | AudioStreamIn* RecordThread::clearInput() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9969 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9970 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9971 | AudioStreamIn *input = mInput; |
| 9972 | mInput = NULL; |
Mikhail Naganov | 49aecf0 | 2023-09-08 15:14:34 -0700 | [diff] [blame] | 9973 | mInputSource.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9974 | return input; |
| 9975 | } |
| 9976 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 9977 | // this method must always be called either with ThreadBase mutex() held or inside the thread loop |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9978 | sp<StreamHalInterface> RecordThread::stream() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9979 | { |
| 9980 | if (mInput == NULL) { |
| 9981 | return NULL; |
| 9982 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 9983 | return mInput->stream; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9984 | } |
| 9985 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9986 | status_t RecordThread::addEffectChain_l(const sp<IAfEffectChain>& chain) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9987 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9988 | ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 9989 | chain->setThread(this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9990 | chain->setInBuffer(NULL); |
| 9991 | chain->setOutBuffer(NULL); |
| 9992 | |
| 9993 | checkSuspendOnAddEffectChain_l(chain); |
| 9994 | |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 9995 | // make sure enabled pre processing effects state is communicated to the HAL as we |
| 9996 | // just moved them to a new input stream. |
Shunkai Yao | d125e40 | 2024-01-20 03:19:06 +0000 | [diff] [blame] | 9997 | chain->syncHalEffectsState_l(); |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 9998 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9999 | mEffectChains.add(chain); |
| 10000 | |
| 10001 | return NO_ERROR; |
| 10002 | } |
| 10003 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10004 | size_t RecordThread::removeEffectChain_l(const sp<IAfEffectChain>& chain) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 10005 | { |
| 10006 | ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 10007 | |
| 10008 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 10009 | if (chain == mEffectChains[i]) { |
| 10010 | mEffectChains.removeAt(i); |
| 10011 | break; |
| 10012 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 10013 | } |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 10014 | return mEffectChains.size(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 10015 | } |
| 10016 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10017 | status_t RecordThread::createAudioPatch_l(const struct audio_patch* patch, |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 10018 | audio_patch_handle_t *handle) |
| 10019 | { |
| 10020 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 10021 | |
| 10022 | // store new device and send to effects |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10023 | mInDeviceTypeAddr.mType = patch->sources[0].ext.device.type; |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 10024 | mInDeviceTypeAddr.setAddress(patch->sources[0].ext.device.address); |
François Gaffie | 0c280aa | 2018-07-25 10:02:15 +0200 | [diff] [blame] | 10025 | audio_port_handle_t deviceId = patch->sources[0].id; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 10026 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
jiabin | 8f278ee | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 10027 | mEffectChains[i]->setInputDevice_l(inDeviceTypeAddr()); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 10028 | } |
| 10029 | |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 10030 | checkBtNrec_l(); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 10031 | |
| 10032 | // store new source and send to effects |
| 10033 | if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) { |
| 10034 | mAudioSource = patch->sinks[0].ext.mix.usecase.source; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 10035 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 10036 | mEffectChains[i]->setAudioSource_l(mAudioSource); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 10037 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 10038 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 10039 | |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 10040 | if (mInput->audioHwDev->supportsAudioPatches()) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 10041 | sp<DeviceHalInterface> hwDevice = mInput->audioHwDev->hwDevice(); |
| 10042 | status = hwDevice->createAudioPatch(patch->num_sources, |
| 10043 | patch->sources, |
| 10044 | patch->num_sinks, |
| 10045 | patch->sinks, |
| 10046 | handle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 10047 | } else { |
Ytai Ben-Tsvi | f997ffe | 2022-02-03 16:38:16 -0800 | [diff] [blame] | 10048 | status = mInput->stream->legacyCreateAudioPatch(patch->sources[0], |
| 10049 | patch->sinks[0].ext.mix.usecase.source, |
| 10050 | patch->sources[0].ext.device.type); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 10051 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 10052 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 10053 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10054 | if ((mPatch.num_sources == 0) || (mPatch.sources[0].id != deviceId)) { |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 10055 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10056 | mPatch = *patch; |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 10057 | } |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 10058 | |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 10059 | const std::string pathSourcesAsString = patchSourcesToString(patch); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 10060 | mThreadMetrics.logEndInterval(); |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 10061 | mThreadMetrics.logCreatePatch(pathSourcesAsString, /* outDevices */ {}); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 10062 | mThreadMetrics.logBeginInterval(); |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 10063 | // also dispatch to active AudioRecords |
| 10064 | for (const auto &track : mActiveTracks) { |
| 10065 | track->logEndInterval(); |
| 10066 | track->logBeginInterval(pathSourcesAsString); |
| 10067 | } |
Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 10068 | // Force meteadata update after a route change |
| 10069 | mActiveTracks.setHasChanged(); |
| 10070 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 10071 | return status; |
| 10072 | } |
| 10073 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10074 | status_t RecordThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 10075 | { |
| 10076 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 10077 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10078 | mPatch = audio_patch{}; |
| 10079 | mInDeviceTypeAddr.reset(); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 10080 | |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 10081 | if (mInput->audioHwDev->supportsAudioPatches()) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 10082 | sp<DeviceHalInterface> hwDevice = mInput->audioHwDev->hwDevice(); |
| 10083 | status = hwDevice->releaseAudioPatch(handle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 10084 | } else { |
Ytai Ben-Tsvi | f997ffe | 2022-02-03 16:38:16 -0800 | [diff] [blame] | 10085 | status = mInput->stream->legacyReleaseAudioPatch(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 10086 | } |
Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 10087 | // Force meteadata update after a route change |
| 10088 | mActiveTracks.setHasChanged(); |
| 10089 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 10090 | return status; |
| 10091 | } |
| 10092 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10093 | void RecordThread::updateOutDevices(const DeviceDescriptorBaseVector& outDevices) |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10094 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 10095 | audio_utils::lock_guard _l(mutex()); |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10096 | mOutDevices = outDevices; |
| 10097 | mOutDeviceTypeAddrs = deviceTypeAddrsFromDescriptors(mOutDevices); |
| 10098 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
jiabin | 8f278ee | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 10099 | mEffectChains[i]->setDevices_l(outDeviceTypeAddrs()); |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10100 | } |
| 10101 | } |
| 10102 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10103 | int32_t RecordThread::getOldestFront_l() |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 10104 | { |
| 10105 | if (mTracks.size() == 0) { |
Eric Laurent | 92d0a32 | 2021-07-16 15:32:33 +0200 | [diff] [blame] | 10106 | return mRsmpInRear; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 10107 | } |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 10108 | int32_t oldestFront = mRsmpInRear; |
| 10109 | int32_t maxFilled = 0; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 10110 | for (size_t i = 0; i < mTracks.size(); i++) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 10111 | int32_t front = mTracks[i]->resamplerBufferProvider()->getFront(); |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 10112 | int32_t filled; |
Eric Laurent | 92d0a32 | 2021-07-16 15:32:33 +0200 | [diff] [blame] | 10113 | (void)__builtin_sub_overflow(mRsmpInRear, front, &filled); |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 10114 | if (filled > maxFilled) { |
| 10115 | oldestFront = front; |
| 10116 | maxFilled = filled; |
| 10117 | } |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 10118 | } |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 10119 | if (maxFilled > static_cast<signed>(mRsmpInFrames)) { |
Eric Laurent | 92d0a32 | 2021-07-16 15:32:33 +0200 | [diff] [blame] | 10120 | (void)__builtin_sub_overflow(mRsmpInRear, mRsmpInFrames, &oldestFront); |
| 10121 | } |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 10122 | return oldestFront; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 10123 | } |
| 10124 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10125 | void RecordThread::updateFronts_l(int32_t offset) |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 10126 | { |
| 10127 | if (offset == 0) { |
| 10128 | return; |
| 10129 | } |
| 10130 | for (size_t i = 0; i < mTracks.size(); i++) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 10131 | int32_t front = mTracks[i]->resamplerBufferProvider()->getFront(); |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 10132 | front = audio_utils::safe_sub_overflow(front, offset); |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 10133 | mTracks[i]->resamplerBufferProvider()->setFront(front); |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 10134 | } |
| 10135 | } |
| 10136 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10137 | void RecordThread::resizeInputBuffer_l(int32_t maxSharedAudioHistoryMs) |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 10138 | { |
| 10139 | // This is the formula for calculating the temporary buffer size. |
| 10140 | // With 7 HAL buffers, we can guarantee ability to down-sample the input by ratio of 6:1 to |
| 10141 | // 1 full output buffer, regardless of the alignment of the available input. |
| 10142 | // The value is somewhat arbitrary, and could probably be even larger. |
| 10143 | // A larger value should allow more old data to be read after a track calls start(), |
| 10144 | // without increasing latency. |
| 10145 | // |
| 10146 | // Note this is independent of the maximum downsampling ratio permitted for capture. |
| 10147 | size_t minRsmpInFrames = mFrameCount * 7; |
| 10148 | |
| 10149 | // maxSharedAudioHistoryMs != 0 indicates a request to possibly make some part of the audio |
| 10150 | // capture history available to another client using the same session ID: |
| 10151 | // dimension the resampler input buffer accordingly. |
| 10152 | |
| 10153 | // Get oldest client read position: getOldestFront_l() must be called before altering |
| 10154 | // mRsmpInRear, or mRsmpInFrames |
| 10155 | int32_t previousFront = getOldestFront_l(); |
| 10156 | size_t previousRsmpInFramesP2 = mRsmpInFramesP2; |
| 10157 | int32_t previousRear = mRsmpInRear; |
| 10158 | mRsmpInRear = 0; |
| 10159 | |
Eric Laurent | 5f0fd7b | 2021-05-07 16:33:26 +0200 | [diff] [blame] | 10160 | ALOG_ASSERT(maxSharedAudioHistoryMs >= 0 |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10161 | && maxSharedAudioHistoryMs <= kMaxSharedAudioHistoryMs, |
Eric Laurent | 5f0fd7b | 2021-05-07 16:33:26 +0200 | [diff] [blame] | 10162 | "resizeInputBuffer_l() called with invalid max shared history %d", |
| 10163 | maxSharedAudioHistoryMs); |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 10164 | if (maxSharedAudioHistoryMs != 0) { |
| 10165 | // resizeInputBuffer_l should never be called with a non zero shared history if the |
| 10166 | // buffer was not already allocated |
| 10167 | ALOG_ASSERT(mRsmpInBuffer != nullptr && mRsmpInFrames != 0, |
| 10168 | "resizeInputBuffer_l() called with shared history and unallocated buffer"); |
| 10169 | size_t rsmpInFrames = (size_t)maxSharedAudioHistoryMs * mSampleRate / 1000; |
| 10170 | // never reduce resampler input buffer size |
Eric Laurent | 92d0a32 | 2021-07-16 15:32:33 +0200 | [diff] [blame] | 10171 | if (rsmpInFrames <= mRsmpInFrames) { |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 10172 | return; |
| 10173 | } |
| 10174 | mRsmpInFrames = rsmpInFrames; |
| 10175 | } |
Eric Laurent | 5f0fd7b | 2021-05-07 16:33:26 +0200 | [diff] [blame] | 10176 | mMaxSharedAudioHistoryMs = maxSharedAudioHistoryMs; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 10177 | // Note: mRsmpInFrames is 0 when called with maxSharedAudioHistoryMs equals to 0 so it is always |
| 10178 | // initialized |
| 10179 | if (mRsmpInFrames < minRsmpInFrames) { |
| 10180 | mRsmpInFrames = minRsmpInFrames; |
| 10181 | } |
| 10182 | mRsmpInFramesP2 = roundup(mRsmpInFrames); |
| 10183 | |
| 10184 | // TODO optimize audio capture buffer sizes ... |
| 10185 | // Here we calculate the size of the sliding buffer used as a source |
| 10186 | // for resampling. mRsmpInFramesP2 is currently roundup(mFrameCount * 7). |
| 10187 | // For current HAL frame counts, this is usually 2048 = 40 ms. It would |
| 10188 | // be better to have it derived from the pipe depth in the long term. |
| 10189 | // The current value is higher than necessary. However it should not add to latency. |
| 10190 | |
| 10191 | // Over-allocate beyond mRsmpInFramesP2 to permit a HAL read past end of buffer |
| 10192 | mRsmpInFramesOA = mRsmpInFramesP2 + mFrameCount - 1; |
| 10193 | |
| 10194 | void *rsmpInBuffer; |
| 10195 | (void)posix_memalign(&rsmpInBuffer, 32, mRsmpInFramesOA * mFrameSize); |
| 10196 | // if posix_memalign fails, will segv here. |
| 10197 | memset(rsmpInBuffer, 0, mRsmpInFramesOA * mFrameSize); |
| 10198 | |
| 10199 | // Copy audio history if any from old buffer before freeing it |
| 10200 | if (previousRear != 0) { |
| 10201 | ALOG_ASSERT(mRsmpInBuffer != nullptr, |
| 10202 | "resizeInputBuffer_l() called with null buffer but frames already read from HAL"); |
| 10203 | |
| 10204 | ssize_t unread = audio_utils::safe_sub_overflow(previousRear, previousFront); |
| 10205 | previousFront &= previousRsmpInFramesP2 - 1; |
| 10206 | size_t part1 = previousRsmpInFramesP2 - previousFront; |
| 10207 | if (part1 > (size_t) unread) { |
| 10208 | part1 = unread; |
| 10209 | } |
| 10210 | if (part1 != 0) { |
| 10211 | memcpy(rsmpInBuffer, (const uint8_t*)mRsmpInBuffer + previousFront * mFrameSize, |
| 10212 | part1 * mFrameSize); |
| 10213 | mRsmpInRear = part1; |
| 10214 | part1 = unread - part1; |
| 10215 | if (part1 != 0) { |
| 10216 | memcpy((uint8_t*)rsmpInBuffer + mRsmpInRear * mFrameSize, |
| 10217 | (const uint8_t*)mRsmpInBuffer, part1 * mFrameSize); |
| 10218 | mRsmpInRear += part1; |
| 10219 | } |
| 10220 | } |
| 10221 | // Update front for all clients according to new rear |
| 10222 | updateFronts_l(audio_utils::safe_sub_overflow(previousRear, mRsmpInRear)); |
| 10223 | } else { |
| 10224 | mRsmpInRear = 0; |
| 10225 | } |
| 10226 | free(mRsmpInBuffer); |
| 10227 | mRsmpInBuffer = rsmpInBuffer; |
| 10228 | } |
| 10229 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10230 | void RecordThread::addPatchTrack(const sp<IAfPatchRecord>& record) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 10231 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 10232 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 10233 | mTracks.add(record); |
Mikhail Naganov | 2534b38 | 2019-09-25 13:05:02 -0700 | [diff] [blame] | 10234 | if (record->getSource()) { |
| 10235 | mSource = record->getSource(); |
| 10236 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 10237 | } |
| 10238 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10239 | void RecordThread::deletePatchTrack(const sp<IAfPatchRecord>& record) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 10240 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 10241 | audio_utils::lock_guard _l(mutex()); |
Mikhail Naganov | 2534b38 | 2019-09-25 13:05:02 -0700 | [diff] [blame] | 10242 | if (mSource == record->getSource()) { |
| 10243 | mSource = mInput; |
| 10244 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 10245 | destroyTrack_l(record); |
| 10246 | } |
| 10247 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10248 | void RecordThread::toAudioPortConfig(struct audio_port_config* config) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 10249 | { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 10250 | ThreadBase::toAudioPortConfig(config); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 10251 | config->role = AUDIO_PORT_ROLE_SINK; |
| 10252 | config->ext.mix.hw_module = mInput->audioHwDev->handle(); |
| 10253 | config->ext.mix.usecase.source = mAudioSource; |
Mikhail Naganov | 32abc2b | 2018-05-24 12:57:11 -0700 | [diff] [blame] | 10254 | if (mInput && mInput->flags != AUDIO_INPUT_FLAG_NONE) { |
| 10255 | config->config_mask |= AUDIO_PORT_CONFIG_FLAGS; |
| 10256 | config->flags.input = mInput->flags; |
| 10257 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 10258 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 10259 | |
Atneya Nair | aa3afcb | 2024-10-08 16:36:19 -0700 | [diff] [blame] | 10260 | std::string RecordThread::getLocalLogHeader() const { |
| 10261 | using namespace std::literals; |
| 10262 | static constexpr auto indent = " " |
| 10263 | " "sv; |
| 10264 | return std::string{indent}.append(IAfRecordTrack::getLogHeader()); |
| 10265 | } |
| 10266 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10267 | // ---------------------------------------------------------------------------- |
| 10268 | // Mmap |
| 10269 | // ---------------------------------------------------------------------------- |
| 10270 | |
Andy Hung | 7aa7d10 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 10271 | // Mmap stream control interface implementation. Each MmapThreadHandle controls one |
| 10272 | // MmapPlaybackThread or MmapCaptureThread instance. |
| 10273 | class MmapThreadHandle : public MmapStreamInterface { |
| 10274 | public: |
| 10275 | explicit MmapThreadHandle(const sp<IAfMmapThread>& thread); |
| 10276 | ~MmapThreadHandle() override; |
| 10277 | |
| 10278 | // MmapStreamInterface virtuals |
| 10279 | status_t createMmapBuffer(int32_t minSizeFrames, |
| 10280 | struct audio_mmap_buffer_info* info) final; |
| 10281 | status_t getMmapPosition(struct audio_mmap_position* position) final; |
| 10282 | status_t getExternalPosition(uint64_t* position, int64_t* timeNanos) final; |
| 10283 | status_t start(const AudioClient& client, |
| 10284 | const audio_attributes_t* attr, audio_port_handle_t* handle) final; |
| 10285 | status_t stop(audio_port_handle_t handle) final; |
| 10286 | status_t standby() final; |
| 10287 | status_t reportData(const void* buffer, size_t frameCount) final; |
| 10288 | private: |
| 10289 | const sp<IAfMmapThread> mThread; |
| 10290 | }; |
| 10291 | |
| 10292 | /* static */ |
| 10293 | sp<MmapStreamInterface> IAfMmapThread::createMmapStreamInterfaceAdapter( |
| 10294 | const sp<IAfMmapThread>& mmapThread) { |
| 10295 | return sp<MmapThreadHandle>::make(mmapThread); |
| 10296 | } |
| 10297 | |
| 10298 | MmapThreadHandle::MmapThreadHandle(const sp<IAfMmapThread>& thread) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10299 | : mThread(thread) |
| 10300 | { |
Phil Burk | 9fabbf8 | 2017-08-03 12:02:00 -0700 | [diff] [blame] | 10301 | assert(thread != 0); // thread must start non-null and stay non-null |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10302 | } |
| 10303 | |
Andy Hung | 7aa7d10 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 10304 | // MmapStreamInterface could be directly implemented by MmapThread excepting this |
| 10305 | // special handling on adapter dtor. |
| 10306 | MmapThreadHandle::~MmapThreadHandle() |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10307 | { |
Phil Burk | 9fabbf8 | 2017-08-03 12:02:00 -0700 | [diff] [blame] | 10308 | mThread->disconnect(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10309 | } |
| 10310 | |
Andy Hung | 7aa7d10 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 10311 | status_t MmapThreadHandle::createMmapBuffer(int32_t minSizeFrames, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10312 | struct audio_mmap_buffer_info *info) |
| 10313 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10314 | return mThread->createMmapBuffer(minSizeFrames, info); |
| 10315 | } |
| 10316 | |
Andy Hung | 7aa7d10 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 10317 | status_t MmapThreadHandle::getMmapPosition(struct audio_mmap_position* position) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10318 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10319 | return mThread->getMmapPosition(position); |
| 10320 | } |
| 10321 | |
Andy Hung | 7aa7d10 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 10322 | status_t MmapThreadHandle::getExternalPosition(uint64_t* position, |
jiabin | b7d8c5a | 2020-08-26 17:24:52 -0700 | [diff] [blame] | 10323 | int64_t *timeNanos) { |
| 10324 | return mThread->getExternalPosition(position, timeNanos); |
| 10325 | } |
| 10326 | |
Andy Hung | 7aa7d10 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 10327 | status_t MmapThreadHandle::start(const AudioClient& client, |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 10328 | const audio_attributes_t *attr, audio_port_handle_t *handle) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10329 | { |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 10330 | return mThread->start(client, attr, handle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10331 | } |
| 10332 | |
Andy Hung | 7aa7d10 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 10333 | status_t MmapThreadHandle::stop(audio_port_handle_t handle) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10334 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10335 | return mThread->stop(handle); |
| 10336 | } |
| 10337 | |
Andy Hung | 7aa7d10 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 10338 | status_t MmapThreadHandle::standby() |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 10339 | { |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 10340 | return mThread->standby(); |
| 10341 | } |
| 10342 | |
Andy Hung | 7aa7d10 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 10343 | status_t MmapThreadHandle::reportData(const void* buffer, size_t frameCount) |
| 10344 | { |
jiabin | fc791ee | 2023-02-15 19:43:40 +0000 | [diff] [blame] | 10345 | return mThread->reportData(buffer, frameCount); |
| 10346 | } |
| 10347 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10348 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10349 | MmapThread::MmapThread( |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 10350 | const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id, |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 10351 | AudioHwDevice *hwDev, const sp<StreamHalInterface>& stream, bool systemReady, bool isOut) |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 10352 | : ThreadBase(afThreadCallback, id, (isOut ? MMAP_PLAYBACK : MMAP_CAPTURE), systemReady, isOut), |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 10353 | mSessionId(AUDIO_SESSION_NONE), |
François Gaffie | 0c280aa | 2018-07-25 10:02:15 +0200 | [diff] [blame] | 10354 | mPortId(AUDIO_PORT_HANDLE_NONE), |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 10355 | mHalStream(stream), mHalDevice(hwDev->hwDevice()), mAudioHwDev(hwDev), |
Eric Laurent | 67f9729 | 2018-04-20 18:05:41 -0700 | [diff] [blame] | 10356 | mActiveTracks(&this->mLocalLog), |
| 10357 | mHalVolFloat(-1.0f), // Initialize to illegal value so it always gets set properly later. |
| 10358 | mNoCallbackWarningCount(0) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10359 | { |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 10360 | mStandby = true; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10361 | readHalParameters_l(); |
| 10362 | } |
| 10363 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10364 | void MmapThread::onFirstRef() |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10365 | { |
| 10366 | run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO); |
| 10367 | } |
| 10368 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10369 | void MmapThread::disconnect() |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10370 | { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 10371 | ActiveTracks<IAfMmapTrack> activeTracks; |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10372 | audio_port_handle_t localPortId; |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 10373 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 10374 | audio_utils::lock_guard _l(mutex()); |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 10375 | for (const sp<IAfMmapTrack>& t : mActiveTracks) { |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 10376 | activeTracks.add(t); |
| 10377 | } |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10378 | localPortId = mPortId; |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 10379 | } |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 10380 | for (const sp<IAfMmapTrack>& t : activeTracks) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10381 | stop(t->portId()); |
| 10382 | } |
Phil Burk | 9fabbf8 | 2017-08-03 12:02:00 -0700 | [diff] [blame] | 10383 | // This will decrement references and may cause the destruction of this thread. |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10384 | if (isOutput()) { |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10385 | AudioSystem::releaseOutput(localPortId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10386 | } else { |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10387 | AudioSystem::releaseInput(localPortId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10388 | } |
| 10389 | } |
| 10390 | |
| 10391 | |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 10392 | void MmapThread::configure_l(const audio_attributes_t* attr, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10393 | audio_stream_type_t streamType __unused, |
| 10394 | audio_session_t sessionId, |
| 10395 | const sp<MmapStreamCallback>& callback, |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 10396 | audio_port_handle_t deviceId, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10397 | audio_port_handle_t portId) |
| 10398 | { |
| 10399 | mAttr = *attr; |
| 10400 | mSessionId = sessionId; |
| 10401 | mCallback = callback; |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 10402 | mDeviceId = deviceId; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10403 | mPortId = portId; |
| 10404 | } |
| 10405 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10406 | status_t MmapThread::createMmapBuffer(int32_t minSizeFrames, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10407 | struct audio_mmap_buffer_info *info) |
| 10408 | { |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10409 | audio_utils::lock_guard l(mutex()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10410 | if (mHalStream == 0) { |
| 10411 | return NO_INIT; |
| 10412 | } |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 10413 | mStandby = true; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10414 | return mHalStream->createMmapBuffer(minSizeFrames, info); |
| 10415 | } |
| 10416 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10417 | status_t MmapThread::getMmapPosition(struct audio_mmap_position* position) const |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10418 | { |
Andy Hung | 3f49ebb | 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 | if (mHalStream == 0) { |
| 10421 | return NO_INIT; |
| 10422 | } |
| 10423 | return mHalStream->getMmapPosition(position); |
| 10424 | } |
| 10425 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10426 | status_t MmapThread::exitStandby_l() |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 10427 | { |
Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 10428 | // The HAL must receive track metadata before starting the stream |
| 10429 | updateMetadata_l(); |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 10430 | status_t ret = mHalStream->start(); |
| 10431 | if (ret != NO_ERROR) { |
| 10432 | ALOGE("%s: error mHalStream->start() = %d for first track", __FUNCTION__, ret); |
| 10433 | return ret; |
| 10434 | } |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 10435 | if (mStandby) { |
| 10436 | mThreadMetrics.logBeginInterval(); |
Andy Hung | 44d648b | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 10437 | mThreadSnapshot.onBegin(); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 10438 | mStandby = false; |
| 10439 | } |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 10440 | return NO_ERROR; |
| 10441 | } |
| 10442 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10443 | status_t MmapThread::start(const AudioClient& client, |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 10444 | const audio_attributes_t *attr, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10445 | audio_port_handle_t *handle) |
| 10446 | { |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10447 | audio_utils::lock_guard l(mutex()); |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10448 | ALOGV("%s clientUid %d mStandby %d mPortId %d *handle %d", __FUNCTION__, |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 10449 | client.attributionSource.uid, mStandby, mPortId, *handle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10450 | if (mHalStream == 0) { |
| 10451 | return NO_INIT; |
| 10452 | } |
| 10453 | |
| 10454 | status_t ret; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10455 | |
Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 10456 | // 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] | 10457 | if (*handle == mPortId) { |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10458 | acquireWakeLock_l(); |
Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 10459 | return NO_ERROR; |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10460 | } |
| 10461 | |
| 10462 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
| 10463 | |
| 10464 | audio_io_handle_t io = mId; |
Atneya Nair | 5997a65 | 2024-06-14 17:24:45 -0700 | [diff] [blame] | 10465 | AttributionSourceState adjAttributionSource; |
| 10466 | if (!com::android::media::audio::audioserver_permissions()) { |
| 10467 | adjAttributionSource = afutils::checkAttributionSourcePackage( |
| 10468 | client.attributionSource); |
| 10469 | } else { |
| 10470 | // TODO(b/342475009) validate in oboeservice, and plumb downwards |
| 10471 | auto validatedRes = ValidatedAttributionSourceState::createFromTrustedUidNoPackage( |
| 10472 | client.attributionSource, |
| 10473 | mAfThreadCallback->getPermissionProvider() |
| 10474 | ); |
| 10475 | if (!validatedRes.has_value()) { |
| 10476 | ALOGE("MMAP client package validation fail: %s", |
| 10477 | validatedRes.error().toString8().c_str()); |
| 10478 | return aidl_utils::statusTFromBinderStatus(validatedRes.error()); |
| 10479 | } |
| 10480 | adjAttributionSource = std::move(validatedRes.value()).unwrapInto(); |
| 10481 | } |
Atneya Nair | f59db5c | 2023-05-10 21:37:41 -0700 | [diff] [blame] | 10482 | |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10483 | const auto localSessionId = mSessionId; |
| 10484 | auto localAttr = mAttr; |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 10485 | float volume = 0.0f; |
Vlad Popa | 1e865e6 | 2024-08-15 19:11:42 -0700 | [diff] [blame] | 10486 | bool muted = false; |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10487 | if (isOutput()) { |
| 10488 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 10489 | config.sample_rate = mSampleRate; |
| 10490 | config.channel_mask = mChannelMask; |
| 10491 | config.format = mFormat; |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10492 | audio_stream_type_t stream = streamType_l(); |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10493 | audio_output_flags_t flags = |
| 10494 | (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_MMAP_NOIRQ | AUDIO_OUTPUT_FLAG_DIRECT); |
Robert Wu | fb97119 | 2024-10-30 21:54:35 +0000 | [diff] [blame^] | 10495 | DeviceIdVector deviceIds; |
| 10496 | if (mDeviceId != AUDIO_PORT_HANDLE_NONE) { |
| 10497 | deviceIds.push_back(mDeviceId); |
| 10498 | } else { |
| 10499 | ALOGW("%s no device id set", __func__); |
| 10500 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 10501 | std::vector<audio_io_handle_t> secondaryOutputs; |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 10502 | bool isSpatialized; |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 10503 | bool isBitPerfect; |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10504 | mutex().unlock(); |
| 10505 | ret = AudioSystem::getOutputForAttr(&localAttr, &io, |
| 10506 | localSessionId, |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10507 | &stream, |
Atneya Nair | f59db5c | 2023-05-10 21:37:41 -0700 | [diff] [blame] | 10508 | adjAttributionSource, |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10509 | &config, |
| 10510 | flags, |
Robert Wu | fb97119 | 2024-10-30 21:54:35 +0000 | [diff] [blame^] | 10511 | &deviceIds, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 10512 | &portId, |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 10513 | &secondaryOutputs, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 10514 | &isSpatialized, |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 10515 | &isBitPerfect, |
Vlad Popa | 1e865e6 | 2024-08-15 19:11:42 -0700 | [diff] [blame] | 10516 | &volume, |
| 10517 | &muted); |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10518 | mutex().lock(); |
| 10519 | mAttr = localAttr; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 10520 | ALOGD_IF(!secondaryOutputs.empty(), |
| 10521 | "MmapThread::start does not support secondary outputs, ignoring them"); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10522 | } else { |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10523 | audio_config_base_t config; |
| 10524 | config.sample_rate = mSampleRate; |
| 10525 | config.channel_mask = mChannelMask; |
| 10526 | config.format = mFormat; |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 10527 | audio_port_handle_t deviceId = mDeviceId; |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10528 | mutex().unlock(); |
| 10529 | ret = AudioSystem::getInputForAttr(&localAttr, &io, |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 10530 | RECORD_RIID_INVALID, |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10531 | localSessionId, |
Atneya Nair | f59db5c | 2023-05-10 21:37:41 -0700 | [diff] [blame] | 10532 | adjAttributionSource, |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10533 | &config, |
| 10534 | AUDIO_INPUT_FLAG_MMAP_NOIRQ, |
| 10535 | &deviceId, |
| 10536 | &portId); |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10537 | mutex().lock(); |
| 10538 | // localAttr is const for getInputForAttr. |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10539 | } |
| 10540 | // APM should not chose a different input or output stream for the same set of attributes |
| 10541 | // and audo configuration |
| 10542 | if (ret != NO_ERROR || io != mId) { |
| 10543 | ALOGE("%s: error getting output or input from APM (error %d, io %d expected io %d)", |
| 10544 | __FUNCTION__, ret, io, mId); |
| 10545 | return BAD_VALUE; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10546 | } |
| 10547 | |
| 10548 | if (isOutput()) { |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10549 | mutex().unlock(); |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 10550 | ret = AudioSystem::startOutput(portId); |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10551 | mutex().lock(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10552 | } else { |
jiabin | 0960903 | 2022-06-15 19:26:01 +0000 | [diff] [blame] | 10553 | { |
| 10554 | // Add the track record before starting input so that the silent status for the |
| 10555 | // client can be cached. |
jiabin | 0960903 | 2022-06-15 19:26:01 +0000 | [diff] [blame] | 10556 | setClientSilencedState_l(portId, false /*silenced*/); |
| 10557 | } |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10558 | mutex().unlock(); |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 10559 | ret = AudioSystem::startInput(portId); |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10560 | mutex().lock(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10561 | } |
| 10562 | |
| 10563 | // abort if start is rejected by audio policy manager |
| 10564 | if (ret != NO_ERROR) { |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 10565 | ALOGE("%s: error start rejected by AudioPolicyManager = %d", __FUNCTION__, ret); |
Eric Tan | 39ec8d6 | 2018-07-24 09:49:29 -0700 | [diff] [blame] | 10566 | if (!mActiveTracks.isEmpty()) { |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 10567 | mutex().unlock(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10568 | if (isOutput()) { |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 10569 | AudioSystem::releaseOutput(portId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10570 | } else { |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 10571 | AudioSystem::releaseInput(portId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10572 | } |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 10573 | mutex().lock(); |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 10574 | } else { |
| 10575 | mHalStream->stop(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10576 | } |
jiabin | 0960903 | 2022-06-15 19:26:01 +0000 | [diff] [blame] | 10577 | eraseClientSilencedState_l(portId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10578 | return PERMISSION_DENIED; |
| 10579 | } |
| 10580 | |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 10581 | // Given that MmapThread::mAttr is mutable, should a MmapTrack have attributes ? |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 10582 | sp<IAfMmapTrack> track = IAfMmapTrack::create( |
| 10583 | this, attr == nullptr ? mAttr : *attr, mSampleRate, mFormat, |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 10584 | mChannelMask, mSessionId, isOutput(), |
| 10585 | client.attributionSource, |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 10586 | IPCThreadState::self()->getCallingPid(), portId, |
Vlad Popa | 1e865e6 | 2024-08-15 19:11:42 -0700 | [diff] [blame] | 10587 | volume, muted); |
jiabin | 0960903 | 2022-06-15 19:26:01 +0000 | [diff] [blame] | 10588 | if (!isOutput()) { |
| 10589 | track->setSilenced_l(isClientSilenced_l(portId)); |
| 10590 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10591 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 10592 | if (isOutput()) { |
| 10593 | // force volume update when a new track is added |
| 10594 | mHalVolFloat = -1.0f; |
| 10595 | } else if (!track->isSilenced_l()) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 10596 | for (const sp<IAfMmapTrack>& t : mActiveTracks) { |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 10597 | if (t->isSilenced_l() |
| 10598 | && t->uid() != static_cast<uid_t>(client.attributionSource.uid)) { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 10599 | t->invalidate(); |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 10600 | } |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 10601 | } |
| 10602 | } |
| 10603 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10604 | mActiveTracks.add(track); |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 10605 | sp<IAfEffectChain> chain = getEffectChain_l(mSessionId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10606 | if (chain != 0) { |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10607 | chain->setStrategy(getStrategyForStream(streamType_l())); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10608 | chain->incTrackCnt(); |
| 10609 | chain->incActiveTrackCnt(); |
| 10610 | } |
| 10611 | |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 10612 | track->logBeginInterval(patchSinksToString(&mPatch)); // log to MediaMetrics |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10613 | *handle = portId; |
Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 10614 | |
| 10615 | if (mActiveTracks.size() == 1) { |
| 10616 | ret = exitStandby_l(); |
| 10617 | } |
| 10618 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10619 | broadcast_l(); |
| 10620 | |
Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 10621 | 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] | 10622 | |
Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 10623 | return ret; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10624 | } |
| 10625 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10626 | status_t MmapThread::stop(audio_port_handle_t handle) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10627 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10628 | ALOGV("%s handle %d", __FUNCTION__, handle); |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10629 | audio_utils::lock_guard l(mutex()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10630 | |
| 10631 | if (mHalStream == 0) { |
| 10632 | return NO_INIT; |
| 10633 | } |
| 10634 | |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10635 | if (handle == mPortId) { |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10636 | releaseWakeLock_l(); |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10637 | return NO_ERROR; |
| 10638 | } |
| 10639 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 10640 | sp<IAfMmapTrack> track; |
| 10641 | for (const sp<IAfMmapTrack>& t : mActiveTracks) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10642 | if (handle == t->portId()) { |
| 10643 | track = t; |
| 10644 | break; |
| 10645 | } |
| 10646 | } |
| 10647 | if (track == 0) { |
| 10648 | return BAD_VALUE; |
| 10649 | } |
| 10650 | |
| 10651 | mActiveTracks.remove(track); |
jiabin | 0960903 | 2022-06-15 19:26:01 +0000 | [diff] [blame] | 10652 | eraseClientSilencedState_l(track->portId()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10653 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 10654 | mutex().unlock(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10655 | if (isOutput()) { |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 10656 | AudioSystem::stopOutput(track->portId()); |
| 10657 | AudioSystem::releaseOutput(track->portId()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10658 | } else { |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 10659 | AudioSystem::stopInput(track->portId()); |
| 10660 | AudioSystem::releaseInput(track->portId()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10661 | } |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 10662 | mutex().lock(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10663 | |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 10664 | sp<IAfEffectChain> chain = getEffectChain_l(track->sessionId()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10665 | if (chain != 0) { |
| 10666 | chain->decActiveTrackCnt(); |
| 10667 | chain->decTrackCnt(); |
| 10668 | } |
| 10669 | |
Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 10670 | if (mActiveTracks.isEmpty()) { |
| 10671 | mHalStream->stop(); |
| 10672 | } |
| 10673 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10674 | broadcast_l(); |
| 10675 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10676 | return NO_ERROR; |
| 10677 | } |
| 10678 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10679 | status_t MmapThread::standby() |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10680 | NO_THREAD_SAFETY_ANALYSIS // clang bug |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 10681 | { |
| 10682 | ALOGV("%s", __FUNCTION__); |
Atneya Nair | 97a7388 | 2023-10-30 20:26:21 -0700 | [diff] [blame] | 10683 | audio_utils::lock_guard l_{mutex()}; |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 10684 | |
| 10685 | if (mHalStream == 0) { |
| 10686 | return NO_INIT; |
| 10687 | } |
Eric Tan | 39ec8d6 | 2018-07-24 09:49:29 -0700 | [diff] [blame] | 10688 | if (!mActiveTracks.isEmpty()) { |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 10689 | return INVALID_OPERATION; |
| 10690 | } |
| 10691 | mHalStream->standby(); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 10692 | if (!mStandby) { |
| 10693 | mThreadMetrics.logEndInterval(); |
Andy Hung | 44d648b | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 10694 | mThreadSnapshot.onEnd(); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 10695 | mStandby = true; |
| 10696 | } |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10697 | releaseWakeLock_l(); |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 10698 | return NO_ERROR; |
| 10699 | } |
| 10700 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10701 | status_t MmapThread::reportData(const void* /*buffer*/, size_t /*frameCount*/) { |
jiabin | fc791ee | 2023-02-15 19:43:40 +0000 | [diff] [blame] | 10702 | // This is a stub implementation. The MmapPlaybackThread overrides this function. |
| 10703 | return INVALID_OPERATION; |
| 10704 | } |
| 10705 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10706 | void MmapThread::readHalParameters_l() |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10707 | { |
| 10708 | status_t result = mHalStream->getAudioProperties(&mSampleRate, &mChannelMask, &mHALFormat); |
| 10709 | LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving audio properties from HAL: %d", result); |
| 10710 | mFormat = mHALFormat; |
| 10711 | LOG_ALWAYS_FATAL_IF(!audio_is_linear_pcm(mFormat), "HAL format %#x is not linear pcm", mFormat); |
| 10712 | result = mHalStream->getFrameSize(&mFrameSize); |
| 10713 | 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] | 10714 | LOG_ALWAYS_FATAL_IF(mFrameSize <= 0, "Error frame size was %zu but must be greater than zero", |
| 10715 | mFrameSize); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10716 | result = mHalStream->getBufferSize(&mBufferSize); |
| 10717 | LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving buffer size from HAL: %d", result); |
| 10718 | mFrameCount = mBufferSize / mFrameSize; |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 10719 | |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 10720 | // TODO: make a readHalParameters call? |
| 10721 | mediametrics::LogItem item(mThreadMetrics.getMetricsId()); |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 10722 | item.set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_READPARAMETERS) |
Andy Hung | 25a80ac | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 10723 | .set(AMEDIAMETRICS_PROP_ENCODING, IAfThreadBase::formatToString(mFormat).c_str()) |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 10724 | .set(AMEDIAMETRICS_PROP_SAMPLERATE, (int32_t)mSampleRate) |
| 10725 | .set(AMEDIAMETRICS_PROP_CHANNELMASK, (int32_t)mChannelMask) |
| 10726 | .set(AMEDIAMETRICS_PROP_CHANNELCOUNT, (int32_t)mChannelCount) |
| 10727 | .set(AMEDIAMETRICS_PROP_FRAMECOUNT, (int32_t)mFrameCount) |
| 10728 | /* |
| 10729 | .set(AMEDIAMETRICS_PROP_FLAGS, toString(flags).c_str()) |
| 10730 | .set(AMEDIAMETRICS_PROP_PREFIX_HAPTIC AMEDIAMETRICS_PROP_CHANNELMASK, |
| 10731 | (int32_t)mHapticChannelMask) |
| 10732 | .set(AMEDIAMETRICS_PROP_PREFIX_HAPTIC AMEDIAMETRICS_PROP_CHANNELCOUNT, |
| 10733 | (int32_t)mHapticChannelCount) |
| 10734 | */ |
| 10735 | .set(AMEDIAMETRICS_PROP_PREFIX_HAL AMEDIAMETRICS_PROP_ENCODING, |
Andy Hung | 25a80ac | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 10736 | IAfThreadBase::formatToString(mHALFormat).c_str()) |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 10737 | .set(AMEDIAMETRICS_PROP_PREFIX_HAL AMEDIAMETRICS_PROP_FRAMECOUNT, |
| 10738 | (int32_t)mFrameCount) // sic - added HAL |
| 10739 | .record(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10740 | } |
| 10741 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10742 | bool MmapThread::threadLoop() |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10743 | { |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 10744 | { |
| 10745 | audio_utils::unique_lock _l(mutex()); |
| 10746 | checkSilentMode_l(); |
| 10747 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10748 | |
| 10749 | const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); |
| 10750 | |
| 10751 | while (!exitPending()) |
| 10752 | { |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 10753 | Vector<sp<IAfEffectChain>> effectChains; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10754 | |
Andy Hung | 13850be | 2019-03-14 11:33:09 -0700 | [diff] [blame] | 10755 | { // under Thread lock |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 10756 | audio_utils::unique_lock _l(mutex()); |
Andy Hung | 13850be | 2019-03-14 11:33:09 -0700 | [diff] [blame] | 10757 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10758 | if (mSignalPending) { |
| 10759 | // A signal was raised while we were unlocked |
| 10760 | mSignalPending = false; |
| 10761 | } else { |
| 10762 | if (mConfigEvents.isEmpty()) { |
| 10763 | // we're about to wait, flush the binder command buffer |
| 10764 | IPCThreadState::self()->flushCommands(); |
| 10765 | |
| 10766 | if (exitPending()) { |
| 10767 | break; |
| 10768 | } |
| 10769 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10770 | // wait until we have something to do... |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 10771 | ALOGV("%s going to sleep", myName.c_str()); |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 10772 | mWaitWorkCV.wait(_l); |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 10773 | ALOGV("%s waking up", myName.c_str()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10774 | |
| 10775 | checkSilentMode_l(); |
| 10776 | |
| 10777 | continue; |
| 10778 | } |
| 10779 | } |
| 10780 | |
| 10781 | processConfigEvents_l(); |
| 10782 | |
| 10783 | processVolume_l(); |
| 10784 | |
| 10785 | checkInvalidTracks_l(); |
| 10786 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 10787 | mActiveTracks.updatePowerState_l(this); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10788 | |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 10789 | updateMetadata_l(); |
| 10790 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10791 | lockEffectChains_l(effectChains); |
Andy Hung | 13850be | 2019-03-14 11:33:09 -0700 | [diff] [blame] | 10792 | } // release Thread lock |
| 10793 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10794 | for (size_t i = 0; i < effectChains.size(); i ++) { |
Andy Hung | 13850be | 2019-03-14 11:33:09 -0700 | [diff] [blame] | 10795 | 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] | 10796 | } |
Andy Hung | 13850be | 2019-03-14 11:33:09 -0700 | [diff] [blame] | 10797 | |
| 10798 | // enable changes in effect chain, including moving to another thread. |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10799 | unlockEffectChains(effectChains); |
| 10800 | // Effect chains will be actually deleted here if they were removed from |
| 10801 | // mEffectChains list during mixing or effects processing |
Andy Hung | 56ce2ed | 2024-06-12 16:03:16 -0700 | [diff] [blame] | 10802 | mThreadloopExecutor.process(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10803 | } |
Andy Hung | 56ce2ed | 2024-06-12 16:03:16 -0700 | [diff] [blame] | 10804 | mThreadloopExecutor.process(); // process any remaining deferred actions. |
| 10805 | // deferred actions after this point are ignored. |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10806 | |
| 10807 | threadLoop_exit(); |
| 10808 | |
| 10809 | if (!mStandby) { |
| 10810 | threadLoop_standby(); |
| 10811 | mStandby = true; |
| 10812 | } |
| 10813 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10814 | ALOGV("Thread %p type %d exiting", this, mType); |
| 10815 | return false; |
| 10816 | } |
| 10817 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 10818 | // checkForNewParameter_l() must be called with ThreadBase::mutex() held |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10819 | bool MmapThread::checkForNewParameter_l(const String8& keyValuePair, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10820 | status_t& status) |
| 10821 | { |
| 10822 | AudioParameter param = AudioParameter(keyValuePair); |
| 10823 | int value; |
Eric Laurent | e6e9a48 | 2017-07-25 19:26:02 -0700 | [diff] [blame] | 10824 | bool sendToHal = true; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10825 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10826 | LOG_FATAL("Should not happen set routing device in MmapThread"); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10827 | } |
Eric Laurent | e6e9a48 | 2017-07-25 19:26:02 -0700 | [diff] [blame] | 10828 | if (sendToHal) { |
| 10829 | status = mHalStream->setParameters(keyValuePair); |
| 10830 | } else { |
| 10831 | status = NO_ERROR; |
| 10832 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10833 | |
| 10834 | return false; |
| 10835 | } |
| 10836 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10837 | String8 MmapThread::getParameters(const String8& keys) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10838 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 10839 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10840 | String8 out_s8; |
| 10841 | if (initCheck() == NO_ERROR && mHalStream->getParameters(keys, &out_s8) == OK) { |
| 10842 | return out_s8; |
| 10843 | } |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 10844 | return {}; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10845 | } |
| 10846 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 10847 | 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] | 10848 | audio_port_handle_t portId __unused) { |
Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 10849 | sp<AudioIoDescriptor> desc; |
| 10850 | bool isInput = false; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10851 | switch (event) { |
| 10852 | case AUDIO_INPUT_OPENED: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 10853 | case AUDIO_INPUT_REGISTERED: |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10854 | case AUDIO_INPUT_CONFIG_CHANGED: |
Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 10855 | isInput = true; |
| 10856 | FALLTHROUGH_INTENDED; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10857 | case AUDIO_OUTPUT_OPENED: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 10858 | case AUDIO_OUTPUT_REGISTERED: |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10859 | case AUDIO_OUTPUT_CONFIG_CHANGED: |
Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 10860 | desc = sp<AudioIoDescriptor>::make(mId, mPatch, isInput, |
| 10861 | mSampleRate, mFormat, mChannelMask, mFrameCount, mFrameCount); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10862 | break; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10863 | case AUDIO_INPUT_CLOSED: |
| 10864 | case AUDIO_OUTPUT_CLOSED: |
| 10865 | default: |
Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 10866 | desc = sp<AudioIoDescriptor>::make(mId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10867 | break; |
| 10868 | } |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 10869 | mAfThreadCallback->ioConfigChanged_l(event, desc, pid); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10870 | } |
| 10871 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10872 | status_t MmapThread::createAudioPatch_l(const struct audio_patch* patch, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10873 | audio_patch_handle_t *handle) |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 10874 | NO_THREAD_SAFETY_ANALYSIS // elease and re-acquire mutex() |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10875 | { |
| 10876 | status_t status = NO_ERROR; |
| 10877 | |
| 10878 | // store new device and send to effects |
| 10879 | audio_devices_t type = AUDIO_DEVICE_NONE; |
| 10880 | audio_port_handle_t deviceId; |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10881 | AudioDeviceTypeAddrVector sinkDeviceTypeAddrs; |
| 10882 | AudioDeviceTypeAddr sourceDeviceTypeAddr; |
| 10883 | uint32_t numDevices = 0; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10884 | if (isOutput()) { |
| 10885 | for (unsigned int i = 0; i < patch->num_sinks; i++) { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10886 | LOG_ALWAYS_FATAL_IF(popcount(patch->sinks[i].ext.device.type) > 1 |
| 10887 | && !mAudioHwDev->supportsAudioPatches(), |
| 10888 | "Enumerated device type(%#x) must not be used " |
| 10889 | "as it does not support audio patches", |
| 10890 | patch->sinks[i].ext.device.type); |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 10891 | 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] | 10892 | sinkDeviceTypeAddrs.emplace_back(patch->sinks[i].ext.device.type, |
| 10893 | patch->sinks[i].ext.device.address); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10894 | } |
| 10895 | deviceId = patch->sinks[0].id; |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10896 | numDevices = mPatch.num_sinks; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10897 | } else { |
| 10898 | type = patch->sources[0].ext.device.type; |
| 10899 | deviceId = patch->sources[0].id; |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10900 | numDevices = mPatch.num_sources; |
| 10901 | sourceDeviceTypeAddr.mType = patch->sources[0].ext.device.type; |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 10902 | sourceDeviceTypeAddr.setAddress(patch->sources[0].ext.device.address); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10903 | } |
| 10904 | |
| 10905 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
jiabin | 8f278ee | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 10906 | if (isOutput()) { |
| 10907 | mEffectChains[i]->setDevices_l(sinkDeviceTypeAddrs); |
| 10908 | } else { |
| 10909 | mEffectChains[i]->setInputDevice_l(sourceDeviceTypeAddr); |
| 10910 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10911 | } |
| 10912 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10913 | if (!isOutput()) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10914 | // store new source and send to effects |
| 10915 | if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) { |
| 10916 | mAudioSource = patch->sinks[0].ext.mix.usecase.source; |
| 10917 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 10918 | mEffectChains[i]->setAudioSource_l(mAudioSource); |
| 10919 | } |
| 10920 | } |
| 10921 | } |
| 10922 | |
jiabin | 78b86f2 | 2024-02-22 00:39:29 +0000 | [diff] [blame] | 10923 | // For mmap streams, once the routing has changed, they will be disconnected. It should be |
| 10924 | // okay to notify the client earlier before the new patch creation. |
| 10925 | if (mDeviceId != deviceId) { |
| 10926 | if (const sp<MmapStreamCallback> callback = mCallback.promote()) { |
| 10927 | // The aaudioservice handle the routing changed event asynchronously. In that case, |
| 10928 | // it is safe to hold the lock here. |
| 10929 | callback->onRoutingChanged(deviceId); |
| 10930 | } |
| 10931 | } |
| 10932 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10933 | if (mAudioHwDev->supportsAudioPatches()) { |
Ytai Ben-Tsvi | f997ffe | 2022-02-03 16:38:16 -0800 | [diff] [blame] | 10934 | status = mHalDevice->createAudioPatch(patch->num_sources, patch->sources, patch->num_sinks, |
| 10935 | patch->sinks, handle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10936 | } else { |
Ytai Ben-Tsvi | f997ffe | 2022-02-03 16:38:16 -0800 | [diff] [blame] | 10937 | audio_port_config port; |
| 10938 | std::optional<audio_source_t> source; |
| 10939 | if (isOutput()) { |
| 10940 | port = patch->sinks[0]; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10941 | } else { |
Ytai Ben-Tsvi | f997ffe | 2022-02-03 16:38:16 -0800 | [diff] [blame] | 10942 | port = patch->sources[0]; |
| 10943 | source = patch->sinks[0].ext.mix.usecase.source; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10944 | } |
Ytai Ben-Tsvi | f997ffe | 2022-02-03 16:38:16 -0800 | [diff] [blame] | 10945 | status = mHalStream->legacyCreateAudioPatch(port, source, type); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10946 | *handle = AUDIO_PATCH_HANDLE_NONE; |
| 10947 | } |
| 10948 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10949 | if (numDevices == 0 || mDeviceId != deviceId) { |
| 10950 | if (isOutput()) { |
| 10951 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
| 10952 | mOutDeviceTypeAddrs = sinkDeviceTypeAddrs; |
jiabin | 0a957d3 | 2020-04-29 10:56:20 -0700 | [diff] [blame] | 10953 | checkSilentMode_l(); |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10954 | } else { |
| 10955 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); |
| 10956 | mInDeviceTypeAddr = sourceDeviceTypeAddr; |
| 10957 | } |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10958 | mPatch = *patch; |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 10959 | mDeviceId = deviceId; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10960 | } |
Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 10961 | // Force meteadata update after a route change |
| 10962 | mActiveTracks.setHasChanged(); |
| 10963 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10964 | return status; |
| 10965 | } |
| 10966 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10967 | status_t MmapThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10968 | { |
| 10969 | status_t status = NO_ERROR; |
| 10970 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10971 | mPatch = audio_patch{}; |
| 10972 | mOutDeviceTypeAddrs.clear(); |
| 10973 | mInDeviceTypeAddr.reset(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10974 | |
| 10975 | bool supportsAudioPatches = mHalDevice->supportsAudioPatches(&supportsAudioPatches) == OK ? |
| 10976 | supportsAudioPatches : false; |
| 10977 | |
| 10978 | if (supportsAudioPatches) { |
| 10979 | status = mHalDevice->releaseAudioPatch(handle); |
| 10980 | } else { |
Ytai Ben-Tsvi | f997ffe | 2022-02-03 16:38:16 -0800 | [diff] [blame] | 10981 | status = mHalStream->legacyReleaseAudioPatch(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10982 | } |
Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 10983 | // Force meteadata update after a route change |
| 10984 | mActiveTracks.setHasChanged(); |
| 10985 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10986 | return status; |
| 10987 | } |
| 10988 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10989 | void MmapThread::toAudioPortConfig(struct audio_port_config* config) |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10990 | NO_THREAD_SAFETY_ANALYSIS // mAudioHwDev handle access |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10991 | { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 10992 | ThreadBase::toAudioPortConfig(config); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10993 | if (isOutput()) { |
| 10994 | config->role = AUDIO_PORT_ROLE_SOURCE; |
| 10995 | config->ext.mix.hw_module = mAudioHwDev->handle(); |
| 10996 | config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; |
| 10997 | } else { |
| 10998 | config->role = AUDIO_PORT_ROLE_SINK; |
| 10999 | config->ext.mix.hw_module = mAudioHwDev->handle(); |
| 11000 | config->ext.mix.usecase.source = mAudioSource; |
| 11001 | } |
| 11002 | } |
| 11003 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11004 | status_t MmapThread::addEffectChain_l(const sp<IAfEffectChain>& chain) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11005 | { |
| 11006 | audio_session_t session = chain->sessionId(); |
| 11007 | |
| 11008 | ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); |
| 11009 | // Attach all tracks with same session ID to this chain. |
| 11010 | // indicate all active tracks in the chain |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 11011 | for (const sp<IAfMmapTrack>& track : mActiveTracks) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11012 | if (session == track->sessionId()) { |
| 11013 | chain->incTrackCnt(); |
| 11014 | chain->incActiveTrackCnt(); |
| 11015 | } |
| 11016 | } |
| 11017 | |
| 11018 | chain->setThread(this); |
| 11019 | chain->setInBuffer(nullptr); |
| 11020 | chain->setOutBuffer(nullptr); |
Shunkai Yao | d125e40 | 2024-01-20 03:19:06 +0000 | [diff] [blame] | 11021 | chain->syncHalEffectsState_l(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11022 | |
| 11023 | mEffectChains.add(chain); |
| 11024 | checkSuspendOnAddEffectChain_l(chain); |
| 11025 | return NO_ERROR; |
| 11026 | } |
| 11027 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11028 | size_t MmapThread::removeEffectChain_l(const sp<IAfEffectChain>& chain) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11029 | { |
| 11030 | audio_session_t session = chain->sessionId(); |
| 11031 | |
| 11032 | ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); |
| 11033 | |
| 11034 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 11035 | if (chain == mEffectChains[i]) { |
| 11036 | mEffectChains.removeAt(i); |
| 11037 | // detach all active tracks from the chain |
| 11038 | // detach all tracks with same session ID from this chain |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 11039 | for (const sp<IAfMmapTrack>& track : mActiveTracks) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11040 | if (session == track->sessionId()) { |
| 11041 | chain->decActiveTrackCnt(); |
| 11042 | chain->decTrackCnt(); |
| 11043 | } |
| 11044 | } |
| 11045 | break; |
| 11046 | } |
| 11047 | } |
| 11048 | return mEffectChains.size(); |
| 11049 | } |
| 11050 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11051 | void MmapThread::threadLoop_standby() |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11052 | { |
| 11053 | mHalStream->standby(); |
| 11054 | } |
| 11055 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11056 | void MmapThread::threadLoop_exit() |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11057 | { |
Phil Burk | 7dce728 | 2017-09-27 13:51:41 -0700 | [diff] [blame] | 11058 | // Do not call callback->onTearDown() because it is redundant for thread exit |
| 11059 | // and because it can cause a recursive mutex lock on stop(). |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11060 | } |
| 11061 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11062 | status_t MmapThread::setSyncEvent(const sp<SyncEvent>& /* event */) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11063 | { |
| 11064 | return BAD_VALUE; |
| 11065 | } |
| 11066 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11067 | bool MmapThread::isValidSyncEvent( |
| 11068 | const sp<SyncEvent>& /* event */) const |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11069 | { |
| 11070 | return false; |
| 11071 | } |
| 11072 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11073 | status_t MmapThread::checkEffectCompatibility_l( |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11074 | const effect_descriptor_t *desc, audio_session_t sessionId) |
| 11075 | { |
| 11076 | // No global effect sessions on mmap threads |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 11077 | if (audio_is_global_session(sessionId)) { |
| 11078 | ALOGW("checkEffectCompatibility_l(): global effect %s on MMAP thread %s", |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11079 | desc->name, mThreadName); |
| 11080 | return BAD_VALUE; |
| 11081 | } |
| 11082 | |
| 11083 | if (!isOutput() && ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC)) { |
| 11084 | ALOGW("checkEffectCompatibility_l(): non pre processing effect %s on capture mmap thread", |
| 11085 | desc->name); |
| 11086 | return BAD_VALUE; |
| 11087 | } |
| 11088 | 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] | 11089 | ALOGW("checkEffectCompatibility_l(): pre processing effect %s created on playback mmap " |
| 11090 | "thread", desc->name); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11091 | return BAD_VALUE; |
| 11092 | } |
| 11093 | |
| 11094 | // Only allow effects without processing load or latency |
| 11095 | if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) != EFFECT_FLAG_NO_PROCESS) { |
| 11096 | return BAD_VALUE; |
| 11097 | } |
| 11098 | |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 11099 | if (IAfEffectModule::isHapticGenerator(&desc->type)) { |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 11100 | ALOGE("%s(): HapticGenerator is not supported for MmapThread", __func__); |
| 11101 | return BAD_VALUE; |
| 11102 | } |
| 11103 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11104 | return NO_ERROR; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11105 | } |
| 11106 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11107 | void MmapThread::checkInvalidTracks_l() |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11108 | { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 11109 | for (const sp<IAfMmapTrack>& track : mActiveTracks) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11110 | if (track->isInvalid()) { |
jiabin | 78b86f2 | 2024-02-22 00:39:29 +0000 | [diff] [blame] | 11111 | if (const sp<MmapStreamCallback> callback = mCallback.promote()) { |
| 11112 | // The aaudioservice handle the routing changed event asynchronously. In that case, |
| 11113 | // it is safe to hold the lock here. |
| 11114 | callback->onRoutingChanged(AUDIO_PORT_HANDLE_NONE); |
| 11115 | } else if (mNoCallbackWarningCount < kMaxNoCallbackWarnings) { |
Eric Laurent | 039c24a | 2022-10-07 14:01:59 +0200 | [diff] [blame] | 11116 | ALOGW("Could not notify MMAP stream tear down: no onRoutingChanged callback!"); |
| 11117 | mNoCallbackWarningCount++; |
| 11118 | } |
| 11119 | break; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11120 | } |
| 11121 | } |
| 11122 | } |
| 11123 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11124 | void MmapThread::dumpInternals_l(int fd, const Vector<String16>& /* args */) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11125 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11126 | dprintf(fd, " Attributes: content type %d usage %d source %d\n", |
| 11127 | mAttr.content_type, mAttr.usage, mAttr.source); |
| 11128 | dprintf(fd, " Session: %d port Id: %d\n", mSessionId, mPortId); |
Eric Tan | 39ec8d6 | 2018-07-24 09:49:29 -0700 | [diff] [blame] | 11129 | if (mActiveTracks.isEmpty()) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11130 | dprintf(fd, " No active clients\n"); |
| 11131 | } |
| 11132 | } |
| 11133 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11134 | void MmapThread::dumpTracks_l(int fd, const Vector<String16>& /* args */) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11135 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11136 | String8 result; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11137 | size_t numtracks = mActiveTracks.size(); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 11138 | dprintf(fd, " %zu Tracks\n", numtracks); |
| 11139 | const char *prefix = " "; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11140 | if (numtracks) { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 11141 | result.append(prefix); |
Kevin Rocard | 5f2136e | 2018-05-11 22:03:00 -0700 | [diff] [blame] | 11142 | mActiveTracks[0]->appendDumpHeader(result); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11143 | for (size_t i = 0; i < numtracks ; ++i) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 11144 | sp<IAfMmapTrack> track = mActiveTracks[i]; |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 11145 | result.append(prefix); |
| 11146 | track->appendDump(result, true /* active */); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11147 | } |
| 11148 | } else { |
| 11149 | dprintf(fd, "\n"); |
| 11150 | } |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 11151 | write(fd, result.c_str(), result.size()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11152 | } |
| 11153 | |
Atneya Nair | aa3afcb | 2024-10-08 16:36:19 -0700 | [diff] [blame] | 11154 | std::string MmapThread::getLocalLogHeader() const { |
| 11155 | using namespace std::literals; |
| 11156 | static constexpr auto indent = " " |
| 11157 | " "sv; |
| 11158 | return std::string{indent}.append(IAfMmapTrack::getLogHeader()); |
| 11159 | } |
| 11160 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11161 | /* static */ |
| 11162 | sp<IAfMmapPlaybackThread> IAfMmapPlaybackThread::create( |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 11163 | const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id, |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11164 | AudioHwDevice* hwDev, AudioStreamOut* output, bool systemReady) { |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 11165 | return sp<MmapPlaybackThread>::make(afThreadCallback, id, hwDev, output, systemReady); |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11166 | } |
| 11167 | |
| 11168 | MmapPlaybackThread::MmapPlaybackThread( |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 11169 | const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id, |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 11170 | AudioHwDevice *hwDev, AudioStreamOut *output, bool systemReady) |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 11171 | : MmapThread(afThreadCallback, id, hwDev, output->stream, systemReady, true /* isOut */), |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11172 | mStreamType(AUDIO_STREAM_MUSIC), |
Phil Burk | 56ecf3e | 2018-03-12 15:38:17 -0700 | [diff] [blame] | 11173 | mOutput(output) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11174 | { |
| 11175 | snprintf(mThreadName, kThreadNameLength, "AudioMmapOut_%X", id); |
| 11176 | mChannelCount = audio_channel_count_from_out_mask(mChannelMask); |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 11177 | mMasterVolume = afThreadCallback->masterVolume_l(); |
| 11178 | mMasterMute = afThreadCallback->masterMute_l(); |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 11179 | if (!audioserver_flags::portid_volume_management()) { |
| 11180 | for (int i = AUDIO_STREAM_MIN; i < AUDIO_STREAM_FOR_POLICY_CNT; ++i) { |
| 11181 | const audio_stream_type_t stream{static_cast<audio_stream_type_t>(i)}; |
| 11182 | mStreamTypes[stream].volume = 0.0f; |
| 11183 | mStreamTypes[stream].mute = mAfThreadCallback->streamMute_l(stream); |
| 11184 | } |
| 11185 | // Audio patch and call assistant volume are always max |
| 11186 | mStreamTypes[AUDIO_STREAM_PATCH].volume = 1.0f; |
| 11187 | mStreamTypes[AUDIO_STREAM_PATCH].mute = false; |
| 11188 | mStreamTypes[AUDIO_STREAM_CALL_ASSISTANT].volume = 1.0f; |
| 11189 | mStreamTypes[AUDIO_STREAM_CALL_ASSISTANT].mute = false; |
Eric Laurent | 1f9b5e6 | 2023-07-03 18:14:07 +0200 | [diff] [blame] | 11190 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11191 | if (mAudioHwDev) { |
| 11192 | if (mAudioHwDev->canSetMasterVolume()) { |
| 11193 | mMasterVolume = 1.0; |
| 11194 | } |
| 11195 | |
| 11196 | if (mAudioHwDev->canSetMasterMute()) { |
| 11197 | mMasterMute = false; |
| 11198 | } |
| 11199 | } |
| 11200 | } |
| 11201 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11202 | void MmapPlaybackThread::configure(const audio_attributes_t* attr, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11203 | audio_stream_type_t streamType, |
| 11204 | audio_session_t sessionId, |
| 11205 | const sp<MmapStreamCallback>& callback, |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 11206 | audio_port_handle_t deviceId, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11207 | audio_port_handle_t portId) |
| 11208 | { |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 11209 | audio_utils::lock_guard l(mutex()); |
| 11210 | MmapThread::configure_l(attr, streamType, sessionId, callback, deviceId, portId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11211 | mStreamType = streamType; |
| 11212 | } |
| 11213 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11214 | AudioStreamOut* MmapPlaybackThread::clearOutput() |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11215 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 11216 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11217 | AudioStreamOut *output = mOutput; |
| 11218 | mOutput = NULL; |
| 11219 | return output; |
| 11220 | } |
| 11221 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11222 | void MmapPlaybackThread::setMasterVolume(float value) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11223 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 11224 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11225 | // Don't apply master volume in SW if our HAL can do it for us. |
| 11226 | if (mAudioHwDev && |
| 11227 | mAudioHwDev->canSetMasterVolume()) { |
| 11228 | mMasterVolume = 1.0; |
| 11229 | } else { |
| 11230 | mMasterVolume = value; |
| 11231 | } |
| 11232 | } |
| 11233 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11234 | void MmapPlaybackThread::setMasterMute(bool muted) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11235 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 11236 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11237 | // Don't apply master mute in SW if our HAL can do it for us. |
| 11238 | if (mAudioHwDev && mAudioHwDev->canSetMasterMute()) { |
| 11239 | mMasterMute = false; |
| 11240 | } else { |
| 11241 | mMasterMute = muted; |
| 11242 | } |
| 11243 | } |
| 11244 | |
Vlad Popa | 1e865e6 | 2024-08-15 19:11:42 -0700 | [diff] [blame] | 11245 | void MmapPlaybackThread::setStreamVolume(audio_stream_type_t stream, float value, bool muted) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11246 | { |
Vlad Popa | 1e865e6 | 2024-08-15 19:11:42 -0700 | [diff] [blame] | 11247 | ALOGV("%s: stream %d value %f muted %d", __func__, stream, value, muted); |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 11248 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 1f9b5e6 | 2023-07-03 18:14:07 +0200 | [diff] [blame] | 11249 | mStreamTypes[stream].volume = value; |
Vlad Popa | 1e865e6 | 2024-08-15 19:11:42 -0700 | [diff] [blame] | 11250 | if (com_android_media_audio_ring_my_car()) { |
| 11251 | mStreamTypes[stream].mute = muted; |
| 11252 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11253 | if (stream == mStreamType) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11254 | broadcast_l(); |
| 11255 | } |
| 11256 | } |
| 11257 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11258 | float MmapPlaybackThread::streamVolume(audio_stream_type_t stream) const |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11259 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 11260 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 1f9b5e6 | 2023-07-03 18:14:07 +0200 | [diff] [blame] | 11261 | return mStreamTypes[stream].volume; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11262 | } |
| 11263 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11264 | void MmapPlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11265 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 11266 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 1f9b5e6 | 2023-07-03 18:14:07 +0200 | [diff] [blame] | 11267 | mStreamTypes[stream].mute = muted; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11268 | if (stream == mStreamType) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11269 | broadcast_l(); |
| 11270 | } |
| 11271 | } |
| 11272 | |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 11273 | status_t MmapPlaybackThread::setPortsVolume( |
Vlad Popa | 1e865e6 | 2024-08-15 19:11:42 -0700 | [diff] [blame] | 11274 | const std::vector<audio_port_handle_t>& portIds, float volume, bool muted) { |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 11275 | audio_utils::lock_guard _l(mutex()); |
| 11276 | for (const auto& portId : portIds) { |
| 11277 | for (const sp<IAfMmapTrack>& track : mActiveTracks) { |
| 11278 | if (portId == track->portId()) { |
| 11279 | track->setPortVolume(volume); |
Vlad Popa | 1e865e6 | 2024-08-15 19:11:42 -0700 | [diff] [blame] | 11280 | track->setPortMute(muted); |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 11281 | break; |
| 11282 | } |
| 11283 | } |
| 11284 | } |
| 11285 | broadcast_l(); |
| 11286 | return NO_ERROR; |
| 11287 | } |
| 11288 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11289 | void MmapPlaybackThread::invalidateTracks(audio_stream_type_t streamType) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11290 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 11291 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11292 | if (streamType == mStreamType) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 11293 | for (const sp<IAfMmapTrack>& track : mActiveTracks) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11294 | track->invalidate(); |
| 11295 | } |
| 11296 | broadcast_l(); |
| 11297 | } |
| 11298 | } |
| 11299 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11300 | void MmapPlaybackThread::invalidateTracks(std::set<audio_port_handle_t>& portIds) |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 11301 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 11302 | audio_utils::lock_guard _l(mutex()); |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 11303 | bool trackMatch = false; |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 11304 | for (const sp<IAfMmapTrack>& track : mActiveTracks) { |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 11305 | if (portIds.find(track->portId()) != portIds.end()) { |
| 11306 | track->invalidate(); |
| 11307 | trackMatch = true; |
| 11308 | portIds.erase(track->portId()); |
| 11309 | } |
| 11310 | if (portIds.empty()) { |
| 11311 | break; |
| 11312 | } |
| 11313 | } |
| 11314 | if (trackMatch) { |
| 11315 | broadcast_l(); |
| 11316 | } |
| 11317 | } |
| 11318 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11319 | void MmapPlaybackThread::processVolume_l() |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 11320 | NO_THREAD_SAFETY_ANALYSIS // access of track->processMuteEvent_l |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11321 | { |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 11322 | float volume = 0; |
| 11323 | if (!audioserver_flags::portid_volume_management()) { |
| 11324 | if (mMasterMute || streamMuted_l()) { |
| 11325 | volume = 0; |
| 11326 | } else { |
| 11327 | volume = mMasterVolume * streamVolume_l(); |
| 11328 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11329 | } else { |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 11330 | if (mMasterMute) { |
| 11331 | volume = 0; |
| 11332 | } else { |
| 11333 | // All mmap tracks are declared with the same audio attributes to the audio policy |
| 11334 | // manager. Hence, they follow the same routing / volume group. Any change of volume |
| 11335 | // will be broadcasted to all tracks. Thus, take arbitrarily first track volume. |
| 11336 | size_t numtracks = mActiveTracks.size(); |
| 11337 | if (numtracks) { |
Vlad Popa | 1e865e6 | 2024-08-15 19:11:42 -0700 | [diff] [blame] | 11338 | if (mActiveTracks[0]->getPortMute()) { |
| 11339 | volume = 0; |
| 11340 | } else { |
| 11341 | volume = mMasterVolume * mActiveTracks[0]->getPortVolume(); |
| 11342 | } |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 11343 | } |
| 11344 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11345 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11346 | if (volume != mHalVolFloat) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11347 | // Convert volumes from float to 8.24 |
| 11348 | uint32_t vol = (uint32_t)(volume * (1 << 24)); |
| 11349 | |
| 11350 | // Delegate volume control to effect in track effect chain if needed |
| 11351 | // only one effect chain can be present on DirectOutputThread, so if |
| 11352 | // there is one, the track is connected to it |
| 11353 | if (!mEffectChains.isEmpty()) { |
Shunkai Yao | f484765 | 2024-01-12 00:25:20 +0000 | [diff] [blame] | 11354 | mEffectChains[0]->setVolume(&vol, &vol); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11355 | volume = (float)vol / (1 << 24); |
| 11356 | } |
Eric Laurent | dff774a | 2017-04-21 15:29:38 -0700 | [diff] [blame] | 11357 | // 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] | 11358 | if (mOutput->stream->setVolume(volume, volume) == NO_ERROR) { |
| 11359 | mHalVolFloat = volume; // HW volume control worked, so update value. |
| 11360 | mNoCallbackWarningCount = 0; |
| 11361 | } else { |
Eric Laurent | dff774a | 2017-04-21 15:29:38 -0700 | [diff] [blame] | 11362 | sp<MmapStreamCallback> callback = mCallback.promote(); |
| 11363 | if (callback != 0) { |
Phil Burk | 56ecf3e | 2018-03-12 15:38:17 -0700 | [diff] [blame] | 11364 | mHalVolFloat = volume; // SW volume control worked, so update value. |
| 11365 | mNoCallbackWarningCount = 0; |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 11366 | mutex().unlock(); |
Robert Wu | 4389ae6 | 2022-02-17 18:39:41 +0000 | [diff] [blame] | 11367 | callback->onVolumeChanged(volume); |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 11368 | mutex().lock(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11369 | } else { |
Phil Burk | 56ecf3e | 2018-03-12 15:38:17 -0700 | [diff] [blame] | 11370 | if (mNoCallbackWarningCount < kMaxNoCallbackWarnings) { |
| 11371 | ALOGW("Could not set MMAP stream volume: no volume callback!"); |
| 11372 | mNoCallbackWarningCount++; |
| 11373 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11374 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11375 | } |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 11376 | for (const sp<IAfMmapTrack>& track : mActiveTracks) { |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 11377 | track->setMetadataHasChanged(); |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 11378 | if (!audioserver_flags::portid_volume_management()) { |
| 11379 | track->processMuteEvent_l(mAfThreadCallback->getOrCreateAudioManager(), |
| 11380 | /*muteState=*/{mMasterMute, |
| 11381 | streamVolume_l() == 0.f, |
| 11382 | streamMuted_l(), |
| 11383 | // TODO(b/241533526): adjust logic to include mute from AppOps |
| 11384 | false /*muteFromPlaybackRestricted*/, |
| 11385 | false /*muteFromClientVolume*/, |
Vlad Popa | 1e865e6 | 2024-08-15 19:11:42 -0700 | [diff] [blame] | 11386 | false /*muteFromVolumeShaper*/, |
| 11387 | false /*muteFromPortVolume*/}); |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 11388 | } else { |
| 11389 | track->processMuteEvent_l(mAfThreadCallback->getOrCreateAudioManager(), |
| 11390 | /*muteState=*/{mMasterMute, |
| 11391 | track->getPortVolume() == 0.f, |
| 11392 | /* muteFromStreamMuted= */ false, |
| 11393 | // TODO(b/241533526): adjust logic to include mute from AppOps |
| 11394 | false /*muteFromPlaybackRestricted*/, |
| 11395 | false /*muteFromClientVolume*/, |
Vlad Popa | 1e865e6 | 2024-08-15 19:11:42 -0700 | [diff] [blame] | 11396 | false /*muteFromVolumeShaper*/, |
| 11397 | track->getPortMute()}); |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 11398 | } |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 11399 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11400 | } |
| 11401 | } |
| 11402 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11403 | ThreadBase::MetadataUpdate MmapPlaybackThread::updateMetadata_l() |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11404 | { |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 11405 | if (!isStreamInitialized() || !mActiveTracks.readAndClearHasChanged()) { |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 11406 | return {}; // nothing to do |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11407 | } |
| 11408 | StreamOutHalInterface::SourceMetadata metadata; |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 11409 | for (const sp<IAfMmapTrack>& track : mActiveTracks) { |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11410 | // 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] | 11411 | playback_track_metadata_v7_t trackMetadata; |
| 11412 | trackMetadata.base = { |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11413 | .usage = track->attributes().usage, |
| 11414 | .content_type = track->attributes().content_type, |
| 11415 | .gain = mHalVolFloat, // TODO: propagate from aaudio pre-mix volume |
Eric Laurent | 9457917 | 2020-11-20 18:41:04 +0100 | [diff] [blame] | 11416 | }; |
| 11417 | trackMetadata.channel_mask = track->channelMask(), |
| 11418 | strncpy(trackMetadata.tags, track->attributes().tags, AUDIO_ATTRIBUTES_TAGS_MAX_SIZE); |
| 11419 | metadata.tracks.push_back(trackMetadata); |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11420 | } |
| 11421 | mOutput->stream->updateSourceMetadata(metadata); |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 11422 | |
| 11423 | MetadataUpdate change; |
| 11424 | change.playbackMetadataUpdate = metadata.tracks; |
| 11425 | return change; |
| 11426 | }; |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11427 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11428 | void MmapPlaybackThread::checkSilentMode_l() |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11429 | { |
Atneya Nair | 967c85f | 2024-10-27 16:09:50 -0700 | [diff] [blame] | 11430 | if (property_get_bool("ro.audio.silent", false)) { |
| 11431 | ALOGW("ro.audio.silent is now ignored"); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11432 | } |
| 11433 | } |
| 11434 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11435 | void MmapPlaybackThread::toAudioPortConfig(struct audio_port_config* config) |
Mikhail Naganov | 32abc2b | 2018-05-24 12:57:11 -0700 | [diff] [blame] | 11436 | { |
| 11437 | MmapThread::toAudioPortConfig(config); |
| 11438 | if (mOutput && mOutput->flags != AUDIO_OUTPUT_FLAG_NONE) { |
| 11439 | config->config_mask |= AUDIO_PORT_CONFIG_FLAGS; |
| 11440 | config->flags.output = mOutput->flags; |
| 11441 | } |
| 11442 | } |
| 11443 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11444 | status_t MmapPlaybackThread::getExternalPosition(uint64_t* position, |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 11445 | int64_t* timeNanos) const |
jiabin | b7d8c5a | 2020-08-26 17:24:52 -0700 | [diff] [blame] | 11446 | { |
| 11447 | if (mOutput == nullptr) { |
| 11448 | return NO_INIT; |
| 11449 | } |
| 11450 | struct timespec timestamp; |
| 11451 | status_t status = mOutput->getPresentationPosition(position, ×tamp); |
| 11452 | if (status == NO_ERROR) { |
| 11453 | *timeNanos = timestamp.tv_sec * NANOS_PER_SECOND + timestamp.tv_nsec; |
| 11454 | } |
| 11455 | return status; |
| 11456 | } |
| 11457 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11458 | status_t MmapPlaybackThread::reportData(const void* buffer, size_t frameCount) { |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 11459 | // Send to MelProcessor for sound dose measurement. |
| 11460 | auto processor = mMelProcessor.load(); |
| 11461 | if (processor) { |
| 11462 | processor->process(buffer, frameCount * mFrameSize); |
| 11463 | } |
| 11464 | |
jiabin | fc791ee | 2023-02-15 19:43:40 +0000 | [diff] [blame] | 11465 | return NO_ERROR; |
| 11466 | } |
| 11467 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 11468 | // startMelComputation_l() must be called with AudioFlinger::mutex() held |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11469 | void MmapPlaybackThread::startMelComputation_l( |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 11470 | const sp<audio_utils::MelProcessor>& processor) |
| 11471 | { |
| 11472 | ALOGV("%s: starting mel processor for thread %d", __func__, id()); |
Vlad Popa | 1c2f7e1 | 2023-03-28 02:08:56 +0200 | [diff] [blame] | 11473 | mMelProcessor.store(processor); |
| 11474 | if (processor) { |
| 11475 | processor->resume(); |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 11476 | } |
Vlad Popa | 1c2f7e1 | 2023-03-28 02:08:56 +0200 | [diff] [blame] | 11477 | |
| 11478 | // no need to update output format for MMapPlaybackThread since it is |
| 11479 | // assigned constant for each thread |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 11480 | } |
| 11481 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 11482 | // stopMelComputation_l() must be called with AudioFlinger::mutex() held |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11483 | void MmapPlaybackThread::stopMelComputation_l() |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 11484 | { |
Vlad Popa | 1c2f7e1 | 2023-03-28 02:08:56 +0200 | [diff] [blame] | 11485 | ALOGV("%s: pausing mel processor for thread %d", __func__, id()); |
| 11486 | auto melProcessor = mMelProcessor.load(); |
| 11487 | if (melProcessor != nullptr) { |
| 11488 | melProcessor->pause(); |
| 11489 | } |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 11490 | } |
| 11491 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11492 | void MmapPlaybackThread::dumpInternals_l(int fd, const Vector<String16>& args) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11493 | { |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 11494 | MmapThread::dumpInternals_l(fd, args); |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 11495 | if (!audioserver_flags::portid_volume_management()) { |
| 11496 | dprintf(fd, " Stream type: %d Stream volume: %f HAL volume: %f Stream mute %d", |
| 11497 | mStreamType, streamVolume_l(), mHalVolFloat, streamMuted_l()); |
| 11498 | } else { |
| 11499 | dprintf(fd, " HAL volume: %f", mHalVolFloat); |
| 11500 | } |
| 11501 | dprintf(fd, "\n"); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11502 | dprintf(fd, " Master volume: %f Master mute %d\n", mMasterVolume, mMasterMute); |
| 11503 | } |
| 11504 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11505 | /* static */ |
| 11506 | sp<IAfMmapCaptureThread> IAfMmapCaptureThread::create( |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 11507 | const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id, |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11508 | AudioHwDevice* hwDev, AudioStreamIn* input, bool systemReady) { |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 11509 | return sp<MmapCaptureThread>::make(afThreadCallback, id, hwDev, input, systemReady); |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11510 | } |
| 11511 | |
| 11512 | MmapCaptureThread::MmapCaptureThread( |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 11513 | const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id, |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 11514 | AudioHwDevice *hwDev, AudioStreamIn *input, bool systemReady) |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 11515 | : MmapThread(afThreadCallback, id, hwDev, input->stream, systemReady, false /* isOut */), |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11516 | mInput(input) |
| 11517 | { |
| 11518 | snprintf(mThreadName, kThreadNameLength, "AudioMmapIn_%X", id); |
| 11519 | mChannelCount = audio_channel_count_from_in_mask(mChannelMask); |
| 11520 | } |
| 11521 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11522 | status_t MmapCaptureThread::exitStandby_l() |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 11523 | { |
Phil Burk | f054fc3 | 2018-12-06 09:45:59 -0800 | [diff] [blame] | 11524 | { |
| 11525 | // mInput might have been cleared by clearInput() |
Phil Burk | f054fc3 | 2018-12-06 09:45:59 -0800 | [diff] [blame] | 11526 | if (mInput != nullptr && mInput->stream != nullptr) { |
| 11527 | mInput->stream->setGain(1.0f); |
| 11528 | } |
| 11529 | } |
Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 11530 | return MmapThread::exitStandby_l(); |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 11531 | } |
| 11532 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11533 | AudioStreamIn* MmapCaptureThread::clearInput() |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11534 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 11535 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11536 | AudioStreamIn *input = mInput; |
| 11537 | mInput = NULL; |
| 11538 | return input; |
| 11539 | } |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11540 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11541 | void MmapCaptureThread::processVolume_l() |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 11542 | { |
| 11543 | bool changed = false; |
| 11544 | bool silenced = false; |
| 11545 | |
| 11546 | sp<MmapStreamCallback> callback = mCallback.promote(); |
| 11547 | if (callback == 0) { |
| 11548 | if (mNoCallbackWarningCount < kMaxNoCallbackWarnings) { |
| 11549 | ALOGW("Could not set MMAP stream silenced: no onStreamSilenced callback!"); |
| 11550 | mNoCallbackWarningCount++; |
| 11551 | } |
| 11552 | } |
| 11553 | |
| 11554 | // After a change occurred in track silenced state, mute capture in audio DSP if at least one |
| 11555 | // track is silenced and unmute otherwise |
| 11556 | for (size_t i = 0; i < mActiveTracks.size() && !silenced; i++) { |
| 11557 | if (!mActiveTracks[i]->getAndSetSilencedNotified_l()) { |
| 11558 | changed = true; |
| 11559 | silenced = mActiveTracks[i]->isSilenced_l(); |
| 11560 | } |
| 11561 | } |
| 11562 | |
| 11563 | if (changed) { |
| 11564 | mInput->stream->setGain(silenced ? 0.0f: 1.0f); |
| 11565 | } |
| 11566 | } |
| 11567 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11568 | ThreadBase::MetadataUpdate MmapCaptureThread::updateMetadata_l() |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11569 | { |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 11570 | if (!isStreamInitialized() || !mActiveTracks.readAndClearHasChanged()) { |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 11571 | return {}; // nothing to do |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11572 | } |
| 11573 | StreamInHalInterface::SinkMetadata metadata; |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 11574 | for (const sp<IAfMmapTrack>& track : mActiveTracks) { |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11575 | // 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] | 11576 | record_track_metadata_v7_t trackMetadata; |
| 11577 | trackMetadata.base = { |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11578 | .source = track->attributes().source, |
| 11579 | .gain = 1, // capture tracks do not have volumes |
Eric Laurent | 9457917 | 2020-11-20 18:41:04 +0100 | [diff] [blame] | 11580 | }; |
| 11581 | trackMetadata.channel_mask = track->channelMask(), |
| 11582 | strncpy(trackMetadata.tags, track->attributes().tags, AUDIO_ATTRIBUTES_TAGS_MAX_SIZE); |
| 11583 | metadata.tracks.push_back(trackMetadata); |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11584 | } |
| 11585 | mInput->stream->updateSinkMetadata(metadata); |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 11586 | MetadataUpdate change; |
| 11587 | change.recordMetadataUpdate = metadata.tracks; |
| 11588 | return change; |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11589 | } |
| 11590 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11591 | void MmapCaptureThread::setRecordSilenced(audio_port_handle_t portId, bool silenced) |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 11592 | { |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 11593 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 11594 | for (size_t i = 0; i < mActiveTracks.size() ; i++) { |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 11595 | if (mActiveTracks[i]->portId() == portId) { |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 11596 | mActiveTracks[i]->setSilenced_l(silenced); |
| 11597 | broadcast_l(); |
| 11598 | } |
| 11599 | } |
jiabin | 0960903 | 2022-06-15 19:26:01 +0000 | [diff] [blame] | 11600 | setClientSilencedIfExists_l(portId, silenced); |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 11601 | } |
| 11602 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11603 | void MmapCaptureThread::toAudioPortConfig(struct audio_port_config* config) |
Mikhail Naganov | 32abc2b | 2018-05-24 12:57:11 -0700 | [diff] [blame] | 11604 | { |
| 11605 | MmapThread::toAudioPortConfig(config); |
| 11606 | if (mInput && mInput->flags != AUDIO_INPUT_FLAG_NONE) { |
| 11607 | config->config_mask |= AUDIO_PORT_CONFIG_FLAGS; |
| 11608 | config->flags.input = mInput->flags; |
| 11609 | } |
| 11610 | } |
| 11611 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11612 | status_t MmapCaptureThread::getExternalPosition( |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 11613 | uint64_t* position, int64_t* timeNanos) const |
jiabin | b7d8c5a | 2020-08-26 17:24:52 -0700 | [diff] [blame] | 11614 | { |
| 11615 | if (mInput == nullptr) { |
| 11616 | return NO_INIT; |
| 11617 | } |
| 11618 | return mInput->getCapturePosition((int64_t*)position, timeNanos); |
| 11619 | } |
| 11620 | |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 11621 | // ---------------------------------------------------------------------------- |
| 11622 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11623 | /* static */ |
| 11624 | sp<IAfPlaybackThread> IAfPlaybackThread::createBitPerfectThread( |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 11625 | const sp<IAfThreadCallback>& afThreadCallback, |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11626 | AudioStreamOut* output, audio_io_handle_t id, bool systemReady) { |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 11627 | return sp<BitPerfectThread>::make(afThreadCallback, output, id, systemReady); |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11628 | } |
| 11629 | |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 11630 | BitPerfectThread::BitPerfectThread(const sp<IAfThreadCallback> &afThreadCallback, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 11631 | AudioStreamOut *output, audio_io_handle_t id, bool systemReady) |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 11632 | : MixerThread(afThreadCallback, output, id, systemReady, BIT_PERFECT) {} |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 11633 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11634 | PlaybackThread::mixer_state BitPerfectThread::prepareTracks_l( |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 11635 | Vector<sp<IAfTrack>>* tracksToRemove) { |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 11636 | mixer_state result = MixerThread::prepareTracks_l(tracksToRemove); |
| 11637 | // 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] | 11638 | float volumeLeft = 1.0f; |
| 11639 | float volumeRight = 1.0f; |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 11640 | if (sp<IAfTrack> bitPerfectTrack = getTrackToStreamBitPerfectly_l(); |
| 11641 | bitPerfectTrack != nullptr) { |
| 11642 | const int trackId = bitPerfectTrack->id(); |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 11643 | mAudioMixer->setParameter( |
| 11644 | trackId, AudioMixer::TRACK, AudioMixer::TEE_BUFFER, (void *)mSinkBuffer); |
| 11645 | mAudioMixer->setParameter( |
| 11646 | trackId, AudioMixer::TRACK, AudioMixer::TEE_BUFFER_FRAME_COUNT, |
| 11647 | (void *)(uintptr_t)mNormalFrameCount); |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 11648 | bitPerfectTrack->getFinalVolume(&volumeLeft, &volumeRight); |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 11649 | mIsBitPerfect = true; |
| 11650 | } else { |
| 11651 | mIsBitPerfect = false; |
| 11652 | // No need to copy bit-perfect data directly to sink buffer given there are multiple tracks |
| 11653 | // active. |
| 11654 | for (const auto& track : mActiveTracks) { |
| 11655 | const int trackId = track->id(); |
| 11656 | mAudioMixer->setParameter( |
| 11657 | trackId, AudioMixer::TRACK, AudioMixer::TEE_BUFFER, nullptr); |
| 11658 | } |
| 11659 | } |
jiabin | 76d9469 | 2022-12-15 21:51:21 +0000 | [diff] [blame] | 11660 | if (mVolumeLeft != volumeLeft || mVolumeRight != volumeRight) { |
| 11661 | mVolumeLeft = volumeLeft; |
| 11662 | mVolumeRight = volumeRight; |
| 11663 | setVolumeForOutput_l(volumeLeft, volumeRight); |
| 11664 | } |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 11665 | return result; |
| 11666 | } |
| 11667 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11668 | void BitPerfectThread::threadLoop_mix() { |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 11669 | MixerThread::threadLoop_mix(); |
| 11670 | mHasDataCopiedToSinkBuffer = mIsBitPerfect; |
| 11671 | } |
| 11672 | |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 11673 | void BitPerfectThread::setTracksInternalMute( |
| 11674 | std::map<audio_port_handle_t, bool>* tracksInternalMute) { |
jiabin | 783a1eb | 2024-09-18 22:36:19 +0000 | [diff] [blame] | 11675 | audio_utils::lock_guard _l(mutex()); |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 11676 | for (auto& track : mTracks) { |
| 11677 | if (auto it = tracksInternalMute->find(track->portId()); it != tracksInternalMute->end()) { |
| 11678 | track->setInternalMute(it->second); |
| 11679 | tracksInternalMute->erase(it); |
| 11680 | } |
| 11681 | } |
| 11682 | } |
| 11683 | |
| 11684 | sp<IAfTrack> BitPerfectThread::getTrackToStreamBitPerfectly_l() { |
| 11685 | if (com::android::media::audioserver:: |
| 11686 | fix_concurrent_playback_behavior_with_bit_perfect_client()) { |
| 11687 | sp<IAfTrack> bitPerfectTrack = nullptr; |
| 11688 | bool allOtherTracksMuted = true; |
| 11689 | // Return the bit perfect track if all other tracks are muted |
| 11690 | for (const auto& track : mActiveTracks) { |
| 11691 | if (track->isBitPerfect()) { |
jiabin | 783a1eb | 2024-09-18 22:36:19 +0000 | [diff] [blame] | 11692 | if (track->getInternalMute()) { |
| 11693 | // There can only be one bit-perfect client active. If it is mute internally, |
| 11694 | // there is no need to stream bit-perfectly. |
| 11695 | break; |
| 11696 | } |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 11697 | bitPerfectTrack = track; |
| 11698 | } else if (track->getFinalVolume() != 0.f) { |
| 11699 | allOtherTracksMuted = false; |
| 11700 | if (bitPerfectTrack != nullptr) { |
| 11701 | break; |
| 11702 | } |
| 11703 | } |
| 11704 | } |
| 11705 | return allOtherTracksMuted ? bitPerfectTrack : nullptr; |
| 11706 | } else { |
| 11707 | if (mActiveTracks.size() == 1 && mActiveTracks[0]->isBitPerfect()) { |
| 11708 | return mActiveTracks[0]; |
| 11709 | } |
| 11710 | } |
| 11711 | return nullptr; |
| 11712 | } |
| 11713 | |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 11714 | } // namespace android |