-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.html
225 lines (210 loc) · 9.19 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
<!DOCTYPE html>
<html>
<head>
<title>SimpleWebRTC Demo</title>
</head>
<body>
<h1 id="title">Start a room</h1>
<style>
.videoContainer {
position: relative;
width: 200px;
height: 150px;
}
.videoContainer video {
position: absolute;
width: 100%;
height: 100%;
}
.volume_bar {
position: absolute;
width: 5px;
height: 0px;
right: 0px;
bottom: 0px;
background-color: #12acef;
}
/*body{
display: none;
}*/
</style>
<button id="screenShareButton"></button>
<p id="subTitle"></p>
<form id="createRoom">
<input id="sessionInput"/>
<button type="submit">Create it!</button>
</form>
<div class="videoContainer">
<video id="localVideo" style="height: 150px;" oncontextmenu="return false;"></video>
<div id="localVolume" class="volume_bar"></div>
</div>
<div id="remotes"></div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="simplewebrtc.bundle.js"></script>
<script>
// grab the room from the URL
var room = location.search && location.search.split('?')[1];
// This object will take in an array of XirSys STUN / TURN servers
// and override the original peerConnectionConfig object
var peerConnectionConfig;
var x_data = {
ident: "Ident"
, secret: "Secret"
, domain: "Domain"
, application: "Application"
, secure: 1
};
//if no room defined default into 'default' room
x_data.room = room != null && room.length > 0 ? room : 'default';
$.ajax({
url: "https://service.xirsys.com/ice",
data: x_data,
success: function(data, status){
// data.d is where the iceServers object lives
peerConnectionConfig = data.d;
if(peerConnectionConfig !=null){
start(peerConnectionConfig);
}else{
console.log('/ice error - ', data);
}
}
});
//success callback handler which should provide peerConnectionConfig data
function start(peerConnectionConfig){
// Create our WebRTC connection
var webrtc = new SimpleWebRTC({
// The DOM element that will hold "our" video
localVideoEl: 'localVideo',
// The DOM element that will hold remote videos
remoteVideosEl: '',
// Immediately ask for camera access
autoRequestMedia: true,
debug: false,
detectSpeakingEvents: true,
autoAdjustMic: false,
// Add the new peerConnectionConfig object
peerConnectionConfig: peerConnectionConfig
});
// when it's ready, join if we got a room from the URL
webrtc.on('readyToCall', function () {
// you can name it anything
if (room) webrtc.joinRoom(room);
});
function showVolume(el, volume) {
if (!el) return;
if (volume < -45) { // vary between -45 and -20
el.style.height = '0px';
} else if (volume > -20) {
el.style.height = '100%';
} else {
el.style.height = '' + Math.floor((volume + 100) * 100 / 25 - 220) + '%';
}
}
webrtc.on('channelMessage', function (peer, label, data) {
if (data.type == 'volume') {
showVolume(document.getElementById('volume_' + peer.id), data.volume);
}
});
webrtc.on('videoAdded', function (video, peer) {
console.log('video added', peer);
var remotes = document.getElementById('remotes');
if (remotes) {
var d = document.createElement('div');
d.className = 'videoContainer';
d.id = 'container_' + webrtc.getDomId(peer);
d.appendChild(video);
var vol = document.createElement('div');
vol.id = 'volume_' + peer.id;
vol.className = 'volume_bar';
video.onclick = function () {
video.style.width = video.videoWidth + 'px';
video.style.height = video.videoHeight + 'px';
};
d.appendChild(vol);
remotes.appendChild(d);
}
});
webrtc.on('videoRemoved', function (video, peer) {
console.log('video removed ', peer);
var remotes = document.getElementById('remotes');
var el = document.getElementById('container_' + webrtc.getDomId(peer));
if (remotes && el) {
remotes.removeChild(el);
}
});
webrtc.on('volumeChange', function (volume, treshold) {
//console.log('own volume', volume);
showVolume(document.getElementById('localVolume'), volume);
});
/*webrtc.createRoom = function(room, cb){
console.log('createRoom', room);
}*/
// Since we use this twice we put it here
function setRoom(name) {
$('form').remove();
$('h1').text(name);
$('#subTitle').text('Link to join: ' + location.href);
$('body').addClass('active');
}
if (room) {
setRoom(room);
} else {
$('form').submit(function () {
var val = $('#sessionInput').val().toLowerCase().replace(/\s/g, '-').replace(/[^A-Za-z0-9_\-]/g, '');
webrtc.createRoom(val, function (err, name) {
console.log(' create room cb', arguments);
if (!err) {
//
var d = x_data;
d.room = name;
$.ajax({
url: "https://service.xirsys.com/room",
type: "POST",
data: d,
success: function(data, status){
if(data.d != null){
var newUrl = location.pathname + '?' + name;
history.replaceState({foo: 'bar'}, null, newUrl);
setRoom(name);
}else{
console.log('/room err - ', data);
$('#title').text();
}
}
});
}else{
console.log(err);
}
});
return false;
});
}
webrtc.on('localScreenStopped', function () {
setButton(true);
});
var button = $('#screenShareButton'),
setButton = function (bool) {
button.text(bool ? 'share screen' : 'stop sharing');
};
if (!webrtc.capabilities.screenSharing) {
button.attr('disabled', 'disabled');
}
setButton(true);
button.click(function () {
if (webrtc.getLocalScreen()) {
webrtc.stopScreenShare();
setButton(true);
} else {
webrtc.shareScreen(function (err) {
if (err) {
setButton(true);
} else {
setButton(false);
}
});
}
});
}
</script>
</body>
</html>