-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.html
107 lines (97 loc) · 3.13 KB
/
client.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebRTC Example</title>
<style>
textarea {
width: 500px;
min-height: 75px;
}
</style>
</head>
<body>
Browser base64 Session Description<br />
<textarea id="localSessionDescription" readonly="true"></textarea> <br />
Remote Session Description<br />
<textarea id="remoteSessionDescription"></textarea> <br/>
<button onclick="window.startSession()"> Start Session </button><br />
<br />
Video<br />
<div id="remoteVideos"></div> <br />
Logs<br />
<div id="logs"></div>
<script>
function queryConnect(descr) {
fetch('/api/connect', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(descr)
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then(data => {
console.log("Received data:", data)
let descr = btoa(JSON.stringify(data))
document.getElementById('remoteSessionDescription').value = descr
try {
pc.setRemoteDescription(new RTCSessionDescription(JSON.parse(atob(descr))))
} catch (e) {
alert(e)
}
})
.catch(error => console.error("Fetch error:", error));
}
let pc = new RTCPeerConnection({
iceServers: [
{
urls: 'stun:global.stun.twilio.com:3478'
}
]
})
var log = msg => {
document.getElementById('logs').innerHTML += msg + '<br>'
}
pc.oniceconnectionstatechange = e => log(pc.iceConnectionState)
pc.onicecandidate = event => {
// Use the first ice candidate as otherwise there is a large timeout.
// Maybe we should gather a few candidates instead.
console.log("got ice candidate", event.candidate)
queryConnect(pc.localDescription)
let descr = btoa(JSON.stringify(pc.localDescription))
document.getElementById('localSessionDescription').value = descr
pc.onicecandidate = null
}
pc.ontrack = function (event) {
var el = document.createElement(event.track.kind)
el.srcObject = event.streams[0]
el.autoplay = true
el.controls = true
document.getElementById('remoteVideos').appendChild(el)
}
navigator.mediaDevices.getUserMedia({
video: false,
audio: {
echoCancellation: false,
noiseSuppression: false,
autoGainControl: false
}
})
.then(stream => {
stream.getTracks().forEach(track => pc.addTrack(track, stream));
console.log("got stream, waiting for offer");
}).catch(log)
window.startSession = () => {
console.log("creating offer")
pc.createOffer().then(d => pc.setLocalDescription(d)).catch(log)
}
</script>
</body>
</html>