@fishjam-cloud/webrtc-client 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/LICENSE.txt +142 -0
  2. package/README.md +30 -0
  3. package/dist/index.d.mts +761 -0
  4. package/dist/index.mjs +4952 -0
  5. package/dist/protobufs/fishjam/media_events/peer/peer.d.ts +125 -0
  6. package/dist/protobufs/fishjam/media_events/peer/peer.js +1214 -0
  7. package/dist/protobufs/fishjam/media_events/server/server.d.ts +197 -0
  8. package/dist/protobufs/fishjam/media_events/server/server.js +2121 -0
  9. package/dist/protobufs/fishjam/media_events/shared.d.ts +38 -0
  10. package/dist/protobufs/fishjam/media_events/shared.js +152 -0
  11. package/dist/webrtc-client/src/CommandsQueue.d.ts +23 -0
  12. package/dist/webrtc-client/src/CommandsQueue.js +87 -0
  13. package/dist/webrtc-client/src/ConnectionManager.d.ts +16 -0
  14. package/dist/webrtc-client/src/ConnectionManager.js +46 -0
  15. package/dist/webrtc-client/src/bitrate.d.ts +12 -0
  16. package/dist/webrtc-client/src/bitrate.js +15 -0
  17. package/dist/webrtc-client/src/deferred.d.ts +8 -0
  18. package/dist/webrtc-client/src/deferred.js +17 -0
  19. package/dist/webrtc-client/src/index.d.ts +5 -0
  20. package/dist/webrtc-client/src/index.js +3 -0
  21. package/dist/webrtc-client/src/internal.d.ts +27 -0
  22. package/dist/webrtc-client/src/internal.js +18 -0
  23. package/dist/webrtc-client/src/mediaEvent.d.ts +13 -0
  24. package/dist/webrtc-client/src/mediaEvent.js +20 -0
  25. package/dist/webrtc-client/src/tracks/Local.d.ts +49 -0
  26. package/dist/webrtc-client/src/tracks/Local.js +243 -0
  27. package/dist/webrtc-client/src/tracks/LocalTrack.d.ts +61 -0
  28. package/dist/webrtc-client/src/tracks/LocalTrack.js +212 -0
  29. package/dist/webrtc-client/src/tracks/LocalTrackManager.d.ts +39 -0
  30. package/dist/webrtc-client/src/tracks/LocalTrackManager.js +76 -0
  31. package/dist/webrtc-client/src/tracks/Remote.d.ts +32 -0
  32. package/dist/webrtc-client/src/tracks/Remote.js +165 -0
  33. package/dist/webrtc-client/src/tracks/RemoteTrack.d.ts +40 -0
  34. package/dist/webrtc-client/src/tracks/RemoteTrack.js +71 -0
  35. package/dist/webrtc-client/src/tracks/TrackCommon.d.ts +9 -0
  36. package/dist/webrtc-client/src/tracks/TrackCommon.js +1 -0
  37. package/dist/webrtc-client/src/tracks/bandwidth.d.ts +1 -0
  38. package/dist/webrtc-client/src/tracks/bandwidth.js +27 -0
  39. package/dist/webrtc-client/src/tracks/encodings.d.ts +3 -0
  40. package/dist/webrtc-client/src/tracks/encodings.js +11 -0
  41. package/dist/webrtc-client/src/tracks/muteTrackUtils.d.ts +3 -0
  42. package/dist/webrtc-client/src/tracks/muteTrackUtils.js +18 -0
  43. package/dist/webrtc-client/src/tracks/transceivers.d.ts +2 -0
  44. package/dist/webrtc-client/src/tracks/transceivers.js +93 -0
  45. package/dist/webrtc-client/src/types.d.ts +259 -0
  46. package/dist/webrtc-client/src/types.js +1 -0
  47. package/dist/webrtc-client/src/webRTCEndpoint.d.ts +298 -0
  48. package/dist/webrtc-client/src/webRTCEndpoint.js +659 -0
  49. package/dist/webrtc-client/tests/events/bandwidthEstimationEvent.test.d.ts +1 -0
  50. package/dist/webrtc-client/tests/events/bandwidthEstimationEvent.test.js +21 -0
  51. package/dist/webrtc-client/tests/events/connectedEvent.test.d.ts +1 -0
  52. package/dist/webrtc-client/tests/events/connectedEvent.test.js +70 -0
  53. package/dist/webrtc-client/tests/events/encodingSwitchedEvent.test.d.ts +1 -0
  54. package/dist/webrtc-client/tests/events/encodingSwitchedEvent.test.js +60 -0
  55. package/dist/webrtc-client/tests/events/endpointAddedEvent.test.d.ts +1 -0
  56. package/dist/webrtc-client/tests/events/endpointAddedEvent.test.js +57 -0
  57. package/dist/webrtc-client/tests/events/endpointRemovedEvent.test.d.ts +1 -0
  58. package/dist/webrtc-client/tests/events/endpointRemovedEvent.test.js +64 -0
  59. package/dist/webrtc-client/tests/events/endpointUpdatedEvent.test.d.ts +1 -0
  60. package/dist/webrtc-client/tests/events/endpointUpdatedEvent.test.js +78 -0
  61. package/dist/webrtc-client/tests/events/trackAddedEvent.test.d.ts +1 -0
  62. package/dist/webrtc-client/tests/events/trackAddedEvent.test.js +84 -0
  63. package/dist/webrtc-client/tests/events/trackRemovedEvent.test.d.ts +1 -0
  64. package/dist/webrtc-client/tests/events/trackRemovedEvent.test.js +26 -0
  65. package/dist/webrtc-client/tests/events/trackUpdatedEvent.test.d.ts +1 -0
  66. package/dist/webrtc-client/tests/events/trackUpdatedEvent.test.js +77 -0
  67. package/dist/webrtc-client/tests/events/vadNotificationEvent.test.d.ts +1 -0
  68. package/dist/webrtc-client/tests/events/vadNotificationEvent.test.js +42 -0
  69. package/dist/webrtc-client/tests/fixtures.d.ts +27 -0
  70. package/dist/webrtc-client/tests/fixtures.js +172 -0
  71. package/dist/webrtc-client/tests/methods/addTrackMethod.test.d.ts +1 -0
  72. package/dist/webrtc-client/tests/methods/addTrackMethod.test.js +46 -0
  73. package/dist/webrtc-client/tests/methods/cleanUpMethod.test.d.ts +1 -0
  74. package/dist/webrtc-client/tests/methods/cleanUpMethod.test.js +14 -0
  75. package/dist/webrtc-client/tests/methods/connectMethod.test.d.ts +1 -0
  76. package/dist/webrtc-client/tests/methods/connectMethod.test.js +40 -0
  77. package/dist/webrtc-client/tests/methods/disconnectMethod.test.d.ts +1 -0
  78. package/dist/webrtc-client/tests/methods/disconnectMethod.test.js +29 -0
  79. package/dist/webrtc-client/tests/mocks.d.ts +5 -0
  80. package/dist/webrtc-client/tests/mocks.js +80 -0
  81. package/dist/webrtc-client/tests/utils.d.ts +3 -0
  82. package/dist/webrtc-client/tests/utils.js +56 -0
  83. package/package.json +80 -0
