-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
227 lines (181 loc) · 6.38 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
<title>Web RTC</title>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"
integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<style>
video {
display: none;
}
#vid-out {
width: 100px;
height: 100px;
position: fixed;
top: 0px;
right: 0px;
}
#vid-in {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div class="container">
<h1></h1>
<div class="row">
<div class="col-sm-12" id="waitingRoom">
<div id="messages"></div>
</div>
</div>
</div>
<video id="vid-out" controls></video>
<video id="vid-in" controls></video>
</body>
<script src="simplepeer.min.js"></script>
<script src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.27.4.min.js"></script>
<script src="pubNubWrapper.js"></script>
<script>
// A. GUI interaction
const messagesTop = document.getElementById('messages')
function error(error) {
displayMessage(error, 'danger')
}
let displayMessage = function (message, level = 'success') {
if (message.length > 100) {
message = message.substring(0, 99) + '...'
}
let pmessage = document.createElement('div')
pmessage.className = "alert alert-" + level
pmessage.setAttribute("role", "alert")
pmessage.appendChild(document.createTextNode(message))
messagesTop.append(pmessage)
console.log(message)
}
</script>
<script>
// B. WebRTC video chat
// https://www.grafikart.fr/tutoriels/webrtc-864
// https://github.com/feross/simple-peer
function bindEvents(p, sendMyDataClosure) {
function streamToVideoElement(stream) {
let $video = document.getElementById('vid-in')
$video.srcObject = stream
$video.onloadedmetadata = function (e) {
// hide waitingRoom ... somehow when OK
document.getElementById('waitingRoom').style.display = 'none'
// show peer video
$video.style.display = 'block'
$video.play()
}
}
function linkConfig(data) {
const dataJson = JSON.stringify(data)
// JSON sent to the other party
sendMyDataClosure(dataJson)
}
p.on('error', error)
p.on('stream', streamToVideoElement) // A stream is grabbed
p.on('signal', linkConfig) // My own data is known
}
let p = null
function start(isInitiator, sendMyDataClosure) {
function couldStartVideo(mediaStream) {
p = new SimplePeer({
initiator: isInitiator,
stream: mediaStream,
trickle: false,
// config: { iceServers: [{ urls: 'stun:stun.l.google.com:19302' }, { urls: 'stun:global.stun.twilio.com:3478?transport=udp' }] }
})
bindEvents(p, sendMyDataClosure)
let $video = document.getElementById('vid-out')
$video.srcObject = mediaStream
$video.onloadedmetadata = function (e) {
// show self video
$video.style.display = 'block'
$video.play()
}
}
function failStartVideo(err) {
error(err.name + ": " + err.message)
}
let constraints = {audio: false, video: true}
navigator.mediaDevices.getUserMedia(constraints).then(couldStartVideo).catch(failStartVideo)
displayMessage("Started my own video")
}
</script>
<script>
// C. bring it all together : workflow
function includeRemoteData(peerData) {
let friendDataX = JSON.parse(peerData)
displayMessage('includeRemoteData, renegotiate=' + friendDataX.renegotiate)
if (friendDataX.renegotiate != true && hisData == undefined && myData != undefined) {
hisData = friendDataX
displayMessage('signaled!')
p.signal(friendDataX)
}
}
const teacherIn = 'TEACHER IN'
const studentIn = 'STUDENT IN'
// Configure Comms
let myComms;
function sendMyDataClosure(dataJson) {
displayMessage("Sending to peer my details")
myComms.send(dataJson)
}
function onMessageCallback(whoami) {
return function (a, b) {
if (a != whoami) {
displayMessage('[MESSAGE received] ' + a + ': ' + b)
if (b == studentIn) {
} else if (b == teacherIn) {
} else {
// must be json data from student
includeRemoteData(b)
}
} else {
console.log("[self-message] " + a + ': ' + b)
}
}
}
let myData;
let hisData;
function sayStarted(data) {
displayMessage('Started on my side, my data:' + data)
myData = data
}
const urlParams = new URLSearchParams(window.location.search)
if (urlParams.get('whoami') == 'teacher') {
document.getElementsByTagName('h1')[0].textContent = "Teacher"
myComms = PubNubWrapper('1', 'teacher', onMessageCallback('teacher'), error)
// startTeacherClass
start(true, sayStarted) // sendMyDataClosure
displayMessage('Waiting for student')
} else {
document.getElementsByTagName('h1')[0].textContent = "Student"
myComms = PubNubWrapper('1', 'student', onMessageCallback('student'), error)
// startStudentClass
start(true, sayStarted)
displayMessage('Waiting for teacher')
}
broadCast()
function broadCast() {
if (myData == undefined) {
displayMessage('[broadCast] my missing')
setTimeout(broadCast, 1000)
} else {
sendMyDataClosure(myData)
if (hisData == undefined) {
displayMessage('[broadCast] his missing')
setTimeout(broadCast, 1000)
} else {
displayMessage('[stop broadCast] my+his')
}
}
}
</script>
</html>