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

localmedia.js « simplewebrtc « webrtc « utils « src - github.com/nextcloud/spreed.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 23bf1ba7d8bb8fd23dd428300ca8a9b44af7b36d (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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/* global module */

const util = require('util')
const getScreenMedia = require('./getscreenmedia')
const WildEmitter = require('wildemitter')
const mockconsole = require('mockconsole')
// Only mediaDevicesManager is used, but it can not be assigned here due to not
// being initialized yet.
const webrtcIndex = require('../index.js')
const MediaDevicesSource = require('../../media/pipeline/MediaDevicesSource.js').default
const SpeakingMonitor = require('../../media/pipeline/SpeakingMonitor.js').default
const TrackConstrainer = require('../../media/pipeline/TrackConstrainer.js').default
const TrackEnabler = require('../../media/pipeline/TrackEnabler.js').default
const TrackToStream = require('../../media/pipeline/TrackToStream.js').default
const VirtualBackground = require('../../media/pipeline/VirtualBackground.js').default

/**
 * @param {object} opts the options object.
 */
function LocalMedia(opts) {
	WildEmitter.call(this)

	const config = this.config = {
		audioFallback: false,
		logger: mockconsole,
	}

	let item
	for (item in opts) {
		if (Object.prototype.hasOwnProperty.call(opts, item)) {
			this.config[item] = opts[item]
		}
	}

	this.logger = config.logger
	this._log = this.logger.log.bind(this.logger, 'LocalMedia:')
	this._logerror = this.logger.error.bind(this.logger, 'LocalMedia:')

	this._localMediaActive = false

	this.localStreams = []
	this.sentStreams = []
	this.localScreens = []

	if (!webrtcIndex.mediaDevicesManager.isSupported()) {
		this._logerror('Your browser does not support local media capture.')
	}

	this._mediaDevicesSource = new MediaDevicesSource()

	this._audioTrackEnabler = new TrackEnabler()
	this._videoTrackEnabler = new TrackEnabler()

	this._videoTrackConstrainer = new TrackConstrainer()

	this._virtualBackground = new VirtualBackground()
	this._virtualBackground.on('loadFailed', () => {
		this.emit('virtualBackgroundLoadFailed')
	})

	this._speakingMonitor = new SpeakingMonitor()
	this._speakingMonitor.on('speaking', () => {
		this.emit('speaking')
	})
	this._speakingMonitor.on('speakingWhileMuted', () => {
		this.emit('speakingWhileMuted')
	})
	this._speakingMonitor.on('stoppedSpeaking', () => {
		this.emit('stoppedSpeaking')
	})
	this._speakingMonitor.on('stoppedSpeakingWhileMuted', () => {
		this.emit('stoppedSpeakingWhileMuted')
	})
	this._speakingMonitor.on('volumeChange', (speakingMonitor, volume, threshold) => {
		this.emit('volumeChange', volume, threshold)
	})

	this._trackToStream = new TrackToStream()
	this._trackToStream.addInputTrackSlot('audio')
	this._trackToStream.addInputTrackSlot('video')

	this._trackToSentStream = new TrackToStream()
	this._trackToSentStream.addInputTrackSlot('audio')
	this._trackToSentStream.addInputTrackSlot('video')

	this._handleStreamSetBound = this._handleStreamSet.bind(this)
	this._handleTrackReplacedBound = this._handleTrackReplaced.bind(this)
	this._handleTrackEnabledBound = this._handleTrackEnabled.bind(this)

	this._mediaDevicesSource.connectTrackSink('audio', this._audioTrackEnabler)
	this._mediaDevicesSource.connectTrackSink('video', this._videoTrackEnabler)

	this._audioTrackEnabler.connectTrackSink('default', this._speakingMonitor)
	this._audioTrackEnabler.connectTrackSink('default', this._trackToStream, 'audio')
	this._audioTrackEnabler.connectTrackSink('default', this._trackToSentStream, 'audio')

	this._videoTrackEnabler.connectTrackSink('default', this._videoTrackConstrainer)

	this._videoTrackConstrainer.connectTrackSink('default', this._virtualBackground)

	this._virtualBackground.connectTrackSink('default', this._trackToStream, 'video')
	this._virtualBackground.connectTrackSink('default', this._trackToSentStream, 'video')
}

util.inherits(LocalMedia, WildEmitter)

/**
 * Returns whether the local media is active or not.
 *
 * The local media is active if it has been started and not stopped yet, even if
 * no media was available when started. An active local media will automatically
 * react to changes in the selected media devices.
 *
 * @return {boolean} true if the local media is active, false otherwise
 */
LocalMedia.prototype.isLocalMediaActive = function() {
	return this._localMediaActive
}

LocalMedia.prototype.hasAudioTrack = function() {
	return this._trackToStream.getStream() && this._trackToStream.getStream().getAudioTracks().length > 0
}

LocalMedia.prototype.hasVideoTrack = function() {
	return this._trackToStream.getStream() && this._trackToStream.getStream().getVideoTracks().length > 0
}

LocalMedia.prototype.start = function(mediaConstraints, cb, context) {
	const self = this
	const constraints = mediaConstraints || { audio: true, video: true }

	if (constraints.audio) {
		this.allowAudio()
	} else {
		this.disallowAudio()
	}
	if (constraints.video) {
		this.allowVideo()
	} else {
		this.disallowVideo()
	}

	// If local media is started with neither audio nor video the local media
	// will not be active (it will not react to changes in the selected media
	// devices). It is just a special case in which starting succeeds with a
	// null stream.
	if (!constraints.audio && !constraints.video) {
		self.emit('localStream', null)

		if (cb) {
			return cb(null, null, constraints)
		}

		return
	}

	if (!webrtcIndex.mediaDevicesManager.isSupported()) {
		const error = new Error('MediaStreamError')
		error.name = 'NotSupportedError'

		if (cb) {
			return cb(error, null)
		}

		return
	}

	this.emit('localStreamRequested', context)

	const retryNoVideoCallback = (error) => {
		self.emit('localStreamRequestFailedRetryNoVideo', error)
	}

	this._mediaDevicesSource.start(retryNoVideoCallback).then(() => {
		self.localStreams.push(self._trackToStream.getStream())
		self.sentStreams.push(self._trackToSentStream.getStream())

		self.emit('localStream', self._trackToStream.getStream())

		self._trackToStream.on('streamSet', self._handleStreamSetBound)
		self._trackToStream.on('trackReplaced', self._handleTrackReplacedBound)
		self._trackToStream.on('trackEnabled', self._handleTrackEnabledBound)

		self._trackToSentStream.on('streamSet', self._handleStreamSetBound)
		self._trackToSentStream.on('trackReplaced', self._handleTrackReplacedBound)
		self._trackToSentStream.on('trackEnabled', self._handleTrackEnabledBound)

		self._localMediaActive = true

		if (cb) {
			const actualConstraints = {
				audio: self._trackToStream.getStream().getAudioTracks().length > 0,
				video: self._trackToStream.getStream().getVideoTracks().length > 0,
			}

			return cb(null, self._trackToStream.getStream(), actualConstraints)
		}
	}).catch(err => {
		self.emit('localStreamRequestFailed')

		self._trackToStream.on('streamSet', self._handleStreamSetBound)
		self._trackToStream.on('trackReplaced', self._handleTrackReplacedBound)
		self._trackToStream.on('trackEnabled', self._handleTrackEnabledBound)

		self._trackToSentStream.on('streamSet', self._handleStreamSetBound)
		self._trackToSentStream.on('trackReplaced', self._handleTrackReplacedBound)
		self._trackToSentStream.on('trackEnabled', self._handleTrackEnabledBound)

		self._localMediaActive = true

		if (cb) {
			return cb(err, null)
		}
	})
}

LocalMedia.prototype._handleStreamSet = function(trackToStream, newStream, oldStream) {
	if (oldStream) {
		this._removeStream(oldStream)
	}

	if (newStream) {
		trackToStream === this._trackToStream ? this.localStreams.push(newStream) : this.sentStreams.push(newStream)
	}

	// "streamSet" is always emitted along with "trackReplaced", so the
	// "localStreamChanged" only needs to be relayed on "trackReplaced".
}

LocalMedia.prototype._handleTrackReplaced = function(trackToStream, newTrack, oldTrack) {
	if (trackToStream === this._trackToStream) {
		// "localStreamChanged" is expected to be emitted also when the tracks
		// of the stream change, even if the stream itself is the same.
		this.emit('localStreamChanged', trackToStream.getStream())
		this.emit('localTrackReplaced', newTrack, oldTrack, trackToStream.getStream())
	} else {
		this.emit('sentTrackReplaced', newTrack, oldTrack, trackToStream.getStream())
	}
}

LocalMedia.prototype._handleTrackEnabled = function(trackToStream, track) {
	// MediaStreamTrack does not emit an event when the enabled property
	// changes, so it needs to be explicitly notified.
	if (trackToStream === this._trackToStream) {
		this.emit('localTrackEnabledChanged', track, trackToStream.getStream())
	} else {
		this.emit('sentTrackEnabledChanged', track, trackToStream.getStream())
	}
}

LocalMedia.prototype.stop = function() {
	// Handlers need to be removed before stopping the stream to prevent
	// relaying no longer needed events.
	this._trackToStream.off('streamSet', this._handleStreamSetBound)
	this._trackToStream.off('trackReplaced', this._handleTrackReplacedBound)
	this._trackToStream.off('trackEnabled', this._handleTrackEnabledBound)

	this._trackToSentStream.off('streamSet', this._handleStreamSetBound)
	this._trackToSentStream.off('trackReplaced', this._handleTrackReplacedBound)
	this._trackToSentStream.off('trackEnabled', this._handleTrackEnabledBound)

	this.stopStream()
	this.stopScreenShare()

	this._localMediaActive = false
}

LocalMedia.prototype.stopStream = function() {
	const stream = this._trackToStream.getStream()
	const sentStream = this._trackToSentStream.getStream()

	this._mediaDevicesSource.stop()

	if (stream) {
		this._removeStream(stream)
	}
	if (sentStream) {
		this._removeStream(sentStream)
	}
}

LocalMedia.prototype.startScreenShare = function(mode, constraints, cb) {
	const self = this

	this.emit('localScreenRequested')

	if (typeof constraints === 'function' && !cb) {
		cb = constraints
		constraints = null
	}

	getScreenMedia(mode, constraints, function(err, stream) {
		if (!err) {
			self.localScreens.push(stream)

			stream.getTracks().forEach(function(track) {
				track.addEventListener('ended', function() {
					let isAllTracksEnded = true
					stream.getTracks().forEach(function(t) {
						isAllTracksEnded = t.readyState === 'ended' && isAllTracksEnded
					})

					if (isAllTracksEnded) {
						self._removeStream(stream)
					}
				})
			})

			self.emit('localScreen', stream)
		} else {
			self.emit('localScreenRequestFailed')
		}

		// enable the callback
		if (cb) {
			return cb(err, stream)
		}
	})
}

LocalMedia.prototype.stopScreenShare = function() {
	const self = this

	this.localScreens.forEach(function(stream) {
		stream.getTracks().forEach(function(track) { track.stop() })
		self._removeStream(stream)
	})
}

// Audio controls
LocalMedia.prototype.isAudioAllowed = function() {
	return this._mediaDevicesSource.isAudioAllowed()
}

LocalMedia.prototype.disallowAudio = function() {
	this._mediaDevicesSource.setAudioAllowed(false)
	this.emit('audioDisallowed')
}

LocalMedia.prototype.allowAudio = function() {
	this._mediaDevicesSource.setAudioAllowed(true)
	this.emit('audioAllowed')
}

LocalMedia.prototype.mute = function() {
	this._setAudioEnabled(false)
	this.emit('audioOff')
}

LocalMedia.prototype.unmute = function() {
	this._setAudioEnabled(true)
	this.emit('audioOn')
}

// Video controls
LocalMedia.prototype.isVideoAllowed = function() {
	return this._mediaDevicesSource.isVideoAllowed()
}

LocalMedia.prototype.disallowVideo = function() {
	this._mediaDevicesSource.setVideoAllowed(false)
	this.emit('videoDisallowed')
}

LocalMedia.prototype.allowVideo = function() {
	this._mediaDevicesSource.setVideoAllowed(true)
	this.emit('videoAllowed')
}

LocalMedia.prototype.pauseVideo = function() {
	this._setVideoEnabled(false)
	this.emit('videoOff')
}
LocalMedia.prototype.resumeVideo = function() {
	this._setVideoEnabled(true)
	this.emit('videoOn')
}

LocalMedia.prototype.enableVirtualBackground = function() {
	this._virtualBackground.setEnabled(true)
	this.emit('virtualBackgroundOn')
}

LocalMedia.prototype.disableVirtualBackground = function() {
	this._virtualBackground.setEnabled(false)
	this.emit('virtualBackgroundOff')
}

// Combined controls
LocalMedia.prototype.pause = function() {
	this.mute()
	this.pauseVideo()
}
LocalMedia.prototype.resume = function() {
	this.unmute()
	this.resumeVideo()
}

// Internal methods for enabling/disabling audio/video
LocalMedia.prototype._setAudioEnabled = function(bool) {
	this._audioTrackEnabler.setEnabled(bool)
}
LocalMedia.prototype._setVideoEnabled = function(bool) {
	this._videoTrackEnabler.setEnabled(bool)
}

// check if all audio streams are enabled
LocalMedia.prototype.isAudioEnabled = function() {
	let enabled = true
	let hasAudioTracks = false
	this.localStreams.forEach(function(stream) {
		const audioTracks = stream.getAudioTracks()
		if (audioTracks.length > 0) {
			hasAudioTracks = true
			audioTracks.forEach(function(track) {
				enabled = enabled && track.enabled
			})
		}
	})

	// If no audioTracks were found, that means there is no microphone device.
	// In that case, isAudioEnabled should return false.
	if (!hasAudioTracks) {
		return false
	}

	return enabled
}

// check if all video streams are enabled
LocalMedia.prototype.isVideoEnabled = function() {
	let enabled = true
	let hasVideoTracks = false
	this.localStreams.forEach(function(stream) {
		const videoTracks = stream.getVideoTracks()
		if (videoTracks.length > 0) {
			hasVideoTracks = true
			videoTracks.forEach(function(track) {
				enabled = enabled && track.enabled
			})
		}
	})

	// If no videoTracks were found, that means there is no camera device.
	// In that case, isVideoEnabled should return false.
	if (!hasVideoTracks) {
		return false
	}

	return enabled
}

LocalMedia.prototype.isVirtualBackgroundAvailable = function() {
	return this._virtualBackground.isAvailable()
}

LocalMedia.prototype.isVirtualBackgroundEnabled = function() {
	return this._virtualBackground.isEnabled()
}

LocalMedia.prototype._removeStream = function(stream) {
	let idx = this.localStreams.indexOf(stream)
	if (idx > -1) {
		this.localStreams.splice(idx, 1)
		this.emit('localStreamStopped', stream)

		return
	}

	idx = this.sentStreams.indexOf(stream)
	if (idx > -1) {
		this.sentStreams.splice(idx, 1)
		this.emit('sentStreamStopped', stream)

		return
	}

	idx = this.localScreens.indexOf(stream)
	if (idx > -1) {
		this.localScreens.splice(idx, 1)
		this.emit('localScreenStopped', stream)
	}
}

// fallback for old .localScreen behaviour
Object.defineProperty(LocalMedia.prototype, 'localScreen', {
	get() {
		return this.localScreens.length > 0 ? this.localScreens[0] : null
	},
})

module.exports = LocalMedia