@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,1350 @@
|
|
|
1
|
+
import log from '../../logger';
|
|
2
|
+
import type { InternalRoomOptions } from '../../options';
|
|
3
|
+
import {
|
|
4
|
+
DataPacket,
|
|
5
|
+
DataPacket_Kind,
|
|
6
|
+
Encryption_Type,
|
|
7
|
+
ParticipantInfo,
|
|
8
|
+
ParticipantPermission,
|
|
9
|
+
UserPacket,
|
|
10
|
+
} from '../../proto/livekit_models_pb';
|
|
11
|
+
import {
|
|
12
|
+
AddTrackRequest,
|
|
13
|
+
DataChannelInfo,
|
|
14
|
+
SignalTarget,
|
|
15
|
+
SimulcastCodec,
|
|
16
|
+
SubscribedQualityUpdate,
|
|
17
|
+
TrackPublishedResponse,
|
|
18
|
+
TrackUnpublishedResponse,
|
|
19
|
+
} from '../../proto/livekit_rtc_pb';
|
|
20
|
+
import type RTCEngine from '../RTCEngine';
|
|
21
|
+
import { DeviceUnsupportedError, TrackInvalidError, UnexpectedConnectionState } from '../errors';
|
|
22
|
+
import { EngineEvent, ParticipantEvent, TrackEvent } from '../events';
|
|
23
|
+
import LocalAudioTrack from '../track/LocalAudioTrack';
|
|
24
|
+
import LocalTrack from '../track/LocalTrack';
|
|
25
|
+
import LocalTrackPublication from '../track/LocalTrackPublication';
|
|
26
|
+
import LocalVideoTrack, { videoLayersFromEncodings } from '../track/LocalVideoTrack';
|
|
27
|
+
import { Track } from '../track/Track';
|
|
28
|
+
import type {
|
|
29
|
+
AudioCaptureOptions,
|
|
30
|
+
BackupVideoCodec,
|
|
31
|
+
CreateLocalTracksOptions,
|
|
32
|
+
ScreenShareCaptureOptions,
|
|
33
|
+
TrackPublishOptions,
|
|
34
|
+
VideoCaptureOptions,
|
|
35
|
+
} from '../track/options';
|
|
36
|
+
import { ScreenSharePresets, VideoPresets, isBackupCodec, isCodecEqual } from '../track/options';
|
|
37
|
+
import {
|
|
38
|
+
constraintsForOptions,
|
|
39
|
+
mergeDefaultOptions,
|
|
40
|
+
screenCaptureToDisplayMediaStreamOptions,
|
|
41
|
+
} from '../track/utils';
|
|
42
|
+
import type { DataPublishOptions } from '../types';
|
|
43
|
+
import { Future, isFireFox, isSVCCodec, isSafari, isWeb, supportsAV1, supportsVP9 } from '../utils';
|
|
44
|
+
import Participant from './Participant';
|
|
45
|
+
import type { ParticipantTrackPermission } from './ParticipantTrackPermission';
|
|
46
|
+
import { trackPermissionToProto } from './ParticipantTrackPermission';
|
|
47
|
+
import RemoteParticipant from './RemoteParticipant';
|
|
48
|
+
import {
|
|
49
|
+
computeTrackBackupEncodings,
|
|
50
|
+
computeVideoEncodings,
|
|
51
|
+
mediaTrackToLocalTrack,
|
|
52
|
+
} from './publishUtils';
|
|
53
|
+
|
|
54
|
+
export default class LocalParticipant extends Participant {
|
|
55
|
+
audioTracks: Map<string, LocalTrackPublication>;
|
|
56
|
+
|
|
57
|
+
videoTracks: Map<string, LocalTrackPublication>;
|
|
58
|
+
|
|
59
|
+
/** map of track sid => all published tracks */
|
|
60
|
+
tracks: Map<string, LocalTrackPublication>;
|
|
61
|
+
|
|
62
|
+
/** @internal */
|
|
63
|
+
engine: RTCEngine;
|
|
64
|
+
|
|
65
|
+
/** @internal */
|
|
66
|
+
activeDeviceMap: Map<MediaDeviceKind, string>;
|
|
67
|
+
|
|
68
|
+
private pendingPublishing = new Set<Track.Source>();
|
|
69
|
+
|
|
70
|
+
private pendingPublishPromises = new Map<LocalTrack, Promise<LocalTrackPublication>>();
|
|
71
|
+
|
|
72
|
+
private cameraError: Error | undefined;
|
|
73
|
+
|
|
74
|
+
private microphoneError: Error | undefined;
|
|
75
|
+
|
|
76
|
+
private participantTrackPermissions: Array<ParticipantTrackPermission> = [];
|
|
77
|
+
|
|
78
|
+
private allParticipantsAllowedToSubscribe: boolean = true;
|
|
79
|
+
|
|
80
|
+
// keep a pointer to room options
|
|
81
|
+
private roomOptions: InternalRoomOptions;
|
|
82
|
+
|
|
83
|
+
private encryptionType: Encryption_Type = Encryption_Type.NONE;
|
|
84
|
+
|
|
85
|
+
private reconnectFuture?: Future<void>;
|
|
86
|
+
|
|
87
|
+
/** @internal */
|
|
88
|
+
constructor(sid: string, identity: string, engine: RTCEngine, options: InternalRoomOptions) {
|
|
89
|
+
super(sid, identity);
|
|
90
|
+
this.audioTracks = new Map();
|
|
91
|
+
this.videoTracks = new Map();
|
|
92
|
+
this.tracks = new Map();
|
|
93
|
+
this.engine = engine;
|
|
94
|
+
this.roomOptions = options;
|
|
95
|
+
this.setupEngine(engine);
|
|
96
|
+
this.activeDeviceMap = new Map();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
get lastCameraError(): Error | undefined {
|
|
100
|
+
return this.cameraError;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
get lastMicrophoneError(): Error | undefined {
|
|
104
|
+
return this.microphoneError;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
getTrack(source: Track.Source): LocalTrackPublication | undefined {
|
|
108
|
+
const track = super.getTrack(source);
|
|
109
|
+
if (track) {
|
|
110
|
+
return track as LocalTrackPublication;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
getTrackByName(name: string): LocalTrackPublication | undefined {
|
|
115
|
+
const track = super.getTrackByName(name);
|
|
116
|
+
if (track) {
|
|
117
|
+
return track as LocalTrackPublication;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* @internal
|
|
123
|
+
*/
|
|
124
|
+
setupEngine(engine: RTCEngine) {
|
|
125
|
+
this.engine = engine;
|
|
126
|
+
this.engine.client.onRemoteMuteChanged = (trackSid: string, muted: boolean) => {
|
|
127
|
+
const pub = this.tracks.get(trackSid);
|
|
128
|
+
if (!pub || !pub.track) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
if (muted) {
|
|
132
|
+
pub.mute();
|
|
133
|
+
} else {
|
|
134
|
+
pub.unmute();
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
this.engine.client.onSubscribedQualityUpdate = this.handleSubscribedQualityUpdate;
|
|
139
|
+
|
|
140
|
+
this.engine.client.onLocalTrackUnpublished = this.handleLocalTrackUnpublished;
|
|
141
|
+
|
|
142
|
+
this.engine
|
|
143
|
+
.on(EngineEvent.Connected, this.handleReconnected)
|
|
144
|
+
.on(EngineEvent.Restarted, this.handleReconnected)
|
|
145
|
+
.on(EngineEvent.Resumed, this.handleReconnected)
|
|
146
|
+
.on(EngineEvent.Restarting, this.handleReconnecting)
|
|
147
|
+
.on(EngineEvent.Resuming, this.handleReconnecting)
|
|
148
|
+
.on(EngineEvent.Disconnected, this.handleDisconnected);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
private handleReconnecting = () => {
|
|
152
|
+
if (!this.reconnectFuture) {
|
|
153
|
+
this.reconnectFuture = new Future<void>();
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
private handleReconnected = () => {
|
|
158
|
+
this.reconnectFuture?.resolve?.();
|
|
159
|
+
this.reconnectFuture = undefined;
|
|
160
|
+
this.updateTrackSubscriptionPermissions();
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
private handleDisconnected = () => {
|
|
164
|
+
if (this.reconnectFuture) {
|
|
165
|
+
this.reconnectFuture.promise.catch((e) => log.warn(e));
|
|
166
|
+
this.reconnectFuture?.reject?.('Got disconnected during reconnection attempt');
|
|
167
|
+
this.reconnectFuture = undefined;
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Sets and updates the metadata of the local participant.
|
|
173
|
+
* Note: this requires `canUpdateOwnMetadata` permission encoded in the token.
|
|
174
|
+
* @param metadata
|
|
175
|
+
*/
|
|
176
|
+
setMetadata(metadata: string): void {
|
|
177
|
+
super.setMetadata(metadata);
|
|
178
|
+
this.engine.client.sendUpdateLocalMetadata(metadata, this.name ?? '');
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Sets and updates the name of the local participant.
|
|
183
|
+
* Note: this requires `canUpdateOwnMetadata` permission encoded in the token.
|
|
184
|
+
* @param metadata
|
|
185
|
+
*/
|
|
186
|
+
setName(name: string): void {
|
|
187
|
+
super.setName(name);
|
|
188
|
+
this.engine.client.sendUpdateLocalMetadata(this.metadata ?? '', name);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Enable or disable a participant's camera track.
|
|
193
|
+
*
|
|
194
|
+
* If a track has already published, it'll mute or unmute the track.
|
|
195
|
+
* Resolves with a `LocalTrackPublication` instance if successful and `undefined` otherwise
|
|
196
|
+
*/
|
|
197
|
+
setCameraEnabled(
|
|
198
|
+
enabled: boolean,
|
|
199
|
+
options?: VideoCaptureOptions,
|
|
200
|
+
publishOptions?: TrackPublishOptions,
|
|
201
|
+
): Promise<LocalTrackPublication | undefined> {
|
|
202
|
+
return this.setTrackEnabled(Track.Source.Camera, enabled, options, publishOptions);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Enable or disable a participant's microphone track.
|
|
207
|
+
*
|
|
208
|
+
* If a track has already published, it'll mute or unmute the track.
|
|
209
|
+
* Resolves with a `LocalTrackPublication` instance if successful and `undefined` otherwise
|
|
210
|
+
*/
|
|
211
|
+
setMicrophoneEnabled(
|
|
212
|
+
enabled: boolean,
|
|
213
|
+
options?: AudioCaptureOptions,
|
|
214
|
+
publishOptions?: TrackPublishOptions,
|
|
215
|
+
): Promise<LocalTrackPublication | undefined> {
|
|
216
|
+
return this.setTrackEnabled(Track.Source.Microphone, enabled, options, publishOptions);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Start or stop sharing a participant's screen
|
|
221
|
+
* Resolves with a `LocalTrackPublication` instance if successful and `undefined` otherwise
|
|
222
|
+
*/
|
|
223
|
+
setScreenShareEnabled(
|
|
224
|
+
enabled: boolean,
|
|
225
|
+
options?: ScreenShareCaptureOptions,
|
|
226
|
+
publishOptions?: TrackPublishOptions,
|
|
227
|
+
): Promise<LocalTrackPublication | undefined> {
|
|
228
|
+
return this.setTrackEnabled(Track.Source.ScreenShare, enabled, options, publishOptions);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** @internal */
|
|
232
|
+
setPermissions(permissions: ParticipantPermission): boolean {
|
|
233
|
+
const prevPermissions = this.permissions;
|
|
234
|
+
const changed = super.setPermissions(permissions);
|
|
235
|
+
if (changed && prevPermissions) {
|
|
236
|
+
this.emit(ParticipantEvent.ParticipantPermissionsChanged, prevPermissions);
|
|
237
|
+
}
|
|
238
|
+
return changed;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** @internal */
|
|
242
|
+
async setE2EEEnabled(enabled: boolean) {
|
|
243
|
+
this.encryptionType = enabled ? Encryption_Type.GCM : Encryption_Type.NONE;
|
|
244
|
+
await this.republishAllTracks(undefined, false);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Enable or disable publishing for a track by source. This serves as a simple
|
|
249
|
+
* way to manage the common tracks (camera, mic, or screen share).
|
|
250
|
+
* Resolves with LocalTrackPublication if successful and void otherwise
|
|
251
|
+
*/
|
|
252
|
+
private async setTrackEnabled(
|
|
253
|
+
source: Extract<Track.Source, Track.Source.Camera>,
|
|
254
|
+
enabled: boolean,
|
|
255
|
+
options?: VideoCaptureOptions,
|
|
256
|
+
publishOptions?: TrackPublishOptions,
|
|
257
|
+
): Promise<LocalTrackPublication | undefined>;
|
|
258
|
+
private async setTrackEnabled(
|
|
259
|
+
source: Extract<Track.Source, Track.Source.Microphone>,
|
|
260
|
+
enabled: boolean,
|
|
261
|
+
options?: AudioCaptureOptions,
|
|
262
|
+
publishOptions?: TrackPublishOptions,
|
|
263
|
+
): Promise<LocalTrackPublication | undefined>;
|
|
264
|
+
private async setTrackEnabled(
|
|
265
|
+
source: Extract<Track.Source, Track.Source.ScreenShare>,
|
|
266
|
+
enabled: boolean,
|
|
267
|
+
options?: ScreenShareCaptureOptions,
|
|
268
|
+
publishOptions?: TrackPublishOptions,
|
|
269
|
+
): Promise<LocalTrackPublication | undefined>;
|
|
270
|
+
private async setTrackEnabled(
|
|
271
|
+
source: Track.Source,
|
|
272
|
+
enabled: true,
|
|
273
|
+
options?: VideoCaptureOptions | AudioCaptureOptions | ScreenShareCaptureOptions,
|
|
274
|
+
publishOptions?: TrackPublishOptions,
|
|
275
|
+
) {
|
|
276
|
+
log.debug('setTrackEnabled', { source, enabled });
|
|
277
|
+
let track = this.getTrack(source);
|
|
278
|
+
if (enabled) {
|
|
279
|
+
if (track) {
|
|
280
|
+
await track.unmute();
|
|
281
|
+
} else {
|
|
282
|
+
let localTracks: Array<LocalTrack> | undefined;
|
|
283
|
+
if (this.pendingPublishing.has(source)) {
|
|
284
|
+
log.info('skipping duplicate published source', { source });
|
|
285
|
+
// no-op it's already been requested
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
this.pendingPublishing.add(source);
|
|
289
|
+
try {
|
|
290
|
+
switch (source) {
|
|
291
|
+
case Track.Source.Camera:
|
|
292
|
+
localTracks = await this.createTracks({
|
|
293
|
+
video: (options as VideoCaptureOptions | undefined) ?? true,
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
break;
|
|
297
|
+
case Track.Source.Microphone:
|
|
298
|
+
localTracks = await this.createTracks({
|
|
299
|
+
audio: (options as AudioCaptureOptions | undefined) ?? true,
|
|
300
|
+
});
|
|
301
|
+
break;
|
|
302
|
+
case Track.Source.ScreenShare:
|
|
303
|
+
localTracks = await this.createScreenTracks({
|
|
304
|
+
...(options as ScreenShareCaptureOptions | undefined),
|
|
305
|
+
});
|
|
306
|
+
break;
|
|
307
|
+
default:
|
|
308
|
+
throw new TrackInvalidError(source);
|
|
309
|
+
}
|
|
310
|
+
const publishPromises: Array<Promise<LocalTrackPublication>> = [];
|
|
311
|
+
for (const localTrack of localTracks) {
|
|
312
|
+
log.info('publishing track', { localTrack });
|
|
313
|
+
publishPromises.push(this.publishTrack(localTrack, publishOptions));
|
|
314
|
+
}
|
|
315
|
+
const publishedTracks = await Promise.all(publishPromises);
|
|
316
|
+
// for screen share publications including audio, this will only return the screen share publication, not the screen share audio one
|
|
317
|
+
// revisit if we want to return an array of tracks instead for v2
|
|
318
|
+
[track] = publishedTracks;
|
|
319
|
+
} catch (e) {
|
|
320
|
+
localTracks?.forEach((tr) => {
|
|
321
|
+
tr.stop();
|
|
322
|
+
});
|
|
323
|
+
if (e instanceof Error && !(e instanceof TrackInvalidError)) {
|
|
324
|
+
this.emit(ParticipantEvent.MediaDevicesError, e);
|
|
325
|
+
}
|
|
326
|
+
throw e;
|
|
327
|
+
} finally {
|
|
328
|
+
this.pendingPublishing.delete(source);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
} else if (track && track.track) {
|
|
332
|
+
// screenshare cannot be muted, unpublish instead
|
|
333
|
+
if (source === Track.Source.ScreenShare) {
|
|
334
|
+
track = await this.unpublishTrack(track.track);
|
|
335
|
+
const screenAudioTrack = this.getTrack(Track.Source.ScreenShareAudio);
|
|
336
|
+
if (screenAudioTrack && screenAudioTrack.track) {
|
|
337
|
+
this.unpublishTrack(screenAudioTrack.track);
|
|
338
|
+
}
|
|
339
|
+
} else {
|
|
340
|
+
await track.mute();
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
return track;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Publish both camera and microphone at the same time. This is useful for
|
|
348
|
+
* displaying a single Permission Dialog box to the end user.
|
|
349
|
+
*/
|
|
350
|
+
async enableCameraAndMicrophone() {
|
|
351
|
+
if (
|
|
352
|
+
this.pendingPublishing.has(Track.Source.Camera) ||
|
|
353
|
+
this.pendingPublishing.has(Track.Source.Microphone)
|
|
354
|
+
) {
|
|
355
|
+
// no-op it's already been requested
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
this.pendingPublishing.add(Track.Source.Camera);
|
|
360
|
+
this.pendingPublishing.add(Track.Source.Microphone);
|
|
361
|
+
try {
|
|
362
|
+
const tracks: LocalTrack[] = await this.createTracks({
|
|
363
|
+
audio: true,
|
|
364
|
+
video: true,
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
await Promise.all(tracks.map((track) => this.publishTrack(track)));
|
|
368
|
+
} finally {
|
|
369
|
+
this.pendingPublishing.delete(Track.Source.Camera);
|
|
370
|
+
this.pendingPublishing.delete(Track.Source.Microphone);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Create local camera and/or microphone tracks
|
|
376
|
+
* @param options
|
|
377
|
+
* @returns
|
|
378
|
+
*/
|
|
379
|
+
async createTracks(options?: CreateLocalTracksOptions): Promise<LocalTrack[]> {
|
|
380
|
+
const opts = mergeDefaultOptions(
|
|
381
|
+
options,
|
|
382
|
+
this.roomOptions?.audioCaptureDefaults,
|
|
383
|
+
this.roomOptions?.videoCaptureDefaults,
|
|
384
|
+
);
|
|
385
|
+
|
|
386
|
+
const constraints = constraintsForOptions(opts);
|
|
387
|
+
let stream: MediaStream | undefined;
|
|
388
|
+
try {
|
|
389
|
+
stream = await navigator.mediaDevices.getUserMedia(constraints);
|
|
390
|
+
} catch (err) {
|
|
391
|
+
if (err instanceof Error) {
|
|
392
|
+
if (constraints.audio) {
|
|
393
|
+
this.microphoneError = err;
|
|
394
|
+
}
|
|
395
|
+
if (constraints.video) {
|
|
396
|
+
this.cameraError = err;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
throw err;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
if (constraints.audio) {
|
|
404
|
+
this.microphoneError = undefined;
|
|
405
|
+
}
|
|
406
|
+
if (constraints.video) {
|
|
407
|
+
this.cameraError = undefined;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
return stream.getTracks().map((mediaStreamTrack) => {
|
|
411
|
+
const isAudio = mediaStreamTrack.kind === 'audio';
|
|
412
|
+
let trackOptions = isAudio ? options!.audio : options!.video;
|
|
413
|
+
if (typeof trackOptions === 'boolean' || !trackOptions) {
|
|
414
|
+
trackOptions = {};
|
|
415
|
+
}
|
|
416
|
+
let trackConstraints: MediaTrackConstraints | undefined;
|
|
417
|
+
const conOrBool = isAudio ? constraints.audio : constraints.video;
|
|
418
|
+
if (typeof conOrBool !== 'boolean') {
|
|
419
|
+
trackConstraints = conOrBool;
|
|
420
|
+
}
|
|
421
|
+
const track = mediaTrackToLocalTrack(mediaStreamTrack, trackConstraints);
|
|
422
|
+
if (track.kind === Track.Kind.Video) {
|
|
423
|
+
track.source = Track.Source.Camera;
|
|
424
|
+
} else if (track.kind === Track.Kind.Audio) {
|
|
425
|
+
track.source = Track.Source.Microphone;
|
|
426
|
+
}
|
|
427
|
+
track.mediaStream = stream;
|
|
428
|
+
return track;
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* Creates a screen capture tracks with getDisplayMedia().
|
|
434
|
+
* A LocalVideoTrack is always created and returned.
|
|
435
|
+
* If { audio: true }, and the browser supports audio capture, a LocalAudioTrack is also created.
|
|
436
|
+
*/
|
|
437
|
+
async createScreenTracks(options?: ScreenShareCaptureOptions): Promise<Array<LocalTrack>> {
|
|
438
|
+
if (options === undefined) {
|
|
439
|
+
options = {};
|
|
440
|
+
}
|
|
441
|
+
if (options.resolution === undefined) {
|
|
442
|
+
options.resolution = ScreenSharePresets.h1080fps15.resolution;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
if (navigator.mediaDevices.getDisplayMedia === undefined) {
|
|
446
|
+
throw new DeviceUnsupportedError('getDisplayMedia not supported');
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
const constraints = screenCaptureToDisplayMediaStreamOptions(options);
|
|
450
|
+
const stream: MediaStream = await navigator.mediaDevices.getDisplayMedia(constraints);
|
|
451
|
+
|
|
452
|
+
const tracks = stream.getVideoTracks();
|
|
453
|
+
if (tracks.length === 0) {
|
|
454
|
+
throw new TrackInvalidError('no video track found');
|
|
455
|
+
}
|
|
456
|
+
const screenVideo = new LocalVideoTrack(tracks[0], undefined, false);
|
|
457
|
+
screenVideo.source = Track.Source.ScreenShare;
|
|
458
|
+
const localTracks: Array<LocalTrack> = [screenVideo];
|
|
459
|
+
if (stream.getAudioTracks().length > 0) {
|
|
460
|
+
const screenAudio = new LocalAudioTrack(stream.getAudioTracks()[0], undefined, false);
|
|
461
|
+
screenAudio.source = Track.Source.ScreenShareAudio;
|
|
462
|
+
localTracks.push(screenAudio);
|
|
463
|
+
}
|
|
464
|
+
return localTracks;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* Publish a new track to the room
|
|
469
|
+
* @param track
|
|
470
|
+
* @param options
|
|
471
|
+
*/
|
|
472
|
+
async publishTrack(
|
|
473
|
+
track: LocalTrack | MediaStreamTrack,
|
|
474
|
+
options?: TrackPublishOptions,
|
|
475
|
+
): Promise<LocalTrackPublication> {
|
|
476
|
+
await this.reconnectFuture?.promise;
|
|
477
|
+
if (track instanceof LocalTrack && this.pendingPublishPromises.has(track)) {
|
|
478
|
+
await this.pendingPublishPromises.get(track);
|
|
479
|
+
}
|
|
480
|
+
let defaultConstraints: MediaTrackConstraints | undefined;
|
|
481
|
+
if (track instanceof MediaStreamTrack) {
|
|
482
|
+
defaultConstraints = track.getConstraints();
|
|
483
|
+
} else {
|
|
484
|
+
// we want to access constraints directly as `track.mediaStreamTrack`
|
|
485
|
+
// might be pointing to a non-device track (e.g. processed track) already
|
|
486
|
+
defaultConstraints = track.constraints;
|
|
487
|
+
let deviceKind: MediaDeviceKind | undefined = undefined;
|
|
488
|
+
switch (track.source) {
|
|
489
|
+
case Track.Source.Microphone:
|
|
490
|
+
deviceKind = 'audioinput';
|
|
491
|
+
break;
|
|
492
|
+
case Track.Source.Camera:
|
|
493
|
+
deviceKind = 'videoinput';
|
|
494
|
+
default:
|
|
495
|
+
break;
|
|
496
|
+
}
|
|
497
|
+
if (deviceKind && this.activeDeviceMap.has(deviceKind)) {
|
|
498
|
+
defaultConstraints = {
|
|
499
|
+
...defaultConstraints,
|
|
500
|
+
deviceId: this.activeDeviceMap.get(deviceKind),
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
// convert raw media track into audio or video track
|
|
505
|
+
if (track instanceof MediaStreamTrack) {
|
|
506
|
+
switch (track.kind) {
|
|
507
|
+
case 'audio':
|
|
508
|
+
track = new LocalAudioTrack(track, defaultConstraints, true);
|
|
509
|
+
break;
|
|
510
|
+
case 'video':
|
|
511
|
+
track = new LocalVideoTrack(track, defaultConstraints, true);
|
|
512
|
+
break;
|
|
513
|
+
default:
|
|
514
|
+
throw new TrackInvalidError(`unsupported MediaStreamTrack kind ${track.kind}`);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// is it already published? if so skip
|
|
519
|
+
let existingPublication: LocalTrackPublication | undefined;
|
|
520
|
+
this.tracks.forEach((publication) => {
|
|
521
|
+
if (!publication.track) {
|
|
522
|
+
return;
|
|
523
|
+
}
|
|
524
|
+
if (publication.track === track) {
|
|
525
|
+
existingPublication = <LocalTrackPublication>publication;
|
|
526
|
+
}
|
|
527
|
+
});
|
|
528
|
+
|
|
529
|
+
if (existingPublication) {
|
|
530
|
+
log.warn('track has already been published, skipping');
|
|
531
|
+
return existingPublication;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
const isStereoInput =
|
|
535
|
+
('channelCount' in track.mediaStreamTrack.getSettings() &&
|
|
536
|
+
// @ts-ignore `channelCount` on getSettings() is currently only available for Safari, but is generally the best way to determine a stereo track https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackSettings/channelCount
|
|
537
|
+
track.mediaStreamTrack.getSettings().channelCount === 2) ||
|
|
538
|
+
track.mediaStreamTrack.getConstraints().channelCount === 2;
|
|
539
|
+
const isStereo = options?.forceStereo ?? isStereoInput;
|
|
540
|
+
|
|
541
|
+
// disable dtx for stereo track if not enabled explicitly
|
|
542
|
+
if (isStereo) {
|
|
543
|
+
if (!options) {
|
|
544
|
+
options = {};
|
|
545
|
+
}
|
|
546
|
+
if (options.dtx === undefined) {
|
|
547
|
+
log.info(
|
|
548
|
+
`Opus DTX will be disabled for stereo tracks by default. Enable them explicitly to make it work.`,
|
|
549
|
+
);
|
|
550
|
+
}
|
|
551
|
+
if (options.red === undefined) {
|
|
552
|
+
log.info(
|
|
553
|
+
`Opus RED will be disabled for stereo tracks by default. Enable them explicitly to make it work.`,
|
|
554
|
+
);
|
|
555
|
+
}
|
|
556
|
+
options.dtx ??= false;
|
|
557
|
+
options.red ??= false;
|
|
558
|
+
}
|
|
559
|
+
const opts: TrackPublishOptions = {
|
|
560
|
+
...this.roomOptions.publishDefaults,
|
|
561
|
+
...options,
|
|
562
|
+
};
|
|
563
|
+
|
|
564
|
+
// disable simulcast if e2ee is set on safari
|
|
565
|
+
if (isSafari() && this.roomOptions.e2ee) {
|
|
566
|
+
log.info(`End-to-end encryption is set up, simulcast publishing will be disabled on Safari`);
|
|
567
|
+
opts.simulcast = false;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
if (opts.source) {
|
|
571
|
+
track.source = opts.source;
|
|
572
|
+
}
|
|
573
|
+
const publishPromise = this.publish(track, opts, isStereo);
|
|
574
|
+
this.pendingPublishPromises.set(track, publishPromise);
|
|
575
|
+
try {
|
|
576
|
+
const publication = await publishPromise;
|
|
577
|
+
return publication;
|
|
578
|
+
} catch (e) {
|
|
579
|
+
throw e;
|
|
580
|
+
} finally {
|
|
581
|
+
this.pendingPublishPromises.delete(track);
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
private async publish(track: LocalTrack, opts: TrackPublishOptions, isStereo: boolean) {
|
|
586
|
+
const existingTrackOfSource = Array.from(this.tracks.values()).find(
|
|
587
|
+
(publishedTrack) => track instanceof LocalTrack && publishedTrack.source === track.source,
|
|
588
|
+
);
|
|
589
|
+
if (existingTrackOfSource && track.source !== Track.Source.Unknown) {
|
|
590
|
+
try {
|
|
591
|
+
// throw an Error in order to capture the stack trace
|
|
592
|
+
throw Error(`publishing a second track with the same source: ${track.source}`);
|
|
593
|
+
} catch (e: unknown) {
|
|
594
|
+
if (e instanceof Error) {
|
|
595
|
+
log.warn(e.message, {
|
|
596
|
+
oldTrack: existingTrackOfSource,
|
|
597
|
+
newTrack: track,
|
|
598
|
+
trace: e.stack,
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
if (opts.stopMicTrackOnMute && track instanceof LocalAudioTrack) {
|
|
604
|
+
track.stopOnMute = true;
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
if (track.source === Track.Source.ScreenShare && isFireFox()) {
|
|
608
|
+
// Firefox does not work well with simulcasted screen share
|
|
609
|
+
// we frequently get no data on layer 0 when enabled
|
|
610
|
+
opts.simulcast = false;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
// require full AV1/VP9 SVC support prior to using it
|
|
614
|
+
if (opts.videoCodec === 'av1' && !supportsAV1()) {
|
|
615
|
+
opts.videoCodec = undefined;
|
|
616
|
+
}
|
|
617
|
+
if (opts.videoCodec === 'vp9' && !supportsVP9()) {
|
|
618
|
+
opts.videoCodec = undefined;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
// handle track actions
|
|
622
|
+
track.on(TrackEvent.Muted, this.onTrackMuted);
|
|
623
|
+
track.on(TrackEvent.Unmuted, this.onTrackUnmuted);
|
|
624
|
+
track.on(TrackEvent.Ended, this.handleTrackEnded);
|
|
625
|
+
track.on(TrackEvent.UpstreamPaused, this.onTrackUpstreamPaused);
|
|
626
|
+
track.on(TrackEvent.UpstreamResumed, this.onTrackUpstreamResumed);
|
|
627
|
+
|
|
628
|
+
// create track publication from track
|
|
629
|
+
const req = new AddTrackRequest({
|
|
630
|
+
// get local track id for use during publishing
|
|
631
|
+
cid: track.mediaStreamTrack.id,
|
|
632
|
+
name: opts.name,
|
|
633
|
+
type: Track.kindToProto(track.kind),
|
|
634
|
+
muted: track.isMuted,
|
|
635
|
+
source: Track.sourceToProto(track.source),
|
|
636
|
+
disableDtx: !(opts.dtx ?? true),
|
|
637
|
+
encryption: this.encryptionType,
|
|
638
|
+
stereo: isStereo,
|
|
639
|
+
// disableRed: !(opts.red ?? true),
|
|
640
|
+
});
|
|
641
|
+
|
|
642
|
+
// compute encodings and layers for video
|
|
643
|
+
let encodings: RTCRtpEncodingParameters[] | undefined;
|
|
644
|
+
let simEncodings: RTCRtpEncodingParameters[] | undefined;
|
|
645
|
+
if (track.kind === Track.Kind.Video) {
|
|
646
|
+
let dims: Track.Dimensions = {
|
|
647
|
+
width: 0,
|
|
648
|
+
height: 0,
|
|
649
|
+
};
|
|
650
|
+
try {
|
|
651
|
+
dims = await track.waitForDimensions();
|
|
652
|
+
} catch (e) {
|
|
653
|
+
// use defaults, it's quite painful for congestion control without simulcast
|
|
654
|
+
// so using default dims according to publish settings
|
|
655
|
+
const defaultRes =
|
|
656
|
+
this.roomOptions.videoCaptureDefaults?.resolution ?? VideoPresets.h720.resolution;
|
|
657
|
+
dims = {
|
|
658
|
+
width: defaultRes.width,
|
|
659
|
+
height: defaultRes.height,
|
|
660
|
+
};
|
|
661
|
+
// log failure
|
|
662
|
+
log.error('could not determine track dimensions, using defaults', dims);
|
|
663
|
+
}
|
|
664
|
+
// width and height should be defined for video
|
|
665
|
+
req.width = dims.width;
|
|
666
|
+
req.height = dims.height;
|
|
667
|
+
// for svc codecs, disable simulcast and use vp8 for backup codec
|
|
668
|
+
if (track instanceof LocalVideoTrack) {
|
|
669
|
+
if (isSVCCodec(opts.videoCodec)) {
|
|
670
|
+
// set scalabilityMode to 'L3T3_KEY' by default
|
|
671
|
+
opts.scalabilityMode = opts.scalabilityMode ?? 'L3T3_KEY';
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
// set up backup
|
|
675
|
+
if (opts.videoCodec && opts.backupCodec && opts.videoCodec !== opts.backupCodec.codec) {
|
|
676
|
+
const simOpts = { ...opts };
|
|
677
|
+
simOpts.simulcast = true;
|
|
678
|
+
simEncodings = computeTrackBackupEncodings(track, opts.backupCodec.codec, simOpts);
|
|
679
|
+
|
|
680
|
+
req.simulcastCodecs = [
|
|
681
|
+
new SimulcastCodec({
|
|
682
|
+
codec: opts.videoCodec,
|
|
683
|
+
cid: track.mediaStreamTrack.id,
|
|
684
|
+
enableSimulcastLayers: true,
|
|
685
|
+
}),
|
|
686
|
+
new SimulcastCodec({
|
|
687
|
+
codec: opts.backupCodec.codec,
|
|
688
|
+
cid: '',
|
|
689
|
+
enableSimulcastLayers: true,
|
|
690
|
+
}),
|
|
691
|
+
];
|
|
692
|
+
} else if (opts.videoCodec) {
|
|
693
|
+
// pass codec info to sfu so it can prefer codec for the client which don't support
|
|
694
|
+
// setCodecPreferences
|
|
695
|
+
req.simulcastCodecs = [
|
|
696
|
+
new SimulcastCodec({
|
|
697
|
+
codec: opts.videoCodec,
|
|
698
|
+
cid: track.mediaStreamTrack.id,
|
|
699
|
+
enableSimulcastLayers: opts.simulcast ?? false,
|
|
700
|
+
}),
|
|
701
|
+
];
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
encodings = computeVideoEncodings(
|
|
706
|
+
track.source === Track.Source.ScreenShare,
|
|
707
|
+
dims.width,
|
|
708
|
+
dims.height,
|
|
709
|
+
opts,
|
|
710
|
+
);
|
|
711
|
+
req.layers = videoLayersFromEncodings(
|
|
712
|
+
req.width,
|
|
713
|
+
req.height,
|
|
714
|
+
encodings,
|
|
715
|
+
isSVCCodec(opts.videoCodec),
|
|
716
|
+
);
|
|
717
|
+
} else if (track.kind === Track.Kind.Audio) {
|
|
718
|
+
encodings = [
|
|
719
|
+
{
|
|
720
|
+
maxBitrate: opts.audioPreset?.maxBitrate ?? opts.audioBitrate,
|
|
721
|
+
priority: opts.audioPreset?.priority ?? 'high',
|
|
722
|
+
networkPriority: opts.audioPreset?.priority ?? 'high',
|
|
723
|
+
},
|
|
724
|
+
];
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
if (!this.engine || this.engine.isClosed) {
|
|
728
|
+
throw new UnexpectedConnectionState('cannot publish track when not connected');
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
const ti = await this.engine.addTrack(req);
|
|
732
|
+
let primaryCodecSupported = false;
|
|
733
|
+
let backupCodecSupported = false;
|
|
734
|
+
ti.codecs.forEach((c) => {
|
|
735
|
+
if (isCodecEqual(c.mimeType, opts.videoCodec)) {
|
|
736
|
+
primaryCodecSupported = true;
|
|
737
|
+
} else if (opts.backupCodec && isCodecEqual(c.mimeType, opts.backupCodec.codec)) {
|
|
738
|
+
backupCodecSupported = true;
|
|
739
|
+
}
|
|
740
|
+
});
|
|
741
|
+
|
|
742
|
+
if (req.simulcastCodecs.length > 0) {
|
|
743
|
+
if (!primaryCodecSupported && !backupCodecSupported) {
|
|
744
|
+
throw Error('cannot publish track, codec not supported by server');
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
if (!primaryCodecSupported && opts.backupCodec) {
|
|
748
|
+
const backupCodec = opts.backupCodec;
|
|
749
|
+
opts = { ...opts };
|
|
750
|
+
log.debug(
|
|
751
|
+
`primary codec ${opts.videoCodec} not supported, fallback to ${backupCodec.codec}`,
|
|
752
|
+
);
|
|
753
|
+
opts.videoCodec = backupCodec.codec;
|
|
754
|
+
opts.videoEncoding = backupCodec.encoding;
|
|
755
|
+
encodings = simEncodings;
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
const publication = new LocalTrackPublication(track.kind, ti, track);
|
|
760
|
+
// save options for when it needs to be republished again
|
|
761
|
+
publication.options = opts;
|
|
762
|
+
track.sid = ti.sid;
|
|
763
|
+
|
|
764
|
+
if (!this.engine.publisher) {
|
|
765
|
+
throw new UnexpectedConnectionState('publisher is closed');
|
|
766
|
+
}
|
|
767
|
+
log.debug(`publishing ${track.kind} with encodings`, { encodings, trackInfo: ti });
|
|
768
|
+
|
|
769
|
+
// store RTPSender
|
|
770
|
+
track.sender = await this.engine.createSender(track, opts, encodings);
|
|
771
|
+
|
|
772
|
+
if (encodings) {
|
|
773
|
+
if (isFireFox() && track.kind === Track.Kind.Audio) {
|
|
774
|
+
/* Refer to RFC https://datatracker.ietf.org/doc/html/rfc7587#section-6.1,
|
|
775
|
+
livekit-server uses maxaveragebitrate=510000in the answer sdp to permit client to
|
|
776
|
+
publish high quality audio track. But firefox always uses this value as the actual
|
|
777
|
+
bitrates, causing the audio bitrates to rise to 510Kbps in any stereo case unexpectedly.
|
|
778
|
+
So the client need to modify maxaverragebitrates in answer sdp to user provided value to
|
|
779
|
+
fix the issue.
|
|
780
|
+
*/
|
|
781
|
+
let trackTransceiver: RTCRtpTransceiver | undefined = undefined;
|
|
782
|
+
for (const transceiver of this.engine.publisher.pc.getTransceivers()) {
|
|
783
|
+
if (transceiver.sender === track.sender) {
|
|
784
|
+
trackTransceiver = transceiver;
|
|
785
|
+
break;
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
if (trackTransceiver) {
|
|
789
|
+
this.engine.publisher.setTrackCodecBitrate({
|
|
790
|
+
transceiver: trackTransceiver,
|
|
791
|
+
codec: 'opus',
|
|
792
|
+
maxbr: encodings[0]?.maxBitrate ? encodings[0].maxBitrate / 1000 : 0,
|
|
793
|
+
});
|
|
794
|
+
}
|
|
795
|
+
} else if (track.codec && isSVCCodec(track.codec) && encodings[0]?.maxBitrate) {
|
|
796
|
+
this.engine.publisher.setTrackCodecBitrate({
|
|
797
|
+
cid: req.cid,
|
|
798
|
+
codec: track.codec,
|
|
799
|
+
maxbr: encodings[0].maxBitrate / 1000,
|
|
800
|
+
});
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
await this.engine.negotiate();
|
|
805
|
+
|
|
806
|
+
if (track instanceof LocalVideoTrack) {
|
|
807
|
+
track.startMonitor(this.engine.client);
|
|
808
|
+
} else if (track instanceof LocalAudioTrack) {
|
|
809
|
+
track.startMonitor();
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
this.addTrackPublication(publication);
|
|
813
|
+
|
|
814
|
+
// send event for publication
|
|
815
|
+
this.emit(ParticipantEvent.LocalTrackPublished, publication);
|
|
816
|
+
return publication;
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
override get isLocal(): boolean {
|
|
820
|
+
return true;
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
/** @internal
|
|
824
|
+
* publish additional codec to existing track
|
|
825
|
+
*/
|
|
826
|
+
async publishAdditionalCodecForTrack(
|
|
827
|
+
track: LocalTrack | MediaStreamTrack,
|
|
828
|
+
videoCodec: BackupVideoCodec,
|
|
829
|
+
options?: TrackPublishOptions,
|
|
830
|
+
) {
|
|
831
|
+
// is it not published? if so skip
|
|
832
|
+
let existingPublication: LocalTrackPublication | undefined;
|
|
833
|
+
this.tracks.forEach((publication) => {
|
|
834
|
+
if (!publication.track) {
|
|
835
|
+
return;
|
|
836
|
+
}
|
|
837
|
+
if (publication.track === track) {
|
|
838
|
+
existingPublication = <LocalTrackPublication>publication;
|
|
839
|
+
}
|
|
840
|
+
});
|
|
841
|
+
if (!existingPublication) {
|
|
842
|
+
throw new TrackInvalidError('track is not published');
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
if (!(track instanceof LocalVideoTrack)) {
|
|
846
|
+
throw new TrackInvalidError('track is not a video track');
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
const opts: TrackPublishOptions = {
|
|
850
|
+
...this.roomOptions?.publishDefaults,
|
|
851
|
+
...options,
|
|
852
|
+
};
|
|
853
|
+
|
|
854
|
+
const encodings = computeTrackBackupEncodings(track, videoCodec, opts);
|
|
855
|
+
if (!encodings) {
|
|
856
|
+
log.info(
|
|
857
|
+
`backup codec has been disabled, ignoring request to add additional codec for track`,
|
|
858
|
+
);
|
|
859
|
+
return;
|
|
860
|
+
}
|
|
861
|
+
const simulcastTrack = track.addSimulcastTrack(videoCodec, encodings);
|
|
862
|
+
const req = new AddTrackRequest({
|
|
863
|
+
cid: simulcastTrack.mediaStreamTrack.id,
|
|
864
|
+
type: Track.kindToProto(track.kind),
|
|
865
|
+
muted: track.isMuted,
|
|
866
|
+
source: Track.sourceToProto(track.source),
|
|
867
|
+
sid: track.sid,
|
|
868
|
+
simulcastCodecs: [
|
|
869
|
+
{
|
|
870
|
+
codec: opts.videoCodec,
|
|
871
|
+
cid: simulcastTrack.mediaStreamTrack.id,
|
|
872
|
+
enableSimulcastLayers: opts.simulcast,
|
|
873
|
+
},
|
|
874
|
+
],
|
|
875
|
+
});
|
|
876
|
+
req.layers = videoLayersFromEncodings(req.width, req.height, encodings);
|
|
877
|
+
|
|
878
|
+
if (!this.engine || this.engine.isClosed) {
|
|
879
|
+
throw new UnexpectedConnectionState('cannot publish track when not connected');
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
const ti = await this.engine.addTrack(req);
|
|
883
|
+
|
|
884
|
+
const transceiverInit: RTCRtpTransceiverInit = { direction: 'sendonly' };
|
|
885
|
+
if (encodings) {
|
|
886
|
+
transceiverInit.sendEncodings = encodings;
|
|
887
|
+
}
|
|
888
|
+
await this.engine.createSimulcastSender(track, simulcastTrack, opts, encodings);
|
|
889
|
+
|
|
890
|
+
await this.engine.negotiate();
|
|
891
|
+
log.debug(`published ${videoCodec} for track ${track.sid}`, { encodings, trackInfo: ti });
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
async unpublishTrack(
|
|
895
|
+
track: LocalTrack | MediaStreamTrack,
|
|
896
|
+
stopOnUnpublish?: boolean,
|
|
897
|
+
): Promise<LocalTrackPublication | undefined> {
|
|
898
|
+
// look through all published tracks to find the right ones
|
|
899
|
+
const publication = this.getPublicationForTrack(track);
|
|
900
|
+
|
|
901
|
+
log.debug('unpublishing track', { track, method: 'unpublishTrack' });
|
|
902
|
+
|
|
903
|
+
if (!publication || !publication.track) {
|
|
904
|
+
log.warn('track was not unpublished because no publication was found', {
|
|
905
|
+
track,
|
|
906
|
+
method: 'unpublishTrack',
|
|
907
|
+
});
|
|
908
|
+
return undefined;
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
track = publication.track;
|
|
912
|
+
track.off(TrackEvent.Muted, this.onTrackMuted);
|
|
913
|
+
track.off(TrackEvent.Unmuted, this.onTrackUnmuted);
|
|
914
|
+
track.off(TrackEvent.Ended, this.handleTrackEnded);
|
|
915
|
+
track.off(TrackEvent.UpstreamPaused, this.onTrackUpstreamPaused);
|
|
916
|
+
track.off(TrackEvent.UpstreamResumed, this.onTrackUpstreamResumed);
|
|
917
|
+
|
|
918
|
+
if (stopOnUnpublish === undefined) {
|
|
919
|
+
stopOnUnpublish = this.roomOptions?.stopLocalTrackOnUnpublish ?? true;
|
|
920
|
+
}
|
|
921
|
+
if (stopOnUnpublish) {
|
|
922
|
+
track.stop();
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
let negotiationNeeded = false;
|
|
926
|
+
const trackSender = track.sender;
|
|
927
|
+
track.sender = undefined;
|
|
928
|
+
if (
|
|
929
|
+
this.engine.publisher &&
|
|
930
|
+
this.engine.publisher.pc.connectionState !== 'closed' &&
|
|
931
|
+
trackSender
|
|
932
|
+
) {
|
|
933
|
+
try {
|
|
934
|
+
for (const transceiver of this.engine.publisher.pc.getTransceivers()) {
|
|
935
|
+
// if sender is not currently sending (after replaceTrack(null))
|
|
936
|
+
// removeTrack would have no effect.
|
|
937
|
+
// to ensure we end up successfully removing the track, manually set
|
|
938
|
+
// the transceiver to inactive
|
|
939
|
+
if (transceiver.sender === trackSender) {
|
|
940
|
+
transceiver.direction = 'inactive';
|
|
941
|
+
negotiationNeeded = true;
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
if (this.engine.removeTrack(trackSender)) {
|
|
945
|
+
negotiationNeeded = true;
|
|
946
|
+
}
|
|
947
|
+
if (track instanceof LocalVideoTrack) {
|
|
948
|
+
for (const [, trackInfo] of track.simulcastCodecs) {
|
|
949
|
+
if (trackInfo.sender) {
|
|
950
|
+
if (this.engine.removeTrack(trackInfo.sender)) {
|
|
951
|
+
negotiationNeeded = true;
|
|
952
|
+
}
|
|
953
|
+
trackInfo.sender = undefined;
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
track.simulcastCodecs.clear();
|
|
957
|
+
}
|
|
958
|
+
} catch (e) {
|
|
959
|
+
log.warn('failed to unpublish track', { error: e, method: 'unpublishTrack' });
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
// remove from our maps
|
|
964
|
+
this.tracks.delete(publication.trackSid);
|
|
965
|
+
switch (publication.kind) {
|
|
966
|
+
case Track.Kind.Audio:
|
|
967
|
+
this.audioTracks.delete(publication.trackSid);
|
|
968
|
+
break;
|
|
969
|
+
case Track.Kind.Video:
|
|
970
|
+
this.videoTracks.delete(publication.trackSid);
|
|
971
|
+
break;
|
|
972
|
+
default:
|
|
973
|
+
break;
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
this.emit(ParticipantEvent.LocalTrackUnpublished, publication);
|
|
977
|
+
publication.setTrack(undefined);
|
|
978
|
+
|
|
979
|
+
if (negotiationNeeded) {
|
|
980
|
+
await this.engine.negotiate();
|
|
981
|
+
}
|
|
982
|
+
return publication;
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
async unpublishTracks(
|
|
986
|
+
tracks: LocalTrack[] | MediaStreamTrack[],
|
|
987
|
+
): Promise<LocalTrackPublication[]> {
|
|
988
|
+
const results = await Promise.all(tracks.map((track) => this.unpublishTrack(track)));
|
|
989
|
+
return results.filter(
|
|
990
|
+
(track) => track instanceof LocalTrackPublication,
|
|
991
|
+
) as LocalTrackPublication[];
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
async republishAllTracks(options?: TrackPublishOptions, restartTracks: boolean = true) {
|
|
995
|
+
const localPubs: LocalTrackPublication[] = [];
|
|
996
|
+
this.tracks.forEach((pub) => {
|
|
997
|
+
if (pub.track) {
|
|
998
|
+
if (options) {
|
|
999
|
+
pub.options = { ...pub.options, ...options };
|
|
1000
|
+
}
|
|
1001
|
+
localPubs.push(pub);
|
|
1002
|
+
}
|
|
1003
|
+
});
|
|
1004
|
+
|
|
1005
|
+
await Promise.all(
|
|
1006
|
+
localPubs.map(async (pub) => {
|
|
1007
|
+
const track = pub.track!;
|
|
1008
|
+
await this.unpublishTrack(track, false);
|
|
1009
|
+
if (
|
|
1010
|
+
restartTracks &&
|
|
1011
|
+
!track.isMuted &&
|
|
1012
|
+
(track instanceof LocalAudioTrack || track instanceof LocalVideoTrack) &&
|
|
1013
|
+
!track.isUserProvided
|
|
1014
|
+
) {
|
|
1015
|
+
// generally we need to restart the track before publishing, often a full reconnect
|
|
1016
|
+
// is necessary because computer had gone to sleep.
|
|
1017
|
+
log.debug('restarting existing track', {
|
|
1018
|
+
track: pub.trackSid,
|
|
1019
|
+
});
|
|
1020
|
+
await track.restartTrack();
|
|
1021
|
+
}
|
|
1022
|
+
await this.publishTrack(track, pub.options);
|
|
1023
|
+
}),
|
|
1024
|
+
);
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
/**
|
|
1028
|
+
* Publish a new data payload to the room. Data will be forwarded to each
|
|
1029
|
+
* participant in the room if the destination field in publishOptions is empty
|
|
1030
|
+
*
|
|
1031
|
+
* @param data Uint8Array of the payload. To send string data, use TextEncoder.encode
|
|
1032
|
+
* @param kind whether to send this as reliable or lossy.
|
|
1033
|
+
* For data that you need delivery guarantee (such as chat messages), use Reliable.
|
|
1034
|
+
* For data that should arrive as quickly as possible, but you are ok with dropped
|
|
1035
|
+
* packets, use Lossy.
|
|
1036
|
+
* @param publishOptions optionally specify a `topic` and `destination`
|
|
1037
|
+
*/
|
|
1038
|
+
async publishData(
|
|
1039
|
+
data: Uint8Array,
|
|
1040
|
+
kind: DataPacket_Kind,
|
|
1041
|
+
publishOptions?: DataPublishOptions,
|
|
1042
|
+
): Promise<void>;
|
|
1043
|
+
/**
|
|
1044
|
+
* Publish a new data payload to the room. Data will be forwarded to each
|
|
1045
|
+
* participant in the room if the destination argument is empty
|
|
1046
|
+
*
|
|
1047
|
+
* @param data Uint8Array of the payload. To send string data, use TextEncoder.encode
|
|
1048
|
+
* @param kind whether to send this as reliable or lossy.
|
|
1049
|
+
* For data that you need delivery guarantee (such as chat messages), use Reliable.
|
|
1050
|
+
* For data that should arrive as quickly as possible, but you are ok with dropped
|
|
1051
|
+
* packets, use Lossy.
|
|
1052
|
+
* @param destination the participants who will receive the message
|
|
1053
|
+
*/
|
|
1054
|
+
async publishData(
|
|
1055
|
+
data: Uint8Array,
|
|
1056
|
+
kind: DataPacket_Kind,
|
|
1057
|
+
destination?: RemoteParticipant[] | string[],
|
|
1058
|
+
): Promise<void>;
|
|
1059
|
+
|
|
1060
|
+
async publishData(
|
|
1061
|
+
data: Uint8Array,
|
|
1062
|
+
kind: DataPacket_Kind,
|
|
1063
|
+
publishOptions: DataPublishOptions | RemoteParticipant[] | string[] = {},
|
|
1064
|
+
) {
|
|
1065
|
+
const destination = Array.isArray(publishOptions)
|
|
1066
|
+
? publishOptions
|
|
1067
|
+
: publishOptions?.destination;
|
|
1068
|
+
const destinationSids: string[] = [];
|
|
1069
|
+
|
|
1070
|
+
const topic = !Array.isArray(publishOptions) ? publishOptions.topic : undefined;
|
|
1071
|
+
|
|
1072
|
+
if (destination !== undefined) {
|
|
1073
|
+
destination.forEach((val: any) => {
|
|
1074
|
+
if (val instanceof RemoteParticipant) {
|
|
1075
|
+
destinationSids.push(val.sid);
|
|
1076
|
+
} else {
|
|
1077
|
+
destinationSids.push(val);
|
|
1078
|
+
}
|
|
1079
|
+
});
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
|
+
const packet = new DataPacket({
|
|
1083
|
+
kind,
|
|
1084
|
+
value: {
|
|
1085
|
+
case: 'user',
|
|
1086
|
+
value: new UserPacket({
|
|
1087
|
+
participantSid: this.sid,
|
|
1088
|
+
payload: data,
|
|
1089
|
+
destinationSids: destinationSids,
|
|
1090
|
+
topic,
|
|
1091
|
+
}),
|
|
1092
|
+
},
|
|
1093
|
+
});
|
|
1094
|
+
|
|
1095
|
+
await this.engine.sendDataPacket(packet, kind);
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
/**
|
|
1099
|
+
* Control who can subscribe to LocalParticipant's published tracks.
|
|
1100
|
+
*
|
|
1101
|
+
* By default, all participants can subscribe. This allows fine-grained control over
|
|
1102
|
+
* who is able to subscribe at a participant and track level.
|
|
1103
|
+
*
|
|
1104
|
+
* Note: if access is given at a track-level (i.e. both [allParticipantsAllowed] and
|
|
1105
|
+
* [ParticipantTrackPermission.allTracksAllowed] are false), any newer published tracks
|
|
1106
|
+
* will not grant permissions to any participants and will require a subsequent
|
|
1107
|
+
* permissions update to allow subscription.
|
|
1108
|
+
*
|
|
1109
|
+
* @param allParticipantsAllowed Allows all participants to subscribe all tracks.
|
|
1110
|
+
* Takes precedence over [[participantTrackPermissions]] if set to true.
|
|
1111
|
+
* By default this is set to true.
|
|
1112
|
+
* @param participantTrackPermissions Full list of individual permissions per
|
|
1113
|
+
* participant/track. Any omitted participants will not receive any permissions.
|
|
1114
|
+
*/
|
|
1115
|
+
setTrackSubscriptionPermissions(
|
|
1116
|
+
allParticipantsAllowed: boolean,
|
|
1117
|
+
participantTrackPermissions: ParticipantTrackPermission[] = [],
|
|
1118
|
+
) {
|
|
1119
|
+
this.participantTrackPermissions = participantTrackPermissions;
|
|
1120
|
+
this.allParticipantsAllowedToSubscribe = allParticipantsAllowed;
|
|
1121
|
+
if (this.engine.client.isConnected) {
|
|
1122
|
+
this.updateTrackSubscriptionPermissions();
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
|
|
1126
|
+
/** @internal */
|
|
1127
|
+
updateInfo(info: ParticipantInfo): boolean {
|
|
1128
|
+
if (info.sid !== this.sid) {
|
|
1129
|
+
// drop updates that specify a wrong sid.
|
|
1130
|
+
// the sid for local participant is only explicitly set on join and full reconnect
|
|
1131
|
+
return false;
|
|
1132
|
+
}
|
|
1133
|
+
if (!super.updateInfo(info)) {
|
|
1134
|
+
return false;
|
|
1135
|
+
}
|
|
1136
|
+
|
|
1137
|
+
// reconcile track mute status.
|
|
1138
|
+
// if server's track mute status doesn't match actual, we'll have to update
|
|
1139
|
+
// the server's copy
|
|
1140
|
+
info.tracks.forEach((ti) => {
|
|
1141
|
+
const pub = this.tracks.get(ti.sid);
|
|
1142
|
+
|
|
1143
|
+
if (pub) {
|
|
1144
|
+
const mutedOnServer = pub.isMuted || (pub.track?.isUpstreamPaused ?? false);
|
|
1145
|
+
if (mutedOnServer !== ti.muted) {
|
|
1146
|
+
log.debug('updating server mute state after reconcile', {
|
|
1147
|
+
sid: ti.sid,
|
|
1148
|
+
muted: mutedOnServer,
|
|
1149
|
+
});
|
|
1150
|
+
this.engine.client.sendMuteTrack(ti.sid, mutedOnServer);
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
});
|
|
1154
|
+
return true;
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
private updateTrackSubscriptionPermissions = () => {
|
|
1158
|
+
log.debug('updating track subscription permissions', {
|
|
1159
|
+
allParticipantsAllowed: this.allParticipantsAllowedToSubscribe,
|
|
1160
|
+
participantTrackPermissions: this.participantTrackPermissions,
|
|
1161
|
+
});
|
|
1162
|
+
this.engine.client.sendUpdateSubscriptionPermissions(
|
|
1163
|
+
this.allParticipantsAllowedToSubscribe,
|
|
1164
|
+
this.participantTrackPermissions.map((p) => trackPermissionToProto(p)),
|
|
1165
|
+
);
|
|
1166
|
+
};
|
|
1167
|
+
|
|
1168
|
+
/** @internal */
|
|
1169
|
+
private onTrackUnmuted = (track: LocalTrack) => {
|
|
1170
|
+
this.onTrackMuted(track, track.isUpstreamPaused);
|
|
1171
|
+
};
|
|
1172
|
+
|
|
1173
|
+
// when the local track changes in mute status, we'll notify server as such
|
|
1174
|
+
/** @internal */
|
|
1175
|
+
private onTrackMuted = (track: LocalTrack, muted?: boolean) => {
|
|
1176
|
+
if (muted === undefined) {
|
|
1177
|
+
muted = true;
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1180
|
+
if (!track.sid) {
|
|
1181
|
+
log.error('could not update mute status for unpublished track', track);
|
|
1182
|
+
return;
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
this.engine.updateMuteStatus(track.sid, muted);
|
|
1186
|
+
};
|
|
1187
|
+
|
|
1188
|
+
private onTrackUpstreamPaused = (track: LocalTrack) => {
|
|
1189
|
+
log.debug('upstream paused');
|
|
1190
|
+
this.onTrackMuted(track, true);
|
|
1191
|
+
};
|
|
1192
|
+
|
|
1193
|
+
private onTrackUpstreamResumed = (track: LocalTrack) => {
|
|
1194
|
+
log.debug('upstream resumed');
|
|
1195
|
+
this.onTrackMuted(track, track.isMuted);
|
|
1196
|
+
};
|
|
1197
|
+
|
|
1198
|
+
private handleSubscribedQualityUpdate = async (update: SubscribedQualityUpdate) => {
|
|
1199
|
+
if (!this.roomOptions?.dynacast) {
|
|
1200
|
+
return;
|
|
1201
|
+
}
|
|
1202
|
+
const pub = this.videoTracks.get(update.trackSid);
|
|
1203
|
+
if (!pub) {
|
|
1204
|
+
log.warn('received subscribed quality update for unknown track', {
|
|
1205
|
+
method: 'handleSubscribedQualityUpdate',
|
|
1206
|
+
sid: update.trackSid,
|
|
1207
|
+
});
|
|
1208
|
+
return;
|
|
1209
|
+
}
|
|
1210
|
+
if (update.subscribedCodecs.length > 0) {
|
|
1211
|
+
if (!pub.videoTrack) {
|
|
1212
|
+
return;
|
|
1213
|
+
}
|
|
1214
|
+
const newCodecs = await pub.videoTrack.setPublishingCodecs(update.subscribedCodecs);
|
|
1215
|
+
for await (const codec of newCodecs) {
|
|
1216
|
+
if (isBackupCodec(codec)) {
|
|
1217
|
+
log.debug(`publish ${codec} for ${pub.videoTrack.sid}`);
|
|
1218
|
+
await this.publishAdditionalCodecForTrack(pub.videoTrack, codec, pub.options);
|
|
1219
|
+
}
|
|
1220
|
+
}
|
|
1221
|
+
} else if (update.subscribedQualities.length > 0) {
|
|
1222
|
+
await pub.videoTrack?.setPublishingLayers(update.subscribedQualities);
|
|
1223
|
+
}
|
|
1224
|
+
};
|
|
1225
|
+
|
|
1226
|
+
private handleLocalTrackUnpublished = (unpublished: TrackUnpublishedResponse) => {
|
|
1227
|
+
const track = this.tracks.get(unpublished.trackSid);
|
|
1228
|
+
if (!track) {
|
|
1229
|
+
log.warn('received unpublished event for unknown track', {
|
|
1230
|
+
method: 'handleLocalTrackUnpublished',
|
|
1231
|
+
trackSid: unpublished.trackSid,
|
|
1232
|
+
});
|
|
1233
|
+
return;
|
|
1234
|
+
}
|
|
1235
|
+
this.unpublishTrack(track.track!);
|
|
1236
|
+
};
|
|
1237
|
+
|
|
1238
|
+
private handleTrackEnded = async (track: LocalTrack) => {
|
|
1239
|
+
if (
|
|
1240
|
+
track.source === Track.Source.ScreenShare ||
|
|
1241
|
+
track.source === Track.Source.ScreenShareAudio
|
|
1242
|
+
) {
|
|
1243
|
+
log.debug('unpublishing local track due to TrackEnded', {
|
|
1244
|
+
track: track.sid,
|
|
1245
|
+
});
|
|
1246
|
+
this.unpublishTrack(track);
|
|
1247
|
+
} else if (track.isUserProvided) {
|
|
1248
|
+
await track.mute();
|
|
1249
|
+
} else if (track instanceof LocalAudioTrack || track instanceof LocalVideoTrack) {
|
|
1250
|
+
try {
|
|
1251
|
+
if (isWeb()) {
|
|
1252
|
+
try {
|
|
1253
|
+
const currentPermissions = await navigator?.permissions.query({
|
|
1254
|
+
// the permission query for camera and microphone currently not supported in Safari and Firefox
|
|
1255
|
+
// @ts-ignore
|
|
1256
|
+
name: track.source === Track.Source.Camera ? 'camera' : 'microphone',
|
|
1257
|
+
});
|
|
1258
|
+
if (currentPermissions && currentPermissions.state === 'denied') {
|
|
1259
|
+
log.warn(`user has revoked access to ${track.source}`);
|
|
1260
|
+
|
|
1261
|
+
// detect granted change after permissions were denied to try and resume then
|
|
1262
|
+
currentPermissions.onchange = () => {
|
|
1263
|
+
if (currentPermissions.state !== 'denied') {
|
|
1264
|
+
if (!track.isMuted) {
|
|
1265
|
+
track.restartTrack();
|
|
1266
|
+
}
|
|
1267
|
+
currentPermissions.onchange = null;
|
|
1268
|
+
}
|
|
1269
|
+
};
|
|
1270
|
+
throw new Error('GetUserMedia Permission denied');
|
|
1271
|
+
}
|
|
1272
|
+
} catch (e: any) {
|
|
1273
|
+
// permissions query fails for firefox, we continue and try to restart the track
|
|
1274
|
+
}
|
|
1275
|
+
}
|
|
1276
|
+
if (!track.isMuted) {
|
|
1277
|
+
log.debug('track ended, attempting to use a different device');
|
|
1278
|
+
await track.restartTrack();
|
|
1279
|
+
}
|
|
1280
|
+
} catch (e) {
|
|
1281
|
+
log.warn(`could not restart track, muting instead`);
|
|
1282
|
+
await track.mute();
|
|
1283
|
+
}
|
|
1284
|
+
}
|
|
1285
|
+
};
|
|
1286
|
+
|
|
1287
|
+
private getPublicationForTrack(
|
|
1288
|
+
track: LocalTrack | MediaStreamTrack,
|
|
1289
|
+
): LocalTrackPublication | undefined {
|
|
1290
|
+
let publication: LocalTrackPublication | undefined;
|
|
1291
|
+
this.tracks.forEach((pub) => {
|
|
1292
|
+
const localTrack = pub.track;
|
|
1293
|
+
if (!localTrack) {
|
|
1294
|
+
return;
|
|
1295
|
+
}
|
|
1296
|
+
|
|
1297
|
+
// this looks overly complicated due to this object tree
|
|
1298
|
+
if (track instanceof MediaStreamTrack) {
|
|
1299
|
+
if (localTrack instanceof LocalAudioTrack || localTrack instanceof LocalVideoTrack) {
|
|
1300
|
+
if (localTrack.mediaStreamTrack === track) {
|
|
1301
|
+
publication = <LocalTrackPublication>pub;
|
|
1302
|
+
}
|
|
1303
|
+
}
|
|
1304
|
+
} else if (track === localTrack) {
|
|
1305
|
+
publication = <LocalTrackPublication>pub;
|
|
1306
|
+
}
|
|
1307
|
+
});
|
|
1308
|
+
return publication;
|
|
1309
|
+
}
|
|
1310
|
+
|
|
1311
|
+
/** @internal */
|
|
1312
|
+
publishedTracksInfo(): TrackPublishedResponse[] {
|
|
1313
|
+
const infos: TrackPublishedResponse[] = [];
|
|
1314
|
+
this.tracks.forEach((track: LocalTrackPublication) => {
|
|
1315
|
+
if (track.track !== undefined) {
|
|
1316
|
+
infos.push(
|
|
1317
|
+
new TrackPublishedResponse({
|
|
1318
|
+
cid: track.track.mediaStreamID,
|
|
1319
|
+
track: track.trackInfo,
|
|
1320
|
+
}),
|
|
1321
|
+
);
|
|
1322
|
+
}
|
|
1323
|
+
});
|
|
1324
|
+
return infos;
|
|
1325
|
+
}
|
|
1326
|
+
|
|
1327
|
+
/** @internal */
|
|
1328
|
+
dataChannelsInfo(): DataChannelInfo[] {
|
|
1329
|
+
const infos: DataChannelInfo[] = [];
|
|
1330
|
+
const getInfo = (dc: RTCDataChannel | undefined, target: SignalTarget) => {
|
|
1331
|
+
if (dc?.id !== undefined && dc.id !== null) {
|
|
1332
|
+
infos.push(
|
|
1333
|
+
new DataChannelInfo({
|
|
1334
|
+
label: dc.label,
|
|
1335
|
+
id: dc.id,
|
|
1336
|
+
target,
|
|
1337
|
+
}),
|
|
1338
|
+
);
|
|
1339
|
+
}
|
|
1340
|
+
};
|
|
1341
|
+
getInfo(this.engine.dataChannelForKind(DataPacket_Kind.LOSSY), SignalTarget.PUBLISHER);
|
|
1342
|
+
getInfo(this.engine.dataChannelForKind(DataPacket_Kind.RELIABLE), SignalTarget.PUBLISHER);
|
|
1343
|
+
getInfo(this.engine.dataChannelForKind(DataPacket_Kind.LOSSY, true), SignalTarget.SUBSCRIBER);
|
|
1344
|
+
getInfo(
|
|
1345
|
+
this.engine.dataChannelForKind(DataPacket_Kind.RELIABLE, true),
|
|
1346
|
+
SignalTarget.SUBSCRIBER,
|
|
1347
|
+
);
|
|
1348
|
+
return infos;
|
|
1349
|
+
}
|
|
1350
|
+
}
|