@observertc/observer-js 1.0.0-beta.1 → 1.0.0-beta.11

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 (227) hide show
  1. package/README.md +1032 -381
  2. package/dist/index.d.mts +2956 -0
  3. package/dist/index.d.ts +2956 -0
  4. package/dist/index.js +4095 -0
  5. package/dist/index.js.map +1 -0
  6. package/dist/index.mjs +4025 -0
  7. package/dist/index.mjs.map +1 -0
  8. package/llms-full.txt +1598 -0
  9. package/package.json +33 -16
  10. package/.eslintrc.json +0 -143
  11. package/.prettierignore +0 -5
  12. package/.prettierrc +0 -7
  13. package/jest.config.js +0 -9
  14. package/lib/ObservedCall.d.ts +0 -85
  15. package/lib/ObservedCall.d.ts.map +0 -1
  16. package/lib/ObservedCall.js +0 -219
  17. package/lib/ObservedCallEventMonitor.d.ts +0 -104
  18. package/lib/ObservedCallEventMonitor.d.ts.map +0 -1
  19. package/lib/ObservedCallEventMonitor.js +0 -314
  20. package/lib/ObservedCallSummary.d.ts +0 -18
  21. package/lib/ObservedCallSummary.d.ts.map +0 -1
  22. package/lib/ObservedCallSummary.js +0 -2
  23. package/lib/ObservedCertificate.d.ts +0 -19
  24. package/lib/ObservedCertificate.d.ts.map +0 -1
  25. package/lib/ObservedCertificate.js +0 -38
  26. package/lib/ObservedClient.d.ts +0 -127
  27. package/lib/ObservedClient.d.ts.map +0 -1
  28. package/lib/ObservedClient.js +0 -636
  29. package/lib/ObservedClientEventMonitor.d.ts +0 -91
  30. package/lib/ObservedClientEventMonitor.d.ts.map +0 -1
  31. package/lib/ObservedClientEventMonitor.js +0 -254
  32. package/lib/ObservedClientSummary.d.ts +0 -23
  33. package/lib/ObservedClientSummary.d.ts.map +0 -1
  34. package/lib/ObservedClientSummary.js +0 -2
  35. package/lib/ObservedCodec.d.ts +0 -22
  36. package/lib/ObservedCodec.d.ts.map +0 -1
  37. package/lib/ObservedCodec.js +0 -45
  38. package/lib/ObservedDataChannel.d.ts +0 -30
  39. package/lib/ObservedDataChannel.d.ts.map +0 -1
  40. package/lib/ObservedDataChannel.js +0 -75
  41. package/lib/ObservedIceCandidate.d.ts +0 -29
  42. package/lib/ObservedIceCandidate.d.ts.map +0 -1
  43. package/lib/ObservedIceCandidate.js +0 -59
  44. package/lib/ObservedIceCandidatePair.d.ts +0 -45
  45. package/lib/ObservedIceCandidatePair.d.ts.map +0 -1
  46. package/lib/ObservedIceCandidatePair.js +0 -127
  47. package/lib/ObservedIceTransport.d.ts +0 -36
  48. package/lib/ObservedIceTransport.d.ts.map +0 -1
  49. package/lib/ObservedIceTransport.js +0 -93
  50. package/lib/ObservedInboundRtp.d.ts +0 -92
  51. package/lib/ObservedInboundRtp.d.ts.map +0 -1
  52. package/lib/ObservedInboundRtp.js +0 -193
  53. package/lib/ObservedInboundTrack.d.ts +0 -32
  54. package/lib/ObservedInboundTrack.d.ts.map +0 -1
  55. package/lib/ObservedInboundTrack.js +0 -60
  56. package/lib/ObservedMediaPlayout.d.ts +0 -22
  57. package/lib/ObservedMediaPlayout.d.ts.map +0 -1
  58. package/lib/ObservedMediaPlayout.js +0 -42
  59. package/lib/ObservedMediaSource.d.ts +0 -28
  60. package/lib/ObservedMediaSource.d.ts.map +0 -1
  61. package/lib/ObservedMediaSource.js +0 -55
  62. package/lib/ObservedOutboundRtp.d.ts +0 -62
  63. package/lib/ObservedOutboundRtp.d.ts.map +0 -1
  64. package/lib/ObservedOutboundRtp.js +0 -144
  65. package/lib/ObservedOutboundTrack.d.ts +0 -32
  66. package/lib/ObservedOutboundTrack.d.ts.map +0 -1
  67. package/lib/ObservedOutboundTrack.js +0 -60
  68. package/lib/ObservedPeerConnection.d.ts +0 -207
  69. package/lib/ObservedPeerConnection.d.ts.map +0 -1
  70. package/lib/ObservedPeerConnection.js +0 -773
  71. package/lib/ObservedPeerConnectionTransport.d.ts +0 -17
  72. package/lib/ObservedPeerConnectionTransport.d.ts.map +0 -1
  73. package/lib/ObservedPeerConnectionTransport.js +0 -34
  74. package/lib/ObservedRemoteInboundRtp.d.ts +0 -30
  75. package/lib/ObservedRemoteInboundRtp.d.ts.map +0 -1
  76. package/lib/ObservedRemoteInboundRtp.js +0 -60
  77. package/lib/ObservedRemoteOutboundRtp.d.ts +0 -30
  78. package/lib/ObservedRemoteOutboundRtp.d.ts.map +0 -1
  79. package/lib/ObservedRemoteOutboundRtp.js +0 -60
  80. package/lib/ObservedTURN.d.ts +0 -31
  81. package/lib/ObservedTURN.d.ts.map +0 -1
  82. package/lib/ObservedTURN.js +0 -58
  83. package/lib/ObservedTurnServer.d.ts +0 -25
  84. package/lib/ObservedTurnServer.d.ts.map +0 -1
  85. package/lib/ObservedTurnServer.js +0 -59
  86. package/lib/Observer.d.ts +0 -65
  87. package/lib/Observer.d.ts.map +0 -1
  88. package/lib/Observer.js +0 -160
  89. package/lib/ObserverEventMonitor.d.ts +0 -138
  90. package/lib/ObserverEventMonitor.d.ts.map +0 -1
  91. package/lib/ObserverEventMonitor.js +0 -433
  92. package/lib/ObserverSummary.d.ts +0 -13
  93. package/lib/ObserverSummary.d.ts.map +0 -1
  94. package/lib/ObserverSummary.js +0 -2
  95. package/lib/common/Middleware.d.ts +0 -17
  96. package/lib/common/Middleware.d.ts.map +0 -1
  97. package/lib/common/Middleware.js +0 -60
  98. package/lib/common/SingleExecutor.d.ts +0 -3
  99. package/lib/common/SingleExecutor.d.ts.map +0 -1
  100. package/lib/common/SingleExecutor.js +0 -31
  101. package/lib/common/logger.d.ts +0 -17
  102. package/lib/common/logger.d.ts.map +0 -1
  103. package/lib/common/logger.js +0 -50
  104. package/lib/common/types.d.ts +0 -3
  105. package/lib/common/types.d.ts.map +0 -1
  106. package/lib/common/types.js +0 -3
  107. package/lib/common/utils.d.ts +0 -11
  108. package/lib/common/utils.d.ts.map +0 -1
  109. package/lib/common/utils.js +0 -63
  110. package/lib/detectors/Detector.d.ts +0 -5
  111. package/lib/detectors/Detector.d.ts.map +0 -1
  112. package/lib/detectors/Detector.js +0 -3
  113. package/lib/detectors/Detectors.d.ts +0 -11
  114. package/lib/detectors/Detectors.d.ts.map +0 -1
  115. package/lib/detectors/Detectors.js +0 -34
  116. package/lib/index.d.ts +0 -28
  117. package/lib/index.d.ts.map +0 -1
  118. package/lib/index.js +0 -49
  119. package/lib/mediasoup/ObservedMediaRouter.d.ts +0 -10
  120. package/lib/mediasoup/ObservedMediaRouter.d.ts.map +0 -1
  121. package/lib/mediasoup/ObservedMediaRouter.js +0 -6
  122. package/lib/monitors/TurnUsageMonitor.d.ts +0 -1
  123. package/lib/monitors/TurnUsageMonitor.d.ts.map +0 -1
  124. package/lib/monitors/TurnUsageMonitor.js +0 -146
  125. package/lib/schema/ClientEventTypes.d.ts +0 -228
  126. package/lib/schema/ClientEventTypes.d.ts.map +0 -1
  127. package/lib/schema/ClientEventTypes.js +0 -41
  128. package/lib/schema/ClientMetaTypes.d.ts +0 -34
  129. package/lib/schema/ClientMetaTypes.d.ts.map +0 -1
  130. package/lib/schema/ClientMetaTypes.js +0 -16
  131. package/lib/schema/ClientSample.d.ts +0 -1333
  132. package/lib/schema/ClientSample.d.ts.map +0 -1
  133. package/lib/schema/ClientSample.js +0 -4
  134. package/lib/scores/CalculatedScore.d.ts +0 -6
  135. package/lib/scores/CalculatedScore.d.ts.map +0 -1
  136. package/lib/scores/CalculatedScore.js +0 -2
  137. package/lib/scores/DefaultCallScoreCalculator.d.ts +0 -7
  138. package/lib/scores/DefaultCallScoreCalculator.d.ts.map +0 -1
  139. package/lib/scores/DefaultCallScoreCalculator.js +0 -21
  140. package/lib/scores/ScoreCalculator.d.ts +0 -4
  141. package/lib/scores/ScoreCalculator.d.ts.map +0 -1
  142. package/lib/scores/ScoreCalculator.js +0 -2
  143. package/lib/updaters/CallUpdater.d.ts +0 -5
  144. package/lib/updaters/CallUpdater.d.ts.map +0 -1
  145. package/lib/updaters/CallUpdater.js +0 -2
  146. package/lib/updaters/ObserverUpdater.d.ts +0 -5
  147. package/lib/updaters/ObserverUpdater.d.ts.map +0 -1
  148. package/lib/updaters/ObserverUpdater.js +0 -2
  149. package/lib/updaters/OnAllCallObserverUpdater.d.ts +0 -15
  150. package/lib/updaters/OnAllCallObserverUpdater.d.ts.map +0 -1
  151. package/lib/updaters/OnAllCallObserverUpdater.js +0 -50
  152. package/lib/updaters/OnAllClientCallUpdater.d.ts +0 -15
  153. package/lib/updaters/OnAllClientCallUpdater.d.ts.map +0 -1
  154. package/lib/updaters/OnAllClientCallUpdater.js +0 -53
  155. package/lib/updaters/OnAnyCallObserverUpdater.d.ts +0 -12
  156. package/lib/updaters/OnAnyCallObserverUpdater.d.ts.map +0 -1
  157. package/lib/updaters/OnAnyCallObserverUpdater.js +0 -37
  158. package/lib/updaters/OnAnyClientCallUpdater.d.ts +0 -12
  159. package/lib/updaters/OnAnyClientCallUpdater.d.ts.map +0 -1
  160. package/lib/updaters/OnAnyClientCallUpdater.js +0 -36
  161. package/lib/updaters/OnIntervalUpdater.d.ts +0 -10
  162. package/lib/updaters/OnIntervalUpdater.d.ts.map +0 -1
  163. package/lib/updaters/OnIntervalUpdater.js +0 -17
  164. package/lib/updaters/Updater.d.ts +0 -6
  165. package/lib/updaters/Updater.d.ts.map +0 -1
  166. package/lib/updaters/Updater.js +0 -2
  167. package/lib/utils/MediasoupRemoteTrackResolver.d.ts +0 -25
  168. package/lib/utils/MediasoupRemoteTrackResolver.d.ts.map +0 -1
  169. package/lib/utils/MediasoupRemoteTrackResolver.js +0 -110
  170. package/lib/utils/RemoteTrackResolver.d.ts +0 -7
  171. package/lib/utils/RemoteTrackResolver.d.ts.map +0 -1
  172. package/lib/utils/RemoteTrackResolver.js +0 -2
  173. package/src/ObservedCall.ts +0 -338
  174. package/src/ObservedCallEventMonitor.ts +0 -392
  175. package/src/ObservedCallSummary.ts +0 -22
  176. package/src/ObservedCertificate.ts +0 -43
  177. package/src/ObservedClient.ts +0 -788
  178. package/src/ObservedClientEventMonitor.ts +0 -309
  179. package/src/ObservedClientSummary.ts +0 -24
  180. package/src/ObservedCodec.ts +0 -49
  181. package/src/ObservedDataChannel.ts +0 -85
  182. package/src/ObservedIceCandidate.ts +0 -64
  183. package/src/ObservedIceCandidatePair.ts +0 -134
  184. package/src/ObservedIceTransport.ts +0 -99
  185. package/src/ObservedInboundRtp.ts +0 -205
  186. package/src/ObservedInboundTrack.ts +0 -75
  187. package/src/ObservedMediaPlayout.ts +0 -49
  188. package/src/ObservedMediaSource.ts +0 -60
  189. package/src/ObservedOutboundRtp.ts +0 -156
  190. package/src/ObservedOutboundTrack.ts +0 -74
  191. package/src/ObservedPeerConnection.ts +0 -1088
  192. package/src/ObservedPeerConnectionTransport.ts +0 -38
  193. package/src/ObservedRemoteInboundRtp.ts +0 -64
  194. package/src/ObservedRemoteOutboundRtp.ts +0 -65
  195. package/src/ObservedTURN.ts +0 -86
  196. package/src/ObservedTurnServer.ts +0 -72
  197. package/src/Observer.ts +0 -247
  198. package/src/ObserverEventMonitor.ts +0 -551
  199. package/src/ObserverSummary.ts +0 -15
  200. package/src/common/Middleware.ts +0 -84
  201. package/src/common/SingleExecutor.ts +0 -36
  202. package/src/common/logger.ts +0 -82
  203. package/src/common/types.ts +0 -4
  204. package/src/common/utils.ts +0 -69
  205. package/src/detectors/Detector.ts +0 -6
  206. package/src/detectors/Detectors.ts +0 -38
  207. package/src/index.ts +0 -28
  208. package/src/mediasoup/ObservedMediaRouter.ts +0 -10
  209. package/src/monitors/TurnUsageMonitor.ts +0 -187
  210. package/src/schema/ClientEventTypes.ts +0 -280
  211. package/src/schema/ClientMetaTypes.ts +0 -41
  212. package/src/schema/ClientSample.ts +0 -1682
  213. package/src/scores/CalculatedScore.ts +0 -6
  214. package/src/scores/DefaultCallScoreCalculator.ts +0 -23
  215. package/src/scores/ScoreCalculator.ts +0 -3
  216. package/src/updaters/CallUpdater.ts +0 -5
  217. package/src/updaters/ObserverUpdater.ts +0 -5
  218. package/src/updaters/OnAllCallObserverUpdater.ts +0 -59
  219. package/src/updaters/OnAllClientCallUpdater.ts +0 -61
  220. package/src/updaters/OnAnyCallObserverUpdater.ts +0 -41
  221. package/src/updaters/OnAnyClientCallUpdater.ts +0 -39
  222. package/src/updaters/OnIntervalUpdater.ts +0 -19
  223. package/src/updaters/Updater.ts +0 -5
  224. package/src/utils/MediasoupRemoteTrackResolver.ts +0 -155
  225. package/src/utils/RemoteTrackResolver.ts +0 -12
  226. package/tsconfig.json +0 -19
  227. package/tslint.json +0 -6
