@dtelecom/livekit-client 1.12.12 → 1.12.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14347,7 +14347,7 @@ function getMatch(exp, ua) {
14347
14347
  return match && match.length >= id && match[id] || '';
14348
14348
  }
14349
14349
 
14350
- var version$1 = "1.12.11";
14350
+ var version$1 = "1.12.13";
14351
14351
 
14352
14352
  const version = version$1;
14353
14353
  const protocolVersion = 9;
@@ -15431,29 +15431,6 @@ function supportsSetSinkId(elm) {
15431
15431
  }
15432
15432
  return 'setSinkId' in elm;
15433
15433
  }
15434
- const setCodecPreferencesVersions = {
15435
- Chrome: '100',
15436
- Safari: '15',
15437
- Firefox: '100'
15438
- };
15439
- function supportsSetCodecPreferences(transceiver) {
15440
- if (!isWeb()) {
15441
- return false;
15442
- }
15443
- if (!('setCodecPreferences' in transceiver)) {
15444
- return false;
15445
- }
15446
- const browser = getBrowser();
15447
- if (!(browser === null || browser === void 0 ? void 0 : browser.name) || !browser.version) {
15448
- // version is required
15449
- return false;
15450
- }
15451
- const v = setCodecPreferencesVersions[browser.name];
15452
- if (v) {
15453
- return compareVersions(browser.version, v) >= 0;
15454
- }
15455
- return false;
15456
- }
15457
15434
  function isBrowserSupported() {
15458
15435
  return supportsTransceiver() || supportsAddTrack();
15459
15436
  }
@@ -18459,2039 +18436,2000 @@ const roomConnectOptionDefaults = {
18459
18436
  peerConnectionTimeout: 15000
18460
18437
  };
18461
18438
 
