livekit-client 1.1.1 → 1.1.2

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.
@@ -7049,7 +7049,8 @@ function createBaseAddTrackRequest() {
7049
7049
  disableDtx: false,
7050
7050
  source: 0,
7051
7051
  layers: [],
7052
- simulcastCodecs: []
7052
+ simulcastCodecs: [],
7053
+ sid: ''
7053
7054
  };
7054
7055
  }
7055
7056
 
@@ -7097,6 +7098,10 @@ const AddTrackRequest = {
7097
7098
  SimulcastCodec.encode(v, writer.uint32(82).fork()).ldelim();
7098
7099
  }
7099
7100
 
7101
+ if (message.sid !== '') {
7102
+ writer.uint32(90).string(message.sid);
7103
+ }
7104
+
7100
7105
  return writer;
7101
7106
  },
7102
7107
 
@@ -7149,6 +7154,10 @@ const AddTrackRequest = {
7149
7154
  message.simulcastCodecs.push(SimulcastCodec.decode(reader, reader.uint32()));
7150
7155
  break;
7151
7156
 
7157
+ case 11:
7158
+ message.sid = reader.string();
7159
+ break;
7160
+
7152
7161
  default:
7153
7162
  reader.skipType(tag & 7);
7154
7163
  break;
@@ -7169,7 +7178,8 @@ const AddTrackRequest = {
7169
7178
  disableDtx: isSet(object.disableDtx) ? Boolean(object.disableDtx) : false,
7170
7179
  source: isSet(object.source) ? trackSourceFromJSON(object.source) : 0,
7171
7180
  layers: Array.isArray(object === null || object === void 0 ? void 0 : object.layers) ? object.layers.map(e => VideoLayer.fromJSON(e)) : [],
7172
- simulcastCodecs: Array.isArray(object === null || object === void 0 ? void 0 : object.simulcastCodecs) ? object.simulcastCodecs.map(e => SimulcastCodec.fromJSON(e)) : []
7181
+ simulcastCodecs: Array.isArray(object === null || object === void 0 ? void 0 : object.simulcastCodecs) ? object.simulcastCodecs.map(e => SimulcastCodec.fromJSON(e)) : [],
7182
+ sid: isSet(object.sid) ? String(object.sid) : ''
7173
7183
  };
7174
7184
  },
7175
7185
 
@@ -7196,11 +7206,12 @@ const AddTrackRequest = {
7196
7206
  obj.simulcastCodecs = [];
7197
7207
  }
7198
7208
 
7209
+ message.sid !== undefined && (obj.sid = message.sid);
7199
7210
  return obj;
7200
7211
  },
7201
7212
 
