-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.js
159 lines (130 loc) · 4.57 KB
/
controller.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
$(document).ready(function () {
// Set event listeners for user interface widgets
if (!location.hash.replace('#', '').length) {
location.href = location.href.split('#')[0] + '#' + ("micolseilamigliore").toString().replace('.', '')
location.reload();
}
var channel = "channel";
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) {
};
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();
});
};
peer.userid = "CIAMBELLA!";
/* document.querySelector('#your-name').onchange = function () {
peer.userid = this.value;
};
*/
var videosContainer = document.getElementById('videos-container') || document.body;
// var roomsList = document.getElementById('rooms-list');
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);
});
}
$("#sendToPlayer").on("click", function () {
var message = "NUOVO MESSAGGIO DAL CONTROLLER";
sendMessage(message);
})
});
function sendMessage(message) {
console.log(message);
controllerToPlayer.send(message);
}
function handleReceiveMessage(event) {
console.log("Ricevuto:" + event.data);
var res = event.data.split(",");
//animation(res[0], res[1]);
}