Welcome to mirror list, hosted at ThFree Co, Russian Federation.

simplewebrtc.js « simplewebrtc « webrtc « utils « src - github.com/nextcloud/spreed.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d1dbee6e1512de068bc53e91d8bbc9917cb26e61 (plain)
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/* global module */

const WebRTC = require('./webrtc')
const WildEmitter = require('wildemitter')
const webrtcSupport = require('webrtcsupport')
const attachMediaStream = require('attachmediastream')
const mockconsole = require('mockconsole')

/**
 * @param opts
 */
function SimpleWebRTC(opts) {
	const self = this
	const options = opts || {}
	const config = this.config = {
		socketio: {/* 'force new connection':true */},
		connection: null,
		debug: false,
		localVideoEl: '',
		remoteVideosEl: '',
		enableDataChannels: true,
		enableSimulcast: false,
		maxBitrates: {
			high: 900000,
			medium: 300000,
			low: 100000,
		},
		autoRequestMedia: false,
		autoRemoveVideos: true,
		adjustPeerVolume: false,
		peerVolumeWhenSpeaking: 0.25,
		receiveMedia: {
			offerToReceiveAudio: 1,
			offerToReceiveVideo: 1,
		},
		localVideo: {
			autoplay: true,
			mirror: true,
			muted: true,
		},
	}
	let item, connection

	// We also allow a 'logger' option. It can be any object that implements
	// log, warn, and error methods.
	// We log nothing by default, following "the rule of silence":
	// http://www.linfo.org/rule_of_silence.html
	this.logger = (function() {
		// we assume that if you're in debug mode and you didn't
		// pass in a logger, you actually want to log as much as
		// possible.
		if (opts.debug) {
			return opts.logger || console
		} else {
		// or we'll use your logger which should have its own logic
		// for output. Or we'll return the no-op.
			return opts.logger || mockconsole
		}
	}())

	// set our config from options
	for (item in options) {
		if (Object.prototype.hasOwnProperty.call(options, item)) {
			this.config[item] = options[item]
		}
	}

	// Override screensharing support detection to fit the custom
	// "getScreenMedia" module.
	// Note that this is a coarse check; calling "getScreenMedia" may fail even
	// if "supportScreenSharing" is true.
	const screenSharingSupported
			= (window.navigator.mediaDevices && window.navigator.mediaDevices.getDisplayMedia)
			|| (window.navigator.webkitGetUserMedia)
			|| (window.navigator.userAgent.match('Firefox'))
	webrtcSupport.supportScreenSharing = window.location.protocol === 'https:' && screenSharingSupported

	// attach detected support for convenience
	this.capabilities = webrtcSupport

	// call WildEmitter constructor
	WildEmitter.call(this)

	if (this.config.connection === null) {
		throw new Error('no connection object given in the configuration')
	} else {
		connection = this.connection = this.config.connection
	}

	connection.on('message', function(message) {
		const peers = self.webrtc.getPeers(message.from, message.roomType)
		let peer

		if (message.type === 'offer') {
			if (peers.length) {
				peers.forEach(function(p) {
					if (p.sid === message.sid) {
						peer = p
					}
				})
				// if (!peer) peer = peers[0]; // fallback for old protocol versions
			}
			if (!peer) {
				peer = self.webrtc.createPeer({
					id: message.from,
					sid: message.sid,
					type: message.roomType,
					enableDataChannels: self.config.enableDataChannels && message.roomType !== 'screen',
					sharemyscreen: message.roomType === 'screen' && !message.broadcaster,
					broadcaster: message.roomType === 'screen' && !message.broadcaster ? self.connection.getSessionId() : null,
					sendVideoIfAvailable: self.connection.getSendVideoIfAvailable(),
					receiverOnly: self.connection.hasFeature('mcu'),
				})
				self.emit('createdPeer', peer)
			}
			peer.handleMessage(message)
		} else if (message.type === 'control') {
			if (message.payload.action === 'forceMute') {
				if (message.payload.peerId === self.connection.getSessionId()) {
					if (self.webrtc.isAudioEnabled()) {
						self.mute()
						self.emit('forcedMute')
					}
				} else {
					self.emit('mute', { id: message.payload.peerId })
				}
			}
		} else if (message.type === 'nickChanged') {
			// "nickChanged" can be received from a participant without a Peer
			// object if that participant is not sending audio nor video.
			self.emit('nick', { id: message.from, name: message.payload.name })
		} else if (message.type === 'raiseHand') {
			// "raisedHand" can be received from a participant without a Peer
			// object if that participant is not sending audio nor video.
			self.emit('raisedHand', { id: message.from, raised: message.payload })
		} else if (peers.length) {
			peers.forEach(function(peer) {
				if (message.sid && !self.connection.hasFeature('mcu')) {
					if (peer.sid === message.sid) {
						peer.handleMessage(message)
					}
				} else {
					peer.handleMessage(message)
				}
			})
		}
	})

	connection.on('remove', function(room) {
		if (room.id !== self.connection.getSessionId()) {
			self.webrtc.removePeers(room.id, room.type)
		}
	})

	// instantiate our main WebRTC helper
	// using same logger from logic here
	opts.logger = this.logger
	opts.debug = false
	this.webrtc = new WebRTC(opts);

	// attach a few methods from underlying lib to simple.
	['mute', 'unmute', 'pauseVideo', 'resumeVideo', 'pause', 'resume', 'sendToAll', 'sendDirectlyToAll', 'getPeers', 'createPeer', 'removePeers'].forEach(function(method) {
		self[method] = self.webrtc[method].bind(self.webrtc)
	})

	// proxy events from WebRTC
	this.webrtc.on('*', function() {
		self.emit.apply(self, arguments)
	})

	// log all events in debug mode
	if (config.debug) {
		this.on('*', this.logger.log.bind(this.logger, 'SimpleWebRTC event:'))
	}

	this.webrtc.on('message', function(payload) {
		self.connection.emit('message', payload)
	})

	this.webrtc.on('peerStreamAdded', this.handlePeerStreamAdded.bind(this))
	this.webrtc.on('peerStreamRemoved', this.handlePeerStreamRemoved.bind(this))

	// echo cancellation attempts
	if (this.config.adjustPeerVolume) {
		this.webrtc.on('speaking', this.setVolumeForAll.bind(this, this.config.peerVolumeWhenSpeaking))
		this.webrtc.on('stoppedSpeaking', this.setVolumeForAll.bind(this, 1))
	}

	connection.on('stunservers', function(args) {
		// resets/overrides the config
		self.webrtc.config.peerConnectionConfig.iceServers = args
		self.emit('stunservers', args)
	})
	connection.on('turnservers', function(args) {
		// appends to the config
		self.webrtc.config.peerConnectionConfig.iceServers = self.webrtc.config.peerConnectionConfig.iceServers.concat(args)
		self.emit('turnservers', args)
	})

	this.webrtc.on('iceFailed', function(/* peer */) {
		// local ice failure
	})
	this.webrtc.on('connectivityError', function(/* peer */) {
		// remote ice failure
	})

	// sending mute/unmute to all peers
	this.webrtc.on('audioOn', function() {
		self.webrtc.sendToAll('unmute', { name: 'audio' })
	})
	this.webrtc.on('audioOff', function() {
		self.webrtc.sendToAll('mute', { name: 'audio' })
	})
	this.webrtc.on('videoOn', function() {
		self.webrtc.sendToAll('unmute', { name: 'video' })
	})
	this.webrtc.on('videoOff', function() {
		self.webrtc.sendToAll('mute', { name: 'video' })
	})

	// screensharing events
	this.webrtc.on('localScreen', function(stream) {
		const el = document.createElement('video')
		const container = self.getRemoteVideoContainer()

		el.oncontextmenu = function() { return false }
		el.id = 'localScreen'
		attachMediaStream(stream, el)
		if (container) {
			container.appendChild(el)
		}

		self.emit('localScreenAdded', el)
		self.connection.emit('shareScreen')

		// NOTE: we don't create screen peers for existing video peers here,
		// this is done by the application code in "webrtc.js".
	})
	this.webrtc.on('localScreenStopped', function(/* stream */) {
		self.stopScreenShare()
		/*
		self.connection.emit('unshareScreen');
		self.webrtc.peers.forEach(function (peer) {
			if (peer.sharemyscreen) {
				peer.end();
			}
		});
		*/
	})
}

