@observertc/observer-js 1.0.1-ec27289.0 → 1.1.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 (210) hide show
  1. package/README.md +2184 -1
  2. package/dist/index.d.mts +6842 -0
  3. package/dist/index.d.ts +6842 -0
  4. package/dist/index.js +7736 -0
  5. package/dist/index.js.map +1 -0
  6. package/dist/index.mjs +7608 -0
  7. package/dist/index.mjs.map +1 -0
  8. package/package.json +44 -18
  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 -83
  14. package/lib/ObservedCall.d.ts.map +0 -1
  15. package/lib/ObservedCall.js +0 -203
  16. package/lib/ObservedCallSummary.d.ts +0 -18
  17. package/lib/ObservedCallSummary.d.ts.map +0 -1
  18. package/lib/ObservedCallSummary.js +0 -2
  19. package/lib/ObservedCertificate.d.ts +0 -19
  20. package/lib/ObservedCertificate.d.ts.map +0 -1
  21. package/lib/ObservedCertificate.js +0 -38
  22. package/lib/ObservedClient.d.ts +0 -131
  23. package/lib/ObservedClient.d.ts.map +0 -1
  24. package/lib/ObservedClient.js +0 -520
  25. package/lib/ObservedClientEventMonitor.d.ts +0 -89
  26. package/lib/ObservedClientEventMonitor.d.ts.map +0 -1
  27. package/lib/ObservedClientEventMonitor.js +0 -247
  28. package/lib/ObservedClientSummary.d.ts +0 -23
  29. package/lib/ObservedClientSummary.d.ts.map +0 -1
  30. package/lib/ObservedClientSummary.js +0 -2
  31. package/lib/ObservedCodec.d.ts +0 -22
  32. package/lib/ObservedCodec.d.ts.map +0 -1
  33. package/lib/ObservedCodec.js +0 -45
  34. package/lib/ObservedDataChannel.d.ts +0 -30
  35. package/lib/ObservedDataChannel.d.ts.map +0 -1
  36. package/lib/ObservedDataChannel.js +0 -75
  37. package/lib/ObservedIceCandidate.d.ts +0 -29
  38. package/lib/ObservedIceCandidate.d.ts.map +0 -1
  39. package/lib/ObservedIceCandidate.js +0 -59
  40. package/lib/ObservedIceCandidatePair.d.ts +0 -44
  41. package/lib/ObservedIceCandidatePair.d.ts.map +0 -1
  42. package/lib/ObservedIceCandidatePair.js +0 -111
  43. package/lib/ObservedIceTransport.d.ts +0 -36
  44. package/lib/ObservedIceTransport.d.ts.map +0 -1
  45. package/lib/ObservedIceTransport.js +0 -93
  46. package/lib/ObservedInboundRtp.d.ts +0 -92
  47. package/lib/ObservedInboundRtp.d.ts.map +0 -1
  48. package/lib/ObservedInboundRtp.js +0 -193
  49. package/lib/ObservedInboundTrack.d.ts +0 -31
  50. package/lib/ObservedInboundTrack.d.ts.map +0 -1
  51. package/lib/ObservedInboundTrack.js +0 -59
  52. package/lib/ObservedMediaPlayout.d.ts +0 -22
  53. package/lib/ObservedMediaPlayout.d.ts.map +0 -1
  54. package/lib/ObservedMediaPlayout.js +0 -42
  55. package/lib/ObservedMediaSource.d.ts +0 -28
  56. package/lib/ObservedMediaSource.d.ts.map +0 -1
  57. package/lib/ObservedMediaSource.js +0 -55
  58. package/lib/ObservedOutboundRtp.d.ts +0 -62
  59. package/lib/ObservedOutboundRtp.d.ts.map +0 -1
  60. package/lib/ObservedOutboundRtp.js +0 -144
  61. package/lib/ObservedOutboundTrack.d.ts +0 -31
  62. package/lib/ObservedOutboundTrack.d.ts.map +0 -1
  63. package/lib/ObservedOutboundTrack.js +0 -59
  64. package/lib/ObservedPeerConnection.d.ts +0 -185
  65. package/lib/ObservedPeerConnection.d.ts.map +0 -1
  66. package/lib/ObservedPeerConnection.js +0 -710
  67. package/lib/ObservedPeerConnectionTransport.d.ts +0 -17
  68. package/lib/ObservedPeerConnectionTransport.d.ts.map +0 -1
  69. package/lib/ObservedPeerConnectionTransport.js +0 -34
  70. package/lib/ObservedRemoteInboundRtp.d.ts +0 -30
  71. package/lib/ObservedRemoteInboundRtp.d.ts.map +0 -1
  72. package/lib/ObservedRemoteInboundRtp.js +0 -60
  73. package/lib/ObservedRemoteOutboundRtp.d.ts +0 -30
  74. package/lib/ObservedRemoteOutboundRtp.d.ts.map +0 -1
  75. package/lib/ObservedRemoteOutboundRtp.js +0 -60
  76. package/lib/ObservedTURN.d.ts +0 -31
  77. package/lib/ObservedTURN.d.ts.map +0 -1
  78. package/lib/ObservedTURN.js +0 -58
  79. package/lib/ObservedTurnServer.d.ts +0 -25
  80. package/lib/ObservedTurnServer.d.ts.map +0 -1
  81. package/lib/ObservedTurnServer.js +0 -59
  82. package/lib/Observer.d.ts +0 -61
  83. package/lib/Observer.d.ts.map +0 -1
  84. package/lib/Observer.js +0 -156
  85. package/lib/ObserverSummary.d.ts +0 -13
  86. package/lib/ObserverSummary.d.ts.map +0 -1
  87. package/lib/ObserverSummary.js +0 -2
  88. package/lib/common/Middleware.d.ts +0 -17
  89. package/lib/common/Middleware.d.ts.map +0 -1
  90. package/lib/common/Middleware.js +0 -60
  91. package/lib/common/SingleExecutor.d.ts +0 -3
  92. package/lib/common/SingleExecutor.d.ts.map +0 -1
  93. package/lib/common/SingleExecutor.js +0 -31
  94. package/lib/common/logger.d.ts +0 -17
  95. package/lib/common/logger.d.ts.map +0 -1
  96. package/lib/common/logger.js +0 -50
  97. package/lib/common/types.d.ts +0 -3
  98. package/lib/common/types.d.ts.map +0 -1
  99. package/lib/common/types.js +0 -3
  100. package/lib/common/utils.d.ts +0 -11
  101. package/lib/common/utils.d.ts.map +0 -1
  102. package/lib/common/utils.js +0 -63
  103. package/lib/detectors/Detector.d.ts +0 -5
  104. package/lib/detectors/Detector.d.ts.map +0 -1
  105. package/lib/detectors/Detector.js +0 -3
  106. package/lib/detectors/Detectors.d.ts +0 -11
  107. package/lib/detectors/Detectors.d.ts.map +0 -1
  108. package/lib/detectors/Detectors.js +0 -34
  109. package/lib/index.d.ts +0 -27
  110. package/lib/index.d.ts.map +0 -1
  111. package/lib/index.js +0 -47
  112. package/lib/monitors/TurnUsageMonitor.d.ts +0 -1
  113. package/lib/monitors/TurnUsageMonitor.d.ts.map +0 -1
  114. package/lib/monitors/TurnUsageMonitor.js +0 -146
  115. package/lib/schema/ClientEventTypes.d.ts +0 -100
  116. package/lib/schema/ClientEventTypes.d.ts.map +0 -1
  117. package/lib/schema/ClientEventTypes.js +0 -38
  118. package/lib/schema/ClientMetaTypes.d.ts +0 -34
  119. package/lib/schema/ClientMetaTypes.d.ts.map +0 -1
  120. package/lib/schema/ClientMetaTypes.js +0 -16
  121. package/lib/schema/ClientSample.d.ts +0 -1333
  122. package/lib/schema/ClientSample.d.ts.map +0 -1
  123. package/lib/schema/ClientSample.js +0 -4
  124. package/lib/scores/CalculatedScore.d.ts +0 -6
  125. package/lib/scores/CalculatedScore.d.ts.map +0 -1
  126. package/lib/scores/CalculatedScore.js +0 -2
  127. package/lib/scores/DefaultCallScoreCalculator.d.ts +0 -7
  128. package/lib/scores/DefaultCallScoreCalculator.d.ts.map +0 -1
  129. package/lib/scores/DefaultCallScoreCalculator.js +0 -21
  130. package/lib/scores/ScoreCalculator.d.ts +0 -4
  131. package/lib/scores/ScoreCalculator.d.ts.map +0 -1
  132. package/lib/scores/ScoreCalculator.js +0 -2
  133. package/lib/updaters/CallUpdater.d.ts +0 -5
  134. package/lib/updaters/CallUpdater.d.ts.map +0 -1
  135. package/lib/updaters/CallUpdater.js +0 -2
  136. package/lib/updaters/ObserverUpdater.d.ts +0 -5
  137. package/lib/updaters/ObserverUpdater.d.ts.map +0 -1
  138. package/lib/updaters/ObserverUpdater.js +0 -2
  139. package/lib/updaters/OnAllCallObserverUpdater_.d.ts +0 -13
  140. package/lib/updaters/OnAllCallObserverUpdater_.d.ts.map +0 -1
  141. package/lib/updaters/OnAllCallObserverUpdater_.js +0 -38
  142. package/lib/updaters/OnAllClientCallUpdater.d.ts +0 -13
  143. package/lib/updaters/OnAllClientCallUpdater.d.ts.map +0 -1
  144. package/lib/updaters/OnAllClientCallUpdater.js +0 -37
  145. package/lib/updaters/OnAnyCallObserverUpdater_.d.ts +0 -11
  146. package/lib/updaters/OnAnyCallObserverUpdater_.d.ts.map +0 -1
  147. package/lib/updaters/OnAnyCallObserverUpdater_.js +0 -27
  148. package/lib/updaters/OnAnyClientCallUpdater.d.ts +0 -11
  149. package/lib/updaters/OnAnyClientCallUpdater.d.ts.map +0 -1
  150. package/lib/updaters/OnAnyClientCallUpdater.js +0 -27
  151. package/lib/updaters/OnIntervalUpdater.d.ts +0 -10
  152. package/lib/updaters/OnIntervalUpdater.d.ts.map +0 -1
  153. package/lib/updaters/OnIntervalUpdater.js +0 -17
  154. package/lib/updaters/Updater.d.ts +0 -6
  155. package/lib/updaters/Updater.d.ts.map +0 -1
  156. package/lib/updaters/Updater.js +0 -2
  157. package/lib/utils/RemoteTrackResolver.d.ts +0 -7
  158. package/lib/utils/RemoteTrackResolver.d.ts.map +0 -1
  159. package/lib/utils/RemoteTrackResolver.js +0 -2
  160. package/src/ObservedCall.ts +0 -322
  161. package/src/ObservedCallSummary.ts +0 -22
  162. package/src/ObservedCertificate.ts +0 -43
  163. package/src/ObservedClient.ts +0 -673
  164. package/src/ObservedClientEventMonitor.ts +0 -301
  165. package/src/ObservedClientSummary.ts +0 -24
  166. package/src/ObservedCodec.ts +0 -49
  167. package/src/ObservedDataChannel.ts +0 -85
  168. package/src/ObservedIceCandidate.ts +0 -64
  169. package/src/ObservedIceCandidatePair.ts +0 -118
  170. package/src/ObservedIceTransport.ts +0 -99
  171. package/src/ObservedInboundRtp.ts +0 -205
  172. package/src/ObservedInboundTrack.ts +0 -73
  173. package/src/ObservedMediaPlayout.ts +0 -49
  174. package/src/ObservedMediaSource.ts +0 -60
  175. package/src/ObservedOutboundRtp.ts +0 -156
  176. package/src/ObservedOutboundTrack.ts +0 -72
  177. package/src/ObservedPeerConnection.ts +0 -1019
  178. package/src/ObservedPeerConnectionTransport.ts +0 -38
  179. package/src/ObservedRemoteInboundRtp.ts +0 -64
  180. package/src/ObservedRemoteOutboundRtp.ts +0 -65
  181. package/src/ObservedTURN.ts +0 -86
  182. package/src/ObservedTurnServer.ts +0 -72
  183. package/src/Observer.ts +0 -239
  184. package/src/ObserverSummary.ts +0 -15
  185. package/src/common/Middleware.ts +0 -84
  186. package/src/common/SingleExecutor.ts +0 -36
  187. package/src/common/logger.ts +0 -82
  188. package/src/common/types.ts +0 -4
  189. package/src/common/utils.ts +0 -69
  190. package/src/detectors/Detector.ts +0 -6
  191. package/src/detectors/Detectors.ts +0 -38
  192. package/src/index.ts +0 -28
  193. package/src/monitors/TurnUsageMonitor.ts +0 -187
  194. package/src/schema/ClientEventTypes.ts +0 -112
  195. package/src/schema/ClientMetaTypes.ts +0 -41
  196. package/src/schema/ClientSample.ts +0 -1682
  197. package/src/scores/CalculatedScore.ts +0 -6
  198. package/src/scores/DefaultCallScoreCalculator.ts +0 -23
  199. package/src/scores/ScoreCalculator.ts +0 -3
  200. package/src/updaters/CallUpdater.ts +0 -5
  201. package/src/updaters/ObserverUpdater.ts +0 -5
  202. package/src/updaters/OnAllCallObserverUpdater_.ts +0 -46
  203. package/src/updaters/OnAllClientCallUpdater.ts +0 -46
  204. package/src/updaters/OnAnyCallObserverUpdater_.ts +0 -32
  205. package/src/updaters/OnAnyClientCallUpdater.ts +0 -32
  206. package/src/updaters/OnIntervalUpdater.ts +0 -19
  207. package/src/updaters/Updater.ts +0 -5
  208. package/src/utils/RemoteTrackResolver.ts +0 -12
  209. package/tsconfig.json +0 -19
  210. package/tslint.json +0 -6
