aboutsummaryrefslogtreecommitdiffstats
path: root/frame-steamos/recovery-watchdog.c
diff options
context:
space:
mode:
Diffstat (limited to 'frame-steamos/recovery-watchdog.c')
-rw-r--r--frame-steamos/recovery-watchdog.c97
1 files changed, 97 insertions, 0 deletions
diff --git a/frame-steamos/recovery-watchdog.c b/frame-steamos/recovery-watchdog.c
new file mode 100644
index 0000000..d557c9d
--- /dev/null
+++ b/frame-steamos/recovery-watchdog.c
@@ -0,0 +1,97 @@
+// SPDX-FileCopyrightText: 2026 Tomasz Kramkowski <tomasz@kramkow.ski>
+// SPDX-License-Identifier: CC0-1.0
+
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+
+#define ASSERT_OFFSET(type, field, expected) \
+ _Static_assert( \
+ offsetof(type, field) == (expected), #type "." #field " offset" \
+ )
+
+struct CodecVtable;
+
+// SVLCodecV4L2
+struct Codec {
+ const struct CodecVtable *vtable;
+ unsigned char unknown_008[0x30 - 0x08];
+ uint8_t initialized;
+ unsigned char unknown_031[0x13c - 0x31];
+ uint8_t media_thread_quitting;
+ unsigned char unknown_13d[0x154 - 0x13d];
+ int32_t reset_state;
+ unsigned char unknown_158[0x1c8 - 0x158];
+ uint8_t reset_thread_quitting;
+};
+ASSERT_OFFSET(struct Codec, initialized, 0x30);
+ASSERT_OFFSET(struct Codec, media_thread_quitting, 0x13c);
+ASSERT_OFFSET(struct Codec, reset_state, 0x154);
+ASSERT_OFFSET(struct Codec, reset_thread_quitting, 0x1c8);
+
+struct Renderer {
+ unsigned char unknown_000[0x10];
+ struct Codec *codec;
+};
+ASSERT_OFFSET(struct Renderer, codec, 0x10);
+
+struct Client {
+ unsigned char unknown_000[0x1b0];
+ struct Renderer *renderer;
+ unsigned char unknown_1b8[0x7e06 - 0x1b8];
+ uint8_t did_ever_receive_bookend;
+};
+ASSERT_OFFSET(struct Client, renderer, 0x1b0);
+ASSERT_OFFSET(struct Client, did_ever_receive_bookend, 0x7e06);
+
+struct FecReceiver {
+ unsigned char unknown_000[0x88];
+ int32_t last_submitted_frame;
+ unsigned char unknown_08c[0xb90 - 0x8c];
+ struct Client *client;
+};
+ASSERT_OFFSET(struct FecReceiver, last_submitted_frame, 0x88);
+ASSERT_OFFSET(struct FecReceiver, client, 0xb90);
+
+extern const struct CodecVtable v4l2_vtable __attribute__((visibility("hidden")));
+extern void fec_error_check(struct FecReceiver *);
+// SVLCodecV4L2::CheckStuck
+extern void check_stuck(struct Codec *, bool debug);
+
+// The `EventsThread` runs separately from the video receiving code, and handles
+// scheduled work.
+// There's an existing periodic event that re-schedules itself every 1ms that
+// handles various network related bookkeeping, checks and maintenance. This
+// hooks at the end of that function where it calls a FEC error checking
+// function while holding the receiver lock.
+void recovery_watchdog(struct FecReceiver *fec)
+{
+ // Call the function whose call the hook replaces.
+ fec_error_check(fec);
+ if (fec->last_submitted_frame != -1) return;
+
+ struct Client *client = fec->client;
+ if (!client) return;
+
+ // Decoder initialization happens before the host starts sending video.
+ // Do not run the watchdog until this stream has received its first
+ // "bookend".
+ if (!__atomic_load_n(&client->did_ever_receive_bookend, __ATOMIC_ACQUIRE))
+ return;
+ struct Renderer *renderer = client->renderer;
+ if (!renderer || !renderer->codec) return;
+ struct Codec *codec = renderer->codec;
+ if (codec->vtable != &v4l2_vtable) return;
+ if (__atomic_load_n(&codec->reset_state, __ATOMIC_ACQUIRE)) return;
+ if (!codec->initialized) return;
+ if (__atomic_load_n(&codec->media_thread_quitting, __ATOMIC_ACQUIRE)) return;
+ if (__atomic_load_n(&codec->reset_thread_quitting, __ATOMIC_ACQUIRE)) return;
+
+ // SVLCodecV4L2::CheckStuck is normally called after submitting a frame
+ // for decoding (and also as part of some decoder error handling) and
+ // checks if the decoder is making progress. It requests a stream-reset
+ // after ~330ms of no progress. And then a local decoder reset after
+ // 800ms of no progress. We call it here solely for the stream-reset
+ // functionality.
+ check_stuck(codec, false);
+}