SimpleWebRTC.prototype = Object.create(WildEmitter.prototype, {
	constructor: {
		value: SimpleWebRTC,
	},
})

SimpleWebRTC.prototype.leaveCall = function() {
	if (this.roomName) {
		while (this.webrtc.peers.length) {
			this.webrtc.peers[0].end()
		}
		if (this.getLocalScreen()) {
			this.stopScreenShare()
		}
		this.emit('leftRoom', this.roomName)
		this.stopLocalVideo()
		this.roomName = undefined
	}
}

SimpleWebRTC.prototype.disconnect = function() {
	this.emit('disconnected')
}

SimpleWebRTC.prototype.handlePeerStreamAdded = function(peer) {
	const container = this.getRemoteVideoContainer()
	if (container) {
		// If there is a video track Chromium does not play audio in a video element
		// until the video track starts to play; an audio element is thus needed to
		// play audio when the remote peer starts with the camera available but
		// disabled.
		const audio = attachMediaStream(peer.stream, null, { audio: true })
		const video = attachMediaStream(peer.stream)

		video.muted = true

		// At least Firefox, Opera and Edge move the video to a wrong position
		// instead of keeping it unchanged when "transform: scaleX(1)" is used
		// ("transform: scaleX(-1)" is fine); as it should have no effect the
		// transform is removed.
		if (video.style.transform === 'scaleX(1)') {
			video.style.transform = ''
		}

		// store video element as part of peer for easy removal
		peer.audioEl = audio
		peer.videoEl = video
		audio.id = this.getDomId(peer) + '-audio'
		video.id = this.getDomId(peer)

		container.appendChild(audio)
		container.appendChild(video)

		this.emit('videoAdded', video, audio, peer)
	}
}

