@observertc/observer-js 1.3.0-rc.4 → 1.3.0-rc.5

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/index.mjs CHANGED
@@ -1778,19 +1778,17 @@ function isPublicIpAddress(address) {
1778
1778
 
1779
1779
  // src/ObservedOutboundTrack.ts
1780
1780
  var ObservedOutboundTrack = class {
1781
- constructor(timestamp, id, kind, _peerConnection, _outboundRtps, _mediaSource) {
1781
+ constructor(timestamp, id, kind, _peerConnection, _mediaSource) {
1782
1782
  this.timestamp = timestamp;
1783
1783
  this.id = id;
1784
1784
  this.kind = kind;
1785
1785
  this._peerConnection = _peerConnection;
1786
- this._outboundRtps = _outboundRtps;
1787
1786
  this._mediaSource = _mediaSource;
1788
1787
  }
1789
1788
  timestamp;
1790
1789
  id;
1791
1790
  kind;
1792
1791
  _peerConnection;
1793
- _outboundRtps;
1794
1792
  _mediaSource;
1795
1793
  _visited = false;
1796
1794
  appData;
@@ -1841,8 +1839,11 @@ var ObservedOutboundTrack = class {
1841
1839
  getPeerConnection() {
1842
1840
  return this._peerConnection;
1843
1841
  }
1842
+ /** The outbound RTPs of this track's media source, looked up on every call. */
1844
1843
  getOutboundRtps() {
1845
- return this._outboundRtps;
1844
+ const mediaSource = this._mediaSource;
1845
+ if (!mediaSource) return void 0;
1846
+ return [...this._peerConnection.observedOutboundRtps.values()].filter((rtp) => rtp.mediaSourceId === mediaSource.id);
1846
1847
  }
1847
1848
  getMediaSource() {
1848
1849
  return this._mediaSource;
@@ -1861,7 +1862,7 @@ var ObservedOutboundTrack = class {
1861
1862
  this.remoteFractionLost = void 0;
1862
1863
  this.remoteRttInMs = void 0;
1863
1864
  this.qualityLimitationReason = void 0;
1864
- for (const rtp of this._outboundRtps ?? []) {
1865
+ for (const rtp of this.getOutboundRtps() ?? []) {
1865
1866
  this.bitrate = (this.bitrate ?? 0) + rtp.bitrate;
1866
1867
  this.deltaPacketsSent = (this.deltaPacketsSent ?? 0) + rtp.deltaPacketsSent;
1867
1868
  if (rtp.remoteFractionLost !== void 0) {
@@ -2526,9 +2527,7 @@ var ObservedPeerConnection = class extends EventEmitter2 {
2526
2527
  }
2527
2528
  for (const observedOutboundTrack of [...this.observedOutboundTracks.values()]) {
2528
2529
  if (observedOutboundTrack.visited) continue;
2529
- this.observedOutboundTracks.delete(observedOutboundTrack.id);
2530
- this.emit("removed-outbound-track", observedOutboundTrack);
2531
- this._notify("outbound-track-removed", { ...this.eventScope, observedOutboundTrack });
2530
+ this._removeOutboundTrack(observedOutboundTrack);
2532
2531
  }
2533
2532
  for (const observedPeerConnectionTransport of [...this.observedPeerConnectionTransports.values()]) {
2534
2533
  if (observedPeerConnectionTransport.visited) continue;
@@ -2789,6 +2788,11 @@ var ObservedPeerConnection = class extends EventEmitter2 {
2789
2788
  }
2790
2789
  _updateOutboundTrackSample(stats) {
2791
2790
  let observedOutboundTrack = this.observedOutboundTracks.get(stats.id);
2791
+ const mediaSource = [...this.observedMediaSources.values()].filter((source) => source.trackIdentifier === stats.id).pop();
2792
+ if (observedOutboundTrack && observedOutboundTrack.getMediaSource() !== mediaSource) {
2793
+ this._removeOutboundTrack(observedOutboundTrack);
2794
+ observedOutboundTrack = void 0;
2795
+ }
2792
2796
  if (!observedOutboundTrack) {
2793
2797
  if (!stats.timestamp || !stats.id || !stats.kind) {
2794
2798
  return logger3.warn(
@@ -2796,15 +2800,12 @@ var ObservedPeerConnection = class extends EventEmitter2 {
2796
2800
  stats
2797
2801
  );
2798
2802
  }
2799
- const observedMediaSource = [...this.observedMediaSources.values()].find((mediaSource) => mediaSource.trackIdentifier === stats.id);
2800
- const outboundRtps = observedMediaSource ? [...this.observedOutboundRtps.values()].filter((outboundRtp) => outboundRtp.mediaSourceId === observedMediaSource?.id) : void 0;
2801
2803
  observedOutboundTrack = new ObservedOutboundTrack(
2802
2804
  stats.timestamp,
2803
2805
  stats.id,
2804
2806
  stats.kind,
2805
2807
  this,
2806
- outboundRtps,
2807
- observedMediaSource
2808
+ mediaSource
2808
2809
  );
2809
2810
  observedOutboundTrack.update(stats);
2810
2811
  this.observedOutboundTracks.set(stats.id, observedOutboundTrack);
@@ -2815,6 +2816,11 @@ var ObservedPeerConnection = class extends EventEmitter2 {
2815
2816
  this._notify("outbound-track-updated", { ...this.eventScope, observedOutboundTrack });
2816
2817
  return observedOutboundTrack;
2817
2818
  }
2819
+ _removeOutboundTrack(observedOutboundTrack) {
2820
+ this.observedOutboundTracks.delete(observedOutboundTrack.id);
2821
+ this.emit("removed-outbound-track", observedOutboundTrack);
2822
+ this._notify("outbound-track-removed", { ...this.eventScope, observedOutboundTrack });
2823
+ }
2818
2824
  _updatePeerConnectionTransportStats(stats) {
2819
2825
  let observedPeerConnectionTransport = this.observedPeerConnectionTransports.get(stats.id);
2820
2826
  if (!observedPeerConnectionTransport) {
@@ -8044,6 +8050,7 @@ var RemoteTrackResolver = class {
8044
8050
  observer.off("outbound-track-updated", onOutboundUpdated);
8045
8051
  this._pendingInboundTracks.clear();
8046
8052
  this._pendingOutboundTracks.clear();
8053
+ this._outboundTrackPublisherIds.clear();
8047
8054
  });
8048
8055
  observer.on("inbound-track-added", onInboundAdded);
8049
8056
  observer.on("inbound-track-removed", onInboundRemoved);
@@ -8055,6 +8062,8 @@ var RemoteTrackResolver = class {
8055
8062
  observedCall;
8056
8063
  resolvers;
8057
8064
  _publisherIdToOutboundTrack = /* @__PURE__ */ new Map();
8065
+ // The publisher id each outbound track was indexed under, so removal does not depend on its current attachments.
8066
+ _outboundTrackPublisherIds = /* @__PURE__ */ new Map();
8058
8067
  _subscriberIdToInboundTrack = /* @__PURE__ */ new Map();
8059
8068
  /**
8060
8069
  * Tracks whose publisher id the strategy could not resolve **yet**.
@@ -8130,6 +8139,7 @@ var RemoteTrackResolver = class {
8130
8139
  }
8131
8140
  this._pendingOutboundTracks.delete(outboundTrack);
8132
8141
  this._publisherIdToOutboundTrack.set(publisherId, outboundTrack);
8142
+ this._outboundTrackPublisherIds.set(outboundTrack, publisherId);
8133
8143
  this.observedCall.unconsumedOutboundTracks.add(outboundTrack);
8134
8144
  for (const observedClient of this.observedCall.observedClients.values()) {
8135
8145
  for (const observedPeerConnection of observedClient.observedPeerConnections.values()) {
@@ -8144,7 +8154,8 @@ var RemoteTrackResolver = class {
8144
8154
  }
8145
8155
  _removeOutboundTrack(outboundTrack) {
8146
8156
  this._pendingOutboundTracks.delete(outboundTrack);
8147
- const publisherId = this.resolvers.resolveOutboundTrackPublisherId(outboundTrack);
8157
+ const publisherId = this._outboundTrackPublisherIds.get(outboundTrack);
8158
+ this._outboundTrackPublisherIds.delete(outboundTrack);
8148
8159
  if (publisherId && this._publisherIdToOutboundTrack.get(publisherId) === outboundTrack) {
8149
8160
  this._publisherIdToOutboundTrack.delete(publisherId);
8150
8161
  }