werift 0.15.2 → 0.15.5

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 (57) hide show
  1. package/lib/common/src/index.js +5 -1
  2. package/lib/common/src/index.js.map +1 -1
  3. package/lib/common/src/promise.d.ts +4 -3
  4. package/lib/common/src/promise.js +11 -5
  5. package/lib/common/src/promise.js.map +1 -1
  6. package/lib/dtls/src/cipher/namedCurve.js +5 -1
  7. package/lib/dtls/src/cipher/namedCurve.js.map +1 -1
  8. package/lib/dtls/src/cipher/prf.js +5 -1
  9. package/lib/dtls/src/cipher/prf.js.map +1 -1
  10. package/lib/dtls/src/cipher/suites/aead.js +5 -1
  11. package/lib/dtls/src/cipher/suites/aead.js.map +1 -1
  12. package/lib/dtls/src/context/cipher.js +5 -1
  13. package/lib/dtls/src/context/cipher.js.map +1 -1
  14. package/lib/ice/src/dns/lookup.d.ts +9 -0
  15. package/lib/ice/src/dns/lookup.js +66 -0
  16. package/lib/ice/src/dns/lookup.js.map +1 -0
  17. package/lib/ice/src/ice.d.ts +2 -0
  18. package/lib/ice/src/ice.js +28 -24
  19. package/lib/ice/src/ice.js.map +1 -1
  20. package/lib/ice/src/index.js +5 -1
  21. package/lib/ice/src/index.js.map +1 -1
  22. package/lib/ice/src/stun/attributes.js +5 -1
  23. package/lib/ice/src/stun/attributes.js.map +1 -1
  24. package/lib/rtp/src/container/webm.js +5 -1
  25. package/lib/rtp/src/container/webm.js.map +1 -1
  26. package/lib/rtp/src/index.js +5 -1
  27. package/lib/rtp/src/index.js.map +1 -1
  28. package/lib/webrtc/src/index.js +5 -1
  29. package/lib/webrtc/src/index.js.map +1 -1
  30. package/lib/webrtc/src/media/rtpReceiver.js +1 -1
  31. package/lib/webrtc/src/media/rtpReceiver.js.map +1 -1
  32. package/lib/webrtc/src/media/rtpSender.js +5 -1
  33. package/lib/webrtc/src/media/rtpSender.js.map +1 -1
  34. package/lib/webrtc/src/media/rtpTransceiver.d.ts +3 -1
  35. package/lib/webrtc/src/media/rtpTransceiver.js +12 -2
  36. package/lib/webrtc/src/media/rtpTransceiver.js.map +1 -1
  37. package/lib/webrtc/src/nonstandard/recorder/writer/webm.js +5 -1
  38. package/lib/webrtc/src/nonstandard/recorder/writer/webm.js.map +1 -1
  39. package/lib/webrtc/src/peerConnection.d.ts +6 -2
  40. package/lib/webrtc/src/peerConnection.js +126 -52
  41. package/lib/webrtc/src/peerConnection.js.map +1 -1
  42. package/lib/webrtc/src/sdp.d.ts +0 -2
  43. package/lib/webrtc/src/sdp.js +0 -7
  44. package/lib/webrtc/src/sdp.js.map +1 -1
  45. package/lib/webrtc/src/transport/ice.d.ts +7 -1
  46. package/lib/webrtc/src/transport/ice.js +12 -3
  47. package/lib/webrtc/src/transport/ice.js.map +1 -1
  48. package/lib/webrtc/src/transport/sctp.d.ts +2 -1
  49. package/lib/webrtc/src/transport/sctp.js +6 -2
  50. package/lib/webrtc/src/transport/sctp.js.map +1 -1
  51. package/package.json +9 -19
  52. package/src/media/rtpReceiver.ts +1 -1
  53. package/src/media/rtpTransceiver.ts +7 -1
  54. package/src/peerConnection.ts +197 -124
  55. package/src/sdp.ts +0 -8
  56. package/src/transport/ice.ts +13 -3
  57. package/src/transport/sctp.ts +2 -1
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -190,10 +194,18 @@ class RTCPeerConnection extends helper_1.EventTarget {
190
194
  }
