-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex-screenshare.html
508 lines (431 loc) · 25.1 KB
/
index-screenshare.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
<!--
> 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/Pluginfree-Screen-Sharing
-->
<!DOCTYPE html>
<html lang="en">
<head>
<title>WebRTC Screen Sharing | Plugin-free ® Muaz Khan</title>
<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 type="text/javascript" src="https://code.jquery.com/jquery.js"></script>
<link rel="stylesheet" href="https://webrtcexperiment-webrtc.netdna-ssl.com/style.css">
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<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://webrtcexperiment-webrtc.netdna-ssl.com/firebase.js'> </script>
<script src="conference.js"> </script>
</head>
<body>
<article>
<!-- just copy this <section> and next script -->
<section class="experiment">
<section>
<button id="share-screen" class="setup" disabled>Share Your Screen</button>
</section>
<video id="live" width="1280" height="960" autoplay></video>
<!-- list of all available broadcasting rooms -->
<table style="width: 100%;" id="rooms-list"></table>
<canvas width="1280" id="canvas" height="960" style="display:none" ></canvas>
</section>
<script>
function intallFirefoxScreenCapturingExtension() {
InstallTrigger.install({
'Foo': {
// URL: 'https://addons.mozilla.org/en-US/firefox/addon/enable-screen-capturing/',
URL: 'https://addons.mozilla.org/firefox/downloads/file/355418/enable_screen_capturing_in_firefox-1.0.006-fx.xpi?src=cb-dl-hotness',
toString: function() {
return this.URL;
}
}
});
}
// 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/Pluginfree-Screen-Sharing
var isWebRTCExperimentsDomain = document.domain.indexOf('webrtc-experiment.com') != -1;
var config = {
openSocket: function(config) {
var channel = config.channel || 'screen-capturing-' + location.href.replace( /\/|:|#|%|\.|\[|\]/g , '');
var socket = new Firebase('https://webrtc.firebaseIO.com/' + channel);
socket.channel = channel;
socket.on("child_added", function(data) {
config.onmessage && config.onmessage(data.val());
});
socket.send = function(data) {
this.push(data);
};
config.onopen && setTimeout(config.onopen, 1);
socket.onDisconnect().remove();
return socket;
},
onRemoteStream: function(media) {
var video = media.video;
video.setAttribute('controls', true);
videosContainer.insertBefore(video, videosContainer.firstChild);
video.play();
rotateVideo(video);
},
onRoomFound: function(room) {
if(location.hash.replace('#', '').length) {
// private rooms should auto be joined.
conferenceUI.joinRoom({
roomToken: room.roomToken,
joinUser: room.broadcaster
});
return;
}
var alreadyExist = document.getElementById(room.broadcaster);
if (alreadyExist) return;
if (typeof roomsList === 'undefined') roomsList = document.body;
var tr = document.createElement('tr');
tr.setAttribute('id', room.broadcaster);
tr.innerHTML = '<td>' + room.roomName + '</td>' +
'<td><button class="join" id="' + room.roomToken + '">Open Screen</button></td>';
roomsList.insertBefore(tr, roomsList.firstChild);
var button = tr.querySelector('.join');
button.onclick = function() {
var button = this;
button.disabled = true;
conferenceUI.joinRoom({
roomToken: button.id,
joinUser: button.parentNode.parentNode.id
});
};
},
onNewParticipant: function(numberOfParticipants) {
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!';
}
},
oniceconnectionstatechange: function(state) {
if(state == 'failed') {
alert('Failed to bypass Firewall rules. It seems that target user did not receive your screen. Please ask him reload the page and try again.');
}
if(state == 'connected') {
alert('A user successfully received your screen.');
}
}
};
function captureUserMedia(callback, extensionAvailable) {
console.log('captureUserMedia chromeMediaSource', DetectRTC.screen.chromeMediaSource);
var screen_constraints = {
mandatory: {
chromeMediaSource: DetectRTC.screen.chromeMediaSource,
maxWidth: screen.width > 1920 ? screen.width : 1920,
maxHeight: screen.height > 1080 ? screen.height : 1080
// minAspectRatio: 1.77
},
optional: [{ // non-official Google-only optional constraints
googTemporalLayeredScreencast: true
}, {
googLeakyBucket: true
}]
};
// try to check if extension is installed.
if(isChrome && isWebRTCExperimentsDomain && typeof extensionAvailable == 'undefined' && DetectRTC.screen.chromeMediaSource != 'desktop') {
DetectRTC.screen.isChromeExtensionAvailable(function(available) {
captureUserMedia(callback, available);
});
return;
}
if(isChrome && isWebRTCExperimentsDomain && DetectRTC.screen.chromeMediaSource == 'desktop' && !DetectRTC.screen.sourceId) {
DetectRTC.screen.getSourceId(function(error) {
if(error && error == 'PermissionDeniedError') {
alert('PermissionDeniedError: User denied to share content of his screen.');
}
captureUserMedia(callback);
});
return;
}
// for non-www.webrtc-experiment.com domains
if(isChrome && !isWebRTCExperimentsDomain && !DetectRTC.screen.sourceId) {
window.addEventListener('message', function (event) {
if (event.data && event.data.chromeMediaSourceId) {
var sourceId = event.data.chromeMediaSourceId;
DetectRTC.screen.sourceId = sourceId;
DetectRTC.screen.chromeMediaSource = 'desktop';
if (sourceId == 'PermissionDeniedError') {
return alert('User denied to share content of his screen.');
}
captureUserMedia(callback, true);
}
if (event.data && event.data.chromeExtensionStatus) {
warn('Screen capturing extension status is:', event.data.chromeExtensionStatus);
DetectRTC.screen.chromeMediaSource = 'screen';
captureUserMedia(callback, true);
}
});
screenFrame.postMessage();
return;
}
if(isChrome && DetectRTC.screen.chromeMediaSource == 'desktop') {
screen_constraints.mandatory.chromeMediaSourceId = DetectRTC.screen.sourceId;
}
var constraints = {
audio: false,
video: screen_constraints
};
if(!!navigator.mozGetUserMedia) {
console.warn(Firefox_Screen_Capturing_Warning);
constraints.video = {
mozMediaSource: 'window',
mediaSource: 'window',
maxWidth: 1920,
maxHeight: 1080,
minAspectRatio: 1.77
};
}
console.log( JSON.stringify( constraints , null, '\t') );
// var vi = $("#live").get()[0];
// video.setAttribute('autoplay', true);
// video.setAttribute('controls', true);
// videosContainer.insertBefore(video, videosContainer.firstChild);
getUserMedia({
video: 'w',
constraints: constraints,
onsuccess: function(stream) {
config.attachStream = stream;
callback && callback();
video.setAttribute('muted', true);
rotateVideo(video);
},
onerror: function() {
if (isChrome && location.protocol === 'http:') {
alert('Please test this WebRTC experiment on HTTPS.');
} else if(isChrome) {
alert('Screen capturing is either denied or not supported. Please install chrome extension for screen capturing or run chrome with command-line flag: --enable-usermedia-screen-capturing');
}
else if(!!navigator.mozGetUserMedia) {
alert(Firefox_Screen_Capturing_Warning);
}
}
});
}
/* on page load: get public rooms */
var conferenceUI = conference(config);
/* UI specific */
var videosContainer = document.getElementById("videos-container") || document.body;
var roomsList = document.getElementById('rooms-list');
document.getElementById('share-screen').onclick = function() {
var roomName = "wow";
roomName.disabled = true;
captureUserMedia(function() {
conferenceUI.createRoom({
roomName: (roomName.value || 'Anonymous') + ' shared his screen with you'
});
});
this.disabled = true;
};
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;"><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 , '-');
})();
var Firefox_Screen_Capturing_Warning = 'Make sure that you are using Firefox Nightly and you enabled: media.getusermedia.screensharing.enabled flag from about:config page. You also need to add your domain in "media.getusermedia.screensharing.allowed_domains" flag.';
</script>
<script>
var screenFrame, loadedScreenFrame;
function loadScreenFrame(skip) {
if(loadedScreenFrame) return;
if(!skip) return loadScreenFrame(true);
loadedScreenFrame = true;
var iframe = document.createElement('iframe');
iframe.onload = function () {
iframe.isLoaded = true;
console.log('Screen Capturing frame is loaded.');
document.getElementById('share-screen').disabled = false;
document.getElementById('room-name').disabled = false;
};
iframe.src = 'https://www.webrtc-experiment.com/getSourceId/';
iframe.style.display = 'none';
(document.body || document.documentElement).appendChild(iframe);
screenFrame = {
postMessage: function () {
if (!iframe.isLoaded) {
setTimeout(screenFrame.postMessage, 100);
return;
}
console.log('Asking iframe for sourceId.');
iframe.contentWindow.postMessage({
captureSourceId: true
}, '*');
}
};
};
if(!isWebRTCExperimentsDomain) {
loadScreenFrame();
}
else {
document.getElementById('share-screen').disabled = false;
document.getElementById('room-name').disabled = false;
}
</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? <a href="https://addons.mozilla.org/en-US/firefox/addon/enable-screen-capturing/">Store</a> / <a href="https://github.com/muaz-khan/Firefox-Extensions/tree/master/enable-screen-capturing">Source Code</a> / <button onclick="intallFirefoxScreenCapturingExtension(); this.disabled = true;" style="font-size:inherit; padding-bottom:0;">Click to Install</button>
</li>
</ol>
</section>
</body>
</html>