@observertc/observer-js 1.3.0-rc.3 → 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
@@ -178,11 +178,11 @@ var SinkRegistry = class {
178
178
  }
179
179
  /** End every sink of this registry and of its descendants. */
180
180
  clear() {
181
- for (const sink of [...this._sinks.values()]) {
181
+ for (const [id, sink] of [...this._sinks.entries()]) {
182
182
  try {
183
183
  sink.end();
184
184
  } catch (err) {
185
- logger.warn("Sink %s failed to end: %o", sink.id, err);
185
+ logger.warn("Sink %s failed to end: %o", id, err);
186
186
  }
187
187
  }
188
188
  }
@@ -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) {
@@ -3124,7 +3130,7 @@ var ObservedClient = class extends EventEmitter3 {
3124
3130
  /** The distinct `publicAddress`es currently known by this client's open peer connections. */
3125
3131
  /**
3126
3132
  * The sink this client's samples are written to: whatever is registered under its client id in
3127
- * `call.sinks` (the one `createClientSink` returned, or a sink the application added), if any.
3133
+ * `call.sinks` (the one created through `saveClientSamples`, or a sink the application added), if any.
3128
3134
  */
3129
3135
  get sink() {
3130
3136
  return this.call.sinks.get(this.clientId);
@@ -5277,17 +5283,26 @@ var ObservedCall = class extends EventEmitter4 {
5277
5283
  if (!client.leftAt) return;
5278
5284
  this.endedAt = Math.max(this.endedAt ?? client.leftAt, client.leftAt);
5279
5285
  }
5280
- /** Emit an Observer-bus event scoped to this call. */
5281
5286
  _createClientSink(clientId) {
5282
- const createClientSink = this.observer.config.createClientSink;
5283
- if (!createClientSink) return void 0;
5287
+ const config = this.observer.config.saveClientSamples;
5288
+ if (!config || !config.jsonlPath && !config.sink) return void 0;
5289
+ if (config.jsonlPath && config.sink) {
5290
+ logger6.warn("saveClientSamples: give either jsonlPath or sink, not both; no client samples are saved");
5291
+ return void 0;
5292
+ }
5284
5293
  if (this.observer.sinks.get(clientId)) {
5285
5294
  logger6.warn("No sample sink for client %s in call %s: sink id %s is already in use", clientId, this.callId, clientId);
5286
5295
  return void 0;
5287
5296
  }
5288
- const destination = createClientSink({ id: clientId, observedCall: this, clientId });
5289
- return destination ? this.sinks.createFromDestination(clientId, destination) : void 0;
5297
+ const params = { id: clientId, observedCall: this, clientId };
5298
+ if (config.jsonlPath) {
5299
+ const jsonlPath = config.jsonlPath(params);
5300
+ return jsonlPath === void 0 ? void 0 : this.sinks.createFromDestination(clientId, { jsonlPath });
5301
+ }
5302
+ const sink = config.sink?.(params);
5303
+ return sink ? this.sinks.createFromDestination(clientId, { sink }) : void 0;
5290
5304
  }
5305
+ /** Emit an Observer-bus event scoped to this call. */
5291
5306
  _notify(type, ...args) {
5292
5307
  this.observer.emit(type, ...args);
5293
5308
  }
@@ -8035,6 +8050,7 @@ var RemoteTrackResolver = class {
8035
8050
  observer.off("outbound-track-updated", onOutboundUpdated);
8036
8051
  this._pendingInboundTracks.clear();
8037
8052
  this._pendingOutboundTracks.clear();
8053
+ this._outboundTrackPublisherIds.clear();
8038
8054
  });
8039
8055
  observer.on("inbound-track-added", onInboundAdded);
8040
8056
  observer.on("inbound-track-removed", onInboundRemoved);
@@ -8046,6 +8062,8 @@ var RemoteTrackResolver = class {
8046
8062
  observedCall;
8047
8063
  resolvers;
8048
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();
8049
8067
  _subscriberIdToInboundTrack = /* @__PURE__ */ new Map();
8050
8068
  /**
8051
8069
  * Tracks whose publisher id the strategy could not resolve **yet**.
@@ -8121,6 +8139,7 @@ var RemoteTrackResolver = class {
8121
8139
  }
8122
8140
  this._pendingOutboundTracks.delete(outboundTrack);
8123
8141
  this._publisherIdToOutboundTrack.set(publisherId, outboundTrack);
8142
+ this._outboundTrackPublisherIds.set(outboundTrack, publisherId);
8124
8143
  this.observedCall.unconsumedOutboundTracks.add(outboundTrack);
8125
8144
  for (const observedClient of this.observedCall.observedClients.values()) {
8126
8145
  for (const observedPeerConnection of observedClient.observedPeerConnections.values()) {
@@ -8135,7 +8154,8 @@ var RemoteTrackResolver = class {
8135
8154
  }
8136
8155
  _removeOutboundTrack(outboundTrack) {
8137
8156
  this._pendingOutboundTracks.delete(outboundTrack);
8138
- const publisherId = this.resolvers.resolveOutboundTrackPublisherId(outboundTrack);
8157
+ const publisherId = this._outboundTrackPublisherIds.get(outboundTrack);
8158
+ this._outboundTrackPublisherIds.delete(outboundTrack);
8139
8159
  if (publisherId && this._publisherIdToOutboundTrack.get(publisherId) === outboundTrack) {
8140
8160
  this._publisherIdToOutboundTrack.delete(publisherId);
8141
8161
  }