-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
394 lines (313 loc) · 14 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<!--
> Muaz Khan - https://github.com/muaz-khan
> MIT License - https://www.webrtc-experiment.com/licence/
> Documentation - https://github.com/muaz-khan/WebRTC-Experiment/tree/master/websocket
-->
<!DOCTYPE html>
<html lang="en">
<head>
<title>WebSockets | WebRTC One-to-One</title>
<script>
if(!location.hash.replace('#', '').length) {
location.href = location.href.split('#')[0] + '#' + (Math.random() * 100).toString().replace('.', '');
location.reload();
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<link rel="author" type="text/html" href="https://plus.google.com/+MuazKhan">
<meta name="author" content="Muaz Khan">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<script src="https://unpkg.com/@webcomponents/webcomponentsjs@^2/webcomponents-bundle.js"></script>
<link rel="stylesheet" href="https://cdn.webrtc-experiment.com/style.css">
<link rel="stylesheet" href="./index.css">
<script>
document.createElement('article');
document.createElement('footer');
</script>
<!-- scripts used for peers connection -->
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
<script src="PeerConnection.js"> </script>
<script src="https://cdn.webrtc-experiment.com/view/websocket.js"> </script>
</head>
<body>
<section id="experiment" class="experiment">
<section>
<span>
Private??<a href="/websocket/" target="_blank" title="Setup Private Room!"><code><strong id="unique-token">#123456789</strong></code></a>
</span>
<input type="text" id="your-name" placeholder="insert name">
<button id="start-broadcasting" class="setup">Start Transmitting Yourself!</button>
</section>
<!-- list of all available conferencing rooms -->
<table id="rooms-list" style="width: 100%;"></table>
<select id="send_recorded_video">
</select>
<button id="send_value">Send</button>
<select id="send_recorded_audio1">
</select>
<button id="send_value_rec1">Send</button>
<select id="send_recorded_audio2">
</select>
<button id="send_value_rec2">Send</button>
<select id="send_recorded_audio3">
</select>
<button id="send_value_rec3">Send</button>
<!-- local/remote videos container -->
<div id="videos-container" style="display:none;"></div>
</section>
<div id="alien">
<div id="videos-container-remote"></div>
<div id="videos-recorded" style="display: none;">
</div>
</div>
<script>
// Documentation - https://github.com/muaz-khan/WebRTC-Experiment/tree/master/websocket
var send_video = document.getElementById("send_recorded_video");
var send_audio1 = document.getElementById("send_recorded_audio1");
var send_audio2 = document.getElementById("send_recorded_audio2");
var send_audio3 = document.getElementById("send_recorded_audio3");
var recorded_video_story = ["story1", "story2", "story3"];
var recorded_audio1 = ["1_1", "1_2", "1_3", "1_4", "1_5", "1_6", "1_7", "1_8", "1_9", "1_10", "1_11", "1_12", "1_13", "1_14", "1_15", "1_16", "1_17", "1_18"];
var recorded_audio2 = ["2_1", "2_2", "2_3", "2_4", "2_5", "2_6", "2_7", "2_8", "2_9", "2_10", "2_11", "2_12", "2_13", "2_14", "2_15", "2_16", "2_17", "2_18"];
var recorded_audio3 = ["3_1", "3_2", "3_3", "3_4", "3_5", "3_6", "3_7", "3_8", "3_9", "3_10", "3_11", "3_12", "3_13", "3_14", "3_15", "3_16", "3_17", "3_18"];
var num_story = recorded_video_story.length;
var num_rec1 = recorded_audio1.length;
var num_rec2 = recorded_audio2.length;
var num_rec3 = recorded_audio3.length;
for (i = 0; i < num_story; i++) {
var opt = document.createElement("option");
opt.appendChild(document.createTextNode(recorded_video_story[i]))
opt.value = recorded_video_story[i];
send_video.appendChild(opt);
}
for (i = 0; i < num_rec1; i++) {
var opt = document.createElement("option");
opt.appendChild(document.createTextNode(recorded_audio1[i]))
opt.value = recorded_audio1[i];
send_audio1.appendChild(opt);
}
for (i = 0; i < num_rec2; i++) {
var opt = document.createElement("option");
opt.appendChild(document.createTextNode(recorded_audio2[i]))
opt.value = recorded_audio2[i];
send_audio2.appendChild(opt);
}
for (i = 0; i < num_rec3; i++) {
var opt = document.createElement("option");
opt.appendChild(document.createTextNode(recorded_audio3[i]))
opt.value = recorded_audio3[i];
send_audio3.appendChild(opt);
}
var button_send = document.getElementById("send_value");
var button_send1 = document.getElementById("send_value_rec1");
var button_send2 = document.getElementById("send_value_rec2");
var button_send3 = document.getElementById("send_value_rec3");
button_send.onclick = function() {
var value_video = send_video.options[send_video.selectedIndex].value;
console.log(value_video);
sendMessage(value_video);
};
button_send1.onclick = function() {
var value_video = send_audio1.options[send_audio1.selectedIndex].value;
console.log(value_video);
sendMessage(value_video);
};
button_send2.onclick = function() {
var value_video = send_audio2.options[send_audio2.selectedIndex].value;
console.log(value_video);
sendMessage(value_video);
};
button_send3.onclick = function() {
var value_video = send_audio3.options[send_audio3.selectedIndex].value;
console.log(value_video);
sendMessage(value_video);
};
var channel = location.href.replace(/\/|:|#|%|\.|\[|\]/g, '');
var pub = 'pub-f986077a-73bd-4c28-8e50-2e44076a84e0';
var sub = 'sub-b8f4c07a-352e-11e2-bb9d-c7df1d04ae4a';
WebSocket = PUBNUB.ws;
var websocket = new WebSocket('wss://pubsub.pubnub.com/' + pub + '/' + sub + '/' + channel);
websocket.onopen = function() {}
websocket.onerror = function() {
//location.reload();
};
websocket.onclose = function() {
//location.reload();
};
websocket.push = websocket.send;
websocket.send = function(data) {
websocket.push(JSON.stringify(data));
};
var peer = new PeerConnection(websocket);
peer.onUserFound = function(userid) {
if (document.getElementById(userid)) return;
var tr = document.createElement('tr');
var td1 = document.createElement('td');
var td2 = document.createElement('td');
td1.innerHTML = userid + ' has camera. Are you interested in video chat?';
var button = document.createElement('button');
button.innerHTML = 'Join';
button.id = userid;
button.style.float = 'right';
button.onclick = function() {
button = this;
getUserMedia(function(stream) {
console.log(stream);
peer.addStream(stream);
peer.sendParticipationRequest(button.id);
document.getElementById("experiment").style.display = "none";
});
button.disabled = true;
};
td2.appendChild(button);
tr.appendChild(td1);
tr.appendChild(td2);
roomsList.appendChild(tr);
};
peer.onStreamAdded = function(e) {
if (e.type == 'local') document.querySelector('#start-broadcasting').disabled = false;
var video = e.mediaElement;
video.setAttribute('width', "100%");
video.setAttribute('controls', true);
video.setAttribute("id", e.type);
if (e.type == "remote") {
videosContainerRemote.insertBefore(video, videosContainerRemote.firstChild);
} else {
videosContainer.insertBefore(video, videosContainer.firstChild);
}
//videosContainerRemote.style.display = "none";
video.play();
//rotateVideo(video);
//scaleVideos();
};
peer.onStreamEnded = function(e) {
var video = e.mediaElement;
if (video) {
video.style.opacity = 1;
rotateVideo(video);
setTimeout(function() {
video.parentNode.removeChild(video);
scaleVideos();
}, 1000);
}
};
document.querySelector('#start-broadcasting').onclick = function() {
this.disabled = true;
getUserMedia(function(stream) {
stream.data = "ciao";
console.log(stream);
peer.addStream(stream);
peer.startBroadcasting();
});
};
document.querySelector('#your-name').onchange = function() {
peer.userid = this.value;
};
var videosContainer = document.getElementById('videos-container') || document.body;
var videosContainerRemote = document.getElementById('videos-container-remote');
var btnSetupNewRoom = document.getElementById('setup-new-room');
var roomsList = document.getElementById('rooms-list');
if (btnSetupNewRoom) btnSetupNewRoom.onclick = setupNewRoomButtonClickHandler;
/* function rotateVideo(video) {
video.style[navigator.mozGetUserMedia ? 'transform' : '-webkit-transform'] = 'rotate(0deg)';
setTimeout(function() {
video.style[navigator.mozGetUserMedia ? 'transform' : '-webkit-transform'] = 'rotate(360deg)';
}, 1000);
}*/
function scaleVideos() {
var videos = document.querySelectorAll('video'),
length = videos.length,
video;
var minus = 130;
var windowHeight = 700;
var windowWidth = 600;
var windowAspectRatio = windowWidth / windowHeight;
var videoAspectRatio = 4 / 3;
var blockAspectRatio;
var tempVideoWidth = 0;
var maxVideoWidth = 0;
for (var i = length; i > 0; i--) {
blockAspectRatio = i * videoAspectRatio / Math.ceil(length / i);
if (blockAspectRatio <= windowAspectRatio) {
tempVideoWidth = videoAspectRatio * windowHeight / Math.ceil(length / i);
} else {
tempVideoWidth = windowWidth / i;
}
if (tempVideoWidth > maxVideoWidth)
maxVideoWidth = tempVideoWidth;
}
for (var i = 0; i < length; i++) {
video = videos[i];
if (video)
video.width = maxVideoWidth - minus;
}
}
window.onresize = scaleVideos;
// you need to capture getUserMedia yourself!
function getUserMedia(callback) {
var hints = {
audio: true,
video: true
};
/*video:{
optional: [],
mandatory: {}
}*/
navigator.getUserMedia(hints, function(stream) {
var video = document.createElement('video');
video.srcObject = stream;
video.controls = true;
video.muted = true;
peer.onStreamAdded({
mediaElement: video,
userid: 'self',
stream: stream
});
callback(stream);
});
}
(function() {
var uniqueToken = document.getElementById('unique-token');
if (uniqueToken)
if (location.hash.length > 2) uniqueToken.parentNode.parentNode.parentNode.innerHTML = '<h2 style="text-align:center;font-size:20px"><a href="' + location.href + '" target="_blank">Share this link</a></h2>';
else uniqueToken.innerHTML = uniqueToken.parentNode.parentNode.href = '#' + (Math.random() * new Date().getTime()).toString(36).toUpperCase().replace(/\./g, '-');
})();
// SEND MESSAGES FROM LOCAL TO REMOTE
function sendMessage(message) {
sendChannel.send(message);
}
function handleReceiveMessage(event) {
console.log("Ricevuto:" + event.data);
var received_value = event.data;
showVideo(received_value);
}
function showVideo(received_value) {
var div_live = document.getElementById("videos-container-remote");
var div_video = document.getElementById("videos-recorded");
var video = document.createElement('video');
video.src = "./videos/" + received_value + ".mp4";
video.setAttribute("width", "100%");
video.controls = false;
div_video.appendChild(video);
video.load();
video.addEventListener('loadeddata', function() {
div_video.style.display = "block";
div_live.style.display = "none";
video.play();
}, false);
video.addEventListener("ended", test, false);
function test(e) {
div_video.style.display = "none";
div_live.style.display = "block";
div_video.removeChild(video);
}
}
</script>
<footer>
</footer>
<!-- commits.js is useless for you! -->
<script src="https://cdn.webrtc-experiment.com/commits.js" async> </script>
</body>
</html>