@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.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
 
@@ -840,7 +878,11 @@ var Connect = class {
840
878
  /**
841
879
  * Whether registered
842
880
  */
843
- isRegistered = false;
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);
@@ -880,6 +922,7 @@ var Connect = class {
880
922
  });
881
923
  }
882
924
  }
925
+ this.callKit.config.reset();
883
926
  this.setConnectStatus(CallStatus.init);
884
927
  this.clearObserveOptionsHeartbeatInterval();
885
928
  }
@@ -922,10 +965,22 @@ var Connect = class {
922
965
  return this.connectStatus === CallStatus.ringing;
923
966
  }
924
967
  /**
925
- *
968
+ * isHolding
926
969
  */
927
970
  isHolding() {
928
- return this.connectStatus === CallStatus.holding;
971
+ return this.isHold;
972
+ }
973
+ /**
974
+ * isRegistered
975
+ */
976
+ isRegistered() {
977
+ return this.isRegister;
978
+ }
979
+ /**
980
+ * isMute
981
+ */
982
+ isMuted() {
983
+ return this.isMute;
929
984
  }
930
985
  /**
931
986
  * Call ended, call not started
@@ -955,7 +1010,7 @@ var Connect = class {
955
1010
  }
956
1011
  async register() {
957
1012
  if (this.connectStatus !== CallStatus.init) {
958
- if (this.isRegistered) {
1013
+ if (this.isRegistered()) {
959
1014
  this.callKit.logger.warn("connectStatus is registered", {
960
1015
  caller: "Connect.register",
961
1016
  content: {
@@ -1091,14 +1146,14 @@ var Connect = class {
1091
1146
  type: "SIP",
1092
1147
  content: {
1093
1148
  registererState: state,
1094
- isRegistered: this.isRegistered
1149
+ isRegistered: this.isRegistered()
1095
1150
  }
1096
1151
  });
1097
1152
  this.setRegister(false);
1098
1153
  this.setConnectStatus(CallStatus.init);
1099
1154
  this.callKit.trigger(KitEvent.SIP_REGISTERER_EVENT, {
1100
1155
  registererState: state,
1101
- isRegistered: this.isRegistered
1156
+ isRegistered: this.isRegistered()
1102
1157
  });
1103
1158
  break;
1104
1159
  case RegistererState.Registered:
@@ -1107,14 +1162,10 @@ var Connect = class {
1107
1162
  type: "SIP",
1108
1163
  content: {
1109
1164
  registererState: state,
1110
- isRegistered: this.isRegistered
1165
+ isRegistered: this.isRegistered()
1111
1166
  }
1112
1167
  });
1113
1168
  this.setRegister(true);
1114
- this.callKit.trigger(KitEvent.SIP_REGISTERER_EVENT, {
1115
- registererState: state,
1116
- isRegistered: this.isRegistered
1117
- });
1118
1169
  break;
1119
1170
  case RegistererState.Terminated:
1120
1171
  this.callKit.logger.info("registerer stateChange Terminated", {
@@ -1122,31 +1173,23 @@ var Connect = class {
1122
1173
  type: "SIP",
1123
1174
  content: {
1124
1175
  registererState: state,
1125
- isRegistered: this.isRegistered
1176
+ isRegistered: this.isRegistered()
1126
1177
  }
1127
1178
  });
1128
1179
  this.setRegister(false);
1129
1180
  this.setConnectStatus(CallStatus.init);
1130
- this.callKit.trigger(KitEvent.SIP_REGISTERER_EVENT, {
1131
- isRegistered: this.isRegistered,
1132
- registererState: state
1133
- });
1134
1181
  break;
1135
1182
  case RegistererState.Unregistered:
1136
1183
  this.callKit.logger.info("registerer stateChange Unregistered", {
1137
1184
  caller: "Connect.register.registererStateChange",
1138
1185
  type: "SIP",
1139
1186
  content: {
1140
- isRegistered: this.isRegistered,
1187
+ isRegistered: this.isRegistered(),
1141
1188
  registererState: state
1142
1189
  }
1143
1190
  });
1144
1191
  this.setRegister(false);
1145
1192
  this.setConnectStatus(CallStatus.init);
1146
- this.callKit.trigger(KitEvent.SIP_REGISTERER_EVENT, {
1147
- isRegistered: this.isRegistered,
1148
- registererState: state
1149
- });
1150
1193
  break;
1151
1194
  default:
1152
1195
  break;
@@ -1159,7 +1202,7 @@ var Connect = class {
1159
1202
  caller: "Connect.register.onInvite",
1160
1203
  content: {
1161
1204
  invite,
1162
- isRegistered: this.isRegistered
1205
+ isRegistered: this.isRegistered()
1163
1206
  }
1164
1207
  });
1165
1208
  this.currentSession = invite;
@@ -1179,7 +1222,7 @@ var Connect = class {
1179
1222
  this.setConnectStatus(CallStatus.ringing);
1180
1223
  this.callKit.trigger(KitEvent.SIP_SESSION_EVENT, {
1181
1224
  sessionState: state,
1182
- isRegistered: this.isRegistered
1225
+ isRegistered: this.isRegistered()
1183
1226
  });
1184
1227
  break;
1185
1228
  case SessionState.Established:
@@ -1193,14 +1236,14 @@ var Connect = class {
1193
1236
  this.callKit.connect.setConnectStatus(CallStatus.calling);
1194
1237
  this.callKit.trigger(KitEvent.SIP_SESSION_EVENT, {
1195
1238
  sessionState: state,
1196
- isRegistered: this.isRegistered
1239
+ isRegistered: this.isRegistered()
1197
1240
  });
1198
1241
  setupRemoteMedia(this.currentSession);
1199
1242
  break;
1200
1243
  case SessionState.Terminating:
1201
1244
  this.callKit.trigger(KitEvent.SIP_SESSION_EVENT, {
1202
1245
  sessionState: state,
1203
- isRegistered: this.isRegistered
1246
+ isRegistered: this.isRegistered()
1204
1247
  });
1205
1248
  break;
1206
1249
  case SessionState.Terminated:
@@ -1221,7 +1264,7 @@ var Connect = class {
1221
1264
  this.isUnprompted = false;
1222
1265
  this.callKit.trigger(KitEvent.SIP_SESSION_EVENT, {
1223
1266
  sessionState: state,
1224
- isRegistered: this.isRegistered
1267
+ isRegistered: this.isRegistered()
1225
1268
  });
1226
1269
  break;
1227
1270
  default:
@@ -1408,11 +1451,10 @@ var Connect = class {
1408
1451
  caller: "Connect.unregister",
1409
1452
  type: "SIP",
1410
1453
  content: {
1411
- isRegistered: this.isRegistered,
1412
- registerer: this.registerer
1454
+ isRegistered: this.isRegistered()
1413
1455
  }
1414
1456
  });
1415
- if (!this.isRegistered || !this.registerer) {
1457
+ if (!this.isRegistered() || !this.registerer) {
1416
1458
  this.callKit.logger.warn("No registerer to unregister.", {
1417
1459
  caller: "Connect.unregister",
1418
1460
  type: "SIP",
@@ -1442,7 +1484,7 @@ var Connect = class {
1442
1484
  }
1443
1485
  });
1444
1486
  this.isOutgoing = true;
1445
- if (!this.isRegistered) {
1487
+ if (!this.isRegistered()) {
1446
1488
  await this.register();
1447
1489
  }
1448
1490
  this.setConnectStatus(CallStatus.connecting);
@@ -1462,7 +1504,7 @@ var Connect = class {
1462
1504
  register
1463
1505
  }
1464
1506
  });
1465
- this.isRegistered = register;
1507
+ this.isRegister = register;
1466
1508
  this.callKit.trigger(KitEvent.KIT_REGISTER_CHANGE, register);
1467
1509
  }
1468
1510
  /**
@@ -1601,133 +1643,102 @@ var Connect = class {
1601
1643
  });
1602
1644
  }
1603
1645
  }
1604
- /**
1605
- * Update hold
1606
- * @param hold
1607
- */
1608
- setHoldStatus(hold) {
1609
- this.callKit.logger.info("connect setHold", {
1610
- caller: "Connect.setHoldStatus",
1611
- type: "SIP",
1612
- content: {
1613
- hold
1614
- }
1615
- });
1616
- this.callKit.trigger(KitEvent.KIT_SET_HOLD, hold);
1617
- }
1618
1646
  async setHold(hold) {
1619
- this.setHoldStatus(hold);
1620
- }
1621
- async hold() {
1622
- this.callKit.logger.info("connect hold", {
1623
- caller: "Connect.hold",
1624
- type: "SIP",
1625
- content: {
1626
- hold: true
1627
- }
1628
- });
1629
- if (this.connectStatus !== CallStatus.calling || !this.currentSession) {
1630
- this.callKit.logger.error("Current status is not in call", {
1631
- caller: "Connect.hold",
1647
+ if (this.isHold === hold) {
1648
+ this.callKit.logger.warn("Already holding", {
1649
+ caller: "Connect.setHold",
1632
1650
  type: "SIP",
1633
1651
  content: {
1634
- errCode: ErrorCode.WEBRTC_HOLE_STATUS_ERROR
1652
+ isHold: this.isHold
1635
1653
  }
1636
1654
  });
1655
+ return;
1637
1656
  }
1638
- }
1639
- async unhold() {
1640
- this.callKit.logger.info("connect unhold", {
1641
- 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",
1642
1663
  type: "SIP",
1643
1664
  content: {
1644
- hold: false
1665
+ hold
1645
1666
  }
1646
1667
  });
1668
+ this.callKit.trigger(KitEvent.KIT_SET_HOLD, hold);
1647
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
+ // }
1648
1725
  async setMute(mute) {
1649
- this.callKit.logger.info("connect setMute", {
1650
- caller: "Connect.setMute",
1651
- type: "SIP",
1652
- content: {
1653
- mute
1654
- }
1655
- });
1656
- if (!this.currentSession) {
1657
- this.callKit.logger.error("No active session", {
1726
+ if (this.isMute === mute) {
1727
+ this.callKit.logger.warn("Already muted", {
1658
1728
  caller: "Connect.setMute",
1659
1729
  type: "SIP",
1660
1730
  content: {
1661
- errCode: ErrorCode.WEBRTC_MUTE_STATUS_ERROR
1731
+ isCalling: this.isCalling(),
1732
+ isMuted: this.isMuted()
1662
1733
  }
1663
1734
  });
1664
- this.callKit.reset();
1665
1735
  return;
1666
1736
  }
1667
- try {
1668
- const sdh = this.currentSession.sessionDescriptionHandler;
1669
- if (!sdh || !("peerConnection" in sdh)) {
1670
- throw new Error("Invalid session description handler");
1671
- }
1672
- const pc = sdh.peerConnection;
1673
- const audioSender = pc.getSenders().find((sender) => sender.track?.kind === "audio");
1674
- if (audioSender && audioSender.track) {
1675
- audioSender.track.enabled = !mute;
1676
- this.isMute = mute;
1677
- this.callKit.trigger(KitEvent.KIT_SET_MUTE, mute);
1678
- } else {
1679
- throw new Error("No audio track found");
1680
- }
1681
- } catch (error) {
1682
- this.callKit.logger.error("Failed to set mute state", {
1683
- caller: "Connect.setMute",
1684
- type: "SIP",
1685
- content: {
1686
- err: error.message,
1687
- errCode: ErrorCode.WEBRTC_MUTE_ERROR
1688
- }
1689
- });
1690
- }
1691
- }
1692
- async mute() {
1693
- this.callKit.logger.info("connect mute", {
1694
- caller: "Connect.mute",
1695
- type: "SIP",
1696
- content: {
1697
- mute: true
1698
- }
1699
- });
1700
- if (this.connectStatus !== CallStatus.calling || !this.currentSession) {
1701
- this.callKit.logger.warn("Current status is not in call", {
1702
- caller: "Connect.mute",
1703
- type: "SIP",
1704
- content: {
1705
- errCode: ErrorCode.WEBRTC_MUTE_STATUS_ERROR
1706
- }
1707
- });
1708
- return;
1709
- }
1710
- await this.setMute(true);
1711
- }
1712
- async unmute() {
1713
- this.callKit.logger.info("connect unmute", {
1714
- caller: "Connect.unmute",
1715
- type: "SIP",
1716
- content: {
1717
- mute: false
1718
- }
1719
- });
1720
- if (this.connectStatus !== CallStatus.calling || !this.currentSession) {
1721
- this.callKit.logger.warn("Current status is not in call", {
1722
- caller: "Connect.unmute",
1723
- type: "SIP",
1724
- content: {
1725
- errCode: ErrorCode.WEBRTC_MUTE_STATUS_ERROR
1726
- }
1727
- });
1728
- return;
1729
- }
1730
- 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);
1731
1742
  }
1732
1743
  async refer(referTo, extra) {
1733
1744
  this.callKit.logger.info("connect refer", {
@@ -2024,7 +2035,6 @@ var Socket = class {
2024
2035
  errCode: ErrorCode.SOCKET_CONNECT_ERROR
2025
2036
  }
2026
2037
  });
2027
- this.callKit.config.reset();
2028
2038
  this.callKit.reset();
2029
2039
  return;
2030
2040
  }
@@ -2131,7 +2141,7 @@ var Socket = class {
2131
2141
  * 重置 socket 连接和所有状态
2132
2142
  * @param isWaitConfirm Whether need to wait for close confirmation
2133
2143
  */
2134
- async reset(isWaitConfirm = true) {
2144
+ async reset() {
2135
2145
  if (this.pingTimer) {
2136
2146
  clearInterval(this.pingTimer);
2137
2147
  this.pingTimer = void 0;
@@ -2143,9 +2153,7 @@ var Socket = class {
2143
2153
  this.callKit.logger.info("Closing socket connection", {
2144
2154
  caller: "Socket.reset",
2145
2155
  type: "INCALL",
2146
- content: {
2147
- isWaitConfirm
2148
- }
2156
+ content: {}
2149
2157
  });
2150
2158
  this.ws.close();
2151
2159
  this.isConnected = false;
@@ -2317,8 +2325,8 @@ var CallKit = class {
2317
2325
  }
2318
2326
  }
2319
2327
  await this.hangup();
2320
- this.socket.reset(false);
2321
2328
  this.connect.reset();
2329
+ this.socket.reset();
2322
2330
  this.config.reset();
2323
2331
  this.trigger(KitEvent.KIT_LOGIN_CHANGE, false);
2324
2332
  }
@@ -2430,6 +2438,28 @@ var CallKit = class {
2430
2438
  });
2431
2439
  this.callCenter.callUnhold();
2432
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
+ }
2433
2463
  /**
2434
2464
  * set userstatus
2435
2465
  * @param status