@observertc/observer-js 1.0.0-beta.13 → 1.0.0-beta.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/llms-full.txt DELETED
@@ -1,1632 +0,0 @@
1
- # observer-js (@observertc/observer-js) — full documentation
2
-
3
- > Single-file export of the project's documentation for one-shot LLM ingestion.
4
- > Concatenates: README.md, docs/logging.md, CHANGELOG.md. Generated by scripts/build-llms-full.mjs —
5
- > the canonical, always-current copies live in the repository; see llms.txt for the link index.
6
-
7
- ---
8
-
9
- # ============================================================================
10
- # README.md
11
- # ============================================================================
12
-
13
- # ObserverTC — `@observertc/observer-js`
14
-
15
- [![NPM version](https://img.shields.io/npm/v/@observertc/observer-js.svg)](https://www.npmjs.com/package/@observertc/observer-js)
16
- [![License](https://img.shields.io/npm/l/@observertc/observer-js.svg)](https://github.com/observertc/observer-js/blob/main/LICENSE)
17
-
18
- > **In one line:** feed it WebRTC `getStats()` snapshots, and get back a live, queryable model of
19
- > every call plus a single typed event stream to react to.
20
-
21
- `observer-js` is a **server-side Node.js library for monitoring WebRTC sessions**. A WebRTC
22
- application (typically an SFU or a signaling/stats backend) feeds it `ClientSample` objects —
23
- periodic snapshots of each participant's `RTCPeerConnection.getStats()` output plus
24
- application events — and `observer-js` maintains a live, in-memory model of every call,
25
- participant, peer connection, and media stream, derives per-interval and cumulative metrics,
26
- and emits a single, unified stream of typed events the application can react to.
27
-
28
- **What you can do with it:**
29
-
30
- - **Monitor calls live** — a queryable in-memory tree of every call, client, peer connection,
31
- track, codec, ICE candidate and data channel, each holding current **and** cumulative metrics.
32
- - **React on one event bus** — subscribe once on the `Observer`; every payload carries its full
33
- ancestry (`call → client → peer connection → stat`), so you never walk the tree to subscribe.
34
- - **Get derived metrics for free** — counter-reset-safe per-tick deltas, bitrates, jitter, RTT,
35
- fraction-lost, remote-RTP (RTCP) correlation, and TURN/TCP usage from the selected candidate pair.
36
- - **Correlate across an SFU** — link a publisher's outbound track to every subscriber's inbound
37
- track (`RemoteTrackResolver`), and observe mediasoup routers/transports/producers/consumers
38
- on the server side.
39
- - **Detect server-only problems** — cross-client `Detector`s raise `call-issue`s for conditions no
40
- single client can see (e.g. everyone in a call degrading at once).
41
- - **Persist every sample** — per-client sinks (JSONL file, in-memory, or your own) for archival,
42
- streaming, and offline replay.
43
- - **Drop it in safely** — warn-don't-throw, a pluggable logger, dual **ESM + CommonJS**, and **no**
44
- media-stack dependency in the core.
45
-
46
- > **Status:** `1.0.0-beta`. The API described here is current and intended to be implemented
47
- > against directly. This document is written to be self-sufficient: an engineer (or an AI
48
- > agent) should be able to integrate the library, or develop it further, from this file alone.
49
- > A companion doc, [`docs/logging.md`](./docs/logging.md), covers logging integration in depth.
50
-
51
- > **Packaging:** server-side, **Node.js ≥ 22**, shipped as a **dual ESM + CommonJS** build — so it
52
- > works whether your project uses `import` (ESM) or `require()` (CommonJS). Everything — including
53
- > the built-in file sink — is exported from the single `@observertc/observer-js` entry.
54
-
55
- > **For AI agents:** [`llms.txt`](./llms.txt) is a curated map of these docs (with a one-file
56
- > [`llms-full.txt`](./llms-full.txt) export for one-shot ingestion); [`AGENTS.md`](./AGENTS.md)
57
- > covers build/test commands and conventions for working **in** this repository. `llms-full.txt`
58
- > ships in the npm package (so it's available wherever the library is installed); `llms.txt` and
59
- > `AGENTS.md` live in the repo (and `llms.txt` belongs at the root of the docs site).
60
-
61
- ---
62
-
63
- ## Table of contents
64
-
65
- 1. [Installation](#installation)
66
- 2. [Quick start](#quick-start)
67
- 3. [Data flow](#data-flow)
68
- 4. [Entity hierarchy](#entity-hierarchy)
69
- 5. [Ingestion: `accept()`, context & lifecycle](#ingestion-accept-context--lifecycle)
70
- 6. [Update policies](#update-policies)
71
- 7. [The event bus](#the-event-bus) ← the core of the API
72
- 8. [API reference](#api-reference)
73
- 9. [Schema types (`ClientSample`)](#schema-types-clientsample)
74
- 10. [Detectors (server-side extension point)](#detectors-server-side-extension-point)
75
- 11. [Remote track resolution (mediasoup / SFU)](#remote-track-resolution-mediasoup--sfu)
76
- 12. [Mediasoup router observation](#mediasoup-router-observation)
77
- 13. [Sinks (per-client sample persistence)](#sinks-per-client-sample-persistence)
78
- 14. [Injecting data into a client](#injecting-data-into-a-client)
79
- 15. [Logging](#logging)
80
- 16. [Error-handling philosophy](#error-handling-philosophy)
81
- 17. [Development & extension guide](#development--extension-guide)
82
-
83
- ---
84
-
85
- ## Installation
86
-
87
- ```bash
88
- npm install @observertc/observer-js
89
- # or
90
- yarn add @observertc/observer-js
91
- ```
92
-
93
- **Server-side, Node.js ≥ 22, dual ESM + CommonJS.** The package ships both module formats, so it
94
- works the same whether your project is ESM or CommonJS — your import line is unchanged either way:
95
-
96
- ```ts
97
- import { Observer, ClientSample, createJsonlFileSinkFactory } from '@observertc/observer-js';
98
- ```
99
-
100
- In an ESM project this resolves to the `.mjs` build; in a CommonJS project (where TypeScript
101
- compiles your `import` down to `require()`) it resolves to the `.js` build. Everything is exported
102
- from the single `@observertc/observer-js` entry. Written in TypeScript; ships type declarations for
103
- both formats (`dist/index.d.ts` for `require`, `dist/index.d.mts` for `import`). Runtime
104
- dependencies: `@bufbuild/protobuf`, `events`, `uuid`. The library does **not** bundle a logger or
105
- any transport — see [Logging](#logging).
106
-
107
- `ClientSample` and friends are re-exported from this package, and are also published as the
108
- shared schema in [`@observertc/schemas`](https://github.com/observertc/schemas); samples
109
- produced on the client (e.g. by `@observertc/client-monitor-js`) conform to the same shape.
110
-
111
- ---
112
-
113
- ## Quick start
114
-
115
- ```ts
116
- import { Observer, ClientSample } from '@observertc/observer-js';
117
-
118
- // 1. Create an observer.
119
- const observer = new Observer({
120
- // when the observer aggregates call/client metrics:
121
- updatePolicy: 'update-when-all-call-updated',
122
- // default policy applied to calls created automatically by accept():
123
- defaultCallUpdatePolicy: 'update-on-any-client-updated',
124
- // optional auto-teardown:
125
- closeCallIfEmptyForMs: 20_000,
126
- closeClientIfIdleForMs: 60_000,
127
- });
128
-
129
- // 2. Subscribe on the single bus. Every payload is an object with the ancestry.
130
- observer.on('call-added', ({ observedCall }) => {
131
- console.log('new call', observedCall.callId);
132
- });
133
-
134
- observer.on('client-issue', ({ observedClient, issue }) => {
135
- console.warn(`[${observedClient.clientId}] ${issue.type}`, issue.payload);
136
- });
137
-
138
- observer.on('peer-connection-updated', ({ observedClient, observedPeerConnection }) => {
139
- console.log(observedClient.clientId, 'RTT(ms):', observedPeerConnection.currentRttInMs);
140
- });
141
-
142
- observer.on('sample-rejected', ({ reason, sample }) => {
143
- console.warn('dropped a sample:', reason);
144
- });
145
-
146
- // 3. Feed samples. `context` (optional) is transient per-accept data, carried to the
147
- // `*-updated` events this accept triggers (never written to appData).
148
- function onClientStats(sample: ClientSample) {
149
- observer.accept(sample, { studioVersion: '1.2.3' });
150
- }
151
-
152
- // 4. Tear down.
153
- process.on('SIGINT', () => observer.close());
154
- ```
155
-
156
- ---
157
-
158
- ## Data flow
159
-
160
- ```
161
- client getStats() ──► ClientSample ──► observer.accept(sample, ctx?)
162
- │
163
- ┌────────────────────────────────┘
164
- ▼
165
- get-or-create ObservedCall ──► get-or-create ObservedClient ──► client.accept(sample, ctx)
166
- │
167
- per peerConnections[] in the sample
168
- ▼
169
- get-or-create ObservedPeerConnection
170
- .accept(pcSample, ctx) updates all sub-stats,
171
- derives deltas/bitrates/RTT, correlates remote RTP
172
- │
173
- metrics roll up: PeerConnection → Client → Call → Observer
174
- │
175
- events emitted on the Observer bus ──► your handlers
176
- ```
177
-
178
- - A sample **must** have `callId` and `clientId` (the library sets them, or the app does). If
179
- either is missing, the sample is dropped and `sample-rejected` is emitted.
180
- - Sub-entities that stop appearing in samples are garbage-collected via a "visited"
181
- mark-and-sweep on each `ObservedPeerConnection.accept()`, emitting the corresponding
182
- `*-removed` events.
183
-
184
- ---
185
-
186
- ## Entity hierarchy
187
-
188
- | Class | Created by | Keyed on its parent as | Holds |
189
- |-------|-----------|------------------------|-------|
190
- | `Observer` | `new Observer(config?)` | — (root) | `observedCalls: Map<string, ObservedCall>`, global counters, the event bus |
191
- | `ObservedCall` | `observer.createObservedCall(settings)` / lazily by `accept` | `observedCalls` | `observedClients: Map<string, ObservedClient>`, call-wide metrics, `detectors`, `scoreCalculator` |
192
- | `ObservedClient` | `call.createObservedClient(settings)` / lazily | `observedClients` | `observedPeerConnections: Map<string, ObservedPeerConnection>`, per-client metrics |
193
- | `ObservedPeerConnection` | lazily, from `sample.peerConnections[]` | `observedPeerConnections` | the 15 sub-stat maps below, transport/RTT/bitrate metrics |
194
- | Sub-stats | lazily, from the `PeerConnectionSample` | maps on the PC | individual WebRTC stat objects |
195
-
196
- `ObservedPeerConnection` sub-stat maps (all `public readonly`):
197
-
198
- ```
199
- observedCertificates, observedCodecs, observedDataChannels,
200
- observedIceCandidates, observedIceCandidatesPair, observedIceTransports,
201
- observedInboundRtps, observedInboundTracks, observedMediaPlayouts,
202
- observedMediaSources, observedOutboundRtps, observedOutboundTracks,
203
- observedPeerConnectionTransports, observedRemoteInboundRtps, observedRemoteOutboundRtps
204
- ```
205
-
206
- Each sub-stat class (`ObservedInboundRtp`, `ObservedOutboundRtp`, `ObservedInboundTrack`,
207
- `ObservedOutboundTrack`, `ObservedDataChannel`, `ObservedIceCandidate`,
208
- `ObservedIceCandidatePair`, `ObservedIceTransport`, `ObservedCertificate`, `ObservedCodec`,
209
- `ObservedMediaSource`, `ObservedMediaPlayout`, `ObservedPeerConnectionTransport`,
210
- `ObservedRemoteInboundRtp`, `ObservedRemoteOutboundRtp`) mirrors the corresponding stat
211
- fields from the schema plus derived fields (deltas, bitrates).
212
-
213
- ---
214
-
215
- ## Ingestion: `accept()`, context & lifecycle
216
-
217
- ### `observer.accept(sample, context?)`
218
-
219
- The single entry point. It:
220
-
221
- 1. drops + emits `sample-rejected` if the observer is closed;
222
- 2. runs the sample through the **global accept-middleware chain** (see below);
223
- 3. (chain terminal) drops + emits `sample-rejected` if `callId`/`clientId` is missing;
224
- 4. gets or lazily creates the `ObservedCall` and `ObservedClient` (their `appData` comes from the
225
- configured factories, never from `context`);
226
- 5. delegates to `client.accept(sample, context)`, which fans out to each
227
- `ObservedPeerConnection.accept(pcSample, context)`.
228
-
229
- ### Accept middlewares (global pre-dispatch hook)
230
-
231
- `observer.addAcceptMiddleware(...)` registers middlewares run on **every** sample inside
232
- `accept()`, in order, **before** the sample is dispatched to any call or client. Each middleware
233
- gets a `{ sample, context }` payload; it can inspect or mutate the sample (set/normalize
234
- `callId`/`clientId`, enrich, redact) or the context, then call `next(payload)` to continue.
235
- **Not calling `next` drops the sample** — nothing is created and no event fires. A throwing
236
- middleware is caught and warns (the sample is dropped), never crashing `accept()`.
237
-
238
- ```ts
239
- import { Observer, AcceptMiddleware } from '@observertc/observer-js';
240
-
241
- const observer = new Observer();
242
-
243
- // derive callId/clientId from the app's own attachment, before dispatch
244
- const route: AcceptMiddleware = ({ sample }, next) => {
245
- sample.callId ??= sample.attachments?.roomId as string;
246
- sample.clientId ??= sample.attachments?.peerId as string;
247
- next({ sample });
248
- };
249
-
250
- // drop samples from a blocklisted client (never dispatched)
251
- const filter: AcceptMiddleware = (payload, next) => {
252
- if (blocked.has(payload.sample.clientId)) return; // no next() => dropped
253
- next(payload);
254
- };
255
-
256
- observer.addAcceptMiddleware(route, filter);
257
- // observer.removeAcceptMiddleware(route);
258
- ```
259
-
260
- This is a lightweight global injection point. When no middleware is registered, `accept()`
261
- dispatches directly with no overhead.
262
-
263
- ### `context` (the `AcceptContext`)
264
-
265
- ```ts
266
- type AcceptContext = Record<string, unknown>;
267
- ```
268
-
269
- A single, optional, free-form object threaded down the whole accept chain
270
- (`Observer → Client → PeerConnection`). It is **transient request-scoped data** — temporary or
271
- contextual information the application wants available while an update is processed.
272
-
273
- `context` is **never written to `appData`** and is **not stored** on any entity. The two are
274
- deliberately distinct:
275
-
276
- - **`appData`** — application-assigned extra info that identifies/decorates an entity, fixed at
277
- creation (via `settings.appData` or the `createCallAppData` / `createClientAppData` factories),
278
- or assigned by the app on the `*-added` events. The library never changes it.
279
- - **`context`** — passed per `accept()`, may differ on every call, and is carried straight
280
- through to the `*-updated` events that the `accept()` triggers, then discarded.
281
-
282
- `client-updated` and `peer-connection-updated` carry the exact context of that sample;
283
- `call-updated` carries the context of the client `accept()` that drove the call update (absent
284
- for interval- or teardown-driven call updates). When no context is given, the field is absent.
285
-
286
- ### Get-or-create helpers
287
-
288
- If you want to create/configure entities yourself before/without samples:
289
-
290
- ```ts
291
- const call = observer.getOrCreateObservedCall({ callId, appData }); // ObservedCall | undefined
292
- const client = call?.getOrCreateObservedClient({ clientId, appData }); // ObservedClient | undefined
293
- ```
294
-
295
- These return `undefined` (and warn) when the parent is closed; `createObservedCall`/
296
- `createObservedClient` return the **existing** instance (and warn) if the id already exists.
297
-
298
- ### Automatic teardown
299
-
300
- - `closeClientIfIdleForMs` — a client with no sample for this long auto-closes.
301
- - `closeCallIfEmptyForMs` — a call with zero clients for this long auto-closes.
302
- - Closing cascades down (call → clients → peer connections → sub-stats), unsubscribing
303
- listeners and emitting the `*-closed` / `*-removed` events.
304
-
305
- ---
306
-
307
- ## Update policies
308
-
309
- "Update" means *recompute aggregated metrics and emit the `*-updated` event* at that level.
310
- Both the observer and each call have a configurable trigger. Updates are **event-driven** — there
311
- is no built-in timer. An app that wants a fixed cadence can call `observer.update()` /
312
- `call.update()` from its own `setInterval`. With `'none'`, **nothing auto-updates** — the level
313
- updates only when the application calls the public `update()` itself.
314
-
315
- **Observer-level** (`ObserverConfig.updatePolicy`, default `update-when-all-call-updated`):
316
-
317
- | Policy | Triggers `observer.update()` when… |
318
- |--------|-------------------------------------|
319
- | `update-on-any-call-updated` | any call updates |
320
- | `update-when-all-call-updated` | every call has updated since the last observer update |
321
- | `none` | never automatically — only when the app calls `observer.update()` |
322
-
323
- **Call-level** (`ObservedCallSettings.updatePolicy`, defaulted from
324
- `ObserverConfig.defaultCallUpdatePolicy`):
325
-
326
- | Policy | Triggers `call.update()` when… |
327
- |--------|--------------------------------|
328
- | `update-on-any-client-updated` | any client in the call updates |
329
- | `update-when-all-client-updated` | every client has updated since the last call update |
330
- | `none` | never automatically — only when the app calls `call.update()` |
331
-
332
- ---
333
-
334
- ## The event bus
335
-
336
- This is the primary API. **Subscribe on the `Observer` instance** — it is the single emitter
337
- for the entire hierarchy. The `ObservedCall` / `ObservedClient` / `ObservedPeerConnection`
338
- objects are themselves `EventEmitter`s too, but those local events are reserved for internal
339
- lifecycle/teardown wiring (see [Local lifecycle events](#local-lifecycle-events)); application
340
- code should use the Observer bus.
341
-
342
- ### Payload shape: ancestry + subject
343
-
344
- Every Observer event delivers exactly **one argument: a payload object**. The payload always
345
- contains the ancestry from the observer down to the entity that raised it, plus any event-
346
- specific subject:
347
-
348
- ```ts
349
- type ObserverEventBase = { observer: Observer };
350
- type ObservedCallScope = ObserverEventBase & { observedCall: ObservedCall };
351
- type ObservedClientScope = ObservedCallScope & { observedClient: ObservedClient };
352
- type ObservedPeerConnectionScope = ObservedClientScope & { observedPeerConnection: ObservedPeerConnection };
353
- ```
354
-
355
- So a peer-connection-level event hands you the observer, call, client, **and** peer connection:
356
-
357
- ```ts
358
- observer.on('inbound-rtp-added', ({ observer, observedCall, observedClient, observedPeerConnection, observedInboundRtp }) => {
359
- // all five are present and correctly typed
360
- });
361
- ```
362
-
363
- `observer.on/off/once/emit` are fully typed against the event map — the handler argument is
364
- inferred per event name.
365
-
366
- ### Event catalogue
367
-
368
- All payloads include the ancestry for their level (above). The **Extra** column lists the
369
- additional field(s) on top of that scope.
370
-
371
- #### Observer level — scope `{ observer }`
372
-
373
- | Event | Extra payload | Fires when |
374
- |-------|---------------|-----------|
375
- | `observer-updated` | — | `observer.update()` ran (per the observer update policy) |
376
- | `observer-closed` | — | `observer.close()` |
377
- | `sample-rejected` | `{ reason: 'observer-closed' \| 'missing-callId' \| 'missing-clientId', sample: ClientSample }` | a sample was dropped by `accept()` |
378
-
379
- #### Mediasoup level — scope `{ observer, observedMediasoupRouter }`
380
-
381
- | Event | Extra | Fires when |
382
- |-------|-------|-----------|
383
- | `mediasoup-router-added` | — | `observer.createObservedMediasoupRouter(...)` registered a router |
384
- | `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`. |
385
- | `mediasoup-router-removed` | — | the underlying mediasoup router closed (its `router.observer` `close` fired) |
386
-
387
- See [Mediasoup router observation](#mediasoup-router-observation) for the full design and examples.
388
-
389
- #### Call level — scope `{ observer, observedCall }`
390
-
391
- | Event | Extra | Fires when |
392
- |-------|-------|-----------|
393
- | `call-added` | — | a call is created |
394
- | `call-updated` | `{ context?: AcceptContext }` | `call.update()` ran |
395
- | `call-closed` | — | the call closed |
396
- | `call-empty` | — | last client left the call |
397
- | `call-not-empty` | — | first client joined a previously-empty call |
398
- | `call-issue` | `{ issue: ClientIssue }` | `call.addIssue(...)` (server-side detector finding) |
399
-
400
- #### Client level — scope `{ observer, observedCall, observedClient }`
401
-
402
- | Event | Extra | Fires when |
403
- |-------|-------|-----------|
404
- | `client-added` | — | a client is created |
405
- | `client-sink-created` | `{ sink: ClientSampleSink }` | a per-client sink was created (only when `createClientSink` returns one); fires right after `client-added` |
406
- | `client-updated` | `{ sample: ClientSample, elapsedTimeInMs: number, context?: AcceptContext }` | the client processed a sample |
407
- | `client-closed` | — | the client closed |
408
- | `client-joined` | — | first `CLIENT_JOINED` event seen |
409
- | `client-left` | — | `CLIENT_LEFT` seen (or inferred on close) |
410
- | `client-rejoined` | `{ timestamp: number }` | a later `CLIENT_JOINED` after an earlier join |
411
- | `client-issue` | `{ issue: ClientIssue }` | a client-reported issue arrived, or `client.addIssue(...)` |
412
- | `client-metadata` | `{ metaData: ClientMetaData }` | a client meta item arrived |
413
- | `client-extension-stats` | `{ extensionStats: ExtensionStat }` | an app-defined extension stat arrived |
414
- | `client-event` | `{ event: ClientEvent }` | any client event was processed |
415
-
416
- #### Peer-connection level — scope `{ observer, observedCall, observedClient, observedPeerConnection }`
417
-
418
- | Event | Extra | Notes |
419
- |-------|-------|-------|
420
- | `peer-connection-added` / `peer-connection-closed` | — | lifecycle of the PC |
421
- | `peer-connection-updated` | `{ context?: AcceptContext }` | the PC processed a sample |
422
- | `ice-connection-state-changed` / `ice-gathering-state-changed` / `connection-state-changed` | `{ state: string }` | driven by client events |
423
- | `inbound-track-added` / `-updated` / `-removed` / `-muted` / `-unmuted` | `{ observedInboundTrack }` | |
424
- | `outbound-track-added` / `-updated` / `-removed` / `-muted` / `-unmuted` | `{ observedOutboundTrack }` | |
425
- | `inbound-rtp-added` / `-updated` / `-removed` | `{ observedInboundRtp }` | `-updated` fires every tick |
426
- | `outbound-rtp-added` / `-updated` / `-removed` | `{ observedOutboundRtp }` | `-updated` fires every tick |
427
- | `remote-inbound-rtp-added` / `-updated` / `-removed` | `{ observedRemoteInboundRtp }` | |
428
- | `remote-outbound-rtp-added` / `-updated` / `-removed` | `{ observedRemoteOutboundRtp }` | |
429
- | `data-channel-added` / `-updated` / `-removed` | `{ observedDataChannel }` | |
430
- | `ice-candidate-added` / `-updated` / `-removed` | `{ observedIceCandidate }` | |
431
- | `ice-candidate-pair-added` / `-updated` / `-removed` | `{ observedIceCandidatePair }` | |
432
- | `ice-transport-added` / `-updated` / `-removed` | `{ observedIceTransport }` | |
433
- | `codec-added` / `-updated` / `-removed` | `{ observedCodec }` | |
434
- | `media-source-added` / `-updated` / `-removed` | `{ observedMediaSource }` | |
435
- | `media-playout-added` / `-updated` / `-removed` | `{ observedMediaPlayout }` | |
436
- | `peer-connection-transport-added` / `-updated` / `-removed` | `{ observedPeerConnectionTransport }` | |
437
- | `certificate-added` / `-updated` / `-removed` | `{ observedCertificate }` | |
438
-
439
- > **Volume note.** The `*-updated` sub-stat events fire on every peer-connection `accept()`
440
- > (i.e. per sample, per stream). For high-throughput servers, subscribe only to what you need,
441
- > or read fields off the entities on `client-updated` / `call-updated` instead.
442
-
443
- ### Local lifecycle events
444
-
445
- These remain on the individual entities (not the bus), for teardown/coordination. You can
446
- listen to them, but prefer the bus equivalents above for application logic.
447
-
448
- | Entity | Local events |
449
- |--------|--------------|
450
- | `ObservedCall` | `update`, `newclient`, `empty`, `not-empty`, `close` |
451
- | `ObservedClient` | `update` (`sample`, `elapsedTimeInMs`), `close`, `joined`, `left` |
452
- | `ObservedPeerConnection` | `removed-inbound-track`, `removed-outbound-track`, `close` |
453
-
454
- ---
455
-
456
- ## API reference
457
-
458
- ### `Observer`
459
-
460
- ```ts
461
- new Observer<AppData>(config?: ObserverConfig<AppData>)
462
-
463
- type ObserverConfig<AppData = Record<string, unknown>> = {
464
- updatePolicy?: 'update-on-any-call-updated' | 'update-when-all-call-updated' | 'none';
465
- defaultCallUpdatePolicy?: ObservedCallSettings['updatePolicy'];
466
- appData?: AppData;
467
- closeClientIfIdleForMs?: number;
468
- closeCallIfEmptyForMs?: number;
469
- // appData factories — run when an entity is created without explicit appData
470
- // (incl. lazily by accept()). appData is application-owned; accept `context` never touches it.
471
- createCallAppData?: (p: { callId: string; observer: Observer }) => Record<string, unknown>;
472
- createClientAppData?: (p: { clientId: string; observedCall: ObservedCall }) => Record<string, unknown>;
473
- // sink factory — produces a per-client sink that receives every accepted sample (see Sinks).
474
- createClientSink?: (p: { clientId: string; observedCall: ObservedCall }) => ClientSampleSink | undefined;
475
- // remote-track-resolver factory — produces a call's RemoteTrackResolver (see Remote track resolution).
476
- createRemoteTrackResolver?: (observedCall: ObservedCall) => RemoteTrackResolver | undefined;
477
- };
478
- ```
479
-
480
- **appData factories.** Instead of pre-creating a call/client (or assigning on `call-added` /
481
- `client-added`) just to enrich its `appData`, register a factory once. It runs in the entity's
482
- constructor whenever it's created without an explicit `settings.appData` — including the lazy
483
- creation inside `accept()`. The `client` factory receives the already-created parent
484
- `observedCall`, so it can derive fields from it. `appData` is application-owned and is never
485
- modified by the `accept()` context.
486
-
487
- ```ts
488
- const observer = new Observer({
489
- createCallAppData: ({ callId }) => ({ callId, startedAt: Date.now(), region: 'eu' }),
490
- createClientAppData: ({ clientId, observedCall }) => ({ clientId, region: observedCall.appData.region }),
491
- });
492
- ```
493
-
494
- Key members:
495
-
496
- - `accept(sample: ClientSample, context?: AcceptContext): void`
497
- - `addAcceptMiddleware(...mw: AcceptMiddleware[]): this` / `removeAcceptMiddleware(...mw): this` — global pre-dispatch sample hooks (see [Accept middlewares](#accept-middlewares-global-pre-dispatch-hook))
498
- - `getObservedCall<T>(callId): ObservedCall<T> | undefined`
499
- - `createObservedCall<T>(settings): ObservedCall<T> | undefined`
500
- - `getOrCreateObservedCall<T>(settings): ObservedCall<T> | undefined`
501
- - `update(): void` — force an aggregation/`observer-updated` tick
502
- - `close(): void`
503
- - `readonly observedCalls: Map<string, ObservedCall>`
504
- - `readonly observedTURN: ObservedTURN`
505
- - `get appData()`, `get numberOfCalls()`
506
- - counters: `numberOfClients`, `numberOfClientsUsingTurn`, `numberOfInboundRtpStreams`,
507
- `numberOfOutboundRtpStreams`, `numberOfDataChannels`, `numberOfPeerConnections`,
508
- `totalAddedCall`, `totalRemovedCall`, `closed`
509
- - `on/off/once/emit` typed against the [event map](#event-catalogue)
510
-
511
- ### `ObservedCall`
512
-
513
- ```ts
514
- type ObservedCallSettings<AppData = Record<string, unknown>> = {
515
- updatePolicy?: 'update-on-any-client-updated' | 'update-when-all-client-updated' | 'none';
516
- callId: string;
517
- appData?: AppData;
518
- closeCallIfEmptyForMs?: number;
519
- };
520
- ```
521
-
522
- Key members:
523
-
524
- - `readonly callId: string`, `appData: AppData`
525
- - `readonly observedClients: Map<string, ObservedClient>`, `get numberOfClients()`
526
- - `getObservedClient<T>(clientId)`, `createObservedClient<T>(settings)`, `getOrCreateObservedClient<T>(settings)` (all `… | undefined`)
527
- - `addIssue(issue: ClientIssue): void` — raise a **call-level** issue → emits `call-issue`
528
- - `readonly detectors: Detectors` — server-side detector registry (empty by default; see [Detectors](#detectors-server-side-extension-point))
529
- - `scoreCalculator: ScoreCalculator`, `get score()`, `readonly calculatedScore`
530
- - `remoteTrackResolver?: RemoteTrackResolver` — set from `ObserverConfig.createRemoteTrackResolver` at call creation (see [Remote track resolution](#remote-track-resolution-mediasoup--sfu))
531
- - aggregates: `numberOfIssues`, `numberOfPeerConnections`, `numberOfInboundRtpStreams`,
532
- `numberOfOutboundRtpStreams`, `numberOfDataChannels`, `maxNumberOfClients`,
533
- `clientsUsedTurn: Set<string>`, `startedAt?`, `endedAt?`, `closedAt?`, `closed`
534
- - `update()`, `close()`
535
-
536
- ### `ObservedClient`
537
-
538
- ```ts
539
- type ObservedClientSettings<AppData = Record<string, unknown>> = {
540
- clientId: string;
541
- appData?: AppData;
542
- closeClientIfIdleForMs?: number;
543
- };
544
- ```
545
-
546
- Key members:
547
-
548
- - `readonly clientId: string`, `appData: AppData`, `readonly call: ObservedCall`
549
- - `readonly observedPeerConnections: Map<string, ObservedPeerConnection>`
550
- - `readonly sink?: ClientSampleSink` — the per-client sink (see [Sinks](#sinks-per-client-sample-persistence)), if `createClientSink` is configured; listen on it for `close`/`error`
551
- - **Injection API** (queue app data to be merged into the next sample processing):
552
- `injectEvent(ClientEvent)`, `injectIssue(ClientIssue)`, `injectMetaData(ClientMetaData)`,
553
- `injectExtensionStat(ExtensionStat)`, `injectAttachment(attachments: Record<string, unknown>)`
554
- - **Direct add API** (process immediately): `addIssue(ClientIssue)`, `addMetadata(ClientMetaData)`,
555
- `addExtensionStats(ExtensionStat)`
556
- - Metrics (current/derived): `currentAvgRttInMs?`, `currentMinRttInMs?`, `currentMaxRttInMs?`,
557
- `receivingAudioBitrate`, `receivingVideoBitrate`, `sendingAudioBitrate`, `sendingVideoBitrate`,
558
- `usingTURN`, `usingTCP`, `availableIncomingBitrate`, `availableOutgoingBitrate`
559
- - Counts: `numberOfInboundRtpStreams`, `numberOfOutboundRtpStreams`, `numberOfInbundTracks`,
560
- `numberOfOutboundTracks`, `numberOfDataChannels`, `numberOfPeerConnections`
561
- - Per-tick deltas: `deltaReceivedAudioBytes`, `deltaSentAudioBytes`, … (see source for the full set)
562
- - Lifecycle: `joinedAt?`, `leftAt?`, `closedAt?`, `closed`, `get score()`
563
- - Metadata: `browser?`, `engine?`, `platform?`, `operationSystem?`, `mediaDevices`, `mediaConstraints`
564
- - `accept(sample, context?)`, `close()`
565
-
566
- ### `ObservedPeerConnection`
567
-
568
- Key members:
569
-
570
- - `readonly peerConnectionId: string`, `readonly client: ObservedClient`, `appData?`
571
- - The 15 `observed*` sub-stat `Map`s (listed [above](#entity-hierarchy)), plus array getters:
572
- `codecs`, `inboundRtps`, `outboundRtps`, `remoteInboundRtps`, `remoteOutboundRtps`,
573
- `mediaSources`, `mediaPlayouts`, `dataChannels`, `peerConnectionTransports`, `iceTransports`,
574
- `iceCandidates`, `iceCandidatePairs`, `certificates`, `selectedIceCandidatePairs`,
575
- `selectedIceCandiadtePairForTurn`
576
- - State: `connectionState?`, `iceConnectionState?`, `iceGatheringState?`, `usingTURN`, `usingTCP`
577
- - Metrics: `currentRttInMs?`, `currentJitter?`, `availableIncomingBitrate`,
578
- `availableOutgoingBitrate`, sending/receiving bitrates, packet rates, and `total*` / `delta*`
579
- byte/packet counters
580
- - `accept(pcSample, context?)`, `close()`, `get score()`
581
-
582
- **Remote-RTP correlation (derived).** During `accept()`, receiver/sender reports are linked
583
- to the local streams by `remoteId` (fallback SSRC) and surfaced as fields:
584
-
585
- - on `ObservedOutboundRtp`: `remoteRttInMs?`, `remoteFractionLost?`, `remoteJitter?`, `remotePacketsLost?`
586
- - on `ObservedInboundRtp`: `remoteRttInMs?`, `remoteBytesSent?`, `remotePacketsSent?`, `remoteTimestamp?`
587
-
588
- These are reset each tick and only set when the matching remote report is present.
589
-
590
- ---
591
-
592
- ## Schema types (`ClientSample`)
593
-
594
- The shape of an accepted sample (re-exported from this package; identical to
595
- `@observertc/schemas`). Only the top level is shown — each stat object mirrors the standard
596
- WebRTC `getStats()` dictionaries plus a few extensions.
597
-
598
- ```ts
599
- type ClientSample = {
600
- timestamp: number; // client wall-clock (ms epoch)
601
- callId?: string; // set by you or the library
602
- clientId?: string; // set by you or the library
603
- score?: number; // optional client-computed score (0..5)
604
- attachments?: Record<string, unknown>;
605
- peerConnections?: PeerConnectionSample[];
606
- clientEvents?: ClientEvent[];
607
- clientIssues?: ClientIssue[];
608
- clientMetaItems?: ClientMetaData[];
609
- extensionStats?: ExtensionStat[];
610
- };
611
-
612
- type PeerConnectionSample = {
613
- peerConnectionId: string;
614
- attachments?: Record<string, unknown>; // e.g. { direction: 'send'|'recv', producerId, consumerId, label }
615
- score?: number;
616
- inboundTracks?; outboundTracks?;
617
- codecs?;
618
- inboundRtps?; remoteInboundRtps?;
619
- outboundRtps?; remoteOutboundRtps?;
620
- mediaSources?; mediaPlayouts?;
621
- peerConnectionTransports?; dataChannels?;
622
- iceTransports?; iceCandidates?; iceCandidatePairs?;
623
- certificates?;
624
- };
625
-
626
- type ClientEvent = { type: string; payload?: string; timestamp?: number; /* +ids */ };
627
- type ClientIssue = { type: string; payload?: string; timestamp?: number }; // also used for call-issue
628
- type ClientMetaData = { type: string; payload?: string; timestamp?: number; /* +ids */ };
629
- type ExtensionStat = { type: string; payload?: string };
630
- ```
631
-
632
- `payload` fields are JSON strings; the library parses the ones it understands.
633
-
634
- **`ClientEventTypes`** (enum of known `event.type` values): `CLIENT_JOINED`, `CLIENT_LEFT`,
635
- `PEER_CONNECTION_OPENED/CLOSED/STATE_CHANGED`, `MEDIA_TRACK_ADDED/REMOVED/MUTED/UNMUTED/RESUMED`,
636
- `ICE_GATHERING_STATE_CHANGED`, `ICE_CONNECTION_STATE_CHANGED`, `DATA_CHANNEL_OPEN/CLOSED/ERROR`,
637
- `NEGOTIATION_NEEDED`, `SIGNALING_STATE_CHANGE`, `ICE_CANDIDATE`, `ICE_CANDIDATE_ERROR`, and the
638
- mediasoup set `PRODUCER_*` / `CONSUMER_*` / `DATA_PRODUCER_*` / `DATA_CONSUMER_*`.
639
-
640
- **`ClientMetaTypes`** (enum of known meta `type` values): `MEDIA_CONSTRAINT`, `MEDIA_DEVICE`,
641
- `MEDIA_DEVICES_SUPPORTED_CONSTRAINTS`, `USER_MEDIA_ERROR`, `LOCAL_SDP`, `OPERATION_SYSTEM`,
642
- `ENGINE`, `PLATFORM`, `BROWSER`.
643
-
644
- ### Worked example: a real `ClientSample`
645
-
646
- Two consecutive samples from one participant ("Guest" in room `qq0iwfnd`) of an
647
- edumeet/mediasoup call show what actually flows through `accept()`: a rich **join snapshot**,
648
- then lean **steady-state ticks**.
649
-
650
- **Sample 1 — the join snapshot.** Carries the one-off lifecycle `clientEvents` and device
651
- `clientMetaItems` alongside the first stats. (Abbreviated; ids and times are from the real log.)
652
-
653
- ```jsonc
654
- {
655
- "timestamp": 1780572332518,
656
- "callId": "d3dbf2f5-79be-4cb8-9d43-fb404f07ef27",
657
- "clientId": "c926983c-4468-4046-ae8c-a9cabe1a1868",
658
- "score": 0, // no quality measured yet on the join tick
659
- "attachments": { "displayName": "Guest", "roomId": "qq0iwfnd", "actualSessionId": "d3dbf2f5-…" },
660
-
661
- "clientEvents": [ // chronological lifecycle (12 in the real sample)
662
- { "type": "CLIENT_JOINED", "timestamp": 1780572324515 },
663
- { "type": "PEER_CONNECTION_OPENED", "timestamp": 1780572326790 }, // pc=b81c8d9d (media)
664
- { "type": "ICE_GATHERING_STATE_CHANGED", "timestamp": 1780572326811 }, // → gathering
665
- { "type": "PEER_CONNECTION_STATE_CHANGED", "timestamp": 1780572326812 }, // → connecting
666
- { "type": "PRODUCER_ADDED", "timestamp": 1780572326821 }, // producer=1abdaf82 (audio)
667
- { "type": "MEDIA_TRACK_ADDED", "timestamp": 1780572326821 }, // track=36ae42df (audio)
668
- { "type": "PEER_CONNECTION_STATE_CHANGED", "timestamp": 1780572326827 }, // → connected
669
- { "type": "PRODUCER_ADDED", "timestamp": 1780572326837 }, // producer=ba06a35b (video)
670
- { "type": "DATA_PRODUCER_CREATED", "timestamp": 1780572326853 }
671
- ],
672
-
673
- "clientMetaItems": [ // environment & devices, one-off (10 in the real sample)
674
- { "type": "USER_AGENT_DATA", "payload": "{…Chrome 148 / macOS…}" },
675
- { "type": "MEDIA_DEVICE", "payload": "{…\"BRIO 4K Stream Edition\"…}" }
676
- // …mic / camera / speaker devices…
677
- ],
678
-
679
- "peerConnections": [
680
- {
681
- "peerConnectionId": "b81c8d9d-…", // the media PC — Guest publishes to the SFU
682
- "outboundRtps": [ /* audio + video */ ],
683
- "outboundTracks": [ /* mic + camera: label, settings, capabilities */ ],
684
- "remoteInboundRtps": [ /* RTCP feedback from the SFU */ ],
685
- "codecs": [ /* … */ ], "iceTransports": [ /* … */ ],
686
- "iceCandidatePairs": [ /* … */ ], "dataChannels": [ /* … */ ]
687
- },
688
- { "peerConnectionId": "8635acb7-…", "peerConnectionTransports": [ /* … */ ] } // signaling-only PC
689
- ]
690
- }
691
- ```
692
-
693
- What `accept()` does with it, in order — each step emits on the bus with full ancestry:
694
-
695
- 1. lazily creates the `ObservedCall` → **`call-added`**;
696
- 2. creates the `ObservedClient` → **`client-added`**, then **`client-joined`** (from `CLIENT_JOINED`);
697
- 3. creates an `ObservedPeerConnection` per entry → **`peer-connection-added`** (×2 here);
698
- 4. creates an `ObservedOutboundTrack` per track → **`outbound-track-added`**, plus the matching
699
- **`outbound-rtp-added`**;
700
- 5. replays the device list as **`client-metadata`** events and the lifecycle items as
701
- **`client-event`**; and finally **`client-updated`** for the whole tick.
702
-
703
- `attachments.roomId` lands on `observedClient.attachments` (read it on `client-updated`, **not** at
704
- creation — see [Ingestion](#ingestion-accept-context--lifecycle)).
705
-
706
- **Sample 2 — a steady-state tick** (~8 s later): same `callId` / `clientId`, **no** new
707
- `clientEvents` or `clientMetaItems`, just refreshed `peerConnections` stats. Each PC now scores `5`
708
- and the aggregate client `score` is `4.74` — a healthy call. This is the shape of nearly every
709
- sample: each tick refreshes metrics and fires the `*-updated` events, while the heavy join
710
- snapshot happens only once.
711
-
712
- ---
713
-
714
- ## Detectors (server-side extension point)
715
-
716
- `observer-js` deliberately ships **no built-in detectors**. Per-client signals — packet loss,
717
- jitter, RTT, freezes, etc. — are already detectable on the client and arrive on samples as
718
- `clientIssues` (surfaced via `client-issue`). Server-side detection should focus on what only
719
- the server can see by **correlating data across the clients of a call**.
720
-
721
- The hook lives on **`ObservedCall`**:
722
-
723
- ```ts
724
- import { Observer, Detector } from '@observertc/observer-js';
725
-
726
- class MyCrossClientDetector implements Detector {
727
- readonly name = 'my-detector';
728
- constructor(private readonly call /* : ObservedCall */) {}
729
- update() { // called on every call.update()
730
- // …inspect this.call.observedClients across participants…
731
- if (/* condition only visible server-side */ false) {
732
- this.call.addIssue({ type: this.name, payload: JSON.stringify({ /* … */ }), timestamp: Date.now() });
733
- // → emitted on the bus as 'call-issue'
734
- }
735
- }
736
- }
737
-
738
- const observer = new Observer();
739
- observer.on('call-added', ({ observedCall }) => {
740
- observedCall.detectors.add(new MyCrossClientDetector(observedCall));
741
- });
742
- observer.on('call-issue', ({ observedCall, issue }) => { /* react */ });
743
- ```
744
-
745
- `Detector` interface and the registry:
746
-
747
- ```ts
748
- interface Detector { readonly name: string; update(): void; }
749
-
750
- class Detectors {
751
- add(d: Detector): void;
752
- remove(d: Detector): void;
753
- clear(): void;
754
- update(): void; // called by ObservedCall.update(); guards each detector in try/catch
755
- get listOfNames(): string[];
756
- }
757
- ```
758
-
759
- ---
760
-
761
- ## Remote track resolution (mediasoup / SFU)
762
-
763
- In an SFU, one participant's **outbound** track is delivered to other participants as **inbound**
764
- tracks (one **publisher** → many **subscribers**). Correlation is **opt-in** per observer: set
765
- `ObserverConfig.createRemoteTrackResolver`, a factory invoked when each call is created that returns the
766
- call's `RemoteTrackResolver` (or `undefined` for none).
767
-
768
- `RemoteTrackResolver` is a generic, strategy-driven class. It subscribes to the bus (filtered to
769
- its call) and links tracks by **publisher id** — the link key — maintaining the links directly on
770
- the tracks: `inboundTrack.remoteOutboundTrack` and `outboundTrack.remoteInboundTracks: Set`.
771
-
772
- ```ts
773
- import { Observer, createDefaultMediasoupRemoteTrackResolverFactory } from '@observertc/observer-js';
774
-
775
- const observer = new Observer({
776
- createRemoteTrackResolver: createDefaultMediasoupRemoteTrackResolverFactory(),
777
- });
778
-
779
- // later, given tracks (links are kept up to date as tracks come and go):
780
- const source = inboundTrack.remoteOutboundTrack; // the publishing ObservedOutboundTrack
781
- const receivers = [ ...outboundTrack.remoteInboundTracks ]; // the subscribing ObservedInboundTrack[]
782
- ```
783
-
784
- Two built-in factories ship: `createDefaultMediasoupRemoteTrackResolverFactory()` (publisher =
785
- `attachments.producerId`, subscriber = `attachments.consumerId`) and
786
- `createP2pRemoteTrackResolverFactory()` (matches by RTP **SSRC**, preserved end-to-end in p2p).
787
-
788
- For any other topology, build a `RemoteTrackResolver` with your own key resolvers — the publisher
789
- id is just whatever links a subscribed track to the published one:
790
-
791
- ```ts
792
- import { Observer, RemoteTrackResolver } from '@observertc/observer-js';
793
-
794
- const observer = new Observer({
795
- createRemoteTrackResolver: (observedCall) => new RemoteTrackResolver(observedCall, {
796
- resolveOutboundTrackPublisherId: (out) => out.attachments?.mediaId as string | undefined,
797
- resolveInboundTrackPublisherId: (inb) => inb.attachments?.mediaId as string | undefined,
798
- resolveInboundTrackSubscriberId: (inb) => inb.attachments?.subId as string | undefined, // optional
799
- }),
800
- });
801
- ```
802
-
803
- For the mediasoup factory, the application puts `producerId` / `consumerId` (and optionally
804
- `direction`, `label`) into the track `attachments`.
805
-
806
- ---
807
-
808
- ## Mediasoup router observation
809
-
810
- Everything above is built from the **client-reported** `ClientSample`. When you run a
811
- [mediasoup](https://mediasoup.org) SFU you also have the **server's own** ground truth — its
812
- routers, transports, producers, consumers and data channels, with exact lifetimes and state
813
- transitions. `ObservedMediasoupRouter` captures that server-side view into a
814
- **`MediasoupRouterSample`**, completely independent of the client sample pipeline.
815
-
816
- ### The concept
817
-
818
- You hand the observer a live mediasoup `Router`; it attaches to mediasoup's own `observer` API and,
819
- from then on, **passively tracks** the router's topology and lifecycle — with no polling and no
820
- changes to your media code:
821
-
822
- - new transports (`webrtc` / `plain` / `pipe` / `direct`), their selected `tuple`, ICE/DTLS/SCTP
823
- state transitions and `connectedAt`;
824
- - producers (codec, SSRCs/RIDs, `pause`/`resume`) and consumers (`pause`/`resume`,
825
- `producerPaused`/`producerResumed`);
826
- - data producers and data consumers;
827
- - `createdAt` / `closedAt` for every entity above.
828
-
829
- It keeps all of this **in memory**, in a single `MediasoupRouterSample` exposed as
830
- `observedRouter.sample` — see [`src/schema/MediasoupRouter.ts`](./src/schema/MediasoupRouter.ts). The
831
- sample **accumulates for the life of the router**: closed transports/producers/consumers are kept
832
- (with their `closedAt` set), not removed. Read it whenever you like — it's a plain object you own.
833
-
834
- ### Memory & large meetings
835
-
836
- This is intentionally the **simplest** approach — everything lives in memory and nothing is sampled
837
- or evicted for you. That's fine for typical rooms, but be aware of the cost at scale:
838
-
839
- - **Consumers grow as O(N²)** on a single flat router: with `N` participants each producing audio +
840
- video and consuming everyone else, the sample holds roughly `2·N·(N−1)` consumer records (≈ 19,800
841
- for `N` = 100).
842
- - The sample is **cumulative** — closed entities and their `history` are retained — so it also grows
843
- with call duration and churn (renegotiation, simulcast layer changes, rejoins).
844
-
845
- A 100-participant flat router can therefore reach tens of MB and keep growing. There is **no built-in
846
- sink, snapshotting, or eviction** — by design. **If you run large meetings, do your own sampling:**
847
- on your own cadence read `observedRouter.sample` (snapshot/serialize/persist what you need), drop what
848
- you don't, and close routers you no longer track. (mediasoup also typically shards routers across
849
- workers/cores, which keeps any one router small.)
850
-
851
- ### Matching peer connections — by **event**, not by storage
852
-
853
- The observer correlates the SFU side with the client side **at the peer-connection level**: a
854
- mediasoup WebRTC transport and a client's `RTCPeerConnection` share the same id, so whenever an
855
- observed peer connection's id matches one of the router's WebRTC transport ids, that's a match.
856
-
857
- **The observer does not store the router (or its sample) on any entity.** Instead, for **every**
858
- matching peer connection it emits **`mediasoup-router-matched-with-peer-connection`** and steps
859
- back — *your application* decides what the pairing means. The payload carries the full peer-connection
860
- ancestry, so you get the router **and** the matched `observedPeerConnection`, `observedClient` and
861
- `observedCall` in one place. Stamp the `routerId` into the peer connection's / client's `appData`,
862
- build your own index, attach the server sample to the call in your database — whatever fits.
863
-
864
- This matching is **opt-in**: pass `matchPeerConnectionByWebRtcTransportId: true` to
865
- `createObservedMediasoupRouter`. When enabled, as peer connections are observed
866
- (`peer-connection-added`) the observer checks whether the peer connection's id is one of the router's
867
- WebRTC transport ids; on a hit it emits — once per matching peer connection — and keeps watching, so a
868
- router serving many participants emits one match per participant's transport. When the flag is omitted
869
- or `false`, no matching is performed and the event never fires. The internal listener is removed
870
- automatically when the router closes or the observer closes.
871
-
872
- ### Ordering contract — observe the router first
873
-
874
- Matching is **forward-only by design**, and that is sufficient because the lifecycle ordering is
875
- **guaranteed, not racy**:
876
-
877
- - `ObservedMediasoupRouter` works purely by **subscribing to mediasoup's `observer` API**, so it can
878
- only see events that happen *after* it is created. You therefore create it the moment the router
879
- exists — **before** any transport is added to it — and it captures the rest going forward.
880
- - A mediasoup transport is always created **on the server first**; only then can the client connect
881
- to it, produce/consume, and begin shipping `ClientSample`s. So a peer connection — and the
882
- `peer-connection-added` event it triggers — can never appear before its server-side WebRTC
883
- transport already exists (and has been observed by the router).
884
-
885
- Put together: by the time a `peer-connection-added` fires, the router has already recorded that
886
- transport's id in `webrtcTransportIds`, so a single forward-looking listener catches every match. No
887
- back-scan of existing peer connections and no re-check on transport creation are needed — the
888
- observer deliberately does **not** look backwards.
889
-
890
- **Your responsibility:** call `createObservedMediasoupRouter(...)` as early as the router exists
891
- (before transports are added or samples are accepted). If you register the router *after* its
892
- transports are created or after the client's first sample, those events are already in the past and
893
- the corresponding matches are missed.
894
-
895
- When the underlying mediasoup router closes, its `close` propagates to `ObservedMediasoupRouter`,
896
- which sets the sample's `closedAt` and emits **`mediasoup-router-removed`** — your cue to read /
897
- persist the final `observedRouter.sample` and drop your reference to it.
898
-
899
- ### Options — `observer.createObservedMediasoupRouter(settings)`
900
-
901
- | Field | Type | Required | Meaning |
902
- |-------|------|----------|---------|
903
- | `router` | `mediasoup.types.Router` | yes | the live router to observe; the observer attaches to `router.observer`. `.id` and the sample's `routerId` come from `router.id` |
904
- | `appData` | `Record<string, unknown>` | no | application-owned bag on the `ObservedMediasoupRouter` |
905
- | `attachments` | `Record<string, unknown>` | no | free-form data; carried on `sample.attachments` |
906
- | `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 |
907
-
908
- Peer-connection matching is **off by default**; enable it with
909
- `matchPeerConnectionByWebRtcTransportId: true`. Returns the `ObservedMediasoupRouter`, or `undefined`
910
- if the observer is closed (a router with the same id returns the existing instance — both warn).
911
-
912
- Useful members on the returned object: `.sample` (the in-memory `MediasoupRouterSample`, with
913
- `createdAt` / `closedAt?` on it), `.appData`, `.attachments`, `.webrtcTransportIds: Set<string>`,
914
- `.id`, `.close()`.
915
-
916
- ### Example
917
-
918
- ```ts
919
- import { Observer, InMemorySink } from '@observertc/observer-js';
920
- import type { ObservedMediasoupRouterScope, ObservedPeerConnectionScope } from '@observertc/observer-js';
921
-
922
- const observer = new Observer();
923
-
924
- // 1) Feed client samples as usual so the observer knows about calls, clients & peer connections.
925
- // (e.g. transport-layer: observer.accept(clientSample, context))
926
-
927
- // 2) Observe the SFU side; opt in to peer-connection matching. State accumulates in `.sample`.
928
- const router = /* your mediasoup router */ undefined as any;
929
- const observedRouter = observer.createObservedMediasoupRouter({
930
- router,
931
- matchPeerConnectionByWebRtcTransportId: true,
932
- });
933
-
934
- // For large meetings, sample it yourself on your own cadence (see "Memory & large meetings"):
935
- // setInterval(() => persist(observedRouter.sample), 10_000);
936
-
937
- // 3) Every peer connection whose id matches one of the router's WebRTC transport ids fires this —
938
- // WE decide what to do with each pairing. The payload carries the full ancestry.
939
- observer.on('mediasoup-router-matched-with-peer-connection',
940
- ({ observedMediasoupRouter, observedCall, observedPeerConnection }:
941
- ObservedMediasoupRouterScope & ObservedPeerConnectionScope) => {
942
- (observedPeerConnection.appData ??= {}).routerId = observedMediasoupRouter.id;
943
- myStore.linkRouterToCall(observedCall.callId, observedMediasoupRouter.id);
944
- },
945
- );
946
-
947
- // 4) The router closed — read/persist the final state, then drop your reference.
948
- observer.on('mediasoup-router-removed', ({ observedMediasoupRouter }: ObservedMediasoupRouterScope) => {
949
- persist(observedMediasoupRouter.sample); // its `closedAt` is set
950
- });
951
- ```
952
-
953
- ### Why event-driven matching instead of storing on the call
954
-
955
- - **Loose coupling.** The call model stays about client telemetry; the SFU view lives on its own
956
- `ObservedMediasoupRouter` and is associated only if and how *you* choose.
957
- - **You own the association.** One router serves many peer connections (across clients and calls),
958
- and the right place to keep that mapping is application-specific — so the observer hands you each
959
- peer-connection match and gets out of the way.
960
- - **You own the sampling.** The router sample is plain in-memory state you read on your own terms;
961
- for large meetings, sample/persist it yourself (see [Memory & large meetings](#memory--large-meetings))
962
- rather than relying on the library to evict — it deliberately doesn't.
963
-
964
- ---
965
-
966
- ## Sinks (per-client sample persistence)
967
-
968
- A **sink** receives the samples a client accepts — for archival, streaming, or later offline
969
- replay. Each `ObservedClient` gets its **own** sink, produced by the
970
- `ObserverConfig.createClientSink` factory when the client is created (return `undefined` for no
971
- sink). The client pushes every accepted sample to its sink, and `end()`s it on close.
972
-
973
- ### The `ClientSampleSink` base class
974
-
975
- `ClientSampleSink` is an **abstract base class** (a typed `EventEmitter`). You create a sink by
976
- **subclassing it** and implementing `write` and `end`. It is **object-mode**: `write` receives
977
- the `ClientSample` *object*, so each sink decides how (or whether) to serialize it — JSON line,
978
- protobuf, a remote POST body, an in-memory push, etc.
979
-
980
- ```ts
981
- import { ClientSampleSink, ClientSample } from '@observertc/observer-js';
982
-
983
- abstract class ClientSampleSink /* extends EventEmitter */ {
984
- abstract write(sample: ClientSample): boolean; // accept one sample; `false` = backpressure
985
- abstract end(): void; // flush; emit `close` when the destination is ready
986
-
987
- // typed events (inherited): the listener signature is inferred from the event name
988
- on(event: 'close' | 'finish' | 'drain', listener: () => void): this;
989
- on(event: 'error', listener: (err: Error) => void): this;
990
- // ...and the matching `once` / `off` / `emit`
991
- }
992
- ```
993
-
994
- | Event | Meaning |
995
- |-------|---------|
996
- | `close` | the destination is fully written and closed (e.g. a file flushed and its fd closed) — "ready" |
997
- | `error` | the destination failed |
998
- | `finish` | `end()` was processed and queued data flushed (before `close`) |
999
- | `drain` | the buffer drained after backpressure; safe to write more |
1000
-
1001
- The library calls `write(sample)` **synchronously** per accepted sample (it is not awaited),
1002
- `end()`s the sink when the client closes, and attaches an `error` listener so a failing sink
1003
- can't crash the process (it also catches throws from `write`/`end`). The application — which
1004
- created the sink — listens for `close` (destination ready) and `error`. Because `write` isn't
1005
- awaited in the `accept()` hot path, **backpressure and batching are the sink's concern**.
1006
-
1007
- ### Built-in sinks
1008
-
1009
- ```ts
1010
- import { Observer, createJsonlFileSinkFactory } from '@observertc/observer-js';
1011
-
1012
- const observer = new Observer({
1013
- // one ./stats/<callId>__<clientId>.jsonl per client
1014
- createClientSink: createJsonlFileSinkFactory({ directory: './stats' }),
1015
- });
1016
-
1017
- // React when a sink is created for a client:
1018
- observer.on('client-sink-created', ({ observedClient, sink }) => {
1019
- sink.on('close', () => {
1020
- // the file is fully flushed and its fd closed — ready to upload, move, etc.
1021
- });
1022
- });
1023
- ```
1024
-
1025
- | Export | Signature | Notes |
1026
- |--------|-----------|-------|
1027
- | `createJsonlFileSinkFactory` | `({ directory, flags?, getFileName?, serializeSample? }) => ClientSampleSinkFactory` | per-client JSONL files; path defaults to `${callId}__${clientId}.jsonl` under `directory` (which **must exist**) |
1028
- | `createJsonlFileSink` | `({ path, flags?, serializeSample? }) => ClientSampleSink` | a single JSONL file; wraps `fs.WriteStream` and re-emits its `close`/`finish`/`drain`/`error` |
1029
- | `JsonlFileSink` | `class extends ClientSampleSink` | the underlying class; exposes `readonly path` so a `close` handler knows which file is ready |
1030
- | `createInMemorySink` / `InMemorySink` | `(samples?: ClientSample[]) => InMemorySink` | collects the accepted **sample objects** into `.samples: ClientSample[]`; emits `close` on `end()` |
1031
-
1032
- `serializeSample?: (sample: ClientSample) => string` overrides the default `JSON.stringify` for
1033
- the JSONL sinks (e.g. to redact or reshape before writing).
1034
-
1035
- ### Reading sink-specific info (e.g. the file path)
1036
-
1037
- The bus hands you the sink as the base `ClientSampleSink`. To read information specific to a sink
1038
- type — for a file sink, where it was written — **narrow with `instanceof`** and read the sink's
1039
- public fields. `JsonlFileSink` exposes `path`:
1040
-
1041
- ```ts
1042
- import { JsonlFileSink } from '@observertc/observer-js';
1043
-
1044
- observer.on('client-sink-created', ({ observedClient, sink }) => {
1045
- if (sink instanceof JsonlFileSink) {
1046
- const { path } = sink; // the file this client's samples go to
1047
- sink.once('close', () => uploadFile(path)); // close = flushed & fd closed → ready
1048
- }
1049
- });
1050
- ```
1051
-
1052
- The general pattern: each concrete sink exposes whatever it wants as `public readonly` fields, and
1053
- consumers narrow (`instanceof YourSink`) to read them. Your own sinks do the same.
1054
-
1055
- ### Writing your own sink
1056
-
1057
- Subclass `ClientSampleSink` and emit the lifecycle events yourself — for any non-file
1058
- destination (a remote endpoint, a message queue, an object store, …):
1059
-
1060
- ```ts
1061
- import { ClientSampleSink, ClientSample, ClientSampleSinkFactory } from '@observertc/observer-js';
1062
-
1063
- class HttpSink extends ClientSampleSink {
1064
- private buffer: ClientSample[] = [];
1065
- constructor(private readonly url: string) { super(); }
1066
-
1067
- write(sample: ClientSample): boolean {
1068
- this.buffer.push(sample); // batch; decide your own backpressure
1069
- return true;
1070
- }
1071
- end(): void {
1072
- fetch(this.url, { method: 'POST', body: JSON.stringify(this.buffer) })
1073
- .then(() => this.emit('close')) // signal "destination ready"
1074
- .catch((err) => this.emit('error', err));
1075
- }
1076
- }
1077
-
1078
- const createClientSink: ClientSampleSinkFactory = ({ clientId, observedCall }) =>
1079
- new HttpSink(`https://stats.example.com/${observedCall.callId}/${clientId}`);
1080
-
1081
- const observer = new Observer({ createClientSink });
1082
- ```
1083
-
1084
- `observedClient.sink?` exposes the created sink; the `client-sink-created` event delivers it on
1085
- the bus with full ancestry. `ClientSampleSinkFactory` is
1086
- `(p: { clientId: string; observedCall: ObservedCall }) => ClientSampleSink | undefined`.
1087
-
1088
- ---
1089
-
1090
- ## Injecting data into a client
1091
-
1092
- Sometimes the application holds data that belongs on a client's record but isn't part of the
1093
- client-reported `ClientSample` — a room id or display name, an application-level event
1094
- (*"recording started"*), a server-detected issue, an extension stat, or a device/meta item.
1095
- `ObservedClient` exposes **injection** methods that merge such data into the client's sample stream,
1096
- so it updates the live model **and** is persisted to the client's
1097
- [sink](#sinks-per-client-sample-persistence) exactly like sampled data.
1098
-
1099
- | Method | Adds to the sample's | Surfaces as |
1100
- |--------|----------------------|-------------|
1101
- | `injectAttachment(attachments)` | `attachments` (merged via `Object.assign`) | `observedClient.attachments` |
1102
- | `injectEvent(event: ClientEvent)` | `clientEvents` | `client-event` (plus any state the event drives) |
1103
- | `injectIssue(issue: ClientIssue)` | `clientIssues` | `client-issue` |
1104
- | `injectMetaData(meta: ClientMetaData)` | `clientMetaItems` | `client-metadata` |
1105
- | `injectExtensionStat(stat: ExtensionStat)` | `extensionStats` | `client-extension-stats` |
1106
-
1107
- ### When the injected data lands
1108
-
1109
- Injection is timing-aware so nothing is dropped, regardless of *when* you call it:
1110
-
1111
- - **During a sample's processing** — e.g. from inside a `client-updated` / `client-event` handler,
1112
- which run within `accept()` — the data is applied to the **current** sample immediately: reflected
1113
- in entity state and written to the sink as part of that sample.
1114
- - **Between samples** — the data is buffered and merged into the **next** `accept()`'s sample.
1115
- - **On `close()` with pending injections and no further sample** — the buffer is flushed as a final
1116
- synthetic sample (applied to state and written to the sink) before the sink is ended, so a
1117
- last-moment injection is never lost.
1118
-
1119
- In every case the injected data both updates the live `ObservedClient` and reaches the per-client
1120
- sink — the sink always receives the final, **injection-merged** sample (the sink write happens at the
1121
- end of `accept()`, after the merge).
1122
-
1123
- ### Example
1124
-
1125
- ```ts
1126
- // Enrich at creation from your app's knowledge of the participant. Injecting in `client-added`
1127
- // (which runs just before the first accept) lands on the first sample.
1128
- observer.on('client-added', ({ observedClient }) => {
1129
- observedClient.injectAttachment({ roomId: lookupRoomId(observedClient.clientId) });
1130
- });
1131
-
1132
- // Application-level signals at any time:
1133
- const client = observer.getObservedCall(callId)?.getObservedClient(clientId);
1134
- client?.injectEvent({ type: 'RECORDING_STARTED', timestamp: Date.now() });
1135
- client?.injectIssue({ type: 'app-kicked-participant', timestamp: Date.now() });
1136
- ```
1137
-
1138
- `attachments` are latest-wins (like sampled `attachments`): injecting a key overwrites its previous
1139
- value. `appData` is unaffected — injections flow into the sample/telemetry, not the app-owned
1140
- `appData` bag (see [Ingestion](#ingestion-accept-context--lifecycle)).
1141
-
1142
- ## Logging
1143
-
1144
- `observer-js` logs through a single, swappable sink. Out of the box it writes `debug` and
1145
- above to `console` (verbose — install your own sink for production). Funnel everything into your
1146
- logger:
1147
-
1148
- ```ts
1149
- import { setObserverLogger, type ObserverLogger } from '@observertc/observer-js';
1150
-
1151
- setObserverLogger({
1152
- trace: (m, ...a) => myLogger.trace(`[${m}]`, ...a),
1153
- debug: (m, ...a) => myLogger.debug(`[${m}]`, ...a),
1154
- info: (m, ...a) => myLogger.info(`[${m}]`, ...a),
1155
- warn: (m, ...a) => myLogger.warn(`[${m}]`, ...a),
1156
- error: (m, ...a) => myLogger.error(`[${m}]`, ...a),
1157
- });
1158
- ```
1159
-
1160
- `createLogger(moduleName)` is also exported for your own modules. See
1161
- **[`docs/logging.md`](./docs/logging.md)** for pino / winston / console recipes, level
1162
- filtering, per-module routing, and full silencing.
1163
-
1164
- ---
1165
-
1166
- ## Error-handling philosophy
1167
-
1168
- The library **warns and degrades; it does not throw** on operational problems:
1169
-
1170
- - `createObservedCall` / `createObservedClient` on a closed parent → warn + return `undefined`.
1171
- - Duplicate id → warn + return the **existing** instance.
1172
- - `accept()` on a closed client → warn + no-op.
1173
- - Sample missing `callId`/`clientId`, or observer closed → `sample-rejected` event.
1174
- - A throwing accept-middleware → warn + drop that sample (never crashes `accept()`).
1175
-
1176
- Therefore `create*` and `getOrCreate*` return `T | undefined`; **guard the result.** The
1177
- `Middleware` utility's internal invariants (e.g. calling `next()` twice) throw, but those throws
1178
- are caught by `accept()` and surfaced as a warning.
1179
-
1180
- ---
1181
-
1182
- ## Development & extension guide
1183
-
1184
- ```bash
1185
- yarn install
1186
- yarn build # tsup → dist/ (dual ESM .mjs + CJS .js, single entry, .d.ts/.d.mts + sourcemaps)
1187
- yarn lint # eslint -c .eslintrc.json "src/**/*.ts"
1188
- yarn typecheck # tsc --noEmit
1189
- yarn test # jest
1190
- ```
1191
-
1192
- The build is driven by [`tsup`](https://tsup.egoist.dev) (config in `tsup.config.ts`): a single
1193
- entry (`src/index.ts`), dual ESM + CommonJS output to `dist/` (`index.mjs` / `index.js`) with
1194
- `.d.mts` / `.d.ts` types and sourcemaps, targeting Node 20. CI (`.github/workflows/ci.yml`) runs
1195
- lint + typecheck + **build** + test on every push/PR.
1196
-
1197
- **Project layout** (`src/`): `Observer.ts`, `ObservedCall.ts`, `ObservedClient.ts`,
1198
- `ObservedPeerConnection.ts`, the `Observed*` sub-stat classes, `ObserverEvents.ts` (the typed
1199
- event map + scope types), `detectors/` (`Detector`, `Detectors`), `scores/`, `updaters/`
1200
- (update-policy strategies), `utils/` (remote-track resolvers), `common/` (`logger`, `utils`,
1201
- `Middleware`), `schema/` (sample/event/meta types), and `sinks/` (the `ClientSampleSink` base +
1202
- `JsonlFileSink` / `InMemorySink`, re-exported from the package root).
1203
-
1204
- **Conventions to follow when developing further:**
1205
-
1206
- - *Single event bus.* New consumer-facing events go in `ObserverEvents.ts` with an object
1207
- payload `[<Scope> & { …subject }]`, and are emitted via the component's
1208
- `_notify(type, { ...this.eventScope, …subject })`. Each component has a precomputed
1209
- `eventScope` field and a thin `_notify` wrapper around the right emitter. Keep purely internal
1210
- coordination as **local** EventEmitter events (and remember to `off` them on close).
1211
- - *Warn, don't throw* on operational/edge conditions; return `undefined` where a value can't be produced.
1212
- - *Counter-reset-safe deltas.* When computing a delta from a cumulative counter, never emit a
1213
- negative value (guard `curr >= prev`), to survive counter resets / SSRC reuse.
1214
- - *Explicit accumulation.* The per-sample metric accumulation in `accept()` is intentionally
1215
- explicit and not abstracted — match that style.
1216
- - *Detectors are server-side.* Add cross-client detectors on `ObservedCall.detectors`; don't
1217
- re-implement client-detectable signals.
1218
-
1219
- **Recipes:**
1220
-
1221
- - *Add an event:* add the key + payload to `ObserverEvents`; in the owning component call
1222
- `this._notify('my-event', { ...this.eventScope, subject })`.
1223
- - *Add a per-stream metric:* add the field to the relevant `Observed*Rtp`/track class, populate
1224
- it in its `update()` (reset at the top of `update()` if it's per-tick), and read it from a
1225
- `*-updated` handler.
1226
- - *Add a detector:* implement `Detector`, register it on `call-added` via
1227
- `observedCall.detectors.add(...)`, surface findings with `observedCall.addIssue(...)`.
1228
-
1229
- ---
1230
-
1231
- ## License
1232
-
1233
- Apache-2.0. Part of the [ObserverTC](https://github.com/observertc) ecosystem.
1234
-
1235
- ---
1236
-
1237
- # ============================================================================
1238
- # docs/logging.md
1239
- # ============================================================================
1240
-
1241
- # Logging in `@observertc/observer-js`
1242
-
1243
- `observer-js` writes diagnostic logs from its internal modules (the `Observer`, each
1244
- `Observed*` entity, the updaters, etc.). All of that output is routed through a single,
1245
- swappable sink so that the host application stays in full control of *where* logs go,
1246
- *what level* is kept, and *how* they are formatted. This document explains the model and
1247
- gives copy-paste recipes for funneling the logs into `pino`, `winston`, plain `console`,
1248
- or nothing at all.
1249
-
1250
- ## TL;DR
1251
-
1252
- ```ts
1253
- import { setObserverLogger } from '@observertc/observer-js';
1254
-
1255
- // Funnel every observer-js log line into your own logger.
1256
- setObserverLogger({
1257
- trace: (module, ...args) => myLogger.trace(`[${module}]`, ...args),
1258
- debug: (module, ...args) => myLogger.debug(`[${module}]`, ...args),
1259
- info: (module, ...args) => myLogger.info(`[${module}]`, ...args),
1260
- warn: (module, ...args) => myLogger.warn(`[${module}]`, ...args),
1261
- error: (module, ...args) => myLogger.error(`[${module}]`, ...args),
1262
- });
1263
- ```
1264
-
1265
- That's the whole integration surface. The rest of this document is detail.
1266
-
1267
- ## The model
1268
-
1269
- There are two interfaces and two functions, all exported from the package root:
1270
-
1271
- ```ts
1272
- import {
1273
- createLogger, // (moduleName: string) => Logger
1274
- setObserverLogger, // (logger: ObserverLogger) => void
1275
- type Logger,
1276
- type ObserverLogger,
1277
- } from '@observertc/observer-js';
1278
- ```
1279
-
1280
- - **`Logger`** — what each internal module holds. Five level methods, each variadic:
1281
- `trace`, `debug`, `info`, `warn`, `error`, all `(...args: any[]) => void`.
1282
- - **`ObserverLogger`** — the single *sink* every `Logger` forwards to. Same five levels,
1283
- but each receives the originating **module name** as the first argument:
1284
- `(module: string, ...args: any[]) => void`.
1285
-
1286
- Internally, each file does:
1287
-
1288
- ```ts
1289
- const logger = createLogger('ObservedPeerConnection');
1290
- // ...
1291
- logger.warn('Received sample without callId. %o', sample);
1292
- ```
1293
-
1294
- `createLogger(moduleName)` returns a `Logger` whose every call forwards to the process-wide
1295
- `ObserverLogger`, injecting `moduleName`. So the line above ultimately calls:
1296
-
1297
- ```ts
1298
- observerLogger.warn('ObservedPeerConnection', 'Received sample without callId. %o', sample);
1299
- ```
1300
-
1301
- `setObserverLogger(...)` replaces that process-wide sink. One call reroutes **all** logging
1302
- from **every** `Observer` instance in the process.
1303
-
1304
- ```
1305
- ObservedCall ─┐
1306
- ObservedClient ┤ createLogger('<module>') ──► the single ObserverLogger ──► your logger
1307
- Observer ──────┘ (per module) (set via setObserverLogger)
1308
- ```
1309
-
1310
- ## Default behavior (important)
1311
-
1312
- If you never call `setObserverLogger`, the built-in sink writes to `console`:
1313
-
1314
- | Level | Default destination |
1315
- |-------|---------------------|
1316
- | `trace` | dropped (no-op) |
1317
- | `debug` | `console.log` |
1318
- | `info` | `console.info` |
1319
- | `warn` | `console.warn` |
1320
- | `error` | `console.error` |
1321
-
1322
- …each prefixed with `"[LEVEL] <module>"`. This means **the default is fairly verbose
1323
- (`debug` and up go to the console).** For anything beyond local experimentation you should
1324
- install your own sink with an appropriate level — see below.
1325
-
1326
- ## Message format
1327
-
1328
- Internal log calls use Node/`console`-style printf placeholders (`%s`, `%o`, `%d`, `%j`)
1329
- followed by the substitution values, and sometimes a trailing object:
1330
-
1331
- ```ts
1332
- logger.warn('Observed Call with id %s already exists; returning the existing instance', callId);
1333
- logger.warn('Received sample without clientId %o', sample);
1334
- ```
1335
-
1336
- Your `ObserverLogger` receives these as `(module, formatString, ...values)`. A logger that
1337
- understands printf placeholders (`console`, `pino`) can pass them straight through; for
1338
- others, format them yourself with `util.format` (recipe below).
1339
-
1340
- ## When can I call `setObserverLogger`?
1341
-
1342
- Any time. The sink is read on every log call, not captured at import, so a later
1343
- `setObserverLogger(...)` immediately affects all subsequent logs. There is no ordering
1344
- requirement relative to constructing an `Observer`. Note it is **global/process-wide**:
1345
- the last call wins and applies to all `Observer` instances.
1346
-
1347
- ---
1348
-
1349
- ## Recipes
1350
-
1351
- ### 1. Silence everything
1352
-
1353
- ```ts
1354
- import { setObserverLogger } from '@observertc/observer-js';
1355
-
1356
- const noop = () => undefined;
1357
- setObserverLogger({ trace: noop, debug: noop, info: noop, warn: noop, error: noop });
1358
- ```
1359
-
1360
- ### 2. Console, but only `warn` and above
1361
-
1362
- ```ts
1363
- import { setObserverLogger } from '@observertc/observer-js';
1364
-
1365
- setObserverLogger({
1366
- trace: () => undefined,
1367
- debug: () => undefined,
1368
- info: () => undefined,
1369
- warn: (module, ...args) => console.warn(`[WARN] ${module}`, ...args),
1370
- error: (module, ...args) => console.error(`[ERROR] ${module}`, ...args),
1371
- });
1372
- ```
1373
-
1374
- ### 3. pino (recommended for servers)
1375
-
1376
- `observer-js` does not depend on `pino` — you bring your own. pino understands printf
1377
- placeholders, and passing `{ module }` as the merging object keeps the module name as a
1378
- structured field (so you can filter by it later).
1379
-
1380
- ```ts
1381
- import pino from 'pino';
1382
- import { setObserverLogger, type ObserverLogger } from '@observertc/observer-js';
1383
-
1384
- const root = pino({ level: 'warn', name: 'observer-js' });
1385
-
1386
- const adapter: ObserverLogger = {
1387
- trace: (module, ...args) => root.trace({ module }, ...args),
1388
- debug: (module, ...args) => root.debug({ module }, ...args),
1389
- info: (module, ...args) => root.info({ module }, ...args),
1390
- warn: (module, ...args) => root.warn({ module }, ...args),
1391
- error: (module, ...args) => root.error({ module }, ...args),
1392
- };
1393
-
1394
- setObserverLogger(adapter);
1395
- ```
1396
-
1397
- Level filtering is handled by pino (`level: 'warn'` above), so the dropped levels cost
1398
- almost nothing. If you prefer a dedicated child logger per module:
1399
-
1400
- ```ts
1401
- const children = new Map<string, pino.Logger>();
1402
- const child = (module: string) =>
1403
- children.get(module) ?? children.set(module, root.child({ module })).get(module)!;
1404
-
1405
- const adapter: ObserverLogger = {
1406
- trace: (m, ...a) => child(m).trace(...a),
1407
- debug: (m, ...a) => child(m).debug(...a),
1408
- info: (m, ...a) => child(m).info(...a),
1409
- warn: (m, ...a) => child(m).warn(...a),
1410
- error: (m, ...a) => child(m).error(...a),
1411
- };
1412
- setObserverLogger(adapter);
1413
- ```
1414
-
1415
- ### 4. winston
1416
-
1417
- winston's level methods don't do printf substitution the same way, so format the message
1418
- with `util.format` first:
1419
-
1420
- ```ts
1421
- import { format } from 'node:util';
1422
- import { createLogger as createWinston, transports, format as wformat } from 'winston';
1423
- import { setObserverLogger, type ObserverLogger } from '@observertc/observer-js';
1424
-
1425
- const w = createWinston({
1426
- level: 'warn',
1427
- transports: [new transports.Console()],
1428
- format: wformat.json(),
1429
- });
1430
-
1431
- const adapter: ObserverLogger = {
1432
- trace: (module, ...args) => w.silly(format(...args), { module }),
1433
- debug: (module, ...args) => w.debug(format(...args), { module }),
1434
- info: (module, ...args) => w.info(format(...args), { module }),
1435
- warn: (module, ...args) => w.warn(format(...args), { module }),
1436
- error: (module, ...args) => w.error(format(...args), { module }),
1437
- };
1438
-
1439
- setObserverLogger(adapter);
1440
- ```
1441
-
1442
- ### 5. Generic / any logger (`util.format`)
1443
-
1444
- The most portable adapter — works with any logger that takes a single string:
1445
-
1446
- ```ts
1447
- import { format } from 'node:util';
1448
- import { setObserverLogger, type ObserverLogger } from '@observertc/observer-js';
1449
-
1450
- const toLine = (module: string, args: unknown[]) => `[${module}] ${format(...args)}`;
1451
-
1452
- const adapter: ObserverLogger = {
1453
- trace: (module, ...args) => myLogger.trace(toLine(module, args)),
1454
- debug: (module, ...args) => myLogger.debug(toLine(module, args)),
1455
- info: (module, ...args) => myLogger.info(toLine(module, args)),
1456
- warn: (module, ...args) => myLogger.warn(toLine(module, args)),
1457
- error: (module, ...args) => myLogger.error(toLine(module, args)),
1458
- };
1459
-
1460
- setObserverLogger(adapter);
1461
- ```
1462
-
1463
- ### 6. Filter or re-route specific modules
1464
-
1465
- Because the sink receives the module name, you can route or drop per module:
1466
-
1467
- ```ts
1468
- const NOISY = new Set(['ObservedPeerConnection']);
1469
-
1470
- setObserverLogger({
1471
- trace: () => undefined,
1472
- debug: (module, ...args) => { if (!NOISY.has(module)) root.debug({ module }, ...args); },
1473
- info: (module, ...args) => root.info({ module }, ...args),
1474
- warn: (module, ...args) => root.warn({ module }, ...args),
1475
- error: (module, ...args) => root.error({ module }, ...args),
1476
- });
1477
- ```
1478
-
1479
- ## Reusing the funnel for your own modules
1480
-
1481
- `createLogger` is exported, so your application code can emit through the same sink and
1482
- inherit whatever routing/level you configured with `setObserverLogger`:
1483
-
1484
- ```ts
1485
- import { createLogger } from '@observertc/observer-js';
1486
-
1487
- const logger = createLogger('my-app:ingest');
1488
- logger.info('accepted %d samples in %dms', count, elapsedMs);
1489
- ```
1490
-
1491
- ## Notes & caveats
1492
-
1493
- - **Global, not per-instance.** `setObserverLogger` sets one process-wide sink shared by
1494
- every `Observer`. There is currently no per-`Observer` logger override.
1495
- - **Level filtering belongs in your logger.** `observer-js` always calls the sink for
1496
- `debug`/`info`/`warn`/`error` (and `trace`, though the default drops it); decide what to
1497
- keep inside your `ObserverLogger` (or, with pino/winston, via their `level`).
1498
- - **No bundled transport.** `observer-js` never imports `pino`, `winston`, or any logging
1499
- library — you wire in whichever you already use.
1500
- - **`trace`** is intended for very high-frequency, per-sample diagnostics; keep it dropped
1501
- in production.
1502
-
1503
- ## Type reference
1504
-
1505
- ```ts
1506
- interface Logger {
1507
- trace(...args: any[]): void;
1508
- debug(...args: any[]): void;
1509
- info(...args: any[]): void;
1510
- warn(...args: any[]): void;
1511
- error(...args: any[]): void;
1512
- }
1513
-
1514
- interface ObserverLogger {
1515
- trace(module: string, ...args: any[]): void;
1516
- debug(module: string, ...args: any[]): void;
1517
- info(module: string, ...args: any[]): void;
1518
- warn(module: string, ...args: any[]): void;
1519
- error(module: string, ...args: any[]): void;
1520
- }
1521
-
1522
- function createLogger(moduleName: string): Logger;
1523
- function setObserverLogger(logger: ObserverLogger): void;
1524
- ```
1525
-
1526
- ---
1527
-
1528
- # ============================================================================
1529
- # CHANGELOG.md
1530
- # ============================================================================
1531
-
1532
- # Changelog
1533
-
1534
- ## 1.0.0
1535
-
1536
- First stable release of `@observertc/observer-js` — a server-side Node.js library that turns a
1537
- stream of WebRTC `ClientSample`s into a live, queryable model of every call, with a single typed
1538
- event bus and pluggable extension points. This entry states the full set of features and concepts
1539
- the 1.0.0 API provides.
1540
-
1541
- ### Core model
1542
-
1543
- - **Entity hierarchy.** `Observer → ObservedCall → ObservedClient → ObservedPeerConnection →`
1544
- sub-stats (inbound/outbound RTP, remote inbound/outbound RTP, inbound/outbound tracks, codecs,
1545
- data channels, ICE transports/candidates/candidate-pairs, certificates, media sources, media
1546
- playouts, peer-connection transports). Every node holds current + cumulative metrics and is
1547
- reachable by id through `Map`s on its parent.
1548
- - **One ingestion method.** `observer.accept(sample, context?)` is the only way data gets in.
1549
- Calls, clients and peer connections are **created lazily** the first time their id appears.
1550
- - **Single event bus.** Everything worth subscribing to is emitted on the **`Observer`** itself,
1551
- with a payload object that carries the full ancestry (observer → call → client → peer connection)
1552
- down to the subject. `on`/`off`/`once`/`emit` are fully typed against the event map. Local
1553
- EventEmitter lifecycle events remain on each component for internal teardown wiring.
1554
- - **Pull or react.** Read fields off the entities at any time, and/or subscribe to events.
1555
-
1556
- ### Ingestion & configuration
1557
-
1558
- - **`accept(sample, context?)`** with a transient, free-form `AcceptContext` that is threaded down
1559
- the accept chain and carried to the `*-updated` events of that pass — and **never** written to
1560
- `appData`.
1561
- - **`appData` vs `context`.** `appData` is application-owned, fixed at entity creation (via
1562
- `settings.appData` or the `createCallAppData` / `createClientAppData` factories) and never mutated
1563
- by the library; `context` is per-accept and ephemeral.
1564
- - **Accept middlewares.** `observer.acceptMiddlewares` — a global, pre-dispatch chain run on every
1565
- sample before it reaches any call/client (inspect, mutate/normalize ids, enrich).
1566
- - **Event-driven update policies.** Observer and call aggregation triggers are
1567
- `update-on-any-…-updated` / `update-when-all-…-updated` / `none`. There are **no internal timers**;
1568
- with `none` (or to drive a fixed cadence) the app calls the public `observer.update()` /
1569
- `call.update()` itself.
1570
- - **Automatic teardown.** Optional `closeClientIfIdleForMs` and `closeCallIfEmptyForMs`; closing
1571
- cascades down the tree and emits the matching `*-closed` / `*-removed` events.
1572
-
1573
- ### Derived metrics
1574
-
1575
- - **Counter-reset-safe deltas.** Per-tick deltas never go negative across counter resets / SSRC
1576
- reuse (guarded `curr >= prev`).
1577
- - **Per-stream metrics**: bitrates, packet/byte deltas, jitter, fraction-lost, RTT, etc.
1578
- - **Remote-RTP correlation.** Receiver/sender RTCP reports are linked to the local stream by
1579
- `remoteId`/SSRC and surfaced as `remote*` fields (e.g. `remoteRttInMs`, `remoteFractionLost` on
1580
- outbound; `remoteRttInMs`, `remoteBytesSent` on inbound).
1581
- - **TURN/TCP usage** derived from the selected ICE candidate pair (`usingTURN` / `usingTCP`), with a
1582
- call-level `ObservedTURN` view.
1583
-
1584
- ### Optional track correlation
1585
-
1586
- - **`RemoteTrackResolver`** — a generic, strategy-driven resolver that links a published (outbound)
1587
- track to the subscribed (inbound) tracks carrying it (**one publisher → many subscribers**) by a
1588
- **publisher id** (the link key). Links are maintained directly on the tracks
1589
- (`inboundTrack.remoteOutboundTrack`, `outboundTrack.remoteInboundTracks`).
1590
- - **Opt-in via `ObserverConfig.createRemoteTrackResolver`**, invoked per call. Built-in factories:
1591
- `createDefaultMediasoupRemoteTrackResolverFactory()` (producerId/consumerId attachments) and
1592
- `createP2pRemoteTrackResolverFactory()` (RTP SSRC). Custom topologies supply their own
1593
- publisher/subscriber id resolvers.
1594
-
1595
- ### Per-client sample sinks
1596
-
1597
- - **`ClientSampleSink`** — an object-mode, `EventEmitter`-based base class (`write(sample)`,
1598
- `end()`, typed `close`/`error`/`finish`/`drain` events). One sink per `ObservedClient`, produced
1599
- by `ObserverConfig.createClientSink`; the `client-sink-created` event delivers it on the bus.
1600
- - **Built-ins:** `JsonlFileSink` (+ `createJsonlFileSink` / `createJsonlFileSinkFactory`, exposing
1601
- `path` and a custom `serializeSample`) and the env-agnostic `InMemorySink`.
1602
-
1603
- ### Detectors & call-level issues
1604
-
1605
- - **`Detectors`** — a server-side extension-point registry on `ObservedCall` (ships empty; runs each
1606
- registered detector on `call.update()`, isolating throws). `call.addIssue(...)` raises a
1607
- call-level issue surfaced on the bus as `call-issue`; client-reported issues surface as
1608
- `client-issue`.
1609
-
1610
- ### Logging
1611
-
1612
- - **Pluggable logger** — a single swappable sink (`setObserverLogger`, `createLogger`) with a
1613
- documented funnel for pino/winston/console (`docs/logging.md`).
1614
-
1615
- ### Error-handling philosophy
1616
-
1617
- - **Warn, don't throw** on operational/edge conditions. `create*` / `getOrCreate*` return
1618
- `T | undefined` (closed parent → `undefined` + warn; duplicate id → existing instance + warn).
1619
- Missing `callId`/`clientId` or a closed observer → a `sample-rejected` event.
1620
-
1621
- ### Packaging & tooling
1622
-
1623
- - **Server-side, Node.js ≥ 22.** Shipped as a **dual ESM + CommonJS** build (single entry) via
1624
- `tsup`, with `.d.ts`/`.d.mts` types and sourcemaps; works with both `import` and `require()`.
1625
- - **CI gate** (lint + typecheck + build + tested with a **coverage threshold**) and **npm Trusted
1626
- Publishing (OIDC) with provenance**.
1627
-
1628
- ### Documentation
1629
-
1630
- - A self-sufficient `README.md`; `docs/logging.md`; and design docs for the roadmap, track
1631
- correlation & detectors, the call-level-detector analysis, and the (deferred) report-generation
1632
- approach.