deepsea-components 5.17.36 → 5.18.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/RtspPlayer.cjs +113 -0
- package/dist/components/RtspPlayer.d.ts +9 -0
- package/dist/components/RtspPlayer.js +79 -0
- package/dist/components/rtspPlayer/reader.cjs +423 -0
- package/dist/components/rtspPlayer/reader.d.ts +46 -0
- package/dist/components/rtspPlayer/reader.js +389 -0
- package/dist/index.cjs +48 -39
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +3 -3
- package/src/components/RtspPlayer.tsx +93 -0
- package/src/components/rtspPlayer/reader.ts +554 -0
- package/src/index.ts +1 -0
- package/tsconfig.json +8 -4
|
@@ -0,0 +1,554 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Derived from MediaMTX reader.js:
|
|
3
|
+
* https://github.com/bluenviron/mediamtx/blob/main/internal/servers/webrtc/reader.js
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2019 aler9
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
* in the Software without restriction, including without limitation the rights
|
|
12
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
* furnished to do so, subject to the following conditions:
|
|
15
|
+
*
|
|
16
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
17
|
+
* copies or substantial portions of the Software.
|
|
18
|
+
*
|
|
19
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
* SOFTWARE.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
export interface MediaMTXWebRTCReaderConf {
|
|
29
|
+
url: string
|
|
30
|
+
user?: string
|
|
31
|
+
pass?: string
|
|
32
|
+
token?: string
|
|
33
|
+
onError?: (err: string) => void
|
|
34
|
+
onTrack?: (evt: RTCTrackEvent) => void
|
|
35
|
+
onDataChannel?: (evt: RTCDataChannelEvent) => void
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface OfferData {
|
|
39
|
+
iceUfrag: string
|
|
40
|
+
icePwd: string
|
|
41
|
+
medias: string[]
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
type RTCIceServerWithCredentialType = RTCIceServer & {
|
|
45
|
+
credentialType?: "password"
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
type ReaderState = "getting_codecs" | "running" | "restarting" | "failed" | "closed"
|
|
49
|
+
|
|
50
|
+
export class MediaMTXWebRTCReader {
|
|
51
|
+
private readonly retryPause = 2000
|
|
52
|
+
private readonly conf: MediaMTXWebRTCReaderConf
|
|
53
|
+
private state: ReaderState = "getting_codecs"
|
|
54
|
+
private restartTimeout: number | null = null
|
|
55
|
+
private pc: RTCPeerConnection | null = null
|
|
56
|
+
private offerData: OfferData | null = null
|
|
57
|
+
private sessionUrl: string | null = null
|
|
58
|
+
private queuedCandidates: RTCIceCandidate[] = []
|
|
59
|
+
private nonAdvertisedCodecs: string[] = []
|
|
60
|
+
|
|
61
|
+
constructor(conf: MediaMTXWebRTCReaderConf) {
|
|
62
|
+
this.conf = conf
|
|
63
|
+
this.getNonAdvertisedCodecs()
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
close() {
|
|
67
|
+
this.state = "closed"
|
|
68
|
+
|
|
69
|
+
if (this.pc) {
|
|
70
|
+
this.pc.close()
|
|
71
|
+
this.pc = null
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (this.restartTimeout !== null) {
|
|
75
|
+
clearTimeout(this.restartTimeout)
|
|
76
|
+
this.restartTimeout = null
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
private static supportsNonAdvertisedCodec(codec: string, fmtp?: string) {
|
|
81
|
+
return new Promise<boolean>(resolve => {
|
|
82
|
+
const pc = new RTCPeerConnection({ iceServers: [] })
|
|
83
|
+
const mediaType = "audio"
|
|
84
|
+
let payloadType = ""
|
|
85
|
+
|
|
86
|
+
pc.addTransceiver(mediaType, { direction: "recvonly" })
|
|
87
|
+
pc.createOffer()
|
|
88
|
+
.then(offer => {
|
|
89
|
+
if (offer.sdp === undefined) throw new Error("SDP not present")
|
|
90
|
+
if (offer.sdp.includes(` ${codec}`)) throw new Error("already present")
|
|
91
|
+
|
|
92
|
+
const sections = offer.sdp.split(`m=${mediaType}`)
|
|
93
|
+
const payloadTypes = sections
|
|
94
|
+
.slice(1)
|
|
95
|
+
.map(section => section.split("\r\n")[0].split(" ").slice(3))
|
|
96
|
+
.reduce<string[]>((prev, cur) => [...prev, ...cur], [])
|
|
97
|
+
|
|
98
|
+
payloadType = MediaMTXWebRTCReader.reservePayloadType(payloadTypes)
|
|
99
|
+
|
|
100
|
+
const lines = sections[1].split("\r\n")
|
|
101
|
+
lines[0] += ` ${payloadType}`
|
|
102
|
+
lines.splice(lines.length - 1, 0, `a=rtpmap:${payloadType} ${codec}`)
|
|
103
|
+
if (fmtp !== undefined) lines.splice(lines.length - 1, 0, `a=fmtp:${payloadType} ${fmtp}`)
|
|
104
|
+
|
|
105
|
+
sections[1] = lines.join("\r\n")
|
|
106
|
+
offer.sdp = sections.join(`m=${mediaType}`)
|
|
107
|
+
|
|
108
|
+
return pc.setLocalDescription(offer)
|
|
109
|
+
})
|
|
110
|
+
.then(() =>
|
|
111
|
+
pc.setRemoteDescription(
|
|
112
|
+
new RTCSessionDescription({
|
|
113
|
+
type: "answer",
|
|
114
|
+
sdp:
|
|
115
|
+
"v=0\r\n" +
|
|
116
|
+
"o=- 6539324223450680508 0 IN IP4 0.0.0.0\r\n" +
|
|
117
|
+
"s=-\r\n" +
|
|
118
|
+
"t=0 0\r\n" +
|
|
119
|
+
"a=fingerprint:sha-256 0D:9F:78:15:42:B5:4B:E6:E2:94:3E:5B:37:78:E1:4B:54:59:A3:36:3A:E5:05:EB:27:EE:8F:D2:2D:41:29:25\r\n" +
|
|
120
|
+
`m=${mediaType} 9 UDP/TLS/RTP/SAVPF ${payloadType}\r\n` +
|
|
121
|
+
"c=IN IP4 0.0.0.0\r\n" +
|
|
122
|
+
"a=ice-pwd:7c3bf4770007e7432ee4ea4d697db675\r\n" +
|
|
123
|
+
"a=ice-ufrag:29e036dc\r\n" +
|
|
124
|
+
"a=sendonly\r\n" +
|
|
125
|
+
"a=rtcp-mux\r\n" +
|
|
126
|
+
`a=rtpmap:${payloadType} ${codec}\r\n` +
|
|
127
|
+
(fmtp !== undefined ? `a=fmtp:${payloadType} ${fmtp}\r\n` : ""),
|
|
128
|
+
}),
|
|
129
|
+
),
|
|
130
|
+
)
|
|
131
|
+
.then(() => resolve(true))
|
|
132
|
+
.catch(() => resolve(false))
|
|
133
|
+
.finally(() => pc.close())
|
|
134
|
+
})
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
private static unquoteCredential(value: string) {
|
|
138
|
+
return JSON.parse(`"${value}"`) as string
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
private static linkToIceServers(links: string | null) {
|
|
142
|
+
if (links === null) return []
|
|
143
|
+
|
|
144
|
+
return links
|
|
145
|
+
.split(", ")
|
|
146
|
+
.map(link => {
|
|
147
|
+
const match = link.match(/^<(.+?)>; rel="ice-server"(; username="(.*?)"; credential="(.*?)"; credential-type="password")?/i)
|
|
148
|
+
if (!match) return null
|
|
149
|
+
|
|
150
|
+
const ret: RTCIceServerWithCredentialType = { urls: [match[1]] }
|
|
151
|
+
|
|
152
|
+
if (match[3] !== undefined && match[4] !== undefined) {
|
|
153
|
+
ret.username = MediaMTXWebRTCReader.unquoteCredential(match[3])
|
|
154
|
+
ret.credential = MediaMTXWebRTCReader.unquoteCredential(match[4])
|
|
155
|
+
ret.credentialType = "password"
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return ret
|
|
159
|
+
})
|
|
160
|
+
.filter((server): server is RTCIceServer => server !== null)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
private static parseOffer(sdp: string) {
|
|
164
|
+
const ret: OfferData = {
|
|
165
|
+
iceUfrag: "",
|
|
166
|
+
icePwd: "",
|
|
167
|
+
medias: [],
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
for (const line of sdp.split("\r\n")) {
|
|
171
|
+
if (line.startsWith("m=")) ret.medias.push(line.slice("m=".length))
|
|
172
|
+
else if (ret.iceUfrag === "" && line.startsWith("a=ice-ufrag:")) ret.iceUfrag = line.slice("a=ice-ufrag:".length)
|
|
173
|
+
else if (ret.icePwd === "" && line.startsWith("a=ice-pwd:")) ret.icePwd = line.slice("a=ice-pwd:".length)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return ret
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
private static reservePayloadType(payloadTypes: string[]) {
|
|
180
|
+
// Everything is valid between 30 and 127, except the interval between 64 and 95.
|
|
181
|
+
// https://chromium.googlesource.com/external/webrtc/+/refs/heads/master/call/payload_type.h#29
|
|
182
|
+
for (let i = 30; i <= 127; i++) {
|
|
183
|
+
if ((i <= 63 || i >= 96) && !payloadTypes.includes(i.toString())) {
|
|
184
|
+
const payloadType = i.toString()
|
|
185
|
+
payloadTypes.push(payloadType)
|
|
186
|
+
return payloadType
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
throw new Error("unable to find a free payload type")
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
private static enableStereoPcmau(payloadTypes: string[], section: string) {
|
|
194
|
+
const lines = section.split("\r\n")
|
|
195
|
+
|
|
196
|
+
let payloadType = MediaMTXWebRTCReader.reservePayloadType(payloadTypes)
|
|
197
|
+
lines[0] += ` ${payloadType}`
|
|
198
|
+
lines.splice(lines.length - 1, 0, `a=rtpmap:${payloadType} PCMU/8000/2`)
|
|
199
|
+
lines.splice(lines.length - 1, 0, `a=rtcp-fb:${payloadType} transport-cc`)
|
|
200
|
+
|
|
201
|
+
payloadType = MediaMTXWebRTCReader.reservePayloadType(payloadTypes)
|
|
202
|
+
lines[0] += ` ${payloadType}`
|
|
203
|
+
lines.splice(lines.length - 1, 0, `a=rtpmap:${payloadType} PCMA/8000/2`)
|
|
204
|
+
lines.splice(lines.length - 1, 0, `a=rtcp-fb:${payloadType} transport-cc`)
|
|
205
|
+
|
|
206
|
+
return lines.join("\r\n")
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
private static enableMultichannelOpus(payloadTypes: string[], section: string) {
|
|
210
|
+
const lines = section.split("\r\n")
|
|
211
|
+
|
|
212
|
+
let payloadType = MediaMTXWebRTCReader.reservePayloadType(payloadTypes)
|
|
213
|
+
lines[0] += ` ${payloadType}`
|
|
214
|
+
lines.splice(lines.length - 1, 0, `a=rtpmap:${payloadType} multiopus/48000/3`)
|
|
215
|
+
lines.splice(lines.length - 1, 0, `a=fmtp:${payloadType} channel_mapping=0,2,1;num_streams=2;coupled_streams=1`)
|
|
216
|
+
lines.splice(lines.length - 1, 0, `a=rtcp-fb:${payloadType} transport-cc`)
|
|
217
|
+
|
|
218
|
+
payloadType = MediaMTXWebRTCReader.reservePayloadType(payloadTypes)
|
|
219
|
+
lines[0] += ` ${payloadType}`
|
|
220
|
+
lines.splice(lines.length - 1, 0, `a=rtpmap:${payloadType} multiopus/48000/4`)
|
|
221
|
+
lines.splice(lines.length - 1, 0, `a=fmtp:${payloadType} channel_mapping=0,1,2,3;num_streams=2;coupled_streams=2`)
|
|
222
|
+
lines.splice(lines.length - 1, 0, `a=rtcp-fb:${payloadType} transport-cc`)
|
|
223
|
+
|
|
224
|
+
payloadType = MediaMTXWebRTCReader.reservePayloadType(payloadTypes)
|
|
225
|
+
lines[0] += ` ${payloadType}`
|
|
226
|
+
lines.splice(lines.length - 1, 0, `a=rtpmap:${payloadType} multiopus/48000/5`)
|
|
227
|
+
lines.splice(lines.length - 1, 0, `a=fmtp:${payloadType} channel_mapping=0,4,1,2,3;num_streams=3;coupled_streams=2`)
|
|
228
|
+
lines.splice(lines.length - 1, 0, `a=rtcp-fb:${payloadType} transport-cc`)
|
|
229
|
+
|
|
230
|
+
payloadType = MediaMTXWebRTCReader.reservePayloadType(payloadTypes)
|
|
231
|
+
lines[0] += ` ${payloadType}`
|
|
232
|
+
lines.splice(lines.length - 1, 0, `a=rtpmap:${payloadType} multiopus/48000/6`)
|
|
233
|
+
lines.splice(lines.length - 1, 0, `a=fmtp:${payloadType} channel_mapping=0,4,1,2,3,5;num_streams=4;coupled_streams=2`)
|
|
234
|
+
lines.splice(lines.length - 1, 0, `a=rtcp-fb:${payloadType} transport-cc`)
|
|
235
|
+
|
|
236
|
+
payloadType = MediaMTXWebRTCReader.reservePayloadType(payloadTypes)
|
|
237
|
+
lines[0] += ` ${payloadType}`
|
|
238
|
+
lines.splice(lines.length - 1, 0, `a=rtpmap:${payloadType} multiopus/48000/7`)
|
|
239
|
+
lines.splice(lines.length - 1, 0, `a=fmtp:${payloadType} channel_mapping=0,4,1,2,3,5,6;num_streams=4;coupled_streams=4`)
|
|
240
|
+
lines.splice(lines.length - 1, 0, `a=rtcp-fb:${payloadType} transport-cc`)
|
|
241
|
+
|
|
242
|
+
payloadType = MediaMTXWebRTCReader.reservePayloadType(payloadTypes)
|
|
243
|
+
lines[0] += ` ${payloadType}`
|
|
244
|
+
lines.splice(lines.length - 1, 0, `a=rtpmap:${payloadType} multiopus/48000/8`)
|
|
245
|
+
lines.splice(lines.length - 1, 0, `a=fmtp:${payloadType} channel_mapping=0,6,1,4,5,2,3,7;num_streams=5;coupled_streams=4`)
|
|
246
|
+
lines.splice(lines.length - 1, 0, `a=rtcp-fb:${payloadType} transport-cc`)
|
|
247
|
+
|
|
248
|
+
return lines.join("\r\n")
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
private static enableL16(payloadTypes: string[], section: string) {
|
|
252
|
+
const lines = section.split("\r\n")
|
|
253
|
+
|
|
254
|
+
let payloadType = MediaMTXWebRTCReader.reservePayloadType(payloadTypes)
|
|
255
|
+
lines[0] += ` ${payloadType}`
|
|
256
|
+
lines.splice(lines.length - 1, 0, `a=rtpmap:${payloadType} L16/8000/2`)
|
|
257
|
+
lines.splice(lines.length - 1, 0, `a=rtcp-fb:${payloadType} transport-cc`)
|
|
258
|
+
|
|
259
|
+
payloadType = MediaMTXWebRTCReader.reservePayloadType(payloadTypes)
|
|
260
|
+
lines[0] += ` ${payloadType}`
|
|
261
|
+
lines.splice(lines.length - 1, 0, `a=rtpmap:${payloadType} L16/16000/2`)
|
|
262
|
+
lines.splice(lines.length - 1, 0, `a=rtcp-fb:${payloadType} transport-cc`)
|
|
263
|
+
|
|
264
|
+
payloadType = MediaMTXWebRTCReader.reservePayloadType(payloadTypes)
|
|
265
|
+
lines[0] += ` ${payloadType}`
|
|
266
|
+
lines.splice(lines.length - 1, 0, `a=rtpmap:${payloadType} L16/48000/2`)
|
|
267
|
+
lines.splice(lines.length - 1, 0, `a=rtcp-fb:${payloadType} transport-cc`)
|
|
268
|
+
|
|
269
|
+
return lines.join("\r\n")
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
private static enableStereoOpus(section: string) {
|
|
273
|
+
let opusPayloadFormat = ""
|
|
274
|
+
const lines = section.split("\r\n")
|
|
275
|
+
|
|
276
|
+
for (let i = 0; i < lines.length; i++) {
|
|
277
|
+
if (lines[i].startsWith("a=rtpmap:") && lines[i].toLowerCase().includes("opus/")) {
|
|
278
|
+
opusPayloadFormat = lines[i].slice("a=rtpmap:".length).split(" ")[0]
|
|
279
|
+
break
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
if (opusPayloadFormat === "") return section
|
|
284
|
+
|
|
285
|
+
for (let i = 0; i < lines.length; i++) {
|
|
286
|
+
if (lines[i].startsWith(`a=fmtp:${opusPayloadFormat} `)) {
|
|
287
|
+
if (!lines[i].includes("stereo")) lines[i] += ";stereo=1"
|
|
288
|
+
if (!lines[i].includes("sprop-stereo")) lines[i] += ";sprop-stereo=1"
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
return lines.join("\r\n")
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
private static editOffer(sdp: string, nonAdvertisedCodecs: string[]) {
|
|
296
|
+
const sections = sdp.split("m=")
|
|
297
|
+
const payloadTypes = sections
|
|
298
|
+
.slice(1)
|
|
299
|
+
.map(section => section.split("\r\n")[0].split(" ").slice(3))
|
|
300
|
+
.reduce<string[]>((prev, cur) => [...prev, ...cur], [])
|
|
301
|
+
|
|
302
|
+
for (let i = 1; i < sections.length; i++) {
|
|
303
|
+
if (sections[i].startsWith("audio")) {
|
|
304
|
+
sections[i] = MediaMTXWebRTCReader.enableStereoOpus(sections[i])
|
|
305
|
+
|
|
306
|
+
if (nonAdvertisedCodecs.includes("pcma/8000/2")) sections[i] = MediaMTXWebRTCReader.enableStereoPcmau(payloadTypes, sections[i])
|
|
307
|
+
if (nonAdvertisedCodecs.includes("multiopus/48000/6")) sections[i] = MediaMTXWebRTCReader.enableMultichannelOpus(payloadTypes, sections[i])
|
|
308
|
+
if (nonAdvertisedCodecs.includes("L16/48000/2")) sections[i] = MediaMTXWebRTCReader.enableL16(payloadTypes, sections[i])
|
|
309
|
+
|
|
310
|
+
break
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
return sections.join("m=")
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
private static generateSdpFragment(offerData: OfferData, candidates: RTCIceCandidate[]) {
|
|
318
|
+
const candidatesByMedia: Record<number, RTCIceCandidate[]> = {}
|
|
319
|
+
|
|
320
|
+
for (const candidate of candidates) {
|
|
321
|
+
const mid = candidate.sdpMLineIndex
|
|
322
|
+
if (mid === null) continue
|
|
323
|
+
|
|
324
|
+
candidatesByMedia[mid] ??= []
|
|
325
|
+
candidatesByMedia[mid].push(candidate)
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
let frag = `a=ice-ufrag:${offerData.iceUfrag}\r\n` + `a=ice-pwd:${offerData.icePwd}\r\n`
|
|
329
|
+
|
|
330
|
+
for (let mid = 0; mid < offerData.medias.length; mid++) {
|
|
331
|
+
const mediaCandidates = candidatesByMedia[mid]
|
|
332
|
+
if (mediaCandidates === undefined) continue
|
|
333
|
+
|
|
334
|
+
frag += `m=${offerData.medias[mid]}\r\n` + `a=mid:${mid}\r\n`
|
|
335
|
+
|
|
336
|
+
for (const candidate of mediaCandidates) frag += `a=${candidate.candidate}\r\n`
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
return frag
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
private handleError(err: string) {
|
|
343
|
+
if (this.state === "running") {
|
|
344
|
+
if (this.pc) {
|
|
345
|
+
this.pc.close()
|
|
346
|
+
this.pc = null
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
this.offerData = null
|
|
350
|
+
|
|
351
|
+
if (this.sessionUrl !== null) {
|
|
352
|
+
fetch(this.sessionUrl, { method: "DELETE" })
|
|
353
|
+
this.sessionUrl = null
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
this.queuedCandidates = []
|
|
357
|
+
this.state = "restarting"
|
|
358
|
+
|
|
359
|
+
this.restartTimeout = window.setTimeout(() => {
|
|
360
|
+
this.restartTimeout = null
|
|
361
|
+
this.state = "running"
|
|
362
|
+
this.start()
|
|
363
|
+
}, this.retryPause)
|
|
364
|
+
|
|
365
|
+
this.conf.onError?.(`${err}, retrying in some seconds`)
|
|
366
|
+
} else if (this.state === "getting_codecs") {
|
|
367
|
+
this.state = "failed"
|
|
368
|
+
this.conf.onError?.(err)
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
private getNonAdvertisedCodecs() {
|
|
373
|
+
Promise.all(
|
|
374
|
+
[
|
|
375
|
+
["pcma/8000/2"],
|
|
376
|
+
["multiopus/48000/6", "channel_mapping=0,4,1,2,3,5;num_streams=4;coupled_streams=2"],
|
|
377
|
+
["L16/48000/2"],
|
|
378
|
+
].map(codec =>
|
|
379
|
+
MediaMTXWebRTCReader.supportsNonAdvertisedCodec(codec[0], codec[1]).then(isSupported => {
|
|
380
|
+
if (!isSupported) return false
|
|
381
|
+
return codec[0]
|
|
382
|
+
}),
|
|
383
|
+
),
|
|
384
|
+
)
|
|
385
|
+
.then(codecs => codecs.filter((codec): codec is string => codec !== false))
|
|
386
|
+
.then(codecs => {
|
|
387
|
+
if (this.state !== "getting_codecs") throw new Error("closed")
|
|
388
|
+
|
|
389
|
+
this.nonAdvertisedCodecs = codecs
|
|
390
|
+
this.state = "running"
|
|
391
|
+
this.start()
|
|
392
|
+
})
|
|
393
|
+
.catch(err => this.handleError(String(err)))
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
private start() {
|
|
397
|
+
this.requestICEServers()
|
|
398
|
+
.then(iceServers => this.setupPeerConnection(iceServers))
|
|
399
|
+
.then(offer => this.sendOffer(offer))
|
|
400
|
+
.then(answer => this.setAnswer(answer))
|
|
401
|
+
.catch(err => this.handleError(String(err)))
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
private authHeader(): Record<string, string> {
|
|
405
|
+
if (this.conf.user !== undefined && this.conf.user !== "") {
|
|
406
|
+
const credentials = btoa(`${this.conf.user}:${this.conf.pass ?? ""}`)
|
|
407
|
+
return { Authorization: `Basic ${credentials}` }
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
if (this.conf.token !== undefined && this.conf.token !== "") return { Authorization: `Bearer ${this.conf.token}` }
|
|
411
|
+
|
|
412
|
+
return {}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
private requestICEServers() {
|
|
416
|
+
return fetch(this.conf.url, {
|
|
417
|
+
method: "OPTIONS",
|
|
418
|
+
headers: {
|
|
419
|
+
...this.authHeader(),
|
|
420
|
+
},
|
|
421
|
+
}).then(res => MediaMTXWebRTCReader.linkToIceServers(res.headers.get("Link")))
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
private setupPeerConnection(iceServers: RTCIceServer[]) {
|
|
425
|
+
if (this.state !== "running") throw new Error("closed")
|
|
426
|
+
|
|
427
|
+
const pc = new RTCPeerConnection({
|
|
428
|
+
iceServers,
|
|
429
|
+
sdpSemantics: "unified-plan",
|
|
430
|
+
} as RTCConfiguration)
|
|
431
|
+
|
|
432
|
+
this.pc = pc
|
|
433
|
+
|
|
434
|
+
const direction = "recvonly"
|
|
435
|
+
pc.addTransceiver("video", { direction })
|
|
436
|
+
pc.addTransceiver("audio", { direction })
|
|
437
|
+
pc.createDataChannel("")
|
|
438
|
+
|
|
439
|
+
pc.onicecandidate = evt => this.onLocalCandidate(evt)
|
|
440
|
+
pc.onconnectionstatechange = () => this.onConnectionState()
|
|
441
|
+
pc.ontrack = evt => this.onTrack(evt)
|
|
442
|
+
pc.ondatachannel = evt => this.onDataChannel(evt)
|
|
443
|
+
|
|
444
|
+
return pc.createOffer().then(offer => {
|
|
445
|
+
if (offer.sdp === undefined) throw new Error("SDP not present")
|
|
446
|
+
|
|
447
|
+
offer.sdp = MediaMTXWebRTCReader.editOffer(offer.sdp, this.nonAdvertisedCodecs)
|
|
448
|
+
this.offerData = MediaMTXWebRTCReader.parseOffer(offer.sdp)
|
|
449
|
+
|
|
450
|
+
return pc.setLocalDescription(offer).then(() => offer.sdp!)
|
|
451
|
+
})
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
private sendOffer(offer: string) {
|
|
455
|
+
if (this.state !== "running") throw new Error("closed")
|
|
456
|
+
|
|
457
|
+
return fetch(this.conf.url, {
|
|
458
|
+
method: "POST",
|
|
459
|
+
headers: {
|
|
460
|
+
...this.authHeader(),
|
|
461
|
+
"Content-Type": "application/sdp",
|
|
462
|
+
},
|
|
463
|
+
body: offer,
|
|
464
|
+
}).then(res => {
|
|
465
|
+
switch (res.status) {
|
|
466
|
+
case 201:
|
|
467
|
+
break
|
|
468
|
+
case 404:
|
|
469
|
+
throw new Error("stream not found")
|
|
470
|
+
case 400:
|
|
471
|
+
return res.json().then((body: { error?: string }) => {
|
|
472
|
+
throw new Error(body.error ?? "bad request")
|
|
473
|
+
})
|
|
474
|
+
default:
|
|
475
|
+
throw new Error(`bad status code ${res.status}`)
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
const location = res.headers.get("location")
|
|
479
|
+
if (location === null) throw new Error("session location not present")
|
|
480
|
+
|
|
481
|
+
this.sessionUrl = new URL(location, this.conf.url).toString()
|
|
482
|
+
|
|
483
|
+
return res.text()
|
|
484
|
+
})
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
private setAnswer(answer: string) {
|
|
488
|
+
if (this.state !== "running") throw new Error("closed")
|
|
489
|
+
if (!this.pc) throw new Error("peer connection not present")
|
|
490
|
+
|
|
491
|
+
return this.pc
|
|
492
|
+
.setRemoteDescription(
|
|
493
|
+
new RTCSessionDescription({
|
|
494
|
+
type: "answer",
|
|
495
|
+
sdp: answer,
|
|
496
|
+
}),
|
|
497
|
+
)
|
|
498
|
+
.then(() => {
|
|
499
|
+
if (this.state !== "running") return
|
|
500
|
+
|
|
501
|
+
if (this.queuedCandidates.length !== 0) {
|
|
502
|
+
this.sendLocalCandidates(this.queuedCandidates)
|
|
503
|
+
this.queuedCandidates = []
|
|
504
|
+
}
|
|
505
|
+
})
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
private onLocalCandidate(evt: RTCPeerConnectionIceEvent) {
|
|
509
|
+
if (this.state !== "running") return
|
|
510
|
+
|
|
511
|
+
if (evt.candidate !== null) {
|
|
512
|
+
if (this.sessionUrl === null) this.queuedCandidates.push(evt.candidate)
|
|
513
|
+
else this.sendLocalCandidates([evt.candidate])
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
private sendLocalCandidates(candidates: RTCIceCandidate[]) {
|
|
518
|
+
if (this.sessionUrl === null || this.offerData === null) return
|
|
519
|
+
|
|
520
|
+
fetch(this.sessionUrl, {
|
|
521
|
+
method: "PATCH",
|
|
522
|
+
headers: {
|
|
523
|
+
"Content-Type": "application/trickle-ice-sdpfrag",
|
|
524
|
+
"If-Match": "*",
|
|
525
|
+
},
|
|
526
|
+
body: MediaMTXWebRTCReader.generateSdpFragment(this.offerData, candidates),
|
|
527
|
+
})
|
|
528
|
+
.then(res => {
|
|
529
|
+
switch (res.status) {
|
|
530
|
+
case 204:
|
|
531
|
+
break
|
|
532
|
+
case 404:
|
|
533
|
+
throw new Error("stream not found")
|
|
534
|
+
default:
|
|
535
|
+
throw new Error(`bad status code ${res.status}`)
|
|
536
|
+
}
|
|
537
|
+
})
|
|
538
|
+
.catch(err => this.handleError(String(err)))
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
private onConnectionState() {
|
|
542
|
+
if (this.state !== "running" || !this.pc) return
|
|
543
|
+
|
|
544
|
+
if (this.pc.connectionState === "failed" || this.pc.connectionState === "closed") this.handleError("peer connection closed")
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
private onTrack(evt: RTCTrackEvent) {
|
|
548
|
+
this.conf.onTrack?.(evt)
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
private onDataChannel(evt: RTCDataChannelEvent) {
|
|
552
|
+
this.conf.onDataChannel?.(evt)
|
|
553
|
+
}
|
|
554
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -16,6 +16,7 @@ export * from "@/components/LoopSwiper"
|
|
|
16
16
|
export * from "@/components/ReadExcel"
|
|
17
17
|
export * from "@/components/ReadSheet"
|
|
18
18
|
export * from "@/components/Ring"
|
|
19
|
+
export * from "@/components/RtspPlayer"
|
|
19
20
|
export * from "@/components/Scroll"
|
|
20
21
|
export * from "@/components/ScrollMask"
|
|
21
22
|
export * from "@/components/SectionRing"
|
package/tsconfig.json
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
{
|
|
2
2
|
"compilerOptions": {
|
|
3
|
-
"baseUrl": ".",
|
|
4
3
|
"strict": true,
|
|
5
4
|
"declaration": true,
|
|
6
5
|
"skipLibCheck": true,
|
|
@@ -9,8 +8,13 @@
|
|
|
9
8
|
"module": "ESNext",
|
|
10
9
|
"moduleResolution": "Bundler",
|
|
11
10
|
"paths": {
|
|
12
|
-
"@/*": [
|
|
11
|
+
"@/*": [
|
|
12
|
+
"./src/*"
|
|
13
|
+
]
|
|
13
14
|
}
|
|
14
15
|
},
|
|
15
|
-
"include": [
|
|
16
|
-
|
|
16
|
+
"include": [
|
|
17
|
+
"src",
|
|
18
|
+
"types.d.ts"
|
|
19
|
+
]
|
|
20
|
+
}
|