18462
- const lossyDataChannel = '_lossy';
18463
- const reliableDataChannel = '_reliable';
18464
- const minReconnectWait = 2 * 1000;
18465
- const leaveReconnect = 'leave-reconnect';
18466
- var PCState;
18467
- (function (PCState) {
18468
- PCState[PCState["New"] = 0] = "New";
18469
- PCState[PCState["Connected"] = 1] = "Connected";
18470
- PCState[PCState["Disconnected"] = 2] = "Disconnected";
18471
- PCState[PCState["Reconnecting"] = 3] = "Reconnecting";
18472
- PCState[PCState["Closed"] = 4] = "Closed";
18473
- })(PCState || (PCState = {}));
18474
- /** @internal */
18475
- class RTCEngine extends EventEmitter {
18476
- get isClosed() {
18477
- return this._isClosed;
18439
+ const monitorFrequency = 2000;
18440
+ function computeBitrate(currentStats, prevStats) {
18441
+ if (!prevStats) {
18442
+ return 0;
18478
18443
  }
18479
- constructor(options) {
18480
- super();
18481
- this.options = options;
18482
- this.rtcConfig = {};
18483
- this.peerConnectionTimeout = roomConnectOptionDefaults.peerConnectionTimeout;
18484
- this.fullReconnectOnNext = false;
18485
- this.subscriberPrimary = false;
18486
- this.pcState = PCState.New;
18487
- this._isClosed = true;
18488
- this.pendingTrackResolvers = {};
18489
- // true if publisher connection has already been established.
18490
- // this is helpful to know if we need to restart ICE on the publisher connection
18491
- this.hasPublished = false;
18492
- this.reconnectAttempts = 0;
18493
- this.reconnectStart = 0;
18494
- this.attemptingReconnect = false;
18495
- /** keeps track of how often an initial join connection has been tried */
18496
- this.joinAttempts = 0;
18497
- /** specifies how often an initial join connection is allowed to retry */
18498
- this.maxJoinAttempts = 1;
18499
- this.shouldFailNext = false;
18500
- this.handleDataChannel = _ref => {
18501
- let {
18502
- channel
18503
- } = _ref;
18504
- return __awaiter(this, void 0, void 0, function* () {
18505
- if (!channel) {
18506
- return;
18507
- }
18508
- if (channel.label === reliableDataChannel) {
18509
- this.reliableDCSub = channel;
18510
- } else if (channel.label === lossyDataChannel) {
18511
- this.lossyDCSub = channel;
18512
- } else {
18513
- return;
18514
- }
18515
- livekitLogger.debug("on data channel ".concat(channel.id, ", ").concat(channel.label));
18516
- channel.onmessage = this.handleDataMessage;
18517
- });
18518
- };
18519
- this.handleDataMessage = message => __awaiter(this, void 0, void 0, function* () {
18520
- var _a, _b;
18521
- // make sure to respect incoming data message order by processing message events one after the other
18522
- const unlock = yield this.dataProcessLock.lock();
18523
- try {
18524
- // decode
18525
- let buffer;
18526
- if (message.data instanceof ArrayBuffer) {
18527
- buffer = message.data;
18528
- } else if (message.data instanceof Blob) {
18529
- buffer = yield message.data.arrayBuffer();
18530
- } else {
18531
- livekitLogger.error('unsupported data type', message.data);
18532
- return;
18533
- }
18534
- const dp = DataPacket.decode(new Uint8Array(buffer));
18535
- if (((_a = dp.value) === null || _a === void 0 ? void 0 : _a.$case) === 'speaker') {
18536
- // dispatch speaker updates
18537
- this.emit(EngineEvent.ActiveSpeakersUpdate, dp.value.speaker.speakers);
18538
- } else if (((_b = dp.value) === null || _b === void 0 ? void 0 : _b.$case) === 'user') {
18539
- this.emit(EngineEvent.DataPacketReceived, dp.value.user, dp.kind);
18540
- }
18541
- } finally {
18542
- unlock();
18543
- }
18544
- });
18545
- this.handleDataError = event => {
18546
- const channel = event.currentTarget;
18547
- const channelKind = channel.maxRetransmits === 0 ? 'lossy' : 'reliable';
18548
- if (event instanceof ErrorEvent) {
18549
- const {
18550
- error
18551
- } = event.error;
18552
- livekitLogger.error("DataChannel error on ".concat(channelKind, ": ").concat(event.message), error);
18553
- } else {
18554
- livekitLogger.error("Unknown DataChannel Error on ".concat(channelKind), event);
18555
- }
18556
- };
18557
- this.handleBufferedAmountLow = event => {
18558
- const channel = event.currentTarget;
18559
- const channelKind = channel.maxRetransmits === 0 ? DataPacket_Kind.LOSSY : DataPacket_Kind.RELIABLE;
18560
- this.updateAndEmitDCBufferStatus(channelKind);
18561
- };
18562
- // websocket reconnect behavior. if websocket is interrupted, and the PeerConnection
18563
- // continues to work, we can reconnect to websocket to continue the session
18564
- // after a number of retries, we'll close and give up permanently
18565
- this.handleDisconnect = (connection, disconnectReason) => {
18566
- if (this._isClosed) {
18444
+ let bytesNow;
18445
+ let bytesPrev;
18446
+ if ('bytesReceived' in currentStats) {
18447
+ bytesNow = currentStats.bytesReceived;
18448
+ bytesPrev = prevStats.bytesReceived;
18449
+ } else if ('bytesSent' in currentStats) {
18450
+ bytesNow = currentStats.bytesSent;
18451
+ bytesPrev = prevStats.bytesSent;
18452
+ }
18453
+ if (bytesNow === undefined || bytesPrev === undefined || currentStats.timestamp === undefined || prevStats.timestamp === undefined) {
18454
+ return 0;
18455
+ }
18456
+ return (bytesNow - bytesPrev) * 8 * 1000 / (currentStats.timestamp - prevStats.timestamp);
18457
+ }
18458
+
18459
+ class LocalAudioTrack extends LocalTrack {
18460
+ /**
18461
+ *
18462
+ * @param mediaTrack
18463
+ * @param constraints MediaTrackConstraints that are being used when restarting or reacquiring tracks
18464
+ * @param userProvidedTrack Signals to the SDK whether or not the mediaTrack should be managed (i.e. released and reacquired) internally by the SDK
18465
+ */
18466
+ constructor(mediaTrack, constraints) {
18467
+ let userProvidedTrack = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
18468
+ super(mediaTrack, Track.Kind.Audio, constraints, userProvidedTrack);
18469
+ /** @internal */
18470
+ this.stopOnMute = false;
18471
+ this.monitorSender = () => __awaiter(this, void 0, void 0, function* () {
18472
+ if (!this.sender) {
18473
+ this._currentBitrate = 0;
18567
18474
  return;
18568
18475
  }
18569
- livekitLogger.warn("".concat(connection, " disconnected"));
18570
- if (this.reconnectAttempts === 0) {
18571
- // only reset start time on the first try
18572
- this.reconnectStart = Date.now();
18573
- }
18574
- const disconnect = duration => {
18575
- livekitLogger.warn("could not recover connection after ".concat(this.reconnectAttempts, " attempts, ").concat(duration, "ms. giving up"));
18576
- this.emit(EngineEvent.Disconnected);
18577
- this.close();
18578
- };
18579
- const duration = Date.now() - this.reconnectStart;
18580
- let delay = this.getNextRetryDelay({
18581
- elapsedMs: duration,
18582
- retryCount: this.reconnectAttempts
18583
- });
18584
- if (delay === null) {
18585
- disconnect(duration);
18476
+ let stats;
18477
+ try {
18478
+ stats = yield this.getSenderStats();
18479
+ } catch (e) {
18480
+ livekitLogger.error('could not get audio sender stats', {
18481
+ error: e
18482
+ });
18586
18483
  return;
18587
18484
  }
18588
- if (connection === leaveReconnect) {
18589
- delay = 0;
18590
- }
18591
- livekitLogger.debug("reconnecting in ".concat(delay, "ms"));
18592
- this.clearReconnectTimeout();
18593
- if (this.url && this.token && isCloud(new URL(this.url))) {
18594
- this.regionUrlProvider = new RegionUrlProvider(this.url, this.token);
18595
- }
18596
- this.reconnectTimeout = CriticalTimers.setTimeout(() => this.attemptReconnect(disconnectReason), delay);
18597
- };
18598
- this.waitForRestarted = () => {
18599
- return new Promise((resolve, reject) => {
18600
- if (this.pcState === PCState.Connected) {
18601
- resolve();
18602
- }
18603
- const onRestarted = () => {
18604
- this.off(EngineEvent.Disconnected, onDisconnected);
18605
- resolve();
18606
- };
18607
- const onDisconnected = () => {
18608
- this.off(EngineEvent.Restarted, onRestarted);
18609
- reject();
18610
- };
18611
- this.once(EngineEvent.Restarted, onRestarted);
18612
- this.once(EngineEvent.Disconnected, onDisconnected);
18613
- this.once(EngineEvent.Closing, onDisconnected);
18614
- });
18615
- };
18616
- this.updateAndEmitDCBufferStatus = kind => {
18617
- const status = this.isBufferStatusLow(kind);
18618
- if (typeof status !== 'undefined' && status !== this.dcBufferStatus.get(kind)) {
18619
- this.dcBufferStatus.set(kind, status);
18620
- this.emit(EngineEvent.DCBufferStatusChanged, status, kind);
18485
+ if (stats && this.prevStats) {
18486
+ this._currentBitrate = computeBitrate(stats, this.prevStats);
18621
18487
  }
18622
- };
18623
- this.isBufferStatusLow = kind => {
18624
- const dc = this.dataChannelForKind(kind);
18625
- if (dc) {
18626
- return dc.bufferedAmount <= dc.bufferedAmountLowThreshold;
18488
+ this.prevStats = stats;
18489
+ });
18490
+ this.checkForSilence();
18491
+ }
18492
+ setDeviceId(deviceId) {
18493
+ return __awaiter(this, void 0, void 0, function* () {
18494
+ if (this._constraints.deviceId === deviceId) {
18495
+ return true;
18627
18496
  }
18628
- };
18629
- this.handleBrowserOnLine = () => {
18630
- // in case the engine is currently reconnecting, attempt a reconnect immediately after the browser state has changed to 'onLine'
18631
- if (this.client.isReconnecting) {
18632
- this.clearReconnectTimeout();
18633
- this.attemptReconnect(ReconnectReason.RR_SIGNAL_DISCONNECTED);
18497
+ this._constraints.deviceId = deviceId;
18498
+ if (!this.isMuted) {
18499
+ yield this.restartTrack();
18634
18500
  }
18635
- };
18636
- this.client = new SignalClient();
18637
- this.client.signalLatency = this.options.expSignalLatency;
18638
- this.reconnectPolicy = this.options.reconnectPolicy;
18639
- this.registerOnLineListener();
18640
- this.closingLock = new Mutex();
18641
- this.dataProcessLock = new Mutex();
18642
- this.dcBufferStatus = new Map([[DataPacket_Kind.LOSSY, true], [DataPacket_Kind.RELIABLE, true]]);
18643
- this.client.onParticipantUpdate = updates => this.emit(EngineEvent.ParticipantUpdate, updates);
18644
- this.client.onConnectionQuality = update => this.emit(EngineEvent.ConnectionQualityUpdate, update);
18645
- this.client.onRoomUpdate = update => this.emit(EngineEvent.RoomUpdate, update);
18646
- this.client.onSubscriptionError = resp => this.emit(EngineEvent.SubscriptionError, resp);
18647
- this.client.onSubscriptionPermissionUpdate = update => this.emit(EngineEvent.SubscriptionPermissionUpdate, update);
18648
- this.client.onSpeakersChanged = update => this.emit(EngineEvent.SpeakersChanged, update);
18649
- this.client.onStreamStateUpdate = update => this.emit(EngineEvent.StreamStateChanged, update);
18501
+ return this.isMuted || unwrapConstraint(deviceId) === this.mediaStreamTrack.getSettings().deviceId;
18502
+ });
18650
18503
  }
18651
- join(url, token, opts, abortSignal) {
18504
+ mute() {
18505
+ const _super = Object.create(null, {
18506
+ mute: {
18507
+ get: () => super.mute
18508
+ }
18509
+ });
18652
18510
  return __awaiter(this, void 0, void 0, function* () {
18653
- this.url = url;
18654
- this.token = token;
18655
- this.signalOpts = opts;
18511
+ const unlock = yield this.muteLock.lock();
18656
18512
  try {
18657
- this.joinAttempts += 1;
18658
- const joinResponse = yield this.client.join(url, token, opts, abortSignal);
18659
- this._isClosed = false;
18660
- this.latestJoinResponse = joinResponse;
18661
- this.subscriberPrimary = joinResponse.subscriberPrimary;
18662
- if (!this.publisher) {
18663
- this.configure(joinResponse);
18664
- }
18665
- // create offer
18666
- if (!this.subscriberPrimary) {
18667
- this.negotiate();
18668
- }
18669
- this.setupSignalClientCallbacks();
18670
- this.clientConfiguration = joinResponse.clientConfiguration;
18671
- return joinResponse;
18672
- } catch (e) {
18673
- if (e instanceof ConnectionError) {
18674
- if (e.reason === 1 /* ConnectionErrorReason.ServerUnreachable */) {
18675
- livekitLogger.warn("Couldn't connect to server, attempt ".concat(this.joinAttempts, " of ").concat(this.maxJoinAttempts));
18676
- if (this.joinAttempts < this.maxJoinAttempts) {
18677
- return this.join(url, token, opts, abortSignal);
18678
- }
18679
- }
18513
+ // disabled special handling as it will cause BT headsets to switch communication modes
18514
+ if (this.source === Track.Source.Microphone && this.stopOnMute && !this.isUserProvided) {
18515
+ livekitLogger.debug('stopping mic track');
18516
+ // also stop the track, so that microphone indicator is turned off
18517
+ this._mediaStreamTrack.stop();
18680
18518
  }
18681
- throw e;
18519
+ yield _super.mute.call(this);
18520
+ return this;
18521
+ } finally {
18522
+ unlock();
18682
18523
  }
18683
18524
  });
18684
18525
  }
18685
- close() {
18686
- return __awaiter(this, void 0, void 0, function* () {
18687
- const unlock = yield this.closingLock.lock();
18688
- if (this.isClosed) {
18689
- unlock();
18690
- return;
18526
+ unmute() {
18527
+ const _super = Object.create(null, {
18528
+ unmute: {
18529
+ get: () => super.unmute
18691
18530
  }
18531
+ });
18532
+ return __awaiter(this, void 0, void 0, function* () {
18533
+ const unlock = yield this.muteLock.lock();
18692
18534
  try {
18693
- this._isClosed = true;
18694
- this.emit(EngineEvent.Closing);
18695
- this.removeAllListeners();
18696
- this.deregisterOnLineListener();
18697
- this.clearPendingReconnect();
18698
- yield this.cleanupPeerConnections();
18699
- yield this.cleanupClient();
18535
+ const deviceHasChanged = this._constraints.deviceId && this._mediaStreamTrack.getSettings().deviceId !== unwrapConstraint(this._constraints.deviceId);
18536
+ if (this.source === Track.Source.Microphone && (this.stopOnMute || this._mediaStreamTrack.readyState === 'ended' || deviceHasChanged) && !this.isUserProvided) {
18537
+ livekitLogger.debug('reacquiring mic track');
18538
+ yield this.restartTrack();
18539
+ }
18540
+ yield _super.unmute.call(this);
18541
+ return this;
18700
18542
  } finally {
18701
18543
  unlock();
18702
18544
  }
18703
18545
  });
18704
18546
  }
18705
- cleanupPeerConnections() {
18547
+ restartTrack(options) {
18706
18548
  return __awaiter(this, void 0, void 0, function* () {
18707
- if (this.publisher && this.publisher.pc.signalingState !== 'closed') {
18708
- this.publisher.pc.getSenders().forEach(sender => {
18709
- var _a, _b;
18710
- try {
18711
- // TODO: react-native-webrtc doesn't have removeTrack yet.
18712
- if ((_a = this.publisher) === null || _a === void 0 ? void 0 : _a.pc.removeTrack) {
18713
- (_b = this.publisher) === null || _b === void 0 ? void 0 : _b.pc.removeTrack(sender);
18714
- }
18715
- } catch (e) {
18716
- livekitLogger.warn('could not removeTrack', {
18717
- error: e
18718
- });
18719
- }
18549
+ let constraints;
18550
+ if (options) {
18551
+ const streamConstraints = constraintsForOptions({
18552
+ audio: options
18720
18553
  });
18554
+ if (typeof streamConstraints.audio !== 'boolean') {
18555
+ constraints = streamConstraints.audio;
18556
+ }
18721
18557
  }
18722
- if (this.publisher) {
18723
- this.publisher.close();
18724
- this.publisher = undefined;
18725
- }
18726
- if (this.subscriber) {
18727
- this.subscriber.close();
18728
- this.subscriber = undefined;
18729
- }
18730
- this.primaryPC = undefined;
18731
- const dcCleanup = dc => {
18732
- if (!dc) return;
18733
- dc.close();
18734
- dc.onbufferedamountlow = null;
18735
- dc.onclose = null;
18736
- dc.onclosing = null;
18737
- dc.onerror = null;
18738
- dc.onmessage = null;
18739
- dc.onopen = null;
18740
- };
18741
- dcCleanup(this.lossyDC);
18742
- dcCleanup(this.lossyDCSub);
18743
- dcCleanup(this.reliableDC);
18744
- dcCleanup(this.reliableDCSub);
18745
- this.lossyDC = undefined;
18746
- this.lossyDCSub = undefined;
18747
- this.reliableDC = undefined;
18748
- this.reliableDCSub = undefined;
18558
+ yield this.restart(constraints);
18749
18559
  });
18750
18560
  }
18751
- cleanupClient() {
18561
+ restart(constraints) {
18562
+ const _super = Object.create(null, {
18563
+ restart: {
18564
+ get: () => super.restart
18565
+ }
18566
+ });
18752
18567
  return __awaiter(this, void 0, void 0, function* () {
18753
- yield this.client.close();
18754
- this.client.resetCallbacks();
18568
+ const track = yield _super.restart.call(this, constraints);
18569
+ this.checkForSilence();
18570
+ return track;
18755
18571
  });
18756
18572
  }
18757
- addTrack(req) {
18758
- if (this.pendingTrackResolvers[req.cid]) {
18759
- throw new TrackInvalidError('a track with the same ID has already been published');
18573
+ /* @internal */
18574
+ startMonitor() {
18575
+ if (!isWeb()) {
18576
+ return;
18760
18577
  }
18761
- return new Promise((resolve, reject) => {
18762
- const publicationTimeout = setTimeout(() => {
18763
- delete this.pendingTrackResolvers[req.cid];
18764
- reject(new ConnectionError('publication of local track timed out, no response from server'));
18765
- }, 10000);
18766
- this.pendingTrackResolvers[req.cid] = {
18767
- resolve: info => {
18768
- clearTimeout(publicationTimeout);
18769
- resolve(info);
18770
- },
18771
- reject: () => {
18772
- clearTimeout(publicationTimeout);
18773
- reject(new Error('Cancelled publication by calling unpublish'));
18774
- }
18775
- };
18776
- this.client.sendAddTrack(req);
18777
- });
18578
+ if (this.monitorInterval) {
18579
+ return;
18580
+ }
18581
+ this.monitorInterval = setInterval(() => {
18582
+ this.monitorSender();
18583
+ }, monitorFrequency);
18778
18584
  }
18779
- /**
18780
- * Removes sender from PeerConnection, returning true if it was removed successfully
18781
- * and a negotiation is necessary
18782
- * @param sender
18783
- * @returns
18784
- */
18785
- removeTrack(sender) {
18585
+ getSenderStats() {
18786
18586
  var _a;
18787
- if (sender.track && this.pendingTrackResolvers[sender.track.id]) {
18788
- const {
18789
- reject
18790
- } = this.pendingTrackResolvers[sender.track.id];
18791
- if (reject) {
18792
- reject();
18587
+ return __awaiter(this, void 0, void 0, function* () {
18588
+ if (!((_a = this.sender) === null || _a === void 0 ? void 0 : _a.getStats)) {
18589
+ return undefined;
18793
18590
  }
18794
- delete this.pendingTrackResolvers[sender.track.id];
18795
- }
18796
- try {
18797
- (_a = this.publisher) === null || _a === void 0 ? void 0 : _a.pc.removeTrack(sender);
18798
- return true;
18799
- } catch (e) {
18800
- livekitLogger.warn('failed to remove track', {
18801
- error: e,
18802
- method: 'removeTrack'
18591
+ const stats = yield this.sender.getStats();
18592
+ let audioStats;
18593
+ stats.forEach(v => {
18594
+ if (v.type === 'outbound-rtp') {
18595
+ audioStats = {
18596
+ type: 'audio',
18597
+ streamId: v.id,
18598
+ packetsSent: v.packetsSent,
18599
+ packetsLost: v.packetsLost,
18600
+ bytesSent: v.bytesSent,
18601
+ timestamp: v.timestamp,
18602
+ roundTripTime: v.roundTripTime,
18603
+ jitter: v.jitter
18604
+ };
18605
+ }
18803
18606
  });
18804
- }
18805
- return false;
18806
- }
18807
- updateMuteStatus(trackSid, muted) {
18808
- this.client.sendMuteTrack(trackSid, muted);
18809
- }
18810
- get dataSubscriberReadyState() {
18811
- var _a;
18812
- return (_a = this.reliableDCSub) === null || _a === void 0 ? void 0 : _a.readyState;
18607
+ return audioStats;
18608
+ });
18813
18609
  }
18814
- getConnectedServerAddress() {
18610
+ checkForSilence() {
18815
18611
  return __awaiter(this, void 0, void 0, function* () {
18816
- if (this.primaryPC === undefined) {
18817
- return undefined;
18612
+ const trackIsSilent = yield detectSilence(this);
18613
+ if (trackIsSilent) {
18614
+ if (!this.isMuted) {
18615
+ livekitLogger.warn('silence detected on local audio track');
18616
+ }
18617
+ this.emit(TrackEvent.AudioSilenceDetected);
18818
18618
  }
18819
- return getConnectedAddress(this.primaryPC);
18619
+ return trackIsSilent;
18820
18620
  });
18821
18621
  }
18822
- configure(joinResponse) {
18823
- var _a, _b;
18824
- // already configured
18825
- if (this.publisher || this.subscriber) {
18826
- return;
18827
- }
18828
- this.participantSid = (_a = joinResponse.participant) === null || _a === void 0 ? void 0 : _a.sid;
18829
- const rtcConfig = this.makeRTCConfiguration(joinResponse);
18830
- if ((_b = this.signalOpts) === null || _b === void 0 ? void 0 : _b.e2eeEnabled) {
18831
- livekitLogger.debug('E2EE - setting up transports with insertable streams');
18832
- // this makes sure that no data is sent before the transforms are ready
18833
- // @ts-ignore
18834
- rtcConfig.encodedInsertableStreams = true;
18835
- }
18836
- const googConstraints = {
18837
- optional: [{
18838
- googDscp: true
18839
- }]
18840
- };
18841
- this.publisher = new PCTransport(rtcConfig, googConstraints);
18842
- this.subscriber = new PCTransport(rtcConfig);
18843
- this.emit(EngineEvent.TransportsCreated, this.publisher, this.subscriber);
18844
- this.publisher.pc.onicecandidate = ev => {
18845
- if (!ev.candidate) return;
18846
- livekitLogger.trace('adding ICE candidate for peer', ev.candidate);
18847
- this.client.sendIceCandidate(ev.candidate, SignalTarget.PUBLISHER);
18848
- };
18849
- this.subscriber.pc.onicecandidate = ev => {
18850
- if (!ev.candidate) return;
18851
- this.client.sendIceCandidate(ev.candidate, SignalTarget.SUBSCRIBER);
18852
- };
18853
- this.publisher.onOffer = offer => {
18854
- this.client.sendOffer(offer);
18855
- };
18856
- let primaryPC = this.publisher.pc;
18857
- let secondaryPC = this.subscriber.pc;
18858
- let subscriberPrimary = joinResponse.subscriberPrimary;
18859
- if (subscriberPrimary) {
18860
- primaryPC = this.subscriber.pc;
18861
- secondaryPC = this.publisher.pc;
18862
- // in subscriber primary mode, server side opens sub data channels.
18863
- this.subscriber.pc.ondatachannel = this.handleDataChannel;
18864
- }
18865
- this.primaryPC = primaryPC;
18866
- primaryPC.onconnectionstatechange = () => __awaiter(this, void 0, void 0, function* () {
18867
- livekitLogger.debug("primary PC state changed ".concat(primaryPC.connectionState));
18868
- if (primaryPC.connectionState === 'connected') {
18869
- const shouldEmit = this.pcState === PCState.New;
18870
- this.pcState = PCState.Connected;
18871
- if (shouldEmit) {
18872
- this.emit(EngineEvent.Connected, joinResponse);
18873
- }
18874
- } else if (primaryPC.connectionState === 'failed') {
18875
- // on Safari, PeerConnection will switch to 'disconnected' during renegotiation
18876
- if (this.pcState === PCState.Connected) {
18877
- this.pcState = PCState.Disconnected;
18878
- this.handleDisconnect('primary peerconnection', subscriberPrimary ? ReconnectReason.RR_SUBSCRIBER_FAILED : ReconnectReason.RR_PUBLISHER_FAILED);
18879
- }
18880
- }
18881
- });
18882
- secondaryPC.onconnectionstatechange = () => __awaiter(this, void 0, void 0, function* () {
18883
- livekitLogger.debug("secondary PC state changed ".concat(secondaryPC.connectionState));
18884
- // also reconnect if secondary peerconnection fails
18885
- if (secondaryPC.connectionState === 'failed') {
18886
- this.handleDisconnect('secondary peerconnection', subscriberPrimary ? ReconnectReason.RR_PUBLISHER_FAILED : ReconnectReason.RR_SUBSCRIBER_FAILED);
18887
- }
18888
- });
18889
- this.subscriber.pc.ontrack = ev => {
18890
- this.emit(EngineEvent.MediaTrackAdded, ev.track, ev.streams[0], ev.receiver);
18891
- };
18892
- this.createDataChannels();
18622
+ }
18623
+
18624
+ /** @internal */
18625
+ function mediaTrackToLocalTrack(mediaStreamTrack, constraints) {
18626
+ switch (mediaStreamTrack.kind) {
18627
+ case 'audio':
18628
+ return new LocalAudioTrack(mediaStreamTrack, constraints, false);
18629
+ case 'video':
18630
+ return new LocalVideoTrack(mediaStreamTrack, constraints, false);
18631
+ default:
18632
+ throw new TrackInvalidError("unsupported track type: ".concat(mediaStreamTrack.kind));
18893
18633
  }
18894
- setupSignalClientCallbacks() {
18895
- // configure signaling client
18896
- this.client.onAnswer = sd => __awaiter(this, void 0, void 0, function* () {
18897
- if (!this.publisher) {
18898
- return;
18899
- }
18900
- livekitLogger.debug('received server answer', {
18901
- RTCSdpType: sd.type,
18902
- signalingState: this.publisher.pc.signalingState.toString()
18903
- });
18904
- yield this.publisher.setRemoteDescription(sd);
18905
- });
18906
- // add candidate on trickle
18907
- this.client.onTrickle = (candidate, target) => {
18908
- if (!this.publisher || !this.subscriber) {
18909
- return;
18910
- }
18911
- livekitLogger.trace('got ICE candidate from peer', {
18912
- candidate,
18913
- target
18914
- });
18915
- if (target === SignalTarget.PUBLISHER) {
18916
- this.publisher.addIceCandidate(candidate);
18917
- } else {
18918
- this.subscriber.addIceCandidate(candidate);
18919
- }
18920
- };
18921
- // when server creates an offer for the client
18922
- this.client.onOffer = sd => __awaiter(this, void 0, void 0, function* () {
18923
- if (!this.subscriber) {
18924
- return;
18925
- }
18926
- livekitLogger.debug('received server offer', {
18927
- RTCSdpType: sd.type,
18928
- signalingState: this.subscriber.pc.signalingState.toString()
18929
- });
18930
- yield this.subscriber.setRemoteDescription(sd);
18931
- // answer the offer
18932
- const answer = yield this.subscriber.createAndSetAnswer();
18933
- this.client.sendAnswer(answer);
18934
- });
18935
- this.client.onLocalTrackPublished = res => {
18936
- livekitLogger.debug('received trackPublishedResponse', res);
18937
- const {
18938
- resolve
18939
- } = this.pendingTrackResolvers[res.cid];
18940
- if (!resolve) {
18941
- livekitLogger.error("missing track resolver for ".concat(res.cid));
18942
- return;
18943
- }
18944
- delete this.pendingTrackResolvers[res.cid];
18945
- resolve(res.track);
18946
- };
18947
- this.client.onTokenRefresh = token => {
18948
- this.token = token;
18949
- };
18950
- this.client.onClose = () => {
18951
- this.handleDisconnect('signal', ReconnectReason.RR_SIGNAL_DISCONNECTED);
18952
- };
18953
- this.client.onLeave = leave => {
18954
- if (leave === null || leave === void 0 ? void 0 : leave.canReconnect) {
18955
- this.fullReconnectOnNext = true;
18956
- this.primaryPC = undefined;
18957
- // reconnect immediately instead of waiting for next attempt
18958
- this.handleDisconnect(leaveReconnect);
18959
- } else {
18960
- this.emit(EngineEvent.Disconnected, leave === null || leave === void 0 ? void 0 : leave.reason);
18961
- this.close();
18962
- }
18963
- livekitLogger.trace('leave request', {
18964
- leave
18965
- });
18966
- };
18634
+ }
18635
+ /* @internal */
18636
+ const presets169 = Object.values(VideoPresets);
18637
+ /* @internal */
18638
+ const presets43 = Object.values(VideoPresets43);
18639
+ /* @internal */
18640
+ const presetsScreenShare = Object.values(ScreenSharePresets);
18641
+ /* @internal */
18642
+ const defaultSimulcastPresets169 = [VideoPresets.h180, VideoPresets.h360];
18643
+ /* @internal */
18644
+ const defaultSimulcastPresets43 = [VideoPresets43.h180, VideoPresets43.h360];
18645
+ /* @internal */
18646
+ const computeDefaultScreenShareSimulcastPresets = fromPreset => {
18647
+ const layers = [{
18648
+ scaleResolutionDownBy: 2,
18649
+ fps: 3
18650
+ }];
18651
+ return layers.map(t => {
18652
+ var _a;
18653
+ return new VideoPreset(Math.floor(fromPreset.width / t.scaleResolutionDownBy), Math.floor(fromPreset.height / t.scaleResolutionDownBy), Math.max(150000, Math.floor(fromPreset.encoding.maxBitrate / (Math.pow(t.scaleResolutionDownBy, 2) * (((_a = fromPreset.encoding.maxFramerate) !== null && _a !== void 0 ? _a : 30) / t.fps)))), t.fps, fromPreset.encoding.priority);
18654
+ });
18655
+ };
18656
+ // /**
18657
+ // *
18658
+ // * @internal
18659
+ // * @experimental
18660
+ // */
18661
+ // const computeDefaultMultiCodecSimulcastEncodings = (width: number, height: number) => {
18662
+ // // use vp8 as a default
18663
+ // const vp8 = determineAppropriateEncoding(false, width, height);
18664
+ // const vp9 = { ...vp8, maxBitrate: vp8.maxBitrate * 0.9 };
18665
+ // const h264 = { ...vp8, maxBitrate: vp8.maxBitrate * 1.1 };
18666
+ // const av1 = { ...vp8, maxBitrate: vp8.maxBitrate * 0.7 };
18667
+ // return {
18668
+ // vp8,
18669
+ // vp9,
18670
+ // h264,
18671
+ // av1,
18672
+ // };
18673
+ // };
18674
+ const videoRids = ['q', 'h', 'f'];
18675
+ /* @internal */
18676
+ function computeVideoEncodings(isScreenShare, width, height, options) {
18677
+ var _a, _b;
18678
+ let videoEncoding = options === null || options === void 0 ? void 0 : options.videoEncoding;
18679
+ if (isScreenShare) {
18680
+ videoEncoding = options === null || options === void 0 ? void 0 : options.screenShareEncoding;
18967
18681
  }
18968
- makeRTCConfiguration(serverResponse) {
18969
- const rtcConfig = Object.assign({}, this.rtcConfig);
18970
- // update ICE servers before creating PeerConnection
18971
- if (serverResponse.iceServers && !rtcConfig.iceServers) {
18972
- const rtcIceServers = [];
18973
- serverResponse.iceServers.forEach(iceServer => {
18974
- const rtcIceServer = {
18975
- urls: iceServer.urls
18976
- };
18977
- if (iceServer.username) rtcIceServer.username = iceServer.username;
18978
- if (iceServer.credential) {
18979
- rtcIceServer.credential = iceServer.credential;
18980
- }
18981
- rtcIceServers.push(rtcIceServer);
18982
- });
18983
- rtcConfig.iceServers = rtcIceServers;
18682
+ const useSimulcast = options === null || options === void 0 ? void 0 : options.simulcast;
18683
+ const scalabilityMode = options === null || options === void 0 ? void 0 : options.scalabilityMode;
18684
+ const videoCodec = options === null || options === void 0 ? void 0 : options.videoCodec;
18685
+ if (!videoEncoding && !useSimulcast && !scalabilityMode || !width || !height) {
18686
+ // when we aren't simulcasting or svc, will need to return a single encoding without
18687
+ // capping bandwidth. we always require a encoding for dynacast
18688
+ return [{}];
18689
+ }
18690
+ if (!videoEncoding) {
18691
+ // find the right encoding based on width/height
18692
+ videoEncoding = determineAppropriateEncoding(isScreenShare, width, height, videoCodec);
18693
+ livekitLogger.debug('using video encoding', videoEncoding);
18694
+ }
18695
+ const original = new VideoPreset(width, height, videoEncoding.maxBitrate, videoEncoding.maxFramerate, videoEncoding.priority);
18696
+ if (scalabilityMode && isSVCCodec(videoCodec)) {
18697
+ livekitLogger.debug("using svc with scalabilityMode ".concat(scalabilityMode));
18698
+ const sm = new ScalabilityMode(scalabilityMode);
18699
+ const encodings = [];
18700
+ if (sm.spatial > 3) {
18701
+ throw new Error("unsupported scalabilityMode: ".concat(scalabilityMode));
18984
18702
  }
18985
- if (serverResponse.clientConfiguration && serverResponse.clientConfiguration.forceRelay === ClientConfigSetting.ENABLED) {
18986
- rtcConfig.iceTransportPolicy = 'relay';
18703
+ for (let i = 0; i < sm.spatial; i += 1) {
18704
+ encodings.push({
18705
+ rid: videoRids[2 - i],
18706
+ maxBitrate: videoEncoding.maxBitrate / Math.pow(3, i),
18707
+ /* @ts-ignore */
18708
+ maxFramerate: original.encoding.maxFramerate
18709
+ });
18987
18710
  }
18988
- // @ts-ignore
18989
- rtcConfig.sdpSemantics = 'unified-plan';
18990
- // @ts-ignore
18991
- rtcConfig.continualGatheringPolicy = 'gather_continually';
18992
- return rtcConfig;
18711
+ /* @ts-ignore */
18712
+ encodings[0].scalabilityMode = scalabilityMode;
18713
+ livekitLogger.debug('encodings', encodings);
18714
+ return encodings;
18993
18715
  }
18994
- createDataChannels() {
18995
- if (!this.publisher) {
18996
- return;
18716
+ if (!useSimulcast) {
18717
+ return [videoEncoding];
18718
+ }
18719
+ let presets = [];
18720
+ if (isScreenShare) {
18721
+ presets = (_a = sortPresets(options === null || options === void 0 ? void 0 : options.screenShareSimulcastLayers)) !== null && _a !== void 0 ? _a : defaultSimulcastLayers(isScreenShare, original);
18722
+ } else {
18723
+ presets = (_b = sortPresets(options === null || options === void 0 ? void 0 : options.videoSimulcastLayers)) !== null && _b !== void 0 ? _b : defaultSimulcastLayers(isScreenShare, original);
18724
+ }
18725
+ let midPreset;
18726
+ if (presets.length > 0) {
18727
+ const lowPreset = presets[0];
18728
+ if (presets.length > 1) {
18729
+ [, midPreset] = presets;
18997
18730
  }
18998
- // clear old data channel callbacks if recreate
18999
- if (this.lossyDC) {
19000
- this.lossyDC.onmessage = null;
19001
- this.lossyDC.onerror = null;
18731
+ // NOTE:
18732
+ // 1. Ordering of these encodings is important. Chrome seems
18733
+ // to use the index into encodings to decide which layer
18734
+ // to disable when CPU constrained.
18735
+ // So encodings should be ordered in increasing spatial
18736
+ // resolution order.
18737
+ // 2. ion-sfu translates rids into layers. So, all encodings
18738
+ // should have the base layer `q` and then more added
18739
+ // based on other conditions.
18740
+ const size = Math.max(width, height);
18741
+ if (size >= 960 && midPreset) {
18742
+ return encodingsFromPresets(width, height, [lowPreset, midPreset, original]);
19002
18743
  }
19003
- if (this.reliableDC) {
19004
- this.reliableDC.onmessage = null;
19005
- this.reliableDC.onerror = null;
18744
+ if (size >= 480) {
18745
+ return encodingsFromPresets(width, height, [lowPreset, original]);
19006
18746
  }
19007
- // create data channels
19008
- this.lossyDC = this.publisher.pc.createDataChannel(lossyDataChannel, {
19009
- // will drop older packets that arrive
19010
- ordered: true,
19011
- maxRetransmits: 0
19012
- });
19013
- this.reliableDC = this.publisher.pc.createDataChannel(reliableDataChannel, {
19014
- ordered: true
18747
+ }
18748
+ return encodingsFromPresets(width, height, [original]);
18749
+ }
18750
+ function computeTrackBackupEncodings(track, videoCodec, opts) {
18751
+ var _a, _b, _c, _d;
18752
+ if (!opts.backupCodec || opts.backupCodec.codec === opts.videoCodec) {
18753
+ // backup codec publishing is disabled
18754
+ return;
18755
+ }
18756
+ if (videoCodec !== opts.backupCodec.codec) {
18757
+ livekitLogger.warn('requested a different codec than specified as backup', {
18758
+ serverRequested: videoCodec,
18759
+ backup: opts.backupCodec.codec
19015
18760
  });
19016
- // also handle messages over the pub channel, for backwards compatibility
19017
- this.lossyDC.onmessage = this.handleDataMessage;
19018
- this.reliableDC.onmessage = this.handleDataMessage;
19019
- // handle datachannel errors
19020
- this.lossyDC.onerror = this.handleDataError;
19021
- this.reliableDC.onerror = this.handleDataError;
19022
- // set up dc buffer threshold, set to 64kB (otherwise 0 by default)
19023
- this.lossyDC.bufferedAmountLowThreshold = 65535;
19024
- this.reliableDC.bufferedAmountLowThreshold = 65535;
19025
- // handle buffer amount low events
19026
- this.lossyDC.onbufferedamountlow = this.handleBufferedAmountLow;
19027
- this.reliableDC.onbufferedamountlow = this.handleBufferedAmountLow;
19028
18761
  }
19029
- setPreferredCodec(transceiver, kind, videoCodec) {
19030
- if (!('getCapabilities' in RTCRtpSender)) {
19031
- return;
18762
+ opts.videoCodec = videoCodec;
18763
+ // use backup encoding setting as videoEncoding for backup codec publishing
18764
+ opts.videoEncoding = opts.backupCodec.encoding;
18765
+ const settings = track.mediaStreamTrack.getSettings();
18766
+ const width = (_a = settings.width) !== null && _a !== void 0 ? _a : (_b = track.dimensions) === null || _b === void 0 ? void 0 : _b.width;
18767
+ const height = (_c = settings.height) !== null && _c !== void 0 ? _c : (_d = track.dimensions) === null || _d === void 0 ? void 0 : _d.height;
18768
+ const encodings = computeVideoEncodings(track.source === Track.Source.ScreenShare, width, height, opts);
18769
+ return encodings;
18770
+ }
18771
+ /* @internal */
18772
+ function determineAppropriateEncoding(isScreenShare, width, height, codec) {
18773
+ const presets = presetsForResolution(isScreenShare, width, height);
18774
+ let {
18775
+ encoding
18776
+ } = presets[0];
18777
+ // handle portrait by swapping dimensions
18778
+ const size = Math.max(width, height);
18779
+ for (let i = 0; i < presets.length; i += 1) {
18780
+ const preset = presets[i];
18781
+ encoding = preset.encoding;
18782
+ if (preset.width >= size) {
18783
+ break;
19032
18784
  }
19033
- const cap = RTCRtpSender.getCapabilities(kind);
19034
- if (!cap) return;
19035
- livekitLogger.debug('get capabilities', cap);
19036
- const matched = [];
19037
- const partialMatched = [];
19038
- const unmatched = [];
19039
- cap.codecs.forEach(c => {
19040
- const codec = c.mimeType.toLowerCase();
19041
- if (codec === 'audio/opus') {
19042
- matched.push(c);
19043
- return;
19044
- }
19045
- const matchesVideoCodec = codec === "video/".concat(videoCodec);
19046
- if (!matchesVideoCodec) {
19047
- unmatched.push(c);
19048
- return;
19049
- }
19050
- // for h264 codecs that have sdpFmtpLine available, use only if the
19051
- // profile-level-id is 42e01f for cross-browser compatibility
19052
- if (videoCodec === 'h264') {
19053
- if (c.sdpFmtpLine && c.sdpFmtpLine.includes('profile-level-id=42e01f')) {
19054
- matched.push(c);
19055
- } else {
19056
- partialMatched.push(c);
19057
- }
19058
- return;
19059
- }
19060
- matched.push(c);
19061
- });
19062
- if (supportsSetCodecPreferences(transceiver)) {
19063
- transceiver.setCodecPreferences(matched.concat(partialMatched, unmatched));
18785
+ }
18786
+ // presets are based on the assumption of vp8 as a codec
18787
+ // for other codecs we adjust the maxBitrate if no specific videoEncoding has been provided
18788
+ // TODO make the bitrate multipliers configurable per codec
18789
+ if (codec) {
18790
+ switch (codec) {
18791
+ case 'av1':
18792
+ encoding = Object.assign({}, encoding);
18793
+ encoding.maxBitrate = encoding.maxBitrate * 0.7;
18794
+ break;
18795
+ case 'vp9':
18796
+ encoding = Object.assign({}, encoding);
18797
+ encoding.maxBitrate = encoding.maxBitrate * 0.85;
18798
+ break;
19064
18799
  }
19065
18800
  }
19066
- createSender(track, opts, encodings) {
19067
- return __awaiter(this, void 0, void 0, function* () {
19068
- if (supportsTransceiver()) {
19069
- const sender = yield this.createTransceiverRTCRtpSender(track, opts, encodings);
19070
- return sender;
19071
- }
19072
- if (supportsAddTrack()) {
19073
- livekitLogger.warn('using add-track fallback');
19074
- const sender = yield this.createRTCRtpSender(track.mediaStreamTrack);
19075
- return sender;
19076
- }
19077
- throw new UnexpectedConnectionState('Required webRTC APIs not supported on this device');
19078
- });
18801
+ return encoding;
18802
+ }
18803
+ /* @internal */
18804
+ function presetsForResolution(isScreenShare, width, height) {
18805
+ if (isScreenShare) {
18806
+ return presetsScreenShare;
19079
18807
  }
19080
- createSimulcastSender(track, simulcastTrack, opts, encodings) {
19081
- return __awaiter(this, void 0, void 0, function* () {
19082
- // store RTCRtpSender
19083
- if (supportsTransceiver()) {
19084
- return this.createSimulcastTransceiverSender(track, simulcastTrack, opts, encodings);
19085
- }
19086
- if (supportsAddTrack()) {
19087
- livekitLogger.debug('using add-track fallback');
19088
- return this.createRTCRtpSender(track.mediaStreamTrack);
19089
- }
19090
- throw new UnexpectedConnectionState('Cannot stream on this device');
19091
- });
18808
+ const aspect = width > height ? width / height : height / width;
18809
+ if (Math.abs(aspect - 16.0 / 9) < Math.abs(aspect - 4.0 / 3)) {
18810
+ return presets169;
19092
18811
  }
19093
- createTransceiverRTCRtpSender(track, opts, encodings) {
19094
- return __awaiter(this, void 0, void 0, function* () {
19095
- if (!this.publisher) {
19096
- throw new UnexpectedConnectionState('publisher is closed');
19097
- }
19098
- const streams = [];
19099
- if (track.mediaStream) {
19100
- streams.push(track.mediaStream);
19101
- }
19102
- const transceiverInit = {
19103
- direction: 'sendonly',
19104
- streams
19105
- };
19106
- if (encodings) {
19107
- transceiverInit.sendEncodings = encodings;
19108
- }
19109
- // addTransceiver for react-native is async. web is synchronous, but await won't effect it.
19110
- const transceiver = yield this.publisher.pc.addTransceiver(track.mediaStreamTrack, transceiverInit);
19111
- if (track.kind === Track.Kind.Video && opts.videoCodec) {
19112
- this.setPreferredCodec(transceiver, track.kind, opts.videoCodec);
19113
- track.codec = opts.videoCodec;
19114
- }
19115
- return transceiver.sender;
19116
- });
18812
+ return presets43;
18813
+ }
18814
+ /* @internal */
18815
+ function defaultSimulcastLayers(isScreenShare, original) {
18816
+ if (isScreenShare) {
18817
+ return computeDefaultScreenShareSimulcastPresets(original);
19117
18818
  }
19118
- createSimulcastTransceiverSender(track, simulcastTrack, opts, encodings) {
19119
- return __awaiter(this, void 0, void 0, function* () {
19120
- if (!this.publisher) {
19121
- throw new UnexpectedConnectionState('publisher is closed');
19122
- }
19123
- const transceiverInit = {
19124
- direction: 'sendonly'
19125
- };
19126
- if (encodings) {
19127
- transceiverInit.sendEncodings = encodings;
19128
- }
19129
- // addTransceiver for react-native is async. web is synchronous, but await won't effect it.
19130
- const transceiver = yield this.publisher.pc.addTransceiver(simulcastTrack.mediaStreamTrack, transceiverInit);
19131
- if (!opts.videoCodec) {
19132
- return;
18819
+ const {
18820
+ width,
18821
+ height
18822
+ } = original;
18823
+ const aspect = width > height ? width / height : height / width;
18824
+ if (Math.abs(aspect - 16.0 / 9) < Math.abs(aspect - 4.0 / 3)) {
18825
+ return defaultSimulcastPresets169;
18826
+ }
18827
+ return defaultSimulcastPresets43;
18828
+ }
18829
+ // presets should be ordered by low, medium, high
18830
+ function encodingsFromPresets(width, height, presets) {
18831
+ const encodings = [];
18832
+ presets.forEach((preset, idx) => {
18833
+ if (idx >= videoRids.length) {
18834
+ return;
18835
+ }
18836
+ const size = Math.min(width, height);
18837
+ const rid = videoRids[idx];
18838
+ const encoding = {
18839
+ rid,
18840
+ scaleResolutionDownBy: Math.max(1, size / Math.min(preset.width, preset.height)),
18841
+ maxBitrate: preset.encoding.maxBitrate
18842
+ };
18843
+ if (preset.encoding.maxFramerate) {
18844
+ encoding.maxFramerate = preset.encoding.maxFramerate;
18845
+ }
18846
+ const canSetPriority = isFireFox() || idx === 0;
18847
+ if (preset.encoding.priority && canSetPriority) {
18848
+ encoding.priority = preset.encoding.priority;
18849
+ encoding.networkPriority = preset.encoding.priority;
18850
+ }
18851
+ encodings.push(encoding);
18852
+ });
18853
+ // RN ios simulcast requires all same framerates.
18854
+ if (isReactNative() && getReactNativeOs() === 'ios') {
18855
+ let topFramerate = undefined;
18856
+ encodings.forEach(encoding => {
18857
+ if (!topFramerate) {
18858
+ topFramerate = encoding.maxFramerate;
18859
+ } else if (encoding.maxFramerate && encoding.maxFramerate > topFramerate) {
18860
+ topFramerate = encoding.maxFramerate;
19133
18861
  }
19134
- this.setPreferredCodec(transceiver, track.kind, opts.videoCodec);
19135
- track.setSimulcastTrackSender(opts.videoCodec, transceiver.sender);
19136
- return transceiver.sender;
19137
18862
  });
19138
- }
19139
- createRTCRtpSender(track) {
19140
- return __awaiter(this, void 0, void 0, function* () {
19141
- if (!this.publisher) {
19142
- throw new UnexpectedConnectionState('publisher is closed');
18863
+ let notifyOnce = true;
18864
+ encodings.forEach(encoding => {
18865
+ var _a;
18866
+ if (encoding.maxFramerate != topFramerate) {
18867
+ if (notifyOnce) {
18868
+ notifyOnce = false;
18869
+ livekitLogger.info("Simulcast on iOS React-Native requires all encodings to share the same framerate.");
18870
+ }
18871
+ livekitLogger.info("Setting framerate of encoding \"".concat((_a = encoding.rid) !== null && _a !== void 0 ? _a : '', "\" to ").concat(topFramerate));
18872
+ encoding.maxFramerate = topFramerate;
19143
18873
  }
19144
- return this.publisher.pc.addTrack(track);
19145
18874
  });
19146
18875
  }
19147
- attemptReconnect(reason) {
19148
- var _a, _b, _c;
19149
- return __awaiter(this, void 0, void 0, function* () {
19150
- if (this._isClosed) {
19151
- return;
18876
+ return encodings;
18877
+ }
18878
+ /** @internal */
18879
+ function sortPresets(presets) {
18880
+ if (!presets) return;
18881
+ return presets.sort((a, b) => {
18882
+ const {
18883
+ encoding: aEnc
18884
+ } = a;
18885
+ const {
18886
+ encoding: bEnc
18887
+ } = b;
18888
+ if (aEnc.maxBitrate > bEnc.maxBitrate) {
18889
+ return 1;
18890
+ }
18891
+ if (aEnc.maxBitrate < bEnc.maxBitrate) return -1;
18892
+ if (aEnc.maxBitrate === bEnc.maxBitrate && aEnc.maxFramerate && bEnc.maxFramerate) {
18893
+ return aEnc.maxFramerate > bEnc.maxFramerate ? 1 : -1;
18894
+ }
18895
+ return 0;
18896
+ });
18897
+ }
18898
+ /** @internal */
18899
+ class ScalabilityMode {
18900
+ constructor(scalabilityMode) {
18901
+ const results = scalabilityMode.match(/^L(\d)T(\d)(h|_KEY|_KEY_SHIFT){0,1}$/);
18902
+ if (!results) {
18903
+ throw new Error('invalid scalability mode');
18904
+ }
18905
+ this.spatial = parseInt(results[1]);
18906
+ this.temporal = parseInt(results[2]);
18907
+ if (results.length > 3) {
18908
+ switch (results[3]) {
18909
+ case 'h':
18910
+ case '_KEY':
18911
+ case '_KEY_SHIFT':
18912
+ this.suffix = results[3];
19152
18913
  }
19153
- // guard for attempting reconnection multiple times while one attempt is still not finished
19154
- if (this.attemptingReconnect) {
18914
+ }
18915
+ }
18916
+ toString() {
18917
+ var _a;
18918
+ return "L".concat(this.spatial, "T").concat(this.temporal).concat((_a = this.suffix) !== null && _a !== void 0 ? _a : '');
18919
+ }
18920
+ }
18921
+
18922
+ const refreshSubscribedCodecAfterNewCodec = 5000;
18923
+ class LocalVideoTrack extends LocalTrack {
18924
+ /**
18925
+ *
18926
+ * @param mediaTrack
18927
+ * @param constraints MediaTrackConstraints that are being used when restarting or reacquiring tracks
18928
+ * @param userProvidedTrack Signals to the SDK whether or not the mediaTrack should be managed (i.e. released and reacquired) internally by the SDK
18929
+ */
18930
+ constructor(mediaTrack, constraints) {
18931
+ let userProvidedTrack = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
18932
+ super(mediaTrack, Track.Kind.Video, constraints, userProvidedTrack);
18933
+ /* @internal */
18934
+ this.simulcastCodecs = new Map();
18935
+ this.monitorSender = () => __awaiter(this, void 0, void 0, function* () {
18936
+ if (!this.sender) {
18937
+ this._currentBitrate = 0;
19155
18938
  return;
19156
18939
  }
19157
- if (((_a = this.clientConfiguration) === null || _a === void 0 ? void 0 : _a.resumeConnection) === ClientConfigSetting.DISABLED ||
19158
- // signaling state could change to closed due to hardware sleep
19159
- // those connections cannot be resumed
19160
- ((_c = (_b = this.primaryPC) === null || _b === void 0 ? void 0 : _b.signalingState) !== null && _c !== void 0 ? _c : 'closed') === 'closed') {
19161
- this.fullReconnectOnNext = true;
19162
- }
18940
+ let stats;
19163
18941
  try {
19164
- this.attemptingReconnect = true;
19165
- if (this.fullReconnectOnNext) {
19166
- yield this.restartConnection();
19167
- } else {
19168
- yield this.resumeConnection(reason);
19169
- }
19170
- this.clearPendingReconnect();
19171
- this.fullReconnectOnNext = false;
18942
+ stats = yield this.getSenderStats();
19172
18943
  } catch (e) {
19173
- this.reconnectAttempts += 1;
19174
- let recoverable = true;
19175
- if (e instanceof UnexpectedConnectionState) {
19176
- livekitLogger.debug('received unrecoverable error', {
19177
- error: e
19178
- });
19179
- // unrecoverable
19180
- recoverable = false;
19181
- } else if (!(e instanceof SignalReconnectError)) {
19182
- // cannot resume
19183
- this.fullReconnectOnNext = true;
19184
- }
19185
- if (recoverable) {
19186
- this.handleDisconnect('reconnect', ReconnectReason.RR_UNKNOWN);
19187
- } else {
19188
- livekitLogger.info("could not recover connection after ".concat(this.reconnectAttempts, " attempts, ").concat(Date.now() - this.reconnectStart, "ms. giving up"));
19189
- this.emit(EngineEvent.Disconnected);
19190
- yield this.close();
19191
- }
19192
- } finally {
19193
- this.attemptingReconnect = false;
18944
+ livekitLogger.error('could not get audio sender stats', {
18945
+ error: e
18946
+ });
18947
+ return;
19194
18948
  }
18949
+ const statsMap = new Map(stats.map(s => [s.rid, s]));
18950
+ if (this.prevStats) {
18951
+ let totalBitrate = 0;
18952
+ statsMap.forEach((s, key) => {
18953
+ var _a;
18954
+ const prev = (_a = this.prevStats) === null || _a === void 0 ? void 0 : _a.get(key);
18955
+ totalBitrate += computeBitrate(s, prev);
18956
+ });
18957
+ this._currentBitrate = totalBitrate;
18958
+ }
18959
+ this.prevStats = statsMap;
19195
18960
  });
18961
+ this.senderLock = new Mutex();
19196
18962
  }
19197
- getNextRetryDelay(context) {
19198
- try {
19199
- return this.reconnectPolicy.nextRetryDelayInMs(context);
19200
- } catch (e) {
19201
- livekitLogger.warn('encountered error in reconnect policy', {
19202
- error: e
19203
- });
18963
+ get isSimulcast() {
18964
+ if (this.sender && this.sender.getParameters().encodings.length > 1) {
18965
+ return true;
19204
18966
  }
19205
- // error in user code with provided reconnect policy, stop reconnecting
19206
- return null;
18967
+ return false;
19207
18968
  }
19208
- restartConnection(regionUrl) {
19209
- var _a, _b, _c;
19210
- return __awaiter(this, void 0, void 0, function* () {
19211
- try {
19212
- if (!this.url || !this.token) {
19213
- // permanent failure, don't attempt reconnection
19214
- throw new UnexpectedConnectionState('could not reconnect, url or token not saved');
19215
- }
19216
- livekitLogger.info("reconnecting, attempt: ".concat(this.reconnectAttempts));
19217
- this.emit(EngineEvent.Restarting);
19218
- if (this.client.isConnected) {
19219
- yield this.client.sendLeave();
19220
- }
19221
- yield this.cleanupPeerConnections();
19222
- yield this.cleanupClient();
19223
- let joinResponse;
19224
- try {
19225
- if (!this.signalOpts) {
19226
- livekitLogger.warn('attempted connection restart, without signal options present');
19227
- throw new SignalReconnectError();
19228
- }
19229
- // in case a regionUrl is passed, the region URL takes precedence
19230
- joinResponse = yield this.join(regionUrl !== null && regionUrl !== void 0 ? regionUrl : this.url, this.token, this.signalOpts);
19231
- } catch (e) {
19232
- if (e instanceof ConnectionError && e.reason === 0 /* ConnectionErrorReason.NotAllowed */) {
19233
- throw new UnexpectedConnectionState('could not reconnect, token might be expired');
19234
- }
19235
- throw new SignalReconnectError();
19236
- }
19237
- if (this.shouldFailNext) {
19238
- this.shouldFailNext = false;
19239
- throw new Error('simulated failure');
19240
- }
19241
- this.client.setReconnected();
19242
- this.emit(EngineEvent.SignalRestarted, joinResponse);
19243
- yield this.waitForPCReconnected();
19244
- (_a = this.regionUrlProvider) === null || _a === void 0 ? void 0 : _a.resetAttempts();
19245
- // reconnect success
19246
- this.emit(EngineEvent.Restarted);
19247
- } catch (error) {
19248
- const nextRegionUrl = yield (_b = this.regionUrlProvider) === null || _b === void 0 ? void 0 : _b.getNextBestRegionUrl();
19249
- if (nextRegionUrl) {
19250
- yield this.restartConnection(nextRegionUrl);
19251
- return;
19252
- } else {
19253
- // no more regions to try (or we're not on cloud)
19254
- (_c = this.regionUrlProvider) === null || _c === void 0 ? void 0 : _c.resetAttempts();
19255
- throw error;
19256
- }
19257
- }
18969
+ /* @internal */
18970
+ startMonitor(signalClient) {
18971
+ var _a;
18972
+ this.signalClient = signalClient;
18973
+ if (!isWeb()) {
18974
+ return;
18975
+ }
18976
+ // save original encodings
18977
+ // TODO : merge simulcast tracks stats
18978
+ const params = (_a = this.sender) === null || _a === void 0 ? void 0 : _a.getParameters();
18979
+ if (params) {
18980
+ this.encodings = params.encodings;
18981
+ }
18982
+ if (this.monitorInterval) {
18983
+ return;
18984
+ }
18985
+ this.monitorInterval = setInterval(() => {
18986
+ this.monitorSender();
18987
+ }, monitorFrequency);
18988
+ }
18989
+ stop() {
18990
+ this._mediaStreamTrack.getConstraints();
18991
+ this.simulcastCodecs.forEach(trackInfo => {
18992
+ trackInfo.mediaStreamTrack.stop();
19258
18993
  });
18994
+ super.stop();
19259
18995
  }
19260
- resumeConnection(reason) {
19261
- var _a;
19262
- return __awaiter(this, void 0, void 0, function* () {
19263
- if (!this.url || !this.token) {
19264
- // permanent failure, don't attempt reconnection
19265
- throw new UnexpectedConnectionState('could not reconnect, url or token not saved');
19266
- }
19267
- // trigger publisher reconnect
19268
- if (!this.publisher || !this.subscriber) {
19269
- throw new UnexpectedConnectionState('publisher and subscriber connections unset');
18996
+ mute() {
18997
+ const _super = Object.create(null, {
18998
+ mute: {
18999
+ get: () => super.mute
19270
19000
  }
19271
- livekitLogger.info("resuming signal connection, attempt ".concat(this.reconnectAttempts));
19272
- this.emit(EngineEvent.Resuming);
19001
+ });
19002
+ return __awaiter(this, void 0, void 0, function* () {
19003
+ const unlock = yield this.muteLock.lock();
19273
19004
  try {
19274
- this.setupSignalClientCallbacks();
19275
- const res = yield this.client.reconnect(this.url, this.token, this.participantSid, reason);
19276
- if (res) {
19277
- const rtcConfig = this.makeRTCConfiguration(res);
19278
- this.publisher.pc.setConfiguration(rtcConfig);
19279
- this.subscriber.pc.setConfiguration(rtcConfig);
19280
- }
19281
- } catch (e) {
19282
- let message = '';
19283
- if (e instanceof Error) {
19284
- message = e.message;
19285
- }
19286
- if (e instanceof ConnectionError && e.reason === 0 /* ConnectionErrorReason.NotAllowed */) {
19287
- throw new UnexpectedConnectionState('could not reconnect, token might be expired');
19005
+ if (this.source === Track.Source.Camera && !this.isUserProvided) {
19006
+ livekitLogger.debug('stopping camera track');
19007
+ // also stop the track, so that camera indicator is turned off
19008
+ this._mediaStreamTrack.stop();
19288
19009
  }
19289
- throw new SignalReconnectError(message);
19290
- }
19291
- this.emit(EngineEvent.SignalResumed);
19292
- if (this.shouldFailNext) {
19293
- this.shouldFailNext = false;
19294
- throw new Error('simulated failure');
19010
+ yield _super.mute.call(this);
19011
+ return this;
19012
+ } finally {
19013
+ unlock();
19295
19014
  }
19296
- this.subscriber.restartingIce = true;
19297
- // only restart publisher if it's needed
19298
- if (this.hasPublished) {
19299
- yield this.publisher.createAndSendOffer({
19300
- iceRestart: true
19301
- });
19015
+ });
19016
+ }
19017
+ unmute() {
19018
+ const _super = Object.create(null, {
19019
+ unmute: {
19020
+ get: () => super.unmute
19302
19021
  }
19303
- yield this.waitForPCReconnected();
19304
- this.client.setReconnected();
19305
- // recreate publish datachannel if it's id is null
19306
- // (for safari https://bugs.webkit.org/show_bug.cgi?id=184688)
19307
- if (((_a = this.reliableDC) === null || _a === void 0 ? void 0 : _a.readyState) === 'open' && this.reliableDC.id === null) {
19308
- this.createDataChannels();
19022
+ });
19023
+ return __awaiter(this, void 0, void 0, function* () {
19024
+ const unlock = yield this.muteLock.lock();
19025
+ try {
19026
+ if (this.source === Track.Source.Camera && !this.isUserProvided) {
19027
+ livekitLogger.debug('reacquiring camera track');
19028
+ yield this.restartTrack();
19029
+ }
19030
+ yield _super.unmute.call(this);
19031
+ return this;
19032
+ } finally {
19033
+ unlock();
19309
19034
  }
19310
- // resume success
19311
- this.emit(EngineEvent.Resumed);
19312
19035
  });
19313
19036
  }
19314
- waitForPCInitialConnection(timeout, abortController) {
19037
+ getSenderStats() {
19038
+ var _a;
19315
19039
  return __awaiter(this, void 0, void 0, function* () {
19316
- if (this.pcState === PCState.Connected) {
19317
- return;
19318
- }
19319
- if (this.pcState !== PCState.New) {
19320
- throw new UnexpectedConnectionState('Expected peer connection to be new on initial connection');
19040
+ if (!((_a = this.sender) === null || _a === void 0 ? void 0 : _a.getStats)) {
19041
+ return [];
19321
19042
  }
19322
- return new Promise((resolve, reject) => {
19323
- const abortHandler = () => {
19324
- livekitLogger.warn('closing engine');
19325
- CriticalTimers.clearTimeout(connectTimeout);
19326
- reject(new ConnectionError('room connection has been cancelled', 3 /* ConnectionErrorReason.Cancelled */));
19327
- };
19328
-
19329
- if (abortController === null || abortController === void 0 ? void 0 : abortController.signal.aborted) {
19330
- abortHandler();
19043
+ const items = [];
19044
+ const stats = yield this.sender.getStats();
19045
+ stats.forEach(v => {
19046
+ var _a;
19047
+ if (v.type === 'outbound-rtp') {
19048
+ const vs = {
19049
+ type: 'video',
19050
+ streamId: v.id,
19051
+ frameHeight: v.frameHeight,
19052
+ frameWidth: v.frameWidth,
19053
+ firCount: v.firCount,
19054
+ pliCount: v.pliCount,
19055
+ nackCount: v.nackCount,
19056
+ packetsSent: v.packetsSent,
19057
+ bytesSent: v.bytesSent,
19058
+ framesSent: v.framesSent,
19059
+ timestamp: v.timestamp,
19060
+ rid: (_a = v.rid) !== null && _a !== void 0 ? _a : v.id,
19061
+ retransmittedPacketsSent: v.retransmittedPacketsSent,
19062
+ qualityLimitationReason: v.qualityLimitationReason,
19063
+ qualityLimitationResolutionChanges: v.qualityLimitationResolutionChanges
19064
+ };
19065
+ // locate the appropriate remote-inbound-rtp item
19066
+ const r = stats.get(v.remoteId);
19067
+ if (r) {
19068
+ vs.jitter = r.jitter;
19069
+ vs.packetsLost = r.packetsLost;
19070
+ vs.roundTripTime = r.roundTripTime;
19071
+ }
19072
+ items.push(vs);
19331
19073
  }
19332
- abortController === null || abortController === void 0 ? void 0 : abortController.signal.addEventListener('abort', abortHandler);
19333
- const onConnected = () => {
19334
- CriticalTimers.clearTimeout(connectTimeout);
19335
- abortController === null || abortController === void 0 ? void 0 : abortController.signal.removeEventListener('abort', abortHandler);
19336
- resolve();
19337
- };
19338
- const connectTimeout = CriticalTimers.setTimeout(() => {
19339
- this.off(EngineEvent.Connected, onConnected);
19340
- reject(new ConnectionError('could not establish pc connection'));
19341
- }, timeout !== null && timeout !== void 0 ? timeout : this.peerConnectionTimeout);
19342
- this.once(EngineEvent.Connected, onConnected);
19343
19074
  });
19075
+ return items;
19344
19076
  });
19345
19077
  }
19346
- waitForPCReconnected() {
19347
- var _a;
19078
+ setPublishingQuality(maxQuality) {
19079
+ const qualities = [];
19080
+ for (let q = VideoQuality.LOW; q <= VideoQuality.HIGH; q += 1) {
19081
+ qualities.push({
19082
+ quality: q,
19083
+ enabled: q <= maxQuality
19084
+ });
19085
+ }
19086
+ livekitLogger.debug("setting publishing quality. max quality ".concat(maxQuality));
19087
+ this.setPublishingLayers(qualities);
19088
+ }
19089
+ setDeviceId(deviceId) {
19348
19090
  return __awaiter(this, void 0, void 0, function* () {
19349
- const startTime = Date.now();
19350
- let now = startTime;
19351
- this.pcState = PCState.Reconnecting;
19352
- livekitLogger.debug('waiting for peer connection to reconnect');
19353
- while (now - startTime < this.peerConnectionTimeout) {
19354
- if (this.primaryPC === undefined) {
19355
- // we can abort early, connection is hosed
19356
- break;
19357
- } else if (
19358
- // on Safari, we don't get a connectionstatechanged event during ICE restart
19359
- // this means we'd have to check its status manually and update address
19360
- // manually
19361
- now - startTime > minReconnectWait && ((_a = this.primaryPC) === null || _a === void 0 ? void 0 : _a.connectionState) === 'connected') {
19362
- this.pcState = PCState.Connected;
19363
- }
19364
- if (this.pcState === PCState.Connected) {
19365
- return;
19366
- }
19367
- yield sleep(100);
19368
- now = Date.now();
19091
+ if (this._constraints.deviceId === deviceId && this._mediaStreamTrack.getSettings().deviceId === unwrapConstraint(deviceId)) {
19092
+ return true;
19369
19093
  }
19370
- // have not reconnected, throw
19371
- throw new ConnectionError('could not establish PC connection');
19094
+ this._constraints.deviceId = deviceId;
19095
+ // when video is muted, underlying media stream track is stopped and
19096
+ // will be restarted later
19097
+ if (!this.isMuted) {
19098
+ yield this.restartTrack();
19099
+ }
19100
+ return this.isMuted || unwrapConstraint(deviceId) === this._mediaStreamTrack.getSettings().deviceId;
19372
19101
  });
19373
19102
  }
19374
- /* @internal */
19375
- sendDataPacket(packet, kind) {
19103
+ restartTrack(options) {
19376
19104
  return __awaiter(this, void 0, void 0, function* () {
19377
- const msg = DataPacket.encode(packet).finish();
19378
- // make sure we do have a data connection
19379
- yield this.ensurePublisherConnected(kind);
19380
- const dc = this.dataChannelForKind(kind);
19381
- if (dc) {
19382
- dc.send(msg);
19105
+ let constraints;
19106
+ if (options) {
19107
+ const streamConstraints = constraintsForOptions({
19108
+ video: options
19109
+ });
19110
+ if (typeof streamConstraints.video !== 'boolean') {
19111
+ constraints = streamConstraints.video;
19112
+ }
19383
19113
  }
19384
- this.updateAndEmitDCBufferStatus(kind);
19114
+ yield this.restart(constraints);
19385
19115
  });
19386
19116
  }
19117
+ addSimulcastTrack(codec, encodings) {
19118
+ if (this.simulcastCodecs.has(codec)) {
19119
+ throw new Error("".concat(codec, " already added"));
19120
+ }
19121
+ const simulcastCodecInfo = {
19122
+ codec,
19123
+ mediaStreamTrack: this.mediaStreamTrack.clone(),
19124
+ sender: undefined,
19125
+ encodings
19126
+ };
19127
+ this.simulcastCodecs.set(codec, simulcastCodecInfo);
19128
+ return simulcastCodecInfo;
19129
+ }
19130
+ setSimulcastTrackSender(codec, sender) {
19131
+ const simulcastCodecInfo = this.simulcastCodecs.get(codec);
19132
+ if (!simulcastCodecInfo) {
19133
+ return;
19134
+ }
19135
+ simulcastCodecInfo.sender = sender;
19136
+ // browser will reenable disabled codec/layers after new codec has been published,
19137
+ // so refresh subscribedCodecs after publish a new codec
19138
+ setTimeout(() => {
19139
+ if (this.subscribedCodecs) {
19140
+ this.setPublishingCodecs(this.subscribedCodecs);
19141
+ }
19142
+ }, refreshSubscribedCodecAfterNewCodec);
19143
+ }
19387
19144
  /**
19388
19145
  * @internal
19146
+ * Sets codecs that should be publishing
19389
19147
  */
19390
- ensureDataTransportConnected(kind) {
19391
- let subscriber = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.subscriberPrimary;
19392
- var _a, _b, _c;
19148
+ setPublishingCodecs(codecs) {
19149
+ var _a, codecs_1, codecs_1_1;
19150
+ var _b, e_1, _c, _d;
19393
19151
  return __awaiter(this, void 0, void 0, function* () {
19394
- const transport = subscriber ? this.subscriber : this.publisher;
19395
- const transportName = subscriber ? 'Subscriber' : 'Publisher';
19396
- if (!transport) {
19397
- throw new ConnectionError("".concat(transportName, " connection not set"));
19398
- }
19399
- if (!subscriber && !((_a = this.publisher) === null || _a === void 0 ? void 0 : _a.isICEConnected) && ((_b = this.publisher) === null || _b === void 0 ? void 0 : _b.pc.iceConnectionState) !== 'checking') {
19400
- // start negotiation
19401
- this.negotiate();
19402
- }
19403
- const targetChannel = this.dataChannelForKind(kind, subscriber);
19404
- if ((targetChannel === null || targetChannel === void 0 ? void 0 : targetChannel.readyState) === 'open') {
19405
- return;
19152
+ livekitLogger.debug('setting publishing codecs', {
19153
+ codecs,
19154
+ currentCodec: this.codec
19155
+ });
19156
+ // only enable simulcast codec for preference codec setted
19157
+ if (!this.codec && codecs.length > 0) {
19158
+ yield this.setPublishingLayers(codecs[0].qualities);
19159
+ return [];
19406
19160
  }
19407
- // wait until ICE connected
19408
- const endTime = new Date().getTime() + this.peerConnectionTimeout;
19409
- while (new Date().getTime() < endTime) {
19410
- if (transport.isICEConnected && ((_c = this.dataChannelForKind(kind, subscriber)) === null || _c === void 0 ? void 0 : _c.readyState) === 'open') {
19411
- return;
19161
+ this.subscribedCodecs = codecs;
19162
+ const newCodecs = [];
19163
+ try {
19164
+ for (_a = true, codecs_1 = __asyncValues(codecs); codecs_1_1 = yield codecs_1.next(), _b = codecs_1_1.done, !_b; _a = true) {
19165
+ _d = codecs_1_1.value;
19166
+ _a = false;
19167
+ const codec = _d;
19168
+ if (!this.codec || this.codec === codec.codec) {
19169
+ yield this.setPublishingLayers(codec.qualities);
19170
+ } else {
19171
+ const simulcastCodecInfo = this.simulcastCodecs.get(codec.codec);
19172
+ livekitLogger.debug("try setPublishingCodec for ".concat(codec.codec), simulcastCodecInfo);
19173
+ if (!simulcastCodecInfo || !simulcastCodecInfo.sender) {
19174
+ for (const q of codec.qualities) {
19175
+ if (q.enabled) {
19176
+ newCodecs.push(codec.codec);
19177
+ break;
19178
+ }
19179
+ }
19180
+ } else if (simulcastCodecInfo.encodings) {
19181
+ livekitLogger.debug("try setPublishingLayersForSender ".concat(codec.codec));
19182
+ yield setPublishingLayersForSender(simulcastCodecInfo.sender, simulcastCodecInfo.encodings, codec.qualities, this.senderLock);
19183
+ }
19184
+ }
19185
+ }
19186
+ } catch (e_1_1) {
19187
+ e_1 = {
19188
+ error: e_1_1
19189
+ };
19190
+ } finally {
19191
+ try {
19192
+ if (!_a && !_b && (_c = codecs_1.return)) yield _c.call(codecs_1);
19193
+ } finally {
19194
+ if (e_1) throw e_1.error;
19412
19195
  }
19413
- yield sleep(50);
19414
19196
  }
19415
- throw new ConnectionError("could not establish ".concat(transportName, " connection, state: ").concat(transport.pc.iceConnectionState));
19197
+ return newCodecs;
19416
19198
  });
19417
19199
  }
19418
- ensurePublisherConnected(kind) {
19200
+ /**
19201
+ * @internal
19202
+ * Sets layers that should be publishing
19203
+ */
19204
+ setPublishingLayers(qualities) {
19419
19205
  return __awaiter(this, void 0, void 0, function* () {
19420
- yield this.ensureDataTransportConnected(kind, false);
19206
+ livekitLogger.debug('setting publishing layers', qualities);
19207
+ if (!this.sender || !this.encodings) {
19208
+ return;
19209
+ }
19210
+ yield setPublishingLayersForSender(this.sender, this.encodings, qualities, this.senderLock);
19421
19211
  });
19422
19212
  }
19423
- /* @internal */
19424
- verifyTransport() {
19425
- // primary connection
19426
- if (!this.primaryPC) {
19427
- return false;
19428
- }
19429
- if (this.primaryPC.connectionState === 'closed' || this.primaryPC.connectionState === 'failed') {
19430
- return false;
19431
- }
19432
- // also verify publisher connection if it's needed or different
19433
- if (this.hasPublished && this.subscriberPrimary) {
19434
- if (!this.publisher) {
19435
- return false;
19213
+ handleAppVisibilityChanged() {
19214
+ const _super = Object.create(null, {
19215
+ handleAppVisibilityChanged: {
19216
+ get: () => super.handleAppVisibilityChanged
19436
19217
  }
19437
- if (this.publisher.pc.connectionState === 'closed' || this.publisher.pc.connectionState === 'failed') {
19438
- return false;
19218
+ });
19219
+ return __awaiter(this, void 0, void 0, function* () {
19220
+ yield _super.handleAppVisibilityChanged.call(this);
19221
+ if (!isMobile()) return;
19222
+ if (this.isInBackground && this.source === Track.Source.Camera) {
19223
+ this._mediaStreamTrack.enabled = false;
19439
19224
  }
19440
- }
19441
- // ensure signal is connected
19442
- if (!this.client.ws || this.client.ws.readyState === WebSocket.CLOSED) {
19443
- return false;
19444
- }
19445
- return true;
19225
+ });
19446
19226
  }
19447
- /** @internal */
19448
- negotiate() {
19449
- // observe signal state
19450
- return new Promise((resolve, reject) => {
19451
- if (!this.publisher) {
19452
- reject(new NegotiationError('publisher is not defined'));
19227
+ }
19228
+ function setPublishingLayersForSender(sender, senderEncodings, qualities, senderLock) {
19229
+ return __awaiter(this, void 0, void 0, function* () {
19230
+ const unlock = yield senderLock.lock();
19231
+ livekitLogger.debug('setPublishingLayersForSender', {
19232
+ sender,
19233
+ qualities,
19234
+ senderEncodings
19235
+ });
19236
+ try {
19237
+ const params = sender.getParameters();
19238
+ const {
19239
+ encodings
19240
+ } = params;
19241
+ if (!encodings) {
19453
19242
  return;
19454
19243
  }
19455
- this.hasPublished = true;
19456
- const handleClosed = () => {
19457
- livekitLogger.warn('engine disconnected while negotiation was ongoing');
19458
- cleanup();
19459
- resolve();
19244
+ if (encodings.length !== senderEncodings.length) {
19245
+ livekitLogger.warn('cannot set publishing layers, encodings mismatch');
19460
19246
  return;
19461
- };
19462
- if (this.isClosed) {
19463
- reject('cannot negotiate on closed engine');
19464
19247
  }
19465
- this.on(EngineEvent.Closing, handleClosed);
19466
- const negotiationTimeout = setTimeout(() => {
19467
- reject('negotiation timed out');
19468
- this.handleDisconnect('negotiation', ReconnectReason.RR_SIGNAL_DISCONNECTED);
19469
- }, this.peerConnectionTimeout);
19470
- const cleanup = () => {
19471
- clearTimeout(negotiationTimeout);
19472
- this.off(EngineEvent.Closing, handleClosed);
19473
- };
19474
- this.publisher.once(PCEvents.NegotiationStarted, () => {
19475
- var _a;
19476
- (_a = this.publisher) === null || _a === void 0 ? void 0 : _a.once(PCEvents.NegotiationComplete, () => {
19477
- cleanup();
19478
- resolve();
19479
- });
19480
- });
19481
- this.publisher.once(PCEvents.RTPVideoPayloadTypes, rtpTypes => {
19482
- const rtpMap = new Map();
19483
- rtpTypes.forEach(rtp => {
19484
- const codec = rtp.codec.toLowerCase();
19485
- if (isVideoCodec(codec)) {
19486
- rtpMap.set(rtp.payload, codec);
19248
+ let hasChanged = false;
19249
+ /* disable closable spatial layer as it has video blur / frozen issue with current server / client
19250
+ 1. chrome 113: when switching to up layer with scalability Mode change, it will generate a
19251
+ low resolution frame and recover very quickly, but noticable
19252
+ 2. livekit sfu: additional pli request cause video frozen for a few frames, also noticable */
19253
+ const closableSpatial = false;
19254
+ /* @ts-ignore */
19255
+ if (closableSpatial && encodings[0].scalabilityMode) ; else {
19256
+ // simulcast dynacast encodings
19257
+ encodings.forEach((encoding, idx) => {
19258
+ var _a;
19259
+ let rid = (_a = encoding.rid) !== null && _a !== void 0 ? _a : '';
19260
+ if (rid === '') {
19261
+ rid = 'q';
19262
+ }
19263
+ const quality = videoQualityForRid(rid);
19264
+ const subscribedQuality = qualities.find(q => q.quality === quality);
19265
+ if (!subscribedQuality) {
19266
+ return;
19267
+ }
19268
+ if (encoding.active !== subscribedQuality.enabled) {
19269
+ hasChanged = true;
19270
+ encoding.active = subscribedQuality.enabled;
19271
+ livekitLogger.debug("setting layer ".concat(subscribedQuality.quality, " to ").concat(encoding.active ? 'enabled' : 'disabled'));
19272
+ // FireFox does not support setting encoding.active to false, so we
19273
+ // have a workaround of lowering its bitrate and resolution to the min.
19274
+ if (isFireFox()) {
19275
+ if (subscribedQuality.enabled) {
19276
+ encoding.scaleResolutionDownBy = senderEncodings[idx].scaleResolutionDownBy;
19277
+ encoding.maxBitrate = senderEncodings[idx].maxBitrate;
19278
+ /* @ts-ignore */
19279
+ encoding.maxFrameRate = senderEncodings[idx].maxFrameRate;
19280
+ } else {
19281
+ encoding.scaleResolutionDownBy = 4;
19282
+ encoding.maxBitrate = 10;
19283
+ /* @ts-ignore */
19284
+ encoding.maxFrameRate = 2;
19285
+ }
19286
+ }
19487
19287
  }
19488
19288
  });
19489
- this.emit(EngineEvent.RTPVideoMapUpdate, rtpMap);
19490
- });
19491
- this.publisher.negotiate(e => {
19492
- cleanup();
19493
- reject(e);
19494
- if (e instanceof NegotiationError) {
19495
- this.fullReconnectOnNext = true;
19496
- }
19497
- this.handleDisconnect('negotiation', ReconnectReason.RR_UNKNOWN);
19498
- });
19499
- });
19500
- }
19501
- dataChannelForKind(kind, sub) {
19502
- if (!sub) {
19503
- if (kind === DataPacket_Kind.LOSSY) {
19504
- return this.lossyDC;
19505
19289
  }
19506
- if (kind === DataPacket_Kind.RELIABLE) {
19507
- return this.reliableDC;
19508
- }
19509
- } else {
19510
- if (kind === DataPacket_Kind.LOSSY) {
19511
- return this.lossyDCSub;
19512
- }
19513
- if (kind === DataPacket_Kind.RELIABLE) {
19514
- return this.reliableDCSub;
19290
+ if (hasChanged) {
19291
+ params.encodings = encodings;
19292
+ livekitLogger.debug("setting encodings", params.encodings);
19293
+ yield sender.setParameters(params);
19515
19294
  }
19295
+ } finally {
19296
+ unlock();
19516
19297
  }
19517
- }
19518
- /* @internal */
19519
- failNext() {
19520
- // debugging method to fail the next reconnect/resume attempt
19521
- this.shouldFailNext = true;
19522
- }
19523
- clearReconnectTimeout() {
19524
- if (this.reconnectTimeout) {
19525
- CriticalTimers.clearTimeout(this.reconnectTimeout);
19526
- }
19527
- }
19528
- clearPendingReconnect() {
19529
- this.clearReconnectTimeout();
19530
- this.reconnectAttempts = 0;
19531
- }
19532
- registerOnLineListener() {
19533
- if (isWeb()) {
19534
- window.addEventListener('online', this.handleBrowserOnLine);
19535
- }
19536
- }
19537
- deregisterOnLineListener() {
19538
- if (isWeb()) {
19539
- window.removeEventListener('online', this.handleBrowserOnLine);
19540
- }
19298
+ });
19299
+ }
19300
+ function videoQualityForRid(rid) {
19301
+ switch (rid) {
19302
+ case 'f':
19303
+ return VideoQuality.HIGH;
19304
+ case 'h':
19305
+ return VideoQuality.MEDIUM;
19306
+ case 'q':
19307
+ return VideoQuality.LOW;
19308
+ default:
19309
+ return VideoQuality.UNRECOGNIZED;
19541
19310
  }
19542
19311
  }
19543
- function getConnectedAddress(pc) {
19544
- var _a;
19545
- return __awaiter(this, void 0, void 0, function* () {
19546
- let selectedCandidatePairId = '';
19547
- const candidatePairs = new Map();
19548
- // id -> candidate ip
19549
- const candidates = new Map();
19550
- const stats = yield pc.getStats();
19551
- stats.forEach(v => {
19552
- switch (v.type) {
19553
- case 'transport':
19554
- selectedCandidatePairId = v.selectedCandidatePairId;
19555
- break;
19556
- case 'candidate-pair':
19557
- if (selectedCandidatePairId === '' && v.selected) {
19558
- selectedCandidatePairId = v.id;
19559
- }
19560
- candidatePairs.set(v.id, v);
19561
- break;
19562
- case 'remote-candidate':
19563
- candidates.set(v.id, "".concat(v.address, ":").concat(v.port));
19564
- break;
19565
- }
19566
- });
19567
- if (selectedCandidatePairId === '') {
19568
- return undefined;
19569
- }
19570
- const selectedID = (_a = candidatePairs.get(selectedCandidatePairId)) === null || _a === void 0 ? void 0 : _a.remoteCandidateId;
19571
- if (selectedID === undefined) {
19572
- return undefined;
19312
+ function videoLayersFromEncodings(width, height, encodings, svc) {
19313
+ // default to a single layer, HQ
19314
+ if (!encodings) {
19315
+ return [{
19316
+ quality: VideoQuality.HIGH,
19317
+ width,
19318
+ height,
19319
+ bitrate: 0,
19320
+ ssrc: 0
19321
+ }];
19322
+ }
19323
+ if (svc) {
19324
+ // svc layers
19325
+ /* @ts-ignore */
19326
+ const sm = new ScalabilityMode(encodings[0].scalabilityMode);
19327
+ const layers = [];
19328
+ for (let i = 0; i < sm.spatial; i += 1) {
19329
+ layers.push({
19330
+ quality: VideoQuality.HIGH - i,
19331
+ width: width / Math.pow(2, i),
19332
+ height: height / Math.pow(2, i),
19333
+ bitrate: encodings[0].maxBitrate ? encodings[0].maxBitrate / Math.pow(3, i) : 0,
19334
+ ssrc: 0
19335
+ });
19573
19336
  }
19574
- return candidates.get(selectedID);
19337
+ return layers;
19338
+ }
19339
+ return encodings.map(encoding => {
19340
+ var _a, _b, _c;
19341
+ const scale = (_a = encoding.scaleResolutionDownBy) !== null && _a !== void 0 ? _a : 1;
19342
+ let quality = videoQualityForRid((_b = encoding.rid) !== null && _b !== void 0 ? _b : '');
19343
+ if (quality === VideoQuality.UNRECOGNIZED && encodings.length === 1) {
19344
+ quality = VideoQuality.HIGH;
19345
+ }
19346
+ return {
19347
+ quality,
19348
+ width: width / scale,
19349
+ height: height / scale,
19350
+ bitrate: (_c = encoding.maxBitrate) !== null && _c !== void 0 ? _c : 0,
19351
+ ssrc: 0
19352
+ };
19575
19353
  });
19576
19354
  }
19577
- class SignalReconnectError extends Error {}
19578
19355
 
19579
- const monitorFrequency = 2000;
19580
- function computeBitrate(currentStats, prevStats) {
19581
- if (!prevStats) {
19582
- return 0;
19583
- }
19584
- let bytesNow;
19585
- let bytesPrev;
19586
- if ('bytesReceived' in currentStats) {
19587
- bytesNow = currentStats.bytesReceived;
19588
- bytesPrev = prevStats.bytesReceived;
19589
- } else if ('bytesSent' in currentStats) {
19590
- bytesNow = currentStats.bytesSent;
19591
- bytesPrev = prevStats.bytesSent;
19592
- }
19593
- if (bytesNow === undefined || bytesPrev === undefined || currentStats.timestamp === undefined || prevStats.timestamp === undefined) {
19594
- return 0;
19356
+ const lossyDataChannel = '_lossy';
19357
+ const reliableDataChannel = '_reliable';
19358
+ const minReconnectWait = 2 * 1000;
19359
+ const leaveReconnect = 'leave-reconnect';
19360
+ var PCState;
19361
+ (function (PCState) {
19362
+ PCState[PCState["New"] = 0] = "New";
19363
+ PCState[PCState["Connected"] = 1] = "Connected";
19364
+ PCState[PCState["Disconnected"] = 2] = "Disconnected";
19365
+ PCState[PCState["Reconnecting"] = 3] = "Reconnecting";
19366
+ PCState[PCState["Closed"] = 4] = "Closed";
19367
+ })(PCState || (PCState = {}));
19368
+ /** @internal */
19369
+ class RTCEngine extends EventEmitter {
19370
+ get isClosed() {
19371
+ return this._isClosed;
19595
19372
  }
19596
- return (bytesNow - bytesPrev) * 8 * 1000 / (currentStats.timestamp - prevStats.timestamp);
19597
- }
19598
-
19599
- class LocalAudioTrack extends LocalTrack {
19600
- /**
19601
- *
19602
- * @param mediaTrack
19603
- * @param constraints MediaTrackConstraints that are being used when restarting or reacquiring tracks
19604
- * @param userProvidedTrack Signals to the SDK whether or not the mediaTrack should be managed (i.e. released and reacquired) internally by the SDK
19605
- */
19606
- constructor(mediaTrack, constraints) {
19607
- let userProvidedTrack = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
19608
- super(mediaTrack, Track.Kind.Audio, constraints, userProvidedTrack);
19609
- /** @internal */
19610
- this.stopOnMute = false;
19611
- this.monitorSender = () => __awaiter(this, void 0, void 0, function* () {
19612
- if (!this.sender) {
19613
- this._currentBitrate = 0;
19614
- return;
19615
- }
19616
- let stats;
19373
+ constructor(options) {
19374
+ super();
19375
+ this.options = options;
19376
+ this.rtcConfig = {};
19377
+ this.peerConnectionTimeout = roomConnectOptionDefaults.peerConnectionTimeout;
19378
+ this.fullReconnectOnNext = false;
19379
+ this.subscriberPrimary = false;
19380
+ this.pcState = PCState.New;
19381
+ this._isClosed = true;
19382
+ this.pendingTrackResolvers = {};
19383
+ // true if publisher connection has already been established.
19384
+ // this is helpful to know if we need to restart ICE on the publisher connection
19385
+ this.hasPublished = false;
19386
+ this.reconnectAttempts = 0;
19387
+ this.reconnectStart = 0;
19388
+ this.attemptingReconnect = false;
19389
+ /** keeps track of how often an initial join connection has been tried */
19390
+ this.joinAttempts = 0;
19391
+ /** specifies how often an initial join connection is allowed to retry */
19392
+ this.maxJoinAttempts = 1;
19393
+ this.shouldFailNext = false;
19394
+ this.handleDataChannel = _ref => {
19395
+ let {
19396
+ channel
19397
+ } = _ref;
19398
+ return __awaiter(this, void 0, void 0, function* () {
19399
+ if (!channel) {
19400
+ return;
19401
+ }
19402
+ if (channel.label === reliableDataChannel) {
19403
+ this.reliableDCSub = channel;
19404
+ } else if (channel.label === lossyDataChannel) {
19405
+ this.lossyDCSub = channel;
19406
+ } else {
19407
+ return;
19408
+ }
19409
+ livekitLogger.debug("on data channel ".concat(channel.id, ", ").concat(channel.label));
19410
+ channel.onmessage = this.handleDataMessage;
19411
+ });
19412
+ };
19413
+ this.handleDataMessage = message => __awaiter(this, void 0, void 0, function* () {
19414
+ var _a, _b;
19415
+ // make sure to respect incoming data message order by processing message events one after the other
19416
+ const unlock = yield this.dataProcessLock.lock();
19617
19417
  try {
19618
- stats = yield this.getSenderStats();
19619
- } catch (e) {
19620
- livekitLogger.error('could not get audio sender stats', {
19621
- error: e
19622
- });
19623
- return;
19624
- }
19625
- if (stats && this.prevStats) {
19626
- this._currentBitrate = computeBitrate(stats, this.prevStats);
19418
+ // decode
19419
+ let buffer;
19420
+ if (message.data instanceof ArrayBuffer) {
19421
+ buffer = message.data;
19422
+ } else if (message.data instanceof Blob) {
19423
+ buffer = yield message.data.arrayBuffer();
19424
+ } else {
19425
+ livekitLogger.error('unsupported data type', message.data);
19426
+ return;
19427
+ }
19428
+ const dp = DataPacket.decode(new Uint8Array(buffer));
19429
+ if (((_a = dp.value) === null || _a === void 0 ? void 0 : _a.$case) === 'speaker') {
19430
+ // dispatch speaker updates
19431
+ this.emit(EngineEvent.ActiveSpeakersUpdate, dp.value.speaker.speakers);
19432
+ } else if (((_b = dp.value) === null || _b === void 0 ? void 0 : _b.$case) === 'user') {
19433
+ this.emit(EngineEvent.DataPacketReceived, dp.value.user, dp.kind);
19434
+ }
19435
+ } finally {
19436
+ unlock();
19627
19437
  }
19628
- this.prevStats = stats;
19629
19438
  });
19630
- this.checkForSilence();
19631
- }
19632
- setDeviceId(deviceId) {
19633
- return __awaiter(this, void 0, void 0, function* () {
19634
- if (this._constraints.deviceId === deviceId) {
19635
- return true;
19439
+ this.handleDataError = event => {
19440
+ const channel = event.currentTarget;
19441
+ const channelKind = channel.maxRetransmits === 0 ? 'lossy' : 'reliable';
19442
+ if (event instanceof ErrorEvent) {
19443
+ const {
19444
+ error
19445
+ } = event.error;
19446
+ livekitLogger.error("DataChannel error on ".concat(channelKind, ": ").concat(event.message), error);
19447
+ } else {
19448
+ livekitLogger.error("Unknown DataChannel Error on ".concat(channelKind), event);
19636
19449
  }
19637
- this._constraints.deviceId = deviceId;
19638
- if (!this.isMuted) {
19639
- yield this.restartTrack();
19450
+ };
19451
+ this.handleBufferedAmountLow = event => {
19452
+ const channel = event.currentTarget;
19453
+ const channelKind = channel.maxRetransmits === 0 ? DataPacket_Kind.LOSSY : DataPacket_Kind.RELIABLE;
19454
+ this.updateAndEmitDCBufferStatus(channelKind);
19455
+ };
19456
+ // websocket reconnect behavior. if websocket is interrupted, and the PeerConnection
19457
+ // continues to work, we can reconnect to websocket to continue the session
19458
+ // after a number of retries, we'll close and give up permanently
19459
+ this.handleDisconnect = (connection, disconnectReason) => {
19460
+ if (this._isClosed) {
19461
+ return;
19640
19462
  }
19641
- return this.isMuted || unwrapConstraint(deviceId) === this.mediaStreamTrack.getSettings().deviceId;
19642
- });
19643
- }
19644
- mute() {
19645
- const _super = Object.create(null, {
19646
- mute: {
19647
- get: () => super.mute
19463
+ livekitLogger.warn("".concat(connection, " disconnected"));
19464
+ if (this.reconnectAttempts === 0) {
19465
+ // only reset start time on the first try
19466
+ this.reconnectStart = Date.now();
19648
19467
  }
19649
- });
19650
- return __awaiter(this, void 0, void 0, function* () {
19651
- const unlock = yield this.muteLock.lock();
19652
- try {
19653
- // disabled special handling as it will cause BT headsets to switch communication modes
19654
- if (this.source === Track.Source.Microphone && this.stopOnMute && !this.isUserProvided) {
19655
- livekitLogger.debug('stopping mic track');
19656
- // also stop the track, so that microphone indicator is turned off
19657
- this._mediaStreamTrack.stop();
19658
- }
19659
- yield _super.mute.call(this);
19660
- return this;
19661
- } finally {
19662
- unlock();
19468
+ const disconnect = duration => {
19469
+ livekitLogger.warn("could not recover connection after ".concat(this.reconnectAttempts, " attempts, ").concat(duration, "ms. giving up"));
19470
+ this.emit(EngineEvent.Disconnected);
19471
+ this.close();
19472
+ };
19473
+ const duration = Date.now() - this.reconnectStart;
19474
+ let delay = this.getNextRetryDelay({
19475
+ elapsedMs: duration,
19476
+ retryCount: this.reconnectAttempts
19477
+ });
19478
+ if (delay === null) {
19479
+ disconnect(duration);
19480
+ return;
19663
19481
  }
19664
- });
19665
- }
19666
- unmute() {
19667
- const _super = Object.create(null, {
19668
- unmute: {
19669
- get: () => super.unmute
19482
+ if (connection === leaveReconnect) {
19483
+ delay = 0;
19670
19484
  }
19671
- });
19672
- return __awaiter(this, void 0, void 0, function* () {
19673
- const unlock = yield this.muteLock.lock();
19674
- try {
19675
- const deviceHasChanged = this._constraints.deviceId && this._mediaStreamTrack.getSettings().deviceId !== unwrapConstraint(this._constraints.deviceId);
19676
- if (this.source === Track.Source.Microphone && (this.stopOnMute || this._mediaStreamTrack.readyState === 'ended' || deviceHasChanged) && !this.isUserProvided) {
19677
- livekitLogger.debug('reacquiring mic track');
19678
- yield this.restartTrack();
19679
- }
19680
- yield _super.unmute.call(this);
19681
- return this;
19682
- } finally {
19683
- unlock();
19485
+ livekitLogger.debug("reconnecting in ".concat(delay, "ms"));
19486
+ this.clearReconnectTimeout();
19487
+ if (this.url && this.token && isCloud(new URL(this.url))) {
19488
+ this.regionUrlProvider = new RegionUrlProvider(this.url, this.token);
19684
19489
  }
19685
- });
19686
- }
19687
- restartTrack(options) {
19688
- return __awaiter(this, void 0, void 0, function* () {
19689
- let constraints;
19690
- if (options) {
19691
- const streamConstraints = constraintsForOptions({
19692
- audio: options
19693
- });
19694
- if (typeof streamConstraints.audio !== 'boolean') {
19695
- constraints = streamConstraints.audio;
19490
+ this.reconnectTimeout = CriticalTimers.setTimeout(() => this.attemptReconnect(disconnectReason), delay);
19491
+ };
19492
+ this.waitForRestarted = () => {
19493
+ return new Promise((resolve, reject) => {
19494
+ if (this.pcState === PCState.Connected) {
19495
+ resolve();
19696
19496
  }
19497
+ const onRestarted = () => {
19498
+ this.off(EngineEvent.Disconnected, onDisconnected);
19499
+ resolve();
19500
+ };
19501
+ const onDisconnected = () => {
19502
+ this.off(EngineEvent.Restarted, onRestarted);
19503
+ reject();
19504
+ };
19505
+ this.once(EngineEvent.Restarted, onRestarted);
19506
+ this.once(EngineEvent.Disconnected, onDisconnected);
19507
+ this.once(EngineEvent.Closing, onDisconnected);
19508
+ });
19509
+ };
19510
+ this.updateAndEmitDCBufferStatus = kind => {
19511
+ const status = this.isBufferStatusLow(kind);
19512
+ if (typeof status !== 'undefined' && status !== this.dcBufferStatus.get(kind)) {
19513
+ this.dcBufferStatus.set(kind, status);
19514
+ this.emit(EngineEvent.DCBufferStatusChanged, status, kind);
19697
19515
  }
19698
- yield this.restart(constraints);
19699
- });
19700
- }
19701
- restart(constraints) {
19702
- const _super = Object.create(null, {
19703
- restart: {
19704
- get: () => super.restart
19516
+ };
19517
+ this.isBufferStatusLow = kind => {
19518
+ const dc = this.dataChannelForKind(kind);
19519
+ if (dc) {
19520
+ return dc.bufferedAmount <= dc.bufferedAmountLowThreshold;
19705
19521
  }
19706
- });
19707
- return __awaiter(this, void 0, void 0, function* () {
19708
- const track = yield _super.restart.call(this, constraints);
19709
- this.checkForSilence();
19710
- return track;
19711
- });
19712
- }
19713
- /* @internal */
19714
- startMonitor() {
19715
- if (!isWeb()) {
19716
- return;
19717
- }
19718
- if (this.monitorInterval) {
19719
- return;
19720
- }
19721
- this.monitorInterval = setInterval(() => {
19722
- this.monitorSender();
19723
- }, monitorFrequency);
19724
- }
19725
- getSenderStats() {
19726
- var _a;
19727
- return __awaiter(this, void 0, void 0, function* () {
19728
- if (!((_a = this.sender) === null || _a === void 0 ? void 0 : _a.getStats)) {
19729
- return undefined;
19522
+ };
19523
+ this.handleBrowserOnLine = () => {
19524
+ // in case the engine is currently reconnecting, attempt a reconnect immediately after the browser state has changed to 'onLine'
19525
+ if (this.client.isReconnecting) {
19526
+ this.clearReconnectTimeout();
19527
+ this.attemptReconnect(ReconnectReason.RR_SIGNAL_DISCONNECTED);
19730
19528
  }
19731
- const stats = yield this.sender.getStats();
19732
- let audioStats;
19733
- stats.forEach(v => {
19734
- if (v.type === 'outbound-rtp') {
19735
- audioStats = {
19736
- type: 'audio',
19737
- streamId: v.id,
19738
- packetsSent: v.packetsSent,
19739
- packetsLost: v.packetsLost,
19740
- bytesSent: v.bytesSent,
19741
- timestamp: v.timestamp,
19742
- roundTripTime: v.roundTripTime,
19743
- jitter: v.jitter
19744
- };
19745
- }
19746
- });
19747
- return audioStats;
19748
- });
19529
+ };
19530
+ this.client = new SignalClient();
19531
+ this.client.signalLatency = this.options.expSignalLatency;
19532
+ this.reconnectPolicy = this.options.reconnectPolicy;
19533
+ this.registerOnLineListener();
19534
+ this.closingLock = new Mutex();
19535
+ this.dataProcessLock = new Mutex();
19536
+ this.dcBufferStatus = new Map([[DataPacket_Kind.LOSSY, true], [DataPacket_Kind.RELIABLE, true]]);
19537
+ this.client.onParticipantUpdate = updates => this.emit(EngineEvent.ParticipantUpdate, updates);
19538
+ this.client.onConnectionQuality = update => this.emit(EngineEvent.ConnectionQualityUpdate, update);
19539
+ this.client.onRoomUpdate = update => this.emit(EngineEvent.RoomUpdate, update);
19540
+ this.client.onSubscriptionError = resp => this.emit(EngineEvent.SubscriptionError, resp);
19541
+ this.client.onSubscriptionPermissionUpdate = update => this.emit(EngineEvent.SubscriptionPermissionUpdate, update);
19542
+ this.client.onSpeakersChanged = update => this.emit(EngineEvent.SpeakersChanged, update);
19543
+ this.client.onStreamStateUpdate = update => this.emit(EngineEvent.StreamStateChanged, update);
19749
19544
  }
19750
- checkForSilence() {
19545
+ join(url, token, opts, abortSignal) {
19751
19546
  return __awaiter(this, void 0, void 0, function* () {
19752
- const trackIsSilent = yield detectSilence(this);
19753
- if (trackIsSilent) {
19754
- if (!this.isMuted) {
19755
- livekitLogger.warn('silence detected on local audio track');
19547
+ this.url = url;
19548
+ this.token = token;
19549
+ this.signalOpts = opts;
19550
+ try {
19551
+ this.joinAttempts += 1;
19552
+ const joinResponse = yield this.client.join(url, token, opts, abortSignal);
19553
+ this._isClosed = false;
19554
+ this.latestJoinResponse = joinResponse;
19555
+ this.subscriberPrimary = joinResponse.subscriberPrimary;
19556
+ if (!this.publisher) {
19557
+ this.configure(joinResponse);
19756
19558
  }
19757
- this.emit(TrackEvent.AudioSilenceDetected);
19559
+ // create offer
19560
+ if (!this.subscriberPrimary) {
19561
+ this.negotiate();
19562
+ }
19563
+ this.setupSignalClientCallbacks();
19564
+ this.clientConfiguration = joinResponse.clientConfiguration;
19565
+ return joinResponse;
19566
+ } catch (e) {
19567
+ if (e instanceof ConnectionError) {
19568
+ if (e.reason === 1 /* ConnectionErrorReason.ServerUnreachable */) {
19569
+ livekitLogger.warn("Couldn't connect to server, attempt ".concat(this.joinAttempts, " of ").concat(this.maxJoinAttempts));
19570
+ if (this.joinAttempts < this.maxJoinAttempts) {
19571
+ return this.join(url, token, opts, abortSignal);
19572
+ }
19573
+ }
19574
+ }
19575
+ throw e;
19758
19576
  }
19759
- return trackIsSilent;
19760
- });
19761
- }
19762
- }
19763
-
19764
- /** @internal */
19765
- function mediaTrackToLocalTrack(mediaStreamTrack, constraints) {
19766
- switch (mediaStreamTrack.kind) {
19767
- case 'audio':
19768
- return new LocalAudioTrack(mediaStreamTrack, constraints, false);
19769
- case 'video':
19770
- return new LocalVideoTrack(mediaStreamTrack, constraints, false);
19771
- default:
19772
- throw new TrackInvalidError("unsupported track type: ".concat(mediaStreamTrack.kind));
19773
- }
19774
- }
19775
- /* @internal */
19776
- const presets169 = Object.values(VideoPresets);
19777
- /* @internal */
19778
- const presets43 = Object.values(VideoPresets43);
19779
- /* @internal */
19780
- const presetsScreenShare = Object.values(ScreenSharePresets);
19781
- /* @internal */
19782
- const defaultSimulcastPresets169 = [VideoPresets.h180, VideoPresets.h360];
19783
- /* @internal */
19784
- const defaultSimulcastPresets43 = [VideoPresets43.h180, VideoPresets43.h360];
19785
- /* @internal */
19786
- const computeDefaultScreenShareSimulcastPresets = fromPreset => {
19787
- const layers = [{
19788
- scaleResolutionDownBy: 2,
19789
- fps: 3
19790
- }];
19791
- return layers.map(t => {
19792
- var _a;
19793
- return new VideoPreset(Math.floor(fromPreset.width / t.scaleResolutionDownBy), Math.floor(fromPreset.height / t.scaleResolutionDownBy), Math.max(150000, Math.floor(fromPreset.encoding.maxBitrate / (Math.pow(t.scaleResolutionDownBy, 2) * (((_a = fromPreset.encoding.maxFramerate) !== null && _a !== void 0 ? _a : 30) / t.fps)))), t.fps, fromPreset.encoding.priority);
19794
- });
19795
- };
19796
- // /**
19797
- // *
19798
- // * @internal
19799
- // * @experimental
19800
- // */
19801
- // const computeDefaultMultiCodecSimulcastEncodings = (width: number, height: number) => {
19802
- // // use vp8 as a default
19803
- // const vp8 = determineAppropriateEncoding(false, width, height);
19804
- // const vp9 = { ...vp8, maxBitrate: vp8.maxBitrate * 0.9 };
19805
- // const h264 = { ...vp8, maxBitrate: vp8.maxBitrate * 1.1 };
19806
- // const av1 = { ...vp8, maxBitrate: vp8.maxBitrate * 0.7 };
19807
- // return {
19808
- // vp8,
19809
- // vp9,
19810
- // h264,
19811
- // av1,
19812
- // };
19813
- // };
19814
- const videoRids = ['q', 'h', 'f'];
19815
- /* @internal */
19816
- function computeVideoEncodings(isScreenShare, width, height, options) {
19817
- var _a, _b;
19818
- let videoEncoding = options === null || options === void 0 ? void 0 : options.videoEncoding;
19819
- if (isScreenShare) {
19820
- videoEncoding = options === null || options === void 0 ? void 0 : options.screenShareEncoding;
19821
- }
19822
- const useSimulcast = options === null || options === void 0 ? void 0 : options.simulcast;
19823
- const scalabilityMode = options === null || options === void 0 ? void 0 : options.scalabilityMode;
19824
- const videoCodec = options === null || options === void 0 ? void 0 : options.videoCodec;
19825
- if (!videoEncoding && !useSimulcast && !scalabilityMode || !width || !height) {
19826
- // when we aren't simulcasting or svc, will need to return a single encoding without
19827
- // capping bandwidth. we always require a encoding for dynacast
19828
- return [{}];
19829
- }
19830
- if (!videoEncoding) {
19831
- // find the right encoding based on width/height
19832
- videoEncoding = determineAppropriateEncoding(isScreenShare, width, height, videoCodec);
19833
- livekitLogger.debug('using video encoding', videoEncoding);
19834
- }
19835
- const original = new VideoPreset(width, height, videoEncoding.maxBitrate, videoEncoding.maxFramerate, videoEncoding.priority);
19836
- if (scalabilityMode && isSVCCodec(videoCodec)) {
19837
- livekitLogger.debug("using svc with scalabilityMode ".concat(scalabilityMode));
19838
- const sm = new ScalabilityMode(scalabilityMode);
19839
- const encodings = [];
19840
- if (sm.spatial > 3) {
19841
- throw new Error("unsupported scalabilityMode: ".concat(scalabilityMode));
19842
- }
19843
- for (let i = 0; i < sm.spatial; i += 1) {
19844
- encodings.push({
19845
- rid: videoRids[2 - i],
19846
- maxBitrate: videoEncoding.maxBitrate / Math.pow(3, i),
19847
- /* @ts-ignore */
19848
- maxFramerate: original.encoding.maxFramerate
19849
- });
19850
- }
19851
- /* @ts-ignore */
19852
- encodings[0].scalabilityMode = scalabilityMode;
19853
- livekitLogger.debug('encodings', encodings);
19854
- return encodings;
19855
- }
19856
- if (!useSimulcast) {
19857
- return [videoEncoding];
19858
- }
19859
- let presets = [];
19860
- if (isScreenShare) {
19861
- presets = (_a = sortPresets(options === null || options === void 0 ? void 0 : options.screenShareSimulcastLayers)) !== null && _a !== void 0 ? _a : defaultSimulcastLayers(isScreenShare, original);
19862
- } else {
19863
- presets = (_b = sortPresets(options === null || options === void 0 ? void 0 : options.videoSimulcastLayers)) !== null && _b !== void 0 ? _b : defaultSimulcastLayers(isScreenShare, original);
19864
- }
19865
- let midPreset;
19866
- if (presets.length > 0) {
19867
- const lowPreset = presets[0];
19868
- if (presets.length > 1) {
19869
- [, midPreset] = presets;
19870
- }
19871
- // NOTE:
19872
- // 1. Ordering of these encodings is important. Chrome seems
19873
- // to use the index into encodings to decide which layer
19874
- // to disable when CPU constrained.
19875
- // So encodings should be ordered in increasing spatial
19876
- // resolution order.
19877
- // 2. ion-sfu translates rids into layers. So, all encodings
19878
- // should have the base layer `q` and then more added
19879
- // based on other conditions.
19880
- const size = Math.max(width, height);
19881
- if (size >= 960 && midPreset) {
19882
- return encodingsFromPresets(width, height, [lowPreset, midPreset, original]);
19883
- }
19884
- if (size >= 480) {
19885
- return encodingsFromPresets(width, height, [lowPreset, original]);
19886
- }
19577
+ });
19887
19578
  }
19888
- return encodingsFromPresets(width, height, [original]);
19889
- }
19890
- function computeTrackBackupEncodings(track, videoCodec, opts) {
19891
- var _a, _b, _c, _d;
19892
- if (!opts.backupCodec || opts.backupCodec.codec === opts.videoCodec) {
19893
- // backup codec publishing is disabled
19894
- return;
19579
+ close() {
19580
+ return __awaiter(this, void 0, void 0, function* () {
19581
+ const unlock = yield this.closingLock.lock();
19582
+ if (this.isClosed) {
19583
+ unlock();
19584
+ return;
19585
+ }
19586
+ try {
19587
+ this._isClosed = true;
19588
+ this.emit(EngineEvent.Closing);
19589
+ this.removeAllListeners();
19590
+ this.deregisterOnLineListener();
19591
+ this.clearPendingReconnect();
19592
+ yield this.cleanupPeerConnections();
19593
+ yield this.cleanupClient();
19594
+ } finally {
19595
+ unlock();
19596
+ }
19597
+ });
19895
19598
  }
19896
- if (videoCodec !== opts.backupCodec.codec) {
19897
- livekitLogger.warn('requested a different codec than specified as backup', {
19898
- serverRequested: videoCodec,
19899
- backup: opts.backupCodec.codec
19599
+ cleanupPeerConnections() {
19600
+ return __awaiter(this, void 0, void 0, function* () {
19601
+ if (this.publisher && this.publisher.pc.signalingState !== 'closed') {
19602
+ this.publisher.pc.getSenders().forEach(sender => {
19603
+ var _a, _b;
19604
+ try {
19605
+ // TODO: react-native-webrtc doesn't have removeTrack yet.
19606
+ if ((_a = this.publisher) === null || _a === void 0 ? void 0 : _a.pc.removeTrack) {
19607
+ (_b = this.publisher) === null || _b === void 0 ? void 0 : _b.pc.removeTrack(sender);
19608
+ }
19609
+ } catch (e) {
19610
+ livekitLogger.warn('could not removeTrack', {
19611
+ error: e
19612
+ });
19613
+ }
19614
+ });
19615
+ }
19616
+ if (this.publisher) {
19617
+ this.publisher.close();
19618
+ this.publisher = undefined;
19619
+ }
19620
+ if (this.subscriber) {
19621
+ this.subscriber.close();
19622
+ this.subscriber = undefined;
19623
+ }
19624
+ this.primaryPC = undefined;
19625
+ const dcCleanup = dc => {
19626
+ if (!dc) return;
19627
+ dc.close();
19628
+ dc.onbufferedamountlow = null;
19629
+ dc.onclose = null;
19630
+ dc.onclosing = null;
19631
+ dc.onerror = null;
19632
+ dc.onmessage = null;
19633
+ dc.onopen = null;
19634
+ };
19635
+ dcCleanup(this.lossyDC);
19636
+ dcCleanup(this.lossyDCSub);
19637
+ dcCleanup(this.reliableDC);
19638
+ dcCleanup(this.reliableDCSub);
19639
+ this.lossyDC = undefined;
19640
+ this.lossyDCSub = undefined;
19641
+ this.reliableDC = undefined;
19642
+ this.reliableDCSub = undefined;
19900
19643
  });
19901
19644
  }
19902
- opts.videoCodec = videoCodec;
19903
- // use backup encoding setting as videoEncoding for backup codec publishing
19904
- opts.videoEncoding = opts.backupCodec.encoding;
19905
- const settings = track.mediaStreamTrack.getSettings();
19906
- const width = (_a = settings.width) !== null && _a !== void 0 ? _a : (_b = track.dimensions) === null || _b === void 0 ? void 0 : _b.width;
19907
- const height = (_c = settings.height) !== null && _c !== void 0 ? _c : (_d = track.dimensions) === null || _d === void 0 ? void 0 : _d.height;
19908
- const encodings = computeVideoEncodings(track.source === Track.Source.ScreenShare, width, height, opts);
19909
- return encodings;
19910
- }
19911
- /* @internal */
19912
- function determineAppropriateEncoding(isScreenShare, width, height, codec) {
19913
- const presets = presetsForResolution(isScreenShare, width, height);
19914
- let {
19915
- encoding
19916
- } = presets[0];
19917
- // handle portrait by swapping dimensions
19918
- const size = Math.max(width, height);
19919
- for (let i = 0; i < presets.length; i += 1) {
19920
- const preset = presets[i];
19921
- encoding = preset.encoding;
19922
- if (preset.width >= size) {
19923
- break;
19924
- }
19645
+ cleanupClient() {
19646
+ return __awaiter(this, void 0, void 0, function* () {
19647
+ yield this.client.close();
19648
+ this.client.resetCallbacks();
19649
+ });
19925
19650
  }
19926
- // presets are based on the assumption of vp8 as a codec
19927
- // for other codecs we adjust the maxBitrate if no specific videoEncoding has been provided
19928
- // TODO make the bitrate multipliers configurable per codec
19929
- if (codec) {
19930
- switch (codec) {
19931
- case 'av1':
19932
- encoding = Object.assign({}, encoding);
19933
- encoding.maxBitrate = encoding.maxBitrate * 0.7;
19934
- break;
19935
- case 'vp9':
19936
- encoding = Object.assign({}, encoding);
19937
- encoding.maxBitrate = encoding.maxBitrate * 0.85;
19938
- break;
19651
+ addTrack(req) {
19652
+ if (this.pendingTrackResolvers[req.cid]) {
19653
+ throw new TrackInvalidError('a track with the same ID has already been published');
19939
19654
  }
19655
+ return new Promise((resolve, reject) => {
19656
+ const publicationTimeout = setTimeout(() => {
19657
+ delete this.pendingTrackResolvers[req.cid];
19658
+ reject(new ConnectionError('publication of local track timed out, no response from server'));
19659
+ }, 10000);
19660
+ this.pendingTrackResolvers[req.cid] = {
19661
+ resolve: info => {
19662
+ clearTimeout(publicationTimeout);
19663
+ resolve(info);
19664
+ },
19665
+ reject: () => {
19666
+ clearTimeout(publicationTimeout);
19667
+ reject(new Error('Cancelled publication by calling unpublish'));
19668
+ }
19669
+ };
19670
+ this.client.sendAddTrack(req);
19671
+ });
19940
19672
  }
19941
- return encoding;
19942
- }
19943
- /* @internal */
19944
- function presetsForResolution(isScreenShare, width, height) {
19945
- if (isScreenShare) {
19946
- return presetsScreenShare;
19673
+ /**
19674
+ * Removes sender from PeerConnection, returning true if it was removed successfully
19675
+ * and a negotiation is necessary
19676
+ * @param sender
19677
+ * @returns
19678
+ */
19679
+ removeTrack(sender) {
19680
+ var _a;
19681
+ if (sender.track && this.pendingTrackResolvers[sender.track.id]) {
19682
+ const {
19683
+ reject
19684
+ } = this.pendingTrackResolvers[sender.track.id];
19685
+ if (reject) {
19686
+ reject();
19687
+ }
19688
+ delete this.pendingTrackResolvers[sender.track.id];
19689
+ }
19690
+ try {
19691
+ (_a = this.publisher) === null || _a === void 0 ? void 0 : _a.pc.removeTrack(sender);
19692
+ return true;
19693
+ } catch (e) {
19694
+ livekitLogger.warn('failed to remove track', {
19695
+ error: e,
19696
+ method: 'removeTrack'
19697
+ });
19698
+ }
19699
+ return false;
19947
19700
  }
19948
- const aspect = width > height ? width / height : height / width;
19949
- if (Math.abs(aspect - 16.0 / 9) < Math.abs(aspect - 4.0 / 3)) {
19950
- return presets169;
19701
+ updateMuteStatus(trackSid, muted) {
19702
+ this.client.sendMuteTrack(trackSid, muted);
19951
19703
  }
19952
- return presets43;
19953
- }
19954
- /* @internal */
19955
- function defaultSimulcastLayers(isScreenShare, original) {
19956
- if (isScreenShare) {
19957
- return computeDefaultScreenShareSimulcastPresets(original);
19704
+ get dataSubscriberReadyState() {
19705
+ var _a;
19706
+ return (_a = this.reliableDCSub) === null || _a === void 0 ? void 0 : _a.readyState;
19958
19707
  }
19959
- const {
19960
- width,
19961
- height
19962
- } = original;
19963
- const aspect = width > height ? width / height : height / width;
19964
- if (Math.abs(aspect - 16.0 / 9) < Math.abs(aspect - 4.0 / 3)) {
19965
- return defaultSimulcastPresets169;
19708
+ getConnectedServerAddress() {
19709
+ return __awaiter(this, void 0, void 0, function* () {
19710
+ if (this.primaryPC === undefined) {
19711
+ return undefined;
19712
+ }
19713
+ return getConnectedAddress(this.primaryPC);
19714
+ });
19966
19715
  }
19967
- return defaultSimulcastPresets43;
19968
- }
19969
- // presets should be ordered by low, medium, high
19970
- function encodingsFromPresets(width, height, presets) {
19971
- const encodings = [];
19972
- presets.forEach((preset, idx) => {
19973
- if (idx >= videoRids.length) {
19716
+ configure(joinResponse) {
19717
+ var _a, _b;
19718
+ // already configured
19719
+ if (this.publisher || this.subscriber) {
19974
19720
  return;
19975
19721
  }
19976
- const size = Math.min(width, height);
19977
- const rid = videoRids[idx];
19978
- const encoding = {
19979
- rid,
19980
- scaleResolutionDownBy: Math.max(1, size / Math.min(preset.width, preset.height)),
19981
- maxBitrate: preset.encoding.maxBitrate
19982
- };
19983
- if (preset.encoding.maxFramerate) {
19984
- encoding.maxFramerate = preset.encoding.maxFramerate;
19722
+ this.participantSid = (_a = joinResponse.participant) === null || _a === void 0 ? void 0 : _a.sid;
19723
+ const rtcConfig = this.makeRTCConfiguration(joinResponse);
19724
+ if ((_b = this.signalOpts) === null || _b === void 0 ? void 0 : _b.e2eeEnabled) {
19725
+ livekitLogger.debug('E2EE - setting up transports with insertable streams');
19726
+ // this makes sure that no data is sent before the transforms are ready
19727
+ // @ts-ignore
19728
+ rtcConfig.encodedInsertableStreams = true;
19985
19729
  }
19986
- const canSetPriority = isFireFox() || idx === 0;
19987
- if (preset.encoding.priority && canSetPriority) {
19988
- encoding.priority = preset.encoding.priority;
19989
- encoding.networkPriority = preset.encoding.priority;
19730
+ const googConstraints = {
19731
+ optional: [{
19732
+ googDscp: true
19733
+ }]
19734
+ };
19735
+ this.publisher = new PCTransport(rtcConfig, googConstraints);
19736
+ this.subscriber = new PCTransport(rtcConfig);
19737
+ this.emit(EngineEvent.TransportsCreated, this.publisher, this.subscriber);
19738
+ this.publisher.pc.onicecandidate = ev => {
19739
+ if (!ev.candidate) return;
19740
+ livekitLogger.trace('adding ICE candidate for peer', ev.candidate);
19741
+ this.client.sendIceCandidate(ev.candidate, SignalTarget.PUBLISHER);
19742
+ };
19743
+ this.subscriber.pc.onicecandidate = ev => {
19744
+ if (!ev.candidate) return;
19745
+ this.client.sendIceCandidate(ev.candidate, SignalTarget.SUBSCRIBER);
19746
+ };
19747
+ this.publisher.onOffer = offer => {
19748
+ this.client.sendOffer(offer);
19749
+ };
19750
+ let primaryPC = this.publisher.pc;
19751
+ let secondaryPC = this.subscriber.pc;
19752
+ let subscriberPrimary = joinResponse.subscriberPrimary;
19753
+ if (subscriberPrimary) {
19754
+ primaryPC = this.subscriber.pc;
19755
+ secondaryPC = this.publisher.pc;
19756
+ // in subscriber primary mode, server side opens sub data channels.
19757
+ this.subscriber.pc.ondatachannel = this.handleDataChannel;
19990
19758
  }
19991
- encodings.push(encoding);
19992
- });
19993
- // RN ios simulcast requires all same framerates.
19994
- if (isReactNative() && getReactNativeOs() === 'ios') {
19995
- let topFramerate = undefined;
19996
- encodings.forEach(encoding => {
19997
- if (!topFramerate) {
19998
- topFramerate = encoding.maxFramerate;
19999
- } else if (encoding.maxFramerate && encoding.maxFramerate > topFramerate) {
20000
- topFramerate = encoding.maxFramerate;
19759
+ this.primaryPC = primaryPC;
19760
+ primaryPC.onconnectionstatechange = () => __awaiter(this, void 0, void 0, function* () {
19761
+ livekitLogger.debug("primary PC state changed ".concat(primaryPC.connectionState));
19762
+ if (primaryPC.connectionState === 'connected') {
19763
+ const shouldEmit = this.pcState === PCState.New;
19764
+ this.pcState = PCState.Connected;
19765
+ if (shouldEmit) {
19766
+ this.emit(EngineEvent.Connected, joinResponse);
19767
+ }
19768
+ } else if (primaryPC.connectionState === 'failed') {
19769
+ // on Safari, PeerConnection will switch to 'disconnected' during renegotiation
19770
+ if (this.pcState === PCState.Connected) {
19771
+ this.pcState = PCState.Disconnected;
19772
+ this.handleDisconnect('primary peerconnection', subscriberPrimary ? ReconnectReason.RR_SUBSCRIBER_FAILED : ReconnectReason.RR_PUBLISHER_FAILED);
19773
+ }
20001
19774
  }
20002
19775
  });
20003
- let notifyOnce = true;
20004
- encodings.forEach(encoding => {
20005
- var _a;
20006
- if (encoding.maxFramerate != topFramerate) {
20007
- if (notifyOnce) {
20008
- notifyOnce = false;
20009
- livekitLogger.info("Simulcast on iOS React-Native requires all encodings to share the same framerate.");
20010
- }
20011
- livekitLogger.info("Setting framerate of encoding \"".concat((_a = encoding.rid) !== null && _a !== void 0 ? _a : '', "\" to ").concat(topFramerate));
20012
- encoding.maxFramerate = topFramerate;
19776
+ secondaryPC.onconnectionstatechange = () => __awaiter(this, void 0, void 0, function* () {
19777
+ livekitLogger.debug("secondary PC state changed ".concat(secondaryPC.connectionState));
19778
+ // also reconnect if secondary peerconnection fails
19779
+ if (secondaryPC.connectionState === 'failed') {
19780
+ this.handleDisconnect('secondary peerconnection', subscriberPrimary ? ReconnectReason.RR_PUBLISHER_FAILED : ReconnectReason.RR_SUBSCRIBER_FAILED);
20013
19781
  }
20014
19782
  });
19783
+ this.subscriber.pc.ontrack = ev => {
19784
+ this.emit(EngineEvent.MediaTrackAdded, ev.track, ev.streams[0], ev.receiver);
19785
+ };
19786
+ this.createDataChannels();
20015
19787
  }
20016
- return encodings;
20017
- }
20018
- /** @internal */
20019
- function sortPresets(presets) {
20020
- if (!presets) return;
20021
- return presets.sort((a, b) => {
20022
- const {
20023
- encoding: aEnc
20024
- } = a;
20025
- const {
20026
- encoding: bEnc
20027
- } = b;
20028
- if (aEnc.maxBitrate > bEnc.maxBitrate) {
20029
- return 1;
19788
+ setupSignalClientCallbacks() {
19789
+ // configure signaling client
19790
+ this.client.onAnswer = sd => __awaiter(this, void 0, void 0, function* () {
19791
+ if (!this.publisher) {
19792
+ return;
19793
+ }
19794
+ livekitLogger.debug('received server answer', {
19795
+ RTCSdpType: sd.type,
19796
+ signalingState: this.publisher.pc.signalingState.toString()
19797
+ });
19798
+ yield this.publisher.setRemoteDescription(sd);
19799
+ });
19800
+ // add candidate on trickle
19801
+ this.client.onTrickle = (candidate, target) => {
19802
+ if (!this.publisher || !this.subscriber) {
19803
+ return;
19804
+ }
19805
+ livekitLogger.trace('got ICE candidate from peer', {
19806
+ candidate,
19807
+ target
19808
+ });
19809
+ if (target === SignalTarget.PUBLISHER) {
19810
+ this.publisher.addIceCandidate(candidate);
19811
+ } else {
19812
+ this.subscriber.addIceCandidate(candidate);
19813
+ }
19814
+ };
19815
+ // when server creates an offer for the client
19816
+ this.client.onOffer = sd => __awaiter(this, void 0, void 0, function* () {
19817
+ if (!this.subscriber) {
19818
+ return;
19819
+ }
19820
+ livekitLogger.debug('received server offer', {
19821
+ RTCSdpType: sd.type,
19822
+ signalingState: this.subscriber.pc.signalingState.toString()
19823
+ });
19824
+ yield this.subscriber.setRemoteDescription(sd);
19825
+ // answer the offer
19826
+ const answer = yield this.subscriber.createAndSetAnswer();
19827
+ this.client.sendAnswer(answer);
19828
+ });
19829
+ this.client.onLocalTrackPublished = res => {
19830
+ livekitLogger.debug('received trackPublishedResponse', res);
19831
+ const {
19832
+ resolve
19833
+ } = this.pendingTrackResolvers[res.cid];
19834
+ if (!resolve) {
19835
+ livekitLogger.error("missing track resolver for ".concat(res.cid));
19836
+ return;
19837
+ }
19838
+ delete this.pendingTrackResolvers[res.cid];
19839
+ resolve(res.track);
19840
+ };
19841
+ this.client.onTokenRefresh = token => {
19842
+ this.token = token;
19843
+ };
19844
+ this.client.onClose = () => {
19845
+ this.handleDisconnect('signal', ReconnectReason.RR_SIGNAL_DISCONNECTED);
19846
+ };
19847
+ this.client.onLeave = leave => {
19848
+ if (leave === null || leave === void 0 ? void 0 : leave.canReconnect) {
19849
+ this.fullReconnectOnNext = true;
19850
+ this.primaryPC = undefined;
19851
+ // reconnect immediately instead of waiting for next attempt
19852
+ this.handleDisconnect(leaveReconnect);
19853
+ } else {
19854
+ this.emit(EngineEvent.Disconnected, leave === null || leave === void 0 ? void 0 : leave.reason);
19855
+ this.close();
19856
+ }
19857
+ livekitLogger.trace('leave request', {
19858
+ leave
19859
+ });
19860
+ };
19861
+ }
19862
+ makeRTCConfiguration(serverResponse) {
19863
+ const rtcConfig = Object.assign({}, this.rtcConfig);
19864
+ // update ICE servers before creating PeerConnection
19865
+ if (serverResponse.iceServers && !rtcConfig.iceServers) {
19866
+ const rtcIceServers = [];
19867
+ serverResponse.iceServers.forEach(iceServer => {
19868
+ const rtcIceServer = {
19869
+ urls: iceServer.urls
19870
+ };
19871
+ if (iceServer.username) rtcIceServer.username = iceServer.username;
19872
+ if (iceServer.credential) {
19873
+ rtcIceServer.credential = iceServer.credential;
19874
+ }
19875
+ rtcIceServers.push(rtcIceServer);
19876
+ });
19877
+ rtcConfig.iceServers = rtcIceServers;
20030
19878
  }
20031
- if (aEnc.maxBitrate < bEnc.maxBitrate) return -1;
20032
- if (aEnc.maxBitrate === bEnc.maxBitrate && aEnc.maxFramerate && bEnc.maxFramerate) {
20033
- return aEnc.maxFramerate > bEnc.maxFramerate ? 1 : -1;
19879
+ if (serverResponse.clientConfiguration && serverResponse.clientConfiguration.forceRelay === ClientConfigSetting.ENABLED) {
19880
+ rtcConfig.iceTransportPolicy = 'relay';
20034
19881
  }
20035
- return 0;
20036
- });
20037
- }
20038
- /** @internal */
20039
- class ScalabilityMode {
20040
- constructor(scalabilityMode) {
20041
- const results = scalabilityMode.match(/^L(\d)T(\d)(h|_KEY|_KEY_SHIFT){0,1}$/);
20042
- if (!results) {
20043
- throw new Error('invalid scalability mode');
19882
+ // @ts-ignore
19883
+ rtcConfig.sdpSemantics = 'unified-plan';
19884
+ // @ts-ignore
19885
+ rtcConfig.continualGatheringPolicy = 'gather_continually';
19886
+ return rtcConfig;
19887
+ }
19888
+ createDataChannels() {
19889
+ if (!this.publisher) {
19890
+ return;
20044
19891
  }
20045
- this.spatial = parseInt(results[1]);
20046
- this.temporal = parseInt(results[2]);
20047
- if (results.length > 3) {
20048
- switch (results[3]) {
20049
- case 'h':
20050
- case '_KEY':
20051
- case '_KEY_SHIFT':
20052
- this.suffix = results[3];
20053
- }
19892
+ // clear old data channel callbacks if recreate
19893
+ if (this.lossyDC) {
19894
+ this.lossyDC.onmessage = null;
19895
+ this.lossyDC.onerror = null;
20054
19896
  }
19897
+ if (this.reliableDC) {
19898
+ this.reliableDC.onmessage = null;
19899
+ this.reliableDC.onerror = null;
19900
+ }
19901
+ // create data channels
19902
+ this.lossyDC = this.publisher.pc.createDataChannel(lossyDataChannel, {
19903
+ // will drop older packets that arrive
19904
+ ordered: true,
19905
+ maxRetransmits: 0
19906
+ });
19907
+ this.reliableDC = this.publisher.pc.createDataChannel(reliableDataChannel, {
19908
+ ordered: true
19909
+ });
19910
+ // also handle messages over the pub channel, for backwards compatibility
19911
+ this.lossyDC.onmessage = this.handleDataMessage;
19912
+ this.reliableDC.onmessage = this.handleDataMessage;
19913
+ // handle datachannel errors
19914
+ this.lossyDC.onerror = this.handleDataError;
19915
+ this.reliableDC.onerror = this.handleDataError;
19916
+ // set up dc buffer threshold, set to 64kB (otherwise 0 by default)
19917
+ this.lossyDC.bufferedAmountLowThreshold = 65535;
19918
+ this.reliableDC.bufferedAmountLowThreshold = 65535;
19919
+ // handle buffer amount low events
19920
+ this.lossyDC.onbufferedamountlow = this.handleBufferedAmountLow;
19921
+ this.reliableDC.onbufferedamountlow = this.handleBufferedAmountLow;
20055
19922
  }
20056
- toString() {
20057
- var _a;
20058
- return "L".concat(this.spatial, "T").concat(this.temporal).concat((_a = this.suffix) !== null && _a !== void 0 ? _a : '');
20059
- }
20060
- }
20061
-
20062
- const refreshSubscribedCodecAfterNewCodec = 5000;
20063
- class LocalVideoTrack extends LocalTrack {
20064
- /**
20065
- *
20066
- * @param mediaTrack
20067
- * @param constraints MediaTrackConstraints that are being used when restarting or reacquiring tracks
20068
- * @param userProvidedTrack Signals to the SDK whether or not the mediaTrack should be managed (i.e. released and reacquired) internally by the SDK
20069
- */
20070
- constructor(mediaTrack, constraints) {
20071
- let userProvidedTrack = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
20072
- super(mediaTrack, Track.Kind.Video, constraints, userProvidedTrack);
20073
- /* @internal */
20074
- this.simulcastCodecs = new Map();
20075
- this.monitorSender = () => __awaiter(this, void 0, void 0, function* () {
20076
- if (!this.sender) {
20077
- this._currentBitrate = 0;
20078
- return;
20079
- }
20080
- let stats;
20081
- try {
20082
- stats = yield this.getSenderStats();
20083
- } catch (e) {
20084
- livekitLogger.error('could not get audio sender stats', {
20085
- error: e
20086
- });
20087
- return;
19923
+ createSender(track, opts, encodings) {
19924
+ return __awaiter(this, void 0, void 0, function* () {
19925
+ if (supportsTransceiver()) {
19926
+ const sender = yield this.createTransceiverRTCRtpSender(track, opts, encodings);
19927
+ return sender;
20088
19928
  }
20089
- const statsMap = new Map(stats.map(s => [s.rid, s]));
20090
- if (this.prevStats) {
20091
- let totalBitrate = 0;
20092
- statsMap.forEach((s, key) => {
20093
- var _a;
20094
- const prev = (_a = this.prevStats) === null || _a === void 0 ? void 0 : _a.get(key);
20095
- totalBitrate += computeBitrate(s, prev);
20096
- });
20097
- this._currentBitrate = totalBitrate;
19929
+ if (supportsAddTrack()) {
19930
+ livekitLogger.warn('using add-track fallback');
19931
+ const sender = yield this.createRTCRtpSender(track.mediaStreamTrack);
19932
+ return sender;
20098
19933
  }
20099
- this.prevStats = statsMap;
19934
+ throw new UnexpectedConnectionState('Required webRTC APIs not supported on this device');
20100
19935
  });
20101
- this.senderLock = new Mutex();
20102
19936
  }
20103
- get isSimulcast() {
20104
- if (this.sender && this.sender.getParameters().encodings.length > 1) {
20105
- return true;
20106
- }
20107
- return false;
19937
+ createSimulcastSender(track, simulcastTrack, opts, encodings) {
19938
+ return __awaiter(this, void 0, void 0, function* () {
19939
+ // store RTCRtpSender
19940
+ if (supportsTransceiver()) {
19941
+ return this.createSimulcastTransceiverSender(track, simulcastTrack, opts, encodings);
19942
+ }
19943
+ if (supportsAddTrack()) {
19944
+ livekitLogger.debug('using add-track fallback');
19945
+ return this.createRTCRtpSender(track.mediaStreamTrack);
19946
+ }
19947
+ throw new UnexpectedConnectionState('Cannot stream on this device');
19948
+ });
20108
19949
  }
20109
- /* @internal */
20110
- startMonitor(signalClient) {
20111
- var _a;
20112
- this.signalClient = signalClient;
20113
- if (!isWeb()) {
20114
- return;
20115
- }
20116
- // save original encodings
20117
- // TODO : merge simulcast tracks stats
20118
- const params = (_a = this.sender) === null || _a === void 0 ? void 0 : _a.getParameters();
20119
- if (params) {
20120
- this.encodings = params.encodings;
20121
- }
20122
- if (this.monitorInterval) {
20123
- return;
20124
- }
20125
- this.monitorInterval = setInterval(() => {
20126
- this.monitorSender();
20127
- }, monitorFrequency);
19950
+ createTransceiverRTCRtpSender(track, opts, encodings) {
19951
+ return __awaiter(this, void 0, void 0, function* () {
19952
+ if (!this.publisher) {
19953
+ throw new UnexpectedConnectionState('publisher is closed');
19954
+ }
19955
+ const streams = [];
19956
+ if (track.mediaStream) {
19957
+ streams.push(track.mediaStream);
19958
+ }
19959
+ if (track instanceof LocalVideoTrack) {
19960
+ track.codec = opts.videoCodec;
19961
+ }
19962
+ const transceiverInit = {
19963
+ direction: 'sendonly',
19964
+ streams
19965
+ };
19966
+ if (encodings) {
19967
+ transceiverInit.sendEncodings = encodings;
19968
+ }
19969
+ // addTransceiver for react-native is async. web is synchronous, but await won't effect it.
19970
+ const transceiver = yield this.publisher.pc.addTransceiver(track.mediaStreamTrack, transceiverInit);
19971
+ return transceiver.sender;
19972
+ });
20128
19973
  }
20129
- stop() {
20130
- this._mediaStreamTrack.getConstraints();
20131
- this.simulcastCodecs.forEach(trackInfo => {
20132
- trackInfo.mediaStreamTrack.stop();
19974
+ createSimulcastTransceiverSender(track, simulcastTrack, opts, encodings) {
19975
+ return __awaiter(this, void 0, void 0, function* () {
19976
+ if (!this.publisher) {
19977
+ throw new UnexpectedConnectionState('publisher is closed');
19978
+ }
19979
+ const transceiverInit = {
19980
+ direction: 'sendonly'
19981
+ };
19982
+ if (encodings) {
19983
+ transceiverInit.sendEncodings = encodings;
19984
+ }
19985
+ // addTransceiver for react-native is async. web is synchronous, but await won't effect it.
19986
+ const transceiver = yield this.publisher.pc.addTransceiver(simulcastTrack.mediaStreamTrack, transceiverInit);
19987
+ if (!opts.videoCodec) {
19988
+ return;
19989
+ }
19990
+ track.setSimulcastTrackSender(opts.videoCodec, transceiver.sender);
19991
+ return transceiver.sender;
20133
19992
  });
20134
- super.stop();
20135
19993
  }
20136
- mute() {
20137
- const _super = Object.create(null, {
20138
- mute: {
20139
- get: () => super.mute
19994
+ createRTCRtpSender(track) {
19995
+ return __awaiter(this, void 0, void 0, function* () {
19996
+ if (!this.publisher) {
19997
+ throw new UnexpectedConnectionState('publisher is closed');
20140
19998
  }
19999
+ return this.publisher.pc.addTrack(track);
20141
20000
  });
20001
+ }
20002
+ attemptReconnect(reason) {
20003
+ var _a, _b, _c;
20142
20004
  return __awaiter(this, void 0, void 0, function* () {
20143
- const unlock = yield this.muteLock.lock();
20005
+ if (this._isClosed) {
20006
+ return;
20007
+ }
20008
+ // guard for attempting reconnection multiple times while one attempt is still not finished
20009
+ if (this.attemptingReconnect) {
20010
+ return;
20011
+ }
20012
+ if (((_a = this.clientConfiguration) === null || _a === void 0 ? void 0 : _a.resumeConnection) === ClientConfigSetting.DISABLED ||
20013
+ // signaling state could change to closed due to hardware sleep
20014
+ // those connections cannot be resumed
20015
+ ((_c = (_b = this.primaryPC) === null || _b === void 0 ? void 0 : _b.signalingState) !== null && _c !== void 0 ? _c : 'closed') === 'closed') {
20016
+ this.fullReconnectOnNext = true;
20017
+ }
20144
20018
  try {
20145
- if (this.source === Track.Source.Camera && !this.isUserProvided) {
20146
- livekitLogger.debug('stopping camera track');
20147
- // also stop the track, so that camera indicator is turned off
20148
- this._mediaStreamTrack.stop();
20019
+ this.attemptingReconnect = true;
20020
+ if (this.fullReconnectOnNext) {
20021
+ yield this.restartConnection();
20022
+ } else {
20023
+ yield this.resumeConnection(reason);
20024
+ }
20025
+ this.clearPendingReconnect();
20026
+ this.fullReconnectOnNext = false;
20027
+ } catch (e) {
20028
+ this.reconnectAttempts += 1;
20029
+ let recoverable = true;
20030
+ if (e instanceof UnexpectedConnectionState) {
20031
+ livekitLogger.debug('received unrecoverable error', {
20032
+ error: e
20033
+ });
20034
+ // unrecoverable
20035
+ recoverable = false;
20036
+ } else if (!(e instanceof SignalReconnectError)) {
20037
+ // cannot resume
20038
+ this.fullReconnectOnNext = true;
20039
+ }
20040
+ if (recoverable) {
20041
+ this.handleDisconnect('reconnect', ReconnectReason.RR_UNKNOWN);
20042
+ } else {
20043
+ livekitLogger.info("could not recover connection after ".concat(this.reconnectAttempts, " attempts, ").concat(Date.now() - this.reconnectStart, "ms. giving up"));
20044
+ this.emit(EngineEvent.Disconnected);
20045
+ yield this.close();
20149
20046
  }
20150
- yield _super.mute.call(this);
20151
- return this;
20152
20047
  } finally {
20153
- unlock();
20048
+ this.attemptingReconnect = false;
20154
20049
  }
20155
20050
  });
20156
20051
  }
20157
- unmute() {
20158
- const _super = Object.create(null, {
20159
- unmute: {
20160
- get: () => super.unmute
20052
+ getNextRetryDelay(context) {
20053
+ try {
20054
+ return this.reconnectPolicy.nextRetryDelayInMs(context);
20055
+ } catch (e) {
20056
+ livekitLogger.warn('encountered error in reconnect policy', {
20057
+ error: e
20058
+ });
20059
+ }
20060
+ // error in user code with provided reconnect policy, stop reconnecting
20061
+ return null;
20062
+ }
20063
+ restartConnection(regionUrl) {
20064
+ var _a, _b, _c;
20065
+ return __awaiter(this, void 0, void 0, function* () {
20066
+ try {
20067
+ if (!this.url || !this.token) {
20068
+ // permanent failure, don't attempt reconnection
20069
+ throw new UnexpectedConnectionState('could not reconnect, url or token not saved');
20070
+ }
20071
+ livekitLogger.info("reconnecting, attempt: ".concat(this.reconnectAttempts));
20072
+ this.emit(EngineEvent.Restarting);
20073
+ if (this.client.isConnected) {
20074
+ yield this.client.sendLeave();
20075
+ }
20076
+ yield this.cleanupPeerConnections();
20077
+ yield this.cleanupClient();
20078
+ let joinResponse;
20079
+ try {
20080
+ if (!this.signalOpts) {
20081
+ livekitLogger.warn('attempted connection restart, without signal options present');
20082
+ throw new SignalReconnectError();
20083
+ }
20084
+ // in case a regionUrl is passed, the region URL takes precedence
20085
+ joinResponse = yield this.join(regionUrl !== null && regionUrl !== void 0 ? regionUrl : this.url, this.token, this.signalOpts);
20086
+ } catch (e) {
20087
+ if (e instanceof ConnectionError && e.reason === 0 /* ConnectionErrorReason.NotAllowed */) {
20088
+ throw new UnexpectedConnectionState('could not reconnect, token might be expired');
20089
+ }
20090
+ throw new SignalReconnectError();
20091
+ }
20092
+ if (this.shouldFailNext) {
20093
+ this.shouldFailNext = false;
20094
+ throw new Error('simulated failure');
20095
+ }
20096
+ this.client.setReconnected();
20097
+ this.emit(EngineEvent.SignalRestarted, joinResponse);
20098
+ yield this.waitForPCReconnected();
20099
+ (_a = this.regionUrlProvider) === null || _a === void 0 ? void 0 : _a.resetAttempts();
20100
+ // reconnect success
20101
+ this.emit(EngineEvent.Restarted);
20102
+ } catch (error) {
20103
+ const nextRegionUrl = yield (_b = this.regionUrlProvider) === null || _b === void 0 ? void 0 : _b.getNextBestRegionUrl();
20104
+ if (nextRegionUrl) {
20105
+ yield this.restartConnection(nextRegionUrl);
20106
+ return;
20107
+ } else {
20108
+ // no more regions to try (or we're not on cloud)
20109
+ (_c = this.regionUrlProvider) === null || _c === void 0 ? void 0 : _c.resetAttempts();
20110
+ throw error;
20111
+ }
20161
20112
  }
20162
20113
  });
20114
+ }
20115
+ resumeConnection(reason) {
20116
+ var _a;
20163
20117
  return __awaiter(this, void 0, void 0, function* () {
20164
- const unlock = yield this.muteLock.lock();
20118
+ if (!this.url || !this.token) {
20119
+ // permanent failure, don't attempt reconnection
20120
+ throw new UnexpectedConnectionState('could not reconnect, url or token not saved');
20121
+ }
20122
+ // trigger publisher reconnect
20123
+ if (!this.publisher || !this.subscriber) {
20124
+ throw new UnexpectedConnectionState('publisher and subscriber connections unset');
20125
+ }
20126
+ livekitLogger.info("resuming signal connection, attempt ".concat(this.reconnectAttempts));
20127
+ this.emit(EngineEvent.Resuming);
20165
20128
  try {
20166
- if (this.source === Track.Source.Camera && !this.isUserProvided) {
20167
- livekitLogger.debug('reacquiring camera track');
20168
- yield this.restartTrack();
20129
+ this.setupSignalClientCallbacks();
20130
+ const res = yield this.client.reconnect(this.url, this.token, this.participantSid, reason);
20131
+ if (res) {
20132
+ const rtcConfig = this.makeRTCConfiguration(res);
20133
+ this.publisher.pc.setConfiguration(rtcConfig);
20134
+ this.subscriber.pc.setConfiguration(rtcConfig);
20169
20135
  }
20170
- yield _super.unmute.call(this);
20171
- return this;
20172
- } finally {
20173
- unlock();
20136
+ } catch (e) {
20137
+ let message = '';
20138
+ if (e instanceof Error) {
20139
+ message = e.message;
20140
+ }
20141
+ if (e instanceof ConnectionError && e.reason === 0 /* ConnectionErrorReason.NotAllowed */) {
20142
+ throw new UnexpectedConnectionState('could not reconnect, token might be expired');
20143
+ }
20144
+ throw new SignalReconnectError(message);
20145
+ }
20146
+ this.emit(EngineEvent.SignalResumed);
20147
+ if (this.shouldFailNext) {
20148
+ this.shouldFailNext = false;
20149
+ throw new Error('simulated failure');
20150
+ }
20151
+ this.subscriber.restartingIce = true;
20152
+ // only restart publisher if it's needed
20153
+ if (this.hasPublished) {
20154
+ yield this.publisher.createAndSendOffer({
20155
+ iceRestart: true
20156
+ });
20157
+ }
20158
+ yield this.waitForPCReconnected();
20159
+ this.client.setReconnected();
20160
+ // recreate publish datachannel if it's id is null
20161
+ // (for safari https://bugs.webkit.org/show_bug.cgi?id=184688)
20162
+ if (((_a = this.reliableDC) === null || _a === void 0 ? void 0 : _a.readyState) === 'open' && this.reliableDC.id === null) {
20163
+ this.createDataChannels();
20174
20164
  }
20165
+ // resume success
20166
+ this.emit(EngineEvent.Resumed);
20175
20167
  });
20176
20168
  }
20177
- getSenderStats() {
20178
- var _a;
20169
+ waitForPCInitialConnection(timeout, abortController) {
20179
20170
  return __awaiter(this, void 0, void 0, function* () {
20180
- if (!((_a = this.sender) === null || _a === void 0 ? void 0 : _a.getStats)) {
20181
- return [];
20171
+ if (this.pcState === PCState.Connected) {
20172
+ return;
20182
20173
  }
20183
- const items = [];
20184
- const stats = yield this.sender.getStats();
20185
- stats.forEach(v => {
20186
- var _a;
20187
- if (v.type === 'outbound-rtp') {
20188
- const vs = {
20189
- type: 'video',
20190
- streamId: v.id,
20191
- frameHeight: v.frameHeight,
20192
- frameWidth: v.frameWidth,
20193
- firCount: v.firCount,
20194
- pliCount: v.pliCount,
20195
- nackCount: v.nackCount,
20196
- packetsSent: v.packetsSent,
20197
- bytesSent: v.bytesSent,
20198
- framesSent: v.framesSent,
20199
- timestamp: v.timestamp,
20200
- rid: (_a = v.rid) !== null && _a !== void 0 ? _a : v.id,
20201
- retransmittedPacketsSent: v.retransmittedPacketsSent,
20202
- qualityLimitationReason: v.qualityLimitationReason,
20203
- qualityLimitationResolutionChanges: v.qualityLimitationResolutionChanges
20204
- };
20205
- // locate the appropriate remote-inbound-rtp item
20206
- const r = stats.get(v.remoteId);
20207
- if (r) {
20208
- vs.jitter = r.jitter;
20209
- vs.packetsLost = r.packetsLost;
20210
- vs.roundTripTime = r.roundTripTime;
20211
- }
20212
- items.push(vs);
20174
+ if (this.pcState !== PCState.New) {
20175
+ throw new UnexpectedConnectionState('Expected peer connection to be new on initial connection');
20176
+ }
20177
+ return new Promise((resolve, reject) => {
20178
+ const abortHandler = () => {
20179
+ livekitLogger.warn('closing engine');
20180
+ CriticalTimers.clearTimeout(connectTimeout);
20181
+ reject(new ConnectionError('room connection has been cancelled', 3 /* ConnectionErrorReason.Cancelled */));
20182
+ };
20183
+
20184
+ if (abortController === null || abortController === void 0 ? void 0 : abortController.signal.aborted) {
20185
+ abortHandler();
20213
20186
  }
20187
+ abortController === null || abortController === void 0 ? void 0 : abortController.signal.addEventListener('abort', abortHandler);
20188
+ const onConnected = () => {
20189
+ CriticalTimers.clearTimeout(connectTimeout);
20190
+ abortController === null || abortController === void 0 ? void 0 : abortController.signal.removeEventListener('abort', abortHandler);
20191
+ resolve();
20192
+ };
20193
+ const connectTimeout = CriticalTimers.setTimeout(() => {
20194
+ this.off(EngineEvent.Connected, onConnected);
20195
+ reject(new ConnectionError('could not establish pc connection'));
20196
+ }, timeout !== null && timeout !== void 0 ? timeout : this.peerConnectionTimeout);
20197
+ this.once(EngineEvent.Connected, onConnected);
20214
20198
  });
20215
- return items;
20216
20199
  });
20217
20200
  }
20218
- setPublishingQuality(maxQuality) {
20219
- const qualities = [];
20220
- for (let q = VideoQuality.LOW; q <= VideoQuality.HIGH; q += 1) {
20221
- qualities.push({
20222
- quality: q,
20223
- enabled: q <= maxQuality
20224
- });
20225
- }
20226
- livekitLogger.debug("setting publishing quality. max quality ".concat(maxQuality));
20227
- this.setPublishingLayers(qualities);
20228
- }
20229
- setDeviceId(deviceId) {
20201
+ waitForPCReconnected() {
20202
+ var _a;
20230
20203
  return __awaiter(this, void 0, void 0, function* () {
20231
- if (this._constraints.deviceId === deviceId && this._mediaStreamTrack.getSettings().deviceId === unwrapConstraint(deviceId)) {
20232
- return true;
20233
- }
20234
- this._constraints.deviceId = deviceId;
20235
- // when video is muted, underlying media stream track is stopped and
20236
- // will be restarted later
20237
- if (!this.isMuted) {
20238
- yield this.restartTrack();
20204
+ const startTime = Date.now();
20205
+ let now = startTime;
20206
+ this.pcState = PCState.Reconnecting;
20207
+ livekitLogger.debug('waiting for peer connection to reconnect');
20208
+ while (now - startTime < this.peerConnectionTimeout) {
20209
+ if (this.primaryPC === undefined) {
20210
+ // we can abort early, connection is hosed
20211
+ break;
20212
+ } else if (
20213
+ // on Safari, we don't get a connectionstatechanged event during ICE restart
20214
+ // this means we'd have to check its status manually and update address
20215
+ // manually
20216
+ now - startTime > minReconnectWait && ((_a = this.primaryPC) === null || _a === void 0 ? void 0 : _a.connectionState) === 'connected') {
20217
+ this.pcState = PCState.Connected;
20218
+ }
20219
+ if (this.pcState === PCState.Connected) {
20220
+ return;
20221
+ }
20222
+ yield sleep(100);
20223
+ now = Date.now();
20239
20224
  }
20240
- return this.isMuted || unwrapConstraint(deviceId) === this._mediaStreamTrack.getSettings().deviceId;
20225
+ // have not reconnected, throw
20226
+ throw new ConnectionError('could not establish PC connection');
20241
20227
  });
20242
20228
  }
20243
- restartTrack(options) {
20229
+ /* @internal */
20230
+ sendDataPacket(packet, kind) {
20244
20231
  return __awaiter(this, void 0, void 0, function* () {
20245
- let constraints;
20246
- if (options) {
20247
- const streamConstraints = constraintsForOptions({
20248
- video: options
20249
- });
20250
- if (typeof streamConstraints.video !== 'boolean') {
20251
- constraints = streamConstraints.video;
20252
- }
20232
+ const msg = DataPacket.encode(packet).finish();
20233
+ // make sure we do have a data connection
20234
+ yield this.ensurePublisherConnected(kind);
20235
+ const dc = this.dataChannelForKind(kind);
20236
+ if (dc) {
20237
+ dc.send(msg);
20253
20238
  }
20254
- yield this.restart(constraints);
20239
+ this.updateAndEmitDCBufferStatus(kind);
20255
20240
  });
20256
20241
  }
20257
- addSimulcastTrack(codec, encodings) {
20258
- if (this.simulcastCodecs.has(codec)) {
20259
- throw new Error("".concat(codec, " already added"));
20260
- }
20261
- const simulcastCodecInfo = {
20262
- codec,
20263
- mediaStreamTrack: this.mediaStreamTrack.clone(),
20264
- sender: undefined,
20265
- encodings
20266
- };
20267
- this.simulcastCodecs.set(codec, simulcastCodecInfo);
20268
- return simulcastCodecInfo;
20269
- }
20270
- setSimulcastTrackSender(codec, sender) {
20271
- const simulcastCodecInfo = this.simulcastCodecs.get(codec);
20272
- if (!simulcastCodecInfo) {
20273
- return;
20274
- }
20275
- simulcastCodecInfo.sender = sender;
20276
- // browser will reenable disabled codec/layers after new codec has been published,
20277
- // so refresh subscribedCodecs after publish a new codec
20278
- setTimeout(() => {
20279
- if (this.subscribedCodecs) {
20280
- this.setPublishingCodecs(this.subscribedCodecs);
20281
- }
20282
- }, refreshSubscribedCodecAfterNewCodec);
20283
- }
20284
20242
  /**
20285
20243
  * @internal
20286
- * Sets codecs that should be publishing
20287
20244
  */
20288
- setPublishingCodecs(codecs) {
20289
- var _a, codecs_1, codecs_1_1;
20290
- var _b, e_1, _c, _d;
20245
+ ensureDataTransportConnected(kind) {
20246
+ let subscriber = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.subscriberPrimary;
20247
+ var _a, _b, _c;
20291
20248
  return __awaiter(this, void 0, void 0, function* () {
20292
- livekitLogger.debug('setting publishing codecs', {
20293
- codecs,
20294
- currentCodec: this.codec
20295
- });
20296
- // only enable simulcast codec for preference codec setted
20297
- if (!this.codec && codecs.length > 0) {
20298
- yield this.setPublishingLayers(codecs[0].qualities);
20299
- return [];
20249
+ const transport = subscriber ? this.subscriber : this.publisher;
20250
+ const transportName = subscriber ? 'Subscriber' : 'Publisher';
20251
+ if (!transport) {
20252
+ throw new ConnectionError("".concat(transportName, " connection not set"));
20300
20253
  }
20301
- this.subscribedCodecs = codecs;
20302
- const newCodecs = [];
20303
- try {
20304
- for (_a = true, codecs_1 = __asyncValues(codecs); codecs_1_1 = yield codecs_1.next(), _b = codecs_1_1.done, !_b; _a = true) {
20305
- _d = codecs_1_1.value;
20306
- _a = false;
20307
- const codec = _d;
20308
- if (!this.codec || this.codec === codec.codec) {
20309
- yield this.setPublishingLayers(codec.qualities);
20310
- } else {
20311
- const simulcastCodecInfo = this.simulcastCodecs.get(codec.codec);
20312
- livekitLogger.debug("try setPublishingCodec for ".concat(codec.codec), simulcastCodecInfo);
20313
- if (!simulcastCodecInfo || !simulcastCodecInfo.sender) {
20314
- for (const q of codec.qualities) {
20315
- if (q.enabled) {
20316
- newCodecs.push(codec.codec);
20317
- break;
20318
- }
20319
- }
20320
- } else if (simulcastCodecInfo.encodings) {
20321
- livekitLogger.debug("try setPublishingLayersForSender ".concat(codec.codec));
20322
- yield setPublishingLayersForSender(simulcastCodecInfo.sender, simulcastCodecInfo.encodings, codec.qualities, this.senderLock);
20323
- }
20324
- }
20325
- }
20326
- } catch (e_1_1) {
20327
- e_1 = {
20328
- error: e_1_1
20329
- };
20330
- } finally {
20331
- try {
20332
- if (!_a && !_b && (_c = codecs_1.return)) yield _c.call(codecs_1);
20333
- } finally {
20334
- if (e_1) throw e_1.error;
20335
- }
20254
+ if (!subscriber && !((_a = this.publisher) === null || _a === void 0 ? void 0 : _a.isICEConnected) && ((_b = this.publisher) === null || _b === void 0 ? void 0 : _b.pc.iceConnectionState) !== 'checking') {
20255
+ // start negotiation
20256
+ this.negotiate();
20336
20257
  }
20337
- return newCodecs;
20338
- });
20339
- }
20340
- /**
20341
- * @internal
20342
- * Sets layers that should be publishing
20343
- */
20344
- setPublishingLayers(qualities) {
20345
- return __awaiter(this, void 0, void 0, function* () {
20346
- livekitLogger.debug('setting publishing layers', qualities);
20347
- if (!this.sender || !this.encodings) {
20258
+ const targetChannel = this.dataChannelForKind(kind, subscriber);
20259
+ if ((targetChannel === null || targetChannel === void 0 ? void 0 : targetChannel.readyState) === 'open') {
20348
20260
  return;
20349
20261
  }
20350
- yield setPublishingLayersForSender(this.sender, this.encodings, qualities, this.senderLock);
20351
- });
20352
- }
20353
- handleAppVisibilityChanged() {
20354
- const _super = Object.create(null, {
20355
- handleAppVisibilityChanged: {
20356
- get: () => super.handleAppVisibilityChanged
20357
- }
20358
- });
20359
- return __awaiter(this, void 0, void 0, function* () {
20360
- yield _super.handleAppVisibilityChanged.call(this);
20361
- if (!isMobile()) return;
20362
- if (this.isInBackground && this.source === Track.Source.Camera) {
20363
- this._mediaStreamTrack.enabled = false;
20262
+ // wait until ICE connected
20263
+ const endTime = new Date().getTime() + this.peerConnectionTimeout;
20264
+ while (new Date().getTime() < endTime) {
20265
+ if (transport.isICEConnected && ((_c = this.dataChannelForKind(kind, subscriber)) === null || _c === void 0 ? void 0 : _c.readyState) === 'open') {
20266
+ return;
20267
+ }
20268
+ yield sleep(50);
20364
20269
  }
20270
+ throw new ConnectionError("could not establish ".concat(transportName, " connection, state: ").concat(transport.pc.iceConnectionState));
20365
20271
  });
20366
20272
  }
20367
- }
20368
- function setPublishingLayersForSender(sender, senderEncodings, qualities, senderLock) {
20369
- return __awaiter(this, void 0, void 0, function* () {
20370
- const unlock = yield senderLock.lock();
20371
- livekitLogger.debug('setPublishingLayersForSender', {
20372
- sender,
20373
- qualities,
20374
- senderEncodings
20273
+ ensurePublisherConnected(kind) {
20274
+ return __awaiter(this, void 0, void 0, function* () {
20275
+ yield this.ensureDataTransportConnected(kind, false);
20375
20276
  });
20376
- try {
20377
- const params = sender.getParameters();
20378
- const {
20379
- encodings
20380
- } = params;
20381
- if (!encodings) {
20277
+ }
20278
+ /* @internal */
20279
+ verifyTransport() {
20280
+ // primary connection
20281
+ if (!this.primaryPC) {
20282
+ return false;
20283
+ }
20284
+ if (this.primaryPC.connectionState === 'closed' || this.primaryPC.connectionState === 'failed') {
20285
+ return false;
20286
+ }
20287
+ // also verify publisher connection if it's needed or different
20288
+ if (this.hasPublished && this.subscriberPrimary) {
20289
+ if (!this.publisher) {
20290
+ return false;
20291
+ }
20292
+ if (this.publisher.pc.connectionState === 'closed' || this.publisher.pc.connectionState === 'failed') {
20293
+ return false;
20294
+ }
20295
+ }
20296
+ // ensure signal is connected
20297
+ if (!this.client.ws || this.client.ws.readyState === WebSocket.CLOSED) {
20298
+ return false;
20299
+ }
20300
+ return true;
20301
+ }
20302
+ /** @internal */
20303
+ negotiate() {
20304
+ // observe signal state
20305
+ return new Promise((resolve, reject) => {
20306
+ if (!this.publisher) {
20307
+ reject(new NegotiationError('publisher is not defined'));
20382
20308
  return;
20383
20309
  }
20384
- if (encodings.length !== senderEncodings.length) {
20385
- livekitLogger.warn('cannot set publishing layers, encodings mismatch');
20310
+ this.hasPublished = true;
20311
+ const handleClosed = () => {
20312
+ livekitLogger.warn('engine disconnected while negotiation was ongoing');
20313
+ cleanup();
20314
+ resolve();
20386
20315
  return;
20316
+ };
20317
+ if (this.isClosed) {
20318
+ reject('cannot negotiate on closed engine');
20387
20319
  }
20388
- let hasChanged = false;
20389
- /* disable closable spatial layer as it has video blur / frozen issue with current server / client
20390
- 1. chrome 113: when switching to up layer with scalability Mode change, it will generate a
20391
- low resolution frame and recover very quickly, but noticable
20392
- 2. livekit sfu: additional pli request cause video frozen for a few frames, also noticable */
20393
- const closableSpatial = false;
20394
- /* @ts-ignore */
20395
- if (closableSpatial && encodings[0].scalabilityMode) ; else {
20396
- // simulcast dynacast encodings
20397
- encodings.forEach((encoding, idx) => {
20398
- var _a;
20399
- let rid = (_a = encoding.rid) !== null && _a !== void 0 ? _a : '';
20400
- if (rid === '') {
20401
- rid = 'q';
20402
- }
20403
- const quality = videoQualityForRid(rid);
20404
- const subscribedQuality = qualities.find(q => q.quality === quality);
20405
- if (!subscribedQuality) {
20406
- return;
20407
- }
20408
- if (encoding.active !== subscribedQuality.enabled) {
20409
- hasChanged = true;
20410
- encoding.active = subscribedQuality.enabled;
20411
- livekitLogger.debug("setting layer ".concat(subscribedQuality.quality, " to ").concat(encoding.active ? 'enabled' : 'disabled'));
20412
- // FireFox does not support setting encoding.active to false, so we
20413
- // have a workaround of lowering its bitrate and resolution to the min.
20414
- if (isFireFox()) {
20415
- if (subscribedQuality.enabled) {
20416
- encoding.scaleResolutionDownBy = senderEncodings[idx].scaleResolutionDownBy;
20417
- encoding.maxBitrate = senderEncodings[idx].maxBitrate;
20418
- /* @ts-ignore */
20419
- encoding.maxFrameRate = senderEncodings[idx].maxFrameRate;
20420
- } else {
20421
- encoding.scaleResolutionDownBy = 4;
20422
- encoding.maxBitrate = 10;
20423
- /* @ts-ignore */
20424
- encoding.maxFrameRate = 2;
20425
- }
20426
- }
20320
+ this.on(EngineEvent.Closing, handleClosed);
20321
+ const negotiationTimeout = setTimeout(() => {
20322
+ reject('negotiation timed out');
20323
+ this.handleDisconnect('negotiation', ReconnectReason.RR_SIGNAL_DISCONNECTED);
20324
+ }, this.peerConnectionTimeout);
20325
+ const cleanup = () => {
20326
+ clearTimeout(negotiationTimeout);
20327
+ this.off(EngineEvent.Closing, handleClosed);
20328
+ };
20329
+ this.publisher.once(PCEvents.NegotiationStarted, () => {
20330
+ var _a;
20331
+ (_a = this.publisher) === null || _a === void 0 ? void 0 : _a.once(PCEvents.NegotiationComplete, () => {
20332
+ cleanup();
20333
+ resolve();
20334
+ });
20335
+ });
20336
+ this.publisher.once(PCEvents.RTPVideoPayloadTypes, rtpTypes => {
20337
+ const rtpMap = new Map();
20338
+ rtpTypes.forEach(rtp => {
20339
+ const codec = rtp.codec.toLowerCase();
20340
+ if (isVideoCodec(codec)) {
20341
+ rtpMap.set(rtp.payload, codec);
20427
20342
  }
20428
20343
  });
20344
+ this.emit(EngineEvent.RTPVideoMapUpdate, rtpMap);
20345
+ });
20346
+ this.publisher.negotiate(e => {
20347
+ cleanup();
20348
+ reject(e);
20349
+ if (e instanceof NegotiationError) {
20350
+ this.fullReconnectOnNext = true;
20351
+ }
20352
+ this.handleDisconnect('negotiation', ReconnectReason.RR_UNKNOWN);
20353
+ });
20354
+ });
20355
+ }
20356
+ dataChannelForKind(kind, sub) {
20357
+ if (!sub) {
20358
+ if (kind === DataPacket_Kind.LOSSY) {
20359
+ return this.lossyDC;
20429
20360
  }
20430
- if (hasChanged) {
20431
- params.encodings = encodings;
20432
- livekitLogger.debug("setting encodings", params.encodings);
20433
- yield sender.setParameters(params);
20361
+ if (kind === DataPacket_Kind.RELIABLE) {
20362
+ return this.reliableDC;
20363
+ }
20364
+ } else {
20365
+ if (kind === DataPacket_Kind.LOSSY) {
20366
+ return this.lossyDCSub;
20367
+ }
20368
+ if (kind === DataPacket_Kind.RELIABLE) {
20369
+ return this.reliableDCSub;
20434
20370
  }
20435
- } finally {
20436
- unlock();
20437
20371
  }
20438
- });
20439
- }
20440
- function videoQualityForRid(rid) {
20441
- switch (rid) {
20442
- case 'f':
20443
- return VideoQuality.HIGH;
20444
- case 'h':
20445
- return VideoQuality.MEDIUM;
20446
- case 'q':
20447
- return VideoQuality.LOW;
20448
- default:
20449
- return VideoQuality.UNRECOGNIZED;
20450
20372
  }
20451
- }
20452
- function videoLayersFromEncodings(width, height, encodings, svc) {
20453
- // default to a single layer, HQ
20454
- if (!encodings) {
20455
- return [{
20456
- quality: VideoQuality.HIGH,
20457
- width,
20458
- height,
20459
- bitrate: 0,
20460
- ssrc: 0
20461
- }];
20373
+ /* @internal */
20374
+ failNext() {
20375
+ // debugging method to fail the next reconnect/resume attempt
20376
+ this.shouldFailNext = true;
20462
20377
  }
20463
- if (svc) {
20464
- // svc layers
20465
- /* @ts-ignore */
20466
- const sm = new ScalabilityMode(encodings[0].scalabilityMode);
20467
- const layers = [];
20468
- for (let i = 0; i < sm.spatial; i += 1) {
20469
- layers.push({
20470
- quality: VideoQuality.HIGH - i,
20471
- width: width / Math.pow(2, i),
20472
- height: height / Math.pow(2, i),
20473
- bitrate: encodings[0].maxBitrate ? encodings[0].maxBitrate / Math.pow(3, i) : 0,
20474
- ssrc: 0
20475
- });
20378
+ clearReconnectTimeout() {
20379
+ if (this.reconnectTimeout) {
20380
+ CriticalTimers.clearTimeout(this.reconnectTimeout);
20476
20381
  }
20477
- return layers;
20478
20382
  }
20479
- return encodings.map(encoding => {
20480
- var _a, _b, _c;
20481
- const scale = (_a = encoding.scaleResolutionDownBy) !== null && _a !== void 0 ? _a : 1;
20482
- let quality = videoQualityForRid((_b = encoding.rid) !== null && _b !== void 0 ? _b : '');
20483
- if (quality === VideoQuality.UNRECOGNIZED && encodings.length === 1) {
20484
- quality = VideoQuality.HIGH;
20383
+ clearPendingReconnect() {
20384
+ this.clearReconnectTimeout();
20385
+ this.reconnectAttempts = 0;
20386
+ }
20387
+ registerOnLineListener() {
20388
+ if (isWeb()) {
20389
+ window.addEventListener('online', this.handleBrowserOnLine);
20485
20390
  }
20486
- return {
20487
- quality,
20488
- width: width / scale,
20489
- height: height / scale,
20490
- bitrate: (_c = encoding.maxBitrate) !== null && _c !== void 0 ? _c : 0,
20491
- ssrc: 0
20492
- };
20391
+ }
20392
+ deregisterOnLineListener() {
20393
+ if (isWeb()) {
20394
+ window.removeEventListener('online', this.handleBrowserOnLine);
20395
+ }
20396
+ }
20397
+ }
20398
+ function getConnectedAddress(pc) {
20399
+ var _a;
20400
+ return __awaiter(this, void 0, void 0, function* () {
20401
+ let selectedCandidatePairId = '';
20402
+ const candidatePairs = new Map();
20403
+ // id -> candidate ip
20404
+ const candidates = new Map();
20405
+ const stats = yield pc.getStats();
20406
+ stats.forEach(v => {
20407
+ switch (v.type) {
20408
+ case 'transport':
20409
+ selectedCandidatePairId = v.selectedCandidatePairId;
20410
+ break;
20411
+ case 'candidate-pair':
20412
+ if (selectedCandidatePairId === '' && v.selected) {
20413
+ selectedCandidatePairId = v.id;
20414
+ }
20415
+ candidatePairs.set(v.id, v);
20416
+ break;
20417
+ case 'remote-candidate':
20418
+ candidates.set(v.id, "".concat(v.address, ":").concat(v.port));
20419
+ break;
20420
+ }
20421
+ });
20422
+ if (selectedCandidatePairId === '') {
20423
+ return undefined;
20424
+ }
20425
+ const selectedID = (_a = candidatePairs.get(selectedCandidatePairId)) === null || _a === void 0 ? void 0 : _a.remoteCandidateId;
20426
+ if (selectedID === undefined) {
20427
+ return undefined;
20428
+ }
20429
+ return candidates.get(selectedID);
20493
20430
  });
20494
20431
  }
20432
+ class SignalReconnectError extends Error {}
20495
20433
 
20496
20434
  class RemoteTrack extends Track {
20497
20435
  constructor(mediaTrack, sid, kind, receiver) {
@@ -21428,7 +21366,7 @@ class RemoteTrackPublication extends TrackPublication {
21428
21366
  /** @internal */
21429
21367
  this.allowed = true;
21430
21368
  this.disabled = false;
21431
- this.currentVideoQuality = VideoQuality.LOW;
21369
+ this.currentVideoQuality = VideoQuality.HIGH;
21432
21370
  this.handleEnded = track => {
21433
21371
  this.setTrack(undefined);
21434
21372
  this.emit(TrackEvent.Ended, track);
@@ -21658,8 +21596,8 @@ class RemoteTrackPublication extends TrackPublication {
21658
21596
  } else if (this.currentVideoQuality !== undefined) {
21659
21597
  settings.quality = this.currentVideoQuality;
21660
21598
  } else {
21661
- // defaults to low quality
21662
- settings.quality = VideoQuality.LOW;
21599
+ // defaults to high quality
21600
+ settings.quality = VideoQuality.HIGH;
21663
21601
  }
21664
21602
  this.emit(TrackEvent.UpdateSettings, settings);
21665
21603
  }