@koi-design/callkit 2.0.0-beta.3 → 2.0.0-beta.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.
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
  */
@@ -210,7 +210,8 @@ var KitEvent = {
210
210
  * User status change
211
211
  */
212
212
  USER_STATUS_CHANGE: "userStatusChange",
213
- CONNECT_EVENT: "CONNECT_EVENT",
213
+ INCALL_CONNECT_EVENT: "IncallConnectEvent",
214
+ SIP_CONNECT_EVENT: "sipConnectEvent",
214
215
  SIP_REGISTERER_EVENT: "sipRegistererEvent",
215
216
  SIP_SESSION_EVENT: "sipSessionEvent"
216
217
  };
@@ -332,6 +333,14 @@ var SocketSendEvent = {
332
333
  * Unhold
333
334
  */
334
335
  UNHOLD: "AGENT_UN_HOLD",
336
+ /**
337
+ * Mute
338
+ */
339
+ MUTE: "AGENT_MUTE",
340
+ /**
341
+ * Unmute
342
+ */
343
+ UNMUTE: "AGENT_UN_MUTE",
335
344
  /**
336
345
  * Call
337
346
  */
@@ -506,28 +515,58 @@ var Call = class {
506
515
  this.callKit.logger.warn("Current state cannot be held", {
507
516
  caller: "Call.callHold",
508
517
  content: {
518
+ isHold: this.callKit.connect.isHolding(),
509
519
  isCalling: this.callKit.connect.isCalling()
510
520
  }
511
521
  });
512
522
  return;
513
523
  }
514
- this.callKit.socket.send(SocketSendEvent.HOLD);
515
- this.callKit.connect.setConnectStatus(CallStatus.holding);
524
+ this.callKit.connect.setHold(true);
516
525
  }
517
526
  async callUnhold() {
518
527
  if (!this.callKit.config.check())
519
528
  return;
520
- if (!this.callKit.connect.isHolding()) {
529
+ if (!this.callKit.connect.isCalling()) {
521
530
  this.callKit.logger.warn("Current state cannot unhold", {
522
531
  caller: "Call.callUnhold",
523
532
  content: {
524
- isHolding: this.callKit.connect.isHolding()
533
+ isHold: this.callKit.connect.isHolding(),
534
+ isCalling: this.callKit.connect.isCalling()
535
+ }
536
+ });
537
+ return;
538
+ }
539
+ this.callKit.connect.setHold(false);
540
+ }
541
+ async callMute() {
542
+ if (!this.callKit.config.check())
543
+ return;
544
+ if (!this.callKit.connect.isCalling()) {
545
+ this.callKit.logger.warn("Current state cannot be muted", {
546
+ caller: "Call.callMute",
547
+ content: {
548
+ isMuted: this.callKit.connect.isMuted(),
549
+ isCalling: this.callKit.connect.isCalling()
550
+ }
551
+ });
552
+ return;
553
+ }
554
+ this.callKit.connect.setMute(true);
555
+ }
556
+ async callUnmute() {
557
+ if (!this.callKit.config.check())
558
+ return;
559
+ if (!this.callKit.connect.isCalling()) {
560
+ this.callKit.logger.warn("Current state cannot be unmuted", {
561
+ caller: "Call.callUnmute",
562
+ content: {
563
+ isMuted: this.callKit.connect.isMuted(),
564
+ isCalling: this.callKit.connect.isCalling()
525
565
  }
526
566
  });
527
567
  return;
528
568
  }
529
- this.callKit.socket.send(SocketSendEvent.UNHOLD);
530
- this.callKit.connect.setConnectStatus(CallStatus.calling);
569
+ this.callKit.connect.setMute(false);
531
570
  }
532
571
  };
533
572
 
