| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of the Falcon Player (FPP) and is Copyright (C) | ||
| 3 | * 2013-2026 by the Falcon Player Developers. | ||
| 4 | * | ||
| 5 | * The Falcon Player (FPP) is free software, and is covered under | ||
| 6 | * multiple Open Source licenses. Please see the included 'LICENSES' | ||
| 7 | * file for descriptions of what files are covered by each license. | ||
| 8 | * | ||
| 9 | * This source file is covered under the LGPL v2.1 as described in the | ||
| 10 | * included LICENSE.LGPL file. | ||
| 11 | */ | ||
| 12 | |||
| 13 | #include "fpp-pch.h" | ||
| 14 | |||
| 15 | #include "PipeWireOutputStream.h" | ||
| 16 | |||
| 17 | #include <gst/base/gstbasesink.h> | ||
| 18 | |||
| 19 | #ifdef HAS_GSTREAMER | ||
| 20 | |||
| 21 | #include <gst/app/gstappsink.h> | ||
| 22 | |||
| 23 | // The stream is a native PipeWire stream (see OpenLocked()). Without | ||
| 24 | // libpipewire's headers (macOS) there is no stream: ForSlot() returns none, | ||
| 25 | // and media plays through a pipewiresink of its own. | ||
| 26 | #if __has_include(<pipewire/pipewire.h>) | ||
| 27 | #define FPP_NATIVE_PIPEWIRE 1 | ||
| 28 | #include <pipewire/pipewire.h> | ||
| 29 | #include <spa/param/audio/format-utils.h> | ||
| 30 | #endif | ||
| 31 | |||
| 32 | #if defined(__ARM_NEON) || defined(__ARM_NEON__) | ||
| 33 | #include <arm_neon.h> | ||
| 34 | #endif | ||
| 35 | |||
| 36 | #include <algorithm> | ||
| 37 | #include <array> | ||
| 38 | #include <atomic> | ||
| 39 | #include <chrono> | ||
| 40 | #include <climits> | ||
| 41 | #include <cmath> | ||
| 42 | #include <ctime> | ||
| 43 | #include <cstdio> | ||
| 44 | #include <cstring> | ||
| 45 | #include <thread> | ||
| 46 | #include <vector> | ||
| 47 | |||
| 48 | #include "common.h" | ||
| 49 | #include "common_mini.h" | ||
| 50 | #include "log.h" | ||
| 51 | #include "settings.h" | ||
| 52 | #include "StreamSlotManager.h" | ||
| 53 | |||
| 54 | namespace { | ||
| 55 | // Every stream carries F32 stereo at the graph's own rate, so PipeWire never | ||
| 56 | // resamples it and a feed only converts what differs. | ||
| 57 | constexpr int kChannels = 2; | ||
| 58 | constexpr int kBytesPerFrame = 4 * kChannels; | ||
| 59 | |||
| 60 | // The graph's cycle (FPP's default quantum is 1024 frames). | ||
| 61 | constexpr int64_t kGraphQuantum = 1024; | ||
| 62 | // How many graph cycles ahead of its time a feed hands each buffer over (see | ||
| 63 | // AttachFeed()), and how far ahead of its time the graph may take a sample | ||
| 64 | // from the stream: a whole cycle, at worst. Carrying on a track's timeline | ||
| 65 | // (ContinueTimeline()) needs the last track's sound still queued when its | ||
| 66 | // end of stream comes, a cycle more than the graph may already have taken. | ||
| 67 | constexpr int64_t kFeedLeadQuanta = 2; | ||
| 68 | constexpr int64_t kGraphAheadQuanta = 1; | ||
| 69 | // The least time the last track's sound is left queued, beyond what the | ||
| 70 | // graph may already have taken, when its end of stream comes: the time the | ||
| 71 | // next track has to carry on before silence goes in between them -- the end | ||
| 72 | // of stream reaching the playlist, which only looks on its main loop's ticks | ||
| 73 | // (a sequence's frame, up to 50 ms at 20 fps), the next entry starting, and | ||
| 74 | // its pipeline going to PLAYING. Measured from the end of stream to the next | ||
| 75 | // track carrying on: 1-5 ms on a Pi, 3-6 ms on an AM62x, 16-32 ms on a | ||
| 76 | // BeagleBone Black at 100 fps; and its first buffer comes some ms after that. | ||
| 77 | // Short of it, the first buffer comes after the graph may need it, and the | ||
| 78 | // stream is flushed (the track's head is lost, and the next track starts on a | ||
| 79 | // drained stream). Where a sample plays doesn't depend on this. | ||
| 80 | constexpr int64_t kFeedHandoverMs = 100; | ||
| 81 | |||
| 82 | // Longest a stream may take to come up before it is abandoned. A cold open is | ||
| 83 | // ~0.6 s on an AM335x; this only trips when PipeWire itself is unwell. | ||
| 84 | constexpr int kOpenTimeoutMs = 8000; | ||
| 85 | // How often an opening stream's links are checked (each check runs pw-link). | ||
| 86 | constexpr int kLinkCheckIntervalMs = 50; | ||
| 87 | // With no card to watch (a group of network sinks), how long the stream must | ||
| 88 | // have been linked before it is trusted to be on the real driver. | ||
| 89 | constexpr int kNoCardSettleMs = 150; | ||
| 90 | // With the stream kept open, how long after a failure to try again. A | ||
| 91 | // PipeWire restart under a running fppd leaves GStreamer's PipeWire connection | ||
| 92 | // dead until fppd restarts, so retrying hard would only fill the log. | ||
| 93 | constexpr int kKeepOpenRetryMs = 30000; | ||
| 94 | |||
| 95 | // The card clock (see DisciplineClock()). How often the card's position is | ||
| 96 | // read, how much of its history the rate is fitted over, how fast a phase | ||
| 97 | // error is closed, and how far the clock may be pushed off the system's. | ||
| 98 | constexpr int kClockSampleMs = 250; | ||
| 99 | constexpr double kClockWindowS = 120.0; | ||
| 100 | constexpr double kClockMinFitS = 4.0; | ||
| 101 | constexpr double kClockPhaseGain = 1.0 / 20.0; // per second of error, per second | ||
| 102 | constexpr double kClockMaxPhaseSlew = 500e-6; | ||
| 103 | constexpr double kClockMaxOffset = 2000e-6; | ||
| 104 | // A jump this big in the card's position is a restart or an xrun, not drift: | ||
| 105 | // start again from it rather than chase it. | ||
| 106 | constexpr double kClockMaxPhaseError = 0.050; | ||
| 107 | // How long after one of our own flushes the card stopping or restarting is | ||
| 108 | // put down to the flush. A flush takes the stream inactive and back, and | ||
| 109 | // where it is the card's only follower (a Pi 4's headphone jack) PipeWire | ||
| 110 | // stops the device and starts it again; the samples a few hundred ms later | ||
| 111 | // see that. | ||
| 112 | constexpr uint64_t kFlushRestartMs = 1000; | ||
| 113 | // The card's queue (MeasureCardQueue()): how often it is read, how often for | ||
| 114 | // the first while after the card starts -- its queue deepens for ~2 s as | ||
| 115 | // PipeWire settles on the card's pace -- and how many readings are taken the | ||
| 116 | // median of. A correction past kCardQueueMaxCorrMs is a misreading. The fast | ||
| 117 | // readings are spaced at random: at a fixed interval close to the graph's | ||
| 118 | // cycle, a run of them all fell at the same point in it. | ||
| 119 | constexpr int kCardQueueSampleMs = 250; | ||
| 120 | constexpr int kCardQueueFastMs = 20; // ...plus up to kCardQueueJitterMs | ||
| 121 | constexpr int kCardQueueJitterMs = 17; | ||
| 122 | constexpr uint64_t kCardQueueSettleMs = 3000; | ||
| 123 | constexpr size_t kCardQueueMedianOf = 5; | ||
| 124 | constexpr int64_t kCardQueueMaxCorrMs = 250; | ||
| 125 | // How long a stopped track's timeline is kept running with silence for the | ||
| 126 | // next track to carry on (Release(), ContinueTimeline()). A playlist's next | ||
| 127 | // track starts within tens of ms of the last one stopping; one that comes | ||
| 128 | // later than this (after a pause entry, or a sequence that outlasts its | ||
| 129 | // media) starts on a flushed stream, as every track used to. | ||
| 130 | constexpr int kBridgeMs = 3000; | ||
| 131 | // How little of the timeline a bridge lets be left before it adds silence, | ||
| 132 | // and how far ahead it then takes it: a little more than the worker's | ||
| 133 | // wake-ups (4 ms apart). The next track normally comes before the last | ||
| 134 | // one's sound has run out, and any silence laid before it needs it is a gap | ||
| 135 | // between them. A bridge that runs out anyway (a worker held up) only ends | ||
| 136 | // the timeline: the next track starts on a flushed stream. | ||
| 137 | constexpr int kBridgeLowMs = 6; | ||
| 138 | // Most silence put in front of a carried-on track whose sound starts after | ||
| 139 | // the start of its timeline (see NoteTimelineLocked()). | ||
| 140 | constexpr int kMaxLeadInGapMs = 500; | ||
| 141 | constexpr int kBridgeHighMs = 10; | ||
| 142 | // How long a pipeline carrying on the timeline is given to go to PLAYING | ||
| 143 | // and hand over its first buffer, and the least it is left in the queue | ||
| 144 | // beyond that (silence is added if less is left). A prepared pipeline goes | ||
| 145 | // to PLAYING in about a millisecond. | ||
| 146 | constexpr int kContinueHandoverMs = 3; | ||
| 147 | // How far behind its sink's schedule a feed buffer may come before it counts | ||
| 148 | // as late (NoteTimelineLocked()): a thread's wake-up, not a stall. | ||
| 149 | constexpr int kFeedLateToleranceMs = 5; | ||
| 150 | constexpr int kContinueMinQueuedMs = 8; | ||
| 151 | |||
| 152 | std::mutex s_slotsLock; | ||
| 153 | std::array<PipeWireOutputStream*, StreamSlotManager::MAX_SLOTS + 1> s_slots{}; | ||
| 154 | |||
| 155 | std::mutex s_workerLock; | ||
| 156 | std::condition_variable s_workerWake; | ||
| 157 | std::thread s_worker; | ||
| 158 | bool s_workerRunning = false; | ||
| 159 | // Set by ShutdownAll() and never cleared: a ForSlot() racing it (a prewarm, a | ||
| 160 | // settings listener) must not start a worker again, let alone assign a new | ||
| 161 | // std::thread over the one ShutdownAll() is joining. | ||
| 162 | bool s_workerShutDown = false; | ||
| 163 | bool s_workerKick = false; | ||
| 164 | |||
| 165 | std::atomic<bool> s_keepOpen{ false }; | ||
| 166 | |||
| 167 | 295962 | uint64_t NowMs() { | |
| 168 | 295962 | return (uint64_t)GetMonotonicTimeMS(); // holds and timeouts: must not step with the wall clock | |
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | 239 | PipeWireOutputStream::PipeWireOutputStream(int slot) : | |
| 173 |
4/8✓ Branch 0 taken 239 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 239 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 239 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 239 times.
✗ Branch 7 not taken.
|
239 | m_slot(slot) { |
| 174 | 239 | } | |
| 175 | |||
| 176 | 6420 | PipeWireOutputStream* PipeWireOutputStream::ForSlot(int slot) { | |
| 177 | #ifndef FPP_NATIVE_PIPEWIRE | ||
| 178 | return nullptr; | ||
| 179 | #endif | ||
| 180 |
7/10✓ Branch 0 taken 6420 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6420 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6420 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 6418 times.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 6418 times.
|
6420 | if (slot < 1 || slot > StreamSlotManager::MAX_SLOTS || !isPipeWireBackend()) { |
| 181 | 2 | return nullptr; | |
| 182 | } | ||
| 183 |
1/2✓ Branch 0 taken 6418 times.
✗ Branch 1 not taken.
|
6418 | std::unique_lock<std::mutex> lock(s_slotsLock); |
| 184 |
2/2✓ Branch 0 taken 239 times.
✓ Branch 1 taken 6179 times.
|
6418 | if (!s_slots[slot]) { |
| 185 |
3/8✓ Branch 0 taken 239 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 239 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 239 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
239 | s_slots[slot] = new PipeWireOutputStream(slot); |
| 186 | } | ||
| 187 | { | ||
| 188 |
1/2✓ Branch 0 taken 6418 times.
✗ Branch 1 not taken.
|
6418 | std::unique_lock<std::mutex> wlock(s_workerLock); |
| 189 |
4/4✓ Branch 0 taken 371 times.
✓ Branch 1 taken 6047 times.
✓ Branch 2 taken 222 times.
✓ Branch 3 taken 149 times.
|
6418 | if (!s_workerRunning && !s_workerShutDown) { |
| 190 | 222 | s_workerRunning = true; | |
| 191 |
1/2✓ Branch 0 taken 222 times.
✗ Branch 1 not taken.
|
222 | s_worker = std::thread(WorkerLoop); |
| 192 | } | ||
| 193 | 6418 | } | |
| 194 | 6418 | return s_slots[slot]; | |
| 195 | 6418 | } | |
| 196 | |||
| 197 | 4428 | bool PipeWireOutputStream::SlotMayCarryOn(int slot) { | |
| 198 |
2/4✓ Branch 0 taken 4428 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4428 times.
|
4428 | if (slot < 1 || slot > StreamSlotManager::MAX_SLOTS) { |
| 199 | ✗ | return false; | |
| 200 | } | ||
| 201 | PipeWireOutputStream* s; | ||
| 202 | { | ||
| 203 |
1/2✓ Branch 0 taken 4428 times.
✗ Branch 1 not taken.
|
4428 | std::unique_lock<std::mutex> lock(s_slotsLock); |
| 204 | 4428 | s = s_slots[slot]; | |
| 205 | 4428 | } | |
| 206 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 4332 times.
|
4428 | if (!s) { |
| 207 | 96 | return false; | |
| 208 | } | ||
| 209 |
1/2✓ Branch 0 taken 4332 times.
✗ Branch 1 not taken.
|
4332 | std::unique_lock<std::mutex> lock(s->m_lock); |
| 210 |
3/4✓ Branch 0 taken 1649 times.
✓ Branch 1 taken 2683 times.
✓ Branch 2 taken 1649 times.
✗ Branch 3 not taken.
|
4332 | return s->m_timelineValid && s->m_state == State::Ready; |
| 211 | 4332 | } | |
| 212 | |||
| 213 | 236 | int64_t PipeWireOutputStream::QueuedAheadNsForSlot(int slot) { | |
| 214 |
2/4✓ Branch 0 taken 236 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 236 times.
|
236 | if (slot < 1 || slot > StreamSlotManager::MAX_SLOTS) { |
| 215 | ✗ | return -1; | |
| 216 | } | ||
| 217 | PipeWireOutputStream* s; | ||
| 218 | { | ||
| 219 |
1/2✓ Branch 0 taken 236 times.
✗ Branch 1 not taken.
|
236 | std::unique_lock<std::mutex> lock(s_slotsLock); |
| 220 | 236 | s = s_slots[slot]; | |
| 221 | 236 | } | |
| 222 |
1/2✓ Branch 0 taken 236 times.
✗ Branch 1 not taken.
|
236 | return s ? s->QueuedAheadNs() : -1; |
| 223 | } | ||
| 224 | |||
| 225 | 236 | int64_t PipeWireOutputStream::QueuedAheadNs() { | |
| 226 | struct timespec ts; | ||
| 227 | 236 | clock_gettime(CLOCK_MONOTONIC, &ts); | |
| 228 | 236 | int64_t nowNs = (int64_t)ts.tv_sec * GST_SECOND + ts.tv_nsec; | |
| 229 |
1/2✓ Branch 0 taken 236 times.
✗ Branch 1 not taken.
|
236 | std::unique_lock<std::mutex> lock(m_lock); |
| 230 | uint64_t heard; | ||
| 231 |
7/10✓ Branch 0 taken 234 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 234 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 234 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 234 times.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 234 times.
|
236 | if (!m_pwStream || m_rate <= 0 || !FrameHeardAt(nowNs, &heard)) { |
| 232 | 2 | return -1; | |
| 233 | } | ||
| 234 | 234 | uint64_t write = m_writePos.load(std::memory_order_relaxed); | |
| 235 |
2/4✓ Branch 0 taken 234 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 234 times.
✗ Branch 3 not taken.
|
234 | return write > heard ? (int64_t)gst_util_uint64_scale(write - heard, GST_SECOND, m_rate) : 0; |
| 236 | 236 | } | |
| 237 | |||
| 238 | 2344 | void PipeWireOutputStream::Prewarm(int slot, int holdMs) { | |
| 239 |
1/2✓ Branch 0 taken 2344 times.
✗ Branch 1 not taken.
|
2344 | PipeWireOutputStream* s = ForSlot(slot); |
| 240 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2342 times.
|
2344 | if (!s) { |
| 241 | 2 | return; | |
| 242 | } | ||
| 243 |
1/2✓ Branch 0 taken 2342 times.
✗ Branch 1 not taken.
|
2342 | std::string target = GStreamerOutput::PipeWireSinkNameForSlot(slot); |
| 244 | { | ||
| 245 |
1/2✓ Branch 0 taken 2342 times.
✗ Branch 1 not taken.
|
2342 | std::unique_lock<std::mutex> lock(s->m_lock); |
| 246 |
1/2✓ Branch 0 taken 2342 times.
✗ Branch 1 not taken.
|
2342 | s->m_wantTarget = target; |
| 247 |
1/2✓ Branch 0 taken 2342 times.
✗ Branch 1 not taken.
|
2342 | s->m_holdUntilMs = std::max(s->m_holdUntilMs, NowMs() + (uint64_t)std::max(holdMs, 0)); |
| 248 | 2342 | s->m_wantOpen = true; | |
| 249 |
1/2✓ Branch 0 taken 2342 times.
✗ Branch 1 not taken.
|
2342 | s->ForgetFailureLocked(); |
| 250 | 2342 | } | |
| 251 |
1/2✓ Branch 0 taken 2342 times.
✗ Branch 1 not taken.
|
2342 | WakeWorker(); |
| 252 | 2342 | } | |
| 253 | |||
| 254 | 2651 | void PipeWireOutputStream::SetKeepOpen(bool keepOpen) { | |
| 255 |
2/2✓ Branch 0 taken 2629 times.
✓ Branch 1 taken 22 times.
|
2651 | if (s_keepOpen.exchange(keepOpen) == keepOpen) { |
| 256 | 2629 | return; | |
| 257 | } | ||
| 258 |
1/2✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
|
22 | if (keepOpen) { |
| 259 | 22 | LogInfo(VB_MEDIAOUT, "PipeWire output stream 1 will be kept open\n"); | |
| 260 | 22 | Prewarm(1, 0); | |
| 261 | } else { | ||
| 262 | // Falls back to the usual linger; the worker closes it once that | ||
| 263 | // and any playing track are done. | ||
| 264 | ✗ | LogInfo(VB_MEDIAOUT, "PipeWire output stream 1 no longer kept open\n"); | |
| 265 | ✗ | if (PipeWireOutputStream* s = ForSlot(1)) { | |
| 266 | ✗ | std::unique_lock<std::mutex> lock(s->m_lock); | |
| 267 | ✗ | s->m_holdUntilMs = std::max(s->m_holdUntilMs, NowMs() + LINGER_MS); | |
| 268 | ✗ | } | |
| 269 | ✗ | WakeWorker(); | |
| 270 | } | ||
| 271 | } | ||
| 272 | |||
| 273 | 2279 | std::string PipeWireOutputStream::CapsFields() { | |
| 274 |
2/4✓ Branch 0 taken 2279 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2279 times.
✗ Branch 3 not taken.
|
4558 | return ",format=F32LE,rate=" + std::to_string(GStreamerOutput::PipeWireGraphRate()) + |
| 275 |
3/6✓ Branch 0 taken 2279 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2279 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2279 times.
✗ Branch 5 not taken.
|
6748 | ",channels=" + std::to_string(kChannels) + ",layout=interleaved"; |
| 276 | } | ||
| 277 | |||
| 278 | 2620 | void PipeWireOutputStream::ShutdownAll() { | |
| 279 | 2620 | std::thread worker; | |
| 280 | { | ||
| 281 |
1/2✓ Branch 0 taken 2620 times.
✗ Branch 1 not taken.
|
2620 | std::unique_lock<std::mutex> wlock(s_workerLock); |
| 282 | 2620 | s_workerShutDown = true; | |
| 283 |
2/2✓ Branch 0 taken 2398 times.
✓ Branch 1 taken 222 times.
|
2620 | if (!s_workerRunning) { |
| 284 | 2398 | return; | |
| 285 | } | ||
| 286 | 222 | s_workerRunning = false; | |
| 287 | 222 | worker = std::move(s_worker); | |
| 288 |
2/2✓ Branch 0 taken 222 times.
✓ Branch 1 taken 2398 times.
|
2620 | } |
| 289 | 222 | s_workerWake.notify_all(); | |
| 290 |
1/2✓ Branch 0 taken 222 times.
✗ Branch 1 not taken.
|
222 | if (worker.joinable()) { |
| 291 |
1/2✓ Branch 0 taken 222 times.
✗ Branch 1 not taken.
|
222 | worker.join(); |
| 292 | } | ||
| 293 |
1/2✓ Branch 0 taken 222 times.
✗ Branch 1 not taken.
|
222 | std::unique_lock<std::mutex> lock(s_slotsLock); |
| 294 |
2/2✓ Branch 0 taken 1332 times.
✓ Branch 1 taken 222 times.
|
1554 | for (PipeWireOutputStream* s : s_slots) { |
| 295 |
2/2✓ Branch 0 taken 239 times.
✓ Branch 1 taken 1093 times.
|
1332 | if (s) { |
| 296 |
1/2✓ Branch 0 taken 239 times.
✗ Branch 1 not taken.
|
239 | std::unique_lock<std::mutex> slock(s->m_lock); |
| 297 | 239 | s->m_users = 0; | |
| 298 | 239 | s->m_holdUntilMs = 0; | |
| 299 |
1/2✓ Branch 0 taken 239 times.
✗ Branch 1 not taken.
|
239 | s->CloseLocked("fppd shutting down"); |
| 300 | 239 | } | |
| 301 | } | ||
| 302 |
2/2✓ Branch 0 taken 222 times.
✓ Branch 1 taken 2398 times.
|
2620 | } |
| 303 | |||
| 304 | 2041 | void PipeWireOutputStream::Acquire(const std::string& target) { | |
| 305 | { | ||
| 306 |
1/2✓ Branch 0 taken 2041 times.
✗ Branch 1 not taken.
|
2041 | std::unique_lock<std::mutex> lock(m_lock); |
| 307 | 2041 | m_users++; | |
| 308 |
1/2✓ Branch 0 taken 2041 times.
✗ Branch 1 not taken.
|
2041 | m_wantTarget = target; |
| 309 | 2041 | m_wantOpen = true; | |
| 310 |
1/2✓ Branch 0 taken 2041 times.
✗ Branch 1 not taken.
|
2041 | ForgetFailureLocked(); |
| 311 | 2041 | } | |
| 312 | 2041 | WakeWorker(); | |
| 313 | 2041 | } | |
| 314 | |||
| 315 | // A failure belongs to the attempt that made it. Left in place, the next | ||
| 316 | // track's WaitReady() returned on it at once while the worker was already | ||
| 317 | // opening the stream again, and the head of that track went nowhere. | ||
| 318 | 4383 | void PipeWireOutputStream::ForgetFailureLocked() { | |
| 319 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 4319 times.
|
4383 | if (m_state == State::Failed) { |
| 320 | 64 | m_state = State::Closed; | |
| 321 | } | ||
| 322 | 4383 | } | |
| 323 | |||
| 324 | static int64_t MonoUs(); | ||
| 325 | |||
| 326 | 1989 | void PipeWireOutputStream::Release(GstElement* feed) { | |
| 327 | { | ||
| 328 |
1/2✓ Branch 0 taken 1989 times.
✗ Branch 1 not taken.
|
1989 | std::unique_lock<std::mutex> lock(m_lock); |
| 329 |
1/2✓ Branch 0 taken 1989 times.
✗ Branch 1 not taken.
|
1989 | if (feed) { |
| 330 | // Nothing asks what this feed is heard at once it stops. | ||
| 331 |
1/2✓ Branch 0 taken 1989 times.
✗ Branch 1 not taken.
|
1989 | DropFeedSpansLocked(feed); |
| 332 | } | ||
| 333 |
2/4✓ Branch 0 taken 1989 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1989 times.
✗ Branch 3 not taken.
|
1989 | if (feed && m_feed == feed) { |
| 334 | 1989 | m_feed = nullptr; | |
| 335 | 1989 | m_continuedFirst = false; | |
| 336 |
3/4✓ Branch 0 taken 790 times.
✓ Branch 1 taken 1199 times.
✓ Branch 2 taken 790 times.
✗ Branch 3 not taken.
|
1989 | if (m_timelineValid && m_state == State::Ready) { |
| 337 | // Keep the timeline running for the next track to carry on | ||
| 338 | // (ContinueTimeline()): silence laid right behind the last | ||
| 339 | // sample, topped up by the worker (ServiceBridge()). It also | ||
| 340 | // pushes the track's last fragment out, as below. | ||
| 341 | 790 | m_bridging = true; | |
| 342 |
1/2✓ Branch 0 taken 790 times.
✗ Branch 1 not taken.
|
790 | m_bridgeUntilMs = NowMs() + kBridgeMs; |
| 343 | 790 | m_releasedUs = MonoUs(); | |
| 344 | 790 | m_bridgedNs = 0; | |
| 345 | } else { | ||
| 346 |
1/2✓ Branch 0 taken 1199 times.
✗ Branch 1 not taken.
|
1199 | EndTimelineLocked(); |
| 347 | } | ||
| 348 | } | ||
| 349 |
1/2✓ Branch 0 taken 1989 times.
✗ Branch 1 not taken.
|
1989 | if (m_users > 0) { |
| 350 | 1989 | m_users--; | |
| 351 | } | ||
| 352 |
1/2✓ Branch 0 taken 1989 times.
✗ Branch 1 not taken.
|
1989 | m_holdUntilMs = std::max(m_holdUntilMs, NowMs() + LINGER_MS); |
| 353 | 1989 | } | |
| 354 | 1989 | WakeWorker(); | |
| 355 | 1989 | } | |
| 356 | |||
| 357 | // How far ahead of its time the graph may take a sample from the stream: the | ||
| 358 | // queue counts as run out that much before the timeline's end. | ||
| 359 | 136988 | static GstClockTime GraphAheadNs() { | |
| 360 | 136988 | return (GstClockTime)(kGraphAheadQuanta * kGraphQuantum * 1000000000LL / GStreamerOutput::PipeWireGraphRate()); | |
| 361 | } | ||
| 362 | |||
| 363 | 35782 | static int64_t MonoUs() { | |
| 364 | struct timespec ts; | ||
| 365 | 35782 | clock_gettime(CLOCK_MONOTONIC, &ts); | |
| 366 | 35782 | return (int64_t)ts.tv_sec * 1000000LL + ts.tv_nsec / 1000; | |
| 367 | } | ||
| 368 | |||
| 369 | 34950 | bool PipeWireOutputStream::WaitQueueEmpty(int64_t deadlineUs) { | |
| 370 | while (true) { | ||
| 371 | uint64_t left; | ||
| 372 | int rate; | ||
| 373 | { | ||
| 374 |
1/2✓ Branch 0 taken 34950 times.
✗ Branch 1 not taken.
|
34950 | std::unique_lock<std::mutex> lock(m_lock); |
| 375 | 104850 | uint64_t read = std::max(m_readPos.load(std::memory_order_acquire), m_dropTo.load(std::memory_order_acquire)); | |
| 376 | 34950 | uint64_t write = m_writePos.load(std::memory_order_acquire); | |
| 377 |
2/2✓ Branch 0 taken 32966 times.
✓ Branch 1 taken 1984 times.
|
34950 | left = write > read ? write - read : 0; |
| 378 | 34950 | rate = m_rate; | |
| 379 |
3/4✓ Branch 0 taken 34934 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 34934 times.
|
34950 | if (!m_pwStream || rate <= 0) { |
| 380 | 16 | return true; | |
| 381 | } | ||
| 382 |
2/2✓ Branch 0 taken 34934 times.
✓ Branch 1 taken 16 times.
|
34950 | } |
| 383 |
2/2✓ Branch 0 taken 1971 times.
✓ Branch 1 taken 32963 times.
|
34934 | if (left == 0) { |
| 384 | 1971 | return true; | |
| 385 | } | ||
| 386 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 32946 times.
|
32963 | if (MonoUs() >= deadlineUs) { |
| 387 | 17 | return false; | |
| 388 | } | ||
| 389 | 32946 | int64_t us = (int64_t)(left * 1000000 / (uint64_t)rate); | |
| 390 |
2/4✓ Branch 0 taken 32946 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32946 times.
✗ Branch 3 not taken.
|
32946 | std::this_thread::sleep_for(std::chrono::microseconds(std::clamp<int64_t>(us, 1000, 5000))); |
| 391 | 32946 | } | |
| 392 | } | ||
| 393 | |||
| 394 | 2004 | void PipeWireOutputStream::WaitDrained(int maxMs) { | |
| 395 | { | ||
| 396 | // A bridge would never let it drain. | ||
| 397 |
1/2✓ Branch 0 taken 2004 times.
✗ Branch 1 not taken.
|
2004 | std::unique_lock<std::mutex> lock(m_lock); |
| 398 |
1/2✓ Branch 0 taken 2004 times.
✗ Branch 1 not taken.
|
2004 | EndTimelineLocked(); |
| 399 | 2004 | } | |
| 400 | 2004 | WaitQueueEmpty(MonoUs() + (int64_t)maxMs * 1000); | |
| 401 | 2004 | Flush(FlushFor::NextTrack); | |
| 402 | 2004 | } | |
| 403 | |||
| 404 | // A flush drops whatever is still queued for the graph. Between tracks that | ||
| 405 | // is normally nothing; after a stall that never reached the card (a late | ||
| 406 | // group node) it is what the stall held up, which would otherwise sit ahead | ||
| 407 | // of every track to come (issue #3083). When the stall is an xrun on the | ||
| 408 | // card itself, DisciplineClock() sees the card stop or restart, and once it | ||
| 409 | // runs again the stream is flushed mid-track: the sound skips what the stall | ||
| 410 | // held up -- the xrun was already audible -- and is back on time at once | ||
| 411 | // rather than at the next track. A stall that never reaches the card is | ||
| 412 | // left to the flush between tracks. | ||
| 413 | // | ||
| 414 | // Only the queue is dropped: the PipeWire stream itself runs on, playing | ||
| 415 | // silence while there is nothing to play, so nothing restarts and nothing | ||
| 416 | // after the flush is lost. Where what follows lands doesn't need guessing | ||
| 417 | // either: HeardRunningTime() measures it. | ||
| 418 | 2039 | void PipeWireOutputStream::Flush(FlushFor why) { | |
| 419 | { | ||
| 420 |
1/2✓ Branch 0 taken 2039 times.
✗ Branch 1 not taken.
|
2039 | std::unique_lock<std::mutex> lock(m_lock); |
| 421 | // Whatever the timeline was, a flush ends it, and with it anything | ||
| 422 | // that may have been late in it. | ||
| 423 |
1/2✓ Branch 0 taken 2039 times.
✗ Branch 1 not taken.
|
2039 | EndTimelineLocked(); |
| 424 | 2039 | m_continuedFirst = false; | |
| 425 | // A flush covers an xrun still waiting for one. | ||
| 426 | 2039 | m_xrunPending = false; | |
| 427 |
5/6✓ Branch 0 taken 2023 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 2023 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1433 times.
✓ Branch 5 taken 590 times.
|
2039 | if (!m_pwStream || m_state != State::Ready || m_flushed) { |
| 428 | 1449 | return; | |
| 429 | } | ||
| 430 | 590 | m_flushed = true; | |
| 431 |
1/2✓ Branch 0 taken 590 times.
✗ Branch 1 not taken.
|
590 | m_lastFlushMs = NowMs(); |
| 432 | 1180 | m_dropTo.store(m_writePos.load(std::memory_order_relaxed), std::memory_order_release); | |
| 433 |
2/2✓ Branch 0 taken 590 times.
✓ Branch 1 taken 1449 times.
|
2039 | } |
| 434 |
4/4✓ Branch 0 taken 35 times.
✓ Branch 1 taken 555 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 30 times.
|
590 | LogDebug(VB_MEDIAOUT, "PipeWire output stream %d flushed%s\n", m_slot, |
| 435 | why == FlushFor::NextTrack ? " between tracks" : why == FlushFor::MidFeed ? " mid-feed" : " for a seek"); | ||
| 436 | } | ||
| 437 | |||
| 438 | 2041 | bool PipeWireOutputStream::WaitReady(int timeoutMs) { | |
| 439 | // Ready only counts on the target asked for: a stream still Ready on the | ||
| 440 | // previous sink is about to be closed and reopened by the worker. | ||
| 441 |
1/2✓ Branch 0 taken 2041 times.
✗ Branch 1 not taken.
|
2041 | std::unique_lock<std::mutex> lock(m_lock); |
| 442 |
4/4✓ Branch 0 taken 4033 times.
✓ Branch 1 taken 226 times.
✓ Branch 2 taken 4032 times.
✓ Branch 3 taken 1 times.
|
4259 | auto readyHere = [this] { return m_state == State::Ready && m_target == m_wantTarget; }; |
| 443 |
1/2✓ Branch 0 taken 2029 times.
✗ Branch 1 not taken.
|
2041 | m_stateChanged.wait_for(lock, std::chrono::milliseconds(timeoutMs), [this, &readyHere] { |
| 444 |
4/4✓ Branch 0 taken 214 times.
✓ Branch 1 taken 2016 times.
✓ Branch 2 taken 201 times.
✓ Branch 3 taken 13 times.
|
2431 | return readyHere() || m_state == State::Failed || |
| 445 |
3/4✓ Branch 0 taken 30 times.
✓ Branch 1 taken 171 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 30 times.
|
2431 | (m_state == State::Closed && !m_wantOpen); |
| 446 | }); | ||
| 447 | 4058 | return readyHere(); | |
| 448 | 2029 | } | |
| 449 | |||
| 450 | 237350 | int64_t PipeWireOutputStream::FeedLeadNs() { | |
| 451 | 237350 | int64_t quantum = kGraphQuantum * (int64_t)1000000000 / GStreamerOutput::PipeWireGraphRate(); | |
| 452 | 237350 | return std::max(kFeedLeadQuanta * quantum, kGraphAheadQuanta * quantum + kFeedHandoverMs * (int64_t)1000000); | |
| 453 | } | ||
| 454 | |||
| 455 | |||
| 456 | 4030 | void PipeWireOutputStream::DropFeedSpansLocked(GstElement* feed) { | |
| 457 |
2/4✓ Branch 0 taken 4030 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4030 times.
✗ Branch 3 not taken.
|
4146 | m_feedSpans.erase(std::remove_if(m_feedSpans.begin(), m_feedSpans.end(), |
| 458 | 9326 | [feed](const FeedSpan& s) { return s.feed == feed; }), | |
| 459 | 4030 | m_feedSpans.end()); | |
| 460 | 4030 | } | |
| 461 | |||
| 462 | 2041 | void PipeWireOutputStream::AttachFeed(GstElement* appsink) { | |
| 463 | { | ||
| 464 |
1/2✓ Branch 0 taken 2041 times.
✗ Branch 1 not taken.
|
2041 | std::unique_lock<std::mutex> lock(m_lock); |
| 465 | // Spans are known by their feed's address, and an appsink can be | ||
| 466 | // allocated where a freed one was: any already under this one are | ||
| 467 | // that one's, and would be taken for this track's sound. | ||
| 468 |
1/2✓ Branch 0 taken 2041 times.
✗ Branch 1 not taken.
|
2041 | DropFeedSpansLocked(appsink); |
| 469 | 2041 | m_feed = appsink; | |
| 470 | 2041 | m_continuedFirst = false; | |
| 471 | // Each feed starts at the card's own pace. The feed clock is put back | ||
| 472 | // on the card clock now, before this feed's pipeline has a base time | ||
| 473 | // on it, so a speed the last track was steered at doesn't carry over. | ||
| 474 | 2041 | m_feedSpeed = 1.0; | |
| 475 | 2041 | m_feedDiverged = false; | |
| 476 | 2041 | m_rsActive = false; | |
| 477 | 2041 | m_rsPos = 0.0; | |
| 478 | 2041 | m_rsHistFrames = 0; | |
| 479 |
2/4✓ Branch 0 taken 2041 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2041 times.
✗ Branch 3 not taken.
|
2041 | if (m_clock && m_feedClock) { |
| 480 |
2/4✓ Branch 0 taken 2041 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2041 times.
✗ Branch 3 not taken.
|
2041 | RecalibrateFeedClockLocked(gst_clock_get_internal_time(m_clock)); |
| 481 | } | ||
| 482 | 2041 | } | |
| 483 | // Run the feed two graph cycles (FeedLeadNs()) ahead of the clock. Handed over exactly | ||
| 484 | // on time, a buffer can miss the cycle that needed it -- the graph may | ||
| 485 | // take a sample up to a cycle before its time; PipeWire then plays a | ||
| 486 | // quantum of silence there and the rest of the track is a quantum late. | ||
| 487 | // Two cycles' worth queued absorbs that, and leaves the last track's | ||
| 488 | // sound still queued when a track carrying on its timeline takes over | ||
| 489 | // (at least kFeedHandoverMs beyond the cycle, whatever the graph's rate). | ||
| 490 | // (Where a sample plays doesn't depend on this: the queue is contiguous | ||
| 491 | // from the track's first sample.) | ||
| 492 | 2041 | g_object_set(appsink, "emit-signals", TRUE, "ts-offset", -FeedLeadNs(), NULL); | |
| 493 | 2041 | g_signal_connect(appsink, "new-sample", G_CALLBACK(OnFeedSample), this); | |
| 494 | 2041 | } | |
| 495 | |||
| 496 | // Runs on the feeding pipeline's streaming thread. `this` is a per-slot | ||
| 497 | // singleton that is never freed, so a feed outliving its GStreamerOutput (async | ||
| 498 | // teardown) can still land here safely; it is simply no longer m_feed. | ||
| 499 | 64559 | GstFlowReturn PipeWireOutputStream::OnFeedSample(GstElement* appsink, gpointer userData) { | |
| 500 | 64559 | PipeWireOutputStream* self = static_cast<PipeWireOutputStream*>(userData); | |
| 501 | 64559 | GstSample* sample = gst_app_sink_pull_sample(GST_APP_SINK(appsink)); | |
| 502 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 64559 times.
|
64559 | if (!sample) { |
| 503 | ✗ | return GST_FLOW_OK; | |
| 504 | } | ||
| 505 | 64559 | GstBuffer* in = gst_sample_get_buffer(sample); | |
| 506 | 64559 | const GstSegment* segment = gst_sample_get_segment(sample); | |
| 507 | 64559 | GstCaps* caps = gst_sample_get_caps(sample); | |
| 508 | |||
| 509 | 64559 | GstBuffer* out = nullptr; | |
| 510 | { | ||
| 511 |
1/2✓ Branch 0 taken 64559 times.
✗ Branch 1 not taken.
|
64559 | std::unique_lock<std::mutex> lock(self->m_lock); |
| 512 |
5/8✓ Branch 0 taken 64559 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 64559 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 63865 times.
✓ Branch 5 taken 694 times.
✓ Branch 6 taken 63865 times.
✗ Branch 7 not taken.
|
64559 | bool ours = in && caps && self->m_pwStream && self->m_feed == appsink; |
| 513 |
6/10✓ Branch 0 taken 63865 times.
✓ Branch 1 taken 694 times.
✓ Branch 2 taken 63865 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 63865 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 63865 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 64559 times.
|
64559 | if (ours && self->m_caps && !gst_caps_can_intersect(self->m_caps, caps)) { |
| 514 | // Can't happen unless the graph rate moved under a running fppd; | ||
| 515 | // playing it would be noise, so drop it and say so once. | ||
| 516 | ✗ | if (!self->m_warnedCaps) { | |
| 517 | ✗ | self->m_warnedCaps = true; | |
| 518 | ✗ | gchar* got = gst_caps_to_string(caps); | |
| 519 | ✗ | gchar* want = gst_caps_to_string(self->m_caps); | |
| 520 | ✗ | LogErr(VB_MEDIAOUT, "PipeWire output stream %d: feed is %s, stream is %s -- dropping it\n", | |
| 521 | self->m_slot, got, want); | ||
| 522 | ✗ | g_free(got); | |
| 523 | ✗ | g_free(want); | |
| 524 | } | ||
| 525 | ✗ | ours = false; | |
| 526 | } | ||
| 527 |
2/2✓ Branch 0 taken 63865 times.
✓ Branch 1 taken 694 times.
|
64559 | if (ours) { |
| 528 | // Steered to another speed (SetFeedSpeed()), the media is | ||
| 529 | // resampled to the card's rate here; otherwise it goes in as it | ||
| 530 | // came, and only its last frames are kept for a later start. | ||
| 531 |
2/2✓ Branch 0 taken 15504 times.
✓ Branch 1 taken 48361 times.
|
63865 | if (self->m_rsActive) { |
| 532 |
1/2✓ Branch 0 taken 15504 times.
✗ Branch 1 not taken.
|
15504 | out = self->ResampleLocked(in); |
| 533 | } else { | ||
| 534 |
1/2✓ Branch 0 taken 48361 times.
✗ Branch 1 not taken.
|
48361 | self->KeepResampleHistoryLocked(in); |
| 535 |
1/2✓ Branch 0 taken 48361 times.
✗ Branch 1 not taken.
|
48361 | out = gst_buffer_ref(in); |
| 536 | } | ||
| 537 | 63865 | ours = out != nullptr; | |
| 538 | } | ||
| 539 | GstMapInfo map; | ||
| 540 |
6/8✓ Branch 0 taken 63865 times.
✓ Branch 1 taken 694 times.
✓ Branch 2 taken 63865 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 63865 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 63865 times.
✓ Branch 7 taken 694 times.
|
64559 | if (ours && gst_buffer_map(out, &map, GST_MAP_READ)) { |
| 541 | 63865 | size_t frames = map.size / kBytesPerFrame; | |
| 542 | // The feed clock is the card clock (until it is steered, then off | ||
| 543 | // it by however far it has run ahead or behind), so a buffer's | ||
| 544 | // running time in the feeding pipeline maps onto the stream's | ||
| 545 | // timeline through the base time and that offset. | ||
| 546 |
1/2✓ Branch 0 taken 63865 times.
✗ Branch 1 not taken.
|
63865 | GstClockTime rt = gst_segment_to_running_time(segment, GST_FORMAT_TIME, GST_BUFFER_PTS(in)); |
| 547 |
1/2✓ Branch 0 taken 63865 times.
✗ Branch 1 not taken.
|
63865 | if (GST_CLOCK_TIME_IS_VALID(rt)) { |
| 548 | // A feed normally hands over a lead early (a ts-offset of minus | ||
| 549 | // FeedLeadNs()); one carrying on the timeline runs on a base | ||
| 550 | // time that much earlier with no offset instead (see | ||
| 551 | // GStreamerOutput's m_timelineShiftNs). Either way the sample | ||
| 552 | // belongs where base + running time + offset + lead says. | ||
| 553 |
3/6✓ Branch 0 taken 63865 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 63865 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 63865 times.
✗ Branch 5 not taken.
|
63865 | GstClockTimeDiff tsOffset = gst_base_sink_get_ts_offset(GST_BASE_SINK(appsink)); |
| 554 |
1/2✓ Branch 0 taken 63865 times.
✗ Branch 1 not taken.
|
63865 | GstClockTimeDiff shift = tsOffset + FeedLeadNs(); |
| 555 |
1/2✓ Branch 0 taken 63865 times.
✗ Branch 1 not taken.
|
63865 | GstClockTime abs = gst_element_get_base_time(appsink) + rt; |
| 556 | // When the sink was due to hand it over: as for any sink, no | ||
| 557 | // earlier than its base time. The first buffers of a track all | ||
| 558 | // come at once, at that, and that is on schedule. | ||
| 559 | 63865 | GstClockTimeDiff late = 0; | |
| 560 |
2/4✓ Branch 0 taken 63865 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 63865 times.
✗ Branch 3 not taken.
|
63865 | if (GstClock* feedClock = gst_element_get_clock(appsink)) { |
| 561 | 63865 | GstClockTimeDiff due = (GstClockTimeDiff)rt + tsOffset; | |
| 562 |
2/4✓ Branch 0 taken 63865 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 63865 times.
✗ Branch 3 not taken.
|
63865 | late = GST_CLOCK_DIFF(gst_element_get_base_time(appsink) + (due > 0 ? due : 0), |
| 563 | gst_clock_get_time(feedClock)); | ||
| 564 |
1/2✓ Branch 0 taken 63865 times.
✗ Branch 1 not taken.
|
63865 | gst_object_unref(feedClock); |
| 565 | } | ||
| 566 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 63865 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
63865 | abs = shift < 0 && (GstClockTime)-shift > abs ? 0 : abs + shift; |
| 567 |
2/2✓ Branch 0 taken 15504 times.
✓ Branch 1 taken 48361 times.
|
63865 | if (self->m_feedDiverged) { |
| 568 |
2/4✓ Branch 0 taken 15504 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15504 times.
✗ Branch 3 not taken.
|
15504 | GstClockTimeDiff ahead = GST_CLOCK_DIFF(gst_clock_get_time(self->m_clock), |
| 569 | gst_clock_get_time(self->m_feedClock)); | ||
| 570 |
3/4✓ Branch 0 taken 8848 times.
✓ Branch 1 taken 6656 times.
✓ Branch 2 taken 8848 times.
✗ Branch 3 not taken.
|
15504 | abs = ahead > 0 && (GstClockTime)ahead > abs ? 0 : abs - ahead; |
| 571 | } | ||
| 572 | // May queue silence ahead of it (a carried-on track's lead-in). | ||
| 573 |
1/2✓ Branch 0 taken 63865 times.
✗ Branch 1 not taken.
|
63865 | self->NoteTimelineLocked(abs, frames, late); |
| 574 | ✗ | } else if (self->m_timelineValid && self->m_rate > 0) { | |
| 575 | // Outside the segment (a decoder's last, clipped buffer): no | ||
| 576 | // time of its own, but it is queued right behind the one | ||
| 577 | // before it all the same. | ||
| 578 | ✗ | LogDebug(VB_MEDIAOUT, "PipeWire output stream %d: a feed buffer outside its segment\n", self->m_slot); | |
| 579 | ✗ | self->m_timelineEnd += gst_util_uint64_scale(frames, GST_SECOND, self->m_rate); | |
| 580 | } | ||
| 581 |
1/2✓ Branch 0 taken 63865 times.
✗ Branch 1 not taken.
|
63865 | uint64_t pos = self->WriteQueueLocked(reinterpret_cast<const float*>(map.data), frames); |
| 582 |
1/2✓ Branch 0 taken 63865 times.
✗ Branch 1 not taken.
|
63865 | if (pos != UINT64_MAX) { |
| 583 | // What this buffer is, for HeardRunningTime(): its running | ||
| 584 | // time, or (outside the segment) the end of the one before. | ||
| 585 |
2/2✓ Branch 0 taken 15504 times.
✓ Branch 1 taken 48361 times.
|
63865 | double speed = self->m_rsActive ? self->m_feedSpeed : 1.0; |
| 586 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 63865 times.
|
63865 | if (!GST_CLOCK_TIME_IS_VALID(rt)) { |
| 587 | ✗ | if (!self->m_feedSpans.empty() && self->m_feedSpans.back().feed == appsink) { | |
| 588 | ✗ | const FeedSpan& last = self->m_feedSpans.back(); | |
| 589 | ✗ | rt = last.runningTime + (GstClockTime)(last.frames * last.speed * GST_SECOND / self->m_rate); | |
| 590 | } | ||
| 591 | } | ||
| 592 |
1/2✓ Branch 0 taken 63865 times.
✗ Branch 1 not taken.
|
63865 | if (GST_CLOCK_TIME_IS_VALID(rt)) { |
| 593 |
1/2✓ Branch 0 taken 63865 times.
✗ Branch 1 not taken.
|
63865 | self->m_feedSpans.push_back({ pos, frames, rt, speed, appsink }); |
| 594 | } | ||
| 595 | // Long gone to the graph (a second is kept, as in | ||
| 596 | // HeardRunningTime(), which trims too: a track whose position | ||
| 597 | // comes from elsewhere never asks). | ||
| 598 | 63865 | uint64_t read = self->m_readPos.load(std::memory_order_acquire); | |
| 599 |
3/4✓ Branch 0 taken 103870 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40005 times.
✓ Branch 3 taken 63865 times.
|
207740 | while (!self->m_feedSpans.empty() && |
| 600 |
2/2✓ Branch 0 taken 40005 times.
✓ Branch 1 taken 63865 times.
|
103870 | self->m_feedSpans.front().queuePos + self->m_feedSpans.front().frames + (uint64_t)self->m_rate < read) { |
| 601 | 40005 | self->m_feedSpans.pop_front(); | |
| 602 | } | ||
| 603 | } | ||
| 604 |
1/2✓ Branch 0 taken 63865 times.
✗ Branch 1 not taken.
|
63865 | gst_buffer_unmap(out, &map); |
| 605 | 63865 | self->m_flushed = false; | |
| 606 | } | ||
| 607 | 64559 | } | |
| 608 |
2/2✓ Branch 0 taken 63865 times.
✓ Branch 1 taken 694 times.
|
64559 | if (out) { |
| 609 | 63865 | gst_buffer_unref(out); | |
| 610 | } | ||
| 611 | 64559 | gst_sample_unref(sample); | |
| 612 | 64559 | return GST_FLOW_OK; | |
| 613 | } | ||
| 614 | |||
| 615 | 6372 | void PipeWireOutputStream::WakeWorker() { | |
| 616 | { | ||
| 617 |
1/2✓ Branch 0 taken 6372 times.
✗ Branch 1 not taken.
|
6372 | std::unique_lock<std::mutex> wlock(s_workerLock); |
| 618 | 6372 | s_workerKick = true; | |
| 619 | 6372 | } | |
| 620 | 6372 | s_workerWake.notify_all(); | |
| 621 | 6372 | } | |
| 622 | |||
| 623 | 222 | void PipeWireOutputStream::WorkerLoop() { | |
| 624 |
2/4✓ Branch 0 taken 222 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 222 times.
✗ Branch 3 not taken.
|
444 | SetThreadName("FPP-PWStream"); |
| 625 | while (true) { | ||
| 626 | // Slots are created on demand but never freed, so a snapshot of the | ||
| 627 | // table is safe to service without holding s_slotsLock -- which | ||
| 628 | // ForSlot() callers on the main loop would otherwise wait on while a | ||
| 629 | // stream connects to the daemon. | ||
| 630 | std::array<PipeWireOutputStream*, StreamSlotManager::MAX_SLOTS + 1> slots; | ||
| 631 | { | ||
| 632 |
1/2✓ Branch 0 taken 93500 times.
✗ Branch 1 not taken.
|
93500 | std::unique_lock<std::mutex> lock(s_slotsLock); |
| 633 | 93500 | slots = s_slots; | |
| 634 | 93500 | } | |
| 635 | 93500 | bool anyOpening = false; | |
| 636 | 93500 | bool anyBridging = false; | |
| 637 | 93500 | int measureMs = 250; | |
| 638 |
1/2✓ Branch 0 taken 93500 times.
✗ Branch 1 not taken.
|
93500 | uint64_t now = NowMs(); |
| 639 |
2/2✓ Branch 0 taken 561000 times.
✓ Branch 1 taken 93500 times.
|
654500 | for (PipeWireOutputStream* s : slots) { |
| 640 |
2/2✓ Branch 0 taken 108558 times.
✓ Branch 1 taken 452442 times.
|
561000 | if (s) { |
| 641 |
3/4✓ Branch 0 taken 108558 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 108553 times.
|
108558 | if (s->DisciplineClock()) { |
| 642 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | LogInfo(VB_MEDIAOUT, "PipeWire output stream %d: the card lost time (xrun) while playing; " |
| 643 | "flushing so the sound stays with the lights\n", s->m_slot); | ||
| 644 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | s->Flush(FlushFor::MidFeed); |
| 645 | } | ||
| 646 | bool flushNow; | ||
| 647 | { | ||
| 648 |
1/2✓ Branch 0 taken 108558 times.
✗ Branch 1 not taken.
|
108558 | std::unique_lock<std::mutex> slock(s->m_lock); |
| 649 | 108558 | flushNow = s->m_flushNow; | |
| 650 | 108558 | s->m_flushNow = false; | |
| 651 | 108558 | } | |
| 652 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 108558 times.
|
108558 | if (flushNow) { |
| 653 | ✗ | LogInfo(VB_MEDIAOUT, "PipeWire output stream %d: a track carrying on the timeline starts too far " | |
| 654 | "into it; flushing\n", s->m_slot); | ||
| 655 | ✗ | s->Flush(FlushFor::MidFeed); | |
| 656 | } | ||
| 657 |
1/2✓ Branch 0 taken 108558 times.
✗ Branch 1 not taken.
|
108558 | anyBridging |= s->ServiceBridge(); |
| 658 |
1/2✓ Branch 0 taken 108558 times.
✗ Branch 1 not taken.
|
108558 | s->Service(now); |
| 659 |
2/4✓ Branch 0 taken 108558 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 108558 times.
✗ Branch 3 not taken.
|
108558 | measureMs = std::min(measureMs, s->MeasureCardQueue(NowMs())); |
| 660 |
1/2✓ Branch 0 taken 108558 times.
✗ Branch 1 not taken.
|
108558 | std::unique_lock<std::mutex> slock(s->m_lock); |
| 661 | 108558 | anyOpening |= (s->m_state == State::Opening); | |
| 662 | 108558 | } | |
| 663 | } | ||
| 664 |
1/2✓ Branch 0 taken 93500 times.
✗ Branch 1 not taken.
|
93500 | std::unique_lock<std::mutex> wlock(s_workerLock); |
| 665 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 93498 times.
|
93500 | if (!s_workerRunning) { |
| 666 | 2 | break; | |
| 667 | } | ||
| 668 | // Opening streams are polled every 10 ms; otherwise the only thing to | ||
| 669 | // watch for is a linger running out. | ||
| 670 | // A bridge between tracks is topped up every few ms. A card that has | ||
| 671 | // just started is measured often while its queue settles. | ||
| 672 |
4/4✓ Branch 0 taken 70745 times.
✓ Branch 1 taken 22753 times.
✓ Branch 2 taken 4023 times.
✓ Branch 3 taken 18730 times.
|
93498 | int waitMs = anyBridging ? 4 : anyOpening ? 10 : std::max(measureMs, 1); |
| 673 |
1/2✓ Branch 0 taken 93498 times.
✗ Branch 1 not taken.
|
93498 | s_workerWake.wait_for(wlock, std::chrono::milliseconds(waitMs), |
| 674 |
4/4✓ Branch 0 taken 180498 times.
✓ Branch 1 taken 6160 times.
✓ Branch 2 taken 219 times.
✓ Branch 3 taken 180279 times.
|
186658 | [] { return s_workerKick || !s_workerRunning; }); |
| 675 | 93498 | s_workerKick = false; | |
| 676 |
2/2✓ Branch 0 taken 220 times.
✓ Branch 1 taken 93278 times.
|
93498 | if (!s_workerRunning) { |
| 677 | 220 | break; | |
| 678 | } | ||
| 679 |
2/2✓ Branch 0 taken 93278 times.
✓ Branch 1 taken 222 times.
|
186778 | } |
| 680 | 222 | } | |
| 681 | |||
| 682 | 108558 | void PipeWireOutputStream::Service(uint64_t now) { | |
| 683 |
1/2✓ Branch 0 taken 108558 times.
✗ Branch 1 not taken.
|
108558 | std::unique_lock<std::mutex> lock(m_lock); |
| 684 | |||
| 685 |
3/4✓ Branch 0 taken 20865 times.
✓ Branch 1 taken 87693 times.
✓ Branch 2 taken 20865 times.
✗ Branch 3 not taken.
|
108558 | bool keepOpen = s_keepOpen && m_slot == 1; |
| 686 |
3/8✓ Branch 0 taken 20865 times.
✓ Branch 1 taken 87693 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 20865 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
108558 | if (keepOpen && !m_wantOpen && (m_state == State::Closed || m_state == State::Failed) && |
| 687 | ✗ | now - m_failedMs >= (uint64_t)kKeepOpenRetryMs) { | |
| 688 | ✗ | if (m_wantTarget.empty()) { | |
| 689 | ✗ | m_wantTarget = GStreamerOutput::PipeWireSinkNameForSlot(m_slot); | |
| 690 | } | ||
| 691 | ✗ | m_wantOpen = true; | |
| 692 | } | ||
| 693 |
6/6✓ Branch 0 taken 87693 times.
✓ Branch 1 taken 20865 times.
✓ Branch 2 taken 66579 times.
✓ Branch 3 taken 21114 times.
✓ Branch 4 taken 65405 times.
✓ Branch 5 taken 1174 times.
|
108558 | bool wanted = keepOpen || m_users > 0 || now < m_holdUntilMs; |
| 694 |
2/2✓ Branch 0 taken 1174 times.
✓ Branch 1 taken 107384 times.
|
108558 | if (!wanted) { |
| 695 | 1174 | m_wantOpen = false; | |
| 696 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 1148 times.
|
1174 | if (m_state != State::Closed) { |
| 697 |
1/2✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
|
26 | CloseLocked("idle"); |
| 698 | } | ||
| 699 | 1174 | return; | |
| 700 | } | ||
| 701 | |||
| 702 |
4/6✓ Branch 0 taken 106619 times.
✓ Branch 1 taken 765 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 106619 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 107384 times.
|
107384 | if (m_pwStream && m_pwError.exchange(false)) { |
| 703 | // The stream is dead (PipeWire restarted, the sink went). Drop it; | ||
| 704 | // the next track or prewarm opens a fresh one. | ||
| 705 | ✗ | m_wantOpen = false; | |
| 706 | ✗ | CloseLocked("stream error"); | |
| 707 | ✗ | m_state = State::Failed; | |
| 708 | ✗ | m_failedMs = now; | |
| 709 | ✗ | m_stateChanged.notify_all(); | |
| 710 | } | ||
| 711 | |||
| 712 |
8/8✓ Branch 0 taken 107079 times.
✓ Branch 1 taken 305 times.
✓ Branch 2 taken 106619 times.
✓ Branch 3 taken 460 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 106618 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 107383 times.
|
107384 | if (m_state != State::Closed && m_state != State::Failed && m_wantTarget != m_target) { |
| 713 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogInfo(VB_MEDIAOUT, "PipeWire output stream %d: target changed '%s' -> '%s', reopening\n", |
| 714 | m_slot, m_target.c_str(), m_wantTarget.c_str()); | ||
| 715 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | CloseLocked("target changed"); |
| 716 | 1 | m_wantOpen = true; | |
| 717 | } | ||
| 718 | |||
| 719 |
6/6✓ Branch 0 taken 107078 times.
✓ Branch 1 taken 306 times.
✓ Branch 2 taken 460 times.
✓ Branch 3 taken 106618 times.
✓ Branch 4 taken 306 times.
✓ Branch 5 taken 460 times.
|
107384 | if ((m_state == State::Closed || m_state == State::Failed) && m_wantOpen) { |
| 720 |
3/4✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 87 times.
✓ Branch 3 taken 219 times.
|
306 | if (!OpenLocked(lock)) { |
| 721 | 87 | m_wantOpen = false; | |
| 722 | 87 | m_state = State::Failed; | |
| 723 |
1/2✓ Branch 0 taken 87 times.
✗ Branch 1 not taken.
|
87 | m_failedMs = NowMs(); |
| 724 | 87 | m_stateChanged.notify_all(); | |
| 725 | } | ||
| 726 | 306 | return; | |
| 727 | } | ||
| 728 | |||
| 729 |
2/2✓ Branch 0 taken 102747 times.
✓ Branch 1 taken 4331 times.
|
107078 | if (m_state != State::Opening) { |
| 730 | 102747 | return; | |
| 731 | } | ||
| 732 | |||
| 733 | // Opening. Two things have to be true, and neither implies the other. | ||
| 734 | // Our stream must be linked into the graph -- until it is, what it plays | ||
| 735 | // goes nowhere. And the card must be RUNNING: PipeWire runs | ||
| 736 | // a newly linked chain on a placeholder driver for ~100 ms while the ALSA | ||
| 737 | // node is still opening, and audio fed then is consumed into nothing. | ||
| 738 | // | ||
| 739 | // If the card was idle when we opened, it starting is the proof of both: | ||
| 740 | // nothing else woke it. If something else (AES67, an input mix) already | ||
| 741 | // had it running, that proves nothing about us, so ask pw-link -- as for a | ||
| 742 | // sink that reaches no card at all (a group of network senders only), | ||
| 743 | // where there is nothing to watch but the link. | ||
| 744 | // | ||
| 745 | // (The stream's GstPipeWireClock would be the natural thing to watch, but | ||
| 746 | // it does not advance at all for a follower of an ALSA-driven graph -- | ||
| 747 | // pw_stream_get_time_n() reports a zero rate and the clock returns its last | ||
| 748 | // value -- so nothing here runs on it.) | ||
| 749 | 4331 | bool ready = false; | |
| 750 | // | ||
| 751 | // No settling time after the card starts is needed: a cold-start sweep on | ||
| 752 | // an AM335x with a USB card, checking what actually crossed the bus to the | ||
| 753 | // card, kept the head of the track intact with none, and 25-400 ms bought | ||
| 754 | // nothing. | ||
| 755 |
6/6✓ Branch 0 taken 387 times.
✓ Branch 1 taken 3944 times.
✓ Branch 2 taken 195 times.
✓ Branch 3 taken 192 times.
✓ Branch 4 taken 195 times.
✓ Branch 5 taken 4136 times.
|
4331 | if (!m_cardStatusPath.empty() && !m_cardWasRunning) { |
| 756 |
1/2✓ Branch 0 taken 195 times.
✗ Branch 1 not taken.
|
195 | ready = CardRunning(); |
| 757 | } else { | ||
| 758 |
7/8✓ Branch 0 taken 1168 times.
✓ Branch 1 taken 2968 times.
✓ Branch 2 taken 1168 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 360 times.
✓ Branch 5 taken 808 times.
✓ Branch 6 taken 360 times.
✓ Branch 7 taken 3776 times.
|
4136 | if (!m_linked && NowMs() - m_lastLinkCheckMs >= (uint64_t)kLinkCheckIntervalMs) { |
| 759 | // pw-link talks to the daemon; don't hold the lock the feed and | ||
| 760 | // Acquire() need while it does. | ||
| 761 | 360 | struct pw_stream* stream = m_pwStream; | |
| 762 |
1/2✓ Branch 0 taken 360 times.
✗ Branch 1 not taken.
|
360 | lock.unlock(); |
| 763 |
1/2✓ Branch 0 taken 360 times.
✗ Branch 1 not taken.
|
360 | bool linked = StreamLinked(); |
| 764 |
1/2✓ Branch 0 taken 360 times.
✗ Branch 1 not taken.
|
360 | lock.lock(); |
| 765 |
2/4✓ Branch 0 taken 360 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 360 times.
|
360 | if (m_pwStream != stream || m_state != State::Opening) { |
| 766 | ✗ | return; | |
| 767 | } | ||
| 768 |
1/2✓ Branch 0 taken 360 times.
✗ Branch 1 not taken.
|
360 | m_lastLinkCheckMs = NowMs(); |
| 769 |
2/2✓ Branch 0 taken 194 times.
✓ Branch 1 taken 166 times.
|
360 | if (linked) { |
| 770 | 194 | m_linked = true; | |
| 771 |
1/2✓ Branch 0 taken 194 times.
✗ Branch 1 not taken.
|
194 | m_linkedMs = NowMs(); |
| 772 | } | ||
| 773 | } | ||
| 774 |
7/8✓ Branch 0 taken 3162 times.
✓ Branch 1 taken 974 times.
✓ Branch 2 taken 3162 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 187 times.
✓ Branch 5 taken 2975 times.
✓ Branch 6 taken 8 times.
✓ Branch 7 taken 179 times.
|
4331 | ready = m_linked && NowMs() - m_linkedMs >= (uint64_t)kNoCardSettleMs && |
| 775 |
2/4✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
195 | (m_cardStatusPath.empty() || CardRunning()); |
| 776 | } | ||
| 777 |
1/2✓ Branch 0 taken 4331 times.
✗ Branch 1 not taken.
|
4331 | now = NowMs(); |
| 778 |
2/2✓ Branch 0 taken 208 times.
✓ Branch 1 taken 4123 times.
|
4331 | if (ready) { |
| 779 | 208 | m_state = State::Ready; | |
| 780 |
1/2✓ Branch 0 taken 208 times.
✗ Branch 1 not taken.
|
208 | LogDebug(VB_MEDIAOUT, "PipeWire output stream %d ready on '%s' after %d ms\n", |
| 781 | m_slot, m_target.c_str(), (int)(now - m_openStartedMs)); | ||
| 782 | 208 | m_stateChanged.notify_all(); | |
| 783 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4123 times.
|
4123 | } else if (now - m_openStartedMs > (uint64_t)kOpenTimeoutMs) { |
| 784 | ✗ | LogWarn(VB_MEDIAOUT, "PipeWire output stream %d: not running after %d ms (linked %s, card %s), giving up\n", | |
| 785 | m_slot, kOpenTimeoutMs, m_linked ? "yes" : "no", | ||
| 786 | m_cardStatusPath.empty() ? "n/a" : (CardRunning() ? "running" : "not running")); | ||
| 787 | ✗ | m_wantOpen = false; | |
| 788 | ✗ | CloseLocked("open timed out"); | |
| 789 | ✗ | m_state = State::Failed; | |
| 790 | ✗ | m_failedMs = now; | |
| 791 | ✗ | m_stateChanged.notify_all(); | |
| 792 | } | ||
| 793 |
2/2✓ Branch 0 taken 4331 times.
✓ Branch 1 taken 104227 times.
|
108558 | } |
| 794 | |||
| 795 | 306 | bool PipeWireOutputStream::OpenLocked(std::unique_lock<std::mutex>& lock) { | |
| 796 | #ifdef FPP_NATIVE_PIPEWIRE | ||
| 797 |
1/2✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
|
306 | std::string target = m_wantTarget; |
| 798 |
1/2✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
|
306 | int rate = GStreamerOutput::PipeWireGraphRate(); |
| 799 | |||
| 800 | // Nothing below touches the stream's state until the stream exists, and | ||
| 801 | // connecting to the daemon can be slow, so none of it runs under the slot | ||
| 802 | // lock, which the feed callback and Acquire() on the playlist thread need. | ||
| 803 |
1/2✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
|
306 | lock.unlock(); |
| 804 |
1/2✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
|
306 | GStreamerOutput::EnsureGStreamerInit(); |
| 805 | static std::once_flag pwInit; | ||
| 806 |
1/2✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
|
523 | std::call_once(pwInit, [] { pw_init(nullptr, nullptr); }); |
| 807 |
1/2✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
|
306 | std::string cardStatus = GStreamerOutput::AlsaSinkStatusPath(target); |
| 808 |
4/4✓ Branch 0 taken 29 times.
✓ Branch 1 taken 277 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 21 times.
|
335 | bool cardWasRunning = !cardStatus.empty() && |
| 809 |
3/6✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✓ Branch 3 taken 277 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
335 | GetFileContents(cardStatus).find("state: RUNNING") != std::string::npos; |
| 810 |
1/2✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
|
306 | std::string loopName = "fpp-pwstream-" + std::to_string(m_slot); |
| 811 |
1/2✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
|
306 | struct pw_thread_loop* loop = pw_thread_loop_new(loopName.c_str(), nullptr); |
| 812 |
1/2✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
|
306 | lock.lock(); |
| 813 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 306 times.
|
306 | if (!loop) { |
| 814 | ✗ | LogErr(VB_MEDIAOUT, "PipeWire output stream %d: could not create its thread loop\n", m_slot); | |
| 815 | ✗ | return false; | |
| 816 | } | ||
| 817 | |||
| 818 | // A queue of four seconds: a feed runs a lead ahead of the graph and the | ||
| 819 | // bridge a few ms, so it only fills if the graph stops taking from it. | ||
| 820 | 306 | m_rate = rate; | |
| 821 | 306 | m_ringFrames = (uint64_t)rate * 4; | |
| 822 |
1/2✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
|
306 | m_ring.assign(m_ringFrames * kChannels, 0.0f); |
| 823 | 306 | m_writePos.store(0, std::memory_order_relaxed); | |
| 824 | 306 | m_readPos.store(0, std::memory_order_relaxed); | |
| 825 | 306 | m_dropTo.store(0, std::memory_order_relaxed); | |
| 826 | 306 | m_cycleCount.store(0, std::memory_order_relaxed); | |
| 827 | 306 | m_outFrames = 0; | |
| 828 |
1/2✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
|
306 | ResetCardQueue(); |
| 829 | 306 | m_feedSpans.clear(); | |
| 830 | 306 | m_warnedFull = false; | |
| 831 | 306 | m_pwError = false; | |
| 832 | |||
| 833 | // The identity every per-track pipewiresink used to present, so anything | ||
| 834 | // that matches or routes fppd_stream_N sees no difference. The target is | ||
| 835 | // a property: it comes from the PipeWireSinkName setting. | ||
| 836 |
1/2✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
|
306 | std::string nodeName = StreamSlotManager::GetNodeName(m_slot); |
| 837 |
1/2✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
|
306 | std::string nodeDesc = StreamSlotManager::GetNodeDescription(m_slot); |
| 838 |
1/2✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
|
306 | struct pw_properties* props = pw_properties_new(PW_KEY_MEDIA_TYPE, "Audio", |
| 839 | PW_KEY_MEDIA_CATEGORY, "Playback", | ||
| 840 | PW_KEY_NODE_NAME, nodeName.c_str(), | ||
| 841 | PW_KEY_NODE_DESCRIPTION, nodeDesc.c_str(), | ||
| 842 | nullptr); | ||
| 843 |
2/2✓ Branch 0 taken 222 times.
✓ Branch 1 taken 84 times.
|
306 | if (!target.empty()) { |
| 844 |
1/2✓ Branch 0 taken 222 times.
✗ Branch 1 not taken.
|
222 | pw_properties_set(props, PW_KEY_TARGET_OBJECT, target.c_str()); |
| 845 | } | ||
| 846 | // (Lambdas here, inside a member, may call its private statics.) | ||
| 847 | static const struct pw_stream_events events = [] { | ||
| 848 | struct pw_stream_events e = {}; | ||
| 849 | e.version = PW_VERSION_STREAM_EVENTS; | ||
| 850 | 1182 | e.state_changed = [](void* data, enum pw_stream_state, enum pw_stream_state state, const char* error) { | |
| 851 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1182 times.
|
1182 | if (state == PW_STREAM_STATE_ERROR) { |
| 852 | ✗ | OnStreamError(data, error); | |
| 853 | } | ||
| 854 | 1182 | }; | |
| 855 | 166674 | e.process = [](void* data) { OnProcess(data); }; | |
| 856 | return e; | ||
| 857 | }(); | ||
| 858 |
1/2✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
|
306 | pw_thread_loop_lock(loop); |
| 859 | // Nothing calls back before the stream connects and the loop runs, so | ||
| 860 | // m_pwStream is set by the time OnProcess() first reads it. | ||
| 861 |
2/4✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 306 times.
✗ Branch 3 not taken.
|
306 | struct pw_stream* stream = pw_stream_new_simple(pw_thread_loop_get_loop(loop), nodeName.c_str(), props, |
| 862 | &events, this); | ||
| 863 | 306 | bool ok = stream != nullptr; | |
| 864 |
1/2✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
|
306 | if (ok) { |
| 865 | 306 | m_pwStream = stream; | |
| 866 | 306 | m_pwLoop = loop; | |
| 867 | uint8_t podBuffer[1024]; | ||
| 868 | 306 | struct spa_pod_builder b = SPA_POD_BUILDER_INIT(podBuffer, sizeof(podBuffer)); | |
| 869 | 306 | struct spa_audio_info_raw info = {}; | |
| 870 | 306 | info.format = SPA_AUDIO_FORMAT_F32; | |
| 871 | 306 | info.channels = kChannels; | |
| 872 | 306 | info.rate = (uint32_t)rate; | |
| 873 | 306 | info.position[0] = SPA_AUDIO_CHANNEL_FL; | |
| 874 | 306 | info.position[1] = SPA_AUDIO_CHANNEL_FR; | |
| 875 |
1/2✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
|
306 | const struct spa_pod* params[1] = { spa_format_audio_raw_build(&b, SPA_PARAM_EnumFormat, &info) }; |
| 876 |
1/2✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
|
306 | ok = pw_stream_connect(stream, PW_DIRECTION_OUTPUT, PW_ID_ANY, |
| 877 | (enum pw_stream_flags)(PW_STREAM_FLAG_AUTOCONNECT | PW_STREAM_FLAG_MAP_BUFFERS | | ||
| 878 | PW_STREAM_FLAG_RT_PROCESS), | ||
| 879 | params, 1) == 0; | ||
| 880 | } | ||
| 881 | // (pw_stream_new_simple() owns the properties, created or not.) | ||
| 882 |
1/2✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
|
306 | pw_thread_loop_unlock(loop); |
| 883 |
2/2✓ Branch 0 taken 219 times.
✓ Branch 1 taken 87 times.
|
306 | if (ok) { |
| 884 |
1/2✓ Branch 0 taken 219 times.
✗ Branch 1 not taken.
|
219 | ok = pw_thread_loop_start(loop) == 0; |
| 885 | } | ||
| 886 |
2/2✓ Branch 0 taken 87 times.
✓ Branch 1 taken 219 times.
|
306 | if (!ok) { |
| 887 |
1/2✓ Branch 0 taken 87 times.
✗ Branch 1 not taken.
|
87 | LogErr(VB_MEDIAOUT, "PipeWire output stream %d: could not connect to '%s'\n", m_slot, target.c_str()); |
| 888 | // Once the stream exists, m_pwLoop owns the loop and CloseLocked() | ||
| 889 | // destroys it (and clears m_pwLoop, so it can't be asked afterwards). | ||
| 890 | // Only a loop that never got that far is still ours to destroy. | ||
| 891 | 87 | bool loopHandedOver = m_pwLoop == loop; | |
| 892 |
1/2✓ Branch 0 taken 87 times.
✗ Branch 1 not taken.
|
87 | CloseLocked("could not connect"); |
| 893 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
|
87 | if (!loopHandedOver) { |
| 894 | ✗ | pw_thread_loop_destroy(loop); | |
| 895 | } | ||
| 896 | 87 | return false; | |
| 897 | } | ||
| 898 | |||
| 899 |
3/6✓ Branch 0 taken 219 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 219 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 219 times.
✗ Branch 5 not taken.
|
219 | GstCaps* fixedCaps = gst_caps_from_string(("audio/x-raw" + CapsFields()).c_str()); |
| 900 |
1/2✓ Branch 0 taken 219 times.
✗ Branch 1 not taken.
|
219 | gst_caps_replace(&m_caps, fixedCaps); |
| 901 |
1/2✓ Branch 0 taken 219 times.
✗ Branch 1 not taken.
|
219 | gst_caps_unref(fixedCaps); |
| 902 | 219 | m_warnedCaps = false; | |
| 903 |
1/2✓ Branch 0 taken 219 times.
✗ Branch 1 not taken.
|
219 | EndTimelineLocked(); |
| 904 | 219 | m_continuedFirst = false; | |
| 905 | 219 | m_flushNow = false; | |
| 906 | 219 | m_xrunPending = false; | |
| 907 | 219 | m_flushed = true; | |
| 908 |
1/2✓ Branch 0 taken 219 times.
✗ Branch 1 not taken.
|
219 | m_target = target; |
| 909 |
1/2✓ Branch 0 taken 219 times.
✗ Branch 1 not taken.
|
219 | m_cardStatusPath = cardStatus; |
| 910 | 219 | m_cardWasRunning = cardWasRunning; | |
| 911 |
1/2✓ Branch 0 taken 219 times.
✗ Branch 1 not taken.
|
219 | m_openStartedMs = NowMs(); |
| 912 | 219 | m_linked = false; | |
| 913 | 219 | m_linkedMs = 0; | |
| 914 | 219 | m_lastLinkCheckMs = 0; | |
| 915 | 219 | m_state = State::Opening; | |
| 916 |
5/6✓ Branch 0 taken 190 times.
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 218 times.
✓ Branch 4 taken 219 times.
✗ Branch 5 not taken.
|
219 | LogDebug(VB_MEDIAOUT, "PipeWire output stream %d opening on '%s' (card status %s)\n", m_slot, |
| 917 | target.empty() ? "(default)" : target.c_str(), cardStatus.empty() ? "n/a" : cardStatus.c_str()); | ||
| 918 | 219 | return true; | |
| 919 | #else | ||
| 920 | return false; | ||
| 921 | #endif | ||
| 922 | 306 | } | |
| 923 | |||
| 924 | ✗ | void PipeWireOutputStream::OnStreamError(void* data, const char* error) { | |
| 925 | ✗ | PipeWireOutputStream* self = static_cast<PipeWireOutputStream*>(data); | |
| 926 | ✗ | LogWarn(VB_MEDIAOUT, "PipeWire output stream %d: %s\n", self->m_slot, error ? error : "error"); | |
| 927 | ✗ | self->m_pwError = true; | |
| 928 | ✗ | WakeWorker(); | |
| 929 | ✗ | } | |
| 930 | |||
| 931 | // PipeWire's data thread, once a graph cycle: the next `requested` frames | ||
| 932 | // from the queue (silence for whatever isn't there), and a record of where | ||
| 933 | // they came from and when PipeWire says they will be heard. | ||
| 934 | 166674 | void PipeWireOutputStream::OnProcess(void* data) { | |
| 935 | #ifdef FPP_NATIVE_PIPEWIRE | ||
| 936 | 166674 | PipeWireOutputStream* self = static_cast<PipeWireOutputStream*>(data); | |
| 937 |
1/2✓ Branch 0 taken 166674 times.
✗ Branch 1 not taken.
|
166674 | struct pw_buffer* b = pw_stream_dequeue_buffer(self->m_pwStream); |
| 938 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 166674 times.
|
166674 | if (!b) { |
| 939 | ✗ | return; | |
| 940 | } | ||
| 941 | 166674 | struct spa_data& d = b->buffer->datas[0]; | |
| 942 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 166674 times.
|
166674 | if (!d.data) { |
| 943 | ✗ | pw_stream_queue_buffer(self->m_pwStream, b); | |
| 944 | ✗ | return; | |
| 945 | } | ||
| 946 | 166674 | uint32_t n = d.maxsize / kBytesPerFrame; | |
| 947 |
2/4✓ Branch 0 taken 166674 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 166674 times.
✗ Branch 3 not taken.
|
166674 | if (b->requested && b->requested < n) { |
| 948 | 166674 | n = (uint32_t)b->requested; | |
| 949 | } | ||
| 950 | // The time this cycle's samples reach the device: the graph's delay to | ||
| 951 | // it, and anything the stream's own converter holds or we queued before. | ||
| 952 | 166674 | struct pw_time t = {}; | |
| 953 |
1/2✓ Branch 0 taken 166674 times.
✗ Branch 1 not taken.
|
166674 | pw_stream_get_time_n(self->m_pwStream, &t, sizeof(t)); |
| 954 | // t.delay is the latency the nodes downstream declare, the same in every | ||
| 955 | // cycle; how far the card's queue really is from that is measured apart | ||
| 956 | // (MeasureCardQueue()) and applied when the records are read. | ||
| 957 | 166674 | int64_t heardNs = 0; | |
| 958 | 166674 | int64_t deviceNs = 0; | |
| 959 |
2/4✓ Branch 0 taken 166674 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 166674 times.
✗ Branch 3 not taken.
|
166674 | if (t.rate.denom > 0 && t.now > 0) { |
| 960 | 166674 | deviceNs = t.now + t.delay * (int64_t)GST_SECOND * t.rate.num / t.rate.denom; | |
| 961 | 166674 | heardNs = deviceNs + ((int64_t)t.buffered + (int64_t)(t.queued / kBytesPerFrame)) * (int64_t)GST_SECOND / self->m_rate; | |
| 962 | } | ||
| 963 | |||
| 964 | 166674 | uint64_t read = self->m_readPos.load(std::memory_order_relaxed); | |
| 965 | 166674 | uint64_t drop = self->m_dropTo.load(std::memory_order_acquire); | |
| 966 |
2/2✓ Branch 0 taken 52 times.
✓ Branch 1 taken 166622 times.
|
166674 | if (read < drop) { |
| 967 | 52 | read = drop; | |
| 968 | } | ||
| 969 | 166674 | uint64_t write = self->m_writePos.load(std::memory_order_acquire); | |
| 970 |
2/2✓ Branch 0 taken 148111 times.
✓ Branch 1 taken 18563 times.
|
166674 | uint64_t avail = write > read ? write - read : 0; |
| 971 | 166674 | uint32_t take = (uint32_t)std::min<uint64_t>(avail, n); | |
| 972 | 166674 | float* out = static_cast<float*>(d.data); | |
| 973 | 166674 | uint64_t at = read % self->m_ringFrames; | |
| 974 | 166674 | uint64_t first = std::min<uint64_t>(take, self->m_ringFrames - at); | |
| 975 | 166674 | const float* ring = self->m_ring.data(); | |
| 976 | 166674 | std::memcpy(out, ring + at * kChannels, first * kBytesPerFrame); | |
| 977 |
2/2✓ Branch 0 taken 566 times.
✓ Branch 1 taken 166108 times.
|
166674 | if (take > first) { |
| 978 | 566 | std::memcpy(out + first * kChannels, ring, (take - first) * kBytesPerFrame); | |
| 979 | } | ||
| 980 |
2/2✓ Branch 0 taken 18821 times.
✓ Branch 1 taken 147853 times.
|
166674 | if (take < n) { |
| 981 | 18821 | std::memset(out + (size_t)take * kChannels, 0, (size_t)(n - take) * kBytesPerFrame); | |
| 982 | } | ||
| 983 | 166674 | d.chunk->offset = 0; | |
| 984 | 166674 | d.chunk->stride = kBytesPerFrame; | |
| 985 | 166674 | d.chunk->size = n * kBytesPerFrame; | |
| 986 | |||
| 987 | 166674 | uint64_t count = self->m_cycleCount.load(std::memory_order_relaxed); | |
| 988 | 166674 | CycleRecord& r = self->m_cycles[count % kCycleRecords]; | |
| 989 | 166674 | r.seq.fetch_add(1, std::memory_order_acq_rel); // odd: being written | |
| 990 | 166674 | r.readPos.store(read, std::memory_order_relaxed); | |
| 991 | 166674 | r.frames.store(take, std::memory_order_relaxed); | |
| 992 | 166674 | r.heardNs.store(heardNs, std::memory_order_relaxed); | |
| 993 | 166674 | self->m_outFrames += n; | |
| 994 | 166674 | r.outFrames.store(n, std::memory_order_relaxed); | |
| 995 | 166674 | r.outEnd.store(self->m_outFrames, std::memory_order_relaxed); | |
| 996 | 166674 | r.deviceNs.store(deviceNs, std::memory_order_relaxed); | |
| 997 | 166674 | r.seq.fetch_add(1, std::memory_order_release); // even: done | |
| 998 | 166674 | self->m_cycleCount.store(count + 1, std::memory_order_release); | |
| 999 | 166674 | self->m_readPos.store(read + take, std::memory_order_release); | |
| 1000 |
1/2✓ Branch 0 taken 166674 times.
✗ Branch 1 not taken.
|
166674 | pw_stream_queue_buffer(self->m_pwStream, b); |
| 1001 | #else | ||
| 1002 | (void)data; | ||
| 1003 | #endif | ||
| 1004 | } | ||
| 1005 | |||
| 1006 | 125628 | uint64_t PipeWireOutputStream::WriteQueueLocked(const float* data, size_t frames) { | |
| 1007 |
3/6✓ Branch 0 taken 125628 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 125628 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 125628 times.
|
125628 | if (!m_pwStream || m_ringFrames == 0 || frames == 0) { |
| 1008 | ✗ | return UINT64_MAX; | |
| 1009 | } | ||
| 1010 | 125628 | uint64_t write = m_writePos.load(std::memory_order_relaxed); | |
| 1011 | 376884 | uint64_t read = std::max(m_readPos.load(std::memory_order_acquire), m_dropTo.load(std::memory_order_relaxed)); | |
| 1012 |
2/2✓ Branch 0 taken 124819 times.
✓ Branch 1 taken 809 times.
|
125628 | uint64_t used = write > read ? write - read : 0; |
| 1013 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 125628 times.
|
125628 | if (used + frames > m_ringFrames) { |
| 1014 | // The graph stopped taking from the stream for seconds. Dropping | ||
| 1015 | // what comes is the least bad thing; the xrun handling flushes. | ||
| 1016 | ✗ | if (!m_warnedFull) { | |
| 1017 | ✗ | m_warnedFull = true; | |
| 1018 | ✗ | LogWarn(VB_MEDIAOUT, "PipeWire output stream %d: queue full, dropping audio\n", m_slot); | |
| 1019 | } | ||
| 1020 | ✗ | return UINT64_MAX; | |
| 1021 | } | ||
| 1022 | 125628 | m_warnedFull = false; | |
| 1023 | 125628 | uint64_t at = write % m_ringFrames; | |
| 1024 | 125628 | uint64_t first = std::min<uint64_t>(frames, m_ringFrames - at); | |
| 1025 | 125628 | float* ring = m_ring.data(); | |
| 1026 |
2/2✓ Branch 0 taken 63865 times.
✓ Branch 1 taken 61763 times.
|
125628 | if (data) { |
| 1027 | 63865 | std::memcpy(ring + at * kChannels, data, first * kBytesPerFrame); | |
| 1028 | 63865 | std::memcpy(ring, data + first * kChannels, (frames - first) * kBytesPerFrame); | |
| 1029 | } else { | ||
| 1030 | 61763 | std::memset(ring + at * kChannels, 0, first * kBytesPerFrame); | |
| 1031 | 61763 | std::memset(ring, 0, (frames - first) * kBytesPerFrame); | |
| 1032 | } | ||
| 1033 | 125628 | m_writePos.store(write + frames, std::memory_order_release); | |
| 1034 | 125628 | return write; | |
| 1035 | } | ||
| 1036 | |||
| 1037 | 172910 | bool PipeWireOutputStream::FrameHeardAt(int64_t nowNs, uint64_t* frame) { | |
| 1038 | 172910 | uint64_t count = m_cycleCount.load(std::memory_order_acquire); | |
| 1039 |
3/4✓ Branch 0 taken 172868 times.
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 172868 times.
|
172910 | if (count == 0 || m_rate <= 0) { |
| 1040 | 42 | return false; | |
| 1041 | } | ||
| 1042 | // The newest cycle whose first sample is heard by now; samples go out at | ||
| 1043 | // the rate from there, up to the last one that cycle was given. What | ||
| 1044 | // PipeWire says, put right by what the card's queue really holds. | ||
| 1045 | 172868 | int64_t corrNs = m_cardCorrNs.load(std::memory_order_relaxed); | |
| 1046 | 172868 | uint64_t oldest = count > (uint64_t)kCycleRecords - 4 ? count - (kCycleRecords - 4) : 0; | |
| 1047 |
1/2✓ Branch 0 taken 335773 times.
✗ Branch 1 not taken.
|
335773 | for (uint64_t c = count; c-- > oldest;) { |
| 1048 | 335773 | CycleRecord& r = m_cycles[c % kCycleRecords]; | |
| 1049 | 335773 | uint32_t seq = r.seq.load(std::memory_order_acquire); | |
| 1050 | 335773 | uint64_t readPos = r.readPos.load(std::memory_order_relaxed); | |
| 1051 | 335773 | uint32_t frames = r.frames.load(std::memory_order_relaxed); | |
| 1052 | 671546 | int64_t heardNs = r.heardNs.load(std::memory_order_relaxed); | |
| 1053 | std::atomic_thread_fence(std::memory_order_acquire); | ||
| 1054 |
4/8✓ Branch 0 taken 335773 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 335773 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 335773 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 335773 times.
|
671546 | if ((seq & 1) || r.seq.load(std::memory_order_relaxed) != seq || heardNs == 0) { |
| 1055 | ✗ | continue; | |
| 1056 | } | ||
| 1057 | 335773 | heardNs += corrNs; | |
| 1058 |
2/2✓ Branch 0 taken 172861 times.
✓ Branch 1 taken 162912 times.
|
335773 | if (heardNs <= nowNs) { |
| 1059 | 172861 | uint64_t since = (uint64_t)((nowNs - heardNs) * (int64_t)m_rate / (int64_t)GST_SECOND); | |
| 1060 | 172861 | *frame = readPos + std::min<uint64_t>(since, frames); | |
| 1061 | 172861 | return true; | |
| 1062 | } | ||
| 1063 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 162905 times.
|
162912 | if (c == oldest) { |
| 1064 | // Even the oldest record isn't heard yet: nothing after it is. | ||
| 1065 | 7 | *frame = readPos; | |
| 1066 | 7 | return true; | |
| 1067 | } | ||
| 1068 | } | ||
| 1069 | ✗ | return false; | |
| 1070 | } | ||
| 1071 | |||
| 1072 | 172976 | int PipeWireOutputStream::HeardRunningTime(GstElement* feed, GstClockTime* runningTime) { | |
| 1073 | struct timespec ts; | ||
| 1074 | 172976 | clock_gettime(CLOCK_MONOTONIC, &ts); | |
| 1075 | 172976 | int64_t nowNs = (int64_t)ts.tv_sec * GST_SECOND + ts.tv_nsec; | |
| 1076 |
1/2✓ Branch 0 taken 172976 times.
✗ Branch 1 not taken.
|
172976 | std::unique_lock<std::mutex> lock(m_lock); |
| 1077 | uint64_t heard; | ||
| 1078 |
8/10✓ Branch 0 taken 172976 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 172676 times.
✓ Branch 3 taken 300 times.
✓ Branch 4 taken 172676 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 42 times.
✓ Branch 7 taken 172634 times.
✓ Branch 8 taken 342 times.
✓ Branch 9 taken 172634 times.
|
172976 | if (!feed || !m_pwStream || !FrameHeardAt(nowNs, &heard)) { |
| 1079 | 342 | return -1; | |
| 1080 | } | ||
| 1081 | // Spans long heard are no longer needed (a second is kept). | ||
| 1082 |
4/4✓ Branch 0 taken 183449 times.
✓ Branch 1 taken 2589 times.
✓ Branch 2 taken 13404 times.
✓ Branch 3 taken 172634 times.
|
369487 | while (!m_feedSpans.empty() && |
| 1083 |
2/2✓ Branch 0 taken 13404 times.
✓ Branch 1 taken 170045 times.
|
183449 | m_feedSpans.front().queuePos + m_feedSpans.front().frames + (uint64_t)m_rate < heard) { |
| 1084 | 13404 | m_feedSpans.pop_front(); | |
| 1085 | } | ||
| 1086 | 172634 | const FeedSpan* last = nullptr; | |
| 1087 | 172634 | bool inside = false; | |
| 1088 |
2/2✓ Branch 0 taken 4081723 times.
✓ Branch 1 taken 2590 times.
|
4084313 | for (const FeedSpan& span : m_feedSpans) { |
| 1089 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4081723 times.
|
4081723 | if (span.feed != feed) { |
| 1090 | ✗ | continue; | |
| 1091 | } | ||
| 1092 |
2/2✓ Branch 0 taken 430 times.
✓ Branch 1 taken 4081293 times.
|
4081723 | if (heard < span.queuePos) { |
| 1093 | 430 | inside = true; // between two of its spans: at the end of the first | |
| 1094 | 430 | break; | |
| 1095 | } | ||
| 1096 | 4081293 | last = &span; | |
| 1097 |
2/2✓ Branch 0 taken 169614 times.
✓ Branch 1 taken 3911679 times.
|
4081293 | if (heard < span.queuePos + span.frames) { |
| 1098 | 169614 | inside = true; | |
| 1099 | 169614 | break; | |
| 1100 | } | ||
| 1101 | } | ||
| 1102 |
2/2✓ Branch 0 taken 3019 times.
✓ Branch 1 taken 169615 times.
|
172634 | if (!last) { |
| 1103 | 3019 | return 0; | |
| 1104 | } | ||
| 1105 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 169614 times.
|
169615 | if (!inside) { |
| 1106 | // All of it heard and nothing more given: a seek flushed it and it | ||
| 1107 | // hasn't resumed, or it ran dry. The speaker isn't playing it, so | ||
| 1108 | // its last sample's running time stays put while the pipeline's | ||
| 1109 | // runs on (or, PAUSED, its clock does): the difference between them | ||
| 1110 | // is no latency. | ||
| 1111 | 1 | return -2; | |
| 1112 | } | ||
| 1113 | 169614 | uint64_t into = std::min<uint64_t>(heard - last->queuePos, last->frames); | |
| 1114 | 169614 | *runningTime = last->runningTime + (GstClockTime)(into * last->speed * GST_SECOND / m_rate); | |
| 1115 | 169614 | return 1; | |
| 1116 | 172976 | } | |
| 1117 | |||
| 1118 | 63865 | void PipeWireOutputStream::NoteTimelineLocked(GstClockTime start, size_t frames, GstClockTimeDiff lateNs) { | |
| 1119 |
2/4✓ Branch 0 taken 63865 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 63865 times.
|
63865 | if (!m_clock || m_rate <= 0) { |
| 1120 | ✗ | return; | |
| 1121 | } | ||
| 1122 |
2/2✓ Branch 0 taken 25 times.
✓ Branch 1 taken 63840 times.
|
63865 | if (m_continuedFirst) { |
| 1123 | 25 | GstClockTime nowT = gst_clock_get_time(m_clock); | |
| 1124 | 25 | LogDebug(VB_MEDIAOUT, "PipeWire output stream %d: first buffer of a carried-on track: starts %+.1f ms from the timeline's end, handed over %+.1f ms from it\n", | |
| 1125 | m_slot, ((double)(GstClockTimeDiff)(start - m_timelineEnd)) / 1e6, | ||
| 1126 | ((double)(GstClockTimeDiff)(nowT - m_timelineEnd)) / 1e6); | ||
| 1127 | } | ||
| 1128 | 63865 | bool ranDry = gst_clock_get_time(m_clock) + GraphAheadNs() > m_timelineEnd; | |
| 1129 |
5/6✓ Branch 0 taken 25 times.
✓ Branch 1 taken 63840 times.
✓ Branch 2 taken 25 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 22 times.
|
63865 | if (m_continuedFirst && m_timelineValid && start > m_timelineEnd + GST_SECOND / (GstClockTime)m_rate) { |
| 1130 | // Its sound doesn't start at the start of its timeline (an MP4's | ||
| 1131 | // audio often starts a frame or so after its video): the stream plays | ||
| 1132 | // what it is given back to back, so what lies between goes in as | ||
| 1133 | // silence, or the track would play that much ahead of its position. | ||
| 1134 | // The silence is pushed now, ahead of this buffer. (Whether or not | ||
| 1135 | // the graph may already be taking from the end of the queue: if it | ||
| 1136 | // ran dry, the check below sees this buffer late.) | ||
| 1137 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (start - m_timelineEnd <= (GstClockTime)kMaxLeadInGapMs * GST_MSECOND) { |
| 1138 | 3 | LogDebug(VB_MEDIAOUT, "PipeWire output stream %d: the carried-on track's sound starts %.1f ms in; " | |
| 1139 | "silence until then\n", m_slot, (double)(start - m_timelineEnd) / 1e6); | ||
| 1140 | 3 | ExtendTimelineLocked(start); | |
| 1141 | } else { | ||
| 1142 | // Far too much to make up in silence: take it as a fault. | ||
| 1143 | ✗ | m_flushNow = true; | |
| 1144 | ✗ | WakeWorker(); | |
| 1145 | } | ||
| 1146 | } | ||
| 1147 |
5/6✓ Branch 0 taken 63097 times.
✓ Branch 1 taken 768 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 63085 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 12 times.
|
63865 | if (m_timelineValid && lateNs > (GstClockTimeDiff)kFeedLateToleranceMs * GST_MSECOND && ranDry) { |
| 1148 | // Handed over late, and after the graph may have needed it: the queue | ||
| 1149 | // may have run dry, and then this and everything after it play that | ||
| 1150 | // much later. Nothing to do about it: the position follows what is | ||
| 1151 | // heard (HeardRunningTime()), so the lights stay with the sound, and | ||
| 1152 | // a next track carrying on still queues right behind it. (Before the | ||
| 1153 | // position was measured, a late feed was a lag nothing could see: the | ||
| 1154 | // track carrying on was flushed instead, and the next one drained.) | ||
| 1155 | ✗ | LogDebug(VB_MEDIAOUT, "PipeWire output stream %d: a feed buffer came %.1f ms late\n", m_slot, lateNs / 1e6); | |
| 1156 | } | ||
| 1157 | 63865 | m_continuedFirst = false; | |
| 1158 | 63865 | m_bridging = false; | |
| 1159 | 63865 | m_timelineValid = true; | |
| 1160 | 63865 | m_timelineEnd = start + gst_util_uint64_scale(frames, GST_SECOND, m_rate); | |
| 1161 | } | ||
| 1162 | |||
| 1163 | 7639 | void PipeWireOutputStream::EndTimelineLocked() { | |
| 1164 | 7639 | m_timelineValid = false; | |
| 1165 | 7639 | m_bridging = false; | |
| 1166 | 7639 | } | |
| 1167 | |||
| 1168 | 61788 | void PipeWireOutputStream::ExtendTimelineLocked(GstClockTime until) { | |
| 1169 |
5/8✓ Branch 0 taken 61788 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 61788 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 61788 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 25 times.
✓ Branch 7 taken 61763 times.
|
61788 | if (!m_pwStream || m_rate <= 0 || !m_timelineValid || until <= m_timelineEnd) { |
| 1170 | 25 | return; | |
| 1171 | } | ||
| 1172 | 61763 | size_t frames = gst_util_uint64_scale_ceil(until - m_timelineEnd, m_rate, GST_SECOND); | |
| 1173 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 61763 times.
|
61763 | if (WriteQueueLocked(nullptr, frames) == UINT64_MAX) { |
| 1174 | ✗ | return; | |
| 1175 | } | ||
| 1176 | 61763 | m_timelineEnd += gst_util_uint64_scale(frames, GST_SECOND, m_rate); | |
| 1177 | 61763 | m_flushed = false; | |
| 1178 | } | ||
| 1179 | |||
| 1180 | 108558 | bool PipeWireOutputStream::ServiceBridge() { | |
| 1181 |
1/2✓ Branch 0 taken 108558 times.
✗ Branch 1 not taken.
|
108558 | std::unique_lock<std::mutex> lock(m_lock); |
| 1182 |
2/2✓ Branch 0 taken 37228 times.
✓ Branch 1 taken 71330 times.
|
108558 | if (!m_bridging) { |
| 1183 | 37228 | return false; | |
| 1184 | } | ||
| 1185 |
8/12✓ Branch 0 taken 71330 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 71330 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 71330 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 71330 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 37 times.
✓ Branch 9 taken 71293 times.
✓ Branch 10 taken 37 times.
✓ Branch 11 taken 71293 times.
|
71330 | if (m_state != State::Ready || !m_timelineValid || !m_clock || NowMs() >= m_bridgeUntilMs) { |
| 1186 |
1/2✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
|
37 | EndTimelineLocked(); |
| 1187 | 37 | return false; | |
| 1188 | } | ||
| 1189 |
2/4✓ Branch 0 taken 71293 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 71293 times.
✗ Branch 3 not taken.
|
71293 | GstClockTime now = gst_clock_get_time(m_clock) + GraphAheadNs(); |
| 1190 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 71285 times.
|
71293 | if (now > m_timelineEnd) { |
| 1191 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | LogDebug(VB_MEDIAOUT, "PipeWire output stream %d: the bridge between tracks ran out\n", m_slot); |
| 1192 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | EndTimelineLocked(); |
| 1193 | 8 | return false; | |
| 1194 | } | ||
| 1195 |
2/2✓ Branch 0 taken 61760 times.
✓ Branch 1 taken 9525 times.
|
71285 | if (m_timelineEnd < now + (GstClockTime)kBridgeLowMs * GST_MSECOND) { |
| 1196 | 61760 | GstClockTime before = m_timelineEnd; | |
| 1197 |
1/2✓ Branch 0 taken 61760 times.
✗ Branch 1 not taken.
|
61760 | ExtendTimelineLocked(now + (GstClockTime)kBridgeHighMs * GST_MSECOND); |
| 1198 | 61760 | m_bridgedNs += m_timelineEnd - before; | |
| 1199 | } | ||
| 1200 | 71285 | return true; | |
| 1201 | 108558 | } | |
| 1202 | |||
| 1203 | 1805 | bool PipeWireOutputStream::ContinueTimeline(GstClockTime* baseTime, int64_t* earlyNs) { | |
| 1204 |
1/2✓ Branch 0 taken 1805 times.
✗ Branch 1 not taken.
|
1805 | std::unique_lock<std::mutex> lock(m_lock); |
| 1205 | 1805 | const char* why = nullptr; | |
| 1206 |
1/2✓ Branch 0 taken 1805 times.
✗ Branch 1 not taken.
|
1805 | GstClockTime lead = (GstClockTime)FeedLeadNs(); |
| 1207 |
2/2✓ Branch 0 taken 1316 times.
✓ Branch 1 taken 489 times.
|
1805 | if (!m_timelineValid) { |
| 1208 | 1316 | why = "nothing to carry on"; | |
| 1209 |
5/10✓ Branch 0 taken 489 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 489 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 489 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 489 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 489 times.
|
489 | } else if (m_state != State::Ready || m_target != m_wantTarget || !m_pwStream || !m_clock) { |
| 1210 | ✗ | why = "the stream is not ready on this sink"; | |
| 1211 |
5/6✓ Branch 0 taken 25 times.
✓ Branch 1 taken 464 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 25 times.
✓ Branch 4 taken 464 times.
✓ Branch 5 taken 25 times.
|
489 | } else if (m_cardStatusPath.empty() || !m_syncAnchored) { |
| 1212 | // Nothing would see a stall in the graph (DisciplineClock()). | ||
| 1213 | 464 | why = "no card is watched"; | |
| 1214 | } else { | ||
| 1215 | 75 | uint64_t read = std::max(m_readPos.load(std::memory_order_acquire), m_dropTo.load(std::memory_order_relaxed)); | |
| 1216 | 25 | uint64_t write = m_writePos.load(std::memory_order_relaxed); | |
| 1217 |
3/4✓ Branch 0 taken 24 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
|
25 | GstClockTime backlog = write > read ? gst_util_uint64_scale(write - read, GST_SECOND, m_rate) : 0; |
| 1218 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
|
25 | if (backlog > 2 * lead) { |
| 1219 | ✗ | why = "audio is backed up in front of the stream"; | |
| 1220 | } | ||
| 1221 | } | ||
| 1222 | // Counted from where the graph may already be taking samples. | ||
| 1223 |
3/6✓ Branch 0 taken 1805 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1805 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1805 times.
✗ Branch 5 not taken.
|
1805 | GstClockTime now = m_clock ? gst_clock_get_time(m_clock) + GraphAheadNs() : 0; |
| 1224 |
3/4✓ Branch 0 taken 25 times.
✓ Branch 1 taken 1780 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 25 times.
|
1805 | if (!why && now > m_timelineEnd) { |
| 1225 | ✗ | why = "it ran out"; | |
| 1226 | } | ||
| 1227 |
2/2✓ Branch 0 taken 1780 times.
✓ Branch 1 taken 25 times.
|
1805 | if (why) { |
| 1228 |
1/2✓ Branch 0 taken 1780 times.
✗ Branch 1 not taken.
|
1780 | LogDebug(VB_MEDIAOUT, "PipeWire output stream %d: not carrying on the timeline (%s)\n", m_slot, why); |
| 1229 |
1/2✓ Branch 0 taken 1780 times.
✗ Branch 1 not taken.
|
1780 | EndTimelineLocked(); |
| 1230 | 1780 | return false; | |
| 1231 | } | ||
| 1232 | 25 | GstClockTime queued = m_timelineEnd - now; | |
| 1233 |
1/2✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
|
25 | ExtendTimelineLocked(now + (GstClockTime)kContinueMinQueuedMs * GST_MSECOND); |
| 1234 | 25 | m_bridging = false; | |
| 1235 | 25 | m_continuedFirst = true; | |
| 1236 | 25 | *baseTime = m_timelineEnd; | |
| 1237 | // Handed over by `now` (a cycle ahead of the clock) plus the handover | ||
| 1238 | // time; never more than the feed's lead ahead of its time. | ||
| 1239 |
1/2✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
|
25 | *earlyNs = (int64_t)std::min(lead, m_timelineEnd - now + GraphAheadNs() - (GstClockTime)kContinueHandoverMs * GST_MSECOND); |
| 1240 |
1/2✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
|
25 | LogDebug(VB_MEDIAOUT, "PipeWire output stream %d: carrying on the timeline %.1f ms after the last track ended (%.1f ms queued, " |
| 1241 | "%.1f ms of silence added, %.1f ms by the bridge before; first sample handed over %.1f ms early)\n", | ||
| 1242 | m_slot, (MonoUs() - m_releasedUs) / 1e3, queued / 1e6, (m_timelineEnd - now - queued) / 1e6, | ||
| 1243 | m_bridgedNs / 1e6, *earlyNs / 1e6); | ||
| 1244 | 25 | return true; | |
| 1245 | 1805 | } | |
| 1246 | |||
| 1247 | // Whether this slot's stream has an output port linked to anything, per | ||
| 1248 | // pw-link. Only run while a stream is opening, every kLinkCheckIntervalMs. | ||
| 1249 | // Time-limited, like the other PipeWire tools fppd runs: a pw-link that never | ||
| 1250 | // answers (a wedged daemon) would otherwise hold this worker for good, and | ||
| 1251 | // shutdown joins it. | ||
| 1252 | 360 | bool PipeWireOutputStream::StreamLinked() { | |
| 1253 |
2/4✓ Branch 0 taken 360 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 360 times.
✗ Branch 3 not taken.
|
360 | std::string prefix = StreamSlotManager::GetNodeName(m_slot) + ":"; |
| 1254 |
1/2✓ Branch 0 taken 360 times.
✗ Branch 1 not taken.
|
360 | FILE* f = popen("timeout -k 1 2 pw-link -o -l 2>/dev/null", "r"); |
| 1255 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 360 times.
|
360 | if (!f) { |
| 1256 | ✗ | return false; | |
| 1257 | } | ||
| 1258 | // Output ports are listed at column 0, each followed by its links | ||
| 1259 | // indented as " |-> peer:port". | ||
| 1260 | 360 | bool ours = false; | |
| 1261 | 360 | bool linked = false; | |
| 1262 | char line[512]; | ||
| 1263 |
3/4✓ Branch 0 taken 2985 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2625 times.
✓ Branch 3 taken 360 times.
|
2985 | while (fgets(line, sizeof(line), f)) { |
| 1264 |
2/2✓ Branch 0 taken 1811 times.
✓ Branch 1 taken 814 times.
|
2625 | if (line[0] != ' ') { |
| 1265 | 1811 | ours = strncmp(line, prefix.c_str(), prefix.size()) == 0; | |
| 1266 |
3/4✓ Branch 0 taken 409 times.
✓ Branch 1 taken 405 times.
✓ Branch 2 taken 409 times.
✗ Branch 3 not taken.
|
814 | } else if (ours && strstr(line, "|->")) { |
| 1267 | 409 | linked = true; | |
| 1268 | } | ||
| 1269 | } | ||
| 1270 |
1/2✓ Branch 0 taken 360 times.
✗ Branch 1 not taken.
|
360 | pclose(f); |
| 1271 | 360 | return linked; | |
| 1272 | 360 | } | |
| 1273 | |||
| 1274 | // --------------------------------------------------------------------------- | ||
| 1275 | // The card clock | ||
| 1276 | // | ||
| 1277 | // Nothing in this stream resamples: PipeWire takes the stream's samples at | ||
| 1278 | // the card's pace, which is whatever the card's crystal makes it -- 432 ppm | ||
| 1279 | // fast on an AM62x I2S cape, 47 ppm slow on a Pi 5 HAT, tens of ppm either way | ||
| 1280 | // for USB. A feed paced by the system clock delivers at the system's pace | ||
| 1281 | // instead. A fast card then drains the stream's cushion (one quantum) and | ||
| 1282 | // plays a quantum of silence: on the 432 ppm cape, 12 ms every 27 s. A slow | ||
| 1283 | // one builds a queue that delays the sound further behind the lights for as | ||
| 1284 | // long as the stream is fed. | ||
| 1285 | // | ||
| 1286 | // So every pipeline feeding the stream runs on this clock, which is held to | ||
| 1287 | // the card. Its rate is fitted to the card's own hardware pointer (hw_ptr in | ||
| 1288 | // /proc/asound/.../status, which ALSA updates from the DMA position when the | ||
| 1289 | // file is read) against CLOCK_MONOTONIC, the clock's internal time, over the | ||
| 1290 | // last two minutes. On top of that, any accumulated phase error is closed | ||
| 1291 | // over ~20 s, so the queue between the feed and the card stays where it | ||
| 1292 | // started instead of wandering with every error in the fit. The media | ||
| 1293 | // position the lights follow comes from the feed, so it is card time too; | ||
| 1294 | // ClockRate() lets the channel output thread run its frames at the same pace. | ||
| 1295 | // | ||
| 1296 | // Without a card (a group of network sinks) the clock is left at the system | ||
| 1297 | // rate. | ||
| 1298 | // --------------------------------------------------------------------------- | ||
| 1299 | |||
| 1300 | 2057 | void PipeWireOutputStream::EnsureClocksLocked() { | |
| 1301 |
2/2✓ Branch 0 taken 212 times.
✓ Branch 1 taken 1845 times.
|
2057 | if (!m_clock) { |
| 1302 |
1/2✓ Branch 0 taken 212 times.
✗ Branch 1 not taken.
|
212 | std::string name = "fpp-card-clock-" + std::to_string(m_slot); |
| 1303 |
4/8✓ Branch 0 taken 212 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 212 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 212 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 212 times.
✗ Branch 7 not taken.
|
212 | m_clock = GST_CLOCK(g_object_new(GST_TYPE_SYSTEM_CLOCK, "name", name.c_str(), |
| 1304 | "clock-type", GST_CLOCK_TYPE_MONOTONIC, NULL)); | ||
| 1305 |
1/2✓ Branch 0 taken 212 times.
✗ Branch 1 not taken.
|
212 | gst_object_ref_sink(m_clock); |
| 1306 | 212 | } | |
| 1307 |
2/2✓ Branch 0 taken 212 times.
✓ Branch 1 taken 1845 times.
|
2057 | if (!m_feedClock) { |
| 1308 | // Both read CLOCK_MONOTONIC as their internal time, and start out | ||
| 1309 | // calibrated alike, so at speed 1 the feed clock reads exactly what | ||
| 1310 | // the card clock does. | ||
| 1311 |
1/2✓ Branch 0 taken 212 times.
✗ Branch 1 not taken.
|
212 | std::string name = "fpp-feed-clock-" + std::to_string(m_slot); |
| 1312 |
4/8✓ Branch 0 taken 212 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 212 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 212 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 212 times.
✗ Branch 7 not taken.
|
212 | m_feedClock = GST_CLOCK(g_object_new(GST_TYPE_SYSTEM_CLOCK, "name", name.c_str(), |
| 1313 | "clock-type", GST_CLOCK_TYPE_MONOTONIC, NULL)); | ||
| 1314 |
1/2✓ Branch 0 taken 212 times.
✗ Branch 1 not taken.
|
212 | gst_object_ref_sink(m_feedClock); |
| 1315 | GstClockTime cInt, cExt, num, den; | ||
| 1316 |
1/2✓ Branch 0 taken 212 times.
✗ Branch 1 not taken.
|
212 | gst_clock_get_calibration(m_clock, &cInt, &cExt, &num, &den); |
| 1317 |
1/2✓ Branch 0 taken 212 times.
✗ Branch 1 not taken.
|
212 | gst_clock_set_calibration(m_feedClock, cInt, cExt, num, den); |
| 1318 | 212 | } | |
| 1319 | 2057 | } | |
| 1320 | |||
| 1321 | ✗ | GstClock* PipeWireOutputStream::Clock() { | |
| 1322 | ✗ | std::unique_lock<std::mutex> lock(m_lock); | |
| 1323 | ✗ | EnsureClocksLocked(); | |
| 1324 | ✗ | return GST_CLOCK(gst_object_ref(m_clock)); | |
| 1325 | ✗ | } | |
| 1326 | |||
| 1327 | 2057 | GstClock* PipeWireOutputStream::FeedClock() { | |
| 1328 |
1/2✓ Branch 0 taken 2057 times.
✗ Branch 1 not taken.
|
2057 | std::unique_lock<std::mutex> lock(m_lock); |
| 1329 |
1/2✓ Branch 0 taken 2057 times.
✗ Branch 1 not taken.
|
2057 | EnsureClocksLocked(); |
| 1330 |
3/6✓ Branch 0 taken 2057 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2057 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2057 times.
✗ Branch 5 not taken.
|
4114 | return GST_CLOCK(gst_object_ref(m_feedClock)); |
| 1331 | 2057 | } | |
| 1332 | |||
| 1333 | 11534 | void PipeWireOutputStream::RecalibrateFeedClockLocked(GstClockTime internal) { | |
| 1334 |
2/4✓ Branch 0 taken 11534 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 11534 times.
|
11534 | if (!m_clock || !m_feedClock) { |
| 1335 | 7442 | return; | |
| 1336 | } | ||
| 1337 | GstClockTime cInt, cExt, num, den; | ||
| 1338 |
1/2✓ Branch 0 taken 11534 times.
✗ Branch 1 not taken.
|
11534 | gst_clock_get_calibration(m_clock, &cInt, &cExt, &num, &den); |
| 1339 |
2/2✓ Branch 0 taken 7442 times.
✓ Branch 1 taken 4092 times.
|
11534 | if (!m_feedDiverged) { |
| 1340 | // Not yet steered: keep it the card clock exactly, no rounding. | ||
| 1341 |
1/2✓ Branch 0 taken 7442 times.
✗ Branch 1 not taken.
|
7442 | gst_clock_set_calibration(m_feedClock, cInt, cExt, num, den); |
| 1342 | 7442 | return; | |
| 1343 | } | ||
| 1344 | // Where the feed clock is now on its current calibration, from here on | ||
| 1345 | // running at the card clock's rate times the speed: continuous at | ||
| 1346 | // `internal`, so a feed waiting on it never sees it step. | ||
| 1347 | GstClockTime fInt, fExt, fNum, fDen; | ||
| 1348 |
1/2✓ Branch 0 taken 4092 times.
✗ Branch 1 not taken.
|
4092 | gst_clock_get_calibration(m_feedClock, &fInt, &fExt, &fNum, &fDen); |
| 1349 |
1/2✓ Branch 0 taken 4092 times.
✗ Branch 1 not taken.
|
4092 | GstClockTime external = gst_clock_adjust_with_calibration(m_feedClock, internal, fInt, fExt, fNum, fDen); |
| 1350 |
1/2✓ Branch 0 taken 4092 times.
✗ Branch 1 not taken.
|
4092 | double rate = (double)num / (double)(den ? den : 1) * m_feedSpeed; |
| 1351 |
1/2✓ Branch 0 taken 4092 times.
✗ Branch 1 not taken.
|
4092 | gst_clock_set_calibration(m_feedClock, internal, external, (GstClockTime)llround(rate * 1e9), 1000000000ULL); |
| 1352 | } | ||
| 1353 | |||
| 1354 | 1124 | void PipeWireOutputStream::SetFeedSpeed(GstElement* feed, double speed) { | |
| 1355 |
1/2✓ Branch 0 taken 1124 times.
✗ Branch 1 not taken.
|
1124 | std::unique_lock<std::mutex> lock(m_lock); |
| 1356 |
5/10✓ Branch 0 taken 1124 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1124 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1124 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1124 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 1124 times.
|
1124 | if (!feed || feed != m_feed || !m_clock || !m_feedClock || speed <= 0.0) { |
| 1357 | ✗ | return; | |
| 1358 | } | ||
| 1359 |
3/4✓ Branch 0 taken 46 times.
✓ Branch 1 taken 1078 times.
✓ Branch 2 taken 46 times.
✗ Branch 3 not taken.
|
1124 | if (speed == m_feedSpeed && m_feedDiverged) { |
| 1360 | 46 | return; | |
| 1361 | } | ||
| 1362 | // A speed of 1 before any other leaves everything untouched. | ||
| 1363 |
3/4✓ Branch 0 taken 30 times.
✓ Branch 1 taken 1048 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 30 times.
|
1078 | if (speed == 1.0 && !m_feedDiverged) { |
| 1364 | ✗ | return; | |
| 1365 | } | ||
| 1366 |
1/2✓ Branch 0 taken 1078 times.
✗ Branch 1 not taken.
|
1078 | GstClockTime internal = gst_clock_get_internal_time(m_clock); |
| 1367 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 1050 times.
|
1078 | if (!m_feedDiverged) { |
| 1368 | 28 | m_feedDiverged = true; | |
| 1369 | 28 | m_rsActive = true; | |
| 1370 | // The next output frame is the next buffer's first: everything up to | ||
| 1371 | // it has gone out as it came, and m_rsHist holds the frames before. | ||
| 1372 | 28 | m_rsPos = 0.0; | |
| 1373 | } | ||
| 1374 | // Recalibrate at the old speed first (a no-op continuation), then switch. | ||
| 1375 |
1/2✓ Branch 0 taken 1078 times.
✗ Branch 1 not taken.
|
1078 | RecalibrateFeedClockLocked(internal); |
| 1376 | 1078 | m_feedSpeed = speed; | |
| 1377 |
1/2✓ Branch 0 taken 1078 times.
✗ Branch 1 not taken.
|
1078 | RecalibrateFeedClockLocked(internal); |
| 1378 |
2/2✓ Branch 0 taken 1078 times.
✓ Branch 1 taken 46 times.
|
1124 | } |
| 1379 | |||
| 1380 | 30 | void PipeWireOutputStream::FlushFeed(GstElement* feed) { | |
| 1381 | { | ||
| 1382 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 | std::unique_lock<std::mutex> lock(m_lock); |
| 1383 |
2/4✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 30 times.
|
30 | if (!feed || feed != m_feed) { |
| 1384 | ✗ | return; | |
| 1385 | } | ||
| 1386 | // What comes next doesn't follow on from what was resampled before. | ||
| 1387 | 30 | m_rsHistFrames = 0; | |
| 1388 | 30 | m_rsPos = 0.0; | |
| 1389 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 | } |
| 1390 | // Unprimed: the seek is already a jump in the sound, and the feed's | ||
| 1391 | // running time starts again, so its first buffers come on time (a sink | ||
| 1392 | // can't render ahead of its base time). Priming would only put silence | ||
| 1393 | // ahead of where the feed lands. | ||
| 1394 | 30 | Flush(FlushFor::Seek); | |
| 1395 | } | ||
| 1396 | |||
| 1397 | // The feed's resampler (SetFeedSpeed()): a 16-tap Kaiser-windowed sinc, its | ||
| 1398 | // fractional delay taken from a 256-phase table with linear interpolation | ||
| 1399 | // between phases. A speed servo only moves the ratio a few tenths of a | ||
| 1400 | // percent from 1, but every sample then sits at a fraction that sweeps | ||
| 1401 | // through the whole range, and a short interpolator's response changes with | ||
| 1402 | // that fraction: a 4-point cubic measured -33 dB THD+N on an 8 kHz tone, | ||
| 1403 | // audible as warbling highs. This one measures -81 dB at 1 kHz, -88 dB at | ||
| 1404 | // 8 kHz and -74 dB at 15 kHz (44.1 kHz, ratios 0.9995-1.003), and with NEON | ||
| 1405 | // costs 1.7% of a BBB's core (0.3% of a PocketBeagle2's) for 44.1 kHz stereo. | ||
| 1406 | namespace { | ||
| 1407 | constexpr int kRsTaps = 16; | ||
| 1408 | constexpr int kRsHalf = kRsTaps / 2; | ||
| 1409 | constexpr int kRsPhases = 256; | ||
| 1410 | constexpr int kRsHist = kRsTaps - 1; // frames before the next buffer it reaches back to | ||
| 1411 | constexpr double kRsKaiserBeta = 7.0; | ||
| 1412 | |||
| 1413 | 15504 | const float* ResampleTable() { | |
| 1414 | 42 | static const std::vector<float> table = [] { | |
| 1415 | 172620 | auto bessel0 = [](double x) { | |
| 1416 | 172620 | double sum = 1.0, term = 1.0; | |
| 1417 |
2/2✓ Branch 0 taken 5005980 times.
✓ Branch 1 taken 172620 times.
|
5178600 | for (int k = 1; k < 30; k++) { |
| 1418 | 5005980 | term *= (x / (2 * k)) * (x / (2 * k)); | |
| 1419 | 5005980 | sum += term; | |
| 1420 | } | ||
| 1421 | 172620 | return sum; | |
| 1422 | }; | ||
| 1423 |
1/2✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
|
21 | std::vector<float> t((kRsPhases + 1) * kRsTaps); |
| 1424 |
2/2✓ Branch 0 taken 5397 times.
✓ Branch 1 taken 21 times.
|
5418 | for (int ph = 0; ph <= kRsPhases; ph++) { |
| 1425 | 5397 | double frac = (double)ph / kRsPhases; | |
| 1426 | 5397 | double sum = 0.0; | |
| 1427 |
2/2✓ Branch 0 taken 86352 times.
✓ Branch 1 taken 5397 times.
|
91749 | for (int k = 0; k < kRsTaps; k++) { |
| 1428 | // Tap k reads input frame i + k - (kRsHalf - 1). | ||
| 1429 | 86352 | double x = (double)(k - (kRsHalf - 1)) - frac; | |
| 1430 |
2/2✓ Branch 0 taken 86310 times.
✓ Branch 1 taken 42 times.
|
86352 | double sinc = x == 0.0 ? 1.0 : std::sin(M_PI * x) / (M_PI * x); |
| 1431 | 86352 | double r = x / kRsHalf; | |
| 1432 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 86310 times.
|
86352 | double w = std::fabs(r) >= 1.0 ? 0.0 : bessel0(kRsKaiserBeta * std::sqrt(1.0 - r * r)) / bessel0(kRsKaiserBeta); |
| 1433 | 86352 | t[ph * kRsTaps + k] = (float)(sinc * w); | |
| 1434 | 86352 | sum += sinc * w; | |
| 1435 | } | ||
| 1436 |
2/2✓ Branch 0 taken 86352 times.
✓ Branch 1 taken 5397 times.
|
91749 | for (int k = 0; k < kRsTaps; k++) { |
| 1437 | 86352 | t[ph * kRsTaps + k] = (float)(t[ph * kRsTaps + k] / sum); // unity gain at DC | |
| 1438 | } | ||
| 1439 | } | ||
| 1440 | 21 | return t; | |
| 1441 |
4/8✓ Branch 0 taken 21 times.
✓ Branch 1 taken 15483 times.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 21 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
15504 | }(); |
| 1442 | 15504 | return table.data(); | |
| 1443 | } | ||
| 1444 | } | ||
| 1445 | |||
| 1446 | 63865 | void PipeWireOutputStream::KeepResampleHistoryLocked(GstBuffer* in) { | |
| 1447 | GstMapInfo map; | ||
| 1448 |
2/4✓ Branch 0 taken 63865 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 63865 times.
|
63865 | if (!gst_buffer_map(in, &map, GST_MAP_READ)) { |
| 1449 | ✗ | return; | |
| 1450 | } | ||
| 1451 | 63865 | const float* x = reinterpret_cast<const float*>(map.data); | |
| 1452 | 63865 | int n = (int)(map.size / kBytesPerFrame); | |
| 1453 | 63865 | int take = std::min(n, kRsHist); | |
| 1454 | // Shift out what the new frames replace, then append them. | ||
| 1455 | 63865 | int keep = std::min(m_rsHistFrames, kRsHist - take); | |
| 1456 | 63865 | std::memmove(m_rsHist, m_rsHist + (m_rsHistFrames - keep) * kChannels, keep * kChannels * sizeof(float)); | |
| 1457 | 63865 | std::memcpy(m_rsHist + keep * kChannels, x + (n - take) * kChannels, take * kChannels * sizeof(float)); | |
| 1458 | 63865 | m_rsHistFrames = keep + take; | |
| 1459 |
1/2✓ Branch 0 taken 63865 times.
✗ Branch 1 not taken.
|
63865 | gst_buffer_unmap(in, &map); |
| 1460 | } | ||
| 1461 | |||
| 1462 | 15504 | GstBuffer* PipeWireOutputStream::ResampleLocked(GstBuffer* in) { | |
| 1463 | static_assert(sizeof(m_rsHist) == kRsHist * kChannels * sizeof(float), "m_rsHist holds kRsHist frames"); | ||
| 1464 | GstMapInfo im; | ||
| 1465 |
2/4✓ Branch 0 taken 15504 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 15504 times.
|
15504 | if (!gst_buffer_map(in, &im, GST_MAP_READ)) { |
| 1466 | ✗ | return nullptr; | |
| 1467 | } | ||
| 1468 | 15504 | const float* x = reinterpret_cast<const float*>(im.data); | |
| 1469 | 15504 | const long n = (long)(im.size / kBytesPerFrame); | |
| 1470 | 15504 | const double step = m_feedSpeed; | |
| 1471 |
1/2✓ Branch 0 taken 15504 times.
✗ Branch 1 not taken.
|
15504 | const float* table = ResampleTable(); |
| 1472 | // Input frame i of this buffer, or of the history for i in -kRsHist..-1 | ||
| 1473 | // (a frame before the first of the track reads as silence). | ||
| 1474 | 15504 | const int hist = m_rsHistFrames; | |
| 1475 | 7418208 | auto at = [&](long i, int c) -> float { | |
| 1476 |
2/2✓ Branch 0 taken 3712314 times.
✓ Branch 1 taken 3705894 times.
|
7418208 | if (i >= 0) { |
| 1477 | 3712314 | return x[i * kChannels + c]; | |
| 1478 | } | ||
| 1479 | 3705894 | long h = hist + i; | |
| 1480 |
2/2✓ Branch 0 taken 3704214 times.
✓ Branch 1 taken 1680 times.
|
3705894 | return h >= 0 ? m_rsHist[h * kChannels + c] : 0.0f; |
| 1481 | 15504 | }; | |
| 1482 | 15504 | size_t cap = (size_t)((double)(n + kRsHist) / step) + 4; | |
| 1483 |
1/2✓ Branch 0 taken 15504 times.
✗ Branch 1 not taken.
|
15504 | GstBuffer* out = gst_buffer_new_allocate(nullptr, cap * kBytesPerFrame, nullptr); |
| 1484 | GstMapInfo om; | ||
| 1485 |
4/8✓ Branch 0 taken 15504 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15504 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 15504 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 15504 times.
|
15504 | if (!out || !gst_buffer_map(out, &om, GST_MAP_WRITE)) { |
| 1486 | ✗ | gst_buffer_unmap(in, &im); | |
| 1487 | ✗ | if (out) { | |
| 1488 | ✗ | gst_buffer_unref(out); | |
| 1489 | } | ||
| 1490 | ✗ | return nullptr; | |
| 1491 | } | ||
| 1492 | 15504 | float* y = reinterpret_cast<float*>(om.data); | |
| 1493 | 15504 | size_t m = 0; | |
| 1494 | 15504 | double p = m_rsPos; | |
| 1495 | float coef[kRsTaps]; | ||
| 1496 |
1/2✓ Branch 0 taken 37562879 times.
✗ Branch 1 not taken.
|
37562879 | while (m < cap) { |
| 1497 | 37562879 | long i = (long)std::floor(p); | |
| 1498 |
2/2✓ Branch 0 taken 15504 times.
✓ Branch 1 taken 37547375 times.
|
37562879 | if (i + kRsHalf > n - 1) { |
| 1499 | 15504 | break; | |
| 1500 | } | ||
| 1501 | 37547375 | double fph = (p - (double)i) * kRsPhases; | |
| 1502 | 37547375 | int ph = std::min((int)fph, kRsPhases - 1); | |
| 1503 | 37547375 | float fr = (float)(fph - ph); | |
| 1504 | 37547375 | const float* c0 = table + ph * kRsTaps; | |
| 1505 | 37547375 | const float* c1 = c0 + kRsTaps; | |
| 1506 | 37547375 | long first = i - (kRsHalf - 1); | |
| 1507 | #if defined(__ARM_NEON) || defined(__ARM_NEON__) | ||
| 1508 |
2/2✓ Branch 0 taken 31468825 times.
✓ Branch 1 taken 179325 times.
|
31648150 | if (first >= 0) { |
| 1509 | // Both channels four taps at a time: 1.7% of a BBB's core for | ||
| 1510 | // 44.1 kHz stereo, against 8.7% for the plain loop below (GCC | ||
| 1511 | // won't vectorize a float sum without -ffast-math). | ||
| 1512 | static_assert(kChannels == 2 && kRsTaps % 4 == 0, "the NEON path is stereo, four taps a step"); | ||
| 1513 | 31468825 | const float* xs = x + first * kChannels; | |
| 1514 | 31468825 | float32x4_t accL = vdupq_n_f32(0.0f); | |
| 1515 | 31468825 | float32x4_t accR = vdupq_n_f32(0.0f); | |
| 1516 |
2/2✓ Branch 0 taken 125875300 times.
✓ Branch 1 taken 31468825 times.
|
157344125 | for (int k = 0; k < kRsTaps; k += 4) { |
| 1517 |
0/2✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
125875300 | float32x4_t a = vld1q_f32(c0 + k); |
| 1518 |
0/6✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
377625900 | float32x4_t co = vmlaq_n_f32(a, vsubq_f32(vld1q_f32(c1 + k), a), fr); |
| 1519 |
0/2✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
125875300 | float32x4x2_t lr = vld2q_f32(xs + 2 * k); |
| 1520 |
0/2✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
125875300 | accL = vmlaq_f32(accL, lr.val[0], co); |
| 1521 |
0/2✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
251750600 | accR = vmlaq_f32(accR, lr.val[1], co); |
| 1522 | } | ||
| 1523 |
0/6✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
94406475 | float32x2_t l2 = vadd_f32(vget_low_f32(accL), vget_high_f32(accL)); |
| 1524 |
0/4✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
62937650 | float32x2_t r2 = vadd_f32(vget_low_f32(accR), vget_high_f32(accR)); |
| 1525 |
0/4✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
62937650 | vst1_f32(y + m * kChannels, vpadd_f32(l2, r2)); |
| 1526 | 31468825 | m++; | |
| 1527 | 31468825 | p += step; | |
| 1528 | 31468825 | continue; | |
| 1529 | 31468825 | } | |
| 1530 | #endif | ||
| 1531 |
2/2✓ Branch 0 taken 97256800 times.
✓ Branch 1 taken 6078550 times.
|
103335350 | for (int k = 0; k < kRsTaps; k++) { |
| 1532 | 97256800 | coef[k] = c0[k] + fr * (c1[k] - c0[k]); | |
| 1533 | } | ||
| 1534 |
2/2✓ Branch 0 taken 12157100 times.
✓ Branch 1 taken 6078550 times.
|
18235650 | for (int c = 0; c < kChannels; c++) { |
| 1535 | 12157100 | float acc = 0.0f; | |
| 1536 |
2/2✓ Branch 0 taken 11693462 times.
✓ Branch 1 taken 463638 times.
|
12157100 | if (first >= 0) { |
| 1537 | 11693462 | const float* xs = x + first * kChannels + c; | |
| 1538 |
2/2✓ Branch 0 taken 187095392 times.
✓ Branch 1 taken 11693462 times.
|
198788854 | for (int k = 0; k < kRsTaps; k++) { |
| 1539 | 187095392 | acc += coef[k] * xs[k * kChannels]; | |
| 1540 | } | ||
| 1541 | } else { | ||
| 1542 |
2/2✓ Branch 0 taken 7418208 times.
✓ Branch 1 taken 463638 times.
|
7881846 | for (int k = 0; k < kRsTaps; k++) { |
| 1543 | 7418208 | acc += coef[k] * at(first + k, c); | |
| 1544 | } | ||
| 1545 | } | ||
| 1546 | 12157100 | y[m * kChannels + c] = acc; | |
| 1547 | } | ||
| 1548 | 6078550 | m++; | |
| 1549 | 6078550 | p += step; | |
| 1550 | } | ||
| 1551 | // At the exit p >= n - kRsHalf, so the next buffer reaches back no | ||
| 1552 | // further than the history's kRsHist frames. | ||
| 1553 | 15504 | m_rsPos = p - (double)n; | |
| 1554 |
1/2✓ Branch 0 taken 15504 times.
✗ Branch 1 not taken.
|
15504 | gst_buffer_unmap(out, &om); |
| 1555 |
1/2✓ Branch 0 taken 15504 times.
✗ Branch 1 not taken.
|
15504 | gst_buffer_unmap(in, &im); |
| 1556 |
1/2✓ Branch 0 taken 15504 times.
✗ Branch 1 not taken.
|
15504 | gst_buffer_set_size(out, (gssize)(m * kBytesPerFrame)); |
| 1557 |
1/2✓ Branch 0 taken 15504 times.
✗ Branch 1 not taken.
|
15504 | KeepResampleHistoryLocked(in); |
| 1558 | 15504 | return out; | |
| 1559 | } | ||
| 1560 | |||
| 1561 | 78659 | void PipeWireOutputStream::ResetClockSyncLocked() { | |
| 1562 | 78659 | m_syncAnchored = false; | |
| 1563 | 78659 | m_syncWindow.clear(); | |
| 1564 | 78659 | } | |
| 1565 | |||
| 1566 | // `trigger` is when the stream was last started (trigger_time), "" if the | ||
| 1567 | // file has none: a restart changes it, hw_ptr wrapping doesn't. | ||
| 1568 | 7342 | static bool ReadCardPosition(const std::string& path, int64_t& hw, std::string& trigger) { | |
| 1569 |
1/2✓ Branch 0 taken 7342 times.
✗ Branch 1 not taken.
|
7342 | std::string status = GetFileContents(path); |
| 1570 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 7337 times.
|
7342 | if (status.find("state: RUNNING") == std::string::npos) { |
| 1571 | 5 | return false; | |
| 1572 | } | ||
| 1573 | 7337 | std::size_t p = status.find("hw_ptr"); | |
| 1574 |
3/6✓ Branch 0 taken 7337 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7337 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 7337 times.
|
7337 | if (p == std::string::npos || (p = status.find(':', p)) == std::string::npos) { |
| 1575 | ✗ | return false; | |
| 1576 | } | ||
| 1577 | 7337 | hw = strtoll(status.c_str() + p + 1, nullptr, 10); | |
| 1578 | 7337 | trigger.clear(); | |
| 1579 | 7337 | std::size_t t = status.find("trigger_time"); | |
| 1580 |
3/6✓ Branch 0 taken 7337 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7337 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7337 times.
✗ Branch 5 not taken.
|
7337 | if (t != std::string::npos && (t = status.find(':', t)) != std::string::npos) { |
| 1581 | 7337 | std::size_t e = status.find('\n', t); | |
| 1582 |
2/4✓ Branch 0 taken 7337 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7337 times.
✗ Branch 3 not taken.
|
7337 | trigger = status.substr(t + 1, e == std::string::npos ? std::string::npos : e - t - 1); |
| 1583 |
1/2✓ Branch 0 taken 7337 times.
✗ Branch 1 not taken.
|
7337 | TrimWhiteSpace(trigger); |
| 1584 | } | ||
| 1585 | 7337 | return true; | |
| 1586 | 7342 | } | |
| 1587 | |||
| 1588 | // Where the card's hw_ptr wraps back to 0 (sw_params "boundary"), 0 if it | ||
| 1589 | // can't be read. ALSA picks the largest power-of-two multiple of the buffer | ||
| 1590 | // below LONG_MAX: 2^62 frames with a 64-bit kernel, but 2^30 with a 32-bit | ||
| 1591 | // one (a BBB) -- about 6 h at 48 kHz, which a card kept running across songs | ||
| 1592 | // reaches in a long show. | ||
| 1593 | 2 | static int64_t ReadCardBoundary(const std::string& statusPath) { | |
| 1594 |
3/6✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
2 | std::string text = GetFileContents(statusPath.substr(0, statusPath.rfind('/')) + "/sw_params"); |
| 1595 | 2 | std::size_t p = text.find("boundary"); | |
| 1596 |
3/6✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
|
2 | if (p == std::string::npos || (p = text.find(':', p)) == std::string::npos) { |
| 1597 | ✗ | return 0; | |
| 1598 | } | ||
| 1599 | 2 | return strtoll(text.c_str() + p + 1, nullptr, 10); | |
| 1600 | 2 | } | |
| 1601 | |||
| 1602 | 108558 | bool PipeWireOutputStream::DisciplineClock() { | |
| 1603 | 108558 | std::string path; | |
| 1604 | { | ||
| 1605 |
1/2✓ Branch 0 taken 108558 times.
✗ Branch 1 not taken.
|
108558 | std::unique_lock<std::mutex> lock(m_lock); |
| 1606 |
8/8✓ Branch 0 taken 104747 times.
✓ Branch 1 taken 3811 times.
✓ Branch 2 taken 100600 times.
✓ Branch 3 taken 4147 times.
✓ Branch 4 taken 70669 times.
✓ Branch 5 taken 29931 times.
✓ Branch 6 taken 78627 times.
✓ Branch 7 taken 29931 times.
|
108558 | if (!m_clock || m_state != State::Ready || m_cardStatusPath.empty()) { |
| 1607 |
1/2✓ Branch 0 taken 78627 times.
✗ Branch 1 not taken.
|
78627 | ResetClockSyncLocked(); |
| 1608 | 78627 | return false; | |
| 1609 | } | ||
| 1610 |
1/2✓ Branch 0 taken 29931 times.
✗ Branch 1 not taken.
|
29931 | GstClockTime nowInt = gst_clock_get_internal_time(m_clock); |
| 1611 |
4/4✓ Branch 0 taken 29899 times.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 22589 times.
✓ Branch 3 taken 7310 times.
|
29931 | if (m_syncAnchored && nowInt - m_syncLastInt < (GstClockTime)kClockSampleMs * GST_MSECOND) { |
| 1612 | 22589 | return false; | |
| 1613 | } | ||
| 1614 |
1/2✓ Branch 0 taken 7342 times.
✗ Branch 1 not taken.
|
7342 | path = m_cardStatusPath; |
| 1615 |
2/2✓ Branch 0 taken 27 times.
✓ Branch 1 taken 7315 times.
|
7342 | if (path != m_syncPath) { |
| 1616 | // Another card: what was learnt about the last one doesn't apply. | ||
| 1617 |
1/2✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
|
27 | m_syncPath = path; |
| 1618 | 27 | m_syncCardRate = 0; | |
| 1619 | 27 | m_clockRate = 1.0; | |
| 1620 |
1/2✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
|
27 | ResetClockSyncLocked(); |
| 1621 | } | ||
| 1622 |
2/2✓ Branch 0 taken 7342 times.
✓ Branch 1 taken 101216 times.
|
108558 | } |
| 1623 |
2/2✓ Branch 0 taken 27 times.
✓ Branch 1 taken 7315 times.
|
7342 | if (m_syncCardRate <= 0) { |
| 1624 |
2/4✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
|
27 | std::string hwParams = path.substr(0, path.rfind('/')) + "/hw_params"; |
| 1625 |
1/2✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
|
27 | std::string text = GetFileContents(hwParams); |
| 1626 | 27 | std::size_t p = text.find("rate:"); | |
| 1627 |
1/2✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
|
27 | m_syncCardRate = p == std::string::npos ? 0 : atoi(text.c_str() + p + 5); |
| 1628 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | if (m_syncCardRate <= 0) { |
| 1629 | ✗ | return false; | |
| 1630 | } | ||
| 1631 |
2/4✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
|
27 | } |
| 1632 | // The card's position and the clock's internal time, as close together | ||
| 1633 | // as they can be read: the midpoint of the read. | ||
| 1634 |
1/2✓ Branch 0 taken 7342 times.
✗ Branch 1 not taken.
|
7342 | GstClockTime before = gst_clock_get_internal_time(m_clock); |
| 1635 | 7342 | int64_t hw = 0; | |
| 1636 | 7342 | std::string trigger; | |
| 1637 |
1/2✓ Branch 0 taken 7342 times.
✗ Branch 1 not taken.
|
7342 | bool running = ReadCardPosition(path, hw, trigger); |
| 1638 |
1/2✓ Branch 0 taken 7342 times.
✗ Branch 1 not taken.
|
7342 | GstClockTime after = gst_clock_get_internal_time(m_clock); |
| 1639 | 7342 | GstClockTime internal = before + (after - before) / 2; | |
| 1640 | |||
| 1641 |
1/2✓ Branch 0 taken 7342 times.
✗ Branch 1 not taken.
|
7342 | std::unique_lock<std::mutex> lock(m_lock); |
| 1642 |
3/6✓ Branch 0 taken 7342 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7342 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 7342 times.
|
7342 | if (path != m_cardStatusPath || m_state != State::Ready) { |
| 1643 | ✗ | return false; | |
| 1644 | } | ||
| 1645 | // The card stopping, or restarting, while a track feeds the stream is an | ||
| 1646 | // xrun: PipeWire restarted the device, and what the feed delivered | ||
| 1647 | // meanwhile is now queued behind it (see Flush()). | ||
| 1648 | // | ||
| 1649 | // A stop or restart right after one of our own flushes is the flush's | ||
| 1650 | // doing, not lost time. Taken for an xrun, it was flushed again, which | ||
| 1651 | // restarted the card again: on a card that restarts on a flush, one xrun | ||
| 1652 | // -- or the flush before a track -- had the stream flushed every 250 ms | ||
| 1653 | // for as long as it played. | ||
| 1654 | 7342 | bool lostTime = false; | |
| 1655 |
1/2✓ Branch 0 taken 7342 times.
✗ Branch 1 not taken.
|
7342 | bool ownRestart = NowMs() - m_lastFlushMs < kFlushRestartMs; |
| 1656 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 7337 times.
|
7342 | if (!running) { |
| 1657 | // The card stopped under a feed: an xrun. Flushed only once the card | ||
| 1658 | // runs again. Seen stopped, the graph usually still is (the stall | ||
| 1659 | // that caused it hasn't ended), and a flush then dropped only what was | ||
| 1660 | // queued so far: what the feed handed over for the rest of the stall | ||
| 1661 | // queued up behind it, and the sound stayed that far behind the lights | ||
| 1662 | // -- up to the whole stall, by where in it the card was read. | ||
| 1663 |
4/6✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
|
5 | if (m_syncAnchored && m_feed && !ownRestart) { |
| 1664 | 4 | m_xrunPending = true; | |
| 1665 | } | ||
| 1666 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | ResetClockSyncLocked(); |
| 1667 | 5 | return false; | |
| 1668 | } | ||
| 1669 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 7333 times.
|
7337 | if (m_xrunPending) { |
| 1670 | 4 | m_xrunPending = false; | |
| 1671 | 4 | lostTime = m_feed != nullptr; | |
| 1672 | } | ||
| 1673 | // A restart resets hw_ptr and changes trigger_time. hw_ptr also goes back | ||
| 1674 | // when it wraps at the ALSA boundary, with the card running on -- on a | ||
| 1675 | // 32-bit kernel every ~6 h -- and taking that for an xrun flushed a song | ||
| 1676 | // in mid-play. A wrap is told apart by an unchanged trigger_time and an | ||
| 1677 | // advance across the boundary no longer than the time since the last | ||
| 1678 | // sample allows; the position is then carried on past it. | ||
| 1679 |
4/4✓ Branch 0 taken 7306 times.
✓ Branch 1 taken 31 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 7305 times.
|
7337 | bool restarted = m_syncAnchored && trigger != m_syncTrigger; |
| 1680 |
6/6✓ Branch 0 taken 7306 times.
✓ Branch 1 taken 31 times.
✓ Branch 2 taken 7305 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 7303 times.
|
7337 | if (m_syncAnchored && !restarted && hw < m_syncLastHw) { |
| 1681 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | int64_t boundary = trigger.empty() ? 0 : ReadCardBoundary(path); |
| 1682 | 2 | double sinceS = (double)(internal - m_syncLastInt) / GST_SECOND; | |
| 1683 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | double advanceS = boundary > m_syncLastHw ? (double)(hw + boundary - m_syncLastHw) / m_syncCardRate : -1.0; |
| 1684 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | if (advanceS >= 0.0 && advanceS <= sinceS + 1.0) { |
| 1685 | 2 | m_syncWrap += boundary; | |
| 1686 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | LogDebug(VB_MEDIAOUT, "PipeWire output stream %d: card position wrapped at %lld\n", |
| 1687 | m_slot, (long long)boundary); | ||
| 1688 | } else { | ||
| 1689 | ✗ | restarted = true; | |
| 1690 | } | ||
| 1691 | } | ||
| 1692 |
1/2✓ Branch 0 taken 7337 times.
✗ Branch 1 not taken.
|
7337 | m_syncTrigger = trigger; |
| 1693 | 7337 | int64_t pos = hw + m_syncWrap; | |
| 1694 | |||
| 1695 | GstClockTime cInt, cExt, num, den; | ||
| 1696 |
1/2✓ Branch 0 taken 7337 times.
✗ Branch 1 not taken.
|
7337 | gst_clock_get_calibration(m_clock, &cInt, &cExt, &num, &den); |
| 1697 |
1/2✓ Branch 0 taken 7337 times.
✗ Branch 1 not taken.
|
7337 | GstClockTime external = gst_clock_adjust_with_calibration(m_clock, internal, cInt, cExt, num, den); |
| 1698 | |||
| 1699 |
2/2✓ Branch 0 taken 7306 times.
✓ Branch 1 taken 31 times.
|
7337 | double cardS = m_syncAnchored ? (double)(pos - m_syncHw0) / m_syncCardRate : 0.0; |
| 1700 |
2/2✓ Branch 0 taken 7306 times.
✓ Branch 1 taken 31 times.
|
7337 | double clockS = m_syncAnchored ? (double)(GstClockTimeDiff)(external - m_syncExt0) / GST_SECOND : 0.0; |
| 1701 | 7337 | double phase = cardS - clockS; // > 0: the card is ahead of the clock | |
| 1702 |
5/6✓ Branch 0 taken 7306 times.
✓ Branch 1 taken 31 times.
✓ Branch 2 taken 7305 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 7305 times.
|
7337 | if (!m_syncAnchored || restarted || std::fabs(phase) > kClockMaxPhaseError) { |
| 1703 |
7/12✓ Branch 0 taken 28 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 27 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
32 | lostTime = lostTime || (m_syncAnchored && m_feed && (restarted ? !ownRestart : phase < -kClockMaxPhaseError)); |
| 1704 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 31 times.
|
32 | if (m_syncAnchored) { |
| 1705 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogDebug(VB_MEDIAOUT, "PipeWire output stream %d: card clock re-anchored (phase %.1f ms)\n", |
| 1706 | m_slot, phase * 1000.0); | ||
| 1707 | } | ||
| 1708 | // Start (again) from here, keeping the rate learnt so far. | ||
| 1709 | 32 | m_syncAnchored = true; | |
| 1710 | 32 | m_syncHw0 = pos; | |
| 1711 | 32 | m_syncInt0 = internal; | |
| 1712 | 32 | m_syncExt0 = external; | |
| 1713 | 32 | m_syncWindow.clear(); | |
| 1714 | 32 | cardS = clockS = phase = 0.0; | |
| 1715 | } | ||
| 1716 | 7337 | m_syncLastHw = hw; | |
| 1717 | 7337 | m_syncLastInt = internal; | |
| 1718 | |||
| 1719 | 7337 | double intS = (double)(internal - m_syncInt0) / GST_SECOND; | |
| 1720 |
1/2✓ Branch 0 taken 7337 times.
✗ Branch 1 not taken.
|
7337 | m_syncWindow.emplace_back(intS, cardS); |
| 1721 |
6/6✓ Branch 0 taken 8801 times.
✓ Branch 1 taken 64 times.
✓ Branch 2 taken 1528 times.
✓ Branch 3 taken 7273 times.
✓ Branch 4 taken 1528 times.
✓ Branch 5 taken 7337 times.
|
8865 | while (m_syncWindow.size() > 2 && intS - m_syncWindow.front().first > kClockWindowS) { |
| 1722 | 1528 | m_syncWindow.pop_front(); | |
| 1723 | } | ||
| 1724 | 7337 | double rate = m_clockRate.load(std::memory_order_relaxed); | |
| 1725 |
2/2✓ Branch 0 taken 6839 times.
✓ Branch 1 taken 498 times.
|
7337 | if (intS - m_syncWindow.front().first >= kClockMinFitS) { |
| 1726 | 6839 | double mx = 0, my = 0; | |
| 1727 |
2/2✓ Branch 0 taken 1751740 times.
✓ Branch 1 taken 6839 times.
|
1758579 | for (auto& [x, y] : m_syncWindow) { |
| 1728 | 1751740 | mx += x; | |
| 1729 | 1751740 | my += y; | |
| 1730 | } | ||
| 1731 | 6839 | mx /= m_syncWindow.size(); | |
| 1732 | 6839 | my /= m_syncWindow.size(); | |
| 1733 | 6839 | double sxy = 0, sxx = 0; | |
| 1734 |
2/2✓ Branch 0 taken 1751740 times.
✓ Branch 1 taken 6839 times.
|
1758579 | for (auto& [x, y] : m_syncWindow) { |
| 1735 | 1751740 | sxy += (x - mx) * (y - my); | |
| 1736 | 1751740 | sxx += (x - mx) * (x - mx); | |
| 1737 | } | ||
| 1738 |
1/2✓ Branch 0 taken 6839 times.
✗ Branch 1 not taken.
|
6839 | if (sxx > 0) { |
| 1739 |
1/2✓ Branch 0 taken 6839 times.
✗ Branch 1 not taken.
|
6839 | rate = std::clamp(sxy / sxx, 1.0 - kClockMaxOffset, 1.0 + kClockMaxOffset); |
| 1740 | 6839 | m_clockRate = rate; | |
| 1741 | } | ||
| 1742 | } | ||
| 1743 |
1/2✓ Branch 0 taken 7337 times.
✗ Branch 1 not taken.
|
7337 | double applied = rate + std::clamp(phase * kClockPhaseGain, -kClockMaxPhaseSlew, kClockMaxPhaseSlew); |
| 1744 |
1/2✓ Branch 0 taken 7337 times.
✗ Branch 1 not taken.
|
7337 | applied = std::clamp(applied, 1.0 - kClockMaxOffset, 1.0 + kClockMaxOffset); |
| 1745 | // Continuous at `internal`: only the rate from here on changes. | ||
| 1746 |
1/2✓ Branch 0 taken 7337 times.
✗ Branch 1 not taken.
|
7337 | gst_clock_set_calibration(m_clock, internal, external, (GstClockTime)llround(applied * 1e9), 1000000000ULL); |
| 1747 |
1/2✓ Branch 0 taken 7337 times.
✗ Branch 1 not taken.
|
7337 | RecalibrateFeedClockLocked(internal); |
| 1748 |
1/2✓ Branch 0 taken 7337 times.
✗ Branch 1 not taken.
|
7337 | LogExcess(VB_MEDIAOUT, "PipeWire output stream %d: card %+.1f ppm, phase %+.2f ms, clock %+.1f ppm\n", |
| 1749 | m_slot, (rate - 1.0) * 1e6, phase * 1000.0, (applied - 1.0) * 1e6); | ||
| 1750 | 7337 | return lostTime; | |
| 1751 | 108558 | } | |
| 1752 | |||
| 1753 | // How much later than PipeWire says the card really plays what it is given. | ||
| 1754 | // | ||
| 1755 | // PipeWire's delay to the device (pw_stream_get_time_n()) is the latency the | ||
| 1756 | // nodes downstream declare: the same in every cycle, whatever the card's | ||
| 1757 | // queue holds. The card's queue is deeper. On a BeagleBone with a USB card, | ||
| 1758 | // PipeWire said 58 ms and the card held 75 ms ahead of each cycle's samples, | ||
| 1759 | // so the media position -- what the lights follow, what MultiSync sends -- | ||
| 1760 | // led the sound by 17 ms, measured on the wire. And the queue isn't steady: | ||
| 1761 | // a card that has just started (every track after a pause, with the card | ||
| 1762 | // suspending when idle) holds a few ms more than PipeWire declares at first | ||
| 1763 | // and deepens to its steady depth over ~2 s, so the lead changed through the | ||
| 1764 | // first seconds of every such song. | ||
| 1765 | // | ||
| 1766 | // The card's own pointers say exactly where each cycle's samples are. | ||
| 1767 | // PipeWire writes each cycle's samples to the card in that cycle, so the | ||
| 1768 | // card's appl_ptr, less the frames handed to the graph so far, is a constant | ||
| 1769 | // offset for as long as the card runs. Read between one cycle's write and the | ||
| 1770 | // next's, that offset is the one it settled on, and the newest cycle's first | ||
| 1771 | // sample is then `delay` (the frames the card still has to play) less that | ||
| 1772 | // cycle's frames from being heard. A reading that falls just as the next | ||
| 1773 | // cycle is written gives a different offset and is passed over. | ||
| 1774 | // | ||
| 1775 | // None of this applies to a card the graph resamples for (its frames aren't | ||
| 1776 | // the stream's) or to a group with no card: the correction stays 0 there, and | ||
| 1777 | // PipeWire's figure is used as it was. | ||
| 1778 | 377 | void PipeWireOutputStream::ResetCardQueue() { | |
| 1779 | 377 | m_cqTrigger.clear(); | |
| 1780 | 377 | m_cqCardRate = 0; | |
| 1781 | 377 | m_cqOffsets.clear(); | |
| 1782 | 377 | m_cqOffsetValid = false; | |
| 1783 | 377 | m_cqCorr.clear(); | |
| 1784 | 377 | m_cqLogged = false; | |
| 1785 | 377 | m_cardCorrNs.store(0, std::memory_order_relaxed); | |
| 1786 | 377 | } | |
| 1787 | |||
| 1788 | 21478 | static int64_t StatusField(const std::string& status, const char* key) { | |
| 1789 | 21478 | std::size_t p = status.find(key); | |
| 1790 |
3/6✓ Branch 0 taken 21478 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 21478 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 21478 times.
|
21478 | if (p == std::string::npos || (p = status.find(':', p)) == std::string::npos) { |
| 1791 | ✗ | return INT64_MIN; | |
| 1792 | } | ||
| 1793 | 21478 | return strtoll(status.c_str() + p + 1, nullptr, 10); | |
| 1794 | } | ||
| 1795 | |||
| 1796 | 108558 | int PipeWireOutputStream::MeasureCardQueue(uint64_t nowMs) { | |
| 1797 | 108558 | std::string path; | |
| 1798 | int rate; | ||
| 1799 | { | ||
| 1800 |
1/2✓ Branch 0 taken 108558 times.
✗ Branch 1 not taken.
|
108558 | std::unique_lock<std::mutex> lock(m_lock); |
| 1801 |
7/8✓ Branch 0 taken 102495 times.
✓ Branch 1 taken 6063 times.
✓ Branch 2 taken 31304 times.
✓ Branch 3 taken 71191 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 31304 times.
✓ Branch 6 taken 77254 times.
✓ Branch 7 taken 31304 times.
|
108558 | if (m_state != State::Ready || m_cardStatusPath.empty() || !m_pwStream) { |
| 1802 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 77251 times.
|
77254 | if (!m_cqPath.empty()) { |
| 1803 | 3 | m_cqPath.clear(); | |
| 1804 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | ResetCardQueue(); |
| 1805 | } | ||
| 1806 | 77254 | return INT_MAX; | |
| 1807 | } | ||
| 1808 |
1/2✓ Branch 0 taken 31304 times.
✗ Branch 1 not taken.
|
31304 | path = m_cardStatusPath; |
| 1809 | 31304 | rate = m_rate; | |
| 1810 |
2/2✓ Branch 0 taken 31304 times.
✓ Branch 1 taken 77254 times.
|
108558 | } |
| 1811 |
2/2✓ Branch 0 taken 29 times.
✓ Branch 1 taken 31275 times.
|
31304 | if (path != m_cqPath) { |
| 1812 |
1/2✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 | m_cqPath = path; |
| 1813 |
1/2✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 | ResetCardQueue(); |
| 1814 | } | ||
| 1815 |
2/2✓ Branch 0 taken 20576 times.
✓ Branch 1 taken 10728 times.
|
31304 | if (nowMs < m_cqNextMs) { |
| 1816 | 20576 | return (int)(m_cqNextMs - nowMs); | |
| 1817 | } | ||
| 1818 | // When to read it next: soon while its queue settles, which is from the | ||
| 1819 | // moment it is seen to start until a few seconds after. | ||
| 1820 | 10722 | auto next = [this, nowMs]() { | |
| 1821 |
4/4✓ Branch 0 taken 10650 times.
✓ Branch 1 taken 72 times.
✓ Branch 2 taken 3467 times.
✓ Branch 3 taken 7183 times.
|
10722 | bool settling = !m_cqOffsetValid || nowMs - m_cqStartMs < kCardQueueSettleMs; |
| 1822 |
2/2✓ Branch 0 taken 3539 times.
✓ Branch 1 taken 7183 times.
|
10722 | int ms = settling ? kCardQueueFastMs + (int)(random() % (kCardQueueJitterMs + 1)) : kCardQueueSampleMs; |
| 1823 | 10722 | m_cqNextMs = nowMs + ms; | |
| 1824 | 10722 | return ms; | |
| 1825 | 10728 | }; | |
| 1826 | |||
| 1827 | 10728 | uint64_t countBefore = m_cycleCount.load(std::memory_order_acquire); | |
| 1828 | struct timespec ts; | ||
| 1829 | 10728 | clock_gettime(CLOCK_MONOTONIC, &ts); | |
| 1830 | 10728 | int64_t before = (int64_t)ts.tv_sec * GST_SECOND + ts.tv_nsec; | |
| 1831 |
1/2✓ Branch 0 taken 10728 times.
✗ Branch 1 not taken.
|
10728 | std::string status = GetFileContents(path); |
| 1832 | 10728 | clock_gettime(CLOCK_MONOTONIC, &ts); | |
| 1833 | 10728 | int64_t after = (int64_t)ts.tv_sec * GST_SECOND + ts.tv_nsec; | |
| 1834 | 10728 | uint64_t count = m_cycleCount.load(std::memory_order_acquire); | |
| 1835 | |||
| 1836 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 10722 times.
|
10728 | if (status.find("state: RUNNING") == std::string::npos) { |
| 1837 | // Stopped, or not started yet: when it starts its queue starts over. | ||
| 1838 |
5/6✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 1 times.
|
6 | if (!m_cqTrigger.empty() || m_cqOffsetValid) { |
| 1839 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | ResetCardQueue(); |
| 1840 | } | ||
| 1841 | 6 | m_cqNextMs = nowMs + kCardQueueSampleMs; | |
| 1842 | 6 | return kCardQueueSampleMs; | |
| 1843 | } | ||
| 1844 | 10722 | std::string trigger; | |
| 1845 | 10722 | std::size_t tp = status.find("trigger_time"); | |
| 1846 |
3/6✓ Branch 0 taken 10722 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10722 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10722 times.
✗ Branch 5 not taken.
|
10722 | if (tp != std::string::npos && (tp = status.find(':', tp)) != std::string::npos) { |
| 1847 | 10722 | std::size_t e = status.find('\n', tp); | |
| 1848 |
2/4✓ Branch 0 taken 10722 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10722 times.
✗ Branch 3 not taken.
|
10722 | trigger = status.substr(tp + 1, e == std::string::npos ? std::string::npos : e - tp - 1); |
| 1849 |
1/2✓ Branch 0 taken 10722 times.
✗ Branch 1 not taken.
|
10722 | TrimWhiteSpace(trigger); |
| 1850 | } | ||
| 1851 |
2/2✓ Branch 0 taken 34 times.
✓ Branch 1 taken 10688 times.
|
10722 | if (trigger != m_cqTrigger) { |
| 1852 | // (Re)started: a new queue, which starts shallower than it settles. | ||
| 1853 |
1/2✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
|
34 | ResetCardQueue(); |
| 1854 |
1/2✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
|
34 | m_cqTrigger = trigger; |
| 1855 | 34 | m_cqStartMs = nowMs; | |
| 1856 | } | ||
| 1857 |
2/2✓ Branch 0 taken 34 times.
✓ Branch 1 taken 10688 times.
|
10722 | if (m_cqCardRate <= 0) { |
| 1858 |
3/6✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 34 times.
✗ Branch 5 not taken.
|
34 | std::string hwParams = GetFileContents(path.substr(0, path.rfind('/')) + "/hw_params"); |
| 1859 | 34 | int64_t r = StatusField(hwParams, "rate"); | |
| 1860 |
2/4✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
✗ Branch 3 not taken.
|
34 | m_cqCardRate = r > 0 && r < INT32_MAX ? (int)r : 0; |
| 1861 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
|
34 | if (m_cqCardRate <= 0) { |
| 1862 | ✗ | return next(); | |
| 1863 | } | ||
| 1864 |
1/2✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
|
34 | } |
| 1865 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10722 times.
|
10722 | if (m_cqCardRate != rate) { |
| 1866 | // Resampled on the way: card frames aren't stream frames. | ||
| 1867 | ✗ | m_cqNextMs = nowMs + kCardQueueSampleMs; | |
| 1868 | ✗ | return kCardQueueSampleMs; | |
| 1869 | } | ||
| 1870 | 10722 | int64_t appl = StatusField(status, "appl_ptr"); | |
| 1871 | 10722 | int64_t delay = StatusField(status, "delay"); | |
| 1872 |
5/8✓ Branch 0 taken 10722 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10722 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10722 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 96 times.
✓ Branch 7 taken 10626 times.
|
10722 | if (appl == INT64_MIN || delay == INT64_MIN || count == 0 || count != countBefore) { |
| 1873 | 96 | return next(); // a cycle went by while it was read: which one is unclear | |
| 1874 | } | ||
| 1875 | 10626 | CycleRecord& r = m_cycles[(count - 1) % kCycleRecords]; | |
| 1876 | 10626 | uint32_t seq = r.seq.load(std::memory_order_acquire); | |
| 1877 | 10626 | uint32_t outFrames = r.outFrames.load(std::memory_order_relaxed); | |
| 1878 | 10626 | uint64_t outEnd = r.outEnd.load(std::memory_order_relaxed); | |
| 1879 | 21252 | int64_t deviceNs = r.deviceNs.load(std::memory_order_relaxed); | |
| 1880 | std::atomic_thread_fence(std::memory_order_acquire); | ||
| 1881 |
5/10✓ Branch 0 taken 10626 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10626 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10626 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 10626 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 10626 times.
|
21252 | if ((seq & 1) || r.seq.load(std::memory_order_relaxed) != seq || deviceNs == 0 || outFrames == 0) { |
| 1882 | ✗ | return next(); | |
| 1883 | } | ||
| 1884 | |||
| 1885 | // The offset has to agree over a few readings before any is trusted (a | ||
| 1886 | // reading taken as a cycle is written is off by that cycle), and a new | ||
| 1887 | // one is only taken up the same way -- appl_ptr wrapping, or a restart | ||
| 1888 | // that kept its trigger_time. | ||
| 1889 | 10626 | int64_t offset = appl - (int64_t)outEnd; | |
| 1890 |
1/2✓ Branch 0 taken 10626 times.
✗ Branch 1 not taken.
|
10626 | m_cqOffsets.push_back(offset); |
| 1891 |
2/2✓ Branch 0 taken 10524 times.
✓ Branch 1 taken 10626 times.
|
21150 | while (m_cqOffsets.size() > 3) { |
| 1892 | 10524 | m_cqOffsets.pop_front(); | |
| 1893 | } | ||
| 1894 |
4/4✓ Branch 0 taken 10524 times.
✓ Branch 1 taken 102 times.
✓ Branch 2 taken 52 times.
✓ Branch 3 taken 10472 times.
|
10626 | if (!m_cqOffsetValid || offset != m_cqOffset) { |
| 1895 |
7/8✓ Branch 0 taken 86 times.
✓ Branch 1 taken 68 times.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 50 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 36 times.
✓ Branch 6 taken 118 times.
✓ Branch 7 taken 36 times.
|
154 | if (m_cqOffsets.size() < 3 || m_cqOffsets[0] != offset || m_cqOffsets[1] != offset) { |
| 1896 | 118 | return next(); | |
| 1897 | } | ||
| 1898 | 36 | m_cqOffset = offset; | |
| 1899 | 36 | m_cqOffsetValid = true; | |
| 1900 | } | ||
| 1901 | |||
| 1902 | 10508 | int64_t heardNs = before + (after - before) / 2 + (delay - (int64_t)outFrames) * (int64_t)GST_SECOND / m_cqCardRate; | |
| 1903 | 10508 | int64_t corr = heardNs - deviceNs; | |
| 1904 |
2/4✓ Branch 0 taken 10508 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 10508 times.
|
10508 | if (corr > kCardQueueMaxCorrMs * (int64_t)GST_MSECOND || corr < -kCardQueueMaxCorrMs * (int64_t)GST_MSECOND) { |
| 1905 | ✗ | return next(); | |
| 1906 | } | ||
| 1907 |
1/2✓ Branch 0 taken 10508 times.
✗ Branch 1 not taken.
|
10508 | m_cqCorr.push_back(corr); |
| 1908 |
2/2✓ Branch 0 taken 10338 times.
✓ Branch 1 taken 10508 times.
|
20846 | while (m_cqCorr.size() > kCardQueueMedianOf) { |
| 1909 | 10338 | m_cqCorr.pop_front(); | |
| 1910 | } | ||
| 1911 |
1/2✓ Branch 0 taken 10508 times.
✗ Branch 1 not taken.
|
21016 | std::vector<int64_t> sorted(m_cqCorr.begin(), m_cqCorr.end()); |
| 1912 |
1/2✓ Branch 0 taken 10508 times.
✗ Branch 1 not taken.
|
10508 | std::nth_element(sorted.begin(), sorted.begin() + sorted.size() / 2, sorted.end()); |
| 1913 | 10508 | int64_t median = sorted[sorted.size() / 2]; | |
| 1914 | 10508 | m_cardCorrNs.store(median, std::memory_order_relaxed); | |
| 1915 | 10508 | int ms = next(); | |
| 1916 |
4/4✓ Branch 0 taken 3436 times.
✓ Branch 1 taken 7072 times.
✓ Branch 2 taken 34 times.
✓ Branch 3 taken 3402 times.
|
10508 | if (!m_cqLogged && ms == kCardQueueSampleMs) { |
| 1917 | 34 | m_cqLogged = true; | |
| 1918 |
1/2✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
|
34 | LogDebug(VB_MEDIAOUT, "PipeWire output stream %d: the card plays %.1f ms later than PipeWire declares\n", |
| 1919 | m_slot, (double)median / 1e6); | ||
| 1920 | } | ||
| 1921 |
1/2✓ Branch 0 taken 10508 times.
✗ Branch 1 not taken.
|
10508 | LogExcess(VB_MEDIAOUT, "PipeWire output stream %d: card queue correction %+.2f ms (median %+.2f)\n", |
| 1922 | m_slot, (double)corr / 1e6, (double)median / 1e6); | ||
| 1923 | 10508 | return ms; | |
| 1924 | 108558 | } | |
| 1925 | |||
| 1926 | 203 | bool PipeWireOutputStream::CardRunning() { | |
| 1927 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 203 times.
|
203 | if (m_cardStatusPath.empty()) { |
| 1928 | ✗ | return false; | |
| 1929 | } | ||
| 1930 |
1/2✓ Branch 0 taken 203 times.
✗ Branch 1 not taken.
|
203 | return GetFileContents(m_cardStatusPath).find("state: RUNNING") != std::string::npos; |
| 1931 | } | ||
| 1932 | |||
| 1933 | 353 | void PipeWireOutputStream::CloseLocked(const char* why) { | |
| 1934 | #ifdef FPP_NATIVE_PIPEWIRE | ||
| 1935 |
2/2✓ Branch 0 taken 306 times.
✓ Branch 1 taken 47 times.
|
353 | if (m_pwLoop) { |
| 1936 |
1/2✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
|
306 | if (m_pwStream) { |
| 1937 | // Closing when idle is routine -- a playlist that alternates | ||
| 1938 | // clips with sound and without closes and reopens the stream | ||
| 1939 | // at every pass -- so only another reason is worth a line at | ||
| 1940 | // the default level. | ||
| 1941 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 288 times.
|
306 | if (strcmp(why, "idle") == 0) { |
| 1942 | 18 | LogDebug(VB_MEDIAOUT, "PipeWire output stream %d closing (%s)\n", m_slot, why); | |
| 1943 | } else { | ||
| 1944 | 288 | LogInfo(VB_MEDIAOUT, "PipeWire output stream %d closing (%s)\n", m_slot, why); | |
| 1945 | } | ||
| 1946 | } | ||
| 1947 | // Stopping the loop joins its thread, so OnProcess() is done with the | ||
| 1948 | // queue before anything below changes it. | ||
| 1949 | 306 | pw_thread_loop_lock(m_pwLoop); | |
| 1950 |
1/2✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
|
306 | if (m_pwStream) { |
| 1951 | 306 | pw_stream_destroy(m_pwStream); | |
| 1952 | } | ||
| 1953 | 306 | pw_thread_loop_unlock(m_pwLoop); | |
| 1954 | 306 | pw_thread_loop_stop(m_pwLoop); | |
| 1955 | 306 | pw_thread_loop_destroy(m_pwLoop); | |
| 1956 | } | ||
| 1957 | #endif | ||
| 1958 | 353 | m_pwStream = nullptr; | |
| 1959 | 353 | m_pwLoop = nullptr; | |
| 1960 | 353 | m_feedSpans.clear(); | |
| 1961 | 353 | gst_caps_replace(&m_caps, nullptr); | |
| 1962 | 353 | m_rate = 0; | |
| 1963 | 353 | EndTimelineLocked(); | |
| 1964 | 353 | m_continuedFirst = false; | |
| 1965 | 353 | m_flushNow = false; | |
| 1966 | 353 | m_xrunPending = false; | |
| 1967 | // m_feed stays: it belongs to the track using the slot (AttachFeed() / | ||
| 1968 | // Release()), not to this stream. Clearing it here orphaned a track | ||
| 1969 | // whose start triggered a reopen -- a new target -- and the whole track | ||
| 1970 | // played silent. Its samples are dropped while there is no stream. | ||
| 1971 | 353 | m_target.clear(); | |
| 1972 | 353 | m_linked = false; | |
| 1973 | 353 | m_state = State::Closed; | |
| 1974 | 353 | m_stateChanged.notify_all(); | |
| 1975 | 353 | } | |
| 1976 | |||
| 1977 | #endif | ||
| 1978 |