From 0c9e54656ba40d205f5306965ee57560c13718e4 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 19 Jun 2026 00:59:05 +0100 Subject: [PATCH] Make Muninn OBS reconnect receiver-owned --- docs/obs-stem-plugin.md | 15 ++- .../mimir_obs_muninn_source.cpp | 125 ++++++++++++++++-- .../obs_stem_source/muninn_rudp_fragments.h | 24 ++++ state/evidence.jsonl | 1 + 4 files changed, 153 insertions(+), 12 deletions(-) diff --git a/docs/obs-stem-plugin.md b/docs/obs-stem-plugin.md index b37aa64..5fb61fe 100644 --- a/docs/obs-stem-plugin.md +++ b/docs/obs-stem-plugin.md @@ -164,9 +164,13 @@ The current Raven-room default route is LAN-first: command requests go to deprecated WireGuard defaults `10.77.0.4:17873` and `10.77.0.2` are migrated on load. Custom operator-entered endpoints are left alone. -The source polls the catalog while active and republishes activation requests on -a bounded reconnect cadence, so a Raven daemon restart or media child exit does -not require reopening the source properties. +The source polls the catalog while active, but reconnect is owned by the RUDP +receiver bridge rather than catalog labels. The bridge tracks the last packet +and the last forwarded video/audio payload. If startup produces no media, or a +previously live stream goes stale, OBS refreshes its local FFmpeg child readers +and republishes the typed activation command on a short bounded cadence. A Raven +daemon restart or media child exit should therefore recover without reopening +source properties. For `rudp://` stream URLs, the plugin binds the advertised media port, accepts the CultNet RUDP handshake, acknowledges packets, reassembles RUDP fragments, @@ -178,6 +182,11 @@ media is reliable but unordered, so frame ids, chunk ids, and the advertised `assembly_deadline_ms` own video reconstruction. Incomplete video frames expire inside that budget and emit `muninn.media_receiver_feedback.v1` keyframe pressure instead of stalling newer media behind an obsolete sequence gap. +RUDP fragment ids are quarantined briefly after completion/expiry so late +duplicates cannot seed a corrupt assembly when the sender's 16-bit fragment id +space wraps under sustained LAN pressure. The local OBS FFmpeg UDP readers use +large loopback buffers; those buffers absorb receiver bursts without becoming a +second transport authority. ## Install diff --git a/native/obs_stem_source/mimir_obs_muninn_source.cpp b/native/obs_stem_source/mimir_obs_muninn_source.cpp index e580f98..1229f7f 100644 --- a/native/obs_stem_source/mimir_obs_muninn_source.cpp +++ b/native/obs_stem_source/mimir_obs_muninn_source.cpp @@ -47,7 +47,10 @@ constexpr const char *MuninnObsCatalogType = "muninn.obs_stream_catalog"; constexpr const char *MuninnObsCatalogSchema = "muninn.obs_stream_catalog.v1"; constexpr const char *MuninnObsCatalogKey = "obs"; constexpr uint64_t MuninnCatalogRefreshMs = 2000; -constexpr uint64_t MuninnActivationRetryMs = 120000; +constexpr uint64_t MuninnActivationRetryMs = 5000; +constexpr uint64_t MuninnMediaStartupGraceMs = 3000; +constexpr uint64_t MuninnMediaStaleMs = 1500; +constexpr uint64_t MuninnMediaRestartCooldownMs = 5000; constexpr uint16_t MuninnCatalogDiscoveryPort = 17874; constexpr uint32_t MuninnCatalogDiscoveryConnectionId = 0x6d750003; @@ -89,6 +92,7 @@ struct MuninnSource { std::unique_ptr catalog_discovery; std::chrono::steady_clock::time_point next_catalog_refresh = std::chrono::steady_clock::now(); std::chrono::steady_clock::time_point last_activation_request = std::chrono::steady_clock::time_point::min(); + std::chrono::steady_clock::time_point last_media_restart = std::chrono::steady_clock::time_point::min(); }; static void muninn_child_audio(void *param, obs_source_t *source, const struct audio_data *audio_data, bool muted); @@ -125,6 +129,14 @@ static bool starts_with(const std::string &value, const std::string &prefix) return value.rfind(prefix, 0) == 0; } +static uint64_t steady_millis() +{ + return static_cast( + std::chrono::duration_cast( + std::chrono::steady_clock::now().time_since_epoch()) + .count()); +} + static uint32_t read_be32(const uint8_t *data) { return (static_cast(data[0]) << 24) | (static_cast(data[1]) << 16) | @@ -473,9 +485,16 @@ public: source_url_ = url; parts_ = *parsed; outputs_.video_url = "udp://127.0.0.1:" + std::to_string(parts_.video_local_port) + - "?fifo_size=32768&overrun_nonfatal=1&buffer_size=262144"; + "?fifo_size=1048576&overrun_nonfatal=1&buffer_size=16777216"; outputs_.audio_url = "udp://127.0.0.1:" + std::to_string(parts_.audio_local_port) + - "?fifo_size=32768&overrun_nonfatal=1&buffer_size=262144"; + "?fifo_size=262144&overrun_nonfatal=1&buffer_size=4194304"; + const uint64_t now = steady_millis(); + started_ms_.store(now); + last_packet_ms_.store(0); + last_video_forwarded_ms_.store(0); + last_audio_forwarded_ms_.store(0); + stale_reported_.store(false); + media_connected_ = false; running_ = true; worker_ = std::thread([this] { run(); }); blog(LOG_INFO, @@ -500,6 +519,49 @@ public: } source_url_.clear(); outputs_ = {}; + started_ms_.store(0); + last_packet_ms_.store(0); + last_video_forwarded_ms_.store(0); + last_audio_forwarded_ms_.store(0); + stale_reported_.store(false); + media_connected_ = false; + } + + bool has_forwarded_media() const + { + return last_video_forwarded_ms_.load() != 0 || last_audio_forwarded_ms_.load() != 0; + } + + bool needs_activation(uint64_t now_ms) const + { + const uint64_t started = started_ms_.load(); + if (started == 0) { + return true; + } + if (now_ms >= started && now_ms - started < MuninnMediaStartupGraceMs) { + return false; + } + if (!running_.load()) { + return true; + } + const uint64_t last_packet = last_packet_ms_.load(); + if (last_packet == 0) { + return true; + } + const uint64_t last_video = last_video_forwarded_ms_.load(); + const uint64_t last_audio = last_audio_forwarded_ms_.load(); + const uint64_t last_media = std::max(last_video, last_audio); + return last_media == 0 || (now_ms > last_media && now_ms - last_media >= MuninnMediaStaleMs); + } + + bool consume_stale_transition(uint64_t now_ms) + { + if (!needs_activation(now_ms)) { + stale_reported_.store(false); + return false; + } + bool expected = false; + return stale_reported_.compare_exchange_strong(expected, true); } private: @@ -512,6 +574,7 @@ private: socket_ = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (socket_ == INVALID_SOCKET) { blog(LOG_WARNING, "Muninn RUDP bridge could not create socket"); + running_ = false; WSACleanup(); return; } @@ -530,6 +593,7 @@ private: blog(LOG_WARNING, "Muninn RUDP bridge could not bind udp/%u", parts_.port); closesocket(socket_); socket_ = INVALID_SOCKET; + running_ = false; WSACleanup(); return; } @@ -602,6 +666,7 @@ private: if (version != 0 || connection_id != parts_.connection_id || header_bytes < 36 || size < header_bytes) { return; } + last_packet_ms_.store(steady_millis()); last_remote_ = remote; const size_t payload_offset = static_cast(header_bytes); if (payload_offset + payload_len > size || 36 + channel_len > header_bytes) { @@ -621,6 +686,7 @@ private: waiting_for_video_keyframe_ = true; video_forward_failures_ = 0; audio_forward_failures_ = 0; + media_connected_ = true; const auto [ack, ack_mask] = ack_tracker_.state(); send_control_packet(2, 0x03, bridge_sequence++, ack, ack_mask, remote); blog(LOG_INFO, "Muninn RUDP bridge accepted media connection sequence %u", sequence); @@ -628,10 +694,18 @@ private: } if (packet_type == 3) { + if (!media_connected_) { + return; + } ack_tracker_.remember(sequence); const std::string channel_id( reinterpret_cast(packet + 36), reinterpret_cast(packet + 36 + channel_len)); + if (channel_id != "media") { + const auto [ack, ack_mask] = ack_tracker_.state(); + send_control_packet(4, 0x00, bridge_sequence++, ack, ack_mask, remote); + return; + } std::optional>> delivered; if (fragment_count == 0) { delivered = std::make_pair( @@ -728,6 +802,7 @@ private: log_bridge_pressure("failed forwarding video payload to local decoder socket"); return; } + last_video_forwarded_ms_.store(steady_millis()); } else if (media->kind == MuninnMediaPayload::Kind::Audio) { forwarded_bytes = media->payload.size(); const int sent = sendto(audio_socket, reinterpret_cast(media->payload.data()), static_cast(media->payload.size()), 0, @@ -737,6 +812,7 @@ private: log_bridge_pressure("failed forwarding audio payload to local decoder socket"); return; } + last_audio_forwarded_ms_.store(steady_millis()); } else { ++payloads_decoded_dropped_; log_bridge_pressure("dropped unknown media payload"); @@ -966,6 +1042,11 @@ private: std::optional last_remote_; #endif std::atomic running_{false}; + std::atomic started_ms_{0}; + std::atomic last_packet_ms_{0}; + std::atomic last_video_forwarded_ms_{0}; + std::atomic last_audio_forwarded_ms_{0}; + std::atomic stale_reported_{false}; std::thread worker_; muninn_rudp_url::RudpUrlParts parts_; std::map video_assemblies_; @@ -979,6 +1060,7 @@ private: uint64_t video_forward_failures_ = 0; uint64_t audio_forward_failures_ = 0; bool waiting_for_video_keyframe_ = true; + bool media_connected_ = false; std::string source_url_; RudpBridgeOutputs outputs_; }; @@ -1150,14 +1232,15 @@ static std::string endpoint_setting(obs_data_t *settings, const char *key, const return value; } -static void request_stream_activation(MuninnSource *ctx, const std::string &stream_id) +static void request_stream_activation(MuninnSource *ctx, const std::string &stream_id, bool force = false) { const auto canonical_stream_id = canonical_muninn_stream_id(stream_id); if (!ctx || canonical_stream_id.empty() || ctx->command_exe_path.empty() || ctx->command_rudp_target.empty()) { return; } const auto now = std::chrono::steady_clock::now(); - if (ctx->last_requested_stream_id == canonical_stream_id && + if (!force && + ctx->last_requested_stream_id == canonical_stream_id && now - ctx->last_activation_request < std::chrono::milliseconds(MuninnActivationRetryMs)) { return; } @@ -1734,7 +1817,7 @@ static obs_source_t *create_ffmpeg_child_source(MuninnSource *ctx, return source; } -static void create_or_update_media(MuninnSource *ctx, const std::string &url) +static void create_or_update_media(MuninnSource *ctx, const std::string &url, bool force_restart = false) { if (url.empty()) { release_media(ctx); @@ -1742,7 +1825,7 @@ static void create_or_update_media(MuninnSource *ctx, const std::string &url) return; } - if (ctx->media && equivalent_rudp_stream_url(ctx->stream_url, url)) { + if (!force_restart && ctx->media && equivalent_rudp_stream_url(ctx->stream_url, url)) { ctx->stream_url = url; return; } @@ -1814,10 +1897,34 @@ static void reconcile_selected_stream(MuninnSource *ctx) ctx->stream_id = selected->stream_id; if (selection_changed) { ctx->last_requested_stream_id.clear(); + ctx->last_media_restart = std::chrono::steady_clock::time_point::min(); } - request_stream_activation(ctx, selected->stream_id); - create_or_update_media(ctx, playable_rudp_stream_url(ctx, *selected)); + const auto stream_url = playable_rudp_stream_url(ctx, *selected); + create_or_update_media(ctx, stream_url); + + bool activation_needed = ctx->last_requested_stream_id != selected_canonical_stream_id; + bool force_activation = false; + if (ctx->bridge && starts_with(stream_url, "rudp://")) { + const uint64_t now_ms = steady_millis(); + if (ctx->bridge->consume_stale_transition(now_ms)) { + blog(LOG_WARNING, "Muninn Stream receiver media is stale; refreshing local media readers and re-requesting activation"); + const auto now = std::chrono::steady_clock::now(); + if (now - ctx->last_media_restart >= std::chrono::milliseconds(MuninnMediaRestartCooldownMs)) { + create_or_update_media(ctx, stream_url, true); + ctx->last_media_restart = now; + } + ctx->last_requested_stream_id.clear(); + activation_needed = true; + force_activation = true; + } else if (ctx->bridge->needs_activation(now_ms)) { + activation_needed = true; + } + } + + if (activation_needed) { + request_stream_activation(ctx, selected->stream_id, force_activation); + } } static void populate_stream_list(obs_property_t *property, const std::string &store_path) diff --git a/native/obs_stem_source/muninn_rudp_fragments.h b/native/obs_stem_source/muninn_rudp_fragments.h index 6c371a2..1da82a5 100644 --- a/native/obs_stem_source/muninn_rudp_fragments.h +++ b/native/obs_stem_source/muninn_rudp_fragments.h @@ -11,6 +11,8 @@ namespace muninn_rudp_fragments { +constexpr uint32_t CompletedFragmentIdQuarantineMs = 2000; + struct FragmentResult { uint32_t first_sequence = 0; std::vector payload; @@ -21,6 +23,7 @@ public: void reset() { assemblies_.clear(); + recently_closed_.clear(); expired_count_ = 0; } @@ -38,6 +41,12 @@ public: } const std::string key = channel_id + ":" + std::to_string(fragment_id); + expire_recently_closed(now); + const auto existing = assemblies_.find(key); + if (existing == assemblies_.end() && recently_closed_.find(key) != recently_closed_.end()) { + return std::nullopt; + } + auto &assembly = assemblies_[key]; if (assembly.payloads.empty()) { assembly.channel_id = channel_id; @@ -67,17 +76,20 @@ public: result.first_sequence = std::min(result.first_sequence, sequence_found->second); } assemblies_.erase(key); + recently_closed_[key] = now; return result; } uint64_t expire(std::chrono::steady_clock::time_point now, uint32_t expire_after_ms) { + expire_recently_closed(now); uint64_t expired = 0; for (auto iterator = assemblies_.begin(); iterator != assemblies_.end();) { if (now - iterator->second.started_at < std::chrono::milliseconds(expire_after_ms)) { ++iterator; continue; } + recently_closed_[iterator->first] = now; iterator = assemblies_.erase(iterator); ++expired; } @@ -88,6 +100,17 @@ public: uint64_t expired_count() const { return expired_count_; } private: + void expire_recently_closed(std::chrono::steady_clock::time_point now) + { + for (auto iterator = recently_closed_.begin(); iterator != recently_closed_.end();) { + if (now - iterator->second < std::chrono::milliseconds(CompletedFragmentIdQuarantineMs)) { + ++iterator; + continue; + } + iterator = recently_closed_.erase(iterator); + } + } + struct Assembly { std::string channel_id; uint16_t fragment_count = 0; @@ -97,6 +120,7 @@ private: }; std::map assemblies_; + std::map recently_closed_; uint64_t expired_count_ = 0; }; diff --git a/state/evidence.jsonl b/state/evidence.jsonl index ea62e86..2fbabdb 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -381,3 +381,4 @@ {"ts": "2026-06-19T00:02:36+01:00", "type": "implementation", "status": "partial", "note": "Muninn OBS source now migrates stale WireGuard endpoints to Raven-room LAN defaults, uses aac demuxer for ADTS loopback, and was installed to OBS. Live OBS proof showed LAN activation to 192.168.1.84:17873 and RUDP media forwarding; sender-side CultNet RUDP pressure remains the active fault."} {"ts": "2026-06-19T00:27:59+01:00", "type": "implementation", "status": "ok", "note": "Muninn OBS source UI is now stream-dropdown only, filters non-playable catalog affordances, auto-selects Raven A/V when the local CultMesh catalog projection is absent or stale, sanitizes stale RUDP URLs back to the LAN low-latency profile, and retries activation on a bounded reconnect cadence. Live OBS smoke showed auto activation to 192.168.1.84:17873 and RUDP bridge startup with reliable_expire_after_ms=250 and assembly_deadline_ms=150."} {"ts": "2026-06-19T00:45:57+01:00", "type": "implementation", "status": "ok", "note": "Muninn OBS source now listens on UDP 17874 for Raven's typed OBS catalog projection over a dedicated CultNet RUDP lane (connection 0x6d750003), prefers that live discovery over the local .cc fallback, and treats equivalent RUDP stream URLs as the same media session so live discovery does not bounce the bridge. Live OBS smoke accepted one discovered stream option, auto-activated Raven, started the bridge with reliable_expire_after_ms=250 and assembly_deadline_ms=150, forwarded beyond 13k payloads, and showed no duplicate bind failure after discovery."} +{"ts": "2026-06-19T01:08:55+01:00", "type": "verification", "status": "ok", "note": "Muninn OBS RUDP receiver liveness now owns reconnect. The bridge tracks startup grace, last packet, and last forwarded video/audio payload, drops media data before a fresh CNR0 handshake, refreshes local child readers only on bridge-proven stale media, gates typed media decode by the CNR0 media channel, quarantines recently completed/expired fragment ids across the sender's 16-bit wrap window, and uses larger local OBS FFmpeg UDP buffers. Live OBS smoke from C:\\Users\\Meta\\AppData\\Roaming\\obs-studio\\logs\\2026-06-19 01-07-32.txt ran 80 seconds with one activation, catalog discovery accepted, media connection accepted, forwarding beyond 27900 payloads, zero typed decode failures, zero undecodable payloads, zero circular-buffer overruns, and no repeated activation while media was flowing."}