package/README.md CHANGED
@@ -1,2 +1,2185 @@
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
+ | `ClientPopulationIssueDetector` | | **observer** | `CLIENT_POPULATION_ISSUE` |
1130
+ | `SfuCongestionDetector` | | **observer** | `sfu-congestion` |
1131
+ | `TurnServerHealthDetector` | | **observer** | `TURN_SERVER_DEGRADED` |
1132
+ | `TurnServerOutageDetector` | | **observer** | `TURN_SERVER_OUTAGE` |
1133
+
1134
+ What each adds that no endpoint can know:
1135
+
1136
+ - **`CallConcurrentIssueDetector`** — *who else in this meeting is in this state right now?* The
1137
+ difference between "one person's Wi-Fi" and "this room is broken".
1138
+ - **`ObserverConcurrentIssueDetector`** — *is our infrastructure in trouble?* A **separate class**,
1139
+ not the call one with a bigger denominator, because it is a different question with different
1140
+ gates. It requires the group to span at least `minAffectedCalls` **independent calls** (default
1141
+ `2`) and raises its own `CROSS_CALL_*` types. Without that gate, one thirty-person meeting where
1142
+ everyone is congested clears every client threshold and pages you for a single bad room the
1143
+ call-scoped detector already reported. Clients in different calls share no room, no publisher and
1144
+ no host — only the servers, which is what makes the finding conclusive. Note there is deliberately
1145
+ **no participant ratio** at this scope: six broken calls out of forty is a small share of all
1146
+ clients, and a ratio gate would hide exactly the event you want.
1147
+ - **`IssueFanOutDetector`** — *does this issue follow one published source, or one receiver?*
1148
+ - **`PublisherFaultCorroborationDetector`** — *do **both ends** of one track agree the source is at
1149
+ fault?* Fan-out sees one end and infers; this sees the publisher reporting `encoder-bottleneck`
1150
+ about its own send path *while* its subscribers report `freezed-video-track` about receiving it.
1151
+ Two independent parties, one conclusion, nothing left to deduce — hence the highest confidence in
1152
+ the library. Run both: fan-out is broader and catches the case where the publisher is fine and the
1153
+ SFU's forwarding is not.
1154
+ - **`ClientPopulationIssueDetector`** — *is this concentrated on one **kind of client**?* The one
1155
+ correlation here that is neither per-call nor per-server. Every other observer-scoped detector
1156
+ reasons "clients in unrelated calls share only the infrastructure, so it must be us" — right for
1157
+ network symptoms, **wrong for endpoint ones**. `cpulimitation` across six unrelated calls is not an
1158
+ SFU event; CPU is owned by the endpoint, so what those endpoints share is a browser version or a
1159
+ client release. Groups by `browser` / `engine` / `platform` / `operationSystem` / `location`, one
1160
+ axis per instance. The gate is **relative risk**, not share: "30% of Chrome 141 is unhappy" means
1161
+ nothing if 30% of everyone is, and a share-based rule simply indicts whichever browser is most
1162
+ popular. See [the `location` axis](#grouping-by-place-the-location-axis) for the geographic form.
1163
+ - **`SfuCongestionDetector`** — *is congestion spiking across the fleet right now?* Counts distinct
1164
+ clients reporting congestion in fixed wall-clock buckets and compares each bucket against a
1165
+ median+MAD baseline of the ones before it. Buckets rather than update ticks on purpose: the tick is
1166
+ unevenly spaced and shorter than a client's sampling period, so counting on it compares windows of
1167
+ different lengths and calls the difference a signal. Only add it when the observer's calls all come
1168
+ from the **same SFU** — the finding's meaning is "these clients share only that server".
1169
+ - **`TrackDeliveryMismatchDetector`** — *are the two ends of a track disagreeing?*
1170
+ - **`UnconsumedTrackDetector`** — *is anyone actually subscribed?* (reads the resolver's silence)
1171
+ - **`TurnServerHealthDetector`** — *does trouble cluster on one relay?*
1172
+ - **`TurnServerOutageDetector`** — covers the case the health detector structurally cannot. The
1173
+ health detector groups clients by the server relaying them and asks how many report issues — it
1174
+ needs clients *on* the server to ask. When a TURN server dies, allocation fails: existing sessions
1175
+ drop and new clients never obtain a relay candidate through it, so they are never attributed to it
1176
+ at all. Its population goes to zero and the health detector falls silent for the worst possible
1177
+ reason. **Degradation makes clients unhappy; an outage makes them disappear.** Absence is a
1178
+ dangerous signal, so the **control group** is the heart of the design: a call ending, everyone
1179
+ leaving at 6pm, and a fleet-wide network event all look identical to an outage. It refuses to blame
1180
+ a server unless clients *not* relayed through it are demonstrably still connected
1181
+ (`requireControlGroup`, on by default).
1182
+
1183
+ #### There is no ICE detector
1184
+
1185
+ ICE trouble is reported by `client-monitor-js` >= 4.6.0 as the keyed issues `ice-disconnected`,
1186
+ `ice-connection-failed`, `ice-transport-stalled` and `unstable-ice-path`, each with hysteresis and
1187
+ multi-signal confirmation behind it. An `IceDisruptionDetector` used to re-derive that server-side
1188
+ from raw state transitions; it has been removed, because the server sees less and guesses more. The
1189
+ client knows whether `disconnected` persisted or healed in 200 ms; the observer does not.
1190
+
1191
+ Correlating ICE trouble is now configuration, not a class:
1192
+
1193
+ ```ts
1194
+ observer.addObserverDetector('observer-concurrent-issue-detector', {
1195
+ issueTypes: [ 'ice-disconnected', 'ice-connection-failed', 'ice-transport-stalled' ],
1196
+ });
1197
+ ```
1198
+
1199
+ ### Grouping by place: the `location` axis
1200
+
1201
+ If your clients report coordinates, `ClientPopulationIssueDetector` can group by **where they are**
1202
+ instead of what they run — which is the grouping network symptoms actually cluster by:
1203
+
1204
+ ```ts
1205
+ observer.addObserverDetector('client-population-issue-detector', {
1206
+ issueTypes: [ 'congestion', 'ice-disconnected' ],
1207
+ groupBy: 'location',
1208
+ locationPrecision: 3, // geohash chars: 3 ~156 km, 4 ~39 km, 5 ~5 km
1209
+ resolveClientLocation: (client) => client.attachments?.geo as { latitude: number, longitude: number },
1210
+ });
1211
+ ```
1212
+
1213
+ **The client still owns "RTT jumped".** client-monitor's `CongestionDetector` compares each peer
1214
+ connection's RTT against its own EWMA baseline and requires a bandwidth-limitation corroboration
1215
+ before raising `congestion`. Absolute RTT is not comparable between clients — someone 200 ms away is
1216
+ *always* 200 ms away, so the only signal is deviation from that client's own baseline, which is
1217
+ exactly what the client measures. The observer's contribution is the part no endpoint can see: that
1218
+ many of the affected clients are **in the same place at the same time**.
1219
+
1220
+ Three things to know:
1221
+
1222
+ - **Cells, not radii.** The population is a geohash prefix. "Within N km" is a clustering problem —
1223
+ order-dependent, no stable group name, pairwise cost — and a detector needs the *same* group key on
1224
+ every tick for its cooldown and control group to mean anything. The cost is that a cell boundary can
1225
+ split two adjacent clients, which biases towards missing a finding rather than inventing one.
1226
+ - **Only the cell key is reported.** `payload.population` is the geohash; coordinates never enter the
1227
+ issue. These payloads get archived into [call summaries](#call-summaries), so that matters.
1228
+ - **Geography is confounded with your topology.** The control group is "everyone outside this cell",
1229
+ which cannot separate *"the path into this region degraded"* from *"the SFU serving this region
1230
+ degraded"*. If a region maps largely onto one deployment, both hypotheses fit the same evidence —
1231
+ so the finding concludes `infrastructure` and points you at `SfuCongestionDetector` /
1232
+ `TurnServerHealthDetector`, which answer whether clients elsewhere on the same server also
1233
+ degraded. It does not claim an attribution it cannot support.
1234
+
1235
+ Coordinates are not in `ClientSample`, so `resolveClientLocation` is required; without it the detector
1236
+ logs a warning at construction and finds nothing, rather than quietly reporting no findings forever.
1237
+
1238
+ ### Validators — one-shot structural checks
1239
+
1240
+ Every detector above answers *"is something wrong right now?"* and runs on every tick, because the
1241
+ answer legitimately changes. A **validator** answers *"is this deployment built correctly?"* — which
1242
+ only changes when you deploy. So it is not configured on and left running: you **start** one, it runs
1243
+ until it can decide, reports once, and the observer drops it.
1244
+
1245
+ ```ts
1246
+ observer.addValidator('simulcast-receivers', { minChecks: 5 });
1247
+
1248
+ observer.on('validation-ready', ({ validator, report }) => {
1249
+ if (!report.ready) return;
1250
+ if (report.verdict === 'layer-decided-lowest-common-denominator') page(validator, report);
1251
+ });
1252
+
1253
+ onDeploy(() => observer.addValidator('simulcast-receivers')); // check again
1254
+ ```
1255
+
1256
+ `observer.validators` is the set currently running — normally empty, since each removes itself on
1257
+ finishing. There is no revalidation timer: a deploy, not elapsed time, is what makes a structural
1258
+ verdict stale, so re-checking means starting another.
1259
+
1260
+ **Cancelling.** A check that has not decided can be stopped, by name or by instance:
1261
+
1262
+ ```ts
1263
+ observer.cancelValidator('simulcast-receivers', 'sfu redeployed');
1264
+
1265
+ // or one specific instance — `observer.validators` holds what is running
1266
+ for (const validator of observer.validators) observer.cancelValidator(validator, 'shutting down');
1267
+ ```
1268
+
1269
+ Cancelling is **not** silent discarding. The validator finishes `inconclusive` with your reason,
1270
+ emits `validation-ready` like any other completion, and removes itself. That matters twice over:
1271
+ anything waiting on the verdict would otherwise wait forever, and *"we stopped asking"* is a
1272
+ materially different outcome from *"we asked and learned nothing"* — which is exactly what an
1273
+ `inconclusive` carrying a reason records. Pass a real reason; the default tells the reader nothing
1274
+ they could not already infer. `observer.close()` cancels whatever is still running with
1275
+ `'observer closed'`.
1276
+
1277
+ | Validator | `addValidator` name | Question | Also raises |
1278
+ |-----------|---------------------|----------|-------------|
1279
+ | `SimulcastReceiverValidator` 🔗 | `simulcast-receivers` | Does the SFU pick layers per receiver, or drag the publisher down to the worst one? | `WORST_RECEIVER_CONTAGION` |
1280
+ | `RemoteTrackResolverValidator` | `remote-track-resolver` | Is the resolver actually linking anything? | `REMOTE_TRACK_LINKS_UNRESOLVED` |
1281
+ | `CodecConsistencyValidator` | `codec-consistency` | Is everyone on the same codec — and is it the one you think you negotiated? | `CODEC_INCONSISTENCY` |
1282
+
1283
+ **`SimulcastReceiverValidator`** — simulcast (or SVC) exists so one slow
1284
+ participant doesn't set everyone's quality: with several encodings the server hands the struggling
1285
+ receiver a lower layer and leaves the rest alone. Without it — or with a server that relays RTCP end
1286
+ to end, so the publisher's bandwidth estimate collapses to the slowest receiver — the only way to
1287
+ serve them is to make the *source* send less. Both causes look identical from outside; what the check
1288
+ establishes is whether per-receiver adaptation happens at all.
1289
+
1290
+ | `verdict` | meaning |
1291
+ |-----------|---------|
1292
+ | `layer-decided-per-receiver` | verified — a receiver fell far behind and the publisher carried on |
1293
+ | `layer-decided-lowest-common-denominator` | the publisher followed its worst receiver; everyone gets the slowest participant's quality |
1294
+ | `inconclusive` | cancelled, or the observer closed, before it could decide |
1295
+
1296
+ **Not finishing is not a pass.** The check only runs when a publisher has 3+ receivers and one is at
1297
+ most half the median; plenty of healthy deployments never present that. A validator that never sees it
1298
+ simply keeps running and never reports — it does not quietly succeed. `report.checks` counts the times
1299
+ the check genuinely ran, so an `inconclusive` with `checks: 0` says plainly that nothing was verified.
1300
+
1301
+ **`RemoteTrackResolverValidator`** exists because of a specific, nasty failure mode. Four things here
1302
+ are built on publisher↔subscriber links — `IssueFanOutDetector`,
1303
+ `PublisherFaultCorroborationDetector`, `TrackDeliveryMismatchDetector`, `UnconsumedTrackDetector`
1304
+ (and `SimulcastReceiverValidator`) — and every one of them correctly does *nothing* when the links
1305
+ are missing rather than guessing. So a resolver wired to the wrong id field leaves all of them
1306
+ permanently silent, and **silence is what a healthy deployment looks like too**: you would conclude
1307
+ your calls were clean when in fact nothing was ever examined. Verdicts: `links-resolved` /
1308
+ `no-links-resolved` / `inconclusive`. Run it at start-up and after changing the resolver or the SFU's
1309
+ id scheme.
1310
+
1311
+ **`CodecConsistencyValidator`** answers two things at once. A **split** — participants of one call on
1312
+ different codecs — is a real fault with a confusing symptom: an SFU that forwards without transcoding
1313
+ cannot serve them all, so some pairs see each other and some do not, with no error anywhere. Only
1314
+ something holding every participant at once can see it. The quieter half is the silent fallback: a
1315
+ deployment configured for VP9 or AV1 drops to VP8 whenever one endpoint cannot negotiate the
1316
+ preference, the call keeps working at a higher bitrate than budgeted, and the team believes it
1317
+ shipped AV1 months ago. Give it `expected` and it says so. Verdicts: `codec-consistent` /
1318
+ `codec-split` / `unexpected-codec` / `inconclusive`.
1319
+
1320
+ ```ts
1321
+ observer.addValidator('remote-track-resolver');
1322
+ observer.addValidator('codec-consistency', { expected: { video: 'video/VP9', audio: 'audio/opus' } });
1323
+ ```
1324
+
1325
+ #### `CallIssue` vs `ObserverIssue`
1326
+
1327
+ Server-raised findings come in two kinds, distinguished by the scope that raised them:
1328
+
1329
+ | | raised by | delivered as | `scope` |
1330
+ |---|---|---|---|
1331
+ | `CallIssue` | `observedCall.addIssue(...)` | `call-issue` | `'call'` |
1332
+ | `ObserverIssue` | `observer.addIssue(...)` | `observer-issue` | `'observer'` |
1333
+
1334
+ Both share `IssueBase` — `type`, `timestamp`, `conclusion?`, `payload?` — and `Issue` is the union,
1335
+ discriminated on `scope`.
1336
+
1337
+ ```ts
1338
+ observer.on('call-issue', ({ observedCall, issue }) => {
1339
+ issue.scope; // 'call'
1340
+ observedCall.callId; // the call — NOT repeated in the payload
1341
+ issue.conclusion?.faultDomain;
1342
+ issue.payload; // evidence only
1343
+ });
1344
+
1345
+ observer.on('observer-issue', ({ issue }) => {
1346
+ issue.scope; // 'observer'
1347
+ });
1348
+ ```
1349
+
1350
+ `scope` is stamped by `addIssue` rather than asked of the detector: it is a fact about *where the
1351
+ finding was raised*, which the entity knows and a detector should not have to restate. Having it on
1352
+ the issue — not merely implied by which event fired — keeps a finding self-describing once it leaves
1353
+ the bus, into a shared handler, a log line or a queue.
1354
+
1355
+ **The payload is evidence and nothing else.** It no longer repeats `type`, `scope`, or the `callId`
1356
+ already carried by the event, and `conclusion` was lifted out of it to a first-class field. A payload
1357
+ that restates its own envelope invites the two to disagree — and they did, because nothing kept them
1358
+ in step. `payload` is always an object (the `string` form is gone, along with `issuePayloadOf`); use
1359
+ `issuePayloadAsString(issue)` at a boundary that genuinely needs text.
1360
+
1361
+ #### Conclusions
1362
+
1363
+ Every issue-driven finding carries a `conclusion` — the interpretation step, so the person reading
1364
+ the alert doesn't have to perform it. It sits **beside** the evidence, not inside it:
1365
+
1366
+ ```jsonc
1367
+ {
1368
+ "type": "CROSS_CALL_ISSUE_ONSET_BURST",
1369
+ "scope": "observer",
1370
+ "timestamp": 1739812345678,
1371
+ "conclusion": {
1372
+ "faultDomain": "infrastructure",
1373
+ "summary": "network congestion is open across independent calls at the same time — 6 of 40 calls (11/300 clients)",
1374
+ "recommendation": "check SFU egress bandwidth and host network saturation before looking at any single participant",
1375
+ "confidence": 0.85
1376
+ },
1377
+ "payload": {
1378
+ "issueType": "congestion",
1379
+ "calls": 40, "affectedCalls": 6,
1380
+ "perCall": [ { "callId": "…", "affectedClients": 4, "totalClients": 9 } ]
1381
+ }
1382
+ }
1383
+ ```
1384
+
1385
+ `faultDomain` is one of `infrastructure`, `call`, `published-track`, `endpoint`, `client-population`
1386
+ or `unknown`, and it comes from the **spread**, not the issue type — congestion in one call is a
1387
+ meeting problem, congestion in six calls is a server problem, and the client reported the identical
1388
+ symptom in both.
1389
+
1390
+ One case is worth knowing about because it inverts the usual reading: **`cpu-limitation` spread
1391
+ across many independent calls concludes `client-population`, not `infrastructure`.** Endpoint CPU is
1392
+ owned by the endpoint, so breadth there points at what those endpoints share — a recent client
1393
+ release, a browser version, shared VDI hardware — and paging the SFU on-call would be wrong. The
1394
+ conclusion table encodes that so nobody has to rediscover it during an incident.
1395
+
1396
+ Unknown issue types (your own custom client detectors) still produce a structurally valid conclusion
1397
+ from the spread alone; they just get generic wording.
1398
+
1399
+ Two functions are exported, one per scope: `concludeCallIssue()` and `concludeObserverIssue()`. They
1400
+ are separate because a detector already knows its scope, and a single generic function forced every
1401
+ caller to pass the other scope's fields as placeholders — call-scoped detectors passing
1402
+ `affectedCalls: 1, totalCalls: 1` forever, observer-scoped ones passing a participant ratio that was
1403
+ deliberately never read. Placeholders like that invite being read as if they meant something.
1404
+
1405
+ #### Cost
1406
+
1407
+ Detectors run inside `call.update()`, on your event loop, so their cost matters. Two things keep it
1408
+ off the participant axis:
1409
+
1410
+ - **Issues are pushed, not polled.** A detector holds only what the registry handed it, so an
1411
+ `update()` that finds `size === 0` — the overwhelmingly common case — costs one comparison,
1412
+ whatever the participant count. Nothing iterates clients looking for trouble.
1413
+ - **So are unconsumed tracks.** `observedCall.unconsumedOutboundTracks` is maintained by the resolver
1414
+ as tracks gain and lose subscribers, so `UnconsumedTrackDetector` reads a set that is normally
1415
+ empty instead of walking every published track (529 µs → 65 µs per tick at 1 200 tracks).
1416
+ - **Track lookups start from the affected minority.** A detector resolving an issue to its published
1417
+ track searches the *reporting client's* peer connections (typically one or two), not the call.
1418
+
1419
+ At 20 calls × 12 participants (2 640 subscriptions) the whole detector pass costs ~1.3 ms per tick.
1420
+ `yarn bench` prints a per-detector breakdown for your own shape.
1421
+
1422
+ #### Worked examples
1423
+
1424
+ [`examples/detectors.ts`](./examples/detectors.ts) (`yarn example:detectors`) runs one scenario per
1425
+ detector — the question it answers, its full config, the synthetic traffic that makes it fire, and
1426
+ the finding with its conclusion. It asserts every expected finding is produced, so it doubles as a
1427
+ smoke test. [`examples/sfu-observer.ts`](./examples/sfu-observer.ts) (`yarn example`) is the end-to-end
1428
+ tour instead: ingest → correlate → react, with the mediasoup wiring alongside.
1429
+
1430
+ #### `TrackDeliveryMismatchDetector` — resolving an ambiguous symptom
1431
+
1432
+ A dry track ("no bytes are arriving") is the clearest symptom there is and, on its own, completely
1433
+ ambiguous. A receiver seeing silence cannot distinguish *the camera was switched off* from *the SFU
1434
+ stopped forwarding* from *my own consumer wedged* — all three look identical from the browser.
1435
+
1436
+ Joining the two ends of the published track resolves it:
1437
+
1438
+ | publisher | subscribers | verdict |
1439
+ |---|---|---|
1440
+ | sending | **all** dry | `PUBLISHED_TRACK_NOT_DELIVERED` — the forwarding path |
1441
+ | sending | **some** dry | `RECEIVER_TRACK_NOT_DELIVERED` — those consumers (in mediasoup: recreate them) |
1442
+ | dry | any dry | `PUBLISHER_TRACK_DRY` — the source stopped; **not** an SFU fault |
1443
+
1444
+ The publisher side is judged from both available signals: its own `dry-outbound-track` issue when the
1445
+ client reports one, and the observed outbound RTP (`deltaPacketsSent`) as fallback and corroboration.
1446
+ That combination is what makes the first row trustworthy — the server can state that packets
1447
+ demonstrably left the publisher during the same interval in which every receiver got nothing.
1448
+
1449
+ This is the "SFU forwarding mismatch" check, and it needs **no mediasoup instrumentation at all** —
1450
+ the clients' own dry-track verdicts plus the resolver links are sufficient.
1451
+
1452
+ #### `UnconsumedTrackDetector` — reading the resolver's silence
1453
+
1454
+ The one detector where the *absence* of links is the signal: a track still pushing packets whose
1455
+ `remoteInboundTracks` set is empty, i.e. uplink and SFU ingress spent on media nobody receives
1456
+ (everyone has the publisher hidden, a simulcast layer no viewer selects, or an app that forgot to
1457
+ stop a track). It waits `minUnconsumedDurationInMs` first, since a gap between publishing and the
1458
+ first subscription is normal at join time.
1459
+
1460
+ Note the trap this one has to guard against, and why it checks `call.remoteTrackResolver` at runtime
1461
+ rather than trusting the flag alone: **"no subscribers" and "no resolver configured" produce the
1462
+ identical observation.** Without a resolver it would report every published track in the call as
1463
+ unconsumed.
1464
+
1465
+ ## Call summaries
1466
+
1467
+ Everything else in this library is about *now*. Detectors answer "is something wrong right now",
1468
+ validators answer a structural question once, and both read state the call throws away when it ends.
1469
+ A **call summary** is the one thing that outlives the call: who was in it, what was raised against
1470
+ it, how it scored — the questions asked *after* the meeting, by support, by billing, by whoever is
1471
+ writing the incident note.
1472
+
1473
+ It is configured on the observer, at construction:
1474
+
1475
+ ```ts
1476
+ const observer = new Observer({
1477
+ callSummary: {
1478
+ include: [ 'clients', 'issues', 'turnServers', 'scores' ],
1479
+ },
1480
+ });
1481
+
1482
+ observer.on('call-summary', ({ summary }) => archive(summary));
1483
+ ```
1484
+
1485
+ Omit `callSummary`, or set it to `null`, and there are no summaries and **not one extra bus
1486
+ subscription**. Pass an object — `{}` is valid — and every call this observer creates carries one.
1487
+
1488
+ > **Why construction-time, when detectors are added per call?** A summary is a record of what
1489
+ > happened, and a record you can switch on halfway through is a record with a hole in it. Calls that
1490
+ > started before the switch would carry different sections from calls that started after, with
1491
+ > nothing on either to say which. One shape for every call, or none.
1492
+
1493
+ ### Sections are opt-in, and absence means "not collected"
1494
+
1495
+ `include` picks from four built-ins, and **the default is `[]`** — none of them:
1496
+
1497
+ | Section | Contains |
1498
+ |---------|----------|
1499
+ | `clients` | `clientIds` (join order), `peak`, `joined`, `left`. Identifiers and counts only |
1500
+ | `issues` | `CallIssue[]`, in the order raised, capped by `maxIssues` |
1501
+ | `turnServers` | `serverUrls` that carried media, and `clientsRelayed` |
1502
+ | `scores` | `min` / `max` / `median` of the call score, and `samples` |
1503
+
1504
+ **A missing section means it was never collected — never "nothing happened".** Reading
1505
+ `summary.issues === undefined` as "this call was clean" is the one misreading this type invites, so
1506
+ there is no default-empty section to make it easy. This is the same rule as `inconclusive` on a
1507
+ validator: silence is not success.
1508
+
1509
+ The `clients` section is deliberately identifiers and counts. Anything *about* a client — browser,
1510
+ platform, region — is already on `observedClient` while the call is live, and belongs in
1511
+ `attachments` via an enricher if you want it kept; see below.
1512
+
1513
+ ### Enrichers: fold in anything, from any call-scoped event
1514
+
1515
+ ```ts
1516
+ new Observer({
1517
+ callSummary: {
1518
+ include: [ 'issues' ],
1519
+ enrich: {
1520
+ 'client-joined': (summary, { observedClient }) => {
1521
+ // serialisable facts only — the region string, never the live object it came from
1522
+ ((summary.attachments.regions ??= []) as string[]).push(String(observedClient.appData.region));
1523
+ },
1524
+ },
1525
+ },
1526
+ });
1527
+ ```
1528
+
1529
+ Each enricher is typed against its own event's payload. Only **call-scoped** events are accepted —
1530
+ the ones carrying an `observedCall`. An enricher on `observer-issue` or `validation-ready` will not
1531
+ compile, because there is no single call to attribute a fleet-wide fact to, and quietly writing it
1532
+ into every open summary would be worse than a type error.
1533
+
1534
+ The library never writes to `summary.attachments`, so nothing you put there can collide with a
1535
+ section added in a future version.
1536
+
1537
+ > **Why `attachments` and not `appData`.** `appData` is live working state hung off an entity for
1538
+ > that entity's lifetime, and it may hold things that cannot be serialised — a mediasoup router, a
1539
+ > socket. A summary is the opposite: it outlives the call so it can be **shipped**, and it reaches
1540
+ > you on `call-summary` while the call it describes is being torn down, so an unserialisable value
1541
+ > in it points at something already gone. Same contract as `attachments` on a `ClientSample`: read
1542
+ > the live object off `observedCall` / `observedClient` in the enricher, attach what serialises —
1543
+ > the router's `id`, not the router. An enricher that throws is logged and skipped — a summary is a
1544
+ side-channel, and nothing about a call should break because a field could not be recorded.
1545
+
1546
+ ### Caps announce what they dropped
1547
+
1548
+ `maxIssues` (default `500`) and `maxClientIds` (default `10_000`) bound the two unbounded lists.
1549
+ When either bites, `summary.truncated` appears with the shortfall — present **only** when something
1550
+ was actually dropped. That is what makes dropping safe: the true count is recoverable as
1551
+ `issues.length + (truncated?.issues ?? 0)`. A silently truncated summary is worse than no summary,
1552
+ because someone will count `issues.length` and report it as the issue count.
1553
+
1554
+ `issues` is the plain array, with no derived tallies alongside it. A count is `issues.length` and a
1555
+ per-type count is one `filter` — both cheaper at the call site than kept correct here.
1556
+
1557
+ ### Reading it
1558
+
1559
+ `observedCall.summary` is live: read it at any point during the call. It is also delivered once on
1560
+ `call-summary`, emitted **inside** `close()` while the call is still in `observer.observedCalls` —
1561
+ after that the call is gone and there is nothing left to ask. `observer.close()` closes its calls
1562
+ first and its collector afterwards, so every summary still makes it out.
1563
+
1564
+ Cost is **one bus listener per subscribed event type, for the whole observer** — not one per call. A
1565
+ per-call design would be quadratic in concurrent calls: at 500 calls and eight events, 4 000
1566
+ listeners each doing 500 no-op invocations per event. Percentiles are computed once, at close.
1567
+
1568
+ ## Remote track resolution (mediasoup / SFU)
1569
+
1570
+ In an SFU, one participant's **outbound** track is delivered to other participants as **inbound**
1571
+ tracks (one **publisher** → many **subscribers**). Correlation is **opt-in** per observer: set
1572
+ `ObserverConfig.createRemoteTrackResolver`, a factory invoked when each call is created that returns the
1573
+ call's `RemoteTrackResolver` (or `undefined` for none).
1574
+
1575
+ `RemoteTrackResolver` is a generic, strategy-driven class. It subscribes to the bus (filtered to
1576
+ its call) and links tracks by **publisher id** — the link key — maintaining the links directly on
1577
+ the tracks: `inboundTrack.remoteOutboundTrack` and `outboundTrack.remoteInboundTracks: Set`.
1578
+
1579
+ ```ts
1580
+ import { Observer, createDefaultMediasoupRemoteTrackResolverFactory } from '@observertc/observer-js';
1581
+
1582
+ const observer = new Observer({
1583
+ createRemoteTrackResolver: createDefaultMediasoupRemoteTrackResolverFactory(),
1584
+ });
1585
+
1586
+ // later, given tracks (links are kept up to date as tracks come and go):
1587
+ const source = inboundTrack.remoteOutboundTrack; // the publishing ObservedOutboundTrack
1588
+ const receivers = [ ...outboundTrack.remoteInboundTracks ]; // the subscribing ObservedInboundTrack[]
1589
+ ```
1590
+
1591
+ Two built-in factories ship: `createDefaultMediasoupRemoteTrackResolverFactory()` (publisher =
1592
+ `attachments.producerId`, subscriber = `attachments.consumerId`) and
1593
+ `createP2pRemoteTrackResolverFactory()` (matches by RTP **SSRC**, preserved end-to-end in p2p).
1594
+
1595
+ For any other topology, build a `RemoteTrackResolver` with your own key resolvers — the publisher
1596
+ id is just whatever links a subscribed track to the published one:
1597
+
1598
+ ```ts
1599
+ import { Observer, RemoteTrackResolver } from '@observertc/observer-js';
1600
+
1601
+ const observer = new Observer({
1602
+ createRemoteTrackResolver: (observedCall) => new RemoteTrackResolver(observedCall, {
1603
+ resolveOutboundTrackPublisherId: (out) => out.attachments?.mediaId as string | undefined,
1604
+ resolveInboundTrackPublisherId: (inb) => inb.attachments?.mediaId as string | undefined,
1605
+ resolveInboundTrackSubscriberId: (inb) => inb.attachments?.subId as string | undefined, // optional
1606
+ }),
1607
+ });
1608
+ ```
1609
+
1610
+ For the mediasoup factory, the application puts `producerId` / `consumerId` (and optionally
1611
+ `direction`, `label`) into the track `attachments`.
1612
+
1613
+ ### Resolution stands alone — you do not need router observation
1614
+
1615
+ Track resolution and [mediasoup router observation](#mediasoup-router-observation) are two
1616
+ **independent** opt-ins that happen to share a vendor name. Nothing in `RemoteTrackResolver` reads
1617
+ `ObservedMediasoupRouter`, and nothing in `ObservedMediasoupRouter` touches calls, clients or tracks.
1618
+
1619
+ So if your application already builds its own per-router report and you only want the detectors that
1620
+ need publisher↔subscriber links, set `createRemoteTrackResolver` and simply never call
1621
+ `observer.createObservedMediasoupRouter(...)`. No `MediasoupRouterSample` is created, nothing
1622
+ accumulates, and these keep working:
1623
+
1624
+ `UnconsumedTrackDetector`, `PublisherFaultCorroborationDetector`, `TrackDeliveryMismatchDetector`,
1625
+ `IssueFanOutDetector`, `RemoteTrackResolverValidator`, `SimulcastReceiverValidator`.
1626
+
1627
+ ### Backing a strategy with your own mapping
1628
+
1629
+ The three resolvers are plain functions returning a link key, so they can read a table your own
1630
+ report already maintains rather than something the client attached. The only requirement is a key
1631
+ **visible on both sides**. SSRC is the useful one, because it needs no client cooperation at all —
1632
+ mediasoup knows each consumer's `rtpParameters.encodings[].ssrc` server-side, and the subscriber's
1633
+ inbound RTP reports the same value:
1634
+
1635
+ ```ts
1636
+ // your own table, filled where you already create consumers
1637
+ const ssrcToProducerId = new Map<number, string>();
1638
+
1639
+ const observer = new Observer({
1640
+ createRemoteTrackResolver: (observedCall) => new RemoteTrackResolver(observedCall, {
1641
+ resolveOutboundTrackPublisherId: (out) => out.attachments?.producerId as string | undefined,
1642
+ resolveInboundTrackPublisherId: (inb) => {
1643
+ const ssrc = inb.getInboundRtp()?.ssrc;
1644
+
1645
+ return ssrc === undefined ? undefined : ssrcToProducerId.get(ssrc);
1646
+ },
1647
+ }),
1648
+ });
1649
+ ```
1650
+
1651
+ > **A key that arrives late still links.** A track announces itself once, but its `attachments` are
1652
+ > replaced on every sample and a table like the one above is inherently racy against sample arrival.
1653
+ > Tracks whose key does not resolve at first sight are held and retried on their own
1654
+ > `*-track-updated`, i.e. exactly when new stats arrive for them — so a key that appears on the second
1655
+ > sample links then, rather than being lost for the track's lifetime. `resolver.pendingTrackCounts`
1656
+ > reports how many are still waiting; in a healthy setup it is `{ inbound: 0, outbound: 0 }`.
1657
+
1658
+ If a strategy resolves *nothing* the failure is quiet — "no subscribers" and "no links resolved" look
1659
+ identical from the outside. That is what
1660
+ [`RemoteTrackResolverValidator`](#validators--one-shot-structural-checks) is for; run it once in
1661
+ staging after wiring up a custom strategy.
1662
+
1663
+ ---
1664
+
1665
+ ## Mediasoup router observation
1666
+
1667
+ Everything above is built from the **client-reported** `ClientSample`. When you run a
1668
+ [mediasoup](https://mediasoup.org) SFU you also have the **server's own** ground truth — its
1669
+ routers, transports, producers, consumers and data channels, with exact lifetimes and state
1670
+ transitions. `ObservedMediasoupRouter` captures that server-side view into a
1671
+ **`MediasoupRouterSample`**, completely independent of the client sample pipeline.
1672
+
1673
+ ### The concept
1674
+
1675
+ You hand the observer a live mediasoup `Router`; it attaches to mediasoup's own `observer` API and,
1676
+ from then on, **passively tracks** the router's topology and lifecycle — with no polling and no
1677
+ changes to your media code:
1678
+
1679
+ - new transports (`webrtc` / `plain` / `pipe` / `direct`), their selected `tuple`, ICE/DTLS/SCTP
1680
+ state transitions and `connectedAt`;
1681
+ - producers (codec, SSRCs/RIDs, `pause`/`resume`) and consumers (`pause`/`resume`,
1682
+ `producerPaused`/`producerResumed`);
1683
+ - data producers and data consumers;
1684
+ - `createdAt` / `closedAt` for every entity above.
1685
+
1686
+ It keeps all of this **in memory**, in a single `MediasoupRouterSample` exposed as
1687
+ `observedRouter.sample` — see [`src/schema/MediasoupRouter.ts`](./src/schema/MediasoupRouter.ts). The
1688
+ sample **accumulates for the life of the router**: closed transports/producers/consumers are kept
1689
+ (with their `closedAt` set), not removed. Read it whenever you like — it's a plain object you own.
1690
+
1691
+ ### Memory & large meetings
1692
+
1693
+ This is intentionally the **simplest** approach — everything lives in memory and nothing is sampled
1694
+ or evicted for you. That's fine for typical rooms, but be aware of the cost at scale:
1695
+
1696
+ - **Consumers grow as O(N²)** on a single flat router: with `N` participants each producing audio +
1697
+ video and consuming everyone else, the sample holds roughly `2·N·(N−1)` consumer records (≈ 19,800
1698
+ for `N` = 100).
1699
+ - The sample is **cumulative** — closed entities and their `history` are retained — so it also grows
1700
+ with call duration and churn (renegotiation, simulcast layer changes, rejoins).
1701
+
1702
+ A 100-participant flat router can therefore reach tens of MB and keep growing. There is **no built-in
1703
+ sink, snapshotting, or eviction** — by design. **If you run large meetings, do your own sampling:**
1704
+ on your own cadence read `observedRouter.sample` (snapshot/serialize/persist what you need), drop what
1705
+ you don't, and close routers you no longer track. (mediasoup also typically shards routers across
1706
+ workers/cores, which keeps any one router small.)
1707
+
1708
+ ### Extending the sample, and building your own report
1709
+
1710
+ The sample is yours to annotate. Every entity — the router, each transport, producer, consumer, data
1711
+ producer and data consumer — has an `attachments?: Record<string, unknown>` slot, and there are three
1712
+ ways to fill it, from most declarative to most ad-hoc.
1713
+
1714
+ **1. `enrich` — mirror mediasoup's own `appData`.** The common case: your application already keeps
1715
+ `participantId`, `purpose` and similar on the mediasoup objects, and you want them on the sample.
1716
+ Runs once per entity at creation, before the corresponding event:
1717
+
1718
+ ```ts
1719
+ observer.createObservedMediasoupRouter({
1720
+ router,
1721
+ enrich: {
1722
+ producer: (producer) => ({ participantId: producer.appData.participantId, purpose: producer.appData.purpose }),
1723
+ consumer: (consumer) => ({ subscriberId: consumer.appData.subscriberId }),
1724
+ transport: (transport) => ({ role: transport.appData.role }),
1725
+ },
1726
+ });
1727
+ ```
1728
+
1729
+ A throwing enricher is caught and logged — it can't take the router's bookkeeping down with it.
1730
+
1731
+ **2. Lifecycle events — enrich on the fly.** Each entity announces itself as
1732
+ `<entity>-sample-added` and `<entity>-sample-closed`, carrying **the live sample object** (not a
1733
+ copy) plus the mediasoup object it came from. Mutating it in the handler is the intended pattern:
1734
+
1735
+ ```ts
1736
+ observedRouter.on('producer-sample-added', ({ sample, producer, transport }) => {
1737
+ sample.attachments = { ...sample.attachments, participantId: lookup(producer.id) };
1738
+ });
1739
+
1740
+ observedRouter.on('producer-sample-closed', ({ sample }) => {
1741
+ archive(sample); // its `closedAt` is set
1742
+ });
1743
+ ```
1744
+
1745
+ Events: `transport-sample-added` / `-closed`, `producer-sample-added` / `-closed`,
1746
+ `consumer-sample-added` / `-closed`, `data-producer-sample-added` / `-closed`,
1747
+ `data-consumer-sample-added` / `-closed`.
1748
+
1749
+ **3. `attachTo(id, attachments)` — annotate later, from anywhere.** When the knowledge arrives after
1750
+ the entity did (a signalling message, a database lookup that resolved):
1751
+
1752
+ ```ts
1753
+ observedRouter.attachTo(producerId, { participantId, joinedFrom: 'mobile' }); // merges
1754
+ ```
1755
+
1756
+ Ids are unique across mediasoup entity kinds, so one method covers all of them. It returns `false`
1757
+ for an unknown id rather than failing quietly — which matters when application events race the
1758
+ mediasoup ones. For direct access there are typed accessors: `getTransportSample(id)`,
1759
+ `getProducerSample(id)`, `getConsumerSample(id)`, `getDataProducerSample(id)`,
1760
+ `getDataConsumerSample(id)`. They index the *same* objects the arrays hold, so a lookup is O(1)
1761
+ instead of a `sample.producers.find(...)` scan.
1762
+
1763
+ #### Building your own report
1764
+
1765
+ `observedRouter.sample` is live — arrays grow and `history` entries are appended as the router runs,
1766
+ so a report built directly on it keeps changing after you think you're done. Use **`snapshot()`** for
1767
+ a detached deep copy:
1768
+
1769
+ ```ts
1770
+ const report = {
1771
+ ...observedRouter.snapshot(), // never moves again
1772
+ generatedAt: Date.now(),
1773
+ region: process.env.REGION,
1774
+ };
1775
+ ```
1776
+
1777
+ > **Note on typing.** The sample types no longer carry a `Record<string, unknown>` index signature.
1778
+ > That signature allowed arbitrary top-level keys but also silently accepted typos on real fields and
1779
+ > weakened autocomplete. Custom data belongs in `attachments`, which is typed as such. If you were
1780
+ > assigning ad-hoc keys directly onto a sample object, move them into `attachments`.
1781
+
1782
+ ### Matching peer connections — by **event**, not by storage
1783
+
1784
+ The observer correlates the SFU side with the client side **at the peer-connection level**: a
1785
+ mediasoup WebRTC transport and a client's `RTCPeerConnection` share the same id, so whenever an
1786
+ observed peer connection's id matches one of the router's WebRTC transport ids, that's a match.
1787
+
1788
+ **The observer does not store the router (or its sample) on any entity.** Instead, for **every**
1789
+ matching peer connection it emits **`mediasoup-router-matched-with-peer-connection`** and steps
1790
+ back — *your application* decides what the pairing means. The payload carries the full peer-connection
1791
+ ancestry, so you get the router **and** the matched `observedPeerConnection`, `observedClient` and
1792
+ `observedCall` in one place. Stamp the `routerId` into the peer connection's / client's `appData`,
1793
+ build your own index, attach the server sample to the call in your database — whatever fits.
1794
+
1795
+ This matching is **opt-in**: pass `matchPeerConnectionByWebRtcTransportId: true` to
1796
+ `createObservedMediasoupRouter`. When enabled, as peer connections are observed
1797
+ (`peer-connection-added`) the observer checks whether the peer connection's id is one of the router's
1798
+ WebRTC transport ids; on a hit it emits — once per matching peer connection — and keeps watching, so a
1799
+ router serving many participants emits one match per participant's transport. When the flag is omitted
1800
+ or `false`, no matching is performed and the event never fires. The internal listener is removed
1801
+ automatically when the router closes or the observer closes.
1802
+
1803
+ ### Ordering contract — observe the router first
1804
+
1805
+ Matching is **forward-only by design**, and that is sufficient because the lifecycle ordering is
1806
+ **guaranteed, not racy**:
1807
+
1808
+ - `ObservedMediasoupRouter` works purely by **subscribing to mediasoup's `observer` API**, so it can
1809
+ only see events that happen *after* it is created. You therefore create it the moment the router
1810
+ exists — **before** any transport is added to it — and it captures the rest going forward.
1811
+ - A mediasoup transport is always created **on the server first**; only then can the client connect
1812
+ to it, produce/consume, and begin shipping `ClientSample`s. So a peer connection — and the
1813
+ `peer-connection-added` event it triggers — can never appear before its server-side WebRTC
1814
+ transport already exists (and has been observed by the router).
1815
+
1816
+ Put together: by the time a `peer-connection-added` fires, the router has already recorded that
1817
+ transport's id in `webrtcTransportIds`, so a single forward-looking listener catches every match. No
1818
+ back-scan of existing peer connections and no re-check on transport creation are needed — the
1819
+ observer deliberately does **not** look backwards.
1820
+
1821
+ **Your responsibility:** call `createObservedMediasoupRouter(...)` as early as the router exists
1822
+ (before transports are added or samples are accepted). If you register the router *after* its
1823
+ transports are created or after the client's first sample, those events are already in the past and
1824
+ the corresponding matches are missed.
1825
+
1826
+ When the underlying mediasoup router closes, its `close` propagates to `ObservedMediasoupRouter`,
1827
+ which sets the sample's `closedAt` and emits **`mediasoup-router-removed`** — your cue to read /
1828
+ persist the final `observedRouter.sample` and drop your reference to it.
1829
+
1830
+ ### Options — `observer.createObservedMediasoupRouter(settings)`
1831
+
1832
+ | Field | Type | Required | Meaning |
1833
+ |-------|------|----------|---------|
1834
+ | `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` |
1835
+ | `appData` | `Record<string, unknown>` | no | application-owned bag on the `ObservedMediasoupRouter` |
1836
+ | `attachments` | `Record<string, unknown>` | no | free-form data; carried on `sample.attachments` |
1837
+ | `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 |
1838
+
1839
+ Peer-connection matching is **off by default**; enable it with
1840
+ `matchPeerConnectionByWebRtcTransportId: true`. Returns the `ObservedMediasoupRouter`, or `undefined`
1841
+ if the observer is closed (a router with the same id returns the existing instance — both warn).
1842
+
1843
+ Useful members on the returned object: `.sample` (the in-memory `MediasoupRouterSample`, with
1844
+ `createdAt` / `closedAt?` on it), `.appData`, `.attachments`, `.webrtcTransportIds: Set<string>`,
1845
+ `.id`, `.close()`.
1846
+
1847
+ ### Example
1848
+
1849
+ ```ts
1850
+ import { Observer, InMemorySink } from '@observertc/observer-js';
1851
+ import type { ObservedMediasoupRouterScope, ObservedPeerConnectionScope } from '@observertc/observer-js';
1852
+
1853
+ const observer = new Observer();
1854
+
1855
+ // 1) Feed client samples as usual so the observer knows about calls, clients & peer connections.
1856
+ // (e.g. transport-layer: observer.accept(clientSample, context))
1857
+
1858
+ // 2) Observe the SFU side; opt in to peer-connection matching. State accumulates in `.sample`.
1859
+ const router = /* your mediasoup router */ undefined as any;
1860
+ const observedRouter = observer.createObservedMediasoupRouter({
1861
+ router,
1862
+ matchPeerConnectionByWebRtcTransportId: true,
1863
+ });
1864
+
1865
+ // For large meetings, sample it yourself on your own cadence (see "Memory & large meetings"):
1866
+ // setInterval(() => persist(observedRouter.sample), 10_000);
1867
+
1868
+ // 3) Every peer connection whose id matches one of the router's WebRTC transport ids fires this —
1869
+ // WE decide what to do with each pairing. The payload carries the full ancestry.
1870
+ observer.on('mediasoup-router-matched-with-peer-connection',
1871
+ ({ observedMediasoupRouter, observedCall, observedPeerConnection }:
1872
+ ObservedMediasoupRouterScope & ObservedPeerConnectionScope) => {
1873
+ (observedPeerConnection.appData ??= {}).routerId = observedMediasoupRouter.id;
1874
+ myStore.linkRouterToCall(observedCall.callId, observedMediasoupRouter.id);
1875
+ },
1876
+ );
1877
+
1878
+ // 4) The router closed — read/persist the final state, then drop your reference.
1879
+ observer.on('mediasoup-router-removed', ({ observedMediasoupRouter }: ObservedMediasoupRouterScope) => {
1880
+ persist(observedMediasoupRouter.sample); // its `closedAt` is set
1881
+ });
1882
+ ```
1883
+
1884
+ ### Why event-driven matching instead of storing on the call
1885
+
1886
+ - **Loose coupling.** The call model stays about client telemetry; the SFU view lives on its own
1887
+ `ObservedMediasoupRouter` and is associated only if and how *you* choose.
1888
+ - **You own the association.** One router serves many peer connections (across clients and calls),
1889
+ and the right place to keep that mapping is application-specific — so the observer hands you each
1890
+ peer-connection match and gets out of the way.
1891
+ - **You own the sampling.** The router sample is plain in-memory state you read on your own terms;
1892
+ for large meetings, sample/persist it yourself (see [Memory & large meetings](#memory--large-meetings))
1893
+ rather than relying on the library to evict — it deliberately doesn't.
1894
+
1895
+ ---
1896
+
1897
+ ## Sinks (per-client sample persistence)
1898
+
1899
+ A **sink** receives the samples a client accepts — for archival, streaming, or later offline
1900
+ replay. Each `ObservedClient` gets its **own** sink, produced by the
1901
+ `ObserverConfig.createClientSink` factory when the client is created (return `undefined` for no
1902
+ sink). The client pushes every accepted sample to its sink, and `end()`s it on close.
1903
+
1904
+ ### The `ClientSampleSink` base class
1905
+
1906
+ `ClientSampleSink` is an **abstract base class** (a typed `EventEmitter`). You create a sink by
1907
+ **subclassing it** and implementing `write` and `end`. It is **object-mode**: `write` receives
1908
+ the `ClientSample` *object*, so each sink decides how (or whether) to serialize it — JSON line,
1909
+ protobuf, a remote POST body, an in-memory push, etc.
1910
+
1911
+ ```ts
1912
+ import { ClientSampleSink, ClientSample } from '@observertc/observer-js';
1913
+
1914
+ abstract class ClientSampleSink /* extends EventEmitter */ {
1915
+ abstract write(sample: ClientSample): boolean; // accept one sample; `false` = backpressure
1916
+ abstract end(): void; // flush; emit `close` when the destination is ready
1917
+
1918
+ // typed events (inherited): the listener signature is inferred from the event name
1919
+ on(event: 'close' | 'finish' | 'drain', listener: () => void): this;
1920
+ on(event: 'error', listener: (err: Error) => void): this;
1921
+ // ...and the matching `once` / `off` / `emit`
1922
+ }
1923
+ ```
1924
+
1925
+ | Event | Meaning |
1926
+ |-------|---------|
1927
+ | `close` | the destination is fully written and closed (e.g. a file flushed and its fd closed) — "ready" |
1928
+ | `error` | the destination failed |
1929
+ | `finish` | `end()` was processed and queued data flushed (before `close`) |
1930
+ | `drain` | the buffer drained after backpressure; safe to write more |
1931
+
1932
+ The library calls `write(sample)` **synchronously** per accepted sample (it is not awaited),
1933
+ `end()`s the sink when the client closes, and attaches an `error` listener so a failing sink
1934
+ can't crash the process (it also catches throws from `write`/`end`). The application — which
1935
+ created the sink — listens for `close` (destination ready) and `error`. Because `write` isn't
1936
+ awaited in the `accept()` hot path, **backpressure and batching are the sink's concern**.
1937
+
1938
+ ### Built-in sinks
1939
+
1940
+ ```ts
1941
+ import { Observer, createJsonlFileSinkFactory } from '@observertc/observer-js';
1942
+
1943
+ const observer = new Observer({
1944
+ // one ./stats/<callId>__<clientId>.jsonl per client
1945
+ createClientSink: createJsonlFileSinkFactory({ directory: './stats' }),
1946
+ });
1947
+
1948
+ // React when a sink is created for a client:
1949
+ observer.on('client-sink-created', ({ observedClient, sink }) => {
1950
+ sink.on('close', () => {
1951
+ // the file is fully flushed and its fd closed — ready to upload, move, etc.
1952
+ });
1953
+ });
1954
+ ```
1955
+
1956
+ | Export | Signature | Notes |
1957
+ |--------|-----------|-------|
1958
+ | `createJsonlFileSinkFactory` | `({ directory, flags?, getFileName?, serializeSample? }) => ClientSampleSinkFactory` | per-client JSONL files; path defaults to `${callId}__${clientId}.jsonl` under `directory` (which **must exist**) |
1959
+ | `createJsonlFileSink` | `({ path, flags?, serializeSample? }) => ClientSampleSink` | a single JSONL file; wraps `fs.WriteStream` and re-emits its `close`/`finish`/`drain`/`error` |
1960
+ | `JsonlFileSink` | `class extends ClientSampleSink` | the underlying class; exposes `readonly path` so a `close` handler knows which file is ready |
1961
+ | `createInMemorySink` / `InMemorySink` | `(samples?: ClientSample[]) => InMemorySink` | collects the accepted **sample objects** into `.samples: ClientSample[]`; emits `close` on `end()` |
1962
+
1963
+ `serializeSample?: (sample: ClientSample) => string` overrides the default `JSON.stringify` for
1964
+ the JSONL sinks (e.g. to redact or reshape before writing).
1965
+
1966
+ ### Reading sink-specific info (e.g. the file path)
1967
+
1968
+ The bus hands you the sink as the base `ClientSampleSink`. To read information specific to a sink
1969
+ type — for a file sink, where it was written — **narrow with `instanceof`** and read the sink's
1970
+ public fields. `JsonlFileSink` exposes `path`:
1971
+
1972
+ ```ts
1973
+ import { JsonlFileSink } from '@observertc/observer-js';
1974
+
1975
+ observer.on('client-sink-created', ({ observedClient, sink }) => {
1976
+ if (sink instanceof JsonlFileSink) {
1977
+ const { path } = sink; // the file this client's samples go to
1978
+ sink.once('close', () => uploadFile(path)); // close = flushed & fd closed → ready
1979
+ }
1980
+ });
1981
+ ```
1982
+
1983
+ The general pattern: each concrete sink exposes whatever it wants as `public readonly` fields, and
1984
+ consumers narrow (`instanceof YourSink`) to read them. Your own sinks do the same.
1985
+
1986
+ ### Writing your own sink
1987
+
1988
+ Subclass `ClientSampleSink` and emit the lifecycle events yourself — for any non-file
1989
+ destination (a remote endpoint, a message queue, an object store, …):
1990
+
1991
+ ```ts
1992
+ import { ClientSampleSink, ClientSample, ClientSampleSinkFactory } from '@observertc/observer-js';
1993
+
1994
+ class HttpSink extends ClientSampleSink {
1995
+ private buffer: ClientSample[] = [];
1996
+ constructor(private readonly url: string) { super(); }
1997
+
1998
+ write(sample: ClientSample): boolean {
1999
+ this.buffer.push(sample); // batch; decide your own backpressure
2000
+ return true;
2001
+ }
2002
+ end(): void {
2003
+ fetch(this.url, { method: 'POST', body: JSON.stringify(this.buffer) })
2004
+ .then(() => this.emit('close')) // signal "destination ready"
2005
+ .catch((err) => this.emit('error', err));
2006
+ }
2007
+ }
2008
+
2009
+ const createClientSink: ClientSampleSinkFactory = ({ clientId, observedCall }) =>
2010
+ new HttpSink(`https://stats.example.com/${observedCall.callId}/${clientId}`);
2011
+
2012
+ const observer = new Observer({ createClientSink });
2013
+ ```
2014
+
2015
+ `observedClient.sink?` exposes the created sink; the `client-sink-created` event delivers it on
2016
+ the bus with full ancestry. `ClientSampleSinkFactory` is
2017
+ `(p: { clientId: string; observedCall: ObservedCall }) => ClientSampleSink | undefined`.
2018
+
2019
+ ---
2020
+
2021
+ ## Injecting data into a client
2022
+
2023
+ Sometimes the application holds data that belongs on a client's record but isn't part of the
2024
+ client-reported `ClientSample` — a room id or display name, an application-level event
2025
+ (*"recording started"*), a server-detected issue, an extension stat, or a device/meta item.
2026
+ `ObservedClient` exposes **injection** methods that merge such data into the client's sample stream,
2027
+ so it updates the live model **and** is persisted to the client's
2028
+ [sink](#sinks-per-client-sample-persistence) exactly like sampled data.
2029
+
2030
+ | Method | Adds to the sample's | Surfaces as |
2031
+ |--------|----------------------|-------------|
2032
+ | `injectAttachment(attachments)` | `attachments` (merged via `Object.assign`) | `observedClient.attachments` |
2033
+ | `injectEvent(event: ClientEvent)` | `clientEvents` | `client-event` (plus any state the event drives) |
2034
+ | `injectIssue(issue: ClientIssue)` | `clientIssues` | `client-issue` |
2035
+ | `injectMetaData(meta: ClientMetaData)` | `clientMetaItems` | `client-metadata` |
2036
+ | `injectExtensionStat(stat: ExtensionStat)` | `extensionStats` | `client-extension-stats` |
2037
+
2038
+ ### When the injected data lands
2039
+
2040
+ Injection is timing-aware so nothing is dropped, regardless of *when* you call it:
2041
+
2042
+ - **During a sample's processing** — e.g. from inside a `client-updated` / `client-event` handler,
2043
+ which run within `accept()` — the data is applied to the **current** sample immediately: reflected
2044
+ in entity state and written to the sink as part of that sample.
2045
+ - **Between samples** — the data is buffered and merged into the **next** `accept()`'s sample.
2046
+ - **On `close()` with pending injections and no further sample** — the buffer is flushed as a final
2047
+ synthetic sample (applied to state and written to the sink) before the sink is ended, so a
2048
+ last-moment injection is never lost.
2049
+
2050
+ In every case the injected data both updates the live `ObservedClient` and reaches the per-client
2051
+ sink — the sink always receives the final, **injection-merged** sample (the sink write happens at the
2052
+ end of `accept()`, after the merge).
2053
+
2054
+ ### Example
2055
+
2056
+ ```ts
2057
+ // Enrich at creation from your app's knowledge of the participant. Injecting in `client-added`
2058
+ // (which runs just before the first accept) lands on the first sample.
2059
+ observer.on('client-added', ({ observedClient }) => {
2060
+ observedClient.injectAttachment({ roomId: lookupRoomId(observedClient.clientId) });
2061
+ });
2062
+
2063
+ // Application-level signals at any time:
2064
+ const client = observer.getObservedCall(callId)?.getObservedClient(clientId);
2065
+ client?.injectEvent({ type: 'RECORDING_STARTED', timestamp: Date.now() });
2066
+ client?.injectIssue({ type: 'app-kicked-participant', timestamp: Date.now() });
2067
+ ```
2068
+
2069
+ `attachments` are latest-wins (like sampled `attachments`): injecting a key overwrites its previous
2070
+ value. `appData` is unaffected — injections flow into the sample/telemetry, not the app-owned
2071
+ `appData` bag (see [Ingestion](#ingestion-accept-context--lifecycle)).
2072
+
2073
+ ## Logging
2074
+
2075
+ `observer-js` logs through a single, swappable sink. Out of the box it writes `debug` and
2076
+ above to `console` (verbose — install your own sink for production). Funnel everything into your
2077
+ logger:
2078
+
2079
+ ```ts
2080
+ import { format } from 'node:util';
2081
+ import { setObserverLogger, type ObserverLogger } from '@observertc/observer-js';
2082
+
2083
+ // (module, ...args) — the module name is a separate leading argument, never part
2084
+ // of the message. `args` is a printf-style format string plus its values.
2085
+ setObserverLogger({
2086
+ trace: (m, ...a) => myLogger.trace(`[${m}] ${format(...a)}`),
2087
+ debug: (m, ...a) => myLogger.debug(`[${m}] ${format(...a)}`),
2088
+ info: (m, ...a) => myLogger.info(`[${m}] ${format(...a)}`),
2089
+ warn: (m, ...a) => myLogger.warn(`[${m}] ${format(...a)}`),
2090
+ error: (m, ...a) => myLogger.error(`[${m}] ${format(...a)}`),
2091
+ });
2092
+ ```
2093
+
2094
+ Spreading the arguments directly (`myLogger.warn(prefix, ...a)`) works with `console`, but
2095
+ **not** with loggers whose first parameter is the message — pino, bunyan, roarr. There the
2096
+ module name becomes the whole message and the rest is silently dropped; pass the module as
2097
+ structured metadata (`log.warn({ module }, ...a)`) or collapse it with `util.format` as above.
2098
+
2099
+ `createLogger(moduleName)` is also exported for your own modules. See
2100
+ **[`docs/logging.md`](./docs/logging.md)** for pino / winston / console recipes, level
2101
+ filtering, per-module routing, and full silencing.
2102
+
2103
+ ---
2104
+
2105
+ ## Design notes
2106
+
2107
+ **[`docs/design-notes.md`](./docs/design-notes.md)** covers the reasoning behind the library rather
2108
+ than its API: why client-detectable conditions are never re-derived server-side, why each shipped
2109
+ detector exists, what was deliberately *not* built and why, the WebRTC domain facts that shaped the
2110
+ implementation (ICE-Lite disconnect waves, counter resets, why ICE and RTCP RTT must never be
2111
+ blended), and an operational threshold reference.
2112
+
2113
+ ---
2114
+
2115
+ ## Error-handling philosophy
2116
+
2117
+ The library **warns and degrades; it does not throw** on operational problems:
2118
+
2119
+ - `createObservedCall` / `createObservedClient` on a closed parent → warn + return `undefined`.
2120
+ - Duplicate id → warn + return the **existing** instance.
2121
+ - `accept()` on a closed client → warn + no-op.
2122
+ - Sample missing `callId`/`clientId`, or observer closed → `sample-rejected` event.
2123
+ - A throwing accept-middleware → warn + drop that sample (never crashes `accept()`).
2124
+
2125
+ Therefore `create*` and `getOrCreate*` return `T | undefined`; **guard the result.** The
2126
+ `Middleware` utility's internal invariants (e.g. calling `next()` twice) throw, but those throws
2127
+ are caught by `accept()` and surfaced as a warning.
2128
+
2129
+ ---
2130
+
2131
+ ## Development & extension guide
2132
+
2133
+ ```bash
2134
+ yarn install
2135
+ yarn build # tsup → dist/ (dual ESM .mjs + CJS .js, single entry, .d.ts/.d.mts + sourcemaps)
2136
+ yarn lint # eslint -c .eslintrc.json "src/**/*.ts"
2137
+ yarn typecheck # tsc --noEmit
2138
+ yarn test # jest
2139
+ ```
2140
+
2141
+ The build is driven by [`tsup`](https://tsup.egoist.dev) (config in `tsup.config.ts`): a single
2142
+ entry (`src/index.ts`), dual ESM + CommonJS output to `dist/` (`index.mjs` / `index.js`) with
2143
+ `.d.mts` / `.d.ts` types and sourcemaps, targeting Node 20. CI (`.github/workflows/ci.yml`) runs
2144
+ lint + typecheck + **build** + test on every push/PR.
2145
+
2146
+ **Project layout** (`src/`): `Observer.ts`, `ObservedCall.ts`, `ObservedClient.ts`,
2147
+ `ObservedPeerConnection.ts`, the `Observed*` sub-stat classes, `ObserverEvents.ts` (the typed
2148
+ event map + scope types), `detectors/` (`Detector`, `Detectors`, and one file per detector),
2149
+ `validators/` (`Validator`, `Validators`, one file per validator), `issues/` (`ActiveClientIssue`,
2150
+ `ActiveIssueTracker`, `ActiveIssuesRegistry`, `ObservedClientIssueRegistry`), `scores/`,
2151
+ `resolvers/` (remote-track resolvers), `utils/` (`stats`, `SlidingWindow`, `TrendTester`,
2152
+ `CallHealthAggregator`), `common/` (`logger`, `utils`, `Middleware`), `schema/`
2153
+ (sample/event/meta types), and `sinks/` (the `ClientSampleSink` base + `JsonlFileSink` /
2154
+ `InMemorySink`, re-exported from the package root).
2155
+
2156
+ **Conventions to follow when developing further:**
2157
+
2158
+ - *Single event bus.* New consumer-facing events go in `ObserverEvents.ts` with an object
2159
+ payload `[<Scope> & { …subject }]`, and are emitted via the component's
2160
+ `_notify(type, { ...this.eventScope, …subject })`. Each component has a precomputed
2161
+ `eventScope` field and a thin `_notify` wrapper around the right emitter. Keep purely internal
2162
+ coordination as **local** EventEmitter events (and remember to `off` them on close).
2163
+ - *Warn, don't throw* on operational/edge conditions; return `undefined` where a value can't be produced.
2164
+ - *Counter-reset-safe deltas.* When computing a delta from a cumulative counter, never emit a
2165
+ negative value (guard `curr >= prev`), to survive counter resets / SSRC reuse.
2166
+ - *Explicit accumulation.* The per-sample metric accumulation in `accept()` is intentionally
2167
+ explicit and not abstracted — match that style.
2168
+ - *Detectors are server-side.* Add cross-client detectors on `ObservedCall.detectors`; don't
2169
+ re-implement client-detectable signals.
2170
+
2171
+ **Recipes:**
2172
+
2173
+ - *Add an event:* add the key + payload to `ObserverEvents`; in the owning component call
2174
+ `this._notify('my-event', { ...this.eventScope, subject })`.
2175
+ - *Add a per-stream metric:* add the field to the relevant `Observed*Rtp`/track class, populate
2176
+ it in its `update()` (reset at the top of `update()` if it's per-tick), and read it from a
2177
+ `*-updated` handler.
2178
+ - *Add a detector:* implement `Detector`, register it on `call-added` via
2179
+ `observedCall.detectors.add(...)`, surface findings with `observedCall.addIssue(...)`.
2180
+
2181
+ ---
2182
+
2183
+ ## License
2184
+
2185
+ Apache-2.0. Part of the [ObserveRTC](https://github.com/ObserveRTC) ecosystem.