@observertc/observer-js 1.0.1-f31d7a4.0 → 1.2.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 (174) hide show
  1. package/README.md +2195 -1
  2. package/dist/index.d.mts +7094 -0
  3. package/dist/index.d.ts +7094 -0
  4. package/dist/index.js +8166 -0
  5. package/dist/index.js.map +1 -0
  6. package/dist/index.mjs +8031 -0
  7. package/dist/index.mjs.map +1 -0
  8. package/package.json +44 -19
  9. package/.eslintrc.json +0 -143
  10. package/.prettierignore +0 -5
  11. package/.prettierrc +0 -7
  12. package/jest.config.js +0 -9
  13. package/lib/ObservedCall.d.ts +0 -62
  14. package/lib/ObservedCall.d.ts.map +0 -1
  15. package/lib/ObservedCall.js +0 -127
  16. package/lib/ObservedCertificate.d.ts +0 -19
  17. package/lib/ObservedCertificate.d.ts.map +0 -1
  18. package/lib/ObservedCertificate.js +0 -38
  19. package/lib/ObservedClient.d.ts +0 -104
  20. package/lib/ObservedClient.d.ts.map +0 -1
  21. package/lib/ObservedClient.js +0 -250
  22. package/lib/ObservedCodec.d.ts +0 -22
  23. package/lib/ObservedCodec.d.ts.map +0 -1
  24. package/lib/ObservedCodec.js +0 -45
  25. package/lib/ObservedDataChannel.d.ts +0 -28
  26. package/lib/ObservedDataChannel.d.ts.map +0 -1
  27. package/lib/ObservedDataChannel.js +0 -73
  28. package/lib/ObservedIceCandidate.d.ts +0 -29
  29. package/lib/ObservedIceCandidate.d.ts.map +0 -1
  30. package/lib/ObservedIceCandidate.js +0 -59
  31. package/lib/ObservedIceCandidatePair.d.ts +0 -44
  32. package/lib/ObservedIceCandidatePair.d.ts.map +0 -1
  33. package/lib/ObservedIceCandidatePair.js +0 -111
  34. package/lib/ObservedIceTransport.d.ts +0 -32
  35. package/lib/ObservedIceTransport.d.ts.map +0 -1
  36. package/lib/ObservedIceTransport.js +0 -65
  37. package/lib/ObservedInboundRtp.d.ts +0 -92
  38. package/lib/ObservedInboundRtp.d.ts.map +0 -1
  39. package/lib/ObservedInboundRtp.js +0 -193
  40. package/lib/ObservedInboundTrack.d.ts +0 -29
  41. package/lib/ObservedInboundTrack.d.ts.map +0 -1
  42. package/lib/ObservedInboundTrack.js +0 -57
  43. package/lib/ObservedMediaPlayout.d.ts +0 -22
  44. package/lib/ObservedMediaPlayout.d.ts.map +0 -1
  45. package/lib/ObservedMediaPlayout.js +0 -42
  46. package/lib/ObservedMediaSource.d.ts +0 -28
  47. package/lib/ObservedMediaSource.d.ts.map +0 -1
  48. package/lib/ObservedMediaSource.js +0 -55
  49. package/lib/ObservedOutboundRtp.d.ts +0 -62
  50. package/lib/ObservedOutboundRtp.d.ts.map +0 -1
  51. package/lib/ObservedOutboundRtp.js +0 -144
  52. package/lib/ObservedOutboundTrack.d.ts +0 -29
  53. package/lib/ObservedOutboundTrack.d.ts.map +0 -1
  54. package/lib/ObservedOutboundTrack.js +0 -57
  55. package/lib/ObservedPeerConnection.d.ts +0 -183
  56. package/lib/ObservedPeerConnection.d.ts.map +0 -1
  57. package/lib/ObservedPeerConnection.js +0 -688
  58. package/lib/ObservedPeerConnectionTransport.d.ts +0 -17
  59. package/lib/ObservedPeerConnectionTransport.d.ts.map +0 -1
  60. package/lib/ObservedPeerConnectionTransport.js +0 -34
  61. package/lib/ObservedRemoteInboundRtp.d.ts +0 -30
  62. package/lib/ObservedRemoteInboundRtp.d.ts.map +0 -1
  63. package/lib/ObservedRemoteInboundRtp.js +0 -60
  64. package/lib/ObservedRemoteOutboundRtp.d.ts +0 -30
  65. package/lib/ObservedRemoteOutboundRtp.d.ts.map +0 -1
  66. package/lib/ObservedRemoteOutboundRtp.js +0 -60
  67. package/lib/ObservedTURN.d.ts +0 -30
  68. package/lib/ObservedTURN.d.ts.map +0 -1
  69. package/lib/ObservedTURN.js +0 -54
  70. package/lib/ObservedTurnServer.d.ts +0 -25
  71. package/lib/ObservedTurnServer.d.ts.map +0 -1
  72. package/lib/ObservedTurnServer.js +0 -59
  73. package/lib/Observer.d.ts +0 -50
  74. package/lib/Observer.d.ts.map +0 -1
  75. package/lib/Observer.js +0 -101
  76. package/lib/common/Middleware.d.ts +0 -17
  77. package/lib/common/Middleware.d.ts.map +0 -1
  78. package/lib/common/Middleware.js +0 -60
  79. package/lib/common/SingleExecutor.d.ts +0 -3
  80. package/lib/common/SingleExecutor.d.ts.map +0 -1
  81. package/lib/common/SingleExecutor.js +0 -31
  82. package/lib/common/logger.d.ts +0 -17
  83. package/lib/common/logger.d.ts.map +0 -1
  84. package/lib/common/logger.js +0 -50
  85. package/lib/common/types.d.ts +0 -3
  86. package/lib/common/types.d.ts.map +0 -1
  87. package/lib/common/types.js +0 -3
  88. package/lib/common/utils.d.ts +0 -10
  89. package/lib/common/utils.d.ts.map +0 -1
  90. package/lib/common/utils.js +0 -55
  91. package/lib/detectors/Detector.d.ts +0 -5
  92. package/lib/detectors/Detector.d.ts.map +0 -1
  93. package/lib/detectors/Detector.js +0 -3
  94. package/lib/detectors/Detectors.d.ts +0 -11
  95. package/lib/detectors/Detectors.d.ts.map +0 -1
  96. package/lib/detectors/Detectors.js +0 -34
  97. package/lib/index.d.ts +0 -26
  98. package/lib/index.d.ts.map +0 -1
  99. package/lib/index.js +0 -41
  100. package/lib/monitors/TurnUsageMonitor.d.ts +0 -1
  101. package/lib/monitors/TurnUsageMonitor.d.ts.map +0 -1
  102. package/lib/monitors/TurnUsageMonitor.js +0 -146
  103. package/lib/schema/ClientEventTypes.d.ts +0 -33
  104. package/lib/schema/ClientEventTypes.d.ts.map +0 -1
  105. package/lib/schema/ClientEventTypes.js +0 -38
  106. package/lib/schema/ClientMetaTypes.d.ts +0 -34
  107. package/lib/schema/ClientMetaTypes.d.ts.map +0 -1
  108. package/lib/schema/ClientMetaTypes.js +0 -16
  109. package/lib/schema/ClientSample.d.ts +0 -1333
  110. package/lib/schema/ClientSample.d.ts.map +0 -1
  111. package/lib/schema/ClientSample.js +0 -4
  112. package/lib/scores/CalculatedScore.d.ts +0 -6
  113. package/lib/scores/CalculatedScore.d.ts.map +0 -1
  114. package/lib/scores/CalculatedScore.js +0 -2
  115. package/lib/scores/DefaultCallScoreCalculator.d.ts +0 -7
  116. package/lib/scores/DefaultCallScoreCalculator.d.ts.map +0 -1
  117. package/lib/scores/DefaultCallScoreCalculator.js +0 -21
  118. package/lib/scores/ScoreCalculator.d.ts +0 -4
  119. package/lib/scores/ScoreCalculator.d.ts.map +0 -1
  120. package/lib/scores/ScoreCalculator.js +0 -2
  121. package/lib/utils/CallUpdater.d.ts +0 -5
  122. package/lib/utils/CallUpdater.d.ts.map +0 -1
  123. package/lib/utils/CallUpdater.js +0 -2
  124. package/lib/utils/OnAllClientCallUpdater.d.ts +0 -16
  125. package/lib/utils/OnAllClientCallUpdater.d.ts.map +0 -1
  126. package/lib/utils/OnAllClientCallUpdater.js +0 -58
  127. package/lib/utils/RemoteTrackResolver.d.ts +0 -7
  128. package/lib/utils/RemoteTrackResolver.d.ts.map +0 -1
  129. package/lib/utils/RemoteTrackResolver.js +0 -2
  130. package/lib/utils/onAnyClientCallUpdater.d.ts +0 -14
  131. package/lib/utils/onAnyClientCallUpdater.d.ts.map +0 -1
  132. package/lib/utils/onAnyClientCallUpdater.js +0 -51
  133. package/src/ObservedCall.ts +0 -176
  134. package/src/ObservedCertificate.ts +0 -43
  135. package/src/ObservedClient.ts +0 -324
  136. package/src/ObservedCodec.ts +0 -49
  137. package/src/ObservedDataChannel.ts +0 -82
  138. package/src/ObservedIceCandidate.ts +0 -64
  139. package/src/ObservedIceCandidatePair.ts +0 -118
  140. package/src/ObservedIceTransport.ts +0 -70
  141. package/src/ObservedInboundRtp.ts +0 -205
  142. package/src/ObservedInboundTrack.ts +0 -70
  143. package/src/ObservedMediaPlayout.ts +0 -49
  144. package/src/ObservedMediaSource.ts +0 -60
  145. package/src/ObservedOutboundRtp.ts +0 -156
  146. package/src/ObservedOutboundTrack.ts +0 -69
  147. package/src/ObservedPeerConnection.ts +0 -995
  148. package/src/ObservedPeerConnectionTransport.ts +0 -38
  149. package/src/ObservedRemoteInboundRtp.ts +0 -64
  150. package/src/ObservedRemoteOutboundRtp.ts +0 -65
  151. package/src/ObservedTURN.ts +0 -80
  152. package/src/ObservedTurnServer.ts +0 -72
  153. package/src/Observer.ts +0 -152
  154. package/src/common/Middleware.ts +0 -84
  155. package/src/common/SingleExecutor.ts +0 -36
  156. package/src/common/logger.ts +0 -82
  157. package/src/common/types.ts +0 -4
  158. package/src/common/utils.ts +0 -59
  159. package/src/detectors/Detector.ts +0 -6
  160. package/src/detectors/Detectors.ts +0 -38
  161. package/src/index.ts +0 -27
  162. package/src/monitors/TurnUsageMonitor.ts +0 -187
  163. package/src/schema/ClientEventTypes.ts +0 -36
  164. package/src/schema/ClientMetaTypes.ts +0 -41
  165. package/src/schema/ClientSample.ts +0 -1682
  166. package/src/scores/CalculatedScore.ts +0 -6
  167. package/src/scores/DefaultCallScoreCalculator.ts +0 -23
  168. package/src/scores/ScoreCalculator.ts +0 -3
  169. package/src/utils/CallUpdater.ts +0 -5
  170. package/src/utils/OnAllClientCallUpdater.ts +0 -68
  171. package/src/utils/RemoteTrackResolver.ts +0 -12
  172. package/src/utils/onAnyClientCallUpdater.ts +0 -59
  173. package/tsconfig.json +0 -19
  174. package/tslint.json +0 -6