191
195
  async createOffer() {
192
196
  await this.ensureCerts();
197
+ const description = this.buildOfferSdp();
198
+ return description.toJSON();
199
+ }
200
+ buildOfferSdp() {
193
201
  this.transceivers.forEach((transceiver) => {
194
- transceiver.codecs = this.configuration.codecs[transceiver.kind];
195
- transceiver.headerExtensions =
196
- this.configuration.headerExtensions[transceiver.kind];
202
+ if (transceiver.codecs.length === 0) {
203
+ transceiver.codecs = this.configuration.codecs[transceiver.kind];
204
+ }
205
+ if (transceiver.headerExtensions.length === 0) {
206
+ transceiver.headerExtensions =
207
+ this.configuration.headerExtensions[transceiver.kind];
208
+ }
197
209
  });
198
210
  const description = new sdp_1.SessionDescription();
199
211
  (0, sdp_1.addSDPHeader)("offer", description);
@@ -208,16 +220,19 @@ class RTCPeerConnection extends helper_1.EventTarget {
208
220
  return;
209
221
  }
210
222
  if (m.kind === "application") {
211
- description.media.push(createMediaDescriptionForSctp(this.sctpTransport, mid));
223
+ if (!this.sctpTransport) {
224
+ throw new Error("sctpTransport not found");
225
+ }
226
+ this.sctpTransport.mLineIndex = i;
227
+ description.media.push(createMediaDescriptionForSctp(this.sctpTransport));
212
228
  }
213
229
  else {
214
230
  const transceiver = this.getTransceiverByMid(mid);
215
231
  if (!transceiver) {
216
- log("transceiver by mid not found", mid);
217
- return;
232
+ throw new Error("transceiver not found");
218
233
  }
219
234
  transceiver.mLineIndex = i;
220
- description.media.push(createMediaDescriptionForTransceiver(transceiver, this.cname, transceiver.direction, mid));
235
+ description.media.push(createMediaDescriptionForTransceiver(transceiver, this.cname, transceiver.direction));
221
236
  }
222
237
  });
223
238
  // # handle new transceivers / sctp
