From 70098f0c49bdb8eb7a8db4e8027284f12827af90 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 19 Jun 2026 00:29:23 +0100 Subject: [PATCH] Make Muninn OBS source self-connecting --- docs/obs-stem-plugin.md | 22 +- .../mimir_obs_muninn_source.cpp | 211 +++++++++++++++--- state/evidence.jsonl | 2 + 3 files changed, 200 insertions(+), 35 deletions(-) diff --git a/docs/obs-stem-plugin.md b/docs/obs-stem-plugin.md index 22b6a7a..5b12185 100644 --- a/docs/obs-stem-plugin.md +++ b/docs/obs-stem-plugin.md @@ -122,7 +122,11 @@ Muninn Stream ### Muninn Streams The `Muninn Stream` source is a thin OBS wrapper over Muninn's typed -CultCache/CultMesh stream catalog. It reads Muninn's local `.cc` store: +CultCache/CultMesh stream catalog. The operator-facing UI is intentionally just +the stream dropdown. Store paths, command endpoints, and media return endpoints +are source defaults and migration state, not user-facing stream selection. + +The source reads Muninn's local `.cc` store: ```text C:\Meta\Odin\state\muninn.telemetry.cc @@ -141,6 +145,22 @@ media profile, the source publishes the same typed Muninn `serve` still owns whether capture starts and which local FFmpeg/WASAPI children exist. +If the local catalog projection is absent or stale, the source keeps a +LAN-default Raven A/V stream option instead of showing an empty source. That is +a resilience fallback for the OBS machine, not a second stream authority. Stale +catalog `rudp://` URLs that do not carry the current low-latency LAN profile are +sanitized back to the source-derived URL before the bridge starts. + +The current Raven-room default route is LAN-first: command requests go to +`192.168.1.84:17873`, and Raven sends media back to Starfire at +`192.168.1.66:5204`. Older saved source settings that still point at the +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. + For `rudp://` stream URLs, the plugin binds the advertised media port, accepts the CultNet RUDP handshake, acknowledges packets, reassembles RUDP fragments, decodes typed `muninn.media_video_access_unit.v1` and diff --git a/native/obs_stem_source/mimir_obs_muninn_source.cpp b/native/obs_stem_source/mimir_obs_muninn_source.cpp index 693f442..ee5a544 100644 --- a/native/obs_stem_source/mimir_obs_muninn_source.cpp +++ b/native/obs_stem_source/mimir_obs_muninn_source.cpp @@ -36,13 +36,17 @@ namespace { constexpr const char *MuninnStorePath = "C:\\Meta\\Odin\\state\\muninn.telemetry.cc"; constexpr const char *MuninnCommandExePath = "C:\\Meta\\Odin\\Muninn\\muninn.exe"; -constexpr const char *MuninnCommandRudpTarget = "10.77.0.4:17873"; +constexpr const char *MuninnCommandRudpTarget = "192.168.1.84:17873"; +constexpr const char *MuninnDeprecatedWireGuardCommandRudpTarget = "10.77.0.4:17873"; constexpr const char *MuninnDefaultHostId = "raven"; -constexpr const char *MuninnDefaultMediaTargetHost = "10.77.0.2"; +constexpr const char *MuninnDefaultMediaTargetHost = "192.168.1.66"; +constexpr const char *MuninnDeprecatedWireGuardMediaTargetHost = "10.77.0.2"; constexpr uint16_t MuninnDefaultMediaPort = 5204; 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 = 30000; struct MuninnStreamOption { std::string stream_id; @@ -78,6 +82,9 @@ struct MuninnSource { std::string stream_id; std::string stream_url; std::string last_requested_stream_id; + std::vector catalog_options; + 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(); }; static void muninn_child_audio(void *param, obs_source_t *source, const struct audio_data *audio_data, bool muted); @@ -606,6 +613,8 @@ private: bytes_forwarded_ = 0; payloads_decoded_dropped_ = 0; video_assemblies_expired_ = 0; + video_recovery_dropped_ = 0; + waiting_for_video_keyframe_ = true; video_forward_failures_ = 0; audio_forward_failures_ = 0; const auto [ack, ack_mask] = ack_tracker_.state(); @@ -699,6 +708,14 @@ private: if (!assembled) { return; } + if (waiting_for_video_keyframe_ && !h264_access_unit_has_idr(*assembled)) { + ++video_recovery_dropped_; + log_bridge_pressure("dropped video payload while waiting for decoder recovery keyframe"); + return; + } + if (h264_access_unit_has_idr(*assembled)) { + waiting_for_video_keyframe_ = false; + } forwarded_bytes = assembled->size(); const int sent = sendto(video_socket, reinterpret_cast(assembled->data()), static_cast(assembled->size()), 0, reinterpret_cast(&video_addr), sizeof(video_addr)); @@ -798,6 +815,7 @@ private: send_receiver_feedback(iterator->second, missing_chunk_keys, bridge_sequence); iterator = video_assemblies_.erase(iterator); ++video_assemblies_expired_; + waiting_for_video_keyframe_ = true; log_bridge_pressure("expired incomplete video frame assembly"); } } @@ -837,21 +855,53 @@ private: { const uint64_t pressure = payloads_decoded_dropped_ + video_assemblies_expired_ + packet_fragments_.expired_count() + - video_forward_failures_ + audio_forward_failures_; + video_recovery_dropped_ + video_forward_failures_ + audio_forward_failures_; if (pressure == 1 || pressure % 300 == 0) { blog(LOG_WARNING, - "Muninn RUDP bridge %s: forwarded=%llu bytes=%llu decode_dropped=%llu assemblies_expired=%llu packet_fragments_expired=%llu video_failures=%llu audio_failures=%llu", + "Muninn RUDP bridge %s: forwarded=%llu bytes=%llu decode_dropped=%llu assemblies_expired=%llu packet_fragments_expired=%llu recovery_dropped=%llu video_failures=%llu audio_failures=%llu", reason, static_cast(packets_forwarded_), static_cast(bytes_forwarded_), static_cast(payloads_decoded_dropped_), static_cast(video_assemblies_expired_), static_cast(packet_fragments_.expired_count()), + static_cast(video_recovery_dropped_), static_cast(video_forward_failures_), static_cast(audio_forward_failures_)); } } + static bool h264_access_unit_has_idr(const std::vector &access_unit) + { + for (size_t index = 0; index + 4 < access_unit.size();) { + size_t start_code = std::string::npos; + size_t nal_offset = 0; + for (size_t cursor = index; cursor + 3 < access_unit.size(); ++cursor) { + if (access_unit[cursor] == 0x00 && access_unit[cursor + 1] == 0x00) { + if (access_unit[cursor + 2] == 0x01) { + start_code = cursor; + nal_offset = cursor + 3; + break; + } + if (cursor + 4 < access_unit.size() && access_unit[cursor + 2] == 0x00 && access_unit[cursor + 3] == 0x01) { + start_code = cursor; + nal_offset = cursor + 4; + break; + } + } + } + if (start_code == std::string::npos || nal_offset >= access_unit.size()) { + return false; + } + const uint8_t nal_type = access_unit[nal_offset] & 0x1f; + if (nal_type == 5) { + return true; + } + index = nal_offset + 1; + } + return false; + } + void send_control_packet(uint8_t packet_type, uint8_t flags, uint32_t sequence, uint32_t ack, uint32_t ack_mask, const sockaddr_in &remote) { const std::string channel = "control"; @@ -921,8 +971,10 @@ private: uint64_t bytes_forwarded_ = 0; uint64_t payloads_decoded_dropped_ = 0; uint64_t video_assemblies_expired_ = 0; + uint64_t video_recovery_dropped_ = 0; uint64_t video_forward_failures_ = 0; uint64_t audio_forward_failures_ = 0; + bool waiting_for_video_keyframe_ = true; std::string source_url_; RudpBridgeOutputs outputs_; }; @@ -943,6 +995,21 @@ static const MuninnStreamOption *find_selected(const std::vector &args) static std::string expected_rudp_stream_url(const MuninnSource *ctx, const std::string &stream_id) { return "rudp://" + ctx->media_target_host + ":" + std::to_string(ctx->media_port) + "/" + stream_id + - "?channel=media&format=muninn-typed-media&connection=0x6d750001"; + "?channel=media&format=muninn-typed-media&connection=0x6d750001&profile=muninn.rudp.low_latency_h264_lan.v1&sender_resend_delay_ms=5&reliable_expire_after_ms=250&assembly_deadline_ms=150&gap_wait_ms=8"; +} + +static std::string playable_rudp_stream_url(const MuninnSource *ctx, const MuninnStreamOption &selected) +{ + if (starts_with(selected.url, "rudp://") && + selected.url.find("reliable_expire_after_ms=") != std::string::npos && + selected.url.find("assembly_deadline_ms=") != std::string::npos) { + return selected.url; + } + return expected_rudp_stream_url(ctx, selected.stream_id); } static std::string canonical_muninn_stream_id(const std::string &stream_id) @@ -1029,16 +1106,29 @@ static std::string canonical_muninn_stream_id(const std::string &stream_id) return stream_id; } +static std::string endpoint_setting(obs_data_t *settings, const char *key, const char *current_default, const char *deprecated_default) +{ + std::string value = obs_string(settings, key, current_default); + if (value == deprecated_default) { + obs_data_set_string(settings, key, current_default); + return current_default; + } + return value; +} + static void request_stream_activation(MuninnSource *ctx, const std::string &stream_id) { 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; } - if (ctx->last_requested_stream_id == canonical_stream_id) { + const auto now = std::chrono::steady_clock::now(); + if (ctx->last_requested_stream_id == canonical_stream_id && + now - ctx->last_activation_request < std::chrono::milliseconds(MuninnActivationRetryMs)) { return; } ctx->last_requested_stream_id = canonical_stream_id; + ctx->last_activation_request = now; const std::vector args = { ctx->command_exe_path, @@ -1313,12 +1403,16 @@ static std::vector decode_obs_catalog_payload(const std::vec if (stream_ids[index].empty()) { continue; } - options.push_back(MuninnStreamOption{ + MuninnStreamOption option{ stream_ids[index], labels[index].empty() ? stream_ids[index] : labels[index], urls[index], states[index], - }); + }; + if (!is_playable_muninn_stream(option)) { + continue; + } + options.push_back(std::move(option)); } std::sort(options.begin(), options.end(), [](const auto &left, const auto &right) { @@ -1447,7 +1541,7 @@ static void create_or_update_media(MuninnSource *ctx, const std::string &url) } const auto outputs = ctx->bridge->start(url); ctx->media = create_ffmpeg_child_source(ctx, "Muninn Stream Video", outputs.video_url, "h264", false); - ctx->audio_media = create_ffmpeg_child_source(ctx, "Muninn Stream Audio", outputs.audio_url, "adts", true); + ctx->audio_media = create_ffmpeg_child_source(ctx, "Muninn Stream Audio", outputs.audio_url, "aac", true); if (ctx->media || ctx->audio_media) { ctx->stream_url = url; } else { @@ -1466,13 +1560,51 @@ static void create_or_update_media(MuninnSource *ctx, const std::string &url) } } +static void refresh_catalog(MuninnSource *ctx) +{ + if (!ctx) { + return; + } + ctx->catalog_options = read_catalog(ctx->store_path); + if (ctx->catalog_options.empty()) { + ctx->catalog_options.push_back(default_muninn_stream_option()); + } + ctx->next_catalog_refresh = + std::chrono::steady_clock::now() + std::chrono::milliseconds(MuninnCatalogRefreshMs); +} + +static void reconcile_selected_stream(MuninnSource *ctx) +{ + if (!ctx) { + return; + } + if (ctx->catalog_options.empty()) { + create_or_update_media(ctx, ""); + return; + } + + const auto *selected = find_selected(ctx->catalog_options, ctx->stream_id); + if (!selected) { + create_or_update_media(ctx, ""); + return; + } + + const bool selection_changed = ctx->stream_id != selected->stream_id; + ctx->stream_id = selected->stream_id; + if (selection_changed) { + ctx->last_requested_stream_id.clear(); + } + + request_stream_activation(ctx, selected->stream_id); + create_or_update_media(ctx, playable_rudp_stream_url(ctx, *selected)); +} + static void populate_stream_list(obs_property_t *property, const std::string &store_path) { obs_property_list_clear(property); - const auto options = read_catalog(store_path); + auto options = read_catalog(store_path); if (options.empty()) { - obs_property_list_add_string(property, "No active Muninn streams", ""); - return; + options.push_back(default_muninn_stream_option()); } for (const auto &option : options) { @@ -1566,30 +1698,35 @@ static void stop_audio_worker(MuninnSource *ctx) static void muninn_update(void *data, obs_data_t *settings) { auto *ctx = static_cast(data); + const auto previous_command_rudp_target = ctx->command_rudp_target; + const auto previous_media_target_host = ctx->media_target_host; + const auto previous_media_port = ctx->media_port; + ctx->store_path = obs_string(settings, "store_path", MuninnStorePath); ctx->command_exe_path = obs_string(settings, "command_exe_path", MuninnCommandExePath); - ctx->command_rudp_target = obs_string(settings, "command_rudp_target", MuninnCommandRudpTarget); + ctx->command_rudp_target = endpoint_setting( + settings, + "command_rudp_target", + MuninnCommandRudpTarget, + MuninnDeprecatedWireGuardCommandRudpTarget); ctx->host_id = obs_string(settings, "host_id", MuninnDefaultHostId); - ctx->media_target_host = obs_string(settings, "media_target_host", MuninnDefaultMediaTargetHost); + ctx->media_target_host = endpoint_setting( + settings, + "media_target_host", + MuninnDefaultMediaTargetHost, + MuninnDeprecatedWireGuardMediaTargetHost); ctx->media_port = static_cast(obs_data_get_int(settings, "media_port") > 0 ? obs_data_get_int(settings, "media_port") : MuninnDefaultMediaPort); ctx->stream_id = obs_string(settings, "stream_id", ""); - - const auto options = read_catalog(ctx->store_path); - const auto *selected = find_selected(options, ctx->stream_id); - if (!selected) { - create_or_update_media(ctx, ""); - return; + if (ctx->command_rudp_target != previous_command_rudp_target || + ctx->media_target_host != previous_media_target_host || + ctx->media_port != previous_media_port) { + ctx->last_requested_stream_id.clear(); } - ctx->stream_id = selected->stream_id; - request_stream_activation(ctx, selected->stream_id); - if (selected->url.empty()) { - create_or_update_media(ctx, expected_rudp_stream_url(ctx, selected->stream_id)); - } else { - create_or_update_media(ctx, selected->url); - } + refresh_catalog(ctx); + reconcile_selected_stream(ctx); } static void *muninn_create(obs_data_t *settings, obs_source_t *source) @@ -1639,18 +1776,23 @@ static obs_properties_t *muninn_properties(void *data) const auto store_path = ctx ? ctx->store_path : std::string(MuninnStorePath); obs_properties_t *props = obs_properties_create(); - obs_properties_add_text(props, "store_path", "Muninn CultCache store", OBS_TEXT_DEFAULT); - obs_properties_add_text(props, "command_exe_path", "Muninn command executable", OBS_TEXT_DEFAULT); - obs_properties_add_text(props, "command_rudp_target", "Muninn command RUDP target", OBS_TEXT_DEFAULT); - obs_properties_add_text(props, "host_id", "Muninn host", OBS_TEXT_DEFAULT); - obs_properties_add_text(props, "media_target_host", "Muninn media target host", OBS_TEXT_DEFAULT); - obs_properties_add_int(props, "media_port", "Muninn media UDP port", 1, 65535, 1); obs_property_t *streams = obs_properties_add_list(props, "stream_id", "Muninn stream", OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING); populate_stream_list(streams, store_path); - obs_properties_add_button(props, "refresh_streams", "Refresh Muninn streams", muninn_refresh_streams); return props; } +static void muninn_video_tick(void *data, float) +{ + auto *ctx = static_cast(data); + if (!ctx) { + return; + } + if (std::chrono::steady_clock::now() >= ctx->next_catalog_refresh) { + refresh_catalog(ctx); + } + reconcile_selected_stream(ctx); +} + static void muninn_render(void *data, gs_effect_t *) { auto *ctx = static_cast(data); @@ -1721,6 +1863,7 @@ static obs_source_info muninn_source_info = { .get_defaults = muninn_defaults, .get_properties = muninn_properties, .update = muninn_update, + .video_tick = muninn_video_tick, .video_render = muninn_render, .enum_active_sources = muninn_enum_active_sources, }; diff --git a/state/evidence.jsonl b/state/evidence.jsonl index 29d2554..a3c047b 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -378,3 +378,5 @@ {"ts": "2026-06-04T11:07:48+01:00", "type": "decision", "status": "ok", "note": "Existing Well captures are synchronization evidence, not continuous media bodies: future recording/streaming daemons must drink typed stream-frame/body refs from the Well, while the Well owns ingest, clock mapping, source heartbeat/reacquire, and operator-alert publication only."} {"ts": "2026-06-07T17:23:25+01:00", "type": "implementation", "status": "ok", "note": "Added Mimir.RavenDaemon as Raven-local capture mux supervisor: publishes mimir.raven_capture_mux_state.v1 via CultMesh/CultCache, exposes Eve dashboard state on 8801, and reuses FFmpeg/WASAPI for display plus Realtek loopback mux to Starfire over WireGuard."} {"ts": "2026-06-18T23:29:22+01:00", "type": "decision", "status": "ok", "note": "Muninn OBS RUDP receive bridge demoted packet sequence from media ordering authority. CultNet media packets remain reliable/unordered; the plugin now forwards decoded typed media payloads immediately, while video frame_id/chunk_id plus assembly_deadline_ms own reconstruction and receiver feedback requests keyframes for expired incomplete frames."} +{"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."}