Commit 2a01b7a
refactor: webstreams overhaul (#171)
* refactor(webstreams): remove dead code and add WebRTC type extensions
- Logger: drop LogLevel enum and setLevel/getLevel; use plain number constant
- Queue: remove 7 unused methods (dequeue, peek, size, isEmpty, clear, toArray,
Symbol.iterator); replace boolean processing flag with drainPromise; replace
Math.random() IDs with sequential counter; drain() now re-throws on error
- WebRtcExtensions.ts: add typed interfaces for insertable streams API
(EncodedStreams, RTCRtpScriptTransformOptions with optional kind, sender/
receiver with transform, RTCConfigurationWithInsertableStreams,
WindowWithRTCRtpScriptTransform, WindowWithWasmHandler)
* refactor(webstreams): fix layer boundaries and extract EncryptTransform
Move service/ types into their owning layer:
- src/service/EventDispatcher.ts → src/webStreams/EventDispatcher.ts
(fix removeOnStateChangeListener filter, add JSDoc, wrap emit in try/catch)
- src/service/WebRtcInterface.ts → src/webStreams/WebRtcInterface.ts
(remove server event types that belong in native layer; add WebRtcMethodCall
discriminated union; import Jsep from ApiTypes)
ApiTypes.ts:
- Add Jsep interface (sdp + RTCSdpType) replacing the loose string in WebRtcInterface
- Add StreamTrack interface (replaces StreamApi-local definition)
- Remove dead types: StreamRemoteInfo, StreamList, StreamTrackList, PublishMeta,
TrackInfo, StreamRoomInfo, StreamRoomList, TrackType
WebRtcInterfaceImpl.ts:
- Update import paths to new locations
- Replace { [K: string]: Function } methodsMap with typed MethodMap
- Fix method bindings with .bind(this) and typed lambda for updateSessionId
- methodCall now uses WebRtcMethodCall["name"] as key type
EncryptTransform.ts (new src/webStreams/worker/):
- Extract per-frame AES-256-GCM logic out of the monolithic worker.ts
- Pure class: no worker globals, no postMessage, no module state
- encryptFrame takes lastRms as a parameter; decryptFrame returns rms | null
worker.ts:
- Use EncryptTransform and new WorkerEvents discriminated unions
- Add RTCRtpScriptTransform entry point (modern browsers) alongside postMessage
- Add stop operation with AbortController-based pipeline cancellation
- logPipelineError ignores AbortError in addition to "Destination stream closed"
KeyStore.ts:
- Make setKeys async (was fire-and-forget; importKeyAndWipeMaterial is async)
- Add getEncryptionExternalKeyId() and resolveKeyId() stubs
(same value as getEncryptionKeyId for now; session-prefix refactor comes later)
Add EncryptTransform.test.ts covering audio/video encrypt+decrypt round-trips,
header preservation, tamper detection, and short-frame passthrough.
* fix(streaming): ICE trickle serialisation, kind propagation, DataChannelCryptor
ICE trickle candidate serialisation (StreamApiNative):
- Replace JSON.stringify(candidate) with serializeCandidate() which explicitly
reads all 15 RTCIceCandidate fields; toJSON() only emits 4 fields causing
bridge-side parse failures for candidates with extension fields
RTCRtpScriptTransform kind propagation (WebRtcClient):
- Pass kind: track.kind to every encode/decode transform option so the worker
selects the correct header-size table (audio=1B, video key=10B, delta=3B)
instead of always defaulting to video; fixes silent audio encryption
DataChannelCryptor:
- Use getEncryptionExternalKeyId() for the wire key ID and resolveKeyId() when
passing to CryptoFacade; separates wire format from internal registry key
- Extract GCM_TAG_LENGTH_BYTES = 16 named constant (was magic number)
- Move Logger to class field (avoids per-call allocation)
- Rename keyId → externalKeyId throughout for clarity
- Add JSDoc on encrypt/decryptFromWireFormat
* refactor(webstreams): decompose WebRtcClient into focused services
Splits the 750-line monolithic WebRtcClient into ten single-responsibility
services (PublisherManager, SubscriberManager, DataChannelSession,
KeySyncManager, PeerConnectionFactory, PeerConnectionManager,
E2eeWorker, E2eeTransformManager, AudioManager,
RemoteStreamListenerRegistry). WebRtcClient becomes a thin facade.
Circular dependencies (PeerConnectionManager→WebRtcClient trickle,
PeerConnectionFactory→SubscriberManager.onRemoteTrack,
E2eeWorker→AudioManager RMS callback) are broken by resolving lazily
inside callback closures in EndpointFactory.createStreamApi.
StreamApiNative now takes WebRtcInterfaceImpl directly; bindApiInterface
wiring is done in EndpointFactory. Removes dead files: WebWorkerHelper,
WebRtcConfig, WebRtcClientTypes, PeerConnectionsManager, Utils.
* refactor(ioc): add IoC container, rename api→native, wire EndpointFactory
- Rename src/api/ → src/native/ (git mv, all imports updated)
- Delete ApiStatic singleton; pass Api explicitly via constructor injection
- Add src/ioc/ with Container, Tokens, buildConnectionApis, buildWebRtcClient
- Replace EndpointFactory manual wiring with GlobalContainer/ConnectionContainer
- Update Connection: private apisRefs, registerApi()/hasApi(), restore freeApis
- Update ExtKey static methods to accept Api as first argument
- Remove abstract newApi() from BaseNative
* fix(webrtc-lifecycle): session-scoped KeyStore, destroyRefs teardown, worker fix
- KeyStore: add sessionPrefix (crypto.randomUUID) to prevent cross-session
key ID collisions; resolveKeyId() maps external→internal; renamed
getEncryptionExternalKeyId() strips prefix for wire format
- StreamApi: override destroyRefs() to call client.destroy() on disconnect
- StreamApi: tighten Map types, use EndpointTypes.StreamRoomId throughout
- BaseApi: remove unused BaseNative import
- EventQueue: initialise deferedPromise as null to prevent stale state
- ConnectionNative/EventQueueNative: remove now-dead stub newApi() overrides
- worker.ts: default kind to "video" when undefined
- Add tests: AudioManager, E2eeTransformManager, RemoteStreamListenerRegistry
* refactor(audio): translate rms-processor comments to English, add frame throttling
Polish the AudioWorkletProcessor: replace Polish inline comments with English,
add per-4-frame throttle on postMessage to reduce main-thread pressure.
* refactor(service): tighten optional param signatures, make native fields private
Remove redundant `| undefined` from optional parameters in CryptoApi and
InboxApi; tighten ThreadApi and KvdbApi native fields from protected to
private to prevent accidental subclass access.
* refactor(rename-disambiguate): delete obsolete types, simplify PrivmxClient caching
Remove five dead Janus-era type files (BaseServerTypes, MediaServerWebSocketApiTypes,
SignalingReceiverTypes, SignalingSenderTypes, StreamsApiTypes) and the ServerTypes.ts
barrel that re-exported them. Strip unused ListQuery and StreamAndTracksSelector from
ApiTypes. Simplify PrivmxClient: CryptoApi and EventQueue are now global container
singletons so the local Promise caches are redundant.
* refactor(crypto): add explicit type casts and fix constructor signature forms
Tighten type precision in the crypto layer: explicit `as ArrayBuffer` in Utils,
`as string` and `as elliptic.ec.Signature & { recoveryParam }` casts in EmCrypto,
`!` non-null assertion for `crypto.subtle`, and `{ new(...): T }` form for
constructor-type parameters in assert helpers.
* fix: lint fix
* devel merge and adjustments
* fix(audio): replace activeSince/active with activeUntil in ActiveSpeakerDetector
The old hold-off logic tracked `activeSince` (start of continuous speech)
and marked speakers inactive once `now - activeSince >= holdMs` — the
opposite of the intended behaviour: speakers went silent after holdMs of
*continuous* speech, not holdMs after speech *ended*.
Replace with `activeUntil = timestamp + holdMs`, reset on every
above-threshold frame (aligned with Java PR #145 SpeakingAnalyzer).
Callers evaluate `Date.now() <= state.activeUntil` instead of a stale
boolean snapshot. Add 25 unit tests covering detection, hold-off,
activity window, multi-speaker, pruning, and noise floor adaptation.
* ExtKey api fix
---------
Co-authored-by: Paweł Aniszewski <paniszewski@simplito.com>1 parent a954ef5 commit 2a01b7a
83 files changed
Lines changed: 4481 additions & 2686 deletions
File tree
- src
- api
- crypto
- extra
- ioc
- native
- service
- webStreams
- __tests__
- audio
- types
- worker
- __tests__
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
| 83 | + | |
83 | 84 | | |
84 | 85 | | |
85 | 86 | | |
| |||
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
173 | 173 | | |
174 | 174 | | |
175 | 175 | | |
176 | | - | |
| 176 | + | |
177 | 177 | | |
178 | 178 | | |
179 | 179 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
553 | 553 | | |
554 | 554 | | |
555 | 555 | | |
556 | | - | |
| 556 | + | |
557 | 557 | | |
558 | 558 | | |
559 | 559 | | |
| |||
654 | 654 | | |
655 | 655 | | |
656 | 656 | | |
657 | | - | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
658 | 661 | | |
659 | 662 | | |
660 | 663 | | |
| |||
716 | 719 | | |
717 | 720 | | |
718 | 721 | | |
719 | | - | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
720 | 725 | | |
721 | 726 | | |
722 | 727 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
| 16 | + | |
20 | 17 | | |
21 | 18 | | |
22 | 19 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
29 | | - | |
| 29 | + | |
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
| 42 | + | |
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | | - | |
52 | | - | |
53 | 51 | | |
54 | 52 | | |
55 | 53 | | |
| |||
97 | 95 | | |
98 | 96 | | |
99 | 97 | | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | 98 | | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
| 99 | + | |
111 | 100 | | |
112 | 101 | | |
113 | 102 | | |
114 | 103 | | |
115 | 104 | | |
116 | 105 | | |
117 | 106 | | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | 107 | | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
| 108 | + | |
129 | 109 | | |
130 | 110 | | |
131 | 111 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
31 | | - | |
| 31 | + | |
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| |||
0 commit comments