7202
7213
  fromPartial(object) {
7203
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
7214
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
7204
7215
 
7205
7216
  const message = createBaseAddTrackRequest();
7206
7217
  message.cid = (_a = object.cid) !== null && _a !== void 0 ? _a : '';
@@ -7213,6 +7224,7 @@ const AddTrackRequest = {
7213
7224
  message.source = (_h = object.source) !== null && _h !== void 0 ? _h : 0;
7214
7225
  message.layers = ((_j = object.layers) === null || _j === void 0 ? void 0 : _j.map(e => VideoLayer.fromPartial(e))) || [];
7215
7226
  message.simulcastCodecs = ((_k = object.simulcastCodecs) === null || _k === void 0 ? void 0 : _k.map(e => SimulcastCodec.fromPartial(e))) || [];
7227
+ message.sid = (_l = object.sid) !== null && _l !== void 0 ? _l : '';
7216
7228
  return message;
7217
7229
  }
7218
7230
 
@@ -10058,7 +10070,7 @@ function computeBitrate(currentStats, prevStats) {
10058
10070
  return (bytesNow - bytesPrev) * 8 * 1000 / (currentStats.timestamp - prevStats.timestamp);
10059
10071
  }
10060
10072
 
10061
- var version$1 = "1.1.1";
10073
+ var version$1 = "1.1.2";
10062
10074
 
10063
10075
  const version = version$1;
10064
10076
  const protocolVersion = 8;
@@ -10130,9 +10142,10 @@ function getEmptyVideoStreamTrack() {
10130
10142
  var _a;
10131
10143
 
10132
10144
  if (!emptyVideoStreamTrack) {
10133
- const canvas = document.createElement('canvas');
10134
- canvas.width = 2;
10135
- canvas.height = 2;
10145
+ const canvas = document.createElement('canvas'); // the canvas size is set to 16, because electron apps seem to fail with smaller values
10146
+
10147
+ canvas.width = 16;
10148
+ canvas.height = 16;
10136
10149
  (_a = canvas.getContext('2d')) === null || _a === void 0 ? void 0 : _a.fillRect(0, 0, canvas.width, canvas.height); // @ts-ignore
10137
10150
 
10138
10151
  const emptyStream = canvas.captureStream();
@@ -11514,6 +11527,7 @@ class LocalAudioTrack extends LocalTrack {
11514
11527
 
11515
11528
  }
11516
11529
 
11530
+ const refreshSubscribedCodecAfterNewCodec = 5000;
11517
11531
  class LocalVideoTrack extends LocalTrack {
11518
11532
  constructor(mediaTrack, constraints) {
11519
11533
  super(mediaTrack, Track.Kind.Video, constraints);
@@ -11730,7 +11744,14 @@ class LocalVideoTrack extends LocalTrack {
11730
11744
  return;
11731
11745
  }
11732
11746
 
11733
- simulcastCodecInfo.sender = sender;
11747
+ simulcastCodecInfo.sender = sender; // browser will reenable disabled codec/layers after new codec has been published,
11748
+ // so refresh subscribedCodecs after publish a new codec
11749
+
11750
+ setTimeout(() => {
11751
+ if (this.subscribedCodecs) {
11752
+ this.setPublishingCodecs(this.subscribedCodecs);
11753
+ }
11754
+ }, refreshSubscribedCodecAfterNewCodec);
11734
11755
  }
11735
11756
  /**
11736
11757
  * @internal
@@ -11739,25 +11760,41 @@ class LocalVideoTrack extends LocalTrack {
11739
11760
 
11740
11761
 
11741
11762
  async setPublishingCodecs(codecs) {
11742
- livekitLogger.debug('setting publishing codecs', codecs);
11763
+ livekitLogger.debug('setting publishing codecs', {
11764
+ codecs,
11765
+ currentCodec: this.codec
11766
+ }); // only enable simulcast codec for preference codec setted
11767
+
11768
+ if (!this.codec && codecs.length > 0) {
11769
+ await this.setPublishingLayers(codecs[0].qualities);
11770
+ return [];
11771
+ }
11772
+
11773
+ this.subscribedCodecs = codecs;
11774
+ const newCodecs = [];
11743
11775
 
11744
11776
  for await (const codec of codecs) {
11745
- if (this.codec === codec.codec) {
11777
+ if (!this.codec || this.codec === codec.codec) {
11746
11778
  await this.setPublishingLayers(codec.qualities);
11747
11779
  } else {
11748
11780
  const simulcastCodecInfo = this.simulcastCodecs.get(codec.codec);
11749
11781
  livekitLogger.debug("try setPublishingCodec for ".concat(codec.codec), simulcastCodecInfo);
11750
11782
 
11751
11783
  if (!simulcastCodecInfo || !simulcastCodecInfo.sender) {
11752
- return;
11753
- }
11754
-
11755
- if (simulcastCodecInfo.encodings) {
11784
+ for (const q of codec.qualities) {
11785
+ if (q.enabled) {
11786
+ newCodecs.push(codec.codec);
11787
+ break;
11788
+ }
11789
+ }
11790
+ } else if (simulcastCodecInfo.encodings) {
11756
11791
  livekitLogger.debug("try setPublishingLayersForSender ".concat(codec.codec));
11757
11792
  await setPublishingLayersForSender(simulcastCodecInfo.sender, simulcastCodecInfo.encodings, codec.qualities);
11758
11793
  }
11759
11794
  }
11760
11795
  }
11796
+
11797
+ return newCodecs;
11761
11798
  }
11762
11799
  /**
11763
11800
  * @internal
@@ -13605,7 +13642,7 @@ class RemoteParticipant extends Participant {
13605
13642
 
13606
13643
  }
13607
13644
 
13608
- const compatibleCodecForSVC = 'vp8';
13645
+ const compatibleCodec = 'vp8';
13609
13646
  class LocalParticipant extends Participant {
13610
13647
  /** @internal */
13611
13648
  constructor(sid, identity, engine, options) {
@@ -13654,8 +13691,8 @@ class LocalParticipant extends Participant {
13654
13691
  this.onTrackMuted(track, track.isMuted);
13655
13692
  };
13656
13693
 
13657
- this.handleSubscribedQualityUpdate = update => {
13658
- var _a, _b, _c;
13694
+ this.handleSubscribedQualityUpdate = async update => {
13695
+ var _a, _b;
13659
13696
 
13660
13697
  if (!((_a = this.roomOptions) === null || _a === void 0 ? void 0 : _a.dynacast)) {
13661
13698
  return;
@@ -13672,9 +13709,18 @@ class LocalParticipant extends Participant {
13672
13709
  }
13673
13710
 
13674
13711
  if (update.subscribedCodecs.length > 0) {
13675
- (_b = pub.videoTrack) === null || _b === void 0 ? void 0 : _b.setPublishingCodecs(update.subscribedCodecs);
13712
+ if (!pub.videoTrack) {
13713
+ return;
13714
+ }
13715
+
13716
+ const newCodecs = await pub.videoTrack.setPublishingCodecs(update.subscribedCodecs);
13717
+
13718
+ for await (const codec of newCodecs) {
13719
+ livekitLogger.debug("publish ".concat(codec, " for ").concat(pub.videoTrack.sid));
13720
+ await this.publishAdditionalCodecForTrack(pub.videoTrack, codec, pub.options);
13721
+ }
13676
13722
  } else if (update.subscribedQualities.length > 0) {
13677
- (_c = pub.videoTrack) === null || _c === void 0 ? void 0 : _c.setPublishingLayers(update.subscribedQualities);
13723
+ (_b = pub.videoTrack) === null || _b === void 0 ? void 0 : _b.setPublishingLayers(update.subscribedQualities);
13678
13724
  }
13679
13725
  };
13680
13726
 
@@ -14088,7 +14134,6 @@ class LocalParticipant extends Participant {
14088
14134
 
14089
14135
  let encodings;
14090
14136
  let simEncodings;
14091
- let simulcastTracks;
14092
14137
 
14093
14138
  if (track.kind === Track.Kind.Video) {
14094
14139
  // TODO: support react native, which doesn't expose getSettings
@@ -14099,26 +14144,30 @@ class LocalParticipant extends Participant {
14099
14144
  req.width = width !== null && width !== void 0 ? width : 0;
14100
14145
  req.height = height !== null && height !== void 0 ? height : 0; // for svc codecs, disable simulcast and use vp8 for backup codec
14101
14146
 
14102
- if (track instanceof LocalVideoTrack && ((opts === null || opts === void 0 ? void 0 : opts.videoCodec) === 'vp9' || (opts === null || opts === void 0 ? void 0 : opts.videoCodec) === 'av1')) {
14103
- // set scalabilityMode to 'L3T3' by default
14104
- opts.scalabilityMode = (_g = opts.scalabilityMode) !== null && _g !== void 0 ? _g : 'L3T3'; // add backup codec track
14105
-
14106
- const simOpts = _objectSpread2({}, opts);
14107
-
14108
- simOpts.simulcast = true;
14109
- simOpts.scalabilityMode = undefined;
14110
- simEncodings = computeVideoEncodings(track.source === Track.Source.ScreenShare, width, height, simOpts);
14111
- const simulcastTrack = track.addSimulcastTrack(compatibleCodecForSVC, simEncodings);
14112
- simulcastTracks = [simulcastTrack];
14113
- req.simulcastCodecs = [{
14114
- codec: opts.videoCodec,
14115
- cid: track.mediaStreamTrack.id,
14116
- enableSimulcastLayers: true
14117
- }, {
14118
- codec: simulcastTrack.codec,
14119
- cid: simulcastTrack.mediaStreamTrack.id,
14120
- enableSimulcastLayers: true
14121
- }];
14147
+ if (track instanceof LocalVideoTrack) {
14148
+ if ((opts === null || opts === void 0 ? void 0 : opts.videoCodec) === 'vp9' || (opts === null || opts === void 0 ? void 0 : opts.videoCodec) === 'av1') {
14149
+ // set scalabilityMode to 'L3T3' by default
14150
+ opts.scalabilityMode = (_g = opts.scalabilityMode) !== null && _g !== void 0 ? _g : 'L3T3'; // add backup codec track
14151
+
14152
+ const simOpts = _objectSpread2({}, opts);
14153
+
14154
+ simOpts.simulcast = true;
14155
+ simOpts.scalabilityMode = undefined;
14156
+ simEncodings = computeVideoEncodings(track.source === Track.Source.ScreenShare, width, height, simOpts);
14157
+ } // set vp8 codec as backup for any other codecs
14158
+
14159
+
14160
+ if (opts.videoCodec && opts.videoCodec !== 'vp8') {
14161
+ req.simulcastCodecs = [{
14162
+ codec: opts.videoCodec,
14163
+ cid: track.mediaStreamTrack.id,
14164
+ enableSimulcastLayers: true
14165
+ }, {
14166
+ codec: compatibleCodec,
14167
+ cid: '',
14168
+ enableSimulcastLayers: true
14169
+ }];
14170
+ }
14122
14171
  }
14123
14172
 
14124
14173
  encodings = computeVideoEncodings(track.source === Track.Source.ScreenShare, width, height, opts);
@@ -14163,24 +14212,6 @@ class LocalParticipant extends Participant {
14163
14212
  track.codec = opts.videoCodec;
14164
14213
  }
14165
14214
 
14166
- const localTrack = track;
14167
-
14168
- if (simulcastTracks) {
14169
- for await (const simulcastTrack of simulcastTracks) {
14170
- const simTransceiverInit = {
14171
- direction: 'sendonly'
14172
- };
14173
-
14174
- if (simulcastTrack.encodings) {
14175
- simTransceiverInit.sendEncodings = simulcastTrack.encodings;
14176
- }
14177
-
14178
- const simTransceiver = await this.engine.publisher.pc.addTransceiver(simulcastTrack.mediaStreamTrack, simTransceiverInit);
14179
- this.setPreferredCodec(simTransceiver, localTrack.kind, simulcastTrack.codec);
14180
- localTrack.setSimulcastTrackSender(simulcastTrack.codec, simTransceiver.sender);
14181
- }
14182
- }
14183
-
14184
14215
  this.engine.negotiate(); // store RTPSender
14185
14216
 
14186
14217
  track.sender = transceiver.sender;
@@ -14196,6 +14227,86 @@ class LocalParticipant extends Participant {
14196
14227
  this.emit(ParticipantEvent.LocalTrackPublished, publication);
14197
14228
  return publication;
14198
14229
  }
14230
+ /** @internal
14231
+ * publish additional codec to existing track
14232
+ */
14233
+
14234
+
14235
+ async publishAdditionalCodecForTrack(track, videoCodec, options) {
14236
+ var _a, _b, _c, _d, _e;
14237
+
14238
+ const opts = _objectSpread2(_objectSpread2({}, (_a = this.roomOptions) === null || _a === void 0 ? void 0 : _a.publishDefaults), options); // clear scalabilityMode setting for backup codec
14239
+
14240
+
14241
+ opts.scalabilityMode = undefined;
14242
+ opts.videoCodec = videoCodec; // is it not published? if so skip
14243
+
14244
+ let existingPublication;
14245
+ this.tracks.forEach(publication => {
14246
+ if (!publication.track) {
14247
+ return;
14248
+ }
14249
+
14250
+ if (publication.track === track) {
14251
+ existingPublication = publication;
14252
+ }
14253
+ });
14254
+
14255
+ if (!existingPublication) {
14256
+ throw new TrackInvalidError('track is not published');
14257
+ }
14258
+
14259
+ if (!(track instanceof LocalVideoTrack)) {
14260
+ throw new TrackInvalidError('track is not a video track');
14261
+ }
14262
+
14263
+ const settings = track.mediaStreamTrack.getSettings();
14264
+ const width = (_b = settings.width) !== null && _b !== void 0 ? _b : (_c = track.dimensions) === null || _c === void 0 ? void 0 : _c.width;
14265
+ const height = (_d = settings.height) !== null && _d !== void 0 ? _d : (_e = track.dimensions) === null || _e === void 0 ? void 0 : _e.height;
14266
+ const encodings = computeVideoEncodings(track.source === Track.Source.ScreenShare, width, height, opts);
14267
+ const simulcastTrack = track.addSimulcastTrack(opts.videoCodec, encodings);
14268
+ const req = AddTrackRequest.fromPartial({
14269
+ cid: simulcastTrack.mediaStreamTrack.id,
14270
+ type: Track.kindToProto(track.kind),
14271
+ muted: track.isMuted,
14272
+ source: Track.sourceToProto(track.source),
14273
+ sid: track.sid,
14274
+ simulcastCodecs: [{
14275
+ codec: opts.videoCodec,
14276
+ cid: simulcastTrack.mediaStreamTrack.id,
14277
+ enableSimulcastLayers: opts.simulcast
14278
+ }]
14279
+ });
14280
+ req.layers = videoLayersFromEncodings(req.width, req.height, encodings);
14281
+
14282
+ if (!this.engine || this.engine.isClosed) {
14283
+ throw new UnexpectedConnectionState('cannot publish track when not connected');
14284
+ }
14285
+
14286
+ const ti = await this.engine.addTrack(req);
14287
+
14288
+ if (!this.engine.publisher) {
14289
+ throw new UnexpectedConnectionState('publisher is closed');
14290
+ }
14291
+
14292
+ const transceiverInit = {
14293
+ direction: 'sendonly'
14294
+ };
14295
+
14296
+ if (encodings) {
14297
+ transceiverInit.sendEncodings = encodings;
14298
+ } // addTransceiver for react-native is async. web is synchronous, but await won't effect it.
14299
+
14300
+
14301
+ const transceiver = await this.engine.publisher.pc.addTransceiver(simulcastTrack.mediaStreamTrack, transceiverInit);
14302
+ this.setPreferredCodec(transceiver, track.kind, opts.videoCodec);
14303
+ track.setSimulcastTrackSender(opts.videoCodec, transceiver.sender);
14304
+ this.engine.negotiate();
14305
+ livekitLogger.debug("published ".concat(opts.videoCodec, " for track ").concat(track.sid), {
14306
+ encodings,
14307
+ trackInfo: ti
14308
+ });
14309
+ }
14199
14310
 
14200
14311
  unpublishTrack(track, stopOnUnpublish) {
14201
14312
  var _a, _b; // look through all published tracks to find the right ones
@@ -19266,8 +19377,6 @@ class Room extends events.exports.EventEmitter {
19266
19377
  constructor(options) {
19267
19378
  var _this;
19268
19379
 
19269
- var _a, _b;
19270
-
19271
19380
  super();
19272
19381
  _this = this;
19273
19382
  this.state = ConnectionState.Disconnected;
@@ -19692,14 +19801,6 @@ class Room extends events.exports.EventEmitter {
19692
19801
  this.participants = new Map();
19693
19802
  this.identityToSid = new Map();
19694
19803
  this.options = options || {};
19695
-
19696
- switch ((_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.publishDefaults) === null || _b === void 0 ? void 0 : _b.videoCodec) {
19697
- case 'av1':
19698
- case 'vp9':
19699
- this.options.publishDefaults.simulcast = undefined;
19700
- break;
19701
- }
19702
-
19703
19804
  this.options.audioCaptureDefaults = _objectSpread2(_objectSpread2({}, audioDefaults), options === null || options === void 0 ? void 0 : options.audioCaptureDefaults);
19704
19805
  this.options.videoCaptureDefaults = _objectSpread2(_objectSpread2({}, videoDefaults), options === null || options === void 0 ? void 0 : options.videoCaptureDefaults);
19705
19806
  this.options.publishDefaults = _objectSpread2(_objectSpread2({}, publishDefaults), options === null || options === void 0 ? void 0 : options.publishDefaults);