@auxilium/datalynk-client 1.1.6 → 1.1.8

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/README.md CHANGED
@@ -411,18 +411,24 @@ Connect to a WebRTC chatroom to send audio/video to peers
411
411
 
412
412
  ```js
413
413
  // Video elements to display stream on
414
- const local = document.querySelector('#video1');
415
- const remote = document.querySelector('#video1');
416
-
417
- const roomID = 'TEST_ROOM'
418
- const session = await api.webrtc.connect('test', (peer) => { // This callback fires everytime a new peer connects
419
- // Set remote audio/video stream to first track once availible
420
- const streams = peer.stream.getRemoteStreams();
421
- if(streams[0]) remoteVideo.srcObject = streams[0];
422
- else peer.stream.ontrack = ({streams}) => remoteVideo.srcObject = streams[0];
423
- }, true, false); // audio = true, video = false
424
- local.srcObject = session.stream; // Set local audio/video stream
414
+ const local = document.querySelector('#video1'); // <video id="video1" autoplay muted playsinline></video>
415
+ const remote = document.querySelector('#video2'); // <video id="video2" autoplay playsinline></video>
425
416
 
417
+ // Connect to a WebRTC room
418
+ const roomID = 'TEST_ROOM';
419
+ const session = await api.webrtc.connect(roomID, true, true); // audio = true, video = true
420
+
421
+ // Set local audio/video stream
422
+ local.srcObject = session.stream;
423
+
424
+ // Track is null on the initial connection, will fire again when the stream is ready
425
+ session.onConnected = (peer, stream) => {
426
+ // Set remote audio/video stream if ready
427
+ if(stream) remote.srcObject = stream;
428
+ }
429
+
430
+ // Disconnect from WebRTC room
431
+ session.disconnect();
426
432
  ```
427
433
 
428
434
  </details>
package/dist/index.js CHANGED
@@ -3245,20 +3245,23 @@ class Superuser {
3245
3245
  } });
3246
3246
  }
3247
3247
  }
