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