package/README.md CHANGED
@@ -1,2 +1,2196 @@
1
- Server side component for monitoring WebRTC applications and services
1
+ # ObserveRTC — `@observertc/observer-js`
2
+
3
+ [![NPM version](https://img.shields.io/npm/v/@observertc/observer-js.svg)](https://www.npmjs.com/package/@observertc/observer-js)
4
+ [![License](https://img.shields.io/npm/l/@observertc/observer-js.svg)](https://github.com/observertc/observer-js/blob/main/LICENSE)
5
+
6
+ > **In one line:** feed it WebRTC `getStats()` snapshots, and get back a live, queryable model of
7
+ > every call plus a single typed event stream to react to.
8
+
9
+ `observer-js` is a **server-side Node.js library for monitoring WebRTC sessions**. A WebRTC
10
+ application (typically an SFU or a signaling/stats backend) feeds it `ClientSample` objects —
11
+ periodic snapshots of each participant's `RTCPeerConnection.getStats()` output plus
12
+ application events — and `observer-js` maintains a live, in-memory model of every call,
13
+ participant, peer connection, and media stream, derives per-interval and cumulative metrics,
14
+ and emits a single, unified stream of typed events the application can react to.
15
+
16
+ **What you can do with it:**
17
+
18
+ - **Monitor calls live** — a queryable in-memory tree of every call, client, peer connection,
19
+ track, codec, ICE candidate and data channel, each holding current **and** cumulative metrics.
20
+ - **React on one event bus** — subscribe once on the `Observer`; every payload carries its full
21
+ ancestry (`call → client → peer connection → stat`), so you never walk the tree to subscribe.
22
+ - **Get derived metrics for free** — counter-reset-safe per-tick deltas, bitrates, jitter, RTT,
23
+ fraction-lost, remote-RTP (RTCP) correlation, and TURN/TCP usage from the selected candidate pair.
24
+ - **Correlate across an SFU** — link a publisher's outbound track to every subscriber's inbound
25
+ track (`RemoteTrackResolver`), and observe mediasoup routers/transports/producers/consumers
26
+ on the server side.
27
+ - **Detect server-only problems** — cross-client `Detector`s raise `call-issue`s for conditions no
28
+ single client can see (e.g. everyone in a call degrading at once).
29
+ - **Persist every sample** — per-client sinks (JSONL file, in-memory, or your own) for archival,
30
+ streaming, and offline replay.
31
+ - **Drop it in safely** — warn-don't-throw, a pluggable logger, dual **ESM + CommonJS**, and **no**
32
+ media-stack dependency in the core.
33
+
34
+ > **Status:** `1.1.0` — the 1.x stable line. The API described here is current and intended to
35
+ > be implemented against directly; it follows semver, so a breaking change means a 2.0.0.
36
+ > [`CHANGELOG.md`](./CHANGELOG.md) is the full statement of what the 1.x line contains. This document is written to be self-sufficient: an engineer (or an AI
37
+ > agent) should be able to integrate the library, or develop it further, from this file alone.
38
+ > A companion doc, [`docs/logging.md`](./docs/logging.md), covers logging integration in depth.
39
+
40
+ > **Packaging:** server-side, **Node.js ≥ 22**, shipped as a **dual ESM + CommonJS** build — so it
41
+ > works whether your project uses `import` (ESM) or `require()` (CommonJS). Everything — including
42
+ > the built-in file sink — is exported from the single `@observertc/observer-js` entry.
43
+
44
+ > **For AI agents:** [`llms.txt`](./llms.txt) is a curated map of these docs (it belongs at the root
45
+ > of the docs site); [`AGENTS.md`](./AGENTS.md) covers build/test commands and the conventions for
46
+ > working **in** this repository.
47
+
2
48
  ---
49
+
50
+ ## Table of contents
51
+
52
+ 1. [Installation](#installation)
53
+ 2. [Quick start](#quick-start)
54
+ 3. [Data flow](#data-flow)
55
+ 4. [Entity hierarchy](#entity-hierarchy)
56
+ 5. [Ingestion: `accept()`, context & lifecycle](#ingestion-accept-context--lifecycle)
57
+ 6. [When things update](#when-things-update)
58
+ 7. [The event bus](#the-event-bus) ← the core of the API
59
+ 8. [API reference](#api-reference)
60
+ 9. [Schema types (`ClientSample`)](#schema-types-clientsample)
61
+ 10. [Detectors (server-side extension point)](#detectors-server-side-extension-point)
62
+ 11. [Call summaries](#call-summaries)
63
+ 12. [Remote track resolution (mediasoup / SFU)](#remote-track-resolution-mediasoup--sfu)
64
+ 13. [Mediasoup router observation](#mediasoup-router-observation)
65
+ 14. [Sinks (per-client sample persistence)](#sinks-per-client-sample-persistence)
66
+ 15. [Injecting data into a client](#injecting-data-into-a-client)
67
+ 16. [Logging](#logging)
68
+ 17. [Design notes](#design-notes)
69
+ 18. [Error-handling philosophy](#error-handling-philosophy)
70
+ 19. [Development & extension guide](#development--extension-guide)
71
+
72
+ ---
73
+
74
+ ## Installation
75
+
76
+ ```bash
77
+ npm install @observertc/observer-js
78
+ # or
79
+ yarn add @observertc/observer-js
80
+ ```
81
+
82
+ **Server-side, Node.js ≥ 22, dual ESM + CommonJS.** The package ships both module formats, so it
83
+ works the same whether your project is ESM or CommonJS — your import line is unchanged either way:
84
+
85
+ ```ts
86
+ import { Observer, ClientSample, createJsonlFileSinkFactory } from '@observertc/observer-js';
87
+ ```
88
+
89
+ In an ESM project this resolves to the `.mjs` build; in a CommonJS project (where TypeScript
90
+ compiles your `import` down to `require()`) it resolves to the `.js` build. Everything is exported
91
+ from the single `@observertc/observer-js` entry. Written in TypeScript; ships type declarations for
92
+ both formats (`dist/index.d.ts` for `require`, `dist/index.d.mts` for `import`). Runtime
93
+ dependencies: `@bufbuild/protobuf`, `events`, `uuid`. The library does **not** bundle a logger or
94
+ any transport — see [Logging](#logging).
95
+
96
+ `ClientSample` and friends are re-exported from this package, and are also published as the
97
+ shared schema in [`@observertc/schemas`](https://github.com/observertc/schemas); samples
98
+ produced on the client (e.g. by `@observertc/client-monitor-js`) conform to the same shape.
99
+
100
+ ---
101
+
102
+ ## Quick start
103
+
104
+ ```ts
105
+ import { Observer, ClientSample } from '@observertc/observer-js';
106
+
107
+ // 1. Create an observer.
108
+ const observer = new Observer({
109
+ // a call updates when any of its clients does, and the observer when any of its calls does —
110
+ // both default to true, so this line is only here to show the knob exists:
111
+ autoUpdateOnCallUpdate: true,
112
+ // optional auto-teardown:
113
+ closeCallIfEmptyForMs: 20_000,
114
+ closeClientIfIdleForMs: 60_000,
115
+ });
116
+
117
+ // 2. Subscribe on the single bus. Every payload is an object with the ancestry.
118
+ observer.on('call-added', ({ observedCall }) => {
119
+ console.log('new call', observedCall.callId);
120
+ });
121
+
122
+ observer.on('client-issue', ({ observedClient, issue }) => {
123
+ console.warn(`[${observedClient.clientId}] ${issue.type}`, issue.payload);
124
+ });
125
+
126
+ observer.on('peer-connection-updated', ({ observedClient, observedPeerConnection }) => {
127
+ console.log(observedClient.clientId, 'RTT(ms):', observedPeerConnection.currentRttInMs);
128
+ });
129
+
130
+ observer.on('sample-rejected', ({ reason, sample }) => {
131
+ console.warn('dropped a sample:', reason);
132
+ });
133
+
134
+ // 3. Feed samples. `context` (optional) is transient per-accept data, carried to the
135
+ // `*-updated` events this accept triggers (never written to appData).
136
+ function onClientStats(sample: ClientSample) {
137
+ observer.accept(sample, { studioVersion: '1.2.3' });
138
+ }
139
+
140
+ // 4. Tear down.
141
+ process.on('SIGINT', () => observer.close());
142
+ ```
143
+
144
+ ---
145
+
146
+ ## Data flow
147
+
148
+ ```
149
+ client getStats() ──► ClientSample ──► observer.accept(sample, ctx?)
150
+ │
151
+ ┌────────────────────────────────┘
152
+ ▼
153
+ get-or-create ObservedCall ──► get-or-create ObservedClient ──► client.accept(sample, ctx)
154
+ │
155
+ per peerConnections[] in the sample
156
+ ▼
157
+ get-or-create ObservedPeerConnection
158
+ .accept(pcSample, ctx) updates all sub-stats,
159
+ derives deltas/bitrates/RTT, correlates remote RTP
160
+ │
161
+ metrics roll up: PeerConnection → Client → Call → Observer
162
+ │
163
+ events emitted on the Observer bus ──► your handlers
164
+ ```
165
+
166
+ - A sample **must** have `callId` and `clientId` (the library sets them, or the app does). If
167
+ either is missing, the sample is dropped and `sample-rejected` is emitted.
168
+ - Sub-entities that stop appearing in samples are garbage-collected via a "visited"
169
+ mark-and-sweep on each `ObservedPeerConnection.accept()`, emitting the corresponding
170
+ `*-removed` events.
171
+
172
+ ---
173
+
174
+ ## Entity hierarchy
175
+
176
+ | Class | Created by | Keyed on its parent as | Holds |
177
+ |-------|-----------|------------------------|-------|
178
+ | `Observer` | `new Observer(config?)` | — (root) | `observedCalls: Map<string, ObservedCall>`, global counters, the event bus |
179
+ | `ObservedCall` | `observer.createObservedCall(settings)` / lazily by `accept` | `observedCalls` | `observedClients: Map<string, ObservedClient>`, call-wide metrics, `detectors`, `scoreCalculator` |
180
+ | `ObservedClient` | `call.createObservedClient(settings)` / lazily | `observedClients` | `observedPeerConnections: Map<string, ObservedPeerConnection>`, per-client metrics |
181
+ | `ObservedPeerConnection` | lazily, from `sample.peerConnections[]` | `observedPeerConnections` | the 15 sub-stat maps below, transport/RTT/bitrate metrics |
182
+ | Sub-stats | lazily, from the `PeerConnectionSample` | maps on the PC | individual WebRTC stat objects |
183
+
184
+ `ObservedPeerConnection` sub-stat maps (all `public readonly`):
185
+
186
+ ```
187
+ observedCertificates, observedCodecs, observedDataChannels,
188
+ observedIceCandidates, observedIceCandidatesPair, observedIceTransports,
189
+ observedInboundRtps, observedInboundTracks, observedMediaPlayouts,
190
+ observedMediaSources, observedOutboundRtps, observedOutboundTracks,
191
+ observedPeerConnectionTransports, observedRemoteInboundRtps, observedRemoteOutboundRtps
192
+ ```
193
+
194
+ Each sub-stat class (`ObservedInboundRtp`, `ObservedOutboundRtp`, `ObservedInboundTrack`,
195
+ `ObservedOutboundTrack`, `ObservedDataChannel`, `ObservedIceCandidate`,
196
+ `ObservedIceCandidatePair`, `ObservedIceTransport`, `ObservedCertificate`, `ObservedCodec`,
197
+ `ObservedMediaSource`, `ObservedMediaPlayout`, `ObservedPeerConnectionTransport`,
198
+ `ObservedRemoteInboundRtp`, `ObservedRemoteOutboundRtp`) mirrors the corresponding stat
199
+ fields from the schema plus derived fields (deltas, bitrates).
200
+
201
+ ---
202
+
203
+ ## Ingestion: `accept()`, context & lifecycle
204
+
205
+ ### `observer.accept(sample, context?)`
206
+
207
+ The single entry point. It:
208
+
209
+ 1. drops + emits `sample-rejected` if the observer is closed;
210
+ 2. runs the sample through the **global accept-middleware chain** (see below);
211
+ 3. (chain terminal) drops + emits `sample-rejected` if `callId`/`clientId` is missing;
212
+ 4. gets or lazily creates the `ObservedCall` and `ObservedClient` (their `appData` comes from the
213
+ configured factories, never from `context`);
214
+ 5. delegates to `client.accept(sample, context)`, which fans out to each
215
+ `ObservedPeerConnection.accept(pcSample, context)`.
216
+
217
+ ### Accept middlewares (global pre-dispatch hook)
218
+
219
+ `observer.addAcceptMiddleware(...)` registers middlewares run on **every** sample inside
220
+ `accept()`, in order, **before** the sample is dispatched to any call or client. Each middleware
221
+ gets a `{ sample, context }` payload; it can inspect or mutate the sample (set/normalize
222
+ `callId`/`clientId`, enrich, redact) or the context, then call `next(payload)` to continue.
223
+ **Not calling `next` drops the sample** — nothing is created and no event fires. A throwing
224
+ middleware is caught and warns (the sample is dropped), never crashing `accept()`.
225
+
226
+ ```ts
227
+ import { Observer, AcceptMiddleware } from '@observertc/observer-js';
228
+
229
+ const observer = new Observer();
230
+
231
+ // derive callId/clientId from the app's own attachment, before dispatch
232
+ const route: AcceptMiddleware = ({ sample }, next) => {
233
+ sample.callId ??= sample.attachments?.roomId as string;
234
+ sample.clientId ??= sample.attachments?.peerId as string;
235
+ next({ sample });
236
+ };
237
+
238
+ // drop samples from a blocklisted client (never dispatched)
239
+ const filter: AcceptMiddleware = (payload, next) => {
240
+ if (blocked.has(payload.sample.clientId)) return; // no next() => dropped
241
+ next(payload);
242
+ };
243
+
244
+ observer.addAcceptMiddleware(route, filter);
245
+ // observer.removeAcceptMiddleware(route);
246
+ ```
247
+
248
+ This is a lightweight global injection point. When no middleware is registered, `accept()`
249
+ dispatches directly with no overhead.
250
+
251
+ ### `context` (the `AcceptContext`)
252
+
253
+ ```ts
254
+ type AcceptContext = Record<string, unknown>;
255
+ ```
256
+
257
+ A single, optional, free-form object threaded down the whole accept chain
258
+ (`Observer → Client → PeerConnection`). It is **transient request-scoped data** — temporary or
259
+ contextual information the application wants available while an update is processed.
260
+
261
+ `context` is **never written to `appData`** and is **not stored** on any entity. The two are
262
+ deliberately distinct:
263
+
264
+ - **`appData`** — application-assigned extra info that identifies/decorates an entity, fixed at
265
+ creation (via `settings.appData` or the `createCallAppData` / `createClientAppData` factories),
266
+ or assigned by the app on the `*-added` events. The library never changes it. The factories
267
+ **receive the context of the `accept()` that triggered the creation**, so a fact carried on the
268
+ context can be baked into `appData` at birth — but it is copied by the factory, deliberately, not
269
+ written across by the library.
270
+ - **`context`** — passed per `accept()`, may differ on every call, and is carried straight
271
+ through to the `*-updated` events that the `accept()` triggers, then discarded.
272
+
273
+ `client-updated` and `peer-connection-updated` carry the exact context of that sample;
274
+ `call-updated` carries the context of the client `accept()` that drove the call update (absent
275
+ for interval- or teardown-driven call updates). When no context is given, the field is absent.
276
+
277
+ ### Get-or-create helpers
278
+
279
+ If you want to create/configure entities yourself before/without samples:
280
+
281
+ ```ts
282
+ const call = observer.getOrCreateObservedCall({ callId, appData }); // ObservedCall | undefined
283
+ const client = call?.getOrCreateObservedClient({ clientId, appData }); // ObservedClient | undefined
284
+ ```
285
+
286
+ These return `undefined` (and warn) when the parent is closed; `createObservedCall`/
287
+ `createObservedClient` return the **existing** instance (and warn) if the id already exists.
288
+
289
+ ### Automatic teardown
290
+
291
+ - `closeClientIfIdleForMs` — a client with no sample for this long auto-closes.
292
+ - `closeCallIfEmptyForMs` — a call with zero clients for this long auto-closes.
293
+ - Closing cascades down (call → clients → peer connections → sub-stats), unsubscribing
294
+ listeners and emitting the `*-closed` / `*-removed` events.
295
+
296
+ ---
297
+
298
+ ## When things update
299
+
300
+ "Update" means *recompute aggregated metrics, run the detectors, and emit the `*-updated` event* at
301
+ that level. Updates are **event-driven** — there is no built-in timer.
302
+
303
+ The rule is structural rather than configurable:
304
+
305
+ > **A call is updated when any of its clients is updated. The observer is updated when any of its
306
+ > calls is updated.** Composed, that means the observer is updated exactly when any client anywhere
307
+ > is updated.
308
+
309
+ Two booleans, both defaulting to `true`, let you opt out of a link in that chain:
310
+
311
+ | Setting | Where | Effect when `false` |
312
+ |---------|-------|---------------------|
313
+ | `autoUpdateOnClientUpdate` | `ObservedCallSettings` | the call updates only when you call `call.update()` |
314
+ | `autoUpdateOnCallUpdate` | `ObserverConfig` | the observer updates only when you call `observer.update()` |
315
+
316
+ An app that wants a fixed cadence sets both to `false` and drives `observer.update()` from its own
317
+ `setInterval`. Note that **observer-scoped detectors and validators run nowhere else** — if the
318
+ observer never updates, they never run.
319
+
320
+ ```ts
321
+ const observer = new Observer({ autoUpdateOnCallUpdate: false });
322
+
323
+ setInterval(() => observer.update(), 5_000);
324
+ ```
325
+
326
+ > Earlier versions had an `updatePolicy` / `defaultCallUpdatePolicy` enum (`'update-on-any-…'`,
327
+ > `'update-when-all-…'`, `'none'`) and a pluggable `Updater` object. Both are gone. "When all clients
328
+ > have updated" sounds appealing and deadlocks on the first client that stops sending — one silent
329
+ > participant froze the whole call's aggregation until it timed out.
330
+
331
+ ---
332
+
333
+ ## The event bus
334
+
335
+ This is the primary API. **Subscribe on the `Observer` instance** — it is the single emitter
336
+ for the entire hierarchy. The `ObservedCall` / `ObservedClient` / `ObservedPeerConnection`
337
+ objects are themselves `EventEmitter`s too, but those local events are reserved for internal
338
+ lifecycle/teardown wiring (see [Local lifecycle events](#local-lifecycle-events)); application
339
+ code should use the Observer bus.
340
+
341
+ ### Payload shape: ancestry + subject
342
+
343
+ Every Observer event delivers exactly **one argument: a payload object**. The payload always
344
+ contains the ancestry from the observer down to the entity that raised it, plus any event-
345
+ specific subject:
346
+
347
+ ```ts
348
+ type ObserverEventBase = { observer: Observer, context?: AcceptContext };
349
+ type ObservedCallScope = ObserverEventBase & { observedCall: ObservedCall };
350
+ type ObservedClientScope = ObservedCallScope & { observedClient: ObservedClient };
351
+ type ObservedPeerConnectionScope = ObservedClientScope & { observedPeerConnection: ObservedPeerConnection };
352
+ ```
353
+
354
+ So a peer-connection-level event hands you the observer, call, client, **and** peer connection:
355
+
356
+ ```ts
357
+ observer.on('inbound-rtp-added', ({ observer, observedCall, observedClient, observedPeerConnection, observedInboundRtp }) => {
358
+ // all five are present and correctly typed
359
+ });
360
+ ```
361
+
362
+ `observer.on/off/once/emit` are fully typed against the event map — the handler argument is
363
+ inferred per event name.
364
+
365
+ ### Event catalogue
366
+
367
+ All payloads include the ancestry for their level (above). The **Extra** column lists the
368
+ additional field(s) on top of that scope.
369
+
370
+ #### Observer level — scope `{ observer }`
371
+
372
+ | Event | Extra payload | Fires when |
373
+ |-------|---------------|-----------|
374
+ | `observer-updated` | — | `observer.update()` ran (see [When things update](#when-things-update)) |
375
+ | `observer-closed` | — | `observer.close()` |
376
+ | `sample-rejected` | `{ reason: 'observer-closed' \| 'missing-callId' \| 'missing-clientId', sample: ClientSample }` | a sample was dropped by `accept()` |
377
+ | `observer-issue` | `{ issue: ObserverIssue }` | `observer.addIssue(...)` — a cross-call / SFU-wide finding (see [observer-level detectors](#observer-level-detectors-cross-call--sfu-wide)) |
378
+ | `validation-ready` | `{ validator: string, report: ValidationReport }` | a [validator](#validators--one-shot-structural-checks) settled — fires once per check, not per tick |
379
+
380
+ #### Mediasoup level — scope `{ observer, observedMediasoupRouter }`
381
+
382
+ | Event | Extra | Fires when |
383
+ |-------|-------|-----------|
384
+ | `mediasoup-router-added` | — | `observer.createObservedMediasoupRouter(...)` registered a router |
385
+ | `mediasoup-router-matched-with-peer-connection` | `{ observedCall, observedClient, observedPeerConnection }` | a newly added peer connection's id matched one of the router's WebRTC transport ids. **Opt-in** via `matchPeerConnectionByWebRtcTransportId: true`. |
386
+ | `mediasoup-router-removed` | — | the underlying mediasoup router closed (its `router.observer` `close` fired) |
387
+
388
+ See [Mediasoup router observation](#mediasoup-router-observation) for the full design and examples.
389
+
390
+ #### Call level — scope `{ observer, observedCall }`
391
+
392
+ | Event | Extra | Fires when |
393
+ |-------|-------|-----------|
394
+ | `call-added` | — | a call is created |
395
+ | `call-updated` | `{ context?: AcceptContext }` | `call.update()` ran |
396
+ | `call-closed` | — | the call closed |
397
+ | `call-empty` | — | last client left the call |
398
+ | `call-not-empty` | — | first client joined a previously-empty call |
399
+ | `call-issue` | `{ issue: CallIssue }` | `call.addIssue(...)` (server-side detector finding) |
400
+ | `call-summary` | `{ summary: CallSummary }` | the call is closing and a [summary](#call-summaries) was configured. Emitted **inside** `close()`, while the call is still reachable |
401
+
402
+ #### Client level — scope `{ observer, observedCall, observedClient }`
403
+
404
+ | Event | Extra | Fires when |
405
+ |-------|-------|-----------|
406
+ | `client-added` | — | a client is created |
407
+ | `client-sink-created` | `{ sink: ClientSampleSink }` | a per-client sink was created (only when `createClientSink` returns one); fires right after `client-added` |
408
+ | `client-updated` | `{ sample: ClientSample, elapsedTimeInMs: number, context?: AcceptContext }` | the client processed a sample |
409
+ | `client-closed` | — | the client closed |
410
+ | `client-joined` | — | first `CLIENT_JOINED` event seen |
411
+ | `client-left` | — | `CLIENT_LEFT` seen (or inferred on close) |
412
+ | `client-rejoined` | `{ timestamp: number }` | a later `CLIENT_JOINED` after an earlier join |
413
+ | `client-issue` | `{ issue: ClientIssue }` | a client-reported issue arrived, or `client.addIssue(...)`. A keyed issue also opens an entry in `observedClient.activeIssues` |
414
+ | `client-issue-resolved` | `{ resolvedIssue: ResolvedActiveClientIssue }` | a stateful issue ended — the client sent its `<type>-resolved` companion, or the observer force-closed it. Carries the finished interval (`durationInMs`, `resolvedBy`) — see [client issues](#client-issues-the-lifecycle-and-the-division-of-labour) |
415
+ | `client-metadata` | `{ metaData: ClientMetaData }` | a client meta item arrived |
416
+ | `client-extension-stats` | `{ extensionStats: ExtensionStat }` | an app-defined extension stat arrived |
417
+ | `client-event` | `{ event: ClientEvent }` | any client event was processed (once per event, also when it had to wait for its peer connection / track in the same sample) |
418
+ | `client-public-address-changed` | `{ publicAddress: string, previousPublicAddress?: string }` | the client's public (internet-facing) IP was learned or changed — see [Public address](#public-address-of-a-client) |
419
+
420
+ #### Peer-connection level — scope `{ observer, observedCall, observedClient, observedPeerConnection }`
421
+
422
+ | Event | Extra | Notes |
423
+ |-------|-------|-------|
424
+ | `peer-connection-added` / `peer-connection-closed` | — | lifecycle of the PC. `-closed` fires when the client reports `PEER_CONNECTION_CLOSED` (after that sample is applied; later stats for that id are ignored) or when the client closes |
425
+ | `peer-connection-updated` | `{ context?: AcceptContext }` | the PC processed a sample |
426
+ | `ice-connection-state-changed` / `ice-gathering-state-changed` / `connection-state-changed` | `{ state: string }` | driven by client events |
427
+ | `selected-candidate-pair-changed` | `{ observedIceTransport, observedIceCandidatePair?, previousCandidatePairId? }` | an ICE transport's `selectedCandidatePairId` changed: first selection, switch, or deselection (`observedIceCandidatePair` undefined). Fires after the whole PC sample is applied. Needs `transport` stats with `selectedCandidatePairId` (not reported by Firefox) |
428
+ | `peer-connection-public-address-changed` | `{ publicAddress: string, previousPublicAddress?: string }` | the PC's public IP was learned or changed — see [Public address](#public-address-of-a-client) |
429
+ | `inbound-track-added` / `-updated` / `-removed` / `-muted` / `-unmuted` | `{ observedInboundTrack }` | |
430
+ | `outbound-track-added` / `-updated` / `-removed` / `-muted` / `-unmuted` | `{ observedOutboundTrack }` | |
431
+ | `inbound-rtp-added` / `-updated` / `-removed` | `{ observedInboundRtp }` | `-updated` fires every tick |
432
+ | `outbound-rtp-added` / `-updated` / `-removed` | `{ observedOutboundRtp }` | `-updated` fires every tick |
433
+ | `remote-inbound-rtp-added` / `-updated` / `-removed` | `{ observedRemoteInboundRtp }` | |
434
+ | `remote-outbound-rtp-added` / `-updated` / `-removed` | `{ observedRemoteOutboundRtp }` | |
435
+ | `data-channel-added` / `-updated` / `-removed` | `{ observedDataChannel }` | |
436
+ | `ice-candidate-added` / `-updated` / `-removed` | `{ observedIceCandidate }` | |
437
+ | `ice-candidate-pair-added` / `-updated` / `-removed` | `{ observedIceCandidatePair }` | |
438
+ | `ice-transport-added` / `-updated` / `-removed` | `{ observedIceTransport }` | |
439
+ | `codec-added` / `-updated` / `-removed` | `{ observedCodec }` | |
440
+ | `media-source-added` / `-updated` / `-removed` | `{ observedMediaSource }` | |
441
+ | `media-playout-added` / `-updated` / `-removed` | `{ observedMediaPlayout }` | |
442
+ | `peer-connection-transport-added` / `-updated` / `-removed` | `{ observedPeerConnectionTransport }` | |
443
+ | `certificate-added` / `-updated` / `-removed` | `{ observedCertificate }` | `-updated` fires every tick the certificate is reported (the first included) |
444
+
445
+ > **Volume note.** The `*-updated` sub-stat events fire on every peer-connection `accept()`
446
+ > (i.e. per sample, per stream). For high-throughput servers, subscribe only to what you need,
447
+ > or read fields off the entities on `client-updated` / `call-updated` instead.
448
+
449
+ ### Local lifecycle events
450
+
451
+ These remain on the individual entities (not the bus), for teardown/coordination. You can
452
+ listen to them, but prefer the bus equivalents above for application logic.
453
+
454
+ | Entity | Local events |
455
+ |--------|--------------|
456
+ | `ObservedCall` | `update`, `newclient`, `empty`, `not-empty`, `close` |
457
+ | `ObservedClient` | `update` (`sample`, `elapsedTimeInMs`), `close`, `joined`, `left` |
458
+ | `ObservedPeerConnection` | `removed-inbound-track`, `removed-outbound-track`, `close` |
459
+
460
+ ---
461
+
462
+ ## API reference
463
+
464
+ ### `Observer`
465
+
466
+ ```ts
467
+ new Observer<AppData>(config?: ObserverConfig<AppData>)
468
+
469
+ type ObserverConfig<AppData = Record<string, unknown>> = {
470
+ // a call updates when any client does; the observer when any call does. Default true.
471
+ autoUpdateOnCallUpdate?: boolean;
472
+ appData?: AppData;
473
+ closeClientIfIdleForMs?: number;
474
+ closeCallIfEmptyForMs?: number;
475
+ // accumulate a per-call summary (see Call summaries). Absent or null = off, and nothing
476
+ // subscribes to anything. `{}` is valid: a summary with no built-in sections.
477
+ callSummary?: Partial<CallSummaryConfig> | null;
478
+ // appData factories — run when an entity is created without explicit appData
479
+ // (incl. lazily by accept()). appData is application-owned; accept `context` never touches it.
480
+ createCallAppData?: (p: { callId: string; observer: Observer; acceptCtx?: AcceptContext }) => Record<string, unknown>;
481
+ createClientAppData?: (p: { clientId: string; observedCall: ObservedCall; acceptCtx?: AcceptContext }) => Record<string, unknown>;
482
+ // sink factory — produces a per-client sink that receives every accepted sample (see Sinks).
483
+ createClientSink?: (p: { clientId: string; observedCall: ObservedCall }) => ClientSampleSink | undefined;
484
+ // remote-track-resolver factory — produces a call's RemoteTrackResolver (see Remote track resolution).
485
+ createRemoteTrackResolver?: (observedCall: ObservedCall) => RemoteTrackResolver | undefined;
486
+ };
487
+ ```
488
+
489
+ **appData factories.** Instead of pre-creating a call/client (or assigning on `call-added` /
490
+ `client-added`) just to enrich its `appData`, register a factory once. It runs whenever the entity
491
+ is created without an explicit `settings.appData` — including the lazy creation inside `accept()`.
492
+ The `client` factory receives the already-created parent `observedCall`, so it can derive fields
493
+ from it.
494
+
495
+ Both also receive **`acceptCtx`**: the [`AcceptContext`](#context-the-acceptcontext) of the
496
+ `accept()` that caused the creation, or `undefined` when you created the entity yourself. This is
497
+ what lets an [accept middleware](#accept-middlewares-global-pre-dispatch-hook) resolve something
498
+ once — a tenant, a trace id — and have it land in `appData` at birth, instead of every factory
499
+ re-deriving it from the sample.
500
+
501
+ ```ts
502
+ const observer = new Observer({
503
+ createCallAppData: ({ callId, acceptCtx }) => ({ callId, startedAt: Date.now(), tenant: acceptCtx?.tenant }),
504
+ createClientAppData: ({ clientId, observedCall }) => ({ clientId, tenant: observedCall.appData.tenant }),
505
+ });
506
+
507
+ observer.accept(sample, { tenant: 'acme' });
508
+ ```
509
+
510
+ `appData` stays application-owned: the context is *offered* to the factory, never written across by
511
+ the library, and it is still not stored on any entity.
512
+
513
+ Key members:
514
+
515
+ - `accept(sample: ClientSample, context?: AcceptContext): void`
516
+ - `addAcceptMiddleware(...mw: AcceptMiddleware[]): this` / `removeAcceptMiddleware(...mw): this` — global pre-dispatch sample hooks (see [Accept middlewares](#accept-middlewares-global-pre-dispatch-hook))
517
+ - `getObservedCall<T>(callId): ObservedCall<T> | undefined`
518
+ - `createObservedCall<T>(settings, acceptCtx?): ObservedCall<T> | undefined`
519
+ - `getOrCreateObservedCall<T>(settings, acceptCtx?): ObservedCall<T> | undefined`
520
+ - `addIssue(issue: Omit<ObserverIssue, 'scope'>): void` — raise an **observer-level** finding → emits `observer-issue`. `scope` is stamped for you
521
+ - `update(): void` — force an aggregation/`observer-updated` tick
522
+ - `addObserverDetector(name, config?): this` — build a cross-call detector onto `observer.detectors`
523
+ - `addCallDetector(name, config?): this` — register a call-scoped detector for every call created
524
+ from now on
525
+ - `removeCallDetector(name, { includeOpenCalls? }): number` — stop building it, and (by default) drop
526
+ it from calls already open. Returns how many live instances were removed
527
+ - `removeObserverDetector(name): number` — remove an observer-scoped detector. For one specific
528
+ instance use `observer.detectors.remove(detector)`
529
+ - `addValidator(name, config?): this` — start a one-shot structural check
530
+ - `cancelValidator(name | validator, reason?): number` — stop a running check; it finishes
531
+ `inconclusive` with the reason and emits `validation-ready`
532
+ - `close(): void`
533
+ - `readonly detectors: Detectors` — observer-scoped registry. **Starts empty**; nothing is implicit
534
+ - `readonly callDetectorConfigs: Map<name, config>` — what `addCallDetector` recorded
535
+ - `readonly callSummaryCollector?: CallSummaryCollector` — owns the resolved `config.callSummary`,
536
+ the summary subscriptions, and the summaries. `undefined` when summaries are off, which is the
537
+ only place that answer lives
538
+ - `readonly validators: Set<RunningValidator>` — normally empty; each removes itself on finishing
539
+ - `readonly activeIssuesRegistry: ActiveIssuesRegistry` — the fleet's open client issues
540
+ - `readonly observedCalls: Map<string, ObservedCall>`
541
+ - `readonly observedTURN: ObservedTURN`
542
+ - `get appData()`, `get numberOfCalls()`
543
+ - counters: `numberOfClients`, `numberOfClientsUsingTurn`, `numberOfInboundRtpStreams`,
544
+ `numberOfOutboundRtpStreams`, `numberOfDataChannels`, `numberOfPeerConnections`,
545
+ `totalAddedCall`, `totalRemovedCall`, `closed`
546
+ - `on/off/once/emit` typed against the [event map](#event-catalogue)
547
+
548
+ ### `ObservedCall`
549
+
550
+ ```ts
551
+ type ObservedCallSettings<AppData = Record<string, unknown>> = {
552
+ // update this call whenever one of its clients accepts a sample. Default true.
553
+ autoUpdateOnClientUpdate?: boolean;
554
+ callId: string;
555
+ appData?: AppData;
556
+ closeCallIfEmptyForMs?: number;
557
+ };
558
+ ```
559
+
560
+ Key members:
561
+
562
+ - `readonly callId: string`, `appData: AppData`
563
+ - `readonly observedClients: Map<string, ObservedClient>`, `get numberOfClients()`
564
+ - `getObservedClient<T>(clientId)`, `createObservedClient<T>(settings, acceptCtx?)`, `getOrCreateObservedClient<T>(settings, acceptCtx?)` (all `… | undefined`)
565
+ - `addIssue(issue: Omit<CallIssue, 'scope'>): void` — raise a **call-level** finding → emits `call-issue`. `scope` is stamped for you
566
+ - `addDetector(name, config?): this` — build a call-scoped detector onto this call only
567
+ - `removeDetector(name): number` — remove it from this call, `close()`ing it. For one specific
568
+ instance use `call.detectors.remove(detector)`
569
+ - `readonly detectors: Detectors` — server-side detector registry (empty by default; see [Detectors](#detectors-server-side-extension-point))
570
+ - `readonly activeIssuesRegistry: ActiveIssuesRegistry` — this call's open client issues, propagating into the observer's
571
+ - `readonly unconsumedOutboundTracks: Set<ObservedOutboundTrack>` — maintained by the resolver
572
+ - `scoreCalculator: ScoreCalculator`, `get score()`, `readonly calculatedScore`
573
+ - `remoteTrackResolver?: RemoteTrackResolver` — set from `ObserverConfig.createRemoteTrackResolver` at call creation (see [Remote track resolution](#remote-track-resolution-mediasoup--sfu))
574
+ - aggregates: `numberOfIssues`, `numberOfPeerConnections`, `numberOfInboundRtpStreams`,
575
+ `numberOfOutboundRtpStreams`, `numberOfDataChannels`, `maxNumberOfClients`,
576
+ `clientsUsedTurn: Set<string>`, `startedAt?`, `endedAt?`, `closedAt?`, `closed`
577
+ - `summary?: CallSummary` — the live record of this call, when [summaries](#call-summaries) are on
578
+ - `update()`, `close()`
579
+
580
+ ### `ObservedClient`
581
+
582
+ ```ts
583
+ type ObservedClientSettings<AppData = Record<string, unknown>> = {
584
+ clientId: string;
585
+ appData?: AppData;
586
+ closeClientIfIdleForMs?: number;
587
+ };
588
+ ```
589
+
590
+ Key members:
591
+
592
+ - `readonly clientId: string`, `appData: AppData`, `readonly call: ObservedCall`
593
+ - `readonly observedPeerConnections: Map<string, ObservedPeerConnection>`
594
+ - `readonly sink?: ClientSampleSink` — the per-client sink (see [Sinks](#sinks-per-client-sample-persistence)), if `createClientSink` is configured; listen on it for `close`/`error`
595
+ - **Injection API** (queue app data to be merged into the next sample processing):
596
+ `injectEvent(ClientEvent)`, `injectIssue(ClientIssue)`, `injectMetaData(ClientMetaData)`,
597
+ `injectExtensionStat(ExtensionStat)`, `injectAttachment(attachments: Record<string, unknown>)`
598
+ - **Direct add API** (process immediately): `addIssue(ClientIssue)`, `addMetadata(ClientMetaData)`,
599
+ `addExtensionStats(ExtensionStat)`
600
+ - Metrics (current/derived): `currentAvgRttInMs?`, `currentMinRttInMs?`, `currentMaxRttInMs?`,
601
+ `receivingAudioBitrate`, `receivingVideoBitrate`, `sendingAudioBitrate`, `sendingVideoBitrate`,
602
+ `usingTURN`, `usingTCP`, `availableIncomingBitrate`, `availableOutgoingBitrate`
603
+ - Network: `publicAddress?` (last known public IP, sticky), `publicAddresses` (distinct addresses of the open PCs) — see [Public address](#public-address-of-a-client)
604
+ - Counts: `numberOfInboundRtpStreams`, `numberOfOutboundRtpStreams`, `numberOfInbundTracks`,
605
+ `numberOfOutboundTracks`, `numberOfDataChannels`, `numberOfPeerConnections`
606
+ - Per-tick deltas: `deltaReceivedAudioBytes`, `deltaSentAudioBytes`, … (see source for the full set)
607
+ - Lifecycle: `joinedAt?`, `leftAt?`, `closedAt?`, `closed`, `get score()`
608
+ - Metadata: `browser?`, `engine?`, `platform?`, `operationSystem?`, `mediaDevices`, `mediaConstraints`
609
+ - `accept(sample, context?)`, `close()`
610
+
611
+ ### `ObservedPeerConnection`
612
+
613
+ Key members:
614
+
615
+ - `readonly peerConnectionId: string`, `readonly client: ObservedClient`, `appData?`
616
+ - The 15 `observed*` sub-stat `Map`s (listed [above](#entity-hierarchy)), plus array getters:
617
+ `codecs`, `inboundRtps`, `outboundRtps`, `remoteInboundRtps`, `remoteOutboundRtps`,
618
+ `mediaSources`, `mediaPlayouts`, `dataChannels`, `peerConnectionTransports`, `iceTransports`,
619
+ `iceCandidates`, `iceCandidatePairs`, `certificates`, `selectedIceCandidatePairs`,
620
+ `selectedIceCandiadtePairForTurn`
621
+ - State: `connectionState?`, `iceConnectionState?`, `iceGatheringState?`, `usingTURN`, `usingTCP`
622
+ - Network: `publicAddress?`, `publicAddressUpdatedAt?` — see [Public address](#public-address-of-a-client)
623
+ - Metrics: `currentRttInMs?`, `iceRttInMs?`, `rtcpRttInMs?`, `sfuHopRttInMs?`, `currentJitter?`,
624
+ `availableIncomingBitrate`, `availableOutgoingBitrate`, sending/receiving bitrates, packet rates,
625
+ and `total*` / `delta*` byte/packet counters
626
+ - `accept(pcSample, context?)`, `close()`, `get score()`
627
+
628
+ **Two different round trips — don't mix them.** `iceRttInMs` comes from ICE/STUN consent checks and
629
+ measures the trip to *whatever terminates ICE*: **in an SFU topology that is the SFU**, so it is the
630
+ client↔SFU leg. `rtcpRttInMs` comes from RTCP receiver reports and is an **end-to-end** media-path
631
+ round trip. They are not interchangeable, and averaging them together produces a number that moves
632
+ as streams come and go for reasons unrelated to the network. `currentRttInMs` therefore *prefers*
633
+ RTCP and falls back to ICE — always one kind within a tick, never a blend. `sfuHopRttInMs`
634
+ (`rtcp − ice`) estimates everything past the SFU, which separates "this client's last mile is slow"
635
+ from "the path beyond the SFU is slow".
636
+
637
+ **Counter-reset boundaries.** Chrome resets an SSRC's cumulative counters when the codec switches
638
+ ([crbug/webrtc/5361](https://bugs.chromium.org/p/webrtc/issues/detail?id=5361), open since 2015),
639
+ which otherwise shows up as a sawtooth spike or a negative bitrate. `ObservedInboundRtp` /
640
+ `ObservedOutboundRtp` therefore set **`counterResetBoundary`** on any tick where `codecId`,
641
+ `encoder`/`decoderImplementation` or `scalabilityMode` changed, and suppress every delta for that
642
+ tick. Without this, a room-wide codec rollout fires a synchronized fake-degradation alert across
643
+ every participant at once.
644
+
645
+ **Remote-RTP correlation (derived).** During `accept()`, receiver/sender reports are linked
646
+ to the local streams by `remoteId` (fallback SSRC) and surfaced as fields:
647
+
648
+ - on `ObservedOutboundRtp`: `remoteRttInMs?`, `remoteFractionLost?`, `remoteJitter?`, `remotePacketsLost?`
649
+ - on `ObservedInboundRtp`: `remoteRttInMs?`, `remoteBytesSent?`, `remotePacketsSent?`, `remoteTimestamp?`
650
+
651
+ These are reset each tick and only set when the matching remote report is present.
652
+
653
+ ### Public address of a client
654
+
655
+ The address a client is seen from on the internet is already in the samples: it is carried by the
656
+ **local** ICE candidates. `ObservedPeerConnection.publicAddress` is derived every tick as:
657
+
658
+ 1. the local candidate of the **selected pair**: `address` for `srflx` / `prflx` (or `host`, when
659
+ the host address itself is public); for `relay`, `relatedAddress` — the mapped address the
660
+ TURN server saw the client from (`address` is the relay's own address, never the client's);
661
+ 2. failing that, the same rule over the local candidates of **every reported pair** (for example a
662
+ gathered `srflx` candidate while the selected pair is a LAN `host` pair).
663
+
664
+ Only public IP literals count: private (RFC 1918 / ULA), carrier-grade NAT, loopback, link-local,
665
+ multicast, mDNS `.local` names and the `0.0.0.0` browsers use to redact `relatedAddress` are
666
+ skipped (`isPublicIpAddress()` is exported). Remote candidates are never used.
667
+
668
+ The value is **sticky** — it changes only when a *different* public address is found, never back to
669
+ `undefined` — and every change emits `peer-connection-public-address-changed`.
670
+ `ObservedClient.publicAddress` follows whichever of its peer connections changed most recently,
671
+ survives that peer connection closing, and emits `client-public-address-changed`;
672
+ `ObservedClient.publicAddresses` lists the distinct addresses of the currently open ones.
673
+
674
+ ```ts
675
+ observer.on('client-public-address-changed', ({ observedClient, publicAddress, previousPublicAddress }) => {
676
+ log.info('client %s seen from %s (was %s)', observedClient.clientId, publicAddress, previousPublicAddress);
677
+ });
678
+ ```
679
+
680
+ > Browsers may redact `relatedAddress`, so a client whose only working path is a TURN relay and
681
+ > that gathered no `srflx` candidate can stay without a `publicAddress`. For that case, the address
682
+ > your signalling server sees is the authoritative fallback.
683
+
684
+ ---
685
+
686
+ ## Schema types (`ClientSample`)
687
+
688
+ The shape of an accepted sample (re-exported from this package; identical to
689
+ `@observertc/schemas`). Only the top level is shown — each stat object mirrors the standard
690
+ WebRTC `getStats()` dictionaries plus a few extensions.
691
+
692
+ ```ts
693
+ type ClientSample = {
694
+ timestamp: number; // client wall-clock (ms epoch)
695
+ callId?: string; // set by you or the library
696
+ clientId?: string; // set by you or the library
697
+ score?: number; // optional client-computed score (0..5)
698
+ attachments?: Record<string, unknown>;
699
+ peerConnections?: PeerConnectionSample[];
700
+ clientEvents?: ClientEvent[];
701
+ clientIssues?: ClientIssue[];
702
+ clientMetaItems?: ClientMetaData[];
703
+ extensionStats?: ExtensionStat[];
704
+ };
705
+
706
+ type PeerConnectionSample = {
707
+ peerConnectionId: string;
708
+ attachments?: Record<string, unknown>; // e.g. { direction: 'send'|'recv', producerId, consumerId, label }
709
+ score?: number;
710
+ inboundTracks?; outboundTracks?;
711
+ codecs?;
712
+ inboundRtps?; remoteInboundRtps?;
713
+ outboundRtps?; remoteOutboundRtps?;
714
+ mediaSources?; mediaPlayouts?;
715
+ peerConnectionTransports?; dataChannels?;
716
+ iceTransports?; iceCandidates?; iceCandidatePairs?;
717
+ certificates?;
718
+ };
719
+
720
+ type ClientEvent = { type: string; payload?: Record<string, unknown>; timestamp?: number; /* +ids */ };
721
+ type ClientIssue = { type: string; payload?: Record<string, unknown>; key?: string; timestamp?: number };
722
+ type ClientMetaData = { type: string; payload?: Record<string, unknown>; timestamp?: number; /* +ids */ };
723
+ type ExtensionStat = { type: string; payload?: Record<string, unknown> };
724
+ ```
725
+
726
+ `payload` fields are objects (schema **3.7.0**: free-form JSON, so a payload may nest objects and
727
+ arrays), and the library reads the ones it understands. **Older clients keep working**: a payload
728
+ from a pre-3.5.0 client arrives as a JSON string and is parsed on the way in, so a fleet running a
729
+ mix of client versions needs no coordination — every reader sees the same object either way. Values
730
+ inside a payload are `unknown`, so narrow before use (`typeof p.trackId === 'string'`).
731
+
732
+ **`ClientEventTypes`** (enum of known `event.type` values): `CLIENT_JOINED`, `CLIENT_LEFT`,
733
+ `PEER_CONNECTION_OPENED/CLOSED/STATE_CHANGED`, `MEDIA_TRACK_ADDED/REMOVED/MUTED/UNMUTED/RESUMED`,
734
+ `ICE_GATHERING_STATE_CHANGED`, `ICE_CONNECTION_STATE_CHANGED`, `DATA_CHANNEL_OPEN/CLOSED/ERROR`,
735
+ `NEGOTIATION_NEEDED`, `SIGNALING_STATE_CHANGE`, `ICE_CANDIDATE`, `ICE_CANDIDATE_ERROR`, and the
736
+ mediasoup set `PRODUCER_*` / `CONSUMER_*` / `DATA_PRODUCER_*` / `DATA_CONSUMER_*`.
737
+
738
+ **`ClientMetaTypes`** (enum of known meta `type` values): `MEDIA_CONSTRAINT`, `MEDIA_DEVICE`,
739
+ `MEDIA_DEVICES_SUPPORTED_CONSTRAINTS`, `USER_MEDIA_ERROR`, `LOCAL_SDP`, `OPERATION_SYSTEM`,
740
+ `ENGINE`, `PLATFORM`, `BROWSER`.
741
+
742
+ ### Worked example: a real `ClientSample`
743
+
744
+ Two consecutive samples from one participant ("Guest" in room `qq0iwfnd`) of an
745
+ edumeet/mediasoup call show what actually flows through `accept()`: a rich **join snapshot**,
746
+ then lean **steady-state ticks**.
747
+
748
+ **Sample 1 — the join snapshot.** Carries the one-off lifecycle `clientEvents` and device
749
+ `clientMetaItems` alongside the first stats. (Abbreviated; ids and times are from the real log.)
750
+
751
+ ```jsonc
752
+ {
753
+ "timestamp": 1780572332518,
754
+ "callId": "d3dbf2f5-79be-4cb8-9d43-fb404f07ef27",
755
+ "clientId": "c926983c-4468-4046-ae8c-a9cabe1a1868",
756
+ "score": 0, // no quality measured yet on the join tick
757
+ "attachments": { "displayName": "Guest", "roomId": "qq0iwfnd", "actualSessionId": "d3dbf2f5-…" },
758
+
759
+ "clientEvents": [ // chronological lifecycle (12 in the real sample)
760
+ { "type": "CLIENT_JOINED", "timestamp": 1780572324515 },
761
+ { "type": "PEER_CONNECTION_OPENED", "timestamp": 1780572326790 }, // pc=b81c8d9d (media)
762
+ { "type": "ICE_GATHERING_STATE_CHANGED", "timestamp": 1780572326811 }, // → gathering
763
+ { "type": "PEER_CONNECTION_STATE_CHANGED", "timestamp": 1780572326812 }, // → connecting
764
+ { "type": "PRODUCER_ADDED", "timestamp": 1780572326821 }, // producer=1abdaf82 (audio)
765
+ { "type": "MEDIA_TRACK_ADDED", "timestamp": 1780572326821 }, // track=36ae42df (audio)
766
+ { "type": "PEER_CONNECTION_STATE_CHANGED", "timestamp": 1780572326827 }, // → connected
767
+ { "type": "PRODUCER_ADDED", "timestamp": 1780572326837 }, // producer=ba06a35b (video)
768
+ { "type": "DATA_PRODUCER_CREATED", "timestamp": 1780572326853 }
769
+ ],
770
+
771
+ "clientMetaItems": [ // environment & devices, one-off (10 in the real sample)
772
+ { "type": "USER_AGENT_DATA", "payload": { "…": "Chrome 148 / macOS" } },
773
+ { "type": "MEDIA_DEVICE", "payload": { "label": "BRIO 4K Stream Edition" } }
774
+ // …mic / camera / speaker devices…
775
+ ],
776
+
777
+ "peerConnections": [
778
+ {
779
+ "peerConnectionId": "b81c8d9d-…", // the media PC — Guest publishes to the SFU
780
+ "outboundRtps": [ /* audio + video */ ],
781
+ "outboundTracks": [ /* mic + camera: label, settings, capabilities */ ],
782
+ "remoteInboundRtps": [ /* RTCP feedback from the SFU */ ],
783
+ "codecs": [ /* … */ ], "iceTransports": [ /* … */ ],
784
+ "iceCandidatePairs": [ /* … */ ], "dataChannels": [ /* … */ ]
785
+ },
786
+ { "peerConnectionId": "8635acb7-…", "peerConnectionTransports": [ /* … */ ] } // signaling-only PC
787
+ ]
788
+ }
789
+ ```
790
+
791
+ What `accept()` does with it, in order — each step emits on the bus with full ancestry:
792
+
793
+ 1. lazily creates the `ObservedCall` → **`call-added`**;
794
+ 2. creates the `ObservedClient` → **`client-added`**, then **`client-joined`** (from `CLIENT_JOINED`);
795
+ 3. creates an `ObservedPeerConnection` per entry → **`peer-connection-added`** (×2 here);
796
+ 4. creates an `ObservedOutboundTrack` per track → **`outbound-track-added`**, plus the matching
797
+ **`outbound-rtp-added`**;
798
+ 5. replays the device list as **`client-metadata`** events and the lifecycle items as
799
+ **`client-event`**; and finally **`client-updated`** for the whole tick.
800
+
801
+ `attachments.roomId` lands on `observedClient.attachments` (read it on `client-updated`, **not** at
802
+ creation — see [Ingestion](#ingestion-accept-context--lifecycle)).
803
+
804
+ **Sample 2 — a steady-state tick** (~8 s later): same `callId` / `clientId`, **no** new
805
+ `clientEvents` or `clientMetaItems`, just refreshed `peerConnections` stats. Each PC now scores `5`
806
+ and the aggregate client `score` is `4.74` — a healthy call. This is the shape of nearly every
807
+ sample: each tick refreshes metrics and fires the `*-updated` events, while the heavy join
808
+ snapshot happens only once.
809
+
810
+ ---
811
+
812
+ ## Detectors (server-side extension point)
813
+
814
+ `observer-js` ships [ten detectors](#built-in-detectors), each an opt-in extension you register
815
+ explicitly with `addObserverDetector` / `addCallDetector` / `addDetector` (see
816
+ [Registering detectors](#registering-detectors)) — none are created automatically. All of them
817
+ correlate **across** the clients of a call or the calls of a fleet, because that is the only thing a
818
+ server can do better than a browser: per-client signals — packet loss, jitter, RTT, freezes — are
819
+ already detected on the client and arrive on samples as `clientIssues` (surfaced via `client-issue`).
820
+
821
+ Findings are raised as **`CallIssue`** or **`ObserverIssue`** — both share `IssueBase`: `{ type,
822
+ timestamp, conclusion?, payload? }` — and the payload is the **object**, not a JSON string. A
823
+ server-raised finding is delivered to an in-process handler, so there is nothing to serialise for:
824
+
825
+ ```ts
826
+ observer.on('call-issue', ({ observedCall, issue }) => {
827
+ issue.payload; // the object; no JSON.parse
828
+ issue.conclusion?.faultDomain; // a first-class field, not payload.conclusion
829
+ issuePayloadAsString(issue); // only at an edge that needs text (log, HTTP, queue)
830
+ });
831
+ ```
832
+
833
+ (`ClientIssue`, the type on samples, is a wire format — its payload may arrive as a JSON string from
834
+ a pre-3.5.0 client, which is why the library parses it before you see it.)
835
+
836
+ The registry is also an open extension point, on **`ObservedCall`**:
837
+
838
+ ```ts
839
+ import { Observer, Detector } from '@observertc/observer-js';
840
+
841
+ class MyCrossClientDetector implements Detector {
842
+ readonly name = 'my-detector';
843
+ constructor(private readonly call /* : ObservedCall */) {}
844
+ update() { // called on every call.update()
845
+ // …inspect this.call.observedClients across participants…
846
+ if (/* condition only visible server-side */ false) {
847
+ this.call.addIssue({ type: this.name, payload: { /* … */ }, timestamp: Date.now() });
848
+ // → emitted on the bus as 'call-issue'
849
+ }
850
+ }
851
+ }
852
+
853
+ const observer = new Observer();
854
+ observer.on('call-added', ({ observedCall }) => {
855
+ observedCall.detectors.add(new MyCrossClientDetector(observedCall));
856
+ });
857
+ observer.on('call-issue', ({ observedCall, issue }) => { /* react */ });
858
+ ```
859
+
860
+ ### Client issues: the lifecycle, and the division of labour
861
+
862
+ The most important thing to understand about detection in this library is **what it deliberately
863
+ does not do**. A client running
864
+ [`client-monitor-js`](https://github.com/ObserveRTC/client-monitor-js) already ships ~20 detectors
865
+ that decide *what is wrong with that endpoint* — `congestion`, `cpulimitation`, `audio-concealment`,
866
+ `freezed-video-track`, `keyframe-storm`, `video-decoder-overloaded`, `stuck-decoder`,
867
+ `ice-disconnected`, and so on. Those verdicts are better than anything re-derived from raw counters
868
+ server-side, because they carry hysteresis and multi-signal confirmation: `audio-concealment`
869
+ subtracts silent concealment (raw `concealedSamples` rises during ordinary silence, so a naive
870
+ detector flags every quiet moment); `audio-jitter-buffer-stress` requires the buffer to be grown
871
+ **and** NetEQ to be time-stretching (a grown buffer alone means NetEQ is *succeeding*);
872
+ `ice-disconnected` only fires once `disconnected` has persisted, so the blips ICE heals on its own
873
+ never surface.
874
+
875
+ **observer-js does not repeat that work.** Its job is the question no browser can answer: *who else
876
+ is in this state right now, what do they have in common, and where in publisher → SFU → subscriber
877
+ does the fault begin?*
878
+
879
+ #### The wire format
880
+
881
+ From client-monitor-js **4.6.0** the whole issue lifecycle reaches the server. A stateful issue
882
+ arrives as two `clientIssues[]` entries sharing a `key`:
883
+
884
+ ```
885
+ raise: { type: 'stuck-decoder', key, payload, timestamp: raisedAt }
886
+ resolution: { type: 'stuck-decoder-resolved', key, payload: { raisedAt, comment, …final }, timestamp: resolvedAt }
887
+ ```
888
+
889
+ The observer opens an entry in `observedClient.activeIssues` on the raise and closes it on the
890
+ matching key, emitting **`client-issue-resolved`** with the finished interval. Handled for you:
891
+
892
+ - the `-resolved` **suffix** is stripped, so both entries share one logical `type`;
893
+ - a **re-raise** of a live key refreshes the payload without restarting `raisedAt`;
894
+ - **keyless** entries are one-shot — reported via `client-issue`, never tracked;
895
+ - issues still open when a client closes are **force-resolved** (`resolvedBy: 'client-closed'`), and
896
+ the registry additionally expires stale entries, so a crashed participant can't leave an issue
897
+ "active" forever.
898
+
899
+ #### Why intervals beat windows
900
+
901
+ This turns point-in-time symptom reports into **intervals**, and that is the whole game. *"Several
902
+ clients reported congestion in the last 10 seconds"* is a heuristic that has to guess whether the
903
+ symptoms are still happening. *"Several clients are congested **right now, simultaneously**"* is
904
+ ground truth, because the client says when the episode ends. Overlapping intervals are far stronger
905
+ evidence of a shared cause than near-in-time reports.
906
+
907
+ ```ts
908
+ observer.on('client-issue', ({ observedClient, issue }) => { /* opened (or one-shot) */ });
909
+ observer.on('client-issue-resolved', ({ resolvedIssue }) => {
910
+ resolvedIssue.type; // 'stuck-decoder' — suffix stripped
911
+ resolvedIssue.durationInMs; // how long the episode lasted
912
+ resolvedIssue.resolvedBy; // 'client' | 'timeout' | 'client-closed'
913
+ });
914
+
915
+ // the live per-client mirror
916
+ observedClient.activeIssues; // ObservedClientIssueRegistry, keyed by issue.key
917
+ ```
918
+
919
+ > **client-monitor-js >= 4.6.0 is required** for every issue-driven detector. There is no fallback
920
+ > path that infers these conditions from raw counters — the client decides better, and maintaining a
921
+ > worse second implementation to be polite to old clients is how both end up wrong. Issues without a
922
+ > `key` have no lifecycle (nothing could ever close them), so they stay one-shot: reported on
923
+ > `client-issue`, never registered.
924
+
925
+ #### `ActiveIssuesRegistry` — issues are **pushed**, not polled
926
+
927
+ A detector does not go looking for the issues it cares about. It implements `ActiveIssueTracker` and
928
+ registers for the types it consumes; the registry hands them over as they open and close.
929
+
930
+ ```ts
931
+ observedCall.activeIssuesRegistry // this meeting
932
+ observer.activeIssuesRegistry // the fleet; every call's registry propagates into it
933
+
934
+ observer.activeIssuesRegistry.addIssueTracker('congestion', myDetector);
935
+ observer.activeIssuesRegistry.removeIssueTracker(myDetector);
936
+
937
+ registry.values(); // the open issues in this scope, oldest first
938
+ registry.size; // how many
939
+ ```
940
+
941
+ The cost of a detector is then proportional to the issues it actually receives, not to the number of
942
+ participants: a healthy 500-client fleet does no per-tick work at all, because nothing was pushed.
943
+
944
+ **There is no wildcard.** A tracker names its types and sees nothing else. "Feed me everything and
945
+ I'll work out what matters" moves the decision from the application — which knows its client build
946
+ and its issue vocabulary — onto a detector that has to guess, and it makes the cost of a
947
+ subscription unbounded and invisible. If a detector should watch five types, list five types.
948
+
949
+ Onset spread is measured on the **observer clock**, never the client's. `raisedAt` comes from each
950
+ participant's own machine, and comparing those across clients makes clock skew look like a
951
+ synchronized infrastructure event.
952
+
953
+ ### Observer-level detectors (cross-call / SFU-wide)
954
+
955
+ Some findings only exist **above** call scope — "many calls on the same SFU degraded at once" is far
956
+ more actionable than fifty individual client alerts. The same detector registry exists on the `Observer`,
957
+ runs on every `observer.update()`, and raises findings through `observer.addIssue(...)`, surfaced on
958
+ the bus as **`observer-issue`**:
959
+
960
+ ```ts
961
+ observer.detectors.add({
962
+ name: 'sfu-wide-degradation',
963
+ update: () => {
964
+ const degradedCalls = [ ...observer.observedCalls.values() ].filter(isDegraded);
965
+
966
+ if (observer.numberOfCalls > 3 && degradedCalls.length / observer.numberOfCalls > 0.6) {
967
+ observer.addIssue({ type: 'SFU_WIDE_QUALITY_DEGRADATION', timestamp: Date.now() });
968
+ }
969
+ },
970
+ });
971
+
972
+ observer.on('observer-issue', ({ issue }) => alert(issue));
973
+ ```
974
+
975
+ ### Publisher → subscribers: the resolver links
976
+
977
+ The question a single browser can never answer is *"did **everyone** receiving Alice see the same
978
+ degradation?"*. The join is the publisher↔subscriber links maintained by a
979
+ [`RemoteTrackResolver`](#remote-track-resolution-mediasoup--sfu), and detectors walk them directly:
980
+
981
+ ```ts
982
+ outboundTrack.remoteInboundTracks; // Set<ObservedInboundTrack> — every subscriber of this source
983
+ inboundTrack.remoteOutboundTrack; // the publisher, or undefined if unlinked
984
+ inboundTrack.getInboundRtp(); // that receiver's RTP stats
985
+ observedCall.unconsumedOutboundTracks; // published tracks with no subscriber at all
986
+ ```
987
+
988
+ > A `TrackDistributionAggregator` class used to sit in front of these links and summarise every
989
+ > published track against all of its receivers, on every tick. It is gone. It scanned the majority
990
+ > (all published tracks) to find the interesting minority, which is the wrong axis — the detectors
991
+ > now start from the handful of *affected* tracks the issue registry pushed at them and resolve only
992
+ > those. The statistics helpers it used (`percentile`, `median`, `summarize`, `counterDelta`,
993
+ > `robustZScore`, `SlidingWindow`, `TrendTester`) are all still exported for building your own.
994
+
995
+ ### Call health: `CallHealthAggregator`
996
+
997
+ The **client** axis. Where the resolver links answer "how was *this source* delivered?", this asks
998
+ "how is *each participant* doing, sending vs receiving?":
999
+
1000
+ ```ts
1001
+ import { CallHealthAggregator } from '@observertc/observer-js';
1002
+
1003
+ const health = new CallHealthAggregator(observedCall).aggregate();
1004
+
1005
+ health.degradedRatio; // 0.82 — the number that distinguishes shared faults from individual ones
1006
+ health.inboundDegradedRatio; // receiving side → egress/downstream suspicion
1007
+ health.outboundDegradedRatio; // sending side → ingress suspicion
1008
+ health.rttInMs?.median; // percentile rollups, never means
1009
+ health.qualityLimitation; // { cpu, bandwidth, other } client counts
1010
+ health.clients; // per-client entries with `reasons`, direction flags, TURN/TCP
1011
+ ```
1012
+
1013
+ ### Registering detectors
1014
+
1015
+ **Nothing is created implicitly.** A `new Observer()` has zero detectors. There is no detector
1016
+ configuration in `ObserverConfig` and no default set — an application says what it wants to watch, or
1017
+ it watches nothing.
1018
+
1019
+ ```ts
1020
+ const observer = new Observer({
1021
+ createRemoteTrackResolver: createDefaultMediasoupRemoteTrackResolverFactory(),
1022
+ });
1023
+
1024
+ // observer-scoped (cross-call) — built immediately onto `observer.detectors`
1025
+ observer.addObserverDetector('observer-concurrent-issue-detector', {
1026
+ issueTypes: [ 'congestion', 'ice-disconnected', 'ice-connection-failed' ],
1027
+ minAffectedCalls: 3,
1028
+ });
1029
+ observer.addObserverDetector('turn-server-outage-detector', { minClientsAtPeak: 10 });
1030
+
1031
+ // call-scoped — recorded in `observer.callDetectorConfigs`, applied to every call created AFTER this
1032
+ observer.addCallDetector('call-concurrent-issue-detector', {
1033
+ issueTypes: [ 'congestion', 'ice-disconnected' ],
1034
+ });
1035
+ // one specific call
1036
+ observedCall.addDetector('issue-fan-out-detector', { issueTypes: [ 'freezed-video-track' ] });
1037
+ ```
1038
+
1039
+ Every `add*` is **chainable** — it returns the owning entity:
1040
+
1041
+ ```ts
1042
+ observer
1043
+ .addObserverDetector('turn-server-health-detector')
1044
+ .addObserverDetector('turn-server-outage-detector', { minClientsAtPeak: 10 })
1045
+ .addValidator('remote-track-resolver');
1046
+ ```
1047
+
1048
+ #### Removing them
1049
+
1050
+ By **name**, on the entity — which removes *every* instance under that name:
1051
+
1052
+ ```ts
1053
+ observer.removeObserverDetector('turn-server-outage-detector'); // → 1
1054
+ observer.removeCallDetector('call-concurrent-issue-detector'); // stops it everywhere
1055
+ observedCall.removeDetector('issue-fan-out-detector'); // this call only
1056
+ ```
1057
+
1058
+ By **instance**, through the registry — which is where instances live, since `add*` returns the
1059
+ entity rather than the detector:
1060
+
1061
+ ```ts
1062
+ observer
1063
+ .addObserverDetector('client-population-issue-detector', { issueTypes: [ 'cpulimitation' ], groupBy: 'browser' })
1064
+ .addObserverDetector('client-population-issue-detector', { issueTypes: [ 'cpulimitation' ], groupBy: 'operationSystem' });
1065
+
1066
+ const [ byBrowser, byOs ] = observer.detectors.getAll('client-population-issue-detector');
1067
+
1068
+ observer.detectors.remove(byOs); // keeps the browser axis running
1069
+ ```
1070
+
1071
+ `Detectors` is a small collection: `instances` (a copy, in registration order), `listOfNames`,
1072
+ `size`, `get(name)`, `getAll(name)`, `has(name)`, `add(detector)`, `remove(detector)`,
1073
+ `removeByName(name)`, `clear()`, and it is iterable — `for (const detector of call.detectors)`.
1074
+ `instances` being a copy is deliberate: removing while iterating the live array would skip entries,
1075
+ and "drop the ones that look like X" is the most natural thing to want to write.
1076
+
1077
+ Two things worth knowing:
1078
+
1079
+ - **By name removes every instance under it**, not the first. A name can legitimately be registered
1080
+ more than once — `ClientPopulationIssueDetector` is meant to be added once per `groupBy` axis — and
1081
+ "remove whichever is first in the array" is not something a caller can predict from a name. Go via
1082
+ `detectors.getAll(name)` + `detectors.remove(instance)` when you mean one of them.
1083
+ - **`removeCallDetector` affects calls already open, by default.** Otherwise whether a detector runs
1084
+ would depend on when a call happened to join, which is not a state anyone can reason about. Pass
1085
+ `{ includeOpenCalls: false }` to change only what future calls are built with.
1086
+
1087
+ Every removal path calls the detector's `close()`, so it unsubscribes from the issue registry and
1088
+ drops any timers or bus listeners. A detector removed without closing would keep being fed matching
1089
+ issues for the life of the call — invisible, unbounded, and it would still look healthy if you
1090
+ inspected it.
1091
+
1092
+ Detectors are named by their kebab-case `NAME`, and the name types the config — an unknown name or a
1093
+ key that belongs to a different detector will not compile. Each detector owns its defaults in its own
1094
+ constructor, beside the doc explaining what each threshold means; there is no central table to keep
1095
+ in sync.
1096
+
1097
+ > **Why no defaults?** A detector nobody asked for is a detector nobody will act on. It costs time on
1098
+ > every tick and raises findings into a handler that was not written to expect them. Earlier versions
1099
+ > auto-created everything from a three-state config slot; the result was applications receiving
1100
+ > finding types they had never heard of.
1101
+
1102
+ Every issue-driven detector takes an explicit, non-empty `issueTypes` (or
1103
+ `publisherIssueTypes`/`receiverIssueTypes`). There is no "watch everything" option — see
1104
+ [the registry](#activeissuesregistry--issues-are-pushed-not-polled).
1105
+
1106
+ > **🔗 marks detectors that require a
1107
+ > [`RemoteTrackResolver`](#remote-track-resolution-mediasoup--sfu).** They reason about a published
1108
+ > track and its subscribers, so without the publisher↔subscriber links they see nothing and stay
1109
+ > **silent forever** — which looks exactly like "no problems found". Configure
1110
+ > `ObserverConfig.createRemoteTrackResolver`, and start the
1111
+ > [`remote-track-resolver` validator](#validators--one-shot-structural-checks) to prove it is wired.
1112
+
1113
+ ### Built-in detectors
1114
+
1115
+ They consume the verdicts `client-monitor-js` >= 4.6.0 already ships (raise + `<type>-resolved`) and
1116
+ add only the cross-participant conclusion. **None re-derives a per-endpoint verdict from raw
1117
+ counters** — that is the rule the whole design hangs on:
1118
+
1119
+ > **If a condition is detectable on the client, the client's issue is the source of truth.**
1120
+
1121
+ | Detector | 🔗 | Scope | Raises |
1122
+ |----------|:--:|-------|--------|
1123
+ | `CallConcurrentIssueDetector` | | call | `CONCURRENT_CLIENT_ISSUES`, `ISSUE_ONSET_BURST` |
1124
+ | `ObserverConcurrentIssueDetector` | | **observer** | `CROSS_CALL_CONCURRENT_ISSUES`, `CROSS_CALL_ISSUE_ONSET_BURST` |
1125
+ | `IssueFanOutDetector` | 🔗 | call | `PUBLISHED_TRACK_ISSUE_FAN_OUT`, `SINGLE_RECEIVER_ISSUE` |
1126
+ | `PublisherFaultCorroborationDetector` | 🔗 | call | `CORROBORATED_PUBLISHER_FAULT` |
1127
+ | `TrackDeliveryMismatchDetector` | 🔗 | call | `PUBLISHED_TRACK_NOT_DELIVERED`, `RECEIVER_TRACK_NOT_DELIVERED`, `PUBLISHER_TRACK_DRY` |
1128
+ | `UnconsumedTrackDetector` | 🔗 | call | `UNCONSUMED_PUBLISHED_TRACK` |
1129
+ | `AudioEchoDetector` | 🔗¹ | call | `AUDIO_ECHO_SUSPECTED` |
1130
+ | `ClientPopulationIssueDetector` | | **observer** | `CLIENT_POPULATION_ISSUE` |
1131
+ | `SfuCongestionDetector` | | **observer** | `sfu-congestion` |
1132
+ | `TurnServerHealthDetector` | | **observer** | `TURN_SERVER_DEGRADED` |
1133
+ | `TurnServerOutageDetector` | | **observer** | `TURN_SERVER_OUTAGE` |
1134
+
1135
+ What each adds that no endpoint can know:
1136
+
1137
+ - **`CallConcurrentIssueDetector`** — *who else in this meeting is in this state right now?* The
1138
+ difference between "one person's Wi-Fi" and "this room is broken".
1139
+ - **`ObserverConcurrentIssueDetector`** — *is our infrastructure in trouble?* A **separate class**,
1140
+ not the call one with a bigger denominator, because it is a different question with different
1141
+ gates. It requires the group to span at least `minAffectedCalls` **independent calls** (default
1142
+ `2`) and raises its own `CROSS_CALL_*` types. Without that gate, one thirty-person meeting where
1143
+ everyone is congested clears every client threshold and pages you for a single bad room the
1144
+ call-scoped detector already reported. Clients in different calls share no room, no publisher and
1145
+ no host — only the servers, which is what makes the finding conclusive. Note there is deliberately
1146
+ **no participant ratio** at this scope: six broken calls out of forty is a small share of all
1147
+ clients, and a ratio gate would hide exactly the event you want.
1148
+ - **`IssueFanOutDetector`** — *does this issue follow one published source, or one receiver?*
1149
+ - **`PublisherFaultCorroborationDetector`** — *do **both ends** of one track agree the source is at
1150
+ fault?* Fan-out sees one end and infers; this sees the publisher reporting `encoder-bottleneck`
1151
+ about its own send path *while* its subscribers report `freezed-video-track` about receiving it.
1152
+ Two independent parties, one conclusion, nothing left to deduce — hence the highest confidence in
1153
+ the library. Run both: fan-out is broader and catches the case where the publisher is fine and the
1154
+ SFU's forwarding is not.
1155
+ - **`ClientPopulationIssueDetector`** — *is this concentrated on one **kind of client**?* The one
1156
+ correlation here that is neither per-call nor per-server. Every other observer-scoped detector
1157
+ reasons "clients in unrelated calls share only the infrastructure, so it must be us" — right for
1158
+ network symptoms, **wrong for endpoint ones**. `cpulimitation` across six unrelated calls is not an
1159
+ SFU event; CPU is owned by the endpoint, so what those endpoints share is a browser version or a
1160
+ client release. Groups by `browser` / `engine` / `platform` / `operationSystem` / `location`, one
1161
+ axis per instance. The gate is **relative risk**, not share: "30% of Chrome 141 is unhappy" means
1162
+ nothing if 30% of everyone is, and a share-based rule simply indicts whichever browser is most
1163
+ popular. See [the `location` axis](#grouping-by-place-the-location-axis) for the geographic form.
1164
+ - **`SfuCongestionDetector`** — *is congestion spiking across the fleet right now?* Counts distinct
1165
+ clients reporting congestion in fixed wall-clock buckets and compares each bucket against a
1166
+ median+MAD baseline of the ones before it. Buckets rather than update ticks on purpose: the tick is
1167
+ unevenly spaced and shorter than a client's sampling period, so counting on it compares windows of
1168
+ different lengths and calls the difference a signal. Only add it when the observer's calls all come
1169
+ from the **same SFU** — the finding's meaning is "these clients share only that server".
1170
+ - **`TrackDeliveryMismatchDetector`** — *are the two ends of a track disagreeing?*
1171
+ - **`UnconsumedTrackDetector`** — *is anyone actually subscribed?* (reads the resolver's silence)
1172
+ - **`AudioEchoDetector`** — *is someone sending the others' voices back into the call?* Compares
1173
+ each outbound audio track's per-tick level (`ObservedOutboundTrack.rmsAudioLevel`) with the
1174
+ loudest other participant's, bucket by bucket. An echo is active whenever the source speaks, and
1175
+ louder when the source is louder; turn-taking fails the first test and cross-talk the second.
1176
+ When both tracks of a pair pass, it reports the one with the client's `loopback-audio-input`
1177
+ issue open, else the one that follows more reliably, else the quieter one (`ambiguousDirection`).
1178
+ Works at the clients' sampling granularity, so it catches sustained echo (a `Monitor of …`
1179
+ loopback device, speakers into an open mic) rather than faint echo.
1180
+ ¹ The resolver is needed only for publishers whose browser reports no media-source audio level
1181
+ (Firefox): their level is then read from what their receivers heard.
1182
+ - **`TurnServerHealthDetector`** — *does trouble cluster on one relay?*
1183
+ - **`TurnServerOutageDetector`** — covers the case the health detector structurally cannot. The
1184
+ health detector groups clients by the server relaying them and asks how many report issues — it
1185
+ needs clients *on* the server to ask. When a TURN server dies, allocation fails: existing sessions
1186
+ drop and new clients never obtain a relay candidate through it, so they are never attributed to it
1187
+ at all. Its population goes to zero and the health detector falls silent for the worst possible
1188
+ reason. **Degradation makes clients unhappy; an outage makes them disappear.** Absence is a
1189
+ dangerous signal, so the **control group** is the heart of the design: a call ending, everyone
1190
+ leaving at 6pm, and a fleet-wide network event all look identical to an outage. It refuses to blame
1191
+ a server unless clients *not* relayed through it are demonstrably still connected
1192
+ (`requireControlGroup`, on by default).
1193
+
1194
+ #### There is no ICE detector
1195
+
1196
+ ICE trouble is reported by `client-monitor-js` >= 4.6.0 as the keyed issues `ice-disconnected`,
1197
+ `ice-connection-failed`, `ice-transport-stalled` and `unstable-ice-path`, each with hysteresis and
1198
+ multi-signal confirmation behind it. An `IceDisruptionDetector` used to re-derive that server-side
1199
+ from raw state transitions; it has been removed, because the server sees less and guesses more. The
1200
+ client knows whether `disconnected` persisted or healed in 200 ms; the observer does not.
1201
+
1202
+ Correlating ICE trouble is now configuration, not a class:
1203
+
1204
+ ```ts
1205
+ observer.addObserverDetector('observer-concurrent-issue-detector', {
1206
+ issueTypes: [ 'ice-disconnected', 'ice-connection-failed', 'ice-transport-stalled' ],
1207
+ });
1208
+ ```
1209
+
1210
+ ### Grouping by place: the `location` axis
1211
+
1212
+ If your clients report coordinates, `ClientPopulationIssueDetector` can group by **where they are**
1213
+ instead of what they run — which is the grouping network symptoms actually cluster by:
1214
+
1215
+ ```ts
1216
+ observer.addObserverDetector('client-population-issue-detector', {
1217
+ issueTypes: [ 'congestion', 'ice-disconnected' ],
1218
+ groupBy: 'location',
1219
+ locationPrecision: 3, // geohash chars: 3 ~156 km, 4 ~39 km, 5 ~5 km
1220
+ resolveClientLocation: (client) => client.attachments?.geo as { latitude: number, longitude: number },
1221
+ });
1222
+ ```
1223
+
1224
+ **The client still owns "RTT jumped".** client-monitor's `CongestionDetector` compares each peer
1225
+ connection's RTT against its own EWMA baseline and requires a bandwidth-limitation corroboration
1226
+ before raising `congestion`. Absolute RTT is not comparable between clients — someone 200 ms away is
1227
+ *always* 200 ms away, so the only signal is deviation from that client's own baseline, which is
1228
+ exactly what the client measures. The observer's contribution is the part no endpoint can see: that
1229
+ many of the affected clients are **in the same place at the same time**.
1230
+
1231
+ Three things to know:
1232
+
1233
+ - **Cells, not radii.** The population is a geohash prefix. "Within N km" is a clustering problem —
1234
+ order-dependent, no stable group name, pairwise cost — and a detector needs the *same* group key on
1235
+ every tick for its cooldown and control group to mean anything. The cost is that a cell boundary can
1236
+ split two adjacent clients, which biases towards missing a finding rather than inventing one.
1237
+ - **Only the cell key is reported.** `payload.population` is the geohash; coordinates never enter the
1238
+ issue. These payloads get archived into [call summaries](#call-summaries), so that matters.
1239
+ - **Geography is confounded with your topology.** The control group is "everyone outside this cell",
1240
+ which cannot separate *"the path into this region degraded"* from *"the SFU serving this region
1241
+ degraded"*. If a region maps largely onto one deployment, both hypotheses fit the same evidence —
1242
+ so the finding concludes `infrastructure` and points you at `SfuCongestionDetector` /
1243
+ `TurnServerHealthDetector`, which answer whether clients elsewhere on the same server also
1244
+ degraded. It does not claim an attribution it cannot support.
1245
+
1246
+ Coordinates are not in `ClientSample`, so `resolveClientLocation` is required; without it the detector
1247
+ logs a warning at construction and finds nothing, rather than quietly reporting no findings forever.
1248
+
1249
+ ### Validators — one-shot structural checks
1250
+
1251
+ Every detector above answers *"is something wrong right now?"* and runs on every tick, because the
1252
+ answer legitimately changes. A **validator** answers *"is this deployment built correctly?"* — which
1253
+ only changes when you deploy. So it is not configured on and left running: you **start** one, it runs
1254
+ until it can decide, reports once, and the observer drops it.
1255
+
1256
+ ```ts
1257
+ observer.addValidator('simulcast-receivers', { minChecks: 5 });
1258
+
1259
+ observer.on('validation-ready', ({ validator, report }) => {
1260
+ if (!report.ready) return;
1261
+ if (report.verdict === 'layer-decided-lowest-common-denominator') page(validator, report);
1262
+ });
1263
+
1264
+ onDeploy(() => observer.addValidator('simulcast-receivers')); // check again
1265
+ ```
1266
+
1267
+ `observer.validators` is the set currently running — normally empty, since each removes itself on
1268
+ finishing. There is no revalidation timer: a deploy, not elapsed time, is what makes a structural
1269
+ verdict stale, so re-checking means starting another.
1270
+
1271
+ **Cancelling.** A check that has not decided can be stopped, by name or by instance:
1272
+
1273
+ ```ts
1274
+ observer.cancelValidator('simulcast-receivers', 'sfu redeployed');
1275
+
1276
+ // or one specific instance — `observer.validators` holds what is running
1277
+ for (const validator of observer.validators) observer.cancelValidator(validator, 'shutting down');
1278
+ ```
1279
+
1280
+ Cancelling is **not** silent discarding. The validator finishes `inconclusive` with your reason,
1281
+ emits `validation-ready` like any other completion, and removes itself. That matters twice over:
1282
+ anything waiting on the verdict would otherwise wait forever, and *"we stopped asking"* is a
1283
+ materially different outcome from *"we asked and learned nothing"* — which is exactly what an
1284
+ `inconclusive` carrying a reason records. Pass a real reason; the default tells the reader nothing
1285
+ they could not already infer. `observer.close()` cancels whatever is still running with
1286
+ `'observer closed'`.
1287
+
1288
+ | Validator | `addValidator` name | Question | Also raises |
1289
+ |-----------|---------------------|----------|-------------|
1290
+ | `SimulcastReceiverValidator` 🔗 | `simulcast-receivers` | Does the SFU pick layers per receiver, or drag the publisher down to the worst one? | `WORST_RECEIVER_CONTAGION` |
1291
+ | `RemoteTrackResolverValidator` | `remote-track-resolver` | Is the resolver actually linking anything? | `REMOTE_TRACK_LINKS_UNRESOLVED` |
1292
+ | `CodecConsistencyValidator` | `codec-consistency` | Is everyone on the same codec — and is it the one you think you negotiated? | `CODEC_INCONSISTENCY` |
1293
+
1294
+ **`SimulcastReceiverValidator`** — simulcast (or SVC) exists so one slow
1295
+ participant doesn't set everyone's quality: with several encodings the server hands the struggling
1296
+ receiver a lower layer and leaves the rest alone. Without it — or with a server that relays RTCP end
1297
+ to end, so the publisher's bandwidth estimate collapses to the slowest receiver — the only way to
1298
+ serve them is to make the *source* send less. Both causes look identical from outside; what the check
1299
+ establishes is whether per-receiver adaptation happens at all.
1300
+
1301
+ | `verdict` | meaning |
1302
+ |-----------|---------|
1303
+ | `layer-decided-per-receiver` | verified — a receiver fell far behind and the publisher carried on |
1304
+ | `layer-decided-lowest-common-denominator` | the publisher followed its worst receiver; everyone gets the slowest participant's quality |
1305
+ | `inconclusive` | cancelled, or the observer closed, before it could decide |
1306
+
1307
+ **Not finishing is not a pass.** The check only runs when a publisher has 3+ receivers and one is at
1308
+ most half the median; plenty of healthy deployments never present that. A validator that never sees it
1309
+ simply keeps running and never reports — it does not quietly succeed. `report.checks` counts the times
1310
+ the check genuinely ran, so an `inconclusive` with `checks: 0` says plainly that nothing was verified.
1311
+
1312
+ **`RemoteTrackResolverValidator`** exists because of a specific, nasty failure mode. Four things here
1313
+ are built on publisher↔subscriber links — `IssueFanOutDetector`,
1314
+ `PublisherFaultCorroborationDetector`, `TrackDeliveryMismatchDetector`, `UnconsumedTrackDetector`
1315
+ (and `SimulcastReceiverValidator`) — and every one of them correctly does *nothing* when the links
1316
+ are missing rather than guessing. So a resolver wired to the wrong id field leaves all of them
1317
+ permanently silent, and **silence is what a healthy deployment looks like too**: you would conclude
1318
+ your calls were clean when in fact nothing was ever examined. Verdicts: `links-resolved` /
1319
+ `no-links-resolved` / `inconclusive`. Run it at start-up and after changing the resolver or the SFU's
1320
+ id scheme.
1321
+
1322
+ **`CodecConsistencyValidator`** answers two things at once. A **split** — participants of one call on
1323
+ different codecs — is a real fault with a confusing symptom: an SFU that forwards without transcoding
1324
+ cannot serve them all, so some pairs see each other and some do not, with no error anywhere. Only
1325
+ something holding every participant at once can see it. The quieter half is the silent fallback: a
1326
+ deployment configured for VP9 or AV1 drops to VP8 whenever one endpoint cannot negotiate the
1327
+ preference, the call keeps working at a higher bitrate than budgeted, and the team believes it
1328
+ shipped AV1 months ago. Give it `expected` and it says so. Verdicts: `codec-consistent` /
1329
+ `codec-split` / `unexpected-codec` / `inconclusive`.
1330
+
1331
+ ```ts
1332
+ observer.addValidator('remote-track-resolver');
1333
+ observer.addValidator('codec-consistency', { expected: { video: 'video/VP9', audio: 'audio/opus' } });
1334
+ ```
1335
+
1336
+ #### `CallIssue` vs `ObserverIssue`
1337
+
1338
+ Server-raised findings come in two kinds, distinguished by the scope that raised them:
1339
+
1340
+ | | raised by | delivered as | `scope` |
1341
+ |---|---|---|---|
1342
+ | `CallIssue` | `observedCall.addIssue(...)` | `call-issue` | `'call'` |
1343
+ | `ObserverIssue` | `observer.addIssue(...)` | `observer-issue` | `'observer'` |
1344
+
1345
+ Both share `IssueBase` — `type`, `timestamp`, `conclusion?`, `payload?` — and `Issue` is the union,
1346
+ discriminated on `scope`.
1347
+
1348
+ ```ts
1349
+ observer.on('call-issue', ({ observedCall, issue }) => {
1350
+ issue.scope; // 'call'
1351
+ observedCall.callId; // the call — NOT repeated in the payload
1352
+ issue.conclusion?.faultDomain;
1353
+ issue.payload; // evidence only
1354
+ });
1355
+
1356
+ observer.on('observer-issue', ({ issue }) => {
1357
+ issue.scope; // 'observer'
1358
+ });
1359
+ ```
1360
+
1361
+ `scope` is stamped by `addIssue` rather than asked of the detector: it is a fact about *where the
1362
+ finding was raised*, which the entity knows and a detector should not have to restate. Having it on
1363
+ the issue — not merely implied by which event fired — keeps a finding self-describing once it leaves
1364
+ the bus, into a shared handler, a log line or a queue.
1365
+
1366
+ **The payload is evidence and nothing else.** It no longer repeats `type`, `scope`, or the `callId`
1367
+ already carried by the event, and `conclusion` was lifted out of it to a first-class field. A payload
1368
+ that restates its own envelope invites the two to disagree — and they did, because nothing kept them
1369
+ in step. `payload` is always an object (the `string` form is gone, along with `issuePayloadOf`); use
1370
+ `issuePayloadAsString(issue)` at a boundary that genuinely needs text.
1371
+
1372
+ #### Conclusions
1373
+
1374
+ Every issue-driven finding carries a `conclusion` — the interpretation step, so the person reading
1375
+ the alert doesn't have to perform it. It sits **beside** the evidence, not inside it:
1376
+
1377
+ ```jsonc
1378
+ {
1379
+ "type": "CROSS_CALL_ISSUE_ONSET_BURST",
1380
+ "scope": "observer",
1381
+ "timestamp": 1739812345678,
1382
+ "conclusion": {
1383
+ "faultDomain": "infrastructure",
1384
+ "summary": "network congestion is open across independent calls at the same time — 6 of 40 calls (11/300 clients)",
1385
+ "recommendation": "check SFU egress bandwidth and host network saturation before looking at any single participant",
1386
+ "confidence": 0.85
1387
+ },
1388
+ "payload": {
1389
+ "issueType": "congestion",
1390
+ "calls": 40, "affectedCalls": 6,
1391
+ "perCall": [ { "callId": "…", "affectedClients": 4, "totalClients": 9 } ]
1392
+ }
1393
+ }
1394
+ ```
1395
+
1396
+ `faultDomain` is one of `infrastructure`, `call`, `published-track`, `endpoint`, `client-population`
1397
+ or `unknown`, and it comes from the **spread**, not the issue type — congestion in one call is a
1398
+ meeting problem, congestion in six calls is a server problem, and the client reported the identical
1399
+ symptom in both.
1400
+
1401
+ One case is worth knowing about because it inverts the usual reading: **`cpu-limitation` spread
1402
+ across many independent calls concludes `client-population`, not `infrastructure`.** Endpoint CPU is
1403
+ owned by the endpoint, so breadth there points at what those endpoints share — a recent client
1404
+ release, a browser version, shared VDI hardware — and paging the SFU on-call would be wrong. The
1405
+ conclusion table encodes that so nobody has to rediscover it during an incident.
1406
+
1407
+ Unknown issue types (your own custom client detectors) still produce a structurally valid conclusion
1408
+ from the spread alone; they just get generic wording.
1409
+
1410
+ Two functions are exported, one per scope: `concludeCallIssue()` and `concludeObserverIssue()`. They
1411
+ are separate because a detector already knows its scope, and a single generic function forced every
1412
+ caller to pass the other scope's fields as placeholders — call-scoped detectors passing
1413
+ `affectedCalls: 1, totalCalls: 1` forever, observer-scoped ones passing a participant ratio that was
1414
+ deliberately never read. Placeholders like that invite being read as if they meant something.
1415
+
1416
+ #### Cost
1417
+
1418
+ Detectors run inside `call.update()`, on your event loop, so their cost matters. Two things keep it
1419
+ off the participant axis:
1420
+
1421
+ - **Issues are pushed, not polled.** A detector holds only what the registry handed it, so an
1422
+ `update()` that finds `size === 0` — the overwhelmingly common case — costs one comparison,
1423
+ whatever the participant count. Nothing iterates clients looking for trouble.
1424
+ - **So are unconsumed tracks.** `observedCall.unconsumedOutboundTracks` is maintained by the resolver
1425
+ as tracks gain and lose subscribers, so `UnconsumedTrackDetector` reads a set that is normally
1426
+ empty instead of walking every published track (529 µs → 65 µs per tick at 1 200 tracks).
1427
+ - **Track lookups start from the affected minority.** A detector resolving an issue to its published
1428
+ track searches the *reporting client's* peer connections (typically one or two), not the call.
1429
+
1430
+ At 20 calls × 12 participants (2 640 subscriptions) the whole detector pass costs ~1.3 ms per tick.
1431
+ `yarn bench` prints a per-detector breakdown for your own shape.
1432
+
1433
+ #### Worked examples
1434
+
1435
+ [`examples/detectors.ts`](./examples/detectors.ts) (`yarn example:detectors`) runs one scenario per
1436
+ detector — the question it answers, its full config, the synthetic traffic that makes it fire, and
1437
+ the finding with its conclusion. It asserts every expected finding is produced, so it doubles as a
1438
+ smoke test. [`examples/sfu-observer.ts`](./examples/sfu-observer.ts) (`yarn example`) is the end-to-end
1439
+ tour instead: ingest → correlate → react, with the mediasoup wiring alongside.
1440
+
1441
+ #### `TrackDeliveryMismatchDetector` — resolving an ambiguous symptom
1442
+
1443
+ A dry track ("no bytes are arriving") is the clearest symptom there is and, on its own, completely
1444
+ ambiguous. A receiver seeing silence cannot distinguish *the camera was switched off* from *the SFU
1445
+ stopped forwarding* from *my own consumer wedged* — all three look identical from the browser.
1446
+
1447
+ Joining the two ends of the published track resolves it:
1448
+
1449
+ | publisher | subscribers | verdict |
1450
+ |---|---|---|
1451
+ | sending | **all** dry | `PUBLISHED_TRACK_NOT_DELIVERED` — the forwarding path |
1452
+ | sending | **some** dry | `RECEIVER_TRACK_NOT_DELIVERED` — those consumers (in mediasoup: recreate them) |
1453
+ | dry | any dry | `PUBLISHER_TRACK_DRY` — the source stopped; **not** an SFU fault |
1454
+
1455
+ The publisher side is judged from both available signals: its own `dry-outbound-track` issue when the
1456
+ client reports one, and the observed outbound RTP (`deltaPacketsSent`) as fallback and corroboration.
1457
+ That combination is what makes the first row trustworthy — the server can state that packets
1458
+ demonstrably left the publisher during the same interval in which every receiver got nothing.
1459
+
1460
+ This is the "SFU forwarding mismatch" check, and it needs **no mediasoup instrumentation at all** —
1461
+ the clients' own dry-track verdicts plus the resolver links are sufficient.
1462
+
1463
+ #### `UnconsumedTrackDetector` — reading the resolver's silence
1464
+
1465
+ The one detector where the *absence* of links is the signal: a track still pushing packets whose
1466
+ `remoteInboundTracks` set is empty, i.e. uplink and SFU ingress spent on media nobody receives
1467
+ (everyone has the publisher hidden, a simulcast layer no viewer selects, or an app that forgot to
1468
+ stop a track). It waits `minUnconsumedDurationInMs` first, since a gap between publishing and the
1469
+ first subscription is normal at join time.
1470
+
1471
+ Note the trap this one has to guard against, and why it checks `call.remoteTrackResolver` at runtime
1472
+ rather than trusting the flag alone: **"no subscribers" and "no resolver configured" produce the
1473
+ identical observation.** Without a resolver it would report every published track in the call as
1474
+ unconsumed.
1475
+
1476
+ ## Call summaries
1477
+
1478
+ Everything else in this library is about *now*. Detectors answer "is something wrong right now",
1479
+ validators answer a structural question once, and both read state the call throws away when it ends.
1480
+ A **call summary** is the one thing that outlives the call: who was in it, what was raised against
1481
+ it, how it scored — the questions asked *after* the meeting, by support, by billing, by whoever is
1482
+ writing the incident note.
1483
+
1484
+ It is configured on the observer, at construction:
1485
+
1486
+ ```ts
1487
+ const observer = new Observer({
1488
+ callSummary: {
1489
+ include: [ 'clients', 'issues', 'turnServers', 'scores' ],
1490
+ },
1491
+ });
1492
+
1493
+ observer.on('call-summary', ({ summary }) => archive(summary));
1494
+ ```
1495
+
1496
+ Omit `callSummary`, or set it to `null`, and there are no summaries and **not one extra bus
1497
+ subscription**. Pass an object — `{}` is valid — and every call this observer creates carries one.
1498
+
1499
+ > **Why construction-time, when detectors are added per call?** A summary is a record of what
1500
+ > happened, and a record you can switch on halfway through is a record with a hole in it. Calls that
1501
+ > started before the switch would carry different sections from calls that started after, with
1502
+ > nothing on either to say which. One shape for every call, or none.
1503
+
1504
+ ### Sections are opt-in, and absence means "not collected"
1505
+
1506
+ `include` picks from four built-ins, and **the default is `[]`** — none of them:
1507
+
1508
+ | Section | Contains |
1509
+ |---------|----------|
1510
+ | `clients` | `clientIds` (join order), `peak`, `joined`, `left`. Identifiers and counts only |
1511
+ | `issues` | `CallIssue[]`, in the order raised, capped by `maxIssues` |
1512
+ | `turnServers` | `serverUrls` that carried media, and `clientsRelayed` |
1513
+ | `scores` | `min` / `max` / `median` of the call score, and `samples` |
1514
+
1515
+ **A missing section means it was never collected — never "nothing happened".** Reading
1516
+ `summary.issues === undefined` as "this call was clean" is the one misreading this type invites, so
1517
+ there is no default-empty section to make it easy. This is the same rule as `inconclusive` on a
1518
+ validator: silence is not success.
1519
+
1520
+ The `clients` section is deliberately identifiers and counts. Anything *about* a client — browser,
1521
+ platform, region — is already on `observedClient` while the call is live, and belongs in
1522
+ `attachments` via an enricher if you want it kept; see below.
1523
+
1524
+ ### Enrichers: fold in anything, from any call-scoped event
1525
+
1526
+ ```ts
1527
+ new Observer({
1528
+ callSummary: {
1529
+ include: [ 'issues' ],
1530
+ enrich: {
1531
+ 'client-joined': (summary, { observedClient }) => {
1532
+ // serialisable facts only — the region string, never the live object it came from
1533
+ ((summary.attachments.regions ??= []) as string[]).push(String(observedClient.appData.region));
1534
+ },
1535
+ },
1536
+ },
1537
+ });
1538
+ ```
1539
+
1540
+ Each enricher is typed against its own event's payload. Only **call-scoped** events are accepted —
1541
+ the ones carrying an `observedCall`. An enricher on `observer-issue` or `validation-ready` will not
1542
+ compile, because there is no single call to attribute a fleet-wide fact to, and quietly writing it
1543
+ into every open summary would be worse than a type error.
1544
+
1545
+ The library never writes to `summary.attachments`, so nothing you put there can collide with a
1546
+ section added in a future version.
1547
+
1548
+ > **Why `attachments` and not `appData`.** `appData` is live working state hung off an entity for
1549
+ > that entity's lifetime, and it may hold things that cannot be serialised — a mediasoup router, a
1550
+ > socket. A summary is the opposite: it outlives the call so it can be **shipped**, and it reaches
1551
+ > you on `call-summary` while the call it describes is being torn down, so an unserialisable value
1552
+ > in it points at something already gone. Same contract as `attachments` on a `ClientSample`: read
1553
+ > the live object off `observedCall` / `observedClient` in the enricher, attach what serialises —
1554
+ > the router's `id`, not the router. An enricher that throws is logged and skipped — a summary is a
1555
+ side-channel, and nothing about a call should break because a field could not be recorded.
1556
+
1557
+ ### Caps announce what they dropped
1558
+
1559
+ `maxIssues` (default `500`) and `maxClientIds` (default `10_000`) bound the two unbounded lists.
1560
+ When either bites, `summary.truncated` appears with the shortfall — present **only** when something
1561
+ was actually dropped. That is what makes dropping safe: the true count is recoverable as
1562
+ `issues.length + (truncated?.issues ?? 0)`. A silently truncated summary is worse than no summary,
1563
+ because someone will count `issues.length` and report it as the issue count.
1564
+
1565
+ `issues` is the plain array, with no derived tallies alongside it. A count is `issues.length` and a
1566
+ per-type count is one `filter` — both cheaper at the call site than kept correct here.
1567
+
1568
+ ### Reading it
1569
+
1570
+ `observedCall.summary` is live: read it at any point during the call. It is also delivered once on
1571
+ `call-summary`, emitted **inside** `close()` while the call is still in `observer.observedCalls` —
1572
+ after that the call is gone and there is nothing left to ask. `observer.close()` closes its calls
1573
+ first and its collector afterwards, so every summary still makes it out.
1574
+
1575
+ Cost is **one bus listener per subscribed event type, for the whole observer** — not one per call. A
1576
+ per-call design would be quadratic in concurrent calls: at 500 calls and eight events, 4 000
1577
+ listeners each doing 500 no-op invocations per event. Percentiles are computed once, at close.
1578
+
1579
+ ## Remote track resolution (mediasoup / SFU)
1580
+
1581
+ In an SFU, one participant's **outbound** track is delivered to other participants as **inbound**
1582
+ tracks (one **publisher** → many **subscribers**). Correlation is **opt-in** per observer: set
1583
+ `ObserverConfig.createRemoteTrackResolver`, a factory invoked when each call is created that returns the
1584
+ call's `RemoteTrackResolver` (or `undefined` for none).
1585
+
1586
+ `RemoteTrackResolver` is a generic, strategy-driven class. It subscribes to the bus (filtered to
1587
+ its call) and links tracks by **publisher id** — the link key — maintaining the links directly on
1588
+ the tracks: `inboundTrack.remoteOutboundTrack` and `outboundTrack.remoteInboundTracks: Set`.
1589
+
1590
+ ```ts
1591
+ import { Observer, createDefaultMediasoupRemoteTrackResolverFactory } from '@observertc/observer-js';
1592
+
1593
+ const observer = new Observer({
1594
+ createRemoteTrackResolver: createDefaultMediasoupRemoteTrackResolverFactory(),
1595
+ });
1596
+
1597
+ // later, given tracks (links are kept up to date as tracks come and go):
1598
+ const source = inboundTrack.remoteOutboundTrack; // the publishing ObservedOutboundTrack
1599
+ const receivers = [ ...outboundTrack.remoteInboundTracks ]; // the subscribing ObservedInboundTrack[]
1600
+ ```
1601
+
1602
+ Two built-in factories ship: `createDefaultMediasoupRemoteTrackResolverFactory()` (publisher =
1603
+ `attachments.producerId`, subscriber = `attachments.consumerId`) and
1604
+ `createP2pRemoteTrackResolverFactory()` (matches by RTP **SSRC**, preserved end-to-end in p2p).
1605
+
1606
+ For any other topology, build a `RemoteTrackResolver` with your own key resolvers — the publisher
1607
+ id is just whatever links a subscribed track to the published one:
1608
+
1609
+ ```ts
1610
+ import { Observer, RemoteTrackResolver } from '@observertc/observer-js';
1611
+
1612
+ const observer = new Observer({
1613
+ createRemoteTrackResolver: (observedCall) => new RemoteTrackResolver(observedCall, {
1614
+ resolveOutboundTrackPublisherId: (out) => out.attachments?.mediaId as string | undefined,
1615
+ resolveInboundTrackPublisherId: (inb) => inb.attachments?.mediaId as string | undefined,
1616
+ resolveInboundTrackSubscriberId: (inb) => inb.attachments?.subId as string | undefined, // optional
1617
+ }),
1618
+ });
1619
+ ```
1620
+
1621
+ For the mediasoup factory, the application puts `producerId` / `consumerId` (and optionally
1622
+ `direction`, `label`) into the track `attachments`.
1623
+
1624
+ ### Resolution stands alone — you do not need router observation
1625
+
1626
+ Track resolution and [mediasoup router observation](#mediasoup-router-observation) are two
1627
+ **independent** opt-ins that happen to share a vendor name. Nothing in `RemoteTrackResolver` reads
1628
+ `ObservedMediasoupRouter`, and nothing in `ObservedMediasoupRouter` touches calls, clients or tracks.
1629
+
1630
+ So if your application already builds its own per-router report and you only want the detectors that
1631
+ need publisher↔subscriber links, set `createRemoteTrackResolver` and simply never call
1632
+ `observer.createObservedMediasoupRouter(...)`. No `MediasoupRouterSample` is created, nothing
1633
+ accumulates, and these keep working:
1634
+
1635
+ `UnconsumedTrackDetector`, `PublisherFaultCorroborationDetector`, `TrackDeliveryMismatchDetector`,
1636
+ `IssueFanOutDetector`, `RemoteTrackResolverValidator`, `SimulcastReceiverValidator`.
1637
+
1638
+ ### Backing a strategy with your own mapping
1639
+
1640
+ The three resolvers are plain functions returning a link key, so they can read a table your own
1641
+ report already maintains rather than something the client attached. The only requirement is a key
1642
+ **visible on both sides**. SSRC is the useful one, because it needs no client cooperation at all —
1643
+ mediasoup knows each consumer's `rtpParameters.encodings[].ssrc` server-side, and the subscriber's
1644
+ inbound RTP reports the same value:
1645
+
1646
+ ```ts
1647
+ // your own table, filled where you already create consumers
1648
+ const ssrcToProducerId = new Map<number, string>();
1649
+
1650
+ const observer = new Observer({
1651
+ createRemoteTrackResolver: (observedCall) => new RemoteTrackResolver(observedCall, {
1652
+ resolveOutboundTrackPublisherId: (out) => out.attachments?.producerId as string | undefined,
1653
+ resolveInboundTrackPublisherId: (inb) => {
1654
+ const ssrc = inb.getInboundRtp()?.ssrc;
1655
+
1656
+ return ssrc === undefined ? undefined : ssrcToProducerId.get(ssrc);
1657
+ },
1658
+ }),
1659
+ });
1660
+ ```
1661
+
1662
+ > **A key that arrives late still links.** A track announces itself once, but its `attachments` are
1663
+ > replaced on every sample and a table like the one above is inherently racy against sample arrival.
1664
+ > Tracks whose key does not resolve at first sight are held and retried on their own
1665
+ > `*-track-updated`, i.e. exactly when new stats arrive for them — so a key that appears on the second
1666
+ > sample links then, rather than being lost for the track's lifetime. `resolver.pendingTrackCounts`
1667
+ > reports how many are still waiting; in a healthy setup it is `{ inbound: 0, outbound: 0 }`.
1668
+
1669
+ If a strategy resolves *nothing* the failure is quiet — "no subscribers" and "no links resolved" look
1670
+ identical from the outside. That is what
1671
+ [`RemoteTrackResolverValidator`](#validators--one-shot-structural-checks) is for; run it once in
1672
+ staging after wiring up a custom strategy.
1673
+
1674
+ ---
1675
+
1676
+ ## Mediasoup router observation
1677
+
1678
+ Everything above is built from the **client-reported** `ClientSample`. When you run a
1679
+ [mediasoup](https://mediasoup.org) SFU you also have the **server's own** ground truth — its
1680
+ routers, transports, producers, consumers and data channels, with exact lifetimes and state
1681
+ transitions. `ObservedMediasoupRouter` captures that server-side view into a
1682
+ **`MediasoupRouterSample`**, completely independent of the client sample pipeline.
1683
+
1684
+ ### The concept
1685
+
1686
+ You hand the observer a live mediasoup `Router`; it attaches to mediasoup's own `observer` API and,
1687
+ from then on, **passively tracks** the router's topology and lifecycle — with no polling and no
1688
+ changes to your media code:
1689
+
1690
+ - new transports (`webrtc` / `plain` / `pipe` / `direct`), their selected `tuple`, ICE/DTLS/SCTP
1691
+ state transitions and `connectedAt`;
1692
+ - producers (codec, SSRCs/RIDs, `pause`/`resume`) and consumers (`pause`/`resume`,
1693
+ `producerPaused`/`producerResumed`);
1694
+ - data producers and data consumers;
1695
+ - `createdAt` / `closedAt` for every entity above.
1696
+
1697
+ It keeps all of this **in memory**, in a single `MediasoupRouterSample` exposed as
1698
+ `observedRouter.sample` — see [`src/schema/MediasoupRouter.ts`](./src/schema/MediasoupRouter.ts). The
1699
+ sample **accumulates for the life of the router**: closed transports/producers/consumers are kept
1700
+ (with their `closedAt` set), not removed. Read it whenever you like — it's a plain object you own.
1701
+
1702
+ ### Memory & large meetings
1703
+
1704
+ This is intentionally the **simplest** approach — everything lives in memory and nothing is sampled
1705
+ or evicted for you. That's fine for typical rooms, but be aware of the cost at scale:
1706
+
1707
+ - **Consumers grow as O(N²)** on a single flat router: with `N` participants each producing audio +
1708
+ video and consuming everyone else, the sample holds roughly `2·N·(N−1)` consumer records (≈ 19,800
1709
+ for `N` = 100).
1710
+ - The sample is **cumulative** — closed entities and their `history` are retained — so it also grows
1711
+ with call duration and churn (renegotiation, simulcast layer changes, rejoins).
1712
+
1713
+ A 100-participant flat router can therefore reach tens of MB and keep growing. There is **no built-in
1714
+ sink, snapshotting, or eviction** — by design. **If you run large meetings, do your own sampling:**
1715
+ on your own cadence read `observedRouter.sample` (snapshot/serialize/persist what you need), drop what
1716
+ you don't, and close routers you no longer track. (mediasoup also typically shards routers across
1717
+ workers/cores, which keeps any one router small.)
1718
+
1719
+ ### Extending the sample, and building your own report
1720
+
1721
+ The sample is yours to annotate. Every entity — the router, each transport, producer, consumer, data
1722
+ producer and data consumer — has an `attachments?: Record<string, unknown>` slot, and there are three
1723
+ ways to fill it, from most declarative to most ad-hoc.
1724
+
1725
+ **1. `enrich` — mirror mediasoup's own `appData`.** The common case: your application already keeps
1726
+ `participantId`, `purpose` and similar on the mediasoup objects, and you want them on the sample.
1727
+ Runs once per entity at creation, before the corresponding event:
1728
+
1729
+ ```ts
1730
+ observer.createObservedMediasoupRouter({
1731
+ router,
1732
+ enrich: {
1733
+ producer: (producer) => ({ participantId: producer.appData.participantId, purpose: producer.appData.purpose }),
1734
+ consumer: (consumer) => ({ subscriberId: consumer.appData.subscriberId }),
1735
+ transport: (transport) => ({ role: transport.appData.role }),
1736
+ },
1737
+ });
1738
+ ```
1739
+
1740
+ A throwing enricher is caught and logged — it can't take the router's bookkeeping down with it.
1741
+
1742
+ **2. Lifecycle events — enrich on the fly.** Each entity announces itself as
1743
+ `<entity>-sample-added` and `<entity>-sample-closed`, carrying **the live sample object** (not a
1744
+ copy) plus the mediasoup object it came from. Mutating it in the handler is the intended pattern:
1745
+
1746
+ ```ts
1747
+ observedRouter.on('producer-sample-added', ({ sample, producer, transport }) => {
1748
+ sample.attachments = { ...sample.attachments, participantId: lookup(producer.id) };
1749
+ });
1750
+
1751
+ observedRouter.on('producer-sample-closed', ({ sample }) => {
1752
+ archive(sample); // its `closedAt` is set
1753
+ });
1754
+ ```
1755
+
1756
+ Events: `transport-sample-added` / `-closed`, `producer-sample-added` / `-closed`,
1757
+ `consumer-sample-added` / `-closed`, `data-producer-sample-added` / `-closed`,
1758
+ `data-consumer-sample-added` / `-closed`.
1759
+
1760
+ **3. `attachTo(id, attachments)` — annotate later, from anywhere.** When the knowledge arrives after
1761
+ the entity did (a signalling message, a database lookup that resolved):
1762
+
1763
+ ```ts
1764
+ observedRouter.attachTo(producerId, { participantId, joinedFrom: 'mobile' }); // merges
1765
+ ```
1766
+
1767
+ Ids are unique across mediasoup entity kinds, so one method covers all of them. It returns `false`
1768
+ for an unknown id rather than failing quietly — which matters when application events race the
1769
+ mediasoup ones. For direct access there are typed accessors: `getTransportSample(id)`,
1770
+ `getProducerSample(id)`, `getConsumerSample(id)`, `getDataProducerSample(id)`,
1771
+ `getDataConsumerSample(id)`. They index the *same* objects the arrays hold, so a lookup is O(1)
1772
+ instead of a `sample.producers.find(...)` scan.
1773
+
1774
+ #### Building your own report
1775
+
1776
+ `observedRouter.sample` is live — arrays grow and `history` entries are appended as the router runs,
1777
+ so a report built directly on it keeps changing after you think you're done. Use **`snapshot()`** for
1778
+ a detached deep copy:
1779
+
1780
+ ```ts
1781
+ const report = {
1782
+ ...observedRouter.snapshot(), // never moves again
1783
+ generatedAt: Date.now(),
1784
+ region: process.env.REGION,
1785
+ };
1786
+ ```
1787
+
1788
+ > **Note on typing.** The sample types no longer carry a `Record<string, unknown>` index signature.
1789
+ > That signature allowed arbitrary top-level keys but also silently accepted typos on real fields and
1790
+ > weakened autocomplete. Custom data belongs in `attachments`, which is typed as such. If you were
1791
+ > assigning ad-hoc keys directly onto a sample object, move them into `attachments`.
1792
+
1793
+ ### Matching peer connections — by **event**, not by storage
1794
+
1795
+ The observer correlates the SFU side with the client side **at the peer-connection level**: a
1796
+ mediasoup WebRTC transport and a client's `RTCPeerConnection` share the same id, so whenever an
1797
+ observed peer connection's id matches one of the router's WebRTC transport ids, that's a match.
1798
+
1799
+ **The observer does not store the router (or its sample) on any entity.** Instead, for **every**
1800
+ matching peer connection it emits **`mediasoup-router-matched-with-peer-connection`** and steps
1801
+ back — *your application* decides what the pairing means. The payload carries the full peer-connection
1802
+ ancestry, so you get the router **and** the matched `observedPeerConnection`, `observedClient` and
1803
+ `observedCall` in one place. Stamp the `routerId` into the peer connection's / client's `appData`,
1804
+ build your own index, attach the server sample to the call in your database — whatever fits.
1805
+
1806
+ This matching is **opt-in**: pass `matchPeerConnectionByWebRtcTransportId: true` to
1807
+ `createObservedMediasoupRouter`. When enabled, as peer connections are observed
1808
+ (`peer-connection-added`) the observer checks whether the peer connection's id is one of the router's
1809
+ WebRTC transport ids; on a hit it emits — once per matching peer connection — and keeps watching, so a
1810
+ router serving many participants emits one match per participant's transport. When the flag is omitted
1811
+ or `false`, no matching is performed and the event never fires. The internal listener is removed
1812
+ automatically when the router closes or the observer closes.
1813
+
1814
+ ### Ordering contract — observe the router first
1815
+
1816
+ Matching is **forward-only by design**, and that is sufficient because the lifecycle ordering is
1817
+ **guaranteed, not racy**:
1818
+
1819
+ - `ObservedMediasoupRouter` works purely by **subscribing to mediasoup's `observer` API**, so it can
1820
+ only see events that happen *after* it is created. You therefore create it the moment the router
1821
+ exists — **before** any transport is added to it — and it captures the rest going forward.
1822
+ - A mediasoup transport is always created **on the server first**; only then can the client connect
1823
+ to it, produce/consume, and begin shipping `ClientSample`s. So a peer connection — and the
1824
+ `peer-connection-added` event it triggers — can never appear before its server-side WebRTC
1825
+ transport already exists (and has been observed by the router).
1826
+
1827
+ Put together: by the time a `peer-connection-added` fires, the router has already recorded that
1828
+ transport's id in `webrtcTransportIds`, so a single forward-looking listener catches every match. No
1829
+ back-scan of existing peer connections and no re-check on transport creation are needed — the
1830
+ observer deliberately does **not** look backwards.
1831
+
1832
+ **Your responsibility:** call `createObservedMediasoupRouter(...)` as early as the router exists
1833
+ (before transports are added or samples are accepted). If you register the router *after* its
1834
+ transports are created or after the client's first sample, those events are already in the past and
1835
+ the corresponding matches are missed.
1836
+
1837
+ When the underlying mediasoup router closes, its `close` propagates to `ObservedMediasoupRouter`,
1838
+ which sets the sample's `closedAt` and emits **`mediasoup-router-removed`** — your cue to read /
1839
+ persist the final `observedRouter.sample` and drop your reference to it.
1840
+
1841
+ ### Options — `observer.createObservedMediasoupRouter(settings)`
1842
+
1843
+ | Field | Type | Required | Meaning |
1844
+ |-------|------|----------|---------|
1845
+ | `router` | `mediasoup.types.Router` | yes | the live router to observe; the observer attaches to `router.observer`. `.id` and the sample's `routerId` come from `router.id` |
1846
+ | `appData` | `Record<string, unknown>` | no | application-owned bag on the `ObservedMediasoupRouter` |
1847
+ | `attachments` | `Record<string, unknown>` | no | free-form data; carried on `sample.attachments` |
1848
+ | `matchPeerConnectionByWebRtcTransportId` | `boolean` | no | opt in to peer-connection matching: emit `mediasoup-router-matched-with-peer-connection` for each peer connection whose id matches one of the router's WebRTC transport ids. Omitted / `false` → no matching, the event never fires |
1849
+
1850
+ Peer-connection matching is **off by default**; enable it with
1851
+ `matchPeerConnectionByWebRtcTransportId: true`. Returns the `ObservedMediasoupRouter`, or `undefined`
1852
+ if the observer is closed (a router with the same id returns the existing instance — both warn).
1853
+
1854
+ Useful members on the returned object: `.sample` (the in-memory `MediasoupRouterSample`, with
1855
+ `createdAt` / `closedAt?` on it), `.appData`, `.attachments`, `.webrtcTransportIds: Set<string>`,
1856
+ `.id`, `.close()`.
1857
+
1858
+ ### Example
1859
+
1860
+ ```ts
1861
+ import { Observer, InMemorySink } from '@observertc/observer-js';
1862
+ import type { ObservedMediasoupRouterScope, ObservedPeerConnectionScope } from '@observertc/observer-js';
1863
+
1864
+ const observer = new Observer();
1865
+
1866
+ // 1) Feed client samples as usual so the observer knows about calls, clients & peer connections.
1867
+ // (e.g. transport-layer: observer.accept(clientSample, context))
1868
+
1869
+ // 2) Observe the SFU side; opt in to peer-connection matching. State accumulates in `.sample`.
1870
+ const router = /* your mediasoup router */ undefined as any;
1871
+ const observedRouter = observer.createObservedMediasoupRouter({
1872
+ router,
1873
+ matchPeerConnectionByWebRtcTransportId: true,
1874
+ });
1875
+
1876
+ // For large meetings, sample it yourself on your own cadence (see "Memory & large meetings"):
1877
+ // setInterval(() => persist(observedRouter.sample), 10_000);
1878
+
1879
+ // 3) Every peer connection whose id matches one of the router's WebRTC transport ids fires this —
1880
+ // WE decide what to do with each pairing. The payload carries the full ancestry.
1881
+ observer.on('mediasoup-router-matched-with-peer-connection',
1882
+ ({ observedMediasoupRouter, observedCall, observedPeerConnection }:
1883
+ ObservedMediasoupRouterScope & ObservedPeerConnectionScope) => {
1884
+ (observedPeerConnection.appData ??= {}).routerId = observedMediasoupRouter.id;
1885
+ myStore.linkRouterToCall(observedCall.callId, observedMediasoupRouter.id);
1886
+ },
1887
+ );
1888
+
1889
+ // 4) The router closed — read/persist the final state, then drop your reference.
1890
+ observer.on('mediasoup-router-removed', ({ observedMediasoupRouter }: ObservedMediasoupRouterScope) => {
1891
+ persist(observedMediasoupRouter.sample); // its `closedAt` is set
1892
+ });
1893
+ ```
1894
+
1895
+ ### Why event-driven matching instead of storing on the call
1896
+
1897
+ - **Loose coupling.** The call model stays about client telemetry; the SFU view lives on its own
1898
+ `ObservedMediasoupRouter` and is associated only if and how *you* choose.
1899
+ - **You own the association.** One router serves many peer connections (across clients and calls),
1900
+ and the right place to keep that mapping is application-specific — so the observer hands you each
1901
+ peer-connection match and gets out of the way.
1902
+ - **You own the sampling.** The router sample is plain in-memory state you read on your own terms;
1903
+ for large meetings, sample/persist it yourself (see [Memory & large meetings](#memory--large-meetings))
1904
+ rather than relying on the library to evict — it deliberately doesn't.
1905
+
1906
+ ---
1907
+
1908
+ ## Sinks (per-client sample persistence)
1909
+
1910
+ A **sink** receives the samples a client accepts — for archival, streaming, or later offline
1911
+ replay. Each `ObservedClient` gets its **own** sink, produced by the
1912
+ `ObserverConfig.createClientSink` factory when the client is created (return `undefined` for no
1913
+ sink). The client pushes every accepted sample to its sink, and `end()`s it on close.
1914
+
1915
+ ### The `ClientSampleSink` base class
1916
+
1917
+ `ClientSampleSink` is an **abstract base class** (a typed `EventEmitter`). You create a sink by
1918
+ **subclassing it** and implementing `write` and `end`. It is **object-mode**: `write` receives
1919
+ the `ClientSample` *object*, so each sink decides how (or whether) to serialize it — JSON line,
1920
+ protobuf, a remote POST body, an in-memory push, etc.
1921
+
1922
+ ```ts
1923
+ import { ClientSampleSink, ClientSample } from '@observertc/observer-js';
1924
+
1925
+ abstract class ClientSampleSink /* extends EventEmitter */ {
1926
+ abstract write(sample: ClientSample): boolean; // accept one sample; `false` = backpressure
1927
+ abstract end(): void; // flush; emit `close` when the destination is ready
1928
+
1929
+ // typed events (inherited): the listener signature is inferred from the event name
1930
+ on(event: 'close' | 'finish' | 'drain', listener: () => void): this;
1931
+ on(event: 'error', listener: (err: Error) => void): this;
1932
+ // ...and the matching `once` / `off` / `emit`
1933
+ }
1934
+ ```
1935
+
1936
+ | Event | Meaning |
1937
+ |-------|---------|
1938
+ | `close` | the destination is fully written and closed (e.g. a file flushed and its fd closed) — "ready" |
1939
+ | `error` | the destination failed |
1940
+ | `finish` | `end()` was processed and queued data flushed (before `close`) |
1941
+ | `drain` | the buffer drained after backpressure; safe to write more |
1942
+
1943
+ The library calls `write(sample)` **synchronously** per accepted sample (it is not awaited),
1944
+ `end()`s the sink when the client closes, and attaches an `error` listener so a failing sink
1945
+ can't crash the process (it also catches throws from `write`/`end`). The application — which
1946
+ created the sink — listens for `close` (destination ready) and `error`. Because `write` isn't
1947
+ awaited in the `accept()` hot path, **backpressure and batching are the sink's concern**.
1948
+
1949
+ ### Built-in sinks
1950
+
1951
+ ```ts
1952
+ import { Observer, createJsonlFileSinkFactory } from '@observertc/observer-js';
1953
+
1954
+ const observer = new Observer({
1955
+ // one ./stats/<callId>__<clientId>.jsonl per client
1956
+ createClientSink: createJsonlFileSinkFactory({ directory: './stats' }),
1957
+ });
1958
+
1959
+ // React when a sink is created for a client:
1960
+ observer.on('client-sink-created', ({ observedClient, sink }) => {
1961
+ sink.on('close', () => {
1962
+ // the file is fully flushed and its fd closed — ready to upload, move, etc.
1963
+ });
1964
+ });
1965
+ ```
1966
+
1967
+ | Export | Signature | Notes |
1968
+ |--------|-----------|-------|
1969
+ | `createJsonlFileSinkFactory` | `({ directory, flags?, getFileName?, serializeSample? }) => ClientSampleSinkFactory` | per-client JSONL files; path defaults to `${callId}__${clientId}.jsonl` under `directory` (which **must exist**) |
1970
+ | `createJsonlFileSink` | `({ path, flags?, serializeSample? }) => ClientSampleSink` | a single JSONL file; wraps `fs.WriteStream` and re-emits its `close`/`finish`/`drain`/`error` |
1971
+ | `JsonlFileSink` | `class extends ClientSampleSink` | the underlying class; exposes `readonly path` so a `close` handler knows which file is ready |
1972
+ | `createInMemorySink` / `InMemorySink` | `(samples?: ClientSample[]) => InMemorySink` | collects the accepted **sample objects** into `.samples: ClientSample[]`; emits `close` on `end()` |
1973
+
1974
+ `serializeSample?: (sample: ClientSample) => string` overrides the default `JSON.stringify` for
1975
+ the JSONL sinks (e.g. to redact or reshape before writing).
1976
+
1977
+ ### Reading sink-specific info (e.g. the file path)
1978
+
1979
+ The bus hands you the sink as the base `ClientSampleSink`. To read information specific to a sink
1980
+ type — for a file sink, where it was written — **narrow with `instanceof`** and read the sink's
1981
+ public fields. `JsonlFileSink` exposes `path`:
1982
+
1983
+ ```ts
1984
+ import { JsonlFileSink } from '@observertc/observer-js';
1985
+
1986
+ observer.on('client-sink-created', ({ observedClient, sink }) => {
1987
+ if (sink instanceof JsonlFileSink) {
1988
+ const { path } = sink; // the file this client's samples go to
1989
+ sink.once('close', () => uploadFile(path)); // close = flushed & fd closed → ready
1990
+ }
1991
+ });
1992
+ ```
1993
+
1994
+ The general pattern: each concrete sink exposes whatever it wants as `public readonly` fields, and
1995
+ consumers narrow (`instanceof YourSink`) to read them. Your own sinks do the same.
1996
+
1997
+ ### Writing your own sink
1998
+
1999
+ Subclass `ClientSampleSink` and emit the lifecycle events yourself — for any non-file
2000
+ destination (a remote endpoint, a message queue, an object store, …):
2001
+
2002
+ ```ts
2003
+ import { ClientSampleSink, ClientSample, ClientSampleSinkFactory } from '@observertc/observer-js';
2004
+
2005
+ class HttpSink extends ClientSampleSink {
2006
+ private buffer: ClientSample[] = [];
2007
+ constructor(private readonly url: string) { super(); }
2008
+
2009
+ write(sample: ClientSample): boolean {
2010
+ this.buffer.push(sample); // batch; decide your own backpressure
2011
+ return true;
2012
+ }
2013
+ end(): void {
2014
+ fetch(this.url, { method: 'POST', body: JSON.stringify(this.buffer) })
2015
+ .then(() => this.emit('close')) // signal "destination ready"
2016
+ .catch((err) => this.emit('error', err));
2017
+ }
2018
+ }
2019
+
2020
+ const createClientSink: ClientSampleSinkFactory = ({ clientId, observedCall }) =>
2021
+ new HttpSink(`https://stats.example.com/${observedCall.callId}/${clientId}`);
2022
+
2023
+ const observer = new Observer({ createClientSink });
2024
+ ```
2025
+
2026
+ `observedClient.sink?` exposes the created sink; the `client-sink-created` event delivers it on
2027
+ the bus with full ancestry. `ClientSampleSinkFactory` is
2028
+ `(p: { clientId: string; observedCall: ObservedCall }) => ClientSampleSink | undefined`.
2029
+
2030
+ ---
2031
+
2032
+ ## Injecting data into a client
2033
+
2034
+ Sometimes the application holds data that belongs on a client's record but isn't part of the
2035
+ client-reported `ClientSample` — a room id or display name, an application-level event
2036
+ (*"recording started"*), a server-detected issue, an extension stat, or a device/meta item.
2037
+ `ObservedClient` exposes **injection** methods that merge such data into the client's sample stream,
2038
+ so it updates the live model **and** is persisted to the client's
2039
+ [sink](#sinks-per-client-sample-persistence) exactly like sampled data.
2040
+
2041
+ | Method | Adds to the sample's | Surfaces as |
2042
+ |--------|----------------------|-------------|
2043
+ | `injectAttachment(attachments)` | `attachments` (merged via `Object.assign`) | `observedClient.attachments` |
2044
+ | `injectEvent(event: ClientEvent)` | `clientEvents` | `client-event` (plus any state the event drives) |
2045
+ | `injectIssue(issue: ClientIssue)` | `clientIssues` | `client-issue` |
2046
+ | `injectMetaData(meta: ClientMetaData)` | `clientMetaItems` | `client-metadata` |
2047
+ | `injectExtensionStat(stat: ExtensionStat)` | `extensionStats` | `client-extension-stats` |
2048
+
2049
+ ### When the injected data lands
2050
+
2051
+ Injection is timing-aware so nothing is dropped, regardless of *when* you call it:
2052
+
2053
+ - **During a sample's processing** — e.g. from inside a `client-updated` / `client-event` handler,
2054
+ which run within `accept()` — the data is applied to the **current** sample immediately: reflected
2055
+ in entity state and written to the sink as part of that sample.
2056
+ - **Between samples** — the data is buffered and merged into the **next** `accept()`'s sample.
2057
+ - **On `close()` with pending injections and no further sample** — the buffer is flushed as a final
2058
+ synthetic sample (applied to state and written to the sink) before the sink is ended, so a
2059
+ last-moment injection is never lost.
2060
+
2061
+ In every case the injected data both updates the live `ObservedClient` and reaches the per-client
2062
+ sink — the sink always receives the final, **injection-merged** sample (the sink write happens at the
2063
+ end of `accept()`, after the merge).
2064
+
2065
+ ### Example
2066
+
2067
+ ```ts
2068
+ // Enrich at creation from your app's knowledge of the participant. Injecting in `client-added`
2069
+ // (which runs just before the first accept) lands on the first sample.
2070
+ observer.on('client-added', ({ observedClient }) => {
2071
+ observedClient.injectAttachment({ roomId: lookupRoomId(observedClient.clientId) });
2072
+ });
2073
+
2074
+ // Application-level signals at any time:
2075
+ const client = observer.getObservedCall(callId)?.getObservedClient(clientId);
2076
+ client?.injectEvent({ type: 'RECORDING_STARTED', timestamp: Date.now() });
2077
+ client?.injectIssue({ type: 'app-kicked-participant', timestamp: Date.now() });
2078
+ ```
2079
+
2080
+ `attachments` are latest-wins (like sampled `attachments`): injecting a key overwrites its previous
2081
+ value. `appData` is unaffected — injections flow into the sample/telemetry, not the app-owned
2082
+ `appData` bag (see [Ingestion](#ingestion-accept-context--lifecycle)).
2083
+
2084
+ ## Logging
2085
+
2086
+ `observer-js` logs through a single, swappable sink. Out of the box it writes `debug` and
2087
+ above to `console` (verbose — install your own sink for production). Funnel everything into your
2088
+ logger:
2089
+
2090
+ ```ts
2091
+ import { format } from 'node:util';
2092
+ import { setObserverLogger, type ObserverLogger } from '@observertc/observer-js';
2093
+
2094
+ // (module, ...args) — the module name is a separate leading argument, never part
2095
+ // of the message. `args` is a printf-style format string plus its values.
2096
+ setObserverLogger({
2097
+ trace: (m, ...a) => myLogger.trace(`[${m}] ${format(...a)}`),
2098
+ debug: (m, ...a) => myLogger.debug(`[${m}] ${format(...a)}`),
2099
+ info: (m, ...a) => myLogger.info(`[${m}] ${format(...a)}`),
2100
+ warn: (m, ...a) => myLogger.warn(`[${m}] ${format(...a)}`),
2101
+ error: (m, ...a) => myLogger.error(`[${m}] ${format(...a)}`),
2102
+ });
2103
+ ```
2104
+
2105
+ Spreading the arguments directly (`myLogger.warn(prefix, ...a)`) works with `console`, but
2106
+ **not** with loggers whose first parameter is the message — pino, bunyan, roarr. There the
2107
+ module name becomes the whole message and the rest is silently dropped; pass the module as
2108
+ structured metadata (`log.warn({ module }, ...a)`) or collapse it with `util.format` as above.
2109
+
2110
+ `createLogger(moduleName)` is also exported for your own modules. See
2111
+ **[`docs/logging.md`](./docs/logging.md)** for pino / winston / console recipes, level
2112
+ filtering, per-module routing, and full silencing.
2113
+
2114
+ ---
2115
+
2116
+ ## Design notes
2117
+
2118
+ **[`docs/design-notes.md`](./docs/design-notes.md)** covers the reasoning behind the library rather
2119
+ than its API: why client-detectable conditions are never re-derived server-side, why each shipped
2120
+ detector exists, what was deliberately *not* built and why, the WebRTC domain facts that shaped the
2121
+ implementation (ICE-Lite disconnect waves, counter resets, why ICE and RTCP RTT must never be
2122
+ blended), and an operational threshold reference.
2123
+
2124
+ ---
2125
+
2126
+ ## Error-handling philosophy
2127
+
2128
+ The library **warns and degrades; it does not throw** on operational problems:
2129
+
2130
+ - `createObservedCall` / `createObservedClient` on a closed parent → warn + return `undefined`.
2131
+ - Duplicate id → warn + return the **existing** instance.
2132
+ - `accept()` on a closed client → warn + no-op.
2133
+ - Sample missing `callId`/`clientId`, or observer closed → `sample-rejected` event.
2134
+ - A throwing accept-middleware → warn + drop that sample (never crashes `accept()`).
2135
+
2136
+ Therefore `create*` and `getOrCreate*` return `T | undefined`; **guard the result.** The
2137
+ `Middleware` utility's internal invariants (e.g. calling `next()` twice) throw, but those throws
2138
+ are caught by `accept()` and surfaced as a warning.
2139
+
2140
+ ---
2141
+
2142
+ ## Development & extension guide
2143
+
2144
+ ```bash
2145
+ yarn install
2146
+ yarn build # tsup → dist/ (dual ESM .mjs + CJS .js, single entry, .d.ts/.d.mts + sourcemaps)
2147
+ yarn lint # eslint -c .eslintrc.json "src/**/*.ts"
2148
+ yarn typecheck # tsc --noEmit
2149
+ yarn test # jest
2150
+ ```
2151
+
2152
+ The build is driven by [`tsup`](https://tsup.egoist.dev) (config in `tsup.config.ts`): a single
2153
+ entry (`src/index.ts`), dual ESM + CommonJS output to `dist/` (`index.mjs` / `index.js`) with
2154
+ `.d.mts` / `.d.ts` types and sourcemaps, targeting Node 20. CI (`.github/workflows/ci.yml`) runs
2155
+ lint + typecheck + **build** + test on every push/PR.
2156
+
2157
+ **Project layout** (`src/`): `Observer.ts`, `ObservedCall.ts`, `ObservedClient.ts`,
2158
+ `ObservedPeerConnection.ts`, the `Observed*` sub-stat classes, `ObserverEvents.ts` (the typed
2159
+ event map + scope types), `detectors/` (`Detector`, `Detectors`, and one file per detector),
2160
+ `validators/` (`Validator`, `Validators`, one file per validator), `issues/` (`ActiveClientIssue`,
2161
+ `ActiveIssueTracker`, `ActiveIssuesRegistry`, `ObservedClientIssueRegistry`), `scores/`,
2162
+ `resolvers/` (remote-track resolvers), `utils/` (`stats`, `SlidingWindow`, `TrendTester`,
2163
+ `CallHealthAggregator`), `common/` (`logger`, `utils`, `Middleware`), `schema/`
2164
+ (sample/event/meta types), and `sinks/` (the `ClientSampleSink` base + `JsonlFileSink` /
2165
+ `InMemorySink`, re-exported from the package root).
2166
+
2167
+ **Conventions to follow when developing further:**
2168
+
2169
+ - *Single event bus.* New consumer-facing events go in `ObserverEvents.ts` with an object
2170
+ payload `[<Scope> & { …subject }]`, and are emitted via the component's
2171
+ `_notify(type, { ...this.eventScope, …subject })`. Each component has a precomputed
2172
+ `eventScope` field and a thin `_notify` wrapper around the right emitter. Keep purely internal
2173
+ coordination as **local** EventEmitter events (and remember to `off` them on close).
2174
+ - *Warn, don't throw* on operational/edge conditions; return `undefined` where a value can't be produced.
2175
+ - *Counter-reset-safe deltas.* When computing a delta from a cumulative counter, never emit a
2176
+ negative value (guard `curr >= prev`), to survive counter resets / SSRC reuse.
2177
+ - *Explicit accumulation.* The per-sample metric accumulation in `accept()` is intentionally
2178
+ explicit and not abstracted — match that style.
2179
+ - *Detectors are server-side.* Add cross-client detectors on `ObservedCall.detectors`; don't
2180
+ re-implement client-detectable signals.
2181
+
2182
+ **Recipes:**
2183
+
2184
+ - *Add an event:* add the key + payload to `ObserverEvents`; in the owning component call
2185
+ `this._notify('my-event', { ...this.eventScope, subject })`.
2186
+ - *Add a per-stream metric:* add the field to the relevant `Observed*Rtp`/track class, populate
2187
+ it in its `update()` (reset at the top of `update()` if it's per-tick), and read it from a
2188
+ `*-updated` handler.
2189
+ - *Add a detector:* implement `Detector`, register it on `call-added` via
2190
+ `observedCall.detectors.add(...)`, surface findings with `observedCall.addIssue(...)`.
2191
+
2192
+ ---
2193
+
2194
+ ## License
2195
+
2196
+ Apache-2.0. Part of the [ObserveRTC](https://github.com/ObserveRTC) ecosystem.