@@ -841,6 +880,10 @@ var Connect = class {
841
880
  * Whether registered
842
881
  */
843
882
  isRegister = false;
883
+ /**
884
+ * Whether holding
885
+ */
886
+ isHold = false;
844
887
  constructor(callKit) {
845
888
  this.callKit = callKit;
846
889
  const { reconnect = {} } = this.callKit.config.getConfig();
@@ -851,12 +894,12 @@ var Connect = class {
851
894
  }
852
895
  reset() {
853
896
  if (this.isHolding()) {
854
- this.setHoldStatus(false);
897
+ this.setHold(false);
855
898
  }
856
899
  if (this.connectStatus !== CallStatus.init) {
857
900
  this.setConnectStatus(CallStatus.init);
858
901
  }
859
- if (this.isRegistered) {
902
+ if (this.isRegistered()) {
860
903
  this.unregister();
861
904
  }
862
905
  this.currentSession = void 0;
@@ -926,7 +969,7 @@ var Connect = class {
926
969
  * isHolding
927
970
  */
928
971
  isHolding() {
929
- return this.connectStatus === CallStatus.holding;
972
+ return this.isHold;
930
973
  }
931
974
  /**
932
975
  * isRegistered
@@ -960,7 +1003,7 @@ var Connect = class {
960
1003
  this.heartbeatFlag -= 1;
961
1004
  if (this.heartbeatFlag <= 0) {
962
1005
  this.heartbeatFlag = MAX_HEARTBEAT_COUNT;
963
- this.callKit.trigger(KitEvent.CONNECT_EVENT, {
1006
+ this.callKit.trigger(KitEvent.SIP_CONNECT_EVENT, {
964
1007
  event: "OPTIONS_HEARTBEAT_EXPIRED"
965
1008
  });
966
1009
  }
@@ -1141,8 +1184,8 @@ var Connect = class {
1141
1184
  this.setRegister(false);
1142
1185
  this.setConnectStatus(CallStatus.init);
1143
1186
  this.callKit.trigger(KitEvent.SIP_REGISTERER_EVENT, {
1144
- isRegistered: this.isRegistered(),
1145
- registererState: state
1187
+ registererState: state,
1188
+ isRegistered: this.isRegistered()
1146
1189
  });
1147
1190
  break;
1148
1191
  case RegistererState.Unregistered:
@@ -1157,8 +1200,8 @@ var Connect = class {
1157
1200
  this.setRegister(false);
1158
1201
  this.setConnectStatus(CallStatus.init);
1159
1202
  this.callKit.trigger(KitEvent.SIP_REGISTERER_EVENT, {
1160
- isRegistered: this.isRegistered(),
1161
- registererState: state
1203
+ registererState: state,
1204
+ isRegistered: this.isRegistered()
1162
1205
  });
1163
1206
  break;
1164
1207
  default:
@@ -1249,7 +1292,6 @@ var Connect = class {
1249
1292
  };
1250
1293
  if (this.isOutgoing) {
1251
1294
  this.currentSession.accept(options);
1252
- this.setConnectStatus(CallStatus.connecting);
1253
1295
  this.callKit.trigger(KitEvent.KIT_OUTGOING_INVITE, {
1254
1296
  getInviteData: () => {
1255
1297
  const { request: request2 } = this.currentSession;
@@ -1613,133 +1655,102 @@ var Connect = class {
1613
1655
  });
1614
1656
  }
1615
1657
  }
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
1658
  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",
1659
+ if (this.isHold === hold) {
1660
+ this.callKit.logger.warn("Already holding", {
1661
+ caller: "Connect.setHold",
1644
1662
  type: "SIP",
1645
1663
  content: {
1646
- errCode: ErrorCode.WEBRTC_HOLE_STATUS_ERROR
1664
+ isHold: this.isHold
1647
1665
  }
1648
1666
  });
1667
+ return;
1649
1668
  }
1650
- }
1651
- async unhold() {
1652
- this.callKit.logger.info("connect unhold", {
1653
- caller: "Connect.unhold",
1669
+ this.callKit.socket.send(
1670
+ hold ? SocketSendEvent.HOLD : SocketSendEvent.UNHOLD
1671
+ );
1672
+ this.isHold = hold;
1673
+ this.callKit.logger.info("connect setHold", {
1674
+ caller: "Connect.setHold",
1654
1675
  type: "SIP",
1655
1676
  content: {
1656
- hold: false
1677
+ hold
1657
1678
  }
1658
1679
  });
1680
+ this.callKit.trigger(KitEvent.KIT_SET_HOLD, hold);
1659
1681
  }
1682
+ // /**
1683
+ // * Set mute
1684
+ // * @param mute Whether to mute
1685
+ // * @deprecated just send socket event to server to mute or unmute
1686
+ // */
1687
+ // async setMute(mute: boolean) {
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', {
1697
+ // caller: 'Connect.setMute',
1698
+ // type: 'SIP',
1699
+ // content: {
1700
+ // errCode: ErrorCode.WEBRTC_MUTE_STATUS_ERROR
1701
+ // }
1702
+ // });
1703
+ // this.callKit.reset();
1704
+ // return;
1705
+ // }
1706
+ // try {
1707
+ // // Get SessionDescriptionHandler
1708
+ // const sdh = this.currentSession.sessionDescriptionHandler;
1709
+ // if (!sdh || !('peerConnection' in sdh)) {
1710
+ // throw new Error('Invalid session description handler');
1711
+ // }
1712
+ // // Get PeerConnection
1713
+ // const pc = (sdh as any).peerConnection as RTCPeerConnection;
1714
+ // // Get local audio track and set status
1715
+ // const audioSender = pc
1716
+ // .getSenders()
1717
+ // .find((sender) => sender.track?.kind === 'audio');
1718
+ // if (audioSender && audioSender.track) {
1719
+ // audioSender.track.enabled = !mute;
1720
+ // // Update status and trigger event
1721
+ // this.isMute = mute;
1722
+ // this.callKit.trigger(KitEvent.KIT_SET_MUTE, mute);
1723
+ // } else {
1724
+ // throw new Error('No audio track found');
1725
+ // }
1726
+ // } catch (error) {
1727
+ // this.callKit.logger.error('Failed to set mute state', {
1728
+ // caller: 'Connect.setMute',
1729
+ // type: 'SIP',
1730
+ // content: {
1731
+ // err: error.message,
1732
+ // errCode: ErrorCode.WEBRTC_MUTE_ERROR
1733
+ // }
1734
+ // });
1735
+ // }
1736
+ // }
1660
1737
  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", {
1738
+ if (this.isMute === mute) {
1739
+ this.callKit.logger.warn("Already muted", {
1670
1740
  caller: "Connect.setMute",
1671
1741
  type: "SIP",
1672
1742
  content: {
1673
- errCode: ErrorCode.WEBRTC_MUTE_STATUS_ERROR
1743
+ isCalling: this.isCalling(),
1744
+ isMuted: this.isMuted()
1674
1745
  }
1675
1746
  });
1676
- this.callKit.reset();
1677
1747
  return;
1678
1748
  }
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
1718
- }
1719
- });
1720
- return;
1721
- }
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);
1749
+ this.callKit.socket.send(
1750
+ mute ? SocketSendEvent.MUTE : SocketSendEvent.UNMUTE
1751
+ );
1752
+ this.isMute = mute;
1753
+ this.callKit.trigger(KitEvent.KIT_SET_MUTE, mute);
1743
1754
  }
