@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.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
  */
@@ -243,7 +243,8 @@ var KitEvent = {
243
243
  * User status change
244
244
  */
245
245
  USER_STATUS_CHANGE: "userStatusChange",
246
- CONNECT_EVENT: "CONNECT_EVENT",
246
+ INCALL_CONNECT_EVENT: "IncallConnectEvent",
247
+ SIP_CONNECT_EVENT: "sipConnectEvent",
247
248
  SIP_REGISTERER_EVENT: "sipRegistererEvent",
248
249
  SIP_SESSION_EVENT: "sipSessionEvent"
249
250
  };
@@ -365,6 +366,14 @@ var SocketSendEvent = {
365
366
  * Unhold
366
367
  */
367
368
  UNHOLD: "AGENT_UN_HOLD",
369
+ /**
370
+ * Mute
371
+ */
372
+ MUTE: "AGENT_MUTE",
373
+ /**
374
+ * Unmute
375
+ */
376
+ UNMUTE: "AGENT_UN_MUTE",
368
377
  /**
369
378
  * Call
370
379
  */
@@ -539,28 +548,58 @@ var Call = class {
539
548
  this.callKit.logger.warn("Current state cannot be held", {
540
549
  caller: "Call.callHold",
541
550
  content: {
551
+ isHold: this.callKit.connect.isHolding(),
542
552
  isCalling: this.callKit.connect.isCalling()
543
553
  }
544
554
  });
545
555
  return;
546
556
  }
547
- this.callKit.socket.send(SocketSendEvent.HOLD);
548
- this.callKit.connect.setConnectStatus(CallStatus.holding);
557
+ this.callKit.connect.setHold(true);
549
558
  }
550
559
  async callUnhold() {
551
560
  if (!this.callKit.config.check())
552
561
  return;
553
- if (!this.callKit.connect.isHolding()) {
562
+ if (!this.callKit.connect.isCalling()) {
554
563
  this.callKit.logger.warn("Current state cannot unhold", {
555
564
  caller: "Call.callUnhold",
556
565
  content: {
557
- isHolding: this.callKit.connect.isHolding()
566
+ isHold: this.callKit.connect.isHolding(),
567
+ isCalling: this.callKit.connect.isCalling()
568
+ }
569
+ });
570
+ return;
571
+ }
572
+ this.callKit.connect.setHold(false);
573
+ }
574
+ async callMute() {
575
+ if (!this.callKit.config.check())
576
+ return;
577
+ if (!this.callKit.connect.isCalling()) {
578
+ this.callKit.logger.warn("Current state cannot be muted", {
579
+ caller: "Call.callMute",
580
+ content: {
581
+ isMuted: this.callKit.connect.isMuted(),
582
+ isCalling: this.callKit.connect.isCalling()
583
+ }
584
+ });
585
+ return;
586
+ }
587
+ this.callKit.connect.setMute(true);
588
+ }
589
+ async callUnmute() {
590
+ if (!this.callKit.config.check())
591
+ return;
592
+ if (!this.callKit.connect.isCalling()) {
593
+ this.callKit.logger.warn("Current state cannot be unmuted", {
594
+ caller: "Call.callUnmute",
595
+ content: {
596
+ isMuted: this.callKit.connect.isMuted(),
597
+ isCalling: this.callKit.connect.isCalling()
558
598
  }
559
599
  });
560
600
  return;
561
601
  }
562
- this.callKit.socket.send(SocketSendEvent.UNHOLD);
563
- this.callKit.connect.setConnectStatus(CallStatus.calling);
602
+ this.callKit.connect.setMute(false);
564
603
  }
565
604
  };
566
605
 
