livekit-client 2.13.8 → 2.14.0

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.
Files changed (49) hide show
  1. package/dist/livekit-client.esm.mjs +184 -88
  2. package/dist/livekit-client.esm.mjs.map +1 -1
  3. package/dist/livekit-client.umd.js +1 -1
  4. package/dist/livekit-client.umd.js.map +1 -1
  5. package/dist/src/e2ee/E2eeManager.d.ts.map +1 -1
  6. package/dist/src/room/PCTransport.d.ts +1 -0
  7. package/dist/src/room/PCTransport.d.ts.map +1 -1
  8. package/dist/src/room/events.d.ts +18 -0
  9. package/dist/src/room/events.d.ts.map +1 -1
  10. package/dist/src/room/participant/LocalParticipant.d.ts +1 -0
  11. package/dist/src/room/participant/LocalParticipant.d.ts.map +1 -1
  12. package/dist/src/room/participant/Participant.d.ts +3 -0
  13. package/dist/src/room/participant/Participant.d.ts.map +1 -1
  14. package/dist/src/room/track/LocalTrackPublication.d.ts +1 -0
  15. package/dist/src/room/track/LocalTrackPublication.d.ts.map +1 -1
  16. package/dist/src/room/track/LocalVideoTrack.d.ts +7 -0
  17. package/dist/src/room/track/LocalVideoTrack.d.ts.map +1 -1
  18. package/dist/src/room/track/Track.d.ts +1 -0
  19. package/dist/src/room/track/Track.d.ts.map +1 -1
  20. package/dist/src/room/track/TrackPublication.d.ts +1 -0
  21. package/dist/src/room/track/TrackPublication.d.ts.map +1 -1
  22. package/dist/src/room/track/options.d.ts +4 -0
  23. package/dist/src/room/track/options.d.ts.map +1 -1
  24. package/dist/src/room/utils.d.ts +1 -1
  25. package/dist/src/room/utils.d.ts.map +1 -1
  26. package/dist/ts4.2/src/room/PCTransport.d.ts +1 -0
  27. package/dist/ts4.2/src/room/events.d.ts +18 -0
  28. package/dist/ts4.2/src/room/participant/LocalParticipant.d.ts +1 -0
  29. package/dist/ts4.2/src/room/participant/Participant.d.ts +3 -0
  30. package/dist/ts4.2/src/room/track/LocalTrackPublication.d.ts +1 -0
  31. package/dist/ts4.2/src/room/track/LocalVideoTrack.d.ts +7 -0
  32. package/dist/ts4.2/src/room/track/Track.d.ts +1 -0
  33. package/dist/ts4.2/src/room/track/TrackPublication.d.ts +1 -0
  34. package/dist/ts4.2/src/room/track/options.d.ts +4 -0
  35. package/dist/ts4.2/src/room/utils.d.ts +1 -1
  36. package/package.json +1 -1
  37. package/src/e2ee/E2eeManager.ts +3 -2
  38. package/src/room/PCTransport.ts +88 -77
  39. package/src/room/events.ts +21 -0
  40. package/src/room/participant/LocalParticipant.ts +14 -2
  41. package/src/room/participant/Participant.ts +3 -0
  42. package/src/room/participant/publishUtils.ts +2 -2
  43. package/src/room/track/LocalTrackPublication.ts +9 -1
  44. package/src/room/track/LocalVideoTrack.ts +68 -1
  45. package/src/room/track/Track.ts +1 -0
  46. package/src/room/track/TrackPublication.ts +1 -0
  47. package/src/room/track/create.ts +2 -2
  48. package/src/room/track/options.ts +5 -0
  49. package/src/room/utils.ts +19 -3
@@ -1,3 +1,4 @@
1
+ import { Mutex } from '@livekit/mutex';
1
2
  import { EventEmitter } from 'events';
2
3
  import type { MediaDescription, SessionDescription } from 'sdp-transform';
3
4
  import { parse, write } from 'sdp-transform';
