@koi-design/callkit 2.0.0-beta.2 → 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
 
@@ -867,7 +905,11 @@ var Connect = class {
867
905
  /**
868
906
  * Whether registered
869
907
  */
870
- isRegistered = false;
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);
@@ -907,6 +949,7 @@ var Connect = class {
907
949
  });
908
950
  }
909
951
  }
952
+ this.callKit.config.reset();
910
953
  this.setConnectStatus(CallStatus.init);
911
954
  this.clearObserveOptionsHeartbeatInterval();
912
955
  }
@@ -949,10 +992,22 @@ var Connect = class {
949
992
  return this.connectStatus === CallStatus.ringing;
950
993
  }
951
994
  /**
952
- *
995
+ * isHolding
953
996
  */
954
997
  isHolding() {
955
- return this.connectStatus === CallStatus.holding;
998
+ return this.isHold;
999
+ }
1000
+ /**
1001
+ * isRegistered
1002
+ */
1003
+ isRegistered() {
1004
+ return this.isRegister;
1005
+ }
1006
+ /**
1007
+ * isMute
1008
+ */
1009
+ isMuted() {
1010
+ return this.isMute;
956
1011
  }
957
1012
  /**
958
1013
  * Call ended, call not started
@@ -982,7 +1037,7 @@ var Connect = class {
982
1037
  }
983
1038
  async register() {
984
1039
  if (this.connectStatus !== CallStatus.init) {
985
- if (this.isRegistered) {
1040
+ if (this.isRegistered()) {
986
1041
  this.callKit.logger.warn("connectStatus is registered", {
987
1042
  caller: "Connect.register",
988
1043
  content: {
@@ -1118,14 +1173,14 @@ var Connect = class {
1118
1173
  type: "SIP",
1119
1174
  content: {
1120
1175
  registererState: state,
1121
- isRegistered: this.isRegistered
1176
+ isRegistered: this.isRegistered()
1122
1177
  }
1123
1178
  });
1124
1179
  this.setRegister(false);
1125
1180
  this.setConnectStatus(CallStatus.init);
1126
1181
  this.callKit.trigger(KitEvent.SIP_REGISTERER_EVENT, {
1127
1182
  registererState: state,
1128
- isRegistered: this.isRegistered
1183
+ isRegistered: this.isRegistered()
1129
1184
  });
1130
1185
  break;
1131
1186
  case import_sip.RegistererState.Registered:
@@ -1134,14 +1189,10 @@ var Connect = class {
1134
1189
  type: "SIP",
1135
1190
  content: {
1136
1191
  registererState: state,
1137
- isRegistered: this.isRegistered
1192
+ isRegistered: this.isRegistered()
1138
1193
  }
1139
1194
  });
1140
1195
  this.setRegister(true);
1141
- this.callKit.trigger(KitEvent.SIP_REGISTERER_EVENT, {
1142
- registererState: state,
1143
- isRegistered: this.isRegistered
1144
- });
1145
1196
  break;
1146
1197
  case import_sip.RegistererState.Terminated:
1147
1198
  this.callKit.logger.info("registerer stateChange Terminated", {
@@ -1149,31 +1200,23 @@ var Connect = class {
1149
1200
  type: "SIP",
1150
1201
  content: {
1151
1202
  registererState: state,
1152
- isRegistered: this.isRegistered
1203
+ isRegistered: this.isRegistered()
1153
1204
  }
1154
1205
  });
1155
1206
  this.setRegister(false);
1156
1207
  this.setConnectStatus(CallStatus.init);
1157
- this.callKit.trigger(KitEvent.SIP_REGISTERER_EVENT, {
1158
- isRegistered: this.isRegistered,
1159
- registererState: state
1160
- });
1161
1208
  break;
1162
1209
  case import_sip.RegistererState.Unregistered:
1163
1210
  this.callKit.logger.info("registerer stateChange Unregistered", {
1164
1211
  caller: "Connect.register.registererStateChange",
1165
1212
  type: "SIP",
1166
1213
  content: {
1167
- isRegistered: this.isRegistered,
1214
+ isRegistered: this.isRegistered(),
1168
1215
  registererState: state
1169
1216
  }
1170
1217
  });
1171
1218
  this.setRegister(false);
1172
1219
  this.setConnectStatus(CallStatus.init);
1173
- this.callKit.trigger(KitEvent.SIP_REGISTERER_EVENT, {
1174
- isRegistered: this.isRegistered,
1175
- registererState: state
1176
- });
1177
1220
  break;
1178
1221
  default:
1179
1222
  break;
@@ -1186,7 +1229,7 @@ var Connect = class {
1186
1229
  caller: "Connect.register.onInvite",
1187
1230
  content: {
1188
1231
  invite,
1189
- isRegistered: this.isRegistered
1232
+ isRegistered: this.isRegistered()
1190
1233
  }
1191
1234
  });
1192
1235
  this.currentSession = invite;
@@ -1206,7 +1249,7 @@ var Connect = class {
1206
1249
  this.setConnectStatus(CallStatus.ringing);
1207
1250
  this.callKit.trigger(KitEvent.SIP_SESSION_EVENT, {
1208
1251
  sessionState: state,
1209
- isRegistered: this.isRegistered
1252
+ isRegistered: this.isRegistered()
1210
1253
  });
1211
1254
  break;
1212
1255
  case import_sip.SessionState.Established:
@@ -1220,14 +1263,14 @@ var Connect = class {
1220
1263
  this.callKit.connect.setConnectStatus(CallStatus.calling);
1221
1264
  this.callKit.trigger(KitEvent.SIP_SESSION_EVENT, {
1222
1265
  sessionState: state,
1223
- isRegistered: this.isRegistered
1266
+ isRegistered: this.isRegistered()
1224
1267
  });
1225
1268
  setupRemoteMedia(this.currentSession);
1226
1269
  break;
1227
1270
  case import_sip.SessionState.Terminating:
1228
1271
  this.callKit.trigger(KitEvent.SIP_SESSION_EVENT, {
1229
1272
  sessionState: state,
1230
- isRegistered: this.isRegistered
1273
+ isRegistered: this.isRegistered()
1231
1274
  });
1232
1275
  break;
1233
1276
  case import_sip.SessionState.Terminated:
@@ -1248,7 +1291,7 @@ var Connect = class {
1248
1291
  this.isUnprompted = false;
1249
1292
  this.callKit.trigger(KitEvent.SIP_SESSION_EVENT, {
1250
1293
  sessionState: state,
1251
- isRegistered: this.isRegistered
1294
+ isRegistered: this.isRegistered()
1252
1295
  });
1253
1296
  break;
1254
1297
  default:
@@ -1435,11 +1478,10 @@ var Connect = class {
1435
1478
  caller: "Connect.unregister",
1436
1479
  type: "SIP",
1437
1480
  content: {
1438
- isRegistered: this.isRegistered,
1439
- registerer: this.registerer
1481
+ isRegistered: this.isRegistered()
1440
1482
  }
1441
1483
  });
1442
- if (!this.isRegistered || !this.registerer) {
1484
+ if (!this.isRegistered() || !this.registerer) {
1443
1485
  this.callKit.logger.warn("No registerer to unregister.", {
1444
1486
  caller: "Connect.unregister",
1445
1487
  type: "SIP",
@@ -1469,7 +1511,7 @@ var Connect = class {
1469
1511
  }
1470
1512
  });
1471
1513
  this.isOutgoing = true;
1472
- if (!this.isRegistered) {
1514
+ if (!this.isRegistered()) {
1473
1515
  await this.register();
1474
1516
  }
1475
1517
  this.setConnectStatus(CallStatus.connecting);
@@ -1489,7 +1531,7 @@ var Connect = class {
1489
1531
  register
1490
1532
  }
1491
1533
  });
1492
- this.isRegistered = register;
1534
+ this.isRegister = register;
1493
1535
  this.callKit.trigger(KitEvent.KIT_REGISTER_CHANGE, register);
1494
1536
  }
1495
1537
  /**
@@ -1628,133 +1670,102 @@ var Connect = class {
1628
1670
  });
1629
1671
  }
1630
1672
  }
1631
- /**
1632
- * Update hold
1633
- * @param hold
1634
- */
1635
- setHoldStatus(hold) {
1636
- this.callKit.logger.info("connect setHold", {
1637
- caller: "Connect.setHoldStatus",
1638
- type: "SIP",
1639
- content: {
1640
- hold
1641
- }
1642
- });
1643
- this.callKit.trigger(KitEvent.KIT_SET_HOLD, hold);
1644
- }
1645
1673
  async setHold(hold) {
1646
- this.setHoldStatus(hold);
1647
- }
1648
- async hold() {
1649
- this.callKit.logger.info("connect hold", {
1650
- caller: "Connect.hold",
1651
- type: "SIP",
1652
- content: {
1653
- hold: true
1654
- }
1655
- });
1656
- if (this.connectStatus !== CallStatus.calling || !this.currentSession) {
1657
- this.callKit.logger.error("Current status is not in call", {
1658
- caller: "Connect.hold",
1674
+ if (this.isHold === hold) {
1675
+ this.callKit.logger.warn("Already holding", {
1676
+ caller: "Connect.setHold",
1659
1677
  type: "SIP",
1660
1678
  content: {
1661
- errCode: ErrorCode.WEBRTC_HOLE_STATUS_ERROR
1679
+ isHold: this.isHold
1662
1680
  }
1663
1681
  });
1682
+ return;
1664
1683
  }
1665
- }
1666
- async unhold() {
1667
- this.callKit.logger.info("connect unhold", {
1668
- 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",
1669
1690
  type: "SIP",
1670
1691
  content: {
1671
- hold: false
1692
+ hold
1672
1693
  }
1673
1694
  });
1695
+ this.callKit.trigger(KitEvent.KIT_SET_HOLD, hold);
1674
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
+ // }
1675
1752
  async setMute(mute) {
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", {
1753
+ if (this.isMute === mute) {
1754
+ this.callKit.logger.warn("Already muted", {
1685
1755
  caller: "Connect.setMute",
1686
1756
  type: "SIP",
1687
1757
  content: {
1688
- errCode: ErrorCode.WEBRTC_MUTE_STATUS_ERROR
1758
+ isCalling: this.isCalling(),
1759
+ isMuted: this.isMuted()
1689
1760
  }
1690
1761
  });
1691
- this.callKit.reset();
1692
1762
  return;
1693
1763
  }
1694
- try {
1695
- const sdh = this.currentSession.sessionDescriptionHandler;
1696
- if (!sdh || !("peerConnection" in sdh)) {
1697
- throw new Error("Invalid session description handler");
1698
- }
1699
- const pc = sdh.peerConnection;
1700
- const audioSender = pc.getSenders().find((sender) => sender.track?.kind === "audio");
1701
- if (audioSender && audioSender.track) {
1702
- audioSender.track.enabled = !mute;
1703
- this.isMute = mute;
1704
- this.callKit.trigger(KitEvent.KIT_SET_MUTE, mute);
1705
- } else {
1706
- throw new Error("No audio track found");
1707
- }
1708
- } catch (error) {
1709
- this.callKit.logger.error("Failed to set mute state", {
1710
- caller: "Connect.setMute",
1711
- type: "SIP",
1712
- content: {
1713
- err: error.message,
1714
- errCode: ErrorCode.WEBRTC_MUTE_ERROR
1715
- }
1716
- });
1717
- }
1718
- }
1719
- async mute() {
1720
- this.callKit.logger.info("connect mute", {
1721
- caller: "Connect.mute",
1722
- type: "SIP",
1723
- content: {
1724
- mute: true
1725
- }
1726
- });
1727
- if (this.connectStatus !== CallStatus.calling || !this.currentSession) {
1728
- this.callKit.logger.warn("Current status is not in call", {
1729
- caller: "Connect.mute",
1730
- type: "SIP",
1731
- content: {
1732
- errCode: ErrorCode.WEBRTC_MUTE_STATUS_ERROR
1733
- }
1734
- });
1735
- return;
1736
- }
1737
- await this.setMute(true);
1738
- }
1739
- async unmute() {
1740
- this.callKit.logger.info("connect unmute", {
1741
- caller: "Connect.unmute",
1742
- type: "SIP",
1743
- content: {
1744
- mute: false
1745
- }
1746
- });
1747
- if (this.connectStatus !== CallStatus.calling || !this.currentSession) {
1748
- this.callKit.logger.warn("Current status is not in call", {
1749
- caller: "Connect.unmute",
1750
- type: "SIP",
1751
- content: {
1752
- errCode: ErrorCode.WEBRTC_MUTE_STATUS_ERROR
1753
- }
1754
- });
1755
- return;
1756
- }
1757
- 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);
1758
1769
  }
1759
1770
  async refer(referTo, extra) {
1760
1771
  this.callKit.logger.info("connect refer", {
@@ -2051,7 +2062,6 @@ var Socket = class {
2051
2062
  errCode: ErrorCode.SOCKET_CONNECT_ERROR
2052
2063
  }
2053
2064
  });
2054
- this.callKit.config.reset();
2055
2065
  this.callKit.reset();
2056
2066
  return;
2057
2067
  }
@@ -2158,7 +2168,7 @@ var Socket = class {
2158
2168
  * 重置 socket 连接和所有状态
2159
2169
  * @param isWaitConfirm Whether need to wait for close confirmation
2160
2170
  */
2161
- async reset(isWaitConfirm = true) {
2171
+ async reset() {
2162
2172
  if (this.pingTimer) {
2163
2173
  clearInterval(this.pingTimer);
2164
2174
  this.pingTimer = void 0;
@@ -2170,9 +2180,7 @@ var Socket = class {
2170
2180
  this.callKit.logger.info("Closing socket connection", {
2171
2181
  caller: "Socket.reset",
2172
2182
  type: "INCALL",
2173
- content: {
2174
- isWaitConfirm
2175
- }
2183
+ content: {}
2176
2184
  });
2177
2185
  this.ws.close();
2178
2186
  this.isConnected = false;
@@ -2344,8 +2352,8 @@ var CallKit = class {
2344
2352
  }
2345
2353
  }
2346
2354
  await this.hangup();
2347
- this.socket.reset(false);
2348
2355
  this.connect.reset();
2356
+ this.socket.reset();
2349
2357
  this.config.reset();
2350
2358
  this.trigger(KitEvent.KIT_LOGIN_CHANGE, false);
2351
2359
  }
@@ -2457,6 +2465,28 @@ var CallKit = class {
2457
2465
  });
2458
2466
  this.callCenter.callUnhold();
2459
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
+ }
2460
2490
  /**
2461
2491
  * set userstatus
2462
2492
  * @param status