forked from pion/webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
77 lines (64 loc) · 1.82 KB
/
index.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
<html>
<head>
<title>play-from-disk-renegotation</title>
</head>
<body>
<button onclick="window.addVideo()"> Add Video </button><br />
<button onclick="window.removeVideo()"> Remove Video </button><br />
<h3> Video </h3>
<div id="remoteVideos"></div> <br />
<h3> Logs </h3>
<div id="logs"></div>
</body>
<script>
let activeVideos = 0
let pc = new RTCPeerConnection({
iceServers: [
{
urls: 'stun:stun.l.google.com:19302'
}
]
})
pc.ontrack = function (event) {
var el = document.createElement(event.track.kind)
el.srcObject = event.streams[0]
el.autoplay = true
el.controls = true
event.track.onmute = function(event) {
el.parentNode.removeChild(el);
}
document.getElementById('remoteVideos').appendChild(el)
}
let doSignaling = method => {
pc.createOffer()
.then(offer => {
pc.setLocalDescription(offer)
return fetch(`/${method}`, {
method: 'post',
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json'
},
body: JSON.stringify(offer)
})
})
.then(res => res.json())
.then(res => pc.setRemoteDescription(res))
.catch(alert)
}
// Create a noop DataChannel. By default PeerConnections do not connect
// if they have no media tracks or DataChannels
pc.createDataChannel('noop')
doSignaling('createPeerConnection')
window.addVideo = () => {
if (pc.getTransceivers().length <= activeVideos) {
pc.addTransceiver('video')
activeVideos++
}
doSignaling('addVideo')
};
window.removeVideo = () => {
doSignaling('removeVideo')
};
</script>
</html>