whepts 1.0.3 → 1.1.0

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/src/utils/sdp.ts DELETED
@@ -1,253 +0,0 @@
1
- /** Type for parsed offer data */
2
- export interface ParsedOffer {
3
- iceUfrag: string
4
- icePwd: string
5
- medias: string[]
6
- }
7
-
8
- /**
9
- * SDP processing utilities
10
- */
11
- export class SdpUtils {
12
- /**
13
- * Parse an offer SDP into iceUfrag, icePwd, and medias.
14
- */
15
- static parseOffer(sdp: string): ParsedOffer {
16
- const ret: ParsedOffer = {
17
- iceUfrag: '',
18
- icePwd: '',
19
- medias: [],
20
- }
21
-
22
- for (const line of sdp.split('\r\n')) {
23
- if (line.startsWith('m=')) {
24
- ret.medias.push(line.slice('m='.length))
25
- }
26
- else if (ret.iceUfrag === '' && line.startsWith('a=ice-ufrag:')) {
27
- ret.iceUfrag = line.slice('a=ice-ufrag:'.length)
28
- }
29
- else if (ret.icePwd === '' && line.startsWith('a=ice-pwd:')) {
30
- ret.icePwd = line.slice('a=ice-pwd:'.length)
31
- }
32
- }
33
-
34
- return ret
35
- }
36
-
37
- /**
38
- * Reserve a payload type.
39
- */
40
- static reservePayloadType(payloadTypes: string[]): string {
41
- // everything is valid between 30 and 127, except for interval between 64 and 95
42
- // https://chromium.googlesource.com/external/webrtc/+/refs/heads/master/call/payload_type.h#29
43
- for (let i = 30; i <= 127; i++) {
44
- if ((i <= 63 || i >= 96) && !payloadTypes.includes(i.toString())) {
45
- const pl = i.toString()
46
- payloadTypes.push(pl)
47
- return pl
48
- }
49
- }
50
- throw new Error('unable to find a free payload type')
51
- }
52
-
53
- /**
54
- * Enable stereo PCMA/PCMU codecs.
55
- */
56
- static enableStereoPcmau(payloadTypes: string[], section: string): string {
57
- const lines = section.split('\r\n')
58
-
59
- let payloadType = SdpUtils.reservePayloadType(payloadTypes)
60
- lines[0] += ` ${payloadType}`
61
- lines.splice(lines.length - 1, 0, `a=rtpmap:${payloadType} PCMU/8000/2`)
62
- lines.splice(lines.length - 1, 0, `a=rtcp-fb:${payloadType} transport-cc`)
63
-
64
- payloadType = SdpUtils.reservePayloadType(payloadTypes)
65
- lines[0] += ` ${payloadType}`
66
- lines.splice(lines.length - 1, 0, `a=rtpmap:${payloadType} PCMA/8000/2`)
67
- lines.splice(lines.length - 1, 0, `a=rtcp-fb:${payloadType} transport-cc`)
68
-
69
- return lines.join('\r\n')
70
- }
71
-
72
- /**
73
- * Enable multichannel Opus codec.
74
- */
75
- static enableMultichannelOpus(payloadTypes: string[], section: string): string {
76
- const lines = section.split('\r\n')
77
-
78
- let payloadType = SdpUtils.reservePayloadType(payloadTypes)
79
- lines[0] += ` ${payloadType}`
80
- lines.splice(lines.length - 1, 0, `a=rtpmap:${payloadType} multiopus/48000/3`)
81
- lines.splice(lines.length - 1, 0, `a=fmtp:${payloadType} channel_mapping=0,2,1;num_streams=2;coupled_streams=1`)
82
- lines.splice(lines.length - 1, 0, `a=rtcp-fb:${payloadType} transport-cc`)
83
-
84
- payloadType = SdpUtils.reservePayloadType(payloadTypes)
85
- lines[0] += ` ${payloadType}`
86
- lines.splice(lines.length - 1, 0, `a=rtpmap:${payloadType} multiopus/48000/4`)
87
- lines.splice(lines.length - 1, 0, `a=fmtp:${payloadType} channel_mapping=0,1,2,3;num_streams=2;coupled_streams=2`)
88
- lines.splice(lines.length - 1, 0, `a=rtcp-fb:${payloadType} transport-cc`)
89
-
90
- payloadType = SdpUtils.reservePayloadType(payloadTypes)
91
- lines[0] += ` ${payloadType}`
92
- lines.splice(lines.length - 1, 0, `a=rtpmap:${payloadType} multiopus/48000/5`)
93
- lines.splice(
94
- lines.length - 1,
95
- 0,
96
- `a=fmtp:${payloadType} channel_mapping=0,4,1,2,3;num_streams=3;coupled_streams=2`,
97
- )
98
- lines.splice(lines.length - 1, 0, `a=rtcp-fb:${payloadType} transport-cc`)
99
-
100
- payloadType = SdpUtils.reservePayloadType(payloadTypes)
101
- lines[0] += ` ${payloadType}`
102
- lines.splice(lines.length - 1, 0, `a=rtpmap:${payloadType} multiopus/48000/6`)
103
- lines.splice(
104
- lines.length - 1,
105
- 0,
106
- `a=fmtp:${payloadType} channel_mapping=0,4,1,2,3,5;num_streams=4;coupled_streams=2`,
107
- )
108
- lines.splice(lines.length - 1, 0, `a=rtcp-fb:${payloadType} transport-cc`)
109
-
110
- payloadType = SdpUtils.reservePayloadType(payloadTypes)
111
- lines[0] += ` ${payloadType}`
112
- lines.splice(lines.length - 1, 0, `a=rtpmap:${payloadType} multiopus/48000/7`)
113
- lines.splice(
114
- lines.length - 1,
115
- 0,
116
- `a=fmtp:${payloadType} channel_mapping=0,4,1,2,3,5,6;num_streams=4;coupled_streams=4`,
117
- )
118
- lines.splice(lines.length - 1, 0, `a=rtcp-fb:${payloadType} transport-cc`)
119
-
120
- payloadType = SdpUtils.reservePayloadType(payloadTypes)
121
- lines[0] += ` ${payloadType}`
122
- lines.splice(lines.length - 1, 0, `a=rtpmap:${payloadType} multiopus/48000/8`)
123
- lines.splice(
124
- lines.length - 1,
125
- 0,
126
- `a=fmtp:${payloadType} channel_mapping=0,6,1,4,5,2,3,7;num_streams=5;coupled_streams=4`,
127
- )
128
- lines.splice(lines.length - 1, 0, `a=rtcp-fb:${payloadType} transport-cc`)
129
-
130
- return lines.join('\r\n')
131
- }
132
-
133
- /**
134
- * Enable L16 codec.
135
- */
136
- static enableL16(payloadTypes: string[], section: string): string {
137
- const lines = section.split('\r\n')
138
-
139
- let payloadType = SdpUtils.reservePayloadType(payloadTypes)
140
- lines[0] += ` ${payloadType}`
141
- lines.splice(lines.length - 1, 0, `a=rtpmap:${payloadType} L16/8000/2`)
142
- lines.splice(lines.length - 1, 0, `a=rtcp-fb:${payloadType} transport-cc`)
143
-
144
- payloadType = SdpUtils.reservePayloadType(payloadTypes)
145
- lines[0] += ` ${payloadType}`
146
- lines.splice(lines.length - 1, 0, `a=rtpmap:${payloadType} L16/16000/2`)
147
- lines.splice(lines.length - 1, 0, `a=rtcp-fb:${payloadType} transport-cc`)
148
-
149
- payloadType = SdpUtils.reservePayloadType(payloadTypes)
150
- lines[0] += ` ${payloadType}`
151
- lines.splice(lines.length - 1, 0, `a=rtpmap:${payloadType} L16/48000/2`)
152
- lines.splice(lines.length - 1, 0, `a=rtcp-fb:${payloadType} transport-cc`)
153
-
154
- return lines.join('\r\n')
155
- }
156
-
157
- /**
158
- * Enable stereo Opus codec.
159
- */
160
- static enableStereoOpus(section: string): string {
161
- let opusPayloadFormat = ''
162
- const lines = section.split('\r\n')
163
-
164
- for (let i = 0; i < lines.length; i++) {
165
- if (lines[i].startsWith('a=rtpmap:') && lines[i].toLowerCase().includes('opus/')) {
166
- opusPayloadFormat = lines[i].slice('a=rtpmap:'.length).split(' ')[0]
167
- break
168
- }
169
- }
170
-
171
- if (opusPayloadFormat === '') {
172
- return section
173
- }
174
-
175
- for (let i = 0; i < lines.length; i++) {
176
- if (lines[i].startsWith(`a=fmtp:${opusPayloadFormat} `)) {
177
- if (!lines[i].includes('stereo')) {
178
- lines[i] += ';stereo=1'
179
- }
180
- if (!lines[i].includes('sprop-stereo')) {
181
- lines[i] += ';sprop-stereo=1'
182
- }
183
- }
184
- }
185
-
186
- return lines.join('\r\n')
187
- }
188
-
189
- /**
190
- * Edit an offer SDP to enable non-advertised codecs.
191
- */
192
- static editOffer(sdp: string, nonAdvertisedCodecs: string[]): string {
193
- const sections = sdp.split('m=')
194
-
195
- const payloadTypes = sections
196
- .slice(1)
197
- .map(s => s.split('\r\n')[0].split(' ').slice(3))
198
- .reduce((prev, cur) => [...prev, ...cur], [])
199
-
200
- for (let i = 1; i < sections.length; i++) {
201
- if (sections[i].startsWith('audio')) {
202
- sections[i] = SdpUtils.enableStereoOpus(sections[i])
203
-
204
- if (nonAdvertisedCodecs.includes('pcma/8000/2')) {
205
- sections[i] = SdpUtils.enableStereoPcmau(payloadTypes, sections[i])
206
- }
207
- if (nonAdvertisedCodecs.includes('multiopus/48000/6')) {
208
- sections[i] = SdpUtils.enableMultichannelOpus(payloadTypes, sections[i])
209
- }
210
- if (nonAdvertisedCodecs.includes('L16/48000/2')) {
211
- sections[i] = SdpUtils.enableL16(payloadTypes, sections[i])
212
- }
213
-
214
- break
215
- }
216
- }
217
-
218
- return sections.join('m=')
219
- }
220
-
221
- /**
222
- * Generate an SDP fragment.
223
- */
224
- static generateSdpFragment(od: ParsedOffer, candidates: RTCIceCandidate[]): string {
225
- const candidatesByMedia: Record<number, RTCIceCandidate[]> = {}
226
- for (const candidate of candidates) {
227
- const mid = candidate.sdpMLineIndex
228
- if (mid) {
229
- if (candidatesByMedia[mid] === undefined) {
230
- candidatesByMedia[mid] = []
231
- }
232
- candidatesByMedia[mid].push(candidate)
233
- }
234
- }
235
-
236
- let frag = `a=ice-ufrag:${od.iceUfrag}\r\n` + `a=ice-pwd:${od.icePwd}\r\n`
237
-
238
- let mid = 0
239
-
240
- for (const media of od.medias) {
241
- if (candidatesByMedia[mid] !== undefined) {
242
- frag += `m=${media}\r\n` + `a=mid:${mid}\r\n`
243
-
244
- for (const candidate of candidatesByMedia[mid]) {
245
- frag += `a=${candidate.candidate}\r\n`
246
- }
247
- }
248
- mid++
249
- }
250
-
251
- return frag
252
- }
253
- }
@@ -1,123 +0,0 @@
1
- import { ErrorTypes, WebRTCError } from '~/errors'
2
-
3
- /**
4
- * WebRTC utilities
5
- */
6
- export class WebRtcUtils {
7
- /**
8
- * Check if the browser supports a non-advertised codec.
9
- */
10
- static async supportsNonAdvertisedCodec(codec: string, fmtp?: string): Promise<boolean> {
11
- return new Promise((resolve) => {
12
- const pc = new RTCPeerConnection({ iceServers: [] })
13
- const mediaType = 'audio'
14
- let payloadType = ''
15
-
16
- pc.addTransceiver(mediaType, { direction: 'recvonly' })
17
- pc.createOffer()
18
- .then((offer) => {
19
- if (!offer.sdp) {
20
- throw new Error('SDP not present')
21
- }
22
- if (offer.sdp.includes(` ${codec}`)) {
23
- // codec is advertised, there's no need to add it manually
24
- throw new Error('already present')
25
- }
26
-
27
- const sections = offer.sdp.split(`m=${mediaType}`)
28
-
29
- const payloadTypes = sections
30
- .slice(1)
31
- .map(s => s.split('\r\n')[0].split(' ').slice(3))
32
- .reduce((prev, cur) => [...prev, ...cur], [])
33
- payloadType = WebRtcUtils.reservePayloadType(payloadTypes)
34
-
35
- const lines = sections[1].split('\r\n')
36
- lines[0] += ` ${payloadType}`
37
- lines.splice(lines.length - 1, 0, `a=rtpmap:${payloadType} ${codec}`)
38
- if (fmtp !== undefined) {
39
- lines.splice(lines.length - 1, 0, `a=fmtp:${payloadType} ${fmtp}`)
40
- }
41
- sections[1] = lines.join('\r\n')
42
- offer.sdp = sections.join(`m=${mediaType}`)
43
- return pc.setLocalDescription(offer)
44
- })
45
- .then(() =>
46
- pc.setRemoteDescription(
47
- new RTCSessionDescription({
48
- type: 'answer',
49
- sdp:
50
- `v=0\r\n`
51
- + `o=- 6539324223450680508 0 IN IP4 0.0.0.0\r\n`
52
- + `s=-\r\n`
53
- + `t=0 0\r\n`
54
- + `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`
55
- + `m=${mediaType} 9 UDP/TLS/RTP/SAVPF ${payloadType}\r\n`
56
- + `c=IN IP4 0.0.0.0\r\n`
57
- + `a=ice-pwd:7c3bf4770007e7432ee4ea4d697db675\r\n`
58
- + `a=ice-ufrag:29e036dc\r\n`
59
- + `a=sendonly\r\n`
60
- + `a=rtcp-mux\r\n`
61
- + `a=rtpmap:${payloadType} ${codec}\r\n${fmtp !== undefined ? `a=fmtp:${payloadType} ${fmtp}\r\n` : ''}`,
62
- }),
63
- ),
64
- )
65
- .then(() => resolve(true))
66
- .catch(() => resolve(false))
67
- .finally(() => pc.close())
68
- })
69
- }
70
-
71
- /**
72
- * Unquote a credential string.
73
- */
74
- static unquoteCredential(v: string): string {
75
- return JSON.parse(`"${v}"`)
76
- }
77
-
78
- /**
79
- * Convert Link header to iceServers array.
80
- */
81
- static linkToIceServers(links: string | null): RTCIceServer[] {
82
- if (links) {
83
- return links.split(', ').map((link) => {
84
- const m = link.match(
85
- /^<(.+?)>; rel="ice-server"(; username="(.*?)"; credential="(.*?)"; credential-type="password")?/i,
86
- )
87
-
88
- if (!m) {
89
- throw new WebRTCError(ErrorTypes.SIGNAL_ERROR, 'Invalid ICE server link format')
90
- }
91
-
92
- const ret: RTCIceServer = {
93
- urls: [m[1]],
94
- }
95
-
96
- if (m[3]) {
97
- ret.username = WebRtcUtils.unquoteCredential(m[3])
98
- ret.credential = WebRtcUtils.unquoteCredential(m[4])
99
- ret.credentialType = 'password'
100
- }
101
-
102
- return ret
103
- })
104
- }
105
- return []
106
- }
107
-
108
- /**
109
- * Reserve a payload type.
110
- */
111
- static reservePayloadType(payloadTypes: string[]): string {
112
- // everything is valid between 30 and 127, except for interval between 64 and 95
113
- // https://chromium.googlesource.com/external/webrtc/+/refs/heads/master/call/payload_type.h#29
114
- for (let i = 30; i <= 127; i++) {
115
- if ((i <= 63 || i >= 96) && !payloadTypes.includes(i.toString())) {
116
- const pl = i.toString()
117
- payloadTypes.push(pl)
118
- return pl
119
- }
120
- }
121
- throw new Error('unable to find a free payload type')
122
- }
123
- }
package/src/whep.ts DELETED
@@ -1,175 +0,0 @@
1
- import type { Conf, State } from './types'
2
- import { CodecDetector } from './core/codec'
3
- import { ConnectionManager } from './core/connection'
4
- import { HttpClient } from './core/http'
5
- import { TrackManager } from './core/track'
6
- import { ErrorTypes, WebRTCError } from './errors'
7
- import { FlowCheck } from './utils/flow-check'
8
-
9
- /** WebRTC/WHEP reader. */
10
- export default class WebRTCWhep {
11
- private retryPause: number = 2000
12
- private conf: Conf
13
- private state: State
14
- private restartTimeout?: NodeJS.Timeout
15
- private sessionUrl?: string
16
- private queuedCandidates: RTCIceCandidate[] = []
17
- private nonAdvertisedCodecs: string[] = []
18
- private flowCheck: FlowCheck
19
- private httpClient: HttpClient
20
- private connectionManager: ConnectionManager
21
- private trackManager: TrackManager
22
- private codecDetector: CodecDetector
23
-
24
- constructor(conf: Conf) {
25
- this.conf = conf
26
- this.state = 'getting_codecs'
27
-
28
- this.flowCheck = new FlowCheck({
29
- interval: 5000,
30
- onError: (err: WebRTCError) => this.handleError(err),
31
- })
32
-
33
- this.httpClient = new HttpClient(
34
- this.conf,
35
- () => this.state,
36
- err => this.handleError(err),
37
- )
38
-
39
- this.connectionManager = new ConnectionManager(
40
- () => this.state,
41
- {
42
- onCandidate: candidate => this.handleCandidate(candidate),
43
- onTrack: (evt) => {
44
- this.trackManager.onTrack(evt)
45
- this.flowCheck.start()
46
- },
47
- onError: err => this.handleError(err),
48
- },
49
- this.nonAdvertisedCodecs,
50
- )
51
-
52
- this.trackManager = new TrackManager(this.conf.container)
53
-
54
- this.codecDetector = new CodecDetector(
55
- () => this.state,
56
- {
57
- onCodecsDetected: codecs => this.handleCodecsDetected(codecs),
58
- onError: err => this.handleError(err),
59
- },
60
- )
61
-
62
- this.codecDetector.detect()
63
- }
64
-
65
- get isRunning(): boolean {
66
- return this.state === 'running'
67
- }
68
-
69
- close(): void {
70
- this.state = 'closed'
71
- this.connectionManager.close()
72
- this.trackManager.stop()
73
- this.flowCheck.stop()
74
- if (this.restartTimeout) {
75
- clearTimeout(this.restartTimeout)
76
- }
77
- }
78
-
79
- private handleError(err: Error | WebRTCError): void {
80
- this.flowCheck.stop()
81
-
82
- if (this.state === 'getting_codecs') {
83
- this.state = 'failed'
84
- }
85
- else if (err instanceof WebRTCError && err.type === ErrorTypes.SIGNAL_ERROR) {
86
- this.state = 'failed'
87
- }
88
- else if (this.state === 'running') {
89
- this.connectionManager.close()
90
- this.queuedCandidates = []
91
-
92
- if (this.sessionUrl) {
93
- fetch(this.sessionUrl, {
94
- method: 'DELETE',
95
- })
96
- this.sessionUrl = undefined
97
- }
98
-
99
- this.state = 'restarting'
100
-
101
- this.restartTimeout = setTimeout(() => {
102
- this.restartTimeout = undefined
103
- this.state = 'running'
104
- this.start()
105
- }, this.retryPause)
106
-
107
- err.message = `${err.message}, retrying in some seconds`
108
- }
109
-
110
- if (this.conf.onError) {
111
- if (err instanceof WebRTCError) {
112
- this.conf.onError(err)
113
- }
114
- else {
115
- this.conf.onError(new WebRTCError(ErrorTypes.OTHER_ERROR, err.message))
116
- }
117
- }
118
- }
119
-
120
- private handleCodecsDetected(codecs: string[]): void {
121
- this.nonAdvertisedCodecs = codecs
122
- this.state = 'running'
123
- this.start()
124
- }
125
-
126
- private start(): void {
127
- this.httpClient.requestICEServers()
128
- .then(iceServers => this.connectionManager.setupPeerConnection(iceServers))
129
- .then(offer => this.httpClient.sendOffer(offer))
130
- .then(({ sessionUrl, answer }) => this.handleOfferResponse(sessionUrl, answer))
131
- .catch(err => this.handleError(err))
132
- }
133
-
134
- private handleOfferResponse(sessionUrl: string | undefined, answer: string): Promise<void> {
135
- if (sessionUrl)
136
- this.sessionUrl = sessionUrl
137
-
138
- return this.connectionManager.setAnswer(answer).then(() => {
139
- if (this.state !== 'running')
140
- return
141
-
142
- if (this.queuedCandidates.length !== 0) {
143
- const offerData = this.connectionManager.getOfferData()
144
- if (offerData && this.sessionUrl) {
145
- this.httpClient.sendLocalCandidates(this.sessionUrl, offerData, this.queuedCandidates)
146
- this.queuedCandidates = []
147
- }
148
- }
149
- })
150
- }
151
-
152
- private handleCandidate(candidate: RTCIceCandidate): void {
153
- if (this.sessionUrl) {
154
- const offerData = this.connectionManager.getOfferData()
155
- if (offerData) {
156
- this.httpClient.sendLocalCandidates(this.sessionUrl, offerData, [candidate])
157
- }
158
- }
159
- else {
160
- this.queuedCandidates.push(candidate)
161
- }
162
- }
163
-
164
- get paused(): boolean {
165
- return this.trackManager.paused
166
- }
167
-
168
- pause(): void {
169
- this.trackManager.pause()
170
- }
171
-
172
- resume(): void {
173
- this.trackManager.resume()
174
- }
175
- }