| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of the Falcon Player (FPP) and is Copyright (C) | ||
| 3 | * 2013-2025 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 "fpp-json.h" | ||
| 16 | #include "fpphttp.h" // drogon/HTTP helpers used here; no longer pulled transitively (see fpphttp_types.h) | ||
| 17 | |||
| 18 | #include "Warnings.h" // WarningHolder -- needed directly for NOPCH builds | ||
| 19 | |||
| 20 | #include "AES67Manager.h" | ||
| 21 | #include "GStreamerOut.h" | ||
| 22 | #include "PipeWireGraphConfig.h" | ||
| 23 | |||
| 24 | #ifdef HAS_AES67_GSTREAMER | ||
| 25 | |||
| 26 | #include <gst/gst.h> | ||
| 27 | |||
| 28 | #if __has_include(<samplerate.h>) | ||
| 29 | #include <samplerate.h> | ||
| 30 | #define FPP_HAVE_SAMPLERATE 1 | ||
| 31 | #endif | ||
| 32 | |||
| 33 | #include <arpa/inet.h> | ||
| 34 | #include <fcntl.h> | ||
| 35 | #include <ifaddrs.h> | ||
| 36 | // Only for PhcIndexForInterface(): finding a NIC's hardware PTP clock is a | ||
| 37 | // Linux ethtool query. Without it (macOS) every interface reports no PHC and the | ||
| 38 | // clock falls back to CLOCK_REALTIME, as it does on Linux for software-only NICs. | ||
| 39 | #if __has_include(<linux/ethtool.h>) | ||
| 40 | #define FPP_AES67_HAS_ETHTOOL | ||
| 41 | #include <linux/ethtool.h> | ||
| 42 | #include <linux/sockios.h> | ||
| 43 | #endif | ||
| 44 | #include <net/if.h> | ||
| 45 | #include <netinet/in.h> | ||
| 46 | #include <signal.h> | ||
| 47 | #include <sys/ioctl.h> | ||
| 48 | #include <sys/socket.h> | ||
| 49 | #include <sys/wait.h> | ||
| 50 | #include <unistd.h> | ||
| 51 | #if defined(__linux__) | ||
| 52 | #include <poll.h> | ||
| 53 | #include <sys/syscall.h> | ||
| 54 | #endif | ||
| 55 | |||
| 56 | #include <algorithm> | ||
| 57 | #include <cctype> | ||
| 58 | #include <chrono> | ||
| 59 | #include <cmath> | ||
| 60 | #include <cstdio> | ||
| 61 | #include <cstring> | ||
| 62 | #include <deque> | ||
| 63 | #include <fstream> | ||
| 64 | #include <memory> | ||
| 65 | #include <sstream> | ||
| 66 | |||
| 67 | #include "common.h" | ||
| 68 | #include "log.h" | ||
| 69 | #include "mediaoutput.h" | ||
| 70 | #include "settings.h" | ||
| 71 | #include "../SysSocket.h" | ||
| 72 | |||
| 73 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 74 | // AES67 namespace helpers | ||
| 75 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 76 | namespace AES67 { | ||
| 77 | |||
| 78 | ✗ | const char* GetSDPChannelNames(int channels) { | |
| 79 | ✗ | switch (channels) { | |
| 80 | ✗ | case 1: return "M"; | |
| 81 | ✗ | case 2: return "FL, FR"; | |
| 82 | ✗ | case 4: return "FL, FR, RL, RR"; | |
| 83 | ✗ | case 6: return "FL, FR, FC, LFE, RL, RR"; | |
| 84 | ✗ | case 8: return "FL, FR, FC, LFE, RL, RR, SL, SR"; | |
| 85 | ✗ | default: return ""; | |
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | } // namespace AES67 | ||
| 90 | |||
| 91 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 92 | // Singleton instance | ||
| 93 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 94 | static AES67Manager s_aes67Manager; | ||
| 95 | AES67Manager& AES67Manager::INSTANCE = s_aes67Manager; | ||
| 96 | |||
| 97 | // Set a send pipeline's master volume element (see SetMasterVolume()). | ||
| 98 | 32 | static void SetPipelineMasterVolume(GstElement* pipeline, int volume) { | |
| 99 | 32 | GstElement* vol = gst_bin_get_by_name(GST_BIN(pipeline), "mastervol"); | |
| 100 |
1/2✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
|
32 | if (vol) { |
| 101 | 32 | g_object_set(vol, "volume", masterVolumeGain(volume), nullptr); | |
| 102 | 32 | gst_object_unref(vol); | |
| 103 | } | ||
| 104 | 32 | } | |
| 105 | |||
| 106 |
1/2✓ Branch 0 taken 3252 times.
✗ Branch 1 not taken.
|
3252 | AES67Manager::AES67Manager() { |
| 107 |
2/4✓ Branch 0 taken 3252 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3252 times.
✗ Branch 3 not taken.
|
3252 | m_configPath = getFPPMediaDir("/config/pipewire-aes67-instances.json"); |
| 108 | 3252 | } | |
| 109 | |||
| 110 | 1200 | AES67Manager::~AES67Manager() { | |
| 111 | 1200 | Shutdown(); | |
| 112 | 1200 | } | |
| 113 | |||
| 114 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 115 | // Lifecycle | ||
| 116 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 117 | 5229 | bool AES67Manager::Init() { | |
| 118 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5229 times.
|
5229 | if (m_initialized.load()) { |
| 119 | ✗ | return true; | |
| 120 | } | ||
| 121 | |||
| 122 | // AES67 uses pipewiresrc/pipewiresink which require the full PipeWire graph. | ||
| 123 | // Only proceed when MediaBackend is "pipewire" (the advanced PipeWire mode). | ||
| 124 | // - ALSA / no backend: PipeWire daemon is not running; gst_parse_launch with | ||
| 125 | // pipewiresrc crashes in gst_value_deserialize (NULL deref at addr 0x4). | ||
| 126 | // - pipewire-simple: the graph lacks the node connections needed for audio | ||
| 127 | // format negotiation, causing the state change to block indefinitely. | ||
| 128 |
2/4✓ Branch 0 taken 5229 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5229 times.
✗ Branch 3 not taken.
|
5229 | std::string mediaBackend = toLowerCopy(getSetting("MediaBackend")); |
| 129 |
8/10✓ Branch 0 taken 5229 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5215 times.
✓ Branch 3 taken 14 times.
✓ Branch 4 taken 5215 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 5106 times.
✓ Branch 7 taken 109 times.
✓ Branch 8 taken 5120 times.
✓ Branch 9 taken 109 times.
|
5229 | if (!isPipeWireBackend() || mediaBackend != "pipewire") { |
| 130 |
1/2✓ Branch 0 taken 5120 times.
✗ Branch 1 not taken.
|
5120 | LogDebug(VB_MEDIAOUT, "AES67Manager: MediaBackend='%s' (need 'pipewire'), skipping AES67 init\n", |
| 131 | mediaBackend.c_str()); | ||
| 132 | 5120 | return true; | |
| 133 | } | ||
| 134 | |||
| 135 | // Check if the config file exists | ||
| 136 |
3/4✓ Branch 0 taken 109 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 73 times.
|
109 | if (!FileExists(m_configPath)) { |
| 137 |
1/2✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
|
36 | LogDebug(VB_MEDIAOUT, "AES67Manager: No config file at %s, skipping init\n", |
| 138 | m_configPath.c_str()); | ||
| 139 | 36 | return true; // not an error — just no AES67 configured | |
| 140 | } | ||
| 141 | |||
| 142 | // Shared with the playback path and the video managers so gst_init() | ||
| 143 | // happens exactly once, under one lock, whichever subsystem gets here | ||
| 144 | // first. | ||
| 145 |
1/2✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
|
73 | GStreamerOutput::EnsureGStreamerInit(); |
| 146 | |||
| 147 | // Set PipeWire env vars so pipewiresrc/pipewiresink can find the FPP PipeWire runtime | ||
| 148 | 73 | setenv("PIPEWIRE_RUNTIME_DIR", "/run/pipewire-fpp", 0); | |
| 149 | 73 | setenv("XDG_RUNTIME_DIR", "/run/pipewire-fpp", 0); | |
| 150 | 73 | setenv("PULSE_RUNTIME_PATH", "/run/pipewire-fpp/pulse", 0); | |
| 151 | |||
| 152 | 73 | m_initialized.store(true); | |
| 153 |
1/2✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
|
73 | LogInfo(VB_MEDIAOUT, "AES67Manager: Initialized\n"); |
| 154 | 73 | return true; | |
| 155 | 5229 | } | |
| 156 | |||
| 157 | 3220 | void AES67Manager::Shutdown() { | |
| 158 |
2/2✓ Branch 0 taken 3147 times.
✓ Branch 1 taken 73 times.
|
3220 | if (!m_initialized.load()) { |
| 159 | 3147 | return; | |
| 160 | } | ||
| 161 | |||
| 162 |
1/2✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
|
73 | LogInfo(VB_MEDIAOUT, "AES67Manager: Shutting down\n"); |
| 163 | |||
| 164 | // Nothing may apply from here on, including a watchdog rebuild that is | ||
| 165 | // already scheduled: ApplyConfig() would re-Init() and bring back the | ||
| 166 | // pipelines this is about to tear down. | ||
| 167 | 73 | m_stopping.store(true); | |
| 168 | 73 | m_applyGeneration++; | |
| 169 | 73 | m_initialized.store(false); | |
| 170 | |||
| 171 | // Join any in-flight watchdog rebuild thread FIRST, before touching | ||
| 172 | // m_sapAnnounceThread/m_sapRecvThread. The rebuild thread internally | ||
| 173 | // calls ApplyConfig(), which itself joins those two threads -- joining | ||
| 174 | // them here concurrently with the rebuild thread would be a double | ||
| 175 | // join on the same std::thread object (undefined behavior). Taken out | ||
| 176 | // under m_rebuildMutex: the watchdog thread assigns it. | ||
| 177 | 73 | std::thread rebuild; | |
| 178 | { | ||
| 179 |
1/2✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
|
73 | std::lock_guard<std::mutex> rl(m_rebuildMutex); |
| 180 | 73 | rebuild = std::move(m_rebuildThread); | |
| 181 | 73 | } | |
| 182 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 67 times.
|
73 | if (rebuild.joinable()) { |
| 183 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | rebuild.join(); |
| 184 | } | ||
| 185 | |||
| 186 | // Only now take the apply lock -- m_rebuildThread calls ApplyConfig(), | ||
| 187 | // so holding this across the join above would deadlock. Taking it here | ||
| 188 | // still blocks until any in-flight ApplyConfig() (e.g. from a command | ||
| 189 | // thread) has finished, so we never tear down threads it is mid-rebuild. | ||
| 190 |
1/2✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
|
73 | std::lock_guard<std::mutex> applyLock(m_applyMutex); |
| 191 | |||
| 192 | // Stop SAP threads | ||
| 193 |
1/2✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
|
73 | StopWorkerThreads(); |
| 194 | |||
| 195 |
1/2✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
|
73 | StopAllPipelines(); |
| 196 |
1/2✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
|
73 | ShutdownPTP(); |
| 197 |
1/2✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
|
73 | ReleaseMediaClock(); |
| 198 | |||
| 199 | 73 | m_active.store(false); | |
| 200 |
1/2✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
|
73 | LogInfo(VB_MEDIAOUT, "AES67Manager: Shutdown complete\n"); |
| 201 | 73 | } | |
| 202 | |||
| 203 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 204 | // Config loading — reads pipewire-aes67-instances.json | ||
| 205 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 206 | // Accessors -- see m_configMutex. Each copies out under the lock so callers | ||
| 207 | // never hold a reference into a vector or string LoadConfig() may reallocate. | ||
| 208 | 1443 | AES67Config AES67Manager::GetConfigSnapshot() { | |
| 209 |
1/2✓ Branch 0 taken 1443 times.
✗ Branch 1 not taken.
|
1443 | std::lock_guard<std::mutex> lock(m_configMutex); |
| 210 |
1/2✓ Branch 0 taken 1443 times.
✗ Branch 1 not taken.
|
2886 | return m_config; |
| 211 | 1443 | } | |
| 212 | |||
| 213 | 1817 | bool AES67Manager::IsPtpEnabled() { | |
| 214 |
1/2✓ Branch 0 taken 1817 times.
✗ Branch 1 not taken.
|
1817 | std::lock_guard<std::mutex> lock(m_configMutex); |
| 215 | 1817 | return m_config.ptpEnabled; | |
| 216 | 1817 | } | |
| 217 | |||
| 218 | 77 | int AES67Manager::GetPtpDomain() { | |
| 219 |
1/2✓ Branch 0 taken 77 times.
✗ Branch 1 not taken.
|
77 | std::lock_guard<std::mutex> lock(m_configMutex); |
| 220 | 77 | return m_config.ptpDomain; | |
| 221 | 77 | } | |
| 222 | |||
| 223 | 1760 | std::string AES67Manager::GetPtpInterface() { | |
| 224 |
1/2✓ Branch 0 taken 1760 times.
✗ Branch 1 not taken.
|
1760 | std::lock_guard<std::mutex> lock(m_configMutex); |
| 225 |
1/2✓ Branch 0 taken 1760 times.
✗ Branch 1 not taken.
|
3520 | return m_config.ptpInterface; |
| 226 | 1760 | } | |
| 227 | |||
| 228 | // cfg80211 drivers expose phy80211, older wext drivers expose wireless/ | ||
| 229 | 1 | static bool IsWirelessInterface(const std::string& iface) { | |
| 230 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | std::string base = "/sys/class/net/" + iface; |
| 231 |
3/8✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
3 | return access((base + "/wireless").c_str(), F_OK) == 0 || |
| 232 |
3/8✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
3 | access((base + "/phy80211").c_str(), F_OK) == 0; |
| 233 | 1 | } | |
| 234 | |||
| 235 | // A blank ptpInterface used to be offered by the web page as "(Default)", but | ||
| 236 | // nothing downstream can act on it: ptp4l is exec'd with -i "" and exits | ||
| 237 | // immediately, and GetInterfaceIP("") returns whichever address getifaddrs | ||
| 238 | // hands back first -- on a Wi-Fi equipped Pi often wlan0, which cannot hold a | ||
| 239 | // PTP lock. The page no longer offers the blank, but configs written by | ||
| 240 | // older builds still carry one, so resolve it to a wired interface that has | ||
| 241 | // an IPv4 address, preferring eth0. | ||
| 242 | 1 | static std::string FirstWiredInterface() { | |
| 243 | 1 | struct ifaddrs* addrs = nullptr; | |
| 244 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (getifaddrs(&addrs) != 0) { |
| 245 | ✗ | return "eth0"; | |
| 246 | } | ||
| 247 | |||
| 248 | 1 | std::string first; | |
| 249 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | for (struct ifaddrs* ifa = addrs; ifa; ifa = ifa->ifa_next) { |
| 250 |
3/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
|
5 | if (!ifa->ifa_addr || ifa->ifa_addr->sa_family != AF_INET) continue; |
| 251 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | std::string name = ifa->ifa_name; |
| 252 |
7/10✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 1 times.
✓ Branch 9 taken 1 times.
|
2 | if (name == "lo" || IsWirelessInterface(name)) continue; |
| 253 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | if (name == "eth0") { |
| 254 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | first = name; |
| 255 | 1 | break; | |
| 256 | } | ||
| 257 | ✗ | if (first.empty()) first = name; | |
| 258 |
2/3✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
|
2 | } |
| 259 | 1 | freeifaddrs(addrs); | |
| 260 |
3/10✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
|
1 | return first.empty() ? "eth0" : first; |
| 261 | 1 | } | |
| 262 | |||
| 263 | |||
| 264 | // Values that go into a gst_parse_launch() description unquoted. Anything | ||
| 265 | // outside these characters is refused at load: a space or '!' in a | ||
| 266 | // destination address re-parses as extra pipeline elements (a udpsrc feeding | ||
| 267 | // a filesink is a root file write), and no real address or interface name | ||
| 268 | // needs them. | ||
| 269 | 203 | static bool IsPipelineSafeHost(const std::string& host) { | |
| 270 |
3/6✓ Branch 0 taken 203 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 203 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 203 times.
|
203 | if (host.empty() || host.size() > 253) { |
| 271 | ✗ | return false; | |
| 272 | } | ||
| 273 |
2/2✓ Branch 0 taken 1827 times.
✓ Branch 1 taken 203 times.
|
2030 | for (unsigned char c : host) { |
| 274 |
3/8✓ Branch 0 taken 609 times.
✓ Branch 1 taken 1218 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 609 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
1827 | if (!std::isalnum(c) && c != '.' && c != '-' && c != ':') { |
| 275 | ✗ | return false; | |
| 276 | } | ||
| 277 | } | ||
| 278 | 203 | return true; | |
| 279 | } | ||
| 280 | 203 | static bool IsPipelineSafeInterface(const std::string& iface) { | |
| 281 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 203 times.
|
203 | if (iface.size() > 15) { // IFNAMSIZ - 1 |
| 282 | ✗ | return false; | |
| 283 | } | ||
| 284 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 203 times.
|
224 | for (unsigned char c : iface) { |
| 285 |
1/10✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
|
21 | if (!std::isalnum(c) && c != '.' && c != '-' && c != '_' && c != '@') { |
| 286 | ✗ | return false; | |
| 287 | } | ||
| 288 | } | ||
| 289 | 203 | return true; | |
| 290 | } | ||
| 291 | |||
| 292 | // A top-level setting of the wrong JSON type ("ptpEnabled": "yes") threw from | ||
| 293 | // LoadConfig() to std::terminate, at startup and again on every restart. Such | ||
| 294 | // a setting is reported and left at its default. | ||
| 295 | template <typename T> | ||
| 296 | 7824 | static T ConfigField(const Json::Value& root, const char* key, const T& def, T (Json::Value::*as)() const) { | |
| 297 | try { | ||
| 298 |
4/6✓ Branch 0 taken 3912 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3912 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3910 times.
✓ Branch 5 taken 2 times.
|
7832 | return (root.get(key, Json::Value(def)).*as)(); |
| 299 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
4 | } catch (const std::exception& e) { |
| 300 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
4 | WarningHolder::InvalidConfigEntry("pipewire-aes67-instances.json", |
| 301 |
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.
|
12 | std::string(key) + ": " + e.what()); |
| 302 |
0/2✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
4 | return def; |
| 303 | } | ||
| 304 | } | ||
| 305 | |||
| 306 | 164 | bool AES67Manager::LoadConfig() { | |
| 307 |
1/2✓ Branch 0 taken 164 times.
✗ Branch 1 not taken.
|
164 | Json::Value root; |
| 308 |
3/4✓ Branch 0 taken 164 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 163 times.
|
164 | if (!LoadJsonFromFile(m_configPath, root, JsonRoot::Object)) { |
| 309 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogWarn(VB_MEDIAOUT, "AES67Manager: Failed to load config from %s\n", |
| 310 | m_configPath.c_str()); | ||
| 311 | 1 | return false; | |
| 312 | } | ||
| 313 | |||
| 314 | // Parse into a local config and publish it in one locked swap at the end. | ||
| 315 | // Filling m_config in place would let a status query on another thread | ||
| 316 | // observe a half-built instance list, or iterate the vector while | ||
| 317 | // push_back() reallocates it. | ||
| 318 | // Every fallback below is the member's own default rather than a literal. | ||
| 319 | // Spelling them out twice does not stay in step: nativeSourceRate and | ||
| 320 | // sourceBufferCopy were changed to false in the struct and kept a literal | ||
| 321 | // true here, so this function -- which always runs -- quietly put them | ||
| 322 | // back. The shipped behaviour was then the opposite of both the struct | ||
| 323 | // and the commit message that changed it. | ||
| 324 |
1/2✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
|
163 | AES67Config cfg; |
| 325 |
3/4✓ Branch 0 taken 72 times.
✓ Branch 1 taken 91 times.
✓ Branch 2 taken 72 times.
✗ Branch 3 not taken.
|
163 | static const AES67Config kDefault; |
| 326 |
1/2✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
|
163 | cfg.ptpEnabled = ConfigField(root, "ptpEnabled", kDefault.ptpEnabled, &Json::Value::asBool); |
| 327 |
1/2✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
|
163 | cfg.ptpInterface = ConfigField(root, "ptpInterface", kDefault.ptpInterface, &Json::Value::asString); |
| 328 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 162 times.
|
163 | if (cfg.ptpInterface.empty()) { |
| 329 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | cfg.ptpInterface = FirstWiredInterface(); |
| 330 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogInfo(VB_MEDIAOUT, "AES67Manager: no PTP interface configured, using %s\n", |
| 331 | cfg.ptpInterface.c_str()); | ||
| 332 | } | ||
| 333 |
1/2✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
|
163 | cfg.ptpDomain = ConfigField(root, "ptpDomain", kDefault.ptpDomain, &Json::Value::asInt); |
| 334 |
1/2✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
|
163 | cfg.ptpRole = ConfigField(root, "ptpRole", kDefault.ptpRole, &Json::Value::asString); |
| 335 |
1/2✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
|
163 | cfg.ptpLockWaitMs = ConfigField(root, "ptpLockWaitMs", kDefault.ptpLockWaitMs, &Json::Value::asInt); |
| 336 | // The wait runs inside ApplyConfig(), which holds the apply lock and, at | ||
| 337 | // boot, fppd's startup; Shutdown() has to wait it out too. Anything past | ||
| 338 | // half a minute is not a wait for lock any more. | ||
| 339 | static constexpr int MAX_PTP_LOCK_WAIT_MS = 30000; | ||
| 340 |
2/4✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 163 times.
|
163 | if (cfg.ptpLockWaitMs < 0 || cfg.ptpLockWaitMs > MAX_PTP_LOCK_WAIT_MS) { |
| 341 | ✗ | int clamped = cfg.ptpLockWaitMs < 0 ? 0 : MAX_PTP_LOCK_WAIT_MS; | |
| 342 | ✗ | LogWarn(VB_MEDIAOUT, "AES67Manager: ptpLockWaitMs %d out of range, using %d\n", | |
| 343 | cfg.ptpLockWaitMs, clamped); | ||
| 344 | ✗ | cfg.ptpLockWaitMs = clamped; | |
| 345 | } | ||
| 346 |
1/2✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
|
163 | cfg.ptpMediaClock = ConfigField(root, "ptpMediaClock", kDefault.ptpMediaClock, &Json::Value::asBool); |
| 347 |
1/2✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
|
163 | cfg.sourcePacing = ConfigField(root, "sourcePacing", kDefault.sourcePacing, &Json::Value::asBool); |
| 348 |
1/2✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
|
163 | cfg.sinkPacing = ConfigField(root, "sinkPacing", kDefault.sinkPacing, &Json::Value::asBool); |
| 349 |
1/2✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
|
163 | cfg.sinkPacingMs = ConfigField(root, "sinkPacingMs", kDefault.sinkPacingMs, &Json::Value::asInt); |
| 350 |
1/2✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
|
163 | cfg.driftResample = ConfigField(root, "driftResample", kDefault.driftResample, &Json::Value::asBool); |
| 351 |
1/2✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
|
163 | cfg.pipelineStats = ConfigField(root, "pipelineStats", kDefault.pipelineStats, &Json::Value::asBool); |
| 352 | 163 | cfg.nativeSourceRate = | |
| 353 |
1/2✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
|
163 | ConfigField(root, "nativeSourceRate", kDefault.nativeSourceRate, &Json::Value::asBool); |
| 354 | 163 | cfg.sourceBufferCopy = | |
| 355 |
1/2✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
|
163 | ConfigField(root, "sourceBufferCopy", kDefault.sourceBufferCopy, &Json::Value::asBool); |
| 356 | 163 | cfg.sourceMinBuffers = | |
| 357 |
1/2✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
|
163 | ConfigField(root, "sourceMinBuffers", kDefault.sourceMinBuffers, &Json::Value::asInt); |
| 358 | 163 | cfg.splitClockDomains = | |
| 359 |
1/2✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
|
163 | ConfigField(root, "splitClockDomains", kDefault.splitClockDomains, &Json::Value::asBool); |
| 360 |
1/2✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
|
163 | cfg.targetLeadMs = ConfigField(root, "targetLeadMs", kDefault.targetLeadMs, &Json::Value::asInt); |
| 361 | 163 | cfg.sourceSilenceFloor = | |
| 362 |
1/2✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
|
163 | ConfigField(root, "sourceSilenceFloor", kDefault.sourceSilenceFloor, &Json::Value::asBool); |
| 363 | 163 | cfg.sourcePtpGroup = | |
| 364 |
1/2✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
|
163 | ConfigField(root, "sourcePtpGroup", kDefault.sourcePtpGroup, &Json::Value::asBool); |
| 365 | cfg.sourcePtpGroupName = | ||
| 366 |
1/2✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
|
163 | ConfigField(root, "sourcePtpGroupName", kDefault.sourcePtpGroupName, &Json::Value::asString); |
| 367 |
1/2✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
|
163 | cfg.sourceAsync = ConfigField(root, "sourceAsync", kDefault.sourceAsync, &Json::Value::asBool); |
| 368 |
1/2✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
|
163 | cfg.rateMatch = ConfigField(root, "rateMatch", kDefault.rateMatch, &Json::Value::asBool); |
| 369 | 163 | cfg.rateMatchToleranceNs = | |
| 370 | 326 | (guint64)ConfigField(root, "rateMatchToleranceNs", | |
| 371 |
1/2✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
|
163 | (Json::UInt64)kDefault.rateMatchToleranceNs, &Json::Value::asUInt64); |
| 372 |
1/2✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
|
163 | cfg.adaptiveResample = ConfigField(root, "adaptiveResample", false, &Json::Value::asBool); |
| 373 | 163 | cfg.requireGroupSource = | |
| 374 |
1/2✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
|
163 | ConfigField(root, "requireGroupSource", kDefault.requireGroupSource, &Json::Value::asBool); |
| 375 | |||
| 376 | // A domain outside 0-127 is not representable in the PTP header; an | ||
| 377 | // unknown role would silently fall through to the "auto" branch below, | ||
| 378 | // so normalise both here where we can tell the user about it. | ||
| 379 |
3/4✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 162 times.
|
163 | if (cfg.ptpDomain < 0 || cfg.ptpDomain > 127) { |
| 380 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogWarn(VB_MEDIAOUT, "AES67Manager: Invalid PTP domain %d, using %d\n", |
| 381 | cfg.ptpDomain, AES67::DEFAULT_PTP_DOMAIN); | ||
| 382 | 1 | cfg.ptpDomain = AES67::DEFAULT_PTP_DOMAIN; | |
| 383 | } | ||
| 384 |
8/10✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 157 times.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 4 times.
✓ Branch 8 taken 1 times.
✓ Branch 9 taken 162 times.
|
165 | if (cfg.ptpRole != "auto" && cfg.ptpRole != "follower" && |
| 385 |
3/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
|
2 | cfg.ptpRole != "master") { |
| 386 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogWarn(VB_MEDIAOUT, "AES67Manager: Unknown PTP role '%s', using 'auto'\n", |
| 387 | cfg.ptpRole.c_str()); | ||
| 388 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | cfg.ptpRole = "auto"; |
| 389 | } | ||
| 390 | |||
| 391 |
6/12✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 163 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 163 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 163 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 163 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 163 times.
✗ Branch 11 not taken.
|
163 | if (root.isMember("instances") && root["instances"].isArray()) { |
| 392 |
8/14✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 163 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 163 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 204 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 204 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 367 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 204 times.
✓ Branch 13 taken 163 times.
|
367 | for (const auto& instJson : root["instances"]) { |
| 393 |
1/2✓ Branch 0 taken 204 times.
✗ Branch 1 not taken.
|
204 | AES67Instance inst; |
| 394 | // A value of the wrong JSON type threw from here to | ||
| 395 | // std::terminate, at startup; skip such an instance instead. | ||
| 396 | try { | ||
| 397 |
4/6✓ Branch 0 taken 204 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 204 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 203 times.
✓ Branch 5 taken 1 times.
|
206 | inst.id = instJson.get("id", 0).asInt(); |
| 398 |
3/6✓ Branch 0 taken 203 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 203 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 203 times.
✗ Branch 5 not taken.
|
203 | inst.name = instJson.get("name", "AES67").asString(); |
| 399 |
3/6✓ Branch 0 taken 203 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 203 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 203 times.
✗ Branch 5 not taken.
|
203 | inst.enabled = instJson.get("enabled", true).asBool(); |
| 400 |
3/6✓ Branch 0 taken 203 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 203 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 203 times.
✗ Branch 5 not taken.
|
203 | inst.mode = instJson.get("mode", "send").asString(); |
| 401 |
3/6✓ Branch 0 taken 203 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 203 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 203 times.
✗ Branch 5 not taken.
|
203 | inst.multicastIP = instJson.get("multicastIP", AES67::DEFAULT_MULTICAST_IP).asString(); |
| 402 |
3/6✓ Branch 0 taken 203 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 203 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 203 times.
✗ Branch 5 not taken.
|
203 | inst.port = instJson.get("port", AES67::DEFAULT_PORT).asInt(); |
| 403 |
3/6✓ Branch 0 taken 203 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 203 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 203 times.
✗ Branch 5 not taken.
|
203 | inst.channels = instJson.get("channels", AES67::DEFAULT_CHANNELS).asInt(); |
| 404 |
3/6✓ Branch 0 taken 203 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 203 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 203 times.
✗ Branch 5 not taken.
|
203 | inst.interface = instJson.get("interface", "").asString(); |
| 405 |
3/6✓ Branch 0 taken 203 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 203 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 203 times.
✗ Branch 5 not taken.
|
203 | inst.sessionName = instJson.get("sessionName", inst.name).asString(); |
| 406 |
3/6✓ Branch 0 taken 203 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 203 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 203 times.
✗ Branch 5 not taken.
|
203 | inst.latency = instJson.get("latency", AES67::DEFAULT_LATENCY_MS).asInt(); |
| 407 |
3/6✓ Branch 0 taken 203 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 203 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 203 times.
✗ Branch 5 not taken.
|
203 | inst.sapEnabled = instJson.get("sapEnabled", true).asBool(); |
| 408 |
3/6✓ Branch 0 taken 203 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 203 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 203 times.
✗ Branch 5 not taken.
|
203 | inst.ptime = instJson.get("ptime", AES67::DEFAULT_PTIME_MS).asInt(); |
| 409 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | } catch (const std::exception& e) { |
| 410 |
3/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
3 | WarningHolder::InvalidConfigEntry("pipewire-aes67-instances.json", e.what()); |
| 411 | 1 | continue; | |
| 412 | 1 | } | |
| 413 | |||
| 414 | // Validate ptime | ||
| 415 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 203 times.
|
203 | if (!AES67::IsValidPtime(inst.ptime)) { |
| 416 | ✗ | inst.ptime = AES67::DEFAULT_PTIME_MS; | |
| 417 | } | ||
| 418 | |||
| 419 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 200 times.
|
203 | if (inst.channels > AES67::MAX_SUPPORTED_CHANNELS) { |
| 420 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | LogWarn(VB_MEDIAOUT, |
| 421 | "AES67Manager: instance '%s' asks for %d channels, but the " | ||
| 422 | "audio graph only carries %d -- the stream would not start " | ||
| 423 | "at all. Using %d.\n", | ||
| 424 | inst.name.c_str(), inst.channels, | ||
| 425 | AES67::MAX_SUPPORTED_CHANNELS, AES67::MAX_SUPPORTED_CHANNELS); | ||
| 426 | 3 | inst.channels = AES67::MAX_SUPPORTED_CHANNELS; | |
| 427 | } | ||
| 428 | |||
| 429 | // ...then against the channel count. These are independent | ||
| 430 | // dropdowns in the UI with no interlock, and most of their | ||
| 431 | // combinations describe a packet that cannot be sent: 4ms of L24 | ||
| 432 | // is 2304 bytes at 4 channels and 4608 at 8, against a 1440-byte | ||
| 433 | // limit. Left alone the payloader quietly splits each one, so | ||
| 434 | // the receiver gets several short packets per ptime instead of | ||
| 435 | // the single one a=ptime promises. Clamping is better than | ||
| 436 | // failing -- the stream stays up and stays legal -- but it is a | ||
| 437 | // config error, so say so at a level the user will see. | ||
| 438 | 203 | int maxPtime = AES67::MaxPtimeForChannels(inst.channels); | |
| 439 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 200 times.
|
203 | if (inst.ptime > maxPtime) { |
| 440 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | LogWarn(VB_MEDIAOUT, |
| 441 | "AES67Manager: instance '%s' asks for %dms ptime at %d channels, " | ||
| 442 | "which is %d bytes of L24 -- over the %d-byte packet limit. " | ||
| 443 | "Using %dms instead.\n", | ||
| 444 | inst.name.c_str(), inst.ptime, inst.channels, | ||
| 445 | AES67::L24PayloadBytes(inst.ptime, inst.channels), | ||
| 446 | AES67::MAX_RTP_PACKET_BYTES, maxPtime); | ||
| 447 | 3 | inst.ptime = maxPtime; | |
| 448 | } | ||
| 449 | |||
| 450 |
3/6✓ Branch 0 taken 203 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 203 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 203 times.
|
203 | if (!IsPipelineSafeHost(inst.multicastIP) || !IsPipelineSafeInterface(inst.interface)) { |
| 451 | ✗ | LogErr(VB_MEDIAOUT, "AES67Manager: instance '%s' has an invalid address or interface, skipping it\n", | |
| 452 | inst.name.c_str()); | ||
| 453 | ✗ | WarningHolder::AddWarningTimeout(300, AES67::WARNING_ID_PIPELINE, "AES67: stream '" + inst.name + | |
| 454 | ✗ | "' has an invalid address or interface and was not started"); | |
| 455 | ✗ | continue; | |
| 456 | } | ||
| 457 | |||
| 458 |
1/2✓ Branch 0 taken 203 times.
✗ Branch 1 not taken.
|
203 | cfg.instances.push_back(inst); |
| 459 |
2/2✓ Branch 0 taken 203 times.
✓ Branch 1 taken 1 times.
|
204 | } |
| 460 | } | ||
| 461 | |||
| 462 | // These flags are not independent, and a partial set fails quietly in ways | ||
| 463 | // that look like a different bug entirely. A tester enabling only | ||
| 464 | // splitClockDomains and sourceSilenceFloor reported a stream bursting a | ||
| 465 | // whole PipeWire quantum at a time (23 packets back-to-back every 23.22ms) | ||
| 466 | // -- the payload, sequence and timestamps were all perfect, so nothing | ||
| 467 | // pointed at the config. Say so at load rather than let the measurement | ||
| 468 | // be blamed. | ||
| 469 |
3/4✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 160 times.
|
163 | if (cfg.sourceSilenceFloor && !cfg.driftResample) { |
| 470 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | LogWarn(VB_MEDIAOUT, |
| 471 | "AES67Manager: sourceSilenceFloor is set but driftResample is not, " | ||
| 472 | "so it does nothing -- the gap fill runs inside the drift probe. " | ||
| 473 | "Set driftResample too.\n"); | ||
| 474 | } | ||
| 475 |
3/4✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 162 times.
|
163 | if (cfg.splitClockDomains && !cfg.ptpMediaClock) { |
| 476 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogWarn(VB_MEDIAOUT, |
| 477 | "AES67Manager: splitClockDomains is set but ptpMediaClock is not. " | ||
| 478 | "Without a PTP media clock there is nothing to split, so the whole " | ||
| 479 | "block is skipped -- no PTP anchor and no lead servo. " | ||
| 480 | "ptpMediaClock defaults on, so something has turned it off.\n"); | ||
| 481 | } | ||
| 482 |
3/4✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 81 times.
✓ Branch 3 taken 82 times.
|
163 | if (cfg.splitClockDomains && !cfg.ptpEnabled) { |
| 483 |
1/2✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
|
81 | LogWarn(VB_MEDIAOUT, |
| 484 | "AES67Manager: splitClockDomains is set but PTP is disabled, so the " | ||
| 485 | "media clock falls back to an undisciplined system clock and the RTP " | ||
| 486 | "timestamps mean nothing to a receiver.\n"); | ||
| 487 | } | ||
| 488 |
3/4✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 162 times.
|
163 | if (cfg.splitClockDomains && !cfg.sinkPacing) { |
| 489 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogWarn(VB_MEDIAOUT, |
| 490 | "AES67Manager: splitClockDomains is set but sinkPacing is not. " | ||
| 491 | "The sink will transmit a whole quantum at once instead of pacing " | ||
| 492 | "it, so receivers see bursts rather than one packet per ptime. " | ||
| 493 | "Set sinkPacing too.\n"); | ||
| 494 | } | ||
| 495 | #ifdef FPP_HAVE_SAMPLERATE | ||
| 496 | // The rate trim (pitch) and driftResample correct the same drift, and the | ||
| 497 | // send pipeline can't be built with both. driftResample is the one that | ||
| 498 | // works (see AES67Config::adaptiveResample), so keep it. | ||
| 499 |
4/4✓ Branch 0 taken 3 times.
✓ Branch 1 taken 160 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
|
163 | if (cfg.adaptiveResample && cfg.driftResample) { |
| 500 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogWarn(VB_MEDIAOUT, |
| 501 | "AES67Manager: adaptiveResample and driftResample are both set; " | ||
| 502 | "driftResample already corrects the rate, so adaptiveResample is ignored\n"); | ||
| 503 | 1 | cfg.adaptiveResample = false; | |
| 504 | } | ||
| 505 | #endif | ||
| 506 | |||
| 507 |
3/4✓ Branch 0 taken 82 times.
✓ Branch 1 taken 81 times.
✓ Branch 2 taken 163 times.
✗ Branch 3 not taken.
|
163 | LogInfo(VB_MEDIAOUT, "AES67Manager: Loaded config with %d instances, PTP=%s interface=%s domain=%d role=%s\n", |
| 508 | (int)cfg.instances.size(), | ||
| 509 | cfg.ptpEnabled ? "enabled" : "disabled", | ||
| 510 | cfg.ptpInterface.c_str(), | ||
| 511 | cfg.ptpDomain, | ||
| 512 | cfg.ptpRole.c_str()); | ||
| 513 | |||
| 514 | { | ||
| 515 |
1/2✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
|
163 | std::lock_guard<std::mutex> lock(m_configMutex); |
| 516 | 163 | m_config = std::move(cfg); | |
| 517 | 163 | } | |
| 518 | 163 | return true; | |
| 519 | 164 | } | |
| 520 | |||
| 521 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 522 | // ApplyConfig — called from PHP API and boot sequence | ||
| 523 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 524 | // Retract the pipeline warnings for the kinds of stream that are no longer | ||
| 525 | // failing. CreateSendPipeline()/CreateRecvPipeline() raise these, and nothing | ||
| 526 | // used to take them back: a warning with no timeout stays in the banner until | ||
| 527 | // fppd restarts, so a user who fixed the cause (usually routing a group to the | ||
| 528 | // sender) and re-applied successfully was still told the stream had failed. | ||
| 529 | // Called from every path that ends with no failing pipeline of that kind -- | ||
| 530 | // including the ones that end with no pipelines at all. | ||
| 531 | 165 | static void ClearAES67PipelineWarnings(bool clearSend, bool clearRecv) { | |
| 532 |
2/2✓ Branch 0 taken 46 times.
✓ Branch 1 taken 119 times.
|
165 | if (clearSend) { |
| 533 |
3/6✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 46 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 46 times.
✗ Branch 5 not taken.
|
184 | WarningHolder::RemoveWarning(AES67::WARNING_ID_PIPELINE, AES67::WARNING_SEND_FAILED); |
| 534 | } | ||
| 535 |
2/2✓ Branch 0 taken 159 times.
✓ Branch 1 taken 6 times.
|
165 | if (clearRecv) { |
| 536 |
3/6✓ Branch 0 taken 159 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 159 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 159 times.
✗ Branch 5 not taken.
|
636 | WarningHolder::RemoveWarning(AES67::WARNING_ID_PIPELINE, AES67::WARNING_RECV_FAILED); |
| 537 | } | ||
| 538 | 165 | } | |
| 539 | |||
| 540 | // Retract the "PTP could not start" warnings. Mirrors | ||
| 541 | // ClearAES67PipelineWarnings() above, and exists for the same reason: these | ||
| 542 | // have no timeout either, so the only thing that ever takes one back is a call | ||
| 543 | // like this one. Without it a user who installed linuxptp, freed the | ||
| 544 | // interface or fixed the config path was still told PTP could not start, for | ||
| 545 | // as long as fppd stayed up. | ||
| 546 | // | ||
| 547 | // Called once ptp4l is confirmed running, and again on the way down -- a | ||
| 548 | // daemon that is not running cannot be failing to start, and InitPTP() raises | ||
| 549 | // whichever of these still applies on the next attempt. | ||
| 550 | 274 | static void ClearAES67PtpStartWarnings() { | |
| 551 |
3/6✓ Branch 0 taken 274 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 274 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 274 times.
✗ Branch 5 not taken.
|
1096 | WarningHolder::RemoveWarning(AES67::WARNING_ID_PTP, AES67::WARNING_PTP_NOT_INSTALLED); |
| 552 |
3/6✓ Branch 0 taken 274 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 274 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 274 times.
✗ Branch 5 not taken.
|
1096 | WarningHolder::RemoveWarning(AES67::WARNING_ID_PTP, AES67::WARNING_PTP_CONF_WRITE); |
| 553 |
3/6✓ Branch 0 taken 274 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 274 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 274 times.
✗ Branch 5 not taken.
|
1096 | WarningHolder::RemoveWarning(AES67::WARNING_ID_PTP, AES67::WARNING_PTP_SPAWN_FAILED); |
| 554 |
3/6✓ Branch 0 taken 274 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 274 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 274 times.
✗ Branch 5 not taken.
|
822 | WarningHolder::RemoveWarning(AES67::WARNING_ID_PTP, AES67::WARNING_PTP_START_FAILED); |
| 555 | 274 | } | |
| 556 | |||
| 557 | 2741 | bool AES67Manager::ApplyConfig() { | |
| 558 | // Serialize against concurrent ApplyConfig()/Shutdown()/Cleanup() calls - | ||
| 559 | // see m_applyMutex. Without this, two callers can both get past the SAP | ||
| 560 | // thread joins below and both reach the std::thread assignments at the | ||
| 561 | // end, where assigning over a joinable thread calls std::terminate(). | ||
| 562 |
1/2✓ Branch 0 taken 2741 times.
✗ Branch 1 not taken.
|
2741 | std::lock_guard<std::mutex> applyLock(m_applyMutex); |
| 563 |
1/2✓ Branch 0 taken 2741 times.
✗ Branch 1 not taken.
|
5482 | return ApplyConfigLocked(); |
| 564 | 2741 | } | |
| 565 | |||
| 566 | 2743 | bool AES67Manager::ApplyConfigLocked() { | |
| 567 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2743 times.
|
2743 | if (m_stopping.load()) { |
| 568 | ✗ | return false; | |
| 569 | } | ||
| 570 | // Any rebuild scheduled before this apply is now redundant. | ||
| 571 | 2743 | m_applyGeneration++; | |
| 572 | |||
| 573 |
2/2✓ Branch 0 taken 2578 times.
✓ Branch 1 taken 165 times.
|
2743 | if (!m_initialized.load()) { |
| 574 |
2/4✓ Branch 0 taken 2578 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2578 times.
|
2578 | if (!Init()) { |
| 575 | ✗ | return false; | |
| 576 | } | ||
| 577 | // Init() may return true but skip initialization (e.g. wrong backend). | ||
| 578 | // If m_initialized is still false, there's nothing to do. | ||
| 579 |
1/2✓ Branch 0 taken 2578 times.
✗ Branch 1 not taken.
|
2578 | if (!m_initialized.load()) { |
| 580 | 2578 | return true; | |
| 581 | } | ||
| 582 | } | ||
| 583 | |||
| 584 | // Stop existing pipelines and SAP threads. ptp4l is left for now: it is | ||
| 585 | // only restarted below if the new config changes what it runs with. | ||
| 586 |
1/2✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
|
165 | StopWorkerThreads(); |
| 587 |
1/2✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
|
165 | StopAllPipelines(); |
| 588 | // Re-resolved below against the new config: the PTP interface (and so the | ||
| 589 | // PHC backing the media clock) may have changed. | ||
| 590 |
1/2✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
|
165 | ReleaseMediaClock(); |
| 591 | |||
| 592 |
3/4✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 164 times.
|
165 | if (!FileExists(m_configPath)) { |
| 593 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogInfo(VB_MEDIAOUT, "AES67Manager: No config file, nothing to apply\n"); |
| 594 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | ShutdownPTP(); |
| 595 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | ClearAES67PipelineWarnings(true, true); |
| 596 | 1 | m_active.store(false); | |
| 597 | 1 | return true; | |
| 598 | } | ||
| 599 | |||
| 600 |
3/4✓ Branch 0 taken 164 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 163 times.
|
164 | if (!LoadConfig()) { |
| 601 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | ShutdownPTP(); |
| 602 | 1 | return false; | |
| 603 | } | ||
| 604 | |||
| 605 | // Count enabled instances | ||
| 606 | 163 | int enabledCount = 0; | |
| 607 |
2/2✓ Branch 0 taken 203 times.
✓ Branch 1 taken 163 times.
|
366 | for (const auto& inst : m_config.instances) { |
| 608 |
2/2✓ Branch 0 taken 197 times.
✓ Branch 1 taken 6 times.
|
203 | if (inst.enabled) enabledCount++; |
| 609 | } | ||
| 610 | |||
| 611 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 159 times.
|
163 | if (enabledCount == 0) { |
| 612 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | LogInfo(VB_MEDIAOUT, "AES67Manager: No enabled instances\n"); |
| 613 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | ShutdownPTP(); |
| 614 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | ClearAES67PipelineWarnings(true, true); |
| 615 | 4 | m_active.store(false); | |
| 616 | 4 | return true; | |
| 617 | } | ||
| 618 | |||
| 619 | // Initialize PTP if enabled. A rebuild -- one stream's error, a watchdog | ||
| 620 | // pass, an Apply that changed only the streams -- keeps a running ptp4l | ||
| 621 | // whose settings are unchanged: restarting it takes every receiver in the | ||
| 622 | // domain off the clock, all of them when this box is the grandmaster. | ||
| 623 |
2/2✓ Branch 0 taken 79 times.
✓ Branch 1 taken 80 times.
|
159 | if (m_config.ptpEnabled) { |
| 624 |
2/6✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 79 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
79 | PtpParams wanted{ m_config.ptpInterface, m_config.ptpDomain, m_config.ptpRole }; |
| 625 |
8/10✓ Branch 0 taken 11 times.
✓ Branch 1 taken 68 times.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 7 times.
✓ Branch 7 taken 4 times.
✓ Branch 8 taken 7 times.
✓ Branch 9 taken 72 times.
|
79 | if (m_ptpInitialized && IsPtp4lRunning() && wanted == m_ptpStartedWith) { |
| 626 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 | LogInfo(VB_MEDIAOUT, "AES67Manager: PTP settings unchanged, keeping ptp4l (PID %d)\n", |
| 627 | (int)m_ptp4lPid); | ||
| 628 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 | AnchorToPtpLock(); |
| 629 | } else { | ||
| 630 |
1/2✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
|
72 | ShutdownPTP(); |
| 631 |
3/4✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 39 times.
|
72 | if (!InitPTP()) { |
| 632 |
1/2✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
|
33 | LogWarn(VB_MEDIAOUT, "AES67Manager: PTP init failed, continuing without PTP clock\n"); |
| 633 | } | ||
| 634 | } | ||
| 635 | 79 | } else { | |
| 636 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | ShutdownPTP(); |
| 637 | } | ||
| 638 | |||
| 639 | // Create pipelines for each enabled instance | ||
| 640 | 159 | bool anySend = false; | |
| 641 | 159 | bool anyRecv = false; | |
| 642 | 159 | bool anySAP = false; | |
| 643 | 159 | bool sendFailed = false; | |
| 644 | 159 | bool recvFailed = false; | |
| 645 | 159 | int failedStarts = 0; | |
| 646 | // Senders held idle because nothing feeds them -- published to | ||
| 647 | // m_deferredSenders below, once the whole pass has run. | ||
| 648 | 159 | std::map<int, std::string> deferred; | |
| 649 | |||
| 650 |
2/2✓ Branch 0 taken 200 times.
✓ Branch 1 taken 159 times.
|
359 | for (const auto& inst : m_config.instances) { |
| 651 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 197 times.
|
200 | if (!inst.enabled) continue; |
| 652 | |||
| 653 |
5/8✓ Branch 0 taken 197 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 186 times.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 11 times.
|
197 | bool wantSend = (inst.mode == "send" || inst.mode == "both"); |
| 654 |
5/8✓ Branch 0 taken 197 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 186 times.
✓ Branch 3 taken 11 times.
✓ Branch 4 taken 186 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 186 times.
|
197 | bool wantRecv = (inst.mode == "receive" || inst.mode == "both"); |
| 655 | |||
| 656 |
2/2✓ Branch 0 taken 186 times.
✓ Branch 1 taken 11 times.
|
197 | if (wantSend) { |
| 657 | // Nothing feeds this sender, so starting it would cost 30 seconds | ||
| 658 | // of blocked apply and end in FAILURE. Hold it instead -- see | ||
| 659 | // AES67Config::requireGroupSource. | ||
| 660 |
2/4✓ Branch 0 taken 186 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 186 times.
✗ Branch 3 not taken.
|
186 | const std::string nodeName = SafeNodeName(inst.name) + "_send"; |
| 661 |
7/8✓ Branch 0 taken 48 times.
✓ Branch 1 taken 138 times.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 47 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 185 times.
|
186 | if (m_config.requireGroupSource && !PipeWireGraphFeedsNode(nodeName)) { |
| 662 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogInfo(VB_MEDIAOUT, |
| 663 | "AES67 send [%d] '%s': nothing in the audio graph feeds %s, " | ||
| 664 | "holding the stream idle. Add it to an Audio Output Group " | ||
| 665 | "and apply that config to start it.\n", | ||
| 666 | inst.id, inst.name.c_str(), nodeName.c_str()); | ||
| 667 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | deferred[inst.id] = |
| 668 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | "Waiting for audio — not a member of any enabled Audio Output Group"; |
| 669 |
3/4✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
✓ Branch 3 taken 154 times.
|
185 | } else if (CreateSendPipeline(inst)) { |
| 670 | 31 | anySend = true; | |
| 671 | } else { | ||
| 672 | 154 | sendFailed = true; | |
| 673 | 154 | failedStarts++; | |
| 674 | } | ||
| 675 | 186 | } | |
| 676 | |||
| 677 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 186 times.
|
197 | if (wantRecv) { |
| 678 |
3/4✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 6 times.
|
11 | if (CreateRecvPipeline(inst)) { |
| 679 | 5 | anyRecv = true; | |
| 680 | } else { | ||
| 681 | 6 | recvFailed = true; | |
| 682 | 6 | failedStarts++; | |
| 683 | } | ||
| 684 | } | ||
| 685 | |||
| 686 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 149 times.
|
197 | if (inst.sapEnabled) { |
| 687 | 48 | anySAP = true; | |
| 688 | } | ||
| 689 | } | ||
| 690 | |||
| 691 | { | ||
| 692 |
1/2✓ Branch 0 taken 159 times.
✗ Branch 1 not taken.
|
159 | std::lock_guard<std::mutex> lock(m_pipelineMutex); |
| 693 | 159 | m_deferredSenders = std::move(deferred); | |
| 694 | 159 | m_failedStarts = failedStarts; | |
| 695 | 159 | } | |
| 696 | |||
| 697 | // Every enabled pipeline of that kind started, so whatever raised the | ||
| 698 | // warning last time has been dealt with. Note this runs after the create | ||
| 699 | // calls that raise it, so a still-failing kind keeps its warning. A held | ||
| 700 | // sender does not count as a failure -- nothing was attempted, and there | ||
| 701 | // is nothing wrong with the stream itself. | ||
| 702 |
1/2✓ Branch 0 taken 159 times.
✗ Branch 1 not taken.
|
159 | ClearAES67PipelineWarnings(!sendFailed, !recvFailed); |
| 703 | |||
| 704 | // Start the drift control loop if anything is sending on the PTP clock | ||
| 705 |
5/6✓ Branch 0 taken 31 times.
✓ Branch 1 taken 128 times.
✓ Branch 2 taken 31 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 30 times.
|
159 | if (anySend && m_config.ptpMediaClock && m_config.adaptiveResample) { |
| 706 | 1 | m_driftRunning.store(true); | |
| 707 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | m_driftThread = std::thread(&AES67Manager::DriftControlLoop, this); |
| 708 | } | ||
| 709 | |||
| 710 | // Start SAP announcer if any send instances have SAP enabled | ||
| 711 |
4/4✓ Branch 0 taken 48 times.
✓ Branch 1 taken 111 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 44 times.
|
159 | if (anySAP && anySend) { |
| 712 | 4 | m_sapAnnounceRunning.store(true); | |
| 713 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | m_sapAnnounceThread = std::thread(&AES67Manager::SAPAnnounceLoop, this); |
| 714 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | LogInfo(VB_MEDIAOUT, "AES67Manager: SAP announcer started\n"); |
| 715 | } | ||
| 716 | |||
| 717 | // Start SAP receiver if any receive instances or SAP enabled | ||
| 718 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 111 times.
|
159 | if (anySAP) { |
| 719 | 48 | m_sapRecvRunning.store(true); | |
| 720 |
1/2✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
48 | m_sapRecvThread = std::thread(&AES67Manager::SAPReceiveLoop, this); |
| 721 |
1/2✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
48 | LogInfo(VB_MEDIAOUT, "AES67Manager: SAP receiver started\n"); |
| 722 | } | ||
| 723 | |||
| 724 | // The pipelines are anchored now, so this is where the step detector's | ||
| 725 | // reference belongs -- taking it here rather than at its first check means | ||
| 726 | // a clock that steps in the next 30 seconds is caught on that check | ||
| 727 | // instead of the one after it. | ||
| 728 |
1/2✓ Branch 0 taken 159 times.
✗ Branch 1 not taken.
|
159 | ResetPtpStepReference(); |
| 729 | |||
| 730 | 159 | m_watchdogRunning.store(true); | |
| 731 |
1/2✓ Branch 0 taken 159 times.
✗ Branch 1 not taken.
|
159 | m_watchdogThread = std::thread(&AES67Manager::WatchdogLoop, this); |
| 732 | |||
| 733 | 159 | m_active.store(true); | |
| 734 |
1/2✓ Branch 0 taken 159 times.
✗ Branch 1 not taken.
|
159 | LogInfo(VB_MEDIAOUT, "AES67Manager: Applied config — %d send, %d receive pipelines\n", |
| 735 | (int)m_sendPipelines.size(), (int)m_recvPipelines.size()); | ||
| 736 | 159 | return true; | |
| 737 | 159 | } | |
| 738 | |||
| 739 | // Stops and joins the drift, SAP and watchdog threads. Caller holds | ||
| 740 | // m_applyMutex. Never call it from one of those threads. | ||
| 741 | 239 | void AES67Manager::StopWorkerThreads() { | |
| 742 | 239 | m_sapAnnounceRunning.store(false); | |
| 743 | 239 | m_sapRecvRunning.store(false); | |
| 744 | 239 | m_driftRunning.store(false); | |
| 745 | 239 | m_watchdogRunning.store(false); | |
| 746 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 238 times.
|
239 | if (m_driftThread.joinable()) { |
| 747 | 1 | m_driftThread.join(); | |
| 748 | } | ||
| 749 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 235 times.
|
239 | if (m_sapAnnounceThread.joinable()) { |
| 750 | 4 | m_sapAnnounceThread.join(); | |
| 751 | } | ||
| 752 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 191 times.
|
239 | if (m_sapRecvThread.joinable()) { |
| 753 | 48 | m_sapRecvThread.join(); | |
| 754 | } | ||
| 755 |
2/2✓ Branch 0 taken 159 times.
✓ Branch 1 taken 80 times.
|
239 | if (m_watchdogThread.joinable()) { |
| 756 | 159 | m_watchdogThread.join(); | |
| 757 | } | ||
| 758 | 239 | } | |
| 759 | |||
| 760 | // Pipeline recovery, ptp4l supervision, the PTP step detector and the | ||
| 761 | // no-lock warning review, every SAP_ANNOUNCE_INTERVAL_S. This used to run | ||
| 762 | // inside the SAP announcer, which only exists when something sends with SAP | ||
| 763 | // on: a receive-only box, or one with SAP turned off, got none of it -- a | ||
| 764 | // dead pipeline or ptp4l stayed dead and the no-lock warning never cleared. | ||
| 765 | 159 | void AES67Manager::WatchdogLoop() { | |
| 766 |
1/2✓ Branch 0 taken 170 times.
✗ Branch 1 not taken.
|
170 | while (m_watchdogRunning.load()) { |
| 767 |
6/6✓ Branch 0 taken 977 times.
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 825 times.
✓ Branch 3 taken 152 times.
✓ Branch 4 taken 825 times.
✓ Branch 5 taken 170 times.
|
995 | for (int i = 0; i < AES67::SAP_ANNOUNCE_INTERVAL_S && m_watchdogRunning.load(); i++) { |
| 768 |
1/2✓ Branch 0 taken 825 times.
✗ Branch 1 not taken.
|
825 | std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 769 | } | ||
| 770 |
2/2✓ Branch 0 taken 152 times.
✓ Branch 1 taken 18 times.
|
170 | if (!m_watchdogRunning.load()) { |
| 771 | 152 | break; | |
| 772 | } | ||
| 773 | // Evaluate both before deciding: PollPipelinesWatchdog() also drains | ||
| 774 | // each pipeline's bus, so short-circuiting past it would leave errors | ||
| 775 | // unread until the next pass. | ||
| 776 | 18 | const bool ptpStepped = CheckPtpWatchdog(); | |
| 777 |
5/6✓ Branch 0 taken 11 times.
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 11 times.
✓ Branch 4 taken 7 times.
✓ Branch 5 taken 11 times.
|
18 | if (PollPipelinesWatchdog() || ptpStepped) { |
| 778 | // ApplyConfig() joins this thread, so the rebuild runs on its own | ||
| 779 | // thread once this one has stopped. | ||
| 780 | 7 | LogWarn(VB_MEDIAOUT, "AES67 watchdog: rebuild needed, scheduling ApplyConfig\n"); | |
| 781 | 7 | m_watchdogRunning.store(false); | |
| 782 | 7 | ScheduleRebuild(); | |
| 783 | 7 | break; | |
| 784 | } | ||
| 785 | } | ||
| 786 | 159 | } | |
| 787 | |||
| 788 | 7 | void AES67Manager::ScheduleRebuild() { | |
| 789 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 | std::lock_guard<std::mutex> rl(m_rebuildMutex); |
| 790 |
5/6✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 6 times.
|
7 | if (m_stopping.load() || m_rebuildRunning.load()) { |
| 791 | // Shutting down, or a rebuild is already on its way. Never join a | ||
| 792 | // running one here: its ApplyConfig() may be waiting to join the | ||
| 793 | // watchdog thread that is calling this. | ||
| 794 | 1 | return; | |
| 795 | } | ||
| 796 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (m_rebuildThread.joinable()) { |
| 797 | ✗ | m_rebuildThread.join(); // finished: returns at once | |
| 798 | } | ||
| 799 | 6 | uint64_t generation = m_applyGeneration.load(); | |
| 800 | 6 | m_rebuildRunning.store(true); | |
| 801 | 6 | m_rebuildThread = std::thread([this, generation]() { | |
| 802 | // Brief delay to let the SAP announce thread finish exiting and | ||
| 803 | // become joinable. | ||
| 804 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 805 | { | ||
| 806 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | std::lock_guard<std::mutex> applyLock(m_applyMutex); |
| 807 | // Nothing to do if anything applied, cleaned up or shut down in | ||
| 808 | // the meantime -- it either did this rebuild's work or undid it | ||
| 809 | // on purpose. | ||
| 810 |
6/6✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 4 times.
|
9 | if (!m_stopping.load() && generation == m_applyGeneration.load()) { |
| 811 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | ApplyConfigLocked(); |
| 812 | } | ||
| 813 | 6 | } | |
| 814 | 6 | m_rebuildRunning.store(false); | |
| 815 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
12 | }); |
| 816 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
|
7 | } |
| 817 | |||
| 818 | 1 | void AES67Manager::Cleanup() { | |
| 819 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogInfo(VB_MEDIAOUT, "AES67Manager: Cleanup\n"); |
| 820 | |||
| 821 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | std::lock_guard<std::mutex> applyLock(m_applyMutex); |
| 822 | // A watchdog rebuild already scheduled must not bring back what this | ||
| 823 | // tears down. | ||
| 824 | 1 | m_applyGeneration++; | |
| 825 | |||
| 826 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | StopWorkerThreads(); |
| 827 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | StopAllPipelines(); |
| 828 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | ShutdownPTP(); |
| 829 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | ReleaseMediaClock(); |
| 830 | |||
| 831 | // Nothing is running any more, so a "failed to start" warning describes a | ||
| 832 | // stream that no longer exists. | ||
| 833 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | ClearAES67PipelineWarnings(true, true); |
| 834 | |||
| 835 | 1 | m_active.store(false); | |
| 836 | 1 | } | |
| 837 | |||
| 838 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 839 | // PTP clock management — uses ptp4l (linuxptp) as an IEEE 1588 PTP daemon | ||
| 840 | // | ||
| 841 | // GStreamer's gst-ptp-helper is a PTP *client only* — it listens for PTP | ||
| 842 | // Announce/Sync messages but never originates them. For AES67, FPP must | ||
| 843 | // participate in PTP as either grandmaster or follower (via BMCA). | ||
| 844 | // | ||
| 845 | // We launch ptp4l as a managed subprocess with an AES67-appropriate config: | ||
| 846 | // - Configurable domain (default 0), two-step, announce every 2s, 8 syncs/sec | ||
| 847 | // - Hardware timestamping when available (/dev/ptp0) | ||
| 848 | // - BMCA priority driven by the "ptpRole" setting -- see AES67Config | ||
| 849 | // - DSCP EF (46) on PTP event/general messages -- see AES67::PTP_DSCP | ||
| 850 | // | ||
| 851 | // phc2sys is deliberately not launched -- see the note in InitPTP(). The | ||
| 852 | // pipeline reads PTP time directly rather than via the system clock. | ||
| 853 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 854 | |||
| 855 | // Absolute path so we do not depend on whatever PATH systemd handed fppd -- | ||
| 856 | // the ptp4l presence check below is absolute for the same reason. | ||
| 857 | static const char* PMC_BINARY = "/usr/sbin/pmc"; | ||
| 858 | static const char* PTP4L_BINARY = "/usr/sbin/ptp4l"; | ||
| 859 | |||
| 860 | // Does this ptp4l understand the given config key? | ||
| 861 | // | ||
| 862 | // linuxptp 4.0 renamed slaveOnly to clientOnly (and masterOnly to serverOnly) | ||
| 863 | // and kept the old spellings as deprecated aliases, which 4.2 announces on | ||
| 864 | // every start: "option slaveOnly is deprecated, please use clientOnly | ||
| 865 | // instead". Neither spelling is safe to hard-code. An unknown key is a hard | ||
| 866 | // config-parse failure -- ptp4l exits before it does anything at all -- so | ||
| 867 | // writing clientOnly takes PTP down entirely on a pre-4.0 install, while | ||
| 868 | // writing slaveOnly is fine today but relies on an alias that has already | ||
| 869 | // been announced as going away. | ||
| 870 | // | ||
| 871 | // Asking is better than either. ptp4l parses its config file before it looks | ||
| 872 | // at anything else, so a config carrying nothing but the key under test, run | ||
| 873 | // with no -i, answers the question without starting PTP, touching the network | ||
| 874 | // or opening a clock. Measured on linuxptp 4.2: | ||
| 875 | // | ||
| 876 | // clientOnly -> "no interface specified" (255) | ||
| 877 | // notARealOption -> "unknown option notARealOption at line 2 ..." (254) | ||
| 878 | // | ||
| 879 | // The exit codes differ too, but the message is the part that is actually | ||
| 880 | // documented behaviour, so that is what this reads. Anything unexpected -- | ||
| 881 | // no output at all, ptp4l missing, popen failing -- is treated as "not | ||
| 882 | // supported", which falls back to the spelling that works on every release | ||
| 883 | // shipped so far rather than the one that only works on new ones. | ||
| 884 | 66 | static bool Ptp4lSupportsOption(const std::string& option) { | |
| 885 | struct PipeCloser { | ||
| 886 |
1/2✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
|
66 | void operator()(FILE* f) const { if (f) pclose(f); } |
| 887 | }; | ||
| 888 |
1/2✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
|
66 | const std::string probePath = "/tmp/fpp-ptp4l-probe.conf"; |
| 889 | { | ||
| 890 |
1/2✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
|
66 | std::ofstream probe(probePath); |
| 891 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
|
66 | if (!probe.is_open()) { |
| 892 | ✗ | return false; | |
| 893 | } | ||
| 894 |
3/6✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 66 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 66 times.
✗ Branch 5 not taken.
|
66 | probe << "[global]\n" << option << "\t\t1\n"; |
| 895 |
1/2✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
|
66 | } |
| 896 |
4/8✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 66 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 66 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 66 times.
✗ Branch 7 not taken.
|
66 | std::string cmd = std::string(PTP4L_BINARY) + " -f " + probePath + " 2>&1"; |
| 897 | 66 | std::string output; | |
| 898 | { | ||
| 899 |
1/2✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
|
66 | std::unique_ptr<FILE, PipeCloser> pipe(popen(cmd.c_str(), "r")); |
| 900 |
1/2✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
|
66 | if (pipe) { |
| 901 | char buffer[256]; | ||
| 902 |
3/4✓ Branch 0 taken 130 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 64 times.
✓ Branch 3 taken 66 times.
|
130 | while (fgets(buffer, sizeof(buffer), pipe.get()) != nullptr) { |
| 903 |
1/2✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
|
64 | output += buffer; |
| 904 | } | ||
| 905 | } | ||
| 906 | 66 | } | |
| 907 | 66 | unlink(probePath.c_str()); | |
| 908 | |||
| 909 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 64 times.
|
66 | if (output.empty()) { |
| 910 | 2 | return false; | |
| 911 | } | ||
| 912 | 64 | return output.find("unknown option") == std::string::npos; | |
| 913 | 66 | } | |
| 914 | |||
| 915 | // The spelling of the follower-role key this ptp4l accepts. Probed once: the | ||
| 916 | // binary does not change under a running fppd, and InitPTP() writes the config | ||
| 917 | // up to three times per start. | ||
| 918 | 45 | static const char* Ptp4lClientOnlyKey() { | |
| 919 | static const char* key = | ||
| 920 |
7/12✓ Branch 0 taken 33 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 33 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 31 times.
✓ Branch 9 taken 2 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
111 | Ptp4lSupportsOption("clientOnly") ? "clientOnly" : "slaveOnly"; |
| 921 | 45 | return key; | |
| 922 | } | ||
| 923 | |||
| 924 | // Whether this ptp4l takes ptp_minor_version (linuxptp 4.0 and later). | ||
| 925 | // | ||
| 926 | // Those releases send PTP v2.1 (minorVersionPTP = 1) by default, and some | ||
| 927 | // MACs only hardware-timestamp a received event message whose version byte is | ||
| 928 | // exactly v2.0. The Pi 5's Ethernet is one: between two Pi 5s, both on | ||
| 929 | // linuxptp 4.2 with hardware timestamping, the follower logs "received SYNC | ||
| 930 | // without timestamp" and the leader "received DELAY_REQ without timestamp" | ||
| 931 | // for as long as they run, and the follower sits in UNCALIBRATED. Both | ||
| 932 | // speaking v2.0 locks within seconds. A Pi 5 following other gear never | ||
| 933 | // shows it, because AES67 equipment is IEEE 1588-2008 and sends v2.0. | ||
| 934 | // | ||
| 935 | // v2.0 is also what AES67 itself specifies, and a v2.1 node accepts it, so | ||
| 936 | // FPP sends v2.0 everywhere. Older linuxptp sends v2.0 already and rejects | ||
| 937 | // the key (see Ptp4lSupportsOption()), so it is written only where it exists. | ||
| 938 | 45 | static bool Ptp4lHasMinorVersionOption() { | |
| 939 |
5/10✓ Branch 0 taken 33 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 33 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
|
111 | static const bool has = Ptp4lSupportsOption("ptp_minor_version"); |
| 940 | 45 | return has; | |
| 941 | } | ||
| 942 | |||
| 943 | 45 | bool AES67Manager::WritePtpConf(const std::string& path, bool hwTimestamping, bool includeDscp) { | |
| 944 |
1/2✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
|
45 | std::ofstream conf(path); |
| 945 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
|
45 | if (!conf.is_open()) { |
| 946 | ✗ | LogErr(VB_MEDIAOUT, "AES67Manager: Cannot write PTP config to %s\n", path.c_str()); | |
| 947 | ✗ | WarningHolder::AddWarning(AES67::WARNING_ID_PTP, AES67::WARNING_PTP_CONF_WRITE); | |
| 948 | ✗ | return false; | |
| 949 | } | ||
| 950 | |||
| 951 | // BMCA behaviour. "auto" deliberately runs at a worse priority1 than the | ||
| 952 | // 128 that professional gear ships with: a tie on priority is broken by | ||
| 953 | // clock identity (lowest MAC wins), which is how an FPP box ends up | ||
| 954 | // grandmastering a Q-SYS or Yamaha domain it should have been following. | ||
| 955 | 45 | int priority1 = AES67::PTP_PRIORITY_AUTO; | |
| 956 | 45 | bool slaveOnly = false; | |
| 957 |
3/4✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 44 times.
|
45 | if (m_config.ptpRole == "master") { |
| 958 | 1 | priority1 = AES67::PTP_PRIORITY_PREFER_MASTER; | |
| 959 |
3/4✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 40 times.
|
44 | } else if (m_config.ptpRole == "follower") { |
| 960 | 4 | slaveOnly = true; | |
| 961 | } | ||
| 962 | |||
| 963 | // AES67 media profile: announce every 2s, sync 8/sec, delay req 1/sec. | ||
| 964 | conf << "[global]\n" | ||
| 965 |
3/6✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 45 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 45 times.
✗ Branch 5 not taken.
|
45 | << "domainNumber\t\t" << m_config.ptpDomain << "\n" |
| 966 | << "twoStepFlag\t\t1\n" | ||
| 967 |
8/14✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 45 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 45 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 45 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 45 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 4 times.
✓ Branch 11 taken 41 times.
✓ Branch 12 taken 45 times.
✗ Branch 13 not taken.
|
45 | << Ptp4lClientOnlyKey() << "\t\t" << (slaveOnly ? 1 : 0) << "\n" |
| 968 |
3/6✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 45 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 45 times.
✗ Branch 5 not taken.
|
45 | << "priority1\t\t" << priority1 << "\n" |
| 969 | << "priority2\t\t128\n" | ||
| 970 | << "clockClass\t\t248\n" | ||
| 971 | << "clockAccuracy\t\t0xFE\n" | ||
| 972 | << "offsetScaledLogVariance\t0xFFFF\n" | ||
| 973 | << "logAnnounceInterval\t1\n" // 1 announce/2s — matches announceReceiptTimeout cadence | ||
| 974 | << "logSyncInterval\t\t-3\n" // 8/sec (AES67 recommends -3) | ||
| 975 | << "logMinDelayReqInterval\t0\n" | ||
| 976 | << "announceReceiptTimeout\t3\n" | ||
| 977 | << "syncReceiptTimeout\t0\n" | ||
| 978 | // Step rather than slew a large offset. linuxptp defaults | ||
| 979 | // step_threshold to 0, meaning it only ever steps on the very first | ||
| 980 | // correction (first_step_threshold, 20us) and slews everything after. | ||
| 981 | // Initial lock is therefore fine -- a follower does converge on real | ||
| 982 | // hardware -- but a grandmaster changeover or a GM time jump after | ||
| 983 | // that leaves a large offset to be slewed out at the servo's frequency | ||
| 984 | // limit, which takes hours for anything past a second. The published | ||
| 985 | // AES67 profile sets this for the same reason ("converge faster when | ||
| 986 | // time jumps"). Harmless as grandmaster, where there is nothing to | ||
| 987 | // step towards. | ||
| 988 | << "step_threshold\t\t1\n" | ||
| 989 | << "transportSpecific\t0x0\n" | ||
| 990 | << "network_transport\tUDPv4\n" | ||
| 991 | << "delay_mechanism\t\tE2E\n" | ||
| 992 |
19/36✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 45 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 45 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 45 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 45 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 45 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 45 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 45 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 45 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 45 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 45 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 45 times.
✗ Branch 23 not taken.
✓ Branch 24 taken 45 times.
✗ Branch 25 not taken.
✓ Branch 26 taken 45 times.
✗ Branch 27 not taken.
✓ Branch 28 taken 45 times.
✗ Branch 29 not taken.
✓ Branch 30 taken 42 times.
✓ Branch 31 taken 3 times.
✓ Branch 32 taken 45 times.
✗ Branch 33 not taken.
✓ Branch 34 taken 45 times.
✗ Branch 35 not taken.
|
45 | << "time_stamping\t\t" << (hwTimestamping ? "hardware" : "software") << "\n"; |
| 993 | |||
| 994 |
3/4✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✓ Branch 3 taken 5 times.
|
45 | if (Ptp4lHasMinorVersionOption()) { |
| 995 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | conf << "ptp_minor_version\t0\n"; |
| 996 | } | ||
| 997 | |||
| 998 | // Without a PHC, ptp4l's clock is CLOCK_REALTIME, and as a follower it | ||
| 999 | // steps the system clock to the grandmaster's time. That is chrony's | ||
| 1000 | // clock, and the grandmaster's time is often not wall time: a Q-SYS Core | ||
| 1001 | // distributes an arbitrary epoch near 0, so a K32-Max on a USB NIC went | ||
| 1002 | // to January 1970 every few minutes and back each time chrony stepped it | ||
| 1003 | // forward. phc2sys was dropped for the same reason (see InitPTP()). | ||
| 1004 | // free_running keeps ptp4l in the domain -- it still answers, still | ||
| 1005 | // elects, still grandmasters when alone -- but it never adjusts the | ||
| 1006 | // clock, so a follower's streams are timed from NTP rather than locked | ||
| 1007 | // to the grandmaster. RaisePtpFreeRunningWarning() says so. | ||
| 1008 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 42 times.
|
45 | if (!hwTimestamping) { |
| 1009 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | conf << "free_running\t\t1\n"; |
| 1010 | } | ||
| 1011 | |||
| 1012 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 1 times.
|
45 | if (includeDscp) { |
| 1013 |
2/4✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 44 times.
✗ Branch 3 not taken.
|
44 | conf << "dscp_event\t\t" << AES67::PTP_DSCP << "\n" // EF (46) -- Sync/Delay_Req |
| 1014 |
4/8✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 44 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 44 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 44 times.
✗ Branch 7 not taken.
|
44 | << "dscp_general\t\t" << AES67::PTP_DSCP << "\n"; // EF (46) -- Announce/Follow_Up/etc. |
| 1015 | } | ||
| 1016 |
1/2✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
|
45 | conf << "# AES67 uses L2 multicast on 224.0.1.129/224.0.0.107\n"; |
| 1017 |
1/2✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
|
45 | conf.close(); |
| 1018 | 45 | return true; | |
| 1019 | 45 | } | |
| 1020 | |||
| 1021 | // A pidfd for our own child, or -1 with errno set (ESRCH: already gone; | ||
| 1022 | // ENOSYS: kernel or platform without pidfds, where callers fall back to | ||
| 1023 | // the PID). | ||
| 1024 | 45 | static int OpenPidfd(pid_t pid) { | |
| 1025 | #if defined(__linux__) && defined(SYS_pidfd_open) | ||
| 1026 | 45 | return (int)syscall(SYS_pidfd_open, pid, 0); // close-on-exec by default | |
| 1027 | #else | ||
| 1028 | (void)pid; | ||
| 1029 | errno = ENOSYS; | ||
| 1030 | return -1; | ||
| 1031 | #endif | ||
| 1032 | } | ||
| 1033 | |||
| 1034 | // Whether the child is alive. A pidfd becomes readable when its process | ||
| 1035 | // exits. Without one, the PID has to belong to a process with the | ||
| 1036 | // expected name: a dead child's PID may already be someone else's. | ||
| 1037 | 589 | static bool ChildAlive(pid_t pid, int pidfd, const char* name) { | |
| 1038 | #if defined(__linux__) | ||
| 1039 |
1/2✓ Branch 0 taken 589 times.
✗ Branch 1 not taken.
|
589 | if (pidfd >= 0) { |
| 1040 | 589 | struct pollfd pfd = { pidfd, POLLIN, 0 }; | |
| 1041 |
1/2✓ Branch 0 taken 589 times.
✗ Branch 1 not taken.
|
589 | return poll(&pfd, 1, 0) == 0; |
| 1042 | } | ||
| 1043 | #endif | ||
| 1044 | ✗ | if (pid <= 0 || kill(pid, 0) != 0) { | |
| 1045 | ✗ | return false; | |
| 1046 | } | ||
| 1047 | ✗ | std::ifstream f("/proc/" + std::to_string((int)pid) + "/comm"); | |
| 1048 | ✗ | if (!f.is_open()) { | |
| 1049 | ✗ | return true; // no procfs (macOS): the signal probe is all there is | |
| 1050 | } | ||
| 1051 | ✗ | std::string comm; | |
| 1052 | ✗ | std::getline(f, comm); | |
| 1053 | ✗ | return comm == name; | |
| 1054 | ✗ | } | |
| 1055 | |||
| 1056 | 39 | static void SignalChild(pid_t pid, int pidfd, int sig, const char* name) { | |
| 1057 | #if defined(__linux__) && defined(SYS_pidfd_send_signal) | ||
| 1058 |
1/2✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
|
39 | if (pidfd >= 0) { |
| 1059 | 39 | syscall(SYS_pidfd_send_signal, pidfd, sig, nullptr, 0); | |
| 1060 | 39 | return; | |
| 1061 | } | ||
| 1062 | #endif | ||
| 1063 | ✗ | if (ChildAlive(pid, -1, name)) { | |
| 1064 | ✗ | kill(pid, sig); | |
| 1065 | } | ||
| 1066 | } | ||
| 1067 | |||
| 1068 | 5 | void AES67Manager::ForgetPtp4l() { | |
| 1069 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | std::lock_guard<std::mutex> lock(m_ptp4lProcMutex); |
| 1070 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
|
5 | if (m_ptp4lPidfd >= 0) { |
| 1071 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | close(m_ptp4lPidfd); |
| 1072 | 4 | m_ptp4lPidfd = -1; | |
| 1073 | } | ||
| 1074 | 5 | m_ptp4lPid = -1; | |
| 1075 | 5 | } | |
| 1076 | |||
| 1077 | 45 | bool AES67Manager::StartPtp4l(bool hwTimestamping, bool includeDscp) { | |
| 1078 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
|
45 | if (!WritePtpConf(m_ptpConfPath, hwTimestamping, includeDscp)) { |
| 1079 | ✗ | return false; | |
| 1080 | } | ||
| 1081 | |||
| 1082 | 45 | pid_t pid = fork(); | |
| 1083 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
|
90 | if (pid < 0) { |
| 1084 | ✗ | LogErr(VB_MEDIAOUT, "AES67Manager: fork() failed for ptp4l: %s\n", FPPstrerror(errno)); | |
| 1085 | ✗ | WarningHolder::AddWarning(AES67::WARNING_ID_PTP, AES67::WARNING_PTP_SPAWN_FAILED); | |
| 1086 | ✗ | return false; | |
| 1087 | } | ||
| 1088 |
2/2✓ Branch 0 taken 45 times.
✓ Branch 1 taken 45 times.
|
90 | if (pid == 0) { |
| 1089 | // Child process — exec ptp4l (-m: log to stderr) | ||
| 1090 | 45 | execlp("ptp4l", "ptp4l", | |
| 1091 | "-i", m_config.ptpInterface.c_str(), | ||
| 1092 | "-f", m_ptpConfPath.c_str(), | ||
| 1093 | "-m", | ||
| 1094 | nullptr); | ||
| 1095 | // If exec fails | ||
| 1096 | 45 | _exit(127); | |
| 1097 | } | ||
| 1098 | 45 | int pidfd = OpenPidfd(pid); | |
| 1099 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
45 | bool gone = pidfd < 0 && errno == ESRCH; |
| 1100 | { | ||
| 1101 |
1/2✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
|
45 | std::lock_guard<std::mutex> lock(m_ptp4lProcMutex); |
| 1102 | 45 | m_ptp4lPid = pid; | |
| 1103 | 45 | m_ptp4lPidfd = pidfd; | |
| 1104 | 45 | } | |
| 1105 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
|
45 | if (gone) { |
| 1106 | // Exited and was reaped before we could even look. | ||
| 1107 | ✗ | ForgetPtp4l(); | |
| 1108 | ✗ | return false; | |
| 1109 | } | ||
| 1110 | |||
| 1111 | // Give ptp4l a moment to start, then check it did not exit immediately | ||
| 1112 | // (bad config key, no hardware timestamping, interface down, ...). | ||
| 1113 | 45 | usleep(500000); // 500ms | |
| 1114 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 41 times.
|
45 | if (!IsPtp4lRunning()) { |
| 1115 | 4 | ForgetPtp4l(); | |
| 1116 | 4 | return false; | |
| 1117 | } | ||
| 1118 | |||
| 1119 | 41 | m_ptpHwTimestamping.store(hwTimestamping); | |
| 1120 |
3/4✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✓ Branch 3 taken 1 times.
|
41 | LogInfo(VB_MEDIAOUT, "AES67Manager: ptp4l started (PID %d) on %s — %s timestamping%s, domain %d, role %s\n", |
| 1121 | (int)m_ptp4lPid, m_config.ptpInterface.c_str(), | ||
| 1122 | hwTimestamping ? "hardware" : "software", | ||
| 1123 | includeDscp ? "" : ", DSCP disabled", | ||
| 1124 | m_config.ptpDomain, m_config.ptpRole.c_str()); | ||
| 1125 | 41 | return true; | |
| 1126 | } | ||
| 1127 | |||
| 1128 | 81 | bool AES67Manager::InitPTP() { | |
| 1129 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 81 times.
|
81 | if (m_ptpInitialized) { |
| 1130 | ✗ | return true; | |
| 1131 | } | ||
| 1132 | |||
| 1133 | // Check if ptp4l binary exists | ||
| 1134 |
2/2✓ Branch 0 taken 39 times.
✓ Branch 1 taken 42 times.
|
81 | if (!FileExists(PTP4L_BINARY)) { |
| 1135 | 39 | LogErr(VB_MEDIAOUT, "AES67Manager: ptp4l not found — install linuxptp package\n"); | |
| 1136 |
2/4✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
✗ Branch 3 not taken.
|
117 | WarningHolder::AddWarning(AES67::WARNING_ID_PTP, AES67::WARNING_PTP_NOT_INSTALLED); |
| 1137 | 39 | return false; | |
| 1138 | } | ||
| 1139 | |||
| 1140 | 42 | m_ptpConfPath = "/tmp/fpp-ptp4l.conf"; | |
| 1141 | |||
| 1142 | 42 | LogInfo(VB_MEDIAOUT, "AES67Manager: Starting ptp4l on %s (AES67 profile, domain %d, role %s)\n", | |
| 1143 | m_config.ptpInterface.c_str(), m_config.ptpDomain, m_config.ptpRole.c_str()); | ||
| 1144 | |||
| 1145 | // Start attempts, most capable first. The last one drops the DSCP keys: | ||
| 1146 | // they are only understood by linuxptp >= 2.0, and an unknown key is a | ||
| 1147 | // hard config-parse failure, which would otherwise take PTP down entirely | ||
| 1148 | // on an older install rather than just losing the QoS marking. | ||
| 1149 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 40 times.
|
42 | if (!StartPtp4l(true, true)) { |
| 1150 | 2 | LogErr(VB_MEDIAOUT, "AES67Manager: ptp4l exited immediately — " | |
| 1151 | "check hardware timestamping support on %s\n", | ||
| 1152 | m_config.ptpInterface.c_str()); | ||
| 1153 | 2 | LogInfo(VB_MEDIAOUT, "AES67Manager: Retrying ptp4l with software timestamping\n"); | |
| 1154 | |||
| 1155 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (!StartPtp4l(false, true)) { |
| 1156 | 1 | LogInfo(VB_MEDIAOUT, "AES67Manager: Retrying ptp4l without DSCP marking " | |
| 1157 | "(linuxptp may predate dscp_event/dscp_general)\n"); | ||
| 1158 | |||
| 1159 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (!StartPtp4l(false, false)) { |
| 1160 | 1 | LogErr(VB_MEDIAOUT, "AES67Manager: ptp4l failed to start\n"); | |
| 1161 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
3 | WarningHolder::AddWarning(AES67::WARNING_ID_PTP, AES67::WARNING_PTP_START_FAILED); |
| 1162 | 1 | ForgetPtp4l(); | |
| 1163 | 1 | return false; | |
| 1164 | } | ||
| 1165 | } | ||
| 1166 | } | ||
| 1167 | |||
| 1168 | // ptp4l is up, so none of the four "PTP could not start" warnings describes | ||
| 1169 | // anything current any more -- including one raised by an earlier attempt | ||
| 1170 | // on this same call, since the cascade above retries with weaker options. | ||
| 1171 | 41 | ClearAES67PtpStartWarnings(); | |
| 1172 | |||
| 1173 | // phc2sys is deliberately NOT started. | ||
| 1174 | // | ||
| 1175 | // Its only job was to copy PTP time onto the system clock, and nothing | ||
| 1176 | // needs that any more: the media clock reads the PTP time source directly | ||
| 1177 | // (the PHC with hardware timestamping, CLOCK_REALTIME with software), so | ||
| 1178 | // the pipeline no longer depends on the system clock tracking PTP. | ||
| 1179 | // | ||
| 1180 | // Running it is actively harmful against real grandmasters. PTP's | ||
| 1181 | // timescale is whatever the grandmaster distributes, and Dante/Brooklyn | ||
| 1182 | // devices commonly distribute an arbitrary epoch rather than wall time -- | ||
| 1183 | // one measured at ~4411 seconds, i.e. time since the device booted. | ||
| 1184 | // "phc2sys -a -r" faithfully slaved CLOCK_REALTIME to that and dragged the | ||
| 1185 | // Pi's clock back to 1970 (reported on issue #2848), taking the scheduler, | ||
| 1186 | // logs and anything else on wall time with it. | ||
| 1187 | // | ||
| 1188 | // A box that wants PTP-disciplined system time should run phc2sys from | ||
| 1189 | // systemd with an offset appropriate to its grandmaster; that is a | ||
| 1190 | // deliberate site decision, not something an audio stream should impose. | ||
| 1191 | |||
| 1192 | // Passive reader for the grandmaster's IP address -- see | ||
| 1193 | // PtpAnnounceListenLoop(). Started only once ptp4l is up, so it lives | ||
| 1194 | // exactly as long as the domain membership it is reporting on. | ||
| 1195 |
1/2✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
|
41 | if (!m_ptpAnnounceThread.joinable()) { |
| 1196 | 41 | m_ptpAnnounceRunning.store(true); | |
| 1197 |
1/2✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
|
41 | m_ptpAnnounceThread = std::thread(&AES67Manager::PtpAnnounceListenLoop, this); |
| 1198 | } | ||
| 1199 | |||
| 1200 | 41 | AnchorToPtpLock(); | |
| 1201 |
2/6✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
41 | m_ptpStartedWith = PtpParams{ m_config.ptpInterface, m_config.ptpDomain, m_config.ptpRole }; |
| 1202 | |||
| 1203 | 41 | m_ptpInitialized = true; | |
| 1204 | 41 | LogInfo(VB_MEDIAOUT, "AES67Manager: PTP initialized — ptp4l PID %d on %s\n", | |
| 1205 | (int)m_ptp4lPid, m_config.ptpInterface.c_str()); | ||
| 1206 | 41 | return true; | |
| 1207 | } | ||
| 1208 | |||
| 1209 | 48 | void AES67Manager::AnchorToPtpLock() { | |
| 1210 | // Do not let anything anchor to the clock until it has stopped moving. | ||
| 1211 | // | ||
| 1212 | // ptp4l being alive is not the same as the clock being right: as a | ||
| 1213 | // follower the PHC free-runs until BMCA picks a grandmaster and the first | ||
| 1214 | // measurement steps it, which on #2848 was 6.81 hours later than the value | ||
| 1215 | // four streams had already anchored to. See AES67::PTP_LOCK_OFFSET_NS. | ||
| 1216 | // | ||
| 1217 | // Timing out is not fatal -- the streams start anyway, because a box with | ||
| 1218 | // no grandmaster on the domain yet should still come up and play -- but it | ||
| 1219 | // does mean the anchors are provisional, so say so, and leave the step | ||
| 1220 | // detector in CheckPtpWatchdog() to rebuild them when the clock does lock. | ||
| 1221 | 48 | bool locked = false; | |
| 1222 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 37 times.
|
48 | if (m_config.ptpLockWaitMs > 0) { |
| 1223 | 11 | locked = WaitForPtpLock(m_config.ptpLockWaitMs); | |
| 1224 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 4 times.
|
11 | if (!locked) { |
| 1225 |
2/4✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
7 | LogWarn(VB_MEDIAOUT, |
| 1226 | "AES67Manager: PTP did not settle within %dms (state %s) — " | ||
| 1227 | "starting anyway; the RTP timelines will be re-anchored if " | ||
| 1228 | "the clock steps once it locks\n", | ||
| 1229 | m_config.ptpLockWaitMs, GetPtp4lState().c_str()); | ||
| 1230 | 7 | RaisePtpNoLockWarning(); | |
| 1231 | } else { | ||
| 1232 | 4 | ClearPtpNoLockWarning(); | |
| 1233 | } | ||
| 1234 | } else { | ||
| 1235 | 37 | LogInfo(VB_MEDIAOUT, | |
| 1236 | "AES67Manager: ptpLockWaitMs is 0 — not waiting for PTP to " | ||
| 1237 | "lock before anchoring the RTP timelines\n"); | ||
| 1238 | } | ||
| 1239 | // With ptpMediaClock off nothing is anchored to PTP at all, so there is | ||
| 1240 | // no anchor for a late lock to invalidate and the question does not apply. | ||
| 1241 |
3/4✓ Branch 0 taken 44 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 44 times.
|
48 | m_ptpLockedAtStart.store(locked || !m_config.ptpMediaClock); |
| 1242 | |||
| 1243 | 48 | ReviewPtpFreeRunningWarning(); | |
| 1244 | 48 | } | |
| 1245 | |||
| 1246 | // SIGTERM then SIGKILL, polling until the process is really gone. | ||
| 1247 | // | ||
| 1248 | // fppd installs SIGCHLD with SA_NOCLDWAIT (see fppd.cpp), so children are | ||
| 1249 | // reaped by init and waitpid() here fails with ECHILD immediately -- it never | ||
| 1250 | // actually waited. That let ApplyConfig() launch a replacement ptp4l while | ||
| 1251 | // the old one still held /var/run/ptp4l and the PTP ports, leaving two | ||
| 1252 | // daemons briefly running BMCA against each other on one interface. | ||
| 1253 | // | ||
| 1254 | // Signals go through the pidfd where there is one: under SA_NOCLDWAIT a | ||
| 1255 | // child that already died has given up its PID, and kill(pid) -- SIGKILL | ||
| 1256 | // included -- would land on whatever process took it. | ||
| 1257 | 466 | static void StopChildProcess(pid_t& pid, int& pidfd, const char* name) { | |
| 1258 | 466 | auto forget = [&]() { | |
| 1259 |
2/2✓ Branch 0 taken 41 times.
✓ Branch 1 taken 425 times.
|
466 | if (pidfd >= 0) { |
| 1260 | 41 | close(pidfd); | |
| 1261 | 41 | pidfd = -1; | |
| 1262 | } | ||
| 1263 | 466 | pid = -1; | |
| 1264 | 932 | }; | |
| 1265 |
2/2✓ Branch 0 taken 425 times.
✓ Branch 1 taken 41 times.
|
466 | if (pid <= 0) { |
| 1266 |
1/2✓ Branch 0 taken 425 times.
✗ Branch 1 not taken.
|
425 | forget(); |
| 1267 | 466 | return; | |
| 1268 | } | ||
| 1269 |
3/4✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 39 times.
|
41 | if (!ChildAlive(pid, pidfd, name)) { |
| 1270 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | LogInfo(VB_MEDIAOUT, "AES67Manager: %s (PID %d) had already exited\n", name, (int)pid); |
| 1271 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | forget(); |
| 1272 | 2 | return; | |
| 1273 | } | ||
| 1274 |
1/2✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
|
39 | LogInfo(VB_MEDIAOUT, "AES67Manager: Stopping %s (PID %d)\n", name, (int)pid); |
| 1275 |
1/2✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
|
39 | SignalChild(pid, pidfd, SIGTERM, name); |
| 1276 | |||
| 1277 | // Up to ~3s for a clean exit, then insist. | ||
| 1278 |
1/2✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
|
78 | for (int i = 0; i < 60; i++) { |
| 1279 | // Harmless no-op under SA_NOCLDWAIT; reaps the child if a future | ||
| 1280 | // change turns that off. | ||
| 1281 | 78 | int status = 0; | |
| 1282 |
1/2✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
|
78 | waitpid(pid, &status, WNOHANG); |
| 1283 |
3/4✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
✓ Branch 3 taken 39 times.
|
78 | if (!ChildAlive(pid, pidfd, name)) { |
| 1284 |
1/2✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
|
39 | forget(); |
| 1285 | 39 | return; | |
| 1286 | } | ||
| 1287 |
1/2✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
|
39 | usleep(50000); |
| 1288 | } | ||
| 1289 | |||
| 1290 | ✗ | LogWarn(VB_MEDIAOUT, "AES67Manager: %s (PID %d) did not exit, sending SIGKILL\n", | |
| 1291 | name, (int)pid); | ||
| 1292 | ✗ | SignalChild(pid, pidfd, SIGKILL, name); | |
| 1293 | ✗ | for (int i = 0; i < 20; i++) { | |
| 1294 | ✗ | int status = 0; | |
| 1295 | ✗ | waitpid(pid, &status, WNOHANG); | |
| 1296 | ✗ | if (!ChildAlive(pid, pidfd, name)) { | |
| 1297 | ✗ | break; | |
| 1298 | } | ||
| 1299 | ✗ | usleep(50000); | |
| 1300 | } | ||
| 1301 | ✗ | forget(); | |
| 1302 | } | ||
| 1303 | |||
| 1304 | 233 | void AES67Manager::ShutdownPTP() { | |
| 1305 | 233 | m_ptpAnnounceRunning.store(false); | |
| 1306 |
2/2✓ Branch 0 taken 41 times.
✓ Branch 1 taken 192 times.
|
233 | if (m_ptpAnnounceThread.joinable()) { |
| 1307 |
1/2✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
|
41 | m_ptpAnnounceThread.join(); |
| 1308 | } | ||
| 1309 | { | ||
| 1310 |
1/2✓ Branch 0 taken 233 times.
✗ Branch 1 not taken.
|
233 | std::lock_guard<std::mutex> lock(m_ptpAnnounceMutex); |
| 1311 | 233 | m_ptpAnnounceSources.clear(); | |
| 1312 | 233 | } | |
| 1313 | |||
| 1314 |
1/2✓ Branch 0 taken 233 times.
✗ Branch 1 not taken.
|
233 | StopChildProcess(m_phc2sysPid, m_phc2sysPidfd, "phc2sys"); |
| 1315 | { | ||
| 1316 | // out of the members first, so a reader never polls the pidfd as it | ||
| 1317 | // is closed; the wait for the child is made without the lock | ||
| 1318 | pid_t pid; | ||
| 1319 | int pidfd; | ||
| 1320 | { | ||
| 1321 |
1/2✓ Branch 0 taken 233 times.
✗ Branch 1 not taken.
|
233 | std::lock_guard<std::mutex> lock(m_ptp4lProcMutex); |
| 1322 | 233 | pid = m_ptp4lPid; | |
| 1323 | 233 | pidfd = m_ptp4lPidfd; | |
| 1324 | 233 | m_ptp4lPid = -1; | |
| 1325 | 233 | m_ptp4lPidfd = -1; | |
| 1326 | 233 | } | |
| 1327 |
1/2✓ Branch 0 taken 233 times.
✗ Branch 1 not taken.
|
233 | StopChildProcess(pid, pidfd, "ptp4l"); |
| 1328 | } | ||
| 1329 | 233 | m_ptpHwTimestamping.store(false); | |
| 1330 | |||
| 1331 |
2/2✓ Branch 0 taken 41 times.
✓ Branch 1 taken 192 times.
|
233 | if (!m_ptpConfPath.empty()) { |
| 1332 | 41 | unlink(m_ptpConfPath.c_str()); | |
| 1333 | 41 | m_ptpConfPath.clear(); | |
| 1334 | } | ||
| 1335 | 233 | m_ptpInitialized = false; | |
| 1336 | 233 | m_ptpLockedAtStart.store(false); | |
| 1337 | |||
| 1338 | // Nothing is anchored to PTP any more, so a banner saying the anchors are | ||
| 1339 | // provisional describes streams that no longer exist. InitPTP() raises it | ||
| 1340 | // again if the next start still cannot reach a lock -- and likewise for the | ||
| 1341 | // start failures, which cannot be true of a daemon we have just stopped. | ||
| 1342 |
1/2✓ Branch 0 taken 233 times.
✗ Branch 1 not taken.
|
233 | ClearPtpNoLockWarning(); |
| 1343 |
1/2✓ Branch 0 taken 233 times.
✗ Branch 1 not taken.
|
233 | ClearAES67PtpStartWarnings(); |
| 1344 |
3/6✓ Branch 0 taken 233 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 233 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 233 times.
✗ Branch 5 not taken.
|
699 | WarningHolder::RemoveWarning(AES67::WARNING_ID_PTP, AES67::WARNING_PTP_FREE_RUNNING); |
| 1345 | |||
| 1346 | { | ||
| 1347 |
1/2✓ Branch 0 taken 233 times.
✗ Branch 1 not taken.
|
233 | std::lock_guard<std::mutex> lock(m_ptpStepMutex); |
| 1348 | 233 | m_ptpMonotonicDeltaValid = false; | |
| 1349 | 233 | } | |
| 1350 | |||
| 1351 |
1/2✓ Branch 0 taken 233 times.
✗ Branch 1 not taken.
|
233 | std::lock_guard<std::mutex> lock(m_ptpCacheMutex); |
| 1352 |
1/2✓ Branch 0 taken 233 times.
✗ Branch 1 not taken.
|
233 | m_ptpCache = PtpQueryCache(); |
| 1353 | 233 | } | |
| 1354 | |||
| 1355 | 2637 | bool AES67Manager::IsPtp4lRunning() const { | |
| 1356 | // SA_NOCLDWAIT means a dead ptp4l leaves no zombie holding its slot, so | ||
| 1357 | // the PID can be recycled by an unrelated process; see ChildAlive(). | ||
| 1358 |
1/2✓ Branch 0 taken 2637 times.
✗ Branch 1 not taken.
|
2637 | std::lock_guard<std::mutex> lock(m_ptp4lProcMutex); |
| 1359 |
5/6✓ Branch 0 taken 470 times.
✓ Branch 1 taken 2167 times.
✓ Branch 2 taken 470 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 465 times.
✓ Branch 5 taken 5 times.
|
5274 | return m_ptp4lPid > 0 && ChildAlive(m_ptp4lPid, m_ptp4lPidfd, "ptp4l"); |
| 1360 | 2637 | } | |
| 1361 | |||
| 1362 | // Runs a `pmc` management query against ptp4l's UDS socket and returns the | ||
| 1363 | // raw text output, or an empty string if ptp4l isn't running / pmc fails. | ||
| 1364 | 144 | static std::string RunPmcQuery(const std::string& tlv, int domain) { | |
| 1365 | struct PipeCloser { | ||
| 1366 | 144 | void operator()(FILE* f) const { | |
| 1367 | // pclose() returns -1 under fppd's SA_NOCLDWAIT (init already | ||
| 1368 | // reaped the child); the output has been read by then, so the | ||
| 1369 | // status is of no use to us either way. | ||
| 1370 |
1/2✓ Branch 0 taken 144 times.
✗ Branch 1 not taken.
|
144 | if (f) pclose(f); |
| 1371 | 144 | } | |
| 1372 | }; | ||
| 1373 | // ptp4l silently drops a management message whose domainNumber does not | ||
| 1374 | // match its own -- even over the UDS socket -- so -d is not optional once | ||
| 1375 | // the domain is configurable. | ||
| 1376 |
2/4✓ Branch 0 taken 144 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 144 times.
✗ Branch 3 not taken.
|
288 | std::string cmd = std::string(PMC_BINARY) + " -u -b 0 -d " + |
| 1377 |
4/8✓ Branch 0 taken 144 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 144 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 144 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 144 times.
✗ Branch 7 not taken.
|
432 | std::to_string(domain) + " '" + tlv + "' 2>/dev/null"; |
| 1378 |
1/2✓ Branch 0 taken 144 times.
✗ Branch 1 not taken.
|
144 | std::unique_ptr<FILE, PipeCloser> pipe(popen(cmd.c_str(), "r")); |
| 1379 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 144 times.
|
144 | if (!pipe) { |
| 1380 | ✗ | return ""; | |
| 1381 | } | ||
| 1382 | 144 | std::string output; | |
| 1383 | char buffer[256]; | ||
| 1384 |
3/4✓ Branch 0 taken 1119 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 975 times.
✓ Branch 3 taken 144 times.
|
1119 | while (fgets(buffer, sizeof(buffer), pipe.get()) != nullptr) { |
| 1385 |
1/2✓ Branch 0 taken 975 times.
✗ Branch 1 not taken.
|
975 | output += buffer; |
| 1386 | } | ||
| 1387 | 144 | return output; | |
| 1388 | 144 | } | |
| 1389 | |||
| 1390 | // Reformats a pmc clockIdentity like "aabbcc.fffe.ddeeff" into the dashed | ||
| 1391 | // EUI-64 form used elsewhere in this file ("AA-BB-CC-FF-FE-DD-EE-FF"). | ||
| 1392 | 72 | static std::string FormatPmcClockId(const std::string& raw) { | |
| 1393 | 72 | std::string hex; | |
| 1394 |
2/2✓ Branch 0 taken 1296 times.
✓ Branch 1 taken 72 times.
|
1368 | for (char c : raw) { |
| 1395 |
3/4✓ Branch 0 taken 1152 times.
✓ Branch 1 taken 144 times.
✓ Branch 2 taken 1152 times.
✗ Branch 3 not taken.
|
1296 | if (c != '.') hex += c; |
| 1396 | } | ||
| 1397 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 72 times.
|
72 | if (hex.length() != 16) { |
| 1398 | ✗ | return raw; | |
| 1399 | } | ||
| 1400 | 72 | std::string dashed; | |
| 1401 |
2/2✓ Branch 0 taken 576 times.
✓ Branch 1 taken 72 times.
|
648 | for (size_t i = 0; i < hex.length(); i += 2) { |
| 1402 |
3/4✓ Branch 0 taken 504 times.
✓ Branch 1 taken 72 times.
✓ Branch 2 taken 504 times.
✗ Branch 3 not taken.
|
576 | if (i) dashed += '-'; |
| 1403 |
1/2✓ Branch 0 taken 576 times.
✗ Branch 1 not taken.
|
576 | dashed += (char)toupper((unsigned char)hex[i]); |
| 1404 |
1/2✓ Branch 0 taken 576 times.
✗ Branch 1 not taken.
|
576 | dashed += (char)toupper((unsigned char)hex[i + 1]); |
| 1405 | } | ||
| 1406 | 72 | return dashed; | |
| 1407 | 72 | } | |
| 1408 | |||
| 1409 | // True if a ptp4l portState string means "this node is the domain master". | ||
| 1410 | // PRE_MASTER is deliberately excluded -- it is a transitional state on the | ||
| 1411 | // way to MASTER, not a settled BMCA outcome. | ||
| 1412 | 883 | static bool IsGrandmasterPortState(const std::string& portState) { | |
| 1413 |
4/4✓ Branch 0 taken 880 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 879 times.
|
883 | return portState == "MASTER" || portState == "GRAND_MASTER"; |
| 1414 | } | ||
| 1415 | |||
| 1416 | // Runs both management queries at most once per PTP_QUERY_CACHE_MS and keeps | ||
| 1417 | // the parsed results. /aes67/status is HTTP-facing and the SAP thread polls | ||
| 1418 | // once a second while BMCA settles; without this each of those forks a pmc. | ||
| 1419 | 344 | void AES67Manager::RefreshPtpCache(bool force) { | |
| 1420 |
1/2✓ Branch 0 taken 344 times.
✗ Branch 1 not taken.
|
344 | std::lock_guard<std::mutex> lock(m_ptpCacheMutex); |
| 1421 | |||
| 1422 | 344 | auto now = std::chrono::steady_clock::now(); | |
| 1423 |
4/4✓ Branch 0 taken 286 times.
✓ Branch 1 taken 58 times.
✓ Branch 2 taken 272 times.
✓ Branch 3 taken 14 times.
|
344 | if (!force && m_ptpCache.valid) { |
| 1424 |
2/4✓ Branch 0 taken 272 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 272 times.
✗ Branch 3 not taken.
|
272 | auto age = std::chrono::duration_cast<std::chrono::milliseconds>(now - m_ptpCache.when); |
| 1425 |
2/2✓ Branch 0 taken 270 times.
✓ Branch 1 taken 2 times.
|
272 | if (age.count() < AES67::PTP_QUERY_CACHE_MS) { |
| 1426 | 270 | return; | |
| 1427 | } | ||
| 1428 | } | ||
| 1429 | |||
| 1430 |
1/2✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
|
74 | PtpQueryCache fresh; |
| 1431 | 74 | fresh.when = now; | |
| 1432 | |||
| 1433 |
3/4✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 72 times.
|
74 | if (!IsPtp4lRunning()) { |
| 1434 | 2 | fresh.valid = true; | |
| 1435 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | fresh.portState = "not running"; |
| 1436 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | m_ptpCache = fresh; |
| 1437 | 2 | return; | |
| 1438 | } | ||
| 1439 | |||
| 1440 | // Grandmaster / offset | ||
| 1441 |
1/2✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
|
72 | int domain = GetPtpDomain(); |
| 1442 |
2/4✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
✗ Branch 3 not taken.
|
72 | std::string output = RunPmcQuery("GET TIME_STATUS_NP", domain); |
| 1443 |
1/2✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
|
72 | std::istringstream iss(output); |
| 1444 | 72 | std::string line; | |
| 1445 |
4/6✓ Branch 0 taken 615 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 615 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 543 times.
✓ Branch 5 taken 72 times.
|
615 | while (std::getline(iss, line)) { |
| 1446 |
1/2✓ Branch 0 taken 543 times.
✗ Branch 1 not taken.
|
543 | std::istringstream ls(line); |
| 1447 | 543 | std::string key; | |
| 1448 |
1/2✓ Branch 0 taken 543 times.
✗ Branch 1 not taken.
|
543 | ls >> key; |
| 1449 |
3/4✓ Branch 0 taken 543 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 65 times.
✓ Branch 3 taken 478 times.
|
543 | if (key == "gmPresent") { |
| 1450 | 65 | std::string val; | |
| 1451 |
1/2✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
|
65 | ls >> val; |
| 1452 |
1/2✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
|
65 | fresh.gmPresent = (val == "true"); |
| 1453 |
6/8✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 39 times.
✓ Branch 4 taken 26 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 19 times.
✓ Branch 7 taken 7 times.
|
65 | fresh.gmPresentValid = (val == "true" || val == "false"); |
| 1454 | // Only ever raise validity here. gmIdentity sets it too, and a | ||
| 1455 | // malformed gmPresent must not retract what another field has | ||
| 1456 | // already established -- pmc happens to print gmPresent first | ||
| 1457 | // today, which is the only reason a plain assignment has not | ||
| 1458 | // bitten anyone. | ||
| 1459 |
2/2✓ Branch 0 taken 58 times.
✓ Branch 1 taken 7 times.
|
65 | if (fresh.gmPresentValid) { |
| 1460 | 58 | fresh.valid = true; | |
| 1461 | } | ||
| 1462 |
3/4✓ Branch 0 taken 478 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
✓ Branch 3 taken 406 times.
|
543 | } else if (key == "gmIdentity") { |
| 1463 | 72 | std::string val; | |
| 1464 |
1/2✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
|
72 | ls >> val; |
| 1465 |
1/2✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
|
72 | if (!val.empty()) { |
| 1466 |
1/2✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
|
72 | fresh.gmIdentity = FormatPmcClockId(val); |
| 1467 | 72 | fresh.valid = true; | |
| 1468 | } | ||
| 1469 |
3/4✓ Branch 0 taken 406 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 46 times.
✓ Branch 3 taken 360 times.
|
478 | } else if (key == "master_offset") { |
| 1470 | 46 | std::string val; | |
| 1471 |
1/2✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
|
46 | ls >> val; |
| 1472 | try { | ||
| 1473 | 46 | size_t consumed = 0; | |
| 1474 |
2/2✓ Branch 0 taken 39 times.
✓ Branch 1 taken 7 times.
|
46 | fresh.offsetNs = std::stoll(val, &consumed); |
| 1475 | 39 | fresh.offsetValid = (consumed == val.size()); | |
| 1476 | 7 | } catch (...) { | |
| 1477 | 7 | fresh.offsetValid = false; | |
| 1478 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 | } |
| 1479 | 46 | } | |
| 1480 | 543 | } | |
| 1481 | |||
| 1482 | // Port state | ||
| 1483 |
2/4✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
✗ Branch 3 not taken.
|
72 | std::string portOutput = RunPmcQuery("GET PORT_DATA_SET", domain); |
| 1484 |
1/2✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
|
72 | std::istringstream pss(portOutput); |
| 1485 |
3/6✓ Branch 0 taken 288 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 288 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 288 times.
✗ Branch 5 not taken.
|
288 | while (std::getline(pss, line)) { |
| 1486 |
1/2✓ Branch 0 taken 288 times.
✗ Branch 1 not taken.
|
288 | std::istringstream ls(line); |
| 1487 | 288 | std::string key; | |
| 1488 |
1/2✓ Branch 0 taken 288 times.
✗ Branch 1 not taken.
|
288 | ls >> key; |
| 1489 |
3/4✓ Branch 0 taken 288 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
✓ Branch 3 taken 216 times.
|
288 | if (key == "portState") { |
| 1490 | 72 | std::string val; | |
| 1491 |
1/2✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
|
72 | ls >> val; |
| 1492 |
1/2✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
|
72 | if (!val.empty()) { |
| 1493 |
1/2✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
|
72 | fresh.portState = val; |
| 1494 | 72 | break; | |
| 1495 | } | ||
| 1496 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 72 times.
|
72 | } |
| 1497 |
4/4✓ Branch 0 taken 216 times.
✓ Branch 1 taken 72 times.
✓ Branch 2 taken 216 times.
✓ Branch 3 taken 72 times.
|
360 | } |
| 1498 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 72 times.
|
72 | if (fresh.portState.empty()) { |
| 1499 | ✗ | fresh.portState = "running (state unknown)"; | |
| 1500 | } | ||
| 1501 | |||
| 1502 |
1/2✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
|
72 | m_ptpCache = fresh; |
| 1503 |
4/4✓ Branch 0 taken 72 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 72 times.
✓ Branch 3 taken 272 times.
|
346 | } |
| 1504 | |||
| 1505 | 147 | std::string AES67Manager::GetPtp4lState() { | |
| 1506 |
1/2✓ Branch 0 taken 147 times.
✗ Branch 1 not taken.
|
147 | RefreshPtpCache(); |
| 1507 |
1/2✓ Branch 0 taken 147 times.
✗ Branch 1 not taken.
|
147 | std::lock_guard<std::mutex> lock(m_ptpCacheMutex); |
| 1508 |
1/2✓ Branch 0 taken 147 times.
✗ Branch 1 not taken.
|
294 | return m_ptpCache.portState; |
| 1509 | 147 | } | |
| 1510 | |||
| 1511 | // Is the PTP clock one that will not step under the RTP anchors? See | ||
| 1512 | // AES67::PTP_LOCK_OFFSET_NS for what went wrong without asking this and why | ||
| 1513 | // the offset -- not the port state -- is the signal. | ||
| 1514 | // | ||
| 1515 | // Two callers: WaitForPtpLock() spins on it before the anchors are built, and | ||
| 1516 | // ReviewPtpNoLockWarning() asks it again afterwards, so that a clock which | ||
| 1517 | // settles late is noticed rather than left described by a warning nobody | ||
| 1518 | // retracts. One definition, so the two cannot disagree about what "settled" | ||
| 1519 | // means. | ||
| 1520 | // | ||
| 1521 | // Three outcomes count as settled: | ||
| 1522 | // | ||
| 1523 | // MASTER / GRAND_MASTER -- we won the BMCA, so the PHC *is* the domain's | ||
| 1524 | // time and there is nothing for ptp4l to step it towards. Returns as | ||
| 1525 | // soon as the election is decided rather than burning the whole cap, | ||
| 1526 | // which matters because declaring yourself master takes announceReceipt- | ||
| 1527 | // Timeout x the announce interval (3 x 2s here) of listening first. | ||
| 1528 | // | ||
| 1529 | // SLAVE with a small offset from master -- ptp4l has measured against the | ||
| 1530 | // grandmaster and the correction it needed has already been applied. | ||
| 1531 | // Both halves have to be evidence rather than a default: a gmPresent | ||
| 1532 | // that actually read "true" and a master_offset that actually parsed. | ||
| 1533 | // A missing or malformed management field leaves the offset at zero, | ||
| 1534 | // which is indistinguishable from a perfect lock -- and zero is exactly | ||
| 1535 | // what #2848 settled on before the clock stepped 6.81 hours. | ||
| 1536 | // | ||
| 1537 | // LISTENING with no grandmaster on the domain, once the cap has run out -- | ||
| 1538 | // there is nothing to be stepped towards, so the free-running PHC is as | ||
| 1539 | // settled as it is going to get. "No grandmaster" means gmPresent was | ||
| 1540 | // read and said false; a management query that failed outright says | ||
| 1541 | // nothing about the domain and is not an answer. This one is only | ||
| 1542 | // tested at the deadline: during the wait "no announce seen" and "no | ||
| 1543 | // announce yet" look identical, and returning on it early would defeat | ||
| 1544 | // the whole point on a follower whose grandmaster had not spoken yet. | ||
| 1545 | // It matters because a box that is about to become grandmaster spends | ||
| 1546 | // announceReceiptTimeout x the announce interval -- 3 x 2s here -- | ||
| 1547 | // listening first, so a solo FPP would otherwise time out and cry wolf | ||
| 1548 | // on every single boot. Measured on a Pi 5 with nothing else on the | ||
| 1549 | // domain: LISTENING throughout, gmPresent false throughout, and | ||
| 1550 | // "assuming the grand master role" at t=7.46s -- well past the cap. | ||
| 1551 | // UNCALIBRATED or a wide SLAVE offset at the deadline is the opposite | ||
| 1552 | // case: a grandmaster is there and we have not caught up with it yet, | ||
| 1553 | // which is exactly when the caller should worry. | ||
| 1554 | // | ||
| 1555 | // Everything else is not settled, and the caller decides what to do about it. | ||
| 1556 | // The third outcome is returned as its own value rather than folded into | ||
| 1557 | // "settled" precisely because of the deadline caveat above: only a caller that | ||
| 1558 | // knows it has waited long enough may accept it. | ||
| 1559 | 58 | AES67Manager::PtpSettleState AES67Manager::PtpSettledNow(std::string* portStateOut, | |
| 1560 | int64_t* offsetNsOut) { | ||
| 1561 | // force: the whole point is the live answer, and the cache is a second | ||
| 1562 | // deep -- four polls of the same stale reading would just burn the cap. | ||
| 1563 |
1/2✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
|
58 | RefreshPtpCache(true); |
| 1564 | 58 | std::string portState; | |
| 1565 | 58 | int64_t offsetNs = 0; | |
| 1566 | 58 | bool gmPresent = false; | |
| 1567 | 58 | bool gmPresentValid = false; | |
| 1568 | 58 | bool offsetValid = false; | |
| 1569 | { | ||
| 1570 |
1/2✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
|
58 | std::lock_guard<std::mutex> lock(m_ptpCacheMutex); |
| 1571 |
1/2✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
|
58 | portState = m_ptpCache.portState; |
| 1572 | 58 | offsetNs = m_ptpCache.offsetNs; | |
| 1573 | 58 | gmPresent = m_ptpCache.gmPresent; | |
| 1574 | 58 | gmPresentValid = m_ptpCache.gmPresentValid; | |
| 1575 | 58 | offsetValid = m_ptpCache.offsetValid; | |
| 1576 | 58 | } | |
| 1577 |
1/2✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
|
58 | if (portStateOut) { |
| 1578 |
1/2✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
|
58 | *portStateOut = portState; |
| 1579 | } | ||
| 1580 |
2/2✓ Branch 0 taken 57 times.
✓ Branch 1 taken 1 times.
|
58 | if (offsetNsOut) { |
| 1581 | 57 | *offsetNsOut = offsetNs; | |
| 1582 | } | ||
| 1583 | |||
| 1584 |
3/4✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 57 times.
|
58 | if (IsGrandmasterPortState(portState)) { |
| 1585 | 1 | return PtpSettleState::Grandmaster; | |
| 1586 | } | ||
| 1587 | // free_running: whatever ptp4l measures, it never applies, so there is | ||
| 1588 | // nothing to wait for and nothing that will step the clock later. | ||
| 1589 |
3/4✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 56 times.
|
57 | if (PtpFreeRunning()) { |
| 1590 | 1 | return PtpSettleState::FreeRunning; | |
| 1591 | } | ||
| 1592 | // Missing/malformed management data is not a zero offset. Compare | ||
| 1593 | // signed bounds directly so INT64_MIN cannot overflow on negation. | ||
| 1594 |
7/10✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
✓ Branch 3 taken 14 times.
✓ Branch 4 taken 23 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 21 times.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
|
93 | if (portState == "SLAVE" && gmPresentValid && gmPresent && offsetValid && |
| 1595 |
5/6✓ Branch 0 taken 37 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 54 times.
|
93 | offsetNs > -AES67::PTP_LOCK_OFFSET_NS && |
| 1596 | offsetNs < AES67::PTP_LOCK_OFFSET_NS) { | ||
| 1597 | 2 | return PtpSettleState::SlaveLocked; | |
| 1598 | } | ||
| 1599 |
7/10✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 47 times.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 7 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 7 times.
✓ Branch 9 taken 47 times.
|
54 | if (portState == "LISTENING" && gmPresentValid && !gmPresent) { |
| 1600 | 7 | return PtpSettleState::NoGrandmasterYet; | |
| 1601 | } | ||
| 1602 | 47 | return PtpSettleState::NotSettled; | |
| 1603 | 58 | } | |
| 1604 | |||
| 1605 | // Block on PtpSettledNow() above until it says the clock has settled, or until | ||
| 1606 | // the caller's patience runs out. Returns true if it settled. | ||
| 1607 | 11 | bool AES67Manager::WaitForPtpLock(int timeoutMs) { | |
| 1608 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | if (timeoutMs <= 0) { |
| 1609 | ✗ | return false; | |
| 1610 | } | ||
| 1611 | // Every answer here comes from pmc. Without it the wait can only ever | ||
| 1612 | // time out, so spending the cap to find that out helps nobody -- say so | ||
| 1613 | // once and carry on. (linuxptp ships both binaries, and InitPTP() has | ||
| 1614 | // already required ptp4l, so this is a broken install rather than an | ||
| 1615 | // ordinary one.) | ||
| 1616 |
2/4✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 11 times.
|
11 | if (!FileExists(PMC_BINARY)) { |
| 1617 | ✗ | LogWarn(VB_MEDIAOUT, | |
| 1618 | "AES67Manager: %s is missing — cannot tell whether PTP has " | ||
| 1619 | "locked, so not waiting for it\n", PMC_BINARY); | ||
| 1620 | ✗ | return false; | |
| 1621 | } | ||
| 1622 | 11 | const auto start = std::chrono::steady_clock::now(); | |
| 1623 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | const auto deadline = start + std::chrono::milliseconds(timeoutMs); |
| 1624 | |||
| 1625 | 11 | std::string reported; | |
| 1626 | for (;;) { | ||
| 1627 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
|
57 | if (m_stopping.load()) { |
| 1628 | ✗ | return false; | |
| 1629 | } | ||
| 1630 |
2/4✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 57 times.
|
57 | if (!IsPtp4lRunning()) { |
| 1631 | ✗ | LogWarn(VB_MEDIAOUT, | |
| 1632 | "AES67Manager: ptp4l exited while waiting for it to lock\n"); | ||
| 1633 | ✗ | return false; | |
| 1634 | } | ||
| 1635 | |||
| 1636 | 57 | std::string portState; | |
| 1637 | 57 | int64_t offsetNs = 0; | |
| 1638 |
1/2✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
|
57 | const PtpSettleState settled = PtpSettledNow(&portState, &offsetNs); |
| 1639 | |||
| 1640 | // One line per transition, not per poll: this runs four times a second | ||
| 1641 | // and the interesting thing is LISTENING -> UNCALIBRATED -> SLAVE. | ||
| 1642 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 46 times.
|
57 | if (portState != reported) { |
| 1643 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | reported = portState; |
| 1644 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | LogInfo(VB_MEDIAOUT, "AES67Manager: waiting for PTP — %s\n", |
| 1645 | portState.c_str()); | ||
| 1646 | } | ||
| 1647 | |||
| 1648 | ✗ | const double waitedS = std::chrono::duration<double>( | |
| 1649 |
2/4✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 57 times.
✗ Branch 3 not taken.
|
57 | std::chrono::steady_clock::now() - start).count(); |
| 1650 | |||
| 1651 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 56 times.
|
57 | if (settled == PtpSettleState::Grandmaster) { |
| 1652 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogInfo(VB_MEDIAOUT, |
| 1653 | "AES67Manager: PTP settled after %.1fs — this node is the " | ||
| 1654 | "grandmaster, so the clock is already the domain's time\n", | ||
| 1655 | waitedS); | ||
| 1656 | 1 | return true; | |
| 1657 | } | ||
| 1658 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 55 times.
|
56 | if (settled == PtpSettleState::SlaveLocked) { |
| 1659 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogInfo(VB_MEDIAOUT, |
| 1660 | "AES67Manager: PTP settled after %.1fs — SLAVE, offset " | ||
| 1661 | "%+lldns\n", waitedS, (long long)offsetNs); | ||
| 1662 | 1 | return true; | |
| 1663 | } | ||
| 1664 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 54 times.
|
55 | if (settled == PtpSettleState::FreeRunning) { |
| 1665 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogInfo(VB_MEDIAOUT, |
| 1666 | "AES67Manager: PTP is free-running (software timestamping) — " | ||
| 1667 | "it will not move the clock, so not waiting for a lock\n"); | ||
| 1668 | 1 | return true; | |
| 1669 | } | ||
| 1670 | |||
| 1671 | 54 | const auto now = std::chrono::steady_clock::now(); | |
| 1672 |
3/4✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 46 times.
|
54 | if (now >= deadline) { |
| 1673 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7 times.
|
8 | if (settled == PtpSettleState::NoGrandmasterYet) { |
| 1674 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | LogInfo(VB_MEDIAOUT, |
| 1675 | "AES67Manager: no PTP grandmaster on domain %d after " | ||
| 1676 | "%.1fs — nothing to step this clock, carrying on\n", | ||
| 1677 | GetPtpDomain(), waitedS); | ||
| 1678 | 1 | return true; | |
| 1679 | } | ||
| 1680 | 7 | return false; | |
| 1681 | } | ||
| 1682 | 46 | auto slice = std::chrono::milliseconds(AES67::PTP_LOCK_POLL_MS); | |
| 1683 |
1/2✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
|
46 | const auto left = std::chrono::duration_cast<std::chrono::milliseconds>( |
| 1684 |
1/2✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
|
46 | deadline - now); |
| 1685 |
1/2✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
|
46 | std::this_thread::sleep_for(std::min(slice, left)); |
| 1686 |
2/2✓ Branch 0 taken 46 times.
✓ Branch 1 taken 11 times.
|
103 | } |
| 1687 | 11 | } | |
| 1688 | |||
| 1689 | // Queries the *actual* domain grandmaster via `pmc GET TIME_STATUS_NP` — | ||
| 1690 | // this is the remote/upstream clock ptp4l has selected via BMCA, which may | ||
| 1691 | // or may not be this node. GetPTPClockId() (below) only ever returns this | ||
| 1692 | // node's own identity and must not be used to report "who is the master". | ||
| 1693 | 825 | bool AES67Manager::QueryPtp4lTimeStatus(bool& gmPresent, std::string& gmIdentity, int64_t& offsetNs, | |
| 1694 | bool* offsetValid) { | ||
| 1695 |
3/4✓ Branch 0 taken 825 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 688 times.
✓ Branch 3 taken 137 times.
|
825 | if (!IsPtp4lRunning()) { |
| 1696 | 688 | return false; | |
| 1697 | } | ||
| 1698 |
1/2✓ Branch 0 taken 137 times.
✗ Branch 1 not taken.
|
137 | RefreshPtpCache(); |
| 1699 | |||
| 1700 |
1/2✓ Branch 0 taken 137 times.
✗ Branch 1 not taken.
|
137 | std::lock_guard<std::mutex> lock(m_ptpCacheMutex); |
| 1701 |
3/6✓ Branch 0 taken 137 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 137 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 137 times.
|
137 | if (!m_ptpCache.valid || m_ptpCache.gmIdentity.empty()) { |
| 1702 | ✗ | return false; | |
| 1703 | } | ||
| 1704 | 137 | gmPresent = m_ptpCache.gmPresent; | |
| 1705 |
1/2✓ Branch 0 taken 137 times.
✗ Branch 1 not taken.
|
137 | gmIdentity = m_ptpCache.gmIdentity; |
| 1706 | 137 | offsetNs = m_ptpCache.offsetNs; | |
| 1707 |
2/2✓ Branch 0 taken 136 times.
✓ Branch 1 taken 1 times.
|
137 | if (offsetValid) { |
| 1708 | 136 | *offsetValid = m_ptpCache.offsetValid; | |
| 1709 | } | ||
| 1710 | 137 | return true; | |
| 1711 | 137 | } | |
| 1712 | |||
| 1713 | // The clock identity to advertise/report: the upstream grandmaster we follow, | ||
| 1714 | // or our own identity when we hold the role. Empty while ptp4l is still | ||
| 1715 | // converging, so callers can avoid announcing a refclk that is about to | ||
| 1716 | // change. | ||
| 1717 | 1636 | std::string AES67Manager::GetActiveGrandmasterId() { | |
| 1718 |
8/10✓ Branch 0 taken 1636 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 94 times.
✓ Branch 3 taken 1542 times.
✓ Branch 4 taken 94 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 93 times.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 1635 times.
✓ Branch 9 taken 1 times.
|
1636 | if (!IsPtpEnabled() || !IsPtp4lRunning()) { |
| 1719 |
1/2✓ Branch 0 taken 1635 times.
✗ Branch 1 not taken.
|
3270 | return ""; |
| 1720 | } | ||
| 1721 | |||
| 1722 | 1 | bool gmPresent = false; | |
| 1723 | 1 | std::string gmId; | |
| 1724 | 1 | int64_t offsetNs = 0; | |
| 1725 |
5/10✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ 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.
|
1 | if (QueryPtp4lTimeStatus(gmPresent, gmId, offsetNs) && gmPresent && !gmId.empty()) { |
| 1726 | 1 | return gmId; | |
| 1727 | } | ||
| 1728 | ✗ | if (IsGrandmasterPortState(GetPtp4lState())) { | |
| 1729 | // We won the BMCA -- we are the refclk. | ||
| 1730 | ✗ | return GetPTPClockId(); | |
| 1731 | } | ||
| 1732 | ✗ | return ""; | |
| 1733 | 1 | } | |
| 1734 | |||
| 1735 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 1736 | // PTP Announce listener — resolves a grandmaster identity to an IP address | ||
| 1737 | // | ||
| 1738 | // "Which box is the clock?" is the first question at a commissioning, and a | ||
| 1739 | // clock identity does not answer it: it is an EUI-64 built from *a* MAC on the | ||
| 1740 | // device, which need not be the interface carrying PTP and cannot be looked up | ||
| 1741 | // in ARP. Nothing in PTP management gives the address either -- TIME_STATUS_NP | ||
| 1742 | // names the grandmaster by identity only. | ||
| 1743 | // | ||
| 1744 | // The address is therefore taken from where the Announce messages arrive from. | ||
| 1745 | // This is a passive second reader on the group ptp4l is already joined to: | ||
| 1746 | // SO_REUSEADDR (never SO_REUSEPORT, which would load-balance the flow away | ||
| 1747 | // from ptp4l instead of duplicating it) means both sockets get every packet. | ||
| 1748 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 1749 | |||
| 1750 | // Parses one PTPv2 Announce and records the address it came from against the | ||
| 1751 | // grandmaster it advertises. Anything else on the group is ignored. | ||
| 1752 | 4 | void AES67Manager::HandlePtpAnnounce(const uint8_t* data, size_t len, | |
| 1753 | const std::string& senderAddr) { | ||
| 1754 | // PTPv2 common header is 34 bytes. Within the Announce body: | ||
| 1755 | // originTimestamp(10) currentUtcOffset(2) reserved(1) priority1(1) | ||
| 1756 | // clockQuality(4) priority2(1) grandmasterIdentity(8) stepsRemoved(2). | ||
| 1757 | 4 | constexpr size_t PTP_HEADER_LEN = 34; | |
| 1758 | 4 | constexpr size_t GM_ID_OFFSET = PTP_HEADER_LEN + 19; // 53 | |
| 1759 | 4 | constexpr size_t STEPS_OFFSET = PTP_HEADER_LEN + 27; // 61 | |
| 1760 | 4 | constexpr uint8_t MSG_ANNOUNCE = 0x0B; | |
| 1761 | |||
| 1762 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (len < STEPS_OFFSET + 2) { |
| 1763 | 2 | return; | |
| 1764 | } | ||
| 1765 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if ((data[0] & 0x0F) != MSG_ANNOUNCE) { |
| 1766 | ✗ | return; | |
| 1767 | } | ||
| 1768 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if ((data[1] & 0x0F) != 2) { // versionPTP |
| 1769 | ✗ | return; | |
| 1770 | } | ||
| 1771 | // Other domains share the multicast group; their grandmaster is not ours. | ||
| 1772 |
3/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 3 times.
|
4 | if (data[4] != (uint8_t)GetPtpDomain()) { |
| 1773 | 1 | return; | |
| 1774 | } | ||
| 1775 | |||
| 1776 | char gmId[24]; | ||
| 1777 | 3 | snprintf(gmId, sizeof(gmId), "%02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X", | |
| 1778 | 3 | data[GM_ID_OFFSET], data[GM_ID_OFFSET + 1], data[GM_ID_OFFSET + 2], | |
| 1779 | 3 | data[GM_ID_OFFSET + 3], data[GM_ID_OFFSET + 4], data[GM_ID_OFFSET + 5], | |
| 1780 | 3 | data[GM_ID_OFFSET + 6], data[GM_ID_OFFSET + 7]); | |
| 1781 | |||
| 1782 | 3 | uint16_t stepsRemoved = ((uint16_t)data[STEPS_OFFSET] << 8) | data[STEPS_OFFSET + 1]; | |
| 1783 | |||
| 1784 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | int64_t nowMs = std::chrono::duration_cast<std::chrono::milliseconds>( |
| 1785 | 6 | std::chrono::steady_clock::now().time_since_epoch()).count(); | |
| 1786 | |||
| 1787 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | std::lock_guard<std::mutex> lock(m_ptpAnnounceMutex); |
| 1788 | |||
| 1789 | // A boundary clock relays the domain under the real grandmaster's | ||
| 1790 | // identity, so its address is the nearest master rather than the clock | ||
| 1791 | // itself. Never let one overwrite an address heard direct from the | ||
| 1792 | // grandmaster -- that is the one the user is looking for. | ||
| 1793 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
6 | auto it = m_ptpAnnounceSources.find(gmId); |
| 1794 |
7/8✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 2 times.
|
4 | if (it != m_ptpAnnounceSources.end() && it->second.direct && stepsRemoved > 0 && |
| 1795 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | (nowMs - it->second.lastSeenMs) < AES67::PTP_ANNOUNCE_STALE_MS) { |
| 1796 | 1 | return; | |
| 1797 | } | ||
| 1798 | |||
| 1799 | 2 | PtpAnnounceSource src; | |
| 1800 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | src.address = senderAddr; |
| 1801 | 2 | src.direct = (stepsRemoved == 0); | |
| 1802 | 2 | src.lastSeenMs = nowMs; | |
| 1803 | |||
| 1804 | // Drop clocks that have stopped announcing. A domain holds a handful of | ||
| 1805 | // masters at most, but BMCA churn would otherwise accumulate them for the | ||
| 1806 | // life of the process. | ||
| 1807 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | for (auto i = m_ptpAnnounceSources.begin(); i != m_ptpAnnounceSources.end();) { |
| 1808 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if ((nowMs - i->second.lastSeenMs) > 10 * AES67::PTP_ANNOUNCE_STALE_MS) { |
| 1809 | ✗ | i = m_ptpAnnounceSources.erase(i); | |
| 1810 | } else { | ||
| 1811 | 1 | ++i; | |
| 1812 | } | ||
| 1813 | } | ||
| 1814 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
4 | it = m_ptpAnnounceSources.find(gmId); // the prune may have erased it |
| 1815 |
4/6✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
|
3 | if (it == m_ptpAnnounceSources.end() && |
| 1816 | 1 | m_ptpAnnounceSources.size() >= AES67::MAX_PTP_ANNOUNCE_SOURCES) { | |
| 1817 | ✗ | auto oldest = std::min_element(m_ptpAnnounceSources.begin(), m_ptpAnnounceSources.end(), | |
| 1818 | ✗ | [](const auto& a, const auto& b) { | |
| 1819 | ✗ | return a.second.lastSeenMs < b.second.lastSeenMs; | |
| 1820 | }); | ||
| 1821 | ✗ | m_ptpAnnounceSources.erase(oldest); | |
| 1822 | } | ||
| 1823 |
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.
|
4 | m_ptpAnnounceSources[gmId] = src; |
| 1824 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | } |
| 1825 | |||
| 1826 | 12 | bool AES67Manager::GetGrandmasterAddress(const std::string& gmId, std::string& address, | |
| 1827 | bool& viaBoundary) { | ||
| 1828 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (gmId.empty()) { |
| 1829 | ✗ | return false; | |
| 1830 | } | ||
| 1831 | |||
| 1832 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | int64_t nowMs = std::chrono::duration_cast<std::chrono::milliseconds>( |
| 1833 | 24 | std::chrono::steady_clock::now().time_since_epoch()).count(); | |
| 1834 | |||
| 1835 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | std::lock_guard<std::mutex> lock(m_ptpAnnounceMutex); |
| 1836 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | auto it = m_ptpAnnounceSources.find(gmId); |
| 1837 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3 times.
|
12 | if (it == m_ptpAnnounceSources.end()) { |
| 1838 | 9 | return false; | |
| 1839 | } | ||
| 1840 | // Stale means the clock we are reporting is no longer announcing from | ||
| 1841 | // there -- a changeover, or the listener socket having been shut out. | ||
| 1842 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if ((nowMs - it->second.lastSeenMs) > AES67::PTP_ANNOUNCE_STALE_MS) { |
| 1843 | ✗ | return false; | |
| 1844 | } | ||
| 1845 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | address = it->second.address; |
| 1846 | 3 | viaBoundary = !it->second.direct; | |
| 1847 | 3 | return true; | |
| 1848 | 12 | } | |
| 1849 | |||
| 1850 | 41 | void AES67Manager::PtpAnnounceListenLoop() { | |
| 1851 |
1/2✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
|
41 | LogInfo(VB_MEDIAOUT, "AES67 PTP announce listener thread started\n"); |
| 1852 | |||
| 1853 | 41 | int sock = OpenSocket(AF_INET, SOCK_DGRAM, 0); | |
| 1854 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
|
41 | if (sock < 0) { |
| 1855 | ✗ | LogWarn(VB_MEDIAOUT, "AES67 PTP announce: socket failed: %s\n", FPPstrerror(errno)); | |
| 1856 | ✗ | return; | |
| 1857 | } | ||
| 1858 | |||
| 1859 | // SO_REUSEADDR only: ptp4l holds this port and multicast is delivered to | ||
| 1860 | // every socket bound to it. SO_REUSEPORT would instead put us in a | ||
| 1861 | // load-balancing group with ptp4l and steal its Announce messages. | ||
| 1862 | 41 | int reuse = 1; | |
| 1863 | 41 | setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)); | |
| 1864 | |||
| 1865 | struct sockaddr_in bindAddr; | ||
| 1866 | 41 | memset(&bindAddr, 0, sizeof(bindAddr)); | |
| 1867 | 41 | bindAddr.sin_family = AF_INET; | |
| 1868 | 41 | bindAddr.sin_port = htons(AES67::PTP_GENERAL_PORT); | |
| 1869 | 41 | bindAddr.sin_addr.s_addr = htonl(INADDR_ANY); | |
| 1870 | |||
| 1871 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
|
41 | if (bind(sock, (struct sockaddr*)&bindAddr, sizeof(bindAddr)) < 0) { |
| 1872 | // Not fatal to anything: PTP itself is ptp4l's socket, so all that is | ||
| 1873 | // lost here is the grandmaster's address in the status display. | ||
| 1874 | ✗ | LogWarn(VB_MEDIAOUT, "AES67 PTP announce: bind to port %d failed (%s) — " | |
| 1875 | "grandmaster address will not be shown\n", | ||
| 1876 | AES67::PTP_GENERAL_PORT, FPPstrerror(errno)); | ||
| 1877 | ✗ | close(sock); | |
| 1878 | ✗ | return; | |
| 1879 | } | ||
| 1880 | |||
| 1881 | struct ip_mreq mreq; | ||
| 1882 | 41 | memset(&mreq, 0, sizeof(mreq)); | |
| 1883 | 41 | inet_pton(AF_INET, AES67::PTP_MCAST_ADDRESS, &mreq.imr_multiaddr); | |
| 1884 | |||
| 1885 |
1/2✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
|
41 | std::string iface = GetPtpInterface(); |
| 1886 |
1/2✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
|
41 | if (!iface.empty()) { |
| 1887 |
1/2✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
|
41 | std::string ifIP = GetInterfaceIP(iface); |
| 1888 | 41 | inet_pton(AF_INET, ifIP.c_str(), &mreq.imr_interface); | |
| 1889 | 41 | } else { | |
| 1890 | ✗ | mreq.imr_interface.s_addr = htonl(INADDR_ANY); | |
| 1891 | } | ||
| 1892 | |||
| 1893 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
|
41 | if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) { |
| 1894 | ✗ | LogWarn(VB_MEDIAOUT, "AES67 PTP announce: join %s failed: %s\n", | |
| 1895 | AES67::PTP_MCAST_ADDRESS, FPPstrerror(errno)); | ||
| 1896 | ✗ | close(sock); | |
| 1897 | ✗ | return; | |
| 1898 | } | ||
| 1899 | |||
| 1900 | // Bounds how long ShutdownPTP() waits on the join. | ||
| 1901 | struct timeval tv; | ||
| 1902 | 41 | tv.tv_sec = 1; | |
| 1903 | 41 | tv.tv_usec = 0; | |
| 1904 | 41 | setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); | |
| 1905 | |||
| 1906 | uint8_t buf[512]; | ||
| 1907 |
2/2✓ Branch 0 taken 286 times.
✓ Branch 1 taken 41 times.
|
327 | while (m_ptpAnnounceRunning.load()) { |
| 1908 | struct sockaddr_in senderAddr; | ||
| 1909 | 286 | socklen_t addrLen = sizeof(senderAddr); | |
| 1910 | |||
| 1911 |
1/2✓ Branch 0 taken 286 times.
✗ Branch 1 not taken.
|
286 | ssize_t n = recvfrom(sock, buf, sizeof(buf), 0, |
| 1912 | (struct sockaddr*)&senderAddr, &addrLen); | ||
| 1913 |
2/2✓ Branch 0 taken 282 times.
✓ Branch 1 taken 4 times.
|
286 | if (n <= 0) continue; // timeout or error |
| 1914 | |||
| 1915 | char senderIP[INET_ADDRSTRLEN]; | ||
| 1916 | 4 | inet_ntop(AF_INET, &senderAddr.sin_addr, senderIP, sizeof(senderIP)); | |
| 1917 | |||
| 1918 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
8 | HandlePtpAnnounce(buf, (size_t)n, senderIP); |
| 1919 | } | ||
| 1920 | |||
| 1921 | 41 | setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)); | |
| 1922 |
1/2✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
|
41 | close(sock); |
| 1923 |
1/2✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
|
41 | LogInfo(VB_MEDIAOUT, "AES67 PTP announce listener thread stopped\n"); |
| 1924 |
1/2✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
|
41 | } |
| 1925 | |||
| 1926 | 1639 | std::string AES67Manager::GetPTPClockId() { | |
| 1927 | // Derive EUI-64 clock ID from interface MAC address | ||
| 1928 | // Read /sys/class/net/<iface>/address → AA:BB:CC:DD:EE:FF | ||
| 1929 | // Insert FF:FE → AA-BB-CC-FF-FE-DD-EE-FF | ||
| 1930 |
3/6✓ Branch 0 taken 1639 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1639 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1639 times.
✗ Branch 5 not taken.
|
1639 | std::string macPath = "/sys/class/net/" + GetPtpInterface() + "/address"; |
| 1931 |
1/2✓ Branch 0 taken 1639 times.
✗ Branch 1 not taken.
|
1639 | std::ifstream macFile(macPath); |
| 1932 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1639 times.
|
1639 | if (!macFile.is_open()) { |
| 1933 | ✗ | LogWarn(VB_MEDIAOUT, "AES67Manager: Cannot read MAC from %s\n", macPath.c_str()); | |
| 1934 | ✗ | return "00-00-00-FF-FE-00-00-00"; | |
| 1935 | } | ||
| 1936 | |||
| 1937 | 1639 | std::string mac; | |
| 1938 |
1/2✓ Branch 0 taken 1639 times.
✗ Branch 1 not taken.
|
1639 | std::getline(macFile, mac); |
| 1939 |
1/2✓ Branch 0 taken 1639 times.
✗ Branch 1 not taken.
|
1639 | macFile.close(); |
| 1940 | |||
| 1941 | // Parse MAC: "aa:bb:cc:dd:ee:ff" | ||
| 1942 | 1639 | unsigned int m[6] = {0}; | |
| 1943 | 1639 | if (sscanf(mac.c_str(), "%x:%x:%x:%x:%x:%x", | |
| 1944 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1639 times.
|
1639 | &m[0], &m[1], &m[2], &m[3], &m[4], &m[5]) != 6) { |
| 1945 | ✗ | return "00-00-00-FF-FE-00-00-00"; | |
| 1946 | } | ||
| 1947 | |||
| 1948 | // EUI-64: m0-m1-m2-FF-FE-m3-m4-m5 | ||
| 1949 | char eui64[24]; | ||
| 1950 | 1639 | snprintf(eui64, sizeof(eui64), "%02X-%02X-%02X-FF-FE-%02X-%02X-%02X", | |
| 1951 | m[0], m[1], m[2], m[3], m[4], m[5]); | ||
| 1952 |
1/2✓ Branch 0 taken 1639 times.
✗ Branch 1 not taken.
|
3278 | return std::string(eui64); |
| 1953 | 1639 | } | |
| 1954 | |||
| 1955 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 1956 | // AES67 media clock | ||
| 1957 | // | ||
| 1958 | // AES67 requires the RTP timestamp to be the PTP media clock count, and the SDP | ||
| 1959 | // we publish asserts exactly that with "a=mediaclk:direct=0". Honouring it | ||
| 1960 | // means the pipeline has to be driven by PTP time itself, not by GStreamer's | ||
| 1961 | // default monotonic system clock. | ||
| 1962 | // | ||
| 1963 | // Which clock actually holds PTP time depends on how ptp4l is running, and it | ||
| 1964 | // has to be right in BOTH roles: | ||
| 1965 | // | ||
| 1966 | // hardware timestamping -- ptp4l's clock is the NIC's PHC. As a follower it | ||
| 1967 | // disciplines the PHC to the grandmaster; as grandmaster it distributes | ||
| 1968 | // the PHC's own time. Either way the PHC is the domain's time, so we | ||
| 1969 | // read the PHC. | ||
| 1970 | // | ||
| 1971 | // software timestamping -- there is no PHC; ptp4l uses CLOCK_REALTIME as its | ||
| 1972 | // clock and runs free_running, so it never adjusts it (see | ||
| 1973 | // WritePtpConf()). As grandmaster CLOCK_REALTIME is the domain's time. | ||
| 1974 | // As a follower it is not: the streams run on NTP time, unlocked from | ||
| 1975 | // the grandmaster, which is the price of not letting PTP set the | ||
| 1976 | // system clock. | ||
| 1977 | // | ||
| 1978 | // Note what we deliberately do NOT use: | ||
| 1979 | // | ||
| 1980 | // CLOCK_TAI / GST_CLOCK_TYPE_TAI -- only correct while something is setting | ||
| 1981 | // the kernel TAI offset, which phc2sys does as a follower and not at all | ||
| 1982 | // as grandmaster. Measured on a grandmaster box: adjtimex offset 0 and | ||
| 1983 | // CLOCK_TAI == CLOCK_REALTIME, i.e. silently not PTP time. | ||
| 1984 | // | ||
| 1985 | // GstPtpClock (gst_ptp_init) -- would run gst-ptp-helper as a second PTP | ||
| 1986 | // client on the same domain alongside our ptp4l, adding a participant to | ||
| 1987 | // a network we just stopped FPP from disrupting. | ||
| 1988 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 1989 | |||
| 1990 | // Linux exposes a dynamic POSIX clock for an open /dev/ptpN fd. | ||
| 1991 | #define FPP_FD_TO_CLOCKID(fd) ((clockid_t)((((unsigned int)~(fd)) << 3) | 3)) | ||
| 1992 | |||
| 1993 | // Resolve the PHC backing an interface via ETHTOOL_GET_TS_INFO. Returns -1 | ||
| 1994 | // when the NIC has no PHC (software timestamping), which is not an error. | ||
| 1995 | 115 | static int PhcIndexForInterface(const std::string& iface) { | |
| 1996 | #ifndef FPP_AES67_HAS_ETHTOOL | ||
| 1997 | return -1; | ||
| 1998 | #else | ||
| 1999 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 115 times.
|
115 | if (iface.empty()) { |
| 2000 | ✗ | return -1; | |
| 2001 | } | ||
| 2002 | 115 | int sock = OpenSocket(AF_INET, SOCK_DGRAM, 0); | |
| 2003 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 115 times.
|
115 | if (sock < 0) { |
| 2004 | ✗ | return -1; | |
| 2005 | } | ||
| 2006 | struct ethtool_ts_info tsi; | ||
| 2007 | 115 | memset(&tsi, 0, sizeof(tsi)); | |
| 2008 | 115 | tsi.cmd = ETHTOOL_GET_TS_INFO; | |
| 2009 | |||
| 2010 | struct ifreq ifr; | ||
| 2011 | 115 | memset(&ifr, 0, sizeof(ifr)); | |
| 2012 | 115 | snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", iface.c_str()); | |
| 2013 | 115 | ifr.ifr_data = (char*)&tsi; | |
| 2014 | |||
| 2015 | 115 | int idx = -1; | |
| 2016 |
1/2✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
|
115 | if (ioctl(sock, SIOCETHTOOL, &ifr) == 0) { |
| 2017 | 115 | idx = tsi.phc_index; | |
| 2018 | } | ||
| 2019 |
1/2✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
|
115 | close(sock); |
| 2020 | 115 | return idx; | |
| 2021 | #endif | ||
| 2022 | } | ||
| 2023 | |||
| 2024 | // GstClock reading PTP time. Subclasses GstSystemClock so that all of its | ||
| 2025 | // wait/scheduling machinery keeps working -- only the time source changes, | ||
| 2026 | // since GstSystemClock routes its waits through the virtual get_internal_time. | ||
| 2027 | struct FppPtpClock { | ||
| 2028 | GstSystemClock parent; | ||
| 2029 | clockid_t clkid; // PHC dynamic clockid, or CLOCK_REALTIME | ||
| 2030 | int phcFd; // open /dev/ptpN, or -1 when using CLOCK_REALTIME | ||
| 2031 | }; | ||
| 2032 | struct FppPtpClockClass { | ||
| 2033 | GstSystemClockClass parent_class; | ||
| 2034 | }; | ||
| 2035 | |||
| 2036 |
6/7✓ Branch 0 taken 58 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 58 times.
✓ Branch 3 taken 58 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 58 times.
✓ Branch 6 taken 19 times.
|
386 | G_DEFINE_TYPE(FppPtpClock, fpp_ptp_clock, GST_TYPE_SYSTEM_CLOCK) |
| 2037 | |||
| 2038 | 20847 | static GstClockTime fpp_ptp_clock_get_internal_time(GstClock* clock) { | |
| 2039 | 20847 | FppPtpClock* self = (FppPtpClock*)clock; | |
| 2040 | struct timespec ts; | ||
| 2041 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20847 times.
|
20847 | if (clock_gettime(self->clkid, &ts) != 0) { |
| 2042 | // Losing the clock mid-stream would wedge every waiting element, so | ||
| 2043 | // fall back rather than returning an error the caller cannot act on. | ||
| 2044 | ✗ | clock_gettime(CLOCK_REALTIME, &ts); | |
| 2045 | } | ||
| 2046 | 20847 | return GST_TIMESPEC_TO_TIME(ts); | |
| 2047 | } | ||
| 2048 | |||
| 2049 | 77 | static void fpp_ptp_clock_finalize(GObject* object) { | |
| 2050 | 77 | FppPtpClock* self = (FppPtpClock*)object; | |
| 2051 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 71 times.
|
77 | if (self->phcFd >= 0) { |
| 2052 | 6 | close(self->phcFd); | |
| 2053 | 6 | self->phcFd = -1; | |
| 2054 | } | ||
| 2055 | 77 | G_OBJECT_CLASS(fpp_ptp_clock_parent_class)->finalize(object); | |
| 2056 | 77 | } | |
| 2057 | |||
| 2058 | 58 | static void fpp_ptp_clock_class_init(FppPtpClockClass* klass) { | |
| 2059 | 58 | GST_CLOCK_CLASS(klass)->get_internal_time = fpp_ptp_clock_get_internal_time; | |
| 2060 | 58 | G_OBJECT_CLASS(klass)->finalize = fpp_ptp_clock_finalize; | |
| 2061 | 58 | } | |
| 2062 | |||
| 2063 | 77 | static void fpp_ptp_clock_init(FppPtpClock* self) { | |
| 2064 | 77 | self->clkid = CLOCK_REALTIME; | |
| 2065 | 77 | self->phcFd = -1; | |
| 2066 | 77 | } | |
| 2067 | |||
| 2068 | |||
| 2069 | // Opens the PTP time source and wraps it in a GstClock. One clock is shared by | ||
| 2070 | // every send pipeline: they must all carry the same media timeline, and a | ||
| 2071 | // second clock object would mean a second set of rate estimates. | ||
| 2072 | 184 | GstClock* AES67Manager::GetOrCreateMediaClock() { | |
| 2073 |
1/2✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
|
184 | std::lock_guard<std::mutex> lock(m_ptpClockMutex); |
| 2074 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 181 times.
|
184 | if (m_ptpClock) { |
| 2075 | 3 | return m_ptpClock; | |
| 2076 | } | ||
| 2077 |
3/4✓ Branch 0 taken 181 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 104 times.
✓ Branch 3 taken 77 times.
|
181 | if (!IsPtpEnabled()) { |
| 2078 | 104 | return nullptr; | |
| 2079 | } | ||
| 2080 | |||
| 2081 |
2/4✓ Branch 0 taken 77 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 77 times.
✗ Branch 3 not taken.
|
77 | FppPtpClock* clock = (FppPtpClock*)g_object_new(fpp_ptp_clock_get_type(), |
| 2082 | "name", "fppaes67ptpclock", NULL); | ||
| 2083 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 77 times.
|
77 | if (!clock) { |
| 2084 | ✗ | return nullptr; | |
| 2085 | } | ||
| 2086 | // A GstClock is born floating, and m_ptpClock is our own reference, | ||
| 2087 | // released in ReleaseMediaClock(). The pipelines it goes to only ref it | ||
| 2088 | // (gst_object_replace), so the counts balance today, but anything that | ||
| 2089 | // ref_sinks it would take our reference over and our unref would then | ||
| 2090 | // free a clock still in use. Sink it here, as its owner. | ||
| 2091 |
1/2✓ Branch 0 taken 77 times.
✗ Branch 1 not taken.
|
77 | gst_object_ref_sink(clock); |
| 2092 | |||
| 2093 |
1/2✓ Branch 0 taken 77 times.
✗ Branch 1 not taken.
|
77 | std::string iface = GetPtpInterface(); |
| 2094 | // The PHC is the domain's time only while ptp4l disciplines it. A NIC | ||
| 2095 | // that has one but rejected hardware timestamping leaves ptp4l on | ||
| 2096 | // software timestamping (CLOCK_REALTIME, left alone), and with ptp4l not | ||
| 2097 | // running at all CLOCK_REALTIME is at least NTP-steered -- a free-running | ||
| 2098 | // PHC is neither, and every box would drift on its own crystal. | ||
| 2099 |
3/4✓ Branch 0 taken 44 times.
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 44 times.
✗ Branch 3 not taken.
|
77 | int phcIndex = m_ptpHwTimestamping.load() ? PhcIndexForInterface(iface) : -1; |
| 2100 |
7/8✓ Branch 0 taken 71 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 71 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 70 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 76 times.
|
77 | if (phcIndex < 0 && PhcIndexForInterface(iface) >= 0) { |
| 2101 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogInfo(VB_MEDIAOUT, "AES67 media clock: %s has a PHC, but ptp4l is not running with " |
| 2102 | "hardware timestamping, so it is not disciplined\n", iface.c_str()); | ||
| 2103 | } | ||
| 2104 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 71 times.
|
77 | if (phcIndex >= 0) { |
| 2105 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | std::string dev = "/dev/ptp" + std::to_string(phcIndex); |
| 2106 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | int fd = open(dev.c_str(), O_RDONLY); |
| 2107 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (fd >= 0) { |
| 2108 | 6 | clock->phcFd = fd; | |
| 2109 | 6 | clock->clkid = FPP_FD_TO_CLOCKID(fd); | |
| 2110 | // Prove it is readable before we hand it to a pipeline -- a PHC | ||
| 2111 | // that exists but will not answer would stall every element | ||
| 2112 | // waiting on it. | ||
| 2113 | struct timespec ts; | ||
| 2114 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (clock_gettime(clock->clkid, &ts) == 0) { |
| 2115 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | LogInfo(VB_MEDIAOUT, "AES67 media clock: using PHC %s (interface %s)\n", |
| 2116 | dev.c_str(), iface.c_str()); | ||
| 2117 |
2/4✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
6 | m_ptpClock = GST_CLOCK(clock); |
| 2118 | 6 | return m_ptpClock; | |
| 2119 | } | ||
| 2120 | ✗ | LogWarn(VB_MEDIAOUT, "AES67 media clock: %s is not readable (%s), " | |
| 2121 | "falling back to CLOCK_REALTIME\n", dev.c_str(), FPPstrerror(errno)); | ||
| 2122 | ✗ | close(fd); | |
| 2123 | ✗ | clock->phcFd = -1; | |
| 2124 | } else { | ||
| 2125 | ✗ | LogWarn(VB_MEDIAOUT, "AES67 media clock: cannot open %s (%s), " | |
| 2126 | "falling back to CLOCK_REALTIME\n", dev.c_str(), FPPstrerror(errno)); | ||
| 2127 | } | ||
| 2128 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | } |
| 2129 | |||
| 2130 | // Software-timestamping path: ptp4l has no PHC and runs free_running on | ||
| 2131 | // CLOCK_REALTIME (see WritePtpConf()). As grandmaster that is the | ||
| 2132 | // domain's time; as a follower it is NTP time, not the grandmaster's. | ||
| 2133 | 71 | clock->clkid = CLOCK_REALTIME; | |
| 2134 |
1/2✓ Branch 0 taken 71 times.
✗ Branch 1 not taken.
|
71 | LogInfo(VB_MEDIAOUT, "AES67 media clock: using CLOCK_REALTIME " |
| 2135 | "(no disciplined PHC on %s — software timestamping)\n", iface.c_str()); | ||
| 2136 |
2/4✓ Branch 0 taken 71 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 71 times.
✗ Branch 3 not taken.
|
71 | m_ptpClock = GST_CLOCK(clock); |
| 2137 | 71 | return m_ptpClock; | |
| 2138 | 184 | } | |
| 2139 | |||
| 2140 | 239 | void AES67Manager::ReleaseMediaClock() { | |
| 2141 |
1/2✓ Branch 0 taken 239 times.
✗ Branch 1 not taken.
|
239 | std::lock_guard<std::mutex> lock(m_ptpClockMutex); |
| 2142 |
2/2✓ Branch 0 taken 77 times.
✓ Branch 1 taken 162 times.
|
239 | if (m_ptpClock) { |
| 2143 |
1/2✓ Branch 0 taken 77 times.
✗ Branch 1 not taken.
|
77 | gst_object_unref(m_ptpClock); |
| 2144 | 77 | m_ptpClock = nullptr; | |
| 2145 | } | ||
| 2146 | 239 | } | |
| 2147 | |||
| 2148 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 2149 | // Pipeline creation — Send | ||
| 2150 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 2151 | |||
| 2152 | // Source-gap instrumentation, deliberately independent of every config switch. | ||
| 2153 | // | ||
| 2154 | // The same measurement used to live inside DriftResampleProbe, which meant it | ||
| 2155 | // only existed when ptpMediaClock and driftResample were both on -- and those | ||
| 2156 | // are exactly the flags a diagnostic needs to turn off. Testing whether the | ||
| 2157 | // PTP pipeline clock is what drains the source pool was impossible for that | ||
| 2158 | // reason: turning off the clock also turned off the instrument. | ||
| 2159 | // | ||
| 2160 | // Sits directly on pipewiresrc's src pad so it sees the source's own timeline | ||
| 2161 | // before any conversion, and reports each PTS discontinuity against the buffer | ||
| 2162 | // index it happened at. The pool empties after (N-1) quanta of accumulated | ||
| 2163 | // drift -- N with always-copy, which frees the slot a wrapped buffer would | ||
| 2164 | // hold through the chain -- and from then on the source skips one graph cycle | ||
| 2165 | // per rotation. Both the onset and the spacing are read off these lines. | ||
| 2166 | |||
| 2167 | // Holds back the per-buffer gap lines past the first few; see | ||
| 2168 | // AES67::GAP_LOG_IN_FULL. Only ever touched from one pad's streaming thread. | ||
| 2169 | struct GapLogLimit { | ||
| 2170 | guint64 heldBack = 0; | ||
| 2171 | double heldBackMs = 0.0; | ||
| 2172 | std::chrono::steady_clock::time_point since{}; | ||
| 2173 | |||
| 2174 | // Whether the n-th event (counting from 1) is logged in full. If not, it | ||
| 2175 | // goes into the next summary. | ||
| 2176 | 102 | bool InFull(guint64 n, double ms) { | |
| 2177 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 30 times.
|
102 | if (n <= AES67::GAP_LOG_IN_FULL) { |
| 2178 | 72 | return true; | |
| 2179 | } | ||
| 2180 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 27 times.
|
30 | if (heldBack == 0) { |
| 2181 | 3 | since = std::chrono::steady_clock::now(); | |
| 2182 | } | ||
| 2183 | 30 | heldBack++; | |
| 2184 | 30 | heldBackMs += ms; | |
| 2185 | 30 | return false; | |
| 2186 | } | ||
| 2187 | |||
| 2188 | // Whether a summary is due (always, with force, if anything is held | ||
| 2189 | // back). If it is, hands over the totals and starts again. | ||
| 2190 | 61147 | bool Due(guint64& count, double& ms, double& seconds, bool force = false) { | |
| 2191 |
2/2✓ Branch 0 taken 59914 times.
✓ Branch 1 taken 1233 times.
|
61147 | if (heldBack == 0) { |
| 2192 | 59914 | return false; | |
| 2193 | } | ||
| 2194 | 1233 | const auto now = std::chrono::steady_clock::now(); | |
| 2195 |
7/10✓ Branch 0 taken 1233 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1233 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1233 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1230 times.
✓ Branch 7 taken 3 times.
✓ Branch 8 taken 1230 times.
✓ Branch 9 taken 3 times.
|
1233 | if (!force && now - since < std::chrono::milliseconds(AES67::GAP_LOG_SUMMARY_MS)) { |
| 2196 | 1230 | return false; | |
| 2197 | } | ||
| 2198 | 3 | count = heldBack; | |
| 2199 | 3 | ms = heldBackMs; | |
| 2200 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | seconds = std::chrono::duration<double>(now - since).count(); |
| 2201 | 3 | heldBack = 0; | |
| 2202 | 3 | heldBackMs = 0.0; | |
| 2203 | 3 | return true; | |
| 2204 | } | ||
| 2205 | }; | ||
| 2206 | |||
| 2207 | // Appended to the last line logged in full, so the log says why they stop. | ||
| 2208 | 72 | static const char* GapLogSuffix(guint64 n) { | |
| 2209 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 69 times.
|
72 | return n == AES67::GAP_LOG_IN_FULL ? ", further ones are summarised every 10 s" : ""; |
| 2210 | } | ||
| 2211 | |||
| 2212 | // Bytes in one sample of a raw audio format named as GStreamer spells it | ||
| 2213 | // ("S16LE", "S24LE" packed in 3, "S24_32LE" in 4, "F32LE"...); 0 if unknown. | ||
| 2214 | // gst-audio is not linked, and this is all the probe below needs of it. | ||
| 2215 | 31 | static int RawSampleBytes(const char* format) { | |
| 2216 |
3/6✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 31 times.
|
31 | if (!format || !format[0] || !format[1]) { |
| 2217 | ✗ | return 0; | |
| 2218 | } | ||
| 2219 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
|
31 | if (strstr(format, "_32")) { |
| 2220 | ✗ | return 4; | |
| 2221 | } | ||
| 2222 | 31 | const int bits = atoi(format + 1); | |
| 2223 |
3/6✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 31 times.
✗ Branch 5 not taken.
|
31 | return (bits >= 8 && bits % 8 == 0 && bits <= 64) ? bits / 8 : 0; |
| 2224 | } | ||
| 2225 | |||
| 2226 | struct SourceGapState { | ||
| 2227 | int instanceId = 0; | ||
| 2228 | int rate = AES67::AUDIO_RATE; | ||
| 2229 | GstClockTime lastPts = 0; | ||
| 2230 | guint64 lastDur = 0; | ||
| 2231 | bool have = false; | ||
| 2232 | guint64 seen = 0; | ||
| 2233 | guint64 gaps = 0; | ||
| 2234 | GapLogLimit gapLog; | ||
| 2235 | |||
| 2236 | // The source's negotiated caps, as bytes per frame and frames per second, | ||
| 2237 | // so a buffer's duration can be worked out from what it holds. | ||
| 2238 | GstCaps* caps = nullptr; | ||
| 2239 | int bytesPerFrame = 0; | ||
| 2240 | int capsRate = 0; | ||
| 2241 | }; | ||
| 2242 | |||
| 2243 | 23565 | static void LogSourceGapSummary(SourceGapState* st, bool force) { | |
| 2244 | 23565 | guint64 count = 0; | |
| 2245 | 23565 | double ms = 0.0; | |
| 2246 | 23565 | double seconds = 0.0; | |
| 2247 |
3/4✓ Branch 0 taken 23565 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 23564 times.
|
23565 | if (st->gapLog.Due(count, ms, seconds, force)) { |
| 2248 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogInfo(VB_MEDIAOUT, |
| 2249 | "AES67 source [%d]: %llu more gaps (%.1f ms in all) in %.1f s, " | ||
| 2250 | "%llu so far\n", | ||
| 2251 | st->instanceId, (unsigned long long)count, ms, seconds, | ||
| 2252 | (unsigned long long)st->gaps); | ||
| 2253 | } | ||
| 2254 | 23565 | } | |
| 2255 | |||
| 2256 | 185 | static void DestroySourceGapState(gpointer data) { | |
| 2257 | 185 | auto* st = static_cast<SourceGapState*>(data); | |
| 2258 | 185 | LogSourceGapSummary(st, true); | |
| 2259 |
2/2✓ Branch 0 taken 31 times.
✓ Branch 1 taken 154 times.
|
185 | if (st->caps) { |
| 2260 | 31 | gst_caps_unref(st->caps); | |
| 2261 | } | ||
| 2262 |
1/2✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
|
185 | delete st; |
| 2263 | 185 | } | |
| 2264 | |||
| 2265 | 23380 | static GstPadProbeReturn SourceGapProbe(GstPad* pad, GstPadProbeInfo* info, | |
| 2266 | gpointer user) { | ||
| 2267 | 23380 | auto* st = static_cast<SourceGapState*>(user); | |
| 2268 | 23380 | GstBuffer* buf = GST_PAD_PROBE_INFO_BUFFER(info); | |
| 2269 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23380 times.
|
23380 | if (!buf) { |
| 2270 | ✗ | return GST_PAD_PROBE_OK; | |
| 2271 | } | ||
| 2272 | 23380 | st->seen++; | |
| 2273 | |||
| 2274 | // How long this buffer's audio lasts, from what it holds and the rate it | ||
| 2275 | // was negotiated at. GST_BUFFER_DURATION is not used because pipewiresrc | ||
| 2276 | // fills it in from the graph's clock rate rather than the stream's: with | ||
| 2277 | // the graph at 88.2 kHz and the stream at 48 kHz every 557-frame buffer | ||
| 2278 | // (11.6 ms, and its PTS step says so) carries a duration of 6.3 ms, which | ||
| 2279 | // read as a 5.3 ms gap on every buffer -- about 85 a second, with the | ||
| 2280 | // audio intact. | ||
| 2281 | 23380 | GstCaps* cur = gst_pad_get_current_caps(pad); | |
| 2282 |
1/2✓ Branch 0 taken 23380 times.
✗ Branch 1 not taken.
|
23380 | if (cur) { |
| 2283 |
2/2✓ Branch 0 taken 31 times.
✓ Branch 1 taken 23349 times.
|
23380 | if (cur != st->caps) { |
| 2284 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
|
31 | if (st->caps) { |
| 2285 | ✗ | gst_caps_unref(st->caps); | |
| 2286 | } | ||
| 2287 | 31 | st->caps = cur; // keeps the reference | |
| 2288 | 31 | st->bytesPerFrame = 0; | |
| 2289 | 31 | st->capsRate = 0; | |
| 2290 |
1/2✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
|
31 | const GstStructure* cs = gst_caps_get_structure(cur, 0); |
| 2291 | 31 | gint channels = 0; | |
| 2292 | 31 | gint rate = 0; | |
| 2293 |
2/4✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
✗ Branch 3 not taken.
|
31 | if (cs && gst_structure_get_int(cs, "rate", &rate) && |
| 2294 |
6/12✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 31 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 31 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 31 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 31 times.
✗ Branch 11 not taken.
|
62 | gst_structure_get_int(cs, "channels", &channels) && rate > 0 && channels > 0) { |
| 2295 |
1/2✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
|
31 | const int sb = RawSampleBytes(gst_structure_get_string(cs, "format")); |
| 2296 |
1/2✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
|
31 | if (sb > 0) { |
| 2297 | 31 | st->bytesPerFrame = sb * channels; | |
| 2298 | 31 | st->capsRate = rate; | |
| 2299 | } | ||
| 2300 | } | ||
| 2301 | } else { | ||
| 2302 | 23349 | gst_caps_unref(cur); | |
| 2303 | } | ||
| 2304 | } | ||
| 2305 |
2/4✓ Branch 0 taken 23380 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 23380 times.
✗ Branch 3 not taken.
|
23380 | const bool known = st->bytesPerFrame > 0 && st->capsRate > 0; |
| 2306 |
1/2✓ Branch 0 taken 23380 times.
✗ Branch 1 not taken.
|
46760 | const guint64 thisDur = known ? gst_util_uint64_scale(gst_buffer_get_size(buf) / st->bytesPerFrame, |
| 2307 | 23380 | GST_SECOND, st->capsRate) | |
| 2308 | ✗ | : (GST_BUFFER_DURATION_IS_VALID(buf) ? GST_BUFFER_DURATION(buf) : 0); | |
| 2309 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 23380 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
23380 | const bool durKnown = known || GST_BUFFER_DURATION_IS_VALID(buf); |
| 2310 | |||
| 2311 |
1/2✓ Branch 0 taken 23380 times.
✗ Branch 1 not taken.
|
23380 | if (GST_BUFFER_PTS_IS_VALID(buf)) { |
| 2312 | 23380 | const GstClockTime pts = GST_BUFFER_PTS(buf); | |
| 2313 |
3/4✓ Branch 0 taken 23349 times.
✓ Branch 1 taken 31 times.
✓ Branch 2 taken 23349 times.
✗ Branch 3 not taken.
|
23380 | if (st->have && pts > st->lastPts) { |
| 2314 | 23349 | const gint64 jump = (gint64)(pts - st->lastPts) - (gint64)st->lastDur; | |
| 2315 |
2/2✓ Branch 0 taken 34 times.
✓ Branch 1 taken 23315 times.
|
23349 | if (jump > (gint64)GST_MSECOND) { |
| 2316 | 34 | st->gaps++; | |
| 2317 | 34 | const double ms = (double)jump / (double)GST_MSECOND; | |
| 2318 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 10 times.
|
34 | if (st->gapLog.InFull(st->gaps, ms)) { |
| 2319 | 24 | LogInfo(VB_MEDIAOUT, | |
| 2320 | "AES67 source [%d]: gap %+.2f ms at buffer %llu " | ||
| 2321 | "(gap #%llu%s)\n", | ||
| 2322 | st->instanceId, ms, | ||
| 2323 | (unsigned long long)st->seen, | ||
| 2324 | (unsigned long long)st->gaps, GapLogSuffix(st->gaps)); | ||
| 2325 | } | ||
| 2326 | } | ||
| 2327 | } | ||
| 2328 | 23380 | LogSourceGapSummary(st, false); | |
| 2329 | 23380 | st->lastPts = pts; | |
| 2330 | 23380 | st->lastDur = thisDur; | |
| 2331 | 23380 | st->have = durKnown; | |
| 2332 | } | ||
| 2333 | 23380 | return GST_PAD_PROBE_OK; | |
| 2334 | } | ||
| 2335 | |||
| 2336 | #ifdef FPP_HAVE_SAMPLERATE | ||
| 2337 | // ───────────────────────────────────────────────────────────────────────────── | ||
| 2338 | // Media clock drift correction | ||
| 2339 | // | ||
| 2340 | // The audio is produced on the sound card's crystal and the RTP timeline runs | ||
| 2341 | // on the NIC's PHC; on the reference hardware those differ by ~56ppm, which is | ||
| 2342 | // 3.4ms per minute. That empties any receiver's buffer, and it empties the | ||
| 2343 | // sinkPacing queue, so it has to be corrected in the sender. | ||
| 2344 | // | ||
| 2345 | // Three GStreamer elements were tried first and all three are recorded as dead | ||
| 2346 | // ends in AES67Config: "speed" and "pitch" accept a rate property and never | ||
| 2347 | // apply it to a live stream, and "audiorate" reconciles per-buffer timestamp | ||
| 2348 | // rounding rather than drift and then runs away. libsamplerate is used | ||
| 2349 | // instead because it is built for exactly this: src_process() takes a ratio per | ||
| 2350 | // block, interpolates it across the block so there is no zipper, and keeps a | ||
| 2351 | // fractional read pointer across calls. | ||
| 2352 | // | ||
| 2353 | // The control law deliberately leads with a direct measurement rather than a | ||
| 2354 | // feedback loop, because every previous attempt here oscillated: | ||
| 2355 | // | ||
| 2356 | // feedforward -- the card's true rate is (input frames / PHC seconds), which | ||
| 2357 | // is two crystals and therefore almost perfectly constant. The ratio that | ||
| 2358 | // cancels it is just AUDIO_RATE / that. This carries essentially all of | ||
| 2359 | // the correction and needs no gain. | ||
| 2360 | // feedback -- a small proportional term bleeds off accumulated offset, so a | ||
| 2361 | // startup transient or a rounding residue does not persist. It is | ||
| 2362 | // deliberately weak; it is a trim, not the controller. | ||
| 2363 | // | ||
| 2364 | // The failure that killed audiorate is handled explicitly: on DISCONT the | ||
| 2365 | // counters re-anchor rather than treating the gap as drift to be made up. | ||
| 2366 | struct DriftResampleState { | ||
| 2367 | SRC_STATE* src = nullptr; | ||
| 2368 | int channels = 2; | ||
| 2369 | GstClock* clock = nullptr; | ||
| 2370 | int instanceId = 0; | ||
| 2371 | |||
| 2372 | // Output timeline: monotonic, only re-anchored on a real discontinuity. | ||
| 2373 | bool anchored = false; | ||
| 2374 | GstClockTime anchorPts = 0; | ||
| 2375 | guint64 ptsFrames = 0; | ||
| 2376 | |||
| 2377 | // Control counters: zeroed again once the pipeline is up to speed. Keeping | ||
| 2378 | // these separate is the point -- a pipeline takes a moment to start | ||
| 2379 | // flowing, and counting that startup gap as drift makes the loop chase a | ||
| 2380 | // deficit it did not cause (measured: -188ms, which pinned the trim at the | ||
| 2381 | // clamp and ran the stream 2000ppm fast for minutes). | ||
| 2382 | bool warmed = false; | ||
| 2383 | GstClockTime ctlClock = 0; | ||
| 2384 | guint64 ctlIn = 0; | ||
| 2385 | guint64 ctlOut = 0; | ||
| 2386 | double ratio = 1.0; | ||
| 2387 | |||
| 2388 | std::vector<float> out; | ||
| 2389 | guint64 buffers = 0; | ||
| 2390 | guint64 shortReads = 0; | ||
| 2391 | |||
| 2392 | // Onset instrumentation for the periodic under-delivery. The absorb path | ||
| 2393 | // below only reports gaps over 50ms, because that is the threshold that | ||
| 2394 | // matters to the control loop -- but that hides when a burst actually | ||
| 2395 | // starts, since the first gaps can be a single graph quantum. Measuring | ||
| 2396 | // from the >50ms line put the onset at 275k-284k buffers across seven | ||
| 2397 | // runs; the 3% spread is most likely this bias, not the mechanism. These | ||
| 2398 | // fields report every discontinuity against the buffer index it happened | ||
| 2399 | // at, which is what separates a fixed-count trigger from an accumulation. | ||
| 2400 | GstClockTime lastPts = 0; | ||
| 2401 | long lastFrames = 0; | ||
| 2402 | bool lastPtsValid = false; | ||
| 2403 | guint64 seen = 0; | ||
| 2404 | guint64 gapsSeen = 0; | ||
| 2405 | |||
| 2406 | // Buffer index the last re-anchor happened at. | ||
| 2407 | guint64 lastResyncAt = 0; | ||
| 2408 | bool everResynced = false; | ||
| 2409 | |||
| 2410 | // Frames of silence owed to the timeline, from a source gap not yet filled. | ||
| 2411 | // See AES67Config::sourceSilenceFloor. | ||
| 2412 | bool fillGaps = false; | ||
| 2413 | guint64 fillFrames = 0; | ||
| 2414 | guint64 fills = 0; | ||
| 2415 | |||
| 2416 | GapLogLimit gapLog; | ||
| 2417 | GapLogLimit fillLog; | ||
| 2418 | |||
| 2419 | // Rate estimate that survives a re-anchor. The card's rate against PTP is | ||
| 2420 | // a property of two crystals: it does not change because a track did. The | ||
| 2421 | // sliding window that measures it does get discarded at every re-anchor, | ||
| 2422 | // so without somewhere to keep the answer the loop has to re-learn a 54ppm | ||
| 2423 | // figure from scratch after every track boundary. | ||
| 2424 | double cardRateEst = 0.0; | ||
| 2425 | |||
| 2426 | |||
| 2427 | // Sliding window of (clock, input frames, output frames) used to measure | ||
| 2428 | // the card's rate. A cumulative average was used here first and it is what | ||
| 2429 | // made the loop oscillate: it is itself an integrator, so with the phase | ||
| 2430 | // feedback -- which integrates too -- the loop had two in series. It also | ||
| 2431 | // grew steadily less responsive as the run went on, which showed up as a | ||
| 2432 | // trim still crawling towards its answer 26 minutes in. | ||
| 2433 | struct Sample { | ||
| 2434 | GstClockTime t; | ||
| 2435 | guint64 in; | ||
| 2436 | guint64 out; | ||
| 2437 | }; | ||
| 2438 | std::deque<Sample> window; | ||
| 2439 | }; | ||
| 2440 | |||
| 2441 | 18791 | static void LogDriftGapSummaries(DriftResampleState* st, bool force) { | |
| 2442 | 18791 | guint64 count = 0; | |
| 2443 | 18791 | double ms = 0.0; | |
| 2444 | 18791 | double seconds = 0.0; | |
| 2445 |
3/4✓ Branch 0 taken 18791 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 18790 times.
|
18791 | if (st->gapLog.Due(count, ms, seconds, force)) { |
| 2446 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogInfo(VB_MEDIAOUT, |
| 2447 | "AES67 drift [%d]: %llu more source gaps (%.1f ms in all) in " | ||
| 2448 | "%.1f s, %llu so far\n", | ||
| 2449 | st->instanceId, (unsigned long long)count, ms, seconds, | ||
| 2450 | (unsigned long long)st->gapsSeen); | ||
| 2451 | } | ||
| 2452 |
3/4✓ Branch 0 taken 18791 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 18790 times.
|
18791 | if (st->fillLog.Due(count, ms, seconds, force)) { |
| 2453 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogInfo(VB_MEDIAOUT, |
| 2454 | "AES67 drift [%d]: filled %llu more source gaps with %.1f ms of " | ||
| 2455 | "silence in %.1f s, %llu so far\n", | ||
| 2456 | st->instanceId, (unsigned long long)count, ms, seconds, | ||
| 2457 | (unsigned long long)st->fills); | ||
| 2458 | } | ||
| 2459 | 18791 | } | |
| 2460 | |||
| 2461 | 78 | static void DestroyDriftResampleState(gpointer data) { | |
| 2462 | 78 | auto* st = static_cast<DriftResampleState*>(data); | |
| 2463 | 78 | LogDriftGapSummaries(st, true); | |
| 2464 |
1/2✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
|
78 | if (st->src) { |
| 2465 | 78 | src_delete(st->src); | |
| 2466 | } | ||
| 2467 |
1/2✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
|
78 | if (st->clock) { |
| 2468 | 78 | gst_object_unref(st->clock); | |
| 2469 | } | ||
| 2470 |
1/2✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
|
78 | delete st; |
| 2471 | 78 | } | |
| 2472 | |||
| 2473 | 18713 | static GstPadProbeReturn DriftResampleProbe(GstPad* pad, GstPadProbeInfo* info, | |
| 2474 | gpointer user) { | ||
| 2475 | 18713 | auto* st = static_cast<DriftResampleState*>(user); | |
| 2476 | 18713 | const bool fillGaps = st->fillGaps; | |
| 2477 | 18713 | GstBuffer* buf = GST_PAD_PROBE_INFO_BUFFER(info); | |
| 2478 |
3/6✓ Branch 0 taken 18713 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18713 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 18713 times.
|
18713 | if (!buf || !st->src || !st->clock) { |
| 2479 | ✗ | return GST_PAD_PROBE_OK; | |
| 2480 | } | ||
| 2481 | |||
| 2482 | // A gap is not drift. Re-anchor and let the resampler start clean, rather | ||
| 2483 | // than trying to make up time that was never ours to make up. | ||
| 2484 |
2/2✓ Branch 0 taken 29 times.
✓ Branch 1 taken 18684 times.
|
18713 | if (GST_BUFFER_FLAG_IS_SET(buf, GST_BUFFER_FLAG_DISCONT)) { |
| 2485 | // Worth logging: this resets the resampler and restarts the rate | ||
| 2486 | // estimate, and it was happening every few minutes with nothing in the | ||
| 2487 | // log to say so -- the only visible sign was the loop's own elapsed | ||
| 2488 | // counter starting over. | ||
| 2489 |
1/2✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 | LogInfo(VB_MEDIAOUT, |
| 2490 | "AES67 drift [%d]: source discontinuity, resetting\n", | ||
| 2491 | st->instanceId); | ||
| 2492 |
1/2✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 | src_reset(st->src); |
| 2493 | 29 | st->anchored = false; | |
| 2494 | } | ||
| 2495 | |||
| 2496 | GstMapInfo map; | ||
| 2497 |
2/4✓ Branch 0 taken 18713 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18713 times.
|
18713 | if (!gst_buffer_map(buf, &map, GST_MAP_READ)) { |
| 2498 | ✗ | return GST_PAD_PROBE_OK; | |
| 2499 | } | ||
| 2500 | 18713 | const int ch = st->channels; | |
| 2501 | 18713 | const long inFrames = (long)(map.size / (sizeof(float) * ch)); | |
| 2502 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18713 times.
|
18713 | if (inFrames <= 0) { |
| 2503 | ✗ | gst_buffer_unmap(buf, &map); | |
| 2504 | ✗ | return GST_PAD_PROBE_OK; | |
| 2505 | } | ||
| 2506 | |||
| 2507 | // Report source-side PTS discontinuities at full resolution, tagged with | ||
| 2508 | // the buffer index so the onset can be located exactly. 1ms is chosen to | ||
| 2509 | // sit far below one quantum (21.3ms at 48000, 23.2ms at 44100) while | ||
| 2510 | // staying above any rounding in the PTS arithmetic; every gap observed so | ||
| 2511 | // far has been an exact multiple of the quantum, most often 3x. | ||
| 2512 | 18713 | st->seen++; | |
| 2513 |
1/2✓ Branch 0 taken 18713 times.
✗ Branch 1 not taken.
|
18713 | if (GST_BUFFER_PTS_IS_VALID(buf)) { |
| 2514 | 18713 | const GstClockTime pts = GST_BUFFER_PTS(buf); | |
| 2515 |
3/4✓ Branch 0 taken 18684 times.
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 18684 times.
✗ Branch 3 not taken.
|
18713 | if (st->lastPtsValid && pts > st->lastPts) { |
| 2516 | 18684 | const gint64 expected = | |
| 2517 | 18684 | (gint64)st->lastFrames * GST_SECOND / AES67::AUDIO_RATE; | |
| 2518 | 18684 | const gint64 jump = (gint64)(pts - st->lastPts) - expected; | |
| 2519 |
2/2✓ Branch 0 taken 34 times.
✓ Branch 1 taken 18650 times.
|
18684 | if (jump > (gint64)GST_MSECOND) { |
| 2520 | 34 | st->gapsSeen++; | |
| 2521 | // Owe the timeline this much silence. The RTP timestamp is a | ||
| 2522 | // sample count, so audio that never arrives puts the media | ||
| 2523 | // clock permanently behind PTP -- it cannot be corrected | ||
| 2524 | // later, because the samples are not late, they are absent. | ||
| 2525 | // Filling keeps the count aligned and keeps AES67 in step with | ||
| 2526 | // the local sound card, which already plays the same pause. | ||
| 2527 | // Capped so a pathological jump cannot ask for a huge | ||
| 2528 | // allocation; anything longer is a stream restart, not a gap. | ||
| 2529 |
1/2✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
|
34 | if (fillGaps) { |
| 2530 |
1/2✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
|
34 | const gint64 f = gst_util_uint64_scale( |
| 2531 | 34 | (guint64)jump, AES67::AUDIO_RATE, GST_SECOND); | |
| 2532 | 68 | st->fillFrames = (guint64)std::min<gint64>( | |
| 2533 | 34 | f, (gint64)AES67::AUDIO_RATE); // <= 1 second | |
| 2534 | } | ||
| 2535 | 34 | const double ms = (double)jump / (double)GST_MSECOND; | |
| 2536 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 10 times.
|
34 | if (st->gapLog.InFull(st->gapsSeen, ms)) { |
| 2537 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | LogInfo(VB_MEDIAOUT, |
| 2538 | "AES67 drift [%d]: source gap %+.2f ms at buffer %llu " | ||
| 2539 | "(gap #%llu%s)\n", | ||
| 2540 | st->instanceId, ms, | ||
| 2541 | (unsigned long long)st->seen, | ||
| 2542 | (unsigned long long)st->gapsSeen, | ||
| 2543 | GapLogSuffix(st->gapsSeen)); | ||
| 2544 | } | ||
| 2545 | } | ||
| 2546 | } | ||
| 2547 | 18713 | st->lastPts = pts; | |
| 2548 | 18713 | st->lastFrames = inFrames; | |
| 2549 | 18713 | st->lastPtsValid = true; | |
| 2550 | } | ||
| 2551 | |||
| 2552 |
1/2✓ Branch 0 taken 18713 times.
✗ Branch 1 not taken.
|
18713 | const GstClockTime now = gst_clock_get_time(st->clock); |
| 2553 |
2/2✓ Branch 0 taken 29 times.
✓ Branch 1 taken 18684 times.
|
18713 | if (!st->anchored) { |
| 2554 | 29 | st->anchored = true; | |
| 2555 |
1/2✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 | st->anchorPts = GST_BUFFER_PTS_IS_VALID(buf) ? GST_BUFFER_PTS(buf) : 0; |
| 2556 | 29 | st->ptsFrames = 0; | |
| 2557 | 29 | st->warmed = false; | |
| 2558 | 29 | st->ctlClock = now; | |
| 2559 | 29 | st->ctlIn = 0; | |
| 2560 | 29 | st->ctlOut = 0; | |
| 2561 | 29 | st->ratio = 1.0; | |
| 2562 | } | ||
| 2563 | |||
| 2564 | // A PTP clock can step backwards, which happens when ptp4l first locks to | ||
| 2565 | // a grandmaster -- i.e. only when this device is a follower, never when it | ||
| 2566 | // is the grandmaster itself. Without this the elapsed time pins at zero | ||
| 2567 | // until the clock catches back up to where it was, and for a large step | ||
| 2568 | // (a TAI/UTC correction is 37 seconds) the loop would sit idle that whole | ||
| 2569 | // time. Re-anchoring costs nothing: the learned trim is kept, and only | ||
| 2570 | // the measurement window restarts. | ||
| 2571 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18713 times.
|
18713 | if (now < st->ctlClock) { |
| 2572 | ✗ | LogInfo(VB_MEDIAOUT, | |
| 2573 | "AES67 drift [%d]: clock stepped back, re-anchoring\n", | ||
| 2574 | st->instanceId); | ||
| 2575 | ✗ | st->ctlClock = now; | |
| 2576 | ✗ | st->ctlIn = 0; | |
| 2577 | ✗ | st->ctlOut = 0; | |
| 2578 | ✗ | st->warmed = false; | |
| 2579 | ✗ | st->window.clear(); | |
| 2580 | } | ||
| 2581 | |||
| 2582 | 18713 | double elapsed = | |
| 2583 |
2/2✓ Branch 0 taken 18684 times.
✓ Branch 1 taken 29 times.
|
18713 | (now > st->ctlClock) ? (double)(now - st->ctlClock) / GST_SECOND : 0.0; |
| 2584 | |||
| 2585 | // Start controlling from a steady state, not from the pipeline's first | ||
| 2586 | // gasp. Everything before this is discarded rather than corrected. | ||
| 2587 |
4/4✓ Branch 0 taken 4084 times.
✓ Branch 1 taken 14629 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 4072 times.
|
18713 | if (!st->warmed && elapsed > 5.0) { |
| 2588 | 12 | st->warmed = true; | |
| 2589 | 12 | st->ctlClock = now; | |
| 2590 | 12 | st->ctlIn = 0; | |
| 2591 | 12 | st->ctlOut = 0; | |
| 2592 | 12 | st->window.clear(); | |
| 2593 | 12 | elapsed = 0.0; | |
| 2594 | } | ||
| 2595 | |||
| 2596 | // A large offset is a gap, not drift, and chasing it is the exact mistake | ||
| 2597 | // that made audiorate unusable. Absorb it instead: realign the output | ||
| 2598 | // timeline to PTP, restart the rate estimate, and keep the trim already | ||
| 2599 | // learned. Measured before this was added: a ~200ms startup gap held the | ||
| 2600 | // trim at its clamp for minutes, audibly pitching the stream while it | ||
| 2601 | // clawed back time that was never lost to drift. | ||
| 2602 | 18713 | constexpr double MAX_OFFSET_S = 0.05; | |
| 2603 | 18713 | bool resync = false; | |
| 2604 | 37426 | double err = st->warmed | |
| 2605 |
2/2✓ Branch 0 taken 14641 times.
✓ Branch 1 taken 4072 times.
|
18713 | ? (double)st->ctlOut - (double)AES67::AUDIO_RATE * elapsed |
| 2606 | : 0.0; | ||
| 2607 | // Do not absorb a gap we are about to fill. Absorbing re-anchors the | ||
| 2608 | // timeline to skip the hole; filling puts the missing samples back. Doing | ||
| 2609 | // both means the loop re-anchors and then the fill pushes the timeline | ||
| 2610 | // again -- measured, 4 absorptions still fired alongside 8 successful fills | ||
| 2611 | // and the media clock stayed at -54.7 ppm. Filling is the correct response | ||
| 2612 | // when it is available, so it wins. | ||
| 2613 |
4/4✓ Branch 0 taken 14641 times.
✓ Branch 1 taken 4072 times.
✓ Branch 2 taken 14615 times.
✓ Branch 3 taken 26 times.
|
18713 | if (st->warmed && st->fillFrames == 0 && |
| 2614 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14615 times.
|
14615 | std::fabs(err) > MAX_OFFSET_S * AES67::AUDIO_RATE) { |
| 2615 | ✗ | LogInfo(VB_MEDIAOUT, | |
| 2616 | "AES67 drift [%d]: %+.0f ms gap absorbed, re-anchoring " | ||
| 2617 | "at buffer %llu\n", | ||
| 2618 | st->instanceId, err * 1000.0 / AES67::AUDIO_RATE, | ||
| 2619 | (unsigned long long)st->seen); | ||
| 2620 | ✗ | st->ctlClock = now; | |
| 2621 | ✗ | st->ctlIn = 0; | |
| 2622 | ✗ | st->ctlOut = 0; | |
| 2623 | ✗ | st->window.clear(); | |
| 2624 | ✗ | if (GST_BUFFER_PTS_IS_VALID(buf)) { | |
| 2625 | ✗ | st->anchorPts = GST_BUFFER_PTS(buf); | |
| 2626 | } | ||
| 2627 | ✗ | st->ptsFrames = 0; | |
| 2628 | ✗ | elapsed = 0.0; | |
| 2629 | ✗ | err = 0.0; | |
| 2630 | ✗ | resync = true; | |
| 2631 | // Re-warm rather than resume controlling straight away. Re-anchoring | ||
| 2632 | // rewrites the output timeline, and the buffers right after it measure | ||
| 2633 | // that as a fresh offset -- so absorbing a gap and immediately | ||
| 2634 | // controlling again makes the loop re-trigger on itself. Measured | ||
| 2635 | // under a 25s file on repeat it re-anchored three times in four | ||
| 2636 | // buffers, and a fixed buffer hold-off only paced the oscillation | ||
| 2637 | // rather than stopping it: the spacing histogram came back with 526 | ||
| 2638 | // re-anchors at exactly the hold-off length. | ||
| 2639 | // | ||
| 2640 | // The warm-up path above already does the right thing -- it waits 5s, | ||
| 2641 | // discards what it saw meanwhile, and starts the measurement clean -- | ||
| 2642 | // and the learned trim is deliberately kept across it, so rate | ||
| 2643 | // correction continues at the last good value while adaptation pauses. | ||
| 2644 | ✗ | st->warmed = false; | |
| 2645 | ✗ | st->lastResyncAt = st->seen; | |
| 2646 | ✗ | st->everResynced = true; | |
| 2647 | } | ||
| 2648 | |||
| 2649 | // Hold the trim already learned rather than snapping back to 1.0 whenever | ||
| 2650 | // there is not yet enough data. The rate being corrected is a property of | ||
| 2651 | // two crystals, so the last good value is always a better guess than "no | ||
| 2652 | // correction" -- and resetting it is what turned a burst of absorbed gaps | ||
| 2653 | // into a death spiral: trim collapsed to 0, the pacing queue then drained | ||
| 2654 | // for want of correction, the drain starved the source into more gaps, and | ||
| 2655 | // those gaps absorbed away every chance to re-learn the rate. Measured: | ||
| 2656 | // 9 minutes of correct operation, then trim pinned at 0.0ppm with the send | ||
| 2657 | // rate falling from 250/s to 155/s and absorptions climbing past 3500. | ||
| 2658 | 18713 | double target = st->ratio; | |
| 2659 | 18713 | double cardRate = 0.0; | |
| 2660 |
2/2✓ Branch 0 taken 14641 times.
✓ Branch 1 taken 4072 times.
|
18713 | if (st->warmed) { |
| 2661 | 14641 | constexpr double WINDOW_S = 60.0; | |
| 2662 |
1/2✓ Branch 0 taken 14641 times.
✗ Branch 1 not taken.
|
14641 | st->window.push_back({now, st->ctlIn, st->ctlOut}); |
| 2663 |
4/4✓ Branch 0 taken 17593 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 2964 times.
✓ Branch 3 taken 14641 times.
|
35198 | while (st->window.size() > 1 && |
| 2664 |
2/2✓ Branch 0 taken 2964 times.
✓ Branch 1 taken 14629 times.
|
17593 | (double)(now - st->window.front().t) / GST_SECOND > WINDOW_S) { |
| 2665 | 2964 | st->window.pop_front(); | |
| 2666 | } | ||
| 2667 | |||
| 2668 | 14641 | const auto& a = st->window.front(); | |
| 2669 | 14641 | const double span = (double)(now - a.t) / GST_SECOND; | |
| 2670 | |||
| 2671 | // Long enough to actually resolve the figure being measured. 54ppm | ||
| 2672 | // over 5 seconds is 270us, which sample quantisation and normal jitter | ||
| 2673 | // swamp -- measured, a 5s minimum left trim thrashing between +59, +29 | ||
| 2674 | // and +22 ppm after every track boundary. Over 30s the same drift is | ||
| 2675 | // 1.6ms and stands clear of the noise. | ||
| 2676 | // Two thresholds, because one cannot serve both jobs. Updating a | ||
| 2677 | // settled estimate wants a long window: 54ppm over 5s is 270us, which | ||
| 2678 | // sample quantisation and jitter swamp, and a 5s minimum left trim | ||
| 2679 | // thrashing between +59, +29 and +22 ppm after every track boundary. | ||
| 2680 | // But bootstrapping wants a short one -- a re-anchor clears the window, | ||
| 2681 | // so if tracks change every 25s a 30s minimum is never reached at all | ||
| 2682 | // and the estimate stays unset forever. Measured with only the long | ||
| 2683 | // threshold: trim sat at +0.0 ppm with no feedforward and the media | ||
| 2684 | // clock ran to -4442 ppm. | ||
| 2685 |
2/2✓ Branch 0 taken 10649 times.
✓ Branch 1 taken 3992 times.
|
14641 | const double MIN_ESTIMATE_S = (st->cardRateEst > 1000.0) ? 30.0 : 10.0; |
| 2686 |
2/2✓ Branch 0 taken 6773 times.
✓ Branch 1 taken 7868 times.
|
14641 | if (span > MIN_ESTIMATE_S) { |
| 2687 | // Feedforward over the window, not since the anchor. The card's | ||
| 2688 | // rate against PTP is two crystals, so this is near-constant; the | ||
| 2689 | // window only keeps the estimate from being anchored to whatever | ||
| 2690 | // the first few seconds happened to look like. | ||
| 2691 | 6773 | const double raw = (double)(st->ctlIn - a.in) / span; | |
| 2692 |
1/2✓ Branch 0 taken 6773 times.
✗ Branch 1 not taken.
|
6773 | if (raw > 1000.0) { |
| 2693 | // Smooth into the persistent estimate rather than replacing it, | ||
| 2694 | // so one noisy window cannot move the trim far. | ||
| 2695 | 6773 | st->cardRateEst = (st->cardRateEst > 1000.0) | |
| 2696 |
2/2✓ Branch 0 taken 6767 times.
✓ Branch 1 taken 6 times.
|
6773 | ? st->cardRateEst * 0.8 + raw * 0.2 |
| 2697 | : raw; | ||
| 2698 | } | ||
| 2699 | } | ||
| 2700 | // Use the persistent estimate, which is still valid across the gap the | ||
| 2701 | // window just lost. A track change disturbs phase, not rate. | ||
| 2702 | 14641 | cardRate = st->cardRateEst; | |
| 2703 | } | ||
| 2704 | |||
| 2705 |
2/2✓ Branch 0 taken 10655 times.
✓ Branch 1 taken 8058 times.
|
18713 | if (cardRate > 1000.0) { |
| 2706 | 10655 | const double ff = (double)AES67::AUDIO_RATE / cardRate; | |
| 2707 | |||
| 2708 | // Phase feedback: pull the accumulated offset back towards zero with a | ||
| 2709 | // first-order lag. The plant is already an integrator (a rate error | ||
| 2710 | // accumulates into offset), so proportional is the right shape and a | ||
| 2711 | // second integrator here is what previously rang. Clamping it well | ||
| 2712 | // below the feedforward guarantees it can trim but never take over. | ||
| 2713 | // Gain set by measurement, not by theory. At 3000 the loop settled | ||
| 2714 | // with a standing 23ms offset it would not remove; 30000 drives it to | ||
| 2715 | // +/-0.05ms and holds there, with the trim still sitting on the | ||
| 2716 | // feedforward value. Offset then decays with a ~33s time constant. | ||
| 2717 | 10655 | constexpr double KP_PPM_PER_SEC = 30000.0; | |
| 2718 | // 100ppm is 0.17 cents -- inaudible -- and well under the ~55ppm | ||
| 2719 | // feedforward, so this can trim but never take over. | ||
| 2720 | 10655 | constexpr double MAX_FEEDBACK_PPM = 100.0; | |
| 2721 | 10655 | double fbPpm = -KP_PPM_PER_SEC * err / (double)AES67::AUDIO_RATE; | |
| 2722 |
1/2✓ Branch 0 taken 10655 times.
✗ Branch 1 not taken.
|
10655 | fbPpm = std::clamp(fbPpm, -MAX_FEEDBACK_PPM, MAX_FEEDBACK_PPM); |
| 2723 | |||
| 2724 | 10655 | target = ff * (1.0 + fbPpm * 1e-6); | |
| 2725 | } | ||
| 2726 | |||
| 2727 | // Hard clamp, then slew limit. The clamp is what refuses to act on a | ||
| 2728 | // wrong estimate rather than pitch-shift the audio chasing it, so it has to | ||
| 2729 | // sit above the largest rate error a real card can have. 300ppm was set | ||
| 2730 | // against hardware that needs 56ppm, and is too low for a fixed-clock cape: | ||
| 2731 | // an AM62x PCM5102A derives 88200 from 96 MHz / 1088, which is really | ||
| 2732 | // 88235.3 Hz -- +400ppm, measured as a +450ppm card rate. Clamped at 300 the | ||
| 2733 | // trim pinned, the offset grew without bound (+9 ms in 38 s) until the 50 ms | ||
| 2734 | // absorb re-anchored it. 1200ppm is 2 cents, still inaudible, and still far | ||
| 2735 | // below anything that is not a bad estimate. | ||
| 2736 | 18713 | constexpr double MAX_TRIM = 0.0012; // +/-1200ppm | |
| 2737 | 18713 | constexpr double MAX_STEP = 0.000002; // 2ppm per buffer | |
| 2738 |
1/2✓ Branch 0 taken 18713 times.
✗ Branch 1 not taken.
|
18713 | target = std::clamp(target, 1.0 - MAX_TRIM, 1.0 + MAX_TRIM); |
| 2739 |
1/2✓ Branch 0 taken 18713 times.
✗ Branch 1 not taken.
|
18713 | st->ratio = std::clamp(target, st->ratio - MAX_STEP, st->ratio + MAX_STEP); |
| 2740 | |||
| 2741 | 18713 | const size_t capacity = (size_t)(inFrames / st->ratio) + 32; | |
| 2742 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 18681 times.
|
18713 | if (st->out.size() < capacity * ch) { |
| 2743 |
1/2✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
|
32 | st->out.resize(capacity * ch); |
| 2744 | } | ||
| 2745 | |||
| 2746 | SRC_DATA d; | ||
| 2747 | 18713 | memset(&d, 0, sizeof(d)); | |
| 2748 | 18713 | d.data_in = reinterpret_cast<const float*>(map.data); | |
| 2749 | 18713 | d.input_frames = inFrames; | |
| 2750 | 18713 | d.data_out = st->out.data(); | |
| 2751 | 18713 | d.output_frames = (long)capacity; | |
| 2752 | 18713 | d.src_ratio = st->ratio; | |
| 2753 | 18713 | d.end_of_input = 0; | |
| 2754 | |||
| 2755 |
1/2✓ Branch 0 taken 18713 times.
✗ Branch 1 not taken.
|
18713 | const int rc = src_process(st->src, &d); |
| 2756 |
1/2✓ Branch 0 taken 18713 times.
✗ Branch 1 not taken.
|
18713 | gst_buffer_unmap(buf, &map); |
| 2757 | |||
| 2758 |
2/4✓ Branch 0 taken 18713 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18713 times.
|
18713 | if (rc != 0 || d.output_frames_gen <= 0) { |
| 2759 | ✗ | if (rc != 0) { | |
| 2760 | ✗ | LogWarn(VB_MEDIAOUT, "AES67 drift [%d]: src_process: %s\n", | |
| 2761 | st->instanceId, src_strerror(rc)); | ||
| 2762 | } | ||
| 2763 | ✗ | return GST_PAD_PROBE_OK; | |
| 2764 | } | ||
| 2765 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18713 times.
|
18713 | if (d.input_frames_used < inFrames) { |
| 2766 | // Would mean silently discarding input, so it is counted rather than | ||
| 2767 | // ignored; the output buffer is sized so this should never happen. | ||
| 2768 | ✗ | st->shortReads++; | |
| 2769 | } | ||
| 2770 | |||
| 2771 | // Any silence owed from a source gap goes ahead of this buffer's audio, so | ||
| 2772 | // the sample count downstream stays continuous across a track change. | ||
| 2773 | 18713 | const size_t fill = (size_t)st->fillFrames; | |
| 2774 | 18713 | const size_t fillBytes = fill * ch * sizeof(float); | |
| 2775 | 18713 | const size_t bytes = (size_t)d.output_frames_gen * ch * sizeof(float); | |
| 2776 |
1/2✓ Branch 0 taken 18713 times.
✗ Branch 1 not taken.
|
18713 | GstBuffer* out = gst_buffer_new_allocate(nullptr, fillBytes + bytes, nullptr); |
| 2777 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18713 times.
|
18713 | if (!out) { |
| 2778 | ✗ | return GST_PAD_PROBE_OK; | |
| 2779 | } | ||
| 2780 |
2/2✓ Branch 0 taken 34 times.
✓ Branch 1 taken 18679 times.
|
18713 | if (fill) { |
| 2781 |
1/2✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
|
34 | gst_buffer_memset(out, 0, 0, fillBytes); |
| 2782 | 34 | st->fills++; | |
| 2783 | 34 | const double ms = (double)fill * 1000.0 / (double)AES67::AUDIO_RATE; | |
| 2784 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 10 times.
|
34 | if (st->fillLog.InFull(st->fills, ms)) { |
| 2785 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | LogInfo(VB_MEDIAOUT, |
| 2786 | "AES67 drift [%d]: filled %.1f ms of source gap with silence%s\n", | ||
| 2787 | st->instanceId, ms, GapLogSuffix(st->fills)); | ||
| 2788 | } | ||
| 2789 | 34 | st->fillFrames = 0; | |
| 2790 | } | ||
| 2791 |
1/2✓ Branch 0 taken 18713 times.
✗ Branch 1 not taken.
|
18713 | LogDriftGapSummaries(st, false); |
| 2792 |
1/2✓ Branch 0 taken 18713 times.
✗ Branch 1 not taken.
|
18713 | gst_buffer_fill(out, fillBytes, st->out.data(), bytes); |
| 2793 | |||
| 2794 | // Timestamp from the output sample count, so the timeline downstream | ||
| 2795 | // advances at exactly AUDIO_RATE. Producing AUDIO_RATE samples per PHC | ||
| 2796 | // second is what the loop above enforces, so media time tracks PTP. | ||
| 2797 | 18713 | GST_BUFFER_PTS(out) = | |
| 2798 |
1/2✓ Branch 0 taken 18713 times.
✗ Branch 1 not taken.
|
18713 | st->anchorPts + gst_util_uint64_scale(st->ptsFrames, GST_SECOND, |
| 2799 | AES67::AUDIO_RATE); | ||
| 2800 | 37426 | GST_BUFFER_DURATION(out) = gst_util_uint64_scale( | |
| 2801 |
1/2✓ Branch 0 taken 18713 times.
✗ Branch 1 not taken.
|
18713 | (guint64)d.output_frames_gen, GST_SECOND, AES67::AUDIO_RATE); |
| 2802 |
3/4✓ Branch 0 taken 18713 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✓ Branch 3 taken 18684 times.
|
18713 | if (resync || GST_BUFFER_FLAG_IS_SET(buf, GST_BUFFER_FLAG_DISCONT)) { |
| 2803 | 29 | GST_BUFFER_FLAG_SET(out, GST_BUFFER_FLAG_DISCONT); | |
| 2804 | } | ||
| 2805 | |||
| 2806 | 18713 | st->ptsFrames += (guint64)fill + (guint64)d.output_frames_gen; | |
| 2807 | // Count the filled silence as input as well as output. It is synthetic | ||
| 2808 | // input standing in for audio that never arrived, so leaving it out of the | ||
| 2809 | // input side makes the loop measure a source that is slow by exactly the | ||
| 2810 | // gaps -- measured, cardRate read 47235/s (-15926 ppm) and the trim pinned | ||
| 2811 | // at its +300 ppm clamp with the offset growing 11.5 -> 22.9 ms, even | ||
| 2812 | // though the wire was correct at +1.2 ppm. Counting it on both sides keeps | ||
| 2813 | // the rate estimate honest: after filling, the effective input really is | ||
| 2814 | // aligned to real time. | ||
| 2815 | 18713 | st->ctlIn += (guint64)fill + (guint64)d.input_frames_used; | |
| 2816 | 18713 | st->ctlOut += (guint64)fill + (guint64)d.output_frames_gen; | |
| 2817 | |||
| 2818 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 18708 times.
|
18713 | if ((++st->buffers % 2000) == 0) { |
| 2819 |
3/6✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
|
5 | LogInfo(VB_MEDIAOUT, |
| 2820 | "AES67 drift [%d]: trim %+.1f ppm, card %.1f/s (%+.1f ppm), offset %+.2f ms over %.0fs%s [buffer %llu, %llu gaps]\n", | ||
| 2821 | st->instanceId, (st->ratio - 1.0) * 1e6, cardRate, | ||
| 2822 | cardRate > 1000.0 | ||
| 2823 | ? (cardRate / (double)AES67::AUDIO_RATE - 1.0) * 1e6 | ||
| 2824 | : 0.0, | ||
| 2825 | // err as the controller saw it. Recomputing here instead | ||
| 2826 | // reads one buffer high -- a whole graph quantum, 23.2ms on a | ||
| 2827 | // 1024/44100 graph -- which looked exactly like a stuck offset | ||
| 2828 | // the loop was failing to correct. | ||
| 2829 | err * 1000.0 / AES67::AUDIO_RATE, | ||
| 2830 | elapsed, | ||
| 2831 | st->shortReads ? " (SHORT READS)" : "", | ||
| 2832 | (unsigned long long)st->seen, | ||
| 2833 | (unsigned long long)st->gapsSeen); | ||
| 2834 | } | ||
| 2835 | |||
| 2836 |
1/2✓ Branch 0 taken 18713 times.
✗ Branch 1 not taken.
|
18713 | gst_buffer_unref(buf); |
| 2837 | 18713 | GST_PAD_PROBE_INFO_DATA(info) = out; | |
| 2838 | 18713 | return GST_PAD_PROBE_OK; | |
| 2839 | } | ||
| 2840 | #endif // FPP_HAVE_SAMPLERATE | ||
| 2841 | |||
| 2842 | |||
| 2843 | // Byte counters at both ends of the send pipeline. See | ||
| 2844 | // AES67Config::pipelineStats. | ||
| 2845 | static int PipeWireGraphRate(); | ||
| 2846 | |||
| 2847 | struct PipelineStatsState { | ||
| 2848 | int instanceId = 0; | ||
| 2849 | guint64 inBytes = 0; | ||
| 2850 | guint64 outBytes = 0; | ||
| 2851 | GstClockTime start = 0; | ||
| 2852 | GstClockTime lastLog = 0; | ||
| 2853 | guint64 inAtLast = 0; | ||
| 2854 | guint64 outAtLast = 0; | ||
| 2855 | }; | ||
| 2856 | |||
| 2857 | 1 | static void DestroyPipelineStatsState(gpointer data) { | |
| 2858 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | delete static_cast<PipelineStatsState*>(data); |
| 2859 | 1 | } | |
| 2860 | |||
| 2861 | 1415 | static GstPadProbeReturn PipelineStatsIn(GstPad*, GstPadProbeInfo* info, | |
| 2862 | gpointer user) { | ||
| 2863 | 1415 | auto* st = static_cast<PipelineStatsState*>(user); | |
| 2864 | 1415 | GstBuffer* b = GST_PAD_PROBE_INFO_BUFFER(info); | |
| 2865 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1415 times.
|
1415 | if (!b) { |
| 2866 | ✗ | return GST_PAD_PROBE_OK; | |
| 2867 | } | ||
| 2868 | 1415 | st->inBytes += gst_buffer_get_size(b); | |
| 2869 | |||
| 2870 | 1415 | const GstClockTime now = gst_util_get_timestamp(); | |
| 2871 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1414 times.
|
1415 | if (st->start == 0) { |
| 2872 | 1 | st->start = now; | |
| 2873 | 1 | st->lastLog = now; | |
| 2874 | 1 | return GST_PAD_PROBE_OK; | |
| 2875 | } | ||
| 2876 |
2/2✓ Branch 0 taken 1413 times.
✓ Branch 1 taken 1 times.
|
1414 | if (now - st->lastLog < 30 * GST_SECOND) { |
| 2877 | 1413 | return GST_PAD_PROBE_OK; | |
| 2878 | } | ||
| 2879 | 1 | const double dt = (double)(now - st->lastLog) / GST_SECOND; | |
| 2880 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogInfo(VB_MEDIAOUT, |
| 2881 | "AES67 stats [%d]: pipewiresrc %.0f B/s, udpsink %.0f B/s, " | ||
| 2882 | "ratio %.3f, totals in %llu out %llu\n", | ||
| 2883 | st->instanceId, (double)(st->inBytes - st->inAtLast) / dt, | ||
| 2884 | (double)(st->outBytes - st->outAtLast) / dt, | ||
| 2885 | (st->inBytes > st->inAtLast) | ||
| 2886 | ? (double)(st->outBytes - st->outAtLast) / | ||
| 2887 | (double)(st->inBytes - st->inAtLast) | ||
| 2888 | : 0.0, | ||
| 2889 | (unsigned long long)st->inBytes, (unsigned long long)st->outBytes); | ||
| 2890 | 1 | st->lastLog = now; | |
| 2891 | 1 | st->inAtLast = st->inBytes; | |
| 2892 | 1 | st->outAtLast = st->outBytes; | |
| 2893 | 1 | return GST_PAD_PROBE_OK; | |
| 2894 | } | ||
| 2895 | |||
| 2896 | 30186 | static GstPadProbeReturn PipelineStatsOut(GstPad*, GstPadProbeInfo* info, | |
| 2897 | gpointer user) { | ||
| 2898 | 30186 | auto* st = static_cast<PipelineStatsState*>(user); | |
| 2899 | 30186 | GstBuffer* b = GST_PAD_PROBE_INFO_BUFFER(info); | |
| 2900 |
1/2✓ Branch 0 taken 30186 times.
✗ Branch 1 not taken.
|
30186 | if (b) { |
| 2901 | 30186 | st->outBytes += gst_buffer_get_size(b); | |
| 2902 | } | ||
| 2903 | 30186 | return GST_PAD_PROBE_OK; | |
| 2904 | } | ||
| 2905 | |||
| 2906 | 185 | bool AES67Manager::CreateSendPipeline(const AES67Instance& inst) { | |
| 2907 |
2/4✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
✗ Branch 3 not taken.
|
185 | std::string nodeName = SafeNodeName(inst.name) + "_send"; |
| 2908 |
1/2✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
|
185 | std::string sourceIP = GetInterfaceIP(inst.interface); |
| 2909 | |||
| 2910 | // Calculate ptime in nanoseconds for rtpL24pay | ||
| 2911 | 185 | int64_t ptimeNs = (int64_t)inst.ptime * 1000000LL; | |
| 2912 | |||
| 2913 | // Build pipeline string | ||
| 2914 | // pipewiresrc registers itself as a PipeWire node with the expected name | ||
| 2915 | // so that the audio group's filter-chain output (which has | ||
| 2916 | // node.target=<nodeName>) can connect to it. We do NOT use | ||
| 2917 | // target-object because this node is the DESTINATION, not the source. | ||
| 2918 | // | ||
| 2919 | // Pipeline: | ||
| 2920 | // pipewiresrc stream-properties="props,node.name=<node>" | ||
| 2921 | // ! audioconvert ! audioresample ! audioconvert | ||
| 2922 | // ! audio/x-raw,format=S24BE,rate=48000,channels=N | ||
| 2923 | // ! rtpL24pay pt=96 min-ptime=<ns> max-ptime=<ns> | ||
| 2924 | // ! application/x-rtp,clock-rate=48000 | ||
| 2925 | // ! udpsink host=<multicast> port=<port> multicast-iface=<iface> ttl-mc=4 auto-multicast=true sync=true | ||
| 2926 | // | ||
| 2927 | // The resampler is not optional. AES67 mandates 48 kHz on the wire, and the | ||
| 2928 | // PipeWire graph no longer runs there: since the graph clock started | ||
| 2929 | // following the output card it sits at whatever that card is configured for, | ||
| 2930 | // 44100 by default. pipewiresrc cannot satisfy a caps filter that asks for | ||
| 2931 | // a different rate AND a format PipeWire does not carry natively (S24BE, the | ||
| 2932 | // big-endian packing L24 needs) -- it does not fail, it simply never | ||
| 2933 | // negotiates, so set_state() blocks and eventually returns FAILURE, which is | ||
| 2934 | // the "AES67: audio send stream failed to start" warning. | ||
| 2935 | // | ||
| 2936 | // Measured against the live 44100 graph: | ||
| 2937 | // audioconvert ! S24BE@48000 hangs | ||
| 2938 | // audioconvert ! S24BE@44100 works (no rate change) | ||
| 2939 | // audioresample ! audioconvert ! S24BE@48000 hangs | ||
| 2940 | // audioconvert ! audioresample ! audioconvert ! S24BE@48000 works | ||
| 2941 | // | ||
| 2942 | // So the conversion has to be split: reach a format the resampler is happy | ||
| 2943 | // to work in, resample, and only then pack to S24BE. This costs nothing | ||
| 2944 | // when the graph already runs at 48000, where the resampler passes through. | ||
| 2945 | |||
| 2946 |
1/2✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
|
185 | std::ostringstream oss; |
| 2947 | // Media clock -- see GetOrCreateMediaClock(). Everything below that makes | ||
| 2948 | // the RTP timestamps mean something depends on having it. | ||
| 2949 |
3/4✓ Branch 0 taken 184 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 184 times.
✗ Branch 3 not taken.
|
185 | GstClock* ptpClock = m_config.ptpMediaClock ? GetOrCreateMediaClock() : nullptr; |
| 2950 | // Correcting the rate only means anything when the timeline it is being | ||
| 2951 | // corrected against is PTP. | ||
| 2952 |
4/4✓ Branch 0 taken 80 times.
✓ Branch 1 taken 105 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 78 times.
|
185 | const bool driftControl = (ptpClock != nullptr) && m_config.adaptiveResample; |
| 2953 | 185 | const bool sinkPacing = m_config.sinkPacing; | |
| 2954 | |||
| 2955 | // Caps above stereo must name the channel layout or nothing negotiates -- | ||
| 2956 | // see ChannelMaskFor(). Emitted into both caps filters below. | ||
| 2957 | 185 | const guint64 mask = AES67::ChannelMaskFor(inst.channels); | |
| 2958 | const std::string chanMask = | ||
| 2959 | ✗ | mask ? (",channel-mask=(bitmask)0x" + [&] { | |
| 2960 | char b[32]; | ||
| 2961 | 185 | snprintf(b, sizeof(b), "%llx", (unsigned long long)mask); | |
| 2962 |
1/2✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
|
370 | return std::string(b); |
| 2963 |
2/6✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
370 | }()) |
| 2964 |
2/4✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
✗ Branch 3 not taken.
|
370 | : std::string(); |
| 2965 | 185 | const bool rateMatch = m_config.rateMatch; | |
| 2966 | #ifdef FPP_HAVE_SAMPLERATE | ||
| 2967 |
4/4✓ Branch 0 taken 182 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 78 times.
✓ Branch 3 taken 104 times.
|
185 | const bool driftResample = m_config.driftResample && (ptpClock != nullptr); |
| 2968 | // sourceSilenceFloor is applied by DriftResampleProbe, and that probe is | ||
| 2969 | // only installed when driftResample is on -- so the flag on its own does | ||
| 2970 | // nothing. Say so rather than accepting it into silence: a set flag that | ||
| 2971 | // no-ops reads as a fault in the sender when someone tests against real | ||
| 2972 | // receiver hardware. | ||
| 2973 |
3/4✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 107 times.
✓ Branch 3 taken 78 times.
|
185 | if (m_config.sourceSilenceFloor && !driftResample) { |
| 2974 |
3/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 105 times.
✓ Branch 2 taken 107 times.
✗ Branch 3 not taken.
|
107 | LogWarn(VB_MEDIAOUT, |
| 2975 | "AES67 send [%d]: sourceSilenceFloor is set but has no effect " | ||
| 2976 | "without driftResample%s -- source gaps will not be filled\n", | ||
| 2977 | inst.id, ptpClock ? "" : " and a PTP clock"); | ||
| 2978 | } | ||
| 2979 | #else | ||
| 2980 | const bool driftResample = false; | ||
| 2981 | if (m_config.driftResample) { | ||
| 2982 | LogWarn(VB_MEDIAOUT, | ||
| 2983 | "AES67 send [%d]: driftResample requested but this build has " | ||
| 2984 | "no libsamplerate\n", inst.id); | ||
| 2985 | } | ||
| 2986 | if (m_config.sourceSilenceFloor) { | ||
| 2987 | LogWarn(VB_MEDIAOUT, | ||
| 2988 | "AES67 send [%d]: sourceSilenceFloor requested but this build " | ||
| 2989 | "has no libsamplerate -- source gaps will not be filled\n", | ||
| 2990 | inst.id); | ||
| 2991 | } | ||
| 2992 | #endif | ||
| 2993 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 184 times.
|
185 | if (!m_config.ptpMediaClock) { |
| 2994 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogInfo(VB_MEDIAOUT, "AES67 send [%d]: PTP media clock disabled by config\n", inst.id); |
| 2995 | } | ||
| 2996 | |||
| 2997 | // Ask PipeWire for the graph's own rate. Without this the AES67 rate | ||
| 2998 | // propagates all the way up and PipeWire resamples for this node alone, | ||
| 2999 | // which is what degrades after ~11 minutes. See | ||
| 3000 | // AES67Config::nativeSourceRate. audioresample below then does the | ||
| 3001 | // conversion, which is what it is there for. | ||
| 3002 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 185 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
185 | const int graphRate = m_config.nativeSourceRate ? PipeWireGraphRate() : 0; |
| 3003 | |||
| 3004 | // Multichannel needs the graph clocked at 48kHz. Above stereo, PipeWire | ||
| 3005 | // resampling every quantum into the 48kHz AES67 node delays buffer | ||
| 3006 | // delivery enough that they reach udpsink already past their PTS, and | ||
| 3007 | // sync=true cannot pace what is already late -- it sends back-to-back to | ||
| 3008 | // catch up. Measured at 8 channels on a 44.1kHz graph: 74% of packets | ||
| 3009 | // back-to-back even with always-copy on; at 48kHz, clean, with jitter | ||
| 3010 | // inside the 1ms AES67 recommends. Stereo is unaffected either way. | ||
| 3011 | // | ||
| 3012 | // The graph rate follows the selected playback card's achieved rate (see | ||
| 3013 | // FPPINIT_Audio), so this is not always the AudioFormat setting alone. | ||
| 3014 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 177 times.
|
185 | if (inst.channels > AES67::MULTICHANNEL_NEEDS_BUFFER_COPY) { |
| 3015 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | const int actualRate = PipeWireGraphRate(); |
| 3016 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
8 | if (actualRate > 0 && actualRate != AES67::AUDIO_RATE) { |
| 3017 | ✗ | LogWarn(VB_MEDIAOUT, | |
| 3018 | "AES67 send [%d]: %d channels on a %dHz graph clock. " | ||
| 3019 | "Above stereo this paces badly -- the graph has to be %dHz. " | ||
| 3020 | "Set AudioFormat to 48kHz and use a playback card that runs " | ||
| 3021 | "at 48kHz, or keep this stream at 2 channels.\n", | ||
| 3022 | inst.id, inst.channels, actualRate, AES67::AUDIO_RATE); | ||
| 3023 | } | ||
| 3024 | } | ||
| 3025 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 185 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
185 | if (graphRate > 0 && graphRate != AES67::AUDIO_RATE) { |
| 3026 | ✗ | LogInfo(VB_MEDIAOUT, | |
| 3027 | "AES67 send [%d]: taking source at graph rate %d, converting " | ||
| 3028 | "to %d in the pipeline\n", | ||
| 3029 | inst.id, graphRate, AES67::AUDIO_RATE); | ||
| 3030 | } | ||
| 3031 | |||
| 3032 | oss << "pipewiresrc name=pwsrc" | ||
| 3033 |
3/6✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 185 times.
✗ Branch 5 not taken.
|
185 | << " min-buffers=" << m_config.sourceMinBuffers |
| 3034 | << " always-copy=" | ||
| 3035 | 370 | << ((m_config.sourceBufferCopy || | |
| 3036 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 177 times.
|
185 | inst.channels > AES67::MULTICHANNEL_NEEDS_BUFFER_COPY) |
| 3037 | ? "true" : "false") | ||
| 3038 | << " " | ||
| 3039 | ✗ | << ((graphRate > 0 && graphRate != AES67::AUDIO_RATE) | |
| 3040 |
2/12✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 185 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 185 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
370 | ? ("! audio/x-raw,rate=" + std::to_string(graphRate) + " ") |
| 3041 |
2/6✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 185 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
370 | : "") |
| 3042 | << "! audioconvert " | ||
| 3043 | << "! audioresample " | ||
| 3044 | << "! audioconvert " | ||
| 3045 | // The master volume (see SetMasterVolume()). Passes buffers through | ||
| 3046 | // untouched at 100%. | ||
| 3047 |
13/26✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 185 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 185 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 185 times.
✓ Branch 10 taken 185 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 185 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 185 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 185 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 185 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 185 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 185 times.
✗ Branch 23 not taken.
✓ Branch 24 taken 185 times.
✗ Branch 25 not taken.
|
555 | << "! volume name=mastervol volume=" << masterVolumeGain(getVolume()) << " " |
| 3048 | // Rate trim for the drift control loop. Sits in the float domain | ||
| 3049 | // because that is all this element accepts, and ahead of the S24BE | ||
| 3050 | // conversion and the packet split so both still see a clean stream. | ||
| 3051 | // Pin the format AND channel count across the rate trim. "speed" | ||
| 3052 | // advertises channels [1, MAX] and nothing downstream forces a count | ||
| 3053 | // until the S24BE capsfilter, so negotiation happily settles on mono | ||
| 3054 | // and audioconvert quietly upmixes back to stereo afterwards: correct | ||
| 3055 | // packet sizes, correct timestamps, valid SDP, and a mono stream. | ||
| 3056 | // Nothing at the packet level catches it -- it showed up as a mono | ||
| 3057 | // feed in the PipeWire graph. | ||
| 3058 | << (driftControl | ||
| 3059 |
7/16✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 183 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 183 times.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 183 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
|
372 | ? ("! audio/x-raw,format=F32LE,channels=" + std::to_string(inst.channels) + |
| 3060 | " ! pitch name=drift " | ||
| 3061 |
6/12✓ 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 taken 183 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 183 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
374 | "! audio/x-raw,format=F32LE,channels=" + std::to_string(inst.channels) + " ") |
| 3062 |
4/6✓ Branch 0 taken 183 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 183 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
368 | : "") |
| 3063 | // Tap point for drift correction. identity does nothing itself; the | ||
| 3064 | // resampling happens in a pad probe on its src pad, in the float | ||
| 3065 | // domain and before the audio is cut into packets, so every packet | ||
| 3066 | // downstream is still exactly one ptime long. | ||
| 3067 | << (driftResample | ||
| 3068 |
2/6✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 107 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
263 | ? ("! audio/x-raw,format=F32LE,rate=" + |
| 3069 |
8/16✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 78 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 78 times.
✓ Branch 5 taken 107 times.
✓ Branch 6 taken 78 times.
✓ Branch 7 taken 107 times.
✓ Branch 8 taken 78 times.
✓ Branch 9 taken 107 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
|
341 | std::to_string(AES67::AUDIO_RATE) + ",channels=" + |
| 3070 |
6/14✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 78 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 78 times.
✓ Branch 5 taken 107 times.
✓ Branch 6 taken 78 times.
✓ Branch 7 taken 107 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
|
526 | std::to_string(inst.channels) + chanMask + |
| 3071 | " ! identity name=driftpoint ") | ||
| 3072 |
4/6✓ Branch 0 taken 107 times.
✓ Branch 1 taken 78 times.
✓ Branch 2 taken 78 times.
✓ Branch 3 taken 107 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
292 | : "") |
| 3073 | << "! audioconvert " | ||
| 3074 |
10/16✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 183 times.
✓ Branch 4 taken 185 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 78 times.
✓ Branch 7 taken 107 times.
✓ Branch 8 taken 185 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 185 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 185 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 185 times.
✗ Branch 15 not taken.
|
555 | << "! audio/x-raw,format=S24BE,rate=" << AES67::AUDIO_RATE |
| 3075 |
2/4✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
✗ Branch 3 not taken.
|
185 | << ",channels=" << inst.channels << chanMask << " " |
| 3076 | // Re-block the audio into exactly one packet per buffer, on a timeline | ||
| 3077 | // aligned to sample boundaries. pipewiresrc hands us whatever the graph | ||
| 3078 | // quantum produced, with timestamps that do not land on packet | ||
| 3079 | // boundaries -- and the payloader then has to choose between following | ||
| 3080 | // those timestamps (RTP increments wobble +/-1 sample, audibly | ||
| 3081 | // distorted) or counting samples itself (increments exact, but the | ||
| 3082 | // timeline drifts away from PTP on every dropped buffer, measured at | ||
| 3083 | // -180ms). Splitting first removes the choice: buffers are exactly | ||
| 3084 | // ptime long and correctly timestamped, so the payloader can follow the | ||
| 3085 | // running time (= PTP time) and still step exactly one packet each time. | ||
| 3086 | // Reconcile the card's sample count with PTP time before the audio is | ||
| 3087 | // cut into packets, so the payloader sees a timeline that already | ||
| 3088 | // advances at the PTP rate. See AES67Config::rateMatch. | ||
| 3089 | << (rateMatch ? "! audiorate name=ratematch " : "") | ||
| 3090 | // Also required by sinkPacing, and for a second reason: the payloader | ||
| 3091 | // stamps every packet it emits from one input buffer with that | ||
| 3092 | // buffer's timestamp, so a whole quantum's worth of packets come out | ||
| 3093 | // sharing a PTS. udpsink then has nothing to pace against and sends | ||
| 3094 | // them together -- sync=true on its own leaves the burst untouched | ||
| 3095 | // (measured: 5.8 packets per 23.22ms, identical to sync=false). | ||
| 3096 | // Splitting first gives each packet its own send time. | ||
| 3097 |
2/2✓ Branch 0 taken 104 times.
✓ Branch 1 taken 1 times.
|
105 | << ((ptpClock || sinkPacing) |
| 3098 |
2/6✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
554 | ? ("! audiobuffersplit output-buffer-duration=" + |
| 3099 |
7/12✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 184 times.
✓ Branch 4 taken 184 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 184 times.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
554 | std::to_string(inst.ptime) + "/1000 ") : "") |
| 3100 |
9/16✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 185 times.
✓ Branch 6 taken 185 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 105 times.
✓ Branch 9 taken 80 times.
✓ Branch 10 taken 185 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 185 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 185 times.
✗ Branch 15 not taken.
|
370 | << "! rtpL24pay name=pay pt=" << AES67::RTP_PAYLOAD_TYPE |
| 3101 | // Explicit, not GStreamer's 1400 default: the payloader silently | ||
| 3102 | // splits a payload larger than this, which would contradict the | ||
| 3103 | // min-ptime=max-ptime pair right below. ParseConfig has already | ||
| 3104 | // clamped ptime so one packet fits. | ||
| 3105 |
2/4✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
✗ Branch 3 not taken.
|
185 | << " mtu=" << AES67::MAX_RTP_PACKET_BYTES |
| 3106 |
2/4✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
✗ Branch 3 not taken.
|
185 | << " min-ptime=" << ptimeNs |
| 3107 |
2/4✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
✗ Branch 3 not taken.
|
185 | << " max-ptime=" << ptimeNs |
| 3108 | // timestamp-offset=0 anchors the RTP timeline to PTP (see the media | ||
| 3109 | // clock note below). perfect-rtptime stays TRUE -- deriving each | ||
| 3110 | // packet's timestamp from the buffer running time instead made the | ||
| 3111 | // increments wobble by +/-1 sample (191/193/194 rather than a clean | ||
| 3112 | // 192), and a receiver that places samples by RTP timestamp has to | ||
| 3113 | // absorb that wobble on every single packet. With it true the | ||
| 3114 | // payloader counts samples, so the timeline is anchored to PTP at | ||
| 3115 | // start and then advances exactly one packet at a time. | ||
| 3116 | // perfect-rtptime=false: follow the (now exactly aligned) running time | ||
| 3117 | // so the RTP timeline stays anchored to PTP instead of free-running | ||
| 3118 | // off a sample counter. See the audiobuffersplit note above. | ||
| 3119 | // With splitClockDomains the pipeline is no longer on PTP, so running | ||
| 3120 | // time is not PTP and perfect-rtptime=false would unanchor the RTP | ||
| 3121 | // timeline entirely (measured: -36,135,744 ms of presentation error). | ||
| 3122 | // perfect-rtptime=true makes the payloader count samples instead, and | ||
| 3123 | // driftResample already guarantees exactly 48000 of them per PTP | ||
| 3124 | // second -- so the counter runs at PTP rate whatever clock the pipeline | ||
| 3125 | // uses. The remaining job is anchoring its start via timestamp-offset, | ||
| 3126 | // which is why the absolute lead is still wrong here. | ||
| 3127 | << (ptpClock | ||
| 3128 | 80 | ? (m_config.splitClockDomains | |
| 3129 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | ? " timestamp-offset=0 perfect-rtptime=true" |
| 3130 | : " timestamp-offset=0 perfect-rtptime=false") | ||
| 3131 | : "") | ||
| 3132 | << " " | ||
| 3133 |
6/10✓ Branch 0 taken 80 times.
✓ Branch 1 taken 105 times.
✓ Branch 2 taken 185 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 185 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 185 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 185 times.
✗ Branch 9 not taken.
|
265 | << "! application/x-rtp,clock-rate=" << AES67::AUDIO_RATE << " " |
| 3134 | // A queue here is what makes sink pacing safe: it runs the sink on its | ||
| 3135 | // own thread, so the sink blocking until a packet's send time cannot | ||
| 3136 | // push back into the live pipewiresrc. Non-leaky on purpose -- in | ||
| 3137 | // steady state it drains exactly as fast as it fills, and dropping a | ||
| 3138 | // packet to keep up would defeat the point. See sinkPacing. | ||
| 3139 | // Bounded by time only. A buffer-count cap is a ptime trap: 64 buffers | ||
| 3140 | // is 256ms at 4ms ptime but only 64ms at 1ms, and the queue has to hold | ||
| 3141 | // the sink latency plus a whole graph quantum's burst -- about 61 | ||
| 3142 | // buffers at 1ms. That put the cap right on top of normal steady | ||
| 3143 | // state, the queue ran permanently full, and the backpressure silenced | ||
| 3144 | // the stream entirely on a Yamaha MRX7-D at 1ms ptime (reported on | ||
| 3145 | // #2848: "queue 64 buffers / 64ms" repeating, with no audio). | ||
| 3146 | << (sinkPacing ? "! queue name=sinkq max-size-bytes=0 " | ||
| 3147 | "max-size-buffers=0 max-size-time=200000000 " : "") | ||
| 3148 | 185 | << "! udpsink name=usink host=" << inst.multicastIP | |
| 3149 |
8/14✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 184 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 185 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 185 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 185 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 185 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 185 times.
✗ Branch 13 not taken.
|
185 | << " port=" << inst.port |
| 3150 |
2/4✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
✗ Branch 3 not taken.
|
185 | << " ttl-mc=" << AES67::AUDIO_RTP_TTL |
| 3151 |
2/4✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
✗ Branch 3 not taken.
|
185 | << " qos-dscp=" << AES67::AUDIO_DSCP |
| 3152 |
4/6✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 184 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 185 times.
✗ Branch 5 not taken.
|
185 | << " auto-multicast=true sync=" << (sinkPacing ? "true" : "false"); |
| 3153 | |||
| 3154 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 182 times.
|
185 | if (!inst.interface.empty()) { |
| 3155 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | oss << " multicast-iface=" << inst.interface; |
| 3156 | } | ||
| 3157 | |||
| 3158 |
1/2✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
|
185 | std::string pipelineStr = oss.str(); |
| 3159 |
1/2✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
|
185 | LogInfo(VB_MEDIAOUT, "AES67 send pipeline [%d] %s: %s\n", |
| 3160 | inst.id, inst.name.c_str(), pipelineStr.c_str()); | ||
| 3161 | |||
| 3162 | 185 | GError* error = nullptr; | |
| 3163 |
1/2✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
|
185 | GstElement* pipeline = gst_parse_launch(pipelineStr.c_str(), &error); |
| 3164 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 185 times.
|
185 | if (error) { |
| 3165 | ✗ | LogErr(VB_MEDIAOUT, "AES67 send pipeline error [%d]: %s\n", | |
| 3166 | inst.id, error->message); | ||
| 3167 | ✗ | g_error_free(error); | |
| 3168 | // gst_parse_launch() can return a non-null (partial) pipeline even | ||
| 3169 | // when it also reports an error -- release it so we don't leak. | ||
| 3170 | ✗ | if (pipeline) { | |
| 3171 | ✗ | gst_object_unref(pipeline); | |
| 3172 | } | ||
| 3173 | ✗ | return false; | |
| 3174 | } | ||
| 3175 | |||
| 3176 | // Set stream-properties post-launch — inline GstStructure values in | ||
| 3177 | // gst_parse_launch can crash gst_value_deserialize on some platforms. | ||
| 3178 |
3/6✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 185 times.
✗ Branch 5 not taken.
|
185 | GstElement* pwsrc = gst_bin_get_by_name(GST_BIN(pipeline), "pwsrc"); |
| 3179 |
1/2✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
|
185 | if (pwsrc) { |
| 3180 | // Ask PipeWire for a quantum no larger than one RTP packet. | ||
| 3181 | // | ||
| 3182 | // This is what makes the stream evenly paced. At the stock 1024-sample | ||
| 3183 | // quantum pipewiresrc hands us ~21ms of audio at once and the packets | ||
| 3184 | // for it leave back-to-back in microseconds, then nothing for 21ms -- | ||
| 3185 | // which is far outside the receive window of a Dante/AES67 receiver. | ||
| 3186 | // | ||
| 3187 | // Pacing it at the sink instead (udpsink sync=true) does not work here: | ||
| 3188 | // buffers arrive with a PTS already one quantum in the past, so the | ||
| 3189 | // sink blocks, backpressure reaches the live pipewiresrc, and it drops | ||
| 3190 | // audio rather than stalling. Measured during playback that cost ~36% | ||
| 3191 | // of the stream. Fixing the cadence at the source has no such failure | ||
| 3192 | // mode -- and it cuts sender latency, which the receiver has to absorb | ||
| 3193 | // as link offset. | ||
| 3194 | // | ||
| 3195 | // PipeWire clamps this to its min-quantum, and the graph quantum is the | ||
| 3196 | // smallest any node asks for, so this does raise CPU for the whole | ||
| 3197 | // graph. That is the trade for a stream that is actually usable. | ||
| 3198 | 370 | std::string nodeLatency = std::to_string(inst.ptime * AES67::AUDIO_RATE / 1000) + | |
| 3199 |
2/4✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
✗ Branch 3 not taken.
|
555 | "/" + std::to_string(AES67::AUDIO_RATE); |
| 3200 |
1/2✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
|
185 | GstStructure* props = gst_structure_new("props", |
| 3201 | "node.name", G_TYPE_STRING, nodeName.c_str(), | ||
| 3202 | "node.autoconnect", G_TYPE_BOOLEAN, FALSE, | ||
| 3203 | // Keep this node scheduled even when nothing is feeding it, so the | ||
| 3204 | // AES67 side sees one continuous stream -- silence when idle, audio | ||
| 3205 | // when a track is playing -- instead of a hole at every transition. | ||
| 3206 | // | ||
| 3207 | // Idle already works: with no media at all the stream still runs at | ||
| 3208 | // 100% delivery with no gaps, because PipeWire hands us silence. | ||
| 3209 | // The hole appears only at the moment fppd_stream_1 disconnects and | ||
| 3210 | // the graph relinks, when this node is briefly unscheduled: measured | ||
| 3211 | // as a +232 ms PTS jump at every track change. That matters because | ||
| 3212 | // the RTP timestamp is a sample count, so samples that never arrive | ||
| 3213 | // put the media clock permanently behind PTP -- measured at | ||
| 3214 | // -6078 ppm under a 25s file on repeat, which is AES67 running ahead | ||
| 3215 | // of the local sound card by ~180 ms per track and accumulating. | ||
| 3216 | // | ||
| 3217 | // Upstream sets the same property on module-rtp-sink's stream for | ||
| 3218 | // the same reason (pipewire-aes67.conf). | ||
| 3219 | "node.always-process", G_TYPE_BOOLEAN, TRUE, | ||
| 3220 | NULL); | ||
| 3221 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 185 times.
|
185 | if (m_config.sourcePacing) { |
| 3222 | ✗ | gst_structure_set(props, "node.latency", G_TYPE_STRING, nodeLatency.c_str(), NULL); | |
| 3223 | } else { | ||
| 3224 |
1/2✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
|
185 | LogInfo(VB_MEDIAOUT, "AES67 send [%d]: source pacing disabled by config\n", inst.id); |
| 3225 | } | ||
| 3226 | // Schedule this node from PTP rather than the card. See | ||
| 3227 | // AES67Config::sourcePtpGroup. This has to go in *this* structure -- | ||
| 3228 | // g_object_set replaces the property wholesale, so setting it in the | ||
| 3229 | // gst_parse_launch string is silently discarded here. | ||
| 3230 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 185 times.
|
185 | if (m_config.sourcePtpGroup) { |
| 3231 | ✗ | gst_structure_set(props, "node.group", G_TYPE_STRING, | |
| 3232 | m_config.sourcePtpGroupName.c_str(), NULL); | ||
| 3233 | // node.group alone stalls: joining the PTP group puts this node in | ||
| 3234 | // its own driver domain, and the link back to the card-driven graph | ||
| 3235 | // then has nothing to bridge it -- measured, delivery went to zero | ||
| 3236 | // within two minutes. node.async is the mechanism PipeWire 1.2 | ||
| 3237 | // added for exactly that link: output ports write to the | ||
| 3238 | // (cycle+1)&1 async buffer slot and input ports read (cycle&1), so | ||
| 3239 | // the two ends need not be in the same cycle. Costs one quantum of | ||
| 3240 | // latency and needs at least two buffers negotiated on the link. | ||
| 3241 | ✗ | if (m_config.sourceAsync) { | |
| 3242 | ✗ | gst_structure_set(props, "node.async", G_TYPE_BOOLEAN, TRUE, NULL); | |
| 3243 | } | ||
| 3244 | ✗ | LogInfo(VB_MEDIAOUT, | |
| 3245 | "AES67 send [%d]: source node joining PTP driver group %s%s\n", | ||
| 3246 | inst.id, m_config.sourcePtpGroupName.c_str(), | ||
| 3247 | m_config.sourceAsync ? " (async)" : ""); | ||
| 3248 | } | ||
| 3249 |
1/2✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
|
185 | g_object_set(pwsrc, "stream-properties", props, NULL); |
| 3250 |
1/2✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
|
185 | gst_structure_free(props); |
| 3251 |
1/2✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
|
185 | gst_object_unref(pwsrc); |
| 3252 | 185 | } | |
| 3253 | |||
| 3254 | // Put the pipeline on PTP time, and line the RTP timeline up with it. | ||
| 3255 | // | ||
| 3256 | // "a=mediaclk:direct=0" in our SDP asserts that the RTP timestamp IS the | ||
| 3257 | // media clock count on the reference clock, with zero offset. Three things | ||
| 3258 | // have to be true together for that to hold: | ||
| 3259 | // | ||
| 3260 | // 1. the pipeline clock is PTP time (use_clock below) | ||
| 3261 | // 2. base time is zero, so a buffer's running time IS absolute PTP time | ||
| 3262 | // rather than time-since-this-pipeline-started | ||
| 3263 | // 3. the payloader adds no offset of its own (timestamp-offset=0) | ||
| 3264 | // | ||
| 3265 | // rtpL24pay computes RTP ts = timestamp-offset + running_time * 48000 / 1e9, | ||
| 3266 | // so with (2) and (3) that is exactly PTP nanoseconds scaled to 48 kHz | ||
| 3267 | // samples and wrapped at 2^32 -- which is what a receiver reconstructs from | ||
| 3268 | // its own PTP time. Previously the offset was random and the running time | ||
| 3269 | // was relative to pipeline start, so the mapping was wrong by an arbitrary | ||
| 3270 | // amount up to 2^32 samples (~24 hours) and no conformant receiver could | ||
| 3271 | // place the audio. | ||
| 3272 | // | ||
| 3273 | // Setting the start time to NONE stops GStreamer recalculating base time on | ||
| 3274 | // every PAUSED->PLAYING. That is what keeps one continuous media timeline | ||
| 3275 | // across track changes and flushes: RTP timestamps stay tied to wall-clock | ||
| 3276 | // PTP instead of jumping whenever the pipeline is disturbed, which is how a | ||
| 3277 | // Dante/AES67 receiver expects a transmitter to behave. | ||
| 3278 | // splitClockDomains: leave the pipeline on the graph's own clock and use | ||
| 3279 | // PTP only where it is actually required. Putting the whole pipeline on | ||
| 3280 | // PTP is what drains the source ring: pipewiresrc pulls on pipeline time | ||
| 3281 | // while PipeWire fills at the sound card's rate, so at ~54ppm the ring | ||
| 3282 | // empties after (min-buffers - 1) quanta and the source then skips a graph | ||
| 3283 | // cycle per rotation forever. Measured both ways, min-buffers=8: | ||
| 3284 | // | ||
| 3285 | // pipeline on PTP gap burst at buffer 145407 (~56 min) | ||
| 3286 | // pipeline on graph no burst in 74 min (~190000 buffers) | ||
| 3287 | // | ||
| 3288 | // driftResample keeps its own reference to ptpClock, so the output is still | ||
| 3289 | // converted to exactly 48000 samples per PTP second and its PTS are still | ||
| 3290 | // anchored to PTP -- the RTP timestamps stay conformant either way. What | ||
| 3291 | // this does give up is sink pacing against PTP, since udpsink syncs to the | ||
| 3292 | // pipeline clock; that is the ts-offset servo's job. | ||
| 3293 |
3/4✓ Branch 0 taken 80 times.
✓ Branch 1 taken 105 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 80 times.
|
185 | if (ptpClock && !m_config.splitClockDomains) { |
| 3294 | ✗ | gst_pipeline_use_clock(GST_PIPELINE(pipeline), ptpClock); | |
| 3295 | ✗ | gst_element_set_start_time(pipeline, GST_CLOCK_TIME_NONE); | |
| 3296 | ✗ | gst_element_set_base_time(pipeline, 0); | |
| 3297 |
2/2✓ Branch 0 taken 80 times.
✓ Branch 1 taken 105 times.
|
185 | } else if (ptpClock) { |
| 3298 | // Deliberately do NOT pin start time or force base time to 0 here. | ||
| 3299 | // Those exist so running time equals PTP time on the stock path, where | ||
| 3300 | // the pipeline clock *is* PTP. Carrying them over when the pipeline is | ||
| 3301 | // on the graph clock is what made the sink burst: base time 0 makes | ||
| 3302 | // running time equal raw clock time, which the source's PTS never match, | ||
| 3303 | // so udpsink judges every buffer already overdue and flushes the lot -- | ||
| 3304 | // measured, 83% of packets back-to-back in bursts of one graph quantum. | ||
| 3305 | // With normal base-time handling the sink can pace again, and RTP | ||
| 3306 | // timestamps no longer depend on running time anyway now that | ||
| 3307 | // perfect-rtptime counts samples. | ||
| 3308 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | LogInfo(VB_MEDIAOUT, |
| 3309 | "AES67 send [%d]: split clock domains -- pipeline on the graph " | ||
| 3310 | "clock, PTP for the media clock and RTP timestamps\n", inst.id); | ||
| 3311 | } | ||
| 3312 | |||
| 3313 | // splitClockDomains: anchor the RTP timeline to PTP, then hold the lead. | ||
| 3314 | // | ||
| 3315 | // The anchor sets where the timeline starts; the servo keeps it there. | ||
| 3316 | // Both are needed. perfect-rtptime makes the payloader count samples, so | ||
| 3317 | // the RTP timestamp no longer derives from the PTS and the sinkPacing PTS | ||
| 3318 | // shift no longer produces the transmit lead -- the offset below does. | ||
| 3319 | // It must be set before PLAYING: rtpL24pay latches its base on the segment, | ||
| 3320 | // and setting it from a probe on the first buffer was measured to do | ||
| 3321 | // nothing at all. | ||
| 3322 | // | ||
| 3323 | // That makes it provisional. It is taken when the pipeline is built, and | ||
| 3324 | // the first audio reaches the sink whenever the graph first feeds this node | ||
| 3325 | // -- 100 ms later on a Pi, a second or more when a group links it late. | ||
| 3326 | // Every packet then carries an RTP time that much earlier than it should, | ||
| 3327 | // and the servo below could only ever answer that by moving transmission | ||
| 3328 | // earlier, past the point where the audio has arrived at all: from then on | ||
| 3329 | // udpsink sent each buffer the moment it arrived, a quantum at a time, and | ||
| 3330 | // the lead it measured (its own projection) still read +3 ms while the | ||
| 3331 | // wire was tens of ms late. So the servo's probe re-anchors the timeline | ||
| 3332 | // on the first packet that actually reaches the sink, by shifting the RTP | ||
| 3333 | // timestamp it carries -- and every one after it by the same amount. | ||
| 3334 | // | ||
| 3335 | // On its own that anchor does not hold. Every source discontinuity makes | ||
| 3336 | // the drift loop re-anchor its PTS timeline to absorb the gap ("-231 ms gap | ||
| 3337 | // absorbed"), while the RTP sample counter does not move with it, so the | ||
| 3338 | // fixed relationship the anchor established is broken and the lead steps. | ||
| 3339 | // Measured across a night of media restarts it ratcheted one way only, | ||
| 3340 | // +23.2 -> +30.6 -> +34.1 -> +40.6 -> +44.5 ms, roughly 4-7ms per restart, | ||
| 3341 | // never recovering, with the pacing degrading alongside it. An open-loop | ||
| 3342 | // servo cannot fix that by construction: it corrects the clock difference | ||
| 3343 | // it is told about, not the error it actually produces. | ||
| 3344 | // | ||
| 3345 | // So the servo measures the lead and drives it to targetLeadMs. For each | ||
| 3346 | // outgoing packet it reads the RTP timestamp straight out of the header | ||
| 3347 | // (bytes 4-7; no gstreamer-rtp dependency for four bytes), works out when | ||
| 3348 | // the sink will actually transmit it, projects that onto the sample rate, | ||
| 3349 | // and compares. The subtraction is done in guint32 so the RTP wrap at | ||
| 3350 | // 2^32 handles itself. | ||
| 3351 |
3/4✓ Branch 0 taken 80 times.
✓ Branch 1 taken 105 times.
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
|
185 | if (ptpClock && m_config.splitClockDomains) { |
| 3352 |
3/6✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 80 times.
✗ Branch 5 not taken.
|
80 | GstElement* payEl = gst_bin_get_by_name(GST_BIN(pipeline), "pay"); |
| 3353 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | if (payEl) { |
| 3354 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | const GstClockTime target = gst_clock_get_time(ptpClock) + |
| 3355 | 80 | (GstClockTime)m_config.sinkPacingMs * GST_MSECOND; | |
| 3356 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | const guint32 base = (guint32)gst_util_uint64_scale( |
| 3357 | 80 | target, AES67::AUDIO_RATE, GST_SECOND); | |
| 3358 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | g_object_set(payEl, "timestamp-offset", base, NULL); |
| 3359 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | LogInfo(VB_MEDIAOUT, |
| 3360 | "AES67 send [%d]: RTP timeline anchored to PTP " | ||
| 3361 | "(timestamp-offset %u, holding %dms lead)\n", | ||
| 3362 | inst.id, base, m_config.targetLeadMs); | ||
| 3363 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | gst_object_unref(payEl); |
| 3364 | } | ||
| 3365 | |||
| 3366 | // No reference to the sink here: the probe sits on the sink's own pad | ||
| 3367 | // and pads only drop their probes when their element is disposed, so a | ||
| 3368 | // ref held by the probe kept the sink -- and with it this struct, the | ||
| 3369 | // PTP clock and its /dev/ptp fd -- alive forever after the pipeline was | ||
| 3370 | // freed, one more on every rebuild. The probe reads the sink off the | ||
| 3371 | // pad instead. | ||
| 3372 | struct LeadServo { | ||
| 3373 | GstClock* ptp; | ||
| 3374 | GstClock* pipe; | ||
| 3375 | gint64 targetNs; | ||
| 3376 | guint64 tick; | ||
| 3377 | int instanceId; | ||
| 3378 | bool logged; | ||
| 3379 | gint64 latency; // sink render delay; -1 until queried | ||
| 3380 | gint64 anchorNs; // RTP time ahead of PTP at the first packet | ||
| 3381 | bool anchored; // rtpShift taken from the first packet | ||
| 3382 | guint32 rtpShift; // added to every RTP timestamp from then on | ||
| 3383 | // Set once the struct is filled in, just before the probe is | ||
| 3384 | // installed: the probe's first read pairs with it. The pad's own | ||
| 3385 | // lock orders the hand-off, but inside GStreamer where | ||
| 3386 | // ThreadSanitizer can't see it. | ||
| 3387 | std::atomic<bool> published{false}; | ||
| 3388 | }; | ||
| 3389 |
3/6✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 80 times.
✗ Branch 5 not taken.
|
80 | GstElement* sinkEl = gst_bin_get_by_name(GST_BIN(pipeline), "usink"); |
| 3390 |
2/4✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
|
80 | GstPad* sinkPad = sinkEl ? gst_element_get_static_pad(sinkEl, "sink") : nullptr; |
| 3391 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | if (sinkPad) { |
| 3392 |
2/4✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
|
80 | auto* sv = new LeadServo{ GST_CLOCK(gst_object_ref(ptpClock)), |
| 3393 |
2/4✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
|
80 | gst_pipeline_get_pipeline_clock(GST_PIPELINE(pipeline)), |
| 3394 | 80 | (gint64)m_config.targetLeadMs * GST_MSECOND, | |
| 3395 | 80 | 0, inst.id, false, -1, | |
| 3396 | 80 | (gint64)m_config.sinkPacingMs * GST_MSECOND, | |
| 3397 |
4/10✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 80 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 80 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
|
240 | false, 0 }; |
| 3398 | 80 | sv->published.store(true, std::memory_order_release); | |
| 3399 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | gst_pad_add_probe( |
| 3400 | sinkPad, GST_PAD_PROBE_TYPE_BUFFER, | ||
| 3401 | ✗ | [](GstPad* pad, GstPadProbeInfo* info, gpointer user) -> GstPadProbeReturn { | |
| 3402 | 490614 | auto* v = static_cast<LeadServo*>(user); | |
| 3403 | 490614 | (void)v->published.load(std::memory_order_acquire); | |
| 3404 | // Valid for the call: the pad's element is the one running it. | ||
| 3405 |
2/4✓ Branch 0 taken 490614 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 490614 times.
✗ Branch 3 not taken.
|
490614 | GstElement* sink = GST_ELEMENT(GST_OBJECT_PARENT(pad)); |
| 3406 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 490614 times.
|
490614 | if (!sink) { |
| 3407 | ✗ | return GST_PAD_PROBE_OK; | |
| 3408 | } | ||
| 3409 | 490614 | GstBuffer* b = GST_PAD_PROBE_INFO_BUFFER(info); | |
| 3410 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 490614 times.
|
490614 | if (!b) { |
| 3411 | ✗ | return GST_PAD_PROBE_OK; | |
| 3412 | } | ||
| 3413 | // Anchor the RTP timeline where the build-time anchor would | ||
| 3414 | // have put it had the first audio arrived at once -- see | ||
| 3415 | // above. The payloader counts samples from there, so one | ||
| 3416 | // fixed shift keeps every later timestamp contiguous. | ||
| 3417 |
3/4✓ Branch 0 taken 490584 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 490584 times.
✗ Branch 3 not taken.
|
490614 | if (!v->anchored || v->rtpShift != 0) { |
| 3418 |
1/2✓ Branch 0 taken 490614 times.
✗ Branch 1 not taken.
|
490614 | b = gst_buffer_make_writable(b); |
| 3419 | 490614 | GST_PAD_PROBE_INFO_DATA(info) = b; | |
| 3420 | GstMapInfo wm; | ||
| 3421 |
2/4✓ Branch 0 taken 490614 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 490614 times.
✗ Branch 3 not taken.
|
490614 | if (gst_buffer_map(b, &wm, GST_MAP_READWRITE)) { |
| 3422 |
1/2✓ Branch 0 taken 490614 times.
✗ Branch 1 not taken.
|
490614 | if (wm.size >= 12) { |
| 3423 | 490614 | guint32 ts = ((guint32)wm.data[4] << 24) | | |
| 3424 | 490614 | ((guint32)wm.data[5] << 16) | | |
| 3425 | 490614 | ((guint32)wm.data[6] << 8) | wm.data[7]; | |
| 3426 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 490584 times.
|
490614 | if (!v->anchored) { |
| 3427 | 30 | v->anchored = true; | |
| 3428 | 60 | const guint32 want = (guint32)gst_util_uint64_scale( | |
| 3429 |
2/4✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
|
30 | gst_clock_get_time(v->ptp) + (GstClockTime)v->anchorNs, |
| 3430 | 30 | AES67::AUDIO_RATE, GST_SECOND); | |
| 3431 | 30 | v->rtpShift = want - ts; | |
| 3432 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 | LogInfo(VB_MEDIAOUT, |
| 3433 | "AES67 send [%d]: RTP timeline re-anchored at the " | ||
| 3434 | "first packet (%+.1f ms from the build-time anchor)\n", | ||
| 3435 | v->instanceId, | ||
| 3436 | (double)(gint32)v->rtpShift * 1000.0 / AES67::AUDIO_RATE); | ||
| 3437 | } | ||
| 3438 | 490614 | ts += v->rtpShift; | |
| 3439 | 490614 | wm.data[4] = (guint8)(ts >> 24); | |
| 3440 | 490614 | wm.data[5] = (guint8)(ts >> 16); | |
| 3441 | 490614 | wm.data[6] = (guint8)(ts >> 8); | |
| 3442 | 490614 | wm.data[7] = (guint8)ts; | |
| 3443 | } | ||
| 3444 |
1/2✓ Branch 0 taken 490614 times.
✗ Branch 1 not taken.
|
490614 | gst_buffer_unmap(b, &wm); |
| 3445 | } | ||
| 3446 | } | ||
| 3447 | // ~1Hz at any ptime; the correction is tens of microseconds | ||
| 3448 | // and does not need to be finer. | ||
| 3449 |
5/6✓ Branch 0 taken 1940 times.
✓ Branch 1 taken 488674 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1940 times.
✓ Branch 4 taken 488674 times.
✓ Branch 5 taken 1940 times.
|
490614 | if ((++v->tick % 250) != 0 || !GST_BUFFER_PTS_IS_VALID(b)) { |
| 3450 | 488674 | return GST_PAD_PROBE_OK; | |
| 3451 | } | ||
| 3452 | GstMapInfo m; | ||
| 3453 |
2/4✓ Branch 0 taken 1940 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1940 times.
|
1940 | if (!gst_buffer_map(b, &m, GST_MAP_READ)) { |
| 3454 | ✗ | return GST_PAD_PROBE_OK; | |
| 3455 | } | ||
| 3456 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1940 times.
|
1940 | if (m.size < 12) { |
| 3457 | ✗ | gst_buffer_unmap(b, &m); | |
| 3458 | ✗ | return GST_PAD_PROBE_OK; | |
| 3459 | } | ||
| 3460 | 1940 | const guint32 rtpTs = ((guint32)m.data[4] << 24) | | |
| 3461 | 1940 | ((guint32)m.data[5] << 16) | | |
| 3462 | 1940 | ((guint32)m.data[6] << 8) | m.data[7]; | |
| 3463 |
1/2✓ Branch 0 taken 1940 times.
✗ Branch 1 not taken.
|
1940 | gst_buffer_unmap(b, &m); |
| 3464 | |||
| 3465 | // A sink renders at PTS + base + latency + ts-offset. The | ||
| 3466 | // latency term is not optional: leaving it out biases every | ||
| 3467 | // measurement by exactly the pipeline latency, and the servo | ||
| 3468 | // then drives the real lead that far past target -- measured, | ||
| 3469 | // it ran a 20ms target down to -4.10ms, which is the | ||
| 3470 | // receiver-discards-everything failure. | ||
| 3471 |
2/2✓ Branch 0 taken 29 times.
✓ Branch 1 taken 1911 times.
|
1940 | if (v->latency < 0) { |
| 3472 |
1/2✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 | GstQuery* lq = gst_query_new_latency(); |
| 3473 |
2/4✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
|
29 | if (gst_element_query(sink, lq)) { |
| 3474 | 29 | gboolean live = FALSE; | |
| 3475 | 29 | GstClockTime minL = 0, maxL = 0; | |
| 3476 |
1/2✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 | gst_query_parse_latency(lq, &live, &minL, &maxL); |
| 3477 |
1/2✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 | v->latency = GST_CLOCK_TIME_IS_VALID(minL) ? (gint64)minL : 0; |
| 3478 | } | ||
| 3479 |
1/2✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 | gst_query_unref(lq); |
| 3480 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
|
29 | if (v->latency < 0) { |
| 3481 | ✗ | v->latency = 0; | |
| 3482 | } | ||
| 3483 | } | ||
| 3484 | 1940 | gint64 tsOff = 0; | |
| 3485 |
1/2✓ Branch 0 taken 1940 times.
✗ Branch 1 not taken.
|
1940 | g_object_get(sink, "ts-offset", &tsOff, NULL); |
| 3486 |
1/2✓ Branch 0 taken 1940 times.
✗ Branch 1 not taken.
|
1940 | const GstClockTime baseT = gst_element_get_base_time(sink); |
| 3487 |
1/2✓ Branch 0 taken 1940 times.
✗ Branch 1 not taken.
|
1940 | const GstClockTime pipeNow = gst_clock_get_time(v->pipe); |
| 3488 |
1/2✓ Branch 0 taken 1940 times.
✗ Branch 1 not taken.
|
1940 | const GstClockTime ptpNow = gst_clock_get_time(v->ptp); |
| 3489 | // When the sink will put this packet on the wire, expressed | ||
| 3490 | // in PTP time so it is comparable with the RTP timestamp. | ||
| 3491 | // A buffer whose render time has already passed is sent as it | ||
| 3492 | // arrives, now, so that is when it goes out. | ||
| 3493 | 1940 | const gint64 txPipe = (gint64)GST_BUFFER_PTS(b) + (gint64)baseT + | |
| 3494 | 1940 | v->latency + tsOff; | |
| 3495 | 1940 | const bool late = txPipe < (gint64)pipeNow; | |
| 3496 | 1940 | const gint64 txPtp = (gint64)ptpNow + | |
| 3497 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 1926 times.
|
1940 | (late ? 0 : txPipe - (gint64)pipeNow); |
| 3498 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1940 times.
|
1940 | if (txPtp <= 0) { |
| 3499 | ✗ | return GST_PAD_PROBE_OK; | |
| 3500 | } | ||
| 3501 |
1/2✓ Branch 0 taken 1940 times.
✗ Branch 1 not taken.
|
1940 | const guint32 txRtp = (guint32)gst_util_uint64_scale( |
| 3502 | 1940 | (guint64)txPtp, AES67::AUDIO_RATE, GST_SECOND); | |
| 3503 | 1940 | const gint64 leadNs = (gint64)(gint32)(rtpTs - txRtp) * | |
| 3504 | GST_SECOND / AES67::AUDIO_RATE; | ||
| 3505 | |||
| 3506 | // Delaying transmission (larger ts-offset) reduces the lead. | ||
| 3507 | // | ||
| 3508 | // Step for a large error, slew for a small one -- the same | ||
| 3509 | // split linuxptp makes with step_threshold, and for the same | ||
| 3510 | // reason. Every source discontinuity re-anchors the drift | ||
| 3511 | // loop's PTS timeline while the RTP sample counter stays | ||
| 3512 | // put, so the lead takes a step; slew-only cannot survive | ||
| 3513 | // that if the steps arrive faster than the slew rate. | ||
| 3514 | // Measured with a 25s file on repeat (a restart every ~25s | ||
| 3515 | // against a 200us/s slew, i.e. 12ms/min of authority): the | ||
| 3516 | // lead ran away to +3614 ms in sawtooth cycles, each ending | ||
| 3517 | // in under-delivery and a watchdog rebuild. Stepping puts | ||
| 3518 | // it back at the moment of the disturbance, which is the | ||
| 3519 | // right moment -- the transmission timeline is already | ||
| 3520 | // discontinuous there. | ||
| 3521 | 1940 | const gint64 err = leadNs - v->targetNs; | |
| 3522 | 1940 | const gint64 kStepThreshold = 5 * GST_MSECOND; | |
| 3523 | 1940 | const gint64 kSlew = 200 * GST_USECOND; | |
| 3524 | gint64 corr; | ||
| 3525 |
4/4✓ Branch 0 taken 1911 times.
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 1890 times.
|
1940 | if (err > kStepThreshold || err < -kStepThreshold) { |
| 3526 | 50 | corr = err; | |
| 3527 | } else { | ||
| 3528 | 1890 | corr = err / 4; | |
| 3529 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1890 times.
|
1890 | if (corr > kSlew) corr = kSlew; |
| 3530 |
2/2✓ Branch 0 taken 469 times.
✓ Branch 1 taken 1421 times.
|
1890 | if (corr < -kSlew) corr = -kSlew; |
| 3531 | } | ||
| 3532 | // Transmitting earlier cannot help a packet that is already | ||
| 3533 | // late; it only moves every later one into the past too. | ||
| 3534 |
3/4✓ Branch 0 taken 14 times.
✓ Branch 1 taken 1926 times.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
|
1940 | if (late && corr < 0) { |
| 3535 | 14 | corr = 0; | |
| 3536 | } | ||
| 3537 |
2/2✓ Branch 0 taken 543 times.
✓ Branch 1 taken 1397 times.
|
1940 | if (corr != 0) { |
| 3538 |
1/2✓ Branch 0 taken 543 times.
✗ Branch 1 not taken.
|
543 | g_object_set(sink, "ts-offset", tsOff + corr, NULL); |
| 3539 | } | ||
| 3540 | // Diagnostic: the servo drove the lead to -56ms once and | ||
| 3541 | // took 50 minutes to recover, which no combination of its | ||
| 3542 | // own gains explains. Log what it actually sees so the | ||
| 3543 | // measurement can be checked against aes67_verify rather | ||
| 3544 | // than reasoned about. | ||
| 3545 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 1914 times.
|
1940 | if ((v->tick % 15000) == 0) { |
| 3546 |
4/8✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 26 times.
✓ Branch 6 taken 26 times.
✗ Branch 7 not taken.
|
26 | LogInfo(VB_MEDIAOUT, |
| 3547 | "AES67 send [%d]: servo lead %+.2f ms err %+.2f " | ||
| 3548 | "corr %+.3f ms ts-offset %+.2f ms%s%s\n", | ||
| 3549 | v->instanceId, | ||
| 3550 | (double)leadNs / GST_MSECOND, | ||
| 3551 | (double)err / GST_MSECOND, | ||
| 3552 | (double)corr / GST_MSECOND, | ||
| 3553 | (double)(tsOff + corr) / GST_MSECOND, | ||
| 3554 | (err > kStepThreshold || err < -kStepThreshold) | ||
| 3555 | ? " STEP" : "", | ||
| 3556 | late ? " LATE" : ""); | ||
| 3557 | } | ||
| 3558 |
2/2✓ Branch 0 taken 29 times.
✓ Branch 1 taken 1911 times.
|
1940 | if (!v->logged) { |
| 3559 | 29 | v->logged = true; | |
| 3560 |
1/2✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 | LogInfo(VB_MEDIAOUT, |
| 3561 | "AES67 send [%d]: lead servo active " | ||
| 3562 | "(lead %.2f ms, target %.0f ms, sink latency " | ||
| 3563 | "%.1f ms)\n", | ||
| 3564 | v->instanceId, (double)leadNs / GST_MSECOND, | ||
| 3565 | (double)v->targetNs / GST_MSECOND, | ||
| 3566 | (double)v->latency / GST_MSECOND); | ||
| 3567 | } | ||
| 3568 | 1940 | return GST_PAD_PROBE_OK; | |
| 3569 | }, | ||
| 3570 | sv, | ||
| 3571 | ✗ | [](gpointer user) { | |
| 3572 | 80 | auto* v = static_cast<LeadServo*>(user); | |
| 3573 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | if (v->ptp) gst_object_unref(v->ptp); |
| 3574 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | if (v->pipe) gst_object_unref(v->pipe); |
| 3575 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | delete v; |
| 3576 | 80 | }); | |
| 3577 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | gst_object_unref(sinkPad); |
| 3578 | } | ||
| 3579 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | if (sinkEl) { |
| 3580 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | gst_object_unref(sinkEl); |
| 3581 | } | ||
| 3582 | } | ||
| 3583 | |||
| 3584 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 184 times.
|
185 | if (m_config.pipelineStats) { |
| 3585 |
3/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
1 | GstElement* src = gst_bin_get_by_name(GST_BIN(pipeline), "pwsrc"); |
| 3586 |
3/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
1 | GstElement* snk = gst_bin_get_by_name(GST_BIN(pipeline), "usink"); |
| 3587 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | GstPad* sp = src ? gst_element_get_static_pad(src, "src") : nullptr; |
| 3588 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | GstPad* kp = snk ? gst_element_get_static_pad(snk, "sink") : nullptr; |
| 3589 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | if (sp && kp) { |
| 3590 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | auto* ps = new PipelineStatsState(); |
| 3591 | 1 | ps->instanceId = inst.id; | |
| 3592 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | gst_pad_add_probe(kp, GST_PAD_PROBE_TYPE_BUFFER, PipelineStatsOut, |
| 3593 | ps, nullptr); | ||
| 3594 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | gst_pad_add_probe(sp, GST_PAD_PROBE_TYPE_BUFFER, PipelineStatsIn, |
| 3595 | ps, DestroyPipelineStatsState); | ||
| 3596 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogInfo(VB_MEDIAOUT, "AES67 send [%d]: pipeline stats on\n", |
| 3597 | inst.id); | ||
| 3598 | } | ||
| 3599 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | if (sp) gst_object_unref(sp); |
| 3600 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | if (kp) gst_object_unref(kp); |
| 3601 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | if (src) gst_object_unref(src); |
| 3602 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | if (snk) gst_object_unref(snk); |
| 3603 | } | ||
| 3604 | |||
| 3605 | // Raise the packet-pacing thread to SCHED_FIFO. GStreamer creates this | ||
| 3606 | // thread itself and offers no way to set its scheduling, so we take the | ||
| 3607 | // one opportunity we have to run code on it: a probe fires on the | ||
| 3608 | // streaming thread, so the first buffer through can elevate the thread | ||
| 3609 | // that carried it. The probe removes itself immediately afterwards and | ||
| 3610 | // costs nothing for the rest of the stream. | ||
| 3611 | // | ||
| 3612 | // This is not a splitClockDomains refinement -- an unpaced tail is worse | ||
| 3613 | // in every mode -- so it is unconditional. EPERM is expected and | ||
| 3614 | // survivable: fppd.service sets LimitRTPRIO, but a hand-started fppd or | ||
| 3615 | // a container without CAP_SYS_NICE will not have it, and the stream is | ||
| 3616 | // still conformant without it, just with a wider tail. | ||
| 3617 | // | ||
| 3618 | // The thread is GStreamer's, from a pool it shares with every pipeline, | ||
| 3619 | // so it has to be handed back as it was: left at SCHED_FIFO, each rebuild | ||
| 3620 | // (config apply, watchdog, PTP step, bus error) put one more pool thread | ||
| 3621 | // at FIFO 80 that later media decode or video pipelines could land on, | ||
| 3622 | // above the channel output thread (RT 10). The task that runs the thread | ||
| 3623 | // gets a leave callback that drops it back to SCHED_OTHER when the task | ||
| 3624 | // stops. | ||
| 3625 | { | ||
| 3626 |
3/6✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 185 times.
✗ Branch 5 not taken.
|
185 | GstElement* snk = gst_bin_get_by_name(GST_BIN(pipeline), "usink"); |
| 3627 |
2/4✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
✗ Branch 3 not taken.
|
185 | GstPad* kp = snk ? gst_element_get_static_pad(snk, "sink") : nullptr; |
| 3628 |
1/2✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
|
185 | if (kp) { |
| 3629 |
1/2✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
|
185 | gst_pad_add_probe( |
| 3630 | kp, GST_PAD_PROBE_TYPE_BUFFER, | ||
| 3631 | ✗ | [](GstPad* pad, GstPadProbeInfo*, gpointer user) -> GstPadProbeReturn { | |
| 3632 | 31 | const int id = GPOINTER_TO_INT(user); | |
| 3633 | // The task whose thread carried this buffer: the nearest | ||
| 3634 | // pad upstream that runs one (sinkq's, with sink pacing). | ||
| 3635 | 31 | GstTask* task = nullptr; | |
| 3636 | 31 | GstPad* up = gst_pad_get_peer(pad); | |
| 3637 |
3/6✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 31 times.
✗ Branch 5 not taken.
|
31 | for (int hop = 0; up && !task && hop < 16; hop++) { |
| 3638 | 31 | GST_OBJECT_LOCK(up); | |
| 3639 |
1/2✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
|
31 | if (GST_PAD_TASK(up)) { |
| 3640 | 31 | task = GST_TASK(gst_object_ref(GST_PAD_TASK(up))); | |
| 3641 | } | ||
| 3642 | 31 | GST_OBJECT_UNLOCK(up); | |
| 3643 |
1/2✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
|
31 | if (task) { |
| 3644 | 31 | break; | |
| 3645 | } | ||
| 3646 | ✗ | GstElement* el = gst_pad_get_parent_element(up); | |
| 3647 | ✗ | gst_object_unref(up); | |
| 3648 | ✗ | up = nullptr; | |
| 3649 | ✗ | GstPad* in = el ? gst_element_get_static_pad(el, "sink") : nullptr; | |
| 3650 | ✗ | if (el) gst_object_unref(el); | |
| 3651 | ✗ | if (in) { | |
| 3652 | ✗ | up = gst_pad_get_peer(in); | |
| 3653 | ✗ | gst_object_unref(in); | |
| 3654 | } | ||
| 3655 | } | ||
| 3656 |
1/2✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
|
31 | if (up) gst_object_unref(up); |
| 3657 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
|
31 | if (!task) { |
| 3658 | // Without a task to hand it back through, don't raise it. | ||
| 3659 | ✗ | LogWarn(VB_MEDIAOUT, "AES67 send [%d]: pacing thread not raised: " | |
| 3660 | "could not find its streaming task\n", id); | ||
| 3661 | ✗ | return GST_PAD_PROBE_REMOVE; | |
| 3662 | } | ||
| 3663 |
1/2✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
|
31 | gst_task_set_leave_callback(task, [](GstTask*, GThread*, gpointer) { |
| 3664 | 31 | struct sched_param param = {}; | |
| 3665 | 31 | pthread_setschedparam(pthread_self(), SCHED_OTHER, ¶m); | |
| 3666 | 31 | }, nullptr, nullptr); | |
| 3667 | 31 | gst_object_unref(task); | |
| 3668 |
1/2✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
|
31 | if (SetThreadRealtimePriority(AES67::SINK_RT_PRIORITY)) { |
| 3669 | 31 | LogInfo(VB_MEDIAOUT, | |
| 3670 | "AES67 send [%d]: pacing thread raised to " | ||
| 3671 | "SCHED_FIFO %d\n", id, AES67::SINK_RT_PRIORITY); | ||
| 3672 | } else { | ||
| 3673 | ✗ | LogWarn(VB_MEDIAOUT, | |
| 3674 | "AES67 send [%d]: could not raise pacing thread to " | ||
| 3675 | "SCHED_FIFO %d (%s); packets will occasionally " | ||
| 3676 | "transmit up to a ptime late\n", | ||
| 3677 | id, AES67::SINK_RT_PRIORITY, strerror(errno)); | ||
| 3678 | } | ||
| 3679 | 31 | return GST_PAD_PROBE_REMOVE; | |
| 3680 | }, | ||
| 3681 |
0/2✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
185 | GINT_TO_POINTER(inst.id), nullptr); |
| 3682 |
1/2✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
|
185 | gst_object_unref(kp); |
| 3683 | } | ||
| 3684 |
2/4✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
✗ Branch 3 not taken.
|
185 | if (snk) gst_object_unref(snk); |
| 3685 | } | ||
| 3686 | |||
| 3687 | // Always on: see SourceGapProbe. Costs one comparison per buffer and is | ||
| 3688 | // silent unless the source actually skips a cycle. | ||
| 3689 | { | ||
| 3690 |
3/6✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 185 times.
✗ Branch 5 not taken.
|
185 | GstElement* pw = gst_bin_get_by_name(GST_BIN(pipeline), "pwsrc"); |
| 3691 |
2/4✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
✗ Branch 3 not taken.
|
185 | GstPad* pwpad = pw ? gst_element_get_static_pad(pw, "src") : nullptr; |
| 3692 |
1/2✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
|
185 | if (pwpad) { |
| 3693 |
2/4✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 185 times.
|
185 | auto* gs = new SourceGapState(); |
| 3694 | 185 | gs->instanceId = inst.id; | |
| 3695 |
1/2✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
|
185 | gst_pad_add_probe(pwpad, GST_PAD_PROBE_TYPE_BUFFER, SourceGapProbe, |
| 3696 | gs, DestroySourceGapState); | ||
| 3697 |
1/2✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
|
185 | gst_object_unref(pwpad); |
| 3698 | } | ||
| 3699 |
2/4✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
✗ Branch 3 not taken.
|
185 | if (pw) gst_object_unref(pw); |
| 3700 | } | ||
| 3701 | |||
| 3702 | #ifdef FPP_HAVE_SAMPLERATE | ||
| 3703 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 107 times.
|
185 | if (driftResample) { |
| 3704 |
3/6✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 78 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 78 times.
✗ Branch 5 not taken.
|
78 | GstElement* dp = gst_bin_get_by_name(GST_BIN(pipeline), "driftpoint"); |
| 3705 |
2/4✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 78 times.
✗ Branch 3 not taken.
|
78 | GstPad* dpad = dp ? gst_element_get_static_pad(dp, "src") : nullptr; |
| 3706 | 78 | int err = 0; | |
| 3707 |
3/8✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 78 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 78 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
78 | auto* st = new DriftResampleState(); |
| 3708 | 78 | st->channels = inst.channels; | |
| 3709 | 78 | st->instanceId = inst.id; | |
| 3710 |
3/6✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 78 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 78 times.
✗ Branch 5 not taken.
|
78 | st->clock = GST_CLOCK(gst_object_ref(ptpClock)); |
| 3711 | // SINC_FASTEST is bandlimited and far above what a 56ppm correction | ||
| 3712 | // needs; the cost is a few percent of one core on a Pi. | ||
| 3713 |
1/2✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
|
78 | st->src = src_new(SRC_SINC_FASTEST, inst.channels, &err); |
| 3714 | 78 | st->fillGaps = m_config.sourceSilenceFloor; | |
| 3715 | |||
| 3716 |
2/4✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 78 times.
✗ Branch 3 not taken.
|
78 | if (dpad && st->src) { |
| 3717 |
1/2✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
|
78 | gst_pad_add_probe(dpad, GST_PAD_PROBE_TYPE_BUFFER, |
| 3718 | DriftResampleProbe, st, | ||
| 3719 | DestroyDriftResampleState); | ||
| 3720 |
1/2✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
|
78 | LogInfo(VB_MEDIAOUT, |
| 3721 | "AES67 send [%d]: drift correction on (libsamplerate)\n", | ||
| 3722 | inst.id); | ||
| 3723 | } else { | ||
| 3724 | ✗ | LogWarn(VB_MEDIAOUT, | |
| 3725 | "AES67 send [%d]: drift correction unavailable (%s)\n", | ||
| 3726 | inst.id, st->src ? "no driftpoint pad" : src_strerror(err)); | ||
| 3727 | ✗ | DestroyDriftResampleState(st); | |
| 3728 | } | ||
| 3729 |
1/2✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
|
78 | if (dpad) { |
| 3730 |
1/2✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
|
78 | gst_object_unref(dpad); |
| 3731 | } | ||
| 3732 |
1/2✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
|
78 | if (dp) { |
| 3733 |
1/2✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
|
78 | gst_object_unref(dp); |
| 3734 | } | ||
| 3735 | } | ||
| 3736 | #endif | ||
| 3737 | |||
| 3738 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 185 times.
|
185 | if (rateMatch) { |
| 3739 | ✗ | GstElement* rm = gst_bin_get_by_name(GST_BIN(pipeline), "ratematch"); | |
| 3740 | ✗ | if (rm) { | |
| 3741 | // skip-to-first is mandatory here, not a tuning choice. With the | ||
| 3742 | // PTP media clock the base time is 0, so a buffer's timestamp is | ||
| 3743 | // absolute PTP nanoseconds -- roughly 1.8e18 at present. Without | ||
| 3744 | // this audiorate treats that as a gap starting at zero and fills | ||
| 3745 | // it with silence: measured "in 1081, added 343632000" within a | ||
| 3746 | // minute, i.e. it was manufacturing hours of silence. | ||
| 3747 | ✗ | g_object_set(rm, | |
| 3748 | "tolerance", m_config.rateMatchToleranceNs, | ||
| 3749 | "skip-to-first", TRUE, | ||
| 3750 | NULL); | ||
| 3751 | ✗ | gst_object_unref(rm); | |
| 3752 | ✗ | LogInfo(VB_MEDIAOUT, | |
| 3753 | "AES67 send [%d]: rate matching on, tolerance %lluns\n", | ||
| 3754 | inst.id, | ||
| 3755 | (unsigned long long)m_config.rateMatchToleranceNs); | ||
| 3756 | } | ||
| 3757 | } | ||
| 3758 | |||
| 3759 |
2/2✓ Branch 0 taken 184 times.
✓ Branch 1 taken 1 times.
|
185 | if (sinkPacing) { |
| 3760 | // Hold each packet back so it is still in the future when the sink | ||
| 3761 | // gets it. Without the delay every buffer arrives past its running | ||
| 3762 | // time, udpsink renders each one immediately, and the burst is | ||
| 3763 | // unchanged -- pacing that never waits is not pacing. | ||
| 3764 | // | ||
| 3765 | // This has to be ts-offset on the sink rather than | ||
| 3766 | // gst_pipeline_set_latency(): the pipeline latency is recomputed from | ||
| 3767 | // a LATENCY query when the pipeline goes to PLAYING, so a value set | ||
| 3768 | // beforehand is replaced by the automatic one. That is what made an | ||
| 3769 | // earlier attempt pace for the first few seconds and then burst for | ||
| 3770 | // the rest of the run. ts-offset is applied per-buffer and survives. | ||
| 3771 | // Shift the buffer timestamps forward rather than offsetting the sink. | ||
| 3772 | // | ||
| 3773 | // ts-offset delays only the transmission; the RTP timestamps stay | ||
| 3774 | // where they were, so every packet goes out after the playout deadline | ||
| 3775 | // it declares and a conformant receiver drops the lot -- measured at | ||
| 3776 | // -23.7ms with pacing on against +7.8ms without it, and reported on | ||
| 3777 | // #2848 as a Yamaha MRX7-D sitting subscribed, green and silent on a | ||
| 3778 | // stream carrying perfectly paced, valid stereo L24. | ||
| 3779 | // | ||
| 3780 | // Moving the PTS instead carries both with it: the payloader derives | ||
| 3781 | // the RTP timestamp from the shifted PTS, and the sink renders at that | ||
| 3782 | // same shifted time. The lead over transmission therefore stays | ||
| 3783 | // whatever it was without pacing, whatever latency we choose, instead | ||
| 3784 | // of needing a correction factor fitted to one machine. | ||
| 3785 | // Held on the heap so the value can be corrected once the pipeline is | ||
| 3786 | // PLAYING and its real latency is known. See the retune below. | ||
| 3787 | // Reused rather than reallocated per rebuild. The watchdog rebuilds | ||
| 3788 | // the pipeline on sustained under-delivery, and a probe on the old | ||
| 3789 | // pipeline can still be in flight when the new one is built, so this | ||
| 3790 | // cell has to outlive both -- freeing it here would be a | ||
| 3791 | // use-after-free, and allocating a fresh one each time would leak on | ||
| 3792 | // every rebuild. One cell per instance id, alive for the process. | ||
| 3793 |
1/2✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
|
184 | auto& slot = m_sinkPacingShift[inst.id]; |
| 3794 |
2/2✓ Branch 0 taken 71 times.
✓ Branch 1 taken 113 times.
|
184 | if (!slot) { |
| 3795 |
2/4✓ Branch 0 taken 71 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 71 times.
|
71 | slot = new std::atomic<GstClockTime>(0); |
| 3796 | } | ||
| 3797 | 184 | slot->store((GstClockTime)m_config.sinkPacingMs * GST_MSECOND, | |
| 3798 | std::memory_order_relaxed); | ||
| 3799 | 184 | auto* shiftCell = slot; | |
| 3800 | |||
| 3801 |
3/6✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 184 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 184 times.
✗ Branch 5 not taken.
|
184 | GstElement* pay = gst_bin_get_by_name(GST_BIN(pipeline), "pay"); |
| 3802 |
2/4✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 184 times.
✗ Branch 3 not taken.
|
184 | GstPad* payPad = pay ? gst_element_get_static_pad(pay, "sink") : nullptr; |
| 3803 |
1/2✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
|
184 | if (payPad) { |
| 3804 |
1/2✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
|
184 | gst_pad_add_probe( |
| 3805 | payPad, GST_PAD_PROBE_TYPE_BUFFER, | ||
| 3806 | ✗ | [](GstPad*, GstPadProbeInfo* info, gpointer user) { | |
| 3807 | 497519 | GstBuffer* b = GST_PAD_PROBE_INFO_BUFFER(info); | |
| 3808 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 497519 times.
|
497519 | if (!b) { |
| 3809 | ✗ | return GST_PAD_PROBE_OK; | |
| 3810 | } | ||
| 3811 | const GstClockTime shift = | ||
| 3812 | 497519 | static_cast<std::atomic<GstClockTime>*>(user)->load( | |
| 3813 | std::memory_order_relaxed); | ||
| 3814 | 497519 | b = gst_buffer_make_writable(b); | |
| 3815 |
1/2✓ Branch 0 taken 497519 times.
✗ Branch 1 not taken.
|
497519 | if (GST_BUFFER_PTS_IS_VALID(b)) { |
| 3816 | 497519 | GST_BUFFER_PTS(b) += shift; | |
| 3817 | } | ||
| 3818 |
2/2✓ Branch 0 taken 98 times.
✓ Branch 1 taken 497421 times.
|
497519 | if (GST_BUFFER_DTS_IS_VALID(b)) { |
| 3819 | 98 | GST_BUFFER_DTS(b) += shift; | |
| 3820 | } | ||
| 3821 | 497519 | GST_PAD_PROBE_INFO_DATA(info) = b; | |
| 3822 | 497519 | return GST_PAD_PROBE_OK; | |
| 3823 | }, | ||
| 3824 | (gpointer)shiftCell, nullptr); | ||
| 3825 |
1/2✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
|
184 | gst_object_unref(payPad); |
| 3826 | |||
| 3827 | // Cancel the shift again at the sink. The payloader has already | ||
| 3828 | // taken its RTP timestamp from the shifted PTS, so undoing it here | ||
| 3829 | // moves only the transmission time back, leaving the timestamp | ||
| 3830 | // ahead of the wire by the shift. | ||
| 3831 | // | ||
| 3832 | // This is what the earlier attempts each got wrong. ts-offset | ||
| 3833 | // alone moved transmission but not the timestamp; shifting the PTS | ||
| 3834 | // alone moved both, so the lead did not change at all (-23.73ms | ||
| 3835 | // before, -23.73ms after -- identical, which is what gave it | ||
| 3836 | // away). The -23.2ms is GStreamer's own latency compensation: | ||
| 3837 | // sinks render at PTS + pipeline latency, and the pipeline latency | ||
| 3838 | // here is one graph quantum. | ||
| 3839 |
3/6✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 184 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 184 times.
✗ Branch 5 not taken.
|
184 | GstElement* usink2 = gst_bin_get_by_name(GST_BIN(pipeline), "usink"); |
| 3840 |
1/2✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
|
184 | if (usink2) { |
| 3841 | 184 | g_object_set(usink2, "ts-offset", | |
| 3842 |
1/2✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
|
184 | -((gint64)m_config.sinkPacingMs * GST_MSECOND), |
| 3843 | NULL); | ||
| 3844 |
1/2✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
|
184 | gst_object_unref(usink2); |
| 3845 | } | ||
| 3846 |
1/2✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
|
184 | LogInfo(VB_MEDIAOUT, |
| 3847 | "AES67 send [%d]: sink pacing on, %dms timestamp lead\n", | ||
| 3848 | inst.id, m_config.sinkPacingMs); | ||
| 3849 | } else { | ||
| 3850 | ✗ | LogWarn(VB_MEDIAOUT, | |
| 3851 | "AES67 send [%d]: sink pacing on but payloader not found\n", | ||
| 3852 | inst.id); | ||
| 3853 | } | ||
| 3854 |
1/2✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
|
184 | if (pay) { |
| 3855 |
1/2✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
|
184 | gst_object_unref(pay); |
| 3856 | } | ||
| 3857 | } | ||
| 3858 | |||
| 3859 | // Store the bus for polling in the watchdog thread. | ||
| 3860 | // gst_bus_add_watch() requires a running GLib main loop which fppd does | ||
| 3861 | // not have — use gst_bus_pop() polling instead. | ||
| 3862 |
3/6✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 185 times.
✗ Branch 5 not taken.
|
185 | GstBus* bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline)); |
| 3863 | |||
| 3864 | // Start pipeline in PLAYING state OUTSIDE the mutex — this can block | ||
| 3865 | // waiting for PipeWire and must not hold m_pipelineMutex. | ||
| 3866 |
1/2✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
|
185 | GstStateChangeReturn ret = gst_element_set_state(pipeline, GST_STATE_PLAYING); |
| 3867 |
2/2✓ Branch 0 taken 154 times.
✓ Branch 1 taken 31 times.
|
185 | if (ret == GST_STATE_CHANGE_FAILURE) { |
| 3868 |
1/2✓ Branch 0 taken 154 times.
✗ Branch 1 not taken.
|
154 | LogErr(VB_MEDIAOUT, "AES67 send pipeline [%d] failed to start\n", inst.id); |
| 3869 |
2/4✓ Branch 0 taken 154 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 154 times.
✗ Branch 3 not taken.
|
462 | WarningHolder::AddWarning(AES67::WARNING_ID_PIPELINE, AES67::WARNING_SEND_FAILED); |
| 3870 | // Drop back to NULL before unreffing -- elements may already hold | ||
| 3871 | // READY/PAUSED resources (sockets, threads, PipeWire connections) | ||
| 3872 | // that gst_object_unref() alone will not release. | ||
| 3873 |
1/2✓ Branch 0 taken 154 times.
✗ Branch 1 not taken.
|
154 | gst_element_set_state(pipeline, GST_STATE_NULL); |
| 3874 |
1/2✓ Branch 0 taken 154 times.
✗ Branch 1 not taken.
|
154 | gst_object_unref(bus); |
| 3875 |
1/2✓ Branch 0 taken 154 times.
✗ Branch 1 not taken.
|
154 | gst_object_unref(pipeline); |
| 3876 | 154 | return false; | |
| 3877 | } | ||
| 3878 | |||
| 3879 | // The latency-derived retune happens in the watchdog, not here: at this | ||
| 3880 | // point set_state(PLAYING) has only been *requested*, the pipeline has not | ||
| 3881 | // prerolled, and the latency query returns 0 -- which silently left the | ||
| 3882 | // shift at its configured default, defeating the whole point. | ||
| 3883 | |||
| 3884 | { | ||
| 3885 |
1/2✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
|
31 | std::unique_lock<std::mutex> lock(m_pipelineMutex); |
| 3886 | // If a pipeline already exists for this instance id (e.g. two | ||
| 3887 | // ApplyConfig() runs interleaved), stop it properly instead of | ||
| 3888 | // erasing the map entry directly -- a raw erase() would destroy | ||
| 3889 | // the AES67Pipeline (dropping its GstElement* refs) without ever | ||
| 3890 | // calling gst_element_set_state(NULL), leaking a still-PLAYING | ||
| 3891 | // pipeline (refs, threads, sockets). | ||
| 3892 |
1/2✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
|
31 | auto node = m_sendPipelines.extract(inst.id); |
| 3893 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
|
31 | if (!node.empty()) { |
| 3894 | // StopPipeline() can block on a GStreamer state change to | ||
| 3895 | // PipeWire -- release the lock while it runs, matching the | ||
| 3896 | // no-block-while-locked policy used elsewhere in this file | ||
| 3897 | // (see StopAllPipelines()). | ||
| 3898 | ✗ | lock.unlock(); | |
| 3899 | ✗ | StopPipeline(node.mapped()); | |
| 3900 | ✗ | lock.lock(); | |
| 3901 | } | ||
| 3902 |
1/2✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
|
31 | auto [it, ok] = m_sendPipelines.try_emplace(inst.id); |
| 3903 | 31 | it->second.instanceId = inst.id; | |
| 3904 | 31 | it->second.channels = inst.channels; | |
| 3905 | 31 | it->second.ptime = inst.ptime; | |
| 3906 | 31 | it->second.isSend = true; | |
| 3907 | 31 | it->second.pipeline = pipeline; | |
| 3908 | 31 | it->second.bus = bus; | |
| 3909 | 31 | it->second.running = true; | |
| 3910 | // The gain in the description is the one when it was built; a change | ||
| 3911 | // since then went to the pipelines already in this map, not this one. | ||
| 3912 |
2/4✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
✗ Branch 3 not taken.
|
31 | SetPipelineMasterVolume(pipeline, getVolume()); |
| 3913 | 31 | } | |
| 3914 | |||
| 3915 |
1/2✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
|
31 | LogInfo(VB_MEDIAOUT, "AES67 send pipeline [%d] %s started → %s:%d (%dch, %dms ptime)\n", |
| 3916 | inst.id, inst.name.c_str(), inst.multicastIP.c_str(), inst.port, | ||
| 3917 | inst.channels, inst.ptime); | ||
| 3918 | 31 | return true; | |
| 3919 | 185 | } | |
| 3920 | |||
| 3921 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 3922 | // Pipeline creation — Receive | ||
| 3923 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 3924 | 11 | bool AES67Manager::CreateRecvPipeline(const AES67Instance& inst) { | |
| 3925 |
2/4✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
|
11 | std::string nodeName = SafeNodeName(inst.name) + "_recv"; |
| 3926 | |||
| 3927 | // Build pipeline: | ||
| 3928 | // udpsrc multicast-group=<ip> port=<port> auto-multicast=true | ||
| 3929 | // ! application/x-rtp,media=audio,clock-rate=48000,encoding-name=L24,channels=N,payload=96 | ||
| 3930 | // ! rtpjitterbuffer latency=<ms> | ||
| 3931 | // ! rtpL24depay | ||
| 3932 | // ! audioconvert | ||
| 3933 | // ! pipewiresink target-object=<node> | ||
| 3934 | // stream-properties="props,media.class=Audio/Source,node.name=<node>" | ||
| 3935 | |||
| 3936 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | std::ostringstream oss; |
| 3937 | 11 | oss << "udpsrc multicast-group=" << inst.multicastIP | |
| 3938 |
4/8✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 11 times.
✗ Branch 7 not taken.
|
11 | << " port=" << inst.port |
| 3939 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | << " auto-multicast=true"; |
| 3940 | |||
| 3941 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | if (!inst.interface.empty()) { |
| 3942 | ✗ | oss << " multicast-iface=" << inst.interface; | |
| 3943 | } | ||
| 3944 | |||
| 3945 |
2/4✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
|
11 | oss << " ! application/x-rtp,media=audio,clock-rate=" << AES67::AUDIO_RATE |
| 3946 |
2/4✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
|
11 | << ",encoding-name=L24,channels=" << inst.channels |
| 3947 |
2/4✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
|
11 | << ",payload=" << AES67::RTP_PAYLOAD_TYPE << " " |
| 3948 |
3/6✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
|
11 | << "! rtpjitterbuffer latency=" << inst.latency << " " |
| 3949 | << "! rtpL24depay " | ||
| 3950 | << "! audioconvert " | ||
| 3951 |
4/8✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 11 times.
✗ Branch 7 not taken.
|
11 | << "! pipewiresink name=pwsink"; |
| 3952 | |||
| 3953 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | std::string pipelineStr = oss.str(); |
| 3954 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | LogInfo(VB_MEDIAOUT, "AES67 recv pipeline [%d] %s: %s\n", |
| 3955 | inst.id, inst.name.c_str(), pipelineStr.c_str()); | ||
| 3956 | |||
| 3957 | 11 | GError* error = nullptr; | |
| 3958 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | GstElement* pipeline = gst_parse_launch(pipelineStr.c_str(), &error); |
| 3959 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | if (error) { |
| 3960 | ✗ | LogErr(VB_MEDIAOUT, "AES67 recv pipeline error [%d]: %s\n", | |
| 3961 | inst.id, error->message); | ||
| 3962 | ✗ | g_error_free(error); | |
| 3963 | // gst_parse_launch() can return a non-null (partial) pipeline even | ||
| 3964 | // when it also reports an error -- release it so we don't leak. | ||
| 3965 | ✗ | if (pipeline) { | |
| 3966 | ✗ | gst_object_unref(pipeline); | |
| 3967 | } | ||
| 3968 | ✗ | return false; | |
| 3969 | } | ||
| 3970 | |||
| 3971 | // Set stream-properties post-launch — gst_parse_launch cannot | ||
| 3972 | // deserialize GstStructure values containing '/' (e.g. Audio/Source) | ||
| 3973 | // which crashes gst_value_deserialize. | ||
| 3974 |
2/4✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
|
11 | std::string recvNodeDesc = SafeNodeName(inst.name) + "_recv"; |
| 3975 |
3/6✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
|
11 | GstElement* pwsink = gst_bin_get_by_name(GST_BIN(pipeline), "pwsink"); |
| 3976 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | if (pwsink) { |
| 3977 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | GstStructure* props = gst_structure_new("props", |
| 3978 | "media.class", G_TYPE_STRING, "Audio/Source", | ||
| 3979 | "node.name", G_TYPE_STRING, nodeName.c_str(), | ||
| 3980 | "node.description", G_TYPE_STRING, recvNodeDesc.c_str(), | ||
| 3981 | NULL); | ||
| 3982 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | g_object_set(pwsink, "stream-properties", props, NULL); |
| 3983 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | gst_structure_free(props); |
| 3984 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | gst_object_unref(pwsink); |
| 3985 | } | ||
| 3986 | |||
| 3987 | // Store the bus for polling in the watchdog thread (no GLib main loop). | ||
| 3988 |
3/6✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
|
11 | GstBus* bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline)); |
| 3989 | |||
| 3990 | // Note: the receive path runs on GStreamer's default clock. Only the send | ||
| 3991 | // path is driven by PTP time (see CreateSendPipeline). | ||
| 3992 | |||
| 3993 | // Start pipeline OUTSIDE the mutex — GStreamer state changes can block | ||
| 3994 | // waiting for PipeWire. | ||
| 3995 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | GstStateChangeReturn ret = gst_element_set_state(pipeline, GST_STATE_PLAYING); |
| 3996 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 5 times.
|
11 | if (ret == GST_STATE_CHANGE_FAILURE) { |
| 3997 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | LogErr(VB_MEDIAOUT, "AES67 recv pipeline [%d] failed to start\n", inst.id); |
| 3998 |
2/4✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
18 | WarningHolder::AddWarning(AES67::WARNING_ID_PIPELINE, AES67::WARNING_RECV_FAILED); |
| 3999 | // Drop back to NULL before unreffing -- elements may already hold | ||
| 4000 | // READY/PAUSED resources (sockets, threads, PipeWire connections) | ||
| 4001 | // that gst_object_unref() alone will not release. | ||
| 4002 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | gst_element_set_state(pipeline, GST_STATE_NULL); |
| 4003 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | gst_object_unref(bus); |
| 4004 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | gst_object_unref(pipeline); |
| 4005 | 6 | return false; | |
| 4006 | } | ||
| 4007 | |||
| 4008 | { | ||
| 4009 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | std::unique_lock<std::mutex> lock(m_pipelineMutex); |
| 4010 | // See CreateSendPipeline() for why we stop any existing pipeline | ||
| 4011 | // for this id instead of erasing the map entry directly. | ||
| 4012 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | auto node = m_recvPipelines.extract(inst.id); |
| 4013 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (!node.empty()) { |
| 4014 | ✗ | lock.unlock(); | |
| 4015 | ✗ | StopPipeline(node.mapped()); | |
| 4016 | ✗ | lock.lock(); | |
| 4017 | } | ||
| 4018 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | auto [it, ok] = m_recvPipelines.try_emplace(inst.id); |
| 4019 | 5 | it->second.instanceId = inst.id; | |
| 4020 | 5 | it->second.channels = inst.channels; | |
| 4021 | 5 | it->second.ptime = inst.ptime; | |
| 4022 | 5 | it->second.isSend = false; | |
| 4023 | 5 | it->second.pipeline = pipeline; | |
| 4024 | 5 | it->second.bus = bus; | |
| 4025 | 5 | it->second.running = true; | |
| 4026 | 5 | } | |
| 4027 | |||
| 4028 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | LogInfo(VB_MEDIAOUT, "AES67 recv pipeline [%d] %s started ← %s:%d (%dch, %dms latency)\n", |
| 4029 | inst.id, inst.name.c_str(), inst.multicastIP.c_str(), inst.port, | ||
| 4030 | inst.channels, inst.latency); | ||
| 4031 | 5 | return true; | |
| 4032 | 11 | } | |
| 4033 | |||
| 4034 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 4035 | // Pipeline management | ||
| 4036 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 4037 | 36 | void AES67Manager::StopPipeline(AES67Pipeline& p) { | |
| 4038 |
1/2✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
|
36 | if (p.pipeline) { |
| 4039 | 36 | gst_element_set_state(p.pipeline, GST_STATE_NULL); | |
| 4040 | |||
| 4041 | // Remove the buffer-drop probe installed by FlushSendPipelines() | ||
| 4042 | // (if any) and release our ref on the pad it was installed on. | ||
| 4043 | // Without this, the probe stays registered on the pad with | ||
| 4044 | // &p.dropCounter as userdata -- a dangling pointer once this | ||
| 4045 | // AES67Pipeline is erased from its owning map -- and the pad ref | ||
| 4046 | // taken in FlushSendPipelines() leaks. | ||
| 4047 |
3/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 34 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
36 | if (p.probeId != 0 && p.probePad) { |
| 4048 | 2 | gst_pad_remove_probe(p.probePad, p.probeId); | |
| 4049 | 2 | p.probeId = 0; | |
| 4050 | } | ||
| 4051 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 34 times.
|
36 | if (p.probePad) { |
| 4052 | 2 | gst_object_unref(p.probePad); | |
| 4053 | 2 | p.probePad = nullptr; | |
| 4054 | } | ||
| 4055 | |||
| 4056 | // Release the bus ref we stored at pipeline creation | ||
| 4057 |
1/2✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
|
36 | if (p.bus) { |
| 4058 | 36 | gst_object_unref(p.bus); | |
| 4059 | 36 | p.bus = nullptr; | |
| 4060 | } | ||
| 4061 | 36 | gst_object_unref(p.pipeline); | |
| 4062 | 36 | p.pipeline = nullptr; | |
| 4063 | 36 | p.running = false; | |
| 4064 | } | ||
| 4065 | 36 | } | |
| 4066 | |||
| 4067 | 239 | void AES67Manager::StopAllPipelines() { | |
| 4068 | // Extract pipelines from the maps under the lock, then release the lock | ||
| 4069 | // before calling gst_element_set_state(NULL) — state changes can block | ||
| 4070 | // on PipeWire and must not hold m_pipelineMutex. | ||
| 4071 | 239 | std::map<int, AES67Pipeline> sendCopy, recvCopy; | |
| 4072 | { | ||
| 4073 |
1/2✓ Branch 0 taken 239 times.
✗ Branch 1 not taken.
|
239 | std::lock_guard<std::mutex> lock(m_pipelineMutex); |
| 4074 | 239 | sendCopy.swap(m_sendPipelines); | |
| 4075 | 239 | recvCopy.swap(m_recvPipelines); | |
| 4076 | // Nothing is configured to be running any more, so no sender is | ||
| 4077 | // waiting for a source either. ApplyConfig() refills this after its | ||
| 4078 | // create pass; every other caller is a teardown. | ||
| 4079 | 239 | m_deferredSenders.clear(); | |
| 4080 | 239 | } | |
| 4081 | |||
| 4082 |
2/2✓ Branch 0 taken 31 times.
✓ Branch 1 taken 239 times.
|
270 | for (auto& [id, p] : sendCopy) { |
| 4083 |
1/2✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
|
31 | LogDebug(VB_MEDIAOUT, "AES67Manager: Stopping send pipeline [%d]\n", id); |
| 4084 |
1/2✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
|
31 | StopPipeline(p); |
| 4085 | } | ||
| 4086 | |||
| 4087 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 239 times.
|
244 | for (auto& [id, p] : recvCopy) { |
| 4088 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | LogDebug(VB_MEDIAOUT, "AES67Manager: Stopping recv pipeline [%d]\n", id); |
| 4089 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | StopPipeline(p); |
| 4090 | } | ||
| 4091 | 239 | } | |
| 4092 | |||
| 4093 | // Pad probe callback: drops buffers while dropCounter > 0, passes through otherwise. | ||
| 4094 | // Installed once on pipewiresrc's src pad and stays active for the pipeline's lifetime. | ||
| 4095 | 618 | static GstPadProbeReturn DropBufferProbe(GstPad* pad, GstPadProbeInfo* info, gpointer userData) { | |
| 4096 | 618 | std::atomic<gint64>* remaining = static_cast<std::atomic<gint64>*>(userData); | |
| 4097 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 618 times.
|
618 | if (!(info->type & GST_PAD_PROBE_TYPE_BUFFER)) |
| 4098 | ✗ | return GST_PAD_PROBE_OK; | |
| 4099 |
1/2✓ Branch 0 taken 618 times.
✗ Branch 1 not taken.
|
1236 | if (remaining->load() <= 0) |
| 4100 | 618 | return GST_PAD_PROBE_OK; | |
| 4101 | |||
| 4102 | ✗ | GstBuffer* b = GST_PAD_PROBE_INFO_BUFFER(info); | |
| 4103 | ✗ | GstClockTime dur = b ? GST_BUFFER_DURATION(b) : GST_CLOCK_TIME_NONE; | |
| 4104 | ✗ | if (!GST_CLOCK_TIME_IS_VALID(dur)) { | |
| 4105 | // Nothing to subtract, so stop rather than drop indefinitely. Under- | ||
| 4106 | // dropping leaves a little stale audio; over-dropping is the bug this | ||
| 4107 | // whole change exists to fix. | ||
| 4108 | ✗ | remaining->store(0); | |
| 4109 | ✗ | return GST_PAD_PROBE_DROP; | |
| 4110 | } | ||
| 4111 | |||
| 4112 | // Only drop a buffer that FITS in what is left, and stop otherwise. | ||
| 4113 | // Buffers are whole graph quanta, so testing "is there budget left" and | ||
| 4114 | // subtracting afterwards overshoots by up to a quantum every time: at the | ||
| 4115 | // 21.333ms quantum of a Pi 5 at 48kHz, a 50ms budget took three buffers | ||
| 4116 | // and cut 64.00ms. A tester measured exactly that, and it put the gap | ||
| 4117 | // back over the sink queue's depth, which is the one thing this must not | ||
| 4118 | // do. Stopping short instead is bounded by construction -- 42.67ms at | ||
| 4119 | // that quantum, 46.44ms at 44.1kHz -- and never exceeds what was asked. | ||
| 4120 | ✗ | if ((GstClockTime)remaining->load() < dur) { | |
| 4121 | ✗ | remaining->store(0); | |
| 4122 | ✗ | return GST_PAD_PROBE_OK; | |
| 4123 | } | ||
| 4124 | ✗ | remaining->fetch_sub((gint64)dur); | |
| 4125 | ✗ | return GST_PAD_PROBE_DROP; | |
| 4126 | } | ||
| 4127 | |||
| 4128 | 3886 | void AES67Manager::SetMasterVolume(int volume) { | |
| 4129 |
1/2✓ Branch 0 taken 3886 times.
✗ Branch 1 not taken.
|
3886 | std::lock_guard<std::mutex> lock(m_pipelineMutex); |
| 4130 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3886 times.
|
3887 | for (auto& kv : m_sendPipelines) { |
| 4131 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (kv.second.pipeline) { |
| 4132 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | SetPipelineMasterVolume(kv.second.pipeline, volume); |
| 4133 | } | ||
| 4134 | } | ||
| 4135 | 3886 | } | |
| 4136 | |||
| 4137 | 4 | void AES67Manager::FlushSendPipelines() { | |
| 4138 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | std::lock_guard<std::mutex> lock(m_pipelineMutex); |
| 4139 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 | for (auto& kv : m_sendPipelines) { |
| 4140 | 4 | AES67Pipeline& p = kv.second; | |
| 4141 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
4 | if (!p.pipeline || !p.running) |
| 4142 | 2 | continue; | |
| 4143 | |||
| 4144 | // Drop SOURCE_FLUSH_MS of audio from pipewiresrc's src pad. This | ||
| 4145 | // discards whatever was queued in GStreamer elements between the old | ||
| 4146 | // track stopping and the new one starting, without disrupting the | ||
| 4147 | // pipeline's event flow (no flush-start/stop, no state change). | ||
| 4148 | // | ||
| 4149 | // Three call sites in GStreamerOut fire at a single track change, so | ||
| 4150 | // restart the window rather than adding to it -- the old counter was | ||
| 4151 | // reset to 10 buffers by each one, and only stayed bounded because | ||
| 4152 | // the calls happened to land within one window. Log just the first, | ||
| 4153 | // since three identical lines per transition made this look like | ||
| 4154 | // three separate events in the logs testers sent back. | ||
| 4155 | // Three call sites in GStreamerOut fire at a single track change. | ||
| 4156 | // Re-arming the budget on each one lets a later call land mid-sequence | ||
| 4157 | // and extend the drop: a tester saw transitions alternate between | ||
| 4158 | // three and four quanta (64.00ms and 85.33ms) for one 50ms request. | ||
| 4159 | // Ignore a call while a flush is still running, so one track change | ||
| 4160 | // discards one budget's worth however many code paths announce it. | ||
| 4161 | 4 | const gint64 target = (gint64)AES67::SOURCE_FLUSH_MS * GST_MSECOND; | |
| 4162 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
8 | if (p.dropRemainingNs.load() > 0) { |
| 4163 | ✗ | continue; // already flushing; probe is installed | |
| 4164 | } | ||
| 4165 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | LogInfo(VB_MEDIAOUT, |
| 4166 | "AES67 send pipeline [%d]: discarding up to %dms of stale audio\n", | ||
| 4167 | p.instanceId, AES67::SOURCE_FLUSH_MS); | ||
| 4168 | 4 | p.dropRemainingNs.store(target); | |
| 4169 | |||
| 4170 | // Install the probe once; subsequent calls just reset the counter. | ||
| 4171 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | if (p.probeId != 0) |
| 4172 | 2 | continue; | |
| 4173 | |||
| 4174 | // Find pipewiresrc's src pad and install the permanent probe | ||
| 4175 |
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 | GstIterator* it = gst_bin_iterate_sources(GST_BIN(p.pipeline)); |
| 4176 | 2 | GValue val = G_VALUE_INIT; | |
| 4177 | 2 | GstElement* srcElem = nullptr; | |
| 4178 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | if (gst_iterator_next(it, &val) == GST_ITERATOR_OK) { |
| 4179 |
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 | srcElem = GST_ELEMENT(g_value_get_object(&val)); |
| 4180 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | gst_object_ref(srcElem); |
| 4181 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | g_value_unset(&val); |
| 4182 | } | ||
| 4183 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | gst_iterator_free(it); |
| 4184 | |||
| 4185 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (srcElem) { |
| 4186 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | GstPad* srcpad = gst_element_get_static_pad(srcElem, "src"); |
| 4187 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (srcpad) { |
| 4188 | 2 | p.probePad = srcpad; // takes ownership of ref | |
| 4189 | 2 | p.probeId = gst_pad_add_probe( | |
| 4190 | srcpad, | ||
| 4191 | GST_PAD_PROBE_TYPE_BUFFER, | ||
| 4192 | DropBufferProbe, | ||
| 4193 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | &p.dropRemainingNs, |
| 4194 | nullptr); | ||
| 4195 | } | ||
| 4196 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | gst_object_unref(srcElem); |
| 4197 | } | ||
| 4198 | } | ||
| 4199 | 4 | } | |
| 4200 | |||
| 4201 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 4202 | // GStreamer bus callback | ||
| 4203 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 4204 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 4205 | // Pipeline watchdog — polls bus messages and recovers crashed pipelines. | ||
| 4206 | // Called from WatchdogLoop() (every SAP_ANNOUNCE_INTERVAL_S seconds). | ||
| 4207 | // fppd does not run a GLib main loop, so gst_bus_add_watch() callbacks would | ||
| 4208 | // never fire. We poll the bus manually and promote any ERROR/WARNING messages. | ||
| 4209 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 4210 | // Restart ptp4l/phc2sys if they have died. Nothing else supervised them: a | ||
| 4211 | // link flap, an OOM kill or a stray `killall ptp4l` left FPP silently running | ||
| 4212 | // with no clock discipline at all, reporting "not running" forever. | ||
| 4213 | // | ||
| 4214 | // Called from WatchdogLoop() alongside the pipeline watchdog, so it shares | ||
| 4215 | // that thread's cadence. Note this means PTP is only supervised while the SAP | ||
| 4216 | // announcer runs (i.e. there is at least one SAP-enabled send instance). | ||
| 4217 | |||
| 4218 | // Trims the send stream's sample rate so the media timeline advances at exactly | ||
| 4219 | // PTP rate. See AES67Config::adaptiveResample for why this is not optional. | ||
| 4220 | // | ||
| 4221 | // The loop is deliberately slow and gentle. What it is correcting is a crystal | ||
| 4222 | // offset -- constant, order 100ppm -- not a fast disturbance, so it samples | ||
| 4223 | // every couple of seconds and moves in small steps. Two terms: | ||
| 4224 | // | ||
| 4225 | // rate the media clock's measured advance per unit of PTP time. Feeding | ||
| 4226 | // that straight back (speed *= measured ratio) nulls the offset in | ||
| 4227 | // rate, which is the bulk of the correction. | ||
| 4228 | // offset a slow pull that burns off however much lag accumulated before the | ||
| 4229 | // rate term settled, so the stream ends up at the right rate AND back | ||
| 4230 | // at the latency it started from. | ||
| 4231 | 1 | void AES67Manager::DriftControlLoop() { | |
| 4232 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogInfo(VB_MEDIAOUT, "AES67 drift control thread started\n"); |
| 4233 | |||
| 4234 | 1 | constexpr int SAMPLE_INTERVAL_S = 2; | |
| 4235 | // The payloader's last-emitted RTP timestamp only advances when a burst of | ||
| 4236 | // packets goes out -- one graph quantum at a time -- so sampling it | ||
| 4237 | // asynchronously quantises the reading by ~21ms. Over a 30s endpoint | ||
| 4238 | // measurement that is +/-750ppm of noise on a signal of ~100ppm, and the | ||
| 4239 | // loop simply hunts. Fit a slope across the whole window instead, and make | ||
| 4240 | // the window long: noise falls with both the span and the sample count. | ||
| 4241 | 1 | constexpr int WINDOW_S = 120; // measurement baseline | |
| 4242 | // Actuate far more slowly than we sample. The measurement is a 120s | ||
| 4243 | // window, so a correction takes ~60s to show up in it; correcting every 2s | ||
| 4244 | // means ~30 further corrections are applied before the first is visible, | ||
| 4245 | // and the loop oscillates (measured: trim swinging +140 to -377ppm, drift | ||
| 4246 | // swinging +236 to -99ppm). Classic dead-time instability -- the cure is | ||
| 4247 | // to make the control interval comparable to the lag, not to lower the | ||
| 4248 | // gain until the ringing is slow. | ||
| 4249 | 1 | constexpr int ACTUATE_EVERY = 15; // iterations, i.e. every 30s | |
| 4250 | 1 | constexpr double KP_RATE = 0.3; // fraction of the rate error to take per step | |
| 4251 | 1 | constexpr double KP_OFFSET = 0.00002; // per second of accumulated lag | |
| 4252 | 1 | constexpr double MAX_TRIM = 0.0005; // +/-500ppm, well beyond any crystal | |
| 4253 | 1 | constexpr double MAX_STEP = 0.00002; // +/-20ppm per iteration | |
| 4254 | |||
| 4255 | struct Sample { | ||
| 4256 | GstClockTime clock; | ||
| 4257 | double samples; // cumulative RTP samples emitted | ||
| 4258 | }; | ||
| 4259 | struct PipelineState { | ||
| 4260 | std::deque<Sample> window; | ||
| 4261 | double cumulative = 0.0; | ||
| 4262 | guint lastTs = 0; | ||
| 4263 | GstClockTime lastClock = 0; | ||
| 4264 | bool primed = false; | ||
| 4265 | double speed = 1.0; | ||
| 4266 | int sinceActuate = 0; | ||
| 4267 | }; | ||
| 4268 | 1 | std::map<int, PipelineState> state; | |
| 4269 | |||
| 4270 |
1/2✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
|
47 | while (m_driftRunning.load()) { |
| 4271 |
6/6✓ Branch 0 taken 934 times.
✓ Branch 1 taken 46 times.
✓ Branch 2 taken 933 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 933 times.
✓ Branch 5 taken 47 times.
|
980 | for (int i = 0; i < SAMPLE_INTERVAL_S * 10 && m_driftRunning.load(); i++) { |
| 4272 |
1/2✓ Branch 0 taken 933 times.
✗ Branch 1 not taken.
|
933 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 4273 | } | ||
| 4274 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 46 times.
|
47 | if (!m_driftRunning.load()) { |
| 4275 | 1 | break; | |
| 4276 | } | ||
| 4277 | |||
| 4278 | 46 | std::unique_lock<std::mutex> lock(m_pipelineMutex, std::try_to_lock); | |
| 4279 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
|
46 | if (!lock.owns_lock()) { |
| 4280 | ✗ | continue; | |
| 4281 | } | ||
| 4282 | |||
| 4283 |
2/2✓ Branch 0 taken 46 times.
✓ Branch 1 taken 46 times.
|
92 | for (auto& [id, p] : m_sendPipelines) { |
| 4284 |
2/4✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 46 times.
|
46 | if (!p.running || !p.pipeline) { |
| 4285 | 45 | continue; | |
| 4286 | } | ||
| 4287 |
3/6✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 46 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 46 times.
✗ Branch 5 not taken.
|
46 | GstElement* drift = gst_bin_get_by_name(GST_BIN(p.pipeline), "drift"); |
| 4288 |
3/6✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 46 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 46 times.
✗ Branch 5 not taken.
|
46 | GstElement* pay = gst_bin_get_by_name(GST_BIN(p.pipeline), "pay"); |
| 4289 |
2/4✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 46 times.
|
46 | if (!drift || !pay) { |
| 4290 | ✗ | if (drift) gst_object_unref(drift); | |
| 4291 | ✗ | if (pay) gst_object_unref(pay); | |
| 4292 | ✗ | continue; | |
| 4293 | } | ||
| 4294 |
1/2✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
|
46 | GstClock* clock = gst_element_get_clock(p.pipeline); |
| 4295 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
|
46 | if (!clock) { |
| 4296 | ✗ | gst_object_unref(drift); | |
| 4297 | ✗ | gst_object_unref(pay); | |
| 4298 | ✗ | continue; | |
| 4299 | } | ||
| 4300 | |||
| 4301 | // Measure against the RTP timestamps we actually emitted. These | ||
| 4302 | // step by exactly one packet, so the only error is a single sample | ||
| 4303 | // (21us) over the whole window -- unlike a position query, which | ||
| 4304 | // reports the last buffer the sink handled and therefore jitters by | ||
| 4305 | // a full graph quantum (+/-10ms measured). That jitter is ~45x the | ||
| 4306 | // drift being corrected, and an earlier version of this loop spent | ||
| 4307 | // its life chasing it into the slew limit. | ||
| 4308 | 46 | guint ts = 0; | |
| 4309 |
1/2✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
|
46 | g_object_get(pay, "timestamp", &ts, NULL); |
| 4310 |
1/2✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
|
46 | GstClockTime now = gst_clock_get_time(clock); |
| 4311 |
1/2✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
|
46 | gst_object_unref(clock); |
| 4312 |
1/2✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
|
46 | gst_object_unref(pay); |
| 4313 | |||
| 4314 |
1/2✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
|
46 | PipelineState& st = state[id]; |
| 4315 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 45 times.
|
46 | if (!st.primed) { |
| 4316 | 1 | st.lastTs = ts; | |
| 4317 | 1 | st.lastClock = now; | |
| 4318 | 1 | st.primed = true; | |
| 4319 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | st.window.push_back({now, 0.0}); |
| 4320 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | gst_object_unref(drift); |
| 4321 | 1 | continue; | |
| 4322 | } | ||
| 4323 | |||
| 4324 | 45 | double emitted = (double)((guint32)(ts - st.lastTs)); // wraps correctly | |
| 4325 | 45 | st.lastTs = ts; | |
| 4326 | |||
| 4327 | // Reject discontinuities rather than measuring them as drift. | ||
| 4328 | // Between tracks, and whenever FlushSendPipelines() drops buffers, | ||
| 4329 | // the stream simply stops for a while; the media timeline then | ||
| 4330 | // legitimately loses time against the wall clock, which is | ||
| 4331 | // indistinguishable from a very slow clock if taken at face value. | ||
| 4332 | // Measured with five track restarts in a window it reported | ||
| 4333 | // -7500ppm and nearly a second of "lag" -- all of it gaps. | ||
| 4334 | 45 | double expected = (double)(now - st.lastClock) / 1e9 * AES67::AUDIO_RATE; | |
| 4335 | 45 | st.lastClock = now; | |
| 4336 |
3/6✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 45 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 45 times.
|
45 | if (expected > 0 && (emitted < expected * 0.9 || emitted > expected * 1.1)) { |
| 4337 | ✗ | LogDebug(VB_MEDIAOUT, | |
| 4338 | "AES67 drift [%d]: stream discontinuity (%.0f of %.0f samples), " | ||
| 4339 | "restarting measurement\n", id, emitted, expected); | ||
| 4340 | ✗ | st.window.clear(); | |
| 4341 | ✗ | st.cumulative = 0.0; | |
| 4342 | ✗ | st.window.push_back({now, 0.0}); | |
| 4343 | ✗ | gst_object_unref(drift); | |
| 4344 | ✗ | continue; | |
| 4345 | } | ||
| 4346 | |||
| 4347 | 45 | st.cumulative += emitted; | |
| 4348 |
1/2✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
|
45 | st.window.push_back({now, st.cumulative}); |
| 4349 |
3/4✓ Branch 0 taken 44 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 45 times.
|
89 | while (st.window.size() > 2 && |
| 4350 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
|
44 | (now - st.window.front().clock) > (GstClockTime)WINDOW_S * GST_SECOND) { |
| 4351 | ✗ | st.window.pop_front(); | |
| 4352 | } | ||
| 4353 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 44 times.
|
45 | if (st.window.size() < 3) { |
| 4354 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | gst_object_unref(drift); |
| 4355 | 1 | continue; | |
| 4356 | } | ||
| 4357 | |||
| 4358 | 44 | const Sample& a = st.window.front(); | |
| 4359 | 44 | const Sample& b = st.window.back(); | |
| 4360 | 44 | double dClockS = (double)(b.clock - a.clock) / 1e9; | |
| 4361 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 16 times.
|
44 | if (dClockS < (double)WINDOW_S * 0.5) { |
| 4362 | // Not enough baseline yet to measure anything at this precision. | ||
| 4363 |
1/2✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
|
28 | gst_object_unref(drift); |
| 4364 | 28 | continue; | |
| 4365 | } | ||
| 4366 | |||
| 4367 | // Least-squares slope of media seconds against PTP seconds across | ||
| 4368 | // the window. Averaging down the per-sample quantisation is the | ||
| 4369 | // whole point; endpoints alone are far too noisy. | ||
| 4370 | 16 | double n = 0, sx = 0, sy = 0, sxx = 0, sxy = 0; | |
| 4371 |
2/2✓ Branch 0 taken 616 times.
✓ Branch 1 taken 16 times.
|
632 | for (const Sample& w : st.window) { |
| 4372 | 616 | double x = (double)(w.clock - a.clock) / 1e9; | |
| 4373 | 616 | double y = (w.samples - a.samples) / (double)AES67::AUDIO_RATE; | |
| 4374 | 616 | n += 1; sx += x; sy += y; sxx += x * x; sxy += x * y; | |
| 4375 | } | ||
| 4376 | 16 | double denom = n * sxx - sx * sx; | |
| 4377 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | if (denom <= 0) { |
| 4378 | ✗ | gst_object_unref(drift); | |
| 4379 | ✗ | continue; | |
| 4380 | } | ||
| 4381 | 16 | double ratio = (n * sxy - sx * sy) / denom; // 1.0 = locked to PTP | |
| 4382 | 16 | double lagS = dClockS - (b.samples - a.samples) / (double)AES67::AUDIO_RATE; | |
| 4383 | |||
| 4384 | // Sign: the "speed" element plays at `speed` x rate, so it emits | ||
| 4385 | // FEWER samples as speed rises -- media time advances as 1/speed. | ||
| 4386 | // Media running slow (ratio < 1) therefore needs speed to come | ||
| 4387 | // DOWN. Getting this backwards drives the loop straight into the | ||
| 4388 | // clamp and makes the drift worse, which is exactly what the first | ||
| 4389 | // rewrite of this loop did (-111ppm became -770ppm at +500ppm trim). | ||
| 4390 | // | ||
| 4391 | // Rate term takes a fraction of the error so the loop settles | ||
| 4392 | // rather than ringing; offset term is tiny and only bleeds off | ||
| 4393 | // accumulated lag. | ||
| 4394 | // Keep accumulating the window every iteration, but only move the | ||
| 4395 | // trim once the previous move has had time to show up. | ||
| 4396 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 1 times.
|
16 | if (++st.sinceActuate < ACTUATE_EVERY) { |
| 4397 |
1/2✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
|
15 | gst_object_unref(drift); |
| 4398 | 15 | continue; | |
| 4399 | } | ||
| 4400 | 1 | st.sinceActuate = 0; | |
| 4401 | |||
| 4402 | 1 | double next = st.speed * (1.0 + KP_RATE * (ratio - 1.0)) * (1.0 - KP_OFFSET * lagS); | |
| 4403 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (next > st.speed + MAX_STEP) next = st.speed + MAX_STEP; |
| 4404 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (next < st.speed - MAX_STEP) next = st.speed - MAX_STEP; |
| 4405 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (next < 1.0 - MAX_TRIM) next = 1.0 - MAX_TRIM; |
| 4406 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (next > 1.0 + MAX_TRIM) next = 1.0 + MAX_TRIM; |
| 4407 | |||
| 4408 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (fabs(next - st.speed) > 1e-9) { |
| 4409 | 1 | st.speed = next; | |
| 4410 | // "pitch" (soundtouch) exposes the rate as "rate". The | ||
| 4411 | // previous element, "speed", accepted its property and read it | ||
| 4412 | // back while doing nothing to a live source -- so verify any | ||
| 4413 | // replacement by watching the measured drift respond, never by | ||
| 4414 | // reading the property back. | ||
| 4415 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | g_object_set(drift, "rate", (gfloat)st.speed, NULL); |
| 4416 | } | ||
| 4417 | |||
| 4418 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogDebug(VB_MEDIAOUT, |
| 4419 | "AES67 drift [%d]: over %.0fs media/PTP %.7f (%+.1f ppm), lag %+.2f ms, trim %+.1f ppm\n", | ||
| 4420 | id, dClockS, ratio, (ratio - 1.0) * 1e6, lagS * 1000.0, | ||
| 4421 | (st.speed - 1.0) * 1e6); | ||
| 4422 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | gst_object_unref(drift); |
| 4423 | } | ||
| 4424 |
1/2✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
|
46 | } |
| 4425 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogInfo(VB_MEDIAOUT, "AES67 drift control thread stopped\n"); |
| 4426 | 1 | } | |
| 4427 | |||
| 4428 | // PTP time minus CLOCK_MONOTONIC. This is the quantity a send pipeline's RTP | ||
| 4429 | // anchor depends on: the anchor is a PTP instant frozen at build time, and the | ||
| 4430 | // pipeline that has to hit it runs on the graph clock, which is a system clock | ||
| 4431 | // like this one. When the two move apart by more than drift, the anchor is | ||
| 4432 | // wrong by exactly that much. | ||
| 4433 | 167 | bool AES67Manager::PtpMonotonicDelta(int64_t& deltaNs) { | |
| 4434 |
1/2✓ Branch 0 taken 167 times.
✗ Branch 1 not taken.
|
167 | std::lock_guard<std::mutex> lock(m_ptpClockMutex); |
| 4435 |
2/2✓ Branch 0 taken 83 times.
✓ Branch 1 taken 84 times.
|
167 | if (!m_ptpClock) { |
| 4436 | 83 | return false; | |
| 4437 | } | ||
| 4438 |
1/2✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
|
84 | const GstClockTime ptpNow = gst_clock_get_time(m_ptpClock); |
| 4439 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
|
84 | if (!GST_CLOCK_TIME_IS_VALID(ptpNow)) { |
| 4440 | ✗ | return false; | |
| 4441 | } | ||
| 4442 | struct timespec ts; | ||
| 4443 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
|
84 | if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { |
| 4444 | ✗ | return false; | |
| 4445 | } | ||
| 4446 | 84 | deltaNs = (int64_t)ptpNow - (int64_t)GST_TIMESPEC_TO_TIME(ts); | |
| 4447 | 84 | return true; | |
| 4448 | 167 | } | |
| 4449 | |||
| 4450 | 160 | void AES67Manager::ResetPtpStepReference() { | |
| 4451 | 160 | int64_t delta = 0; | |
| 4452 |
1/2✓ Branch 0 taken 160 times.
✗ Branch 1 not taken.
|
160 | const bool have = PtpMonotonicDelta(delta); |
| 4453 |
1/2✓ Branch 0 taken 160 times.
✗ Branch 1 not taken.
|
160 | std::lock_guard<std::mutex> lock(m_ptpStepMutex); |
| 4454 | 160 | m_ptpMonotonicDeltaNs = delta; | |
| 4455 | 160 | m_ptpMonotonicDeltaValid = have; | |
| 4456 | |||
| 4457 | // Same sample, different job: this one is the fixed point | ||
| 4458 | // ReviewPtpNoLockWarning() measures total drift against, so it is taken | ||
| 4459 | // once here -- where the anchors it describes have just been built -- and | ||
| 4460 | // never re-taken while the warning stands. | ||
| 4461 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 153 times.
|
160 | if (m_ptpNoLockWarned.load()) { |
| 4462 | 7 | m_ptpNoLockBaselineNs = delta; | |
| 4463 | 7 | m_ptpNoLockBaselineValid = have; | |
| 4464 | } | ||
| 4465 | 160 | } | |
| 4466 | |||
| 4467 | // True when the PTP clock has moved out from under the anchors since the last | ||
| 4468 | // sample. Re-samples either way, so what it measures is always one watchdog | ||
| 4469 | // interval of movement rather than everything since the pipelines were built. | ||
| 4470 | 6 | bool AES67Manager::PtpClockStepped() { | |
| 4471 | 6 | int64_t delta = 0; | |
| 4472 |
2/4✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
|
6 | if (!PtpMonotonicDelta(delta)) { |
| 4473 | // No PTP media clock, so nothing is anchored to PTP and there is | ||
| 4474 | // nothing for a step to invalidate. | ||
| 4475 | ✗ | std::lock_guard<std::mutex> lock(m_ptpStepMutex); | |
| 4476 | ✗ | m_ptpMonotonicDeltaValid = false; | |
| 4477 | ✗ | return false; | |
| 4478 | ✗ | } | |
| 4479 | |||
| 4480 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | std::lock_guard<std::mutex> lock(m_ptpStepMutex); |
| 4481 | 6 | const bool had = m_ptpMonotonicDeltaValid; | |
| 4482 | 6 | const int64_t previous = m_ptpMonotonicDeltaNs; | |
| 4483 | 6 | m_ptpMonotonicDeltaNs = delta; | |
| 4484 | 6 | m_ptpMonotonicDeltaValid = true; | |
| 4485 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (!had) { |
| 4486 | ✗ | return false; | |
| 4487 | } | ||
| 4488 | 6 | const int64_t moved = delta - previous; | |
| 4489 | 6 | const int64_t absMoved = moved < 0 ? -moved : moved; | |
| 4490 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (absMoved < AES67::PTP_STEP_DETECT_NS) { |
| 4491 | 6 | return false; | |
| 4492 | } | ||
| 4493 | ✗ | LogWarn(VB_MEDIAOUT, | |
| 4494 | "AES67 watchdog: PTP clock stepped %+.3f s — the RTP timelines are " | ||
| 4495 | "anchored to where it used to be\n", | ||
| 4496 | (double)moved / 1e9); | ||
| 4497 | ✗ | return true; | |
| 4498 | 6 | } | |
| 4499 | |||
| 4500 | // The banner, the flag the watchdog gates on, and the baseline it measures | ||
| 4501 | // against all describe one fact, so they are set and cleared in one place. | ||
| 4502 | // | ||
| 4503 | // The baseline is not taken here. InitPTP() runs before any pipeline exists, | ||
| 4504 | // so the media clock has not been opened yet and PtpMonotonicDelta() has | ||
| 4505 | // nothing to read; ResetPtpStepReference() seeds it instead, at the point | ||
| 4506 | // ApplyConfig() has finished anchoring everything, which is the moment the | ||
| 4507 | // baseline is supposed to describe. | ||
| 4508 | 7 | void AES67Manager::RaisePtpNoLockWarning() { | |
| 4509 | { | ||
| 4510 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 | std::lock_guard<std::mutex> lock(m_ptpStepMutex); |
| 4511 | 7 | m_ptpNoLockBaselineValid = false; | |
| 4512 | 7 | } | |
| 4513 | 7 | m_ptpNoLockWarned.store(true); | |
| 4514 |
2/4✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
21 | WarningHolder::AddWarning(AES67::WARNING_ID_PTP, AES67::WARNING_PTP_NO_LOCK); |
| 4515 | 7 | } | |
| 4516 | |||
| 4517 | 238 | void AES67Manager::ClearPtpNoLockWarning() { | |
| 4518 | 238 | m_ptpNoLockWarned.store(false); | |
| 4519 | { | ||
| 4520 |
1/2✓ Branch 0 taken 238 times.
✗ Branch 1 not taken.
|
238 | std::lock_guard<std::mutex> lock(m_ptpStepMutex); |
| 4521 | 238 | m_ptpNoLockBaselineValid = false; | |
| 4522 | 238 | } | |
| 4523 |
3/6✓ Branch 0 taken 238 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 238 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 238 times.
✗ Branch 5 not taken.
|
714 | WarningHolder::RemoveWarning(AES67::WARNING_ID_PTP, AES67::WARNING_PTP_NO_LOCK); |
| 4524 | 238 | } | |
| 4525 | |||
| 4526 | 935 | bool AES67Manager::PtpFreeRunning() const { | |
| 4527 |
4/4✓ Branch 0 taken 692 times.
✓ Branch 1 taken 243 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 688 times.
|
935 | return !m_ptpHwTimestamping.load() && IsPtp4lRunning(); |
| 4528 | } | ||
| 4529 | |||
| 4530 | 54 | void AES67Manager::ReviewPtpFreeRunningWarning() { | |
| 4531 | 54 | bool following = false; | |
| 4532 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 52 times.
|
54 | if (PtpFreeRunning()) { |
| 4533 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | RefreshPtpCache(); |
| 4534 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | std::lock_guard<std::mutex> lock(m_ptpCacheMutex); |
| 4535 |
3/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
|
3 | following = m_ptpCache.gmPresentValid && m_ptpCache.gmPresent && |
| 4536 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | !IsGrandmasterPortState(m_ptpCache.portState); |
| 4537 | 2 | } | |
| 4538 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 53 times.
|
54 | if (following) { |
| 4539 | // Three watchdog passes, so one slow pass does not drop it. | ||
| 4540 |
3/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
4 | WarningHolder::AddWarningTimeout(3 * AES67::SAP_ANNOUNCE_INTERVAL_S, |
| 4541 | AES67::WARNING_ID_PTP, | ||
| 4542 | AES67::WARNING_PTP_FREE_RUNNING); | ||
| 4543 | } else { | ||
| 4544 |
3/6✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 53 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 53 times.
✗ Branch 5 not taken.
|
212 | WarningHolder::RemoveWarning(AES67::WARNING_ID_PTP, AES67::WARNING_PTP_FREE_RUNNING); |
| 4545 | } | ||
| 4546 | 54 | } | |
| 4547 | |||
| 4548 | // WARNING_PTP_NO_LOCK says the RTP timelines were anchored to a clock that had | ||
| 4549 | // not settled yet. That stops being true on its own: ptp4l commonly reaches | ||
| 4550 | // SLAVE with a usable offset a few seconds after ptpLockWaitMs ran out. | ||
| 4551 | // Nothing used to notice -- the warning has no timeout, ShutdownPTP() was the | ||
| 4552 | // only thing that retracted it, and PtpClockStepped() below only asks for a | ||
| 4553 | // rebuild on a jump of PTP_STEP_DETECT_NS or more *within one watchdog | ||
| 4554 | // interval*. A clock that settles by slewing moves far less than that per | ||
| 4555 | // interval, so the banner and the "rebuilding when it does" badge outlived the | ||
| 4556 | // problem all the way to the next fppd restart. That is what the user sees on | ||
| 4557 | // an ordinary boot where the grandmaster is simply slower to appear than the | ||
| 4558 | // five-second cap allows. | ||
| 4559 | // | ||
| 4560 | // Settling is not on its own enough to retract it, though: the anchors are | ||
| 4561 | // still where InitPTP() left them, so what matters is how far the clock has | ||
| 4562 | // travelled since. Measured against the baseline taken when the warning went | ||
| 4563 | // up -- see m_ptpNoLockBaselineNs for why the step detector's own reference | ||
| 4564 | // cannot answer this. | ||
| 4565 | // | ||
| 4566 | // moved less than a step -> the anchors turned out fine; retract. | ||
| 4567 | // moved a step or more -> they did not; ask for the rebuild, which | ||
| 4568 | // retracts the warning via ShutdownPTP(). | ||
| 4569 | // | ||
| 4570 | // Returns true when the caller should rebuild. | ||
| 4571 | 6 | bool AES67Manager::ReviewPtpNoLockWarning() { | |
| 4572 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
|
6 | if (!m_ptpNoLockWarned.load()) { |
| 4573 | 5 | return false; | |
| 4574 | } | ||
| 4575 | |||
| 4576 | 1 | std::string portState; | |
| 4577 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | const PtpSettleState settled = PtpSettledNow(&portState); |
| 4578 | // NoGrandmasterYet counts as an answer here. Unlike WaitForPtpLock(), | ||
| 4579 | // this runs at least a watchdog interval after the wait gave up, which is | ||
| 4580 | // long past the point where "no announce seen" and "no announce yet" stop | ||
| 4581 | // looking the same. | ||
| 4582 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (settled == PtpSettleState::NotSettled) { |
| 4583 | ✗ | return false; | |
| 4584 | } | ||
| 4585 | |||
| 4586 | 1 | int64_t delta = 0; | |
| 4587 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | const bool haveDelta = PtpMonotonicDelta(delta); |
| 4588 | 1 | int64_t baseline = 0; | |
| 4589 | 1 | bool haveBaseline = false; | |
| 4590 | { | ||
| 4591 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | std::lock_guard<std::mutex> lock(m_ptpStepMutex); |
| 4592 | 1 | baseline = m_ptpNoLockBaselineNs; | |
| 4593 | 1 | haveBaseline = m_ptpNoLockBaselineValid; | |
| 4594 | 1 | } | |
| 4595 | |||
| 4596 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | if (haveDelta && haveBaseline) { |
| 4597 | 1 | const int64_t moved = delta - baseline; | |
| 4598 | 1 | const int64_t absMoved = moved < 0 ? -moved : moved; | |
| 4599 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (absMoved >= AES67::PTP_STEP_DETECT_NS) { |
| 4600 | ✗ | LogWarn(VB_MEDIAOUT, | |
| 4601 | "AES67 watchdog: PTP has settled (%s) but moved %+.3f s " | ||
| 4602 | "getting there — the RTP timelines are anchored to where it " | ||
| 4603 | "used to be\n", | ||
| 4604 | portState.c_str(), (double)moved / 1e9); | ||
| 4605 | ✗ | return true; | |
| 4606 | } | ||
| 4607 | } | ||
| 4608 | |||
| 4609 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogInfo(VB_MEDIAOUT, |
| 4610 | "AES67 watchdog: PTP has settled (%s) close to where the streams " | ||
| 4611 | "were anchored — clearing the provisional-anchor warning\n", | ||
| 4612 | portState.c_str()); | ||
| 4613 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | ClearPtpNoLockWarning(); |
| 4614 | // The badge this drives promises a rebuild that is no longer coming. | ||
| 4615 | 1 | m_ptpLockedAtStart.store(true); | |
| 4616 | 1 | return false; | |
| 4617 | 1 | } | |
| 4618 | |||
| 4619 | 18 | bool AES67Manager::CheckPtpWatchdog() { | |
| 4620 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 16 times.
|
18 | if (!m_config.ptpEnabled) { |
| 4621 | 2 | return false; | |
| 4622 | } | ||
| 4623 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 7 times.
|
16 | if (!m_ptpInitialized) { |
| 4624 | // Its start at apply time or a restart below failed (interface down, | ||
| 4625 | // the port briefly held). Everything after this used to return here, | ||
| 4626 | // so PTP stayed down until someone applied the config again. | ||
| 4627 | 9 | auto now = std::chrono::steady_clock::now(); | |
| 4628 |
7/8✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 8 times.
|
9 | if (m_ptpRetryDelayS > 0 && now < m_ptpRetryAt) { |
| 4629 | 1 | return false; | |
| 4630 | } | ||
| 4631 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 5 times.
|
8 | m_ptpRetryDelayS = m_ptpRetryDelayS == 0 ? 30 : std::min(m_ptpRetryDelayS * 2, 300); |
| 4632 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | m_ptpRetryAt = now + std::chrono::seconds(m_ptpRetryDelayS); |
| 4633 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | LogInfo(VB_MEDIAOUT, "AES67 watchdog: PTP is not running, starting it\n"); |
| 4634 |
3/4✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 1 times.
|
8 | if (!InitPTP()) { |
| 4635 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 | LogWarn(VB_MEDIAOUT, "AES67 watchdog: PTP start failed, trying again in %ds\n", m_ptpRetryDelayS); |
| 4636 | 7 | return false; | |
| 4637 | } | ||
| 4638 | 1 | m_ptpRetryDelayS = 0; | |
| 4639 | // The streams were built without it (or against the ptp4l that died): | ||
| 4640 | // build them again on the clock that is disciplined now. | ||
| 4641 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogInfo(VB_MEDIAOUT, "AES67 watchdog: PTP started, rebuilding the streams on it\n"); |
| 4642 | 1 | return true; | |
| 4643 | } | ||
| 4644 | |||
| 4645 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6 times.
|
7 | if (!IsPtp4lRunning()) { |
| 4646 | 1 | LogWarn(VB_MEDIAOUT, "AES67 watchdog: ptp4l is gone — restarting PTP\n"); | |
| 4647 | // Tear down first: InitPTP() is a no-op while m_ptpInitialized is set, | ||
| 4648 | // and phc2sys must not be left pointed at a dead daemon. | ||
| 4649 | 1 | ShutdownPTP(); | |
| 4650 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (InitPTP()) { |
| 4651 | 1 | LogInfo(VB_MEDIAOUT, "AES67 watchdog: ptp4l restarted\n"); | |
| 4652 | } else { | ||
| 4653 | // Retried on the next passes; see above. | ||
| 4654 | ✗ | m_ptpRetryDelayS = 0; | |
| 4655 | ✗ | LogErr(VB_MEDIAOUT, "AES67 watchdog: ptp4l restart failed\n"); | |
| 4656 | } | ||
| 4657 | // A fresh ptp4l may have stepped the clock on its way back to lock, and | ||
| 4658 | // the reference we were holding describes the old one either way. | ||
| 4659 | 1 | ResetPtpStepReference(); | |
| 4660 | 1 | return false; | |
| 4661 | } | ||
| 4662 | |||
| 4663 | 6 | ReviewPtpFreeRunningWarning(); | |
| 4664 | |||
| 4665 | // A clock that locks *after* the pipelines were built breaks them exactly | ||
| 4666 | // as the cold-boot race did (issue #2848): the grandmaster that boots | ||
| 4667 | // slower than the Pi, or a BMCA changeover to a device on a different | ||
| 4668 | // epoch. rtpL24pay latches timestamp-offset on the segment, so there is | ||
| 4669 | // no re-anchoring in place -- the only fix is to build them again, which | ||
| 4670 | // is cheap and is what the under-delivery path already does 90 seconds | ||
| 4671 | // later. This just gets there without waiting for the symptom. | ||
| 4672 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (PtpClockStepped()) { |
| 4673 | ✗ | return true; | |
| 4674 | } | ||
| 4675 | |||
| 4676 | // No step, but the clock may still have quietly settled since the streams | ||
| 4677 | // started -- in which case a warning is standing that nothing else will | ||
| 4678 | // ever take down. | ||
| 4679 | 6 | return ReviewPtpNoLockWarning(); | |
| 4680 | } | ||
| 4681 | |||
| 4682 | 18 | bool AES67Manager::PollPipelinesWatchdog() { | |
| 4683 | 18 | bool needsRebuild = false; | |
| 4684 | |||
| 4685 | { // scope for pipeline mutex | ||
| 4686 |
1/2✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
|
18 | std::lock_guard<std::mutex> lock(m_pipelineMutex); |
| 4687 | |||
| 4688 | 36 | auto checkPipelines = [this, &needsRebuild](std::map<int, AES67Pipeline>& pipelines, const char* direction) { | |
| 4689 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 36 times.
|
49 | for (auto& kv : pipelines) { |
| 4690 | 13 | AES67Pipeline& p = kv.second; | |
| 4691 |
2/4✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
|
14 | if (!p.pipeline || !p.running) continue; |
| 4692 | |||
| 4693 | // Drain bus messages (error, warning, state changes) | ||
| 4694 |
1/2✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
|
13 | if (p.bus) { |
| 4695 | GstMessage* msg; | ||
| 4696 |
3/4✓ Branch 0 taken 64260 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 64247 times.
✓ Branch 3 taken 13 times.
|
64260 | while ((msg = gst_bus_pop(p.bus)) != nullptr) { |
| 4697 |
3/3✓ Branch 0 taken 1 times.
✓ Branch 1 taken 63940 times.
✓ Branch 2 taken 306 times.
|
64247 | switch (GST_MESSAGE_TYPE(msg)) { |
| 4698 | 1 | case GST_MESSAGE_ERROR: { | |
| 4699 | 1 | GError* err = nullptr; gchar* dbg = nullptr; | |
| 4700 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | gst_message_parse_error(msg, &err, &dbg); |
| 4701 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogErr(VB_MEDIAOUT, |
| 4702 | "AES67 %s pipeline [%d] bus error: %s\n", | ||
| 4703 | direction, p.instanceId, err->message); | ||
| 4704 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | p.errorMessage = std::string("GStreamer error: ") + err->message; |
| 4705 | 1 | p.busError = true; | |
| 4706 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | g_error_free(err); g_free(dbg); |
| 4707 | 1 | break; | |
| 4708 | } | ||
| 4709 | 63940 | case GST_MESSAGE_WARNING: { | |
| 4710 | 63940 | GError* err = nullptr; gchar* dbg = nullptr; | |
| 4711 |
1/2✓ Branch 0 taken 63940 times.
✗ Branch 1 not taken.
|
63940 | gst_message_parse_warning(msg, &err, &dbg); |
| 4712 |
1/2✓ Branch 0 taken 63940 times.
✗ Branch 1 not taken.
|
63940 | LogWarn(VB_MEDIAOUT, |
| 4713 | "AES67 %s pipeline [%d] bus warning: %s\n", | ||
| 4714 | direction, p.instanceId, err->message); | ||
| 4715 |
2/4✓ Branch 0 taken 63940 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 63940 times.
✗ Branch 3 not taken.
|
63940 | g_error_free(err); g_free(dbg); |
| 4716 | 63940 | break; | |
| 4717 | } | ||
| 4718 | 306 | default: break; | |
| 4719 | } | ||
| 4720 |
1/2✓ Branch 0 taken 64247 times.
✗ Branch 1 not taken.
|
64247 | gst_message_unref(msg); |
| 4721 | } | ||
| 4722 | } | ||
| 4723 | |||
| 4724 | // Already known dead; the bus-error backoff below decides when it | ||
| 4725 | // is rebuilt, and the stall checks would only bypass it. | ||
| 4726 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 12 times.
|
13 | if (p.busError) continue; |
| 4727 | |||
| 4728 | // Check current pipeline state | ||
| 4729 | 12 | GstState curState = GST_STATE_NULL, pendingState = GST_STATE_VOID_PENDING; | |
| 4730 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | gst_element_get_state(p.pipeline, &curState, &pendingState, 0 /* no blocking */); |
| 4731 | |||
| 4732 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
12 | if (curState != GST_STATE_PLAYING && pendingState != GST_STATE_PLAYING) { |
| 4733 | // Don't attempt recovery while holding m_pipelineMutex — | ||
| 4734 | // gst_element_set_state can block on PipeWire indefinitely. | ||
| 4735 | // Just flag for a full rebuild instead. | ||
| 4736 | ✗ | LogWarn(VB_MEDIAOUT, | |
| 4737 | "AES67 %s pipeline [%d] is in %s state — flagging for rebuild\n", | ||
| 4738 | direction, p.instanceId, | ||
| 4739 | gst_element_state_get_name(curState)); | ||
| 4740 | ✗ | p.running = false; | |
| 4741 | ✗ | p.errorMessage = "Watchdog: not in PLAYING state"; | |
| 4742 | ✗ | needsRebuild = true; | |
| 4743 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | } else if (p.isSend) { |
| 4744 | // Zombie detection for send pipelines: check if udpsink | ||
| 4745 | // is actually pushing bytes. pipewiresrc can lose its | ||
| 4746 | // PipeWire socket connection (e.g. PipeWire restart) while | ||
| 4747 | // GStreamer still reports PLAYING — producing no output. | ||
| 4748 |
3/6✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
|
12 | GstElement* usink = gst_bin_get_by_name(GST_BIN(p.pipeline), "usink"); |
| 4749 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | if (usink) { |
| 4750 | 12 | guint64 bytesSent = 0; | |
| 4751 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | g_object_get(usink, "bytes-served", &bytesSent, NULL); |
| 4752 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | gst_object_unref(usink); |
| 4753 | |||
| 4754 | // Retune the pacing shift once, when the pipeline has | ||
| 4755 | // actually prerolled and can answer a latency query. The | ||
| 4756 | // presentation lead is (shift - latency), and latency is | ||
| 4757 | // one graph quantum -- 21.3ms at 1024/48000 but 42.7ms at | ||
| 4758 | // 2048. A fixed 40ms shift is comfortably positive on the | ||
| 4759 | // first and negative on the second, and negative means a | ||
| 4760 | // conformant receiver discards every packet. | ||
| 4761 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 7 times.
|
12 | if (!p.pacingTuned) { |
| 4762 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | auto sit = m_sinkPacingShift.find(p.instanceId); |
| 4763 |
3/6✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
|
5 | if (sit != m_sinkPacingShift.end() && sit->second) { |
| 4764 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | GstQuery* lq = gst_query_new_latency(); |
| 4765 |
2/4✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
5 | if (gst_element_query(p.pipeline, lq)) { |
| 4766 | 5 | gboolean live = FALSE; | |
| 4767 | 5 | GstClockTime minL = 0, maxL = 0; | |
| 4768 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | gst_query_parse_latency(lq, &live, &minL, &maxL); |
| 4769 |
2/4✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
5 | if (GST_CLOCK_TIME_IS_VALID(minL) && minL > 0) { |
| 4770 | 5 | const GstClockTime want = | |
| 4771 | 5 | minL + 16 * GST_MSECOND; | |
| 4772 | const GstClockTime cur = | ||
| 4773 | 5 | sit->second->load( | |
| 4774 | 5 | std::memory_order_relaxed); | |
| 4775 | const GstClockTime shift = | ||
| 4776 | 5 | std::max(want, cur); | |
| 4777 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (shift != cur) { |
| 4778 | ✗ | sit->second->store( | |
| 4779 | shift, std::memory_order_relaxed); | ||
| 4780 | ✗ | GstElement* us2 = gst_bin_get_by_name( | |
| 4781 | ✗ | GST_BIN(p.pipeline), "usink"); | |
| 4782 | ✗ | if (us2) { | |
| 4783 | ✗ | g_object_set(us2, "ts-offset", | |
| 4784 | ✗ | -(gint64)shift, NULL); | |
| 4785 | ✗ | gst_object_unref(us2); | |
| 4786 | } | ||
| 4787 | } | ||
| 4788 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | LogInfo(VB_MEDIAOUT, |
| 4789 | "AES67 send [%d]: pipeline latency %.1fms, timestamp lead %.1fms\n", | ||
| 4790 | p.instanceId, | ||
| 4791 | (double)minL / GST_MSECOND, | ||
| 4792 | (double)shift / GST_MSECOND); | ||
| 4793 | 5 | p.pacingTuned = true; | |
| 4794 | } | ||
| 4795 | } | ||
| 4796 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | gst_query_unref(lq); |
| 4797 | } | ||
| 4798 | } | ||
| 4799 | |||
| 4800 | // Sink-pacing diagnostics. The queue is the whole reason | ||
| 4801 | // pacing can be done without starving the source, so its | ||
| 4802 | // depth is what explains a stream that paces correctly for | ||
| 4803 | // a minute and then reverts to bursting: empty means the | ||
| 4804 | // sink is starved and every buffer arrives past its send | ||
| 4805 | // time, full means backpressure has reached pipewiresrc and | ||
| 4806 | // audio is being dropped there. No element by this name | ||
| 4807 | // exists unless sink pacing is on, so this costs nothing | ||
| 4808 | // and needs no lock on the config. | ||
| 4809 | // Whether the rate matcher is actually correcting is a | ||
| 4810 | // readable fact here, not an inference -- which is the | ||
| 4811 | // whole reason it was chosen over "speed" and "pitch". | ||
| 4812 | // At ~56ppm this should climb by ~2.7 samples/s. | ||
| 4813 |
3/6✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
|
12 | GstElement* rm = gst_bin_get_by_name(GST_BIN(p.pipeline), |
| 4814 | "ratematch"); | ||
| 4815 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (rm) { |
| 4816 | ✗ | guint64 added = 0, dropped = 0, in = 0, out = 0; | |
| 4817 | ✗ | g_object_get(rm, "add", &added, "drop", &dropped, | |
| 4818 | "in", &in, "out", &out, NULL); | ||
| 4819 | ✗ | gst_object_unref(rm); | |
| 4820 | ✗ | LogInfo(VB_MEDIAOUT, | |
| 4821 | "AES67 send [%d] ratematch: in %llu out %llu, added %llu dropped %llu\n", | ||
| 4822 | p.instanceId, (unsigned long long)in, | ||
| 4823 | (unsigned long long)out, | ||
| 4824 | (unsigned long long)added, | ||
| 4825 | (unsigned long long)dropped); | ||
| 4826 | } | ||
| 4827 | |||
| 4828 |
3/6✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
|
12 | GstElement* sinkq = gst_bin_get_by_name(GST_BIN(p.pipeline), |
| 4829 | "sinkq"); | ||
| 4830 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | if (sinkq) { |
| 4831 | 12 | guint qBuffers = 0; | |
| 4832 | 12 | guint64 qTime = 0; | |
| 4833 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | g_object_get(sinkq, "current-level-buffers", &qBuffers, |
| 4834 | "current-level-time", &qTime, NULL); | ||
| 4835 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | gst_object_unref(sinkq); |
| 4836 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | LogInfo(VB_MEDIAOUT, |
| 4837 | "AES67 send [%d] pacing: queue %u buffers / %llums, +%llu bytes since last check\n", | ||
| 4838 | p.instanceId, qBuffers, | ||
| 4839 | (unsigned long long)(qTime / GST_MSECOND), | ||
| 4840 | (unsigned long long)(bytesSent - p.lastByteCount)); | ||
| 4841 | } | ||
| 4842 | |||
| 4843 | // Compare throughput against what this instance should | ||
| 4844 | // be emitting. Payload is ptime worth of 24-bit frames, | ||
| 4845 | // and at nominal that is AUDIO_RATE * 3 * channels bytes a | ||
| 4846 | // second regardless of ptime. bytes-served counts whole | ||
| 4847 | // RTP packets, though, so each packet's 12-byte header is | ||
| 4848 | // in it too: leaving that out had a healthy stream read | ||
| 4849 | // 104% at 1ms ptime (101% at 4ms), and a stream delivering | ||
| 4850 | // 94% of its audio read 98% and never tripped the check. | ||
| 4851 | 12 | const auto nowT = std::chrono::steady_clock::now(); | |
| 4852 |
4/4✓ Branch 0 taken 7 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 6 times.
|
19 | if (p.lastByteTime.time_since_epoch().count() != 0 && |
| 4853 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
|
7 | bytesSent > p.lastByteCount) { |
| 4854 | const double secs = | ||
| 4855 |
2/4✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
6 | std::chrono::duration<double>(nowT - p.lastByteTime) |
| 4856 | 6 | .count(); | |
| 4857 | 6 | constexpr double RTP_HEADER_BYTES = 12.0; | |
| 4858 | 6 | const double expected = | |
| 4859 | 6 | (double)AES67::AUDIO_RATE * 3.0 * p.channels + | |
| 4860 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | (p.ptime > 0 ? RTP_HEADER_BYTES * 1000.0 / p.ptime : 0.0); |
| 4861 |
2/4✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
6 | if (secs > 5.0 && expected > 0) { |
| 4862 | 6 | const double got = | |
| 4863 | 6 | (double)(bytesSent - p.lastByteCount) / secs; | |
| 4864 | // 95%, not something looser: the degradation | ||
| 4865 | // settles at 89-94% of nominal, so an 85% trigger | ||
| 4866 | // sat below every case actually observed and would | ||
| 4867 | // never have fired. A healthy stream measures | ||
| 4868 | // 100.0% consistently, so the margin is real, and | ||
| 4869 | // three consecutive checks means ~90s of sustained | ||
| 4870 | // under-delivery -- far longer than the brief dip a | ||
| 4871 | // track change produces. | ||
| 4872 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
|
6 | if (got < expected * 0.95) { |
| 4873 | 1 | p.lowRateCount++; | |
| 4874 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogWarn(VB_MEDIAOUT, |
| 4875 | "AES67 %s pipeline [%d] under-delivering: %.0f of %.0f B/s (%.0f%%), check %d\n", | ||
| 4876 | direction, p.instanceId, got, expected, | ||
| 4877 | 100.0 * got / expected, p.lowRateCount); | ||
| 4878 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (p.lowRateCount >= 3) { |
| 4879 | ✗ | p.running = false; | |
| 4880 | p.errorMessage = | ||
| 4881 | ✗ | "Watchdog: sustained under-delivery"; | |
| 4882 | ✗ | needsRebuild = true; | |
| 4883 | } | ||
| 4884 | } else { | ||
| 4885 | 5 | p.lowRateCount = 0; | |
| 4886 | } | ||
| 4887 | } | ||
| 4888 | } | ||
| 4889 | 12 | p.lastByteTime = nowT; | |
| 4890 | |||
| 4891 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
|
12 | if (bytesSent == p.lastByteCount) { |
| 4892 | 2 | p.stallCount++; | |
| 4893 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (p.stallCount >= 2) { |
| 4894 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogWarn(VB_MEDIAOUT, |
| 4895 | "AES67 %s pipeline [%d] stalled (bytes-sent=%llu for %d checks) — scheduling rebuild\n", | ||
| 4896 | direction, p.instanceId, (unsigned long long)bytesSent, p.stallCount); | ||
| 4897 | 1 | p.running = false; | |
| 4898 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | p.errorMessage = "Watchdog: pipeline stalled"; |
| 4899 | 1 | needsRebuild = true; | |
| 4900 | } | ||
| 4901 | } else { | ||
| 4902 | 10 | p.stallCount = 0; | |
| 4903 | } | ||
| 4904 | 12 | p.lastByteCount = bytesSent; | |
| 4905 | } | ||
| 4906 | } | ||
| 4907 | } | ||
| 4908 | 36 | }; | |
| 4909 | |||
| 4910 |
1/2✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
|
18 | checkPipelines(m_sendPipelines, "send"); |
| 4911 |
1/2✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
|
18 | checkPipelines(m_recvPipelines, "recv"); |
| 4912 | |||
| 4913 | // An ERROR stops the pipeline's streaming for good, and a receive | ||
| 4914 | // pipeline has nothing else that would ever notice. The first one | ||
| 4915 | // rebuilds at once; one that keeps coming back waits 30s, 60s, ... up to | ||
| 4916 | // 8 minutes between rebuilds, and the count starts over after 10 quiet | ||
| 4917 | // minutes. A stream that failed to start at all is never stored, so | ||
| 4918 | // nothing above sees it; it is retried on the same backoff. | ||
| 4919 | { | ||
| 4920 | 18 | const auto now = std::chrono::steady_clock::now(); | |
| 4921 | 18 | std::vector<AES67Pipeline*> errored; | |
| 4922 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 18 times.
|
54 | for (auto* pipelines : { &m_sendPipelines, &m_recvPipelines }) { |
| 4923 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 36 times.
|
49 | for (auto& kv : *pipelines) { |
| 4924 |
3/4✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 12 times.
|
13 | if (kv.second.pipeline && kv.second.busError) { |
| 4925 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | errored.push_back(&kv.second); |
| 4926 | } | ||
| 4927 | } | ||
| 4928 | } | ||
| 4929 |
2/10✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 18 times.
|
18 | if (m_errorRebuilds > 0 && now - m_lastErrorRebuild > std::chrono::minutes(10)) { |
| 4930 | ✗ | m_errorRebuilds = 0; | |
| 4931 | } | ||
| 4932 |
6/6✓ Branch 0 taken 17 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 12 times.
|
18 | if (!errored.empty() || m_failedStarts > 0) { |
| 4933 | 6 | const auto wait = std::chrono::seconds(30) * (1 << std::min(std::max(m_errorRebuilds - 1, 0), 4)); | |
| 4934 |
3/12✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 6 times.
✗ Branch 11 not taken.
|
6 | if (needsRebuild || m_errorRebuilds == 0 || now - m_lastErrorRebuild >= wait) { |
| 4935 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (!needsRebuild) { |
| 4936 | 6 | m_errorRebuilds++; | |
| 4937 | 6 | m_lastErrorRebuild = now; | |
| 4938 | } | ||
| 4939 | 6 | needsRebuild = true; | |
| 4940 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6 times.
|
7 | for (AES67Pipeline* p : errored) { |
| 4941 | 1 | p->running = false; | |
| 4942 | } | ||
| 4943 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | LogWarn(VB_MEDIAOUT, "AES67 watchdog: %d pipeline(s) stopped on a bus error, " |
| 4944 | "%d failed to start\n", | ||
| 4945 | (int)errored.size(), m_failedStarts); | ||
| 4946 | } else { | ||
| 4947 | ✗ | LogWarn(VB_MEDIAOUT, | |
| 4948 | "AES67 watchdog: %d pipeline(s) stopped on a bus error, %d failed to start, " | ||
| 4949 | "rebuilding in %llds (error rebuild %d)\n", | ||
| 4950 | (int)errored.size(), m_failedStarts, | ||
| 4951 | (long long)std::chrono::duration_cast<std::chrono::seconds>( | ||
| 4952 | wait - (now - m_lastErrorRebuild)).count(), | ||
| 4953 | m_errorRebuilds + 1); | ||
| 4954 | } | ||
| 4955 | } | ||
| 4956 | 18 | } | |
| 4957 | |||
| 4958 | // If any pipeline needs rebuilding, re-apply config. | ||
| 4959 | // ApplyConfig() tears down all pipelines and recreates them with fresh | ||
| 4960 | // PipeWire connections. | ||
| 4961 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 11 times.
|
18 | if (needsRebuild) { |
| 4962 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 | LogWarn(VB_MEDIAOUT, "AES67 watchdog: triggering full pipeline rebuild\n"); |
| 4963 | } | ||
| 4964 | 18 | } // end pipeline mutex scope | |
| 4965 | |||
| 4966 | 18 | return needsRebuild; | |
| 4967 | } | ||
| 4968 | |||
| 4969 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 4970 | // Network helpers | ||
| 4971 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 4972 | 2164 | std::string AES67Manager::GetInterfaceIP(const std::string& iface) { | |
| 4973 | 2164 | struct ifaddrs* addrs = nullptr; | |
| 4974 |
1/2✓ Branch 0 taken 2164 times.
✗ Branch 1 not taken.
|
2164 | std::string result = "0.0.0.0"; |
| 4975 | |||
| 4976 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2164 times.
|
2164 | if (getifaddrs(&addrs) != 0) { |
| 4977 | ✗ | return result; | |
| 4978 | } | ||
| 4979 | |||
| 4980 |
2/2✓ Branch 0 taken 10830 times.
✓ Branch 1 taken 2154 times.
|
12984 | for (struct ifaddrs* ifa = addrs; ifa; ifa = ifa->ifa_next) { |
| 4981 |
3/4✓ Branch 0 taken 10830 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6494 times.
✓ Branch 3 taken 4336 times.
|
10830 | if (!ifa->ifa_addr || ifa->ifa_addr->sa_family != AF_INET) continue; |
| 4982 |
7/8✓ Branch 0 taken 3972 times.
✓ Branch 1 taken 364 times.
✓ Branch 2 taken 3972 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1982 times.
✓ Branch 5 taken 1990 times.
✓ Branch 6 taken 2346 times.
✓ Branch 7 taken 1990 times.
|
4336 | if (iface.empty() || iface == ifa->ifa_name) { |
| 4983 |
4/6✓ Branch 0 taken 2346 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2346 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2154 times.
✓ Branch 5 taken 192 times.
|
4692 | if (std::string(ifa->ifa_name) == "lo") continue; |
| 4984 | 192 | struct sockaddr_in* addr = (struct sockaddr_in*)ifa->ifa_addr; | |
| 4985 | char buf[INET_ADDRSTRLEN]; | ||
| 4986 | 192 | inet_ntop(AF_INET, &addr->sin_addr, buf, sizeof(buf)); | |
| 4987 |
1/2✓ Branch 0 taken 192 times.
✗ Branch 1 not taken.
|
192 | result = buf; |
| 4988 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 182 times.
|
192 | if (!iface.empty()) break; // found the specific interface |
| 4989 | } | ||
| 4990 | } | ||
| 4991 | |||
| 4992 | 2164 | freeifaddrs(addrs); | |
| 4993 | 2164 | return result; | |
| 4994 | ✗ | } | |
| 4995 | |||
| 4996 | 393 | std::string AES67Manager::SafeNodeName(const std::string& name) { | |
| 4997 |
1/2✓ Branch 0 taken 393 times.
✗ Branch 1 not taken.
|
393 | std::string result = "aes67_"; |
| 4998 |
2/2✓ Branch 0 taken 860 times.
✓ Branch 1 taken 393 times.
|
1253 | for (char c : name) { |
| 4999 |
3/4✓ Branch 0 taken 6 times.
✓ Branch 1 taken 854 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
|
860 | if (std::isalnum(c) || c == '_') { |
| 5000 |
1/2✓ Branch 0 taken 854 times.
✗ Branch 1 not taken.
|
854 | result += std::tolower(c); |
| 5001 | } else { | ||
| 5002 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | result += '_'; |
| 5003 | } | ||
| 5004 | } | ||
| 5005 | 393 | return result; | |
| 5006 | ✗ | } | |
| 5007 | |||
| 5008 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 5009 | // SAP Announcer — RFC 2974 compliant | ||
| 5010 | // Replaces external fpp_aes67_sap Python daemon | ||
| 5011 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 5012 | 4 | uint16_t AES67Manager::ComputeSAPHash(const std::string& sdp) { | |
| 5013 | // RFC 2974 §6: the message id hash identifies "the precise version of this | ||
| 5014 | // announcement" and MUST change when the payload changes. It is therefore | ||
| 5015 | // computed over the SDP text, not over the instance: an announcement whose | ||
| 5016 | // refclk we corrected but whose hash stayed put is discarded as a repeat by | ||
| 5017 | // a compliant receiver, which is how a stale ts-refclk survives forever. | ||
| 5018 | // | ||
| 5019 | // Session *identity* is carried by the SDP o= line (stable sess-id, rising | ||
| 5020 | // sess-version), so a changed hash reads as "this session was modified", | ||
| 5021 | // not as a second session. | ||
| 5022 | 4 | uint32_t hash = 2166136261u; | |
| 5023 |
2/2✓ Branch 0 taken 932 times.
✓ Branch 1 taken 4 times.
|
936 | for (char c : sdp) { |
| 5024 | 932 | hash ^= (uint32_t)(unsigned char)c; | |
| 5025 | 932 | hash *= 16777619u; | |
| 5026 | } | ||
| 5027 | 4 | return (uint16_t)(hash & 0xFFFF); | |
| 5028 | } | ||
| 5029 | |||
| 5030 | // Monotonic SDP o= version. Seeded from the wall clock so it keeps rising | ||
| 5031 | // across fppd restarts -- a receiver that has cached version N ignores a | ||
| 5032 | // re-announcement numbered below it. | ||
| 5033 | 3 | void AES67Manager::LoadSDPVersion() { | |
| 5034 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (m_sdpVersionPath.empty()) { |
| 5035 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
6 | m_sdpVersionPath = getFPPMediaDir("/config/.aes67-sdp-version"); |
| 5036 | } | ||
| 5037 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | std::ifstream f(m_sdpVersionPath); |
| 5038 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | if (!f.is_open()) { |
| 5039 | 2 | return; | |
| 5040 | } | ||
| 5041 | 1 | std::string key; | |
| 5042 | 1 | uint64_t ver = 0; | |
| 5043 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | f >> key >> ver; |
| 5044 |
3/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
1 | if (!key.empty() && ver > 0) { |
| 5045 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | m_sdpBodyKey = key; |
| 5046 | 1 | m_lastSdpVersion.store((uint32_t)ver, std::memory_order_relaxed); | |
| 5047 | } | ||
| 5048 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | } |
| 5049 | |||
| 5050 | 2 | void AES67Manager::SaveSDPVersion() { | |
| 5051 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (m_sdpVersionPath.empty()) { |
| 5052 | ✗ | return; | |
| 5053 | } | ||
| 5054 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | std::ofstream f(m_sdpVersionPath, std::ios::trunc); |
| 5055 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (f.is_open()) { |
| 5056 |
4/8✓ 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.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
|
4 | f << m_sdpBodyKey << " " << m_lastSdpVersion.load(std::memory_order_relaxed) << "\n"; |
| 5057 | } | ||
| 5058 | 2 | } | |
| 5059 | |||
| 5060 | // Returns the version to stamp on this announcement. Same body as last time = | ||
| 5061 | // same version, so the SDP (and therefore the SAP msg id hash derived from it) | ||
| 5062 | // is byte-identical and receivers see one continuing session rather than a new | ||
| 5063 | // one per restart. | ||
| 5064 | 4 | uint32_t AES67Manager::SDPVersionFor(const std::string& body) { | |
| 5065 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
|
4 | if (m_sdpVersionPath.empty()) { |
| 5066 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | LoadSDPVersion(); |
| 5067 | } | ||
| 5068 | |||
| 5069 | 4 | uint32_t h = 2166136261u; | |
| 5070 |
2/2✓ Branch 0 taken 896 times.
✓ Branch 1 taken 4 times.
|
900 | for (char c : body) { |
| 5071 | 896 | h ^= (uint32_t)(unsigned char)c; | |
| 5072 | 896 | h *= 16777619u; | |
| 5073 | } | ||
| 5074 | char keyBuf[16]; | ||
| 5075 | 4 | snprintf(keyBuf, sizeof(keyBuf), "%08x", h); | |
| 5076 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | std::string key(keyBuf); |
| 5077 | |||
| 5078 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | if (key == m_sdpBodyKey) { |
| 5079 | 2 | uint32_t existing = m_lastSdpVersion.load(std::memory_order_relaxed); | |
| 5080 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (existing > 0) { |
| 5081 | 2 | return existing; | |
| 5082 | } | ||
| 5083 | } | ||
| 5084 | |||
| 5085 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | uint32_t v = NextSDPVersion(); |
| 5086 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | m_sdpBodyKey = key; |
| 5087 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | SaveSDPVersion(); |
| 5088 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | LogInfo(VB_MEDIAOUT, "AES67 SAP: announcement content changed, SDP version now %u\n", v); |
| 5089 | 2 | return v; | |
| 5090 | 4 | } | |
| 5091 | |||
| 5092 | 2 | uint32_t AES67Manager::NextSDPVersion() { | |
| 5093 | 2 | uint32_t candidate = (uint32_t)time(nullptr); | |
| 5094 | 4 | uint32_t prev = m_lastSdpVersion.load(std::memory_order_relaxed); | |
| 5095 | uint32_t next; | ||
| 5096 | do { | ||
| 5097 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | next = (candidate > prev) ? candidate : prev + 1; |
| 5098 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
4 | } while (!m_lastSdpVersion.compare_exchange_weak(prev, next, std::memory_order_relaxed)); |
| 5099 | 2 | return next; | |
| 5100 | } | ||
| 5101 | |||
| 5102 | 1564 | std::string AES67Manager::BuildSDP(const AES67Instance& inst, | |
| 5103 | const std::string& sourceIP, | ||
| 5104 | const std::string& ptpClockId, | ||
| 5105 | uint32_t sdpVersion) { | ||
| 5106 | // AES67-compliant SDP — unique session ID per device + stream. | ||
| 5107 | // Combine source IP, stream name, multicast IP, and port so that | ||
| 5108 | // different FPP boxes (or different streams on the same box) always | ||
| 5109 | // produce distinct o= lines. | ||
| 5110 |
2/6✓ Branch 0 taken 1564 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1564 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
3128 | std::string key = sourceIP + ":" + inst.name + ":" + |
| 5111 |
4/8✓ Branch 0 taken 1564 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1564 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1564 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1564 times.
✗ Branch 7 not taken.
|
4692 | inst.multicastIP + ":" + std::to_string(inst.port); |
| 5112 | // FNV-1a hash → deterministic, stable across restarts, unique per key | ||
| 5113 | 1564 | uint32_t h = 2166136261u; | |
| 5114 |
2/2✓ Branch 0 taken 39166 times.
✓ Branch 1 taken 1564 times.
|
40730 | for (char c : key) { |
| 5115 | 39166 | h ^= (uint32_t)(unsigned char)c; | |
| 5116 | 39166 | h *= 16777619u; | |
| 5117 | } | ||
| 5118 | 1564 | int sessionId = (int)(h & 0x3FFFFFFFu); // 30-bit positive value | |
| 5119 | |||
| 5120 | // o=<user> <sess-id> <sess-version> ... RFC 4566: sess-id identifies the | ||
| 5121 | // session and must stay put for its lifetime; sess-version rises each time | ||
| 5122 | // the description is modified, which is how a receiver knows to re-read a | ||
| 5123 | // session it already has (e.g. after BMCA changed the ts-refclk). | ||
| 5124 |
1/2✓ Branch 0 taken 1564 times.
✗ Branch 1 not taken.
|
1564 | std::ostringstream sdp; |
| 5125 | sdp << "v=0\r\n" | ||
| 5126 |
5/10✓ Branch 0 taken 1564 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1564 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1564 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1564 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1564 times.
✗ Branch 9 not taken.
|
1564 | << "o=- " << sessionId << " " << sdpVersion << " IN IP4 " << sourceIP << "\r\n" |
| 5127 | 1564 | << "s=" << inst.sessionName << "\r\n" | |
| 5128 |
10/20✓ Branch 0 taken 1564 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1564 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1564 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1564 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1564 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 1564 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 1564 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 1564 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 1564 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 1564 times.
✗ Branch 19 not taken.
|
1564 | << "c=IN IP4 " << inst.multicastIP << "/" << AES67::AUDIO_RTP_TTL << "\r\n" |
| 5129 | << "t=0 0\r\n" | ||
| 5130 |
6/12✓ Branch 0 taken 1564 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1564 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1564 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1564 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1564 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 1564 times.
✗ Branch 11 not taken.
|
1564 | << "m=audio " << inst.port << " RTP/AVP " << AES67::RTP_PAYLOAD_TYPE << "\r\n" |
| 5131 |
4/8✓ Branch 0 taken 1564 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1564 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1564 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1564 times.
✗ Branch 7 not taken.
|
1564 | << "a=rtpmap:" << AES67::RTP_PAYLOAD_TYPE << " L24/" |
| 5132 |
3/6✓ Branch 0 taken 1564 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1564 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1564 times.
✗ Branch 5 not taken.
|
1564 | << AES67::AUDIO_RATE << "/" << inst.channels << "\r\n" |
| 5133 | << "a=sendonly\r\n" | ||
| 5134 |
4/8✓ Branch 0 taken 1564 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1564 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1564 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1564 times.
✗ Branch 7 not taken.
|
1564 | << "a=ptime:" << inst.ptime << "\r\n" |
| 5135 | << "a=ts-refclk:ptp=IEEE1588-2008:" << ptpClockId << ":0\r\n" | ||
| 5136 |
5/10✓ Branch 0 taken 1564 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1564 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1564 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1564 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1564 times.
✗ Branch 9 not taken.
|
1564 | << "a=mediaclk:direct=0\r\n"; |
| 5137 | // NOTE: AES67 requires a=mediaclk:direct, and "direct=0" asserts that the | ||
| 5138 | // RTP timestamp is derived directly from the reference clock with zero | ||
| 5139 | // offset. That is not yet true here -- the payloader's timestamps come | ||
| 5140 | // from the pipeline's monotonic clock with its own start offset, so the | ||
| 5141 | // rate is PTP-locked but the phase is arbitrary. Making the assertion | ||
| 5142 | // true needs a GstPtpClock on the send pipeline plus an explicit | ||
| 5143 | // rtpL24pay timestamp-offset derived from PTP time. | ||
| 5144 | |||
| 5145 |
1/2✓ Branch 0 taken 1564 times.
✗ Branch 1 not taken.
|
3128 | return sdp.str(); |
| 5146 | 1564 | } | |
| 5147 | |||
| 5148 | // Filename to offer for a downloaded .sdp. Not SafeNodeName(): that prefixes | ||
| 5149 | // "aes67_" for the PipeWire node namespace, and instance names usually already | ||
| 5150 | // start with "AES67", which produced aes67_aes67_stream_1.sdp. | ||
| 5151 | 1555 | static std::string SDPFileName(const AES67Instance& inst) { | |
| 5152 | 1555 | std::string base; | |
| 5153 |
2/2✓ Branch 0 taken 3167 times.
✓ Branch 1 taken 1555 times.
|
4722 | for (char c : inst.name) { |
| 5154 |
4/6✓ Branch 0 taken 5 times.
✓ Branch 1 taken 3162 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 5 times.
|
3167 | if (std::isalnum((unsigned char)c) || c == '_' || c == '-') { |
| 5155 |
1/2✓ Branch 0 taken 3162 times.
✗ Branch 1 not taken.
|
3162 | base += (char)std::tolower((unsigned char)c); |
| 5156 |
3/6✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
|
5 | } else if (!base.empty() && base.back() != '_') { |
| 5157 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | base += '_'; |
| 5158 | } | ||
| 5159 | } | ||
| 5160 |
3/6✓ Branch 0 taken 1555 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1555 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1555 times.
|
1555 | while (!base.empty() && base.back() == '_') { |
| 5161 | ✗ | base.pop_back(); | |
| 5162 | } | ||
| 5163 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1555 times.
|
1555 | if (base.empty()) { |
| 5164 | ✗ | base = "aes67_" + std::to_string(inst.id); | |
| 5165 | } | ||
| 5166 |
1/2✓ Branch 0 taken 1555 times.
✗ Branch 1 not taken.
|
3110 | return base + ".sdp"; |
| 5167 | 1555 | } | |
| 5168 | |||
| 5169 | 1555 | std::string AES67Manager::ExportSDPFor(const AES67Instance& inst, | |
| 5170 | const AES67Config& cfg) { | ||
| 5171 | // Same refclk rule as the announcer: advertise the domain's grandmaster, | ||
| 5172 | // not our own identity, and fall back to our own only when BMCA has not | ||
| 5173 | // settled or PTP is off. | ||
| 5174 |
1/2✓ Branch 0 taken 1555 times.
✗ Branch 1 not taken.
|
1555 | std::string gm = GetActiveGrandmasterId(); |
| 5175 |
4/6✓ Branch 0 taken 1554 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1554 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
1555 | std::string ptpClockId = gm.empty() ? GetPTPClockId() : gm; |
| 5176 | 1555 | std::string sourceIP = GetInterfaceIP(inst.interface.empty() ? | |
| 5177 |
3/4✓ Branch 0 taken 1552 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1555 times.
✗ Branch 3 not taken.
|
1555 | cfg.ptpInterface : inst.interface); |
| 5178 | |||
| 5179 | // Reuse whatever version the announcer last stamped so an exported file | ||
| 5180 | // and the SAP announcement describe the same revision of the session. | ||
| 5181 | // Not SDPVersionFor(): that mints a new version and rewrites the version | ||
| 5182 | // file, and a GET must not renumber the sessions being announced. With | ||
| 5183 | // SAP off, or before the first announcement, there is no version to match | ||
| 5184 | // and 1 will do -- sess-version only ever gets compared against an earlier | ||
| 5185 | // reading of the same session. | ||
| 5186 | 1555 | uint32_t version = m_lastSdpVersion.load(std::memory_order_relaxed); | |
| 5187 |
2/2✓ Branch 0 taken 1552 times.
✓ Branch 1 taken 3 times.
|
1555 | if (version == 0) { |
| 5188 | 1552 | version = 1; | |
| 5189 | } | ||
| 5190 |
1/2✓ Branch 0 taken 1555 times.
✗ Branch 1 not taken.
|
3110 | return BuildSDP(inst, sourceIP, ptpClockId, version); |
| 5191 | 1555 | } | |
| 5192 | |||
| 5193 | 8 | std::vector<uint8_t> AES67Manager::BuildSAPPacket(const std::string& sourceIP, | |
| 5194 | uint16_t msgIdHash, | ||
| 5195 | const std::string& sdp, | ||
| 5196 | bool isDeletion) { | ||
| 5197 | // RFC 2974 SAP packet: | ||
| 5198 | // Byte 0: V=1 (bits 7-5), A=0 (bit 4), R=0 (bit 3), T=isDeletion (bit 2), | ||
| 5199 | // E=0 (bit 1), C=0 (bit 0) | ||
| 5200 | // Byte 1: Auth length = 0 | ||
| 5201 | // Bytes 2-3: Message ID hash (network byte order) | ||
| 5202 | // Bytes 4-7: Originating source (IPv4) | ||
| 5203 | // Payload type: "application/sdp\0" | ||
| 5204 | // SDP data | ||
| 5205 | |||
| 5206 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | std::string payloadType = "application/sdp"; |
| 5207 | |||
| 5208 | 8 | uint8_t header0 = (AES67::SAP_VERSION << 5); // V=1 | |
| 5209 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 | if (isDeletion) { |
| 5210 | 4 | header0 |= 0x04; // T=1 (deletion) | |
| 5211 | } | ||
| 5212 | |||
| 5213 | // Parse source IP | ||
| 5214 | struct in_addr srcAddr; | ||
| 5215 | 8 | inet_pton(AF_INET, sourceIP.c_str(), &srcAddr); | |
| 5216 | |||
| 5217 | 8 | std::vector<uint8_t> packet; | |
| 5218 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | packet.push_back(header0); |
| 5219 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | packet.push_back(0); // auth length |
| 5220 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | packet.push_back((msgIdHash >> 8) & 0xFF); |
| 5221 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | packet.push_back(msgIdHash & 0xFF); |
| 5222 | |||
| 5223 | // Source IP (4 bytes, network order) | ||
| 5224 | 8 | uint8_t* ipBytes = (uint8_t*)&srcAddr.s_addr; | |
| 5225 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | packet.push_back(ipBytes[0]); |
| 5226 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | packet.push_back(ipBytes[1]); |
| 5227 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | packet.push_back(ipBytes[2]); |
| 5228 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | packet.push_back(ipBytes[3]); |
| 5229 | |||
| 5230 | // Payload type string + NUL | ||
| 5231 |
2/2✓ Branch 0 taken 120 times.
✓ Branch 1 taken 8 times.
|
128 | for (char c : payloadType) { |
| 5232 |
1/2✓ Branch 0 taken 120 times.
✗ Branch 1 not taken.
|
120 | packet.push_back((uint8_t)c); |
| 5233 | } | ||
| 5234 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | packet.push_back(0); |
| 5235 | |||
| 5236 | // SDP data | ||
| 5237 |
2/2✓ Branch 0 taken 1864 times.
✓ Branch 1 taken 8 times.
|
1872 | for (char c : sdp) { |
| 5238 |
1/2✓ Branch 0 taken 1864 times.
✗ Branch 1 not taken.
|
1864 | packet.push_back((uint8_t)c); |
| 5239 | } | ||
| 5240 | |||
| 5241 | 8 | return packet; | |
| 5242 | 8 | } | |
| 5243 | |||
| 5244 | 4 | void AES67Manager::SAPAnnounceLoop() { | |
| 5245 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | LogInfo(VB_MEDIAOUT, "AES67 SAP announcer thread started\n"); |
| 5246 | |||
| 5247 | // Create UDP socket for SAP multicast | ||
| 5248 | 4 | int sock = OpenSocket(AF_INET, SOCK_DGRAM, 0); | |
| 5249 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (sock < 0) { |
| 5250 | ✗ | LogErr(VB_MEDIAOUT, "AES67 SAP: Failed to create socket: %s\n", FPPstrerror(errno)); | |
| 5251 | ✗ | return; | |
| 5252 | } | ||
| 5253 | |||
| 5254 | // Set TTL | ||
| 5255 | 4 | int ttl = AES67::SAP_TTL; | |
| 5256 | 4 | setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); | |
| 5257 | |||
| 5258 | // Set multicast interface if specified | ||
| 5259 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (!m_config.ptpInterface.empty()) { |
| 5260 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | std::string ifIP = GetInterfaceIP(m_config.ptpInterface); |
| 5261 | struct in_addr localAddr; | ||
| 5262 | 4 | inet_pton(AF_INET, ifIP.c_str(), &localAddr); | |
| 5263 | 4 | setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, &localAddr, sizeof(localAddr)); | |
| 5264 | 4 | } | |
| 5265 | |||
| 5266 | struct sockaddr_in sapAddr; | ||
| 5267 | 4 | memset(&sapAddr, 0, sizeof(sapAddr)); | |
| 5268 | 4 | sapAddr.sin_family = AF_INET; | |
| 5269 | 4 | sapAddr.sin_port = htons(AES67::SAP_PORT); | |
| 5270 | 4 | inet_pton(AF_INET, AES67::SAP_MCAST_ADDRESS, &sapAddr.sin_addr); | |
| 5271 | |||
| 5272 | // SDP's ts-refclk must identify the domain's actual grandmaster (RFC 7273), | ||
| 5273 | // not this node's own identity — otherwise a follower incorrectly | ||
| 5274 | // advertises itself as the clock source. Fall back to our own derived | ||
| 5275 | // ID only if PTP is disabled or ptp4l hasn't selected a grandmaster yet. | ||
| 5276 | 77 | auto queryPtpClockId = [this]() -> std::string { | |
| 5277 |
1/2✓ Branch 0 taken 77 times.
✗ Branch 1 not taken.
|
77 | std::string gm = GetActiveGrandmasterId(); |
| 5278 |
2/6✓ Branch 0 taken 77 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 77 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
154 | return gm.empty() ? GetPTPClockId() : gm; |
| 5279 | 77 | }; | |
| 5280 | |||
| 5281 | // Build all SAP packets for send instances | ||
| 5282 | struct SAPEntry { | ||
| 5283 | uint16_t hash; | ||
| 5284 | std::vector<uint8_t> announcePacket; | ||
| 5285 | std::vector<uint8_t> deletePacket; | ||
| 5286 | }; | ||
| 5287 | // Senders held idle because nothing feeds them are not on the wire, so | ||
| 5288 | // they must not be announced -- a receiver that subscribes to an announced | ||
| 5289 | // stream carrying no packets has no way to tell that from a broken sender. | ||
| 5290 | // Snapshotted here rather than read per announce: ApplyConfig() restarts | ||
| 5291 | // this thread whenever the set can change. | ||
| 5292 | 4 | std::set<int> heldSenders; | |
| 5293 | { | ||
| 5294 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | std::lock_guard<std::mutex> lock(m_pipelineMutex); |
| 5295 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | for (const auto& [id, d] : m_deferredSenders) { |
| 5296 | ✗ | heldSenders.insert(id); | |
| 5297 | } | ||
| 5298 | 4 | } | |
| 5299 | |||
| 5300 | 4 | auto buildEntries = [this, &heldSenders](const std::string& ptpClockId) -> std::vector<SAPEntry> { | |
| 5301 | 4 | std::vector<SAPEntry> result; | |
| 5302 | |||
| 5303 | // Version the announcement by what is in it. Building the bodies with | ||
| 5304 | // a fixed placeholder version first gives a stable key to compare | ||
| 5305 | // against the last announcement -- see SDPVersionFor(). | ||
| 5306 | 4 | std::string bodyKey; | |
| 5307 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 | for (const auto& inst : m_config.instances) { |
| 5308 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
4 | if (!inst.enabled || !inst.sapEnabled) continue; |
| 5309 |
3/10✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 4 times.
|
4 | if (inst.mode != "send" && inst.mode != "both") continue; |
| 5310 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
4 | if (heldSenders.count(inst.id)) continue; |
| 5311 | 4 | std::string sourceIP = GetInterfaceIP(inst.interface.empty() ? | |
| 5312 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
4 | m_config.ptpInterface : inst.interface); |
| 5313 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
4 | bodyKey += BuildSDP(inst, sourceIP, ptpClockId, 0); |
| 5314 | 4 | } | |
| 5315 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | uint32_t sdpVersion = SDPVersionFor(bodyKey); |
| 5316 | |||
| 5317 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 | for (const auto& inst : m_config.instances) { |
| 5318 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (!inst.enabled) continue; |
| 5319 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (!inst.sapEnabled) continue; |
| 5320 |
3/10✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 4 times.
|
4 | if (inst.mode != "send" && inst.mode != "both") continue; |
| 5321 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
4 | if (heldSenders.count(inst.id)) continue; |
| 5322 | |||
| 5323 | 4 | std::string sourceIP = GetInterfaceIP(inst.interface.empty() ? | |
| 5324 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
4 | m_config.ptpInterface : inst.interface); |
| 5325 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | std::string sdp = BuildSDP(inst, sourceIP, ptpClockId, sdpVersion); |
| 5326 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | uint16_t hash = ComputeSAPHash(sdp); |
| 5327 | |||
| 5328 | 4 | SAPEntry entry; | |
| 5329 | 4 | entry.hash = hash; | |
| 5330 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | entry.announcePacket = BuildSAPPacket(sourceIP, hash, sdp, false); |
| 5331 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | entry.deletePacket = BuildSAPPacket(sourceIP, hash, sdp, true); |
| 5332 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | result.push_back(entry); |
| 5333 | 4 | } | |
| 5334 | 4 | return result; | |
| 5335 | 4 | }; | |
| 5336 | |||
| 5337 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | std::string ptpClockId = queryPtpClockId(); |
| 5338 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | std::vector<SAPEntry> entries = buildEntries(ptpClockId); |
| 5339 | |||
| 5340 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (entries.empty()) { |
| 5341 | ✗ | LogWarn(VB_MEDIAOUT, "AES67 SAP: No SAP-enabled send instances — announcer has nothing to send\n"); | |
| 5342 | } else { | ||
| 5343 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | LogInfo(VB_MEDIAOUT, "AES67 SAP: Announcing %d stream(s) to %s:%d every %ds\n", |
| 5344 | (int)entries.size(), AES67::SAP_MCAST_ADDRESS, AES67::SAP_PORT, | ||
| 5345 | AES67::SAP_ANNOUNCE_INTERVAL_S); | ||
| 5346 | } | ||
| 5347 | |||
| 5348 | 6 | auto announceAll = [&]() { | |
| 5349 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | for (const auto& entry : entries) { |
| 5350 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | ssize_t sent = sendto(sock, entry.announcePacket.data(), entry.announcePacket.size(), 0, |
| 5351 | (struct sockaddr*)&sapAddr, sizeof(sapAddr)); | ||
| 5352 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (sent < 0) { |
| 5353 | ✗ | LogErr(VB_MEDIAOUT, "AES67 SAP: sendto failed: %s\n", FPPstrerror(errno)); | |
| 5354 | } | ||
| 5355 | } | ||
| 5356 | 6 | }; | |
| 5357 | |||
| 5358 | // Rebuild and re-announce immediately when BMCA changes the refclk, rather | ||
| 5359 | // than letting a wrong ts-refclk stand for the rest of the announce cycle. | ||
| 5360 | 73 | auto refreshRefclk = [&]() -> bool { | |
| 5361 |
1/2✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
|
73 | std::string current = queryPtpClockId(); |
| 5362 |
1/2✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
|
73 | if (current == ptpClockId) { |
| 5363 | 73 | return false; | |
| 5364 | } | ||
| 5365 | ✗ | LogInfo(VB_MEDIAOUT, "AES67 SAP: PTP refclk changed (%s -> %s), rebuilding SDP\n", | |
| 5366 | ptpClockId.c_str(), current.c_str()); | ||
| 5367 | ✗ | ptpClockId = current; | |
| 5368 | // The rebuilt entries carry a new msg id hash (it is derived from the | ||
| 5369 | // SDP text). We deliberately do NOT delete the old hash first: the | ||
| 5370 | // o= sess-id is unchanged, so a receiver reads this as a modification | ||
| 5371 | // of a session it already has, whereas a deletion could make it tear | ||
| 5372 | // the stream down for the moment before the new announcement lands. | ||
| 5373 | ✗ | entries = buildEntries(ptpClockId); | |
| 5374 | ✗ | return true; | |
| 5375 | 73 | }; | |
| 5376 | |||
| 5377 | // Announce loop | ||
| 5378 | 4 | auto threadStart = std::chrono::steady_clock::now(); | |
| 5379 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 4 times.
|
10 | while (m_sapAnnounceRunning.load()) { |
| 5380 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | refreshRefclk(); |
| 5381 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | announceAll(); |
| 5382 | |||
| 5383 | // Sleep for SAP_ANNOUNCE_INTERVAL_S, checking shutdown flag every second. | ||
| 5384 | // | ||
| 5385 | // ptp4l needs several seconds after startup to finish BMCA and adopt an | ||
| 5386 | // upstream master; until then it reports itself as its own grandmaster. | ||
| 5387 | // Poll every second through that window so the corrected refclk goes out | ||
| 5388 | // within a second of the election settling instead of up to a full | ||
| 5389 | // announce interval later. Results are cached (PTP_QUERY_CACHE_MS), so | ||
| 5390 | // this is one pmc query per second at worst, and only for a minute. | ||
| 5391 |
6/6✓ Branch 0 taken 76 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 72 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 72 times.
✓ Branch 5 taken 6 times.
|
78 | for (int i = 0; i < AES67::SAP_ANNOUNCE_INTERVAL_S && m_sapAnnounceRunning.load(); i++) { |
| 5392 |
1/2✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
|
72 | std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 5393 | |||
| 5394 |
1/2✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
|
72 | auto elapsed = std::chrono::duration_cast<std::chrono::seconds>( |
| 5395 |
1/2✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
|
144 | std::chrono::steady_clock::now() - threadStart).count(); |
| 5396 |
6/6✓ Branch 0 taken 70 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 67 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 67 times.
✓ Branch 5 taken 5 times.
|
72 | if (elapsed < AES67::PTP_CONVERGENCE_WINDOW_S && m_sapAnnounceRunning.load()) { |
| 5397 |
2/4✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 67 times.
|
67 | if (refreshRefclk()) { |
| 5398 | ✗ | announceAll(); | |
| 5399 | } | ||
| 5400 | } | ||
| 5401 | } | ||
| 5402 | |||
| 5403 | // Pipeline recovery and PTP supervision run in WatchdogLoop(), which | ||
| 5404 | // doesn't depend on anything announcing. | ||
| 5405 | } | ||
| 5406 | |||
| 5407 | // Send deletion packets on shutdown | ||
| 5408 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 | for (const auto& entry : entries) { |
| 5409 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | sendto(sock, entry.deletePacket.data(), entry.deletePacket.size(), 0, |
| 5410 | (struct sockaddr*)&sapAddr, sizeof(sapAddr)); | ||
| 5411 | } | ||
| 5412 | |||
| 5413 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | close(sock); |
| 5414 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | LogInfo(VB_MEDIAOUT, "AES67 SAP announcer thread stopped (deletion packets sent)\n"); |
| 5415 | 4 | } | |
| 5416 | |||
| 5417 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 5418 | // SAP Receiver — listens for remote AES67 announcements | ||
| 5419 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 5420 | 48 | void AES67Manager::SAPReceiveLoop() { | |
| 5421 |
1/2✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
48 | LogInfo(VB_MEDIAOUT, "AES67 SAP receiver thread started\n"); |
| 5422 | |||
| 5423 | 48 | int sock = OpenSocket(AF_INET, SOCK_DGRAM, 0); | |
| 5424 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | if (sock < 0) { |
| 5425 | ✗ | LogErr(VB_MEDIAOUT, "AES67 SAP recv: Failed to create socket: %s\n", FPPstrerror(errno)); | |
| 5426 | ✗ | return; | |
| 5427 | } | ||
| 5428 | |||
| 5429 | // Allow address reuse | ||
| 5430 | 48 | int reuse = 1; | |
| 5431 | 48 | setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)); | |
| 5432 | |||
| 5433 | // Bind to SAP port | ||
| 5434 | struct sockaddr_in bindAddr; | ||
| 5435 | 48 | memset(&bindAddr, 0, sizeof(bindAddr)); | |
| 5436 | 48 | bindAddr.sin_family = AF_INET; | |
| 5437 | 48 | bindAddr.sin_port = htons(AES67::SAP_PORT); | |
| 5438 | 48 | bindAddr.sin_addr.s_addr = htonl(INADDR_ANY); | |
| 5439 | |||
| 5440 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | if (bind(sock, (struct sockaddr*)&bindAddr, sizeof(bindAddr)) < 0) { |
| 5441 | ✗ | LogErr(VB_MEDIAOUT, "AES67 SAP recv: bind failed: %s\n", FPPstrerror(errno)); | |
| 5442 | ✗ | close(sock); | |
| 5443 | ✗ | return; | |
| 5444 | } | ||
| 5445 | |||
| 5446 | // Join SAP multicast group | ||
| 5447 | struct ip_mreq mreq; | ||
| 5448 | 48 | inet_pton(AF_INET, AES67::SAP_MCAST_ADDRESS, &mreq.imr_multiaddr); | |
| 5449 | |||
| 5450 |
1/2✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
48 | if (!m_config.ptpInterface.empty()) { |
| 5451 |
1/2✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
48 | std::string ifIP = GetInterfaceIP(m_config.ptpInterface); |
| 5452 | 48 | inet_pton(AF_INET, ifIP.c_str(), &mreq.imr_interface); | |
| 5453 | 48 | } else { | |
| 5454 | ✗ | mreq.imr_interface.s_addr = htonl(INADDR_ANY); | |
| 5455 | } | ||
| 5456 | |||
| 5457 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) { |
| 5458 | ✗ | LogWarn(VB_MEDIAOUT, "AES67 SAP recv: join multicast failed: %s\n", FPPstrerror(errno)); | |
| 5459 | } | ||
| 5460 | |||
| 5461 | // Set receive timeout so we can check the shutdown flag | ||
| 5462 | struct timeval tv; | ||
| 5463 | 48 | tv.tv_sec = 2; | |
| 5464 | 48 | tv.tv_usec = 0; | |
| 5465 | 48 | setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); | |
| 5466 | |||
| 5467 | uint8_t buf[4096]; | ||
| 5468 |
2/2✓ Branch 0 taken 364 times.
✓ Branch 1 taken 48 times.
|
412 | while (m_sapRecvRunning.load()) { |
| 5469 | struct sockaddr_in senderAddr; | ||
| 5470 | 364 | socklen_t addrLen = sizeof(senderAddr); | |
| 5471 | |||
| 5472 |
1/2✓ Branch 0 taken 364 times.
✗ Branch 1 not taken.
|
364 | ssize_t n = recvfrom(sock, buf, sizeof(buf), 0, |
| 5473 | (struct sockaddr*)&senderAddr, &addrLen); | ||
| 5474 |
2/2✓ Branch 0 taken 49 times.
✓ Branch 1 taken 315 times.
|
364 | if (n <= 0) continue; // timeout or error |
| 5475 | |||
| 5476 | char senderIP[INET_ADDRSTRLEN]; | ||
| 5477 | 315 | inet_ntop(AF_INET, &senderAddr.sin_addr, senderIP, sizeof(senderIP)); | |
| 5478 | |||
| 5479 |
2/4✓ Branch 0 taken 315 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 315 times.
✗ Branch 3 not taken.
|
630 | HandleSAPPacket(buf, (size_t)n, senderIP); |
| 5480 | } | ||
| 5481 | |||
| 5482 | // Leave multicast group | ||
| 5483 | 48 | setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)); | |
| 5484 |
1/2✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
48 | close(sock); |
| 5485 |
1/2✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
48 | LogInfo(VB_MEDIAOUT, "AES67 SAP receiver thread stopped\n"); |
| 5486 | } | ||
| 5487 | |||
| 5488 | 315 | void AES67Manager::HandleSAPPacket(const uint8_t* data, size_t len, | |
| 5489 | const std::string& senderAddr) { | ||
| 5490 | // Minimum SAP header: 8 bytes (4 header + 4 source IP) | ||
| 5491 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 315 times.
|
319 | if (len < 8) return; |
| 5492 | |||
| 5493 | 315 | uint8_t header0 = data[0]; | |
| 5494 | 315 | int version = (header0 >> 5) & 0x07; | |
| 5495 | 315 | bool isDeletion = (header0 & 0x04) != 0; | |
| 5496 | // bool isIPv6 = (header0 & 0x10) != 0; // A bit — we only handle IPv4 | ||
| 5497 | |||
| 5498 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 315 times.
|
315 | if (version != AES67::SAP_VERSION) return; |
| 5499 | |||
| 5500 | 315 | uint16_t msgIdHash = ((uint16_t)data[2] << 8) | data[3]; | |
| 5501 | |||
| 5502 | // Skip auth data | ||
| 5503 | 315 | uint8_t authLen = data[1]; | |
| 5504 | 315 | size_t payloadStart = 8 + authLen * 4; | |
| 5505 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 315 times.
|
315 | if (payloadStart >= len) return; |
| 5506 | |||
| 5507 | // Find end of payload type string (NUL terminated) | ||
| 5508 | 315 | size_t sdpStart = payloadStart; | |
| 5509 |
3/4✓ Branch 0 taken 5040 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4725 times.
✓ Branch 3 taken 315 times.
|
5040 | while (sdpStart < len && data[sdpStart] != 0) { |
| 5510 | 4725 | sdpStart++; | |
| 5511 | } | ||
| 5512 | 315 | sdpStart++; // skip NUL | |
| 5513 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 315 times.
|
315 | if (sdpStart >= len) return; |
| 5514 | |||
| 5515 | // Ignore our own announcements | ||
| 5516 |
1/2✓ Branch 0 taken 315 times.
✗ Branch 1 not taken.
|
315 | std::string ourIP = GetInterfaceIP(m_config.ptpInterface); |
| 5517 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 315 times.
|
315 | if (senderAddr == ourIP) return; |
| 5518 | |||
| 5519 |
1/2✓ Branch 0 taken 315 times.
✗ Branch 1 not taken.
|
315 | const auto key = std::make_pair(senderAddr, msgIdHash); |
| 5520 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 311 times.
|
315 | if (isDeletion) { |
| 5521 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | std::lock_guard<std::mutex> lock(m_discoveredMutex); |
| 5522 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | auto it = m_discoveredStreams.find(key); |
| 5523 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (it != m_discoveredStreams.end()) { |
| 5524 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | LogInfo(VB_MEDIAOUT, "AES67 SAP: Stream deleted: %s\n", |
| 5525 | it->second.sessionName.c_str()); | ||
| 5526 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | m_discoveredStreams.erase(it); |
| 5527 | } | ||
| 5528 | 4 | return; | |
| 5529 | 4 | } | |
| 5530 | |||
| 5531 | // Parse SDP for stream info | ||
| 5532 |
1/2✓ Branch 0 taken 311 times.
✗ Branch 1 not taken.
|
311 | std::string sdp((const char*)data + sdpStart, len - sdpStart); |
| 5533 | 933 | auto clamp = [](std::string v) { | |
| 5534 |
2/2✓ Branch 0 taken 300 times.
✓ Branch 1 taken 633 times.
|
933 | if (v.size() > AES67::MAX_SAP_FIELD_LEN) { |
| 5535 | 300 | v.resize(AES67::MAX_SAP_FIELD_LEN); | |
| 5536 | } | ||
| 5537 | 933 | return v; | |
| 5538 | }; | ||
| 5539 | |||
| 5540 | 311 | SAPDiscoveredStream stream; | |
| 5541 | 311 | stream.msgIdHash = msgIdHash; | |
| 5542 |
1/2✓ Branch 0 taken 311 times.
✗ Branch 1 not taken.
|
311 | stream.originAddress = senderAddr; |
| 5543 |
1/2✓ Branch 0 taken 311 times.
✗ Branch 1 not taken.
|
311 | stream.lastSeenMs = std::chrono::duration_cast<std::chrono::milliseconds>( |
| 5544 | 622 | std::chrono::steady_clock::now().time_since_epoch()).count(); | |
| 5545 | |||
| 5546 | // Parse SDP fields | ||
| 5547 |
1/2✓ Branch 0 taken 311 times.
✗ Branch 1 not taken.
|
311 | std::istringstream sdpStream(sdp); |
| 5548 | 311 | std::string line; | |
| 5549 |
4/6✓ Branch 0 taken 3122 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3122 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2811 times.
✓ Branch 5 taken 311 times.
|
3122 | while (std::getline(sdpStream, line)) { |
| 5550 | // Remove \r | ||
| 5551 |
3/6✓ Branch 0 taken 2811 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2811 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2811 times.
✗ Branch 5 not taken.
|
2811 | if (!line.empty() && line.back() == '\r') line.pop_back(); |
| 5552 | |||
| 5553 |
4/6✓ Branch 0 taken 2811 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2811 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 311 times.
✓ Branch 5 taken 2500 times.
|
2811 | if (line.substr(0, 2) == "s=") { |
| 5554 |
2/4✓ Branch 0 taken 311 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 311 times.
✗ Branch 3 not taken.
|
311 | stream.sessionName = clamp(line.substr(2)); |
| 5555 |
4/6✓ Branch 0 taken 2500 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2500 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 311 times.
✓ Branch 5 taken 2189 times.
|
2500 | } else if (line.substr(0, 2) == "c=") { |
| 5556 | // c=IN IP4 239.69.0.1/255 | ||
| 5557 | 311 | size_t ip4Pos = line.find("IP4 "); | |
| 5558 |
1/2✓ Branch 0 taken 311 times.
✗ Branch 1 not taken.
|
311 | if (ip4Pos != std::string::npos) { |
| 5559 |
1/2✓ Branch 0 taken 311 times.
✗ Branch 1 not taken.
|
311 | std::string addr = line.substr(ip4Pos + 4); |
| 5560 | 311 | size_t slash = addr.find('/'); | |
| 5561 |
2/4✓ Branch 0 taken 311 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 311 times.
✗ Branch 3 not taken.
|
311 | if (slash != std::string::npos) addr = addr.substr(0, slash); |
| 5562 |
2/4✓ Branch 0 taken 311 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 311 times.
✗ Branch 3 not taken.
|
311 | stream.multicastIP = clamp(addr); |
| 5563 | 311 | } | |
| 5564 |
4/6✓ Branch 0 taken 2189 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2189 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 311 times.
✓ Branch 5 taken 1878 times.
|
2189 | } else if (line.substr(0, 8) == "m=audio ") { |
| 5565 | // m=audio 5004 RTP/AVP 96 | ||
| 5566 | 311 | int port = 0; | |
| 5567 |
1/2✓ Branch 0 taken 311 times.
✗ Branch 1 not taken.
|
311 | if (sscanf(line.c_str(), "m=audio %d", &port) == 1) { |
| 5568 | 311 | stream.port = port; | |
| 5569 | } | ||
| 5570 |
4/6✓ Branch 0 taken 1878 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1878 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 311 times.
✓ Branch 5 taken 1567 times.
|
1878 | } else if (line.substr(0, 11) == "a=rtpmap:96") { |
| 5571 | // a=rtpmap:96 L24/48000/2 | ||
| 5572 | 311 | int ch = 2; | |
| 5573 |
1/2✓ Branch 0 taken 311 times.
✗ Branch 1 not taken.
|
311 | if (sscanf(line.c_str(), "a=rtpmap:96 L24/%*d/%d", &ch) == 1) { |
| 5574 | 311 | stream.channels = ch; | |
| 5575 | } | ||
| 5576 |
4/6✓ Branch 0 taken 1567 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1567 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 311 times.
✓ Branch 5 taken 1256 times.
|
1567 | } else if (line.substr(0, 8) == "a=ptime:") { |
| 5577 |
1/2✓ Branch 0 taken 311 times.
✗ Branch 1 not taken.
|
311 | stream.ptime = std::atoi(line.substr(8).c_str()); |
| 5578 |
3/4✓ Branch 0 taken 1256 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 311 times.
✓ Branch 3 taken 945 times.
|
1256 | } else if (line.rfind("a=ts-refclk:ptp=IEEE1588-", 0) == 0) { |
| 5579 | // a=ts-refclk:ptp=IEEE1588-2008:AA-BB-CC-FF-FE-DD-EE-FF:0 | ||
| 5580 | // (The domain suffix is optional.) This used to compare a 26-char | ||
| 5581 | // prefix against the 25-char literal, so it never matched and the | ||
| 5582 | // grandmaster of a discovered stream was never reported. | ||
| 5583 | 311 | size_t colon1 = line.find(':', 15); | |
| 5584 |
1/2✓ Branch 0 taken 311 times.
✗ Branch 1 not taken.
|
311 | if (colon1 != std::string::npos) { |
| 5585 | 311 | size_t colon2 = line.find(':', colon1 + 1); | |
| 5586 | std::string id = line.substr(colon1 + 1, colon2 == std::string::npos ? std::string::npos | ||
| 5587 |
2/4✓ Branch 0 taken 311 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 311 times.
✗ Branch 3 not taken.
|
311 | : colon2 - colon1 - 1); |
| 5588 |
4/8✓ Branch 0 taken 311 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 311 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 311 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 311 times.
|
311 | while (!id.empty() && (id.back() == '\r' || id.back() == ' ')) { |
| 5589 | ✗ | id.pop_back(); | |
| 5590 | } | ||
| 5591 |
2/4✓ Branch 0 taken 311 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 311 times.
✗ Branch 3 not taken.
|
311 | stream.ptpClockId = clamp(id); |
| 5592 | 311 | } | |
| 5593 | } | ||
| 5594 | } | ||
| 5595 | |||
| 5596 | { | ||
| 5597 |
1/2✓ Branch 0 taken 311 times.
✗ Branch 1 not taken.
|
311 | std::lock_guard<std::mutex> lock(m_discoveredMutex); |
| 5598 |
1/2✓ Branch 0 taken 311 times.
✗ Branch 1 not taken.
|
311 | bool isNew = (m_discoveredStreams.find(key) == m_discoveredStreams.end()); |
| 5599 |
6/6✓ Branch 0 taken 309 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 48 times.
✓ Branch 3 taken 261 times.
✓ Branch 4 taken 48 times.
✓ Branch 5 taken 263 times.
|
311 | if (isNew && m_discoveredStreams.size() >= AES67::MAX_SAP_DISCOVERED) { |
| 5600 |
1/2✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
48 | auto oldest = std::min_element(m_discoveredStreams.begin(), m_discoveredStreams.end(), |
| 5601 | 12240 | [](const auto& a, const auto& b) { | |
| 5602 | 12240 | return a.second.lastSeenMs < b.second.lastSeenMs; | |
| 5603 | }); | ||
| 5604 |
1/2✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
48 | m_discoveredStreams.erase(oldest); |
| 5605 | } | ||
| 5606 |
2/4✓ Branch 0 taken 311 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 311 times.
✗ Branch 3 not taken.
|
311 | m_discoveredStreams[key] = stream; |
| 5607 | |||
| 5608 |
2/2✓ Branch 0 taken 309 times.
✓ Branch 1 taken 2 times.
|
311 | if (isNew) { |
| 5609 |
1/2✓ Branch 0 taken 309 times.
✗ Branch 1 not taken.
|
309 | LogInfo(VB_MEDIAOUT, "AES67 SAP: Discovered stream '%s' from %s → %s:%d (%dch)\n", |
| 5610 | stream.sessionName.c_str(), senderAddr.c_str(), | ||
| 5611 | stream.multicastIP.c_str(), stream.port, stream.channels); | ||
| 5612 | } | ||
| 5613 | |||
| 5614 | // Prune stale entries. A remote sender that stops announcing | ||
| 5615 | // without ever transmitting an explicit SAP deletion packet (e.g. | ||
| 5616 | // it crashes, loses power, or drops off the network) would | ||
| 5617 | // otherwise leave its stream in m_discoveredStreams forever. Drop | ||
| 5618 | // anything not seen within ~10 announce intervals. | ||
| 5619 |
1/2✓ Branch 0 taken 311 times.
✗ Branch 1 not taken.
|
311 | uint64_t nowMs = std::chrono::duration_cast<std::chrono::milliseconds>( |
| 5620 | 622 | std::chrono::steady_clock::now().time_since_epoch()).count(); | |
| 5621 | 311 | constexpr uint64_t staleThresholdMs = | |
| 5622 | (uint64_t)AES67::SAP_ANNOUNCE_INTERVAL_S * 1000ULL * 10ULL; | ||
| 5623 |
2/2✓ Branch 0 taken 45191 times.
✓ Branch 1 taken 311 times.
|
45502 | for (auto sit = m_discoveredStreams.begin(); sit != m_discoveredStreams.end(); ) { |
| 5624 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 45191 times.
|
45191 | if (nowMs - sit->second.lastSeenMs > staleThresholdMs) { |
| 5625 | ✗ | LogInfo(VB_MEDIAOUT, "AES67 SAP: Stream '%s' timed out (no announcement in %llus) — removing\n", | |
| 5626 | sit->second.sessionName.c_str(), | ||
| 5627 | (unsigned long long)(staleThresholdMs / 1000)); | ||
| 5628 | ✗ | sit = m_discoveredStreams.erase(sit); | |
| 5629 | } else { | ||
| 5630 | 45191 | ++sit; | |
| 5631 | } | ||
| 5632 | } | ||
| 5633 | 311 | } | |
| 5634 |
4/4✓ Branch 0 taken 311 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 311 times.
✓ Branch 3 taken 4 times.
|
319 | } |
| 5635 | |||
| 5636 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 5637 | // Status reporting — for PHP API | ||
| 5638 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 5639 | // The rate the PipeWire graph is actually clocked at. Mirrors the fallback in | ||
| 5640 | // GStreamerOut.cpp's GetPipeWireGraphRate(), which is file-local there: the | ||
| 5641 | // per-card file carries the rate the hardware really achieved and sorts after | ||
| 5642 | // the defaults, so it wins wherever both exist. | ||
| 5643 | 832 | static int PipeWireGraphRate() { | |
| 5644 | static const char* confs[] = { | ||
| 5645 | "/etc/pipewire/pipewire.conf.d/95-fpp-alsa-sink.conf", | ||
| 5646 | "/etc/pipewire/pipewire.conf.d/90-fpp.conf" | ||
| 5647 | }; | ||
| 5648 |
2/2✓ Branch 0 taken 1664 times.
✓ Branch 1 taken 832 times.
|
2496 | for (const char* conf : confs) { |
| 5649 |
2/4✓ Branch 0 taken 1664 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1664 times.
✗ Branch 3 not taken.
|
1664 | std::string contents = GetFileContents(conf); |
| 5650 | 1664 | size_t p = contents.find("default.clock.rate"); | |
| 5651 |
1/2✓ Branch 0 taken 1664 times.
✗ Branch 1 not taken.
|
1664 | if (p == std::string::npos) |
| 5652 | 1664 | continue; | |
| 5653 | ✗ | p = contents.find('=', p); | |
| 5654 | ✗ | if (p == std::string::npos) | |
| 5655 | ✗ | continue; | |
| 5656 | ✗ | int rate = atoi(contents.c_str() + p + 1); | |
| 5657 | ✗ | if (rate > 0) | |
| 5658 | ✗ | return rate; | |
| 5659 |
1/3✗ Branch 0 not taken.
✓ Branch 1 taken 1664 times.
✗ Branch 2 not taken.
|
1664 | } |
| 5660 | 832 | return 0; | |
| 5661 | } | ||
| 5662 | |||
| 5663 | // The rate pipewiresrc negotiated with the graph for this pipeline, or 0 if it | ||
| 5664 | // has not negotiated yet. Read from the live pad rather than from any config | ||
| 5665 | // file: per-card and per-group rates sit between the graph clock and what this | ||
| 5666 | // stream is actually fed, so default.clock.rate can say 44100 while the stream | ||
| 5667 | // is getting a clean 48000 (or the reverse). | ||
| 5668 | 4 | static int NegotiatedSourceRate(GstElement* pipeline) { | |
| 5669 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (!pipeline) { |
| 5670 | ✗ | return 0; | |
| 5671 | } | ||
| 5672 |
3/6✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
|
4 | GstElement* pwsrc = gst_bin_get_by_name(GST_BIN(pipeline), "pwsrc"); |
| 5673 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (!pwsrc) { |
| 5674 | ✗ | return 0; | |
| 5675 | } | ||
| 5676 | 4 | int rate = 0; | |
| 5677 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | GstPad* pad = gst_element_get_static_pad(pwsrc, "src"); |
| 5678 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (pad) { |
| 5679 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | GstCaps* caps = gst_pad_get_current_caps(pad); |
| 5680 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (caps) { |
| 5681 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | const GstStructure* st = gst_caps_get_structure(caps, 0); |
| 5682 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (st) { |
| 5683 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | gst_structure_get_int(st, "rate", &rate); |
| 5684 | } | ||
| 5685 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | gst_caps_unref(caps); |
| 5686 | } | ||
| 5687 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | gst_object_unref(pad); |
| 5688 | } | ||
| 5689 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | gst_object_unref(pwsrc); |
| 5690 | 4 | return rate; | |
| 5691 | } | ||
| 5692 | |||
| 5693 | 824 | AES67Manager::Status AES67Manager::GetStatus() { | |
| 5694 | 824 | Status status; | |
| 5695 | |||
| 5696 | // Snapshot the config BEFORE taking the pipeline lock, and use the copy | ||
| 5697 | // from here on. Reading m_config directly would race LoadConfig(), which | ||
| 5698 | // reallocates the instance vector on another thread -- and taking the two | ||
| 5699 | // locks together would create an ordering to get wrong. See m_configMutex. | ||
| 5700 |
1/2✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
|
824 | AES67Config config = GetConfigSnapshot(); |
| 5701 | 824 | status.ptpEnabled = config.ptpEnabled; | |
| 5702 | 824 | status.ptpDomain = config.ptpDomain; | |
| 5703 |
1/2✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
|
824 | status.ptpRole = config.ptpRole; |
| 5704 |
1/2✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
|
824 | status.graphSampleRate = PipeWireGraphRate(); |
| 5705 | |||
| 5706 | // Pipeline status — use try_lock to avoid blocking HTTP handlers | ||
| 5707 | // indefinitely if another thread holds m_pipelineMutex during a | ||
| 5708 | // long GStreamer state change. | ||
| 5709 | { | ||
| 5710 | 824 | std::unique_lock<std::mutex> lock(m_pipelineMutex, std::try_to_lock); | |
| 5711 |
2/2✓ Branch 0 taken 823 times.
✓ Branch 1 taken 1 times.
|
824 | if (lock.owns_lock()) { |
| 5712 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 823 times.
|
827 | for (const auto& [id, p] : m_sendPipelines) { |
| 5713 | 4 | Status::PipelineStatus ps; | |
| 5714 | 4 | ps.instanceId = id; | |
| 5715 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | ps.mode = "send"; |
| 5716 | 4 | ps.running = p.running; | |
| 5717 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | ps.error = p.errorMessage; |
| 5718 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | ps.sourceRate = NegotiatedSourceRate(p.pipeline); |
| 5719 | |||
| 5720 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | for (const auto& inst : config.instances) { |
| 5721 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (inst.id == id) { |
| 5722 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | ps.name = inst.name; |
| 5723 | 4 | break; | |
| 5724 | } | ||
| 5725 | } | ||
| 5726 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | status.pipelines.push_back(ps); |
| 5727 | 4 | } | |
| 5728 | |||
| 5729 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 823 times.
|
827 | for (const auto& [id, p] : m_recvPipelines) { |
| 5730 | 4 | Status::PipelineStatus ps; | |
| 5731 | 4 | ps.instanceId = id; | |
| 5732 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | ps.mode = "receive"; |
| 5733 | 4 | ps.running = p.running; | |
| 5734 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | ps.error = p.errorMessage; |
| 5735 | |||
| 5736 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | for (const auto& inst : config.instances) { |
| 5737 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 | if (inst.id == id) { |
| 5738 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | ps.name = inst.name; |
| 5739 | 4 | break; | |
| 5740 | } | ||
| 5741 | } | ||
| 5742 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | status.pipelines.push_back(ps); |
| 5743 | 4 | } | |
| 5744 | |||
| 5745 | // Senders deliberately held idle. Reported alongside the real | ||
| 5746 | // pipelines rather than omitted: a stream the user enabled and | ||
| 5747 | // cannot see anywhere reads as FPP having lost the config. | ||
| 5748 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 823 times.
|
824 | for (const auto& [id, reason] : m_deferredSenders) { |
| 5749 | 1 | Status::PipelineStatus ps; | |
| 5750 | 1 | ps.instanceId = id; | |
| 5751 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | ps.mode = "send"; |
| 5752 | 1 | ps.running = false; | |
| 5753 | 1 | ps.waitingForSource = true; | |
| 5754 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | ps.note = reason; |
| 5755 | |||
| 5756 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | for (const auto& inst : config.instances) { |
| 5757 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (inst.id == id) { |
| 5758 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | ps.name = inst.name; |
| 5759 | 1 | break; | |
| 5760 | } | ||
| 5761 | } | ||
| 5762 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | status.pipelines.push_back(ps); |
| 5763 | 1 | } | |
| 5764 | } else { | ||
| 5765 | // Could not acquire lock — return partial status | ||
| 5766 | 1 | Status::PipelineStatus ps; | |
| 5767 | 1 | ps.instanceId = -1; | |
| 5768 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | ps.name = "(status unavailable — pipeline operation in progress)"; |
| 5769 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | ps.mode = "unknown"; |
| 5770 | 1 | ps.running = false; | |
| 5771 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | status.pipelines.push_back(ps); |
| 5772 | 1 | } | |
| 5773 | 824 | } | |
| 5774 | // PTP status — query ptp4l for the actual selected grandmaster (may be a | ||
| 5775 | // remote/upstream clock) rather than assuming this node is the master. | ||
| 5776 | { | ||
| 5777 | 824 | bool gmPresent = false; | |
| 5778 | 824 | std::string gmId; | |
| 5779 | 824 | int64_t offsetNs = 0; | |
| 5780 | 824 | bool offsetValid = false; | |
| 5781 | |||
| 5782 |
7/12✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 136 times.
✓ Branch 3 taken 688 times.
✓ Branch 4 taken 136 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 688 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 688 times.
✓ Branch 9 taken 136 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
1512 | status.ptpPortState = IsPtp4lRunning() ? GetPtp4lState() : "not running"; |
| 5783 |
1/2✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
|
824 | status.ptpIsGrandmaster = IsGrandmasterPortState(status.ptpPortState); |
| 5784 | 824 | status.ptpLockedAtStart = m_ptpLockedAtStart.load(); | |
| 5785 |
1/2✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
|
824 | status.ptpFreeRunning = PtpFreeRunning(); |
| 5786 | |||
| 5787 |
8/10✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 136 times.
✓ Branch 3 taken 688 times.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 124 times.
✓ Branch 6 taken 12 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 12 times.
✓ Branch 9 taken 812 times.
|
824 | if (QueryPtp4lTimeStatus(gmPresent, gmId, offsetNs, &offsetValid) && gmPresent && !gmId.empty()) { |
| 5788 | // A free-running follower has a grandmaster but does not follow | ||
| 5789 | // it: its offset is how far apart the two clocks are, not a lock. | ||
| 5790 | // Nor is a grandmaster being known one: a follower that hears | ||
| 5791 | // Announce messages but cannot time Sync sits in UNCALIBRATED with | ||
| 5792 | // gmPresent true and an offset of 0 indefinitely. Only a SLAVE | ||
| 5793 | // port with a measured offset is following anything. | ||
| 5794 |
7/8✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 5 times.
✓ Branch 7 taken 3 times.
|
12 | status.ptpSynced = !status.ptpFreeRunning && status.ptpPortState == "SLAVE" && offsetValid; |
| 5795 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | status.ptpGrandmasterId = gmId; |
| 5796 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3 times.
|
12 | status.ptpOffsetNs = offsetValid ? offsetNs : 0; |
| 5797 | 12 | status.ptpOffsetValid = offsetValid; | |
| 5798 | 12 | GetGrandmasterAddress(gmId, status.ptpGrandmasterAddress, | |
| 5799 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | status.ptpGrandmasterViaBoundary); |
| 5800 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 809 times.
|
812 | } else if (status.ptpIsGrandmaster) { |
| 5801 | // We won the BMCA and ARE the domain grandmaster. pmc reports | ||
| 5802 | // gmPresent=false in that case because there is no *remote* GM to | ||
| 5803 | // report -- which is not the same thing as "unsynced". Being the | ||
| 5804 | // clock source is a legitimate AES67 configuration (FPP driving | ||
| 5805 | // the show clock), so report ourselves as the grandmaster with a | ||
| 5806 | // zero offset rather than showing the user a broken PTP status. | ||
| 5807 | 3 | status.ptpSynced = true; | |
| 5808 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | status.ptpGrandmasterId = GetPTPClockId(); |
| 5809 | 3 | status.ptpOffsetNs = 0; | |
| 5810 | 3 | status.ptpOffsetValid = true; | |
| 5811 | // Taken from the interface rather than the Announce listener: we | ||
| 5812 | // are the one clock whose address is known without hearing it. | ||
| 5813 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | status.ptpGrandmasterAddress = GetInterfaceIP(GetPtpInterface()); |
| 5814 | } else { | ||
| 5815 | // ptp4l not running, or no grandmaster selected yet (e.g. still | ||
| 5816 | // in LISTENING/PRE_MASTER) — do not claim we're synced or report | ||
| 5817 | // our own identity as if it were the grandmaster. | ||
| 5818 | 809 | status.ptpSynced = false; | |
| 5819 |
1/2✓ Branch 0 taken 809 times.
✗ Branch 1 not taken.
|
809 | status.ptpGrandmasterId = ""; |
| 5820 | 809 | status.ptpOffsetNs = 0; | |
| 5821 | 809 | status.ptpOffsetValid = false; | |
| 5822 | } | ||
| 5823 | 824 | } | |
| 5824 | |||
| 5825 | // Discovered streams | ||
| 5826 | { | ||
| 5827 |
1/2✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
|
824 | std::lock_guard<std::mutex> lock(m_discoveredMutex); |
| 5828 |
2/2✓ Branch 0 taken 259 times.
✓ Branch 1 taken 824 times.
|
1083 | for (const auto& [key, stream] : m_discoveredStreams) { |
| 5829 |
1/2✓ Branch 0 taken 259 times.
✗ Branch 1 not taken.
|
259 | status.discoveredStreams.push_back(stream); |
| 5830 | } | ||
| 5831 | 824 | } | |
| 5832 | |||
| 5833 | 824 | return status; | |
| 5834 | 824 | } | |
| 5835 | |||
| 5836 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 5837 | // Self-test — validates AES67 subsystem components (7.10) | ||
| 5838 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 5839 | 4 | std::vector<AES67Manager::TestResult> AES67Manager::RunSelfTest() { | |
| 5840 | // Snapshot once -- this runs on an HTTP/command thread and would otherwise | ||
| 5841 | // read m_config while ApplyConfig() reloads it. See m_configMutex. | ||
| 5842 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | AES67Config config = GetConfigSnapshot(); |
| 5843 | 4 | std::vector<TestResult> results; | |
| 5844 | |||
| 5845 | // Test 1: GStreamer initialization | ||
| 5846 | { | ||
| 5847 | 4 | TestResult r; | |
| 5848 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | r.testName = "gstreamer_init"; |
| 5849 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | r.passed = gst_is_initialized(); |
| 5850 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
4 | r.message = r.passed ? "GStreamer is initialized" : "GStreamer is NOT initialized"; |
| 5851 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | results.push_back(r); |
| 5852 | 4 | } | |
| 5853 | |||
| 5854 | // Test 2: Required GStreamer elements available | ||
| 5855 | { | ||
| 5856 | 4 | const char* requiredElements[] = { | |
| 5857 | "rtpL24pay", "rtpL24depay", "rtpjitterbuffer", | ||
| 5858 | "udpsrc", "udpsink", "audioconvert", "audioresample", | ||
| 5859 | "pipewiresrc", "pipewiresink", nullptr | ||
| 5860 | }; | ||
| 5861 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 4 times.
|
40 | for (int i = 0; requiredElements[i]; i++) { |
| 5862 | 36 | TestResult r; | |
| 5863 |
2/4✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
|
72 | r.testName = std::string("element_") + requiredElements[i]; |
| 5864 |
1/2✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
|
36 | GstElementFactory* factory = gst_element_factory_find(requiredElements[i]); |
| 5865 | 36 | r.passed = (factory != nullptr); | |
| 5866 | 36 | r.message = r.passed | |
| 5867 |
4/14✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 36 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 36 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
|
144 | ? std::string(requiredElements[i]) + " element available" |
| 5868 |
3/12✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 36 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 36 times.
✓ Branch 6 taken 36 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
72 | : std::string(requiredElements[i]) + " element NOT FOUND"; |
| 5869 |
2/4✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
|
36 | if (factory) gst_object_unref(factory); |
| 5870 |
1/2✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
|
36 | results.push_back(r); |
| 5871 | 36 | } | |
| 5872 | } | ||
| 5873 | |||
| 5874 | // Test 3: PTP daemon (ptp4l) | ||
| 5875 | { | ||
| 5876 | 4 | TestResult r; | |
| 5877 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | r.testName = "ptp_initialized"; |
| 5878 | 4 | r.passed = m_ptpInitialized; | |
| 5879 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
4 | r.message = r.passed ? "PTP subsystem initialized" : "PTP subsystem not initialized"; |
| 5880 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | results.push_back(r); |
| 5881 | 4 | } | |
| 5882 | { | ||
| 5883 | 4 | TestResult r; | |
| 5884 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | r.testName = "ptp4l_running"; |
| 5885 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | r.passed = IsPtp4lRunning(); |
| 5886 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | std::string state = GetPtp4lState(); |
| 5887 | 4 | r.message = r.passed | |
| 5888 |
4/20✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 4 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 4 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
|
16 | ? "ptp4l is running (PID " + std::to_string(m_ptp4lPid) + ") — " + state |
| 5889 |
2/6✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
12 | : "ptp4l is NOT running"; |
| 5890 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | results.push_back(r); |
| 5891 | 4 | } | |
| 5892 | { | ||
| 5893 | 4 | TestResult r; | |
| 5894 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | r.testName = "ptp4l_binary"; |
| 5895 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | r.passed = FileExists(PTP4L_BINARY); |
| 5896 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
4 | r.message = r.passed ? "ptp4l binary found at /usr/sbin/ptp4l" : "ptp4l binary NOT found — install linuxptp package"; |
| 5897 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | results.push_back(r); |
| 5898 | 4 | } | |
| 5899 | { | ||
| 5900 | // Without pmc every grandmaster query fails, and PTP silently reports | ||
| 5901 | // "not synced" no matter how well it is actually locked. | ||
| 5902 | 4 | TestResult r; | |
| 5903 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | r.testName = "pmc_binary"; |
| 5904 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | r.passed = FileExists(PMC_BINARY); |
| 5905 | 4 | r.message = r.passed | |
| 5906 |
3/14✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 4 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
|
16 | ? std::string("pmc binary found at ") + PMC_BINARY |
| 5907 |
6/18✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 4 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
|
16 | : std::string("pmc binary NOT found at ") + PMC_BINARY + |
| 5908 | 4 | " — grandmaster status cannot be queried (install linuxptp)"; | |
| 5909 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | results.push_back(r); |
| 5910 | 4 | } | |
| 5911 | { | ||
| 5912 | // Surfaces the two settings behind "why did my Pi become the master?" | ||
| 5913 | // and "why does the Q-SYS core not see us?". | ||
| 5914 | 4 | TestResult r; | |
| 5915 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | r.testName = "ptp_grandmaster"; |
| 5916 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | std::string gm = GetActiveGrandmasterId(); |
| 5917 |
4/12✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
8 | std::string state = IsPtp4lRunning() ? GetPtp4lState() : "not running"; |
| 5918 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
4 | r.passed = !config.ptpEnabled || !gm.empty(); |
| 5919 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (!config.ptpEnabled) { |
| 5920 | ✗ | r.message = "PTP is disabled — SDP advertises this node's own clock identity"; | |
| 5921 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | } else if (gm.empty()) { |
| 5922 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
8 | r.message = "No grandmaster selected yet (port state " + state + ") — domain " + |
| 5923 |
3/6✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
|
12 | std::to_string(config.ptpDomain) + ", role " + config.ptpRole; |
| 5924 | } else { | ||
| 5925 | ✗ | std::string where; | |
| 5926 | ✗ | std::string gmAddr; | |
| 5927 | ✗ | bool viaBoundary = false; | |
| 5928 | ✗ | if (IsGrandmasterPortState(state)) { | |
| 5929 | ✗ | where = " at " + GetInterfaceIP(config.ptpInterface); | |
| 5930 | ✗ | } else if (GetGrandmasterAddress(gm, gmAddr, viaBoundary)) { | |
| 5931 | ✗ | where = (viaBoundary ? " via boundary clock " : " at ") + gmAddr; | |
| 5932 | } | ||
| 5933 | ✗ | r.message = "Grandmaster " + gm + where + " (port state " + state + ") — domain " + | |
| 5934 | ✗ | std::to_string(config.ptpDomain) + ", role " + config.ptpRole + | |
| 5935 | ✗ | (IsGrandmasterPortState(state) ? " — this node holds the role" : ""); | |
| 5936 | ✗ | } | |
| 5937 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | results.push_back(r); |
| 5938 | 4 | } | |
| 5939 | |||
| 5940 | // Test 4: Config file | ||
| 5941 | { | ||
| 5942 | 4 | TestResult r; | |
| 5943 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | r.testName = "config_file"; |
| 5944 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | std::ifstream test(m_configPath); |
| 5945 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | r.passed = test.good(); |
| 5946 | 4 | r.message = r.passed | |
| 5947 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
8 | ? "Config file found: " + m_configPath |
| 5948 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
4 | : "Config file missing: " + m_configPath; |
| 5949 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | results.push_back(r); |
| 5950 | 4 | } | |
| 5951 | |||
| 5952 | // Test 5: Config loaded and has instances | ||
| 5953 | { | ||
| 5954 | 4 | TestResult r; | |
| 5955 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | r.testName = "config_instances"; |
| 5956 | 4 | r.passed = !config.instances.empty(); | |
| 5957 | 4 | r.message = r.passed | |
| 5958 |
5/10✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
|
12 | ? std::to_string(config.instances.size()) + " instance(s) configured" |
| 5959 |
4/6✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
11 | : "No instances configured"; |
| 5960 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | results.push_back(r); |
| 5961 | 4 | } | |
| 5962 | |||
| 5963 | // Test 6: Network interface availability | ||
| 5964 | { | ||
| 5965 | 4 | TestResult r; | |
| 5966 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | r.testName = "network_interface"; |
| 5967 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | std::string ip = GetInterfaceIP(config.ptpInterface); |
| 5968 | 4 | r.passed = !ip.empty(); | |
| 5969 | 4 | r.message = r.passed | |
| 5970 |
5/16✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 4 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
|
12 | ? "Interface " + config.ptpInterface + " has IP: " + ip |
| 5971 |
2/8✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
8 | : "Interface " + config.ptpInterface + " not found or has no IP"; |
| 5972 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | results.push_back(r); |
| 5973 | 4 | } | |
| 5974 | |||
| 5975 | // Test 7: PTP clock ID derivation (EUI-64 from MAC) | ||
| 5976 | { | ||
| 5977 | 4 | TestResult r; | |
| 5978 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | r.testName = "ptp_clock_id"; |
| 5979 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | std::string clockId = GetPTPClockId(); |
| 5980 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
4 | r.passed = !clockId.empty() && clockId.length() == 23; // XX-XX-XX-XX-XX-XX-XX-XX |
| 5981 | 4 | r.message = r.passed | |
| 5982 |
2/6✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
8 | ? "PTP Clock ID: " + clockId |
| 5983 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
8 | : "Could not derive PTP Clock ID from MAC address"; |
| 5984 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | results.push_back(r); |
| 5985 | 4 | } | |
| 5986 | |||
| 5987 | // Test 8: Send pipeline status | ||
| 5988 | { | ||
| 5989 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | std::lock_guard<std::mutex> lock(m_pipelineMutex); |
| 5990 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4 times.
|
5 | for (const auto& [id, p] : m_sendPipelines) { |
| 5991 | 1 | TestResult r; | |
| 5992 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | r.testName = "send_pipeline_" + std::to_string(id); |
| 5993 | 1 | r.passed = p.running; | |
| 5994 | 1 | r.message = p.running | |
| 5995 |
4/14✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
|
3 | ? "Send pipeline " + std::to_string(id) + " is running" |
| 5996 |
4/20✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
|
2 | : "Send pipeline " + std::to_string(id) + " is NOT running: " + p.errorMessage; |
| 5997 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | results.push_back(r); |
| 5998 | 1 | } | |
| 5999 | |||
| 6000 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4 times.
|
5 | for (const auto& [id, p] : m_recvPipelines) { |
| 6001 | 1 | TestResult r; | |
| 6002 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | r.testName = "recv_pipeline_" + std::to_string(id); |
| 6003 | 1 | r.passed = p.running; | |
| 6004 | 1 | r.message = p.running | |
| 6005 |
4/14✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
|
3 | ? "Receive pipeline " + std::to_string(id) + " is running" |
| 6006 |
4/20✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
|
2 | : "Receive pipeline " + std::to_string(id) + " is NOT running: " + p.errorMessage; |
| 6007 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | results.push_back(r); |
| 6008 | 1 | } | |
| 6009 | |||
| 6010 | // Held senders have no pipeline to report on, and a configured stream | ||
| 6011 | // that appears in no test at all reads as a lost config. Passing: | ||
| 6012 | // this is the state FPP intends for a sender nothing feeds, not a | ||
| 6013 | // fault to chase. | ||
| 6014 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | for (const auto& [id, reason] : m_deferredSenders) { |
| 6015 | ✗ | TestResult r; | |
| 6016 | ✗ | r.testName = "send_pipeline_" + std::to_string(id); | |
| 6017 | ✗ | r.passed = true; | |
| 6018 | ✗ | r.message = "Send stream " + std::to_string(id) + " is idle: " + reason; | |
| 6019 | ✗ | results.push_back(r); | |
| 6020 | ✗ | } | |
| 6021 | 4 | } | |
| 6022 | |||
| 6023 | // Test 9: SAP announcer running | ||
| 6024 | { | ||
| 6025 | // Nothing to announce is not a failure. The announcer only runs when | ||
| 6026 | // a sender is actually on the wire, so a receive-only box -- or one | ||
| 6027 | // whose senders are all held idle waiting for an Audio Output Group -- | ||
| 6028 | // correctly has no announcer thread, and reporting that as a failed | ||
| 6029 | // test sends the user looking for a fault that is not there. | ||
| 6030 | 4 | bool anySendRunning = false; | |
| 6031 | { | ||
| 6032 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | std::lock_guard<std::mutex> lock(m_pipelineMutex); |
| 6033 | 4 | anySendRunning = !m_sendPipelines.empty(); | |
| 6034 | 4 | } | |
| 6035 | 4 | const bool running = m_sapAnnounceRunning.load(); | |
| 6036 | |||
| 6037 | 4 | TestResult r; | |
| 6038 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | r.testName = "sap_announcer"; |
| 6039 |
3/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1 times.
|
4 | r.passed = running || !anySendRunning; |
| 6040 | r.message = running | ||
| 6041 | ? "SAP announcer thread running" | ||
| 6042 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
|
4 | : (anySendRunning ? "SAP announcer thread not running" |
| 6043 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
8 | : "No SAP announcer needed — no send stream is running"); |
| 6044 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | results.push_back(r); |
| 6045 | 4 | } | |
| 6046 | |||
| 6047 | // Test 10: SAP receiver running | ||
| 6048 | { | ||
| 6049 | 4 | TestResult r; | |
| 6050 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | r.testName = "sap_receiver"; |
| 6051 | 4 | r.passed = m_sapRecvRunning.load(); | |
| 6052 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
4 | r.message = r.passed ? "SAP receiver thread running" : "SAP receiver thread not running"; |
| 6053 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | results.push_back(r); |
| 6054 | 4 | } | |
| 6055 | |||
| 6056 | // Test 11: Multicast socket test — try binding to SAP port (quick check) | ||
| 6057 | { | ||
| 6058 | 4 | TestResult r; | |
| 6059 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | r.testName = "multicast_capability"; |
| 6060 | 4 | int sock = OpenSocket(AF_INET, SOCK_DGRAM, 0); | |
| 6061 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (sock >= 0) { |
| 6062 | 4 | int reuse = 1; | |
| 6063 | 4 | setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)); | |
| 6064 | 4 | struct sockaddr_in addr{}; | |
| 6065 | 4 | addr.sin_family = AF_INET; | |
| 6066 | 4 | addr.sin_port = 0; // any port | |
| 6067 | 4 | addr.sin_addr.s_addr = INADDR_ANY; | |
| 6068 | 4 | r.passed = (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == 0); | |
| 6069 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
4 | r.message = r.passed ? "UDP socket creation and bind OK" : "Failed to create/bind UDP socket"; |
| 6070 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | close(sock); |
| 6071 | } else { | ||
| 6072 | ✗ | r.passed = false; | |
| 6073 | ✗ | r.message = "Failed to create UDP socket"; | |
| 6074 | } | ||
| 6075 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | results.push_back(r); |
| 6076 | 4 | } | |
| 6077 | |||
| 6078 | // Test 12: SDP generation test — verify we can build a valid SDP | ||
| 6079 | { | ||
| 6080 | 4 | TestResult r; | |
| 6081 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | r.testName = "sdp_generation"; |
| 6082 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
|
4 | if (!config.instances.empty()) { |
| 6083 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | std::string sourceIP = GetInterfaceIP(config.ptpInterface); |
| 6084 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | std::string clockId = GetPTPClockId(); |
| 6085 | // Version 0: this is a rendering for the test output, not an | ||
| 6086 | // announcement, and must not advance the announced version. | ||
| 6087 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | std::string sdp = BuildSDP(config.instances[0], sourceIP, clockId, 0); |
| 6088 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
2 | r.passed = !sdp.empty() && sdp.find("v=0") != std::string::npos |
| 6089 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | && sdp.find("ts-refclk") != std::string::npos |
| 6090 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | && sdp.find("mediaclk") != std::string::npos |
| 6091 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | && sdp.find("L24") != std::string::npos; |
| 6092 | 1 | r.message = r.passed | |
| 6093 |
5/16✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
|
3 | ? "SDP generation OK (" + std::to_string(sdp.size()) + " bytes)" |
| 6094 |
2/6✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
2 | : "SDP generation failed or incomplete"; |
| 6095 | 1 | } else { | |
| 6096 | 3 | r.passed = false; | |
| 6097 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | r.message = "No instances configured — cannot test SDP generation"; |
| 6098 | } | ||
| 6099 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | results.push_back(r); |
| 6100 | 4 | } | |
| 6101 | |||
| 6102 | 4 | return results; | |
| 6103 | 4 | } | |
| 6104 | |||
| 6105 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 6106 | // HTTP API endpoint — registered at /aes67 | ||
| 6107 | // ────────────────────────────────────────────────────────────────────────────── | ||
| 6108 | 1442 | HttpResponsePtr AES67Manager::render_GET(const HttpRequestPtr& req) { | |
| 6109 | |||
| 6110 |
2/4✓ Branch 0 taken 1442 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1442 times.
✗ Branch 3 not taken.
|
1442 | std::string url(req->path()); |
| 6111 | |||
| 6112 | // Strip leading /aes67/ | ||
| 6113 |
2/2✓ Branch 0 taken 1437 times.
✓ Branch 1 taken 5 times.
|
1442 | if (url.find("/aes67/") == 0) { |
| 6114 |
1/2✓ Branch 0 taken 1437 times.
✗ Branch 1 not taken.
|
1437 | url = url.substr(7); |
| 6115 |
2/4✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
5 | } else if (url == "/aes67") { |
| 6116 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | url = "status"; |
| 6117 | } | ||
| 6118 | |||
| 6119 |
3/4✓ Branch 0 taken 1442 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 824 times.
✓ Branch 3 taken 618 times.
|
1442 | if (url == "status") { |
| 6120 |
1/2✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
|
824 | Status st = GetStatus(); |
| 6121 |
1/2✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
|
824 | Json::Value result; |
| 6122 | |||
| 6123 | // Pipelines | ||
| 6124 |
1/2✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
|
824 | Json::Value pipelines(Json::arrayValue); |
| 6125 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 824 times.
|
834 | for (const auto& p : st.pipelines) { |
| 6126 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | Json::Value pj; |
| 6127 |
2/4✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
|
10 | pj["instanceId"] = p.instanceId; |
| 6128 |
2/4✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
|
10 | pj["name"] = p.name; |
| 6129 |
2/4✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
|
10 | pj["mode"] = p.mode; |
| 6130 |
2/4✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
|
10 | pj["running"] = p.running; |
| 6131 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6 times.
|
10 | if (p.sourceRate > 0) { |
| 6132 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
4 | pj["sourceRate"] = p.sourceRate; |
| 6133 | } | ||
| 6134 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (!p.error.empty()) { |
| 6135 | ✗ | pj["error"] = p.error; | |
| 6136 | } | ||
| 6137 | // Distinct from "error" on purpose: the UI renders a failure in | ||
| 6138 | // red and this as guidance, so collapsing the two would put a | ||
| 6139 | // brand new instance back in the alarming state this replaced. | ||
| 6140 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9 times.
|
10 | if (p.waitingForSource) { |
| 6141 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | pj["waitingForSource"] = true; |
| 6142 | } | ||
| 6143 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9 times.
|
10 | if (!p.note.empty()) { |
| 6144 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | pj["note"] = p.note; |
| 6145 | } | ||
| 6146 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | pipelines.append(pj); |
| 6147 | 10 | } | |
| 6148 |
2/4✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 824 times.
✗ Branch 3 not taken.
|
824 | result["pipelines"] = pipelines; |
| 6149 | |||
| 6150 | // PTP | ||
| 6151 |
1/2✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
|
824 | Json::Value ptp; |
| 6152 |
2/4✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 824 times.
✗ Branch 3 not taken.
|
824 | ptp["synced"] = st.ptpSynced; |
| 6153 | // null, not 0, when pmc gave nothing usable -- a zero here is the | ||
| 6154 | // same lie WaitForPtpLock() used to believe. The UI already tests | ||
| 6155 | // `typeof offsetNs === 'number'` and falls back to an em dash. | ||
| 6156 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 812 times.
|
824 | if (st.ptpOffsetValid) { |
| 6157 |
2/4✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
|
12 | ptp["offsetNs"] = (Json::Int64)st.ptpOffsetNs; |
| 6158 | } else { | ||
| 6159 |
2/4✓ Branch 0 taken 812 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 812 times.
✗ Branch 3 not taken.
|
812 | ptp["offsetNs"] = Json::Value::null; |
| 6160 | } | ||
| 6161 |
2/4✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 824 times.
✗ Branch 3 not taken.
|
824 | ptp["grandmasterId"] = st.ptpGrandmasterId; |
| 6162 |
2/4✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 824 times.
✗ Branch 3 not taken.
|
824 | ptp["grandmasterAddress"] = st.ptpGrandmasterAddress; |
| 6163 |
2/4✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 824 times.
✗ Branch 3 not taken.
|
824 | ptp["grandmasterViaBoundary"] = st.ptpGrandmasterViaBoundary; |
| 6164 |
2/4✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 824 times.
✗ Branch 3 not taken.
|
824 | ptp["portState"] = st.ptpPortState; |
| 6165 |
2/4✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 824 times.
✗ Branch 3 not taken.
|
824 | ptp["isGrandmaster"] = st.ptpIsGrandmaster; |
| 6166 |
2/4✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 824 times.
✗ Branch 3 not taken.
|
824 | ptp["lockedAtStart"] = st.ptpLockedAtStart; |
| 6167 |
2/4✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 824 times.
✗ Branch 3 not taken.
|
824 | ptp["freeRunning"] = st.ptpFreeRunning; |
| 6168 |
2/4✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 824 times.
✗ Branch 3 not taken.
|
824 | ptp["enabled"] = st.ptpEnabled; |
| 6169 |
2/4✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 824 times.
✗ Branch 3 not taken.
|
824 | ptp["domain"] = st.ptpDomain; |
| 6170 |
2/4✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 824 times.
✗ Branch 3 not taken.
|
824 | ptp["role"] = st.ptpRole; |
| 6171 |
2/4✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 824 times.
✗ Branch 3 not taken.
|
824 | result["ptp"] = ptp; |
| 6172 | |||
| 6173 | // Discovered streams | ||
| 6174 |
1/2✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
|
824 | Json::Value discovered(Json::arrayValue); |
| 6175 |
2/2✓ Branch 0 taken 259 times.
✓ Branch 1 taken 824 times.
|
1083 | for (const auto& s : st.discoveredStreams) { |
| 6176 |
1/2✓ Branch 0 taken 259 times.
✗ Branch 1 not taken.
|
259 | Json::Value sj; |
| 6177 |
2/4✓ Branch 0 taken 259 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 259 times.
✗ Branch 3 not taken.
|
259 | sj["sessionName"] = s.sessionName; |
| 6178 |
2/4✓ Branch 0 taken 259 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 259 times.
✗ Branch 3 not taken.
|
259 | sj["originAddress"] = s.originAddress; |
| 6179 |
2/4✓ Branch 0 taken 259 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 259 times.
✗ Branch 3 not taken.
|
259 | sj["multicastIP"] = s.multicastIP; |
| 6180 |
2/4✓ Branch 0 taken 259 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 259 times.
✗ Branch 3 not taken.
|
259 | sj["port"] = s.port; |
| 6181 |
2/4✓ Branch 0 taken 259 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 259 times.
✗ Branch 3 not taken.
|
259 | sj["channels"] = s.channels; |
| 6182 |
2/4✓ Branch 0 taken 259 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 259 times.
✗ Branch 3 not taken.
|
259 | sj["ptime"] = s.ptime; |
| 6183 |
2/4✓ Branch 0 taken 259 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 259 times.
✗ Branch 3 not taken.
|
259 | sj["ptpClockId"] = s.ptpClockId; |
| 6184 |
1/2✓ Branch 0 taken 259 times.
✗ Branch 1 not taken.
|
259 | discovered.append(sj); |
| 6185 | 259 | } | |
| 6186 |
2/4✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 824 times.
✗ Branch 3 not taken.
|
824 | result["discoveredStreams"] = discovered; |
| 6187 | |||
| 6188 |
2/4✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 824 times.
✗ Branch 3 not taken.
|
824 | result["graphSampleRate"] = st.graphSampleRate; |
| 6189 |
2/4✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 824 times.
✗ Branch 3 not taken.
|
824 | result["active"] = m_active.load(); |
| 6190 | |||
| 6191 |
1/2✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
|
824 | Json::StreamWriterBuilder wbuilder; |
| 6192 |
3/6✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 824 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 824 times.
✗ Branch 5 not taken.
|
2472 | wbuilder["indentation"] = ""; |
| 6193 |
1/2✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
|
824 | std::string resultStr = Json::writeString(wbuilder, result); |
| 6194 | |||
| 6195 |
2/4✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 824 times.
✗ Branch 3 not taken.
|
824 | return makeStringResponse(resultStr, 200, "application/json"); |
| 6196 | 824 | } | |
| 6197 | |||
| 6198 | // SDP export. SAP only reaches receivers on the same subnet with a tool | ||
| 6199 | // that speaks it; anything else -- Stream Monitor on a laptop, VLC, a | ||
| 6200 | // scope on another VLAN -- needs the session description handed to it as a | ||
| 6201 | // file. "sdp" lists every send instance as JSON for the UI; "sdp/<id>" | ||
| 6202 | // returns one instance as a raw .sdp for curl and direct download. | ||
| 6203 |
8/10✓ Branch 0 taken 618 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 612 times.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✓ Branch 7 taken 3 times.
✓ Branch 8 taken 615 times.
✓ Branch 9 taken 3 times.
|
618 | if (url == "sdp" || url.compare(0, 4, "sdp/") == 0) { |
| 6204 |
1/2✓ Branch 0 taken 615 times.
✗ Branch 1 not taken.
|
615 | AES67Config cfg = GetConfigSnapshot(); |
| 6205 |
1/2✓ Branch 0 taken 615 times.
✗ Branch 1 not taken.
|
615 | bool single = (url != "sdp"); |
| 6206 |
5/8✓ Branch 0 taken 3 times.
✓ Branch 1 taken 612 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 612 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
615 | int wantId = single ? atoi(url.substr(4).c_str()) : 0; |
| 6207 | |||
| 6208 |
1/2✓ Branch 0 taken 615 times.
✗ Branch 1 not taken.
|
615 | Json::Value streams(Json::arrayValue); |
| 6209 | 615 | std::string singleSDP; | |
| 6210 | 615 | std::string singleName; | |
| 6211 |
2/2✓ Branch 0 taken 1565 times.
✓ Branch 1 taken 614 times.
|
2179 | for (const auto& inst : cfg.instances) { |
| 6212 | // Only a sender has an SDP to publish; a receive-only instance is | ||
| 6213 | // described by whoever is transmitting to it. | ||
| 6214 |
7/10✓ Branch 0 taken 1565 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 1559 times.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 6 times.
✓ Branch 9 taken 1559 times.
|
1565 | if (inst.mode != "send" && inst.mode != "both") { |
| 6215 | 10 | continue; | |
| 6216 | } | ||
| 6217 |
4/4✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1554 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 1 times.
|
1559 | if (single && inst.id != wantId) { |
| 6218 | 4 | continue; | |
| 6219 | } | ||
| 6220 |
1/2✓ Branch 0 taken 1555 times.
✗ Branch 1 not taken.
|
1555 | std::string sdp = ExportSDPFor(inst, cfg); |
| 6221 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1554 times.
|
1555 | if (single) { |
| 6222 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | singleSDP = sdp; |
| 6223 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | singleName = SDPFileName(inst); |
| 6224 | 1 | break; | |
| 6225 | } | ||
| 6226 |
1/2✓ Branch 0 taken 1554 times.
✗ Branch 1 not taken.
|
1554 | Json::Value sj; |
| 6227 |
2/4✓ Branch 0 taken 1554 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1554 times.
✗ Branch 3 not taken.
|
1554 | sj["instanceId"] = inst.id; |
| 6228 |
2/4✓ Branch 0 taken 1554 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1554 times.
✗ Branch 3 not taken.
|
1554 | sj["name"] = inst.name; |
| 6229 |
2/4✓ Branch 0 taken 1554 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1554 times.
✗ Branch 3 not taken.
|
1554 | sj["sessionName"] = inst.sessionName; |
| 6230 | // Exported even while disabled or with SAP off -- that is exactly | ||
| 6231 | // when an external monitor is the only way to look at the stream | ||
| 6232 | // -- so say which it is and let the UI warn. | ||
| 6233 |
2/4✓ Branch 0 taken 1554 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1554 times.
✗ Branch 3 not taken.
|
1554 | sj["enabled"] = inst.enabled; |
| 6234 |
2/4✓ Branch 0 taken 1554 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1554 times.
✗ Branch 3 not taken.
|
1554 | sj["sapEnabled"] = inst.sapEnabled; |
| 6235 |
2/4✓ Branch 0 taken 1554 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1554 times.
✗ Branch 3 not taken.
|
1554 | sj["multicastIP"] = inst.multicastIP; |
| 6236 |
2/4✓ Branch 0 taken 1554 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1554 times.
✗ Branch 3 not taken.
|
1554 | sj["port"] = inst.port; |
| 6237 |
2/4✓ Branch 0 taken 1554 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1554 times.
✗ Branch 3 not taken.
|
1554 | sj["channels"] = inst.channels; |
| 6238 |
2/4✓ Branch 0 taken 1554 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1554 times.
✗ Branch 3 not taken.
|
1554 | sj["ptime"] = inst.ptime; |
| 6239 |
3/6✓ Branch 0 taken 1554 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1554 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1554 times.
✗ Branch 5 not taken.
|
1554 | sj["filename"] = SDPFileName(inst); |
| 6240 |
2/4✓ Branch 0 taken 1554 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1554 times.
✗ Branch 3 not taken.
|
1554 | sj["sdp"] = sdp; |
| 6241 |
1/2✓ Branch 0 taken 1554 times.
✗ Branch 1 not taken.
|
1554 | streams.append(sj); |
| 6242 |
2/2✓ Branch 0 taken 1554 times.
✓ Branch 1 taken 1 times.
|
1555 | } |
| 6243 | |||
| 6244 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 612 times.
|
615 | if (single) { |
| 6245 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | if (singleSDP.empty()) { |
| 6246 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
10 | return makeStringResponse("{\"error\":\"no such send instance\"}", |
| 6247 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | 404, "application/json"); |
| 6248 | } | ||
| 6249 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | auto resp = makeStringResponse(singleSDP, 200, "application/sdp"); |
| 6250 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | resp->addHeader("Content-Disposition", |
| 6251 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | "attachment; filename=\"" + singleName + "\""); |
| 6252 | 1 | return resp; | |
| 6253 | 1 | } | |
| 6254 | |||
| 6255 |
1/2✓ Branch 0 taken 612 times.
✗ Branch 1 not taken.
|
612 | Json::Value result; |
| 6256 |
2/4✓ Branch 0 taken 612 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 612 times.
✗ Branch 3 not taken.
|
612 | result["streams"] = streams; |
| 6257 |
1/2✓ Branch 0 taken 612 times.
✗ Branch 1 not taken.
|
612 | Json::StreamWriterBuilder wbuilder; |
| 6258 |
3/6✓ Branch 0 taken 612 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 612 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 612 times.
✗ Branch 5 not taken.
|
1836 | wbuilder["indentation"] = ""; |
| 6259 |
2/4✓ Branch 0 taken 612 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 612 times.
✗ Branch 3 not taken.
|
1224 | return makeStringResponse(Json::writeString(wbuilder, result), 200, |
| 6260 |
1/2✓ Branch 0 taken 612 times.
✗ Branch 1 not taken.
|
612 | "application/json"); |
| 6261 | 615 | } | |
| 6262 | |||
| 6263 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | if (url == "test") { |
| 6264 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | auto tests = RunSelfTest(); |
| 6265 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | Json::Value result; |
| 6266 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | Json::Value testArray(Json::arrayValue); |
| 6267 | 3 | int passed = 0, failed = 0; | |
| 6268 | |||
| 6269 |
2/2✓ Branch 0 taken 71 times.
✓ Branch 1 taken 3 times.
|
74 | for (const auto& t : tests) { |
| 6270 |
1/2✓ Branch 0 taken 71 times.
✗ Branch 1 not taken.
|
71 | Json::Value tj; |
| 6271 |
2/4✓ Branch 0 taken 71 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 71 times.
✗ Branch 3 not taken.
|
71 | tj["test"] = t.testName; |
| 6272 |
2/4✓ Branch 0 taken 71 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 71 times.
✗ Branch 3 not taken.
|
71 | tj["passed"] = t.passed; |
| 6273 |
2/4✓ Branch 0 taken 71 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 71 times.
✗ Branch 3 not taken.
|
71 | tj["message"] = t.message; |
| 6274 |
1/2✓ Branch 0 taken 71 times.
✗ Branch 1 not taken.
|
71 | testArray.append(tj); |
| 6275 |
2/2✓ Branch 0 taken 46 times.
✓ Branch 1 taken 25 times.
|
71 | if (t.passed) passed++; else failed++; |
| 6276 | 71 | } | |
| 6277 | |||
| 6278 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | result["tests"] = testArray; |
| 6279 |
3/6✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
3 | result["summary"]["total"] = (int)tests.size(); |
| 6280 |
3/6✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
3 | result["summary"]["passed"] = passed; |
| 6281 |
3/6✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
3 | result["summary"]["failed"] = failed; |
| 6282 |
3/6✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
3 | result["summary"]["allPassed"] = (failed == 0); |
| 6283 | |||
| 6284 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | Json::StreamWriterBuilder wbuilder; |
| 6285 |
3/6✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
9 | wbuilder["indentation"] = " "; |
| 6286 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | std::string resultStr = Json::writeString(wbuilder, result); |
| 6287 | |||
| 6288 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | return makeStringResponse(resultStr, 200, "application/json"); |
| 6289 | 3 | } | |
| 6290 | |||
| 6291 | ✗ | return makeStringResponse("{\"error\":\"unknown endpoint\"}", 404, "application/json"); | |
| 6292 | 1442 | } | |
| 6293 | |||
| 6294 | #endif // HAS_AES67_GSTREAMER | ||
| 6295 |