forked from bahmutov/ng-simple-webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathng-simple-webrtc.js
383 lines (341 loc) · 13 KB
/
ng-simple-webrtc.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
(function (angular) {
'use strict';
if (!angular) {
throw new Error('Missing Angular library');
}
angular.module('SimpleWebRTC', [])
.run(function () {
if (typeof SimpleWebRTC === 'undefined') {
throw new Error('Cannot find SimpleWebRTC code');
}
})
.directive('watchRoom', function () {
return {
template: '<div ng-show="joinedRoom">' +
'<div id="remotes"></div>' +
'</div>',
scope: {
roomName: '=',
joinedRoom: '=',
videoList: '=',
maxNumPeers: '=',
nick: '='
},
link: function (scope, element, attr) {
scope.muted = attr.muted === 'true';
},
controller: function ($scope, $rootScope) {
var webrtc, watchingVideo;
$scope.maxNumPeers = typeof $scope.maxNumPeers === 'number' ?
$scope.maxNumPeers : 10;
function formRTCOptions() {
var webrtcOptions = {
autoRequestMedia: false,
debug: false,
nick: $scope.nick,
receiveMedia: { // FIXME: remove old chrome <= 37 constraints format
mandatory: {
OfferToReceiveAudio: false,
OfferToReceiveVideo: true
}
}
};
grabExtraWebRTCOptions(webrtcOptions);
return webrtcOptions;
}
function postCreationRTCOptions(webrtc) {
}
function rtcEventResponses(webrtc) {
webrtc.on('readyToCall', function () {
console.log('webrtc ready to call');
});
webrtc.on('joinedRoom', function (name) {
console.log('joined room "%s"', name);
var peers = webrtc.getPeers();
if (peers && Array.isArray(peers) &&
peers.length > $scope.maxNumPeers) {
console.error('Too many people in the room, leaving');
webrtc.leaveRoom();
$scope.$emit('room-full');
return;
}
$scope.$emit('joinedRoom', name);
webrtc.on('channelMessage', function (peer, message) {
console.log('received channel message "%s" from peer "%s"',
message, peer.nick || peer.id);
$scope.$emit('channelMessage', peer, JSON.parse(message));
$scope.$apply();
});
});
$scope.$on('messageAll', function (event, message) {
if (message && webrtc) {
webrtc.sendDirectlyToAll(JSON.stringify(message));
}
});
webrtc.on('videoRemoved', function (video, peer) {
if (Array.isArray($scope.videoList)) {
for (var i = 0; i < $scope.videoList.length; i++) {
if (video.id === $scope.videoList[i].id) {
$scope.videoList.splice(i, 1);
$scope.$apply();
return;
}
}
}
});
webrtc.on('videoAdded', function (video, peer) {
console.log('video added from peer nickname', peer.nick);
if ($scope.muted) {
video.setAttribute('muted', true);
video.setAttribute('hidden', true);
}
// videoList is an array, it means the user wants to append the video in it
// so, skip manual addition to dom
if (Array.isArray($scope.videoList)) {
video.isRemote = true;
$scope.videoList.push(video);
$scope.joinedRoom = true;
$scope.$apply();
return;
}
var remotes = document.getElementById('remotes');
remotes.appendChild(video);
watchingVideo = video;
$scope.$emit('videoAdded', video);
$scope.joinedRoom = true;
$scope.$apply();
});
webrtc.on('iceFailed', function (peer) {
console.error('ice failed', peer);
$scope.$emit('iceFailed', peer);
});
webrtc.on('connectivityError', function (peer) {
console.error('connectivity error', peer);
$scope.$emit('connectivityError', peer);
});
$scope.$on('leaveRoom', function leaveRoom() {
console.log('leaving room', $scope.roomName);
if (!$scope.roomName) {
return;
}
webrtc.leaveRoom($scope.roomName);
if (watchingVideo) {
var remotes = document.getElementById('remotes');
remotes.removeChild(watchingVideo);
}
$scope.joinedRoom = false;
});
}
// emit this event, and we join the room.
$scope.$on('joinRoom', function joinRoom() {
console.log('joining room', $scope.roomName);
if (!$scope.roomName) {
return;
}
var webrtcOptions = formRTCOptions();
webrtc = new SimpleWebRTC(webrtcOptions);
postCreationRTCOptions(webrtc);
$rootScope.webrtc = webrtc;
$scope.$emit('haveWebRTC');
rtcEventResponses(webrtc);
// Post WebRTC Options
// And, a joinRoom command.
webrtc.mute();
webrtc.joinRoom($scope.roomName);
});
}
}
})
// ====================================================================================================================================
.directive('broadcaster', function () {
return {
template: '<h2>My video</h2>' +
'<div class="local-video-wrapper" ng-show="hasStream">' +
'<video id="localVideo" ng-attr-muted="{{ muted }}"></video>' +
'</div>',
scope: {
hasStream: '=',
roomName: '=',
isBroadcasting: '=',
sourceId: '=',
minWidth: '=',
minHeight: '=',
videoList: '=',
nick: '=',
doNotHandleLocalStream: '='
},
link: function (scope, element, attr) {
scope.mirror = attr.mirror === 'true';
scope.muted = attr.muted === 'true';
},
controller: function ($scope, $rootScope) {
var webrtc;
function formRTCOptions() {
var webrtcOptions = {
// the id/element dom element that will hold "our" video
localVideoEl: 'localVideo',
autoRequestMedia: false,
debug: false,
nick: $scope.nick,
media: {
audio: false,
video: true
},
receiveMedia: { // FIXME: remove old chrome <= 37 constraints format
mandatory: {
OfferToReceiveAudio: false,
OfferToReceiveVideo: false
}
}
};
grabExtraWebRTCOptions(webrtcOptions);
if ($scope.muted) {
webrtcOptions.media = {
audio: false,
video: true
};
}
// source id returned from navigator.getUserMedia (optional)
var sourceId = $scope.sourceId;
if (sourceId) {
console.log('requesting video camera with id ' + sourceId);
webrtcOptions.media.video = {
optional: [{ sourceId: sourceId }]
};
}
if ($scope.minWidth) {
var minWidth = parseInt($scope.minWidth);
if (typeof webrtcOptions.media.video !== 'object') {
webrtcOptions.media.video = {};
}
webrtcOptions.media.video.mandatory = {
minWidth: minWidth,
maxWidth: minWidth
};
}
return webrtcOptions;
}
// options to make after the webrtc object is created.
function postCreationRTCOptions(webrtc)
{
webrtc.config.localVideo.mirror = Boolean($scope.mirror);
if ($scope.muted) {
webrtc.mute();
}
}
// event Responses to make after the webrtc object is created.
function rtcEventResponses(webrtc)
{
if( ! $scope.doNotHandleLocalStream) {
webrtc.off('localStream');
webrtc.on('localStream', function (stream) {
console.log('got video stream', stream, 'from the local camera');
var videoTracks = stream.getVideoTracks();
console.log('how many video tracks?', videoTracks.length);
if (videoTracks.length) {
var first = videoTracks[0];
console.log('video track label', first.label);
}
// videoList is an array, it means the user wants to append the video in it
if (Array.isArray($scope.videoList)) {
var video = document.createElement("video");
video.id = stream.id;
// TODO use $window service
video.src = window.URL.createObjectURL(stream);
video.play();
video.isRemote = false;
$scope.videoList.push(video);
}
$scope.hasStream = true;
$scope.$apply();
});
}
webrtc.on('localMediaError', function (err) {
console.error('local camera error', err,
'media constraints', webrtc.config.media);
$scope.$emit('localMediaError', {
error: err,
config: webrtc.config.media
});
});
}
$scope.$on('prepare', function prepareToBroadcast() {
if (webrtc) {
console.log('already has prepared');
return;
}
var webrtcOptions = formRTCOptions();
webrtc = new SimpleWebRTC(webrtcOptions);
postCreationRTCOptions(webrtc);
$rootScope.webrtc = webrtc;
$scope.$emit('haveWebRTC');
rtcEventResponses(webrtc);
if (!$scope.doNotHandleLocalStream) webrtc.startLocalVideo(); //otherwise webrtc.startLocalVideo();
});
function isTakenError(err) {
return err === 'taken';
}
function onStartedRoom(name) {
console.log('joining as broadcaster to room ', name);
$scope.isBroadcasting = true;
$scope.$emit('created-room', name);
$scope.$apply();
}
function joinRoomAsBroadcaster() {
console.log('Trying to join existing room "%s" as broadcaster', $scope.roomName);
webrtc.joinRoom($scope.roomName);
$scope.isBroadcasting = true;
$scope.$emit('created-room', name);
}
//
$scope.$on('start', function start() {
console.log('starting room', $scope.roomName);
if (!$scope.roomName) {
return;
}
webrtc.createRoom($scope.roomName, function (err) {
if (err) {
if (isTakenError(err)) {
console.log('Room "%s" is taken', $scope.roomName);
joinRoomAsBroadcaster();
} else {
$scope.$emit('createRoomError', err);
throw new Error(err);
}
} else {
onStartedRoom($scope.roomName);
}
});
// a peer can send message to everyone in the room using
// webrtc.sendDirectlyToAll('hi there') or webrtc.sendToAll('hi there')
webrtc.on('channelMessage', function (peer, message) {
console.log('received channel message "%s" from peer "%s"',
message, peer.nick || peer.id);
var value = JSON.parse(message);
$scope.$emit('channelMessage', peer, value);
$scope.$apply();
});
});
$scope.$on('messageAll', function (event, message) {
if (message && webrtc) {
var str = JSON.stringify(message);
webrtc.sendDirectlyToAll(str);
}
});
}
};
});
function grabExtraWebRTCOptions(webrtcOptions) {
var ngSimpleWebRTC = window.ngSimpleWebRTC || {};
// This is the turn/stun servers.
if (typeof ngSimpleWebRTC.peerConnectionConfig !== 'undefined') {
webrtcOptions.peerConnectionConfig = ngSimpleWebRTC.peerConnectionConfig;
}
if (typeof ngSimpleWebRTC.debug !== 'undefined') {
webrtcOptions.debug = ngSimpleWebRTC.debug;
}
if (typeof ngSimpleWebRTC.socketio === 'object') {
webrtcOptions.socketio = ngSimpleWebRTC.socketio;
}
}
}(window.angular));