forked from muaz-khan/WebRTC-Experiment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroadcast-ui.js
135 lines (111 loc) · 4.46 KB
/
broadcast-ui.js
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
// Muaz Khan - www.MuazKhan.com
// MIT License - www.WebRTC-Experiment.com/licence
// Experiments - github.com/muaz-khan/WebRTC-Experiment
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(media) {
var audio = media.audio;
audio.setAttribute('controls', true);
audio.setAttribute('autoplay', true);
participants.insertBefore(audio, participants.firstChild);
audio.play();
rotateAudio(audio);
},
onRoomFound: function(room) {
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 + '">Join Room</button></td>';
roomsList.insertBefore(tr, roomsList.firstChild);
tr.onclick = function() {
tr = this;
captureUserMedia(function() {
broadcastUI.joinRoom({
roomToken: tr.querySelector('.join').id,
joinUser: tr.id
});
});
hideUnnecessaryStuff();
};
}
};
function createButtonClickHandler() {
captureUserMedia(function() {
broadcastUI.createRoom({
roomName: (document.getElementById('conference-name') || { }).value || 'Anonymous'
});
});
hideUnnecessaryStuff();
}
function captureUserMedia(callback) {
var audio = document.createElement('audio');
audio.setAttribute('autoplay', true);
audio.setAttribute('controls', true);
audio.muted = true;
audio.volume = 0;
participants.insertBefore(audio, participants.firstChild);
getUserMedia({
video: audio,
constraints: { audio: true, video: false },
onsuccess: function(stream) {
config.attachStream = stream;
callback && callback();
audio.muted = true;
audio.volume = 0;
rotateAudio(audio);
},
onerror: function() {
alert('unable to get access to your microphone.');
callback && callback();
}
});
}
/* on page load: get public rooms */
var broadcastUI = broadcast(config);
/* UI specific */
var participants = document.getElementById("participants") || document.body;
var startConferencing = document.getElementById('start-conferencing');
var roomsList = document.getElementById('rooms-list');
if (startConferencing) startConferencing.onclick = createButtonClickHandler;
function hideUnnecessaryStuff() {
var visibleElements = document.getElementsByClassName('visible'),
length = visibleElements.length;
for (var i = 0; i < length; i++) {
visibleElements[i].style.display = 'none';
}
}
function rotateAudio(audio) {
audio.style[navigator.mozGetUserMedia ? 'transform' : '-webkit-transform'] = 'rotate(0deg)';
setTimeout(function() {
audio.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 , '-');
})();