1744
1755
  async refer(referTo, extra) {
1745
1756
  this.callKit.logger.info("connect refer", {
@@ -1761,7 +1772,7 @@ var Connect = class {
1761
1772
  // package/socket.ts
1762
1773
  var RECONNECT_CONFIG = {
1763
1774
  enabled: true,
1764
- maxAttempts: 1,
1775
+ maxAttempts: 3,
1765
1776
  delay: 500,
1766
1777
  backoffMultiplier: 1.5,
1767
1778
  pingInterval: 3e4,
@@ -1779,6 +1790,7 @@ var Socket = class {
1779
1790
  reconnectTimer;
1780
1791
  isReconnecting = false;
1781
1792
  reconnectAttempts = 0;
1793
+ socketError = false;
1782
1794
  constructor(callKit) {
1783
1795
  this.callKit = callKit;
1784
1796
  const { reconnect } = this.callKit.config.getConfig();
@@ -1802,7 +1814,7 @@ var Socket = class {
1802
1814
  this.isConnected = false;
1803
1815
  if (!this.callKit.config.isLogin() || !this.socketConfig.enabled) {
1804
1816
  this.reset();
1805
- this.callKit.trigger(KitEvent.CONNECT_EVENT, {
1817
+ this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
1806
1818
  event: "INCALL_NOT_CONNECTED"
1807
1819
  });
1808
1820
  return;
@@ -1822,6 +1834,7 @@ var Socket = class {
1822
1834
  type: "INCALL",
1823
1835
  content: { ev }
1824
1836
  });
1837
+ this.socketError = false;
1825
1838
  this.isConnected = true;
1826
1839
  this.lastPingTime = Date.now();
1827
1840
  this.checkPing();
@@ -1834,7 +1847,7 @@ var Socket = class {
1834
1847
  reconnectAttempts: this.reconnectAttempts
1835
1848
  }
1836
1849
  });
1837
- this.callKit.trigger(KitEvent.CONNECT_EVENT, {
1850
+ this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
1838
1851
  event: "INCALL_RECONNECT_SUCCESS"
1839
1852
  });
1840
1853
  }
@@ -1854,10 +1867,13 @@ var Socket = class {
1854
1867
  type: "INCALL",
1855
1868
  content: { ev }
1856
1869
  });
1857
- this.callKit.trigger(KitEvent.CONNECT_EVENT, {
1870
+ this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
1858
1871
  event: "INCALL_CONNECT_ERROR",
1859
1872
  err: ev
1860
1873
  });
1874
+ if (this.socketError) {
1875
+ return;
1876
+ }
1861
1877
  this.handleDisconnect();
1862
1878
  }
1863
1879
  onError(ev) {
@@ -1869,6 +1885,7 @@ var Socket = class {
1869
1885
  data: ev
1870
1886
  }
1871
1887
  });
1888
+ this.socketError = true;
1872
1889
  }
1873
1890
  confirmAck(data) {
1874
1891
  const { ack, messageId } = data;
@@ -2026,7 +2043,7 @@ var Socket = class {
2026
2043
  }
2027
2044
  send(event, message) {
2028
2045
  if (!this.isConnected) {
2029
- this.callKit.trigger(KitEvent.CONNECT_EVENT, {
2046
+ this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
2030
2047
  event: "INCALL_NOT_CONNECTED"
2031
2048
  });
2032
2049
  this.callKit.logger.error("socket not connected", {
@@ -2085,7 +2102,7 @@ var Socket = class {
2085
2102
  }
2086
2103
  async sendMessage(event, message) {
2087
2104
  if (!this.isConnected) {
2088
- this.callKit.trigger(KitEvent.CONNECT_EVENT, {
2105
+ this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
2089
2106
  event: "INCALL_NOT_CONNECTED"
2090
2107
  });
2091
2108
  this.callKit.logger.error("socket not connected", {
@@ -2139,8 +2156,7 @@ var Socket = class {
2139
2156
  }, this.socketConfig.pingInterval);
2140
2157
  }
2141
2158
  /**
2142
- * 重置 socket 连接和所有状态
2143
- * @param isWaitConfirm Whether need to wait for close confirmation
2159
+ * reset socket connection and all states
2144
2160
  */
2145
2161
  async reset() {
2146
2162
  if (this.pingTimer) {
@@ -2150,6 +2166,7 @@ var Socket = class {
2150
2166
  this.resetReconnectState();
2151
2167
  this.lastPingTime = void 0;
2152
2168
  this.satrtConfirm = false;
2169
+ this.socketError = false;
2153
2170
  if (this.ws && this.isConnected) {
2154
2171
  this.callKit.logger.info("Closing socket connection", {
2155
2172
  caller: "Socket.reset",
@@ -2170,14 +2187,14 @@ var Socket = class {
2170
2187
  reconnectAttempts: this.reconnectAttempts
2171
2188
  }
2172
2189
  });
2173
- this.callKit.trigger(KitEvent.CONNECT_EVENT, {
2190
+ this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
2174
2191
  event: "INCALL_RECONNECT_ERROR"
2175
2192
  });
2176
2193
  this.reset();
2177
2194
  return;
2178
2195
  }
2179
2196
  if (this.reconnectAttempts === 0) {
2180
- this.callKit.trigger(KitEvent.CONNECT_EVENT, {
2197
+ this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
2181
2198
  event: "INCALL_RECONNECT_START"
2182
2199
  });
2183
2200
  }
@@ -2439,6 +2456,28 @@ var CallKit = class {
2439
2456
  });
2440
2457
  this.callCenter.callUnhold();
2441
2458
  }
2459
+ mute() {
2460
+ if (!this.config.check())
2461
+ return;
2462
+ this.logger.info("mute", {
2463
+ caller: "CallKit.mute",
2464
+ content: {
2465
+ connectStatus: this.connect.connectStatus
2466
+ }
2467
+ });
2468
+ this.callCenter.callMute();
2469
+ }
2470
+ unmute() {
2471
+ if (!this.config.check())
2472
+ return;
2473
+ this.logger.info("unmute", {
2474
+ caller: "CallKit.unmute",
2475
+ content: {
2476
+ connectStatus: this.connect.connectStatus
2477
+ }
2478
+ });
2479
+ this.callCenter.callUnmute();
2480
+ }
2442
2481
  /**
2443
2482
  * set userstatus
2444
2483
  * @param status