tangled
alpha
login
or
join now
stream.place
/
streamplace
Live video on the AT Protocol
74
fork
atom
overview
issues
1
pulls
pipelines
simplify to 'offline'-based video handling
Natalie B.
7 months ago
3e01ac93
69c6700a
+18
-23
1 changed file
expand all
collapse all
unified
split
js
app
components
player
video-retry.tsx
+18
-23
js/app/components/player/video-retry.tsx
···
1
1
-
import {
2
2
-
usePlayerStore,
3
3
-
useSegment,
4
4
-
useStreamplaceStore,
5
5
-
} from "@streamplace/components";
6
6
-
import React, { useEffect } from "react";
7
7
-
8
8
-
import { useRef } from "react";
1
1
+
import { usePlayerStore } from "@streamplace/components";
2
2
+
import React, { useEffect, useRef, useState } from "react";
9
3
10
4
export default function VideoRetry(props: { children: React.ReactNode }) {
11
5
const retryTimeoutRef = useRef<NodeJS.Timeout | null>(null);
12
12
-
// last segment start time
13
13
-
let [lastSegST, setLastSegST] = React.useState<string | null>(null);
14
14
-
const segment = useSegment();
6
6
+
const [retries, setRetries] = useState(0);
7
7
+
const [hasStarted, setHasStarted] = useState(false);
15
8
16
9
const offline = usePlayerStore((x) => x.offline);
17
17
-
const spurl = useStreamplaceStore((x) => x.url);
18
10
19
11
useEffect(() => {
20
20
-
if (
21
21
-
lastSegST !== null &&
22
22
-
segment &&
23
23
-
segment.startTime !== lastSegST &&
24
24
-
offline
25
25
-
) {
26
26
-
console.log("Timeout detected!");
12
12
+
if (!offline && !hasStarted) {
13
13
+
console.log("Player is online. Marking as started.");
14
14
+
setHasStarted(true);
15
15
+
}
16
16
+
17
17
+
if (offline) {
18
18
+
console.log("Player is offline. Incrementing retries.");
19
19
+
setRetries((prevRetries) => prevRetries + 1);
20
20
+
27
21
const jitter = 500 + Math.random() * 1500;
28
22
retryTimeoutRef.current = setTimeout(() => {
29
29
-
console.log("Retrying video segment", segment.startTime);
30
30
-
setLastSegST(segment.startTime);
23
23
+
console.log("Retrying video playback...");
31
24
}, jitter);
32
25
}
26
26
+
33
27
return () => {
34
28
if (retryTimeoutRef.current) {
29
29
+
console.log("Clearing retry timeout");
35
30
clearTimeout(retryTimeoutRef.current);
36
31
}
37
32
};
38
38
-
}, [offline, segment, spurl, lastSegST]);
33
33
+
}, [offline, hasStarted]);
39
34
40
40
-
return <React.Fragment key={lastSegST}>{props.children}</React.Fragment>;
35
35
+
return <React.Fragment key={retries}>{props.children}</React.Fragment>;
41
36
}