code wiki / _hdl_build / nx_video_qoe_live.nx
nx_video_qoe_live.nx source
↩ module page · 206 lines · 12068 B
1// nx_video_qoe_live.nx -- LIVE video-QoE measurement (operator 2026-07-03: "provide fps and stability and
2// other measurements ... too many false positives that our video chat is really good ... honest feedback
3// if we need more hardware"). NOT a simulation: two real TLS-1.3+WSS sessions through the PRODUCTION edge
4// (nishifamily.com -> sites.elf /signal pump -> nx_signaling_v2 relay), sender A paces REAL room-framed
5// video-kind frames (vc_wire_pack kind 0x56, 6000B payload = a realistic ladder JPEG) at a target cadence,
6// receiver B timestamps arrivals. Ping-pong (one frame in flight) = backpressure-safe on the production
7// relay. Measures PER RUNG of a load ramp (20/40/80/160 fps): achieved fps, path latency p50/p95/max,
8// late>150ms (playout-deadline class, the intl_h2h convention), gap>500ms (hard-freeze class), and a
9// connect/TLS setup time. The ramp knee = the honest hardware ceiling of the CURRENT edge.
10// HONESTY BOUNDS (printed, so no false positives): this measures the Texas-side LAN->edge->LAN path ONLY;
11// the Minsk leg needs a probe RUN FROM MINSK (registered follow-up) or calibration vs RIPE-Atlas real-path
12// data (banked by nx_vqoe_research_fetch). Log -> knowledge/status/video_qoe_live.log
13// expect_exit: 0 (measurement SOUND -- controls hold; the numbers themselves print honestly)
14// license_tier: ORIGINAL
15import "nx_ws_client_session.nx" // wscs_connect / wscs_send / wscs_ws_recv (+ TLS/trust/frame transitive)
16import "nx_video_client_wasm.nx" // vc_wire_pack / vc_wire_parse -- the REAL room framing
17import "nx_metric_ledger.nx" // ml_report: return-and-report -- every run prints BETTER/WORSE vs last
18const QOE_MAGIC_8192: i64 = 8192
19const QOE_MAGIC_150000: i64 = 150000
20const QOE_MAGIC_500000: i64 = 500000
21const QOE_MAGIC_10000000: i64 = 10000000
22const QOE_MAGIC_4194304: i64 = 4194304
23const QOE_MAGIC_16384: i64 = 16384
24const QOE_MAGIC_50000: i64 = 50000
25const QOE_MAGIC_25000: i64 = 25000
26const QOE_MAGIC_12500: i64 = 12500
27const QOE_MAGIC_6250: i64 = 6250
28
29const QOE_PAY: i64 = 6000 // realistic ladder-frame payload (JPEG-class)
30const QOE_FRAMES: i64 = 200 // frames per rung
31
32func qw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
33func qn(v: i64) -> i64 {
34 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m}
35 let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}
36 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 }
37func qfw(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 }
38func qfn(fd: i64, v: i64) -> i64 {
39 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(fd,"-" as *u8,1);m=0-m}
40 let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}
41 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(fd,b,k); return 0 }
42
43// insertion sort (200 elems)
44func qsortn(a: *i64, n: i64) -> i64 {
45 var i: i64 = 1
46 while i < n {
47 let v: i64 = a[i]
48 var j: i64 = i - 1
49 var g: i64 = 1
50 while g == 1 { if j >= 0 { if a[j] > v { a[j+1] = a[j]; j = j - 1 } else { g = 0 } } else { g = 0 } }
51 a[j+1] = v
52 i = i + 1
53 }
54 return 0
55}
56
57// one ramp rung: pace QOE_FRAMES frames at intv_us through the LIVE relay; fill res:
58// res[0]=received res[1]=late150 res[2]=gap500 res[3]=p50us res[4]=p95us res[5]=maxus res[6]=achieved_fps_x10 res[7]=seq_mismatch
59func rung(sa: *Tls13ClientSession, fa: i64, sb: *Tls13ClientSession, fb: i64, intv_us: i64, id8: *u8, wirebuf: *u8, payload: *u8, got: *u8, lats: *i64, res: *i64) -> i64 {
60 var received: i64 = 0
61 var late150: i64 = 0
62 var gap500: i64 = 0
63 var mismatch: i64 = 0
64 var last_arr: i64 = 0
65 let winfo: *i64 = sys_mmap(64) as *i64
66 let t_start: i64 = sys_now_us()
67 var i: i64 = 0
68 while i < QOE_FRAMES {
69 let wlen: i64 = vc_wire_pack(wirebuf, 0x56, id8, i, payload, QOE_PAY)
70 let frame: *u8 = sys_mmap(QOE_MAGIC_8192)
71 let mask: i64 = 0x5A6B7C8D
72 let hn: i64 = ws_build_header(frame, QOE_MAGIC_8192, 1, WS_OP_BINARY, 1, mask, wlen)
73 var c: i64 = 0
74 while c < wlen { frame[hn + c] = wirebuf[c]; c = c + 1 }
75 ws_apply_mask(frame, hn, wlen, mask)
76 let t0: i64 = sys_now_us()
77 if wscs_send(sa, fa, frame, hn + wlen) != 0 { res[0]=received; return 0 - 1 }
78 let glen: i64 = wscs_ws_recv(sb, fb, got, QOE_MAGIC_8192)
79 let t1: i64 = sys_now_us()
80 if glen > 0 {
81 if vc_wire_parse(got, glen, winfo) == 0 {
82 if winfo[1] != i { mismatch = mismatch + 1 }
83 let lat: i64 = t1 - t0
84 lats[received] = lat
85 received = received + 1
86 if lat > QOE_MAGIC_150000 { late150 = late150 + 1 }
87 if last_arr > 0 { if (t1 - last_arr) > QOE_MAGIC_500000 { gap500 = gap500 + 1 } }
88 last_arr = t1
89 } else { mismatch = mismatch + 1 }
90 }
91 // pace to the cadence (if the path is slower than the cadence, we fall behind = the honest knee)
92 let t_next: i64 = t_start + (i + 1) * intv_us
93 let now: i64 = sys_now_us()
94 if now < t_next { sys_sleep_ms((t_next - now) / 1000) }
95 i = i + 1
96 }
97 let t_end: i64 = sys_now_us()
98 qsortn(lats, received)
99 res[0] = received
100 res[1] = late150
101 res[2] = gap500
102 if received > 0 {
103 res[3] = lats[received / 2]
104 res[4] = lats[(received * 95) / 100]
105 res[5] = lats[received - 1]
106 } else { res[3]=0; res[4]=0; res[5]=0 }
107 let dur_us: i64 = t_end - t_start
108 if dur_us > 0 { res[6] = (received * QOE_MAGIC_10000000) / dur_us } else { res[6] = 0 }
109 res[7] = mismatch
110 return 0
111}
112
113func main() -> i64 {
114 qw("=== nx_video_qoe_live: LIVE fps/latency/stability through the PRODUCTION edge (not a simulation) ===\n" as *u8)
115 let r: i64 = nx_trust_store_load_from_certdata("data/mozilla_certdata.txt\x00" as *u8, 512, QOE_MAGIC_4194304)
116 if r <= 0 { qw("trust store load failed\n" as *u8); return 1 }
117 let store: *TrustStore = r as *TrustStore
118 let url: *u8 = "https://nishifamily.com/signal/nxqoe\x00" as *u8
119 let sa: *i64 = sys_mmap(16) as *i64
120 let fa: *i64 = sys_mmap(16) as *i64
121 let sb: *i64 = sys_mmap(16) as *i64
122 let fb: *i64 = sys_mmap(16) as *i64
123 let tc0: i64 = sys_now_us()
124 if wscs_connect(store, url, 0xC0, sa, fa) != 1 { qw("peer A connect FAILED\n" as *u8); return 1 }
125 let tc1: i64 = sys_now_us()
126 if wscs_connect(store, url, 0x30, sb, fb) != 1 { qw("peer B connect FAILED\n" as *u8); return 1 }
127 let tc2: i64 = sys_now_us()
128 qw(" connect+TLS+WS-upgrade: peerA=" as *u8); qn((tc1-tc0)/1000); qw("ms peerB=" as *u8); qn((tc2-tc1)/1000); qw("ms (call setup cost)\n" as *u8)
129 let sessA: *Tls13ClientSession = sa[0] as *Tls13ClientSession
130 let sessB: *Tls13ClientSession = sb[0] as *Tls13ClientSession
131
132 let id8: *u8 = sys_mmap(16)
133 var z: i64 = 0
134 while z < 8 { id8[z] = (81 + z) as u8; z = z + 1 }
135 let payload: *u8 = sys_mmap(QOE_PAY + 64)
136 var pz: i64 = 0
137 while pz < QOE_PAY { payload[pz] = ((pz * 7 + 13) & 0xff) as u8; pz = pz + 1 }
138 let wirebuf: *u8 = sys_mmap(QOE_PAY + 256)
139 let got: *u8 = sys_mmap(QOE_MAGIC_16384)
140 let lats: *i64 = sys_mmap(8 * (QOE_FRAMES + 8)) as *i64
141 let res: *i64 = sys_mmap(96) as *i64
142
143 let lf: i64 = sys_openat_append("knowledge/status/video_qoe_live.log" as *u8, 420)
144 if lf >= 0 { qfw(lf, "VQOELIVE t_us=" as *u8); qfn(lf, sys_now_us()); qfw(lf, " setupA_ms=" as *u8); qfn(lf, (tc1-tc0)/1000); qfw(lf, "\n" as *u8) }
145
146 var okmeas: i64 = 1
147 var knee_fps: i64 = 0
148 var p50_20: i64 = 0
149 var p95_20: i64 = 0
150 var late_total: i64 = 0
151 var gap_total: i64 = 0
152 var rungno: i64 = 0
153 while rungno < 4 {
154 var intv: i64 = QOE_MAGIC_50000
155 var tag: *u8 = "20fps (shipped ladder ceiling)" as *u8
156 if rungno == 1 { intv = QOE_MAGIC_25000; tag = "40fps (2x headroom probe)" as *u8 }
157 if rungno == 2 { intv = QOE_MAGIC_12500; tag = "80fps (4x headroom probe)" as *u8 }
158 if rungno == 3 { intv = QOE_MAGIC_6250; tag = "160fps (8x = 8-party-at-20fps proxy)" as *u8 }
159 let rc: i64 = rung(sessA, fa[0], sessB, fb[0], intv, id8, wirebuf, payload, got, lats, res)
160 if rc != 0 { qw(" rung send/recv FAILED (edge dropped) at " as *u8); qw(tag); qw("\n" as *u8); okmeas = 0; rungno = 4 } else {
161 qw(" == " as *u8); qw(tag); qw(" ==\n" as *u8)
162 qw(" delivered=" as *u8); qn(res[0]); qw("/" as *u8); qn(QOE_FRAMES)
163 qw(" achieved_fps=" as *u8); qn(res[6]/10); qw("." as *u8); qn(res[6]%10)
164 qw(" path_lat p50=" as *u8); qn(res[3]/1000); qw("ms p95=" as *u8); qn(res[4]/1000); qw("ms max=" as *u8); qn(res[5]/1000); qw("ms\n" as *u8)
165 qw(" late>150ms=" as *u8); qn(res[1]); qw(" hard-gaps>500ms=" as *u8); qn(res[2]); qw(" seq_mismatch=" as *u8); qn(res[7]); qw("\n" as *u8)
166 if res[7] != 0 { okmeas = 0 }
167 if res[0] < QOE_FRAMES { if res[0] * 10 < QOE_FRAMES * 9 { okmeas = 0 } } // >10% loss on TCP = broken measurement
168 if rungno == 0 { p50_20 = res[3]; p95_20 = res[4] }
169 late_total = late_total + res[1]
170 gap_total = gap_total + res[2]
171 // knee detection: achieved fps must reach >=90% of the target to count as sustained.
172 // 10^7/intv_us = target fps x10 (res[6] is also fps x10) -> knee_fps stored as fps x10.
173 if res[6] * 10 >= (QOE_MAGIC_10000000 / intv) * 9 { knee_fps = (QOE_MAGIC_10000000 / intv) }
174 if lf >= 0 {
175 qfw(lf, "VQOELIVE rung_intv_us=" as *u8); qfn(lf, intv)
176 qfw(lf, " delivered=" as *u8); qfn(lf, res[0])
177 qfw(lf, " fps_x10=" as *u8); qfn(lf, res[6])
178 qfw(lf, " p50us=" as *u8); qfn(lf, res[3])
179 qfw(lf, " p95us=" as *u8); qfn(lf, res[4])
180 qfw(lf, " maxus=" as *u8); qfn(lf, res[5])
181 qfw(lf, " late150=" as *u8); qfn(lf, res[1])
182 qfw(lf, " gap500=" as *u8); qfn(lf, res[2])
183 qfw(lf, "\n" as *u8)
184 }
185 }
186 rungno = rungno + 1
187 }
188 sys_close(fa[0]); sys_close(fb[0])
189
190 // RETURN-AND-REPORT: every axis lands in the metric ledger with a trend verdict vs the last run
191 // (operator doctrine 2026-07-03: always know if we are moving toward more or less performance/quality).
192 qw(" -- return-and-report (trend vs previous run) --\n" as *u8)
193 ml_report("vqoe_p50_20fps_us" as *u8, p50_20, 0 - 1, "us" as *u8, "nx_video_qoe_live" as *u8)
194 ml_report("vqoe_p95_20fps_us" as *u8, p95_20, 0 - 1, "us" as *u8, "nx_video_qoe_live" as *u8)
195 ml_report("vqoe_setup_ms" as *u8, (tc1 - tc0) / 1000, 0 - 1, "ms" as *u8, "nx_video_qoe_live" as *u8)
196 ml_report("vqoe_knee_fps_x10" as *u8, knee_fps, 1, "fps_x10" as *u8, "nx_video_qoe_live" as *u8)
197 ml_report("vqoe_late150_total" as *u8, late_total, 0 - 1, "frames" as *u8, "nx_video_qoe_live" as *u8)
198 ml_report("vqoe_gap500_total" as *u8, gap_total, 0 - 1, "frames" as *u8, "nx_video_qoe_live" as *u8)
199 qw(" HONESTY BOUNDS: this is the Texas-side LAN->edge->LAN path (real TLS, real relay, real framing).\n" as *u8)
200 qw(" It does NOT include the Minsk leg -- that needs a probe run FROM Minsk (or RIPE-Atlas calibration).\n" as *u8)
201 qw(" sustained ceiling measured (>=90% of target): " as *u8); qn(knee_fps/10); qw(" fps single-flow ping-pong\n" as *u8)
202 if lf >= 0 { qfw(lf, "VQOELIVE knee_fps=" as *u8); qfn(lf, knee_fps/10); if okmeas==1 { qfw(lf, " verdict=SOUND\n" as *u8) } else { qfw(lf, " verdict=UNSOUND\n" as *u8) } sys_close(lf) }
203 if okmeas == 1 { qw("VIDEO-QOE-LIVE verdict=SOUND -- numbers above are LIVE-MEASURED, not modeled\n" as *u8); return 0 }
204 qw("VIDEO-QOE-LIVE verdict=UNSOUND -- a control failed; do not cite these numbers\n" as *u8)
205 return 1
206}