-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
347 lines (295 loc) · 15.1 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
<!--
> Balaji Selvam
> MIT License - www.WebRTC-Experiment.com/licence
> Documentation - github.com/balajims88/WebRTC-Experiment/tree/master/video-broadcasting
-->
<!DOCTYPE html>
<html lang="en">
<head>
<title>WebRTC Video Broadcasting</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="style.css">
<style>
audio, 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>
<!-- This Library is used to detect WebRTC features -->
<script src="DetectRTC.js"></script>
<script src="socket.io.js"> </script>
<!-- <script src="adapter/adapter-latest.js"></script> -->
<script src="adapter/adapter.js"></script>
<!-- <script src="IceServersHandler.js"></script>-->
<script src="CodecsHandler.js"></script>
<script src="RTCPeerConnection-v1.5.js"> </script>
<script src="webrtc-broadcasting/broadcast.js"> </script>
</head>
<body>
<article id="artid">
<header style="text-align: center;">
<h1>
WebRTC Video Broadcasting
</h1>
</header>
<!-- copy this <section> and next <script> -->
<section class="experiment">
<section>
<select id="broadcasting-option">
<option>Audio + Video</option>
<option>Only Audio</option>
<option>Screen</option>
</select>
<input type="text" id="broadcast-name">
<button id="setup-new-broadcast" class="setup">Setup New Broadcast</button>
</section>
<!-- list of all available broadcasting rooms -->
<table style="width: 100%;" id="rooms-list"></table>
<!-- local/remote videos container -->
<div id="videos-container"></div>
</section>
<script>
// Muaz Khan - https://github.com/balajims88
// MIT License - licence/
// Documentation - https://github.com/balajims88/WebRTC-Experiment/tree/master/webrtc-broadcasting
document.documentElement.addEventListener("keyup", function(e) {
var ev = e || window.event; // window.event for IE fallback
// $('join').keypress(function(e){
if(e.keyCode==13){
console.log("on click the join button");
document.getElementsByClassName("join")[0].click();
}
//});
});
var config = {
openSocket: function(config) {
var SIGNALING_SERVER = 'https://socketio-over-nodejs2.herokuapp.com:443/';
config.channel = config.channel || location.href.replace(/\/|:|#|%|\.|\[|\]/g, '');
var sender = Math.round(Math.random() * 999999999) + 999999999;
io.connect(SIGNALING_SERVER).emit('new-channel', {
channel: config.channel,
sender: sender
});
var socket = io.connect(SIGNALING_SERVER + config.channel);
socket.channel = config.channel;
socket.on('connect', function () {
if (config.callback) config.callback(socket);
});
socket.send = function (message) {
socket.emit('message', {
sender: sender,
data: message
});
};
socket.on('message', config.onmessage);
},
onRemoteStream: function(htmlElement) {
videosContainer.appendChild(htmlElement);
rotateInCircle(htmlElement);
},
onRoomFound: function(room) {
var alreadyExist = document.querySelector('button[data-broadcaster="' + room.broadcaster + '"]');
if (alreadyExist) return;
if (typeof roomsList === 'undefined') roomsList = document.body;
var tr = document.createElement('tr');
tr.innerHTML = '<td><strong>' + room.roomName + '</strong> is broadcasting his media!</td>' +
'<td><button id="joinbtn" class="join">Join</button></td>';
roomsList.appendChild(tr);
var joinRoomButton = tr.querySelector('.join');
joinRoomButton.setAttribute('data-broadcaster', room.broadcaster);
joinRoomButton.setAttribute('data-roomToken', room.broadcaster);
joinRoomButton.onclick = function() {
this.disabled = true;
var broadcaster = this.getAttribute('data-broadcaster');
var roomToken = this.getAttribute('data-roomToken');
broadcastUI.joinRoom({
roomToken: roomToken,
joinUser: broadcaster
});
hideUnnecessaryStuff();
};
},
onNewParticipant: function(numberOfViewers) {
document.title = 'Viewers: ' + numberOfViewers;
},
onReady: function() {
console.log('now you can open or join rooms');
}
};
function setupNewBroadcastButtonClickHandler() {
document.getElementById('broadcast-name').disabled = true;
document.getElementById('setup-new-broadcast').disabled = true;
DetectRTC.load(function() {
captureUserMedia(function() {
var shared = 'video';
if (window.option == 'Only Audio') {
shared = 'audio';
}
if (window.option == 'Screen') {
shared = 'screen';
}
broadcastUI.createRoom({
roomName: (document.getElementById('broadcast-name') || { }).value || 'Anonymous',
isAudio: shared === 'audio'
});
});
hideUnnecessaryStuff();
});
}
function captureUserMedia(callback) {
var constraints = null;
window.option = broadcastingOption ? broadcastingOption.value : '';
if (option === 'Only Audio') {
constraints = {
audio: true,
video: false
};
if(DetectRTC.hasMicrophone !== true) {
alert('DetectRTC library is unable to find microphone; maybe you denied microphone access once and it is still denied or maybe microphone device is not attached to your system or another app is using same microphone.');
}
}
if (option === 'Screen') {
var video_constraints = {
mandatory: {
chromeMediaSource: 'screen'
},
optional: []
};
constraints = {
audio: false,
video: video_constraints
};
if(DetectRTC.isScreenCapturingSupported !== true) {
alert('DetectRTC library is unable to find screen capturing support. You MUST run chrome with command line flag "chrome --enable-usermedia-screen-capturing"');
}
}
if (option != 'Only Audio' && option != 'Screen' && DetectRTC.hasWebcam !== true) {
alert('DetectRTC library is unable to find webcam; maybe you denied webcam access once and it is still denied or maybe webcam device is not attached to your system or another app is using same webcam.');
}
var htmlElement = document.createElement(option === 'Only Audio' ? 'audio' : 'video');
htmlElement.muted = true;
htmlElement.volume = 0;
try {
htmlElement.setAttributeNode(document.createAttribute('autoplay'));
htmlElement.setAttributeNode(document.createAttribute('playsinline'));
htmlElement.setAttributeNode(document.createAttribute('controls'));
} catch (e) {
htmlElement.setAttribute('autoplay', true);
htmlElement.setAttribute('playsinline', true);
htmlElement.setAttribute('controls', true);
}
var mediaConfig = {
video: htmlElement,
onsuccess: function(stream) {
config.attachStream = stream;
videosContainer.appendChild(htmlElement);
rotateInCircle(htmlElement);
callback && callback();
},
onerror: function() {
if (option === 'Only Audio') alert('unable to get access to your microphone');
else if (option === 'Screen') {
if (location.protocol === 'http:') alert('Please test this WebRTC experiment on HTTPS.');
else alert('Screen capturing is either denied or not supported. Are you enabled flag: "Enable screen capture support in getUserMedia"?');
} else alert('unable to get access to your webcam');
}
};
if (constraints) mediaConfig.constraints = constraints;
getUserMedia(mediaConfig);
}
var broadcastUI = broadcast(config);
/* UI specific */
var videosContainer = document.getElementById('videos-container') || document.body;
var setupNewBroadcast = document.getElementById('setup-new-broadcast');
var roomsList = document.getElementById('rooms-list');
var broadcastingOption = document.getElementById('broadcasting-option');
if (setupNewBroadcast) setupNewBroadcast.onclick = setupNewBroadcastButtonClickHandler;
function hideUnnecessaryStuff() {
var visibleElements = document.getElementsByClassName('visible'),
length = visibleElements.length;
for (var i = 0; i < length; i++) {
visibleElements[i].style.display = 'none';
}
}
function rotateInCircle(video) {
video.style[navigator.mozGetUserMedia ? 'transform' : '-webkit-transform'] = 'rotate(0deg)';
setTimeout(function() {
video.style[navigator.mozGetUserMedia ? 'transform' : '-webkit-transform'] = 'rotate(360deg)';
}, 1000);
}
</script>
<section class="experiment">
<h2 class="header">How it works?</h2>
<ol>
<li>It broadcasts media in one-way direction over number of peers.</li>
<li>All peers can view/watch/listen the broadcast, anonymously.</li>
<li>"Anonymously" means viewers will NEVER be prompted to allow webcam or mic or screen.</li>
<li>Assume like a radio station inside the browser. Remember, it works only browser-to-browser.</li>
</ol>
</section>
<section class="experiment"><small id="send-message"></small></section>
</article>
<a href="https://github.com/balajims88/WebRTC-Experiment/tree/master/webrtc-broadcasting" class="fork-left"></a>
<footer>
<!-- <p>
<a href="">WebRTC Experiments</a>
© <a href="https://github.com/balajims88" rel="author" target="_blank">Balaji Selvam</a>
<a href="mailto:[email protected]" target="_blank">[email protected]</a>
</p> -->
</footer>
<!-- commits.js is useless for you! It is not part of this WebRTC Experiment. -->
<script src="commits.js" async> </script>
</body>
</html>