forked from strukturag/spreed-webrtc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscreenshare.js
366 lines (303 loc) · 10.7 KB
/
screenshare.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
/*
* Spreed WebRTC.
* Copyright (C) 2013-2015 struktur AG
*
* This file is part of Spreed WebRTC.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
"use strict";
define(['jquery', 'underscore', 'text!partials/screenshare.html', 'text!partials/screensharepeer.html', 'bigscreen', 'webrtc.adapter'], function($, _, template, templatePeer, BigScreen) {
return ["$window", "mediaStream", "$compile", "safeApply", "videoWaiter", "$timeout", "alertify", "translation", "screensharing", function($window, mediaStream, $compile, safeApply, videoWaiter, $timeout, alertify, translation, screensharing) {
var peerTemplate = $compile(templatePeer);
var controller = ['$scope', '$element', '$attrs', function($scope, $element, $attrs) {
var screenCount = 0;
var screens = {};
var pane = $element.find(".screensharepane");
$scope.layout.screenshare = false;
$scope.usermedia = null;
$scope.connected = false;
$scope.hideOptionsBar = true;
$scope.fitScreen = true;
var handleRequest = function(event, currenttoken, to, data, type, to2, from, peerscreenshare) {
console.log("Screen share answer message", currenttoken, data, type);
if (typeof data === "string") {
if (data.charCodeAt(0) === 2) {
// Ignore whatever shit is sent us by Firefox.
return;
}
// Control data request.
var msg;
try {
msg = JSON.parse(data);
} catch (e) {
// Invalid JSON.
console.warn("Invalid JSON received from screen share channel.", data);
peerscreenshare.close();
return;
}
switch (msg.m) {
case "bye":
// Close this screen share.
peerscreenshare.close();
break;
default:
console.log("Unknown screen share control request", msg.m, msg);
break;
}
} else {
console.warn("Unkown data type received -> ignored", typeof data, [data]);
peerscreenshare.close();
}
};
mediaStream.api.e.on("received.screenshare", function(event, id, from, data, p2p) {
if (!p2p) {
console.warn("Received screensharing info without p2p. This should not happen!");
return;
}
var token = data.id;
// Bind token.
var handler = mediaStream.tokens.on(token, handleRequest, "screenshare");
// Subscribe to peers screensharing.
mediaStream.webrtc.doSubscribeScreenshare(from, token, {
created: function(peerscreenshare) {
peerscreenshare.e.on("remoteStreamAdded", function(event, stream) {
if (stream) {
$scope.$apply(function(scope) {
scope.addRemoteStream(stream, peerscreenshare);
});
}
});
peerscreenshare.e.on("remoteStreamRemoved", function(event, stream) {
safeApply($scope, function(scope) {
scope.removeRemoteStream(stream, peerscreenshare);
});
});
},
connected: function(peerscreenshare) {
console.log("PeerScreenshare connected", peerscreenshare);
$scope.$apply(function(scope) {
scope.connected = true;
});
},
closed: function(peerscreenshare) {
console.log("PeerScreenshare closed", peerscreenshare);
safeApply($scope, function(scope) {
scope.removeRemoteStream(null, peerscreenshare);
scope.connected = false;
});
mediaStream.tokens.off(token, handler);
handler = null;
}
});
});
$scope.addRemoteStream = function(stream, currentscreenshare) {
$scope.$emit("mainview", "screenshare", true);
var subscope = $scope.$new(true);
var peerid = subscope.peerid = currentscreenshare.id;
peerTemplate(subscope, function(clonedElement, scope) {
pane.append(clonedElement);
scope.element = clonedElement;
var video = clonedElement.find("video")[0];
$window.attachMediaStream(video, stream);
videoWaiter.wait(video, stream, function() {
console.log("Screensharing size: ", video.videoWidth, video.videoHeight);
}, function() {
console.warn("We did not receive screen sharing video data", currentscreenshare, stream, video);
});
screens[peerid] = scope;
});
};
$scope.removeRemoteStream = function(stream, currentscreenshare) {
var subscope = screens[currentscreenshare.id];
if (subscope) {
delete screens[currentscreenshare.id];
if (subscope.element) {
subscope.element.remove();
}
subscope.$destroy();
}
if (_.isEmpty(screens)) {
$scope.$emit("mainview", "screenshare", false);
}
};
$scope.doScreenshare = function() {
if ($scope.layout.screenshare) {
$scope.stopScreenshare();
}
$scope.layout.screenshare = true;
screensharing.globalNotify().screensharingStart();
screensharing.getScreen().then(function(options) {
if (options) {
$scope.startScreenshare(options);
} else {
// No options received - assume cancel.
$scope.stopScreenshare();
}
}, function(err) {
console.log("Screen sharing request returned error", err);
alertify.dialog.alert(translation._("Failed to start screen sharing (%s).", err));
$scope.stopScreenshare();
});
};
$scope.startScreenshare = function(options) {
// Create userMedia with screen share type.
var usermedia = mediaStream.webrtc.doScreenshare(options);
var handler;
var peers = {};
var screenshares = [];
var connector = function(token, peercall) {
if (peers.hasOwnProperty(peercall.id)) {
// Already got a connection.
return;
}
peers[peercall.id] = true;
peercall.e.one("closed", function(event, currentcall) {
delete peers[currentcall.id];
console.log("Removed closed call from screen sharing.", currentcall.id);
});
mediaStream.api.apply("sendScreenshare", {
send: function(type, data) {
//console.log("sent screenshare", data, peercall);
return peercall.peerconnection.send(data);
}
})(peercall.from, token);
};
usermedia.e.one("mediasuccess", function(event, usermedia) {
$scope.$apply(function(scope) {
scope.usermedia = usermedia;
// Create token to register with us and send token out to all peers.
// Peers when connect to us with the token and we answer.
var token = "screenshare_" + scope.id + "_" + (screenCount++);
// Updater function to bring in new calls.
var updater = function(event, state, currentcall) {
switch (state) {
case "completed":
case "connected":
connector(token, currentcall);
break;
}
};
// Create callbacks are called for each incoming connections.
handler = mediaStream.tokens.create(token, function(event, currenttoken, to, data, type, to2, from, peerscreenshare) {
console.log("Screen share create", currenttoken, data, type, peerscreenshare);
screenshares.push(peerscreenshare);
usermedia.addToPeerConnection(peerscreenshare.peerconnection);
}, "screenshare");
// Connect all current calls.
mediaStream.webrtc.callForEachCall(function(peercall) {
connector(token, peercall);
});
// Catch later calls too.
mediaStream.webrtc.e.on("statechange", updater);
// Cleanup on stop of media.
usermedia.e.one("stopped", function() {
mediaStream.tokens.off(token, handler);
mediaStream.webrtc.e.off("statechange", updater);
handler = null;
// Send by to all connected peers.
_.each(screenshares, function(peerscreenshare) {
peerscreenshare.send({
m: "bye"
});
$timeout(function() {
peerscreenshare.close();
}, 0);
});
peers = {};
screenshares = [];
// Make sure to clean up.
safeApply(scope, function(scope) {
scope.stopScreenshare();
});
});
});
});
usermedia.e.one("mediaerror", function(event, usermedia, error) {
$scope.$apply(function(scope) {
scope.stopScreenshare();
});
if (error && error.name) {
switch (error.name) {
case "PermissionDeniedError":
case "InvalidStateError":
if ($window.webrtcDetectedVersion >= 32 &&
$window.webrtcDetectedVersion < 37) {
alertify.dialog.alert(translation._("Permission to start screen sharing was denied. Make sure to have enabled screen sharing access for your browser. Copy chrome://flags/#enable-usermedia-screen-capture and open it with your browser and enable the flag on top. Then restart the browser and you are ready to go."));
} else {
alertify.dialog.alert(translation._("Permission to start screen sharing was denied."));
}
break;
default:
alertify.dialog.alert(translation._("Failed to start screen sharing (%s).", error.name));
break
}
}
});
};
$scope.stopScreenshare = function() {
if ($scope.usermedia) {
$scope.usermedia.stop()
$scope.usermedia = null;
console.log("Screen share stopped.");
}
if ($scope.layout.screenshare) {
screensharing.cancelGetScreen();
$scope.layout.screenshare = false;
}
screensharing.globalNotify().screensharingStop();
};
$scope.toggleFullscreen = function(elem) {
if (BigScreen.enabled) {
if (elem) {
BigScreen.toggle(elem);
} else {
BigScreen.toggle(pane[0]);
}
}
};
mediaStream.webrtc.e.on("done", function() {
$scope.$apply($scope.stopScreenshare);
});
$scope.$watch("layout.screenshare", function(newval, oldval) {
if (newval && !oldval) {
$scope.doScreenshare();
} else if (!newval && oldval) {
$scope.stopScreenshare();
}
});
$scope.$watch("layout.main", function(newval, oldval) {
if (newval && newval !== "screenshare") {
$scope.stopScreenshare();
}
});
}];
var compile = function(tElement, tAttr) {
return function(scope, iElement, iAttrs, controller) {
$(iElement).on("dblclick", ".remotescreen", _.debounce(function(event) {
scope.toggleFullscreen(event.delegateTarget);
}, 100, true));
}
};
return {
restrict: 'E',
replace: true,
scope: true,
template: template,
controller: controller,
compile: compile
}
}];
});