SimpleWebRTC.prototype.handlePeerStreamRemoved = function(peer) {
	const container = this.getRemoteVideoContainer()
	const audioEl = peer.audioEl
	const videoEl = peer.videoEl
	if (this.config.autoRemoveVideos && container && audioEl) {
		container.removeChild(audioEl)
	}
	if (this.config.autoRemoveVideos && container && videoEl) {
		container.removeChild(videoEl)
	}
	if (videoEl) {
		this.emit('videoRemoved', videoEl, peer)
	}
}

SimpleWebRTC.prototype.getDomId = function(peer) {
	return [peer.id, peer.type, peer.broadcaster ? 'broadcasting' : 'incoming'].join('_')
}

// set volume on video tag for all peers takse a value between 0 and 1
SimpleWebRTC.prototype.setVolumeForAll = function(volume) {
	this.webrtc.peers.forEach(function(peer) {
		if (peer.audioEl) {
			peer.audioEl.volume = volume
		}
	})
}

SimpleWebRTC.prototype.joinCall = function(name, mediaConstraints) {
	if (this.config.autoRequestMedia) {
		this.startLocalVideo(mediaConstraints)
	}
	this.roomName = name
	this.emit('joinedRoom', name)
}

SimpleWebRTC.prototype.getEl = function(idOrEl) {
	if (typeof idOrEl === 'string') {
		return document.getElementById(idOrEl)
	} else {
		return idOrEl
	}
}

SimpleWebRTC.prototype.startLocalVideo = function(mediaConstraints) {
	const self = this
	this.webrtc.start(mediaConstraints, function(err, stream, actualConstraints) {
		if (err) {
			self.emit('localMediaError', err)
		} else {
			self.emit('localMediaStarted', actualConstraints)

			const localVideoContainer = self.getLocalVideoContainer()
			if (localVideoContainer) {
				attachMediaStream(stream, localVideoContainer, self.config.localVideo)
			}
		}
	})
}

SimpleWebRTC.prototype.stopLocalVideo = function() {
	this.webrtc.stop()
}

// this accepts either element ID or element
// and either the video tag itself or a container
// that will be used to put the video tag into.
SimpleWebRTC.prototype.getLocalVideoContainer = function() {
	const el = this.getEl(this.config.localVideoEl)
	if (el && el.tagName === 'VIDEO') {
		el.oncontextmenu = function() { return false }
		return el
	} else if (el) {
		const video = document.createElement('video')
		video.oncontextmenu = function() { return false }
		el.appendChild(video)
		return video
	}
}

SimpleWebRTC.prototype.getRemoteVideoContainer = function() {
	return this.getEl(this.config.remoteVideosEl)
}

SimpleWebRTC.prototype.shareScreen = function(mode, cb) {
	this.webrtc.startScreenShare(mode, cb)
}

SimpleWebRTC.prototype.getLocalScreen = function() {
	return this.webrtc.localScreen
}

SimpleWebRTC.prototype.stopScreenShare = function() {
	this.connection.emit('unshareScreen')
	const videoEl = document.getElementById('localScreen')
	const container = this.getRemoteVideoContainer()

	if (this.config.autoRemoveVideos && container && videoEl) {
		container.removeChild(videoEl)
	}

	// a hack to emit the event the removes the video
	// element that we want
	if (videoEl) {
		this.emit('videoRemoved', videoEl)
	}
	if (this.getLocalScreen()) {
		this.webrtc.stopScreenShare()
	}
	// Notify peers were sending to.
	this.webrtc.peers.forEach(function(peer) {
		if (peer.type === 'screen' && peer.sharemyscreen) {
			peer.send('unshareScreen')
		}
		if (peer.broadcaster) {
			peer.end()
		}
	})
}

SimpleWebRTC.prototype.createRoom = function(name, cb) {
	this.roomName = name
	if (arguments.length === 2) {
		this.connection.emit('create', name, cb)
	} else {
		this.connection.emit('create', name)
	}
}

module.exports = SimpleWebRTC