@@ -868,6 +907,10 @@ var Connect = class {
868
907
  * Whether registered
869
908
  */
870
909
  isRegister = false;
910
+ /**
911
+ * Whether holding
912
+ */
913
+ isHold = false;
871
914
  constructor(callKit) {
872
915
  this.callKit = callKit;
873
916
  const { reconnect = {} } = this.callKit.config.getConfig();
@@ -878,12 +921,12 @@ var Connect = class {
878
921
  }
879
922
  reset() {
880
923
  if (this.isHolding()) {
881
- this.setHoldStatus(false);
924
+ this.setHold(false);
882
925
  }
883
926
  if (this.connectStatus !== CallStatus.init) {
884
927
  this.setConnectStatus(CallStatus.init);
885
928
  }
886
- if (this.isRegistered) {
929
+ if (this.isRegistered()) {
887
930
  this.unregister();
888
931
  }
889
932
  this.currentSession = void 0;
@@ -953,7 +996,7 @@ var Connect = class {
953
996
  * isHolding
954
997
  */
955
998
  isHolding() {
956
- return this.connectStatus === CallStatus.holding;
999
+ return this.isHold;
957
1000
  }
958
1001
  /**
959
1002
  * isRegistered
@@ -987,7 +1030,7 @@ var Connect = class {
987
1030
  this.heartbeatFlag -= 1;
988
1031
  if (this.heartbeatFlag <= 0) {
989
1032
  this.heartbeatFlag = MAX_HEARTBEAT_COUNT;
990
- this.callKit.trigger(KitEvent.CONNECT_EVENT, {
1033
+ this.callKit.trigger(KitEvent.SIP_CONNECT_EVENT, {
991
1034
  event: "OPTIONS_HEARTBEAT_EXPIRED"
992
1035
  });
993
1036
  }
@@ -1168,8 +1211,8 @@ var Connect = class {
1168
1211
  this.setRegister(false);
1169
1212
  this.setConnectStatus(CallStatus.init);
1170
1213
  this.callKit.trigger(KitEvent.SIP_REGISTERER_EVENT, {
1171
- isRegistered: this.isRegistered(),
1172
- registererState: state
1214
+ registererState: state,
1215
+ isRegistered: this.isRegistered()
1173
1216
  });
1174
1217
  break;
1175
1218
  case import_sip.RegistererState.Unregistered:
@@ -1184,8 +1227,8 @@ var Connect = class {
1184
1227
  this.setRegister(false);
1185
1228
  this.setConnectStatus(CallStatus.init);
1186
1229
  this.callKit.trigger(KitEvent.SIP_REGISTERER_EVENT, {
1187
- isRegistered: this.isRegistered(),
1188
- registererState: state
1230
+ registererState: state,
1231
+ isRegistered: this.isRegistered()
1189
1232
  });
1190
1233
  break;
1191
1234
  default:
@@ -1276,7 +1319,6 @@ var Connect = class {
1276
1319
  };
1277
1320
  if (this.isOutgoing) {
1278
1321
  this.currentSession.accept(options);
1279
- this.setConnectStatus(CallStatus.connecting);
1280
1322
  this.callKit.trigger(KitEvent.KIT_OUTGOING_INVITE, {
1281
1323
  getInviteData: () => {
1282
1324
  const { request: request2 } = this.currentSession;
@@ -1640,133 +1682,102 @@ var Connect = class {
1640
1682
  });
1641
1683
  }
1642
1684
  }
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
1685
  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",
1686
+ if (this.isHold === hold) {
1687
+ this.callKit.logger.warn("Already holding", {
1688
+ caller: "Connect.setHold",
1671
1689
  type: "SIP",
1672
1690
  content: {
1673
- errCode: ErrorCode.WEBRTC_HOLE_STATUS_ERROR
1691
+ isHold: this.isHold
1674
1692
  }
1675
1693
  });
1694
+ return;
1676
1695
  }
1677
- }
1678
- async unhold() {
1679
- this.callKit.logger.info("connect unhold", {
1680
- caller: "Connect.unhold",
1696
+ this.callKit.socket.send(
1697
+ hold ? SocketSendEvent.HOLD : SocketSendEvent.UNHOLD
1698
+ );
1699
+ this.isHold = hold;
1700
+ this.callKit.logger.info("connect setHold", {
1701
+ caller: "Connect.setHold",
1681
1702
  type: "SIP",
1682
1703
  content: {
1683
- hold: false
1704
+ hold
1684
1705
  }
1685
1706
  });
1707
+ this.callKit.trigger(KitEvent.KIT_SET_HOLD, hold);
1686
1708
  }
1709
+ // /**
1710
+ // * Set mute
1711
+ // * @param mute Whether to mute
1712
+ // * @deprecated just send socket event to server to mute or unmute
1713
+ // */
1714
+ // async setMute(mute: boolean) {
1715
+ // this.callKit.logger.info('connect setMute', {
1716
+ // caller: 'Connect.setMute',
1717
+ // type: 'SIP',
1718
+ // content: {
1719
+ // mute
1720
+ // }
1721
+ // });
1722
+ // if (!this.currentSession) {
1723
+ // this.callKit.logger.error('No active session', {
1724
+ // caller: 'Connect.setMute',
1725
+ // type: 'SIP',
1726
+ // content: {
1727
+ // errCode: ErrorCode.WEBRTC_MUTE_STATUS_ERROR
1728
+ // }
1729
+ // });
1730
+ // this.callKit.reset();
1731
+ // return;
1732
+ // }
1733
+ // try {
1734
+ // // Get SessionDescriptionHandler
1735
+ // const sdh = this.currentSession.sessionDescriptionHandler;
1736
+ // if (!sdh || !('peerConnection' in sdh)) {
1737
+ // throw new Error('Invalid session description handler');
1738
+ // }
1739
+ // // Get PeerConnection
1740
+ // const pc = (sdh as any).peerConnection as RTCPeerConnection;
1741
+ // // Get local audio track and set status
1742
+ // const audioSender = pc
1743
+ // .getSenders()
1744
+ // .find((sender) => sender.track?.kind === 'audio');
1745
+ // if (audioSender && audioSender.track) {
1746
+ // audioSender.track.enabled = !mute;
1747
+ // // Update status and trigger event
1748
+ // this.isMute = mute;
1749
+ // this.callKit.trigger(KitEvent.KIT_SET_MUTE, mute);
1750
+ // } else {
1751
+ // throw new Error('No audio track found');
1752
+ // }
1753
+ // } catch (error) {
1754
+ // this.callKit.logger.error('Failed to set mute state', {
1755
+ // caller: 'Connect.setMute',
1756
+ // type: 'SIP',
1757
+ // content: {
1758
+ // err: error.message,
1759
+ // errCode: ErrorCode.WEBRTC_MUTE_ERROR
1760
+ // }
1761
+ // });
1762
+ // }
1763
+ // }
1687
1764
  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", {
1765
+ if (this.isMute === mute) {
1766
+ this.callKit.logger.warn("Already muted", {
1697
1767
  caller: "Connect.setMute",
1698
1768
  type: "SIP",
1699
1769
  content: {
1700
- errCode: ErrorCode.WEBRTC_MUTE_STATUS_ERROR
1770
+ isCalling: this.isCalling(),
1771
+ isMuted: this.isMuted()
1701
1772
  }
1702
1773
  });
1703
- this.callKit.reset();
1704
1774
  return;
1705
1775
  }
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
1745
- }
1746
- });
1747
- return;
1748
- }
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);
1776
+ this.callKit.socket.send(
1777
+ mute ? SocketSendEvent.MUTE : SocketSendEvent.UNMUTE
1778
+ );
1779
+ this.isMute = mute;
1780
+ this.callKit.trigger(KitEvent.KIT_SET_MUTE, mute);
1770
1781
  }
