-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
192 lines (159 loc) · 6.32 KB
/
index.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
$(document).ready(function () {
// Set event listeners for user interface widgets
if (!location.hash.replace('#', '').length) {
//location.href = location.href.split('#')[0] + '#' + (Math.random() * 100).toString().replace('.', '');
location.href = location.href.split('#')[0] + '#' + ("micol").toString().replace('.', '')
location.reload();
}
var channel = location.href.replace(/\/|:|#|%|\.|\[|\]/g, '');
var pub = 'pub-f986077a-73bd-4c28-8e50-2e44076a84e0';
var sub = 'sub-b8f4c07a-352e-11e2-bb9d-c7df1d04ae4a';
WebSocket = PUBNUB.ws;
var websocket = new WebSocket('wss://pubsub.pubnub.com/' + pub + '/' + sub + '/' + channel);
websocket.onerror = function () {
//location.reload();
};
websocket.onclose = function () {
//location.reload();
};
websocket.push = websocket.send;
websocket.send = function (data) {
websocket.push(JSON.stringify(data));
};
var peer = new PeerConnection(websocket);
peer.onUserFound = function (userid) {
if (document.getElementById(userid)) return;
var tr = document.createElement('tr');
var td1 = document.createElement('td');
var td2 = document.createElement('td');
td1.innerHTML = userid + ' has camera. Are you interested in video chat?';
var button = document.createElement('button');
button.innerHTML = 'Join';
button.id = userid;
button.style.float = 'right';
button.onclick = function () {
button = this;
getUserMedia(function (stream) {
peer.addStream(stream);
peer.sendParticipationRequest(button.id);
document.getElementById("alien").style.display = "block";
document.getElementById("experiment").style.display = "none"
});
button.disabled = true;
};
td2.appendChild(button);
tr.appendChild(td1);
tr.appendChild(td2);
roomsList.appendChild(tr);
};
peer.onStreamAdded = function (e) {
//console.log(e);
if (e.type == 'local') document.querySelector('#start-broadcasting').disabled = false;
var video = e.mediaElement;
video.setAttribute('width', 600);
video.setAttribute('controls', true);
videosContainer.insertBefore(video, videosContainer.firstChild);
video.play();
//rotateVideo(video);
scaleVideos();
};
peer.onStreamEnded = function (e) {
var video = e.mediaElement;
if (video) {
video.style.opacity = 1;
rotateVideo(video);
setTimeout(function () {
video.parentNode.removeChild(video);
scaleVideos();
}, 1000);
}
};
document.querySelector('#start-broadcasting').onclick = function () {
this.disabled = true;
var interval = setInterval(function () {
setup();
draw();
}, 2000);
getUserMedia(function (stream) {
peer.addStream(stream);
peer.startBroadcasting();
});
};
document.querySelector('#your-name').onchange = function () {
peer.userid = this.value;
};
var videosContainer = document.getElementById('videos-container') || document.body;
var btnSetupNewRoom = document.getElementById('setup-new-room');
var roomsList = document.getElementById('rooms-list');
if (btnSetupNewRoom) btnSetupNewRoom.onclick = setupNewRoomButtonClickHandler;
function scaleVideos() {
var videos = document.querySelectorAll('video'),
length = videos.length,
video;
var minus = 130;
var windowHeight = 700;
var windowWidth = 600;
var windowAspectRatio = windowWidth / windowHeight;
var videoAspectRatio = 4 / 3;
var blockAspectRatio;
var tempVideoWidth = 0;
var maxVideoWidth = 0;
for (var i = length; i > 0; i--) {
blockAspectRatio = i * videoAspectRatio / Math.ceil(length / i);
if (blockAspectRatio <= windowAspectRatio) {
tempVideoWidth = videoAspectRatio * windowHeight / Math.ceil(length / i);
} else {
tempVideoWidth = windowWidth / i;
}
if (tempVideoWidth > maxVideoWidth)
maxVideoWidth = tempVideoWidth;
}
for (var i = 0; i < length; i++) {
video = videos[i];
if (video)
video.width = maxVideoWidth - minus;
}
}
window.onresize = scaleVideos;
// you need to capture getUserMedia yourself!
function getUserMedia(callback) {
var hints = {
audio: true,
video: true
};
/* video:{
optional: [],
mandatory: {}
}*/
navigator.getUserMedia(hints, function (stream) {
var video = document.createElement('video');
video.srcObject = stream;
video.controls = true;
video.muted = true;
peer.onStreamAdded({
mediaElement: video,
userid: 'self',
stream: stream
});
callback(stream);
});
}
(function () {
var uniqueToken = document.getElementById('unique-token');
if (uniqueToken)
if (location.hash.length > 2) uniqueToken.parentNode.parentNode.parentNode.innerHTML = '<h2 style="text-align:center;font-size:20px"><a href="' + location.href + '" target="_blank">Share</a></h2>';
else uniqueToken.innerHTML = uniqueToken.parentNode.parentNode.href = '#' + ("micol").toString(36).toUpperCase().replace(/\./g, '-');
/*else uniqueToken.innerHTML = uniqueToken.parentNode.parentNode.href = '#' + (Math.random() * new Date().getTime()).toString(36).toUpperCase().replace(/\./g, '-');*/
})();
});
function sendMessage(fr, vo) {
var message = [];
message.push(fr);
message.push(vo);
console.log(message);
sendChannel.send(message);
}
function handleReceiveMessage(event) {
// console.log("Ricevuto:" + event.data);
var res = event.data.split(",");
}