@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.
- package/dist/livekit-client.esm.mjs +1763 -1825
- package/dist/livekit-client.esm.mjs.map +1 -1
- package/dist/livekit-client.umd.js +1 -1
- package/dist/livekit-client.umd.js.map +1 -1
- package/dist/src/room/RTCEngine.d.ts +2 -3
- package/dist/src/room/RTCEngine.d.ts.map +1 -1
- package/dist/src/room/track/RemoteTrackPublication.d.ts.map +1 -1
- package/dist/src/room/utils.d.ts +0 -1
- package/dist/src/room/utils.d.ts.map +1 -1
- package/dist/ts4.2/src/room/RTCEngine.d.ts +2 -3
- package/dist/ts4.2/src/room/utils.d.ts +0 -1
- package/package.json +1 -1
- package/src/room/RTCEngine.ts +7 -52
- package/src/room/utils.ts +0 -26
|
@@ -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.
|
|
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
|
|
18463
|
-
|
|
18464
|
-
|
|
18465
|
-
|
|
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
|
-
|
|
18480
|
-
|
|
18481
|
-
|
|
18482
|
-
|
|
18483
|
-
|
|
18484
|
-
|
|
18485
|
-
|
|
18486
|
-
|
|
18487
|
-
|
|
18488
|
-
|
|
18489
|
-
|
|
18490
|
-
|
|
18491
|
-
|
|
18492
|
-
|
|
18493
|
-
|
|
18494
|
-
|
|
18495
|
-
|
|
18496
|
-
|
|
18497
|
-
|
|
18498
|
-
|
|
18499
|
-
|
|
18500
|
-
|
|
18501
|
-
|
|
18502
|
-
|
|
18503
|
-
|
|
18504
|
-
|
|
18505
|
-
|
|
18506
|
-
|
|
18507
|
-
|
|
18508
|
-
|
|
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
|
-
|
|
18570
|
-
|
|
18571
|
-
|
|
18572
|
-
|
|
18573
|
-
|
|
18574
|
-
|
|
18575
|
-
|
|
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 (
|
|
18589
|
-
|
|
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
|
-
|
|
18624
|
-
|
|
18625
|
-
|
|
18626
|
-
|
|
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
|
-
|
|
18630
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
18654
|
-
this.token = token;
|
|
18655
|
-
this.signalOpts = opts;
|
|
18511
|
+
const unlock = yield this.muteLock.lock();
|
|
18656
18512
|
try {
|
|
18657
|
-
|
|
18658
|
-
|
|
18659
|
-
|
|
18660
|
-
|
|
18661
|
-
|
|
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
|
-
|
|
18519
|
+
yield _super.mute.call(this);
|
|
18520
|
+
return this;
|
|
18521
|
+
} finally {
|
|
18522
|
+
unlock();
|
|
18682
18523
|
}
|
|
18683
18524
|
});
|
|
18684
18525
|
}
|
|
18685
|
-
|
|
18686
|
-
|
|
18687
|
-
|
|
18688
|
-
|
|
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.
|
|
18694
|
-
this.
|
|
18695
|
-
|
|
18696
|
-
|
|
18697
|
-
|
|
18698
|
-
yield
|
|
18699
|
-
|
|
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
|
-
|
|
18547
|
+
restartTrack(options) {
|
|
18706
18548
|
return __awaiter(this, void 0, void 0, function* () {
|
|
18707
|
-
|
|
18708
|
-
|
|
18709
|
-
|
|
18710
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
18754
|
-
this.
|
|
18568
|
+
const track = yield _super.restart.call(this, constraints);
|
|
18569
|
+
this.checkForSilence();
|
|
18570
|
+
return track;
|
|
18755
18571
|
});
|
|
18756
18572
|
}
|
|
18757
|
-
|
|
18758
|
-
|
|
18759
|
-
|
|
18573
|
+
/* @internal */
|
|
18574
|
+
startMonitor() {
|
|
18575
|
+
if (!isWeb()) {
|
|
18576
|
+
return;
|
|
18760
18577
|
}
|
|
18761
|
-
|
|
18762
|
-
|
|
18763
|
-
|
|
18764
|
-
|
|
18765
|
-
|
|
18766
|
-
|
|
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
|
-
|
|
18788
|
-
|
|
18789
|
-
|
|
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
|
-
|
|
18795
|
-
|
|
18796
|
-
|
|
18797
|
-
|
|
18798
|
-
|
|
18799
|
-
|
|
18800
|
-
|
|
18801
|
-
|
|
18802
|
-
|
|
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
|
-
|
|
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
|
-
|
|
18610
|
+
checkForSilence() {
|
|
18815
18611
|
return __awaiter(this, void 0, void 0, function* () {
|
|
18816
|
-
|
|
18817
|
-
|
|
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
|
|
18619
|
+
return trackIsSilent;
|
|
18820
18620
|
});
|
|
18821
18621
|
}
|
|
18822
|
-
|
|
18823
|
-
|
|
18824
|
-
|
|
18825
|
-
|
|
18826
|
-
|
|
18827
|
-
|
|
18828
|
-
|
|
18829
|
-
|
|
18830
|
-
|
|
18831
|
-
|
|
18832
|
-
|
|
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
|
-
|
|
18895
|
-
|
|
18896
|
-
|
|
18897
|
-
|
|
18898
|
-
|
|
18899
|
-
|
|
18900
|
-
|
|
18901
|
-
|
|
18902
|
-
|
|
18903
|
-
|
|
18904
|
-
|
|
18905
|
-
|
|
18906
|
-
|
|
18907
|
-
|
|
18908
|
-
|
|
18909
|
-
|
|
18910
|
-
|
|
18911
|
-
|
|
18912
|
-
|
|
18913
|
-
|
|
18914
|
-
|
|
18915
|
-
|
|
18916
|
-
|
|
18917
|
-
|
|
18918
|
-
|
|
18919
|
-
|
|
18920
|
-
|
|
18921
|
-
|
|
18922
|
-
|
|
18923
|
-
|
|
18924
|
-
|
|
18925
|
-
|
|
18926
|
-
|
|
18927
|
-
|
|
18928
|
-
|
|
18929
|
-
|
|
18930
|
-
|
|
18931
|
-
|
|
18932
|
-
|
|
18933
|
-
|
|
18934
|
-
|
|
18935
|
-
|
|
18936
|
-
|
|
18937
|
-
|
|
18938
|
-
|
|
18939
|
-
|
|
18940
|
-
|
|
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
|
-
|
|
18969
|
-
|
|
18970
|
-
|
|
18971
|
-
|
|
18972
|
-
|
|
18973
|
-
|
|
18974
|
-
|
|
18975
|
-
|
|
18976
|
-
|
|
18977
|
-
|
|
18978
|
-
|
|
18979
|
-
|
|
18980
|
-
|
|
18981
|
-
|
|
18982
|
-
|
|
18983
|
-
|
|
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
|
-
|
|
18986
|
-
|
|
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
|
-
|
|
18989
|
-
|
|
18990
|
-
|
|
18991
|
-
|
|
18992
|
-
return rtcConfig;
|
|
18711
|
+
/* @ts-ignore */
|
|
18712
|
+
encodings[0].scalabilityMode = scalabilityMode;
|
|
18713
|
+
livekitLogger.debug('encodings', encodings);
|
|
18714
|
+
return encodings;
|
|
18993
18715
|
}
|
|
18994
|
-
|
|
18995
|
-
|
|
18996
|
-
|
|
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
|
-
//
|
|
18999
|
-
|
|
19000
|
-
|
|
19001
|
-
|
|
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 (
|
|
19004
|
-
|
|
19005
|
-
this.reliableDC.onerror = null;
|
|
18744
|
+
if (size >= 480) {
|
|
18745
|
+
return encodingsFromPresets(width, height, [lowPreset, original]);
|
|
19006
18746
|
}
|
|
19007
|
-
|
|
19008
|
-
|
|
19009
|
-
|
|
19010
|
-
|
|
19011
|
-
|
|
19012
|
-
|
|
19013
|
-
|
|
19014
|
-
|
|
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
|
-
|
|
19030
|
-
|
|
19031
|
-
|
|
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
|
-
|
|
19034
|
-
|
|
19035
|
-
|
|
19036
|
-
|
|
19037
|
-
|
|
19038
|
-
|
|
19039
|
-
|
|
19040
|
-
|
|
19041
|
-
|
|
19042
|
-
|
|
19043
|
-
|
|
19044
|
-
|
|
19045
|
-
|
|
19046
|
-
|
|
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
|
-
|
|
19067
|
-
|
|
19068
|
-
|
|
19069
|
-
|
|
19070
|
-
|
|
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
|
-
|
|
19081
|
-
|
|
19082
|
-
|
|
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
|
-
|
|
19094
|
-
|
|
19095
|
-
|
|
19096
|
-
|
|
19097
|
-
|
|
19098
|
-
|
|
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
|
-
|
|
19119
|
-
|
|
19120
|
-
|
|
19121
|
-
|
|
19122
|
-
|
|
19123
|
-
|
|
19124
|
-
|
|
19125
|
-
|
|
19126
|
-
|
|
19127
|
-
|
|
19128
|
-
|
|
19129
|
-
|
|
19130
|
-
|
|
19131
|
-
|
|
19132
|
-
|
|
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
|
-
|
|
19140
|
-
|
|
19141
|
-
if (
|
|
19142
|
-
|
|
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
|
-
|
|
19148
|
-
|
|
19149
|
-
|
|
19150
|
-
|
|
19151
|
-
|
|
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
|
-
|
|
19154
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
19174
|
-
|
|
19175
|
-
|
|
19176
|
-
|
|
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
|
-
|
|
19198
|
-
|
|
19199
|
-
return
|
|
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
|
-
|
|
19206
|
-
return null;
|
|
18967
|
+
return false;
|
|
19207
18968
|
}
|
|
19208
|
-
|
|
19209
|
-
|
|
19210
|
-
|
|
19211
|
-
|
|
19212
|
-
|
|
19213
|
-
|
|
19214
|
-
|
|
19215
|
-
|
|
19216
|
-
|
|
19217
|
-
|
|
19218
|
-
|
|
19219
|
-
|
|
19220
|
-
|
|
19221
|
-
|
|
19222
|
-
|
|
19223
|
-
|
|
19224
|
-
|
|
19225
|
-
|
|
19226
|
-
|
|
19227
|
-
|
|
19228
|
-
|
|
19229
|
-
|
|
19230
|
-
|
|
19231
|
-
|
|
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
|
-
|
|
19261
|
-
|
|
19262
|
-
|
|
19263
|
-
|
|
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
|
-
|
|
19272
|
-
|
|
19001
|
+
});
|
|
19002
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
19003
|
+
const unlock = yield this.muteLock.lock();
|
|
19273
19004
|
try {
|
|
19274
|
-
this.
|
|
19275
|
-
|
|
19276
|
-
|
|
19277
|
-
|
|
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
|
-
|
|
19290
|
-
|
|
19291
|
-
|
|
19292
|
-
|
|
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
|
-
|
|
19297
|
-
|
|
19298
|
-
|
|
19299
|
-
|
|
19300
|
-
|
|
19301
|
-
|
|
19015
|
+
});
|
|
19016
|
+
}
|
|
19017
|
+
unmute() {
|
|
19018
|
+
const _super = Object.create(null, {
|
|
19019
|
+
unmute: {
|
|
19020
|
+
get: () => super.unmute
|
|
19302
19021
|
}
|
|
19303
|
-
|
|
19304
|
-
|
|
19305
|
-
|
|
19306
|
-
|
|
19307
|
-
|
|
19308
|
-
|
|
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
|
-
|
|
19037
|
+
getSenderStats() {
|
|
19038
|
+
var _a;
|
|
19315
19039
|
return __awaiter(this, void 0, void 0, function* () {
|
|
19316
|
-
if (this.
|
|
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
|
-
|
|
19323
|
-
|
|
19324
|
-
|
|
19325
|
-
|
|
19326
|
-
|
|
19327
|
-
|
|
19328
|
-
|
|
19329
|
-
|
|
19330
|
-
|
|
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
|
-
|
|
19347
|
-
|
|
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
|
-
|
|
19350
|
-
|
|
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
|
-
|
|
19371
|
-
|
|
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
|
-
|
|
19375
|
-
sendDataPacket(packet, kind) {
|
|
19103
|
+
restartTrack(options) {
|
|
19376
19104
|
return __awaiter(this, void 0, void 0, function* () {
|
|
19377
|
-
|
|
19378
|
-
|
|
19379
|
-
|
|
19380
|
-
|
|
19381
|
-
|
|
19382
|
-
|
|
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.
|
|
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
|
-
|
|
19391
|
-
|
|
19392
|
-
var
|
|
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
|
-
|
|
19395
|
-
|
|
19396
|
-
|
|
19397
|
-
|
|
19398
|
-
|
|
19399
|
-
if (!
|
|
19400
|
-
|
|
19401
|
-
|
|
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
|
-
|
|
19408
|
-
const
|
|
19409
|
-
|
|
19410
|
-
|
|
19411
|
-
|
|
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
|
-
|
|
19197
|
+
return newCodecs;
|
|
19416
19198
|
});
|
|
19417
19199
|
}
|
|
19418
|
-
|
|
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
|
-
|
|
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
|
-
|
|
19424
|
-
|
|
19425
|
-
|
|
19426
|
-
|
|
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
|
-
|
|
19438
|
-
|
|
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
|
-
|
|
19448
|
-
|
|
19449
|
-
|
|
19450
|
-
|
|
19451
|
-
|
|
19452
|
-
|
|
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
|
-
|
|
19456
|
-
|
|
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
|
-
|
|
19466
|
-
|
|
19467
|
-
|
|
19468
|
-
|
|
19469
|
-
|
|
19470
|
-
const
|
|
19471
|
-
|
|
19472
|
-
|
|
19473
|
-
|
|
19474
|
-
|
|
19475
|
-
|
|
19476
|
-
|
|
19477
|
-
|
|
19478
|
-
|
|
19479
|
-
|
|
19480
|
-
|
|
19481
|
-
|
|
19482
|
-
|
|
19483
|
-
|
|
19484
|
-
|
|
19485
|
-
if (
|
|
19486
|
-
|
|
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 (
|
|
19507
|
-
|
|
19508
|
-
|
|
19509
|
-
|
|
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
|
-
|
|
19519
|
-
|
|
19520
|
-
|
|
19521
|
-
|
|
19522
|
-
|
|
19523
|
-
|
|
19524
|
-
|
|
19525
|
-
|
|
19526
|
-
|
|
19527
|
-
|
|
19528
|
-
|
|
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
|
|
19544
|
-
|
|
19545
|
-
|
|
19546
|
-
|
|
19547
|
-
|
|
19548
|
-
|
|
19549
|
-
|
|
19550
|
-
|
|
19551
|
-
|
|
19552
|
-
|
|
19553
|
-
|
|
19554
|
-
|
|
19555
|
-
|
|
19556
|
-
|
|
19557
|
-
|
|
19558
|
-
|
|
19559
|
-
|
|
19560
|
-
|
|
19561
|
-
|
|
19562
|
-
|
|
19563
|
-
|
|
19564
|
-
|
|
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
|
|
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
|
|
19580
|
-
|
|
19581
|
-
|
|
19582
|
-
|
|
19583
|
-
|
|
19584
|
-
|
|
19585
|
-
|
|
19586
|
-
|
|
19587
|
-
|
|
19588
|
-
|
|
19589
|
-
|
|
19590
|
-
|
|
19591
|
-
|
|
19592
|
-
|
|
19593
|
-
|
|
19594
|
-
return
|
|
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
|
-
|
|
19597
|
-
|
|
19598
|
-
|
|
19599
|
-
|
|
19600
|
-
|
|
19601
|
-
|
|
19602
|
-
|
|
19603
|
-
|
|
19604
|
-
|
|
19605
|
-
|
|
19606
|
-
|
|
19607
|
-
|
|
19608
|
-
|
|
19609
|
-
|
|
19610
|
-
this.
|
|
19611
|
-
this.
|
|
19612
|
-
|
|
19613
|
-
|
|
19614
|
-
|
|
19615
|
-
|
|
19616
|
-
|
|
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
|
-
|
|
19619
|
-
|
|
19620
|
-
|
|
19621
|
-
|
|
19622
|
-
})
|
|
19623
|
-
|
|
19624
|
-
|
|
19625
|
-
|
|
19626
|
-
|
|
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.
|
|
19631
|
-
|
|
19632
|
-
|
|
19633
|
-
|
|
19634
|
-
|
|
19635
|
-
|
|
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
|
-
|
|
19638
|
-
|
|
19639
|
-
|
|
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
|
-
|
|
19642
|
-
|
|
19643
|
-
|
|
19644
|
-
|
|
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
|
-
|
|
19651
|
-
|
|
19652
|
-
|
|
19653
|
-
|
|
19654
|
-
|
|
19655
|
-
|
|
19656
|
-
|
|
19657
|
-
|
|
19658
|
-
|
|
19659
|
-
|
|
19660
|
-
|
|
19661
|
-
|
|
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
|
-
|
|
19673
|
-
|
|
19674
|
-
|
|
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
|
-
|
|
19688
|
-
|
|
19689
|
-
|
|
19690
|
-
|
|
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
|
-
|
|
19699
|
-
|
|
19700
|
-
|
|
19701
|
-
|
|
19702
|
-
|
|
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
|
-
|
|
19708
|
-
|
|
19709
|
-
this.
|
|
19710
|
-
|
|
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
|
-
|
|
19732
|
-
|
|
19733
|
-
|
|
19734
|
-
|
|
19735
|
-
|
|
19736
|
-
|
|
19737
|
-
|
|
19738
|
-
|
|
19739
|
-
|
|
19740
|
-
|
|
19741
|
-
|
|
19742
|
-
|
|
19743
|
-
|
|
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
|
-
|
|
19545
|
+
join(url, token, opts, abortSignal) {
|
|
19751
19546
|
return __awaiter(this, void 0, void 0, function* () {
|
|
19752
|
-
|
|
19753
|
-
|
|
19754
|
-
|
|
19755
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
19889
|
-
|
|
19890
|
-
|
|
19891
|
-
|
|
19892
|
-
|
|
19893
|
-
|
|
19894
|
-
|
|
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
|
-
|
|
19897
|
-
|
|
19898
|
-
|
|
19899
|
-
|
|
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
|
-
|
|
19903
|
-
|
|
19904
|
-
|
|
19905
|
-
|
|
19906
|
-
|
|
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
|
-
|
|
19927
|
-
|
|
19928
|
-
|
|
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
|
-
|
|
19942
|
-
|
|
19943
|
-
|
|
19944
|
-
|
|
19945
|
-
|
|
19946
|
-
|
|
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
|
-
|
|
19949
|
-
|
|
19950
|
-
return presets169;
|
|
19701
|
+
updateMuteStatus(trackSid, muted) {
|
|
19702
|
+
this.client.sendMuteTrack(trackSid, muted);
|
|
19951
19703
|
}
|
|
19952
|
-
|
|
19953
|
-
|
|
19954
|
-
|
|
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
|
-
|
|
19960
|
-
|
|
19961
|
-
|
|
19962
|
-
|
|
19963
|
-
|
|
19964
|
-
|
|
19965
|
-
|
|
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
|
-
|
|
19968
|
-
|
|
19969
|
-
//
|
|
19970
|
-
|
|
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
|
-
|
|
19977
|
-
const
|
|
19978
|
-
|
|
19979
|
-
|
|
19980
|
-
|
|
19981
|
-
|
|
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
|
|
19987
|
-
|
|
19988
|
-
|
|
19989
|
-
|
|
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
|
-
|
|
19992
|
-
|
|
19993
|
-
|
|
19994
|
-
|
|
19995
|
-
|
|
19996
|
-
|
|
19997
|
-
|
|
19998
|
-
|
|
19999
|
-
|
|
20000
|
-
|
|
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
|
-
|
|
20004
|
-
|
|
20005
|
-
|
|
20006
|
-
if (
|
|
20007
|
-
|
|
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
|
-
|
|
20017
|
-
|
|
20018
|
-
|
|
20019
|
-
|
|
20020
|
-
|
|
20021
|
-
|
|
20022
|
-
|
|
20023
|
-
|
|
20024
|
-
|
|
20025
|
-
|
|
20026
|
-
|
|
20027
|
-
}
|
|
20028
|
-
|
|
20029
|
-
|
|
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 (
|
|
20032
|
-
|
|
20033
|
-
return aEnc.maxFramerate > bEnc.maxFramerate ? 1 : -1;
|
|
19879
|
+
if (serverResponse.clientConfiguration && serverResponse.clientConfiguration.forceRelay === ClientConfigSetting.ENABLED) {
|
|
19880
|
+
rtcConfig.iceTransportPolicy = 'relay';
|
|
20034
19881
|
}
|
|
20035
|
-
|
|
20036
|
-
|
|
20037
|
-
|
|
20038
|
-
|
|
20039
|
-
|
|
20040
|
-
|
|
20041
|
-
|
|
20042
|
-
if (!
|
|
20043
|
-
|
|
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
|
-
|
|
20046
|
-
this.
|
|
20047
|
-
|
|
20048
|
-
|
|
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
|
-
|
|
20057
|
-
|
|
20058
|
-
|
|
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
|
-
|
|
20090
|
-
|
|
20091
|
-
|
|
20092
|
-
|
|
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
|
|
19934
|
+
throw new UnexpectedConnectionState('Required webRTC APIs not supported on this device');
|
|
20100
19935
|
});
|
|
20101
|
-
this.senderLock = new Mutex();
|
|
20102
19936
|
}
|
|
20103
|
-
|
|
20104
|
-
|
|
20105
|
-
|
|
20106
|
-
|
|
20107
|
-
|
|
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
|
-
|
|
20110
|
-
|
|
20111
|
-
|
|
20112
|
-
|
|
20113
|
-
|
|
20114
|
-
|
|
20115
|
-
|
|
20116
|
-
|
|
20117
|
-
|
|
20118
|
-
|
|
20119
|
-
|
|
20120
|
-
|
|
20121
|
-
|
|
20122
|
-
|
|
20123
|
-
|
|
20124
|
-
|
|
20125
|
-
|
|
20126
|
-
|
|
20127
|
-
|
|
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
|
-
|
|
20130
|
-
this
|
|
20131
|
-
|
|
20132
|
-
|
|
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
|
-
|
|
20137
|
-
|
|
20138
|
-
|
|
20139
|
-
|
|
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
|
-
|
|
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
|
-
|
|
20146
|
-
|
|
20147
|
-
|
|
20148
|
-
|
|
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
|
-
|
|
20048
|
+
this.attemptingReconnect = false;
|
|
20154
20049
|
}
|
|
20155
20050
|
});
|
|
20156
20051
|
}
|
|
20157
|
-
|
|
20158
|
-
|
|
20159
|
-
|
|
20160
|
-
|
|
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
|
-
|
|
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
|
-
|
|
20167
|
-
|
|
20168
|
-
|
|
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
|
-
|
|
20171
|
-
|
|
20172
|
-
|
|
20173
|
-
|
|
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
|
-
|
|
20178
|
-
var _a;
|
|
20169
|
+
waitForPCInitialConnection(timeout, abortController) {
|
|
20179
20170
|
return __awaiter(this, void 0, void 0, function* () {
|
|
20180
|
-
if (
|
|
20181
|
-
return
|
|
20171
|
+
if (this.pcState === PCState.Connected) {
|
|
20172
|
+
return;
|
|
20182
20173
|
}
|
|
20183
|
-
|
|
20184
|
-
|
|
20185
|
-
|
|
20186
|
-
|
|
20187
|
-
|
|
20188
|
-
|
|
20189
|
-
|
|
20190
|
-
|
|
20191
|
-
|
|
20192
|
-
|
|
20193
|
-
|
|
20194
|
-
|
|
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
|
-
|
|
20219
|
-
|
|
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
|
-
|
|
20232
|
-
|
|
20233
|
-
|
|
20234
|
-
|
|
20235
|
-
|
|
20236
|
-
|
|
20237
|
-
|
|
20238
|
-
|
|
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
|
-
|
|
20225
|
+
// have not reconnected, throw
|
|
20226
|
+
throw new ConnectionError('could not establish PC connection');
|
|
20241
20227
|
});
|
|
20242
20228
|
}
|
|
20243
|
-
|
|
20229
|
+
/* @internal */
|
|
20230
|
+
sendDataPacket(packet, kind) {
|
|
20244
20231
|
return __awaiter(this, void 0, void 0, function* () {
|
|
20245
|
-
|
|
20246
|
-
|
|
20247
|
-
|
|
20248
|
-
|
|
20249
|
-
|
|
20250
|
-
|
|
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
|
-
|
|
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
|
-
|
|
20289
|
-
|
|
20290
|
-
var
|
|
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
|
-
|
|
20293
|
-
|
|
20294
|
-
|
|
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.
|
|
20302
|
-
|
|
20303
|
-
|
|
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
|
-
|
|
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
|
-
|
|
20351
|
-
|
|
20352
|
-
|
|
20353
|
-
|
|
20354
|
-
|
|
20355
|
-
|
|
20356
|
-
|
|
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
|
-
|
|
20369
|
-
|
|
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
|
-
|
|
20377
|
-
|
|
20378
|
-
|
|
20379
|
-
|
|
20380
|
-
|
|
20381
|
-
|
|
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
|
-
|
|
20385
|
-
|
|
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
|
-
|
|
20389
|
-
|
|
20390
|
-
|
|
20391
|
-
|
|
20392
|
-
|
|
20393
|
-
const
|
|
20394
|
-
|
|
20395
|
-
|
|
20396
|
-
|
|
20397
|
-
|
|
20398
|
-
|
|
20399
|
-
|
|
20400
|
-
|
|
20401
|
-
|
|
20402
|
-
|
|
20403
|
-
|
|
20404
|
-
|
|
20405
|
-
|
|
20406
|
-
|
|
20407
|
-
|
|
20408
|
-
if (
|
|
20409
|
-
|
|
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 (
|
|
20431
|
-
|
|
20432
|
-
|
|
20433
|
-
|
|
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
|
-
|
|
20453
|
-
|
|
20454
|
-
|
|
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
|
-
|
|
20464
|
-
|
|
20465
|
-
|
|
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
|
-
|
|
20480
|
-
|
|
20481
|
-
|
|
20482
|
-
|
|
20483
|
-
|
|
20484
|
-
|
|
20383
|
+
clearPendingReconnect() {
|
|
20384
|
+
this.clearReconnectTimeout();
|
|
20385
|
+
this.reconnectAttempts = 0;
|
|
20386
|
+
}
|
|
20387
|
+
registerOnLineListener() {
|
|
20388
|
+
if (isWeb()) {
|
|
20389
|
+
window.addEventListener('online', this.handleBrowserOnLine);
|
|
20485
20390
|
}
|
|
20486
|
-
|
|
20487
|
-
|
|
20488
|
-
|
|
20489
|
-
|
|
20490
|
-
|
|
20491
|
-
|
|
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.
|
|
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
|
|
21662
|
-
settings.quality = VideoQuality.
|
|
21599
|
+
// defaults to high quality
|
|
21600
|
+
settings.quality = VideoQuality.HIGH;
|
|
21663
21601
|
}
|
|
21664
21602
|
this.emit(TrackEvent.UpdateSettings, settings);
|
|
21665
21603
|
}
|