@fishjam-cloud/ts-client 0.5.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.
Files changed (52) hide show
  1. package/README.md +134 -0
  2. package/dist/src/FishjamClient.d.ts +523 -0
  3. package/dist/src/FishjamClient.js +666 -0
  4. package/dist/src/auth.d.ts +3 -0
  5. package/dist/src/auth.js +8 -0
  6. package/dist/src/index.d.ts +7 -0
  7. package/dist/src/index.js +3 -0
  8. package/dist/src/protos/fishjam/peer_notifications.d.ts +78 -0
  9. package/dist/src/protos/fishjam/peer_notifications.js +302 -0
  10. package/dist/src/protos/index.d.ts +1 -0
  11. package/dist/src/protos/index.js +1 -0
  12. package/dist/src/reconnection.d.ts +27 -0
  13. package/dist/src/reconnection.js +121 -0
  14. package/dist/src/webrtc/CommandsQueue.d.ts +23 -0
  15. package/dist/src/webrtc/CommandsQueue.js +102 -0
  16. package/dist/src/webrtc/ConnectionManager.d.ts +29 -0
  17. package/dist/src/webrtc/ConnectionManager.js +82 -0
  18. package/dist/src/webrtc/bitrate.d.ts +12 -0
  19. package/dist/src/webrtc/bitrate.js +12 -0
  20. package/dist/src/webrtc/deferred.d.ts +8 -0
  21. package/dist/src/webrtc/deferred.js +17 -0
  22. package/dist/src/webrtc/index.d.ts +3 -0
  23. package/dist/src/webrtc/index.js +1 -0
  24. package/dist/src/webrtc/internal.d.ts +26 -0
  25. package/dist/src/webrtc/internal.js +34 -0
  26. package/dist/src/webrtc/mediaEvent.d.ts +10 -0
  27. package/dist/src/webrtc/mediaEvent.js +16 -0
  28. package/dist/src/webrtc/tracks/Local.d.ts +51 -0
  29. package/dist/src/webrtc/tracks/Local.js +289 -0
  30. package/dist/src/webrtc/tracks/LocalTrack.d.ts +62 -0
  31. package/dist/src/webrtc/tracks/LocalTrack.js +235 -0
  32. package/dist/src/webrtc/tracks/LocalTrackManager.d.ts +38 -0
  33. package/dist/src/webrtc/tracks/LocalTrackManager.js +83 -0
  34. package/dist/src/webrtc/tracks/Remote.d.ts +38 -0
  35. package/dist/src/webrtc/tracks/Remote.js +200 -0
  36. package/dist/src/webrtc/tracks/RemoteTrack.d.ts +39 -0
  37. package/dist/src/webrtc/tracks/RemoteTrack.js +64 -0
  38. package/dist/src/webrtc/tracks/TrackCommon.d.ts +8 -0
  39. package/dist/src/webrtc/tracks/TrackCommon.js +1 -0
  40. package/dist/src/webrtc/tracks/bandwidth.d.ts +1 -0
  41. package/dist/src/webrtc/tracks/bandwidth.js +27 -0
  42. package/dist/src/webrtc/tracks/encodings.d.ts +1 -0
  43. package/dist/src/webrtc/tracks/encodings.js +7 -0
  44. package/dist/src/webrtc/tracks/transceivers.d.ts +2 -0
  45. package/dist/src/webrtc/tracks/transceivers.js +78 -0
  46. package/dist/src/webrtc/types.d.ts +306 -0
  47. package/dist/src/webrtc/types.js +2 -0
  48. package/dist/src/webrtc/voiceActivityDetection.d.ts +2 -0
  49. package/dist/src/webrtc/voiceActivityDetection.js +2 -0
  50. package/dist/src/webrtc/webRTCEndpoint.d.ts +299 -0
  51. package/dist/src/webrtc/webRTCEndpoint.js +730 -0
  52. package/package.json +82 -0
