code wiki / _hdl_build / nx_media_signal_gate.nx
nx_media_signal_gate.nx source
↩ module page · 63 lines · 4356 B
1import "nx_gate_gn.nx"
2import "nx_gate_base.nx"
3// nx_media_signal_gate.nx -- proves the intelligent, site-resilient classifier: media detected by EXTENSION,
4// PATH pattern (extension-less YouTube URLs -- the probe gap), and CDN host; ranked (manifest>direct>...);
5// smart-name title extraction. license_tier: ORIGINAL expect_exit: 0
6import "nx_syscalls.nx"
7import "nx_media_signal.nx"
8
9func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw("
10" as *u8); return ok }
11func gsl(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
12func streq(a: *u8, b: *u8) -> i64 { var i: i64=0; while 1==1 { let x: i64=a[i]&0xff; let y: i64=b[i]&0xff; if x!=y { return 0 } if x==0 { return 1 } i=i+1 } return 0 }
13
14func chk_cls(url: *u8, want_kind: i64, min_conf: i64) -> i64 {
15 let kb: *i64 = sys_mmap(8) as *i64
16 let conf: i64 = xt_classify(url, gsl(url), kb)
17 if kb[0] != want_kind { gw(" (kind=" as *u8); gn(kb[0]); gw(" conf=" as *u8); gn(conf); gw(")\n" as *u8); return 0 }
18 if conf < min_conf { gw(" (conf=" as *u8); gn(conf); gw(" < " as *u8); gn(min_conf); gw(")\n" as *u8); return 0 }
19 return 1
20}
21
22func main() -> i64 {
23 gw("media-signal SOVEREIGN gate (multi-signal classify + rank + smart-name title)\n" as *u8)
24 var pass: i64 = 0
25 var ttl: i64 = 0
26
27 // --- classification: extension / path / host signals ---
28 ttl=ttl+1; pass=pass+grow("C1 YouTube /videoplayback (no ext) -> DIRECT (probe fix)\x00" as *u8,
29 chk_cls("https://rr3---sn-abc.googlevideo.com/videoplayback?expire=1&itag=22&mime=video%2Fmp4" as *u8, XT_DIRECT, 60))
30 ttl=ttl+1; pass=pass+grow("C2 .m3u8 -> HLS conf95\x00" as *u8, chk_cls("https://cdn/master.m3u8" as *u8, XT_HLS, 95))
31 ttl=ttl+1; pass=pass+grow("C3 .m3u8?token -> HLS (ext beats query)\x00" as *u8, chk_cls("https://cdn/index.m3u8?sig=abc&e=9" as *u8, XT_HLS, 90))
32 ttl=ttl+1; pass=pass+grow("C4 cloudfront .mp4 -> DIRECT\x00" as *u8, chk_cls("https://d1.cloudfront.net/vid/movie.mp4" as *u8, XT_DIRECT, 90))
33 ttl=ttl+1; pass=pass+grow("C5 .mpd -> DASH\x00" as *u8, chk_cls("https://cdn/manifest.mpd" as *u8, XT_DASH, 95))
34 ttl=ttl+1; pass=pass+grow("C6 extension-less /master path -> HLS\x00" as *u8, chk_cls("https://cdn/live/master?t=1" as *u8, XT_HLS, 60))
35 ttl=ttl+1; pass=pass+grow("C7 non-media page URL -> NONE\x00" as *u8, chk_cls("https://example.com/about/team.html" as *u8, XT_NONE, 0))
36 ttl=ttl+1; pass=pass+grow("C8 bunnycdn host + video token -> DIRECT\x00" as *u8, chk_cls("https://vid.b-cdn.net/stream/video123/play" as *u8, XT_DIRECT, 40))
37
38 // --- ranking: pick the best from a candidate list ---
39 let kb: *i64 = sys_mmap(8) as *i64
40 let best: *u8 = sys_mmap(4096)
41 ttl=ttl+1
42 let c1: i64 = xt_best("https://c/seg1.ts\nhttps://c/fallback.mp4\nhttps://c/master.m3u8\x00" as *u8, 52, best, 4096, kb)
43 pass=pass+grow("R1 [ts,mp4,m3u8] -> picks m3u8 (HLS wins)\x00" as *u8, (kb[0] == XT_HLS) as i64)
44 ttl=ttl+1
45 let c2: i64 = xt_best("https://i.ytimg.com/hq.jpg\nhttps://r.googlevideo.com/videoplayback?itag=22\x00" as *u8, 68, best, 4096, kb)
46 pass=pass+grow("R2 [thumbnail,videoplayback] -> picks the stream (DIRECT)\x00" as *u8, (kb[0] == XT_DIRECT) as i64)
47
48 // --- smart naming: title from og:title / "title": / <title> ---
49 let tb: *u8 = sys_mmap(1024)
50 ttl=ttl+1
51 xt_extract_title("<html><head><meta property=\"og:title\" content=\"My Great Video\"></head></html>" as *u8, 80, tb, 1024)
52 pass=pass+grow("T1 og:title -> 'My Great Video'\x00" as *u8, streq(tb, "My Great Video\x00" as *u8))
53 ttl=ttl+1
54 xt_extract_title("<script>window.d={\"title\":\"JSON Title Here\",\"x\":1}</script>" as *u8, 58, tb, 1024)
55 pass=pass+grow("T2 \"title\": -> 'JSON Title Here'\x00" as *u8, streq(tb, "JSON Title Here\x00" as *u8))
56 ttl=ttl+1
57 xt_extract_title("<html><head><title>Page Title - Site</title></head></html>" as *u8, 58, tb, 1024)
58 pass=pass+grow("T3 <title> -> 'Page Title - Site'\x00" as *u8, streq(tb, "Page Title - Site\x00" as *u8))
59
60 gw("pass=" as *u8); gn(pass); gw("/" as *u8); gn(ttl); gw("\n" as *u8)
61 if pass == ttl { gw("verdict=GREEN (site-resilient multi-signal media classify + rank + smart-name)\n" as *u8); sys_exit(0); return 0 }
62 gw("verdict=RED\n" as *u8); sys_exit(1); return 1
63}