-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseWebRTSP.ts
More file actions
168 lines (139 loc) · 4.03 KB
/
useWebRTSP.ts
File metadata and controls
168 lines (139 loc) · 4.03 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
import { useState, useRef, useEffect, useCallback } from "react";
import { WebRTSPClient } from "webrtsp.ts/WebRTSPClient";
import { Method, Options, URI2Description } from "webrtsp.ts/Types";
import { Log, FormatTag } from "webrtsp.ts/helpers/Log";
import { useLazyRef } from "./useLazyRef";
const TAG = FormatTag("useWebRTSP");
export interface URIInfo {
fetching: boolean
options: Options
list: URI2Description
}
export class URI2Info extends Map<string, URIInfo> {}
export class WebRTSP {
connection?: WebRTSPClient;
connected: boolean = false;
rootOptions = new Options();
rootList = new URI2Description();
fetching: boolean = false;
urisInfos = new URI2Info();
fetchList: (uri: string) => Promise<void> = () => { return Promise.resolve(); };
}
export function useWebRTSP(url: string): WebRTSP {
const clientRef = useRef<WebRTSPClient>(undefined);
const [connected, setConnected] = useState(false);
const [rootOptions, setRootOptions] = useState(() => new Options());
const [rootList, setRootList] = useState(() => new URI2Description());
const [fetching, setFetching] = useState(false);
const urisInfosRef = useLazyRef(() => new URI2Info());
const [, setUrisInfosRev] = useState(0);
const incUrisInfosRev = () => {
setUrisInfosRev((rev: number) => {
return rev >= Number.MAX_SAFE_INTEGER ? 0 : rev + 1;
});
};
useEffect(() => {
clientRef.current = new WebRTSPClient(url);
let active = true;
const resetState = () => {
setConnected(false);
setRootOptions(new Options());
setRootList(new URI2Description());
setFetching(false);
urisInfosRef.current.clear();
incUrisInfosRev();
};
const client = clientRef.current;
client.onConnected = () => {
if(active) {
setConnected(true);
}
};
client.onDisconnected = () => {
if(active) {
resetState();
}
};
client.connect();
return () => {
active = false;
client.disconnect().catch();
clientRef.current = undefined;
resetState();
};
}, [url, urisInfosRef]);
useEffect(() => {
const client = clientRef.current;
if(!client || !connected)
return;
let ignoreResult = false;
const fetchOptions = async () => {
try {
const rootOptions = await client.OPTIONS("*");
if(ignoreResult)
return;
setRootOptions(rootOptions);
if(rootOptions.has(Method.LIST)) {
const list = await client.LIST("*");
if(ignoreResult)
return;
setRootList(list);
const urisInfo = new URI2Info();
for (const [key] of list) {
let uriOptions = new Options;
try {
uriOptions = await client.OPTIONS(key);
} catch(e: unknown) {
Log.error(TAG, e);
}
urisInfo.set(key, {
fetching: false,
options: uriOptions,
list: new URI2Description(),
});
if(ignoreResult)
return;
}
urisInfosRef.current = urisInfo;
incUrisInfosRev();
}
} catch(e: unknown) {
Log.error(TAG, e);
}
};
setFetching(true);
fetchOptions()
.catch()
.finally(() => {
if(!ignoreResult)
setFetching(false);
});
return () => {
ignoreResult = true;
};
}, [clientRef, connected, urisInfosRef]);
const fetchList = useCallback(async (uri: string) => {
const client = clientRef.current;
if(!client || !connected)
return;
const urisInfos = urisInfosRef.current;
const uriInfo = urisInfos.get(uri);
if(!uriInfo)
return;
uriInfo.fetching = true;
incUrisInfosRev();
const list = await client.LIST(uri);
uriInfo.list = list;
uriInfo.fetching = false;
incUrisInfosRev();
}, [connected, urisInfosRef]);
return {
connection: clientRef.current,
connected,
rootOptions,
rootList,
fetching,
urisInfos: urisInfosRef.current,
fetchList,
};
}