code wiki / _hdl_build / nx_media_signal_gate.nx
nx_media_signal_gate.nx source
↩ module page · 71 lines · 4739 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"
8import "nx_gate_verdict.nx"
9
10func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw("
11" as *u8); return ok }
12func gsl(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
13func 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 }
14
15func chk_cls(url: *u8, want_kind: i64, min_conf: i64) -> i64 {
16 let kb: *i64 = sys_mmap(8) as *i64
17 let conf: i64 = xt_classify(url, gsl(url), kb)
18 if kb[0] != want_kind { gw(" (kind=" as *u8); gn(kb[0]); gw(" conf=" as *u8); gn(conf); gw(")\n" as *u8); return 0 }
19 if conf < min_conf { gw(" (conf=" as *u8); gn(conf); gw(" < " as *u8); gn(min_conf); gw(")\n" as *u8); return 0 }
20 return 1
21}
22
23func main() -> i64 {
24 gw("media-signal SOVEREIGN gate (multi-signal classify + rank + smart-name title)\n" as *u8)
25 var pass: i64 = 0
26 var ttl: i64 = 0
27
28 // --- classification: extension / path / host signals ---
29 ttl=ttl+1; pass=pass+grow("C1 YouTube /videoplayback (no ext) -> DIRECT (probe fix)\x00" as *u8,
30 chk_cls("https://rr3---sn-abc.googlevideo.com/videoplayback?expire=1&itag=22&mime=video%2Fmp4" as *u8, XT_DIRECT, 60))
31 ttl=ttl+1; pass=pass+grow("C2 .m3u8 -> HLS conf95\x00" as *u8, chk_cls("https://cdn/master.m3u8" as *u8, XT_HLS, 95))
32 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))
33 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))
34 ttl=ttl+1; pass=pass+grow("C5 .mpd -> DASH\x00" as *u8, chk_cls("https://cdn/manifest.mpd" as *u8, XT_DASH, 95))
35 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))
36 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))
37 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))
38
39 // --- ranking: pick the best from a candidate list ---
40 let kb: *i64 = sys_mmap(8) as *i64
41 let best: *u8 = sys_mmap(4096)
42 ttl=ttl+1
43 let c1: i64 = xt_best("https://c/seg1.ts\nhttps://c/fallback.mp4\nhttps://c/master.m3u8\x00" as *u8, 52, best, 4096, kb)
44 pass=pass+grow("R1 [ts,mp4,m3u8] -> picks m3u8 (HLS wins)\x00" as *u8, (kb[0] == XT_HLS) as i64)
45 ttl=ttl+1
46 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)
47 pass=pass+grow("R2 [thumbnail,videoplayback] -> picks the stream (DIRECT)\x00" as *u8, (kb[0] == XT_DIRECT) as i64)
48
49 // --- smart naming: title from og:title / "title": / <title> ---
50 let tb: *u8 = sys_mmap(1024)
51 ttl=ttl+1
52 xt_extract_title("<html><head><meta property=\"og:title\" content=\"My Great Video\"></head></html>" as *u8, 80, tb, 1024)
53 pass=pass+grow("T1 og:title -> 'My Great Video'\x00" as *u8, streq(tb, "My Great Video\x00" as *u8))
54 ttl=ttl+1
55 xt_extract_title("<script>window.d={\"title\":\"JSON Title Here\",\"x\":1}</script>" as *u8, 58, tb, 1024)
56 pass=pass+grow("T2 \"title\": -> 'JSON Title Here'\x00" as *u8, streq(tb, "JSON Title Here\x00" as *u8))
57 ttl=ttl+1
58 xt_extract_title("<html><head><title>Page Title - Site</title></head></html>" as *u8, 58, tb, 1024)
59 pass=pass+grow("T3 <title> -> 'Page Title - Site'\x00" as *u8, streq(tb, "Page Title - Site\x00" as *u8))
60
61 gw("pass=" as *u8); gn(pass); gw("/" as *u8); gn(ttl); gw("\n" as *u8)
62 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
63 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
64 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
65 let ctr__dry: *i64 = gv_ctr()
66 ctr__dry[0] = pass
67 ctr__dry[1] = ttl
68 let rc__dry: i64 = gv_verdict("MEDIA-SIGNAL-GATE" as *u8, ctr__dry, "site-resilient multi-signal media classify + rank + smart-name)" as *u8)
69 sys_exit(rc__dry)
70 return rc__dry
71}