This repository has been archived by the owner on Oct 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
sfu.js
165 lines (124 loc) · 3.97 KB
/
sfu.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
153
154
155
156
157
158
159
160
161
162
163
var log = msg => {
console.log(msg)
}
function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
const wsuri = "wss://" + location.host + "/ws";
const socket = new WebSocket(wsuri);
const config = {
// iceServers: [{
// urls: 'stun:stun.l.google.com:19302'
// }]
}
let localStream
// 0. getmedia and setLocalDescription
navigator.mediaDevices.getUserMedia({ video: true, audio: true})
.then(stream => {
let el = document.createElement("Video")
el.srcObject = stream
el.autoplay = true
el.controls = false
el.muted = true
document.getElementById('local').appendChild(el)
localStream = stream
}).catch(log)
const pcPublish = new RTCPeerConnection(config)
pcPublish.oniceconnectionstatechange = e => log(`[rtc]ICE connection state: ${pcPublish.iceConnectionState}`)
pcPublish.ontrack = function ({ track, streams }) {
if (track.kind === "video") {
log("[rtc]ontrack video")
// track.onunmute = () => {
// log("[rtc]onunmute")
let el = document.createElement(track.kind)
el.srcObject = streams[0]
el.autoplay = true
document.getElementById('remote').appendChild(el)
// }
}
}
pcPublish.onicecandidate = event => {
log("[rtc]onicecandidate, sending trickle on ws")
log(event.candidate)
if (event.candidate !== null) {
socket.send(JSON.stringify({
method: "trickle",
params: {
candidate: event.candidate,
}
}))
}
}
const id = uuidv4()
socket.addEventListener('message', async (event) => {
const resp = JSON.parse(event.data)
// Listen for server renegotiation notifications
if (!resp.id && resp.method === "offer") {
log(`[ws]receive offer`)
log(resp.params)
await pcPublish.setRemoteDescription(resp.params)
const answer = await pcPublish.createAnswer()
await pcPublish.setLocalDescription(answer)
log(`[ws]Sending answer`)
log(answer)
socket.send(JSON.stringify({
method: "answer",
params: { desc: answer },
id
}))
}
})
const join = async () => {
const offer = await pcPublish.createOffer()
await pcPublish.setLocalDescription(offer)
log("[ws]Sending join")
log(pcPublish.localDescription)
socket.send(JSON.stringify({
method: "join",
params: { sid: "test room", offer: pcPublish.localDescription },
id
}))
socket.addEventListener('message', (event) => {
const resp = JSON.parse(event.data)
if (resp.id === id) {
log(`[ws]receive answer`)
log(resp.result)
// Hook this here so it's not called before joining
pcPublish.onnegotiationneeded = async function () {
log("[rtc]onnegotiationneeded")
const offer = await pcPublish.createOffer()
await pcPublish.setLocalDescription(offer)
socket.send(JSON.stringify({
method: "offer",
params: { desc: offer },
id
}))
socket.addEventListener('message', (event) => {
const resp = JSON.parse(event.data)
if (resp.id === id) {
log(`[ws]Got renegotiation answer`)
pcPublish.setRemoteDescription(resp.result)
}
})
}
log(`[rtc]setRemoteDescription`)
// log(resp)
pcPublish.setRemoteDescription(resp.result)
} else if (resp.method == "trickle"){
log("[ws]receive trickle")
log(resp.params)
pcPublish.addIceCandidate(resp.params)
}
})
}
// click pub button
window.Pub = () => {
log("Publishing stream")
localStream.getTracks().forEach((track) => {
pcPublish.addTrack(track, localStream);
});
join()
}