@16x/webphone-sdk 3.1.16 → 3.1.18

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.
@@ -1,5 +1,5 @@
1
1
  import { i as e, t } from "./SharedWorkerProtocol-CffZrUXg.js";
2
- import { r as n, t as r } from "./Diagnostics-Cqmr7y9b.js";
2
+ import { r as n, t as r } from "./Diagnostics-qnipur6s.js";
3
3
  //#region src/events/TypedEventBus.ts
4
4
  var i = class {
5
5
  handlers = /* @__PURE__ */ new Map();
@@ -608,6 +608,27 @@ var v = class {
608
608
  }).then(() => void 0);
609
609
  return this.disposeOperation = n, n;
610
610
  }
611
+ async forceTerminateCalls(e = 5e3) {
612
+ if (this.disposed) return;
613
+ let t = [...this.calls.values()];
614
+ for (let n of t) {
615
+ if (this.disposed) return;
616
+ if (this.calls.get(n.id) === n) {
617
+ try {
618
+ await Promise.race([n.hangup().catch(() => void 0), new Promise((t) => setTimeout(t, Math.max(0, e)))]);
619
+ } catch {}
620
+ try {
621
+ n.dispose();
622
+ } catch {}
623
+ if (this.calls.get(n.id) === n) {
624
+ this.calls.delete(n.id), this.callSessions.delete(n.id), this.callOwners.delete(n.id), this.ownerLostCalls.delete(n.id), this.activeCallId === n.id && (this.activeCallId = null);
625
+ try {
626
+ await this.notifyCallTerminal(n.id);
627
+ } catch {}
628
+ }
629
+ }
630
+ }
631
+ }
611
632
  updateSession(e) {
612
633
  this.disposed || (this.incomingSessionSnapshot = e);
613
634
  }
@@ -652,7 +673,7 @@ var v = class {
652
673
  reason: b(n)
653
674
  }) : this.events.emit("call.failed", {
654
675
  callId: e.id,
655
- error: re(n, e.id)
676
+ error: ie(n, e.id)
656
677
  }));
657
678
  }
