-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwhip-whap-js.js
152 lines (128 loc) · 3.95 KB
/
whip-whap-js.js
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
/**
* WHIP WHAP module.
* @module whip-whap-js
*/
export { handleIceStateChange }
export { handleNegotiationNeeded }
// do not export for now
// export { sendSignalling }
// export { waitToCompleteIceGathering }
/**
* @param {Event} ev
* @param {string} url
*
* https://blog.mozilla.org/webrtc/perfect-negotiation-in-webrtc/
*/
/**
* Event handler for 'negotiationneeded' event.
*
* @function handleNegotiationNeeded
* @param {Event} event
* @param {string} url
* @param {Headers} headers might contain 'Authorization' or 'X-deadsfu-subuuid'
* @returns {string} Location header as string or undefined of not found or disabled by CORS
*
* @example WHIP example
* // pc.onnegotiationneeded = ev => whipwhap.handleNegotiationNeeded(ev, '/pub')
*
* @example WHAP example
* // pc.onnegotiationneeded = ev => whipwhap.handleNegotiationNeeded(ev, '/sub')
*/
async function handleNegotiationNeeded(ev, url, headers) {
let pc = /** @type {RTCPeerConnection} */ (ev.target)
console.debug(">onnegotiationneeded")
const offer = await pc.createOffer()
await pc.setLocalDescription(offer)
let ofr = await waitToCompleteIceGathering(pc, true)
if (typeof headers !== "object") { // is there a better way?
headers = new Headers()
}
if (pc.connectionState === "closed") {
return
}
console.debug("sending N line offer:", ofr.sdp.split(/\r\n|\r|\n/).length)
let opt = {}
opt.method = "POST"
headers.set("Content-Type", "application/sdp")
opt.headers = headers
opt.body = ofr.sdp
let resp = { status: -1 }
try { // without try/catch, a thrown except from fetch exits our 'thread'
resp = await fetch(url, opt)
} catch (error) {
// not needed console.log(error)
}
if (resp.status === 201) {
let anssdp = await resp.text()
console.debug("got N line answer:", anssdp.split(/\r\n|\r|\n/).length)
let sdi = new RTCSessionDescription({ type: "answer", sdp: anssdp })
await pc.setRemoteDescription(sdi)
console.debug('location header', resp.headers.get("Location"))
//happy path
return
}
//failed!
console.log('setting timeout')
setTimeout(() => {
console.log('timeout/rollback')
pc.setLocalDescription({ type: 'rollback' })
pc.restartIce()
}, 2000)
}
/**
* Event handler for 'iceconnectionstatechange' event.
*
* @function handleIceStateChange
* @param {Event} event
*
* @example
* // pc.addEventListener('iceconnectionstatechange', whipwhap.handleIceStateChange)
*/
function handleIceStateChange(event) {
let pc = /** @type {RTCPeerConnection} */ (event.target)
console.debug(">iceconnectionstatechange", pc.iceConnectionState)
// 12 10 21
// I am not really sure of the ideal iceConnectionStates to trigger
// an ice restart.
// Prior to 12/10/21 I had ''
// The MDN perfect negotiation example uses 'failed'
// as of 12/10/21, I am changing from disconnected to failed as per the MDN
// perfect negotation example:
// states definitions:
// https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/iceConnectionState#value
if (
pc.iceConnectionState === "failed" ||
pc.iceConnectionState === "disconnected" ||
pc.iceConnectionState === "closed"
) { //'failed' is also an option
console.debug("*** restarting ice")
pc.restartIce()
}
}
/**
* @ignore
* Wait until ICE is complete, or 250ms has elapsed,
* which ever comes first.
*
* @param {RTCPeerConnection} pc
* @param {boolean} logPerformance
* @return {Promise<RTCSessionDescription>}
*/
async function waitToCompleteIceGathering(pc, logPerformance) {
const t0 = performance.now()
let p = new Promise((resolve) => {
setTimeout(function () {
resolve(pc.localDescription)
}, 250)
pc.onicegatheringstatechange = (ev) =>
pc.iceGatheringState === "complete" && resolve(pc.localDescription)
})
if (logPerformance === true) {
await p
console.debug(
"ice gather blocked for N ms:",
Math.ceil(performance.now() - t0),
)
}
return p
}