package/README.md CHANGED
@@ -1,23 +1,74 @@
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-beta`. The API described here is current and intended to be implemented
35
+ > against directly. This document is written to be self-sufficient: an engineer (or an AI
36
+ > agent) should be able to integrate the library, or develop it further, from this file alone.
37
+ > A companion doc, [`docs/logging.md`](./docs/logging.md), covers logging integration in depth.
38
+
39
+ > **Packaging:** server-side, **Node.js ≥ 22**, shipped as a **dual ESM + CommonJS** build — so it
40
+ > works whether your project uses `import` (ESM) or `require()` (CommonJS). Everything — including
41
+ > the built-in file sink — is exported from the single `@observertc/observer-js` entry.
42
+
43
+ > **For AI agents:** [`llms.txt`](./llms.txt) is a curated map of these docs (with a one-file
44
+ > [`llms-full.txt`](./llms-full.txt) export for one-shot ingestion); [`AGENTS.md`](./AGENTS.md)
45
+ > covers build/test commands and conventions for working **in** this repository. `llms-full.txt`
46
+ > ships in the npm package (so it's available wherever the library is installed); `llms.txt` and
47
+ > `AGENTS.md` live in the repo (and `llms.txt` belongs at the root of the docs site).
7
48
 
8
- This library is a core component of the ObserverTC ecosystem, designed to provide robust server-side monitoring capabilities for WebRTC applications.
49
+ ---
9
50
 
10
- ## Features
51
+ ## Table of contents
52
+
53
+ 1. [Installation](#installation)
54
+ 2. [Quick start](#quick-start)
55
+ 3. [Data flow](#data-flow)
56
+ 4. [Entity hierarchy](#entity-hierarchy)
57
+ 5. [Ingestion: `accept()`, context & lifecycle](#ingestion-accept-context--lifecycle)
58
+ 6. [Update policies](#update-policies)
59
+ 7. [The event bus](#the-event-bus) ← the core of the API
60
+ 8. [API reference](#api-reference)
61
+ 9. [Schema types (`ClientSample`)](#schema-types-clientsample)
62
+ 10. [Detectors (server-side extension point)](#detectors-server-side-extension-point)
63
+ 11. [Remote track resolution (mediasoup / SFU)](#remote-track-resolution-mediasoup--sfu)
64
+ 12. [Mediasoup router observation](#mediasoup-router-observation)
65
+ 13. [Sinks (per-client sample persistence)](#sinks-per-client-sample-persistence)
66
+ 14. [Injecting data into a client](#injecting-data-into-a-client)
67
+ 15. [Logging](#logging)
68
+ 16. [Error-handling philosophy](#error-handling-philosophy)
69
+ 17. [Development & extension guide](#development--extension-guide)
11
70
 
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`).
71
+ ---
21
72
 
22
73
  ## Installation
23
74
 
@@ -27,510 +78,1110 @@ npm install @observertc/observer-js
27
78
  yarn add @observertc/observer-js
