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: a7a008923acbe99e02747afe1595d10e0694d90f (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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
/* global module */

const util = require('util')
const hark = require('hark')
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')

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

function isAllAudioTracksEnded(stream) {
	let isAllAudioTracksEnded = true
	stream.getAudioTracks().forEach(function(t) {
		isAllAudioTracksEnded = t.readyState === 'ended' && isAllAudioTracksEnded
	})
	return isAllAudioTracksEnded
}

function LocalMedia(opts) {
	WildEmitter.call(this)

	const config = this.config = {
		detectSpeakingEvents: false,
		audioFallback: false,
		harkOptions: null,
		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._audioEnabled = true
	this._videoEnabled = true

	this._localMediaActive = false

	this.localStreams = []
	this._audioMonitorStreams = []
	this.localScreens = []

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

	this._audioMonitors = []
	this.on('localScreenStopped', this._stopAudioMonitor.bind(this))

	this._handleAudioInputIdChangedBound = this._handleAudioInputIdChanged.bind(this)
	this._handleVideoInputIdChangedBound = this._handleVideoInputIdChanged.bind(this)
}

util.inherits(LocalMedia, WildEmitter)

/**
 * Clones a MediaStreamTrack that will be ended when the original
 * MediaStreamTrack is ended.
 *
 * @param {MediaStreamTrack} track the track to clone
 * @returns {MediaStreamTrack} the linked track
 */
const cloneLinkedTrack = function(track) {
	const linkedTrack = track.clone()

	// Keep a reference of all the linked clones of a track to be able to
	// remove them when the source track is removed.
	if (!track.linkedTracks) {
		track.linkedTracks = []
	}
	track.linkedTracks.push(linkedTrack)

	track.addEventListener('ended', function() {
		linkedTrack.stop()
	})

	return linkedTrack
}

/**
 * Clones a MediaStream that will be ended when the original MediaStream is
 * ended.
 *
 * @param {MediaStream} stream the stream to clone
 * @returns {MediaStream} the linked stream
 */
const cloneLinkedStream = function(stream) {
	const linkedStream = new MediaStream()

	stream.getTracks().forEach(function(track) {
		linkedStream.addTrack(cloneLinkedTrack(track))
	})

	stream.addEventListener('addtrack', function(event) {
		linkedStream.addTrack(cloneLinkedTrack(event.track))
	})

	stream.addEventListener('removetrack', function(event) {
		event.track.linkedTracks.forEach(linkedTrack => {
			linkedStream.removeTrack(linkedTrack)
		})
	})

	return linkedStream
}

/**
 * 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.
 *
 * @returns {bool} true if the local media is active, false otherwise
 */
LocalMedia.prototype.isLocalMediaActive = function() {
	return this._localMediaActive
}

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

	// 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', constraints, 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', constraints, context)

	if (!context) {
		// Try to get the devices list before getting user media.
		webrtcIndex.mediaDevicesManager.enableDeviceEvents()
		webrtcIndex.mediaDevicesManager.disableDeviceEvents()
	}

	// The handlers for "change:audioInputId" and "change:videoInputId" events
	// expect the initial "getUserMedia" call to have been completed before
	// being used, so they must be set when the promise is resolved or rejected.

	webrtcIndex.mediaDevicesManager.getUserMedia(constraints).then(function(stream) {
		// Although the promise should be resolved only if all the constraints
		// are met Edge resolves it if both audio and video are requested but
		// only audio is available.
		if (constraints.video && stream.getVideoTracks().length === 0) {
			self.emit('localStreamRequestFailedRetryNoVideo', constraints)
			constraints.video = false
			self.start(constraints, cb, 'retry-no-video')
			return
		}

		// The audio monitor stream is never disabled to be able to analyze it
		// even when the stream sent is muted.
		const audioMonitorStream = cloneLinkedStream(stream)
		if (constraints.audio && self.config.detectSpeakingEvents) {
			self._setupAudioMonitor(audioMonitorStream, self.config.harkOptions)
		}
		self.localStreams.push(stream)
		self._audioMonitorStreams.push(audioMonitorStream)

		stream.getTracks().forEach(function(track) {
			if ((track.kind === 'audio' && !self._audioEnabled)
				|| (track.kind === 'video' && !self._videoEnabled)) {
				track.enabled = false
			}

			track.addEventListener('ended', function() {
				if (isAllTracksEnded(stream) && !self._pendingAudioInputIdChangedCount && !self._pendingVideoInputIdChangedCount) {
					self._removeStream(stream)
				}
			})
		})

		self.emit('localStream', constraints, stream)

		webrtcIndex.mediaDevicesManager.on('change:audioInputId', self._handleAudioInputIdChangedBound)
		webrtcIndex.mediaDevicesManager.on('change:videoInputId', self._handleVideoInputIdChangedBound)

		self._localMediaActive = true

		if (cb) {
			return cb(null, stream, constraints)
		}
	}).catch(function(err) {
		// Fallback for users without a camera or with a camera that can not be
		// accessed, but only if audio is meant to be used.
		if (constraints.audio !== false && self.config.audioFallback && constraints.video !== false) {
			self.emit('localStreamRequestFailedRetryNoVideo', constraints, err)
			constraints.video = false
			self.start(constraints, cb, 'retry-no-video')
			return
		}

		self.emit('localStreamRequestFailed', constraints)

		webrtcIndex.mediaDevicesManager.on('change:audioInputId', self._handleAudioInputIdChangedBound)
		webrtcIndex.mediaDevicesManager.on('change:videoInputId', self._handleVideoInputIdChangedBound)

		self._localMediaActive = true

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

LocalMedia.prototype._handleAudioInputIdChanged = function(mediaDevicesManager, audioInputId) {
	if (this._pendingAudioInputIdChangedCount) {
		this._pendingAudioInputIdChangedCount++

		return
	}

	this._pendingAudioInputIdChangedCount = 1

	const resetPendingAudioInputIdChangedCount = () => {
		const audioInputIdChangedAgain = this._pendingAudioInputIdChangedCount > 1

		this._pendingAudioInputIdChangedCount = 0

		if (audioInputIdChangedAgain) {
			this._handleAudioInputIdChanged(webrtcIndex.mediaDevicesManager.get('audioInputId'))
		}

		if (!this._pendingAudioInputIdChangedCount && !this._pendingVideoInputIdChangedCount) {
			this.localStreams.forEach(stream => {
				if (isAllTracksEnded(stream)) {
					this._removeStream(stream)
				}
			})
		}
	}

	const localStreamsChanged = []
	const localTracksReplaced = []

	if (this.localStreams.length === 0 && audioInputId) {
		// Force the creation of a new stream to add a new audio track to it.
		localTracksReplaced.push({ track: null, stream: null })
	}

	this.localStreams.forEach(stream => {
		if (stream.getAudioTracks().length === 0) {
			localStreamsChanged.push(stream)

			localTracksReplaced.push({ track: null, stream })
		}

		stream.getAudioTracks().forEach(track => {
			const settings = track.getSettings()
			if (track.kind === 'audio' && settings && settings.deviceId !== audioInputId) {
				track.stop()

				stream.removeTrack(track)

				if (!localStreamsChanged.includes(stream)) {
					localStreamsChanged.push(stream)
				}

				localTracksReplaced.push({ track, stream })
			}
		})
	})

	if (audioInputId === null) {
		localStreamsChanged.forEach(stream => {
			this.emit('localStreamChanged', stream)
		})

		localTracksReplaced.forEach(trackStreamPair => {
			this.emit('localTrackReplaced', null, trackStreamPair.track, trackStreamPair.stream)
		})

		resetPendingAudioInputIdChangedCount()

		return
	}

	if (localTracksReplaced.length === 0) {
		resetPendingAudioInputIdChangedCount()

		return
	}

	webrtcIndex.mediaDevicesManager.getUserMedia({ audio: true }).then(stream => {
		// According to the specification "getUserMedia({ audio: true })" will
		// return a single audio track.
		const track = stream.getTracks()[0]
		if (stream.getTracks().length > 1) {
			console.error('More than a single audio track returned by getUserMedia, only the first one will be used')
		}

		localTracksReplaced.forEach(trackStreamPair => {
			const clonedTrack = track.clone()

			let stream = trackStreamPair.stream
			let streamIndex = this.localStreams.indexOf(stream)
			if (streamIndex < 0) {
				stream = new MediaStream()
				this.localStreams.push(stream)
				streamIndex = this.localStreams.length - 1
			}

			stream.addTrack(clonedTrack)

			// The audio monitor stream is never disabled to be able to analyze
			// it even when the stream sent is muted.
			let audioMonitorStream
			if (streamIndex > this._audioMonitorStreams.length - 1) {
				audioMonitorStream = cloneLinkedStream(stream)
				this._audioMonitorStreams.push(audioMonitorStream)
			} else {
				audioMonitorStream = this._audioMonitorStreams[streamIndex]
			}

			if (this.config.detectSpeakingEvents) {
				this._setupAudioMonitor(audioMonitorStream, this.config.harkOptions)
			}

			if (!this._audioEnabled) {
				clonedTrack.enabled = false
			}

			clonedTrack.addEventListener('ended', () => {
				if (isAllTracksEnded(stream) && !this._pendingAudioInputIdChangedCount && !this._pendingVideoInputIdChangedCount) {
					this._removeStream(stream)
				}
			})

			this.emit('localStreamChanged', stream)
			this.emit('localTrackReplaced', clonedTrack, trackStreamPair.track, trackStreamPair.stream)
		})

		// After the clones were added to the local streams the original track
		// is no longer needed.
		track.stop()

		resetPendingAudioInputIdChangedCount()
	}).catch(() => {
		localStreamsChanged.forEach(stream => {
			this.emit('localStreamChanged', stream)
		})

		localTracksReplaced.forEach(trackStreamPair => {
			this.emit('localTrackReplaced', null, trackStreamPair.track, trackStreamPair.stream)
		})

		resetPendingAudioInputIdChangedCount()
	})
}

LocalMedia.prototype._handleVideoInputIdChanged = function(mediaDevicesManager, videoInputId) {
	if (this._pendingVideoInputIdChangedCount) {
		this._pendingVideoInputIdChangedCount++

		return
	}

	this._pendingVideoInputIdChangedCount = 1

	const resetPendingVideoInputIdChangedCount = () => {
		const videoInputIdChangedAgain = this._pendingVideoInputIdChangedCount > 1

		this._pendingVideoInputIdChangedCount = 0

		if (videoInputIdChangedAgain) {
			this._handleVideoInputIdChanged(webrtcIndex.mediaDevicesManager.get('videoInputId'))
		}

		if (!this._pendingAudioInputIdChangedCount && !this._pendingVideoInputIdChangedCount) {
			this.localStreams.forEach(stream => {
				if (isAllTracksEnded(stream)) {
					this._removeStream(stream)
				}
			})
		}
	}

	const localStreamsChanged = []
	const localTracksReplaced = []

	if (this.localStreams.length === 0 && videoInputId) {
		// Force the creation of a new stream to add a new video track to it.
		localTracksReplaced.push({ track: null, stream: null })
	}

	this.localStreams.forEach(stream => {
		if (stream.getVideoTracks().length === 0) {
			localStreamsChanged.push(stream)

			localTracksReplaced.push({ track: null, stream })
		}

		stream.getVideoTracks().forEach(track => {
			const settings = track.getSettings()
			if (track.kind === 'video' && settings && settings.deviceId !== videoInputId) {
				track.stop()

				stream.removeTrack(track)

				if (!localStreamsChanged.includes(stream)) {
					localStreamsChanged.push(stream)
				}

				localTracksReplaced.push({ track, stream })
			}
		})
	})

	if (videoInputId === null) {
		localStreamsChanged.forEach(stream => {
			this.emit('localStreamChanged', stream)
		})

		localTracksReplaced.forEach(trackStreamPair => {
			this.emit('localTrackReplaced', null, trackStreamPair.track, trackStreamPair.stream)
		})

		resetPendingVideoInputIdChangedCount()

		return
	}

	if (localTracksReplaced.length === 0) {
		resetPendingVideoInputIdChangedCount()

		return
	}

	webrtcIndex.mediaDevicesManager.getUserMedia({ video: true }).then(stream => {
		// According to the specification "getUserMedia({ video: true })" will
		// return a single video track.
		const track = stream.getTracks()[0]
		if (stream.getTracks().length > 1) {
			console.error('More than a single video track returned by getUserMedia, only the first one will be used')
		}

		localTracksReplaced.forEach(trackStreamPair => {
			const clonedTrack = track.clone()

			let stream = trackStreamPair.stream
			if (!this.localStreams.includes(stream)) {
				stream = new MediaStream()
				this.localStreams.push(stream)

				const audioMonitorStream = cloneLinkedStream(stream)
				this._audioMonitorStreams.push(audioMonitorStream)
			}

			stream.addTrack(clonedTrack)

			if (!this._videoEnabled) {
				clonedTrack.enabled = false
			}

			clonedTrack.addEventListener('ended', () => {
				if (isAllTracksEnded(stream) && !this._pendingAudioInputIdChangedCount && !this._pendingVideoInputIdChangedCount) {
					this._removeStream(stream)
				}
			})

			this.emit('localStreamChanged', stream)
			this.emit('localTrackReplaced', clonedTrack, trackStreamPair.track, trackStreamPair.stream)
		})

		// After the clones were added to the local streams the original track
		// is no longer needed.
		track.stop()

		resetPendingVideoInputIdChangedCount()
	}).catch(() => {
		localStreamsChanged.forEach(stream => {
			this.emit('localStreamChanged', stream)
		})

		localTracksReplaced.forEach(trackStreamPair => {
			this.emit('localTrackReplaced', null, trackStreamPair.track, trackStreamPair.stream)
		})

		resetPendingVideoInputIdChangedCount()
	})
}

LocalMedia.prototype.stop = function(stream) {
	this.stopStream(stream)
	this.stopScreenShare(stream)

	webrtcIndex.mediaDevicesManager.off('change:audioInputId', this._handleAudioInputIdChangedBound)
	webrtcIndex.mediaDevicesManager.off('change:videoInputId', this._handleVideoInputIdChangedBound)

	this._localMediaActive = false
}

LocalMedia.prototype.stopStream = function(stream) {
	if (stream) {
		const idx = this.localStreams.indexOf(stream)
		if (idx > -1) {
			stream.getTracks().forEach(function(track) {
				track.stop()
			})
		}
	} else {
		this.localStreams.forEach(function(stream) {
			stream.getTracks().forEach(function(track) {
				track.stop()
			})
		})
	}
}

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(stream) {
	const self = this

	if (stream) {
		const idx = this.localScreens.indexOf(stream)
		if (idx > -1) {
			stream.getTracks().forEach(function(track) { track.stop() })
			this._removeStream(stream)
		}
	} else {
		this.localScreens.forEach(function(stream) {
			stream.getTracks().forEach(function(track) { track.stop() })
			self._removeStream(stream)
		})
	}
}

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

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

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

// 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._audioEnabled = bool

	this.localStreams.forEach(stream => {
		stream.getAudioTracks().forEach(track => {
			track.enabled = !!bool

			// MediaStreamTrack does not emit an event when the enabled property
			// changes, so it needs to be explicitly notified.
			this.emit('localTrackEnabledChanged', track, stream)
		})
	})
}
LocalMedia.prototype._setVideoEnabled = function(bool) {
	this._videoEnabled = bool

	this.localStreams.forEach(stream => {
		stream.getVideoTracks().forEach(track => {
			track.enabled = !!bool

			// MediaStreamTrack does not emit an event when the enabled property
			// changes, so it needs to be explicitly notified.
			this.emit('localTrackEnabledChanged', track, stream)
		})
	})
}

// 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._removeStream = function(stream) {
	let idx = this.localStreams.indexOf(stream)
	if (idx > -1) {
		this.localStreams.splice(idx, 1)
		this._audioMonitorStreams.splice(idx, 1)
		this.emit('localStreamStopped', stream)
	} else {
		idx = this.localScreens.indexOf(stream)
		if (idx > -1) {
			this.localScreens.splice(idx, 1)
			this.emit('localScreenStopped', stream)
		}
	}
}

LocalMedia.prototype._setupAudioMonitor = function(stream, harkOptions) {
	this._log('Setup audio')
	const audio = hark(stream, harkOptions)
	const self = this
	let timeout

	stream.getAudioTracks().forEach(function(track) {
		track.addEventListener('ended', function() {
			if (isAllAudioTracksEnded(stream)) {
				self._stopAudioMonitor(stream)
			}
		})
	})

	audio.on('speaking', function() {
		self._speaking = true

		if (self._audioEnabled) {
			self.emit('speaking')
		} else {
			self.emit('speakingWhileMuted')
		}
	})

	audio.on('stopped_speaking', function() {
		if (timeout) {
			clearTimeout(timeout)
		}

		timeout = setTimeout(function() {
			self._speaking = false

			if (self._audioEnabled) {
				self.emit('stoppedSpeaking')
			} else {
				self.emit('stoppedSpeakingWhileMuted')
			}
		}, 1000)
	})

	self.on('audioOn', function() {
		if (self._speaking) {
			self.emit('stoppedSpeakingWhileMuted')
			self.emit('speaking')
		}
	})

	self.on('audioOff', function() {
		if (self._speaking) {
			self.emit('stoppedSpeaking')
			self.emit('speakingWhileMuted')
		}
	})

	audio.on('volume_change', function(volume, threshold) {
		self.emit('volumeChange', volume, threshold)
	})

	this._audioMonitors.push({ audio, stream })
}

LocalMedia.prototype._stopAudioMonitor = function(stream) {
	let idx = -1
	this._audioMonitors.forEach(function(monitors, i) {
		if (monitors.stream === stream) {
			idx = i
		}
	})

	if (idx > -1) {
		this._audioMonitors[idx].audio.stop()
		this._audioMonitors.splice(idx, 1)
	}
}

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

module.exports = LocalMedia