@@ -225,11 +240,18 @@ class RTCPeerConnection extends helper_1.EventTarget {
225
240
  .filter((t) => !description.media.find((m) => m.rtp.muxId === t.mid))
226
241
  .forEach((transceiver) => {
227
242
  transceiver.mLineIndex = description.media.length;
228
- description.media.push(createMediaDescriptionForTransceiver(transceiver, this.cname, transceiver.direction, allocateMid(this.seenMid)));
243
+ if (transceiver.mid == undefined) {
244
+ transceiver.mid = allocateMid(this.seenMid) + "_srtp";
245
+ }
246
+ description.media.push(createMediaDescriptionForTransceiver(transceiver, this.cname, transceiver.direction));
229
247
  });
230
248
  if (this.sctpTransport &&
231
- !description.media.find((m) => this.sctpTransport.mid === m.rtp.muxId)) {
232
- description.media.push(createMediaDescriptionForSctp(this.sctpTransport, allocateMid(this.seenMid)));
249
+ !description.media.find((m) => m.kind === "application")) {
250
+ this.sctpTransport.mLineIndex = description.media.length;
251
+ if (this.sctpTransport.mid == undefined) {
252
+ this.sctpTransport.mid = allocateMid(this.seenMid) + "_sctp";
253
+ }
254
+ description.media.push(createMediaDescriptionForSctp(this.sctpTransport));
233
255
  }
234
256
  if (this.configuration.bundlePolicy !== "disable") {
235
257
  const mids = description.media
@@ -238,7 +260,7 @@ class RTCPeerConnection extends helper_1.EventTarget {
238
260
  const bundle = new sdp_1.GroupDescription("BUNDLE", mids);
239
261
  description.group.push(bundle);
240
262
  }
241
- return description.toJSON();
263
+ return description;
242
264
  }
243
265
  createDataChannel(label, options = {}) {
244
266
  const base = {
@@ -317,18 +339,26 @@ class RTCPeerConnection extends helper_1.EventTarget {
317
339
  iceTransport.iceGather.onIceCandidate = (candidate) => {
318
340
  if (!this.localDescription)
319
341
  return;
320
- const sdp = sdp_1.SessionDescription.parse(this.localDescription.sdp);
321
- const media = sdp.media[0];
322
- if (!media) {
323
- log("media not exist");
324
- return;
342
+ if (this.configuration.bundlePolicy === "max-bundle") {
343
+ candidate.sdpMLineIndex = 0;
344
+ candidate.sdpMid = this._localDescription?.media[0].rtp.muxId;
345
+ }
346
+ else {
347
+ const transceiver = this.transceivers.find((t) => t.dtlsTransport.iceTransport.id === iceTransport.id);
348
+ if (transceiver) {
349
+ candidate.sdpMLineIndex = transceiver.mLineIndex;
350
+ candidate.sdpMid = transceiver.mid;
351
+ }
352
+ if (this.sctpTransport?.dtlsTransport.iceTransport.id === iceTransport.id) {
353
+ candidate.sdpMLineIndex = this.sctpTransport.mLineIndex;
354
+ candidate.sdpMid = this.sctpTransport.mid;
355
+ }
325
356
  }
326
- candidate.sdpMLineIndex = 0;
327
- candidate.sdpMid = media.rtp.muxId;
328
357
  candidate.foundation = "candidate:" + candidate.foundation;
329
358
  this.onIceCandidate.execute(candidate.toJSON());
330
- if (this.onicecandidate)
359
+ if (this.onicecandidate) {
331
360
  this.onicecandidate({ candidate: candidate.toJSON() });
361
+ }
332
362
  this.emit("icecandidate", { candidate });
333
363
  };
334
364
  const dtlsTransport = new dtls_1.RTCDtlsTransport(iceTransport, this.router, this.certificates, srtpProfiles);
@@ -412,10 +442,16 @@ class RTCPeerConnection extends helper_1.EventTarget {
412
442
  });
413
443
  // for trickle ice
414
444
  this.setLocal(description);
415
- // # gather candidates
416
- for (const iceTransport of this.iceTransports) {
417
- await iceTransport.iceGather.gather();
445
+ // connect transports
446
+ if (description.type === "answer") {
447
+ log("callee start connect");
448
+ this.connect().catch((err) => {
449
+ log("connect failed", err);
450
+ this.setConnectionState("failed");
451
+ });
418
452
  }
453
+ // # gather candidates
454
+ await Promise.all(this.iceTransports.map((iceTransport) => iceTransport.iceGather.gather()));
419
455
  description.media
420
456
  .filter((m) => ["audio", "video"].includes(m.kind))
421
457
  .forEach((m, i) => {
@@ -426,14 +462,6 @@ class RTCPeerConnection extends helper_1.EventTarget {
426
462
  addTransportDescription(sctpMedia, this.sctpTransport.dtlsTransport);
427
463
  }
428
464
  this.setLocal(description);
429
- // connect transports
430
- if (description.type === "answer") {
431
- log("callee start connect");
432
- this.connect().catch((err) => {
433
- log("connect failed", err);
434
- this.setConnectionState("failed");
435
- });
436
- }
437
465
  if (this.shouldNegotiationneeded) {
438
466
  this.needNegotiation();
439
467
  }
@@ -448,11 +476,47 @@ class RTCPeerConnection extends helper_1.EventTarget {
448
476
  this.pendingLocalDescription = description;
449
477
  }
450
478
  }
479
+ getTransportByMid(mid) {
480
+ let iceTransport;
481
+ const transceiver = this.transceivers.find((t) => t.mid === mid);
482
+ if (transceiver) {
483
+ iceTransport = transceiver.dtlsTransport.iceTransport;
484
+ }
485
+ else if (!iceTransport && this.sctpTransport?.mid === mid) {
486
+ iceTransport = this.sctpTransport?.dtlsTransport.iceTransport;
487
+ }
488
+ return iceTransport;
489
+ }
490
+ getTransportByMLineIndex(index) {
491
+ const sdp = this.buildOfferSdp();
492
+ const media = sdp.media[index];
493
+ if (!media) {
494
+ return;
495
+ }
496
+ const transport = this.getTransportByMid(media.rtp.muxId);
497
+ return transport;
498
+ }
451
499
  async addIceCandidate(candidateMessage) {
452
500
  const candidate = ice_1.IceCandidate.fromJSON(candidateMessage);
453
- for (const iceTransport of this.iceTransports) {
501
+ if (!candidate) {
502
+ return;
503
+ }
504
+ let iceTransport;
505
+ if (typeof candidate.sdpMid === "number") {
506
+ iceTransport = this.getTransportByMid(candidate.sdpMid);
507
+ }
508
+ if (!iceTransport && typeof candidate.sdpMLineIndex === "number") {
509
+ iceTransport = this.getTransportByMLineIndex(candidate.sdpMLineIndex);
510
+ }
511
+ if (!iceTransport) {
512
+ iceTransport = this.iceTransports[0];
513
+ }
514
+ if (iceTransport) {
454
515
  await iceTransport.addRemoteCandidate(candidate);
455
516
  }
517
+ else {
518
+ log("iceTransport not found", candidate);
519
+ }
456
520
  }
457
521
  async connect() {
458
522
  if (this.masterTransportEstablished)
@@ -529,7 +593,7 @@ class RTCPeerConnection extends helper_1.EventTarget {
529
593
  const bundle = remoteSdp.group.find((g) => g.semantic === "BUNDLE" && this.configuration.bundlePolicy !== "disable");
530
594
  let bundleTransport;
531
595
  // # apply description
532
- await Promise.all((0, helper_1.enumerate)(remoteSdp.media).map(async ([i, remoteMedia]) => {
596
+ const transports = (0, helper_1.enumerate)(remoteSdp.media).map(([i, remoteMedia]) => {
533
597
  let dtlsTransport;
534
598
  if (["audio", "video"].includes(remoteMedia.kind)) {
535
599
  let transceiver = this.transceivers.find((t) => t.kind === remoteMedia.kind &&
@@ -539,6 +603,7 @@ class RTCPeerConnection extends helper_1.EventTarget {
539
603
  transceiver = this.addTransceiver(remoteMedia.kind, {
540
604
  direction: "recvonly",
541
605
  });
606
+ transceiver.mid = remoteMedia.rtp.muxId;
542
607
  this.onRemoteTransceiverAdded.execute(transceiver);
543
608
  }
544
609
  if (bundle) {
@@ -555,6 +620,7 @@ class RTCPeerConnection extends helper_1.EventTarget {
555
620
  else if (remoteMedia.kind === "application") {
556
621
  if (!this.sctpTransport) {
557
622
  this.sctpTransport = this.createSctpTransport();
623
+ this.sctpTransport.mid = remoteMedia.rtp.muxId;
558
624
  }
559
625
  if (bundle) {
560
626
  if (!bundleTransport) {
@@ -565,7 +631,7 @@ class RTCPeerConnection extends helper_1.EventTarget {
565
631
  }
566
632
  }
567
633
  dtlsTransport = this.sctpTransport.dtlsTransport;
568
- this.setRemoteSCTP(remoteMedia, this.sctpTransport);
634
+ this.setRemoteSCTP(remoteMedia, this.sctpTransport, i);
569
635
  }
570
636
  else {
571
637
  throw new Error("invalid media kind");
@@ -582,16 +648,22 @@ class RTCPeerConnection extends helper_1.EventTarget {
582
648
  }
583
649
  // # add ICE candidates
584
650
  remoteMedia.iceCandidates.forEach(iceTransport.addRemoteCandidate);
585
- await iceTransport.iceGather.gather();
586
651
  if (remoteMedia.iceCandidatesComplete) {
587
- await iceTransport.addRemoteCandidate(undefined);
652
+ iceTransport.addRemoteCandidate(undefined);
588
653
  }
589
654
  // # set DTLS role
590
655
  if (remoteSdp.type === "answer" && remoteMedia.dtlsParams?.role) {
591
656
  dtlsTransport.role =
592
657
  remoteMedia.dtlsParams.role === "client" ? "server" : "client";
593
658
  }
594
- }));
659
+ return iceTransport;
660
+ });
661
+ if (remoteSdp.type === "offer") {
662
+ this.setSignalingState("have-remote-offer");
663
+ }
664
+ else if (remoteSdp.type === "answer") {
665
+ this.setSignalingState("stable");
666
+ }
595
667
  // connect transports
596
668
  if (remoteSdp.type === "answer") {
597
669
  log("caller start connect");
@@ -600,12 +672,9 @@ class RTCPeerConnection extends helper_1.EventTarget {
600
672
  this.setConnectionState("failed");
601
673
  });
602
674
  }
603
- if (remoteSdp.type === "offer") {
604
- this.setSignalingState("have-remote-offer");
605
- }
606
- else if (remoteSdp.type === "answer") {
607
- this.setSignalingState("stable");
608
- }
675
+ await Promise.all(transports.map(async (iceTransport) => {
676
+ await iceTransport.iceGather.gather();
677
+ }));
609
678
  this.negotiationneeded = false;
610
679
  if (this.shouldNegotiationneeded) {
611
680
  this.needNegotiation();
@@ -672,13 +741,14 @@ class RTCPeerConnection extends helper_1.EventTarget {
672
741
  }
673
742
  transceiver.receiver.setupTWCC(remoteMedia.ssrc[0]?.ssrc);
674
743
  }
675
- setRemoteSCTP(remoteMedia, sctpTransport) {
744
+ setRemoteSCTP(remoteMedia, sctpTransport, mLineIndex) {
676
745
  // # configure sctp
677
746
  this.sctpRemotePort = remoteMedia.sctpPort;
678
747
  if (!this.sctpRemotePort) {
679
748
  throw new Error("sctpRemotePort not exist");
680
749
  }
681
750
  sctpTransport.setRemotePort(this.sctpRemotePort);
751
+ sctpTransport.mLineIndex = mLineIndex;
682
752
  if (!sctpTransport.mid) {
683
753
  sctpTransport.mid = remoteMedia.rtp.muxId;
684
754
  }
@@ -820,6 +890,11 @@ class RTCPeerConnection extends helper_1.EventTarget {
820
890
  }
821
891
  }
822
892
  async createAnswer() {
893
+ await this.ensureCerts();
894
+ const description = this.buildAnswer();
895
+ return description.toJSON();
896
+ }
897
+ buildAnswer() {
823
898
  this.assertNotClosed();
824
899
  if (!["have-remote-offer", "have-local-pranswer"].includes(this.signalingState)) {
825
900
  throw new Error("createAnswer failed");
@@ -827,7 +902,6 @@ class RTCPeerConnection extends helper_1.EventTarget {
827
902
  if (!this._remoteDescription) {
828
903
  throw new Error("wrong state");
829
904
  }
830
- await this.ensureCerts();
831
905
  const description = new sdp_1.SessionDescription();
832
906
  (0, sdp_1.addSDPHeader)("answer", description);
833
907
  this._remoteDescription.media.forEach((remoteMedia) => {
@@ -835,14 +909,14 @@ class RTCPeerConnection extends helper_1.EventTarget {
835
909
  let media;
836
910
  if (["audio", "video"].includes(remoteMedia.kind)) {
837
911
  const transceiver = this.getTransceiverByMid(remoteMedia.rtp.muxId);
838
- media = createMediaDescriptionForTransceiver(transceiver, this.cname, (0, utils_1.andDirection)(transceiver.direction, transceiver.offerDirection), transceiver.mid);
912
+ media = createMediaDescriptionForTransceiver(transceiver, this.cname, (0, utils_1.andDirection)(transceiver.direction, transceiver.offerDirection));
839
913
  dtlsTransport = transceiver.dtlsTransport;
840
914
  }
841
915
  else if (remoteMedia.kind === "application") {
842
916
  if (!this.sctpTransport || !this.sctpTransport.mid) {
843
917
  throw new Error("sctpTransport not found");
844
918
  }
845
- media = createMediaDescriptionForSctp(this.sctpTransport, this.sctpTransport.mid);
919
+ media = createMediaDescriptionForSctp(this.sctpTransport);
846
920
  dtlsTransport = this.sctpTransport.dtlsTransport;
847
921
  }
848
922
  else {
@@ -871,7 +945,7 @@ class RTCPeerConnection extends helper_1.EventTarget {
871
945
  });
872
946
  description.group.push(bundle);
873
947
  }
874
- return description.toJSON();
948
+ return description;
875
949
  }
876
950
  async close() {
877
951
  if (this.isClosed)
@@ -992,14 +1066,14 @@ class RTCPeerConnection extends helper_1.EventTarget {
992
1066
  }
993
1067
  }
994
1068
  exports.RTCPeerConnection = RTCPeerConnection;
995
- function createMediaDescriptionForTransceiver(transceiver, cname, direction, mid) {
1069
+ function createMediaDescriptionForTransceiver(transceiver, cname, direction) {
996
1070
  const media = new sdp_1.MediaDescription(transceiver.kind, 9, "UDP/TLS/RTP/SAVPF", transceiver.codecs.map((c) => c.payloadType));
997
1071
  media.direction = direction;
998
1072
  media.msid = transceiver.msid;
999
1073
  media.rtp = {
1000
1074
  codecs: transceiver.codecs,
1001
1075
  headerExtensions: transceiver.headerExtensions,
1002
- muxId: mid,
1076
+ muxId: transceiver.mid,
1003
1077
  };
1004
1078
  media.rtcpHost = "0.0.0.0";
1005
1079
  media.rtcpPort = 9;
@@ -1021,10 +1095,10 @@ function createMediaDescriptionForTransceiver(transceiver, cname, direction, mid
1021
1095
  return media;
1022
1096
  }
1023
1097
  exports.createMediaDescriptionForTransceiver = createMediaDescriptionForTransceiver;
1024
- function createMediaDescriptionForSctp(sctp, mid) {
1098
+ function createMediaDescriptionForSctp(sctp) {
1025
1099
  const media = new sdp_1.MediaDescription("application", const_1.DISCARD_PORT, "UDP/DTLS/SCTP", ["webrtc-datachannel"]);
1026
1100
  media.sctpPort = sctp.port;
1027
- media.rtp.muxId = mid;
1101
+ media.rtp.muxId = sctp.mid;
1028
1102
  media.sctpCapabilities = sctp_1.RTCSctpTransport.getCapabilities();
1029
1103
  addTransportDescription(media, sctp.dtlsTransport);
1030
1104
  return media;