28
79
  ```
29
80
 
30
- ## Quick Start
81
+ **Server-side, Node.js ≥ 22, dual ESM + CommonJS.** The package ships both module formats, so it
82
+ works the same whether your project is ESM or CommonJS — your import line is unchanged either way:
31
83
 
32
- ```typescript
33
- import { Observer, ObserverConfig } from '@observertc/observer-js';
34
- import { ClientSample } from '@observertc/schemas'; // Assuming you use the official schemas
84
+ ```ts
85
+ import { Observer, ClientSample, createJsonlFileSinkFactory } from '@observertc/observer-js';
86
+ ```
35
87
 
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);
88
+ In an ESM project this resolves to the `.mjs` build; in a CommonJS project (where TypeScript
89
+ compiles your `import` down to `require()`) it resolves to the `.js` build. Everything is exported
90
+ from the single `@observertc/observer-js` entry. Written in TypeScript; ships type declarations for
91
+ both formats (`dist/index.d.ts` for `require`, `dist/index.d.mts` for `import`). Runtime
92
+ dependencies: `@bufbuild/protobuf`, `events`, `uuid`. The library does **not** bundle a logger or
93
+ any transport — see [Logging](#logging).
43
94
 
44
- // 2. Listen to events
45
- observer.on('newcall', (call) => {
46
- console.log(`[Observer] New call detected: ${call.callId}`);
95
+ `ClientSample` and friends are re-exported from this package, and are also published as the
96
+ shared schema in [`@observertc/schemas`](https://github.com/observertc/schemas); samples
97
+ produced on the client (e.g. by `@observertc/client-monitor-js`) conform to the same shape.
47
98
 
48
- call.on('newclient', (client) => {
49
- console.log(`[Call: ${call.callId}] New client joined: ${client.clientId}`);
99
+ ---
100
+
101
+ ## Quick start
50
102
 
51
- client.on('issue', (issue) => {
52
- console.warn(`[Client: ${client.clientId}] Issue: ${issue.type} - ${issue.severity} - ${issue.description}`);
53
- });
54
- });
103
+ ```ts
104
+ import { Observer, ClientSample } from '@observertc/observer-js';
105
+
106
+ // 1. Create an observer.
107
+ const observer = new Observer({
108
+ // when the observer aggregates call/client metrics:
109
+ updatePolicy: 'update-when-all-call-updated',
110
+ // default policy applied to calls created automatically by accept():
111
+ defaultCallUpdatePolicy: 'update-on-any-client-updated',
112
+ // optional auto-teardown:
113
+ closeCallIfEmptyForMs: 20_000,
114
+ closeClientIfIdleForMs: 60_000,
115
+ });
55
116
 
56
- call.on('update', () => {
57
- console.log(
58
- `[Call: ${call.callId}] Metrics updated. Score: ${call.score?.toFixed(1)}, Clients: ${call.numberOfClients}`
59
- );
60
- });
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);
61
120
  });
62
121
 
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);
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
+ ```
81
143
 
82
- // 4. Cleanup when done
83
- // process.on('SIGINT', () => observer.close());
144
+ ---
145
+
146
+ ## Data flow
147
+
148
+ ```
149
+ client getStats() ──► ClientSample ──► observer.accept(sample, ctx?)
150
+ │
151
+ ┌────────────────────────────────┘
152
+ ▼
153
+ get-or-create ObservedCall ──► get-or-create ObservedClient ──► client.accept(sample, ctx)
154
+ │
155
+ per peerConnections[] in the sample
156
+ ▼
157
+ get-or-create ObservedPeerConnection
158
+ .accept(pcSample, ctx) updates all sub-stats,
159
+ derives deltas/bitrates/RTT, correlates remote RTP
160
+ │
161
+ metrics roll up: PeerConnection → Client → Call → Observer
162
+ │
163
+ events emitted on the Observer bus ──► your handlers
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
175
+
176
+ | Class | Created by | Keyed on its parent as | Holds |
177
+ |-------|-----------|------------------------|-------|
178
+ | `Observer` | `new Observer(config?)` | — (root) | `observedCalls: Map<string, ObservedCall>`, global counters, the event bus |
179
+ | `ObservedCall` | `observer.createObservedCall(settings)` / lazily by `accept` | `observedCalls` | `observedClients: Map<string, ObservedClient>`, call-wide metrics, `detectors`, `scoreCalculator` |
180
+ | `ObservedClient` | `call.createObservedClient(settings)` / lazily | `observedClients` | `observedPeerConnections: Map<string, ObservedPeerConnection>`, per-client metrics |
181
+ | `ObservedPeerConnection` | lazily, from `sample.peerConnections[]` | `observedPeerConnections` | the 15 sub-stat maps below, transport/RTT/bitrate metrics |
182
+ | Sub-stats | lazily, from the `PeerConnectionSample` | maps on the PC | individual WebRTC stat objects |
89
183
 
90
- The following sections provide a comprehensive guide to `observer-js`.
184
+ `ObservedPeerConnection` sub-stat maps (all `public readonly`):
91
185
 
92
- ### 1. General Description
186
+ ```
187
+ observedCertificates, observedCodecs, observedDataChannels,
188
+ observedIceCandidates, observedIceCandidatesPair, observedIceTransports,
189
+ observedInboundRtps, observedInboundTracks, observedMediaPlayouts,
190
+ observedMediaSources, observedOutboundRtps, observedOutboundTracks,
191
+ observedPeerConnectionTransports, observedRemoteInboundRtps, observedRemoteOutboundRtps
192
+ ```
93
193
 
94
- (This section is identical to the introductory paragraph at the top of this README)
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).
95
200
 
96
- ### 2. Core Concepts
201
+ ---
97
202
 
98
- #### 2.1. Data Flow
203
+ ## Ingestion: `accept()`, context & lifecycle
99
204
 
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.
205
+ ### `observer.accept(sample, context?)`
106
206
 
107
- #### 2.2. Entity Hierarchy
207
+ The single entry point. It:
108
208
 
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.
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)`.
116
216
 
117
- #### 2.3. Automatic Entity Creation
217
+ ### Accept middlewares (global pre-dispatch hook)
118
218
 
119
- When `observer.accept(sample)` is called:
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()`.
120
225
 
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.
226
+ ```ts
227
+ import { Observer, AcceptMiddleware } from '@observertc/observer-js';
124
228
 
125
- #### 2.4. Metrics Aggregation
229
+ const observer = new Observer();
126
230
 
127
- The library aggregates a wide array of metrics at each level of the hierarchy, including (but not limited to):
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
+ };
128
237
 
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
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
+ };
136
243
 
137
- #### 2.5. Issue Detection
244
+ observer.addAcceptMiddleware(route, filter);
245
+ // observer.removeAcceptMiddleware(route);
246
+ ```
138
247
 
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.
248
+ This is a lightweight global injection point, distinct from the larger (not-yet-built)
249
+ `ClientSampleProcessor` pipeline in the roadmap. When no middleware is registered, `accept()`
250
+ dispatches directly with no overhead.
140
251
 
141
- #### 2.6. Quality Scoring
252
+ ### `context` (the `AcceptContext`)
142
253
 
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).
254
+ ```ts
255
+ type AcceptContext = Record<string, unknown>;
256
+ ```
144
257
 
145
- #### 2.7. Event-Driven Architecture
258
+ A single, optional, free-form object threaded down the whole accept chain
259
+ (`Observer → Client → PeerConnection`). It is **transient request-scoped data** — temporary or
260
+ contextual information the application wants available while an update is processed.
146
261
 
147
- The library uses Node.js `EventEmitter` to signal various occurrences, allowing applications to react to changes in real-time.
262
+ `context` is **never written to `appData`** and is **not stored** on any entity. The two are
263
+ deliberately distinct:
148
264
 
149
- ### 3. API Reference
265
+ - **`appData`** — application-assigned extra info that identifies/decorates an entity, fixed at
266
+ creation (via `settings.appData` or the `createCallAppData` / `createClientAppData` factories),
267
+ or assigned by the app on the `*-added` events. The library never changes it.
268
+ - **`context`** — passed per `accept()`, may differ on every call, and is carried straight
269
+ through to the `*-updated` events that the `accept()` triggers, then discarded.
150
270
 
151
- #### 3.1. `Observer`
271
+ `client-updated` and `peer-connection-updated` carry the exact context of that sample;
272
+ `call-updated` carries the context of the client `accept()` that drove the call update (absent
273
+ for interval- or teardown-driven call updates). When no context is given, the field is absent.
152
274
 
153
- Manages all monitored calls and global observer state.
275
+ ### Get-or-create helpers
154
276
 
155
- **Configuration (`ObserverConfig`)**
277
+ If you want to create/configure entities yourself before/without samples:
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
+ ```ts
280
+ const call = observer.getOrCreateObservedCall({ callId, appData }); // ObservedCall | undefined
281
+ const client = call?.getOrCreateObservedClient({ clientId, appData }); // ObservedClient | undefined
165
282
  ```
166
283
 
167
- **Constructor**
284
+ These return `undefined` (and warn) when the parent is closed; `createObservedCall`/
285
+ `createObservedClient` return the **existing** instance (and warn) if the id already exists.
168
286
 
169
- ```typescript
170
- new Observer<AppData>(config?: ObserverConfig<AppData>)
171
- ```
287
+ ### Automatic teardown
172
288
 
173
- - `config`: Optional. Defaults: `updatePolicy: 'update-when-all-call-updated'`.
289
+ - `closeClientIfIdleForMs` — a client with no sample for this long auto-closes.
290
+ - `closeCallIfEmptyForMs` — a call with zero clients for this long auto-closes.
291
+ - Closing cascades down (call → clients → peer connections → sub-stats), unsubscribing
292
+ listeners and emitting the `*-closed` / `*-removed` events.
174
293
 
175
- **Key Properties**
294
+ ---
176
295
 
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.
296
+ ## Update policies
182
297
 
183
- **Key Methods**
298
+ "Update" means *recompute aggregated metrics and emit the `*-updated` event* at that level.
299
+ Both the observer and each call have a configurable trigger. Updates are **event-driven** — there
300
+ is no built-in timer. An app that wants a fixed cadence can call `observer.update()` /
301
+ `call.update()` from its own `setInterval`. With `'none'`, **nothing auto-updates** — the level
302
+ updates only when the application calls the public `update()` itself.
184
303
 
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.
304
+ **Observer-level** (`ObserverConfig.updatePolicy`, default `update-when-all-call-updated`):
191
305
 
192
- **Events (`ObserverEvents`)**
306
+ | Policy | Triggers `observer.update()` when… |
307
+ |--------|-------------------------------------|
308
+ | `update-on-any-call-updated` | any call updates |
309
+ | `update-when-all-call-updated` | every call has updated since the last observer update |
310
+ | `none` | never automatically — only when the app calls `observer.update()` |
193
311
 
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' ()`
312
+ **Call-level** (`ObservedCallSettings.updatePolicy`, defaulted from
313
+ `ObserverConfig.defaultCallUpdatePolicy`):
202
314
 
203
- #### 3.2. `ObservedCall`
315
+ | Policy | Triggers `call.update()` when… |
316
+ |--------|--------------------------------|
317
+ | `update-on-any-client-updated` | any client in the call updates |
318
+ | `update-when-all-client-updated` | every client has updated since the last call update |
319
+ | `none` | never automatically — only when the app calls `call.update()` |
204
320
 
205
- Represents a single call session.
321
+ ---
206
322
 
207
- **Configuration (`ObservedCallSettings`)**
323
+ ## The event bus
208
324
 
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
216
- };
325
+ This is the primary API. **Subscribe on the `Observer` instance** — it is the single emitter
326
+ for the entire hierarchy. The `ObservedCall` / `ObservedClient` / `ObservedPeerConnection`
327
+ objects are themselves `EventEmitter`s too, but those local events are reserved for internal
328
+ lifecycle/teardown wiring (see [Local lifecycle events](#local-lifecycle-events)); application
329
+ code should use the Observer bus.
330
+
331
+ ### Payload shape: ancestry + subject
332
+
333
+ Every Observer event delivers exactly **one argument: a payload object**. The payload always
334
+ contains the ancestry from the observer down to the entity that raised it, plus any event-
335
+ specific subject:
336
+
337
+ ```ts
338
+ type ObserverEventBase = { observer: Observer };
339
+ type ObservedCallScope = ObserverEventBase & { observedCall: ObservedCall };
340
+ type ObservedClientScope = ObservedCallScope & { observedClient: ObservedClient };
341
+ type ObservedPeerConnectionScope = ObservedClientScope & { observedPeerConnection: ObservedPeerConnection };
217
342
  ```
218
343
 
219
- **Key Properties**
344
+ So a peer-connection-level event hands you the observer, call, client, **and** peer connection:
220
345
 
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.
346
+ ```ts
347
+ observer.on('inbound-rtp-added', ({ observer, observedCall, observedClient, observedPeerConnection, observedInboundRtp }) => {
348
+ // all five are present and correctly typed
349
+ });
350
+ ```
227
351
 
228
- **Key Methods**
352
+ `observer.on/off/once/emit` are fully typed against the event map — the handler argument is
353
+ inferred per event name.
354
+
355
+ ### Event catalogue
356
+
357
+ All payloads include the ancestry for their level (above). The **Extra** column lists the
358
+ additional field(s) on top of that scope.
359
+
360
+ #### Observer level — scope `{ observer }`
361
+
362
+ | Event | Extra payload | Fires when |
363
+ |-------|---------------|-----------|
364
+ | `observer-updated` | — | `observer.update()` ran (per the observer update policy) |
365
+ | `observer-closed` | — | `observer.close()` |
366
+ | `sample-rejected` | `{ reason: 'observer-closed' \| 'missing-callId' \| 'missing-clientId', sample: ClientSample }` | a sample was dropped by `accept()` |
367
+
368
+ #### Mediasoup level — scope `{ observer, observedMediasoupRouter }`
369
+
370
+ | Event | Extra | Fires when |
371
+ |-------|-------|-----------|
372
+ | `mediasoup-router-added` | — | `observer.createObservedMediasoupRouter(...)` registered a router |
373
+ | `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`. |
374
+ | `mediasoup-router-removed` | — | the underlying mediasoup router closed (its `router.observer` `close` fired) |
375
+
376
+ See [Mediasoup router observation](#mediasoup-router-observation) for the full design and examples.
377
+
378
+ #### Call level — scope `{ observer, observedCall }`
379
+
380
+ | Event | Extra | Fires when |
381
+ |-------|-------|-----------|
382
+ | `call-added` | — | a call is created |
383
+ | `call-updated` | `{ context?: AcceptContext }` | `call.update()` ran |
384
+ | `call-closed` | — | the call closed |
385
+ | `call-empty` | — | last client left the call |
386
+ | `call-not-empty` | — | first client joined a previously-empty call |
387
+ | `call-issue` | `{ issue: ClientIssue }` | `call.addIssue(...)` (server-side detector finding) |
388
+
389
+ #### Client level — scope `{ observer, observedCall, observedClient }`
390
+
391
+ | Event | Extra | Fires when |
392
+ |-------|-------|-----------|
393
+ | `client-added` | — | a client is created |
394
+ | `client-sink-created` | `{ sink: ClientSampleSink }` | a per-client sink was created (only when `createClientSink` returns one); fires right after `client-added` |
395
+ | `client-updated` | `{ sample: ClientSample, elapsedTimeInMs: number, context?: AcceptContext }` | the client processed a sample |
396
+ | `client-closed` | — | the client closed |
397
+ | `client-joined` | — | first `CLIENT_JOINED` event seen |
398
+ | `client-left` | — | `CLIENT_LEFT` seen (or inferred on close) |
399
+ | `client-rejoined` | `{ timestamp: number }` | a later `CLIENT_JOINED` after an earlier join |
400
+ | `client-issue` | `{ issue: ClientIssue }` | a client-reported issue arrived, or `client.addIssue(...)` |
401
+ | `client-metadata` | `{ metaData: ClientMetaData }` | a client meta item arrived |
402
+ | `client-extension-stats` | `{ extensionStats: ExtensionStat }` | an app-defined extension stat arrived |
403
+ | `client-event` | `{ event: ClientEvent }` | any client event was processed |
404
+
405
+ #### Peer-connection level — scope `{ observer, observedCall, observedClient, observedPeerConnection }`
406
+
407
+ | Event | Extra | Notes |
408
+ |-------|-------|-------|
409
+ | `peer-connection-added` / `peer-connection-closed` | — | lifecycle of the PC |
410
+ | `peer-connection-updated` | `{ context?: AcceptContext }` | the PC processed a sample |
411
+ | `ice-connection-state-changed` / `ice-gathering-state-changed` / `connection-state-changed` | `{ state: string }` | driven by client events |
412
+ | `selected-candidate-pair-changed` | — | *declared; not currently emitted* |
413
+ | `inbound-track-added` / `-updated` / `-removed` / `-muted` / `-unmuted` | `{ observedInboundTrack }` | |
414
+ | `outbound-track-added` / `-updated` / `-removed` / `-muted` / `-unmuted` | `{ observedOutboundTrack }` | |
415
+ | `inbound-rtp-added` / `-updated` / `-removed` | `{ observedInboundRtp }` | `-updated` fires every tick |
416
+ | `outbound-rtp-added` / `-updated` / `-removed` | `{ observedOutboundRtp }` | `-updated` fires every tick |
417
+ | `remote-inbound-rtp-added` / `-updated` / `-removed` | `{ observedRemoteInboundRtp }` | |
418
+ | `remote-outbound-rtp-added` / `-updated` / `-removed` | `{ observedRemoteOutboundRtp }` | |
419
+ | `data-channel-added` / `-updated` / `-removed` | `{ observedDataChannel }` | |
420
+ | `ice-candidate-added` / `-updated` / `-removed` | `{ observedIceCandidate }` | |
421
+ | `ice-candidate-pair-added` / `-updated` / `-removed` | `{ observedIceCandidatePair }` | |
422
+ | `ice-transport-added` / `-updated` / `-removed` | `{ observedIceTransport }` | |
423
+ | `codec-added` / `-updated` / `-removed` | `{ observedCodec }` | |
424
+ | `media-source-added` / `-updated` / `-removed` | `{ observedMediaSource }` | |
425
+ | `media-playout-added` / `-updated` / `-removed` | `{ observedMediaPlayout }` | |
426
+ | `peer-connection-transport-added` / `-updated` / `-removed` | `{ observedPeerConnectionTransport }` | |
427
+ | `certificate-added` / `-updated` / `-removed` | `{ observedCertificate }` | |
428
+
429
+ > **Volume note.** The `*-updated` sub-stat events fire on every peer-connection `accept()`
430
+ > (i.e. per sample, per stream). For high-throughput servers, subscribe only to what you need,
431
+ > or read fields off the entities on `client-updated` / `call-updated` instead.
432
+
433
+ ### Local lifecycle events
434
+
435
+ These remain on the individual entities (not the bus), for teardown/coordination. You can
436
+ listen to them, but prefer the bus equivalents above for application logic.
437
+
438
+ | Entity | Local events |
439
+ |--------|--------------|
440
+ | `ObservedCall` | `update`, `newclient`, `empty`, `not-empty`, `close` |
441
+ | `ObservedClient` | `update` (`sample`, `elapsedTimeInMs`), `close`, `joined`, `left` |
442
+ | `ObservedPeerConnection` | `removed-inbound-track`, `removed-outbound-track`, `close` |
229
443
 
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>`
444
+ ---
445
+
446
+ ## API reference
447
+
448
+ ### `Observer`
235
449
 
236
- **Events (Emitted via `ObservedCall` instance)**
450
+ ```ts
451
+ new Observer<AppData>(config?: ObserverConfig<AppData>)
237
452
 
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' ()`
453
+ type ObserverConfig<AppData = Record<string, unknown>> = {
454
+ updatePolicy?: 'update-on-any-call-updated' | 'update-when-all-call-updated' | 'none';
455
+ defaultCallUpdatePolicy?: ObservedCallSettings['updatePolicy'];
456
+ appData?: AppData;
457
+ closeClientIfIdleForMs?: number;
458
+ closeCallIfEmptyForMs?: number;
459
+ // appData factories — run when an entity is created without explicit appData
460
+ // (incl. lazily by accept()). appData is application-owned; accept `context` never touches it.
461
+ createCallAppData?: (p: { callId: string; observer: Observer }) => Record<string, unknown>;
462
+ createClientAppData?: (p: { clientId: string; observedCall: ObservedCall }) => Record<string, unknown>;
463
+ // sink factory — produces a per-client sink that receives every accepted sample (see Sinks).
464
+ createClientSink?: (p: { clientId: string; observedCall: ObservedCall }) => ClientSampleSink | undefined;
465
+ // track-resolver factory — produces a call's RemoteTrackResolver (see Remote track resolution).
466
+ createTrackResolver?: (observedCall: ObservedCall) => RemoteTrackResolver | undefined;
467
+ };
468
+ ```
243
469
 
244
- #### 3.3. `ObservedClient`
470
+ **appData factories.** Instead of pre-creating a call/client (or assigning on `call-added` /
471
+ `client-added`) just to enrich its `appData`, register a factory once. It runs in the entity's
472
+ constructor whenever it's created without an explicit `settings.appData` — including the lazy
473
+ creation inside `accept()`. The `client` factory receives the already-created parent
474
+ `observedCall`, so it can derive fields from it. `appData` is application-owned and is never
475
+ modified by the `accept()` context.
476
+
477
+ ```ts
478
+ const observer = new Observer({
479
+ createCallAppData: ({ callId }) => ({ callId, startedAt: Date.now(), region: 'eu' }),
480
+ createClientAppData: ({ clientId, observedCall }) => ({ clientId, region: observedCall.appData.region }),
481
+ });
482
+ ```
245
483
 
246
- Represents a participant in a call.
484
+ Key members:
247
485
 
248
- **Configuration (`ObservedClientSettings`)**
486
+ - `accept(sample: ClientSample, context?: AcceptContext): void`
487
+ - `addAcceptMiddleware(...mw: AcceptMiddleware[]): this` / `removeAcceptMiddleware(...mw): this` — global pre-dispatch sample hooks (see [Accept middlewares](#accept-middlewares-global-pre-dispatch-hook))
488
+ - `getObservedCall<T>(callId): ObservedCall<T> | undefined`
489
+ - `createObservedCall<T>(settings): ObservedCall<T> | undefined`
490
+ - `getOrCreateObservedCall<T>(settings): ObservedCall<T> | undefined`
491
+ - `update(): void` — force an aggregation/`observer-updated` tick
492
+ - `close(): void`
493
+ - `readonly observedCalls: Map<string, ObservedCall>`
494
+ - `readonly observedTURN: ObservedTURN`
495
+ - `get appData()`, `get numberOfCalls()`
496
+ - counters: `numberOfClients`, `numberOfClientsUsingTurn`, `numberOfInboundRtpStreams`,
497
+ `numberOfOutboundRtpStreams`, `numberOfDataChannels`, `numberOfPeerConnections`,
498
+ `totalAddedCall`, `totalRemovedCall`, `closed`
499
+ - `on/off/once/emit` typed against the [event map](#event-catalogue)
500
+
501
+ ### `ObservedCall`
502
+
503
+ ```ts
504
+ type ObservedCallSettings<AppData = Record<string, unknown>> = {
505
+ updatePolicy?: 'update-on-any-client-updated' | 'update-when-all-client-updated' | 'none';
506
+ callId: string;
507
+ appData?: AppData;
508
+ closeCallIfEmptyForMs?: number;
509
+ };
510
+ ```
249
511
 
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
512
+ Key members:
513
+
514
+ - `readonly callId: string`, `appData: AppData`
515
+ - `readonly observedClients: Map<string, ObservedClient>`, `get numberOfClients()`
516
+ - `getObservedClient<T>(clientId)`, `createObservedClient<T>(settings)`, `getOrCreateObservedClient<T>(settings)` (all `… | undefined`)
517
+ - `addIssue(issue: ClientIssue): void` — raise a **call-level** issue → emits `call-issue`
518
+ - `readonly detectors: Detectors` — server-side detector registry (empty by default; see [Detectors](#detectors-server-side-extension-point))
519
+ - `scoreCalculator: ScoreCalculator`, `get score()`, `readonly calculatedScore`
520
+ - `remoteTrackResolver?: RemoteTrackResolver` — set from `ObserverConfig.createTrackResolver` at call creation (see [Remote track resolution](#remote-track-resolution-mediasoup--sfu))
521
+ - aggregates: `numberOfIssues`, `numberOfPeerConnections`, `numberOfInboundRtpStreams`,
522
+ `numberOfOutboundRtpStreams`, `numberOfDataChannels`, `maxNumberOfClients`,
523
+ `clientsUsedTurn: Set<string>`, `startedAt?`, `endedAt?`, `closedAt?`, `closed`
524
+ - `update()`, `close()`
525
+
526
+ ### `ObservedClient`
527
+
528
+ ```ts
529
+ type ObservedClientSettings<AppData = Record<string, unknown>> = {
530
+ clientId: string;
531
+ appData?: AppData;
532
+ closeClientIfIdleForMs?: number;
255
533
  };
256
534
  ```
257
535
 
258
- **Key Properties**
536
+ Key members:
537
+
538
+ - `readonly clientId: string`, `appData: AppData`, `readonly call: ObservedCall`
539
+ - `readonly observedPeerConnections: Map<string, ObservedPeerConnection>`
540
+ - `readonly sink?: ClientSampleSink` — the per-client sink (see [Sinks](#sinks-per-client-sample-persistence)), if `createClientSink` is configured; listen on it for `close`/`error`
541
+ - **Injection API** (queue app data to be merged into the next sample processing):
542
+ `injectEvent(ClientEvent)`, `injectIssue(ClientIssue)`, `injectMetaData(ClientMetaData)`,
543
+ `injectExtensionStat(ExtensionStat)`, `injectAttachment(attachments: Record<string, unknown>)`
544
+ - **Direct add API** (process immediately): `addIssue(ClientIssue)`, `addMetadata(ClientMetaData)`,
545
+ `addExtensionStats(ExtensionStat)`
546
+ - Metrics (current/derived): `currentAvgRttInMs?`, `currentMinRttInMs?`, `currentMaxRttInMs?`,
547
+ `receivingAudioBitrate`, `receivingVideoBitrate`, `sendingAudioBitrate`, `sendingVideoBitrate`,
548
+ `usingTURN`, `usingTCP`, `availableIncomingBitrate`, `availableOutgoingBitrate`
549
+ - Counts: `numberOfInboundRtpStreams`, `numberOfOutboundRtpStreams`, `numberOfInbundTracks`,
550
+ `numberOfOutboundTracks`, `numberOfDataChannels`, `numberOfPeerConnections`
551
+ - Per-tick deltas: `deltaReceivedAudioBytes`, `deltaSentAudioBytes`, … (see source for the full set)
552
+ - Lifecycle: `joinedAt?`, `leftAt?`, `closedAt?`, `closed`, `get score()`
553
+ - Metadata: `browser?`, `engine?`, `platform?`, `operationSystem?`, `mediaDevices`, `mediaConstraints`
554
+ - `accept(sample, context?)`, `close()`
555
+
556
+ ### `ObservedPeerConnection`
557
+
558
+ Key members:
559
+
560
+ - `readonly peerConnectionId: string`, `readonly client: ObservedClient`, `appData?`
561
+ - The 15 `observed*` sub-stat `Map`s (listed [above](#entity-hierarchy)), plus array getters:
562
+ `codecs`, `inboundRtps`, `outboundRtps`, `remoteInboundRtps`, `remoteOutboundRtps`,
563
+ `mediaSources`, `mediaPlayouts`, `dataChannels`, `peerConnectionTransports`, `iceTransports`,
564
+ `iceCandidates`, `iceCandidatePairs`, `certificates`, `selectedIceCandidatePairs`,
565
+ `selectedIceCandiadtePairForTurn`
566
+ - State: `connectionState?`, `iceConnectionState?`, `iceGatheringState?`, `usingTURN`, `usingTCP`
567
+ - Metrics: `currentRttInMs?`, `currentJitter?`, `availableIncomingBitrate`,
568
+ `availableOutgoingBitrate`, sending/receiving bitrates, packet rates, and `total*` / `delta*`
569
+ byte/packet counters
570
+ - `accept(pcSample, context?)`, `close()`, `get score()`
571
+
572
+ **Remote-RTP correlation (derived).** During `accept()`, receiver/sender reports are linked
573
+ to the local streams by `remoteId` (fallback SSRC) and surfaced as fields:
574
+
575
+ - on `ObservedOutboundRtp`: `remoteRttInMs?`, `remoteFractionLost?`, `remoteJitter?`, `remotePacketsLost?`
576
+ - on `ObservedInboundRtp`: `remoteRttInMs?`, `remoteBytesSent?`, `remotePacketsSent?`, `remoteTimestamp?`
577
+
578
+ These are reset each tick and only set when the matching remote report is present.
579
+
580
+ ---
259
581
 
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.
582
+ ## Schema types (`ClientSample`)
583
+
584
+ The shape of an accepted sample (re-exported from this package; identical to
585
+ `@observertc/schemas`). Only the top level is shown — each stat object mirrors the standard
586
+ WebRTC `getStats()` dictionaries plus a few extensions.
587
+
588
+ ```ts
589
+ type ClientSample = {
590
+ timestamp: number; // client wall-clock (ms epoch)
591
+ callId?: string; // set by you or the library
592
+ clientId?: string; // set by you or the library
593
+ score?: number; // optional client-computed score (0..5)
594
+ attachments?: Record<string, unknown>;
595
+ peerConnections?: PeerConnectionSample[];
596
+ clientEvents?: ClientEvent[];
597
+ clientIssues?: ClientIssue[];
598
+ clientMetaItems?: ClientMetaData[];
599
+ extensionStats?: ExtensionStat[];
600
+ };
268
601
 
269
- **Key Methods**
602
+ type PeerConnectionSample = {
603
+ peerConnectionId: string;
604
+ attachments?: Record<string, unknown>; // e.g. { direction: 'send'|'recv', producerId, consumerId, label }
605
+ score?: number;
606
+ inboundTracks?; outboundTracks?;
607
+ codecs?;
608
+ inboundRtps?; remoteInboundRtps?;
609
+ outboundRtps?; remoteOutboundRtps?;
610
+ mediaSources?; mediaPlayouts?;
611
+ peerConnectionTransports?; dataChannels?;
612
+ iceTransports?; iceCandidates?; iceCandidatePairs?;
613
+ certificates?;
614
+ };
270
615
 
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>`
616
+ type ClientEvent = { type: string; payload?: string; timestamp?: number; /* +ids */ };
617
+ type ClientIssue = { type: string; payload?: string; timestamp?: number }; // also used for call-issue
618
+ type ClientMetaData = { type: string; payload?: string; timestamp?: number; /* +ids */ };
619
+ type ExtensionStat = { type: string; payload?: string };
620
+ ```
621
+
622
+ `payload` fields are JSON strings; the library parses the ones it understands.
623
+
624
+ **`ClientEventTypes`** (enum of known `event.type` values): `CLIENT_JOINED`, `CLIENT_LEFT`,
625
+ `PEER_CONNECTION_OPENED/CLOSED/STATE_CHANGED`, `MEDIA_TRACK_ADDED/REMOVED/MUTED/UNMUTED/RESUMED`,
626
+ `ICE_GATHERING_STATE_CHANGED`, `ICE_CONNECTION_STATE_CHANGED`, `DATA_CHANNEL_OPEN/CLOSED/ERROR`,
627
+ `NEGOTIATION_NEEDED`, `SIGNALING_STATE_CHANGE`, `ICE_CANDIDATE`, `ICE_CANDIDATE_ERROR`, and the
628
+ mediasoup set `PRODUCER_*` / `CONSUMER_*` / `DATA_PRODUCER_*` / `DATA_CONSUMER_*`.
629
+
630
+ **`ClientMetaTypes`** (enum of known meta `type` values): `MEDIA_CONSTRAINT`, `MEDIA_DEVICE`,
631
+ `MEDIA_DEVICES_SUPPORTED_CONSTRAINTS`, `USER_MEDIA_ERROR`, `LOCAL_SDP`, `OPERATION_SYSTEM`,
632
+ `ENGINE`, `PLATFORM`, `BROWSER`.
633
+
634
+ ### Worked example: a real `ClientSample`
635
+
636
+ Two consecutive samples from one participant ("Guest" in room `qq0iwfnd`) of an
637
+ edumeet/mediasoup call show what actually flows through `accept()`: a rich **join snapshot**,
638
+ then lean **steady-state ticks**.
639
+
640
+ **Sample 1 — the join snapshot.** Carries the one-off lifecycle `clientEvents` and device
641
+ `clientMetaItems` alongside the first stats. (Abbreviated; ids and times are from the real log.)
642
+
643
+ ```jsonc
644
+ {
645
+ "timestamp": 1780572332518,
646
+ "callId": "d3dbf2f5-79be-4cb8-9d43-fb404f07ef27",
647
+ "clientId": "c926983c-4468-4046-ae8c-a9cabe1a1868",
648
+ "score": 0, // no quality measured yet on the join tick
649
+ "attachments": { "displayName": "Guest", "roomId": "qq0iwfnd", "actualSessionId": "d3dbf2f5-…" },
650
+
651
+ "clientEvents": [ // chronological lifecycle (12 in the real sample)
652
+ { "type": "CLIENT_JOINED", "timestamp": 1780572324515 },
653
+ { "type": "PEER_CONNECTION_OPENED", "timestamp": 1780572326790 }, // pc=b81c8d9d (media)
654
+ { "type": "ICE_GATHERING_STATE_CHANGED", "timestamp": 1780572326811 }, // → gathering
655
+ { "type": "PEER_CONNECTION_STATE_CHANGED", "timestamp": 1780572326812 }, // → connecting
656
+ { "type": "PRODUCER_ADDED", "timestamp": 1780572326821 }, // producer=1abdaf82 (audio)
657
+ { "type": "MEDIA_TRACK_ADDED", "timestamp": 1780572326821 }, // track=36ae42df (audio)
658
+ { "type": "PEER_CONNECTION_STATE_CHANGED", "timestamp": 1780572326827 }, // → connected
659
+ { "type": "PRODUCER_ADDED", "timestamp": 1780572326837 }, // producer=ba06a35b (video)
660
+ { "type": "DATA_PRODUCER_CREATED", "timestamp": 1780572326853 }
661
+ ],
662
+
663
+ "clientMetaItems": [ // environment & devices, one-off (10 in the real sample)
664
+ { "type": "USER_AGENT_DATA", "payload": "{…Chrome 148 / macOS…}" },
665
+ { "type": "MEDIA_DEVICE", "payload": "{…\"BRIO 4K Stream Edition\"…}" }
666
+ // …mic / camera / speaker devices…
667
+ ],
668
+
669
+ "peerConnections": [
670
+ {
671
+ "peerConnectionId": "b81c8d9d-…", // the media PC — Guest publishes to the SFU
672
+ "outboundRtps": [ /* audio + video */ ],
673
+ "outboundTracks": [ /* mic + camera: label, settings, capabilities */ ],
674
+ "remoteInboundRtps": [ /* RTCP feedback from the SFU */ ],
675
+ "codecs": [ /* … */ ], "iceTransports": [ /* … */ ],
676
+ "iceCandidatePairs": [ /* … */ ], "dataChannels": [ /* … */ ]
677
+ },
678
+ { "peerConnectionId": "8635acb7-…", "peerConnectionTransports": [ /* … */ ] } // signaling-only PC
679
+ ]
680
+ }
681
+ ```
277
682
 
278
- **Events (Emitted via `ObservedClient` instance)**
683
+ What `accept()` does with it, in order — each step emits on the bus with full ancestry:
279
684
 
280
- - `'joined' ()`
281
- - `'left' ()`
282
- - `'update' ()`
283
- - `'close' ()`
284
- - `'newpeerconnection' (pc: ObservedPeerConnection)`
285
- - `'issue' (issue: ClientIssue)` (and other specific issue events)
685
+ 1. lazily creates the `ObservedCall` → **`call-added`**;
686
+ 2. creates the `ObservedClient` → **`client-added`**, then **`client-joined`** (from `CLIENT_JOINED`);
687
+ 3. creates an `ObservedPeerConnection` per entry → **`peer-connection-added`** (×2 here);
688
+ 4. creates an `ObservedOutboundTrack` per track → **`outbound-track-added`**, plus the matching
689
+ **`outbound-rtp-added`**;
690
+ 5. replays the device list as **`client-metadata`** events and the lifecycle items as
691
+ **`client-event`**; and finally **`client-updated`** for the whole tick.
286
692
 
287
- #### 3.4. `ObservedPeerConnection`
693
+ `attachments.roomId` lands on `observedClient.attachments` (read it on `client-updated`, **not** at
694
+ creation — see [Ingestion](#ingestion-accept-context--lifecycle)).
288
695
 
289
- Represents an `RTCPeerConnection`.
696
+ **Sample 2 — a steady-state tick** (~8 s later): same `callId` / `clientId`, **no** new
697
+ `clientEvents` or `clientMetaItems`, just refreshed `peerConnections` stats. Each PC now scores `5`
698
+ and the aggregate client `score` is `4.74` — a healthy call. This is the shape of nearly every
699
+ sample: each tick refreshes metrics and fires the `*-updated` events, while the heavy join
700
+ snapshot happens only once.
290
701
 
291
- - Tracks ICE connection state, data channel stats, stream stats.
292
- - Holds `ObservedInboundRtpStream`, `ObservedOutboundRtpStream`, and `ObservedDataChannel` instances.
702
+ ---
293
703
 
294
- #### 3.5. `ObservedInboundRtpStream` / `ObservedOutboundRtpStream`
704
+ ## Detectors (server-side extension point)
295
705
 
296
- - Track metrics for individual media streams (audio/video) like codec, packets lost/received, jitter, bytes, etc.
706
+ `observer-js` deliberately ships **no built-in detectors**. Per-client signals — packet loss,
707
+ jitter, RTT, freezes, etc. — are already detectable on the client and arrive on samples as
708
+ `clientIssues` (surfaced via `client-issue`). Server-side detection should focus on what only
709
+ the server can see by **correlating data across the clients of a call**.
297
710
 
298
- #### 3.6. `ObservedDataChannel`
711
+ The hook lives on **`ObservedCall`**:
299
712
 
300
- - Tracks metrics for data channels like state, messages sent/received, bytes.
713
+ ```ts
714
+ import { Observer, Detector } from '@observertc/observer-js';
301
715
 
302
- #### 3.7. `ClientSample` (Schema)
716
+ class MyCrossClientDetector implements Detector {
717
+ readonly name = 'my-detector';
718
+ constructor(private readonly call /* : ObservedCall */) {}
719
+ update() { // called on every call.update()
720
+ // …inspect this.call.observedClients across participants…
721
+ if (/* condition only visible server-side */ false) {
722
+ this.call.addIssue({ type: this.name, payload: JSON.stringify({ /* … */ }), timestamp: Date.now() });
723
+ // → emitted on the bus as 'call-issue'
724
+ }
725
+ }
726
+ }
303
727
 
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:
728
+ const observer = new Observer();
729
+ observer.on('call-added', ({ observedCall }) => {
730
+ observedCall.detectors.add(new MyCrossClientDetector(observedCall));
731
+ });
732
+ observer.on('call-issue', ({ observedCall, issue }) => { /* react */ });
733
+ ```
305
734
 
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)
735
+ `Detector` interface and the registry:
322
736
 
323
- _(Refer to the [observertc/schemas](https://github.com/observertc/schemas) repository, particularly the `ClientSample.ts` definition, for the exact and complete structure.)_
737
+ ```ts
738
+ interface Detector { readonly name: string; update(): void; }
324
739
 
325
- ### 4. Configuration Possibilities
740
+ class Detectors {
741
+ add(d: Detector): void;
742
+ remove(d: Detector): void;
743
+ clear(): void;
744
+ update(): void; // called by ObservedCall.update(); guards each detector in try/catch
745
+ get listOfNames(): string[];
746
+ }
747
+ ```
326
748
 
327
- #### 4.1. Update Policies
749
+ ---
328
750
 
329
- Control how frequently entities re-calculate metrics and emit `update` events.
751
+ ## Remote track resolution (mediasoup / SFU)
330
752
 
331
- **Observer Level (`ObserverConfig.updatePolicy`)**
753
+ In an SFU, one participant's **outbound** track is delivered to other participants as **inbound**
754
+ tracks (one **publisher** → many **subscribers**). Correlation is **opt-in** per observer: set
755
+ `ObserverConfig.createTrackResolver`, a factory invoked when each call is created that returns the
756
+ call's `RemoteTrackResolver` (or `undefined` for none).
332
757
 
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`.
758
+ `RemoteTrackResolver` is a generic, strategy-driven class. It subscribes to the bus (filtered to
759
+ its call) and links tracks by **publisher id** — the link key — maintaining the links directly on
760
+ the tracks: `inboundTrack.remoteOutboundTrack` and `outboundTrack.remoteInboundTracks: Set`.
336
761
 
337
- **Call Level (`ObservedCallSettings.updatePolicy` or `ObserverConfig.defaultCallUpdatePolicy`)**
762
+ ```ts
763
+ import { Observer, createDefaultMediasoupRemoteTrackResolverFactory } from '@observertc/observer-js';
338
764
 
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`.
765
+ const observer = new Observer({
766
+ createTrackResolver: createDefaultMediasoupRemoteTrackResolverFactory(),
767
+ });
342
768
 
343
- #### 4.2. Intervals
769
+ // later, given tracks (links are kept up to date as tracks come and go):
770
+ const source = inboundTrack.remoteOutboundTrack; // the publishing ObservedOutboundTrack
771
+ const receivers = [ ...outboundTrack.remoteInboundTracks ]; // the subscribing ObservedInboundTrack[]
772
+ ```
344
773
 
345
- - `ObserverConfig.updateIntervalInMs`
346
- - `ObserverConfig.defaultCallUpdateIntervalInMs`
347
- - `ObservedCallSettings.updateIntervalInMs`
774
+ Two built-in factories ship: `createDefaultMediasoupRemoteTrackResolverFactory()` (publisher =
775
+ `attachments.producerId`, subscriber = `attachments.consumerId`) and
776
+ `createP2pRemoteTrackResolverFactory()` (matches by RTP **SSRC**, preserved end-to-end in p2p).
348
777
 
349
- #### 4.3. Application Data (`appData`)
778
+ For any other topology, build a `RemoteTrackResolver` with your own key resolvers — the publisher
779
+ id is just whatever links a subscribed track to the published one:
350
780
 
351
- Associate custom context with `Observer`, `ObservedCall`, and `ObservedClient` instances using generics.
781
+ ```ts
782
+ import { Observer, RemoteTrackResolver } from '@observertc/observer-js';
352
783
 
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() },
784
+ const observer = new Observer({
785
+ createTrackResolver: (observedCall) => new RemoteTrackResolver(observedCall, {
786
+ resolveOutboundTrackPublisherId: (out) => out.attachments?.mediaId as string | undefined,
787
+ resolveInboundTrackPublisherId: (inb) => inb.attachments?.mediaId as string | undefined,
788
+ resolveInboundTrackSubscriberId: (inb) => inb.attachments?.subId as string | undefined, // optional
789
+ }),
361
790
  });
362
- console.log(call.appData?.meetingTitle);
363
791
  ```
364
792
 
365
- #### 4.4. `appData` vs. Attachments
793
+ For the mediasoup factory, the application puts `producerId` / `consumerId` (and optionally
794
+ `direction`, `label`) into the track `attachments`.
366
795
 
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.
796
+ ---
368
797
 
369
- **`appData` (Application Data)**
798
+ ## Mediasoup router observation
799
+
800
+ Everything above is built from the **client-reported** `ClientSample`. When you run a
801
+ [mediasoup](https://mediasoup.org) SFU you also have the **server's own** ground truth — its
802
+ routers, transports, producers, consumers and data channels, with exact lifetimes and state
803
+ transitions. `ObservedMediasoupRouter` captures that server-side view into a
804
+ **`MediasoupRouterSample`**, completely independent of the client sample pipeline.
805
+
806
+ ### The concept
807
+
808
+ You hand the observer a live mediasoup `Router`; it attaches to mediasoup's own `observer` API and,
809
+ from then on, **passively records** the router's topology and lifecycle — with no polling and no
810
+ changes to your media code:
811
+
812
+ - new transports (`webrtc` / `plain` / `pipe` / `direct`), their selected `tuple`, and ICE state
813
+ transitions;
814
+ - producers (codec, SSRCs/RIDs, `pause`/`resume`) and consumers (`pause`/`resume`,
815
+ `producerPaused`/`producerResumed`);
816
+ - data producers and data consumers;
817
+ - `createdAt` / `closedAt` for every entity above.
818
+
819
+ All of it accumulates on `observedMediasoupRouter.sample` (a `MediasoupRouterSample` — see
820
+ [`src/schema/MediasoupRouter.ts`](./src/schema/MediasoupRouter.ts)). This is a *Sample*, not a
821
+ *Report*: it mirrors the naming of `ClientSample` and is yours to snapshot, persist, or correlate.
822
+
823
+ ### Matching peer connections — by **event**, not by storage
824
+
825
+ The observer correlates the SFU side with the client side **at the peer-connection level**: a
826
+ mediasoup WebRTC transport and a client's `RTCPeerConnection` share the same id, so whenever an
827
+ observed peer connection's id matches one of the router's WebRTC transport ids, that's a match.
828
+
829
+ **The observer does not store the router (or its sample) on any entity.** Instead, for **every**
830
+ matching peer connection it emits **`mediasoup-router-matched-with-peer-connection`** and steps
831
+ back — *your application* decides what the pairing means. The payload carries the full peer-connection
832
+ ancestry, so you get the router **and** the matched `observedPeerConnection`, `observedClient` and
833
+ `observedCall` in one place. Stamp the `routerId` into the peer connection's / client's `appData`,
834
+ build your own index, attach the server sample to the call in your database — whatever fits.
835
+
836
+ This matching is **opt-in**: pass `matchPeerConnectionByWebRtcTransportId: true` to
837
+ `createObservedMediasoupRouter`. When enabled, as peer connections are observed
838
+ (`peer-connection-added`) the observer checks whether the peer connection's id is one of the router's
839
+ WebRTC transport ids; on a hit it emits — once per matching peer connection — and keeps watching, so a
840
+ router serving many participants emits one match per participant's transport. When the flag is omitted
841
+ or `false`, no matching is performed and the event never fires. The internal listener is removed
842
+ automatically when the router closes or the observer closes.
843
+
844
+ When the underlying mediasoup router closes, its `close` propagates to `ObservedMediasoupRouter`,
845
+ which emits **`mediasoup-router-removed`**. That is your cue to do whatever cleanup or persistence
846
+ you want with the now-final `sample` — again, the observer itself keeps nothing.
847
+
848
+ ### Options — `observer.createObservedMediasoupRouter(settings)`
849
+
850
+ | Field | Type | Required | Meaning |
851
+ |-------|------|----------|---------|
852
+ | `router` | `mediasoup.types.Router` | yes | the live router to observe; the observer attaches to `router.observer` |
853
+ | `routerId` | `string` | yes | your id for the router (the sample also carries `router.id`) |
854
+ | `appData` | `Record<string, unknown>` | no | application-owned bag on the `ObservedMediasoupRouter` |
855
+ | `attachments` | `Record<string, unknown>` | no | free-form data copied onto `sample.attachments` |
856
+ | `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 |
857
+
858
+ Peer-connection matching is **off by default**; enable it with
859
+ `matchPeerConnectionByWebRtcTransportId: true`. Returns the `ObservedMediasoupRouter`, or `undefined`
860
+ if the observer is closed (a router with the same id returns the existing instance — both warn).
861
+
862
+ Useful members on the returned object: `.sample` (the `MediasoupRouterSample`), `.appData`,
863
+ `.attachments` (getter over `sample.attachments`), `.webrtcTransportIds: Set<string>`, `.id`,
864
+ `.close()`.
865
+
866
+ ### Example
867
+
868
+ ```ts
869
+ import { Observer } from '@observertc/observer-js';
870
+ import type { ObservedMediasoupRouterScope, ObservedPeerConnectionScope } from '@observertc/observer-js';
871
+
872
+ const observer = new Observer();
873
+
874
+ // 1) Feed client samples as usual so the observer knows about calls, clients & peer connections.
875
+ // (e.g. transport-layer: observer.accept(clientSample, context))
876
+
877
+ // 2) Observe the SFU side, opting in to peer-connection matching for this router's transports.
878
+ const router = /* your mediasoup router */ undefined as any;
879
+ const observedRouter = observer.createObservedMediasoupRouter({
880
+ router,
881
+ routerId: router.id,
882
+ matchPeerConnectionByWebRtcTransportId: true,
883
+ });
370
884
 
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`).
885
+ // 3) Every peer connection whose id matches one of the router's WebRTC transport ids fires this —
886
+ // WE decide what to do with each pairing. The payload carries the full ancestry.
887
+ observer.on('mediasoup-router-matched-with-peer-connection',
888
+ ({ observedMediasoupRouter, observedCall, observedClient, observedPeerConnection }:
889
+ ObservedMediasoupRouterScope & ObservedPeerConnectionScope) => {
890
+ // e.g. remember which router serves this peer connection / client…
891
+ (observedPeerConnection.appData ??= {}).routerId = observedMediasoupRouter.id;
892
+ // …or index the server sample by call in your own store:
893
+ myStore.linkRouterToCall(observedCall.callId, observedMediasoupRouter.sample);
894
+ },
895
+ );
896
+
897
+ // 4) The router closed — WE decide what to persist/forward with the final sample.
898
+ observer.on('mediasoup-router-removed', ({ observedMediasoupRouter }: ObservedMediasoupRouterScope) => {
899
+ myStore.saveRouterSample(observedMediasoupRouter.sample);
900
+ });
379
901
 
380
- **`attachments` (Arbitrary Attachments)**
902
+ // (optional) react to the router being registered at all:
903
+ observer.on('mediasoup-router-added', ({ observedMediasoupRouter }) => {
904
+ console.log('observing router', observedMediasoupRouter.id);
905
+ });
906
+ ```
381
907
 
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.
908
+ ### Why event-driven instead of storing on the call
391
909
 
392
- **When to Use Which:**
910
+ - **Loose coupling.** The call model stays about client telemetry; the SFU view lives on its own
911
+ object and is associated only if and how *you* choose.
912
+ - **You own the association.** One router serves many peer connections (across clients and calls),
913
+ and the right place to keep that mapping is application-specific — so the observer hands you each
914
+ peer-connection match and the final sample, and gets out of the way.
915
+ - **No silent accumulation.** Nothing is appended to `ObservedCall`, so there is no hidden growth or
916
+ lifetime you have to reason about; the router sample lives exactly as long as you keep a reference.
393
917
 
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.
918
+ ---
401
919
 
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.
920
+ ## Sinks (per-client sample persistence)
403
921
 
404
- #### 4.5. Remote Track Resolution
922
+ A **sink** receives the samples a client accepts — for archival, streaming, or later offline
923
+ replay. Each `ObservedClient` gets its **own** sink, produced by the
924
+ `ObserverConfig.createClientSink` factory when the client is created (return `undefined` for no
925
+ sink). The client pushes every accepted sample to its sink, and `end()`s it on close.
405
926
 
406
- For SFU scenarios, especially with MediaSoup:
407
- `ObservedCallSettings.remoteTrackResolvePolicy: 'mediasoup-sfu'`
927
+ ### The `ClientSampleSink` base class
408
928
 
409
- ### 5. Examples
929
+ `ClientSampleSink` is an **abstract base class** (a typed `EventEmitter`). You create a sink by
930
+ **subclassing it** and implementing `write` and `end`. It is **object-mode**: `write` receives
931
+ the `ClientSample` *object*, so each sink decides how (or whether) to serialize it — JSON line,
932
+ protobuf, a remote POST body, an in-memory push, etc.
410
933
 
411
- #### 5.1. Basic Observer Setup & Sample Ingestion
934
+ ```ts
935
+ import { ClientSampleSink, ClientSample } from '@observertc/observer-js';
412
936
 
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
937
+ abstract class ClientSampleSink /* extends EventEmitter */ {
938
+ abstract write(sample: ClientSample): boolean; // accept one sample; `false` = backpressure
939
+ abstract end(): void; // flush; emit `close` when the destination is ready
417
940
 
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
- });
941
+ // typed events (inherited): the listener signature is inferred from the event name
942
+ on(event: 'close' | 'finish' | 'drain', listener: () => void): this;
943
+ on(event: 'error', listener: (err: Error) => void): this;
944
+ // ...and the matching `once` / `off` / `emit`
945
+ }
946
+ ```
947
+
948
+ | Event | Meaning |
949
+ |-------|---------|
950
+ | `close` | the destination is fully written and closed (e.g. a file flushed and its fd closed) — "ready" |
951
+ | `error` | the destination failed |
952
+ | `finish` | `end()` was processed and queued data flushed (before `close`) |
953
+ | `drain` | the buffer drained after backpressure; safe to write more |
954
+
955
+ The library calls `write(sample)` **synchronously** per accepted sample (it is not awaited),
956
+ `end()`s the sink when the client closes, and attaches an `error` listener so a failing sink
957
+ can't crash the process (it also catches throws from `write`/`end`). The application — which
958
+ created the sink — listens for `close` (destination ready) and `error`. Because `write` isn't
959
+ awaited in the `accept()` hot path, **backpressure and batching are the sink's concern**.
960
+
961
+ ### Built-in sinks
962
+
963
+ ```ts
964
+ import { Observer, createJsonlFileSinkFactory } from '@observertc/observer-js';
965
+
966
+ const observer = new Observer({
967
+ // one ./stats/<callId>__<clientId>.jsonl per client
968
+ createClientSink: createJsonlFileSinkFactory({ directory: './stats' }),
439
969
  });
440
970
 
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
971
+ // React when a sink is created for a client:
972
+ observer.on('client-sink-created', ({ observedClient, sink }) => {
973
+ sink.on('close', () => {
974
+ // the file is fully flushed and its fd closed — ready to upload, move, etc.
975
+ });
976
+ });
977
+ ```
978
+
979
+ | Export | Signature | Notes |
980
+ |--------|-----------|-------|
981
+ | `createJsonlFileSinkFactory` | `({ directory, flags?, getFileName?, serializeSample? }) => ClientSampleSinkFactory` | per-client JSONL files; path defaults to `${callId}__${clientId}.jsonl` under `directory` (which **must exist**) |
982
+ | `createJsonlFileSink` | `({ path, flags?, serializeSample? }) => ClientSampleSink` | a single JSONL file; wraps `fs.WriteStream` and re-emits its `close`/`finish`/`drain`/`error` |
983
+ | `JsonlFileSink` | `class extends ClientSampleSink` | the underlying class; exposes `readonly path` so a `close` handler knows which file is ready |
984
+ | `createInMemorySink` / `InMemorySink` | `(samples?: ClientSample[]) => InMemorySink` | collects the accepted **sample objects** into `.samples: ClientSample[]`; emits `close` on `end()` |
985
+
986
+ `serializeSample?: (sample: ClientSample) => string` overrides the default `JSON.stringify` for
987
+ the JSONL sinks (e.g. to redact or reshape before writing).
988
+
989
+ ### Reading sink-specific info (e.g. the file path)
990
+
991
+ The bus hands you the sink as the base `ClientSampleSink`. To read information specific to a sink
992
+ type — for a file sink, where it was written — **narrow with `instanceof`** and read the sink's
993
+ public fields. `JsonlFileSink` exposes `path`:
994
+
995
+ ```ts
996
+ import { JsonlFileSink } from '@observertc/observer-js';
997
+
998
+ observer.on('client-sink-created', ({ observedClient, sink }) => {
999
+ if (sink instanceof JsonlFileSink) {
1000
+ const { path } = sink; // the file this client's samples go to
1001
+ sink.once('close', () => uploadFile(path)); // close = flushed & fd closed → ready
1002
+ }
1003
+ });
1004
+ ```
1005
+
1006
+ The general pattern: each concrete sink exposes whatever it wants as `public readonly` fields, and
1007
+ consumers narrow (`instanceof YourSink`) to read them. Your own sinks do the same.
1008
+
1009
+ ### Writing your own sink
1010
+
1011
+ Subclass `ClientSampleSink` and emit the lifecycle events yourself — for any non-file
1012
+ destination (a remote endpoint, a message queue, an object store, …):
1013
+
1014
+ ```ts
1015
+ import { ClientSampleSink, ClientSample, ClientSampleSinkFactory } from '@observertc/observer-js';
1016
+
1017
+ class HttpSink extends ClientSampleSink {
1018
+ private buffer: ClientSample[] = [];
1019
+ constructor(private readonly url: string) { super(); }
1020
+
1021
+ write(sample: ClientSample): boolean {
1022
+ this.buffer.push(sample); // batch; decide your own backpressure
1023
+ return true;
1024
+ }
1025
+ end(): void {
1026
+ fetch(this.url, { method: 'POST', body: JSON.stringify(this.buffer) })
1027
+ .then(() => this.emit('close')) // signal "destination ready"
1028
+ .catch((err) => this.emit('error', err));
1029
+ }
451
1030
  }
452
1031
 
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);
1032
+ const createClientSink: ClientSampleSinkFactory = ({ clientId, observedCall }) =>
1033
+ new HttpSink(`https://stats.example.com/${observedCall.callId}/${clientId}`);
461
1034
 
462
- // Later, on application shutdown:
463
- // observer.close();
1035
+ const observer = new Observer({ createClientSink });
464
1036
  ```
465
1037
 
466
- #### 5.2. Manual Call and Client Creation
1038
+ `observedClient.sink?` exposes the created sink; the `client-sink-created` event delivers it on
1039
+ the bus with full ancestry. `ClientSampleSinkFactory` is
1040
+ `(p: { clientId: string; observedCall: ObservedCall }) => ClientSampleSink | undefined`.
467
1041
 
468
- ```typescript
469
- // ... observer setup ...
1042
+ ---
470
1043
 
471
- const call = observer.createObservedCall({
472
- callId: 'scheduled-webinar-456',
473
- updatePolicy: 'update-on-interval',
474
- updateIntervalInMs: 10000,
1044
+ ## Injecting data into a client
1045
+
1046
+ Sometimes the application holds data that belongs on a client's record but isn't part of the
1047
+ client-reported `ClientSample` — a room id or display name, an application-level event
1048
+ (*"recording started"*), a server-detected issue, an extension stat, or a device/meta item.
1049
+ `ObservedClient` exposes **injection** methods that merge such data into the client's sample stream,
1050
+ so it updates the live model **and** is persisted to the client's
1051
+ [sink](#sinks-per-client-sample-persistence) exactly like sampled data.
1052
+
1053
+ | Method | Adds to the sample's | Surfaces as |
1054
+ |--------|----------------------|-------------|
1055
+ | `injectAttachment(attachments)` | `attachments` (merged via `Object.assign`) | `observedClient.attachments` |
1056
+ | `injectEvent(event: ClientEvent)` | `clientEvents` | `client-event` (plus any state the event drives) |
1057
+ | `injectIssue(issue: ClientIssue)` | `clientIssues` | `client-issue` |
1058
+ | `injectMetaData(meta: ClientMetaData)` | `clientMetaItems` | `client-metadata` |
1059
+ | `injectExtensionStat(stat: ExtensionStat)` | `extensionStats` | `client-extension-stats` |
1060
+
1061
+ ### When the injected data lands
1062
+
1063
+ Injection is timing-aware so nothing is dropped, regardless of *when* you call it:
1064
+
1065
+ - **During a sample's processing** — e.g. from inside a `client-updated` / `client-event` handler,
1066
+ which run within `accept()` — the data is applied to the **current** sample immediately: reflected
1067
+ in entity state and written to the sink as part of that sample.
1068
+ - **Between samples** — the data is buffered and merged into the **next** `accept()`'s sample.
1069
+ - **On `close()` with pending injections and no further sample** — the buffer is flushed as a final
1070
+ synthetic sample (applied to state and written to the sink) before the sink is ended, so a
1071
+ last-moment injection is never lost.
1072
+
1073
+ In every case the injected data both updates the live `ObservedClient` and reaches the per-client
1074
+ sink — the sink always receives the final, **injection-merged** sample (the sink write happens at the
1075
+ end of `accept()`, after the merge).
1076
+
1077
+ ### Example
1078
+
1079
+ ```ts
1080
+ // Enrich at creation from your app's knowledge of the participant. Injecting in `client-added`
1081
+ // (which runs just before the first accept) lands on the first sample.
1082
+ observer.on('client-added', ({ observedClient }) => {
1083
+ observedClient.injectAttachment({ roomId: lookupRoomId(observedClient.clientId) });
475
1084
  });
476
1085
 
477
- const client1 = call.createObservedClient({ clientId: 'presenter-01' });
478
- // Samples for 'presenter-01' in call 'scheduled-webinar-456' will update this client.
1086
+ // Application-level signals at any time:
1087
+ const client = observer.getObservedCall(callId)?.getObservedClient(clientId);
1088
+ client?.injectEvent({ type: 'RECORDING_STARTED', timestamp: Date.now() });
1089
+ client?.injectIssue({ type: 'app-kicked-participant', timestamp: Date.now() });
479
1090
  ```
480
1091
 
481
- #### 5.3. Using Event Monitors for Contextual Logging
482
-
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
- });
493
- }
1092
+ `attachments` are latest-wins (like sampled `attachments`): injecting a key overwrites its previous
1093
+ value. `appData` is unaffected — injections flow into the sample/telemetry, not the app-owned
1094
+ `appData` bag (see [Ingestion](#ingestion-accept-context--lifecycle)).
1095
+
1096
+ ## Logging
1097
+
1098
+ `observer-js` logs through a single, swappable sink. Out of the box it writes `debug` and
1099
+ above to `console` (verbose — install your own sink for production). Funnel everything into your
1100
+ logger:
1101
+
1102
+ ```ts
1103
+ import { setObserverLogger, type ObserverLogger } from '@observertc/observer-js';
1104
+
1105
+ setObserverLogger({
1106
+ trace: (m, ...a) => myLogger.trace(`[${m}]`, ...a),
1107
+ debug: (m, ...a) => myLogger.debug(`[${m}]`, ...a),
1108
+ info: (m, ...a) => myLogger.info(`[${m}]`, ...a),
1109
+ warn: (m, ...a) => myLogger.warn(`[${m}]`, ...a),
1110
+ error: (m, ...a) => myLogger.error(`[${m}]`, ...a),
1111
+ });
494
1112
  ```
495
1113
 
496
- ### 6. Best Practices
1114
+ `createLogger(moduleName)` is also exported for your own modules. See
1115
+ **[`docs/logging.md`](./docs/logging.md)** for pino / winston / console recipes, level
1116
+ filtering, per-module routing, and full silencing.
1117
+
1118
+ ---
497
1119
 
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.
1120
+ ## Error-handling philosophy
503
1121
 
504
- ### 7. Troubleshooting
1122
+ The library **warns and degrades; it does not throw** on operational problems:
505
1123
 
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.
1124
+ - `createObservedCall` / `createObservedClient` on a closed parent → warn + return `undefined`.
1125
+ - Duplicate id → warn + return the **existing** instance.
1126
+ - `accept()` on a closed client → warn + no-op.
1127
+ - Sample missing `callId`/`clientId`, or observer closed → `sample-rejected` event.
1128
+ - A throwing accept-middleware → warn + drop that sample (never crashes `accept()`).
512
1129
 
513
- ### 8. TypeScript Support
1130
+ Therefore `create*` and `getOrCreate*` return `T | undefined`; **guard the result.** The
1131
+ `Middleware` utility's internal invariants (e.g. calling `next()` twice) throw, but those throws
1132
+ are caught by `accept()` and surfaced as a warning.
514
1133
 
515
- The library is written in TypeScript and provides type definitions.
516
- Use generics with `Observer`, `ObservedCall`, and `ObservedClient` to type your custom `appData`.
1134
+ ---
517
1135
 
518
- ```typescript
519
- interface MyClientAppData {
520
- userId: string;
521
- role: 'admin' | 'user';
522
- }
523
- const client = call.createObservedClient<MyClientAppData>({
524
- clientId: 'user1',
525
- appData: { userId: 'u-123', role: 'admin' },
526
- });
527
- // client.appData will be typed as MyClientAppData | undefined
1136
+ ## Development & extension guide
1137
+
1138
+ ```bash
1139
+ yarn install
1140
+ yarn build # tsup → dist/ (dual ESM .mjs + CJS .js, single entry, .d.ts/.d.mts + sourcemaps)
1141
+ yarn lint # eslint -c .eslintrc.json "src/**/*.ts"
1142
+ yarn typecheck # tsc --noEmit
1143
+ yarn test # jest
528
1144
  ```
529
1145
 
530
- ### 9. Contributing
1146
+ The build is driven by [`tsup`](https://tsup.egoist.dev) (config in `tsup.config.ts`): a single
1147
+ entry (`src/index.ts`), dual ESM + CommonJS output to `dist/` (`index.mjs` / `index.js`) with
1148
+ `.d.mts` / `.d.ts` types and sourcemaps, targeting Node 20. CI (`.github/workflows/ci.yml`) runs
1149
+ lint + typecheck + **build** + test on every push/PR.
1150
+
1151
+ **Project layout** (`src/`): `Observer.ts`, `ObservedCall.ts`, `ObservedClient.ts`,
1152
+ `ObservedPeerConnection.ts`, the `Observed*` sub-stat classes, `ObserverEvents.ts` (the typed
1153
+ event map + scope types), `detectors/` (`Detector`, `Detectors`), `scores/`, `updaters/`
1154
+ (update-policy strategies), `utils/` (remote-track resolvers), `common/` (`logger`, `utils`,
1155
+ `Middleware`), `schema/` (sample/event/meta types), and `sinks/` (the `ClientSampleSink` base +
1156
+ `JsonlFileSink` / `InMemorySink`, re-exported from the package root).
1157
+
1158
+ **Conventions to follow when developing further:**
1159
+
1160
+ - *Single event bus.* New consumer-facing events go in `ObserverEvents.ts` with an object
1161
+ payload `[<Scope> & { …subject }]`, and are emitted via the component's
1162
+ `_notify(type, { ...this.eventScope, …subject })`. Each component has a precomputed
1163
+ `eventScope` field and a thin `_notify` wrapper around the right emitter. Keep purely internal
1164
+ coordination as **local** EventEmitter events (and remember to `off` them on close).
1165
+ - *Warn, don't throw* on operational/edge conditions; return `undefined` where a value can't be produced.
1166
+ - *Counter-reset-safe deltas.* When computing a delta from a cumulative counter, never emit a
1167
+ negative value (guard `curr >= prev`), to survive counter resets / SSRC reuse.
1168
+ - *Explicit accumulation.* The per-sample metric accumulation in `accept()` is intentionally
1169
+ explicit and not abstracted — match that style.
1170
+ - *Detectors are server-side.* Add cross-client detectors on `ObservedCall.detectors`; don't
1171
+ re-implement client-detectable signals.
1172
+
1173
+ **Recipes:**
1174
+
1175
+ - *Add an event:* add the key + payload to `ObserverEvents`; in the owning component call
1176
+ `this._notify('my-event', { ...this.eventScope, subject })`.
1177
+ - *Add a per-stream metric:* add the field to the relevant `Observed*Rtp`/track class, populate
1178
+ it in its `update()` (reset at the top of `update()` if it's per-tick), and read it from a
1179
+ `*-updated` handler.
1180
+ - *Add a detector:* implement `Detector`, register it on `call-added` via
1181
+ `observedCall.detectors.add(...)`, surface findings with `observedCall.addIssue(...)`.
531
1182
 
532
- (Placeholder for contribution guidelines - e.g., link to CONTRIBUTING.md, coding standards, pull request process)
1183
+ ---
533
1184
 
534
- ### 10. License
1185
+ ## License
535
1186
 
536
- This project is licensed under the [MIT License](LICENSE).
1187
+ Apache-2.0. Part of the [ObserverTC](https://github.com/observertc) ecosystem.