| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of the Falcon Player (FPP) and is Copyright (C) | ||
| 3 | * 2013-2026 by the Falcon Player Developers. | ||
| 4 | * | ||
| 5 | * The Falcon Player (FPP) is free software, and is covered under | ||
| 6 | * multiple Open Source licenses. Please see the included 'LICENSES' | ||
| 7 | * file for descriptions of what files are covered by each license. | ||
| 8 | * | ||
| 9 | * This source file is covered under the LGPL v2.1 as described in the | ||
| 10 | * included LICENSE.LGPL file. | ||
| 11 | */ | ||
| 12 | |||
| 13 | #include "fpp-pch.h" | ||
| 14 | |||
| 15 | #include "fpp-json.h" | ||
| 16 | #include "log.h" | ||
| 17 | #include "common_mini.h" | ||
| 18 | #include "Timers.h" | ||
| 19 | |||
| 20 | #include "StreamSlotManager.h" | ||
| 21 | #include "GStreamerOut.h" | ||
| 22 | #include "mediaoutput.h" | ||
| 23 | #include "common.h" | ||
| 24 | #include "settings.h" | ||
| 25 | |||
| 26 | // The volume the user last set for a slot, from pipewire-stream-slots.json. | ||
| 27 | // Slot volumes apply to a stream that only exists while that slot is playing, | ||
| 28 | // so they cannot be restored at boot the way group volumes are -- they have to | ||
| 29 | // be reapplied each time the slot starts instead. | ||
| 30 | // | ||
| 31 | // All five slots are treated alike. Slot 1 used to be excluded because its | ||
| 32 | // fader was wired to the global master rather than to the stream, which made it | ||
| 33 | // a duplicate master control instead of a slot of its own. | ||
| 34 | // | ||
| 35 | // KEEP IN SYNC with SaveStreamSlotVolume()/GetStreamSlotVolumes() in | ||
| 36 | // www/api/controllers/pipewire.php. | ||
| 37 | 2049 | static int savedStreamSlotVolume(int slot) { | |
| 38 |
2/4✓ Branch 0 taken 2049 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2049 times.
|
2049 | if (slot < 1 || slot > 5) { |
| 39 | ✗ | return -1; | |
| 40 | } | ||
| 41 | // The file only exists once a slot fader has been moved, so its absence is | ||
| 42 | // the normal case -- don't let LoadJsonFromFile() log it as an error. | ||
| 43 |
3/6✓ Branch 0 taken 2049 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2049 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2049 times.
✗ Branch 5 not taken.
|
2049 | std::string file = FPP_DIR_CONFIG("/pipewire-stream-slots.json"); |
| 44 |
1/2✓ Branch 0 taken 2049 times.
✗ Branch 1 not taken.
|
2049 | Json::Value root; |
| 45 | // Shape-checked, not trusted: a list root, a non-object "slots" or a | ||
| 46 | // volume saved as a string threw from here on every stream start. | ||
| 47 |
8/10✓ Branch 0 taken 2049 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 2046 times.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 2047 times.
✓ Branch 9 taken 2 times.
|
2051 | if (!FileExists(file) || !LoadJsonFromFile(file, root, JsonRoot::Object) || |
| 48 |
3/6✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
|
2 | !root["slots"].isObject()) { |
| 49 | 2047 | return -1; | |
| 50 | } | ||
| 51 | 2 | std::string key = std::to_string(slot); | |
| 52 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | const Json::Value& vol = root["slots"][key]; |
| 53 |
3/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
|
2 | if (!vol.isIntegral()) { |
| 54 | 1 | return -1; | |
| 55 | } | ||
| 56 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | int v = vol.asInt(); |
| 57 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if (v < 0 || v > 100) { |
| 58 | ✗ | return -1; | |
| 59 | } | ||
| 60 | 1 | return v; | |
| 61 | 2049 | } | |
| 62 | |||
| 63 | 2612 | StreamSlotManager::StreamSlotManager() { | |
| 64 |
2/2✓ Branch 0 taken 13060 times.
✓ Branch 1 taken 2612 times.
|
15672 | for (int i = 0; i < MAX_SLOTS; i++) { |
| 65 |
1/2✓ Branch 0 taken 13060 times.
✗ Branch 1 not taken.
|
13060 | m_slots[i].status = {}; |
| 66 | 13060 | m_slots[i].status.status = MEDIAOUTPUTSTATUS_IDLE; | |
| 67 | 13060 | m_slots[i].activeOutput = nullptr; | |
| 68 | 13060 | m_slots[i].isBackground = false; | |
| 69 | } | ||
| 70 | 2612 | } | |
| 71 | |||
| 72 | 753667 | StreamSlotManager& StreamSlotManager::Instance() { | |
| 73 |
4/8✓ Branch 0 taken 2612 times.
✓ Branch 1 taken 751055 times.
✓ Branch 2 taken 2612 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2612 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
753667 | static StreamSlotManager instance; |
| 74 | 753667 | return instance; | |
| 75 | } | ||
| 76 | |||
| 77 | 3023 | MediaOutputStatus* StreamSlotManager::GetStatus(int slot) { | |
| 78 |
2/4✓ Branch 0 taken 3023 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3023 times.
|
3023 | if (slot < 1 || slot > MAX_SLOTS) { |
| 79 | ✗ | LogWarn(VB_MEDIAOUT, "StreamSlotManager::GetStatus: invalid slot %d\n", slot); | |
| 80 | ✗ | return &mediaOutputStatus; // fallback to global | |
| 81 | } | ||
| 82 |
2/2✓ Branch 0 taken 1300 times.
✓ Branch 1 taken 1723 times.
|
3023 | if (slot == 1) { |
| 83 | // Slot 1 uses the global mediaOutputStatus for backward compatibility | ||
| 84 | 1300 | return &mediaOutputStatus; | |
| 85 | } | ||
| 86 | 1723 | return &m_slots[slot - 1].status; | |
| 87 | } | ||
| 88 | |||
| 89 | 2161 | void StreamSlotManager::SetActiveOutput(int slot, GStreamerOutput* output) { | |
| 90 |
2/4✓ Branch 0 taken 2161 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2161 times.
|
2161 | if (slot < 1 || slot > MAX_SLOTS) return; |
| 91 |
1/2✓ Branch 0 taken 2161 times.
✗ Branch 1 not taken.
|
2161 | std::lock_guard<std::recursive_mutex> lock(m_mutex); |
| 92 | 2161 | m_slots[slot - 1].activeOutput = output; | |
| 93 | // Started again after its Start() detached it (DetachOutput()): it is no | ||
| 94 | // longer closing. | ||
| 95 |
3/6✓ Branch 0 taken 2161 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2161 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2161 times.
|
2161 | if (output && m_slots[slot - 1].closingOutput == output) { |
| 96 | ✗ | m_slots[slot - 1].closingOutput = nullptr; | |
| 97 | } | ||
| 98 | #ifdef HAS_GSTREAMER | ||
| 99 |
6/8✓ Branch 0 taken 2161 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2161 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1880 times.
✓ Branch 5 taken 281 times.
✓ Branch 6 taken 1880 times.
✓ Branch 7 taken 281 times.
|
2161 | if (output && output->NeedsSlotPump()) { |
| 100 |
1/2✓ Branch 0 taken 1880 times.
✗ Branch 1 not taken.
|
1880 | StartPumpTimer(); |
| 101 | } | ||
| 102 | #endif | ||
| 103 | // Every start (including a resume) re-arms the settle window. | ||
| 104 |
1/2✓ Branch 0 taken 2161 times.
✗ Branch 1 not taken.
|
2161 | m_slots[slot - 1].syncSettleMS = GetMonotonicTimeMS() + SYNC_SETTLE_MS; |
| 105 | #ifdef HAS_GSTREAMER | ||
| 106 |
1/2✓ Branch 0 taken 2161 times.
✗ Branch 1 not taken.
|
2161 | if (output) { |
| 107 |
1/2✓ Branch 0 taken 2161 times.
✗ Branch 1 not taken.
|
2161 | m_slots[slot - 1].mediaFilename = output->m_mediaFilename; |
| 108 |
1/2✓ Branch 0 taken 2161 times.
✗ Branch 1 not taken.
|
2161 | LogDebug(VB_MEDIAOUT, "StreamSlotManager: slot %d active (%s)\n", slot, |
| 109 | output->m_mediaFilename.c_str()); | ||
| 110 | // A volume set while the slot was idle is newer than the saved one | ||
| 111 | // (and is all there is when it came from a command, not the mixer). | ||
| 112 | 2161 | int saved = m_slots[slot - 1].pendingVolume; | |
| 113 | 2161 | m_slots[slot - 1].pendingVolume = -1; | |
| 114 |
2/2✓ Branch 0 taken 2049 times.
✓ Branch 1 taken 112 times.
|
2161 | if (saved < 0) { |
| 115 |
1/2✓ Branch 0 taken 2049 times.
✗ Branch 1 not taken.
|
2049 | saved = savedStreamSlotVolume(slot); |
| 116 | } | ||
| 117 |
2/2✓ Branch 0 taken 113 times.
✓ Branch 1 taken 2048 times.
|
2161 | if (saved >= 0) { |
| 118 |
1/2✓ Branch 0 taken 113 times.
✗ Branch 1 not taken.
|
113 | output->SetVolume(saved); |
| 119 |
1/2✓ Branch 0 taken 113 times.
✗ Branch 1 not taken.
|
113 | LogDebug(VB_MEDIAOUT, "StreamSlotManager: restored slot %d volume to %d%%\n", slot, saved); |
| 120 | } | ||
| 121 | } | ||
| 122 | #endif | ||
| 123 | 2161 | } | |
| 124 | |||
| 125 | 6206 | void StreamSlotManager::DetachOutput(int slot, GStreamerOutput* owner) { | |
| 126 |
3/6✓ Branch 0 taken 6206 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6206 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 6206 times.
|
6206 | if (slot < 1 || slot > MAX_SLOTS || !owner) return; |
| 127 |
1/2✓ Branch 0 taken 6206 times.
✗ Branch 1 not taken.
|
6206 | std::lock_guard<std::recursive_mutex> lock(m_mutex); |
| 128 |
2/2✓ Branch 0 taken 1338 times.
✓ Branch 1 taken 4868 times.
|
6206 | if (m_slots[slot - 1].activeOutput == owner) { |
| 129 | 1338 | m_slots[slot - 1].activeOutput = nullptr; | |
| 130 | 1338 | m_slots[slot - 1].closingOutput = owner; | |
| 131 | } | ||
| 132 | 6206 | } | |
| 133 | |||
| 134 | 3996 | bool StreamSlotManager::RequestStop(int slot) { | |
| 135 | #ifdef HAS_GSTREAMER | ||
| 136 |
2/4✓ Branch 0 taken 3996 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3996 times.
|
3996 | if (slot < 1 || slot > MAX_SLOTS) return false; |
| 137 |
1/2✓ Branch 0 taken 3996 times.
✗ Branch 1 not taken.
|
3996 | std::lock_guard<std::recursive_mutex> lock(m_mutex); |
| 138 |
2/2✓ Branch 0 taken 27 times.
✓ Branch 1 taken 3969 times.
|
3996 | if (m_slots[slot - 1].activeOutput) { |
| 139 |
1/2✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
|
27 | m_slots[slot - 1].activeOutput->RequestStop(); |
| 140 | 27 | return true; | |
| 141 | } | ||
| 142 | #endif | ||
| 143 | 3969 | return false; | |
| 144 | 3996 | } | |
| 145 | |||
| 146 | 11302 | GStreamerOutput* StreamSlotManager::GetActiveOutput(int slot) { | |
| 147 |
2/4✓ Branch 0 taken 11302 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 11302 times.
|
11302 | if (slot < 1 || slot > MAX_SLOTS) return nullptr; |
| 148 |
1/2✓ Branch 0 taken 11302 times.
✗ Branch 1 not taken.
|
11302 | std::lock_guard<std::recursive_mutex> lock(m_mutex); |
| 149 | 11302 | return m_slots[slot - 1].activeOutput; | |
| 150 | 11302 | } | |
| 151 | |||
| 152 | 2165 | void StreamSlotManager::ClaimSlot(int slot, GStreamerOutput* output) { | |
| 153 |
2/4✓ Branch 0 taken 2165 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2165 times.
|
2165 | if (slot < 1 || slot > MAX_SLOTS) return; |
| 154 |
1/2✓ Branch 0 taken 2165 times.
✗ Branch 1 not taken.
|
2165 | std::lock_guard<std::recursive_mutex> lock(m_mutex); |
| 155 | 2165 | m_slots[slot - 1].claimedBy = output; | |
| 156 | 2165 | } | |
| 157 | |||
| 158 | 2391 | bool StreamSlotManager::ClaimedByOrFree(int slot, GStreamerOutput* output) { | |
| 159 |
2/4✓ Branch 0 taken 2391 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2391 times.
|
2391 | if (slot < 1 || slot > MAX_SLOTS) return true; |
| 160 | // No lock: called from bus sync handlers, on streaming threads that the | ||
| 161 | // slot's Close() (which takes m_mutex) waits for. | ||
| 162 | 2391 | GStreamerOutput* claimedBy = m_slots[slot - 1].claimedBy.load(); | |
| 163 |
3/4✓ Branch 0 taken 2391 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2385 times.
✓ Branch 3 taken 6 times.
|
2391 | return !claimedBy || claimedBy == output; |
| 164 | } | ||
| 165 | |||
| 166 | 3943 | void StreamSlotManager::ClearSlot(int slot, GStreamerOutput* owner) { | |
| 167 |
2/4✓ Branch 0 taken 3943 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3943 times.
|
6573 | if (slot < 1 || slot > MAX_SLOTS) return; |
| 168 |
1/2✓ Branch 0 taken 3943 times.
✗ Branch 1 not taken.
|
3943 | std::lock_guard<std::recursive_mutex> lock(m_mutex); |
| 169 |
3/4✓ Branch 0 taken 3943 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1338 times.
✓ Branch 3 taken 2605 times.
|
3943 | bool closing = owner && m_slots[slot - 1].closingOutput == owner; |
| 170 |
2/2✓ Branch 0 taken 1338 times.
✓ Branch 1 taken 2605 times.
|
3943 | if (closing) { |
| 171 | 1338 | m_slots[slot - 1].closingOutput = nullptr; | |
| 172 | } | ||
| 173 | 3943 | GStreamerOutput* claimedBy = m_slots[slot - 1].claimedBy; | |
| 174 |
5/6✓ Branch 0 taken 3943 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2877 times.
✓ Branch 3 taken 1066 times.
✓ Branch 4 taken 1560 times.
✓ Branch 5 taken 1317 times.
|
3943 | if (owner && claimedBy && claimedBy != owner) { |
| 175 | // A newer stream has started on this slot (ClaimSlot) and may already | ||
| 176 | // have written its status -- its duration, say -- though it has not | ||
| 177 | // registered yet: leave the slot to it. Only our own registration, | ||
| 178 | // if it is somehow still there, goes. | ||
| 179 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1560 times.
|
1560 | if (m_slots[slot - 1].activeOutput == owner) { |
| 180 | ✗ | m_slots[slot - 1].activeOutput = nullptr; | |
| 181 | } | ||
| 182 |
1/2✓ Branch 0 taken 1560 times.
✗ Branch 1 not taken.
|
1560 | LogDebug(VB_MEDIAOUT, "StreamSlotManager: slot %d clear skipped, claimed by a newer stream\n", slot); |
| 183 | 1560 | return; | |
| 184 | } | ||
| 185 |
3/4✓ Branch 0 taken 2383 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1317 times.
✓ Branch 3 taken 1066 times.
|
2383 | if (!owner || claimedBy == owner) { |
| 186 | 1317 | m_slots[slot - 1].claimedBy = nullptr; | |
| 187 | } | ||
| 188 |
4/6✓ Branch 0 taken 2383 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2383 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1070 times.
✓ Branch 5 taken 1313 times.
|
4766 | if (owner && m_slots[slot - 1].activeOutput != owner && |
| 189 |
3/4✓ Branch 0 taken 1313 times.
✓ Branch 1 taken 1070 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1313 times.
|
2383 | !(closing && !m_slots[slot - 1].activeOutput)) { |
| 190 | // Another (newer) stream has already claimed this slot -- our | ||
| 191 | // teardown is stale, don't blank out its registration. | ||
| 192 |
1/2✓ Branch 0 taken 1070 times.
✗ Branch 1 not taken.
|
1070 | LogDebug(VB_MEDIAOUT, "StreamSlotManager: slot %d clear skipped, already reclaimed by a newer stream\n", slot); |
| 193 | 1070 | return; | |
| 194 | } | ||
| 195 | 1313 | m_slots[slot - 1].activeOutput = nullptr; | |
| 196 | 1313 | m_slots[slot - 1].mediaFilename.clear(); | |
| 197 | 1313 | m_slots[slot - 1].isBackground = false; | |
| 198 | 1313 | m_slots[slot - 1].syncToMaster = false; | |
| 199 | 1313 | m_slots[slot - 1].lastSyncMS = 0; | |
| 200 | 1313 | m_slots[slot - 1].syncSettleMS = 0; | |
| 201 | // Zero the timing fields, not just the state: GetAllSlotsStatus() reports | ||
| 202 | // them for any slot holding an active output, so leaving the last track's | ||
| 203 | // numbers behind makes a freshly started stream read as though it were | ||
| 204 | // already minutes in until its first Process() lands. Slot 1 aliases the | ||
| 205 | // global mediaOutputStatus, which is not ours to wipe. | ||
| 206 |
1/2✓ Branch 0 taken 1313 times.
✗ Branch 1 not taken.
|
1313 | std::lock_guard<std::mutex> fieldsLock(MediaOutputStatus::FieldsLock()); |
| 207 |
2/2✓ Branch 0 taken 597 times.
✓ Branch 1 taken 716 times.
|
1313 | if (slot > 1) { |
| 208 |
1/2✓ Branch 0 taken 597 times.
✗ Branch 1 not taken.
|
597 | m_slots[slot - 1].status = {}; |
| 209 | } | ||
| 210 | 1313 | m_slots[slot - 1].status.status = MEDIAOUTPUTSTATUS_IDLE; | |
| 211 | // #2713: release the KMS connector gate (mediaOutputStatus.output) when the | ||
| 212 | // stream tears down. Otherwise it stays set to the video's connector (e.g. | ||
| 213 | // "DSI-1") after playback ends and the KMSFrameBuffer overlay path stays | ||
| 214 | // gated off, so images never scan out until fppd is restarted. Use | ||
| 215 | // GetStatus(slot) so slot 1's global mediaOutputStatus is the object cleared. | ||
| 216 |
2/4✓ Branch 0 taken 1313 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1313 times.
✗ Branch 3 not taken.
|
1313 | GetStatus(slot)->output = ""; |
| 217 |
1/2✓ Branch 0 taken 1313 times.
✗ Branch 1 not taken.
|
1313 | LogDebug(VB_MEDIAOUT, "StreamSlotManager: slot %d cleared\n", slot); |
| 218 |
2/2✓ Branch 0 taken 1313 times.
✓ Branch 1 taken 2630 times.
|
3943 | } |
| 219 | |||
| 220 | 595985 | std::string StreamSlotManager::GetNodeName(int slot) { | |
| 221 |
1/2✓ Branch 0 taken 595985 times.
✗ Branch 1 not taken.
|
595985 | return "fppd_stream_" + std::to_string(slot); |
| 222 | } | ||
| 223 | |||
| 224 | 595625 | std::string StreamSlotManager::GetNodeDescription(int slot) { | |
| 225 |
1/2✓ Branch 0 taken 595625 times.
✗ Branch 1 not taken.
|
595625 | return "FPP Media Stream " + std::to_string(slot); |
| 226 | } | ||
| 227 | |||
| 228 | 124 | std::string StreamSlotManager::GetVideoNodeName(int slot) { | |
| 229 |
1/2✓ Branch 0 taken 124 times.
✗ Branch 1 not taken.
|
124 | return "fppd_video_stream_" + std::to_string(slot); |
| 230 | } | ||
| 231 | |||
| 232 | 44 | std::string StreamSlotManager::GetVideoNodeDescription(int slot) { | |
| 233 |
1/2✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
|
44 | return "FPP Video Stream " + std::to_string(slot); |
| 234 | } | ||
| 235 | |||
| 236 | 48783 | bool StreamSlotManager::SetSlotVolume(int slot, int volume, bool* deferred) { | |
| 237 | #ifdef HAS_GSTREAMER | ||
| 238 |
2/4✓ Branch 0 taken 48783 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 48783 times.
|
48783 | if (slot < 1 || slot > MAX_SLOTS) return false; |
| 239 |
1/2✓ Branch 0 taken 48783 times.
✗ Branch 1 not taken.
|
48783 | std::lock_guard<std::recursive_mutex> lock(m_mutex); |
| 240 | 48783 | GStreamerOutput* output = m_slots[slot - 1].activeOutput; | |
| 241 | // An output being stopped stays registered until its Close() but no | ||
| 242 | // longer takes a volume, and one playing media with no sound has nothing | ||
| 243 | // to take it with: either way the slot's next stream starts at it. | ||
| 244 |
5/6✓ Branch 0 taken 40882 times.
✓ Branch 1 taken 7901 times.
✓ Branch 2 taken 40882 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 40853 times.
✓ Branch 5 taken 29 times.
|
48783 | bool applied = output && output->TrySetVolume(volume); |
| 245 |
2/2✓ Branch 0 taken 7930 times.
✓ Branch 1 taken 40853 times.
|
48783 | if (!applied) { |
| 246 | // Nothing to apply it to yet: the slot's next stream starts at it, | ||
| 247 | // as it does at the level the mixer saves. | ||
| 248 | 7930 | m_slots[slot - 1].pendingVolume = volume; | |
| 249 | } | ||
| 250 |
1/2✓ Branch 0 taken 48783 times.
✗ Branch 1 not taken.
|
48783 | if (deferred) { |
| 251 | 48783 | *deferred = !applied; | |
| 252 | } | ||
| 253 | 48783 | return true; | |
| 254 | #else | ||
| 255 | return false; | ||
| 256 | #endif | ||
| 257 | 48783 | } | |
| 258 | |||
| 259 | 1 | bool StreamSlotManager::SeekSlot(int slot, float seconds) { | |
| 260 | #ifdef HAS_GSTREAMER | ||
| 261 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if (slot < 1 || slot > MAX_SLOTS) return false; |
| 262 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | std::lock_guard<std::recursive_mutex> lock(m_mutex); |
| 263 | 1 | GStreamerOutput* output = m_slots[slot - 1].activeOutput; | |
| 264 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (output) { |
| 265 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | return output->SeekTo(seconds); |
| 266 | } | ||
| 267 | #endif | ||
| 268 | ✗ | return false; | |
| 269 | 1 | } | |
| 270 | |||
| 271 | 118628 | Json::Value StreamSlotManager::GetAllSlotsStatus() { | |
| 272 |
1/2✓ Branch 0 taken 118628 times.
✗ Branch 1 not taken.
|
118628 | Json::Value result(Json::arrayValue); |
| 273 |
1/2✓ Branch 0 taken 118628 times.
✗ Branch 1 not taken.
|
118628 | std::lock_guard<std::recursive_mutex> lock(m_mutex); |
| 274 |
2/2✓ Branch 0 taken 593140 times.
✓ Branch 1 taken 118628 times.
|
711768 | for (int i = 0; i < MAX_SLOTS; i++) { |
| 275 |
1/2✓ Branch 0 taken 593140 times.
✗ Branch 1 not taken.
|
593140 | Json::Value slotJson; |
| 276 |
2/4✓ Branch 0 taken 593140 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 593140 times.
✗ Branch 3 not taken.
|
593140 | slotJson["slot"] = i + 1; |
| 277 |
3/6✓ Branch 0 taken 593140 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 593140 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 593140 times.
✗ Branch 5 not taken.
|
593140 | slotJson["nodeName"] = GetNodeName(i + 1); |
| 278 |
3/6✓ Branch 0 taken 593140 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 593140 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 593140 times.
✗ Branch 5 not taken.
|
593140 | slotJson["nodeDescription"] = GetNodeDescription(i + 1); |
| 279 | |||
| 280 | // The output's own status, which is not always the slot's: Play Media | ||
| 281 | // on slot 1 keeps a private one so it doesn't overwrite the playlist's | ||
| 282 | // (the global mediaOutputStatus), and reading the global reported 0 | ||
| 283 | // for its whole track. Close() takes an output out of the table | ||
| 284 | // before freeing it, so under m_mutex the pointer is live. | ||
| 285 | 593140 | GStreamerOutput* out = m_slots[i].activeOutput; | |
| 286 |
3/4✓ Branch 0 taken 25884 times.
✓ Branch 1 taken 567256 times.
✓ Branch 2 taken 25884 times.
✗ Branch 3 not taken.
|
1160396 | MediaOutputStatus* st = (out && out->m_mediaOutputStatus) ? out->m_mediaOutputStatus |
| 287 |
2/2✓ Branch 0 taken 474108 times.
✓ Branch 1 taken 93148 times.
|
1041364 | : (i == 0) ? &mediaOutputStatus |
| 288 | 474108 | : &m_slots[i].status; | |
| 289 |
2/2✓ Branch 0 taken 25884 times.
✓ Branch 1 taken 567256 times.
|
593140 | if (out) { |
| 290 |
1/2✓ Branch 0 taken 25884 times.
✗ Branch 1 not taken.
|
25884 | std::lock_guard<std::mutex> fieldsLock(MediaOutputStatus::FieldsLock()); |
| 291 |
2/4✓ Branch 0 taken 25884 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25884 times.
✗ Branch 3 not taken.
|
25884 | slotJson["status"] = "playing"; |
| 292 |
2/4✓ Branch 0 taken 25884 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25884 times.
✗ Branch 3 not taken.
|
25884 | slotJson["mediaFilename"] = m_slots[i].mediaFilename; |
| 293 |
2/4✓ Branch 0 taken 25884 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25884 times.
✗ Branch 3 not taken.
|
25884 | slotJson["secondsElapsed"] = st->secondsElapsed; |
| 294 |
2/4✓ Branch 0 taken 25884 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25884 times.
✗ Branch 3 not taken.
|
25884 | slotJson["subSecondsElapsed"] = st->subSecondsElapsed; |
| 295 |
2/4✓ Branch 0 taken 25884 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25884 times.
✗ Branch 3 not taken.
|
25884 | slotJson["secondsRemaining"] = st->secondsRemaining; |
| 296 |
2/4✓ Branch 0 taken 25884 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25884 times.
✗ Branch 3 not taken.
|
25884 | slotJson["subSecondsRemaining"] = st->subSecondsRemaining; |
| 297 |
2/4✓ Branch 0 taken 25884 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25884 times.
✗ Branch 3 not taken.
|
25884 | slotJson["minutesTotal"] = st->minutesTotal; |
| 298 |
2/4✓ Branch 0 taken 25884 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25884 times.
✗ Branch 3 not taken.
|
25884 | slotJson["secondsTotal"] = st->secondsTotal; |
| 299 |
2/4✓ Branch 0 taken 25884 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25884 times.
✗ Branch 3 not taken.
|
25884 | slotJson["isBackground"] = m_slots[i].isBackground; |
| 300 | 25884 | } else { | |
| 301 |
2/4✓ Branch 0 taken 567256 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 567256 times.
✗ Branch 3 not taken.
|
567256 | slotJson["status"] = "idle"; |
| 302 |
2/4✓ Branch 0 taken 567256 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 567256 times.
✗ Branch 3 not taken.
|
567256 | slotJson["mediaFilename"] = ""; |
| 303 |
2/4✓ Branch 0 taken 567256 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 567256 times.
✗ Branch 3 not taken.
|
567256 | slotJson["isBackground"] = false; |
| 304 | } | ||
| 305 |
1/2✓ Branch 0 taken 593140 times.
✗ Branch 1 not taken.
|
593140 | result.append(slotJson); |
| 306 | 593140 | } | |
| 307 | 118628 | return result; | |
| 308 | 118628 | } | |
| 309 | |||
| 310 | ✗ | int StreamSlotManager::ActiveSlotCount() { | |
| 311 | ✗ | std::lock_guard<std::recursive_mutex> lock(m_mutex); | |
| 312 | ✗ | int count = 0; | |
| 313 | ✗ | for (int i = 0; i < MAX_SLOTS; i++) { | |
| 314 | ✗ | if (m_slots[i].activeOutput) count++; | |
| 315 | } | ||
| 316 | ✗ | return count; | |
| 317 | ✗ | } | |
| 318 | |||
| 319 | 1130 | void StreamSlotManager::StopBackgroundSlots() { | |
| 320 | #ifdef HAS_GSTREAMER | ||
| 321 | // These outputs belong to someone else -- Play Media's registry, or a | ||
| 322 | // playlist entry playing on a background slot -- which stops, closes and | ||
| 323 | // frees them on its own thread. Stopping and closing them here, from | ||
| 324 | // whatever thread is stopping the playlist, raced the owner: a second | ||
| 325 | // Close() alongside the deferred delete's, and Stop() under this lock | ||
| 326 | // while the main loop waited on it. Each output is asked to stop, and | ||
| 327 | // its own Close() clears the slot. | ||
| 328 |
1/2✓ Branch 0 taken 1130 times.
✗ Branch 1 not taken.
|
1130 | std::lock_guard<std::recursive_mutex> lock(m_mutex); |
| 329 |
2/2✓ Branch 0 taken 4520 times.
✓ Branch 1 taken 1130 times.
|
5650 | for (int i = 1; i < MAX_SLOTS; i++) { // skip slot 1 (managed by playlist) |
| 330 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 4515 times.
|
4520 | if (m_slots[i].activeOutput) { |
| 331 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | LogInfo(VB_MEDIAOUT, "StreamSlotManager: stopping slot %d\n", i + 1); |
| 332 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | m_slots[i].activeOutput->RequestStop(); |
| 333 | } | ||
| 334 | } | ||
| 335 | #endif | ||
| 336 | 1130 | } | |
| 337 | |||
| 338 | 3 | void StreamSlotManager::SetSyncToMaster(int slot, bool sync) { | |
| 339 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
|
3 | if (slot < 1 || slot > MAX_SLOTS) |
| 340 | ✗ | return; | |
| 341 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | std::lock_guard<std::recursive_mutex> lock(m_mutex); |
| 342 | 3 | m_slots[slot - 1].syncToMaster = sync; | |
| 343 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (sync) { |
| 344 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | LogInfo(VB_MEDIAOUT, "StreamSlotManager: slot %d will follow the primary media clock\n", slot); |
| 345 | } | ||
| 346 | 3 | } | |
| 347 | |||
| 348 | 159779 | void StreamSlotManager::SyncSlotsToMaster(float masterPos) { | |
| 349 | #ifdef HAS_GSTREAMER | ||
| 350 |
1/2✓ Branch 0 taken 159779 times.
✗ Branch 1 not taken.
|
159779 | std::lock_guard<std::recursive_mutex> lock(m_mutex); |
| 351 |
1/2✓ Branch 0 taken 159779 times.
✗ Branch 1 not taken.
|
159779 | long long now = GetMonotonicTimeMS(); |
| 352 | // Slot 1 IS the master (it is the playlist's own media), so start at 2. | ||
| 353 |
2/2✓ Branch 0 taken 639116 times.
✓ Branch 1 taken 159779 times.
|
798895 | for (int i = 1; i < MAX_SLOTS; i++) { |
| 354 |
6/6✓ Branch 0 taken 2598 times.
✓ Branch 1 taken 636518 times.
✓ Branch 2 taken 1474 times.
✓ Branch 3 taken 1124 times.
✓ Branch 4 taken 637992 times.
✓ Branch 5 taken 1124 times.
|
639116 | if (!m_slots[i].activeOutput || !m_slots[i].syncToMaster) |
| 355 | 637992 | continue; | |
| 356 | |||
| 357 | // Process() first: nothing else drives a non-background slot's status, | ||
| 358 | // so without this its mediaSeconds stays 0 and AdjustSpeed bails out | ||
| 359 | // ("can't adjust speed if not playing yet") having never seen the slot | ||
| 360 | // actually move. | ||
| 361 |
1/2✓ Branch 0 taken 1124 times.
✗ Branch 1 not taken.
|
1124 | m_slots[i].activeOutput->Process(); |
| 362 | |||
| 363 |
2/2✓ Branch 0 taken 1100 times.
✓ Branch 1 taken 24 times.
|
1124 | if ((now - m_slots[i].lastSyncMS) < SYNC_INTERVAL_MS) |
| 364 | 1100 | continue; | |
| 365 | 24 | m_slots[i].lastSyncMS = now; | |
| 366 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
|
24 | if (now < m_slots[i].syncSettleMS) |
| 367 | 16 | continue; | |
| 368 | |||
| 369 | // Slots started together on this box share the pipeline clock and stay | ||
| 370 | // sample-locked by themselves -- measured 0.000s offset across a whole | ||
| 371 | // track with no correction at all. So this is a repair path, not a | ||
| 372 | // control loop: it only fires when something has genuinely knocked a | ||
| 373 | // slot off the show timeline (the primary was seeked or restarted, or | ||
| 374 | // MultiSync is rate-adjusting the primary on a remote). | ||
| 375 | // | ||
| 376 | // Correcting by rate is not an option. These pipelines refuse | ||
| 377 | // GST_SEEK_FLAG_INSTANT_RATE_CHANGE, so ApplyRate() falls back to a | ||
| 378 | // flush seek, which moves the position enough to feed the next | ||
| 379 | // correction -- measured 0.9-2.5s oscillating, never converging, on | ||
| 380 | // streams that were exactly locked before the "sync" was applied. | ||
| 381 | float slotPos; | ||
| 382 | { | ||
| 383 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | std::lock_guard<std::mutex> fieldsLock(MediaOutputStatus::FieldsLock()); |
| 384 | 8 | slotPos = m_slots[i].status.mediaSeconds; | |
| 385 | 8 | } | |
| 386 | 8 | float drift = slotPos - masterPos; | |
| 387 |
2/4✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
8 | if (drift > -MAX_DRIFT_SEC && drift < MAX_DRIFT_SEC) |
| 388 | 8 | continue; | |
| 389 | ✗ | LogInfo(VB_MEDIAOUT, "StreamSlotManager: slot %d drifted %0.3fs from the primary, seeking to %0.3f\n", | |
| 390 | i + 1, drift, masterPos); | ||
| 391 | ✗ | m_slots[i].activeOutput->SeekTo(masterPos); | |
| 392 | } | ||
| 393 | #endif | ||
| 394 | 159779 | } | |
| 395 | |||
| 396 | 391469 | void StreamSlotManager::ProcessBackgroundSlots() { | |
| 397 | 391469 | PumpSlotOutputs(); | |
| 398 | 391469 | } | |
| 399 | |||
| 400 | // Calls Process() for every output whose owner doesn't (see | ||
| 401 | // GStreamerOutput::NeedsSlotPump()) -- Play Media's, on any slot. Without it | ||
| 402 | // their status never advances, so the UI and status API report a frozen | ||
| 403 | // position, and the preroll and stall watchdogs never run. This used to | ||
| 404 | // happen only from a playing playlist (ProcessBackgroundSlots()), and only for | ||
| 405 | // slots 2-5: Play Media with the player idle, or on slot 1, got nothing. The | ||
| 406 | // pump timer covers those; the playlist's tick still pumps while it runs, and | ||
| 407 | // the two share one cadence. | ||
| 408 | // | ||
| 409 | // Teardown is deliberately not done here. Media started by the "Play Media" | ||
| 410 | // command is owned by runningCommandMedia and freed off the main loop when | ||
| 411 | // Stopped() fires, so deleting it here would be a double free. | ||
| 412 | // Close()/ClearSlot() on that path is what clears the slot. | ||
| 413 | 407590 | void StreamSlotManager::PumpSlotOutputs() { | |
| 414 | #ifdef HAS_GSTREAMER | ||
| 415 |
1/2✓ Branch 0 taken 407590 times.
✗ Branch 1 not taken.
|
407590 | std::lock_guard<std::recursive_mutex> lock(m_mutex); |
| 416 |
1/2✓ Branch 0 taken 407590 times.
✗ Branch 1 not taken.
|
407590 | long long now = GetMonotonicTimeMS(); |
| 417 |
2/2✓ Branch 0 taken 272079 times.
✓ Branch 1 taken 135511 times.
|
407590 | if (now - m_lastPumpMS < PUMP_INTERVAL_MS / 2) { |
| 418 | 272079 | return; | |
| 419 | } | ||
| 420 | 135511 | m_lastPumpMS = now; | |
| 421 |
2/2✓ Branch 0 taken 677555 times.
✓ Branch 1 taken 135511 times.
|
813066 | for (int i = 0; i < MAX_SLOTS; i++) { |
| 422 |
7/8✓ Branch 0 taken 67343 times.
✓ Branch 1 taken 610212 times.
✓ Branch 2 taken 67343 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11432 times.
✓ Branch 5 taken 55911 times.
✓ Branch 6 taken 11432 times.
✓ Branch 7 taken 666123 times.
|
677555 | if (m_slots[i].activeOutput && m_slots[i].activeOutput->NeedsSlotPump()) { |
| 423 |
1/2✓ Branch 0 taken 11432 times.
✗ Branch 1 not taken.
|
11432 | m_slots[i].activeOutput->Process(); |
| 424 | } | ||
| 425 | } | ||
| 426 | #endif | ||
| 427 |
2/2✓ Branch 0 taken 135511 times.
✓ Branch 1 taken 272079 times.
|
407590 | } |
| 428 | |||
| 429 | 1880 | void StreamSlotManager::StartPumpTimer() { | |
| 430 |
2/2✓ Branch 0 taken 1751 times.
✓ Branch 1 taken 129 times.
|
1880 | if (m_pumpTimerStarted.exchange(true)) { |
| 431 | 1751 | return; | |
| 432 | } | ||
| 433 |
2/4✓ Branch 0 taken 129 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 129 times.
✗ Branch 3 not taken.
|
387 | Timers::INSTANCE.addPeriodicTimer("StreamSlotPump", PUMP_INTERVAL_MS, [this]() { |
| 434 | 16121 | PumpSlotOutputs(); | |
| 435 | 16121 | }); | |
| 436 | } | ||
| 437 |