livekit-client 2.15.10 → 2.15.12

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.
@@ -4262,6 +4262,9 @@ const ParticipantInfo_Kind = /* @__PURE__ */proto3.makeEnum("livekit.Participant
4262
4262
  }, {
4263
4263
  no: 4,
4264
4264
  name: "AGENT"
4265
+ }, {
4266
+ no: 7,
4267
+ name: "CONNECTOR"
4265
4268
  }]);
4266
4269
  const ParticipantInfo_KindDetail = /* @__PURE__ */proto3.makeEnum("livekit.ParticipantInfo.KindDetail", [{
4267
4270
  no: 0,
@@ -5348,6 +5351,19 @@ const DataStream_Trailer = /* @__PURE__ */proto3.makeMessageType("livekit.DataSt
5348
5351
  }], {
5349
5352
  localName: "DataStream_Trailer"
5350
5353
  });
5354
+ const FilterParams = /* @__PURE__ */proto3.makeMessageType("livekit.FilterParams", () => [{
5355
+ no: 1,
5356
+ name: "include_events",
5357
+ kind: "scalar",
5358
+ T: 9,
5359
+ repeated: true
5360
+ }, {
5361
+ no: 2,
5362
+ name: "exclude_events",
5363
+ kind: "scalar",
5364
+ T: 9,
5365
+ repeated: true
5366
+ }]);
5351
5367
  const WebhookConfig = /* @__PURE__ */proto3.makeMessageType("livekit.WebhookConfig", () => [{
5352
5368
  no: 1,
5353
5369
  name: "url",
@@ -5360,6 +5376,11 @@ const WebhookConfig = /* @__PURE__ */proto3.makeMessageType("livekit.WebhookConf
5360
5376
  kind: "scalar",
5361
5377
  T: 9
5362
5378
  /* ScalarType.STRING */
5379
+ }, {
5380
+ no: 3,
5381
+ name: "filter_params",
5382
+ kind: "message",
5383
+ T: FilterParams
5363
5384
  }]);
5364
5385
  const SubscribedAudioCodec = /* @__PURE__ */proto3.makeMessageType("livekit.SubscribedAudioCodec", () => [{
5365
5386
  no: 1,
@@ -6706,6 +6727,16 @@ const SessionDescription = /* @__PURE__ */proto3.makeMessageType("livekit.Sessio
6706
6727
  kind: "scalar",
6707
6728
  T: 13
6708
6729
  /* ScalarType.UINT32 */
6730
+ }, {
6731
+ no: 4,
6732
+ name: "mid_to_track_id",
6733
+ kind: "map",
6734
+ K: 9,
6735
+ V: {
6736
+ kind: "scalar",
6737
+ T: 9
6738
+ /* ScalarType.STRING */
6739
+ }
6709
6740
  }]);
6710
6741
  const ParticipantUpdate = /* @__PURE__ */proto3.makeMessageType("livekit.ParticipantUpdate", () => [{
6711
6742
  no: 1,
@@ -12570,7 +12601,7 @@ function getOSVersion(ua) {
12570
12601
  return ua.includes('mac os') ? getMatch(/\(.+?(\d+_\d+(:?_\d+)?)/, ua, 1).replace(/_/g, '.') : undefined;
12571
12602
  }
12572
12603
 
12573
- var version$1 = "2.15.10";
12604
+ var version$1 = "2.15.12";
12574
12605
 
12575
12606
  const version = version$1;
12576
12607
  const protocolVersion = 16;
@@ -14641,6 +14672,21 @@ function parseSignalResponse(value) {
14641
14672
  }
14642
14673
  throw new Error("could not decode websocket message: ".concat(typeof value));
14643
14674
  }
14675
+ function getAbortReasonAsString(signal) {
14676
+ let defaultMessage = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'Unknown reason';
14677
+ if (!(signal instanceof AbortSignal)) {
14678
+ return defaultMessage;
14679
+ }
14680
+ const reason = signal.reason;
14681
+ switch (typeof reason) {
14682
+ case 'string':
14683
+ return reason;
14684
+ case 'object':
14685
+ return reason instanceof Error ? reason.message : defaultMessage;
14686
+ default:
14687
+ return 'toString' in reason ? reason.toString() : defaultMessage;
14688
+ }
14689
+ }
14644
14690
 
14645
14691
  const passThroughQueueSignals = ['syncState', 'trickle', 'offer', 'answer', 'simulate', 'leave'];
14646
14692
  function canPassThroughQueue(req) {
@@ -14751,26 +14797,33 @@ class SignalClient {
14751
14797
  return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
14752
14798
  var _a, _b;
14753
14799
  try {
14754
- const timeoutAbortController = new AbortController();
14755
- const signals = abortSignal ? [timeoutAbortController.signal, abortSignal] : [timeoutAbortController.signal];
14756
- const combinedAbort = AbortSignal.any(signals);
14757
- const abortHandler = event => __awaiter(this, void 0, void 0, function* () {
14800
+ let alreadyAborted = false;
14801
+ const abortHandler = eventOrError => __awaiter(this, void 0, void 0, function* () {
14802
+ if (alreadyAborted) {
14803
+ return;
14804
+ }
14805
+ alreadyAborted = true;
14806
+ const target = eventOrError instanceof Event ? eventOrError.currentTarget : eventOrError;
14807
+ const reason = getAbortReasonAsString(target, 'Abort handler called');
14758
14808
  // send leave if we have an active stream writer (connection is open)
14759
- if (this.streamWriter) {
14760
- this.sendLeave().then(() => this.close()).catch(e => {
14809
+ if (this.streamWriter && !this.isDisconnected) {
14810
+ this.sendLeave().then(() => this.close(reason)).catch(e => {
14761
14811
  this.log.error(e);
14762
14812
  this.close();
14763
14813
  });
14764
14814
  } else {
14765
14815
  this.close();
14766
14816
  }
14767
- clearTimeout(wsTimeout);
14768
- const target = event.currentTarget;
14817
+ cleanupAbortHandlers();
14769
14818
  reject(target instanceof AbortSignal ? target.reason : target);
14770
14819
  });
14771
- combinedAbort.addEventListener('abort', abortHandler);
14820
+ abortSignal === null || abortSignal === void 0 ? void 0 : abortSignal.addEventListener('abort', abortHandler);
14821
+ const cleanupAbortHandlers = () => {
14822
+ clearTimeout(wsTimeout);
14823
+ abortSignal === null || abortSignal === void 0 ? void 0 : abortSignal.removeEventListener('abort', abortHandler);
14824
+ };
14772
14825
  const wsTimeout = setTimeout(() => {
14773
- timeoutAbortController.abort(new ConnectionError('room connection has timed out (signal)', ConnectionErrorReason.ServerUnreachable));
14826
+ abortHandler(new ConnectionError('room connection has timed out (signal)', ConnectionErrorReason.ServerUnreachable));
14774
14827
  }, opts.websocketTimeout);
14775
14828
  const handleSignalConnected = (connection, firstMessage) => {
14776
14829
  this.handleSignalConnected(connection, wsTimeout, firstMessage);
@@ -14786,9 +14839,7 @@ class SignalClient {
14786
14839
  if (this.ws) {
14787
14840
  yield this.close(false);
14788
14841
  }
14789
- this.ws = new WebSocketStream(rtcUrl, {
14790
- signal: combinedAbort
14791
- });
14842
+ this.ws = new WebSocketStream(rtcUrl);
14792
14843
  try {
14793
14844
  this.ws.closed.then(closeInfo => {
14794
14845
  if (this.isEstablishingConnection) {
@@ -14855,8 +14906,9 @@ class SignalClient {
14855
14906
  handleSignalConnected(connection, firstMessageToProcess);
14856
14907
  resolve(validation.response);
14857
14908
  } catch (e) {
14858
- clearTimeout(wsTimeout);
14859
14909
  reject(e);
14910
+ } finally {
14911
+ cleanupAbortHandlers();
14860
14912
  }
14861
14913
  } finally {
14862
14914
  unlock();
@@ -14889,6 +14941,7 @@ class SignalClient {
14889
14941
  return __awaiter(this, arguments, void 0, function () {
14890
14942
  var _this = this;
14891
14943
  let updateState = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
14944
+ let reason = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'Close method called on signal client';
14892
14945
  return function* () {
14893
14946
  const unlock = yield _this.closingLock.lock();
14894
14947
  try {
@@ -14899,7 +14952,7 @@ class SignalClient {
14899
14952
  if (_this.ws) {
14900
14953
  _this.ws.close({
14901
14954
  closeCode: 1000,
14902
- reason: 'Close method called on signal client'
14955
+ reason
14903
14956
  });
14904
14957
  // calling `ws.close()` only starts the closing handshake (CLOSING state), prefer to wait until state is actually CLOSED
14905
14958
  const closePromise = _this.ws.closed;
@@ -15119,12 +15172,12 @@ class SignalClient {
15119
15172
  if (msg.case === 'answer') {
15120
15173
  const sd = fromProtoSessionDescription(msg.value);
15121
15174
  if (this.onAnswer) {
15122
- this.onAnswer(sd, msg.value.id);
15175
+ this.onAnswer(sd, msg.value.id, msg.value.midToTrackId);
15123
15176
  }
15124
15177
  } else if (msg.case === 'offer') {
15125
15178
  const sd = fromProtoSessionDescription(msg.value);
15126
15179
  if (this.onOffer) {
15127
- this.onOffer(sd, msg.value.id);
15180
+ this.onOffer(sd, msg.value.id, msg.value.midToTrackId);
15128
15181
  }
15129
15182
  } else if (msg.case === 'trickle') {
15130
15183
  const candidate = JSON.parse(msg.value.candidateInit);
@@ -15227,7 +15280,7 @@ class SignalClient {
15227
15280
  return __awaiter(this, void 0, void 0, function* () {
15228
15281
  if (this.state === SignalConnectionState.DISCONNECTED) return;
15229
15282
  const onCloseCallback = this.onClose;
15230
- yield this.close();
15283
+ yield this.close(undefined, reason);
15231
15284
  this.log.debug("websocket connection closed: ".concat(reason), Object.assign(Object.assign({}, this.logContext), {
15232
15285
  reason
15233
15286
  }));
@@ -17184,6 +17237,11 @@ class PCTransportManager {
17184
17237
  addPublisherTransceiverOfKind(kind, transceiverInit) {
17185
17238
  return this.publisher.addTransceiverOfKind(kind, transceiverInit);
17186
17239
  }
17240
+ getMidForReceiver(receiver) {
17241
+ const transceivers = this.subscriber ? this.subscriber.getTransceivers() : this.publisher.getTransceivers();
17242
+ const matchingTransceiver = transceivers.find(transceiver => transceiver.receiver === receiver);
17243
+ return matchingTransceiver === null || matchingTransceiver === void 0 ? void 0 : matchingTransceiver.mid;
17244
+ }
17187
17245
  addPublisherTrack(track) {
17188
17246
  return this.publisher.addTrack(track);
17189
17247
  }
@@ -19344,6 +19402,7 @@ class RTCEngine extends eventsExports.EventEmitter {
19344
19402
  this.reliableDataSequence = 1;
19345
19403
  this.reliableMessageBuffer = new DataPacketBuffer();
19346
19404
  this.reliableReceivedState = new TTLMap(reliabeReceiveStateTTL);
19405
+ this.midToTrackId = {};
19347
19406
  this.handleDataChannel = _a => __awaiter(this, [_a], void 0, function (_ref) {
19348
19407
  var _this = this;
19349
19408
  let {
@@ -19766,14 +19825,16 @@ class RTCEngine extends eventsExports.EventEmitter {
19766
19825
  }
19767
19826
  setupSignalClientCallbacks() {
19768
19827
  // configure signaling client
19769
- this.client.onAnswer = (sd, offerId) => __awaiter(this, void 0, void 0, function* () {
19828
+ this.client.onAnswer = (sd, offerId, midToTrackId) => __awaiter(this, void 0, void 0, function* () {
19770
19829
  if (!this.pcManager) {
19771
19830
  return;
19772
19831
  }
19773
19832
  this.log.debug('received server answer', Object.assign(Object.assign({}, this.logContext), {
19774
19833
  RTCSdpType: sd.type,
19775
- sdp: sd.sdp
19834
+ sdp: sd.sdp,
19835
+ midToTrackId
19776
19836
  }));
19837
+ this.midToTrackId = midToTrackId;
19777
19838
  yield this.pcManager.setPublisherAnswer(sd, offerId);
19778
19839
  });
19779
19840
  // add candidate on trickle
@@ -19788,11 +19849,12 @@ class RTCEngine extends eventsExports.EventEmitter {
19788
19849
  this.pcManager.addIceCandidate(candidate, target);
19789
19850
  };
19790
19851
  // when server creates an offer for the client
19791
- this.client.onOffer = (sd, offerId) => __awaiter(this, void 0, void 0, function* () {
19852
+ this.client.onOffer = (sd, offerId, midToTrackId) => __awaiter(this, void 0, void 0, function* () {
19792
19853
  this.latestRemoteOfferId = offerId;
19793
19854
  if (!this.pcManager) {
19794
19855
  return;
19795
19856
  }
19857
+ this.midToTrackId = midToTrackId;
19796
19858
  const answer = yield this.pcManager.createSubscriberAnswerFromOffer(sd, offerId);
19797
19859
  if (answer) {
19798
19860
  this.client.sendAnswer(answer, offerId);
@@ -20578,6 +20640,19 @@ class RTCEngine extends eventsExports.EventEmitter {
20578
20640
  window.removeEventListener('online', this.handleBrowserOnLine);
20579
20641
  }
20580
20642
  }
20643
+ getTrackIdForReceiver(receiver) {
20644
+ var _a;
20645
+ const mid = (_a = this.pcManager) === null || _a === void 0 ? void 0 : _a.getMidForReceiver(receiver);
20646
+ if (mid) {
20647
+ const match = Object.entries(this.midToTrackId).find(_ref2 => {
20648
+ let [key] = _ref2;
20649
+ return key === mid;
20650
+ });
20651
+ if (match) {
20652
+ return match[1];
20653
+ }
20654
+ }
20655
+ }
20581
20656
  }
20582
20657
  class SignalReconnectError extends Error {}
20583
20658
  function supportOptionalDatachannel(protocol) {
@@ -24259,6 +24334,7 @@ class LocalParticipant extends Participant {
24259
24334
  } = _ref3;
24260
24335
  return function* () {
24261
24336
  const maxRoundTripLatency = 7000;
24337
+ const minEffectiveTimeout = maxRoundTripLatency + 1000;
24262
24338
  return new Promise((resolve, reject) => __awaiter(_this5, void 0, void 0, function* () {
24263
24339
  var _a, _b, _c, _d;
24264
24340
  if (byteLength(payload) > MAX_PAYLOAD_BYTES) {
@@ -24269,8 +24345,9 @@ class LocalParticipant extends Participant {
24269
24345
  reject(RpcError.builtIn('UNSUPPORTED_SERVER'));
24270
24346
  return;
24271
24347
  }
24348
+ const effectiveTimeout = Math.max(responseTimeout, minEffectiveTimeout);
24272
24349
  const id = crypto.randomUUID();
24273
- yield this.publishRpcRequest(destinationIdentity, id, method, payload, responseTimeout - maxRoundTripLatency);
24350
+ yield this.publishRpcRequest(destinationIdentity, id, method, payload, effectiveTimeout);
24274
24351
  const ackTimeoutId = setTimeout(() => {
24275
24352
  this.pendingAcks.delete(id);
24276
24353
  reject(RpcError.builtIn('CONNECTION_TIMEOUT'));
@@ -26361,6 +26438,11 @@ class Room extends eventsExports.EventEmitter {
26361
26438
  // We'll defer these events until when the room is connected or eventually disconnected.
26362
26439
  if (this.state === ConnectionState.Connecting || this.state === ConnectionState.Reconnecting) {
26363
26440
  const reconnectedHandler = () => {
26441
+ this.log.debug('deferring on track for later', {
26442
+ mediaTrackId: mediaTrack.id,
26443
+ mediaStreamId: stream.id,
26444
+ tracksInStream: stream.getTracks().map(track => track.id)
26445
+ });
26364
26446
  this.onTrackAdded(mediaTrack, stream, receiver);
26365
26447
  cleanup();
26366
26448
  };
@@ -26398,6 +26480,24 @@ class Room extends eventsExports.EventEmitter {
26398
26480
  this.log.error("Tried to add a track for a participant, that's not present. Sid: ".concat(participantSid), this.logContext);
26399
26481
  return;
26400
26482
  }
26483
+ // in single peer connection case, the trackID is locally generated,
26484
+ // not the TR_ prefixed one generated by the server,
26485
+ // use `mid` to find the appropriate track.
26486
+ if (!trackId.startsWith('TR')) {
26487
+ const id = this.engine.getTrackIdForReceiver(receiver);
26488
+ if (!id) {
26489
+ this.log.error("Tried to add a track whose 'sid' could not be found for a participant, that's not present. Sid: ".concat(participantSid), this.logContext);
26490
+ return;
26491
+ }
26492
+ trackId = id;
26493
+ }
26494
+ if (!trackId.startsWith('TR')) {
26495
+ this.log.warn("Tried to add a track whose 'sid' could not be determined for a participant, that's not present. Sid: ".concat(participantSid, ", streamId: ").concat(streamId, ", trackId: ").concat(trackId), Object.assign(Object.assign({}, this.logContext), {
26496
+ rpID: participantSid,
26497
+ streamId,
26498
+ trackId
26499
+ }));
26500
+ }
26401
26501
  let adaptiveStreamSettings;
26402
26502
  if (this.options.adaptiveStream) {
26403
26503
  if (typeof this.options.adaptiveStream === 'object') {
@@ -26901,6 +27001,12 @@ class Room extends eventsExports.EventEmitter {
26901
27001
  if (event !== RoomEvent.ActiveSpeakersChanged && event !== RoomEvent.TranscriptionReceived) {
26902
27002
  // only extract logContext from arguments in order to avoid logging the whole object tree
26903
27003
  const minimizedArgs = mapArgs(args).filter(arg => arg !== undefined);
27004
+ if (event === RoomEvent.TrackSubscribed || event === RoomEvent.TrackUnsubscribed) {
27005
+ this.log.trace("subscribe trace: ".concat(event), Object.assign(Object.assign({}, this.logContext), {
27006
+ event,
27007
+ args: minimizedArgs
27008
+ }));
27009
+ }
26904
27010
  this.log.debug("room event ".concat(event), Object.assign(Object.assign({}, this.logContext), {
26905
27011
  event,
26906
27012
  args: minimizedArgs