forked from butorenjin/easyrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasy_app.js
388 lines (364 loc) · 14.6 KB
/
easy_app.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
(function() {
/** @private */
var autoAddCloseButtons = true;
/** By default, the easyApp routine sticks a "close" button on top of each caller
* video object that it manages. Call this function(before calling easyApp) to disable that particular feature.
* @example
* easyrtc.dontAddCloseButtons();
*/
easyrtc.dontAddCloseButtons = function() {
autoAddCloseButtons = false;
};
/**
* This is a helper function for the easyApp method. It manages the assignment of video streams
* to video objects. It assumes
* @param {String} monitorVideoId is the id of the mirror video tag.
* @param {Array} videoIds is an array of ids of the caller video tags.
* @private
*/
function easyAppBody(monitorVideoId, videoIds) {
var numPEOPLE = videoIds.length;
var videoIdsP = videoIds;
var refreshPane = 0;
var onCall = null, onHangup = null;
var videoIdToCallerMap = {};
if (!videoIdsP) {
videoIdsP = [];
}
/**
* Validates that the video ids correspond to dom objects.
* @param {String} monitorVideoId
* @param {Array} videoIds
* @returns {Boolean}
* @private
*/
function validateVideoIds(monitorVideoId, videoIds) {
var i;
// verify that video ids were not typos.
if (monitorVideoId && !document.getElementById(monitorVideoId)) {
self.showError(self.errCodes.DEVELOPER_ERR, "The monitor video id passed to easyApp was bad, saw " + monitorVideoId);
return false;
}
for (i in videoIds) {
if (!videoIds.hasOwnProperty(i)) {
continue;
}
var name = videoIds[i];
if (!document.getElementById(name)) {
self.showError(self.errCodes.DEVELOPER_ERR, "The caller video id '" + name + "' passed to easyApp was bad.");
return false;
}
}
return true;
}
function getCallerOfVideo(videoObject) {
return videoIdToCallerMap[videoObject.id];
}
function setCallerOfVideo(videoObject, callerEasyrtcId) {
videoIdToCallerMap[videoObject.id] = callerEasyrtcId;
}
easyrtc.addEventListener("roomOccupants",
function(eventName, eventData) {
var i;
for (i = 0; i < numPEOPLE; i++) {
var video = getIthVideo(i);
if (!videoIsFree(video)) {
if( !easyrtc.isPeerInAnyRoom(getCallerOfVideo(video))){
if( onHangup ) {
onHangup(getCallerOfVideo(video), i);
}
setCallerOfVideo(video, null);
}
}
}
}
);
function videoIsFree(obj) {
var caller = getCallerOfVideo(obj);
return (caller === "" || caller === null || caller === undefined);
}
if (!validateVideoIds(monitorVideoId, videoIdsP)) {
throw "bad video element id";
}
if (monitorVideoId) {
document.getElementById(monitorVideoId).muted = "muted";
}
/** Sets an event handler that gets called when an incoming MediaStream is assigned
* to a video object. The name is poorly chosen and reflects a simpler era when you could
* only have one media stream per peer connection.
* @param {Function} cb has the signature function(easyrtcid, slot){}
* @example
* easyrtc.setOnCall( function(easyrtcid, slot){
* console.log("call with " + easyrtcid + "established");
* });
*/
easyrtc.setOnCall = function(cb) {
onCall = cb;
};
/** Sets an event handler that gets called when a call is ended.
* it's only purpose (so far) is to support transitions on video elements.
x * this function is only defined after easyrtc.easyApp is called.
* The slot is parameter is the index into the array of video ids.
* Note: if you call easyrtc.getConnectionCount() from inside your callback
* it's count will reflect the number of connections before the hangup started.
* @param {Function} cb has the signature function(easyrtcid, slot){}
* @example
* easyrtc.setOnHangup( function(easyrtcid, slot){
* console.log("call with " + easyrtcid + "ended");
* });
*/
easyrtc.setOnHangup = function(cb) {
onHangup = cb;
};
function getIthVideo(i) {
if (videoIdsP[i]) {
return document.getElementById(videoIdsP[i]);
}
else {
return null;
}
}
easyrtc.getIthCaller = function(i) {
if (i < 0 || i > videoIdsP.length) {
return null;
}
var vid = getIthVideo(i);
return getCallerOfVideo(vid);
};
easyrtc.getSlotOfCaller = function(easyrtcid) {
var i;
for (i = 0; i < numPEOPLE; i++) {
if (easyrtc.getIthCaller(i) === easyrtcid) {
return i;
}
}
return -1; // caller not connected
};
function hideVideo(video) {
easyrtc.setVideoObjectSrc(video, "");
video.style.visibility = "hidden";
}
easyrtc.setOnStreamClosed(function(caller) {
var i;
for (i = 0; i < numPEOPLE; i++) {
var video = getIthVideo(i);
if (getCallerOfVideo(video) === caller) {
hideVideo(video);
setCallerOfVideo(video, "");
if (onHangup) {
onHangup(caller, i);
}
}
}
});
//
// Only accept incoming calls if we have a free video object to display
// them in.
//
easyrtc.setAcceptChecker(function(caller, helper) {
var i;
for (i = 0; i < numPEOPLE; i++) {
var video = getIthVideo(i);
if (videoIsFree(video)) {
helper(true);
return;
}
}
helper(false);
});
easyrtc.setStreamAcceptor(function(caller, stream) {
var i;
if (easyrtc.debugPrinter) {
easyrtc.debugPrinter("stream acceptor called");
}
function showVideo(video, stream) {
easyrtc.setVideoObjectSrc(video, stream);
if (video.style.visibility) {
video.style.visibility = 'visible';
}
}
var video;
if (refreshPane && videoIsFree(refreshPane)) {
showVideo(refreshPane, stream);
if (onCall) {
onCall(caller, refreshPane);
}
refreshPane = null;
return;
}
for (i = 0; i < numPEOPLE; i++) {
video = getIthVideo(i);
if (getCallerOfVideo(video) === caller) {
showVideo(video, stream);
if (onCall) {
onCall(caller, i);
}
return;
}
}
for (i = 0; i < numPEOPLE; i++) {
video = getIthVideo(i);
if (videoIsFree(video)) {
setCallerOfVideo(video, caller);
if (onCall) {
onCall(caller, i);
}
showVideo(video, stream);
return;
}
}
//
// no empty slots, so drop whatever caller we have in the first slot and use that one.
//
video = getIthVideo(0);
if (video) {
easyrtc.hangup(getCallerOfVideo(video));
showVideo(video, stream);
if (onCall) {
onCall(caller, 0);
}
}
setCallerOfVideo(video, caller);
});
(function() {
var addControls, parentDiv, closeButton, i;
if (autoAddCloseButtons) {
addControls = function(video) {
parentDiv = video.parentNode;
setCallerOfVideo(video, "");
closeButton = document.createElement("div");
closeButton.className = "easyrtc_closeButton";
closeButton.onclick = function() {
if (getCallerOfVideo(video)) {
easyrtc.hangup(getCallerOfVideo(video));
hideVideo(video);
setCallerOfVideo(video, "");
}
};
parentDiv.appendChild(closeButton);
};
for (i = 0; i < numPEOPLE; i++) {
addControls(getIthVideo(i));
}
}
})();
var monitorVideo = null;
if (easyrtc.videoEnabled && monitorVideoId !== null) {
monitorVideo = document.getElementById(monitorVideoId);
if (!monitorVideo) {
console.error("Programmer error: no object called " + monitorVideoId);
return;
}
monitorVideo.muted = "muted";
monitorVideo.defaultMuted = true;
}
}
/**
* Provides a layer on top of the easyrtc.initMediaSource and easyrtc.connect, assign the local media stream to
* the video object identified by monitorVideoId, assign remote video streams to
* the video objects identified by videoIds, and then call onReady. One of it's
* side effects is to add hangup buttons to the remote video objects, buttons
* that only appear when you hover over them with the mouse cursor. This method will also add the
* easyrtcMirror class to the monitor video object so that it behaves like a mirror.
* @param {String} applicationName - name of the application.
* @param {String} monitorVideoId - the id of the video object used for monitoring the local stream.
* @param {Array} videoIds - an array of video object ids (strings)
* @param {Function} onReady - a callback function used on success. It is called with the easyrtcId this peer is known to the server as.
* @param {Function} onFailure - a callback function used on failure (failed to get local media or a connection of the signaling server).
* @example
* easyrtc.easyApp('multiChat', 'selfVideo', ['remote1', 'remote2', 'remote3'],
* function(easyrtcId){
* console.log("successfully connected, I am " + easyrtcId);
* },
* function(errorCode, errorText){
* console.log(errorText);
* });
*/
easyrtc.easyApp = function(applicationName, monitorVideoId, videoIds, onReady, onFailure) {
var gotMediaCallback = null,
gotConnectionCallback = null;
easyAppBody(monitorVideoId, videoIds);
easyrtc.setGotMedia = function(gotMediaCB) {
gotMediaCallback = gotMediaCB;
};
//
// try to restablish broken connections that weren't caused by a hangup
//
easyrtc.setPeerClosedListener( function(easyrtcid) {
setTimeout( function() {
if( easyrtc.getSlotOfCaller(easyrtcid) >= 0 && easyrtc.isPeerInAnyRoom(easyrtcid)) {
easyrtc.call(easyrtcid, function(){}, function() {}, function(){});
}
}, 1000);
});
/** Sets an event handler that gets called when a connection to the signaling
* server has or has not been made. Can only be called after calling easyrtc.easyApp.
* @param {Function} gotConnectionCB has the signature (gotConnection, errorText)
* @example
* easyrtc.setGotConnection( function(gotConnection, errorText){
* if( gotConnection ){
* console.log("Successfully connected to signaling server");
* }
* else{
* console.log("Failed to connect to signaling server because: " + errorText);
* }
* });
*/
easyrtc.setGotConnection = function(gotConnectionCB) {
gotConnectionCallback = gotConnectionCB;
};
var nextInitializationStep;
nextInitializationStep = function(/* token */) {
if (gotConnectionCallback) {
gotConnectionCallback(true, "");
}
onReady(easyrtc.myEasyrtcid);
};
function postGetUserMedia() {
if (gotMediaCallback) {
gotMediaCallback(true, null);
}
if (monitorVideoId !== null) {
easyrtc.setVideoObjectSrc(document.getElementById(monitorVideoId), easyrtc.getLocalStream());
}
function connectError(errorCode, errorText) {
if (gotConnectionCallback) {
gotConnectionCallback(false, errorText);
}
else if (onFailure) {
onFailure(easyrtc.errCodes.CONNECT_ERR, errorText);
}
else {
easyrtc.showError(easyrtc.errCodes.CONNECT_ERR, errorText);
}
}
easyrtc.connect(applicationName, nextInitializationStep, connectError);
}
var stream = easyrtc.getLocalStream(null);
if (stream) {
postGetUserMedia();
}
else {
easyrtc.initMediaSource(
postGetUserMedia,
function(errorCode, errorText) {
if (gotMediaCallback) {
gotMediaCallback(false, errorText);
}
else if (onFailure) {
onFailure(easyrtc.errCodes.MEDIA_ERR, errorText);
}
else {
easyrtc.showError(easyrtc.errCodes.MEDIA_ERR, errorText);
}
},
null // default stream
);
}
};
/**
*
* @deprecated now called easyrtc.easyApp.
*/
easyrtc.initManaged = easyrtc.easyApp;
})();