-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebRTSPPlayer.tsx
More file actions
186 lines (166 loc) · 4.48 KB
/
WebRTSPPlayer.tsx
File metadata and controls
186 lines (166 loc) · 4.48 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { useEffect, useRef, useState } from "react";
import {
LoaderCircleIcon,
CircleXIcon,
VideoIcon,
CirclePlayIcon
} from "lucide-react";
import { Log, FormatTag } from "webrtsp.ts/helpers/Log";
import { WebRTSPPlayer as Player } from "webrtsp.ts/WebRTSPPlayer";
import { WebRTSP } from "./useWebRTSP";
import "./WebRTSPPlayer.css";
import type { ClassValue } from "clsx";
const TAG = FormatTag("WebRTSP.Client");
const ConnectionState = {
New: "new",
Connecting: "connecting",
Connected: "connected",
Disconnected: "disconnected",
Failed: "failed",
Closed: "closed",
} as const;
type ConnectionState = typeof ConnectionState[keyof typeof ConnectionState];
function WebRTSPPlayer(
props: {
className?: ClassValue,
webRTSP: WebRTSP,
activeStreamer?: string,
activeStreamerRev?: number,
incActiveStreamerRev: () => void,
}
) {
const videoRef = useRef<HTMLVideoElement>(null);
const playerRef = useRef<Player>(undefined);
type OptionalConnectionState = ConnectionState | undefined;
const [connectionState, setConnectionState] = useState<OptionalConnectionState>();
const [canPlay, setCanPlay] = useState(false);
const webRTSP = props.webRTSP;
const activeStreamer = props.activeStreamer;
const activeStreamerRev = props.activeStreamerRev;
const incActiveStreamerRev = props.incActiveStreamerRev;
useEffect(() => {
const video = videoRef.current;
if(
!video ||
!webRTSP.connection || !webRTSP.connected ||
!activeStreamer
) {
return;
}
let active = true;
setConnectionState(ConnectionState.New);
video.addEventListener("canplay", () => {
if(active) {
setCanPlay(true);
}
});
const player = new Player(
webRTSP.connection,
[{
urls: ["stun:stun.l.google.com:19302"]
}],
activeStreamer,
video,
);
playerRef.current = player;
player.events.addEventListener("connectionstatechanged", (event) => {
if(!(event instanceof CustomEvent))
return;
if(active) {
setConnectionState(event.detail.connectionstate);
}
});
player.play().catch((error: unknown) => {
Log.error(TAG, "play() failed:", error);
if(active) {
setConnectionState(ConnectionState.Failed);
}
});
return () => {
active = false;
player.stop();
setConnectionState(undefined);
setCanPlay(false);
playerRef.current = undefined;
};
}, [
webRTSP.connection,
webRTSP.connected,
activeStreamer,
activeStreamerRev
]);
const idle = activeStreamer == undefined;
const loading = connectionState &&
([
ConnectionState.New,
ConnectionState.Connecting,
ConnectionState.Disconnected
] as string[]).includes(connectionState);
const playing = connectionState &&
([
ConnectionState.Connected,
ConnectionState.Disconnected,
ConnectionState.Closed,
] as string[]).includes(connectionState);
const canRestart = connectionState &&
([
ConnectionState.Closed,
] as string[]).includes(connectionState);
const failed = connectionState == ConnectionState.Failed;
const stateIconClassNameCommon = `
absolute
max-w-1/2 max-h-1/2
w-40 h-40
top-0 bottom-0 left-0 right-0
m-auto`;
return (
<div className = { `relative ${props.className}` }>
{
idle && <VideoIcon
className = {`
${stateIconClassNameCommon}
stroke-primary-500
`}/>
}
{
failed && <CircleXIcon
className = {`
${stateIconClassNameCommon}
stroke-destructive-500
`}
/>
}
<video
className = {`
absolute
max-w-full max-h-full
top-0 bottom-0 left-0 right-0
m-auto
bg-black
`}
ref = { videoRef } muted autoPlay hidden = { !playing || !canPlay } />
{
(loading || (playing && !canPlay && !canRestart)) && <LoaderCircleIcon
className = {`
${stateIconClassNameCommon}
stroke-primary-200
animate-spin
`}
/>
}
{
canRestart && <CirclePlayIcon
className = {`
${stateIconClassNameCommon}
stroke-primary-transparent-400
hover:stroke-primary-transparent-700
`}
onClick = {() => {
incActiveStreamerRev();
}}
/>
}
</div>
);
}
export default WebRTSPPlayer;