@koi-design/callkit 2.0.0-beta.3 → 2.0.0-beta.4

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/dist/index.js CHANGED
@@ -145,10 +145,10 @@ var CallStatus = {
145
145
  * Connecting
146
146
  */
147
147
  connecting: 2,
148
- /**
149
- * Call on hold
150
- */
151
- holding: 3,
148
+ // /**
149
+ // * Call on hold
150
+ // */
151
+ // holding: 3,
152
152
  /**
153
153
  * Ringing
154
154
  */
@@ -365,6 +365,14 @@ var SocketSendEvent = {
365
365
  * Unhold
366
366
  */
367
367
  UNHOLD: "AGENT_UN_HOLD",
368
+ /**
369
+ * Mute
370
+ */
371
+ MUTE: "AGENT_MUTE",
372
+ /**
373
+ * Unmute
374
+ */
375
+ UNMUTE: "AGENT_UN_MUTE",
368
376
  /**
369
377
  * Call
370
378
  */
@@ -539,28 +547,58 @@ var Call = class {
539
547
  this.callKit.logger.warn("Current state cannot be held", {
540
548
  caller: "Call.callHold",
541
549
  content: {
550
+ isHold: this.callKit.connect.isHolding(),
542
551
  isCalling: this.callKit.connect.isCalling()
543
552
  }
544
553
  });
545
554
  return;
546
555
  }
547
- this.callKit.socket.send(SocketSendEvent.HOLD);
548
- this.callKit.connect.setConnectStatus(CallStatus.holding);
556
+ this.callKit.connect.setHold(true);
549
557
  }
550
558
  async callUnhold() {
551
559
  if (!this.callKit.config.check())
552
560
  return;
553
- if (!this.callKit.connect.isHolding()) {
561
+ if (!this.callKit.connect.isCalling()) {
554
562
  this.callKit.logger.warn("Current state cannot unhold", {
555
563
  caller: "Call.callUnhold",
556
564
  content: {
557
- isHolding: this.callKit.connect.isHolding()
565
+ isHold: this.callKit.connect.isHolding(),
566
+ isCalling: this.callKit.connect.isCalling()
567
+ }
568
+ });
569
+ return;
570
+ }
571
+ this.callKit.connect.setHold(true);
572
+ }
573
+ async callMute() {
574
+ if (!this.callKit.config.check())
575
+ return;
576
+ if (!this.callKit.connect.isCalling()) {
577
+ this.callKit.logger.warn("Current state cannot be muted", {
578
+ caller: "Call.callMute",
579
+ content: {
580
+ isMuted: this.callKit.connect.isMuted(),
581
+ isCalling: this.callKit.connect.isCalling()
582
+ }
583
+ });
584
+ return;
585
+ }
586
+ this.callKit.connect.setMute(true);
587
+ }
588
+ async callUnmute() {
589
+ if (!this.callKit.config.check())
590
+ return;
591
+ if (!this.callKit.connect.isCalling()) {
592
+ this.callKit.logger.warn("Current state cannot be unmuted", {
593
+ caller: "Call.callUnmute",
594
+ content: {
595
+ isMuted: this.callKit.connect.isMuted(),
596
+ isCalling: this.callKit.connect.isCalling()
558
597
  }
559
598
  });
560
599
  return;
561
600
  }
562
- this.callKit.socket.send(SocketSendEvent.UNHOLD);
563
- this.callKit.connect.setConnectStatus(CallStatus.calling);
601
+ this.callKit.connect.setMute(false);
564
602
  }
565
603
  };
566
604
 
@@ -868,6 +906,10 @@ var Connect = class {
868
906
  * Whether registered
869
907
  */
870
908
  isRegister = false;
909
+ /**
910
+ * Whether holding
911
+ */
912
+ isHold = false;
871
913
  constructor(callKit) {
872
914
  this.callKit = callKit;
873
915
  const { reconnect = {} } = this.callKit.config.getConfig();
@@ -878,7 +920,7 @@ var Connect = class {
878
920
  }
879
921
  reset() {
880
922
  if (this.isHolding()) {
881
- this.setHoldStatus(false);
923
+ this.setHold(false);
882
924
  }
883
925
  if (this.connectStatus !== CallStatus.init) {
884
926
  this.setConnectStatus(CallStatus.init);
@@ -953,7 +995,7 @@ var Connect = class {
953
995
  * isHolding
954
996
  */
955
997
  isHolding() {
956
- return this.connectStatus === CallStatus.holding;
998
+ return this.isHold;
957
999
  }
958
1000
  /**
959
1001
  * isRegistered
@@ -1151,10 +1193,6 @@ var Connect = class {
1151
1193
  }
1152
1194
  });
1153
1195
  this.setRegister(true);
1154
- this.callKit.trigger(KitEvent.SIP_REGISTERER_EVENT, {
1155
- registererState: state,
1156
- isRegistered: this.isRegistered()
1157
- });
1158
1196
  break;
1159
1197
  case import_sip.RegistererState.Terminated:
1160
1198
  this.callKit.logger.info("registerer stateChange Terminated", {
@@ -1167,10 +1205,6 @@ var Connect = class {
1167
1205
  });
1168
1206
  this.setRegister(false);
1169
1207
  this.setConnectStatus(CallStatus.init);
1170
- this.callKit.trigger(KitEvent.SIP_REGISTERER_EVENT, {
1171
- isRegistered: this.isRegistered(),
1172
- registererState: state
1173
- });
1174
1208
  break;
1175
1209
  case import_sip.RegistererState.Unregistered:
1176
1210
  this.callKit.logger.info("registerer stateChange Unregistered", {
@@ -1183,10 +1217,6 @@ var Connect = class {
1183
1217
  });
1184
1218
  this.setRegister(false);
1185
1219
  this.setConnectStatus(CallStatus.init);
1186
- this.callKit.trigger(KitEvent.SIP_REGISTERER_EVENT, {
1187
- isRegistered: this.isRegistered(),
1188
- registererState: state
1189
- });
1190
1220
  break;
1191
1221
  default:
1192
1222
  break;
@@ -1640,133 +1670,102 @@ var Connect = class {
1640
1670
  });
1641
1671
  }
1642
1672
  }
1643
- /**
1644
- * Update hold
1645
- * @param hold
1646
- */
1647
- setHoldStatus(hold) {
1648
- this.callKit.logger.info("connect setHold", {
1649
- caller: "Connect.setHoldStatus",
1650
- type: "SIP",
1651
- content: {
1652
- hold
1653
- }
1654
- });
1655
- this.callKit.trigger(KitEvent.KIT_SET_HOLD, hold);
1656
- }
1657
1673
  async setHold(hold) {
1658
- this.setHoldStatus(hold);
1659
- }
1660
- async hold() {
1661
- this.callKit.logger.info("connect hold", {
1662
- caller: "Connect.hold",
1663
- type: "SIP",
1664
- content: {
1665
- hold: true
1666
- }
1667
- });
1668
- if (this.connectStatus !== CallStatus.calling || !this.currentSession) {
1669
- this.callKit.logger.error("Current status is not in call", {
1670
- caller: "Connect.hold",
1674
+ if (this.isHold === hold) {
1675
+ this.callKit.logger.warn("Already holding", {
1676
+ caller: "Connect.setHold",
1671
1677
  type: "SIP",
1672
1678
  content: {
1673
- errCode: ErrorCode.WEBRTC_HOLE_STATUS_ERROR
1679
+ isHold: this.isHold
1674
1680
  }
1675
1681
  });
1682
+ return;
1676
1683
  }
1677
- }
1678
- async unhold() {
1679
- this.callKit.logger.info("connect unhold", {
1680
- caller: "Connect.unhold",
1684
+ this.callKit.socket.send(
1685
+ hold ? SocketSendEvent.HOLD : SocketSendEvent.UNHOLD
1686
+ );
1687
+ this.isHold = hold;
1688
+ this.callKit.logger.info("connect setHold", {
1689
+ caller: "Connect.setHold",
1681
1690
  type: "SIP",
1682
1691
  content: {
1683
- hold: false
1692
+ hold
1684
1693
  }
1685
1694
  });
1695
+ this.callKit.trigger(KitEvent.KIT_SET_HOLD, hold);
1686
1696
  }
1697
+ // /**
1698
+ // * Set mute
1699
+ // * @param mute Whether to mute
1700
+ // * @deprecated just send socket event to server to mute or unmute
1701
+ // */
1702
+ // async setMute(mute: boolean) {
1703
+ // this.callKit.logger.info('connect setMute', {
1704
+ // caller: 'Connect.setMute',
1705
+ // type: 'SIP',
1706
+ // content: {
1707
+ // mute
1708
+ // }
1709
+ // });
1710
+ // if (!this.currentSession) {
1711
+ // this.callKit.logger.error('No active session', {
1712
+ // caller: 'Connect.setMute',
1713
+ // type: 'SIP',
1714
+ // content: {
1715
+ // errCode: ErrorCode.WEBRTC_MUTE_STATUS_ERROR
1716
+ // }
1717
+ // });
1718
+ // this.callKit.reset();
1719
+ // return;
1720
+ // }
1721
+ // try {
1722
+ // // Get SessionDescriptionHandler
1723
+ // const sdh = this.currentSession.sessionDescriptionHandler;
1724
+ // if (!sdh || !('peerConnection' in sdh)) {
1725
+ // throw new Error('Invalid session description handler');
1726
+ // }
1727
+ // // Get PeerConnection
1728
+ // const pc = (sdh as any).peerConnection as RTCPeerConnection;
1729
+ // // Get local audio track and set status
1730
+ // const audioSender = pc
1731
+ // .getSenders()
1732
+ // .find((sender) => sender.track?.kind === 'audio');
1733
+ // if (audioSender && audioSender.track) {
1734
+ // audioSender.track.enabled = !mute;
1735
+ // // Update status and trigger event
1736
+ // this.isMute = mute;
1737
+ // this.callKit.trigger(KitEvent.KIT_SET_MUTE, mute);
1738
+ // } else {
1739
+ // throw new Error('No audio track found');
1740
+ // }
1741
+ // } catch (error) {
1742
+ // this.callKit.logger.error('Failed to set mute state', {
1743
+ // caller: 'Connect.setMute',
1744
+ // type: 'SIP',
1745
+ // content: {
1746
+ // err: error.message,
1747
+ // errCode: ErrorCode.WEBRTC_MUTE_ERROR
1748
+ // }
1749
+ // });
1750
+ // }
1751
+ // }
1687
1752
  async setMute(mute) {
1688
- this.callKit.logger.info("connect setMute", {
1689
- caller: "Connect.setMute",
1690
- type: "SIP",
1691
- content: {
1692
- mute
1693
- }
1694
- });
1695
- if (!this.currentSession) {
1696
- this.callKit.logger.error("No active session", {
1753
+ if (this.isMute === mute) {
1754
+ this.callKit.logger.warn("Already muted", {
1697
1755
  caller: "Connect.setMute",
1698
1756
  type: "SIP",
1699
1757
  content: {
1700
- errCode: ErrorCode.WEBRTC_MUTE_STATUS_ERROR
1701
- }
1702
- });
1703
- this.callKit.reset();
1704
- return;
1705
- }
1706
- try {
1707
- const sdh = this.currentSession.sessionDescriptionHandler;
1708
- if (!sdh || !("peerConnection" in sdh)) {
1709
- throw new Error("Invalid session description handler");
1710
- }
1711
- const pc = sdh.peerConnection;
1712
- const audioSender = pc.getSenders().find((sender) => sender.track?.kind === "audio");
1713
- if (audioSender && audioSender.track) {
1714
- audioSender.track.enabled = !mute;
1715
- this.isMute = mute;
1716
- this.callKit.trigger(KitEvent.KIT_SET_MUTE, mute);
1717
- } else {
1718
- throw new Error("No audio track found");
1719
- }
1720
- } catch (error) {
1721
- this.callKit.logger.error("Failed to set mute state", {
1722
- caller: "Connect.setMute",
1723
- type: "SIP",
1724
- content: {
1725
- err: error.message,
1726
- errCode: ErrorCode.WEBRTC_MUTE_ERROR
1727
- }
1728
- });
1729
- }
1730
- }
1731
- async mute() {
1732
- this.callKit.logger.info("connect mute", {
1733
- caller: "Connect.mute",
1734
- type: "SIP",
1735
- content: {
1736
- mute: true
1737
- }
1738
- });
1739
- if (this.connectStatus !== CallStatus.calling || !this.currentSession) {
1740
- this.callKit.logger.warn("Current status is not in call", {
1741
- caller: "Connect.mute",
1742
- type: "SIP",
1743
- content: {
1744
- errCode: ErrorCode.WEBRTC_MUTE_STATUS_ERROR
1758
+ isCalling: this.isCalling(),
1759
+ isMuted: this.isMuted()
1745
1760
  }
1746
1761
  });
1747
1762
  return;
1748
1763
  }
1749
- await this.setMute(true);
1750
- }
1751
- async unmute() {
1752
- this.callKit.logger.info("connect unmute", {
1753
- caller: "Connect.unmute",
1754
- type: "SIP",
1755
- content: {
1756
- mute: false
1757
- }
1758
- });
1759
- if (this.connectStatus !== CallStatus.calling || !this.currentSession) {
1760
- this.callKit.logger.warn("Current status is not in call", {
1761
- caller: "Connect.unmute",
1762
- type: "SIP",
1763
- content: {
1764
- errCode: ErrorCode.WEBRTC_MUTE_STATUS_ERROR
1765
- }
1766
- });
1767
- return;
1768
- }
1769
- await this.setMute(false);
1764
+ this.callKit.socket.send(
1765
+ mute ? SocketSendEvent.MUTE : SocketSendEvent.UNMUTE
1766
+ );
1767
+ this.isMute = mute;
1768
+ this.callKit.trigger(KitEvent.KIT_SET_MUTE, mute);
1770
1769
  }
1771
1770
  async refer(referTo, extra) {
1772
1771
  this.callKit.logger.info("connect refer", {
@@ -2466,6 +2465,28 @@ var CallKit = class {
2466
2465
  });
2467
2466
  this.callCenter.callUnhold();
2468
2467
  }
2468
+ mute() {
2469
+ if (!this.config.check())
2470
+ return;
2471
+ this.logger.info("mute", {
2472
+ caller: "CallKit.mute",
2473
+ content: {
2474
+ connectStatus: this.connect.connectStatus
2475
+ }
2476
+ });
2477
+ this.callCenter.callMute();
2478
+ }
2479
+ unmute() {
2480
+ if (!this.config.check())
2481
+ return;
2482
+ this.logger.info("unmute", {
2483
+ caller: "CallKit.unmute",
2484
+ content: {
2485
+ connectStatus: this.connect.connectStatus
2486
+ }
2487
+ });
2488
+ this.callCenter.callUnmute();
2489
+ }
2469
2490
  /**
2470
2491
  * set userstatus
2471
2492
  * @param status