3248
- const version = "1.1.6";
3248
+ const version = "1.1.8";
3249
3249
  class WebRtc {
3250
3250
  constructor(api) {
3251
3251
  __publicField(this, "ice");
3252
- var _a, _b, _c;
3252
+ var _a, _b, _c, _d;
3253
3253
  this.api = api;
3254
- this.ice = clean({
3255
- urls: [(_a = this.api.options.webrtc) == null ? void 0 : _a.url],
3256
- username: (_b = this.api.options.webrtc) == null ? void 0 : _b.username,
3257
- credential: (_c = this.api.options.webrtc) == null ? void 0 : _c.password
3258
- });
3254
+ this.ice = [
3255
+ { urls: ["stun:" + ((_a = this.api.options.webrtc) == null ? void 0 : _a.url)] },
3256
+ clean({
3257
+ urls: ["turn:" + ((_b = this.api.options.webrtc) == null ? void 0 : _b.url)],
3258
+ username: (_c = this.api.options.webrtc) == null ? void 0 : _c.username,
3259
+ credential: (_d = this.api.options.webrtc) == null ? void 0 : _d.password
3260
+ })
3261
+ ];
3259
3262
  }
3260
3263
  async answer(offer, stream) {
3261
- const rtc = new RTCPeerConnection({ iceServers: [this.ice] });
3264
+ const rtc = new RTCPeerConnection({ iceServers: this.ice });
3262
3265
  stream.getTracks().forEach((track) => rtc.addTrack(track, stream));
3263
3266
  await rtc.setRemoteDescription(new RTCSessionDescription(offer));
3264
3267
  const answer = await rtc.createAnswer();
@@ -3272,7 +3275,7 @@ class WebRtc {
3272
3275
  return rtc;
3273
3276
  }
3274
3277
  async offer(stream) {
3275
- const rtc = new RTCPeerConnection({ iceServers: [this.ice] });
3278
+ const rtc = new RTCPeerConnection({ iceServers: this.ice });
3276
3279
  stream.getTracks().forEach((track) => rtc.addTrack(track, stream));
3277
3280
  await rtc.setLocalDescription(await rtc.createOffer());
3278
3281
  await new Promise((res) => {
@@ -3286,19 +3289,28 @@ class WebRtc {
3286
3289
  /**
3287
3290
  * Create a new WebRTC connection to a room
3288
3291
  * @param {string} id Room ID
3289
- * @param {(peer) => any} onConnect Callback when peers connect
3290
3292
  * @param {boolean} audio Stream local audio to room/peers
3291
3293
  * @param {boolean} video Stream local video to room/peers
3292
3294
  * @returns {Promise<WebRTCSession>} Connection session
3293
3295
  */
3294
- async connect(id = randomStringBuilder(16, true, true), onConnect, audio = true, video = true) {
3296
+ async connect(id = randomStringBuilder(16, true, true), audio = true, video = true) {
3295
3297
  const session = {
3296
3298
  open: true,
3297
3299
  id,
3298
3300
  uid: randomStringBuilder(8, true, true),
3299
3301
  peers: {},
3300
3302
  stream: await navigator.mediaDevices.getUserMedia({ audio, video }),
3301
- disconnect: null
3303
+ disconnect: null,
3304
+ onConnected: null
3305
+ };
3306
+ const handleTracks = (session2, uid) => {
3307
+ if (!session2.open || !session2.onConnected) return;
3308
+ const peer = session2.peers[uid];
3309
+ const streams = peer.connection.getRemoteStreams();
3310
+ if (streams[0]) session2.onConnected(peer, streams[0]);
3311
+ else peer.connection.ontrack = ({ streams: streams2 }) => {
3312
+ if (session2.open && session2.onConnected) session2.onConnected(peer, streams2[0]);
3313
+ };
3302
3314
  };
3303
3315
  const unsubscribe = this.api.socket.addListener(async (event) => {
3304
3316
  var _a, _b, _c, _d;
@@ -3309,34 +3321,34 @@ class WebRtc {
3309
3321
  session.peers[payload.uid] = {
3310
3322
  uid: payload.uid,
3311
3323
  username: payload.username,
3312
- stream: await this.offer(session.stream)
3324
+ connection: await this.offer(session.stream)
3313
3325
  };
3314
3326
  this.api.socket.send({ [`webrtc/${id}`]: { offer: {
3315
3327
  to: payload.uid,
3316
3328
  uid: session.uid,
3317
3329
  username: (_a = this.api.auth.user) == null ? void 0 : _a.login,
3318
- offer: session.peers[payload.uid].stream.localDescription
3330
+ offer: session.peers[payload.uid].connection.localDescription
3319
3331
  } } });
3320
3332
  } else if (event[key].disconnected) {
3321
- session.peers[event[key].disconnected.uid].stream.close();
3333
+ session.peers[event[key].disconnected.uid].connection.close();
3322
3334
  delete session.peers[event[key].disconnected.uid];
3323
3335
  } else if (event[key].offer && event[key].offer.to == session.uid) {
3324
3336
  const payload = event[key].offer;
3325
3337
  session.peers[payload.uid] = {
3326
3338
  uid: payload.uid,
3327
3339
  username: payload.username,
3328
- stream: await this.answer(payload.offer, session.stream)
3340
+ connection: await this.answer(payload.offer, session.stream)
3329
3341
  };
3330
3342
  this.api.socket.send({ [`webrtc/${id}`]: { answer: {
3331
3343
  to: payload.uid,
3332
3344
  uid: session.uid,
3333
3345
  username: (_b = this.api.auth.user) == null ? void 0 : _b.login,
3334
- answer: session.peers[payload.uid].stream.localDescription
3346
+ answer: session.peers[payload.uid].connection.localDescription
3335
3347
  } } });
3336
- if (onConnect) onConnect(session.peers[payload.uid]);
3348
+ handleTracks(session, payload.uid);
3337
3349
  } else if (event[key].answer && event[key].answer.to == session.uid) {
3338
- (_d = (_c = session.peers[event[key].answer.uid]) == null ? void 0 : _c.stream) == null ? void 0 : _d.setRemoteDescription(new RTCSessionDescription(event[key].answer.answer));
3339
- if (onConnect) onConnect(session.peers[event[key].answer.uid]);
3350
+ (_d = (_c = session.peers[event[key].answer.uid]) == null ? void 0 : _c.connection) == null ? void 0 : _d.setRemoteDescription(new RTCSessionDescription(event[key].answer.answer));
3351
+ handleTracks(session, event[key].answer.uid);
3340
3352
  }
3341
3353
  }
3342
3354
  }, () => {
@@ -3410,7 +3422,7 @@ const _Api = class _Api {
3410
3422
  saveSession: true,
3411
3423
  serviceWorker: "/service.worker.mjs",
3412
3424
  webrtc: {
3413
- url: "turn:scarborough.auxilium.world:3478",
3425
+ url: "scarborough.auxilium.world:3478",
3414
3426
  username: "auxilium-webrtc",
3415
3427
  password: "UU3qkD95Tg54eF"
3416
3428
  },
package/dist/index.mjs CHANGED
@@ -3243,20 +3243,23 @@ class Superuser {
3243
3243
  } });
3244
3244
  }
3245
3245
  }
3246
- const version = "1.1.6";
3246
+ const version = "1.1.8";
3247
3247
  class WebRtc {
3248
3248
  constructor(api) {
3249
3249
  __publicField(this, "ice");
3250
- var _a, _b, _c;
3250
+ var _a, _b, _c, _d;
3251
3251
  this.api = api;
3252
- this.ice = clean({
3253
- urls: [(_a = this.api.options.webrtc) == null ? void 0 : _a.url],
3254
- username: (_b = this.api.options.webrtc) == null ? void 0 : _b.username,
3255
- credential: (_c = this.api.options.webrtc) == null ? void 0 : _c.password
3256
- });
3252
+ this.ice = [
3253
+ { urls: ["stun:" + ((_a = this.api.options.webrtc) == null ? void 0 : _a.url)] },
3254
+ clean({
3255
+ urls: ["turn:" + ((_b = this.api.options.webrtc) == null ? void 0 : _b.url)],
3256
+ username: (_c = this.api.options.webrtc) == null ? void 0 : _c.username,
3257
+ credential: (_d = this.api.options.webrtc) == null ? void 0 : _d.password
3258
+ })
3259
+ ];
3257
3260
  }
3258
3261
  async answer(offer, stream) {
3259
- const rtc = new RTCPeerConnection({ iceServers: [this.ice] });
3262
+ const rtc = new RTCPeerConnection({ iceServers: this.ice });
3260
3263
  stream.getTracks().forEach((track) => rtc.addTrack(track, stream));
3261
3264
  await rtc.setRemoteDescription(new RTCSessionDescription(offer));
3262
3265
  const answer = await rtc.createAnswer();
@@ -3270,7 +3273,7 @@ class WebRtc {
3270
3273
  return rtc;
3271
3274
  }
3272
3275
  async offer(stream) {
3273
- const rtc = new RTCPeerConnection({ iceServers: [this.ice] });
3276
+ const rtc = new RTCPeerConnection({ iceServers: this.ice });
3274
3277
  stream.getTracks().forEach((track) => rtc.addTrack(track, stream));
3275
3278
  await rtc.setLocalDescription(await rtc.createOffer());
3276
3279
  await new Promise((res) => {
@@ -3284,19 +3287,28 @@ class WebRtc {
3284
3287
  /**
3285
3288
  * Create a new WebRTC connection to a room
3286
3289
  * @param {string} id Room ID
3287
- * @param {(peer) => any} onConnect Callback when peers connect
3288
3290
  * @param {boolean} audio Stream local audio to room/peers
3289
3291
  * @param {boolean} video Stream local video to room/peers
3290
3292
  * @returns {Promise<WebRTCSession>} Connection session
3291
3293
  */
3292
- async connect(id = randomStringBuilder(16, true, true), onConnect, audio = true, video = true) {
3294
+ async connect(id = randomStringBuilder(16, true, true), audio = true, video = true) {
3293
3295
  const session = {
3294
3296
  open: true,
3295
3297
  id,
3296
3298
  uid: randomStringBuilder(8, true, true),
3297
3299
  peers: {},
3298
3300
  stream: await navigator.mediaDevices.getUserMedia({ audio, video }),
3299
- disconnect: null
3301
+ disconnect: null,
3302
+ onConnected: null
3303
+ };
3304
+ const handleTracks = (session2, uid) => {
3305
+ if (!session2.open || !session2.onConnected) return;
3306
+ const peer = session2.peers[uid];
3307
+ const streams = peer.connection.getRemoteStreams();
3308
+ if (streams[0]) session2.onConnected(peer, streams[0]);
3309
+ else peer.connection.ontrack = ({ streams: streams2 }) => {
3310
+ if (session2.open && session2.onConnected) session2.onConnected(peer, streams2[0]);
3311
+ };
3300
3312
  };
3301
3313
  const unsubscribe = this.api.socket.addListener(async (event) => {
3302
3314
  var _a, _b, _c, _d;
@@ -3307,34 +3319,34 @@ class WebRtc {
3307
3319
  session.peers[payload.uid] = {
3308
3320
  uid: payload.uid,
3309
3321
  username: payload.username,
3310
- stream: await this.offer(session.stream)
3322
+ connection: await this.offer(session.stream)
3311
3323
  };
3312
3324
  this.api.socket.send({ [`webrtc/${id}`]: { offer: {
3313
3325
  to: payload.uid,
3314
3326
  uid: session.uid,
3315
3327
  username: (_a = this.api.auth.user) == null ? void 0 : _a.login,
3316
- offer: session.peers[payload.uid].stream.localDescription
3328
+ offer: session.peers[payload.uid].connection.localDescription
3317
3329
  } } });
3318
3330
  } else if (event[key].disconnected) {
3319
- session.peers[event[key].disconnected.uid].stream.close();
3331
+ session.peers[event[key].disconnected.uid].connection.close();
3320
3332
  delete session.peers[event[key].disconnected.uid];
3321
3333
  } else if (event[key].offer && event[key].offer.to == session.uid) {
3322
3334
  const payload = event[key].offer;
3323
3335
  session.peers[payload.uid] = {
3324
3336
  uid: payload.uid,
3325
3337
  username: payload.username,
3326
- stream: await this.answer(payload.offer, session.stream)
3338
+ connection: await this.answer(payload.offer, session.stream)
3327
3339
  };
3328
3340
  this.api.socket.send({ [`webrtc/${id}`]: { answer: {
3329
3341
  to: payload.uid,
3330
3342
  uid: session.uid,
3331
3343
  username: (_b = this.api.auth.user) == null ? void 0 : _b.login,
3332
- answer: session.peers[payload.uid].stream.localDescription
3344
+ answer: session.peers[payload.uid].connection.localDescription
3333
3345
  } } });
3334
- if (onConnect) onConnect(session.peers[payload.uid]);
3346
+ handleTracks(session, payload.uid);
3335
3347
  } else if (event[key].answer && event[key].answer.to == session.uid) {
3336
- (_d = (_c = session.peers[event[key].answer.uid]) == null ? void 0 : _c.stream) == null ? void 0 : _d.setRemoteDescription(new RTCSessionDescription(event[key].answer.answer));
3337
- if (onConnect) onConnect(session.peers[event[key].answer.uid]);
3348
+ (_d = (_c = session.peers[event[key].answer.uid]) == null ? void 0 : _c.connection) == null ? void 0 : _d.setRemoteDescription(new RTCSessionDescription(event[key].answer.answer));
3349
+ handleTracks(session, event[key].answer.uid);
3338
3350
  }
3339
3351
  }
3340
3352
  }, () => {
@@ -3408,7 +3420,7 @@ const _Api = class _Api {
3408
3420
  saveSession: true,
3409
3421
  serviceWorker: "/service.worker.mjs",
3410
3422
  webrtc: {
3411
- url: "turn:scarborough.auxilium.world:3478",
3423
+ url: "scarborough.auxilium.world:3478",
3412
3424
  username: "auxilium-webrtc",
3413
3425
  password: "UU3qkD95Tg54eF"
3414
3426
  },
package/dist/webrtc.d.ts CHANGED
@@ -1,5 +1,14 @@
1
1
  import { Unsubscribe } from '@ztimson/utils';
2
2
  import { Api } from './api';
3
+ /** WebRTC peer */
4
+ export type WebRTCPeer = {
5
+ /** Peer connection UID */
6
+ uid: string;
7
+ /** Peer username */
8
+ username: string;
9
+ /** Remote connection to stream */
10
+ connection: RTCPeerConnection;
11
+ };
3
12
  /** WebRTC session */
4
13
  export type WebRTCSession = {
5
14
  /** Is the connection active? */
@@ -10,19 +19,14 @@ export type WebRTCSession = {
10
19
  uid: string;
11
20
  /** Local media stream */
12
21
  stream: MediaStream;
13
- /** Connected peers */
22
+ /** Connected peers sorted by UID */
14
23
  peers: {
15
- [key: string]: {
16
- /** Peer connection UID */
17
- uid: string;
18
- /** Peer username */
19
- username: string;
20
- /** Remote connection to stream */
21
- stream: RTCPeerConnection;
22
- };
24
+ [key: string]: WebRTCPeer;
23
25
  };
24
26
  /** Disconnection session */
25
27
  disconnect: Unsubscribe;
28
+ /** Callback when peers connect */
29
+ onConnected?: (peer: WebRTCPeer, track: any) => any;
26
30
  };
27
31
  export declare class WebRtc {
28
32
  private api;
@@ -30,18 +34,17 @@ export declare class WebRtc {
30
34
  urls: string[];
31
35
  username?: string;
32
36
  credential?: string;
33
- };
37
+ }[];
34
38
  constructor(api: Api);
35
39
  private answer;
36
40
  private offer;
37
41
  /**
38
42
  * Create a new WebRTC connection to a room
39
43
  * @param {string} id Room ID
40
- * @param {(peer) => any} onConnect Callback when peers connect
41
44
  * @param {boolean} audio Stream local audio to room/peers
42
45
  * @param {boolean} video Stream local video to room/peers
43
46
  * @returns {Promise<WebRTCSession>} Connection session
44
47
  */
45
- connect(id?: string, onConnect?: (peer: any) => any, audio?: boolean, video?: boolean): Promise<WebRTCSession>;
48
+ connect(id?: string, audio?: boolean, video?: boolean): Promise<WebRTCSession>;
46
49
  }
47
50
  //# sourceMappingURL=webrtc.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"webrtc.d.ts","sourceRoot":"","sources":["../src/webrtc.ts"],"names":[],"mappings":"AAAA,OAAO,EAA6B,WAAW,EAAC,MAAM,gBAAgB,CAAC;AACvE,OAAO,EAAC,GAAG,EAAC,MAAM,OAAO,CAAC;AAE1B,qBAAqB;AACrB,MAAM,MAAM,aAAa,GAAG;IAC3B,gCAAgC;IAChC,IAAI,EAAE,OAAO,CAAC;IACd,cAAc;IACd,EAAE,EAAE,MAAM,CAAC;IACX,qBAAqB;IACrB,GAAG,EAAE,MAAM,CAAC;IACZ,yBAAyB;IACzB,MAAM,EAAE,WAAW,CAAC;IACpB,sBAAsB;IACtB,KAAK,EAAE;QAAC,CAAC,GAAG,EAAE,MAAM,GAAG;YACtB,0BAA0B;YAC1B,GAAG,EAAE,MAAM,CAAC;YACZ,oBAAoB;YACpB,QAAQ,EAAE,MAAM,CAAC;YACjB,kCAAkC;YAClC,MAAM,EAAE,iBAAiB,CAAA;SACzB,CAAA;KAAC,CAAA;IACF,4BAA4B;IAC5B,UAAU,EAAE,WAAW,CAAA;CACvB,CAAA;AAED,qBAAa,MAAM;IAGN,OAAO,CAAC,GAAG;IAF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Aa,MAAM;IAGN,OAAO,CAAC,GAAG;IAFvB,SAAgB,GAAG,EAAG;QAAC,IAAI,EAAE,MAAM,EAAE,CAAC;QAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;QAAC,UAAU,CAAC,EAAE,MAAM,CAAA;KAAC,EAAE,CAAC;gBAE7D,GAAG,EAAE,GAAG;YAWd,MAAM;YAaN,KAAK;IAWnB;;;;;;OAMG;IACG,OAAO,CAAC,EAAE,GAAE,MAA4C,EAAE,KAAK,UAAO,EAAE,KAAK,UAAO,GAAG,OAAO,CAAC,aAAa,CAAC;CAoEnH"}
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@auxilium/datalynk-client",
3
3
  "description": "Datalynk client library",
4
4
  "repository": "https://gitlab.auxiliumgroup.com/auxilium/datalynk/datalynk-client",
5
- "version": "1.1.6",
5
+ "version": "1.1.8",
6
6
  "author": "Zak Timson <zaktimson@gmail.com>",
7
7
  "private": false,
8
8
  "main": "./dist/index.cjs",