@whereby.com/media 1.4.2 → 1.5.1

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.
@@ -10,6 +10,62 @@ import checkIp from 'check-ip';
10
10
  import validate from 'uuid-validate';
11
11
  import { io } from 'socket.io-client';
12
12
 
13
+ const debugOn = process.env.NODE_ENV === "development" || new URLSearchParams(window.location.search).has("debug");
14
+ class Logger {
15
+ constructor() {
16
+ this._isEnabled = false;
17
+ this._debugger = null;
18
+ this._isEnabled = debugOn;
19
+ }
20
+ isEnabled() {
21
+ return this._isEnabled;
22
+ }
23
+ enable() {
24
+ this._isEnabled = true;
25
+ }
26
+ disable() {
27
+ this._isEnabled = false;
28
+ }
29
+ info(...params) {
30
+ if (!this._isEnabled) {
31
+ return;
32
+ }
33
+ return console.info(...params);
34
+ }
35
+ warn(...params) {
36
+ if (!this._isEnabled) {
37
+ return;
38
+ }
39
+ return console.warn(...params);
40
+ }
41
+ error(...params) {
42
+ if (!this._isEnabled) {
43
+ return;
44
+ }
45
+ return console.error(...params);
46
+ }
47
+ withDebugLogger(myDebugger = null) {
48
+ this._debugger = myDebugger;
49
+ return this;
50
+ }
51
+ debug(...params) {
52
+ if (!this._isEnabled || !this._debugger) {
53
+ return;
54
+ }
55
+ const suppliedParams = [];
56
+ params.forEach((param) => {
57
+ if (typeof param === "function") {
58
+ const suppliedParam = param();
59
+ suppliedParams.push(suppliedParam);
60
+ }
61
+ else {
62
+ suppliedParams.push(param);
63
+ }
64
+ });
65
+ this._debugger.print(...suppliedParams);
66
+ }
67
+ }
68
+
13
69
  /******************************************************************************
14
70
  Copyright (c) Microsoft Corporation.
15
71
 
@@ -42,34 +98,6 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
42
98
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
43
99
  };
44
100
 
45
- let peerConnections = [];
46
- let peerConnectionCounter = 0;
47
- const peerConnectionData = new WeakMap();
48
- const removePeerConnection = (pc) => {
49
- peerConnections = peerConnections.filter((old) => old !== pc);
50
- };
51
- if (window.RTCPeerConnection) {
52
- const OriginalRTCPeerConnection = window.RTCPeerConnection;
53
- function PatchedRTCPeerConnection(rtcConfig) {
54
- const pc = new OriginalRTCPeerConnection(rtcConfig);
55
- peerConnections.push(pc);
56
- peerConnectionData.set(pc, { index: peerConnectionCounter++ });
57
- const onConnectionStateChange = () => {
58
- if (pc.connectionState === "closed") {
59
- removePeerConnection(pc);
60
- pc.removeEventListener("connectionstatechange", onConnectionStateChange);
61
- }
62
- };
63
- pc.addEventListener("connectionstatechange", onConnectionStateChange);
64
- return pc;
65
- }
66
- PatchedRTCPeerConnection.prototype = OriginalRTCPeerConnection.prototype;
67
- window.RTCPeerConnection = PatchedRTCPeerConnection;
68
- }
69
- const getCurrentPeerConnections = () => peerConnections;
70
- const getPeerConnectionIndex = (pc) => { var _a; return (_a = peerConnectionData.get(pc)) === null || _a === void 0 ? void 0 : _a.index; };
71
- const setPeerConnectionsForTests = (pcs) => (peerConnections = pcs);
72
-
73
101
  function captureCandidatePairInfoMetrics(cpMetrics, currentCptats, prevCptats, timeDiff, report) {
74
102
  const bytesReceivedDiff = currentCptats.bytesReceived - ((prevCptats === null || prevCptats === void 0 ? void 0 : prevCptats.bytesReceived) || 0);
75
103
  const bytesSentDiff = currentCptats.bytesSent - ((prevCptats === null || prevCptats === void 0 ? void 0 : prevCptats.bytesSent) || 0);
@@ -231,119 +259,34 @@ function captureVideoSsrcMetrics(ssrcMetrics, currentSsrcStats, prevSsrcStats, t
231
259
  }
232
260
  }
233
261
 
234
- const debugOn = process.env.NODE_ENV === "development" || new URLSearchParams(window.location.search).has("debug");
235
- class Logger {
236
- constructor() {
237
- this._isEnabled = false;
238
- this._debugger = null;
239
- this._isEnabled = debugOn;
240
- }
241
- isEnabled() {
242
- return this._isEnabled;
243
- }
244
- enable() {
245
- this._isEnabled = true;
246
- }
247
- disable() {
248
- this._isEnabled = false;
249
- }
250
- info(...params) {
251
- if (!this._isEnabled) {
252
- return;
253
- }
254
- return console.info(...params);
255
- }
256
- warn(...params) {
257
- if (!this._isEnabled) {
258
- return;
259
- }
260
- return console.warn(...params);
261
- }
262
- error(...params) {
263
- if (!this._isEnabled) {
264
- return;
265
- }
266
- return console.error(...params);
267
- }
268
- withDebugLogger(myDebugger = null) {
269
- this._debugger = myDebugger;
270
- return this;
271
- }
272
- debug(...params) {
273
- if (!this._isEnabled || !this._debugger) {
274
- return;
275
- }
276
- const suppliedParams = [];
277
- params.forEach((param) => {
278
- if (typeof param === "function") {
279
- const suppliedParam = param();
280
- suppliedParams.push(suppliedParam);
281
- }
282
- else {
283
- suppliedParams.push(param);
262
+ let peerConnections = [];
263
+ let peerConnectionCounter = 0;
264
+ const peerConnectionData = new WeakMap();
265
+ const removePeerConnection = (pc) => {
266
+ peerConnections = peerConnections.filter((old) => old !== pc);
267
+ };
268
+ if (window.RTCPeerConnection) {
269
+ const OriginalRTCPeerConnection = window.RTCPeerConnection;
270
+ function PatchedRTCPeerConnection(rtcConfig) {
271
+ const pc = new OriginalRTCPeerConnection(rtcConfig);
272
+ peerConnections.push(pc);
273
+ peerConnectionData.set(pc, { index: peerConnectionCounter++ });
274
+ const onConnectionStateChange = () => {
275
+ if (pc.connectionState === "closed") {
276
+ removePeerConnection(pc);
277
+ pc.removeEventListener("connectionstatechange", onConnectionStateChange);
284
278
  }
285
- });
286
- this._debugger.print(...suppliedParams);
279
+ };
280
+ pc.addEventListener("connectionstatechange", onConnectionStateChange);
281
+ return pc;
287
282
  }
283
+ PatchedRTCPeerConnection.prototype = OriginalRTCPeerConnection.prototype;
284
+ window.RTCPeerConnection = PatchedRTCPeerConnection;
288
285
  }
286
+ const getCurrentPeerConnections = () => peerConnections;
287
+ const getPeerConnectionIndex = (pc) => { var _a; return (_a = peerConnectionData.get(pc)) === null || _a === void 0 ? void 0 : _a.index; };
288
+ const setPeerConnectionsForTests = (pcs) => (peerConnections = pcs);
289
289
 
290
- const logger$a = new Logger();
291
- const STATS_INTERVAL = 2000;
292
- let getClients = () => [];
293
- const setClientProvider = (provider) => (getClients = provider);
294
- const statsByView = {};
295
- const getStats = () => {
296
- return Object.assign({}, statsByView);
297
- };
298
- let subscriptions$1 = [];
299
- let currentMonitor = null;
300
- const getUpdatedStats = () => currentMonitor === null || currentMonitor === void 0 ? void 0 : currentMonitor.getUpdatedStats();
301
- const getOrCreateSsrcMetricsContainer = (time, pcIndex, clientId, trackId, ssrc) => {
302
- let viewStats = statsByView[clientId];
303
- if (!viewStats) {
304
- viewStats = { tracks: {}, startTime: time, updated: time };
305
- statsByView[clientId] = viewStats;
306
- }
307
- viewStats.updated = time;
308
- let trackStats = viewStats.tracks[trackId];
309
- if (!trackStats) {
310
- trackStats = { ssrcs: {}, startTime: time, updated: time };
311
- viewStats.tracks[trackId] = trackStats;
312
- }
313
- trackStats.updated = time;
314
- let ssrcStats = trackStats.ssrcs[ssrc];
315
- if (!ssrcStats) {
316
- ssrcStats = {
317
- startTime: time,
318
- updated: time,
319
- pcIndex,
320
- };
321
- trackStats.ssrcs[ssrc] = ssrcStats;
322
- }
323
- ssrcStats.updated = time;
324
- return ssrcStats;
325
- };
326
- const removeNonUpdatedStats = (time) => {
327
- Object.entries(statsByView).forEach(([viewId, viewStats]) => {
328
- if (viewStats.updated < time) {
329
- delete statsByView[viewId];
330
- }
331
- else {
332
- Object.entries(viewStats.tracks).forEach(([trackId, trackStats]) => {
333
- if (trackStats.updated < time) {
334
- delete viewStats.tracks[trackId];
335
- }
336
- else {
337
- Object.entries(trackStats.ssrcs).forEach(([ssrc, ssrcStats]) => {
338
- if (ssrcStats.updated < time) {
339
- delete trackStats.ssrcs[ssrc];
340
- }
341
- });
342
- }
343
- });
344
- }
345
- });
346
- };
347
290
  const pcDataByPc = new WeakMap();
348
291
  const getPeerConnectionsWithStatsReports = () => Promise.all(getCurrentPeerConnections().map((pc) => __awaiter(void 0, void 0, void 0, function* () {
349
292
  let pcData = pcDataByPc.get(pc);
@@ -403,59 +346,74 @@ const getPeerConnectionsWithStatsReports = () => Promise.all(getCurrentPeerConne
403
346
  return [pc, [], pcData];
404
347
  }
405
348
  })));
406
- let originTrialActivated = false;
407
- function activateComputePressureOriginTrial() {
408
- if (originTrialActivated)
409
- return;
410
- const otMeta = document.createElement("meta");
411
- otMeta.httpEquiv = "origin-trial";
412
- if (/hereby\.dev/.test(document.location.hostname)) {
413
- otMeta.content =
414
- "AkSNPHJw6EK08X0QU7kORnK9NABzRLAC7dqfXOwk5JwFAQG4Ey7WxLXxsnhieCgC1QHUdevE2EFICy7uBGDANwUAAABqeyJvcmlnaW4iOiJodHRwczovL2hlcmVieS5kZXY6NDQ0MyIsImZlYXR1cmUiOiJDb21wdXRlUHJlc3N1cmVfdjIiLCJleHBpcnkiOjE3MTY5NDA3OTksImlzU3ViZG9tYWluIjp0cnVlfQ==";
349
+
350
+ const getOrCreateSsrcMetricsContainer = (statsByView, time, pcIndex, clientId, trackId, ssrc) => {
351
+ let viewStats = statsByView[clientId];
352
+ if (!viewStats) {
353
+ viewStats = { tracks: {}, startTime: time, updated: time };
354
+ statsByView[clientId] = viewStats;
415
355
  }
416
- else {
417
- otMeta.content =
418
- "Asc2wu8KpSx648i932NICteQDFcB05yl2QUUSHD7AQo8JGP2Fp6FF91TvYVJBsKGzLMH349rysPw5q9tqPC/PAUAAABqeyJvcmlnaW4iOiJodHRwczovL3doZXJlYnkuY29tOjQ0MyIsImZlYXR1cmUiOiJDb21wdXRlUHJlc3N1cmVfdjIiLCJleHBpcnkiOjE3MTY5NDA3OTksImlzU3ViZG9tYWluIjp0cnVlfQ==";
356
+ viewStats.updated = time;
357
+ let trackStats = viewStats.tracks[trackId];
358
+ if (!trackStats) {
359
+ trackStats = { ssrcs: {}, startTime: time, updated: time };
360
+ viewStats.tracks[trackId] = trackStats;
419
361
  }
420
- document.head.append(otMeta);
421
- originTrialActivated = true;
422
- }
423
- function startStatsMonitor({ interval }) {
424
- let nextTimeout = 0;
425
- activateComputePressureOriginTrial();
426
- let pressureObserver;
427
- let lastPressureObserverRecord;
428
- try {
429
- if ("PressureObserver" in window) {
430
- pressureObserver = new window.PressureObserver((records) => (lastPressureObserverRecord = records.pop()), {
431
- sampleRate: 1,
362
+ trackStats.updated = time;
363
+ let ssrcStats = trackStats.ssrcs[ssrc];
364
+ if (!ssrcStats) {
365
+ ssrcStats = {
366
+ startTime: time,
367
+ updated: time,
368
+ pcIndex,
369
+ };
370
+ trackStats.ssrcs[ssrc] = ssrcStats;
371
+ }
372
+ ssrcStats.updated = time;
373
+ return ssrcStats;
374
+ };
375
+ const removeNonUpdatedStats = (statsByView, time) => {
376
+ Object.entries(statsByView).forEach(([viewId, viewStats]) => {
377
+ if (viewStats.updated < time) {
378
+ delete statsByView[viewId];
379
+ }
380
+ else {
381
+ Object.entries(viewStats.tracks).forEach(([trackId, trackStats]) => {
382
+ if (trackStats.updated < time) {
383
+ delete viewStats.tracks[trackId];
384
+ }
385
+ else {
386
+ Object.entries(trackStats.ssrcs).forEach(([ssrc, ssrcStats]) => {
387
+ if (ssrcStats.updated < time) {
388
+ delete trackStats.ssrcs[ssrc];
389
+ }
390
+ });
391
+ }
432
392
  });
433
- pressureObserver.observe("cpu");
434
393
  }
435
- }
436
- catch (ex) {
437
- logger$a.warn("Failed to observe CPU pressure", ex);
438
- }
439
- let lastUpdateTime = 0;
440
- const collectStats = (immediate) => __awaiter(this, void 0, void 0, function* () {
394
+ });
395
+ };
396
+ function collectStats(state, { logger, interval }, immediate) {
397
+ return __awaiter(this, void 0, void 0, function* () {
398
+ const collectStatsBound = collectStats.bind(null, state, { interval, logger });
441
399
  try {
442
- const clients = getClients();
400
+ const clients = state.getClients();
443
401
  const defaultClient = clients.find((c) => c.isLocalClient && !c.isPresentation) || { id: "unknown" };
444
- let defaultViewStats = statsByView[defaultClient.id];
402
+ let defaultViewStats = state.statsByView[defaultClient.id];
445
403
  if (!defaultViewStats) {
446
404
  defaultViewStats = { tracks: {}, candidatePairs: {}, pressure: null };
447
- statsByView[defaultClient.id] = defaultViewStats;
405
+ state.statsByView[defaultClient.id] = defaultViewStats;
448
406
  }
449
- defaultViewStats.pressure = lastPressureObserverRecord;
450
- const timeSinceLastUpdate = Date.now() - lastUpdateTime;
407
+ defaultViewStats.pressure = state.lastPressureObserverRecord;
408
+ const timeSinceLastUpdate = Date.now() - state.lastUpdateTime;
451
409
  if (timeSinceLastUpdate < 400) {
452
410
  if (immediate)
453
- return statsByView;
454
- subscriptions$1.forEach((subscription) => { var _a; return (_a = subscription.onUpdatedStats) === null || _a === void 0 ? void 0 : _a.call(subscription, statsByView, clients); });
455
- nextTimeout = setTimeout(collectStats, interval || STATS_INTERVAL);
411
+ return state.statsByView;
412
+ state.subscriptions.forEach((subscription) => { var _a; return (_a = subscription.onUpdatedStats) === null || _a === void 0 ? void 0 : _a.call(subscription, state.statsByView, clients); });
413
+ state.nextTimeout = setTimeout(collectStatsBound, interval);
456
414
  return;
457
415
  }
458
- lastUpdateTime = Date.now();
416
+ state.lastUpdateTime = Date.now();
459
417
  (yield getPeerConnectionsWithStatsReports()).forEach(([pc, report, pcData]) => {
460
418
  const pcIndex = getPeerConnectionIndex(pc);
461
419
  if (pc.connectionState === "closed") {
@@ -466,20 +424,17 @@ function startStatsMonitor({ interval }) {
466
424
  pcData.currentSSRCs = {};
467
425
  report.forEach((currentRtcStats) => {
468
426
  var _a, _b;
469
- if (currentRtcStats.type === "candidate-pair" &&
470
- /inprogress|succeeded/.test(currentRtcStats.state)) {
427
+ if (currentRtcStats.type === "candidate-pair" && /inprogress|succeeded/.test(currentRtcStats.state)) {
471
428
  const prevRtcStats = (_a = pcData._oldReport) === null || _a === void 0 ? void 0 : _a.get(currentRtcStats.id);
472
- const timeDiff = prevRtcStats
473
- ? currentRtcStats.timestamp - prevRtcStats.timestamp
474
- : STATS_INTERVAL;
429
+ const timeDiff = prevRtcStats ? currentRtcStats.timestamp - prevRtcStats.timestamp : interval;
475
430
  const cpId = pcIndex + ":" + currentRtcStats.id;
476
431
  let cpMetrics = defaultViewStats.candidatePairs[cpId];
477
432
  if (!cpMetrics) {
478
- cpMetrics = { startTime: lastUpdateTime, id: cpId };
433
+ cpMetrics = { startTime: state.lastUpdateTime, id: cpId };
479
434
  defaultViewStats.candidatePairs[cpId] = cpMetrics;
480
435
  }
481
436
  captureCandidatePairInfoMetrics(cpMetrics, currentRtcStats, prevRtcStats, timeDiff, report);
482
- cpMetrics.lastRtcStatsTime = lastUpdateTime;
437
+ cpMetrics.lastRtcStatsTime = state.lastUpdateTime;
483
438
  }
484
439
  if (currentRtcStats.type === "inbound-rtp" || currentRtcStats.type === "outbound-rtp") {
485
440
  const kind = currentRtcStats.mediaType || currentRtcStats.kind;
@@ -507,10 +462,8 @@ function startStatsMonitor({ interval }) {
507
462
  prevRtcStats = null;
508
463
  }
509
464
  }
510
- const timeDiff = prevRtcStats
511
- ? currentRtcStats.timestamp - prevRtcStats.timestamp
512
- : STATS_INTERVAL;
513
- const ssrcMetrics = getOrCreateSsrcMetricsContainer(lastUpdateTime, pcIndex, client.id, trackId, ssrc);
465
+ const timeDiff = prevRtcStats ? currentRtcStats.timestamp - prevRtcStats.timestamp : interval;
466
+ const ssrcMetrics = getOrCreateSsrcMetricsContainer(state.statsByView, state.lastUpdateTime, pcIndex, client.id, trackId, ssrc);
514
467
  captureSsrcInfo(ssrcMetrics, currentRtcStats, prevRtcStats, timeDiff, report);
515
468
  captureCommonSsrcMetrics(ssrcMetrics, currentRtcStats, prevRtcStats, timeDiff, report);
516
469
  if (kind === "video") {
@@ -527,7 +480,7 @@ function startStatsMonitor({ interval }) {
527
480
  .forEach((ssrc) => {
528
481
  const clientId = pcData.previousSSRCs[ssrc];
529
482
  if (clientId) {
530
- const clientView = statsByView[clientId];
483
+ const clientView = state.statsByView[clientId];
531
484
  if (clientView) {
532
485
  Object.values(clientView.tracks).forEach((trackStats) => {
533
486
  if (trackStats.ssrcs[ssrc]) {
@@ -538,55 +491,127 @@ function startStatsMonitor({ interval }) {
538
491
  }
539
492
  });
540
493
  });
541
- removeNonUpdatedStats(lastUpdateTime);
494
+ removeNonUpdatedStats(state.statsByView, state.lastUpdateTime);
542
495
  Object.entries(defaultViewStats.candidatePairs).forEach(([cpKey, cp]) => {
543
- const active = cp.lastRtcStatsTime === lastUpdateTime;
496
+ const active = cp.lastRtcStatsTime === state.lastUpdateTime;
544
497
  cp.active = active;
545
498
  if (!active) {
546
499
  cp.state = "old/inactive";
547
500
  if (!cp.inactiveFromTime)
548
- cp.inactiveFromTime = lastUpdateTime;
501
+ cp.inactiveFromTime = state.lastUpdateTime;
549
502
  else {
550
- if (lastUpdateTime - cp.inactiveFromTime > 4000) {
503
+ if (state.lastUpdateTime - cp.inactiveFromTime > 4000) {
551
504
  delete defaultViewStats.candidatePairs[cpKey];
552
505
  }
553
506
  }
554
507
  }
555
508
  });
556
509
  if (immediate) {
557
- return statsByView;
510
+ return state.statsByView;
558
511
  }
559
512
  else {
560
- subscriptions$1.forEach((subscription) => { var _a; return (_a = subscription.onUpdatedStats) === null || _a === void 0 ? void 0 : _a.call(subscription, statsByView, clients); });
513
+ state.subscriptions.forEach((subscription) => { var _a; return (_a = subscription.onUpdatedStats) === null || _a === void 0 ? void 0 : _a.call(subscription, state.statsByView, clients); });
561
514
  }
562
515
  }
563
516
  catch (ex) {
564
- logger$a.warn(ex);
517
+ logger.warn(ex);
565
518
  }
566
- nextTimeout = setTimeout(collectStats, interval || STATS_INTERVAL);
519
+ state.nextTimeout = setTimeout(collectStatsBound, interval);
567
520
  });
568
- setTimeout(collectStats, interval || STATS_INTERVAL);
569
- return {
570
- stop: () => {
571
- clearTimeout(nextTimeout);
572
- if (pressureObserver)
573
- pressureObserver.unobserve("cpu");
521
+ }
522
+
523
+ const REGISTERED_TRIALS = {};
524
+ function registerOriginTrials(trials, registeredTrials = REGISTERED_TRIALS, document = window.document) {
525
+ trials.forEach(({ hostnamePattern, token }) => {
526
+ const key = `${hostnamePattern}-${token}`;
527
+ if (registeredTrials[key]) {
528
+ return;
529
+ }
530
+ if (hostnamePattern.test(document.location.hostname)) {
531
+ const otMeta = document.createElement("meta");
532
+ otMeta.httpEquiv = "origin-trial";
533
+ otMeta.content = token;
534
+ document.head.append(otMeta);
535
+ registeredTrials[key] = true;
536
+ }
537
+ });
538
+ }
539
+
540
+ const CPU_OBSERVER_OPTIONS = {
541
+ sampleRate: 1,
542
+ originTrials: [
543
+ {
544
+ hostnamePattern: /hereby\.dev/,
545
+ token: "AkSNPHJw6EK08X0QU7kORnK9NABzRLAC7dqfXOwk5JwFAQG4Ey7WxLXxsnhieCgC1QHUdevE2EFICy7uBGDANwUAAABqeyJvcmlnaW4iOiJodHRwczovL2hlcmVieS5kZXY6NDQ0MyIsImZlYXR1cmUiOiJDb21wdXRlUHJlc3N1cmVfdjIiLCJleHBpcnkiOjE3MTY5NDA3OTksImlzU3ViZG9tYWluIjp0cnVlfQ==",
546
+ },
547
+ {
548
+ hostnamePattern: /whereby\.com/,
549
+ token: "Asc2wu8KpSx648i932NICteQDFcB05yl2QUUSHD7AQo8JGP2Fp6FF91TvYVJBsKGzLMH349rysPw5q9tqPC/PAUAAABqeyJvcmlnaW4iOiJodHRwczovL3doZXJlYnkuY29tOjQ0MyIsImZlYXR1cmUiOiJDb21wdXRlUHJlc3N1cmVfdjIiLCJleHBpcnkiOjE3MTY5NDA3OTksImlzU3ViZG9tYWluIjp0cnVlfQ==",
574
550
  },
551
+ ],
552
+ };
553
+ function startCpuObserver(cb, { sampleRate, originTrials } = CPU_OBSERVER_OPTIONS, window = globalThis.window) {
554
+ registerOriginTrials(originTrials);
555
+ let pressureObserver;
556
+ if ("PressureObserver" in window) {
557
+ pressureObserver = new window.PressureObserver(cb, { sampleRate });
558
+ pressureObserver.observe("cpu", { sampleInterval: sampleRate * 1000 });
559
+ return {
560
+ stop: () => {
561
+ pressureObserver.unobserve("cpu");
562
+ },
563
+ };
564
+ }
565
+ }
566
+
567
+ const STATE = {
568
+ currentMonitor: null,
569
+ getClients: () => [],
570
+ lastUpdateTime: 0,
571
+ statsByView: {},
572
+ subscriptions: [],
573
+ };
574
+ const OPTIONS = {
575
+ interval: 2000,
576
+ logger: new Logger(),
577
+ };
578
+ const getStats = () => {
579
+ return Object.assign({}, STATE.statsByView);
580
+ };
581
+ const getUpdatedStats = () => { var _a; return (_a = STATE.currentMonitor) === null || _a === void 0 ? void 0 : _a.getUpdatedStats(); };
582
+ const setClientProvider = (provider) => (STATE.getClients = provider);
583
+ function startStatsMonitor(state, { interval, logger }) {
584
+ const collectStatsBound = collectStats.bind(null, state, { interval, logger });
585
+ let cpuObserver;
586
+ try {
587
+ cpuObserver = startCpuObserver((records) => (state.lastPressureObserverRecord = records.pop()));
588
+ }
589
+ catch (ex) {
590
+ logger.warn("Failed to observe CPU pressure", ex);
591
+ }
592
+ setTimeout(collectStatsBound, interval);
593
+ return {
575
594
  getUpdatedStats: () => {
576
- return collectStats(true);
595
+ return collectStatsBound(true);
596
+ },
597
+ stop: () => {
598
+ clearTimeout(state.nextTimeout);
599
+ if (cpuObserver)
600
+ cpuObserver.stop();
577
601
  },
578
602
  };
579
603
  }
580
- function subscribeStats(subscription) {
581
- subscriptions$1.push(subscription);
582
- if (!currentMonitor)
583
- currentMonitor = startStatsMonitor({});
604
+ function subscribeStats(subscription, options = OPTIONS, state = STATE) {
605
+ state.subscriptions.push(subscription);
606
+ if (!state.currentMonitor)
607
+ state.currentMonitor = startStatsMonitor(state, options);
584
608
  return {
585
609
  stop() {
586
- subscriptions$1 = subscriptions$1.filter((s) => s !== subscription);
587
- if (!subscriptions$1.length) {
588
- currentMonitor === null || currentMonitor === void 0 ? void 0 : currentMonitor.stop();
589
- currentMonitor = null;
610
+ var _a;
611
+ state.subscriptions = state.subscriptions.filter((s) => s !== subscription);
612
+ if (!state.subscriptions.length) {
613
+ (_a = state.currentMonitor) === null || _a === void 0 ? void 0 : _a.stop();
614
+ state.currentMonitor = null;
590
615
  }
591
616
  },
592
617
  };
@@ -4971,13 +4996,9 @@ class VegaRtcManager {
4971
4996
  }
4972
4997
  _connect() {
4973
4998
  const host = this._features.sfuServerOverrideHost || [this._sfuServer.url];
4974
- const searchParams = new URLSearchParams({
4975
- clientId: this._selfId,
4976
- organizationId: this._room.organizationId,
4977
- roomName: this._room.name,
4978
- eventClaim: this._room.isClaimed ? this._eventClaim : null,
4979
- lowBw: "true",
4980
- });
4999
+ const searchParams = new URLSearchParams(Object.assign({ clientId: this._selfId, organizationId: this._room.organizationId, roomName: this._room.name, eventClaim: this._room.isClaimed ? this._eventClaim : null, lowBw: "true" }, Object.keys(this._features || {})
5000
+ .filter((featureKey) => this._features[featureKey] && /^sfu/.test(featureKey))
5001
+ .reduce((prev, current) => (Object.assign(Object.assign({}, prev), { [current]: this._features[current] })), {})));
4981
5002
  const queryString = searchParams.toString();
4982
5003
  const wsUrl = `wss://${host}?${queryString}`;
4983
5004
  this._vegaConnection = new VegaConnection(wsUrl);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@whereby.com/media",
3
3
  "description": "Media library for Whereby",
4
- "version": "1.4.2",
4
+ "version": "1.5.1",
5
5
  "license": "MIT",
6
6
  "homepage": "https://github.com/whereby/sdk",
7
7
  "repository": {