forked from muaz-khan/WebRTC-Experiment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
520 lines (429 loc) · 23.3 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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
<!--
> 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/screen-sharing
-->
<!DOCTYPE html>
<html lang="en">
<head>
<title>WebRTC Screen Sharing</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">
<link rel="stylesheet" href="https://cdn.webrtc-experiment.com/style.css">
<style>
video {
-moz-transition: all 1s ease;
-ms-transition: all 1s ease;
-o-transition: all 1s ease;
-webkit-transition: all 1s ease;
transition: all 1s ease;
vertical-align: top;
width: 100%;
}
input {
border: 1px solid #d9d9d9;
border-radius: 1px;
font-size: 2em;
margin: .2em;
width: 30%;
}
select {
border: 1px solid #d9d9d9;
border-radius: 1px;
height: 50px;
margin-left: 1em;
margin-right: -12px;
padding: 1.1em;
vertical-align: 6px;
width: 18%;
}
.setup {
border-bottom-left-radius: 0;
border-top-left-radius: 0;
font-size: 102%;
height: 47px;
margin-left: -9px;
margin-top: 8px;
position: absolute;
}
p { padding: 1em; }
li {
border-bottom: 1px solid rgb(189, 189, 189);
border-left: 1px solid rgb(189, 189, 189);
padding: .5em;
}
</style>
<script>
document.createElement('article');
document.createElement('footer');
</script>
<link rel="chrome-webstore-item" href="https://chrome.google.com/webstore/detail/ajhifddimkapgcifgcodmmfdlknahffk">
<!-- scripts used for screen-sharing -->
<script src="https://cdn.webrtc-experiment.com/socket.io.js"> </script>
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
<script src="https://cdn.webrtc-experiment.com/IceServersHandler.js"></script>
<script src="https://cdn.webrtc-experiment.com/getScreenId.js"> </script>
<script src="https://cdn.webrtc-experiment.com/CodecsHandler.js"></script>
<script src="https://cdn.webrtc-experiment.com/BandwidthHandler.js"></script>
<script src="https://cdn.webrtc-experiment.com/screen.js"> </script>
</head>
<body>
<article>
<header style="text-align: center;">
<h1>
WebRTC Screen Sharing
</h1>
<p>
<a href="https://www.webrtc-experiment.com/">HOME</a>
<span> © </span>
<a href="http://www.MuazKhan.com/" target="_blank">Muaz Khan</a>
.
<a href="http://twitter.com/WebRTCWeb" target="_blank" title="Twitter profile for WebRTC Experiments">@WebRTCWeb</a>
.
<a href="https://github.com/muaz-khan?tab=repositories" target="_blank" title="Github Profile">Github</a>
.
<a href="https://github.com/muaz-khan/WebRTC-Experiment/issues?state=open" target="_blank">Latest issues</a>
.
<a href="https://github.com/muaz-khan/WebRTC-Experiment/commits/master" target="_blank">What's New?</a>
</p>
</header>
<div class="github-stargazers"></div>
<h2 id="number-of-participants" style="display: block;text-align: center;border:0;margin-bottom:0;">Its a full-HD (1080p) screen sharing application using <a href="https://www.webrtc-experiment.com/">WebRTC</a>!</h2>
<!-- just copy this <section> and next script -->
<section class="experiment">
<section class="hide-after-join">
<span>
Private ?? <a href="/screen-sharing/" target="_blank" title="Open this link for private screen sharing!"><code><strong id="unique-token">#123456789</strong></code></a>
</span>
<input type="text" id="user-name" placeholder="Your Name">
<button id="share-screen" class="setup">Share Your Screen</button>
</section>
<!-- list of all available broadcasting rooms -->
<table style="width: 100%;" id="rooms-list" class="hide-after-join"></table>
<!-- local/remote videos container -->
<div id="videos-container"></div>
</section>
<script>
// 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/screen-sharing
var videosContainer = document.getElementById("videos-container") || document.body;
var roomsList = document.getElementById('rooms-list');
var screensharing = new Screen();
var channel = location.href.replace(/\/|:|#|%|\.|\[|\]/g, '');
var sender = Math.round(Math.random() * 999999999) + 999999999;
// https://github.com/muaz-khan/WebRTC-Experiment/tree/master/socketio-over-nodejs
var SIGNALING_SERVER = 'https://socketio-over-nodejs2.herokuapp.com:443/';
io.connect(SIGNALING_SERVER).emit('new-channel', {
channel: channel,
sender: sender
});
var socket = io.connect(SIGNALING_SERVER + channel);
socket.on('connect', function () {
// setup peer connection & pass socket object over the constructor!
});
socket.send = function (message) {
socket.emit('message', {
sender: sender,
data: message
});
};
screensharing.openSignalingChannel = function(callback) {
return socket.on('message', callback);
};
screensharing.onscreen = function(_screen) {
var alreadyExist = document.getElementById(_screen.userid);
if (alreadyExist) return;
if (typeof roomsList === 'undefined') roomsList = document.body;
var tr = document.createElement('tr');
tr.id = _screen.userid;
tr.innerHTML = '<td>' + _screen.userid + ' shared his screen.</td>' +
'<td><button class="join">View</button></td>';
roomsList.insertBefore(tr, roomsList.firstChild);
var button = tr.querySelector('.join');
button.setAttribute('data-userid', _screen.userid);
button.setAttribute('data-roomid', _screen.roomid);
button.onclick = function() {
var button = this;
button.disabled = true;
var _screen = {
userid: button.getAttribute('data-userid'),
roomid: button.getAttribute('data-roomid')
};
screensharing.view(_screen);
};
};
// on getting each new screen
screensharing.onaddstream = function(media) {
media.video.id = media.userid;
var video = media.video;
videosContainer.insertBefore(video, videosContainer.firstChild);
rotateVideo(video);
var hideAfterJoin = document.querySelectorAll('.hide-after-join');
for(var i = 0; i < hideAfterJoin.length; i++) {
hideAfterJoin[i].style.display = 'none';
}
if(media.type === 'local') {
addStreamStopListener(media.stream, function() {
location.reload();
});
}
};
// using firebase for signaling
// screen.firebase = 'signaling';
// if someone leaves; just remove his screen
screensharing.onuserleft = function(userid) {
var video = document.getElementById(userid);
if (video && video.parentNode) video.parentNode.removeChild(video);
// location.reload();
};
// check pre-shared screens
screensharing.check();
document.getElementById('share-screen').onclick = function() {
var username = document.getElementById('user-name');
username.disabled = this.disabled = true;
screensharing.isModerator = true;
screensharing.userid = username.value;
screensharing.share();
};
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() {
var uniqueToken = document.getElementById('unique-token');
if (uniqueToken)
if (location.hash.length > 2) uniqueToken.parentNode.parentNode.parentNode.innerHTML = '<h2 style="text-align:center; display: block"><a href="' + location.href + '" target="_blank">Right click to copy & share this private link</a></h2>';
else uniqueToken.innerHTML = uniqueToken.parentNode.parentNode.href = '#' + (Math.random() * new Date().getTime()).toString(36).toUpperCase().replace( /\./g , '-');
})();
screensharing.onNumberOfParticipantsChnaged = function(numberOfParticipants) {
if(!screensharing.isModerator) return;
document.title = numberOfParticipants + ' users are viewing your screen!';
var element = document.getElementById('number-of-participants');
if (element) {
element.innerHTML = numberOfParticipants + ' users are viewing your screen!';
}
};
</script>
<script>
// todo: need to check exact chrome browser because opera also uses chromium framework
var isChrome = !!navigator.webkitGetUserMedia;
// DetectRTC.js - https://github.com/muaz-khan/WebRTC-Experiment/tree/master/DetectRTC
// Below code is taken from RTCMultiConnection-v1.8.js (http://www.rtcmulticonnection.org/changes-log/#v1.8)
var DetectRTC = {};
(function () {
var screenCallback;
DetectRTC.screen = {
chromeMediaSource: 'screen',
getSourceId: function(callback) {
if(!callback) throw '"callback" parameter is mandatory.';
screenCallback = callback;
window.postMessage('get-sourceId', '*');
},
isChromeExtensionAvailable: function(callback) {
if(!callback) return;
if(DetectRTC.screen.chromeMediaSource == 'desktop') return callback(true);
// ask extension if it is available
window.postMessage('are-you-there', '*');
setTimeout(function() {
if(DetectRTC.screen.chromeMediaSource == 'screen') {
callback(false);
}
else callback(true);
}, 2000);
},
onMessageCallback: function(data) {
if (!(typeof data == 'string' || !!data.sourceId)) return;
console.log('chrome message', data);
// "cancel" button is clicked
if(data == 'PermissionDeniedError') {
DetectRTC.screen.chromeMediaSource = 'PermissionDeniedError';
if(screenCallback) return screenCallback('PermissionDeniedError');
else throw new Error('PermissionDeniedError');
}
// extension notified his presence
if(data == 'rtcmulticonnection-extension-loaded') {
if(document.getElementById('install-button')) {
document.getElementById('install-button').parentNode.innerHTML = '<strong>Great!</strong> <a href="https://chrome.google.com/webstore/detail/screen-capturing/ajhifddimkapgcifgcodmmfdlknahffk" target="_blank">Google chrome extension</a> is installed.';
}
DetectRTC.screen.chromeMediaSource = 'desktop';
}
// extension shared temp sourceId
if(data.sourceId) {
DetectRTC.screen.sourceId = data.sourceId;
if(screenCallback) screenCallback( DetectRTC.screen.sourceId );
}
},
getChromeExtensionStatus: function (callback) {
if (!!navigator.mozGetUserMedia) return callback('not-chrome');
var extensionid = 'ajhifddimkapgcifgcodmmfdlknahffk';
var image = document.createElement('img');
image.src = 'chrome-extension://' + extensionid + '/icon.png';
image.onload = function () {
DetectRTC.screen.chromeMediaSource = 'screen';
window.postMessage('are-you-there', '*');
setTimeout(function () {
if (!DetectRTC.screen.notInstalled) {
callback('installed-enabled');
}
}, 2000);
};
image.onerror = function () {
DetectRTC.screen.notInstalled = true;
callback('not-installed');
};
}
};
// check if desktop-capture extension installed.
if(window.postMessage && isChrome) {
DetectRTC.screen.isChromeExtensionAvailable();
}
})();
DetectRTC.screen.getChromeExtensionStatus(function(status) {
if(status == 'installed-enabled') {
if(document.getElementById('install-button')) {
document.getElementById('install-button').parentNode.innerHTML = '<strong>Great!</strong> <a href="https://chrome.google.com/webstore/detail/screen-capturing/ajhifddimkapgcifgcodmmfdlknahffk" target="_blank">Google chrome extension</a> is installed.';
}
DetectRTC.screen.chromeMediaSource = 'desktop';
}
});
window.addEventListener('message', function (event) {
if (event.origin != window.location.origin) {
return;
}
DetectRTC.screen.onMessageCallback(event.data);
});
console.log('current chromeMediaSource', DetectRTC.screen.chromeMediaSource);
</script>
<section class="experiment">
<h2>Prerequisites</h2>
<ol>
<li>
Chrome?
<a href="https://chrome.google.com/webstore/detail/screen-capturing/ajhifddimkapgcifgcodmmfdlknahffk" target="_blank">Store</a>
/ <a href="https://github.com/muaz-khan/Chrome-Extensions/tree/master/desktopCapture">Source Code</a>
/
<button onclick="!!navigator.webkitGetUserMedia && !!window.chrome && !!chrome.webstore && !!chrome.webstore.install && chrome.webstore.install('https://chrome.google.com/webstore/detail/ajhifddimkapgcifgcodmmfdlknahffk', function() {location.reload();})" id="install-button" style="font-size:inherit; padding-bottom:0;">Click to Install</button>
</li>
<li>
Firefox? Please use version 52 or higher. Also use HTTPs.
</li>
<li>
Edge? Please use version 17 or higher. Also use HTTPs.
</li>
<li>
Safari? Please use version 11 (on Mac or iphone/ipad). However Safari can merely view screens. Safari can not share its own screen.
</li>
</ol>
</section>
<section class="experiment"><small id="send-message"></small></section>
<section class="experiment">
<h2>Advantages</h2>
<ol>
<li>Share full screen with one or more users in <strong>HD</strong> format!</li>
<li>Share screen from chrome and view over all WebRTC compatible browsers/plugins.</li>
<li>
You can open private rooms and it will be really "totally" private!<br /><br />
<ol>
<li>Use hashes to open private rooms: <strong>#private-room</strong></li>
<li>Use URL parameters to open private rooms: <strong>?private=room</strong></li>
</ol>
</li>
</ol>
</section>
<section class="experiment">
<h2>Common issues & queries</h2>
<ol>
<li>Recursive cascade images or blurred screen experiences occur only when you try to share screen between two tabs on the same system. This NEVER happens when sharing between unique systems or devices.</li>
<li>Opera/IE/Safari has no support of screen-capturing yet. However, you can view shared screens on Opera/IE/Safari!</li>
<li>Remember, it is not desktop sharing! It is just a state-less screen sharing. Desktop sharing is possible only through native (C++) applications.</li>
</ol>
</section>
<section class="experiment">
<h2 class="header">Why Screen Sharing Fails?</h2>
<ol>
<li>
You've not used '<strong>chromeMediaSource</strong>' or '<strong>mediaSource</strong>' constraint:
<pre>
// for chrome
mandatory: {chromeMediaSource: 'screen'}
// or desktop-Capturing
mandatory: {chromeMediaSource: 'desktop'}
// for Firefox (https-only)
video: {
mediaSource: 'window' || 'screen'
}
// for Edge >= 17 (https-only)
navigator.getDisplayMedia({
video: true
}).then(successCallback, errorCallback);
</pre>
</li>
<li>On chrome, you requested audio-stream alongwith '<strong>chromeMediaSource</strong>' – it is not permitted on chrome. Remember, Firefox is supporting audio+screen from single getUserMedia request.</li>
<li>On chrome, you're not testing it on SSL origin (HTTPS domain) otherwise you didn't enable <code><a href="http://peter.sh/experiments/chromium-command-line-switches/#allow-http-screen-capture" target="_blank">--allow-http-screen-capture</a></code> command-line flag on canary. Firefox is supporting screen capturing from both HTTP and HTTPs domains.</li>
<li>You may used media constraints like min/max frameRate; bandwidth or min/max width/height like 2000*2000 that is "currently" not allowed.</li>
</ol>
</section>
<section class="experiment">
<h2 class="header">How to use <a href="https://github.com/muaz-khan/WebRTC-Experiment/tree/master/screen-sharing">screen.js</a>?</h2>
<pre>
<script src="//cdn.webrtc-experiment.com/screen.js"></script>
</pre>
<pre>
var screen = new Screen('screen-unique-id');
// get shared screens
screen.onaddstream = function(e) {
document.body.appendChild(e.video);
};
// custom signaling channel
// you can use each and every signaling channel
// any websocket, socket.io, or XHR implementation
// any SIP server
// anything! etc.
screen.openSignalingChannel = function(callback) {
return io.connect().on('message', callback);
};
// check pre-shared screens
// it is useful to auto-view
// or search pre-shared screens
screen.check();
document.getElementById('share-screen').onclick = function() {
screen.share('screen name');
};
// to stop sharing screen
// screen.leave();
</pre>
</section>
<section class="experiment">
<h2 class="header">Suggestions</h2>
<ol>
<li>
<a href="https://github.com/muaz-khan/RTCMultiConnection" target="_blank">RTCMultiConnection.js</a> can be used for HD screen sharing; HD audio/video streaming; fastest file sharing; and writing entire skype-like app in the browser!
</li>
</ol>
</section>
</article>
<a href="https://github.com/muaz-khan/WebRTC-Experiment/tree/master/screen-sharing" class="fork-left"></a>
<footer>
<p>
<a href="https://www.webrtc-experiment.com/">WebRTC Experiments</a>
© <a href="https://plus.google.com/+MuazKhan" rel="author" target="_blank">Muaz Khan</a>
<a href="mailto:[email protected]" target="_blank">[email protected]</a>
</p>
</footer>
<!-- commits.js is useless for you! -->
<script src="https://cdn.webrtc-experiment.com/commits.js" async> </script>
</body>
</html>