File size: 4,026 Bytes
abc7cb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Cakra CCTV — pure embed</title>
<style>
  html, body { margin:0; height:100%; background:#000;
               font: 12px ui-monospace, Menlo, Consolas, monospace; color:#9af; }
  #wrap { position:relative; height:100%; display:flex; align-items:center;
          justify-content:center; }
  video { max-width:100%; max-height:100%; display:block; background:#000; }
  #hud { position:absolute; top:8px; right:8px; padding:6px 10px;
         background:rgba(0,0,0,.6); border-radius:4px; }
  #hud b { color:#fff; }
</style>
</head>
<body>
<div id="wrap">
  <video id="v" autoplay muted playsinline></video>
  <div id="hud">
    <div>state: <b id="s">init</b></div>
    <div>buf ahead: <b id="b">—</b>s</div>
    <div>currentTime: <b id="t">—</b>s</div>
    <div>w x h: <b id="r">—</b></div>
  </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/hls.js@1.5.13/dist/hls.min.js"></script>
<script>
const qs = new URLSearchParams(location.search);
const base = qs.get("base") || "http://localhost:8000";
const id = qs.get("camera_id");
const v = document.getElementById("v");
const $ = (i) => document.getElementById(i);

if (!id) { $("s").textContent = "missing ?camera_id="; }
else {
  fetch(`${base}/api/v1/cameras/${id}/connect`, { method:"POST" })
    .then(r => r.ok ? r.json() : Promise.reject(r.status))
    .then(() => {
      const url = `${base}/api/v1/cameras/${id}/hls/playlist.m3u8`;
      if (Hls.isSupported()) {
        const hls = new Hls({
          enableWorker: true,
          lowLatencyMode: false,
          liveSyncDurationCount: 999,
          liveMaxLatencyDurationCount: 1000,
          maxBufferLength: 30,
          maxMaxBufferLength: 60,
          backBufferLength: 0,
          liveDurationInfinity: true,
          manifestLoadingMaxRetry: 8, manifestLoadingRetryDelay: 500,
          levelLoadingMaxRetry: 8,    levelLoadingRetryDelay: 500,
          fragLoadingMaxRetry: 4,     fragLoadingRetryDelay: 500,
        });
        hls.loadSource(url); hls.attachMedia(v);
        let anchored = false;
        hls.on(Hls.Events.LEVEL_LOADED, (_e,d) => {
          if (anchored) return;
          const frags = d?.details?.fragments;
          if (!frags?.length) return;
          anchored = true;
          v.currentTime = frags[0].start;
          // Wait for 5s buffer then play.
          $("s").textContent = "buffering";
          const tick = () => {
            const ahead = v.buffered.length
              ? v.buffered.end(v.buffered.length-1) - v.currentTime : 0;
            if (ahead >= 5) { v.play(); }
            else { setTimeout(tick, 200); }
          };
          tick();
        });
      } else if (v.canPlayType("application/vnd.apple.mpegurl")) {
        v.src = url; v.play();
      } else { $("s").textContent = "browser no HLS"; }
    })
    .catch(e => { $("s").textContent = "connect failed: " + e; });
}

v.addEventListener("playing", () => $("s").textContent = "playing");
v.addEventListener("waiting", () => $("s").textContent = "buffering");
v.addEventListener("stalled", () => $("s").textContent = "stalled");
v.addEventListener("loadedmetadata", () => {
  $("r").textContent = v.videoWidth + " x " + v.videoHeight;
});

// HUD telemetry: track buffer + currentTime over time so we can spot stalls.
setInterval(() => {
  const ahead = v.buffered.length
    ? v.buffered.end(v.buffered.length-1) - v.currentTime : 0;
  $("b").textContent = ahead.toFixed(2);
  $("t").textContent = v.currentTime.toFixed(2);
}, 200);

// Expose a sample-collector for automated probing.
window.__samples = [];
setInterval(() => {
  const ahead = v.buffered.length
    ? v.buffered.end(v.buffered.length-1) - v.currentTime : 0;
  window.__samples.push({
    t: performance.now(),
    ahead: +ahead.toFixed(3),
    ct: +v.currentTime.toFixed(3),
    paused: v.paused,
    state: $("s").textContent,
  });
  if (window.__samples.length > 600) window.__samples.shift();
}, 100);
</script>
</body>
</html>