@dtelecom/livekit-client 1.12.1
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/LICENSE +175 -0
- package/README.md +315 -0
- package/dist/@dtelecom/livekit-client.e2ee.worker.js +2 -0
- package/dist/@dtelecom/livekit-client.e2ee.worker.js.map +1 -0
- package/dist/@dtelecom/livekit-client.e2ee.worker.mjs +1633 -0
- package/dist/@dtelecom/livekit-client.e2ee.worker.mjs.map +1 -0
- package/dist/@dtelecom/livekit-client.esm.mjs +22545 -0
- package/dist/@dtelecom/livekit-client.esm.mjs.map +1 -0
- package/dist/@dtelecom/livekit-client.umd.js +2 -0
- package/dist/@dtelecom/livekit-client.umd.js.map +1 -0
- package/dist/@dtelecom/src/api/SignalClient.d.ts +102 -0
- package/dist/@dtelecom/src/api/SignalClient.d.ts.map +1 -0
- package/dist/@dtelecom/src/connectionHelper/ConnectionCheck.d.ts +26 -0
- package/dist/@dtelecom/src/connectionHelper/ConnectionCheck.d.ts.map +1 -0
- package/dist/@dtelecom/src/connectionHelper/checks/Checker.d.ts +59 -0
- package/dist/@dtelecom/src/connectionHelper/checks/Checker.d.ts.map +1 -0
- package/dist/@dtelecom/src/connectionHelper/checks/publishAudio.d.ts +6 -0
- package/dist/@dtelecom/src/connectionHelper/checks/publishAudio.d.ts.map +1 -0
- package/dist/@dtelecom/src/connectionHelper/checks/publishVideo.d.ts +6 -0
- package/dist/@dtelecom/src/connectionHelper/checks/publishVideo.d.ts.map +1 -0
- package/dist/@dtelecom/src/connectionHelper/checks/reconnect.d.ts +6 -0
- package/dist/@dtelecom/src/connectionHelper/checks/reconnect.d.ts.map +1 -0
- package/dist/@dtelecom/src/connectionHelper/checks/turn.d.ts +6 -0
- package/dist/@dtelecom/src/connectionHelper/checks/turn.d.ts.map +1 -0
- package/dist/@dtelecom/src/connectionHelper/checks/webrtc.d.ts +6 -0
- package/dist/@dtelecom/src/connectionHelper/checks/webrtc.d.ts.map +1 -0
- package/dist/@dtelecom/src/connectionHelper/checks/websocket.d.ts +6 -0
- package/dist/@dtelecom/src/connectionHelper/checks/websocket.d.ts.map +1 -0
- package/dist/@dtelecom/src/e2ee/E2eeManager.d.ts +47 -0
- package/dist/@dtelecom/src/e2ee/E2eeManager.d.ts.map +1 -0
- package/dist/@dtelecom/src/e2ee/KeyProvider.d.ts +44 -0
- package/dist/@dtelecom/src/e2ee/KeyProvider.d.ts.map +1 -0
- package/dist/@dtelecom/src/e2ee/constants.d.ts +15 -0
- package/dist/@dtelecom/src/e2ee/constants.d.ts.map +1 -0
- package/dist/@dtelecom/src/e2ee/errors.d.ts +11 -0
- package/dist/@dtelecom/src/e2ee/errors.d.ts.map +1 -0
- package/dist/@dtelecom/src/e2ee/index.d.ts +4 -0
- package/dist/@dtelecom/src/e2ee/index.d.ts.map +1 -0
- package/dist/@dtelecom/src/e2ee/types.d.ts +130 -0
- package/dist/@dtelecom/src/e2ee/types.d.ts.map +1 -0
- package/dist/@dtelecom/src/e2ee/utils.d.ts +24 -0
- package/dist/@dtelecom/src/e2ee/utils.d.ts.map +1 -0
- package/dist/@dtelecom/src/e2ee/worker/FrameCryptor.d.ts +176 -0
- package/dist/@dtelecom/src/e2ee/worker/FrameCryptor.d.ts.map +1 -0
- package/dist/@dtelecom/src/e2ee/worker/ParticipantKeyHandler.d.ts +65 -0
- package/dist/@dtelecom/src/e2ee/worker/ParticipantKeyHandler.d.ts.map +1 -0
- package/dist/@dtelecom/src/e2ee/worker/e2ee.worker.d.ts +2 -0
- package/dist/@dtelecom/src/e2ee/worker/e2ee.worker.d.ts.map +1 -0
- package/dist/@dtelecom/src/index.d.ts +38 -0
- package/dist/@dtelecom/src/index.d.ts.map +1 -0
- package/dist/@dtelecom/src/logger.d.ts +29 -0
- package/dist/@dtelecom/src/logger.d.ts.map +1 -0
- package/dist/@dtelecom/src/options.d.ts +104 -0
- package/dist/@dtelecom/src/options.d.ts.map +1 -0
- package/dist/@dtelecom/src/proto/livekit_models_pb.d.ts +1264 -0
- package/dist/@dtelecom/src/proto/livekit_models_pb.d.ts.map +1 -0
- package/dist/@dtelecom/src/proto/livekit_rtc_pb.d.ts +1373 -0
- package/dist/@dtelecom/src/proto/livekit_rtc_pb.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/DefaultReconnectPolicy.d.ts +8 -0
- package/dist/@dtelecom/src/room/DefaultReconnectPolicy.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/DeviceManager.d.ts +9 -0
- package/dist/@dtelecom/src/room/DeviceManager.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/PCTransport.d.ts +41 -0
- package/dist/@dtelecom/src/room/PCTransport.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/RTCEngine.d.ts +143 -0
- package/dist/@dtelecom/src/room/RTCEngine.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/ReconnectPolicy.d.ts +24 -0
- package/dist/@dtelecom/src/room/ReconnectPolicy.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/RegionUrlProvider.d.ts +17 -0
- package/dist/@dtelecom/src/room/RegionUrlProvider.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/Room.d.ts +248 -0
- package/dist/@dtelecom/src/room/Room.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/defaults.d.ts +8 -0
- package/dist/@dtelecom/src/room/defaults.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/errors.d.ts +43 -0
- package/dist/@dtelecom/src/room/errors.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/events.d.ts +477 -0
- package/dist/@dtelecom/src/room/events.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/participant/LocalParticipant.d.ts +177 -0
- package/dist/@dtelecom/src/room/participant/LocalParticipant.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/participant/Participant.d.ts +95 -0
- package/dist/@dtelecom/src/room/participant/Participant.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/participant/ParticipantTrackPermission.d.ts +26 -0
- package/dist/@dtelecom/src/room/participant/ParticipantTrackPermission.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/participant/RemoteParticipant.d.ts +55 -0
- package/dist/@dtelecom/src/room/participant/RemoteParticipant.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/participant/publishUtils.d.ts +28 -0
- package/dist/@dtelecom/src/room/participant/publishUtils.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/stats.d.ts +67 -0
- package/dist/@dtelecom/src/room/stats.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/timers.d.ts +13 -0
- package/dist/@dtelecom/src/room/timers.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/track/LocalAudioTrack.d.ts +25 -0
- package/dist/@dtelecom/src/room/track/LocalAudioTrack.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/track/LocalTrack.d.ts +78 -0
- package/dist/@dtelecom/src/room/track/LocalTrack.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/track/LocalTrackPublication.d.ts +38 -0
- package/dist/@dtelecom/src/room/track/LocalTrackPublication.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/track/LocalVideoTrack.d.ts +54 -0
- package/dist/@dtelecom/src/room/track/LocalVideoTrack.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/track/RemoteAudioTrack.d.ts +53 -0
- package/dist/@dtelecom/src/room/track/RemoteAudioTrack.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/track/RemoteTrack.d.ts +15 -0
- package/dist/@dtelecom/src/room/track/RemoteTrack.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/track/RemoteTrackPublication.d.ts +64 -0
- package/dist/@dtelecom/src/room/track/RemoteTrackPublication.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/track/RemoteVideoTrack.d.ts +56 -0
- package/dist/@dtelecom/src/room/track/RemoteVideoTrack.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/track/Track.d.ts +125 -0
- package/dist/@dtelecom/src/room/track/Track.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/track/TrackPublication.d.ts +72 -0
- package/dist/@dtelecom/src/room/track/TrackPublication.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/track/create.d.ts +24 -0
- package/dist/@dtelecom/src/room/track/create.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/track/facingMode.d.ts +41 -0
- package/dist/@dtelecom/src/room/track/facingMode.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/track/options.d.ts +272 -0
- package/dist/@dtelecom/src/room/track/options.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/track/processor/types.d.ts +19 -0
- package/dist/@dtelecom/src/room/track/processor/types.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/track/types.d.ts +24 -0
- package/dist/@dtelecom/src/room/track/types.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/track/utils.d.ts +27 -0
- package/dist/@dtelecom/src/room/track/utils.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/types.d.ts +26 -0
- package/dist/@dtelecom/src/room/types.d.ts.map +1 -0
- package/dist/@dtelecom/src/room/utils.d.ts +92 -0
- package/dist/@dtelecom/src/room/utils.d.ts.map +1 -0
- package/dist/@dtelecom/src/test/MockMediaStreamTrack.d.ts +26 -0
- package/dist/@dtelecom/src/test/MockMediaStreamTrack.d.ts.map +1 -0
- package/dist/@dtelecom/src/test/mocks.d.ts +12 -0
- package/dist/@dtelecom/src/test/mocks.d.ts.map +1 -0
- package/dist/@dtelecom/src/utils/AsyncQueue.d.ts +23 -0
- package/dist/@dtelecom/src/utils/AsyncQueue.d.ts.map +1 -0
- package/dist/@dtelecom/src/utils/browserParser.d.ts +12 -0
- package/dist/@dtelecom/src/utils/browserParser.d.ts.map +1 -0
- package/dist/@dtelecom/src/version.d.ts +3 -0
- package/dist/@dtelecom/src/version.d.ts.map +1 -0
- package/dist/ts4.2/@dtelecom/src/api/SignalClient.d.ts +105 -0
- package/dist/ts4.2/@dtelecom/src/connectionHelper/ConnectionCheck.d.ts +26 -0
- package/dist/ts4.2/@dtelecom/src/connectionHelper/checks/Checker.d.ts +59 -0
- package/dist/ts4.2/@dtelecom/src/connectionHelper/checks/publishAudio.d.ts +6 -0
- package/dist/ts4.2/@dtelecom/src/connectionHelper/checks/publishVideo.d.ts +6 -0
- package/dist/ts4.2/@dtelecom/src/connectionHelper/checks/reconnect.d.ts +6 -0
- package/dist/ts4.2/@dtelecom/src/connectionHelper/checks/turn.d.ts +6 -0
- package/dist/ts4.2/@dtelecom/src/connectionHelper/checks/webrtc.d.ts +6 -0
- package/dist/ts4.2/@dtelecom/src/connectionHelper/checks/websocket.d.ts +6 -0
- package/dist/ts4.2/@dtelecom/src/e2ee/E2eeManager.d.ts +47 -0
- package/dist/ts4.2/@dtelecom/src/e2ee/KeyProvider.d.ts +44 -0
- package/dist/ts4.2/@dtelecom/src/e2ee/constants.d.ts +15 -0
- package/dist/ts4.2/@dtelecom/src/e2ee/errors.d.ts +11 -0
- package/dist/ts4.2/@dtelecom/src/e2ee/index.d.ts +4 -0
- package/dist/ts4.2/@dtelecom/src/e2ee/types.d.ts +130 -0
- package/dist/ts4.2/@dtelecom/src/e2ee/utils.d.ts +24 -0
- package/dist/ts4.2/@dtelecom/src/e2ee/worker/FrameCryptor.d.ts +176 -0
- package/dist/ts4.2/@dtelecom/src/e2ee/worker/ParticipantKeyHandler.d.ts +65 -0
- package/dist/ts4.2/@dtelecom/src/e2ee/worker/e2ee.worker.d.ts +2 -0
- package/dist/ts4.2/@dtelecom/src/index.d.ts +38 -0
- package/dist/ts4.2/@dtelecom/src/logger.d.ts +29 -0
- package/dist/ts4.2/@dtelecom/src/options.d.ts +104 -0
- package/dist/ts4.2/@dtelecom/src/proto/livekit_models_pb.d.ts +1264 -0
- package/dist/ts4.2/@dtelecom/src/proto/livekit_rtc_pb.d.ts +1373 -0
- package/dist/ts4.2/@dtelecom/src/room/DefaultReconnectPolicy.d.ts +8 -0
- package/dist/ts4.2/@dtelecom/src/room/DeviceManager.d.ts +9 -0
- package/dist/ts4.2/@dtelecom/src/room/PCTransport.d.ts +43 -0
- package/dist/ts4.2/@dtelecom/src/room/RTCEngine.d.ts +143 -0
- package/dist/ts4.2/@dtelecom/src/room/ReconnectPolicy.d.ts +24 -0
- package/dist/ts4.2/@dtelecom/src/room/RegionUrlProvider.d.ts +17 -0
- package/dist/ts4.2/@dtelecom/src/room/Room.d.ts +248 -0
- package/dist/ts4.2/@dtelecom/src/room/defaults.d.ts +8 -0
- package/dist/ts4.2/@dtelecom/src/room/errors.d.ts +43 -0
- package/dist/ts4.2/@dtelecom/src/room/events.d.ts +477 -0
- package/dist/ts4.2/@dtelecom/src/room/participant/LocalParticipant.d.ts +177 -0
- package/dist/ts4.2/@dtelecom/src/room/participant/Participant.d.ts +95 -0
- package/dist/ts4.2/@dtelecom/src/room/participant/ParticipantTrackPermission.d.ts +26 -0
- package/dist/ts4.2/@dtelecom/src/room/participant/RemoteParticipant.d.ts +55 -0
- package/dist/ts4.2/@dtelecom/src/room/participant/publishUtils.d.ts +28 -0
- package/dist/ts4.2/@dtelecom/src/room/stats.d.ts +67 -0
- package/dist/ts4.2/@dtelecom/src/room/timers.d.ts +13 -0
- package/dist/ts4.2/@dtelecom/src/room/track/LocalAudioTrack.d.ts +25 -0
- package/dist/ts4.2/@dtelecom/src/room/track/LocalTrack.d.ts +78 -0
- package/dist/ts4.2/@dtelecom/src/room/track/LocalTrackPublication.d.ts +38 -0
- package/dist/ts4.2/@dtelecom/src/room/track/LocalVideoTrack.d.ts +54 -0
- package/dist/ts4.2/@dtelecom/src/room/track/RemoteAudioTrack.d.ts +53 -0
- package/dist/ts4.2/@dtelecom/src/room/track/RemoteTrack.d.ts +15 -0
- package/dist/ts4.2/@dtelecom/src/room/track/RemoteTrackPublication.d.ts +64 -0
- package/dist/ts4.2/@dtelecom/src/room/track/RemoteVideoTrack.d.ts +56 -0
- package/dist/ts4.2/@dtelecom/src/room/track/Track.d.ts +125 -0
- package/dist/ts4.2/@dtelecom/src/room/track/TrackPublication.d.ts +72 -0
- package/dist/ts4.2/@dtelecom/src/room/track/create.d.ts +24 -0
- package/dist/ts4.2/@dtelecom/src/room/track/facingMode.d.ts +41 -0
- package/dist/ts4.2/@dtelecom/src/room/track/options.d.ts +280 -0
- package/dist/ts4.2/@dtelecom/src/room/track/processor/types.d.ts +19 -0
- package/dist/ts4.2/@dtelecom/src/room/track/types.d.ts +24 -0
- package/dist/ts4.2/@dtelecom/src/room/track/utils.d.ts +27 -0
- package/dist/ts4.2/@dtelecom/src/room/types.d.ts +26 -0
- package/dist/ts4.2/@dtelecom/src/room/utils.d.ts +92 -0
- package/dist/ts4.2/@dtelecom/src/test/MockMediaStreamTrack.d.ts +26 -0
- package/dist/ts4.2/@dtelecom/src/test/mocks.d.ts +12 -0
- package/dist/ts4.2/@dtelecom/src/utils/AsyncQueue.d.ts +23 -0
- package/dist/ts4.2/@dtelecom/src/utils/browserParser.d.ts +12 -0
- package/dist/ts4.2/@dtelecom/src/version.d.ts +3 -0
- package/package.json +102 -0
- package/src/api/SignalClient.ts +790 -0
- package/src/connectionHelper/ConnectionCheck.ts +90 -0
- package/src/connectionHelper/checks/Checker.ts +163 -0
- package/src/connectionHelper/checks/publishAudio.ts +33 -0
- package/src/connectionHelper/checks/publishVideo.ts +33 -0
- package/src/connectionHelper/checks/reconnect.ts +45 -0
- package/src/connectionHelper/checks/turn.ts +54 -0
- package/src/connectionHelper/checks/webrtc.ts +82 -0
- package/src/connectionHelper/checks/websocket.ts +27 -0
- package/src/e2ee/E2eeManager.ts +375 -0
- package/src/e2ee/KeyProvider.ts +78 -0
- package/src/e2ee/constants.ts +44 -0
- package/src/e2ee/errors.ts +16 -0
- package/src/e2ee/index.ts +3 -0
- package/src/e2ee/types.ts +161 -0
- package/src/e2ee/utils.ts +127 -0
- package/src/e2ee/worker/FrameCryptor.test.ts +19 -0
- package/src/e2ee/worker/FrameCryptor.ts +612 -0
- package/src/e2ee/worker/ParticipantKeyHandler.ts +175 -0
- package/src/e2ee/worker/e2ee.worker.ts +223 -0
- package/src/e2ee/worker/tsconfig.json +6 -0
- package/src/index.ts +85 -0
- package/src/logger.ts +64 -0
- package/src/options.ts +127 -0
- package/src/proto/livekit_models_pb.ts +2096 -0
- package/src/proto/livekit_rtc_pb.ts +2332 -0
- package/src/room/DefaultReconnectPolicy.ts +35 -0
- package/src/room/DeviceManager.ts +88 -0
- package/src/room/PCTransport.ts +452 -0
- package/src/room/RTCEngine.ts +1421 -0
- package/src/room/ReconnectPolicy.ts +27 -0
- package/src/room/RegionUrlProvider.ts +82 -0
- package/src/room/Room.test.ts +30 -0
- package/src/room/Room.ts +1907 -0
- package/src/room/defaults.ts +49 -0
- package/src/room/errors.ts +90 -0
- package/src/room/events.ts +535 -0
- package/src/room/participant/LocalParticipant.ts +1350 -0
- package/src/room/participant/Participant.ts +299 -0
- package/src/room/participant/ParticipantTrackPermission.ts +42 -0
- package/src/room/participant/RemoteParticipant.ts +359 -0
- package/src/room/participant/publishUtils.test.ts +189 -0
- package/src/room/participant/publishUtils.ts +399 -0
- package/src/room/stats.ts +136 -0
- package/src/room/timers.ts +16 -0
- package/src/room/track/LocalAudioTrack.ts +171 -0
- package/src/room/track/LocalTrack.ts +430 -0
- package/src/room/track/LocalTrackPublication.ts +87 -0
- package/src/room/track/LocalVideoTrack.test.ts +73 -0
- package/src/room/track/LocalVideoTrack.ts +513 -0
- package/src/room/track/RemoteAudioTrack.ts +248 -0
- package/src/room/track/RemoteTrack.ts +62 -0
- package/src/room/track/RemoteTrackPublication.ts +311 -0
- package/src/room/track/RemoteVideoTrack.test.ts +152 -0
- package/src/room/track/RemoteVideoTrack.ts +410 -0
- package/src/room/track/Track.ts +452 -0
- package/src/room/track/TrackPublication.ts +158 -0
- package/src/room/track/create.ts +143 -0
- package/src/room/track/facingMode.test.ts +31 -0
- package/src/room/track/facingMode.ts +103 -0
- package/src/room/track/options.ts +362 -0
- package/src/room/track/processor/types.ts +20 -0
- package/src/room/track/types.ts +25 -0
- package/src/room/track/utils.test.ts +111 -0
- package/src/room/track/utils.ts +183 -0
- package/src/room/types.ts +39 -0
- package/src/room/utils.test.ts +16 -0
- package/src/room/utils.ts +501 -0
- package/src/room/worker.d.ts +4 -0
- package/src/test/MockMediaStreamTrack.ts +84 -0
- package/src/test/mocks.ts +19 -0
- package/src/utils/AsyncQueue.test.ts +100 -0
- package/src/utils/AsyncQueue.ts +57 -0
- package/src/utils/browserParser.test.ts +88 -0
- package/src/utils/browserParser.ts +74 -0
- package/src/version.ts +4 -0
|
@@ -0,0 +1,612 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
+
// TODO code inspired by https://github.com/webrtc/samples/blob/gh-pages/src/content/insertable-streams/endtoend-encryption/js/worker.js
|
|
3
|
+
import { EventEmitter } from 'events';
|
|
4
|
+
import type TypedEventEmitter from 'typed-emitter';
|
|
5
|
+
import { workerLogger } from '../../logger';
|
|
6
|
+
import type { VideoCodec } from '../../room/track/options';
|
|
7
|
+
import { ENCRYPTION_ALGORITHM, IV_LENGTH, UNENCRYPTED_BYTES } from '../constants';
|
|
8
|
+
import { CryptorError, CryptorErrorReason } from '../errors';
|
|
9
|
+
import {
|
|
10
|
+
CryptorCallbacks,
|
|
11
|
+
CryptorEvent,
|
|
12
|
+
DecodeRatchetOptions,
|
|
13
|
+
KeyProviderOptions,
|
|
14
|
+
KeySet,
|
|
15
|
+
} from '../types';
|
|
16
|
+
import { deriveKeys, isVideoFrame } from '../utils';
|
|
17
|
+
import type { ParticipantKeyHandler } from './ParticipantKeyHandler';
|
|
18
|
+
|
|
19
|
+
export interface FrameCryptorConstructor {
|
|
20
|
+
new (opts?: unknown): BaseFrameCryptor;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface TransformerInfo {
|
|
24
|
+
readable: ReadableStream;
|
|
25
|
+
writable: WritableStream;
|
|
26
|
+
transformer: TransformStream;
|
|
27
|
+
abortController: AbortController;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export class BaseFrameCryptor extends (EventEmitter as new () => TypedEventEmitter<CryptorCallbacks>) {
|
|
31
|
+
encodeFunction(
|
|
32
|
+
encodedFrame: RTCEncodedVideoFrame | RTCEncodedAudioFrame,
|
|
33
|
+
controller: TransformStreamDefaultController,
|
|
34
|
+
): Promise<any> {
|
|
35
|
+
throw Error('not implemented for subclass');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
decodeFunction(
|
|
39
|
+
encodedFrame: RTCEncodedVideoFrame | RTCEncodedAudioFrame,
|
|
40
|
+
controller: TransformStreamDefaultController,
|
|
41
|
+
): Promise<any> {
|
|
42
|
+
throw Error('not implemented for subclass');
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Cryptor is responsible for en-/decrypting media frames.
|
|
48
|
+
* Each Cryptor instance is responsible for en-/decrypting a single mediaStreamTrack.
|
|
49
|
+
*/
|
|
50
|
+
export class FrameCryptor extends BaseFrameCryptor {
|
|
51
|
+
private sendCounts: Map<number, number>;
|
|
52
|
+
|
|
53
|
+
private participantId: string | undefined;
|
|
54
|
+
|
|
55
|
+
private trackId: string | undefined;
|
|
56
|
+
|
|
57
|
+
private keys: ParticipantKeyHandler;
|
|
58
|
+
|
|
59
|
+
private videoCodec?: VideoCodec;
|
|
60
|
+
|
|
61
|
+
private rtpMap: Map<number, VideoCodec>;
|
|
62
|
+
|
|
63
|
+
private keyProviderOptions: KeyProviderOptions;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* used for detecting server injected unencrypted frames
|
|
67
|
+
*/
|
|
68
|
+
private unencryptedFrameByteTrailer: Uint8Array;
|
|
69
|
+
|
|
70
|
+
constructor(opts: {
|
|
71
|
+
keys: ParticipantKeyHandler;
|
|
72
|
+
participantId: string;
|
|
73
|
+
keyProviderOptions: KeyProviderOptions;
|
|
74
|
+
unencryptedFrameBytes?: Uint8Array;
|
|
75
|
+
}) {
|
|
76
|
+
super();
|
|
77
|
+
this.sendCounts = new Map();
|
|
78
|
+
this.keys = opts.keys;
|
|
79
|
+
this.participantId = opts.participantId;
|
|
80
|
+
this.rtpMap = new Map();
|
|
81
|
+
this.keyProviderOptions = opts.keyProviderOptions;
|
|
82
|
+
this.unencryptedFrameByteTrailer =
|
|
83
|
+
opts.unencryptedFrameBytes ?? new TextEncoder().encode('LKROCKS');
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Assign a different participant to the cryptor.
|
|
88
|
+
* useful for transceiver re-use
|
|
89
|
+
* @param id
|
|
90
|
+
* @param keys
|
|
91
|
+
*/
|
|
92
|
+
setParticipant(id: string, keys: ParticipantKeyHandler) {
|
|
93
|
+
this.participantId = id;
|
|
94
|
+
this.keys = keys;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
unsetParticipant() {
|
|
98
|
+
this.participantId = undefined;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
getParticipantId() {
|
|
102
|
+
return this.participantId;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
getTrackId() {
|
|
106
|
+
return this.trackId;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Update the video codec used by the mediaStreamTrack
|
|
111
|
+
* @param codec
|
|
112
|
+
*/
|
|
113
|
+
setVideoCodec(codec: VideoCodec) {
|
|
114
|
+
this.videoCodec = codec;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* rtp payload type map used for figuring out codec of payload type when encoding
|
|
119
|
+
* @param map
|
|
120
|
+
*/
|
|
121
|
+
setRtpMap(map: Map<number, VideoCodec>) {
|
|
122
|
+
this.rtpMap = map;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
setupTransform(
|
|
126
|
+
operation: 'encode' | 'decode',
|
|
127
|
+
readable: ReadableStream,
|
|
128
|
+
writable: WritableStream,
|
|
129
|
+
trackId: string,
|
|
130
|
+
codec?: VideoCodec,
|
|
131
|
+
) {
|
|
132
|
+
if (codec) {
|
|
133
|
+
console.info('setting codec on cryptor to', codec);
|
|
134
|
+
this.videoCodec = codec;
|
|
135
|
+
}
|
|
136
|
+
const transformFn = operation === 'encode' ? this.encodeFunction : this.decodeFunction;
|
|
137
|
+
const transformStream = new TransformStream({
|
|
138
|
+
transform: transformFn.bind(this),
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
readable
|
|
142
|
+
.pipeThrough(transformStream)
|
|
143
|
+
.pipeTo(writable)
|
|
144
|
+
.catch((e) => {
|
|
145
|
+
console.error(e);
|
|
146
|
+
this.emit('cryptorError', e instanceof CryptorError ? e : new CryptorError(e.message));
|
|
147
|
+
});
|
|
148
|
+
this.trackId = trackId;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Function that will be injected in a stream and will encrypt the given encoded frames.
|
|
153
|
+
*
|
|
154
|
+
* @param {RTCEncodedVideoFrame|RTCEncodedAudioFrame} encodedFrame - Encoded video frame.
|
|
155
|
+
* @param {TransformStreamDefaultController} controller - TransportStreamController.
|
|
156
|
+
*
|
|
157
|
+
* The VP8 payload descriptor described in
|
|
158
|
+
* https://tools.ietf.org/html/rfc7741#section-4.2
|
|
159
|
+
* is part of the RTP packet and not part of the frame and is not controllable by us.
|
|
160
|
+
* This is fine as the SFU keeps having access to it for routing.
|
|
161
|
+
*
|
|
162
|
+
* The encrypted frame is formed as follows:
|
|
163
|
+
* 1) Find unencrypted byte length, depending on the codec, frame type and kind.
|
|
164
|
+
* 2) Form the GCM IV for the frame as described above.
|
|
165
|
+
* 3) Encrypt the rest of the frame using AES-GCM.
|
|
166
|
+
* 4) Allocate space for the encrypted frame.
|
|
167
|
+
* 5) Copy the unencrypted bytes to the start of the encrypted frame.
|
|
168
|
+
* 6) Append the ciphertext to the encrypted frame.
|
|
169
|
+
* 7) Append the IV.
|
|
170
|
+
* 8) Append a single byte for the key identifier.
|
|
171
|
+
* 9) Enqueue the encrypted frame for sending.
|
|
172
|
+
*/
|
|
173
|
+
async encodeFunction(
|
|
174
|
+
encodedFrame: RTCEncodedVideoFrame | RTCEncodedAudioFrame,
|
|
175
|
+
controller: TransformStreamDefaultController,
|
|
176
|
+
) {
|
|
177
|
+
if (
|
|
178
|
+
!this.keys.isEnabled() ||
|
|
179
|
+
// skip for encryption for empty dtx frames
|
|
180
|
+
encodedFrame.data.byteLength === 0
|
|
181
|
+
) {
|
|
182
|
+
return controller.enqueue(encodedFrame);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const { encryptionKey } = this.keys.getKeySet();
|
|
186
|
+
const keyIndex = this.keys.getCurrentKeyIndex();
|
|
187
|
+
|
|
188
|
+
if (encryptionKey) {
|
|
189
|
+
const iv = this.makeIV(
|
|
190
|
+
encodedFrame.getMetadata().synchronizationSource ?? -1,
|
|
191
|
+
encodedFrame.timestamp,
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
// Thіs is not encrypted and contains the VP8 payload descriptor or the Opus TOC byte.
|
|
195
|
+
const frameHeader = new Uint8Array(
|
|
196
|
+
encodedFrame.data,
|
|
197
|
+
0,
|
|
198
|
+
this.getUnencryptedBytes(encodedFrame),
|
|
199
|
+
);
|
|
200
|
+
|
|
201
|
+
// Frame trailer contains the R|IV_LENGTH and key index
|
|
202
|
+
const frameTrailer = new Uint8Array(2);
|
|
203
|
+
|
|
204
|
+
frameTrailer[0] = IV_LENGTH;
|
|
205
|
+
frameTrailer[1] = keyIndex;
|
|
206
|
+
|
|
207
|
+
// Construct frame trailer. Similar to the frame header described in
|
|
208
|
+
// https://tools.ietf.org/html/draft-omara-sframe-00#section-4.2
|
|
209
|
+
// but we put it at the end.
|
|
210
|
+
//
|
|
211
|
+
// ---------+-------------------------+-+---------+----
|
|
212
|
+
// payload |IV...(length = IV_LENGTH)|R|IV_LENGTH|KID |
|
|
213
|
+
// ---------+-------------------------+-+---------+----
|
|
214
|
+
try {
|
|
215
|
+
const cipherText = await crypto.subtle.encrypt(
|
|
216
|
+
{
|
|
217
|
+
name: ENCRYPTION_ALGORITHM,
|
|
218
|
+
iv,
|
|
219
|
+
additionalData: new Uint8Array(encodedFrame.data, 0, frameHeader.byteLength),
|
|
220
|
+
},
|
|
221
|
+
encryptionKey,
|
|
222
|
+
new Uint8Array(encodedFrame.data, this.getUnencryptedBytes(encodedFrame)),
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
const newData = new ArrayBuffer(
|
|
226
|
+
frameHeader.byteLength + cipherText.byteLength + iv.byteLength + frameTrailer.byteLength,
|
|
227
|
+
);
|
|
228
|
+
const newUint8 = new Uint8Array(newData);
|
|
229
|
+
|
|
230
|
+
newUint8.set(frameHeader); // copy first bytes.
|
|
231
|
+
newUint8.set(new Uint8Array(cipherText), frameHeader.byteLength); // add ciphertext.
|
|
232
|
+
newUint8.set(new Uint8Array(iv), frameHeader.byteLength + cipherText.byteLength); // append IV.
|
|
233
|
+
newUint8.set(frameTrailer, frameHeader.byteLength + cipherText.byteLength + iv.byteLength); // append frame trailer.
|
|
234
|
+
|
|
235
|
+
encodedFrame.data = newData;
|
|
236
|
+
|
|
237
|
+
return controller.enqueue(encodedFrame);
|
|
238
|
+
} catch (e: any) {
|
|
239
|
+
// TODO: surface this to the app.
|
|
240
|
+
workerLogger.error(e);
|
|
241
|
+
}
|
|
242
|
+
} else {
|
|
243
|
+
this.emit(
|
|
244
|
+
CryptorEvent.Error,
|
|
245
|
+
new CryptorError(`encryption key missing for encoding`, CryptorErrorReason.MissingKey),
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Function that will be injected in a stream and will decrypt the given encoded frames.
|
|
252
|
+
*
|
|
253
|
+
* @param {RTCEncodedVideoFrame|RTCEncodedAudioFrame} encodedFrame - Encoded video frame.
|
|
254
|
+
* @param {TransformStreamDefaultController} controller - TransportStreamController.
|
|
255
|
+
*/
|
|
256
|
+
async decodeFunction(
|
|
257
|
+
encodedFrame: RTCEncodedVideoFrame | RTCEncodedAudioFrame,
|
|
258
|
+
controller: TransformStreamDefaultController,
|
|
259
|
+
) {
|
|
260
|
+
if (
|
|
261
|
+
!this.keys.isEnabled() ||
|
|
262
|
+
// skip for decryption for empty dtx frames
|
|
263
|
+
encodedFrame.data.byteLength === 0 ||
|
|
264
|
+
// skip decryption if frame is server injected
|
|
265
|
+
isFrameServerInjected(encodedFrame.data, this.unencryptedFrameByteTrailer)
|
|
266
|
+
) {
|
|
267
|
+
return controller.enqueue(encodedFrame);
|
|
268
|
+
}
|
|
269
|
+
const data = new Uint8Array(encodedFrame.data);
|
|
270
|
+
const keyIndex = data[encodedFrame.data.byteLength - 1];
|
|
271
|
+
|
|
272
|
+
if (this.keys.getKeySet(keyIndex) && this.keys.hasValidKey) {
|
|
273
|
+
try {
|
|
274
|
+
const decodedFrame = await this.decryptFrame(encodedFrame, keyIndex);
|
|
275
|
+
this.keys.decryptionSuccess();
|
|
276
|
+
if (decodedFrame) {
|
|
277
|
+
return controller.enqueue(decodedFrame);
|
|
278
|
+
}
|
|
279
|
+
} catch (error) {
|
|
280
|
+
if (error instanceof CryptorError && error.reason === CryptorErrorReason.InvalidKey) {
|
|
281
|
+
if (this.keys.hasValidKey) {
|
|
282
|
+
workerLogger.warn('invalid key');
|
|
283
|
+
this.emit(
|
|
284
|
+
CryptorEvent.Error,
|
|
285
|
+
new CryptorError(
|
|
286
|
+
`invalid key for participant ${this.participantId}`,
|
|
287
|
+
CryptorErrorReason.InvalidKey,
|
|
288
|
+
),
|
|
289
|
+
);
|
|
290
|
+
this.keys.decryptionFailure();
|
|
291
|
+
}
|
|
292
|
+
} else {
|
|
293
|
+
workerLogger.warn('decoding frame failed', { error });
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
return controller.enqueue(encodedFrame);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Function that will decrypt the given encoded frame. If the decryption fails, it will
|
|
303
|
+
* ratchet the key for up to RATCHET_WINDOW_SIZE times.
|
|
304
|
+
*/
|
|
305
|
+
async decryptFrame(
|
|
306
|
+
encodedFrame: RTCEncodedVideoFrame | RTCEncodedAudioFrame,
|
|
307
|
+
keyIndex: number,
|
|
308
|
+
initialMaterial: KeySet | undefined = undefined,
|
|
309
|
+
ratchetOpts: DecodeRatchetOptions = { ratchetCount: 0 },
|
|
310
|
+
): Promise<RTCEncodedVideoFrame | RTCEncodedAudioFrame | undefined> {
|
|
311
|
+
const keySet = this.keys.getKeySet(keyIndex);
|
|
312
|
+
|
|
313
|
+
// Construct frame trailer. Similar to the frame header described in
|
|
314
|
+
// https://tools.ietf.org/html/draft-omara-sframe-00#section-4.2
|
|
315
|
+
// but we put it at the end.
|
|
316
|
+
//
|
|
317
|
+
// ---------+-------------------------+-+---------+----
|
|
318
|
+
// payload |IV...(length = IV_LENGTH)|R|IV_LENGTH|KID |
|
|
319
|
+
// ---------+-------------------------+-+---------+----
|
|
320
|
+
|
|
321
|
+
try {
|
|
322
|
+
const frameHeader = new Uint8Array(
|
|
323
|
+
encodedFrame.data,
|
|
324
|
+
0,
|
|
325
|
+
this.getUnencryptedBytes(encodedFrame),
|
|
326
|
+
);
|
|
327
|
+
const frameTrailer = new Uint8Array(encodedFrame.data, encodedFrame.data.byteLength - 2, 2);
|
|
328
|
+
|
|
329
|
+
const ivLength = frameTrailer[0];
|
|
330
|
+
const iv = new Uint8Array(
|
|
331
|
+
encodedFrame.data,
|
|
332
|
+
encodedFrame.data.byteLength - ivLength - frameTrailer.byteLength,
|
|
333
|
+
ivLength,
|
|
334
|
+
);
|
|
335
|
+
|
|
336
|
+
const cipherTextStart = frameHeader.byteLength;
|
|
337
|
+
const cipherTextLength =
|
|
338
|
+
encodedFrame.data.byteLength -
|
|
339
|
+
(frameHeader.byteLength + ivLength + frameTrailer.byteLength);
|
|
340
|
+
|
|
341
|
+
const plainText = await crypto.subtle.decrypt(
|
|
342
|
+
{
|
|
343
|
+
name: ENCRYPTION_ALGORITHM,
|
|
344
|
+
iv,
|
|
345
|
+
additionalData: new Uint8Array(encodedFrame.data, 0, frameHeader.byteLength),
|
|
346
|
+
},
|
|
347
|
+
ratchetOpts.encryptionKey ?? keySet.encryptionKey,
|
|
348
|
+
new Uint8Array(encodedFrame.data, cipherTextStart, cipherTextLength),
|
|
349
|
+
);
|
|
350
|
+
|
|
351
|
+
const newData = new ArrayBuffer(frameHeader.byteLength + plainText.byteLength);
|
|
352
|
+
const newUint8 = new Uint8Array(newData);
|
|
353
|
+
|
|
354
|
+
newUint8.set(new Uint8Array(encodedFrame.data, 0, frameHeader.byteLength));
|
|
355
|
+
newUint8.set(new Uint8Array(plainText), frameHeader.byteLength);
|
|
356
|
+
|
|
357
|
+
encodedFrame.data = newData;
|
|
358
|
+
|
|
359
|
+
return encodedFrame;
|
|
360
|
+
} catch (error: any) {
|
|
361
|
+
if (this.keyProviderOptions.ratchetWindowSize > 0) {
|
|
362
|
+
if (ratchetOpts.ratchetCount < this.keyProviderOptions.ratchetWindowSize) {
|
|
363
|
+
workerLogger.debug(
|
|
364
|
+
`ratcheting key attempt ${ratchetOpts.ratchetCount} of ${
|
|
365
|
+
this.keyProviderOptions.ratchetWindowSize
|
|
366
|
+
}, for kind ${encodedFrame instanceof RTCEncodedAudioFrame ? 'audio' : 'video'}`,
|
|
367
|
+
);
|
|
368
|
+
|
|
369
|
+
let ratchetedKeySet: KeySet | undefined;
|
|
370
|
+
if (keySet === this.keys.getKeySet(keyIndex)) {
|
|
371
|
+
// only ratchet if the currently set key is still the same as the one used to decrypt this frame
|
|
372
|
+
// if not, it might be that a different frame has already ratcheted and we try with that one first
|
|
373
|
+
const newMaterial = await this.keys.ratchetKey(keyIndex, false);
|
|
374
|
+
|
|
375
|
+
ratchetedKeySet = await deriveKeys(newMaterial, this.keyProviderOptions.ratchetSalt);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
const frame = await this.decryptFrame(encodedFrame, keyIndex, initialMaterial || keySet, {
|
|
379
|
+
ratchetCount: ratchetOpts.ratchetCount + 1,
|
|
380
|
+
encryptionKey: ratchetedKeySet?.encryptionKey,
|
|
381
|
+
});
|
|
382
|
+
if (frame && ratchetedKeySet) {
|
|
383
|
+
this.keys.setKeySet(ratchetedKeySet, keyIndex, true);
|
|
384
|
+
// decryption was successful, set the new key index to reflect the ratcheted key set
|
|
385
|
+
this.keys.setCurrentKeyIndex(keyIndex);
|
|
386
|
+
}
|
|
387
|
+
return frame;
|
|
388
|
+
} else {
|
|
389
|
+
/**
|
|
390
|
+
* Since the key it is first send and only afterwards actually used for encrypting, there were
|
|
391
|
+
* situations when the decrypting failed due to the fact that the received frame was not encrypted
|
|
392
|
+
* yet and ratcheting, of course, did not solve the problem. So if we fail RATCHET_WINDOW_SIZE times,
|
|
393
|
+
* we come back to the initial key.
|
|
394
|
+
*/
|
|
395
|
+
if (initialMaterial) {
|
|
396
|
+
workerLogger.debug('resetting to initial material');
|
|
397
|
+
this.keys.setKeyFromMaterial(initialMaterial.material, keyIndex);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
workerLogger.warn('maximum ratchet attempts exceeded, resetting key');
|
|
401
|
+
this.emit(
|
|
402
|
+
CryptorEvent.Error,
|
|
403
|
+
new CryptorError(
|
|
404
|
+
`valid key missing for participant ${this.participantId}`,
|
|
405
|
+
CryptorErrorReason.MissingKey,
|
|
406
|
+
),
|
|
407
|
+
);
|
|
408
|
+
}
|
|
409
|
+
} else {
|
|
410
|
+
throw new CryptorError(
|
|
411
|
+
'Decryption failed, most likely because of an invalid key',
|
|
412
|
+
CryptorErrorReason.InvalidKey,
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* Construct the IV used for AES-GCM and sent (in plain) with the packet similar to
|
|
420
|
+
* https://tools.ietf.org/html/rfc7714#section-8.1
|
|
421
|
+
* It concatenates
|
|
422
|
+
* - the 32 bit synchronization source (SSRC) given on the encoded frame,
|
|
423
|
+
* - the 32 bit rtp timestamp given on the encoded frame,
|
|
424
|
+
* - a send counter that is specific to the SSRC. Starts at a random number.
|
|
425
|
+
* The send counter is essentially the pictureId but we currently have to implement this ourselves.
|
|
426
|
+
* There is no XOR with a salt. Note that this IV leaks the SSRC to the receiver but since this is
|
|
427
|
+
* randomly generated and SFUs may not rewrite this is considered acceptable.
|
|
428
|
+
* The SSRC is used to allow demultiplexing multiple streams with the same key, as described in
|
|
429
|
+
* https://tools.ietf.org/html/rfc3711#section-4.1.1
|
|
430
|
+
* The RTP timestamp is 32 bits and advances by the codec clock rate (90khz for video, 48khz for
|
|
431
|
+
* opus audio) every second. For video it rolls over roughly every 13 hours.
|
|
432
|
+
* The send counter will advance at the frame rate (30fps for video, 50fps for 20ms opus audio)
|
|
433
|
+
* every second. It will take a long time to roll over.
|
|
434
|
+
*
|
|
435
|
+
* See also https://developer.mozilla.org/en-US/docs/Web/API/AesGcmParams
|
|
436
|
+
*/
|
|
437
|
+
private makeIV(synchronizationSource: number, timestamp: number) {
|
|
438
|
+
const iv = new ArrayBuffer(IV_LENGTH);
|
|
439
|
+
const ivView = new DataView(iv);
|
|
440
|
+
|
|
441
|
+
// having to keep our own send count (similar to a picture id) is not ideal.
|
|
442
|
+
if (!this.sendCounts.has(synchronizationSource)) {
|
|
443
|
+
// Initialize with a random offset, similar to the RTP sequence number.
|
|
444
|
+
this.sendCounts.set(synchronizationSource, Math.floor(Math.random() * 0xffff));
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
const sendCount = this.sendCounts.get(synchronizationSource) ?? 0;
|
|
448
|
+
|
|
449
|
+
ivView.setUint32(0, synchronizationSource);
|
|
450
|
+
ivView.setUint32(4, timestamp);
|
|
451
|
+
ivView.setUint32(8, timestamp - (sendCount % 0xffff));
|
|
452
|
+
|
|
453
|
+
this.sendCounts.set(synchronizationSource, sendCount + 1);
|
|
454
|
+
|
|
455
|
+
return iv;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
getUnencryptedBytes(frame: RTCEncodedVideoFrame | RTCEncodedAudioFrame): number {
|
|
459
|
+
if (isVideoFrame(frame)) {
|
|
460
|
+
let detectedCodec = this.getVideoCodec(frame) ?? this.videoCodec;
|
|
461
|
+
|
|
462
|
+
if (detectedCodec === 'av1' || detectedCodec === 'vp9') {
|
|
463
|
+
throw new Error(`${detectedCodec} is not yet supported for end to end encryption`);
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
if (detectedCodec === 'vp8') {
|
|
467
|
+
return UNENCRYPTED_BYTES[frame.type];
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
const data = new Uint8Array(frame.data);
|
|
471
|
+
try {
|
|
472
|
+
const naluIndices = findNALUIndices(data);
|
|
473
|
+
|
|
474
|
+
// if the detected codec is undefined we test whether it _looks_ like a h264 frame as a best guess
|
|
475
|
+
const isH264 =
|
|
476
|
+
detectedCodec === 'h264' ||
|
|
477
|
+
naluIndices.some((naluIndex) =>
|
|
478
|
+
[NALUType.SLICE_IDR, NALUType.SLICE_NON_IDR].includes(parseNALUType(data[naluIndex])),
|
|
479
|
+
);
|
|
480
|
+
|
|
481
|
+
if (isH264) {
|
|
482
|
+
for (const index of naluIndices) {
|
|
483
|
+
let type = parseNALUType(data[index]);
|
|
484
|
+
switch (type) {
|
|
485
|
+
case NALUType.SLICE_IDR:
|
|
486
|
+
case NALUType.SLICE_NON_IDR:
|
|
487
|
+
return index + 2;
|
|
488
|
+
default:
|
|
489
|
+
break;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
throw new TypeError('Could not find NALU');
|
|
493
|
+
}
|
|
494
|
+
} catch (e) {
|
|
495
|
+
// no op, we just continue and fallback to vp8
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
return UNENCRYPTED_BYTES[frame.type];
|
|
499
|
+
} else {
|
|
500
|
+
return UNENCRYPTED_BYTES.audio;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* inspects frame payloadtype if available and maps it to the codec specified in rtpMap
|
|
506
|
+
*/
|
|
507
|
+
getVideoCodec(frame: RTCEncodedVideoFrame): VideoCodec | undefined {
|
|
508
|
+
if (this.rtpMap.size === 0) {
|
|
509
|
+
return undefined;
|
|
510
|
+
}
|
|
511
|
+
// @ts-expect-error payloadType is not yet part of the typescript definition and currently not supported in Safari
|
|
512
|
+
const payloadType = frame.getMetadata().payloadType;
|
|
513
|
+
const codec = payloadType ? this.rtpMap.get(payloadType) : undefined;
|
|
514
|
+
return codec;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
/**
|
|
519
|
+
* Slice the NALUs present in the supplied buffer, assuming it is already byte-aligned
|
|
520
|
+
* code adapted from https://github.com/medooze/h264-frame-parser/blob/main/lib/NalUnits.ts to return indices only
|
|
521
|
+
*/
|
|
522
|
+
export function findNALUIndices(stream: Uint8Array): number[] {
|
|
523
|
+
const result: number[] = [];
|
|
524
|
+
let start = 0,
|
|
525
|
+
pos = 0,
|
|
526
|
+
searchLength = stream.length - 2;
|
|
527
|
+
while (pos < searchLength) {
|
|
528
|
+
// skip until end of current NALU
|
|
529
|
+
while (
|
|
530
|
+
pos < searchLength &&
|
|
531
|
+
!(stream[pos] === 0 && stream[pos + 1] === 0 && stream[pos + 2] === 1)
|
|
532
|
+
)
|
|
533
|
+
pos++;
|
|
534
|
+
if (pos >= searchLength) pos = stream.length;
|
|
535
|
+
// remove trailing zeros from current NALU
|
|
536
|
+
let end = pos;
|
|
537
|
+
while (end > start && stream[end - 1] === 0) end--;
|
|
538
|
+
// save current NALU
|
|
539
|
+
if (start === 0) {
|
|
540
|
+
if (end !== start) throw TypeError('byte stream contains leading data');
|
|
541
|
+
} else {
|
|
542
|
+
result.push(start);
|
|
543
|
+
}
|
|
544
|
+
// begin new NALU
|
|
545
|
+
start = pos = pos + 3;
|
|
546
|
+
}
|
|
547
|
+
return result;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
export function parseNALUType(startByte: number): NALUType {
|
|
551
|
+
return startByte & kNaluTypeMask;
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
const kNaluTypeMask = 0x1f;
|
|
555
|
+
|
|
556
|
+
export enum NALUType {
|
|
557
|
+
/** Coded slice of a non-IDR picture */
|
|
558
|
+
SLICE_NON_IDR = 1,
|
|
559
|
+
/** Coded slice data partition A */
|
|
560
|
+
SLICE_PARTITION_A = 2,
|
|
561
|
+
/** Coded slice data partition B */
|
|
562
|
+
SLICE_PARTITION_B = 3,
|
|
563
|
+
/** Coded slice data partition C */
|
|
564
|
+
SLICE_PARTITION_C = 4,
|
|
565
|
+
/** Coded slice of an IDR picture */
|
|
566
|
+
SLICE_IDR = 5,
|
|
567
|
+
/** Supplemental enhancement information */
|
|
568
|
+
SEI = 6,
|
|
569
|
+
/** Sequence parameter set */
|
|
570
|
+
SPS = 7,
|
|
571
|
+
/** Picture parameter set */
|
|
572
|
+
PPS = 8,
|
|
573
|
+
/** Access unit delimiter */
|
|
574
|
+
AUD = 9,
|
|
575
|
+
/** End of sequence */
|
|
576
|
+
END_SEQ = 10,
|
|
577
|
+
/** End of stream */
|
|
578
|
+
END_STREAM = 11,
|
|
579
|
+
/** Filler data */
|
|
580
|
+
FILLER_DATA = 12,
|
|
581
|
+
/** Sequence parameter set extension */
|
|
582
|
+
SPS_EXT = 13,
|
|
583
|
+
/** Prefix NAL unit */
|
|
584
|
+
PREFIX_NALU = 14,
|
|
585
|
+
/** Subset sequence parameter set */
|
|
586
|
+
SUBSET_SPS = 15,
|
|
587
|
+
/** Depth parameter set */
|
|
588
|
+
DPS = 16,
|
|
589
|
+
|
|
590
|
+
// 17, 18 reserved
|
|
591
|
+
|
|
592
|
+
/** Coded slice of an auxiliary coded picture without partitioning */
|
|
593
|
+
SLICE_AUX = 19,
|
|
594
|
+
/** Coded slice extension */
|
|
595
|
+
SLICE_EXT = 20,
|
|
596
|
+
/** Coded slice extension for a depth view component or a 3D-AVC texture view component */
|
|
597
|
+
SLICE_LAYER_EXT = 21,
|
|
598
|
+
|
|
599
|
+
// 22, 23 reserved
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
/**
|
|
603
|
+
* we use a magic frame trailer to detect whether a frame is injected
|
|
604
|
+
* by the livekit server and thus to be treated as unencrypted
|
|
605
|
+
* @internal
|
|
606
|
+
*/
|
|
607
|
+
export function isFrameServerInjected(frameData: ArrayBuffer, trailerBytes: Uint8Array): boolean {
|
|
608
|
+
const frameTrailer = new Uint8Array(
|
|
609
|
+
frameData.slice(frameData.byteLength - trailerBytes.byteLength),
|
|
610
|
+
);
|
|
611
|
+
return trailerBytes.every((value, index) => value === frameTrailer[index]);
|
|
612
|
+
}
|