658
679
  async switchAutomaticActiveCall(e, t, n) {
@@ -892,7 +913,22 @@ function b(e) {
892
913
  let t = e.reason;
893
914
  return typeof t == "string" && t !== "" ? t : "ended";
894
915
  }
895
- function re(t, n) {
916
+ function re(e) {
917
+ if (typeof e != "object" || !e) return;
918
+ let { cause: t, cause_code: n, message: r } = e, i = typeof n == "number" ? n : void 0, a = typeof t == "string" ? t : void 0;
919
+ if (i === void 0 && typeof r == "object" && r) {
920
+ let e = r.data;
921
+ if (typeof e == "string") {
922
+ let t = /^SIP\/2\.0\s+(\d{3})/m.exec(e);
923
+ if (t) return { code: Number(t[1]) };
924
+ }
925
+ }
926
+ return i === void 0 && a === void 0 ? void 0 : {
927
+ code: i,
928
+ cause: a
929
+ };
930
+ }
931
+ function ie(t, n) {
896
932
  if (t instanceof e) return new e(t.code, t.category, t.retryable, {
897
933
  requestId: t.requestId,
898
934
  callId: n,
@@ -916,15 +952,25 @@ function re(t, n) {
916
952
  cause: r
917
953
  });
918
954
  }
919
- return new e("CALL_OPERATION_NOT_ALLOWED", "call", !1, {
955
+ let r = re(t);
956
+ return r === void 0 ? new e("CALL_OPERATION_NOT_ALLOWED", "call", !1, {
957
+ callId: n,
958
+ cause: t
959
+ }) : r.code === 486 || r.cause === "Busy" ? new e("CALL_BUSY", "call", !1, {
960
+ callId: n,
961
+ cause: t
962
+ }) : r.code === 408 || r.cause === "Request Timeout" ? new e("NETWORK_TIMEOUT", "call", !0, {
963
+ callId: n,
964
+ cause: t
965
+ }) : new e("CALL_REJECTED", "call", !1, {
920
966
  callId: n,
921
967
  cause: t
922
968
  });
923
969
  }
924
970
  //#endregion
925
971
  //#region src/crypto/legacySipTicket.ts
926
- var ie = "q7X4p6MvK1z8Lb3A", ae = "W9e2T4mN0aQ7Ru6C", oe = 16, se = "�";
927
- function ce(e) {
972
+ var ae = "q7X4p6MvK1z8Lb3A", oe = "W9e2T4mN0aQ7Ru6C", se = 16, ce = "�";
973
+ function le(e) {
928
974
  for (let t of e) {
929
975
  let e = t.codePointAt(0) ?? 0;
930
976
  if (e === 127 || e < 32 && e !== 9) return !0;
@@ -938,32 +984,32 @@ async function x(t, n = {}) {
938
984
  if (i === void 0) throw new e("CONFIG_INVALID", "configuration", !1, { cause: "需要 WebCrypto 才能解 SIP 密码:请在安全上下文(https / localhost)或 Node 18+ 里运行" });
939
985
  let a = S(r);
940
986
  if (a === void 0) throw new e("LEGACY_PLATFORM_REJECTED", "auth", !1, { cause: "密文不是合法的 Base64:确认取的是 password/registerPassword 原值,没有被截断或转义" });
941
- let o = await i.importKey("raw", C(n.key ?? ie), "AES-CBC", !1, ["decrypt"]);
987
+ let o = await i.importKey("raw", C(n.key ?? ae), "AES-CBC", !1, ["decrypt"]);
942
988
  try {
943
989
  let e = await i.decrypt({
944
990
  name: "AES-CBC",
945
- iv: C(n.iv ?? ae)
991
+ iv: C(n.iv ?? oe)
946
992
  }, o, a);
947
993
  return new TextDecoder("utf-8").decode(e).trim();
948
994
  } catch (t) {
949
995
  throw new e("LEGACY_PLATFORM_REJECTED", "auth", !1, { cause: `SIP 密码解密失败(检查 key/iv 与该环境是否一致):${t instanceof Error ? t.message : String(t)}` });
950
996
  }
951
997
  }
952
- async function le(e, t = {}) {
998
+ async function ue(e, t = {}) {
953
999
  let n = String(e ?? "").replace(/\s+/g, "");
954
- if (ue(n)) try {
1000
+ if (de(n)) try {
955
1001
  let e = await x(n, t);
956
- return de(e) ? e : void 0;
1002
+ return fe(e) ? e : void 0;
957
1003
  } catch {
958
1004
  return;
959
1005
  }
960
1006
  }
961
- function ue(e) {
1007
+ function de(e) {
962
1008
  let t = S(e);
963
- return t !== void 0 && t.byteLength > 0 && t.byteLength % oe === 0;
1009
+ return t !== void 0 && t.byteLength > 0 && t.byteLength % se === 0;
964
1010
  }
965
- function de(e) {
966
- return e.length === 0 || e.includes(se) ? !1 : !ce(e);
1011
+ function fe(e) {
1012
+ return e.length === 0 || e.includes(ce) ? !1 : !le(e);
967
1013
  }
968
1014
  function S(e) {
969
1015
  if (e.length !== 0 && e.length % 4 == 0 && /^[A-Za-z0-9+/]+={0,2}$/.test(e)) try {
@@ -980,7 +1026,7 @@ function C(e) {
980
1026
  }
981
1027
  //#endregion
982
1028
  //#region src/shared/SharedWorkerClient.ts
983
- var fe = class {
1029
+ var pe = class {
984
1030
  port;
985
1031
  timeoutMs;
986
1032
  pending = /* @__PURE__ */ new Map();
@@ -1017,13 +1063,13 @@ var fe = class {
1017
1063
  try {
1018
1064
  let n = t(e);
1019
1065
  if (n.type === "event") {
1020
- n.event === "connection.stateChanged" && pe(n.payload) && (this.lifecycleState = n.payload.state), this.eventHandlers.forEach((e) => e(n));
1066
+ n.event === "connection.stateChanged" && me(n.payload) && (this.lifecycleState = n.payload.state), this.eventHandlers.forEach((e) => e(n));
1021
1067
  return;
1022
1068
  }
1023
1069
  if (n.type !== "response") return;
1024
1070
  let r = this.pending.get(n.requestId);
1025
1071
  if (r === void 0) return;
1026
- this.pending.delete(n.requestId), clearTimeout(r.timer), n.ok ? r.resolve(n.result) : r.reject(me(n));
1072
+ this.pending.delete(n.requestId), clearTimeout(r.timer), n.ok ? r.resolve(n.result) : r.reject(w(n));
1027
1073
  } catch (e) {
1028
1074
  this.pending.forEach((t) => {
1029
1075
  clearTimeout(t.timer), t.reject(e);
@@ -1031,10 +1077,10 @@ var fe = class {
1031
1077
  }
1032
1078
  }
1033
1079
  };
1034
- function pe(e) {
1080
+ function me(e) {
1035
1081
  return typeof e == "object" && !!e && "state" in e && (e.state === "connected" || e.state === "reconnecting");
1036
1082
  }
1037
- function me(t) {
1083
+ function w(t) {
1038
1084
  let n = t.error;
1039
1085
  return new e(n?.code ?? "SDK_INTERNAL_ERROR", n?.category ?? "internal", n?.retryable ?? !1, {
1040
1086
  requestId: t.requestId,
@@ -1046,12 +1092,12 @@ function me(t) {
1046
1092
  }
1047
1093
  //#endregion
1048
1094
  //#region src/shared/SharedWorkerFactory.ts
1049
- function w(t = {}) {
1095
+ function T(t = {}) {
1050
1096
  if ((t.enabled ?? "auto") === !1) return {
1051
1097
  mode: "single-tab",
1052
1098
  reason: "disabled"
1053
1099
  };
1054
- let n = t.locationHref ?? E(), r = T(t.url, n), i = (n) => {
1100
+ let n = t.locationHref ?? D(), r = E(t.url, n), i = (n) => {
1055
1101
  if (t.fallback === "error") throw new e("CONFIG_INVALID", "configuration", !1, { cause: n });
1056
1102
  return {
1057
1103
  mode: "single-tab",
@@ -1059,8 +1105,8 @@ function w(t = {}) {
1059
1105
  };
1060
1106
  };
1061
1107
  if (r === void 0) return i("SharedWorker URL is unavailable");
1062
- if (!O(r, n)) return i("SharedWorker URL must be same-origin");
1063
- let a = t.workerCtor ?? D();
1108
+ if (!k(r, n)) return i("SharedWorker URL must be same-origin");
1109
+ let a = t.workerCtor ?? O();
1064
1110
  if (a === void 0) return i("SharedWorker is not supported");
1065
1111
  let o;
1066
1112
  try {
@@ -1068,10 +1114,10 @@ function w(t = {}) {
1068
1114
  } catch (e) {
1069
1115
  return i(`SharedWorker construction failed: ${e instanceof Error ? e.message : String(e)}`);
1070
1116
  }
1071
- let s = t.tabId ?? k(), c = new fe(o.port), l = {
1117
+ let s = t.tabId ?? A(), c = new pe(o.port), l = {
1072
1118
  version: 1,
1073
1119
  type: "attach",
1074
- requestId: A(),
1120
+ requestId: j(),
1075
1121
  tabId: s
1076
1122
  }, u = c.request(l).then(() => void 0).catch((e) => {
1077
1123
  throw c.close(), e;
@@ -1085,20 +1131,20 @@ function w(t = {}) {
1085
1131
  ready: u
1086
1132
  };
1087
1133
  }
1088
- function T(e, t = E()) {
1134
+ function E(e, t = D()) {
1089
1135
  try {
1090
1136
  return t === void 0 ? void 0 : new URL(e ?? "./shared-worker.js", t).toString();
1091
1137
  } catch {
1092
1138
  return;
1093
1139
  }
1094
1140
  }
1095
- function E() {
1141
+ function D() {
1096
1142
  return typeof location > "u" ? void 0 : location.href;
1097
1143
  }
1098
- function D() {
1144
+ function O() {
1099
1145
  if (typeof SharedWorker == "function") return SharedWorker;
1100
1146
  }
1101
- function O(e, t) {
1147
+ function k(e, t) {
1102
1148
  if (t === void 0) return !0;
1103
1149
  try {
1104
1150
  return new URL(e, t).origin === new URL(t).origin;
@@ -1106,15 +1152,15 @@ function O(e, t) {
1106
1152
  return !1;
1107
1153
  }
1108
1154
  }
1109
- function k() {
1155
+ function A() {
1110
1156
  return `tab-${typeof crypto < "u" && typeof crypto.randomUUID == "function" ? crypto.randomUUID() : Math.random().toString(36).slice(2)}`;
1111
1157
  }
1112
- function A() {
1158
+ function j() {
1113
1159
  return `attach-${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
1114
1160
  }
1115
1161
  //#endregion
1116
1162
  //#region src/media/DeviceManager.ts
1117
- var j = class {
1163
+ var M = class {
1118
1164
  mediaDevices;
1119
1165
  createAudioElement;
1120
1166
  outputCapability;
@@ -1122,23 +1168,23 @@ var j = class {
1122
1168
  outputGeneration = 0;
1123
1169
  outputTail;
1124
1170
  constructor(e = {}) {
1125
- this.mediaDevices = e.mediaDevices === null ? void 0 : e.mediaDevices ?? M(), this.createAudioElement = e.createAudioElement;
1171
+ this.mediaDevices = e.mediaDevices === null ? void 0 : e.mediaDevices ?? N(), this.createAudioElement = e.createAudioElement;
1126
1172
  }
1127
1173
  async requestPermission(e = {}) {
1128
1174
  let t = this.requireMediaDevices(), n;
1129
1175
  try {
1130
1176
  n = await t.getUserMedia({ audio: e });
1131
1177
  } catch (e) {
1132
- throw F(e);
1178
+ throw I(e);
1133
1179
  }
1134
- N(n);
1180
+ P(n);
1135
1181
  }
1136
1182
  async getDevices() {
1137
1183
  if (this.mediaDevices === void 0) return [];
1138
1184
  try {
1139
1185
  return (await this.mediaDevices.enumerateDevices()).filter((e) => e.kind === "audioinput" || e.kind === "audiooutput" && this.supportsOutputSelection());
1140
1186
  } catch (e) {
1141
- throw I(e);
1187
+ throw L(e);
1142
1188
  }
1143
1189
  }
1144
1190
  getCapabilities() {
@@ -1176,7 +1222,7 @@ var j = class {
1176
1222
  try {
1177
1223
  await r.setSinkId(t);
1178
1224
  } catch (e) {
1179
- throw L(e);
1225
+ throw R(e);
1180
1226
  }
1181
1227
  }
1182
1228
  enqueueOutput(e) {
@@ -1190,7 +1236,7 @@ var j = class {
1190
1236
  try {
1191
1237
  return await t.getUserMedia({ audio: { deviceId: { exact: e } } });
1192
1238
  } catch (e) {
1193
- throw P(e);
1239
+ throw F(e);
1194
1240
  }
1195
1241
  }
1196
1242
  requireMediaDevices() {
@@ -1210,10 +1256,10 @@ var j = class {
1210
1256
  }
1211
1257
  }
1212
1258
  };
1213
- function M() {
1259
+ function N() {
1214
1260
  return globalThis.navigator?.mediaDevices;
1215
1261
  }
1216
- function N(e) {
1262
+ function P(e) {
1217
1263
  let t;
1218
1264
  try {
1219
1265
  t = e.getTracks();
@@ -1226,43 +1272,43 @@ function N(e) {
1226
1272
  } catch {}
1227
1273
  });
1228
1274
  }
1229
- function P(t) {
1230
- let n = R(t);
1231
- return n === "NotFoundError" || n === "OverconstrainedError" ? new e("MEDIA_DEVICE_NOT_FOUND", "media", !1, { cause: t }) : n === "NotReadableError" || n === "AbortError" ? new e("MEDIA_DEVICE_IN_USE", "media", !0, { cause: t }) : n === "NotAllowedError" || n === "SecurityError" ? new e("MEDIA_PERMISSION_DENIED", "media", !1, { cause: t }) : new e("SDK_INTERNAL_ERROR", "internal", !1, { cause: t });
1232
- }
1233
1275
  function F(t) {
1234
- let n = R(t);
1276
+ let n = z(t);
1235
1277
  return n === "NotFoundError" || n === "OverconstrainedError" ? new e("MEDIA_DEVICE_NOT_FOUND", "media", !1, { cause: t }) : n === "NotReadableError" || n === "AbortError" ? new e("MEDIA_DEVICE_IN_USE", "media", !0, { cause: t }) : n === "NotAllowedError" || n === "SecurityError" ? new e("MEDIA_PERMISSION_DENIED", "media", !1, { cause: t }) : new e("SDK_INTERNAL_ERROR", "internal", !1, { cause: t });
1236
1278
  }
1237
1279
  function I(t) {
1238
- let n = R(t);
1239
- return n === "NotFoundError" ? new e("MEDIA_DEVICE_NOT_FOUND", "media", !1, { cause: t }) : n === "NotAllowedError" || n === "SecurityError" ? new e("MEDIA_PERMISSION_DENIED", "media", !1, { cause: t }) : n === "AbortError" || n === "NotReadableError" ? new e("MEDIA_DEVICE_IN_USE", "media", !0, { cause: t }) : new e("SDK_INTERNAL_ERROR", "internal", !1, { cause: t });
1280
+ let n = z(t);
1281
+ return n === "NotFoundError" || n === "OverconstrainedError" ? new e("MEDIA_DEVICE_NOT_FOUND", "media", !1, { cause: t }) : n === "NotReadableError" || n === "AbortError" ? new e("MEDIA_DEVICE_IN_USE", "media", !0, { cause: t }) : n === "NotAllowedError" || n === "SecurityError" ? new e("MEDIA_PERMISSION_DENIED", "media", !1, { cause: t }) : new e("SDK_INTERNAL_ERROR", "internal", !1, { cause: t });
1240
1282
  }
1241
1283
  function L(t) {
1242
- let n = R(t);
1284
+ let n = z(t);
1285
+ return n === "NotFoundError" ? new e("MEDIA_DEVICE_NOT_FOUND", "media", !1, { cause: t }) : n === "NotAllowedError" || n === "SecurityError" ? new e("MEDIA_PERMISSION_DENIED", "media", !1, { cause: t }) : n === "AbortError" || n === "NotReadableError" ? new e("MEDIA_DEVICE_IN_USE", "media", !0, { cause: t }) : new e("SDK_INTERNAL_ERROR", "internal", !1, { cause: t });
1286
+ }
1287
+ function R(t) {
1288
+ let n = z(t);
1243
1289
  return n === "NotFoundError" ? new e("MEDIA_DEVICE_NOT_FOUND", "media", !1, { cause: t }) : n === "NotAllowedError" || n === "SecurityError" ? new e("MEDIA_PERMISSION_DENIED", "media", !1, { cause: t }) : n === "AbortError" || n === "NotReadableError" ? new e("MEDIA_DEVICE_IN_USE", "media", !0, { cause: t }) : new e("SDK_INTERNAL_ERROR", "internal", !1, { cause: t });
1244
1290
  }
1245
- function R(e) {
1291
+ function z(e) {
1246
1292
  if (typeof e != "object" || !e || !("name" in e)) return;
1247
1293
  let t = e.name;
1248
1294
  return typeof t == "string" ? t : void 0;
1249
1295
  }
1250
1296
  //#endregion
1251
1297
  //#region src/media/RemoteAudio.ts
1252
- var z = class {
1298
+ var he = class {
1253
1299
  createAudioElement;
1254
1300
  bindings = /* @__PURE__ */ new Map();
1255
1301
  audioOwners = /* @__PURE__ */ new Map();
1256
1302
  trackOwners = /* @__PURE__ */ new Map();
1257
1303
  generations = /* @__PURE__ */ new Map();
1258
1304
  pendingPlays = /* @__PURE__ */ new Map();
1259
- constructor(e = he) {
1305
+ constructor(e = ge) {
1260
1306
  this.createAudioElement = e;
1261
1307
  }
1262
1308
  async bind(t, n) {
1263
1309
  let r = this.bindings.get(t);
1264
1310
  if (r?.stream === n) return;
1265
- let i = ge(t, n), a;
1311
+ let i = _e(t, n), a;
1266
1312
  try {
1267
1313
  a = this.createAudioElement();
1268
1314
  } catch (n) {
@@ -1383,18 +1429,18 @@ var z = class {
1383
1429
  return this.generations.get(e) === t && this.bindings.get(e)?.generation === t;
1384
1430
  }
1385
1431
  };
1386
- function he() {
1432
+ function ge() {
1387
1433
  if (typeof document > "u") throw new e("CAPABILITY_NOT_SUPPORTED", "capability", !1);
1388
1434
  return document.createElement("audio");
1389
1435
  }
1390
- function ge(t, n) {
1436
+ function _e(t, n) {
1391
1437
  try {
1392
1438
  return [...new Set(n.getAudioTracks())].filter((e) => e.kind === "audio");
1393
1439
  } catch (r) {
1394
1440
  try {
1395
1441
  return [...new Set(n.getTracks())].filter((e) => e.kind === "audio");
1396
1442
  } catch (i) {
1397
- throw _e(n), new e("SDK_INTERNAL_ERROR", "internal", !1, {
1443
+ throw ve(n), new e("SDK_INTERNAL_ERROR", "internal", !1, {
1398
1444
  callId: t,
1399
1445
  cause: r ?? i
1400
1446
  });
@@ -1406,7 +1452,7 @@ function B(e, t) {
1406
1452
  (t.get(e) ?? 0) === 0 && V(e);
1407
1453
  });
1408
1454
  }
1409
- function _e(e) {
1455
+ function ve(e) {
1410
1456
  try {
1411
1457
  [...new Set(e.getTracks())].filter((e) => e.kind === "audio").forEach(V);
1412
1458
  } catch {}
@@ -1418,7 +1464,7 @@ function V(e) {
1418
1464
  }
1419
1465
  //#endregion
1420
1466
  //#region src/media/MediaManager.ts
1421
- var ve = class {
1467
+ var ye = class {
1422
1468
  deviceManager;
1423
1469
  remoteAudio;
1424
1470
  getActiveCall;
@@ -1439,7 +1485,7 @@ var ve = class {
1439
1485
  mediaDevices: e.mediaDevices,
1440
1486
  createAudioElement: e.createAudioElement
1441
1487
  };
1442
- this.deviceManager = new j(t), this.remoteAudio = new z(e.createAudioElement), this.getActiveCall = e.getActiveCall, this.createRemoteStream = e.createRemoteStream ?? xe;
1488
+ this.deviceManager = new M(t), this.remoteAudio = new he(e.createAudioElement), this.getActiveCall = e.getActiveCall, this.createRemoteStream = e.createRemoteStream ?? Se;
1443
1489
  }
1444
1490
  requestPermission(e) {
1445
1491
  return this.assertNotDisposed(), this.deviceManager.requestPermission(e);
@@ -1459,7 +1505,7 @@ var ve = class {
1459
1505
  n !== void 0 && this.releaseResource(n).catch((e) => {
1460
1506
  this.releaseErrors.set(n, e);
1461
1507
  });
1462
- let r = ye(t) ? t : { peerConnection: t }, i = {
1508
+ let r = be(t) ? t : { peerConnection: t }, i = {
1463
1509
  callId: e,
1464
1510
  peerConnection: r.peerConnection,
1465
1511
  senders: /* @__PURE__ */ new Map(),
@@ -1646,7 +1692,7 @@ var ve = class {
1646
1692
  try {
1647
1693
  c = s.getTracks();
1648
1694
  } catch (t) {
1649
- throw be(s), new e("SDK_INTERNAL_ERROR", "internal", !1, {
1695
+ throw xe(s), new e("SDK_INTERNAL_ERROR", "internal", !1, {
1650
1696
  callId: n.callId,
1651
1697
  cause: t
1652
1698
  });
@@ -1796,7 +1842,7 @@ var ve = class {
1796
1842
  if (this.disposed || this.disposing) throw new e("SDK_ALREADY_DISPOSED", "internal", !1);
1797
1843
  }
1798
1844
  };
1799
- function ye(e) {
1845
+ function be(e) {
1800
1846
  return "peerConnection" in e;
1801
1847
  }
1802
1848
  function H(e) {
@@ -1818,7 +1864,7 @@ function W(e) {
1818
1864
  return U(e) ? Promise.resolve() : Promise.reject(e);
1819
1865
  }
1820
1866
  }
1821
- function be(e) {
1867
+ function xe(e) {
1822
1868
  try {
1823
1869
  G(e.getTracks());
1824
1870
  } catch {}
@@ -1831,7 +1877,7 @@ function K(e) {
1831
1877
  e.stop();
1832
1878
  } catch {}
1833
1879
  }
1834
- function xe(e) {
1880
+ function Se(e) {
1835
1881
  if (typeof MediaStream < "u") try {
1836
1882
  return new MediaStream([e]);
1837
1883
  } catch {
@@ -1840,8 +1886,8 @@ function xe(e) {
1840
1886
  }
1841
1887
  //#endregion
1842
1888
  //#region src/core/mediaBridge.ts
1843
- function Se(t, n = {}) {
1844
- let r = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map(), a = /* @__PURE__ */ new Set(), o = n.ownershipRequired === !0, s = !1, c = !1, l, u = () => t(), d = new ve({ getActiveCall: u });
1889
+ function Ce(t, n = {}) {
1890
+ let r = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map(), a = /* @__PURE__ */ new Set(), o = n.ownershipRequired === !0, s = !1, c = !1, l, u = () => t(), d = new ye({ getActiveCall: u });
1845
1891
  return {
1846
1892
  publicMedia: {
1847
1893
  requestPermission: () => (f(), d.requestPermission()),
@@ -1899,7 +1945,7 @@ function Se(t, n = {}) {
1899
1945
  function p(e, t) {
1900
1946
  if (!q(t) || typeof t.getMediaRegistration != "function") return;
1901
1947
  let n = t.getMediaRegistration();
1902
- if (Ce(n) && r.get(e)?.peerConnection !== n.peerConnection) {
1948
+ if (we(n) && r.get(e)?.peerConnection !== n.peerConnection) {
1903
1949
  r.set(e, n);
1904
1950
  try {
1905
1951
  d.registerCallMedia(e, n);
@@ -1912,20 +1958,21 @@ function Se(t, n = {}) {
1912
1958
  function q(e) {
1913
1959
  return typeof e == "object" && !!e;
1914
1960
  }
1915
- function Ce(e) {
1961
+ function we(e) {
1916
1962
  return typeof e == "object" && !!e && "peerConnection" in e && typeof e.peerConnection == "object" && e.peerConnection !== null;
1917
1963
  }
1918
1964
  //#endregion
1919
1965
  //#region src/reconnect/backoff.ts
1920
- var we = 1e3, Te = 3e4;
1921
- function Ee(e, t = Math.random) {
1922
- let n = Math.min(Te, we * 2 ** Math.max(0, Math.floor(e))), r = Math.min(1, Math.max(0, t()));
1966
+ var Te = 1e3, Ee = 3e4;
1967
+ function De(e, t = Math.random) {
1968
+ let n = Math.min(Ee, Te * 2 ** Math.max(0, Math.floor(e))), r = Math.min(1, Math.max(0, t()));
1923
1969
  return Math.round(n * (1 + r * .2));
1924
1970
  }
1925
1971
  //#endregion
1926
1972
  //#region src/reconnect/ReconnectManager.ts
1927
- var De = class {
1973
+ var Oe = class {
1928
1974
  recover;
1975
+ isRecovered;
1929
1976
  random;
1930
1977
  isOnline;
1931
1978
  onAttempt;
@@ -1937,11 +1984,11 @@ var De = class {
1937
1984
  disposed = !1;
1938
1985
  restartAfterRun = !1;
1939
1986
  constructor(e) {
1940
- this.recover = e.recover, this.random = e.random ?? Math.random, this.isOnline = e.isOnline ?? (() => !0), this.onAttempt = e.onAttempt;
1987
+ this.recover = e.recover, this.isRecovered = e.isRecovered, this.random = e.random ?? Math.random, this.isOnline = e.isOnline ?? (() => !0), this.onAttempt = e.onAttempt;
1941
1988
  }
1942
1989
  start() {
1943
1990
  if (this.disposed || !this.online || !this.isOnline() || this.pendingTimer !== void 0 || this.running) return;
1944
- let e = this.generation, t = Ee(this.attempt, this.random);
1991
+ let e = this.generation, t = De(this.attempt, this.random);
1945
1992
  this.pendingTimer = setTimeout(() => {
1946
1993
  this.pendingTimer = void 0, this.run(e);
1947
1994
  }, t);
@@ -1975,7 +2022,7 @@ var De = class {
1975
2022
  } finally {
1976
2023
  this.running = !1;
1977
2024
  }
1978
- this.restartAfterRun ? (this.restartAfterRun = !1, this.isCurrent(this.generation) && this.start()) : t && this.isCurrent(e) && this.start();
2025
+ !t && this.isCurrent(e) && this.isRecovered !== void 0 && !this.isRecovered() && (this.attempt += 1, t = !0), this.restartAfterRun ? (this.restartAfterRun = !1, this.isCurrent(this.generation) && this.start()) : t && this.isCurrent(e) && this.start();
1979
2026
  }
1980
2027
  isCurrent(e) {
1981
2028
  return !this.disposed && this.online && e === this.generation && this.isOnline();
@@ -1983,7 +2030,7 @@ var De = class {
1983
2030
  clearPendingTimer() {
1984
2031
  this.pendingTimer !== void 0 && (clearTimeout(this.pendingTimer), this.pendingTimer = void 0);
1985
2032
  }
1986
- }, Oe = class t {
2033
+ }, ke = 12e4, Ae = 5e3, je = class t {
1987
2034
  media;
1988
2035
  options;
1989
2036
  api;
@@ -2014,6 +2061,7 @@ var De = class {
2014
2061
  sipUnsubscribe;
2015
2062
  networkUnsubscribers = [];
2016
2063
  networkProbeTimer;
2064
+ stuckCallsSince;
2017
2065
  sessionDeleteOperations = /* @__PURE__ */ new Map();
2018
2066
  deletedSessions = /* @__PURE__ */ new Set();
2019
2067
  ownedSessionIds = /* @__PURE__ */ new Set();
@@ -2021,8 +2069,9 @@ var De = class {
2021
2069
  sharedWorkerSelection;
2022
2070
  constructor(t) {
2023
2071
  if (this.options = t, t.api === void 0 && t.sessionProvider === void 0) throw new e("CONFIG_INVALID", "configuration", !1, { cause: "CCBarClient 需要 sessionProvider" });
2024
- this.api = t.api === void 0 ? J(X(t.sessionProvider)) : J(t.api), this.suppliedSip = t.sip === void 0 ? void 0 : je(t.sip), this.mediaBridge = Se(() => this.getActiveMediaContext()), this.media = this.mediaBridge.publicMedia, this.reconnect = new De({
2072
+ this.api = t.api === void 0 ? Y(Ne(t.sessionProvider)) : Y(t.api), this.suppliedSip = t.sip === void 0 ? void 0 : Pe(t.sip), this.mediaBridge = Ce(() => this.getActiveMediaContext()), this.media = this.mediaBridge.publicMedia, this.reconnect = new Oe({
2025
2073
  recover: () => this.recoverConnection(),
2074
+ isRecovered: () => this.state === "connected",
2026
2075
  onAttempt: (e) => {
2027
2076
  this.disposed || this.events.emit("connection.reconnecting", { attempt: e });
2028
2077
  }
@@ -2033,13 +2082,13 @@ var De = class {
2033
2082
  }), this.sharedWorkerSelection = t.sharedWorker === void 0 ? {
2034
2083
  mode: "single-tab",
2035
2084
  reason: "disabled"
2036
- } : w({
2085
+ } : T({
2037
2086
  ...t.sharedWorker,
2038
2087
  ...t.sharedWorkerHost ?? {}
2039
2088
  });
2040
2089
  }
2041
2090
  static async checkSupport() {
2042
- let e = typeof navigator < "u" && navigator.mediaDevices !== void 0, t = typeof WebSocket < "u", n = Ae(typeof navigator < "u" ? navigator.userAgent : "");
2091
+ let e = typeof navigator < "u" && navigator.mediaDevices !== void 0, t = typeof WebSocket < "u", n = J(typeof navigator < "u" ? navigator.userAgent : "");
2043
2092
  return {
2044
2093
  supported: e && t,
2045
2094
  platform: n,
@@ -2147,7 +2196,7 @@ var De = class {
2147
2196
  sdkVersion: n,
2148
2197
  platform: a === "mobile-web" ? "mobile_web" : "web",
2149
2198
  ...this.options.locale === void 0 ? {} : { locale: this.options.locale },
2150
- requestedCapabilities: [...this.options.requestedCapabilities ?? Fe(a)]
2199
+ requestedCapabilities: [...this.options.requestedCapabilities ?? Re(a)]
2151
2200
  }), this.ownedSessionIds.add(t.sessionId), this.assertGeneration(e), r = await this.getSip(), this.assertGeneration(e), this.session = t;
2152
2201
  let o = this.callManager ?? this.createCallManager(r, t, a);
2153
2202
  this.callManager === void 0 && (i = o), o.updateSession(t), this.callManager = o, this.attachSipListener(r), this.attachNetworkListeners(), await r.connect(t), this.assertGeneration(e), this.setConnectionState("connected"), this.events.emit("connection.registered", { registered: !0 }), this.scheduleRefresh(t);
@@ -2171,7 +2220,7 @@ var De = class {
2171
2220
  } catch (t) {
2172
2221
  $(t) || (e ??= t);
2173
2222
  }
2174
- this.reconnect.cancel(), this.clearRefreshTimer(), this.intentionalDisconnect = !0, this.detachNetworkListeners(), this.detachSipListener(), this.detachManagerListeners();
2223
+ this.reconnect.cancel(), this.stuckCallsSince = void 0, this.clearRefreshTimer(), this.intentionalDisconnect = !0, this.detachNetworkListeners(), this.detachSipListener(), this.detachManagerListeners();
2175
2224
  let r = this.callManager, i = this.sip;
2176
2225
  this.callManager = void 0;
2177
2226
  try {
@@ -2253,7 +2302,7 @@ var De = class {
2253
2302
  try {
2254
2303
  await this.media.releaseAll();
2255
2304
  } catch (t) {
2256
- Me(t) || (e ??= t);
2305
+ Fe(t) || (e ??= t);
2257
2306
  }
2258
2307
  try {
2259
2308
  await this.mediaBridge.dispose();
@@ -2275,16 +2324,31 @@ var De = class {
2275
2324
  async awaitMediaReleaseBarriers() {
2276
2325
  let e;
2277
2326
  for (let [t, n] of [...this.terminalReleaseOperations]) try {
2278
- await n, this.terminalReleaseOperations.delete(t);
2327
+ await this.withBarrierTimeout(n, t), this.terminalReleaseOperations.delete(t);
2279
2328
  } catch (n) {
2280
2329
  try {
2281
- await this.mediaBridge.releaseCall(t), this.terminalReleaseOperations.delete(t);
2330
+ await this.withBarrierTimeout(this.mediaBridge.releaseCall(t), t), this.terminalReleaseOperations.delete(t);
2282
2331
  } catch (t) {
2283
2332
  e ??= t ?? n;
2284
2333
  }
2285
2334
  }
2286
2335
  if (e !== void 0) throw e;
2287
2336
  }
2337
+ withBarrierTimeout(t, n, r = Ae) {
2338
+ return new Promise((i, a) => {
2339
+ let o = setTimeout(() => {
2340
+ a(new e("SDK_INTERNAL_ERROR", "internal", !0, {
2341
+ callId: n,
2342
+ cause: "media release barrier timeout"
2343
+ }));
2344
+ }, Math.max(0, r));
2345
+ t.then((e) => {
2346
+ clearTimeout(o), i(e);
2347
+ }, (e) => {
2348
+ clearTimeout(o), a(e);
2349
+ });
2350
+ });
2351
+ }
2288
2352
  detachSipListener() {
2289
2353
  let e = this.sipUnsubscribe;
2290
2354
  this.sipUnsubscribe = void 0;
@@ -2394,7 +2458,7 @@ var De = class {
2394
2458
  if (!this.disposed) try {
2395
2459
  await this.performConnect(this.lifecycleGeneration);
2396
2460
  } catch {
2397
- this.reconnect.cancel();
2461
+ this.disposed || this.reconnect.start();
2398
2462
  }
2399
2463
  }
2400
2464
  }
@@ -2406,7 +2470,18 @@ var De = class {
2406
2470
  }, n);
2407
2471
  }
2408
2472
  async recoverConnection() {
2409
- this.disposed || this.intentionalDisconnect || this.hasNonTerminalCalls() || (await this.awaitMediaReleaseBarriers(), await this.refreshAndRebuild());
2473
+ if (!(this.disposed || this.intentionalDisconnect)) {
2474
+ if (this.hasNonTerminalCalls()) {
2475
+ let e = Date.now();
2476
+ if (this.stuckCallsSince === void 0 && (this.stuckCallsSince = e), e - this.stuckCallsSince < ke) return;
2477
+ this.stuckCallsSince = void 0, await this.callManager?.forceTerminateCalls();
2478
+ } else this.stuckCallsSince = void 0;
2479
+ if (await this.awaitMediaReleaseBarriers(), this.session === void 0) {
2480
+ await this.performConnect(this.lifecycleGeneration);
2481
+ return;
2482
+ }
2483
+ await this.refreshAndRebuild();
2484
+ }
2410
2485
  }
2411
2486
  async getSip() {
2412
2487
  if (this.sip !== void 0) return this.sip;
@@ -2466,7 +2541,7 @@ var De = class {
2466
2541
  destination: e.destination
2467
2542
  })),
2468
2543
  permission: this.support?.hasMediaDevices === !1 ? "unsupported" : "unknown",
2469
- ice: ke(e, this.sip?.iceQualitySnapshot?.()),
2544
+ ice: Me(e, this.sip?.iceQualitySnapshot?.()),
2470
2545
  wss: {
2471
2546
  configured: e !== void 0,
2472
2547
  secure: e?.transport.wssUrl.toLowerCase().startsWith("wss://") ?? !1,
@@ -2583,7 +2658,7 @@ var De = class {
2583
2658
  return new e("SDK_ALREADY_DISPOSED", "internal", !1);
2584
2659
  }
2585
2660
  };
2586
- function ke(e, t) {
2661
+ function Me(e, t) {
2587
2662
  if (e === void 0) return {
2588
2663
  configured: !1,
2589
2664
  serverCount: 0,
@@ -2603,17 +2678,17 @@ function ke(e, t) {
2603
2678
  turnServerCount: r
2604
2679
  };
2605
2680
  }
2606
- function Ae(e) {
2681
+ function J(e) {
2607
2682
  return e === "" ? "unknown" : /iPhone|iPad|Android/i.test(e) ? "mobile-web" : "web";
2608
2683
  }
2609
- function J(t) {
2684
+ function Y(t) {
2610
2685
  if (typeof t != "object" || !t) throw new e("CONFIG_INVALID", "configuration", !1);
2611
2686
  return t;
2612
2687
  }
2613
- async function Y(e) {
2688
+ async function X(e) {
2614
2689
  let t = e.sip?.registerTicket;
2615
2690
  if (typeof t != "string" || t === "") return e;
2616
- let n = await le(t);
2691
+ let n = await ue(t);
2617
2692
  return n === void 0 || n === t ? e : {
2618
2693
  ...e,
2619
2694
  sip: {
@@ -2622,10 +2697,10 @@ async function Y(e) {
2622
2697
  }
2623
2698
  };
2624
2699
  }
2625
- function X(t) {
2700
+ function Ne(t) {
2626
2701
  let r = {
2627
- createSession: async (e) => Y(await t.createSession(e)),
2628
- refreshSession: async (e) => Y(t.refreshSession === void 0 ? await t.createSession({
2702
+ createSession: async (e) => X(await t.createSession(e)),
2703
+ refreshSession: async (e) => X(t.refreshSession === void 0 ? await t.createSession({
2629
2704
  sdkVersion: n,
2630
2705
  platform: "web"
2631
2706
  }) : await t.refreshSession(e)),
@@ -2636,7 +2711,7 @@ function X(t) {
2636
2711
  }, i = t.invalidateToken;
2637
2712
  return i !== void 0 && (r.invalidateToken = () => i()), r;
2638
2713
  }
2639
- function je(t) {
2714
+ function Pe(t) {
2640
2715
  if (typeof t != "object" || !t) throw new e("CONFIG_INVALID", "configuration", !1);
2641
2716
  return t;
2642
2717
  }
@@ -2656,13 +2731,13 @@ function Z(t, n, r, i, a) {
2656
2731
  function Q(t) {
2657
2732
  return t instanceof e ? t.status !== void 0 && t.status >= 400 && t.status < 500 || t.serverCode === "SESSION_NOT_FOUND" || t.serverCode === "SESSION_EXPIRED" || t.serverCode === "TOKEN_INVALID" || t.serverCode === "TOKEN_EXPIRED" || t.code === "AUTH_TOKEN_UNAVAILABLE" || t.code === "AUTH_TOKEN_EXPIRED" || t.code === "AUTH_TOKEN_REJECTED" : !1;
2658
2733
  }
2659
- function Me(t) {
2734
+ function Fe(t) {
2660
2735
  return t instanceof e && t.code === "SDK_ALREADY_DISPOSED";
2661
2736
  }
2662
2737
  function $(t) {
2663
2738
  return t instanceof e && (t.code === "SDK_ALREADY_DISPOSED" || t.code === "CALL_NOT_CONNECTED");
2664
2739
  }
2665
- var Ne = [
2740
+ var Ie = [
2666
2741
  "outbound",
2667
2742
  "inbound",
2668
2743
  "mute",
@@ -2670,13 +2745,13 @@ var Ne = [
2670
2745
  "dtmf",
2671
2746
  "blind_transfer",
2672
2747
  "device_selection"
2673
- ], Pe = [
2748
+ ], Le = [
2674
2749
  "outbound",
2675
2750
  "mute",
2676
2751
  "dtmf"
2677
2752
  ];
2678
- function Fe(e) {
2679
- return e === "mobile-web" ? Pe : Ne;
2753
+ function Re(e) {
2754
+ return e === "mobile-web" ? Le : Ie;
2680
2755
  }
2681
2756
  //#endregion
2682
- export { v as a, s as c, a as d, i as f, x as i, o as l, w as n, f as o, T as r, c as s, Oe as t, l as u };
2757
+ export { v as a, s as c, a as d, i as f, x as i, o as l, T as n, f as o, E as r, c as s, je as t, l as u };