code wiki / _hdl_build / nx_video_native_video_gate.nx

nx_video_native_video_gate.nx source

↩ module page · 91 lines · 6765 B

1// nx_video_native_video_gate.nx -- MOBILE-NATIVE VIDEO proof (task #22/#47). The chat gate proved a pure-nx 2// client (no browser/JS/wasm) is a room participant; THIS proves the same native client does VIDEO end to end 3// on the LIVE relay, producing the byte-identical 0x57 wire the browser speaks -> native<->browser interop. 4// Native peer A: synthetic YUV frame -> vv_enc (the SHARED core: DCT-II + in-loop deblock + AQ, native) -> 5// 0x57 payload [key|qp|w|h][vv_enc out] -> vc_wire_pack -> TLS-1.3/WSS -> relay. Native peer B: receives via 6// the content-blind broadcast, vc_wire_parse (asserts kind 0x57), vv_dec -> recon; independently vv_enc+the 7// SAME frame locally and asserts the DECODED-FROM-WIRE recon == the LOCAL recon BIT-EXACT. GREEN = native 8// video works on the real edge with a browser-compatible bitstream (the codec gains ride the shared core for 9// free). Camera(V4L2)/display(framebuffer) are the remaining hardware last-mile. license_tier: ORIGINAL 10import "nx_ws_client_session.nx" 11import "nx_video_client_wasm.nx" // vv_enc / vv_dec / vc_wire_pack / vc_wire_parse -- the SAME browser core, native 12 13func nw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 14func nn(v: i64) -> i64 { 15 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} 16 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} 17 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 } 18func eqb(a: *u8, b: *u8, n: i64) -> i64 { var i: i64=0; while i<n { if (a[i]&0xff)!=(b[i]&0xff) { return 0 } i=i+1 } return 1 } 19func vfill(yuv: *u8, W: i64, H: i64) -> i64 { // gradient + edge + chroma tint = realistic content 20 let N: i64=W*H; let C: i64=(W/2)*(H/2) 21 var r: i64=0 22 while r<H { var c: i64=0 23 while c<W { var v: i64=(c+r)*2+30; if c>W/2 { v=v+45 } if v>255 {v=255}; yuv[r*W+c]=v as u8; c=c+1 } r=r+1 } 24 var i: i64=N; while i<N+2*C { yuv[i]=120 as u8; i=i+1 } return 0 } 25 26// build a browser-compatible single-frame 0x57 PAYLOAD at pay: [key|qp|wlo|whi|hlo|hhi][vv_enc out]. returns len. 27func pack57(pay: *u8, cur: *u8, prevE: *u8, reconE: *u8, W: i64, H: i64, qp: i64, blk: *i64, mv: *i64) -> i64 { 28 let n: i64 = vv_enc(cur, prevE, reconE, W, H, qp, 1, 6016, ((pay as i64)+6) as *u8, 262144, blk, mv) 29 if n <= 0 { return 0 - 1 } 30 pay[0]=1 as u8; pay[1]=qp as u8 // b0=keyframe ; qp 31 pay[2]=(W&255) as u8; pay[3]=(W>>8) as u8; pay[4]=(H&255) as u8; pay[5]=(H>>8) as u8 32 return 6 + n } 33 34func main() -> i64 { 35 nw("=== nx_video_native_video_gate: pure-nx VIDEO (vv_enc/vv_dec, no browser) over the LIVE relay ===\n" as *u8) 36 let r: i64 = nx_trust_store_load_from_certdata("data/mozilla_certdata.txt\x00" as *u8, 512, 4194304) 37 if r <= 0 { nw("trust store load failed\n" as *u8); return 1 } 38 let store: *TrustStore = r as *TrustStore 39 let url: *u8 = "https://nishifamily.com/signal/nxnativevid\x00" as *u8 40 let sa: *i64=sys_mmap(16) as *i64; let fa: *i64=sys_mmap(16) as *i64 41 let sb: *i64=sys_mmap(16) as *i64; let fb: *i64=sys_mmap(16) as *i64 42 if wscs_connect(store, url, 0xC0, sa, fa) != 1 { nw("NATIVE A connect FAILED\n" as *u8); return 1 } 43 if wscs_connect(store, url, 0x30, sb, fb) != 1 { nw("NATIVE B connect FAILED\n" as *u8); return 1 } 44 nw(" both native peers upgraded (TLS-1.3 + WSS), ZERO browser\n" as *u8) 45 let sessA: *Tls13ClientSession = sa[0] as *Tls13ClientSession 46 let sessB: *Tls13ClientSession = sb[0] as *Tls13ClientSession 47 48 let W: i64=128; let H: i64=96; let qp: i64=20 // codec-safe (chroma 64x48 both /16) 49 let N: i64=W*H; let C: i64=(W/2)*(H/2); let sz: i64=N+2*C 50 let blk: *i64=sys_mmap(512) as *i64; let mv: *i64=sys_mmap(128) as *i64 51 let cur: *u8=sys_mmap(sz); let prevE: *u8=sys_mmap(sz); let reconE: *u8=sys_mmap(sz) 52 var z: i64=0; while z<sz { prevE[z]=0 as u8; z=z+1 } 53 vfill(cur, W, H) 54 let pay: *u8=sys_mmap(262144) 55 let plen: i64 = pack57(pay, cur, prevE, reconE, W, H, qp, blk, mv) 56 if plen < 0 { nw("vv_enc FAILED\n" as *u8); return 1 } 57 58 // ---- A: wrap 0x57 payload in the room wire + WSS frame and send ---- 59 let id8: *u8=sys_mmap(16); var q: i64=0; while q<8 { id8[q]=(65+q) as u8; q=q+1 } 60 let wire: *u8=sys_mmap(262144) 61 let wlen: i64 = vc_wire_pack(wire, 0x57, id8, 1, pay, plen) 62 let frame: *u8=sys_mmap(262144); let mask: i64=0x5A6B7C8D 63 let hn: i64 = ws_build_header(frame, 262144, 1, WS_OP_BINARY, 1, mask, wlen) 64 var c: i64=0; while c<wlen { frame[hn+c]=wire[c]; c=c+1 } 65 ws_apply_mask(frame, hn, wlen, mask) 66 if wscs_send(sessA, fa[0], frame, hn+wlen) != 0 { nw("A send FAILED\n" as *u8); return 1 } 67 nw(" native A sent 0x57 video frame (payload=" as *u8); nn(plen); nw("B, wire=" as *u8); nn(wlen); nw("B)\n" as *u8) 68 69 // ---- B: receive, parse 0x57, decode, and compare BIT-EXACT to a local encode+decode of the same frame ---- 70 let got: *u8=sys_mmap(262144) 71 let glen: i64 = wscs_ws_recv(sessB, fb[0], got, 262144) 72 sys_close(fa[0]); sys_close(fb[0]) 73 if glen <= 0 { nw("B received nothing -> RED (relay didn't forward 0x57?)\n" as *u8); return 1 } 74 let winfo: *i64=sys_mmap(64) as *i64 75 if vc_wire_parse(got, glen, winfo) != 0 { nw("vc_wire_parse FAILED -> RED\n" as *u8); return 1 } 76 let kind: i64=winfo[0]; let payoff: i64=winfo[2]; let rplen: i64=winfo[3] 77 nw(" native B received " as *u8); nn(glen); nw("B; kind=" as *u8); nn(kind); nw(" (want 87=0x57)\n" as *u8) 78 if kind != 0x57 { nw("wrong kind -> RED\n" as *u8); return 1 } 79 let rpay: *u8=((got as i64)+payoff) as *u8 80 let rqp: i64=rpay[1]&0xff; let rw: i64=(rpay[2]&0xff)|((rpay[3]&0xff)<<8); let rh: i64=(rpay[4]&0xff)|((rpay[5]&0xff)<<8) 81 let prevD: *u8=sys_mmap(sz); let reconD: *u8=sys_mmap(sz) 82 z=0; while z<sz { prevD[z]=0 as u8; z=z+1 } 83 if vv_dec(prevD, reconD, rw, rh, rqp, ((rpay as i64)+6) as *u8, rplen-6, blk, mv) != 0 { nw("vv_dec REFUSED -> RED\n" as *u8); return 1 } 84 // reconE is A's encode recon = what a correct decode must reproduce (same core, same frame -> bit-exact) 85 let okv: i64 = eqb(reconD, reconE, sz) 86 let lf: i64 = sys_openat_append("knowledge/status/video_native.log" as *u8, 420) 87 if lf >= 0 { if okv==1 { sys_write(lf, "VIDEONATIVE video kind=0x57 decode_bitexact=GREEN\n" as *u8, 47) } else { sys_write(lf, "VIDEONATIVE video verdict=RED\n" as *u8, 30) } sys_close(lf) } 88 if okv == 1 { nw("NATIVE-VIDEO verdict=GREEN -- pure-nx video (DCT+deblock+AQ) round-trips over the LIVE relay, browser-compatible 0x57\n" as *u8); return 0 } 89 nw("NATIVE-VIDEO verdict=RED -- decoded frame != encoder recon (wire/codec mismatch)\n" as *u8) 90 return 1 91}