@@ -52,6 +53,8 @@ export default class PCTransport extends EventEmitter {
52
53
 
53
54
  private latestOfferId: number = 0;
54
55
 
56
+ private offerLock: Mutex;
57
+
55
58
  pendingCandidates: RTCIceCandidateInit[] = [];
56
59
 
57
60
  restartingIce: boolean = false;
@@ -86,6 +89,7 @@ export default class PCTransport extends EventEmitter {
86
89
  this.loggerOptions = loggerOptions;
87
90
  this.config = config;
88
91
  this._pc = this.createPC();
92
+ this.offerLock = new Mutex();
89
93
  }
90
94
 
91
95
  private createPC() {
@@ -251,101 +255,108 @@ export default class PCTransport extends EventEmitter {
251
255
  }, debounceInterval);
252
256
 
253
257
  async createAndSendOffer(options?: RTCOfferOptions) {
254
- // increase the offer id at the start to ensure the offer is always > 0 so that we can use 0 as a default value for legacy behavior
255
- const offerId = this.latestOfferId + 1;
256
- this.latestOfferId = offerId;
257
- if (this.onOffer === undefined) {
258
- return;
259
- }
258
+ const unlock = await this.offerLock.lock();
260
259
 
261
- if (options?.iceRestart) {
262
- this.log.debug('restarting ICE', this.logContext);
263
- this.restartingIce = true;
264
- }
260
+ try {
261
+ // increase the offer id at the start to ensure the offer is always > 0 so that we can use 0 as a default value for legacy behavior
262
+ const offerId = this.latestOfferId + 1;
263
+ this.latestOfferId = offerId;
265
264
 
266
- if (this._pc && this._pc.signalingState === 'have-local-offer') {
267
- // we're waiting for the peer to accept our offer, so we'll just wait
268
- // the only exception to this is when ICE restart is needed
269
- const currentSD = this._pc.remoteDescription;
270
- if (options?.iceRestart && currentSD) {
271
- // TODO: handle when ICE restart is needed but we don't have a remote description
272
- // the best thing to do is to recreate the peerconnection
273
- await this._pc.setRemoteDescription(currentSD);
274
- } else {
275
- this.renegotiate = true;
265
+ if (this.onOffer === undefined) {
276
266
  return;
277
267
  }
278
- } else if (!this._pc || this._pc.signalingState === 'closed') {
279
- this.log.warn('could not createOffer with closed peer connection', this.logContext);
280
- return;
281
- }
282
268
 
283
- // actually negotiate
284
- this.log.debug('starting to negotiate', this.logContext);
285
- const offer = await this.pc.createOffer(options);
286
- this.log.debug('original offer', { sdp: offer.sdp, ...this.logContext });
269
+ if (options?.iceRestart) {
270
+ this.log.debug('restarting ICE', this.logContext);
271
+ this.restartingIce = true;
272
+ }
287
273
 
288
- const sdpParsed = parse(offer.sdp ?? '');
289
- sdpParsed.media.forEach((media) => {
290
- ensureIPAddrMatchVersion(media);
291
- if (media.type === 'audio') {
292
- ensureAudioNackAndStereo(media, [], []);
293
- } else if (media.type === 'video') {
294
- this.trackBitrates.some((trackbr): boolean => {
295
- if (!media.msid || !trackbr.cid || !media.msid.includes(trackbr.cid)) {
296
- return false;
297
- }
274
+ if (this._pc && this._pc.signalingState === 'have-local-offer') {
275
+ // we're waiting for the peer to accept our offer, so we'll just wait
276
+ // the only exception to this is when ICE restart is needed
277
+ const currentSD = this._pc.remoteDescription;
278
+ if (options?.iceRestart && currentSD) {
279
+ // TODO: handle when ICE restart is needed but we don't have a remote description
280
+ // the best thing to do is to recreate the peerconnection
281
+ await this._pc.setRemoteDescription(currentSD);
282
+ } else {
283
+ this.renegotiate = true;
284
+ return;
285
+ }
286
+ } else if (!this._pc || this._pc.signalingState === 'closed') {
287
+ this.log.warn('could not createOffer with closed peer connection', this.logContext);
288
+ return;
289
+ }
298
290
 
299
- let codecPayload = 0;
300
- media.rtp.some((rtp): boolean => {
301
- if (rtp.codec.toUpperCase() === trackbr.codec.toUpperCase()) {
302
- codecPayload = rtp.payload;
303
- return true;
291
+ // actually negotiate
292
+ this.log.debug('starting to negotiate', this.logContext);
293
+ const offer = await this.pc.createOffer(options);
294
+ this.log.debug('original offer', { sdp: offer.sdp, ...this.logContext });
295
+
296
+ const sdpParsed = parse(offer.sdp ?? '');
297
+ sdpParsed.media.forEach((media) => {
298
+ ensureIPAddrMatchVersion(media);
299
+ if (media.type === 'audio') {
300
+ ensureAudioNackAndStereo(media, [], []);
301
+ } else if (media.type === 'video') {
302
+ this.trackBitrates.some((trackbr): boolean => {
303
+ if (!media.msid || !trackbr.cid || !media.msid.includes(trackbr.cid)) {
304
+ return false;
304
305
  }
305
- return false;
306
- });
307
306
 
308
- if (codecPayload === 0) {
309
- return true;
310
- }
307
+ let codecPayload = 0;
308
+ media.rtp.some((rtp): boolean => {
309
+ if (rtp.codec.toUpperCase() === trackbr.codec.toUpperCase()) {
310
+ codecPayload = rtp.payload;
311
+ return true;
312
+ }
313
+ return false;
314
+ });
311
315
 
312
- if (isSVCCodec(trackbr.codec)) {
313
- this.ensureVideoDDExtensionForSVC(media, sdpParsed);
314
- }
316
+ if (codecPayload === 0) {
317
+ return true;
318
+ }
315
319
 
316
- // TODO: av1 slow starting issue already fixed in chrome 124, clean this after some versions
317
- // mung sdp for av1 bitrate setting that can't apply by sendEncoding
318
- if (trackbr.codec !== 'av1') {
319
- return true;
320
- }
320
+ if (isSVCCodec(trackbr.codec)) {
321
+ this.ensureVideoDDExtensionForSVC(media, sdpParsed);
322
+ }
323
+
324
+ // TODO: av1 slow starting issue already fixed in chrome 124, clean this after some versions
325
+ // mung sdp for av1 bitrate setting that can't apply by sendEncoding
326
+ if (trackbr.codec !== 'av1') {
327
+ return true;
328
+ }
321
329
 
322
- const startBitrate = Math.round(trackbr.maxbr * startBitrateForSVC);
330
+ const startBitrate = Math.round(trackbr.maxbr * startBitrateForSVC);
323
331
 
324
- for (const fmtp of media.fmtp) {
325
- if (fmtp.payload === codecPayload) {
326
- // if another track's fmtp already is set, we cannot override the bitrate
327
- // this has the unfortunate consequence of being forced to use the
328
- // initial track's bitrate for all tracks
329
- if (!fmtp.config.includes('x-google-start-bitrate')) {
330
- fmtp.config += `;x-google-start-bitrate=${startBitrate}`;
332
+ for (const fmtp of media.fmtp) {
333
+ if (fmtp.payload === codecPayload) {
334
+ // if another track's fmtp already is set, we cannot override the bitrate
335
+ // this has the unfortunate consequence of being forced to use the
336
+ // initial track's bitrate for all tracks
337
+ if (!fmtp.config.includes('x-google-start-bitrate')) {
338
+ fmtp.config += `;x-google-start-bitrate=${startBitrate}`;
339
+ }
340
+ break;
331
341
  }
332
- break;
333
342
  }
334
- }
335
- return true;
343
+ return true;
344
+ });
345
+ }
346
+ });
347
+ if (this.latestOfferId > offerId) {
348
+ this.log.warn('latestOfferId mismatch', {
349
+ ...this.logContext,
350
+ latestOfferId: this.latestOfferId,
351
+ offerId,
336
352
  });
353
+ return;
337
354
  }
338
- });
339
- if (this.latestOfferId > offerId) {
340
- this.log.warn('latestOfferId mismatch', {
341
- ...this.logContext,
342
- latestOfferId: this.latestOfferId,
343
- offerId,
344
- });
345
- return;
355
+ await this.setMungedSDP(offer, write(sdpParsed));
356
+ this.onOffer(offer, this.latestOfferId);
357
+ } finally {
358
+ unlock();
346
359
  }
347
- await this.setMungedSDP(offer, write(sdpParsed));
348
- this.onOffer(offer, this.latestOfferId);
349
360
  }
350
361
 
351
362
  async createAndSetAnswer(): Promise<RTCSessionDescriptionInit> {
@@ -429,6 +429,21 @@ export enum ParticipantEvent {
429
429
  */
430
430
  LocalTrackUnpublished = 'localTrackUnpublished',
431
431
 
432
+ /**
433
+ * A local track has been constrained by cpu.
434
+ * This event is useful to know when to reduce the capture resolution of the track.
435
+ *
436
+ * This event is emitted on the local participant.
437
+ *
438
+ * args: ([[LocalVideoTrack]], [[LocalTrackPublication]])
439
+ */
440
+ LocalTrackCpuConstrained = 'localTrackCpuConstrained',
441
+
442
+ /**
443
+ * @internal
444
+ */
445
+ LocalSenderCreated = 'localSenderCreated',
446
+
432
447
  /**
433
448
  * Participant metadata is a simple way for app-specific state to be pushed to
434
449
  * all users.
@@ -515,6 +530,11 @@ export enum ParticipantEvent {
515
530
  */
516
531
  TrackSubscriptionStatusChanged = 'trackSubscriptionStatusChanged',
517
532
 
533
+ /**
534
+ * a local track has been constrained by cpu
535
+ */
536
+ TrackCpuConstrained = 'trackCpuConstrained',
537
+
518
538
  // fired only on LocalParticipant
519
539
  /** @internal */
520
540
  MediaDevicesError = 'mediaDevicesError',
@@ -599,6 +619,7 @@ export enum TrackEvent {
599
619
  Ended = 'ended',
600
620
  Subscribed = 'subscribed',
601
621
  Unsubscribed = 'unsubscribed',
622
+ CpuConstrained = 'cpuConstrained',
602
623
  /** @internal */
603
624
  UpdateSettings = 'updateSettings',
604
625
  /** @internal */
@@ -93,7 +93,7 @@ import {
93
93
  isLocalTrack,
94
94
  isLocalVideoTrack,
95
95
  isSVCCodec,
96
- isSafari17,
96
+ isSafari17Based,
97
97
  isVideoTrack,
98
98
  isWeb,
99
99
  numberToBigInt,
@@ -708,7 +708,7 @@ export default class LocalParticipant extends Participant {
708
708
  throw new DeviceUnsupportedError('getDisplayMedia not supported');
709
709
  }
710
710
 
711
- if (options.resolution === undefined && !isSafari17()) {
711
+ if (options.resolution === undefined && !isSafari17Based()) {
712
712
  // we need to constrain the dimensions, otherwise it could lead to low bitrate
713
713
  // due to encoding a huge video. Encoding such large surfaces is really expensive
714
714
  // unfortunately Safari 17 has a but and cannot be constrained by default
@@ -1169,6 +1169,7 @@ export default class LocalParticipant extends Participant {
1169
1169
  }
1170
1170
 
1171
1171
  track.sender = await this.engine.createSender(track, opts, encodings);
1172
+ this.emit(ParticipantEvent.LocalSenderCreated, track.sender, track);
1172
1173
 
1173
1174
  if (isLocalVideoTrack(track)) {
1174
1175
  opts.degradationPreference ??= getDefaultDegradationPreference(track);
@@ -1271,6 +1272,9 @@ export default class LocalParticipant extends Participant {
1271
1272
  loggerName: this.roomOptions.loggerName,
1272
1273
  loggerContextCb: () => this.logContext,
1273
1274
  });
1275
+ publication.on(TrackEvent.CpuConstrained, (constrainedTrack) =>
1276
+ this.onTrackCpuConstrained(constrainedTrack, publication),
1277
+ );
1274
1278
  // save options for when it needs to be republished again
1275
1279
  publication.options = opts;
1276
1280
  track.sid = ti.sid;
@@ -2329,6 +2333,14 @@ export default class LocalParticipant extends Participant {
2329
2333
  this.engine.client.sendUpdateLocalAudioTrack(pub.trackSid, pub.getTrackFeatures());
2330
2334
  };
2331
2335
 
2336
+ private onTrackCpuConstrained = (track: LocalVideoTrack, publication: LocalTrackPublication) => {
2337
+ this.log.debug('track cpu constrained', {
2338
+ ...this.logContext,
2339
+ ...getLogContextFromTrack(publication),
2340
+ });
2341
+ this.emit(ParticipantEvent.LocalTrackCpuConstrained, track, publication);
2342
+ };
2343
+
2332
2344
  private handleSubscribedQualityUpdate = async (update: SubscribedQualityUpdate) => {
2333
2345
  if (!this.roomOptions?.dynacast) {
2334
2346
  return;
@@ -13,6 +13,7 @@ import type TypedEmitter from 'typed-emitter';
13
13
  import log, { LoggerNames, type StructuredLogger, getLogger } from '../../logger';
14
14
  import { ParticipantEvent, TrackEvent } from '../events';
15
15
  import type LocalTrackPublication from '../track/LocalTrackPublication';
16
+ import type LocalVideoTrack from '../track/LocalVideoTrack';
16
17
  import type RemoteTrack from '../track/RemoteTrack';
17
18
  import type RemoteTrackPublication from '../track/RemoteTrackPublication';
18
19
  import { Track } from '../track/Track';
@@ -403,6 +404,8 @@ export type ParticipantEventCallbacks = {
403
404
  trackUnmuted: (publication: TrackPublication) => void;
404
405
  localTrackPublished: (publication: LocalTrackPublication) => void;
405
406
  localTrackUnpublished: (publication: LocalTrackPublication) => void;
407
+ localTrackCpuConstrained: (track: LocalVideoTrack, publication: LocalTrackPublication) => void;
408
+ localSenderCreated: (sender: RTCRtpSender, track: Track) => void;
406
409
  participantMetadataChanged: (prevMetadata: string | undefined, participant?: any) => void;
407
410
  participantNameChanged: (name: string) => void;
408
411
  dataReceived: (payload: Uint8Array, kind: DataPacket_Kind) => void;
@@ -18,7 +18,7 @@ import {
18
18
  isFireFox,
19
19
  isReactNative,
20
20
  isSVCCodec,
21
- isSafari,
21
+ isSafariBased,
22
22
  isSafariSvcApi,
23
23
  unwrapConstraint,
24
24
  } from '../utils';
@@ -151,7 +151,7 @@ export function computeVideoEncodings(
151
151
  // Announced here: https://groups.google.com/g/discuss-webrtc/c/-QQ3pxrl-fw?pli=1
152
152
  const browser = getBrowser();
153
153
  if (
154
- isSafari() ||
154
+ isSafariBased() ||
155
155
  // Even tho RN runs M114, it does not produce SVC layers when a single encoding
156
156
  // is provided. So we'll use the legacy SVC specification for now.
157
157
  // TODO: when we upstream libwebrtc, this will need additional verification
@@ -1,7 +1,7 @@
1
1
  import { AudioTrackFeature, TrackInfo } from '@livekit/protocol';
2
2
  import { TrackEvent } from '../events';
3
3
  import type { LoggerOptions } from '../types';
4
- import { isAudioTrack } from '../utils';
4
+ import { isAudioTrack, isVideoTrack } from '../utils';
5
5
  import LocalAudioTrack from './LocalAudioTrack';
6
6
  import type LocalTrack from './LocalTrack';
7
7
  import type LocalVideoTrack from './LocalVideoTrack';
@@ -28,12 +28,14 @@ export default class LocalTrackPublication extends TrackPublication {
28
28
  setTrack(track?: Track) {
29
29
  if (this.track) {
30
30
  this.track.off(TrackEvent.Ended, this.handleTrackEnded);
31
+ this.track.off(TrackEvent.CpuConstrained, this.handleCpuConstrained);
31
32
  }
32
33
 
33
34
  super.setTrack(track);
34
35
 
35
36
  if (track) {
36
37
  track.on(TrackEvent.Ended, this.handleTrackEnded);
38
+ track.on(TrackEvent.CpuConstrained, this.handleCpuConstrained);
37
39
  }
38
40
  }
39
41
 
@@ -116,4 +118,10 @@ export default class LocalTrackPublication extends TrackPublication {
116
118
  handleTrackEnded = () => {
117
119
  this.emit(TrackEvent.Ended);
118
120
  };
121
+
122
+ private handleCpuConstrained = () => {
123
+ if (this.track && isVideoTrack(this.track)) {
124
+ this.emit(TrackEvent.CpuConstrained, this.track);
125
+ }
126
+ };
119
127
  }
@@ -7,6 +7,7 @@ import {
7
7
  } from '@livekit/protocol';
8
8
  import type { SignalClient } from '../../api/SignalClient';
9
9
  import type { StructuredLogger } from '../../logger';
10
+ import { TrackEvent } from '../events';
10
11
  import { ScalabilityMode } from '../participant/publishUtils';
11
12
  import type { VideoSenderStats } from '../stats';
12
13
  import { computeBitrate, monitorFrequency } from '../stats';
@@ -56,6 +57,10 @@ export default class LocalVideoTrack extends LocalTrack<Track.Kind.Video> {
56
57
 
57
58
  private degradationPreference: RTCDegradationPreference = 'balanced';
58
59
 
60
+ private isCpuConstrained: boolean = false;
61
+
62
+ private optimizeForPerformance: boolean = false;
63
+
59
64
  get sender(): RTCRtpSender | undefined {
60
65
  return this._sender;
61
66
  }
@@ -251,6 +256,9 @@ export default class LocalVideoTrack extends LocalTrack<Track.Kind.Video> {
251
256
  }
252
257
  await this.restart(constraints);
253
258
 
259
+ // reset cpu constrained state after track is restarted
260
+ this.isCpuConstrained = false;
261
+
254
262
  for await (const sc of this.simulcastCodecs.values()) {
255
263
  if (sc.sender && sc.sender.transport?.state !== 'closed') {
256
264
  sc.mediaStreamTrack = this.mediaStreamTrack.clone();
@@ -334,6 +342,7 @@ export default class LocalVideoTrack extends LocalTrack<Track.Kind.Video> {
334
342
  // only enable simulcast codec for preference codec setted
335
343
  if (!this.codec && codecs.length > 0) {
336
344
  await this.setPublishingLayers(isSVCCodec(codecs[0].codec), codecs[0].qualities);
345
+
337
346
  return [];
338
347
  }
339
348
 
@@ -378,6 +387,13 @@ export default class LocalVideoTrack extends LocalTrack<Track.Kind.Video> {
378
387
  * Sets layers that should be publishing
379
388
  */
380
389
  async setPublishingLayers(isSvc: boolean, qualities: SubscribedQuality[]) {
390
+ if (this.optimizeForPerformance) {
391
+ this.log.info('skipping setPublishingLayers due to optimized publishing performance', {
392
+ ...this.logContext,
393
+ qualities,
394
+ });
395
+ return;
396
+ }
381
397
  this.log.debug('setting publishing layers', { ...this.logContext, qualities });
382
398
  if (!this.sender || !this.encodings) {
383
399
  return;
@@ -394,6 +410,49 @@ export default class LocalVideoTrack extends LocalTrack<Track.Kind.Video> {
394
410
  );
395
411
  }
396
412
 
413
+ /**
414
+ * Designed for lower powered devices, reduces video publishing quality and disables simulcast.
415
+ * @experimental
416
+ */
417
+ async prioritizePerformance() {
418
+ if (!this.sender) {
419
+ throw new Error('sender not found');
420
+ }
421
+
422
+ const unlock = await this.senderLock.lock();
423
+
424
+ try {
425
+ this.optimizeForPerformance = true;
426
+ const params = this.sender.getParameters();
427
+
428
+ params.encodings = params.encodings.map((e, idx) => ({
429
+ ...e,
430
+ active: idx === 0,
431
+ scaleResolutionDownBy: Math.max(
432
+ 1,
433
+ Math.ceil((this.mediaStreamTrack.getSettings().height ?? 360) / 360),
434
+ ),
435
+ scalabilityMode: idx === 0 && isSVCCodec(this.codec) ? 'L1T3' : undefined,
436
+ maxFramerate: idx === 0 ? 15 : 0,
437
+ maxBitrate: idx === 0 ? e.maxBitrate : 0,
438
+ }));
439
+ this.log.debug('setting performance optimised encodings', {
440
+ ...this.logContext,
441
+ encodings: params.encodings,
442
+ });
443
+ this.encodings = params.encodings;
444
+ await this.sender.setParameters(params);
445
+ } catch (e) {
446
+ this.log.error('failed to set performance optimised encodings', {
447
+ ...this.logContext,
448
+ error: e,
449
+ });
450
+ this.optimizeForPerformance = false;
451
+ } finally {
452
+ unlock();
453
+ }
454
+ }
455
+
397
456
  protected monitorSender = async () => {
398
457
  if (!this.sender) {
399
458
  this._currentBitrate = 0;
@@ -404,11 +463,19 @@ export default class LocalVideoTrack extends LocalTrack<Track.Kind.Video> {
404
463
  try {
405
464
  stats = await this.getSenderStats();
406
465
  } catch (e) {
407
- this.log.error('could not get audio sender stats', { ...this.logContext, error: e });
466
+ this.log.error('could not get video sender stats', { ...this.logContext, error: e });
408
467
  return;
409
468
  }
410
469
  const statsMap = new Map<string, VideoSenderStats>(stats.map((s) => [s.rid, s]));
411
470
 
471
+ const isCpuConstrained = stats.some((s) => s.qualityLimitationReason === 'cpu');
472
+ if (isCpuConstrained !== this.isCpuConstrained) {
473
+ this.isCpuConstrained = isCpuConstrained;
474
+ if (this.isCpuConstrained) {
475
+ this.emit(TrackEvent.CpuConstrained);
476
+ }
477
+ }
478
+
412
479
  if (this.prevStats) {
413
480
  let totalBitrate = 0;
414
481
  statsMap.forEach((s, key) => {
@@ -529,4 +529,5 @@ export type TrackEventCallbacks = {
529
529
  audioTrackFeatureUpdate: (track: any, feature: AudioTrackFeature, enabled: boolean) => void;
530
530
  timeSyncUpdate: (update: { timestamp: number; rtpTimestamp: number }) => void;
531
531
  preConnectBufferFlushed: (buffer: Uint8Array[]) => void;
532
+ cpuConstrained: () => void;
532
533
  };
@@ -179,4 +179,5 @@ export type PublicationEventCallbacks = {
179
179
  subscriptionFailed: (error: SubscriptionError) => void;
180
180
  transcriptionReceived: (transcription: TranscriptionSegment[]) => void;
181
181
  timeSyncUpdate: (timestamp: number) => void;
182
+ cpuConstrained: (track: LocalVideoTrack) => void;
182
183
  };
@@ -3,7 +3,7 @@ import { audioDefaults, videoDefaults } from '../defaults';
3
3
  import { DeviceUnsupportedError, TrackInvalidError } from '../errors';
4
4
  import { mediaTrackToLocalTrack } from '../participant/publishUtils';
5
5
  import type { LoggerOptions } from '../types';
6
- import { isAudioTrack, isSafari17, isVideoTrack, unwrapConstraint } from '../utils';
6
+ import { isAudioTrack, isSafari17Based, isVideoTrack, unwrapConstraint } from '../utils';
7
7
  import LocalAudioTrack from './LocalAudioTrack';
8
8
  import type LocalTrack from './LocalTrack';
9
9
  import LocalVideoTrack from './LocalVideoTrack';
@@ -198,7 +198,7 @@ export async function createLocalScreenTracks(
198
198
  if (options === undefined) {
199
199
  options = {};
200
200
  }
201
- if (options.resolution === undefined && !isSafari17()) {
201
+ if (options.resolution === undefined && !isSafari17Based()) {
202
202
  options.resolution = ScreenSharePresets.h1080fps30.resolution;
203
203
  }
204
204
 
@@ -173,6 +173,11 @@ export interface VideoCaptureOptions {
173
173
  */
174
174
  deviceId?: ConstrainDOMString;
175
175
 
176
+ /**
177
+ * A ConstrainDouble specifying the frame rate or range of frame rates which are acceptable and/or required.
178
+ */
179
+ frameRate?: ConstrainDouble;
180
+
176
181
  /**
177
182
  * a facing or an array of facings which are acceptable and/or required.
178
183
  */
package/src/room/utils.ts CHANGED
@@ -96,6 +96,14 @@ export function supportsVP9(): boolean {
96
96
  // Safari 16 and below does not support VP9
97
97
  return false;
98
98
  }
99
+ if (
100
+ browser?.os === 'iOS' &&
101
+ browser?.osVersion &&
102
+ compareVersions(browser.osVersion, '16') < 0
103
+ ) {
104
+ // Safari 16 and below on iOS does not support VP9 we need the iOS check to account for other browsers running webkit under the hood
105
+ return false;
106
+ }
99
107
  }
100
108
  const capabilities = RTCRtpSender.getCapabilities('video');
101
109
  let hasVP9 = false;
@@ -148,9 +156,12 @@ export function isSafariBased(): boolean {
148
156
  return b?.name === 'Safari' || b?.os === 'iOS';
149
157
  }
150
158
 
151
- export function isSafari17(): boolean {
159
+ export function isSafari17Based(): boolean {
152
160
  const b = getBrowser();
153
- return b?.name === 'Safari' && b.version.startsWith('17.');
161
+ return (
162
+ (b?.name === 'Safari' && b.version.startsWith('17.')) ||
163
+ (b?.os === 'iOS' && !!b?.osVersion && compareVersions(b.osVersion, '17') >= 0)
164
+ );
154
165
  }
155
166
 
156
167
  export function isSafariSvcApi(browser?: BrowserDetails): boolean {
@@ -158,7 +169,12 @@ export function isSafariSvcApi(browser?: BrowserDetails): boolean {
158
169
  browser = getBrowser();
159
170
  }
160
171
  // Safari 18.4 requires legacy svc api and scaleResolutionDown to be set
161
- return browser?.name === 'Safari' && compareVersions(browser.version, '18.3') > 0;
172
+ return (
173
+ (browser?.name === 'Safari' && compareVersions(browser.version, '18.3') > 0) ||
174
+ (browser?.os === 'iOS' &&
175
+ !!browser?.osVersion &&
176
+ compareVersions(browser.osVersion, '18.3') > 0)
177
+ );
162
178
  }
163
179
 
164
180
  export function isMobile(): boolean {