@@ -0,0 +1,299 @@
1
+ import type { MediaEvent, SerializedMediaEvent } from './mediaEvent';
2
+ import type TypedEmitter from 'typed-emitter';
3
+ import type { BandwidthLimit, Config, Encoding, SimulcastConfig, TrackBandwidthLimit, TrackContext, WebRTCEndpointEvents } from './types';
4
+ import type { EndpointWithTrackContext } from './internal';
5
+ import { ConnectionManager } from './ConnectionManager';
6
+ declare const WebRTCEndpoint_base: new <EndpointMetadata_1, TrackMetadata_1>() => TypedEmitter<Required<WebRTCEndpointEvents<EndpointMetadata_1, TrackMetadata_1>>>;
7
+ /**
8
+ * Main class that is responsible for connecting to the RTC Engine, sending and receiving media.
9
+ */
10
+ export declare class WebRTCEndpoint<EndpointMetadata = any, TrackMetadata = any> extends WebRTCEndpoint_base<EndpointMetadata, TrackMetadata> {
11
+ private readonly endpointMetadataParser;
12
+ private readonly trackMetadataParser;
13
+ private readonly localTrackManager;
14
+ private readonly remote;
15
+ private readonly local;
16
+ private readonly commandsQueue;
17
+ bandwidthEstimation: bigint;
18
+ connectionManager?: ConnectionManager;
19
+ private clearConnectionCallbacks;
20
+ constructor(config?: Config<EndpointMetadata, TrackMetadata>);
21
+ /**
22
+ * Tries to connect to the RTC Engine. If user is successfully connected then {@link WebRTCEndpointEvents.connected}
23
+ * will be emitted.
24
+ *
25
+ * @param metadata - Any information that other endpoints will receive in {@link WebRTCEndpointEvents.endpointAdded}
26
+ * after accepting this endpoint
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * let webrtc = new WebRTCEndpoint();
31
+ * webrtc.connect({displayName: "Bob"});
32
+ * ```
33
+ */
34
+ connect: (metadata: EndpointMetadata) => void;
35
+ /**
36
+ * Feeds media event received from RTC Engine to {@link WebRTCEndpoint}.
37
+ * This function should be called whenever some media event from RTC Engine
38
+ * was received and can result in {@link WebRTCEndpoint} generating some other
39
+ * media events.
40
+ *
41
+ * @param mediaEvent - String data received over custom signalling layer.
42
+ *
43
+ * @example
44
+ * This example assumes phoenix channels as signalling layer.
45
+ * As phoenix channels require objects, RTC Engine encapsulates binary data into
46
+ * map with one field that is converted to object with one field on the TS side.
47
+ * ```ts
48
+ * webrtcChannel.on("mediaEvent", (event) => webrtc.receiveMediaEvent(event.data));
49
+ * ```
50
+ */
51
+ receiveMediaEvent: (mediaEvent: SerializedMediaEvent) => Promise<void>;
52
+ private getEndpointId;
53
+ private onTrackReady;
54
+ /**
55
+ * Retrieves statistics related to the RTCPeerConnection.
56
+ * These statistics provide insights into the performance and status of the connection.
57
+ *
58
+ * @return {Promise<RTCStatsReport>}
59
+ *
60
+ * @external RTCPeerConnection#getStats()
61
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/getStats | MDN Web Docs: RTCPeerConnection.getStats()}
62
+ */
63
+ getStatistics(selector?: MediaStreamTrack | null): Promise<RTCStatsReport>;
64
+ /**
65
+ * Returns a snapshot of currently received remote tracks.
66
+ *
67
+ * @example
68
+ * if (webRTCEndpoint.getRemoteTracks()[trackId]?.simulcastConfig?.enabled) {
69
+ * webRTCEndpoint.setTargetTrackEncoding(trackId, encoding);
70
+ * }
71
+ */
72
+ getRemoteTracks(): Record<string, TrackContext<EndpointMetadata, TrackMetadata>>;
73
+ /**
74
+ * Returns a snapshot of currently received remote endpoints.
75
+ */
76
+ getRemoteEndpoints(): Record<string, EndpointWithTrackContext<EndpointMetadata, TrackMetadata>>;
77
+ getLocalEndpoint(): EndpointWithTrackContext<EndpointMetadata, TrackMetadata>;
78
+ getBandwidthEstimation(): bigint;
79
+ private handleMediaEvent;
80
+ private onSdpAnswer;
81
+ /**
82
+ * Adds track that will be sent to the RTC Engine.
83
+ * @param track - Audio or video track e.g. from your microphone or camera.
84
+ * @param trackMetadata - Any information about this track that other endpoints will
85
+ * receive in {@link WebRTCEndpointEvents.endpointAdded}. E.g. this can source of the track - whether it's
86
+ * screensharing, webcam or some other media device.
87
+ * @param simulcastConfig - Simulcast configuration. By default simulcast is disabled.
88
+ * For more information refer to {@link SimulcastConfig}.
89
+ * @param maxBandwidth - maximal bandwidth this track can use.
90
+ * Defaults to 0 which is unlimited.
91
+ * This option has no effect for simulcast and audio tracks.
92
+ * For simulcast tracks use `{@link WebRTCEndpoint.setTrackBandwidth}.
93
+ * @returns {string} Returns id of added track
94
+ * @example
95
+ * ```ts
96
+ * let localStream: MediaStream = new MediaStream();
97
+ * try {
98
+ * localAudioStream = await navigator.mediaDevices.getUserMedia(
99
+ * AUDIO_CONSTRAINTS
100
+ * );
101
+ * localAudioStream
102
+ * .getTracks()
103
+ * .forEach((track) => localStream.addTrack(track));
104
+ * } catch (error) {
105
+ * console.error("Couldn't get microphone permission:", error);
106
+ * }
107
+ *
108
+ * try {
109
+ * localVideoStream = await navigator.mediaDevices.getUserMedia(
110
+ * VIDEO_CONSTRAINTS
111
+ * );
112
+ * localVideoStream
113
+ * .getTracks()
114
+ * .forEach((track) => localStream.addTrack(track));
115
+ * } catch (error) {
116
+ * console.error("Couldn't get camera permission:", error);
117
+ * }
118
+ *
119
+ * localStream
120
+ * .getTracks()
121
+ * .forEach((track) => webrtc.addTrack(track, localStream));
122
+ * ```
123
+ */
124
+ addTrack(track: MediaStreamTrack, trackMetadata?: TrackMetadata, simulcastConfig?: SimulcastConfig, maxBandwidth?: TrackBandwidthLimit): Promise<string>;
125
+ /**
126
+ * Replaces a track that is being sent to the RTC Engine.
127
+ * @param trackId - Audio or video track.
128
+ * @param {string} trackId - Id of audio or video track to replace.
129
+ * @param {MediaStreamTrack} newTrack
130
+ * @param {any} [newTrackMetadata] - Optional track metadata to apply to the new track. If no
131
+ * track metadata is passed, the old track metadata is retained.
132
+ * @returns {Promise<boolean>} success
133
+ * @example
134
+ * ```ts
135
+ * // setup camera
136
+ * let localStream: MediaStream = new MediaStream();
137
+ * try {
138
+ * localVideoStream = await navigator.mediaDevices.getUserMedia(
139
+ * VIDEO_CONSTRAINTS
140
+ * );
141
+ * localVideoStream
142
+ * .getTracks()
143
+ * .forEach((track) => localStream.addTrack(track));
144
+ * } catch (error) {
145
+ * console.error("Couldn't get camera permission:", error);
146
+ * }
147
+ * let oldTrackId;
148
+ * localStream
149
+ * .getTracks()
150
+ * .forEach((track) => trackId = webrtc.addTrack(track, localStream));
151
+ *
152
+ * // change camera
153
+ * const oldTrack = localStream.getVideoTracks()[0];
154
+ * let videoDeviceId = "abcd-1234";
155
+ * navigator.mediaDevices.getUserMedia({
156
+ * video: {
157
+ * ...(VIDEO_CONSTRAINTS as {}),
158
+ * deviceId: {
159
+ * exact: videoDeviceId,
160
+ * },
161
+ * }
162
+ * })
163
+ * .then((stream) => {
164
+ * let videoTrack = stream.getVideoTracks()[0];
165
+ * webrtc.replaceTrack(oldTrackId, videoTrack);
166
+ * })
167
+ * .catch((error) => {
168
+ * console.error('Error switching camera', error);
169
+ * })
170
+ * ```
171
+ */
172
+ replaceTrack(trackId: string, newTrack: MediaStreamTrack | null, newTrackMetadata?: any): Promise<void>;
173
+ /**
174
+ * Updates maximum bandwidth for the track identified by trackId.
175
+ * This value directly translates to quality of the stream and, in case of video, to the amount of RTP packets being sent.
176
+ * In case trackId points at the simulcast track bandwidth is split between all of the variant streams proportionally to their resolution.
177
+ *
178
+ * @param {string} trackId
179
+ * @param {BandwidthLimit} bandwidth in kbps
180
+ * @returns {Promise<boolean>} success
181
+ */
182
+ setTrackBandwidth(trackId: string, bandwidth: BandwidthLimit): Promise<void>;
183
+ /**
184
+ * Updates maximum bandwidth for the given simulcast encoding of the given track.
185
+ *
186
+ * @param {string} trackId - id of the track
187
+ * @param {string} rid - rid of the encoding
188
+ * @param {BandwidthLimit} bandwidth - desired max bandwidth used by the encoding (in kbps)
189
+ * @returns
190
+ */
191
+ setEncodingBandwidth(trackId: string, rid: string, bandwidth: BandwidthLimit): Promise<void>;
192
+ /**
193
+ * Removes a track from connection that was sent to the RTC Engine.
194
+ * @param {string} trackId - Id of audio or video track to remove.
195
+ * @example
196
+ * ```ts
197
+ * // setup camera
198
+ * let localStream: MediaStream = new MediaStream();
199
+ * try {
200
+ * localVideoStream = await navigator.mediaDevices.getUserMedia(
201
+ * VIDEO_CONSTRAINTS
202
+ * );
203
+ * localVideoStream
204
+ * .getTracks()
205
+ * .forEach((track) => localStream.addTrack(track));
206
+ * } catch (error) {
207
+ * console.error("Couldn't get camera permission:", error);
208
+ * }
209
+ *
210
+ * let trackId
211
+ * localStream
212
+ * .getTracks()
213
+ * .forEach((track) => trackId = webrtc.addTrack(track, localStream));
214
+ *
215
+ * // remove track
216
+ * webrtc.removeTrack(trackId)
217
+ * ```
218
+ */
219
+ removeTrack(trackId: string): Promise<void>;
220
+ /**
221
+ * Sets track variant that server should send to the client library.
222
+ *
223
+ * The variant will be sent whenever it is available.
224
+ * If chosen variant is temporarily unavailable, some other variant
225
+ * will be sent until the chosen variant becomes active again.
226
+ *
227
+ * @param {string} trackId - id of track
228
+ * @param {Encoding} variant - variant to receive
229
+ * @example
230
+ * ```ts
231
+ * webrtc.setTargetTrackEncoding(incomingTrackCtx.trackId, "l")
232
+ * ```
233
+ */
234
+ setTargetTrackEncoding(trackId: string, variant: Encoding): void;
235
+ /**
236
+ * Enables track encoding so that it will be sent to the server.
237
+ * @param {string} trackId - id of track
238
+ * @param {Encoding} encoding - encoding that will be enabled
239
+ * @example
240
+ * ```ts
241
+ * const trackId = webrtc.addTrack(track, stream, {}, {enabled: true, activeEncodings: ["l", "m", "h"]});
242
+ * webrtc.disableTrackEncoding(trackId, "l");
243
+ * // wait some time
244
+ * webrtc.enableTrackEncoding(trackId, "l");
245
+ * ```
246
+ */
247
+ enableTrackEncoding: (trackId: string, encoding: Encoding) => Promise<void>;
248
+ /**
249
+ * Disables track encoding so that it will be no longer sent to the server.
250
+ * @param {string} trackId - id of track
251
+ * @param {Encoding} encoding - encoding that will be disabled
252
+ * @example
253
+ * ```ts
254
+ * const trackId = webrtc.addTrack(track, stream, {}, {enabled: true, activeEncodings: ["l", "m", "h"]});
255
+ * webrtc.disableTrackEncoding(trackId, "l");
256
+ * ```
257
+ */
258
+ disableTrackEncoding: (trackId: string, encoding: Encoding) => Promise<void>;
259
+ /**
260
+ * Updates the metadata for the current endpoint.
261
+ * @param metadata - Data about this endpoint that other endpoints will receive upon being added.
262
+ *
263
+ * If the metadata is different from what is already tracked in the room, the optional
264
+ * event `endpointUpdated` will be emitted for other endpoint in the room.
265
+ */
266
+ updateEndpointMetadata: (metadata: any) => void;
267
+ /**
268
+ * Updates the metadata for a specific track.
269
+ * @param trackId - trackId (generated in addTrack) of audio or video track.
270
+ * @param trackMetadata - Data about this track that other endpoint will receive upon being added.
271
+ *
272
+ * If the metadata is different from what is already tracked in the room, the optional
273
+ * event `trackUpdated` will be emitted for other endpoints in the room.
274
+ */
275
+ updateTrackMetadata: (trackId: string, trackMetadata: any) => void;
276
+ /**
277
+ * Disconnects from the room. This function should be called when user disconnects from the room
278
+ * in a clean way e.g. by clicking a dedicated, custom button `disconnect`.
279
+ * As a result there will be generated one more media event that should be
280
+ * sent to the RTC Engine. Thanks to it each other endpoint will be notified
281
+ * that endpoint was removed in {@link WebRTCEndpointEvents.endpointRemoved},
282
+ */
283
+ disconnect: () => void;
284
+ /**
285
+ * Cleans up {@link WebRTCEndpoint} instance.
286
+ */
287
+ cleanUp: () => void;
288
+ private getTrackId;
289
+ sendMediaEvent: (mediaEvent: MediaEvent) => void;
290
+ private createAndSendOffer;
291
+ private onOfferData;
292
+ private setConnection;
293
+ private onRemoteCandidate;
294
+ private onLocalCandidate;
295
+ private onIceCandidateError;
296
+ private onConnectionStateChange;
297
+ private onIceConnectionStateChange;
298
+ }
299
+ export {};