code wiki / (root) / nx_uxf_media_asset_test.nx

nx_uxf_media_asset_test.nx source

↩ module page · 102 lines · 5963 B

1// nx_uxf_media_asset_test.nx -- UXF arc C: a MEDIA asset as a first-class ENVELOPE citizen, end to 2// end. Composes the whole session: a media descriptor {container,vcodec,acodec} is a canon record 3// -> stamped with a UXF_MEDIA self-describing profiled CID (store-once) -> TOLERANT-decoded by a 4// consumer -> drives the endpoint-adaptive negotiation. Proves the "unified" claim: the SAME 5// envelope (content-address + multicodec + tolerant-reader) that carried the migrated TSV rows also 6// carries media + drives "serve-what-the-endpoint-can-consume". No hardware writes (Rule 26). 7// expect_exit: 0 license_tier: ORIGINAL 8import "nx_syscalls.nx" 9import "nx_canon_cid.nx" 10import "nx_uxf_cid.nx" 11import "nx_uxf_decode.nx" 12import "nx_uxf_negotiate.nx" 13 14func t_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 15func t_putn(v: i64) -> i64 { let bb: *u8 = sys_mmap(28); var m: i64 = v; if m < 0 { m = 0 - m; sys_write(1, "-" as *u8, 1) } 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 } var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } sys_write(1, bb, k); return 0 } 16func t_streq(a: *u8, b: *u8) -> i64 { var i: i64 = 0; while 1 == 1 { if a[i] != b[i] { return 0 } if a[i] == (0 as u8) { return 1 } i = i + 1 } return 1 } 17 18// build a media descriptor record {container,vcodec,acodec} -> canon bytes; returns length. 19func mk_descriptor(container: *u8, vcodec: *u8, acodec: *u8, canon: *u8) -> i64 { 20 let keys: *i64 = sys_mmap(64) as *i64 21 let vals: *i64 = sys_mmap(64) as *i64 22 keys[0] = ("container\x00") as i64; vals[0] = (container as i64) 23 keys[1] = ("vcodec\x00") as i64; vals[1] = (vcodec as i64) 24 keys[2] = ("acodec\x00") as i64; vals[2] = (acodec as i64) 25 return canon_encode(keys, vals, 3, canon) 26} 27 28// linear-find a decoded field's value by key name (decoded set is small). 29func find_val(dkeys: *i64, dvals: *i64, nf: i64, key: *u8) -> *u8 { 30 var i: i64 = 0 31 while i < nf { if t_streq((dkeys[i]) as *u8, key) == 1 { return (dvals[i]) as *u8 } i = i + 1 } 32 return 0 as *u8 33} 34 35func main() -> i64 { 36 let lenp: *i64 = sys_mmap(8) as *i64 37 let tsv: *u8 = sys_read_file("knowledge/registry/endpoint_caps.tsv\x00" as *u8, lenp) 38 if (tsv as i64) == 0 { t_puts("endpoint_caps.tsv not found\n" as *u8); sys_exit(1); return 1 } 39 let tn: i64 = lenp[0] 40 let chrome_c: *u8 = sys_mmap(256) 41 let chrome_k: *u8 = sys_mmap(256) 42 ng_caps_lookup(tsv, tn, "chrome\x00" as *u8, chrome_c, chrome_k) 43 44 t_puts("C MEDIA ASSET THROUGH THE ENVELOPE (descriptor -> UXF_MEDIA CID -> decode -> negotiate)\n" as *u8) 45 46 var pass: i64 = 0 47 var total: i64 = 0 48 49 // ---- asset 1: MKV / H.264 / AAC ---- 50 let c1: *u8 = sys_mmap(8192) 51 let l1: i64 = mk_descriptor("mkv\x00" as *u8, "h264\x00" as *u8, "aac\x00" as *u8, c1) 52 let cid1: *u8 = sys_mmap(128) 53 uxf_cid_profiled(UXF_MEDIA, c1, l1, cid1) 54 t_puts(" asset1 CID = " as *u8); t_puts(cid1); t_puts("\n" as *u8) 55 56 // T1: the CID self-describes as a MEDIA profile 57 total = total + 1 58 t_puts(" T1 self-describes as UXF_MEDIA (read profile=" as *u8); t_putn(uxf_codec_of_cid(cid1)); t_puts("): " as *u8) 59 if uxf_codec_of_cid(cid1) == UXF_MEDIA { pass = pass + 1; t_puts("PASS\n" as *u8) } else { t_puts("FAIL\n" as *u8) } 60 61 // consumer: tolerant-decode the stored descriptor, then negotiate from the decoded fields 62 let dk: *i64 = sys_mmap(64) as *i64 63 let dv: *i64 = sys_mmap(64) as *i64 64 let nf: i64 = canon_decode(c1, l1, dk, dv, 16) 65 let cont1: *u8 = find_val(dk, dv, nf, "container\x00" as *u8) 66 let vc1: *u8 = find_val(dk, dv, nf, "vcodec\x00" as *u8) 67 let ac1: *u8 = find_val(dk, dv, nf, "acodec\x00" as *u8) 68 let plan1: i64 = ng_negotiate(cont1, vc1, ac1, chrome_c, chrome_k) 69 total = total + 1 70 t_puts(" T2 full path [decode->negotiate @ chrome] plan=" as *u8); t_putn(plan1); t_puts(" (want REMUX=" as *u8); t_putn(PLAN_REMUX); t_puts("): " as *u8) 71 if plan1 == PLAN_REMUX { pass = pass + 1; t_puts("PASS\n" as *u8) } else { t_puts("FAIL\n" as *u8) } 72 73 // T3: descriptor round-trips through the envelope (re-encode decoded -> same CID) 74 let rt: *u8 = sys_mmap(8192) 75 let rtlen: i64 = canon_encode(dk, dv, nf, rt) 76 let rtcid: *u8 = sys_mmap(128) 77 uxf_cid_profiled(UXF_MEDIA, rt, rtlen, rtcid) 78 total = total + 1 79 t_puts(" T3 descriptor round-trip CID stable: " as *u8) 80 if t_streq(rtcid, cid1) == 1 { pass = pass + 1; t_puts("PASS\n" as *u8) } else { t_puts("FAIL\n" as *u8) } 81 82 // ---- asset 2: WebM / VP9 / Opus -> DIRECT, distinct CID ---- 83 let c2: *u8 = sys_mmap(8192) 84 let l2: i64 = mk_descriptor("webm\x00" as *u8, "vp9\x00" as *u8, "opus\x00" as *u8, c2) 85 let cid2: *u8 = sys_mmap(128) 86 uxf_cid_profiled(UXF_MEDIA, c2, l2, cid2) 87 let dk2: *i64 = sys_mmap(64) as *i64 88 let dv2: *i64 = sys_mmap(64) as *i64 89 let nf2: i64 = canon_decode(c2, l2, dk2, dv2, 16) 90 let plan2: i64 = ng_negotiate(find_val(dk2, dv2, nf2, "container\x00" as *u8), find_val(dk2, dv2, nf2, "vcodec\x00" as *u8), find_val(dk2, dv2, nf2, "acodec\x00" as *u8), chrome_c, chrome_k) 91 total = total + 1 92 t_puts(" T4 asset2 [webm/vp9/opus @ chrome] plan=" as *u8); t_putn(plan2); t_puts(" (want DIRECT=" as *u8); t_putn(PLAN_DIRECT); t_puts("): " as *u8) 93 if plan2 == PLAN_DIRECT { pass = pass + 1; t_puts("PASS\n" as *u8) } else { t_puts("FAIL\n" as *u8) } 94 95 total = total + 1 96 t_puts(" T5 distinct assets -> distinct CIDs: " as *u8) 97 if t_streq(cid1, cid2) == 0 { pass = pass + 1; t_puts("PASS\n" as *u8) } else { t_puts("FAIL\n" as *u8) } 98 99 t_puts("UXF-MEDIA-ASSET-GATE passed " as *u8); t_putn(pass); t_puts("/" as *u8); t_putn(total) 100 if pass == total { t_puts(" verdict=GREEN\n" as *u8); sys_exit(0); return 0 } 101 t_puts(" verdict=RED\n" as *u8); sys_exit(1); return 1 102}