@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.mjs CHANGED
@@ -112,10 +112,10 @@ var CallStatus = {
112
112
  * Connecting
113
113
  */
114
114
  connecting: 2,
115
- /**
116
- * Call on hold
117
- */
118
- holding: 3,
115
+ // /**
116
+ // * Call on hold
117
+ // */
118
+ // holding: 3,
119
119
  /**
120
120
  * Ringing
121
121
  */
@@ -332,6 +332,14 @@ var SocketSendEvent = {
332
332
  * Unhold
333
333
  */
334
334
  UNHOLD: "AGENT_UN_HOLD",
335
+ /**
336
+ * Mute
337
+ */
338
+ MUTE: "AGENT_MUTE",
339
+ /**
340
+ * Unmute
341
+ */
342
+ UNMUTE: "AGENT_UN_MUTE",
335
343
  /**
336
344
  * Call
337
345
  */
@@ -506,28 +514,58 @@ var Call = class {
506
514
  this.callKit.logger.warn("Current state cannot be held", {
507
515
  caller: "Call.callHold",
508
516
  content: {
517
+ isHold: this.callKit.connect.isHolding(),
509
518
  isCalling: this.callKit.connect.isCalling()
510
519
  }
511
520
  });
512
521
  return;
513
522
  }
514
- this.callKit.socket.send(SocketSendEvent.HOLD);
515
- this.callKit.connect.setConnectStatus(CallStatus.holding);
523
+ this.callKit.connect.setHold(true);
516
524
  }
517
525
  async callUnhold() {
518
526
  if (!this.callKit.config.check())
519
527
  return;
520
- if (!this.callKit.connect.isHolding()) {
528
+ if (!this.callKit.connect.isCalling()) {
521
529
  this.callKit.logger.warn("Current state cannot unhold", {
522
530
  caller: "Call.callUnhold",
523
531
  content: {
524
- isHolding: this.callKit.connect.isHolding()
532
+ isHold: this.callKit.connect.isHolding(),
533
+ isCalling: this.callKit.connect.isCalling()
534
+ }
535
+ });
536
+ return;
537
+ }
538
+ this.callKit.connect.setHold(true);
539
+ }
540
+ async callMute() {
541
+ if (!this.callKit.config.check())
542
+ return;
543
+ if (!this.callKit.connect.isCalling()) {
544
+ this.callKit.logger.warn("Current state cannot be muted", {
545
+ caller: "Call.callMute",
546
+ content: {
547
+ isMuted: this.callKit.connect.isMuted(),
548
+ isCalling: this.callKit.connect.isCalling()
549
+ }
550
+ });
551
+ return;
552
+ }
553
+ this.callKit.connect.setMute(true);
554
+ }
555
+ async callUnmute() {
556
+ if (!this.callKit.config.check())
557
+ return;
558
+ if (!this.callKit.connect.isCalling()) {
559
+ this.callKit.logger.warn("Current state cannot be unmuted", {
560
+ caller: "Call.callUnmute",
561
+ content: {
562
+ isMuted: this.callKit.connect.isMuted(),
563
+ isCalling: this.callKit.connect.isCalling()
525
564
  }
526
565
  });
527
566
  return;
528
567
  }
529
- this.callKit.socket.send(SocketSendEvent.UNHOLD);
530
- this.callKit.connect.setConnectStatus(CallStatus.calling);
568
+ this.callKit.connect.setMute(false);
531
569
  }
532
570
  };
533
571
 
@@ -841,6 +879,10 @@ var Connect = class {
841
879
  * Whether registered
842
880
  */
843
881
  isRegister = false;
882
+ /**
883
+ * Whether holding
884
+ */
885
+ isHold = false;
844
886
  constructor(callKit) {
845
887
  this.callKit = callKit;
846
888
  const { reconnect = {} } = this.callKit.config.getConfig();
@@ -851,7 +893,7 @@ var Connect = class {
851
893
  }
852
894
  reset() {
853
895
  if (this.isHolding()) {
854
- this.setHoldStatus(false);
896
+ this.setHold(false);
855
897
  }
856
898
  if (this.connectStatus !== CallStatus.init) {
857
899
  this.setConnectStatus(CallStatus.init);
@@ -926,7 +968,7 @@ var Connect = class {
926
968
  * isHolding
927
969
  */
928
970
  isHolding() {
929
- return this.connectStatus === CallStatus.holding;
971
+ return this.isHold;
930
972
  }
931
973
  /**
932
974
  * isRegistered
@@ -1124,10 +1166,6 @@ var Connect = class {
1124
1166
  }
1125
1167
  });
1126
1168
  this.setRegister(true);
1127
- this.callKit.trigger(KitEvent.SIP_REGISTERER_EVENT, {
1128
- registererState: state,
1129
- isRegistered: this.isRegistered()
1130
- });
1131
1169
  break;
1132
1170
  case RegistererState.Terminated:
1133
1171
  this.callKit.logger.info("registerer stateChange Terminated", {
@@ -1140,10 +1178,6 @@ var Connect = class {
1140
1178
  });
1141
1179
  this.setRegister(false);
1142
1180
  this.setConnectStatus(CallStatus.init);
1143
- this.callKit.trigger(KitEvent.SIP_REGISTERER_EVENT, {
1144
- isRegistered: this.isRegistered(),
1145
- registererState: state
1146
- });
1147
1181
  break;
1148
1182
  case RegistererState.Unregistered:
1149
1183
  this.callKit.logger.info("registerer stateChange Unregistered", {
@@ -1156,10 +1190,6 @@ var Connect = class {
1156
1190
  });
1157
1191
  this.setRegister(false);
1158
1192
  this.setConnectStatus(CallStatus.init);
1159
- this.callKit.trigger(KitEvent.SIP_REGISTERER_EVENT, {
1160
- isRegistered: this.isRegistered(),
1161
- registererState: state
1162
- });
1163
1193
  break;
1164
1194
  default:
1165
1195
  break;
@@ -1613,133 +1643,102 @@ var Connect = class {
1613
1643
  });
1614
1644
  }
1615
1645
  }
1616
- /**
1617
- * Update hold
1618
- * @param hold
1619
- */
1620
- setHoldStatus(hold) {
1621
- this.callKit.logger.info("connect setHold", {
1622
- caller: "Connect.setHoldStatus",
1623
- type: "SIP",
1624
- content: {
1625
- hold
1626
- }
1627
- });
1628
- this.callKit.trigger(KitEvent.KIT_SET_HOLD, hold);
1629
- }
1630
1646
  async setHold(hold) {
1631
- this.setHoldStatus(hold);
1632
- }
1633
- async hold() {
1634
- this.callKit.logger.info("connect hold", {
1635
- caller: "Connect.hold",
1636
- type: "SIP",
1637
- content: {
1638
- hold: true
1639
- }
1640
- });
1641
- if (this.connectStatus !== CallStatus.calling || !this.currentSession) {
1642
- this.callKit.logger.error("Current status is not in call", {
1643
- caller: "Connect.hold",
1647
+ if (this.isHold === hold) {
1648
+ this.callKit.logger.warn("Already holding", {
1649
+ caller: "Connect.setHold",
1644
1650
  type: "SIP",
1645
1651
  content: {
1646
- errCode: ErrorCode.WEBRTC_HOLE_STATUS_ERROR
1652
+ isHold: this.isHold
1647
1653
  }
1648
1654
  });
1655
+ return;
1649
1656
  }
1650
- }
1651
- async unhold() {
1652
- this.callKit.logger.info("connect unhold", {
1653
- caller: "Connect.unhold",
1657
+ this.callKit.socket.send(
1658
+ hold ? SocketSendEvent.HOLD : SocketSendEvent.UNHOLD
1659
+ );
1660
+ this.isHold = hold;
1661
+ this.callKit.logger.info("connect setHold", {
1662
+ caller: "Connect.setHold",
1654
1663
  type: "SIP",
1655
1664
  content: {
1656
- hold: false
1665
+ hold
1657
1666
  }
1658
1667
  });
1668
+ this.callKit.trigger(KitEvent.KIT_SET_HOLD, hold);
1659
1669
  }
1670
+ // /**
1671
+ // * Set mute
1672
+ // * @param mute Whether to mute
1673
+ // * @deprecated just send socket event to server to mute or unmute
1674
+ // */
1675
+ // async setMute(mute: boolean) {
1676
+ // this.callKit.logger.info('connect setMute', {
1677
+ // caller: 'Connect.setMute',
1678
+ // type: 'SIP',
1679
+ // content: {
1680
+ // mute
1681
+ // }
1682
+ // });
1683
+ // if (!this.currentSession) {
1684
+ // this.callKit.logger.error('No active session', {
1685
+ // caller: 'Connect.setMute',
1686
+ // type: 'SIP',
1687
+ // content: {
1688
+ // errCode: ErrorCode.WEBRTC_MUTE_STATUS_ERROR
1689
+ // }
1690
+ // });
1691
+ // this.callKit.reset();
1692
+ // return;
1693
+ // }
1694
+ // try {
1695
+ // // Get SessionDescriptionHandler
1696
+ // const sdh = this.currentSession.sessionDescriptionHandler;
1697
+ // if (!sdh || !('peerConnection' in sdh)) {
1698
+ // throw new Error('Invalid session description handler');
1699
+ // }
1700
+ // // Get PeerConnection
1701
+ // const pc = (sdh as any).peerConnection as RTCPeerConnection;
1702
+ // // Get local audio track and set status
1703
+ // const audioSender = pc
1704
+ // .getSenders()
1705
+ // .find((sender) => sender.track?.kind === 'audio');
1706
+ // if (audioSender && audioSender.track) {
1707
+ // audioSender.track.enabled = !mute;
1708
+ // // Update status and trigger event
1709
+ // this.isMute = mute;
1710
+ // this.callKit.trigger(KitEvent.KIT_SET_MUTE, mute);
1711
+ // } else {
1712
+ // throw new Error('No audio track found');
1713
+ // }
1714
+ // } catch (error) {
1715
+ // this.callKit.logger.error('Failed to set mute state', {
1716
+ // caller: 'Connect.setMute',
1717
+ // type: 'SIP',
1718
+ // content: {
1719
+ // err: error.message,
1720
+ // errCode: ErrorCode.WEBRTC_MUTE_ERROR
1721
+ // }
1722
+ // });
1723
+ // }
1724
+ // }
1660
1725
  async setMute(mute) {
1661
- this.callKit.logger.info("connect setMute", {
1662
- caller: "Connect.setMute",
1663
- type: "SIP",
1664
- content: {
1665
- mute
1666
- }
1667
- });
1668
- if (!this.currentSession) {
1669
- this.callKit.logger.error("No active session", {
1726
+ if (this.isMute === mute) {
1727
+ this.callKit.logger.warn("Already muted", {
1670
1728
  caller: "Connect.setMute",
1671
1729
  type: "SIP",
1672
1730
  content: {
1673
- errCode: ErrorCode.WEBRTC_MUTE_STATUS_ERROR
1674
- }
1675
- });
1676
- this.callKit.reset();
1677
- return;
1678
- }
1679
- try {
1680
- const sdh = this.currentSession.sessionDescriptionHandler;
1681
- if (!sdh || !("peerConnection" in sdh)) {
1682
- throw new Error("Invalid session description handler");
1683
- }
1684
- const pc = sdh.peerConnection;
1685
- const audioSender = pc.getSenders().find((sender) => sender.track?.kind === "audio");
1686
- if (audioSender && audioSender.track) {
1687
- audioSender.track.enabled = !mute;
1688
- this.isMute = mute;
1689
- this.callKit.trigger(KitEvent.KIT_SET_MUTE, mute);
1690
- } else {
1691
- throw new Error("No audio track found");
1692
- }
1693
- } catch (error) {
1694
- this.callKit.logger.error("Failed to set mute state", {
1695
- caller: "Connect.setMute",
1696
- type: "SIP",
1697
- content: {
1698
- err: error.message,
1699
- errCode: ErrorCode.WEBRTC_MUTE_ERROR
1700
- }
1701
- });
1702
- }
1703
- }
1704
- async mute() {
1705
- this.callKit.logger.info("connect mute", {
1706
- caller: "Connect.mute",
1707
- type: "SIP",
1708
- content: {
1709
- mute: true
1710
- }
1711
- });
1712
- if (this.connectStatus !== CallStatus.calling || !this.currentSession) {
1713
- this.callKit.logger.warn("Current status is not in call", {
1714
- caller: "Connect.mute",
1715
- type: "SIP",
1716
- content: {
1717
- errCode: ErrorCode.WEBRTC_MUTE_STATUS_ERROR
1731
+ isCalling: this.isCalling(),
1732
+ isMuted: this.isMuted()
1718
1733
  }
1719
1734
  });
1720
1735
  return;
1721
1736
  }
1722
- await this.setMute(true);
1723
- }
1724
- async unmute() {
1725
- this.callKit.logger.info("connect unmute", {
1726
- caller: "Connect.unmute",
1727
- type: "SIP",
1728
- content: {
1729
- mute: false
1730
- }
1731
- });
1732
- if (this.connectStatus !== CallStatus.calling || !this.currentSession) {
1733
- this.callKit.logger.warn("Current status is not in call", {
1734
- caller: "Connect.unmute",
1735
- type: "SIP",
1736
- content: {
1737
- errCode: ErrorCode.WEBRTC_MUTE_STATUS_ERROR
1738
- }
1739
- });
1740
- return;
1741
- }
1742
- await this.setMute(false);
1737
+ this.callKit.socket.send(
1738
+ mute ? SocketSendEvent.MUTE : SocketSendEvent.UNMUTE
1739
+ );
1740
+ this.isMute = mute;
1741
+ this.callKit.trigger(KitEvent.KIT_SET_MUTE, mute);
1743
1742
  }
1744
1743
  async refer(referTo, extra) {
1745
1744
  this.callKit.logger.info("connect refer", {
@@ -2439,6 +2438,28 @@ var CallKit = class {
2439
2438
  });
2440
2439
  this.callCenter.callUnhold();
2441
2440
  }
2441
+ mute() {
2442
+ if (!this.config.check())
2443
+ return;
2444
+ this.logger.info("mute", {
2445
+ caller: "CallKit.mute",
2446
+ content: {
2447
+ connectStatus: this.connect.connectStatus
2448
+ }
2449
+ });
2450
+ this.callCenter.callMute();
2451
+ }
2452
+ unmute() {
2453
+ if (!this.config.check())
2454
+ return;
2455
+ this.logger.info("unmute", {
2456
+ caller: "CallKit.unmute",
2457
+ content: {
2458
+ connectStatus: this.connect.connectStatus
2459
+ }
2460
+ });
2461
+ this.callCenter.callUnmute();
2462
+ }
2442
2463
  /**
2443
2464
  * set userstatus
2444
2465
  * @param status