aboutsummaryrefslogtreecommitdiffstats
path: root/frame-steamos/recovery-watchdog.c
blob: d6dfa8ba12fcf486aaef6a7f10871660e9131984 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// SPDX-FileCopyrightText: 2026 Tomasz Kramkowski <tomasz@kramkow.ski>
// SPDX-License-Identifier: CC0-1.0

#include <layout.h>
#include <stdbool.h>
#include <stdint.h>

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);
}