forked from webRTC-io/webrtc.io-demo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
203 lines (175 loc) · 5.22 KB
/
script.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
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
var videos = [];
var rooms = [1, 2, 3, 4, 5];
var PeerConnection = window.PeerConnection || window.webkitPeerConnection00 || window.webkitRTCPeerConnection;
function getNumPerRow() {
var len = videos.length;
var biggest;
// Ensure length is even for better division.
if(len % 2 === 1) {
len++;
}
biggest = Math.ceil(Math.sqrt(len));
while(len % biggest !== 0) {
biggest++;
}
return biggest;
}
function subdivideVideos() {
var perRow = getNumPerRow();
var numInRow = 0;
for(var i = 0, len = videos.length; i < len; i++) {
var video = videos[i];
setWH(video, i);
numInRow = (numInRow + 1) % perRow;
}
}
function setWH(video, i) {
var perRow = getNumPerRow();
var perColumn = Math.ceil(videos.length / perRow);
var width = Math.floor((window.innerWidth) / perRow);
var height = Math.floor((window.innerHeight - 190) / perColumn);
video.width = width;
video.height = height;
video.style.position = "absolute";
video.style.left = (i % perRow) * width + "px";
video.style.top = Math.floor(i / perRow) * height + "px";
}
function cloneVideo(domId, socketId) {
var video = document.getElementById(domId);
var clone = video.cloneNode(false);
clone.id = "remote" + socketId;
document.getElementById('videos').appendChild(clone);
videos.push(clone);
return clone;
}
function removeVideo(socketId) {
var video = document.getElementById('remote' + socketId);
if(video) {
videos.splice(videos.indexOf(video), 1);
video.parentNode.removeChild(video);
}
}
function addToChat(msg, color) {
var messages = document.getElementById('messages');
msg = sanitize(msg);
if(color) {
msg = '<span style="color: ' + color + '; padding-left: 15px">' + msg + '</span>';
} else {
msg = '<strong style="padding-left: 15px">' + msg + '</strong>';
}
messages.innerHTML = messages.innerHTML + msg + '<br>';
messages.scrollTop = 10000;
}
function sanitize(msg) {
return msg.replace(/</g, '<');
}
function initFullScreen() {
var button = document.getElementById("fullscreen");
button.addEventListener('click', function(event) {
var elem = document.getElementById("videos");
//show full screen
elem.webkitRequestFullScreen();
});
}
function initNewRoom() {
var button = document.getElementById("newRoom");
button.addEventListener('click', function(event) {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = 8;
var randomstring = '';
for(var i = 0; i < string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum, rnum + 1);
}
window.location.hash = randomstring;
location.reload();
})
}
var websocketChat = {
send: function(message) {
rtc._socket.send(message);
},
recv: function(message) {
return message;
},
event: 'receive_chat_msg'
};
var dataChannelChat = {
send: function(message) {
for(var connection in rtc.dataChannels) {
var channel = rtc.dataChannels[connection];
channel.send(message);
}
},
recv: function(channel, message) {
return JSON.parse(message).data;
},
event: 'data stream data'
};
function initChat() {
var chat;
if(rtc.dataChannelSupport) {
console.log('initializing data channel chat');
chat = dataChannelChat;
} else {
console.log('initializing websocket chat');
chat = websocketChat;
}
var input = document.getElementById("chatinput");
var room = window.location.hash.slice(1);
var color = "#" + ((1 << 24) * Math.random() | 0).toString(16);
input.addEventListener('keydown', function(event) {
var key = event.which || event.keyCode;
if(key === 13) {
chat.send(JSON.stringify({
"eventName": "chat_msg",
"data": {
"messages": input.value,
"room": room,
"color": color
}
}));
addToChat(input.value);
input.value = "";
}
}, false);
rtc.on(chat.event, function() {
data = chat.recv.apply(this, arguments);
console.log(data.color);
addToChat(data.messages, data.color.toString(16));
});
}
function init() {
if(PeerConnection) {
rtc.createStream({
"video": true,
"audio": true
}, function(stream) {
document.getElementById('you').src = URL.createObjectURL(stream);
videos.push(document.getElementById('you'));
//rtc.attachStream(stream, 'you');
subdivideVideos();
});
} else {
alert('Your browser is not supported or you have to turn on flags. In chrome you go to chrome://flags and turn on Enable PeerConnection remember to restart chrome');
}
var room = window.location.hash.slice(1);
rtc.connect("ws:" + window.location.href.substring(window.location.protocol.length).split('#')[0], room);
rtc.on('add remote stream', function(stream, socketId) {
console.log("ADDING REMOTE STREAM...");
var clone = cloneVideo('you', socketId);
document.getElementById(clone.id).setAttribute("class", "");
rtc.attachStream(stream, clone.id);
subdivideVideos();
});
rtc.on('disconnect stream', function(data) {
console.log('remove ' + data);
removeVideo(data);
});
initFullScreen();
initNewRoom();
initChat();
}
window.onresize = function(event) {
subdivideVideos();
};