@auxilium/datalynk-client 1.1.6 → 1.1.7

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,7 +3245,7 @@ class Superuser {
3245
3245
  } });
3246
3246
  }
3247
3247
  }
3248
- const version = "1.1.6";
3248
+ const version = "1.1.7";
3249
3249
  class WebRtc {
3250
3250
  constructor(api) {
3251
3251
  __publicField(this, "ice");
@@ -3291,14 +3291,24 @@ class WebRtc {
3291
3291
  * @param {boolean} video Stream local video to room/peers
3292
3292
  * @returns {Promise<WebRTCSession>} Connection session
3293
3293
  */
3294
- async connect(id = randomStringBuilder(16, true, true), onConnect, audio = true, video = true) {
3294
+ async connect(id = randomStringBuilder(16, true, true), audio = true, video = true) {
3295
3295
  const session = {
3296
3296
  open: true,
3297
3297
  id,
3298
3298
  uid: randomStringBuilder(8, true, true),
3299
3299
  peers: {},
3300
3300
  stream: await navigator.mediaDevices.getUserMedia({ audio, video }),
3301
- 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
+ };
3302
3312
  };
3303
3313
  const unsubscribe = this.api.socket.addListener(async (event) => {
3304
3314
  var _a, _b, _c, _d;
@@ -3309,34 +3319,34 @@ class WebRtc {
3309
3319
  session.peers[payload.uid] = {
3310
3320
  uid: payload.uid,
3311
3321
  username: payload.username,
3312
- stream: await this.offer(session.stream)
3322
+ connection: await this.offer(session.stream)
3313
3323
  };
3314
3324
  this.api.socket.send({ [`webrtc/${id}`]: { offer: {
3315
3325
  to: payload.uid,
3316
3326
  uid: session.uid,
3317
3327
  username: (_a = this.api.auth.user) == null ? void 0 : _a.login,
3318
- offer: session.peers[payload.uid].stream.localDescription
3328
+ offer: session.peers[payload.uid].connection.localDescription
3319
3329
  } } });
3320
3330
  } else if (event[key].disconnected) {
3321
- session.peers[event[key].disconnected.uid].stream.close();
3331
+ session.peers[event[key].disconnected.uid].connection.close();
3322
3332
  delete session.peers[event[key].disconnected.uid];
3323
3333
  } else if (event[key].offer && event[key].offer.to == session.uid) {
3324
3334
  const payload = event[key].offer;
3325
3335
  session.peers[payload.uid] = {
3326
3336
  uid: payload.uid,
3327
3337
  username: payload.username,
3328
- stream: await this.answer(payload.offer, session.stream)
3338
+ connection: await this.answer(payload.offer, session.stream)
3329
3339
  };
3330
3340
  this.api.socket.send({ [`webrtc/${id}`]: { answer: {
3331
3341
  to: payload.uid,
3332
3342
  uid: session.uid,
3333
3343
  username: (_b = this.api.auth.user) == null ? void 0 : _b.login,
3334
- answer: session.peers[payload.uid].stream.localDescription
3344
+ answer: session.peers[payload.uid].connection.localDescription
3335
3345
  } } });
3336
- if (onConnect) onConnect(session.peers[payload.uid]);
3346
+ handleTracks(session, payload.uid);
3337
3347
  } 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]);
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);
3340
3350
  }
3341
3351
  }
3342
3352
  }, () => {
package/dist/index.mjs CHANGED
@@ -3243,7 +3243,7 @@ class Superuser {
3243
3243
  } });
3244
3244
  }
3245
3245
  }
