@sentinel-nvr/web 0.9.0 → 0.11.0

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,3 +1,7 @@
1
+ import {
2
+ timeoutSignal
3
+ } from "./chunk-LN4VXI2V.js";
4
+
1
5
  // src/player/rlog.ts
2
6
  var brand = "web";
3
7
  var client = null;
@@ -5,8 +9,25 @@ function setRlogClient(c, b) {
5
9
  client = c;
6
10
  if (b) brand = b;
7
11
  }
12
+ var PER_MIN = 20;
13
+ var SAME_TAG_MS = 5e3;
14
+ var sent = [];
15
+ var lastByTag = /* @__PURE__ */ new Map();
16
+ var dropped = 0;
8
17
  function rlog(tag, data) {
9
18
  if (!client) return;
19
+ const now = Date.now();
20
+ sent = sent.filter((t) => now - t < 6e4);
21
+ if (sent.length >= PER_MIN || now - (lastByTag.get(tag) ?? -Infinity) < SAME_TAG_MS) {
22
+ dropped++;
23
+ return;
24
+ }
25
+ sent.push(now);
26
+ lastByTag.set(tag, now);
27
+ if (dropped) {
28
+ data = { ...data ?? {}, dropped };
29
+ dropped = 0;
30
+ }
10
31
  let line;
11
32
  try {
12
33
  line = JSON.stringify({ tag, b: brand, d: data ?? null, ua: navigator.userAgent.slice(0, 80) });
@@ -17,7 +38,9 @@ function rlog(tag, data) {
17
38
  }
18
39
 
19
40
  // src/player/webrtc.ts
20
- var forceRelayGlobal = false;
41
+ var forceRelayUntil = 0;
42
+ var FORCE_RELAY_MS = 10 * 6e4;
43
+ var relayForced = () => Date.now() < forceRelayUntil;
21
44
  function mobileClient() {
22
45
  return /iPhone|iPod|Android|Mobile/i.test(navigator.userAgent) || navigator.maxTouchPoints > 1 && Math.min(screen.width || 9999, screen.height || 9999) < 920;
23
46
  }
@@ -67,8 +90,8 @@ var WebRtcSession = class {
67
90
  this.connectT = window.setTimeout(() => {
68
91
  if (this.active && this.ws === ws && !this.connected) {
69
92
  rlog("connect-timeout", { kind: this.opts.mode, ice: this.pc?.iceConnectionState });
70
- if (!forceRelayGlobal) {
71
- forceRelayGlobal = true;
93
+ if (!relayForced()) {
94
+ forceRelayUntil = Date.now() + FORCE_RELAY_MS;
72
95
  rlog("retry-relay-only", { kind: this.opts.mode });
73
96
  this.stop();
74
97
  this.start();
@@ -145,7 +168,7 @@ var WebRtcSession = class {
145
168
  ensurePc(setup) {
146
169
  if (this.pc) return this.pc;
147
170
  let cfg = setup && setup.configuration || { iceServers: [{ urls: "stun:stun.l.google.com:19302" }] };
148
- if (forceRelayGlobal) cfg = { ...cfg, iceTransportPolicy: "relay" };
171
+ if (relayForced()) cfg = { ...cfg, iceTransportPolicy: "relay" };
149
172
  const pc = new RTCPeerConnection(cfg);
150
173
  this.pc = pc;
151
174
  if (setup?.datachannel) {
@@ -200,16 +223,16 @@ var WebRtcSession = class {
200
223
  if (r.type === "inbound-rtp" && (r.kind || r.mediaType) === "audio") aBytes = r.bytesReceived || 0;
201
224
  });
202
225
  if (dec <= 0) {
203
- rlog("dec-check-fail", { kind: this.opts.mode, dec, recv, vBytes, aBytes, relayForced: forceRelayGlobal });
204
- if (!forceRelayGlobal && vBytes <= 0 && aBytes <= 0) {
205
- forceRelayGlobal = true;
226
+ rlog("dec-check-fail", { kind: this.opts.mode, dec, recv, vBytes, aBytes, relayForced: relayForced() });
227
+ if (!relayForced() && vBytes <= 0 && aBytes <= 0) {
228
+ forceRelayUntil = Date.now() + FORCE_RELAY_MS;
206
229
  rlog("retry-relay-only", { kind: this.opts.mode });
207
230
  this.stop();
208
231
  this.start();
209
232
  return;
210
233
  }
211
234
  this.fail("no-decode");
212
- } else rlog("dec-ok", { kind: this.opts.mode, dec, relayForced: forceRelayGlobal });
235
+ } else rlog("dec-ok", { kind: this.opts.mode, dec, relayForced: relayForced() });
213
236
  }).catch(() => {
214
237
  });
215
238
  }, this.opts.mode === "live" ? 5e3 : 9e3);
@@ -359,6 +382,13 @@ var PlayerController = class {
359
382
  stallTimer = 0;
360
383
  destroyed = false;
361
384
  liveRestartT = 0;
385
+ // live watchdog (presented frames), MJPEG retry, periodic WebRTC retry out of a fallback, relay-pos in flight
386
+ liveWd = { last: -1, at: 0, since: 0 };
387
+ mjpegRetries = 0;
388
+ mjpegRetryT = 0;
389
+ liveUpgradeAt = 0;
390
+ liveUpgradeN = 0;
391
+ rwPosBusy = false;
362
392
  unlisten = [];
363
393
  arPrefix;
364
394
  constructor(api, opts) {
@@ -466,6 +496,7 @@ var PlayerController = class {
466
496
  this.unlisten.push(() => document.removeEventListener("visibilitychange", vis));
467
497
  const edge = window.setInterval(() => {
468
498
  if (this.live && this.L.active) this.liveEdge();
499
+ if (this.live) this.liveWatch();
469
500
  }, 1e3);
470
501
  this.unlisten.push(() => clearInterval(edge));
471
502
  }
@@ -477,17 +508,32 @@ var PlayerController = class {
477
508
  clearTimeout(this.liveRestartT);
478
509
  this.liveRestartT = 0;
479
510
  }
511
+ if (this.mjpegRetryT) {
512
+ clearTimeout(this.mjpegRetryT);
513
+ this.mjpegRetryT = 0;
514
+ }
480
515
  this.recWebrtcTeardown();
481
516
  this.webrtcTeardown();
482
517
  this.mseTeardown();
483
518
  this.liveTeardown();
484
519
  this.chaseAbort();
485
520
  if (this.pendingT) clearInterval(this.pendingT);
521
+ if (this.freezeT) {
522
+ clearTimeout(this.freezeT);
523
+ this.freezeT = 0;
524
+ }
486
525
  for (const u of this.unlisten) u();
487
526
  try {
488
527
  this.v.pause();
489
528
  this.v.srcObject = null;
490
529
  this.v.removeAttribute("src");
530
+ this.v.onloadedmetadata = null;
531
+ } catch {
532
+ }
533
+ try {
534
+ this.img.onload = null;
535
+ this.img.onerror = null;
536
+ this.img.removeAttribute("src");
491
537
  } catch {
492
538
  }
493
539
  }
@@ -495,6 +541,7 @@ var PlayerController = class {
495
541
  if (camId !== this.camId) {
496
542
  this.recWebrtcDisabled = false;
497
543
  this.recoveries = 0;
544
+ if (this.camId) this.stopStreams();
498
545
  }
499
546
  this.camId = camId;
500
547
  this.camName = name;
@@ -506,6 +553,39 @@ var PlayerController = class {
506
553
  if (ar) this.stage.style.setProperty("--stage-ar", ar);
507
554
  else this.stage.style.removeProperty("--stage-ar");
508
555
  }
556
+ /** Stop every stream and clear the stage (camera switch): no picture of the old camera stays. */
557
+ stopStreams() {
558
+ this.chaseAbort();
559
+ if (this.liveRestartT) {
560
+ clearTimeout(this.liveRestartT);
561
+ this.liveRestartT = 0;
562
+ }
563
+ if (this.mjpegRetryT) {
564
+ clearTimeout(this.mjpegRetryT);
565
+ this.mjpegRetryT = 0;
566
+ }
567
+ this.recWebrtcTeardown();
568
+ this.webrtcTeardown();
569
+ this.mseTeardown();
570
+ this.liveTeardown();
571
+ this.live = false;
572
+ this.recPaused = false;
573
+ this.recPausedTs = null;
574
+ this.hiddenTs = null;
575
+ this.playIndex = -1;
576
+ this.curClipId = null;
577
+ try {
578
+ this.v.pause();
579
+ this.v.srcObject = null;
580
+ this.v.removeAttribute("src");
581
+ this.v.onloadedmetadata = null;
582
+ } catch {
583
+ }
584
+ this.setMjpeg(false);
585
+ this.freezeHide();
586
+ this.transport = "none";
587
+ this.setLabel("loading");
588
+ }
509
589
  /** picture aspect → stage box (mobile layout uses it); posters and video both report it */
510
590
  setAspect(w, h) {
511
591
  if (!w || !h) return;
@@ -870,7 +950,10 @@ var PlayerController = class {
870
950
  setMjpeg(on) {
871
951
  this.img.classList.toggle("hidden", !on);
872
952
  this.v.classList.toggle("hidden", on);
873
- if (!on) this.img.removeAttribute("src");
953
+ if (!on) {
954
+ this.img.onerror = null;
955
+ this.img.removeAttribute("src");
956
+ }
874
957
  }
875
958
  webrtcTeardown() {
876
959
  this.w?.stop();
@@ -910,6 +993,7 @@ var PlayerController = class {
910
993
  this.safePlay();
911
994
  this.transport = "webrtc";
912
995
  this.setLabel("liveWebrtc");
996
+ this.liveWd = { last: this.presentedFrames(), at: Date.now() + 8e3, since: Date.now() };
913
997
  },
914
998
  onFail: () => {
915
999
  if (this.w === s) this.liveFallbackMse();
@@ -919,6 +1003,50 @@ var PlayerController = class {
919
1003
  this.transport = "webrtc";
920
1004
  s.start();
921
1005
  }
1006
+ /**
1007
+ * Live health, once a second while live and visible:
1008
+ * - WebRTC: no presented frame for 8 s (a 'disconnected' ICE that never turns 'failed' froze the picture under the
1009
+ * "Live" label) → restart, after 3 restarts in a row the MSE/MJPEG fallback;
1010
+ * - 30 s of flowing frames reset the restart counter (it only ever grew → MJPEG for good after 5 hiccups);
1011
+ * - in a fallback (MSE/MJPEG) WebRTC is tried again every 2 minutes.
1012
+ */
1013
+ liveWatch() {
1014
+ if (document.visibilityState === "hidden" || this.destroyed) return;
1015
+ const now = Date.now();
1016
+ if (this.transport === "webrtc" && this.w?.active && this.v.srcObject) {
1017
+ const q = this.presentedFrames();
1018
+ const W = this.liveWd;
1019
+ if (q !== W.last) {
1020
+ W.last = q;
1021
+ if (now > W.at) W.at = now;
1022
+ if (now - W.since > 3e4) {
1023
+ this.L.restarts = 0;
1024
+ this.liveUpgradeN = 0;
1025
+ W.since = now;
1026
+ }
1027
+ return;
1028
+ }
1029
+ if (now - W.at > 8e3) {
1030
+ this.L.restarts++;
1031
+ rlog("live-stall", { n: this.L.restarts, pf: q, ice: this.w.pc?.iceConnectionState });
1032
+ if (this.L.restarts > 3) this.liveFallbackMse();
1033
+ else this.liveStartWebrtc();
1034
+ }
1035
+ return;
1036
+ }
1037
+ if ((this.transport === "mse" || this.transport === "mjpeg") && window.RTCPeerConnection && window.WebSocket && !this.w) {
1038
+ if (!this.liveUpgradeAt) this.liveUpgradeAt = now + [12e4, 3e5, 9e5][Math.min(this.liveUpgradeN, 2)];
1039
+ else if (now > this.liveUpgradeAt) {
1040
+ this.liveUpgradeAt = 0;
1041
+ this.liveUpgradeN++;
1042
+ this.L.restarts = 0;
1043
+ rlog("live-upgrade", { from: this.transport, n: this.liveUpgradeN });
1044
+ if (this.transport === "mjpeg") this.freezeFromImage(this.img, true, false, "mjpeg");
1045
+ else this.freezeShow(true);
1046
+ this.liveStartWebrtc();
1047
+ }
1048
+ } else this.liveUpgradeAt = 0;
1049
+ }
922
1050
  liveFallbackMse() {
923
1051
  this.webrtcTeardown();
924
1052
  if (!this.live) return;
@@ -1084,14 +1212,26 @@ var PlayerController = class {
1084
1212
  this.v.pause();
1085
1213
  } catch {
1086
1214
  }
1215
+ const cam = this.camId;
1087
1216
  this.img.onload = () => {
1088
1217
  this.img.onload = null;
1218
+ this.mjpegRetries = 0;
1089
1219
  if (this.live) this.freezeLift(0);
1090
1220
  };
1221
+ this.img.onerror = () => {
1222
+ if (!this.live || this.camId !== cam || this.transport !== "mjpeg" || this.destroyed || this.mjpegRetries >= 3) return;
1223
+ const wait = 2e3 * 2 ** this.mjpegRetries++;
1224
+ rlog("mjpeg-retry", { n: this.mjpegRetries });
1225
+ if (this.mjpegRetryT) clearTimeout(this.mjpegRetryT);
1226
+ this.mjpegRetryT = window.setTimeout(() => {
1227
+ this.mjpegRetryT = 0;
1228
+ if (this.live && this.camId === cam && this.transport === "mjpeg") this.img.src = this.api.url(`api/live?camera=${encodeURIComponent(cam)}`) + `&_=${Date.now()}`;
1229
+ }, wait);
1230
+ };
1091
1231
  this.transport = "mjpeg";
1092
1232
  this.setLabel("liveMjpeg");
1093
1233
  this.setMjpeg(true);
1094
- this.img.src = this.api.url(`api/live?camera=${encodeURIComponent(this.camId)}`) + `&_=${Date.now()}`;
1234
+ this.img.src = this.api.url(`api/live?camera=${encodeURIComponent(cam)}`) + `&_=${Date.now()}`;
1095
1235
  }
1096
1236
  // ---- RECORDED via relay -----------------------------------------------------------------
1097
1237
  recWebrtcOk() {
@@ -1150,9 +1290,10 @@ var PlayerController = class {
1150
1290
  this.setLabel("loading");
1151
1291
  this.rwStartMs = ts;
1152
1292
  this.rwBase = null;
1153
- this.rwRate = this.rate;
1293
+ this.rwRate = 1;
1154
1294
  this.rwSrate = 1;
1155
1295
  this.seekTarget = ts;
1296
+ this.cmdSeq++;
1156
1297
  const compat = mobileClient();
1157
1298
  if (compat) rlog("rec-compat-start", { ts: Math.round(ts) });
1158
1299
  const s = new WebRtcSession(this.api, { camId: this.camId, mode: "recorded", startMs: ts, compat }, {
@@ -1190,14 +1331,13 @@ var PlayerController = class {
1190
1331
  if (this.rw === s) this.recFallbackMse(ts);
1191
1332
  },
1192
1333
  onSessionId: () => {
1193
- if (this.rw === s && this.rwPending) {
1194
- const p = this.rwPending;
1195
- this.rwPending = null;
1196
- if (Math.abs(p.ts - this.rwStartMs) > 1500) {
1197
- if (p.mark) this.freezeCurrent();
1198
- this.recRelaySeek(p.ts, p.rate, p.srate, p.mark);
1199
- }
1200
- }
1334
+ if (this.rw !== s) return;
1335
+ const p = this.rwPending;
1336
+ this.rwPending = null;
1337
+ if (p && Math.abs(p.ts - this.rwStartMs) > 1500) {
1338
+ if (p.mark) this.freezeCurrent();
1339
+ this.recRelaySeek(p.ts, p.rate, p.srate, p.mark);
1340
+ } else if (this.rate !== 1) this.recRelaySpeed(this.rate);
1201
1341
  }
1202
1342
  });
1203
1343
  this.sessFrames0 = this.presentedFrames();
@@ -1276,6 +1416,7 @@ var PlayerController = class {
1276
1416
  }
1277
1417
  const seq = this.cmdSeq;
1278
1418
  const done = (ok, w) => {
1419
+ if (this.rw !== s) return;
1279
1420
  this.rwSeekBusy = false;
1280
1421
  if (!ok) {
1281
1422
  this.relayRecover();
@@ -1292,7 +1433,7 @@ var PlayerController = class {
1292
1433
  if (p && this.rw?.id) this.recRelaySeek(p.ts, p.rate, p.srate, p.mark);
1293
1434
  };
1294
1435
  const q = `api/relay-seek?session=${encodeURIComponent(s.id)}&start=${Math.round(ts)}&rate=${rate}&srate=${srate}` + (mark ? `&mark=1&avoid=${this.shownW || this.v.videoWidth || 0}` : "");
1295
- fetch(this.api.url(q), { cache: "no-store" }).then((r) => {
1436
+ fetch(this.api.url(q), { cache: "no-store", signal: timeoutSignal(1e4) }).then((r) => {
1296
1437
  if (!r.ok) return done(false, 0);
1297
1438
  if (r.status === 200) return r.json().then((j) => done(true, Number(j?.w) || 0), () => done(true, 0));
1298
1439
  return done(true, 0);
@@ -1343,8 +1484,10 @@ var PlayerController = class {
1343
1484
  this.cad.lastNew = Date.now();
1344
1485
  this.rwTargetBusy = true;
1345
1486
  this.api.control(`api/relay-target?session=${encodeURIComponent(s.id)}&ts=${Math.round(ts)}`).then((ok) => {
1487
+ if (this.rw !== s) return;
1346
1488
  if (!ok) this.relayRecover();
1347
1489
  }).then(() => {
1490
+ if (this.rw !== s) return;
1348
1491
  this.rwTargetBusy = false;
1349
1492
  const p = this.rwPendingTarget;
1350
1493
  this.rwPendingTarget = null;
@@ -1374,7 +1517,7 @@ var PlayerController = class {
1374
1517
  this.wdGrace = Date.now() + 5e3;
1375
1518
  this.cad.lastNew = Date.now();
1376
1519
  this.api.control(`api/relay-scrub?session=${encodeURIComponent(s.id)}&on=${on ? 1 : 0}`).then((ok) => {
1377
- if (!ok) this.relayRecover();
1520
+ if (this.rw === s && !ok) this.relayRecover();
1378
1521
  });
1379
1522
  }
1380
1523
  relayRecover() {
@@ -1441,9 +1584,11 @@ var PlayerController = class {
1441
1584
  this.wdLastCt = q;
1442
1585
  this.wdLastAt = Date.now();
1443
1586
  }
1587
+ if (this.rwPosBusy) return;
1444
1588
  const seq = this.cmdSeq;
1445
- fetch(this.api.url(`api/relay-pos?session=${encodeURIComponent(s.id)}`), { cache: "no-store" }).then((r) => r.json()).then((d) => {
1446
- if (seq !== this.cmdSeq) return;
1589
+ this.rwPosBusy = true;
1590
+ fetch(this.api.url(`api/relay-pos?session=${encodeURIComponent(s.id)}`), { cache: "no-store", signal: timeoutSignal(3e3) }).then((r) => r.json()).then((d) => {
1591
+ if (this.rw !== s || seq !== this.cmdSeq) return;
1447
1592
  if (d && d.t > 0 && Date.now() - this.seekAt < 2500 && Math.abs(d.t - this.seekTarget) > 5e3) return;
1448
1593
  if (d && d.t > 0) {
1449
1594
  this.wdDead = 0;
@@ -1458,6 +1603,8 @@ var PlayerController = class {
1458
1603
  }
1459
1604
  }
1460
1605
  }).catch(() => {
1606
+ }).finally(() => {
1607
+ this.rwPosBusy = false;
1461
1608
  });
1462
1609
  }, 600);
1463
1610
  }
@@ -1467,6 +1614,7 @@ var PlayerController = class {
1467
1614
  clearInterval(this.relayPoll);
1468
1615
  this.relayPoll = void 0;
1469
1616
  }
1617
+ this.rwPosBusy = false;
1470
1618
  }
1471
1619
  cadStart() {
1472
1620
  this.cadStop();
@@ -2133,4 +2281,4 @@ export {
2133
2281
  WebRtcSession,
2134
2282
  PlayerController
2135
2283
  };
2136
- //# sourceMappingURL=chunk-Z22KWFHI.js.map
2284
+ //# sourceMappingURL=chunk-MAUN6MNY.js.map