-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathRTC.js
963 lines (826 loc) · 29.6 KB
/
RTC.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
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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
/* global __filename */
import { getLogger } from 'jitsi-meet-logger';
import BridgeChannel from './BridgeChannel';
import GlobalOnErrorHandler from '../util/GlobalOnErrorHandler';
import * as JitsiConferenceEvents from '../../JitsiConferenceEvents';
import JitsiLocalTrack from './JitsiLocalTrack';
import Listenable from '../util/Listenable';
import { safeCounterIncrement } from '../util/MathUtil';
import * as MediaType from '../../service/RTC/MediaType';
import browser from '../browser';
import RTCEvents from '../../service/RTC/RTCEvents';
import RTCUtils from './RTCUtils';
import Statistics from '../statistics/statistics';
import TraceablePeerConnection from './TraceablePeerConnection';
import VideoType from '../../service/RTC/VideoType';
const logger = getLogger(__filename);
/**
* The counter used to generated id numbers assigned to peer connections
* @type {number}
*/
let peerConnectionIdCounter = 0;
/**
* The counter used to generate id number for the local
* <code>MediaStreamTrack</code>s.
* @type {number}
*/
let rtcTrackIdCounter = 0;
/**
*
* @param tracksInfo
* @param options
*/
function createLocalTracks(tracksInfo, options) {
const newTracks = [];
let deviceId = null;
tracksInfo.forEach(trackInfo => {
if (trackInfo.mediaType === MediaType.AUDIO) {
deviceId = options.micDeviceId;
} else if (trackInfo.videoType === VideoType.CAMERA) {
deviceId = options.cameraDeviceId;
}
rtcTrackIdCounter = safeCounterIncrement(rtcTrackIdCounter);
const localTrack = new JitsiLocalTrack({
...trackInfo,
deviceId,
facingMode: options.facingMode,
rtcId: rtcTrackIdCounter,
effects: options.effects
});
newTracks.push(localTrack);
});
return newTracks;
}
/**
* Creates {@code JitsiLocalTrack} instances from the passed in meta information
* about MedieaTracks.
*
* @param {Object[]} mediaStreamMetaData - An array of meta information with
* MediaTrack instances. Each can look like:
* {{
* stream: MediaStream instance that holds a track with audio or video,
* track: MediaTrack within the MediaStream,
* videoType: "camera" or "desktop" or falsy,
* sourceId: ID of the desktopsharing source,
* sourceType: The desktopsharing source type,
* effects: Array of effect types
* }}
*/
function _newCreateLocalTracks(mediaStreamMetaData = []) {
return mediaStreamMetaData.map(metaData => {
const {
sourceId,
sourceType,
stream,
track,
videoType,
effects
} = metaData;
const { deviceId, facingMode } = track.getSettings();
// FIXME Move rtcTrackIdCounter to a static method in JitsiLocalTrack
// so RTC does not need to handle ID management. This move would be
// safer to do once the old createLocalTracks is removed.
rtcTrackIdCounter = safeCounterIncrement(rtcTrackIdCounter);
return new JitsiLocalTrack({
deviceId,
facingMode,
mediaType: track.kind,
rtcId: rtcTrackIdCounter,
sourceId,
sourceType,
stream,
track,
videoType: videoType || null,
effects
});
});
}
/**
*
*/
export default class RTC extends Listenable {
/**
*
* @param conference
* @param options
*/
constructor(conference, options = {}) {
super();
this.conference = conference;
/**
* A map of active <tt>TraceablePeerConnection</tt>.
* @type {Map.<number, TraceablePeerConnection>}
*/
this.peerConnections = new Map();
this.localTracks = [];
this.options = options;
// BridgeChannel instance.
// @private
// @type {BridgeChannel}
this._channel = null;
// A flag whether we had received that the channel had opened we can
// get this flag out of sync if for some reason channel got closed
// from server, a desired behaviour so we can see errors when this
// happen.
// @private
// @type {boolean}
this._channelOpen = false;
/**
* The value specified to the last invocation of setLastN before the
* channel completed opening. If non-null, the value will be sent
* through a channel (once) as soon as it opens and will then be
* discarded.
* @private
* @type {number}
*/
this._lastN = -1;
/**
* Defines the last N endpoints list. It can be null or an array once
* initialised with a channel last N event.
* @type {Array<string>|null}
* @private
*/
this._lastNEndpoints = null;
/**
* The number representing the maximum video height the local client
* should receive from the bridge.
*
* @type {number|undefined}
* @private
*/
this._maxFrameHeight = undefined;
/**
* The endpoint ID of currently pinned participant or <tt>null</tt> if
* no user is pinned.
* @type {string|null}
* @private
*/
this._pinnedEndpoint = null;
/**
* The endpoint IDs of currently selected participants.
*
* @type {Array}
* @private
*/
this._selectedEndpoints = [];
// The last N change listener.
this._lastNChangeListener = this._onLastNChanged.bind(this);
this._onDeviceListChanged = this._onDeviceListChanged.bind(this);
this._updateAudioOutputForAudioTracks
= this._updateAudioOutputForAudioTracks.bind(this);
// Switch audio output device on all remote audio tracks. Local audio
// tracks handle this event by themselves.
if (RTCUtils.isDeviceChangeAvailable('output')) {
RTCUtils.addListener(
RTCEvents.AUDIO_OUTPUT_DEVICE_CHANGED,
this._updateAudioOutputForAudioTracks
);
RTCUtils.addListener(
RTCEvents.DEVICE_LIST_CHANGED,
this._onDeviceListChanged
);
}
}
/**
* Removes any listeners and stored state from this {@code RTC} instance.
*
* @returns {void}
*/
destroy() {
RTCUtils.removeListener(
RTCEvents.AUDIO_OUTPUT_DEVICE_CHANGED,
this._updateAudioOutputForAudioTracks
);
RTCUtils.removeListener(
RTCEvents.DEVICE_LIST_CHANGED,
this._onDeviceListChanged
);
this.removeListener(
RTCEvents.LASTN_ENDPOINT_CHANGED,
this._lastNChangeListener
);
if (this._channelOpenListener) {
this.removeListener(
RTCEvents.DATA_CHANNEL_OPEN,
this._channelOpenListener
);
}
}
/**
* Exposes the private helper for converting a WebRTC MediaStream to a
* JitsiLocalTrack.
*
* @param {Array<Object>} tracksInfo
* @returns {Array<JitsiLocalTrack>}
*/
static newCreateLocalTracks(tracksInfo) {
return _newCreateLocalTracks(tracksInfo);
}
/**
* Creates the local MediaStreams.
* @param {object} [options] Optional parameters.
* @param {array} options.devices The devices that will be requested.
* @param {string} options.resolution Resolution constraints.
* @param {string} options.cameraDeviceId
* @param {string} options.micDeviceId
* @returns {*} Promise object that will receive the new JitsiTracks
*/
static obtainAudioAndVideoPermissions(options) {
const usesNewGumFlow = browser.usesNewGumFlow();
const obtainMediaPromise = usesNewGumFlow
? RTCUtils.newObtainAudioAndVideoPermissions(options)
: RTCUtils.obtainAudioAndVideoPermissions(options);
return obtainMediaPromise.then(tracksInfo => {
if (usesNewGumFlow) {
return _newCreateLocalTracks(tracksInfo);
}
return createLocalTracks(tracksInfo, options);
});
}
/**
* Initializes the bridge channel of this instance.
* At least one of both, peerconnection or wsUrl parameters, must be
* given.
* @param {RTCPeerConnection} [peerconnection] WebRTC peer connection
* instance.
* @param {string} [wsUrl] WebSocket URL.
*/
initializeBridgeChannel(peerconnection, wsUrl) {
this._channel = new BridgeChannel(
peerconnection, wsUrl, this.eventEmitter);
this._channelOpenListener = () => {
// Mark that channel as opened.
this._channelOpen = true;
// When the channel becomes available, tell the bridge about
// video selections so that it can do adaptive simulcast,
// we want the notification to trigger even if userJid
// is undefined, or null.
try {
this._channel.sendPinnedEndpointMessage(
this._pinnedEndpoint);
this._channel.sendSelectedEndpointsMessage(
this._selectedEndpoints);
if (typeof this._maxFrameHeight !== 'undefined') {
this._channel.sendReceiverVideoConstraintMessage(
this._maxFrameHeight);
}
} catch (error) {
GlobalOnErrorHandler.callErrorHandler(error);
logger.error(
`Cannot send selected(${this._selectedEndpoint})`
+ `pinned(${this._pinnedEndpoint})`
+ `frameHeight(${this._maxFrameHeight}) endpoint message`,
error);
}
this.removeListener(RTCEvents.DATA_CHANNEL_OPEN,
this._channelOpenListener);
this._channelOpenListener = null;
// If setLastN was invoked before the bridge channel completed
// opening, apply the specified value now that the channel
// is open. NOTE that -1 is the default value assumed by both
// RTC module and the JVB.
if (this._lastN !== -1) {
this._channel.sendSetLastNMessage(this._lastN);
}
};
this.addListener(RTCEvents.DATA_CHANNEL_OPEN,
this._channelOpenListener);
// Add Last N change listener.
this.addListener(RTCEvents.LASTN_ENDPOINT_CHANGED,
this._lastNChangeListener);
}
/**
* Callback invoked when the list of known audio and video devices has
* been updated. Attempts to update the known available audio output
* devices.
*
* @private
* @returns {void}
*/
_onDeviceListChanged() {
this._updateAudioOutputForAudioTracks(RTCUtils.getAudioOutputDevice());
}
/**
* Receives events when Last N had changed.
* @param {array} lastNEndpoints The new Last N endpoints.
* @private
*/
_onLastNChanged(lastNEndpoints = []) {
const oldLastNEndpoints = this._lastNEndpoints || [];
let leavingLastNEndpoints = [];
let enteringLastNEndpoints = [];
this._lastNEndpoints = lastNEndpoints;
leavingLastNEndpoints = oldLastNEndpoints.filter(
id => !this.isInLastN(id));
enteringLastNEndpoints = lastNEndpoints.filter(
id => oldLastNEndpoints.indexOf(id) === -1);
this.conference.eventEmitter.emit(
JitsiConferenceEvents.LAST_N_ENDPOINTS_CHANGED,
leavingLastNEndpoints,
enteringLastNEndpoints);
}
/**
* Should be called when current media session ends and after the
* PeerConnection has been closed using PeerConnection.close() method.
*/
onCallEnded() {
if (this._channel) {
// The BridgeChannel is not explicitly closed as the PeerConnection
// is closed on call ended which triggers datachannel onclose
// events. If using a WebSocket, the channel must be closed since
// it is not managed by the PeerConnection.
// The reference is cleared to disable any logic related to the
// channel.
if (this._channel && this._channel.mode === 'websocket') {
this._channel.close();
}
this._channel = null;
this._channelOpen = false;
}
}
/**
* Sets the maximum video size the local participant should receive from
* remote participants. Will cache the value and send it through the channel
* once it is created.
*
* @param {number} maxFrameHeightPixels the maximum frame height, in pixels,
* this receiver is willing to receive.
* @returns {void}
*/
setReceiverVideoConstraint(maxFrameHeight) {
this._maxFrameHeight = maxFrameHeight;
if (this._channel && this._channelOpen) {
this._channel.sendReceiverVideoConstraintMessage(maxFrameHeight);
}
}
/**
* Elects the participants with the given ids to be the selected
* participants in order to always receive video for this participant (even
* when last n is enabled). If there is no channel we store it and send it
* through the channel once it is created.
*
* @param {Array<string>} ids - The user ids.
* @throws NetworkError or InvalidStateError or Error if the operation
* fails.
* @returns {void}
*/
selectEndpoints(ids) {
this._selectedEndpoints = ids;
if (this._channel && this._channelOpen) {
this._channel.sendSelectedEndpointsMessage(ids);
}
}
/**
* Elects the participant with the given id to be the pinned participant in
* order to always receive video for this participant (even when last n is
* enabled).
* @param {stirng} id The user id.
* @throws NetworkError or InvalidStateError or Error if the operation
* fails.
*/
pinEndpoint(id) {
// Cache the value if channel is missing, till we open it.
this._pinnedEndpoint = id;
if (this._channel && this._channelOpen) {
this._channel.sendPinnedEndpointMessage(id);
}
}
/**
*
* @param eventType
* @param listener
*/
static addListener(eventType, listener) {
RTCUtils.addListener(eventType, listener);
}
/**
*
* @param eventType
* @param listener
*/
static removeListener(eventType, listener) {
RTCUtils.removeListener(eventType, listener);
}
/**
*
* @param options
*/
static init(options = {}) {
this.options = options;
return RTCUtils.init(this.options);
}
/* eslint-disable max-params */
/**
* Creates new <tt>TraceablePeerConnection</tt>
* @param {SignalingLayer} signaling The signaling layer that will
* provide information about the media or participants which is not
* carried over SDP.
* @param {object} iceConfig An object describing the ICE config like
* defined in the WebRTC specification.
* @param {boolean} isP2P Indicates whether or not the new TPC will be used
* in a peer to peer type of session.
* @param {object} options The config options.
* @param {boolean} options.disableSimulcast If set to 'true' will disable
* the simulcast.
* @param {boolean} options.disableRtx If set to 'true' will disable the
* RTX.
* @param {boolean} options.disableH264 If set to 'true' H264 will be
* disabled by removing it from the SDP.
* @param {boolean} options.preferH264 If set to 'true' H264 will be
* preferred over other video codecs.
* @param {boolean} options.startSilent If set to 'true' no audio will be sent or received.
* @return {TraceablePeerConnection}
*/
createPeerConnection(signaling, iceConfig, isP2P, options) {
const pcConstraints = RTC.getPCConstraints(isP2P);
if (typeof options.abtestSuspendVideo !== 'undefined') {
RTCUtils.setSuspendVideo(pcConstraints, options.abtestSuspendVideo);
Statistics.analytics.addPermanentProperties(
{ abtestSuspendVideo: options.abtestSuspendVideo });
}
// FIXME: We should rename iceConfig to pcConfig.
if (browser.supportsInsertableStreams()) {
logger.debug('E2EE - setting insertable streams constraints');
iceConfig.forceEncodedAudioInsertableStreams = true;
iceConfig.forceEncodedVideoInsertableStreams = true;
}
if (browser.supportsSdpSemantics()) {
iceConfig.sdpSemantics = 'plan-b';
}
// Set the RTCBundlePolicy to max-bundle so that only one set of ice candidates is generated.
// The default policy generates separate ice candidates for audio and video connections.
// This change is necessary for Unified plan to work properly on Chrome and Safari.
iceConfig.bundlePolicy = 'max-bundle';
peerConnectionIdCounter = safeCounterIncrement(peerConnectionIdCounter);
const newConnection
= new TraceablePeerConnection(
this,
peerConnectionIdCounter,
signaling,
iceConfig, pcConstraints,
isP2P, options);
this.peerConnections.set(newConnection.id, newConnection);
return newConnection;
}
/* eslint-enable max-params */
/**
* Removed given peer connection from this RTC module instance.
* @param {TraceablePeerConnection} traceablePeerConnection
* @return {boolean} <tt>true</tt> if the given peer connection was removed
* successfully or <tt>false</tt> if there was no peer connection mapped in
* this RTC instance.
*/
_removePeerConnection(traceablePeerConnection) {
const id = traceablePeerConnection.id;
if (this.peerConnections.has(id)) {
// NOTE Remote tracks are not removed here.
this.peerConnections.delete(id);
return true;
}
return false;
}
/**
*
* @param track
*/
addLocalTrack(track) {
if (!track) {
throw new Error('track must not be null nor undefined');
}
this.localTracks.push(track);
track.conference = this.conference;
}
/**
* Returns the current value for "lastN" - the amount of videos are going
* to be delivered. When set to -1 for unlimited or all available videos.
* @return {number}
*/
getLastN() {
return this._lastN;
}
/**
* Get local video track.
* @returns {JitsiLocalTrack|undefined}
*/
getLocalVideoTrack() {
const localVideo = this.getLocalTracks(MediaType.VIDEO);
return localVideo.length ? localVideo[0] : undefined;
}
/**
* Get local audio track.
* @returns {JitsiLocalTrack|undefined}
*/
getLocalAudioTrack() {
const localAudio = this.getLocalTracks(MediaType.AUDIO);
return localAudio.length ? localAudio[0] : undefined;
}
/**
* Returns the local tracks of the given media type, or all local tracks if
* no specific type is given.
* @param {MediaType} [mediaType] Optional media type filter.
* (audio or video).
*/
getLocalTracks(mediaType) {
let tracks = this.localTracks.slice();
if (mediaType !== undefined) {
tracks = tracks.filter(
track => track.getType() === mediaType);
}
return tracks;
}
/**
* Obtains all remote tracks currently known to this RTC module instance.
* @param {MediaType} [mediaType] The remote tracks will be filtered
* by their media type if this argument is specified.
* @return {Array<JitsiRemoteTrack>}
*/
getRemoteTracks(mediaType) {
let remoteTracks = [];
for (const tpc of this.peerConnections.values()) {
const pcRemoteTracks = tpc.getRemoteTracks(undefined, mediaType);
if (pcRemoteTracks) {
remoteTracks = remoteTracks.concat(pcRemoteTracks);
}
}
return remoteTracks;
}
/**
* Set mute for all local audio streams attached to the conference.
* @param value The mute value.
* @returns {Promise}
*/
setAudioMute(value) {
const mutePromises = [];
this.getLocalTracks(MediaType.AUDIO).forEach(audioTrack => {
// this is a Promise
mutePromises.push(value ? audioTrack.mute() : audioTrack.unmute());
});
// We return a Promise from all Promises so we can wait for their
// execution.
return Promise.all(mutePromises);
}
/**
*
* @param track
*/
removeLocalTrack(track) {
const pos = this.localTracks.indexOf(track);
if (pos === -1) {
return;
}
this.localTracks.splice(pos, 1);
}
/**
* Removes all JitsiRemoteTracks associated with given MUC nickname
* (resource part of the JID). Returns array of removed tracks.
*
* @param {string} Owner The resource part of the MUC JID.
* @returns {JitsiRemoteTrack[]}
*/
removeRemoteTracks(owner) {
let removedTracks = [];
for (const tpc of this.peerConnections.values()) {
const pcRemovedTracks = tpc.removeRemoteTracks(owner);
removedTracks = removedTracks.concat(pcRemovedTracks);
}
logger.debug(
`Removed remote tracks for ${owner}`
+ ` count: ${removedTracks.length}`);
return removedTracks;
}
/**
*
*/
static getPCConstraints(isP2P) {
const pcConstraints
= isP2P ? RTCUtils.p2pPcConstraints : RTCUtils.pcConstraints;
if (!pcConstraints) {
return {};
}
return JSON.parse(JSON.stringify(pcConstraints));
}
/**
*
* @param elSelector
* @param stream
*/
static attachMediaStream(elSelector, stream) {
return RTCUtils.attachMediaStream(elSelector, stream);
}
/**
* Returns the id of the given stream.
* @param {MediaStream} stream
*/
static getStreamID(stream) {
return RTCUtils.getStreamID(stream);
}
/**
* Returns the id of the given track.
* @param {MediaStreamTrack} track
*/
static getTrackID(track) {
return RTCUtils.getTrackID(track);
}
/**
* Returns true if retrieving the the list of input devices is supported
* and false if not.
*/
static isDeviceListAvailable() {
return RTCUtils.isDeviceListAvailable();
}
/**
* Returns true if changing the input (camera / microphone) or output
* (audio) device is supported and false if not.
* @param {string} [deviceType] Type of device to change. Default is
* undefined or 'input', 'output' - for audio output device change.
* @returns {boolean} true if available, false otherwise.
*/
static isDeviceChangeAvailable(deviceType) {
return RTCUtils.isDeviceChangeAvailable(deviceType);
}
/**
* Returns whether the current execution environment supports WebRTC (for
* use within this library).
*
* @returns {boolean} {@code true} if WebRTC is supported in the current
* execution environment (for use within this library); {@code false},
* otherwise.
*/
static isWebRtcSupported() {
return browser.isSupported();
}
/**
* Returns currently used audio output device id, '' stands for default
* device
* @returns {string}
*/
static getAudioOutputDevice() {
return RTCUtils.getAudioOutputDevice();
}
/**
* Returns list of available media devices if its obtained, otherwise an
* empty array is returned/
* @returns {array} list of available media devices.
*/
static getCurrentlyAvailableMediaDevices() {
return RTCUtils.getCurrentlyAvailableMediaDevices();
}
/**
* Returns event data for device to be reported to stats.
* @returns {MediaDeviceInfo} device.
*/
static getEventDataForActiveDevice(device) {
return RTCUtils.getEventDataForActiveDevice(device);
}
/**
* Sets current audio output device.
* @param {string} deviceId Id of 'audiooutput' device from
* navigator.mediaDevices.enumerateDevices().
* @returns {Promise} resolves when audio output is changed, is rejected
* otherwise
*/
static setAudioOutputDevice(deviceId) {
return RTCUtils.setAudioOutputDevice(deviceId);
}
/**
* Returns <tt>true<tt/> if given WebRTC MediaStream is considered a valid
* "user" stream which means that it's not a "receive only" stream nor a
* "mixed" JVB stream.
*
* Clients that implement Unified Plan, such as Firefox use recvonly
* "streams/channels/tracks" for receiving remote stream/tracks, as opposed
* to Plan B where there are only 3 channels: audio, video and data.
*
* @param {MediaStream} stream The WebRTC MediaStream instance.
* @returns {boolean}
*/
static isUserStream(stream) {
return RTC.isUserStreamById(RTCUtils.getStreamID(stream));
}
/**
* Returns <tt>true<tt/> if a WebRTC MediaStream identified by given stream
* ID is considered a valid "user" stream which means that it's not a
* "receive only" stream nor a "mixed" JVB stream.
*
* Clients that implement Unified Plan, such as Firefox use recvonly
* "streams/channels/tracks" for receiving remote stream/tracks, as opposed
* to Plan B where there are only 3 channels: audio, video and data.
*
* @param {string} streamId The id of WebRTC MediaStream.
* @returns {boolean}
*/
static isUserStreamById(streamId) {
return streamId && streamId !== 'mixedmslabel'
&& streamId !== 'default';
}
/**
* Allows to receive list of available cameras/microphones.
* @param {function} callback Would receive array of devices as an
* argument.
*/
static enumerateDevices(callback) {
RTCUtils.enumerateDevices(callback);
}
/**
* A method to handle stopping of the stream.
* One point to handle the differences in various implementations.
* @param {MediaStream} mediaStream MediaStream object to stop.
*/
static stopMediaStream(mediaStream) {
RTCUtils.stopMediaStream(mediaStream);
}
/**
* Returns whether the desktop sharing is enabled or not.
* @returns {boolean}
*/
static isDesktopSharingEnabled() {
return RTCUtils.isDesktopSharingEnabled();
}
/**
* Closes the currently opened bridge channel.
*/
closeBridgeChannel() {
if (this._channel) {
this._channel.close();
this._channelOpen = false;
this.removeListener(RTCEvents.LASTN_ENDPOINT_CHANGED,
this._lastNChangeListener);
}
}
/* eslint-disable max-params */
/**
*
* @param {TraceablePeerConnection} tpc
* @param {number} ssrc
* @param {number} audioLevel
* @param {boolean} isLocal
*/
setAudioLevel(tpc, ssrc, audioLevel, isLocal) {
const track = tpc.getTrackBySSRC(ssrc);
if (!track) {
return;
} else if (!track.isAudioTrack()) {
logger.warn(`Received audio level for non-audio track: ${ssrc}`);
return;
} else if (track.isLocal() !== isLocal) {
logger.error(
`${track} was expected to ${isLocal ? 'be' : 'not be'} local`);
}
track.setAudioLevel(audioLevel, tpc);
}
/* eslint-enable max-params */
/**
* Sends message via the bridge channel.
* @param {string} to The id of the endpoint that should receive the
* message. If "" the message will be sent to all participants.
* @param {object} payload The payload of the message.
* @throws NetworkError or InvalidStateError or Error if the operation
* fails or there is no data channel created.
*/
sendChannelMessage(to, payload) {
if (this._channel) {
this._channel.sendMessage(to, payload);
} else {
throw new Error('Channel support is disabled!');
}
}
/**
* Selects a new value for "lastN". The requested amount of videos are going
* to be delivered after the value is in effect. Set to -1 for unlimited or
* all available videos.
* @param {number} value the new value for lastN.
*/
setLastN(value) {
if (this._lastN !== value) {
this._lastN = value;
if (this._channel && this._channelOpen) {
this._channel.sendSetLastNMessage(value);
}
this.eventEmitter.emit(RTCEvents.LASTN_VALUE_CHANGED, value);
}
}
/**
* Indicates if the endpoint id is currently included in the last N.
* @param {string} id The endpoint id that we check for last N.
* @returns {boolean} true if the endpoint id is in the last N or if we
* don't have bridge channel support, otherwise we return false.
*/
isInLastN(id) {
return !this._lastNEndpoints // lastNEndpoints not initialised yet.
|| this._lastNEndpoints.indexOf(id) > -1;
}
/**
* Updates the target audio output device for all remote audio tracks.
*
* @param {string} deviceId - The device id of the audio ouput device to
* use for all remote tracks.
* @private
* @returns {void}
*/
_updateAudioOutputForAudioTracks(deviceId) {
const remoteAudioTracks = this.getRemoteTracks(MediaType.AUDIO);
for (const track of remoteAudioTracks) {
track.setAudioOutput(deviceId);
}
}
}