1771
1782
  async refer(referTo, extra) {
1772
1783
  this.callKit.logger.info("connect refer", {
@@ -1788,7 +1799,7 @@ var Connect = class {
1788
1799
  // package/socket.ts
1789
1800
  var RECONNECT_CONFIG = {
1790
1801
  enabled: true,
1791
- maxAttempts: 1,
1802
+ maxAttempts: 3,
1792
1803
  delay: 500,
1793
1804
  backoffMultiplier: 1.5,
1794
1805
  pingInterval: 3e4,
@@ -1806,6 +1817,7 @@ var Socket = class {
1806
1817
  reconnectTimer;
1807
1818
  isReconnecting = false;
1808
1819
  reconnectAttempts = 0;
1820
+ socketError = false;
1809
1821
  constructor(callKit) {
1810
1822
  this.callKit = callKit;
1811
1823
  const { reconnect } = this.callKit.config.getConfig();
@@ -1829,7 +1841,7 @@ var Socket = class {
1829
1841
  this.isConnected = false;
1830
1842
  if (!this.callKit.config.isLogin() || !this.socketConfig.enabled) {
1831
1843
  this.reset();
1832
- this.callKit.trigger(KitEvent.CONNECT_EVENT, {
1844
+ this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
1833
1845
  event: "INCALL_NOT_CONNECTED"
1834
1846
  });
1835
1847
  return;
@@ -1849,6 +1861,7 @@ var Socket = class {
1849
1861
  type: "INCALL",
1850
1862
  content: { ev }
1851
1863
  });
1864
+ this.socketError = false;
1852
1865
  this.isConnected = true;
1853
1866
  this.lastPingTime = Date.now();
1854
1867
  this.checkPing();
@@ -1861,7 +1874,7 @@ var Socket = class {
1861
1874
  reconnectAttempts: this.reconnectAttempts
1862
1875
  }
1863
1876
  });
1864
- this.callKit.trigger(KitEvent.CONNECT_EVENT, {
1877
+ this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
1865
1878
  event: "INCALL_RECONNECT_SUCCESS"
1866
1879
  });
1867
1880
  }
@@ -1881,10 +1894,13 @@ var Socket = class {
1881
1894
  type: "INCALL",
1882
1895
  content: { ev }
1883
1896
  });
1884
- this.callKit.trigger(KitEvent.CONNECT_EVENT, {
1897
+ this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
1885
1898
  event: "INCALL_CONNECT_ERROR",
1886
1899
  err: ev
1887
1900
  });
1901
+ if (this.socketError) {
1902
+ return;
1903
+ }
1888
1904
  this.handleDisconnect();
1889
1905
  }
1890
1906
  onError(ev) {
@@ -1896,6 +1912,7 @@ var Socket = class {
1896
1912
  data: ev
1897
1913
  }
1898
1914
  });
1915
+ this.socketError = true;
1899
1916
  }
1900
1917
  confirmAck(data) {
1901
1918
  const { ack, messageId } = data;
@@ -2053,7 +2070,7 @@ var Socket = class {
2053
2070
  }
2054
2071
  send(event, message) {
2055
2072
  if (!this.isConnected) {
2056
- this.callKit.trigger(KitEvent.CONNECT_EVENT, {
2073
+ this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
2057
2074
  event: "INCALL_NOT_CONNECTED"
2058
2075
  });
2059
2076
  this.callKit.logger.error("socket not connected", {
@@ -2112,7 +2129,7 @@ var Socket = class {
2112
2129
  }
2113
2130
  async sendMessage(event, message) {
2114
2131
  if (!this.isConnected) {
2115
- this.callKit.trigger(KitEvent.CONNECT_EVENT, {
2132
+ this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
2116
2133
  event: "INCALL_NOT_CONNECTED"
2117
2134
  });
2118
2135
  this.callKit.logger.error("socket not connected", {
@@ -2166,8 +2183,7 @@ var Socket = class {
2166
2183
  }, this.socketConfig.pingInterval);
2167
2184
  }
2168
2185
  /**
2169
- * 重置 socket 连接和所有状态
2170
- * @param isWaitConfirm Whether need to wait for close confirmation
2186
+ * reset socket connection and all states
2171
2187
  */
2172
2188
  async reset() {
2173
2189
  if (this.pingTimer) {
@@ -2177,6 +2193,7 @@ var Socket = class {
2177
2193
  this.resetReconnectState();
2178
2194
  this.lastPingTime = void 0;
2179
2195
  this.satrtConfirm = false;
2196
+ this.socketError = false;
2180
2197
  if (this.ws && this.isConnected) {
2181
2198
  this.callKit.logger.info("Closing socket connection", {
2182
2199
  caller: "Socket.reset",
@@ -2197,14 +2214,14 @@ var Socket = class {
2197
2214
  reconnectAttempts: this.reconnectAttempts
2198
2215
  }
2199
2216
  });
2200
- this.callKit.trigger(KitEvent.CONNECT_EVENT, {
2217
+ this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
2201
2218
  event: "INCALL_RECONNECT_ERROR"
2202
2219
  });
2203
2220
  this.reset();
2204
2221
  return;
2205
2222
  }
2206
2223
  if (this.reconnectAttempts === 0) {
2207
- this.callKit.trigger(KitEvent.CONNECT_EVENT, {
2224
+ this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
2208
2225
  event: "INCALL_RECONNECT_START"
2209
2226
  });
2210
2227
  }
@@ -2466,6 +2483,28 @@ var CallKit = class {
2466
2483
  });
2467
2484
  this.callCenter.callUnhold();
2468
2485
  }
2486
+ mute() {
2487
+ if (!this.config.check())
2488
+ return;
2489
+ this.logger.info("mute", {
2490
+ caller: "CallKit.mute",
2491
+ content: {
2492
+ connectStatus: this.connect.connectStatus
2493
+ }
2494
+ });
2495
+ this.callCenter.callMute();
2496
+ }
2497
+ unmute() {
2498
+ if (!this.config.check())
2499
+ return;
2500
+ this.logger.info("unmute", {
2501
+ caller: "CallKit.unmute",
2502
+ content: {
2503
+ connectStatus: this.connect.connectStatus
2504
+ }
2505
+ });
2506
+ this.callCenter.callUnmute();
2507
+ }
2469
2508
  /**
2470
2509
  * set userstatus
2471
2510
  * @param status