@@ -0,0 +1,761 @@
1
+ import { BinaryWriter, BinaryReader } from '@bufbuild/protobuf/wire';
2
+ import TypedEmitter from 'typed-emitter';
3
+
4
+ declare enum Variant {
5
+ VARIANT_UNSPECIFIED = 0,
6
+ VARIANT_LOW = 1,
7
+ VARIANT_MEDIUM = 2,
8
+ VARIANT_HIGH = 3,
9
+ UNRECOGNIZED = -1
10
+ }
11
+ /** Contains information about an ICE candidate which will be sent to the peer/server */
12
+ interface Candidate {
13
+ candidate: string;
14
+ sdpMLineIndex: number;
15
+ sdpMid: string;
16
+ usernameFragment: string;
17
+ }
18
+ declare const Candidate: MessageFns$2<Candidate>;
19
+ type Builtin$2 = Date | Function | Uint8Array | string | number | boolean | undefined;
20
+ type DeepPartial$2<T> = T extends Builtin$2 ? T : T extends globalThis.Array<infer U> ? globalThis.Array<DeepPartial$2<U>> : T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial$2<U>> : T extends {} ? {
21
+ [K in keyof T]?: DeepPartial$2<T[K]>;
22
+ } : Partial<T>;
23
+ type KeysOfUnion$2<T> = T extends T ? keyof T : never;
24
+ type Exact$2<P, I extends P> = P extends Builtin$2 ? P : P & {
25
+ [K in keyof P]: Exact$2<P[K], I[K]>;
26
+ } & {
27
+ [K in Exclude<keyof I, KeysOfUnion$2<P>>]: never;
28
+ };
29
+ interface MessageFns$2<T> {
30
+ encode(message: T, writer?: BinaryWriter): BinaryWriter;
31
+ decode(input: BinaryReader | Uint8Array, length?: number): T;
32
+ fromJSON(object: any): T;
33
+ toJSON(message: T): unknown;
34
+ create<I extends Exact$2<DeepPartial$2<T>, I>>(base?: I): T;
35
+ fromPartial<I extends Exact$2<DeepPartial$2<T>, I>>(object: I): T;
36
+ }
37
+
38
+ /** Defines any type of message sent from Peer to Membrane RTC Engine */
39
+ interface MediaEvent$1 {
40
+ connect?: MediaEvent_Connect | undefined;
41
+ disconnect?: MediaEvent_Disconnect | undefined;
42
+ updateEndpointMetadata?: MediaEvent_UpdateEndpointMetadata | undefined;
43
+ updateTrackMetadata?: MediaEvent_UpdateTrackMetadata | undefined;
44
+ renegotiateTracks?: MediaEvent_RenegotiateTracks | undefined;
45
+ candidate?: Candidate | undefined;
46
+ sdpOffer?: MediaEvent_SdpOffer | undefined;
47
+ trackBitrates?: MediaEvent_TrackBitrates | undefined;
48
+ enableTrackVariant?: MediaEvent_EnableTrackVariant | undefined;
49
+ disableTrackVariant?: MediaEvent_DisableTrackVariant | undefined;
50
+ setTargetTrackVariant?: MediaEvent_SetTargetTrackVariant | undefined;
51
+ }
52
+ declare const MediaEvent$1: MessageFns$1<MediaEvent$1>;
53
+ interface MediaEvent_VariantBitrate {
54
+ variant: Variant;
55
+ bitrate: number;
56
+ }
57
+ declare const MediaEvent_VariantBitrate: MessageFns$1<MediaEvent_VariantBitrate>;
58
+ /** Sent when a peer wants to join WebRTC Endpoint. */
59
+ interface MediaEvent_Connect {
60
+ metadataJson: string;
61
+ }
62
+ declare const MediaEvent_Connect: MessageFns$1<MediaEvent_Connect>;
63
+ /** Sent when a peer disconnects from WebRTC Endpoint. */
64
+ interface MediaEvent_Disconnect {
65
+ }
66
+ declare const MediaEvent_Disconnect: MessageFns$1<MediaEvent_Disconnect>;
67
+ /** Sent when a peer wants to update its metadata */
68
+ interface MediaEvent_UpdateEndpointMetadata {
69
+ metadataJson: string;
70
+ }
71
+ declare const MediaEvent_UpdateEndpointMetadata: MessageFns$1<MediaEvent_UpdateEndpointMetadata>;
72
+ /** Sent when a peer wants to update its track's metadata */
73
+ interface MediaEvent_UpdateTrackMetadata {
74
+ trackId: string;
75
+ metadataJson: string;
76
+ }
77
+ declare const MediaEvent_UpdateTrackMetadata: MessageFns$1<MediaEvent_UpdateTrackMetadata>;
78
+ /** Sent when peer wants to renegatiate connection due to adding a track or removing a track */
79
+ interface MediaEvent_RenegotiateTracks {
80
+ }
81
+ declare const MediaEvent_RenegotiateTracks: MessageFns$1<MediaEvent_RenegotiateTracks>;
82
+ /**
83
+ * Sent as a response to `offerData` media event during renegotiation
84
+ * Maps contain only information about current peer's `sendonly` tracks.
85
+ * The "mid" is an identifier used to associate an RTP packet with an MLine from the SDP offer/answer.
86
+ */
87
+ interface MediaEvent_SdpOffer {
88
+ /** The value of the `sessionDescription.sdp` */
89
+ sdp: string;
90
+ trackIdToMetadataJson: {
91
+ [key: string]: string;
92
+ };
93
+ /** Maps track_id to its bitrate. The track_id in the TrackBitrates message is ignored (we use the map key), so it can be ommited. */
94
+ trackIdToBitrates: {
95
+ [key: string]: MediaEvent_TrackBitrates;
96
+ };
97
+ midToTrackId: {
98
+ [key: string]: string;
99
+ };
100
+ }
101
+ declare const MediaEvent_SdpOffer: MessageFns$1<MediaEvent_SdpOffer>;
102
+ /** Sent when Peer wants to update its track's bitrate */
103
+ interface MediaEvent_TrackBitrates {
104
+ trackId: string;
105
+ /** Bitrate of each variant. For non-simulcast tracks use VARIANT_UNSPECIFIED. */
106
+ variantBitrates: MediaEvent_VariantBitrate[];
107
+ }
108
+ declare const MediaEvent_TrackBitrates: MessageFns$1<MediaEvent_TrackBitrates>;
109
+ /** Sent when client disables one of the track variants */
110
+ interface MediaEvent_DisableTrackVariant {
111
+ trackId: string;
112
+ variant: Variant;
113
+ }
114
+ declare const MediaEvent_DisableTrackVariant: MessageFns$1<MediaEvent_DisableTrackVariant>;
115
+ /** Sent when client enables one of the track variants */
116
+ interface MediaEvent_EnableTrackVariant {
117
+ trackId: string;
118
+ variant: Variant;
119
+ }
120
+ declare const MediaEvent_EnableTrackVariant: MessageFns$1<MediaEvent_EnableTrackVariant>;
121
+ interface MediaEvent_SetTargetTrackVariant {
122
+ trackId: string;
123
+ variant: Variant;
124
+ }
125
+ declare const MediaEvent_SetTargetTrackVariant: MessageFns$1<MediaEvent_SetTargetTrackVariant>;
126
+ type Builtin$1 = Date | Function | Uint8Array | string | number | boolean | undefined;
127
+ type DeepPartial$1<T> = T extends Builtin$1 ? T : T extends globalThis.Array<infer U> ? globalThis.Array<DeepPartial$1<U>> : T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial$1<U>> : T extends {} ? {
128
+ [K in keyof T]?: DeepPartial$1<T[K]>;
129
+ } : Partial<T>;
130
+ type KeysOfUnion$1<T> = T extends T ? keyof T : never;
131
+ type Exact$1<P, I extends P> = P extends Builtin$1 ? P : P & {
132
+ [K in keyof P]: Exact$1<P[K], I[K]>;
133
+ } & {
134
+ [K in Exclude<keyof I, KeysOfUnion$1<P>>]: never;
135
+ };
136
+ interface MessageFns$1<T> {
137
+ encode(message: T, writer?: BinaryWriter): BinaryWriter;
138
+ decode(input: BinaryReader | Uint8Array, length?: number): T;
139
+ fromJSON(object: any): T;
140
+ toJSON(message: T): unknown;
141
+ create<I extends Exact$1<DeepPartial$1<T>, I>>(base?: I): T;
142
+ fromPartial<I extends Exact$1<DeepPartial$1<T>, I>>(object: I): T;
143
+ }
144
+
145
+ interface MediaEvent_Track_SimulcastConfig {
146
+ enabled: boolean;
147
+ enabledVariants: Variant[];
148
+ disabledVariants: Variant[];
149
+ }
150
+ declare const MediaEvent_Track_SimulcastConfig: MessageFns<MediaEvent_Track_SimulcastConfig>;
151
+ interface MediaEvent_OfferData_TrackTypes {
152
+ audio: number;
153
+ video: number;
154
+ }
155
+ declare const MediaEvent_OfferData_TrackTypes: MessageFns<MediaEvent_OfferData_TrackTypes>;
156
+ type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;
157
+ type DeepPartial<T> = T extends Builtin ? T : T extends globalThis.Array<infer U> ? globalThis.Array<DeepPartial<U>> : T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>> : T extends {} ? {
158
+ [K in keyof T]?: DeepPartial<T[K]>;
159
+ } : Partial<T>;
160
+ type KeysOfUnion<T> = T extends T ? keyof T : never;
161
+ type Exact<P, I extends P> = P extends Builtin ? P : P & {
162
+ [K in keyof P]: Exact<P[K], I[K]>;
163
+ } & {
164
+ [K in Exclude<keyof I, KeysOfUnion<P>>]: never;
165
+ };
166
+ interface MessageFns<T> {
167
+ encode(message: T, writer?: BinaryWriter): BinaryWriter;
168
+ decode(input: BinaryReader | Uint8Array, length?: number): T;
169
+ fromJSON(object: any): T;
170
+ toJSON(message: T): unknown;
171
+ create<I extends Exact<DeepPartial<T>, I>>(base?: I): T;
172
+ fromPartial<I extends Exact<DeepPartial<T>, I>>(object: I): T;
173
+ }
174
+
175
+ type SerializedMediaEvent = Uint8Array;
176
+ interface MediaEvent {
177
+ type: keyof MediaEvent$1;
178
+ key?: string;
179
+ data?: any;
180
+ }
181
+
182
+ type MediaStreamTrackId = string;
183
+ type TrackKind = 'audio' | 'video';
184
+ /**
185
+ * Type describing Voice Activity Detection statuses.
186
+ *
187
+ * - `speech` - voice activity has been detected
188
+ * - `silence` - lack of voice activity has been detected
189
+ */
190
+ type VadStatus = 'speech' | 'silence';
191
+ /**
192
+ * Type describing maximal bandwidth that can be used, in kbps. 0 is interpreted as unlimited bandwidth.
193
+ */
194
+ type BandwidthLimit = number;
195
+ /**
196
+ * Type describing bandwidth limit for simulcast track.
197
+ * It is a mapping (encoding => BandwidthLimit).
198
+ * If encoding isn't present in this mapping, it will be assumed that this particular encoding shouldn't have any bandwidth limit
199
+ */
200
+ type SimulcastBandwidthLimit = Map<Variant, BandwidthLimit>;
201
+ /**
202
+ * Type describing bandwidth limitation of a Track, including simulcast and non-simulcast tracks.
203
+ * A sum type of `BandwidthLimit` and `SimulcastBandwidthLimit`
204
+ */
205
+ type TrackBandwidthLimit = BandwidthLimit | SimulcastBandwidthLimit;
206
+ /**
207
+ * Type describing possible reasons for currently selected encoding.
208
+ * - `other` - the exact reason couldn't be determined
209
+ * - `encodingInactive` - previously selected encoding became inactive
210
+ * - `lowBandwidth` - there is no longer enough bandwidth to maintain previously selected encoding
211
+ */
212
+ type EncodingReason = 'other' | 'encodingInactive' | 'lowBandwidth';
213
+ /**
214
+ * Track's context i.e. all data that can be useful when operating on track.
215
+ */
216
+ interface TrackContextFields {
217
+ readonly track: MediaStreamTrack | null;
218
+ /**
219
+ * Stream this track belongs to.
220
+ */
221
+ readonly stream: MediaStream | null;
222
+ /**
223
+ * Endpoint this track comes from.
224
+ */
225
+ readonly endpoint: Endpoint;
226
+ /**
227
+ * Track id. It is generated by RTC engine and takes form `endpoint_id:<random_uuidv4>`.
228
+ * It is WebRTC agnostic i.e. it does not contain `mid` or `stream id`.
229
+ */
230
+ readonly trackId: string;
231
+ /**
232
+ * Simulcast configuration.
233
+ * Only present for local tracks.
234
+ */
235
+ readonly simulcastConfig?: MediaEvent_Track_SimulcastConfig;
236
+ /**
237
+ * Any info that was passed in {@link WebRTCEndpoint.addTrack}.
238
+ */
239
+ readonly metadata?: unknown;
240
+ readonly maxBandwidth?: TrackBandwidthLimit;
241
+ readonly vadStatus: VadStatus;
242
+ /**
243
+ * Encoding that is currently received.
244
+ * Only present for remote tracks.
245
+ */
246
+ readonly encoding?: Variant;
247
+ /**
248
+ * The reason of currently selected encoding.
249
+ * Only present for remote tracks.
250
+ */
251
+ readonly encodingReason?: EncodingReason;
252
+ }
253
+ interface TrackContextEvents {
254
+ /**
255
+ * Emitted each time track encoding has changed.
256
+ *
257
+ * Track encoding can change in the following cases:
258
+ * - when user requested a change
259
+ * - when sender stopped sending some encoding (because of bandwidth change)
260
+ * - when receiver doesn't have enough bandwidth
261
+ *
262
+ * Some of those reasons are indicated in {@link TrackContext.encodingReason}.
263
+ */
264
+ encodingChanged: (context: TrackContext) => void;
265
+ /**
266
+ * Emitted every time an update about voice activity is received from the server.
267
+ */
268
+ voiceActivityChanged: (context: TrackContext) => void;
269
+ }
270
+ interface TrackContext extends TrackContextFields, TypedEmitter<Required<TrackContextEvents>> {
271
+ }
272
+ type TrackNegotiationStatus = 'awaiting' | 'offered' | 'done';
273
+ /**
274
+ * Events emitted by the {@link WebRTCEndpoint} instance.
275
+ */
276
+ interface WebRTCEndpointEvents {
277
+ /**
278
+ * Emitted each time WebRTCEndpoint need to send some data to the server.
279
+ */
280
+ sendMediaEvent: (mediaEvent: SerializedMediaEvent) => void;
281
+ /**
282
+ * Emitted when endpoint of this {@link WebRTCEndpoint} instance is ready. Triggered by {@link WebRTCEndpoint.connect}
283
+ */
284
+ connected: (endpointId: string, otherEndpoints: Endpoint[]) => void;
285
+ /**
286
+ * Emitted when endpoint of this {@link WebRTCEndpoint} instance was removed.
287
+ */
288
+ disconnected: () => void;
289
+ /**
290
+ * Emitted when data in a new track arrives.
291
+ *
292
+ * This event is always emitted after {@link trackAdded}.
293
+ * It informs the user that data related to the given track arrives and can be played or displayed.
294
+ */
295
+ trackReady: (ctx: TrackContext) => void;
296
+ /**
297
+ * Emitted each time the endpoint which was already in the room, adds new track. Fields track and stream will be set to null.
298
+ * These fields will be set to non-null value in {@link trackReady}
299
+ */
300
+ trackAdded: (ctx: TrackContext) => void;
301
+ /**
302
+ * Emitted when some track will no longer be sent.
303
+ *
304
+ * It will also be emitted before {@link endpointRemoved} for each track of this endpoint.
305
+ */
306
+ trackRemoved: (ctx: TrackContext) => void;
307
+ /**
308
+ * Emitted each time endpoint has its track metadata updated.
309
+ */
310
+ trackUpdated: (ctx: TrackContext) => void;
311
+ /**
312
+ * Emitted each time new endpoint is added to the room.
313
+ */
314
+ endpointAdded: (endpoint: Endpoint) => void;
315
+ /**
316
+ * Emitted each time endpoint is removed, emitted only for other endpoints.
317
+ */
318
+ endpointRemoved: (endpoint: Endpoint) => void;
319
+ /**
320
+ * Emitted each time endpoint has its metadata updated.
321
+ */
322
+ endpointUpdated: (endpoint: Endpoint) => void;
323
+ /**
324
+ * Emitted in case of errors related to multimedia session e.g. ICE connection.
325
+ */
326
+ connectionError: (error: {
327
+ message: string;
328
+ event: Event;
329
+ }) => void;
330
+ /**
331
+ * Emitted in case of errors related to multimedia session e.g. ICE connection.
332
+ */
333
+ signalingError: (error: {
334
+ message: string;
335
+ }) => void;
336
+ /**
337
+ * Currently, this event is only emitted when DisplayManager in RTC Engine is
338
+ * enabled and simulcast is disabled.
339
+ *
340
+ * Emitted when priority of video tracks have changed.
341
+ * @param enabledTracks - list of tracks which will be sent to client from SFU
342
+ * @param disabledTracks - list of tracks which will not be sent to client from SFU
343
+ */
344
+ tracksPriorityChanged: (enabledTracks: TrackContext[], disabledTracks: TrackContext[]) => void;
345
+ /**
346
+ * Emitted every time the server estimates client's bandwidth.
347
+ *
348
+ * @param {bigint} estimation - client's available incoming bitrate estimated
349
+ * by the server. It's measured in bits per second.
350
+ */
351
+ bandwidthEstimationChanged: (estimation: bigint) => void;
352
+ /**
353
+ * Emitted each time track encoding has been disabled.
354
+ */
355
+ trackEncodingDisabled: (context: TrackContext, encoding: Variant) => void;
356
+ /**
357
+ * Emitted each time track encoding has been enabled.
358
+ */
359
+ trackEncodingEnabled: (context: TrackContext, encoding: Variant) => void;
360
+ targetTrackEncodingRequested: (event: {
361
+ trackId: string;
362
+ variant: Variant;
363
+ }) => void;
364
+ disconnectRequested: (event: any) => void;
365
+ localTrackAdded: (event: {
366
+ trackId: string;
367
+ track: MediaStreamTrack;
368
+ stream: MediaStream;
369
+ trackMetadata?: unknown;
370
+ simulcastConfig: MediaEvent_Track_SimulcastConfig;
371
+ maxBandwidth: TrackBandwidthLimit;
372
+ }) => void;
373
+ localTrackRemoved: (event: {
374
+ trackId: string;
375
+ }) => void;
376
+ localTrackReplaced: (event: {
377
+ trackId: string;
378
+ track: MediaStreamTrack | null;
379
+ }) => void;
380
+ localTrackMuted: (event: {
381
+ trackId: string;
382
+ }) => void;
383
+ localTrackUnmuted: (event: {
384
+ trackId: string;
385
+ }) => void;
386
+ localTrackBandwidthSet: (event: {
387
+ trackId: string;
388
+ bandwidth: BandwidthLimit;
389
+ }) => void;
390
+ localTrackEncodingBandwidthSet: (event: {
391
+ trackId: string;
392
+ rid: Variant;
393
+ bandwidth: BandwidthLimit;
394
+ }) => void;
395
+ localTrackEncodingEnabled: (event: {
396
+ trackId: string;
397
+ encoding: Variant;
398
+ }) => void;
399
+ localTrackEncodingDisabled: (event: {
400
+ trackId: string;
401
+ encoding: Variant;
402
+ }) => void;
403
+ localEndpointMetadataChanged: (event: {
404
+ metadata: unknown;
405
+ }) => void;
406
+ localTrackMetadataChanged: (event: {
407
+ trackId: string;
408
+ metadata: unknown;
409
+ }) => void;
410
+ }
411
+ /**
412
+ * Interface describing Endpoint.
413
+ */
414
+ interface Endpoint {
415
+ /**
416
+ * Endpoint's id. It is assigned by user in custom logic that use backend API.
417
+ */
418
+ id: string;
419
+ /**
420
+ * Type of the endpoint, e.g. "webrtc", "hls" or "rtsp".
421
+ */
422
+ type: string;
423
+ /**
424
+ * Any information that was provided in {@link WebRTCEndpoint.connect}.
425
+ */
426
+ metadata?: unknown;
427
+ /**
428
+ * List of tracks that are sent by the endpoint.
429
+ */
430
+ tracks: Map<string, TrackContext>;
431
+ }
432
+
433
+ declare class ConnectionManager {
434
+ private readonly connection;
435
+ constructor(iceServers: RTCIceServer[]);
436
+ isConnectionUnstable: () => boolean;
437
+ getConnection: () => RTCPeerConnection;
438
+ addTransceiversIfNeeded: (serverTracks: MediaEvent_OfferData_TrackTypes) => void;
439
+ addTransceiver: (track: MediaStreamTrack, transceiverConfig: RTCRtpTransceiverInit) => void;
440
+ setOnTrackReady: (onTrackReady: (event: RTCTrackEvent) => void) => void;
441
+ setRemoteDescription: (data: RTCSessionDescriptionInit) => Promise<void>;
442
+ isTrackInUse: (track: MediaStreamTrack) => boolean;
443
+ removeTrack: (sender: RTCRtpSender) => void;
444
+ findSender: (mediaStreamTrackId: MediaStreamTrackId) => RTCRtpSender;
445
+ addIceCandidate: (iceCandidate: RTCIceCandidate) => Promise<void>;
446
+ }
447
+
448
+ declare const TrackContextImpl_base: new () => TypedEmitter<Required<TrackContextEvents>>;
449
+ declare class TrackContextImpl extends TrackContextImpl_base implements TrackContext {
450
+ endpoint: Endpoint;
451
+ trackId: string;
452
+ track: MediaStreamTrack | null;
453
+ trackKind: TrackKind | null;
454
+ stream: MediaStream | null;
455
+ metadata?: unknown;
456
+ metadataParsingError?: any;
457
+ simulcastConfig?: MediaEvent_Track_SimulcastConfig;
458
+ maxBandwidth: TrackBandwidthLimit;
459
+ encoding?: Variant;
460
+ encodingReason?: EncodingReason;
461
+ vadStatus: VadStatus;
462
+ negotiationStatus: TrackNegotiationStatus;
463
+ pendingMetadataUpdate: boolean;
464
+ constructor(endpoint: Endpoint, trackId: string, metadata: any, simulcastConfig?: MediaEvent_Track_SimulcastConfig);
465
+ }
466
+ type EndpointWithTrackContext = Omit<Endpoint, 'tracks'> & {
467
+ tracks: Map<string, TrackContextImpl>;
468
+ };
469
+
470
+ declare const WebRTCEndpoint_base: new () => TypedEmitter<Required<WebRTCEndpointEvents>>;
471
+ /**
472
+ * Main class that is responsible for connecting to the RTC Engine, sending and receiving media.
473
+ */
474
+ declare class WebRTCEndpoint extends WebRTCEndpoint_base {
475
+ private readonly localTrackManager;
476
+ private readonly remote;
477
+ private readonly local;
478
+ private readonly commandsQueue;
479
+ private proposedIceServers;
480
+ bandwidthEstimation: bigint;
481
+ connectionManager?: ConnectionManager;
482
+ private clearConnectionCallbacks;
483
+ constructor();
484
+ /**
485
+ * Tries to connect to the RTC Engine. If user is successfully connected then {@link WebRTCEndpointEvents.connected}
486
+ * will be emitted.
487
+ *
488
+ * @param metadata - Any information that other endpoints will receive in {@link WebRTCEndpointEvents.endpointAdded}
489
+ * after accepting this endpoint
490
+ *
491
+ * @example
492
+ * ```ts
493
+ * let webrtc = new WebRTCEndpoint();
494
+ * webrtc.connect({displayName: "Bob"});
495
+ * ```
496
+ */
497
+ connect: (metadata: unknown) => void;
498
+ /**
499
+ * Feeds media event received from RTC Engine to {@link WebRTCEndpoint}.
500
+ * This function should be called whenever some media event from RTC Engine
501
+ * was received and can result in {@link WebRTCEndpoint} generating some other
502
+ * media events.
503
+ *
504
+ * @param mediaEvent - String data received over custom signalling layer.
505
+ *
506
+ * @example
507
+ * This example assumes phoenix channels as signalling layer.
508
+ * As phoenix channels require objects, RTC Engine encapsulates binary data into
509
+ * map with one field that is converted to object with one field on the TS side.
510
+ * ```ts
511
+ * webrtcChannel.on("mediaEvent", (event) => webrtc.receiveMediaEvent(event.data));
512
+ * ```
513
+ */
514
+ receiveMediaEvent: (mediaEvent: SerializedMediaEvent) => Promise<void>;
515
+ private getEndpointId;
516
+ private onTrackReady;
517
+ /**
518
+ * Retrieves statistics related to the RTCPeerConnection.
519
+ * These statistics provide insights into the performance and status of the connection.
520
+ *
521
+ * @return {Promise<RTCStatsReport>}
522
+ *
523
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/getStats | MDN Web Docs: RTCPeerConnection.getStats()}
524
+ */
525
+ getStatistics(selector?: MediaStreamTrack | null): Promise<RTCStatsReport>;
526
+ /**
527
+ * Returns a snapshot of currently received remote tracks.
528
+ *
529
+ * @example
530
+ * if (webRTCEndpoint.getRemoteTracks()[trackId]?.simulcastConfig?.enabled) {
531
+ * webRTCEndpoint.setTargetTrackEncoding(trackId, encoding);
532
+ * }
533
+ */
534
+ getRemoteTracks(): Record<string, TrackContext>;
535
+ /**
536
+ * Returns a snapshot of currently received remote endpoints.
537
+ */
538
+ getRemoteEndpoints(): Record<string, EndpointWithTrackContext>;
539
+ getLocalEndpoint(): EndpointWithTrackContext;
540
+ getBandwidthEstimation(): bigint;
541
+ private handleMediaEvent;
542
+ private onSdpAnswer;
543
+ /**
544
+ * Adds track that will be sent to the RTC Engine.
545
+ * @param track - Audio or video track e.g. from your microphone or camera.
546
+ * @param trackMetadata - Any information about this track that other endpoints will
547
+ * receive in {@link WebRTCEndpointEvents.endpointAdded}. E.g. this can source of the track - whether it's
548
+ * screensharing, webcam or some other media device.
549
+ * @param simulcastConfig - Simulcast configuration. By default simulcast is disabled.
550
+ * For more information refer to {@link SimulcastConfig}.
551
+ * @param maxBandwidth - maximal bandwidth this track can use.
552
+ * Defaults to 0 which is unlimited.
553
+ * This option has no effect for simulcast and audio tracks.
554
+ * For simulcast tracks use `{@link WebRTCEndpoint.setTrackBandwidth}.
555
+ * @returns {string} Returns id of added track
556
+ * @example
557
+ * ```ts
558
+ * let localStream: MediaStream = new MediaStream();
559
+ * try {
560
+ * localAudioStream = await navigator.mediaDevices.getUserMedia(
561
+ * AUDIO_CONSTRAINTS
562
+ * );
563
+ * localAudioStream
564
+ * .getTracks()
565
+ * .forEach((track) => localStream.addTrack(track));
566
+ * } catch (error) {
567
+ * console.error("Couldn't get microphone permission:", error);
568
+ * }
569
+ *
570
+ * try {
571
+ * localVideoStream = await navigator.mediaDevices.getUserMedia(
572
+ * VIDEO_CONSTRAINTS
573
+ * );
574
+ * localVideoStream
575
+ * .getTracks()
576
+ * .forEach((track) => localStream.addTrack(track));
577
+ * } catch (error) {
578
+ * console.error("Couldn't get camera permission:", error);
579
+ * }
580
+ *
581
+ * localStream
582
+ * .getTracks()
583
+ * .forEach((track) => webrtc.addTrack(track, localStream));
584
+ * ```
585
+ */
586
+ addTrack(track: MediaStreamTrack, trackMetadata?: unknown, simulcastConfig?: MediaEvent_Track_SimulcastConfig, maxBandwidth?: TrackBandwidthLimit): Promise<string>;
587
+ /**
588
+ * Replaces a track that is being sent to the RTC Engine.
589
+ * @param trackId - Audio or video track.
590
+ * @param {string} trackId - Id of audio or video track to replace.
591
+ * @param {MediaStreamTrack} newTrack
592
+ * @returns {Promise<boolean>} success
593
+ * @example
594
+ * ```ts
595
+ * // setup camera
596
+ * let localStream: MediaStream = new MediaStream();
597
+ * try {
598
+ * localVideoStream = await navigator.mediaDevices.getUserMedia(
599
+ * VIDEO_CONSTRAINTS
600
+ * );
601
+ * localVideoStream
602
+ * .getTracks()
603
+ * .forEach((track) => localStream.addTrack(track));
604
+ * } catch (error) {
605
+ * console.error("Couldn't get camera permission:", error);
606
+ * }
607
+ * let oldTrackId;
608
+ * localStream
609
+ * .getTracks()
610
+ * .forEach((track) => trackId = webrtc.addTrack(track, localStream));
611
+ *
612
+ * // change camera
613
+ * const oldTrack = localStream.getVideoTracks()[0];
614
+ * let videoDeviceId = "abcd-1234";
615
+ * navigator.mediaDevices.getUserMedia({
616
+ * video: {
617
+ * ...(VIDEO_CONSTRAINTS as {}),
618
+ * deviceId: {
619
+ * exact: videoDeviceId,
620
+ * },
621
+ * }
622
+ * })
623
+ * .then((stream) => {
624
+ * let videoTrack = stream.getVideoTracks()[0];
625
+ * webrtc.replaceTrack(oldTrackId, videoTrack);
626
+ * })
627
+ * .catch((error) => {
628
+ * console.error('Error switching camera', error);
629
+ * })
630
+ * ```
631
+ */
632
+ replaceTrack(trackId: string, newTrack: MediaStreamTrack | null): Promise<void>;
633
+ /**
634
+ * Updates maximum bandwidth for the track identified by trackId.
635
+ * This value directly translates to quality of the stream and, in case of video, to the amount of RTP packets being sent.
636
+ * In case trackId points at the simulcast track bandwidth is split between all of the variant streams proportionally to their resolution.
637
+ *
638
+ * @param {string} trackId
639
+ * @param {BandwidthLimit} bandwidth in kbps
640
+ * @returns {Promise<boolean>} success
641
+ */
642
+ setTrackBandwidth(trackId: string, bandwidth: BandwidthLimit): Promise<void>;
643
+ /**
644
+ * Updates maximum bandwidth for the given simulcast encoding of the given track.
645
+ *
646
+ * @param {string} trackId - id of the track
647
+ * @param {string} rid - rid of the encoding
648
+ * @param {BandwidthLimit} bandwidth - desired max bandwidth used by the encoding (in kbps)
649
+ * @returns
650
+ */
651
+ setEncodingBandwidth(trackId: string, rid: Variant, bandwidth: BandwidthLimit): Promise<void>;
652
+ /**
653
+ * Removes a track from connection that was sent to the RTC Engine.
654
+ * @param {string} trackId - Id of audio or video track to remove.
655
+ * @example
656
+ * ```ts
657
+ * // setup camera
658
+ * let localStream: MediaStream = new MediaStream();
659
+ * try {
660
+ * localVideoStream = await navigator.mediaDevices.getUserMedia(
661
+ * VIDEO_CONSTRAINTS
662
+ * );
663
+ * localVideoStream
664
+ * .getTracks()
665
+ * .forEach((track) => localStream.addTrack(track));
666
+ * } catch (error) {
667
+ * console.error("Couldn't get camera permission:", error);
668
+ * }
669
+ *
670
+ * let trackId
671
+ * localStream
672
+ * .getTracks()
673
+ * .forEach((track) => trackId = webrtc.addTrack(track, localStream));
674
+ *
675
+ * // remove track
676
+ * webrtc.removeTrack(trackId)
677
+ * ```
678
+ */
679
+ removeTrack(trackId: string): Promise<void>;
680
+ /**
681
+ * Sets track variant that server should send to the client library.
682
+ *
683
+ * The variant will be sent whenever it is available.
684
+ * If chosen variant is temporarily unavailable, some other variant
685
+ * will be sent until the chosen variant becomes active again.
686
+ *
687
+ * @param {string} trackId - id of track
688
+ * @param {Encoding} variant - variant to receive
689
+ * @example
690
+ * ```ts
691
+ * webrtc.setTargetTrackEncoding(incomingTrackCtx.trackId, "l")
692
+ * ```
693
+ */
694
+ setTargetTrackEncoding(trackId: string, variant: Variant): void;
695
+ /**
696
+ * Enables track encoding so that it will be sent to the server.
697
+ * @param {string} trackId - id of track
698
+ * @param {Encoding} encoding - encoding that will be enabled
699
+ * @example
700
+ * ```ts
701
+ * const trackId = webrtc.addTrack(track, stream, {}, {enabled: true, activeEncodings: ["l", "m", "h"]});
702
+ * webrtc.disableTrackEncoding(trackId, "l");
703
+ * // wait some time
704
+ * webrtc.enableTrackEncoding(trackId, "l");
705
+ * ```
706
+ */
707
+ enableTrackEncoding: (trackId: string, encoding: Variant) => Promise<void>;
708
+ /**
709
+ * Disables track encoding so that it will be no longer sent to the server.
710
+ * @param {string} trackId - id of track
711
+ * @param {Encoding} encoding - encoding that will be disabled
712
+ * @example
713
+ * ```ts
714
+ * const trackId = webrtc.addTrack(track, stream, {}, {enabled: true, activeEncodings: ["l", "m", "h"]});
715
+ * webrtc.disableTrackEncoding(trackId, "l");
716
+ * ```
717
+ */
718
+ disableTrackEncoding: (trackId: string, encoding: Variant) => Promise<void>;
719
+ /**
720
+ * Updates the metadata for the current endpoint.
721
+ * @param metadata - Data about this endpoint that other endpoints will receive upon being added.
722
+ *
723
+ * If the metadata is different from what is already tracked in the room, the optional
724
+ * event `endpointUpdated` will be emitted for other endpoint in the room.
725
+ */
726
+ updateEndpointMetadata: (metadata: any) => void;
727
+ /**
728
+ * Updates the metadata for a specific track.
729
+ * @param trackId - trackId (generated in addTrack) of audio or video track.
730
+ * @param trackMetadata - Data about this track that other endpoint will receive upon being added.
731
+ *
732
+ * If the metadata is different from what is already tracked in the room, the optional
733
+ * event `trackUpdated` will be emitted for other endpoints in the room.
734
+ */
735
+ updateTrackMetadata: (trackId: string, trackMetadata: any) => void;
736
+ /**
737
+ * Disconnects from the room. This function should be called when user disconnects from the room
738
+ * in a clean way e.g. by clicking a dedicated, custom button `disconnect`.
739
+ * As a result there will be generated one more media event that should be
740
+ * sent to the RTC Engine. Thanks to it each other endpoint will be notified
741
+ * that endpoint was removed in {@link WebRTCEndpointEvents.endpointRemoved},
742
+ */
743
+ disconnect: () => void;
744
+ /**
745
+ * Cleans up {@link WebRTCEndpoint} instance.
746
+ */
747
+ cleanUp: () => void;
748
+ private getTrackId;
749
+ private sendMediaEvent;
750
+ private createAndSendOffer;
751
+ private onOfferData;
752
+ private setupConnectionListeners;
753
+ private createNewConnection;
754
+ private onRemoteCandidate;
755
+ private onLocalCandidate;
756
+ private onIceCandidateError;
757
+ private onConnectionStateChange;
758
+ private onIceConnectionStateChange;
759
+ }
760
+
761
+ export { type BandwidthLimit, type EncodingReason, type Endpoint, type MediaEvent, type SerializedMediaEvent, type SimulcastBandwidthLimit, MediaEvent_Track_SimulcastConfig as SimulcastConfig, type TrackBandwidthLimit, type TrackContext, type TrackContextEvents, type TrackKind, type VadStatus, Variant, WebRTCEndpoint, type WebRTCEndpointEvents };