3246
- const version = "1.1.6";
3246
+ const version = "1.1.7";
3247
3247
  class WebRtc {
3248
3248
  constructor(api) {
3249
3249
  __publicField(this, "ice");
@@ -3289,14 +3289,24 @@ class WebRtc {
3289
3289
  * @param {boolean} video Stream local video to room/peers
3290
3290
  * @returns {Promise<WebRTCSession>} Connection session
3291
3291
  */
3292
- async connect(id = randomStringBuilder(16, true, true), onConnect, audio = true, video = true) {
3292
+ async connect(id = randomStringBuilder(16, true, true), audio = true, video = true) {
3293
3293
  const session = {
3294
3294
  open: true,
3295
3295
  id,
3296
3296
  uid: randomStringBuilder(8, true, true),
3297
3297
  peers: {},
3298
3298
  stream: await navigator.mediaDevices.getUserMedia({ audio, video }),
3299
- disconnect: null
3299
+ disconnect: null,
3300
+ onConnected: null
3301
+ };
3302
+ const handleTracks = (session2, uid) => {
3303
+ if (!session2.open || !session2.onConnected) return;
3304
+ const peer = session2.peers[uid];
3305
+ const streams = peer.connection.getRemoteStreams();
3306
+ if (streams[0]) session2.onConnected(peer, streams[0]);
3307
+ else peer.connection.ontrack = ({ streams: streams2 }) => {
3308
+ if (session2.open && session2.onConnected) session2.onConnected(peer, streams2[0]);
3309
+ };
3300
3310
  };
3301
3311
  const unsubscribe = this.api.socket.addListener(async (event) => {
3302
3312
  var _a, _b, _c, _d;
@@ -3307,34 +3317,34 @@ class WebRtc {
3307
3317
  session.peers[payload.uid] = {
3308
3318
  uid: payload.uid,
3309
3319
  username: payload.username,
3310
- stream: await this.offer(session.stream)
3320
+ connection: await this.offer(session.stream)
3311
3321
  };
3312
3322
  this.api.socket.send({ [`webrtc/${id}`]: { offer: {
3313
3323
  to: payload.uid,
3314
3324
  uid: session.uid,
3315
3325
  username: (_a = this.api.auth.user) == null ? void 0 : _a.login,
3316
- offer: session.peers[payload.uid].stream.localDescription
3326
+ offer: session.peers[payload.uid].connection.localDescription
3317
3327
  } } });
3318
3328
  } else if (event[key].disconnected) {
3319
- session.peers[event[key].disconnected.uid].stream.close();
3329
+ session.peers[event[key].disconnected.uid].connection.close();
3320
3330
  delete session.peers[event[key].disconnected.uid];
3321
3331
  } else if (event[key].offer && event[key].offer.to == session.uid) {
3322
3332
  const payload = event[key].offer;
3323
3333
  session.peers[payload.uid] = {
3324
3334
  uid: payload.uid,
3325
3335
  username: payload.username,
3326
- stream: await this.answer(payload.offer, session.stream)
3336
+ connection: await this.answer(payload.offer, session.stream)
3327
3337
  };
3328
3338
  this.api.socket.send({ [`webrtc/${id}`]: { answer: {
3329
3339
  to: payload.uid,
3330
3340
  uid: session.uid,
3331
3341
  username: (_b = this.api.auth.user) == null ? void 0 : _b.login,
3332
- answer: session.peers[payload.uid].stream.localDescription
3342
+ answer: session.peers[payload.uid].connection.localDescription
3333
3343
  } } });
3334
- if (onConnect) onConnect(session.peers[payload.uid]);
3344
+ handleTracks(session, payload.uid);
3335
3345
  } 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]);
3346
+ (_d = (_c = session.peers[event[key].answer.uid]) == null ? void 0 : _c.connection) == null ? void 0 : _d.setRemoteDescription(new RTCSessionDescription(event[key].answer.answer));
3347
+ handleTracks(session, event[key].answer.uid);
3338
3348
  }
3339
3349
  }
3340
3350
  }, () => {
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;
@@ -42,6 +46,6 @@ export declare class WebRtc {
42
46
  * @param {boolean} video Stream local video to room/peers
43
47
  * @returns {Promise<WebRTCSession>} Connection session
44
48
  */
45
- connect(id?: string, onConnect?: (peer: any) => any, audio?: boolean, video?: boolean): Promise<WebRTCSession>;
49
+ connect(id?: string, audio?: boolean, video?: boolean): Promise<WebRTCSession>;
46
50
  }
47
51
  //# 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;IAFvB,SAAgB,GAAG,EAAG;QAAC,IAAI,EAAE,MAAM,EAAE,CAAC;QAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;QAAC,UAAU,CAAC,EAAE,MAAM,CAAA;KAAC,CAAC;gBAE3D,GAAG,EAAE,GAAG;YAQd,MAAM;YAaN,KAAK;IAWnB;;;;;;;OAOG;IACG,OAAO,CAAC,EAAE,GAAE,MAA4C,EAAE,SAAS,CAAC,EAAE,CAAC,IAAI,KAAA,KAAK,GAAG,EAAE,KAAK,UAAO,EAAE,KAAK,UAAO,GAAG,OAAO,CAAC,aAAa,CAAC;CAyD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ED
@@ -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.7",
6
6
  "author": "Zak Timson <zaktimson@gmail.com>",
7
7
  "private": false,
8
8
  "main": "./dist/index.cjs",