|
| 1 | +import { describe, expect, it } from "bun:test"; |
| 2 | +import { buildStitchedHeader, readMp4Layout, readTracks } from "./mp4Stitch.js"; |
| 3 | + |
| 4 | +function box(type: string, ...body: Buffer[]): Buffer { |
| 5 | + const header = Buffer.alloc(8); |
| 6 | + header.writeUInt32BE(8 + body.reduce((n, part) => n + part.length, 0)); |
| 7 | + header.write(type, 4, "latin1"); |
| 8 | + return Buffer.concat([header, ...body]); |
| 9 | +} |
| 10 | + |
| 11 | +function u32(...values: number[]): Buffer { |
| 12 | + const buf = Buffer.alloc(values.length * 4); |
| 13 | + values.forEach((value, i) => buf.writeUInt32BE(value, i * 4)); |
| 14 | + return buf; |
| 15 | +} |
| 16 | +const full = (type: string, ...body: Buffer[]) => box(type, u32(0), ...body); |
| 17 | +// Version 0 mvhd/tkhd/mdhd with only the fields the stitcher reads filled in. |
| 18 | +const header = (type: string, timescaleOrTrack: number, duration: number) => |
| 19 | + type === "tkhd" |
| 20 | + ? full(type, u32(0, 0, timescaleOrTrack, 0, duration), Buffer.alloc(60)) |
| 21 | + : full(type, u32(0, 0, timescaleOrTrack, duration), Buffer.alloc(type === "mvhd" ? 80 : 4)); |
| 22 | + |
| 23 | +interface FakeTrack { |
| 24 | + handler: "vide" | "soun"; |
| 25 | + timescale: number; |
| 26 | + mediaTime: number; |
| 27 | + durations: number[]; |
| 28 | + sizes: number[]; |
| 29 | + sync?: number[]; |
| 30 | +} |
| 31 | + |
| 32 | +/** A chunk file shaped like ffmpeg's: ftyp, mdat with samples interleaved one per mp4 chunk, moov. */ |
| 33 | +function fakeChunk(tag: number, tracks: FakeTrack[]): { file: Buffer; samples: Buffer[][] } { |
| 34 | + const samples = tracks.map((track, t) => |
| 35 | + track.sizes.map((size, k) => Buffer.alloc(size, (tag * 61 + t * 17 + k * 5 + 1) & 255)), |
| 36 | + ); |
| 37 | + const ftyp = box("ftyp", Buffer.from("isom\0\0\x02\0isom", "latin1")); |
| 38 | + const offsets = tracks.map(() => [] as number[]); |
| 39 | + const payload: Buffer[] = []; |
| 40 | + let at = ftyp.length + 8; |
| 41 | + for (let k = 0; k < Math.max(...tracks.map((track) => track.sizes.length)); k++) { |
| 42 | + samples.forEach((track, t) => { |
| 43 | + if (!track[k]) return; |
| 44 | + offsets[t]!.push(at); |
| 45 | + payload.push(track[k]!); |
| 46 | + at += track[k]!.length; |
| 47 | + }); |
| 48 | + } |
| 49 | + const traks = tracks.map((track, t) => { |
| 50 | + const duration = track.durations.reduce((n, d) => n + d, 0); |
| 51 | + const stbl = box( |
| 52 | + "stbl", |
| 53 | + full("stsd", u32(1), box(track.handler === "vide" ? "avc1" : "mp4a", Buffer.alloc(8, t))), |
| 54 | + full("stts", u32(track.durations.length, ...track.durations.flatMap((d) => [1, d]))), |
| 55 | + ...(track.sync ? [full("stss", u32(track.sync.length, ...track.sync))] : []), |
| 56 | + full("stsc", u32(1, 1, 1, 1)), |
| 57 | + full("stsz", u32(0, track.sizes.length, ...track.sizes)), |
| 58 | + full("stco", u32(offsets[t]!.length, ...offsets[t]!)), |
| 59 | + ); |
| 60 | + return box( |
| 61 | + "trak", |
| 62 | + header("tkhd", t + 1, 0), |
| 63 | + box("edts", full("elst", u32(1, 0, track.mediaTime, 0x10000))), |
| 64 | + box( |
| 65 | + "mdia", |
| 66 | + header("mdhd", track.timescale, duration), |
| 67 | + full("hdlr", u32(0), Buffer.from(track.handler), Buffer.alloc(13)), |
| 68 | + box("minf", stbl), |
| 69 | + ), |
| 70 | + ); |
| 71 | + }); |
| 72 | + const moov = box("moov", header("mvhd", 1000, 0), ...traks); |
| 73 | + return { file: Buffer.concat([ftyp, box("mdat", ...payload), moov]), samples }; |
| 74 | +} |
| 75 | + |
| 76 | +const memoryRange = (file: Buffer) => async (start: number, end: number) => |
| 77 | + file.subarray(start, end); |
| 78 | + |
| 79 | +describe("buildStitchedHeader", () => { |
| 80 | + const video = (sizes: number[], mediaTime = 0): FakeTrack => ({ |
| 81 | + handler: "vide", |
| 82 | + timescale: 15360, |
| 83 | + mediaTime, |
| 84 | + durations: sizes.map(() => 512), |
| 85 | + sizes, |
| 86 | + sync: [1], |
| 87 | + }); |
| 88 | + const audio = (durations: number[], mediaTime: number): FakeTrack => ({ |
| 89 | + handler: "soun", |
| 90 | + timescale: 48000, |
| 91 | + mediaTime, |
| 92 | + durations, |
| 93 | + sizes: durations.map((_, k) => 40 + k), |
| 94 | + }); |
| 95 | + |
| 96 | + it("indexes every chunk's samples at their place after the header", async () => { |
| 97 | + const chunks = [ |
| 98 | + fakeChunk(0, [video([500, 120, 130]), audio([1024, 1024, 1024, 1024], 1024)]), |
| 99 | + fakeChunk(1, [video([480, 110]), audio([1024, 1024, 640], 0)]), |
| 100 | + ]; |
| 101 | + const layouts = await Promise.all(chunks.map((c) => readMp4Layout(memoryRange(c.file)))); |
| 102 | + const header = buildStitchedHeader(layouts); |
| 103 | + const file = Buffer.concat([ |
| 104 | + header, |
| 105 | + ...layouts.map((l, i) => chunks[i]!.file.subarray(l.payloadStart, l.payloadEnd)), |
| 106 | + ]); |
| 107 | + |
| 108 | + const stitched = await readMp4Layout(memoryRange(file)); |
| 109 | + expect(stitched.payloadStart).toBe(header.length); |
| 110 | + expect(stitched.payloadEnd).toBe(file.length); |
| 111 | + const tracks = readTracks(stitched.moov); |
| 112 | + tracks.forEach((track, t) => { |
| 113 | + const read: Buffer[] = []; |
| 114 | + let k = 0; |
| 115 | + track.chunkOffsets.forEach((offset, c) => { |
| 116 | + for (let s = 0, at = offset; s < track.samplesPerChunk[c]!; s++) { |
| 117 | + read.push(file.subarray(at, at + track.sizes[k]!)); |
| 118 | + at += track.sizes[k++]!; |
| 119 | + } |
| 120 | + }); |
| 121 | + expect(read).toEqual(chunks.flatMap((chunk) => chunk.samples[t]!)); |
| 122 | + }); |
| 123 | + expect(tracks.map((track) => [track.handler, track.mediaTime])).toEqual([ |
| 124 | + ["vide", 0], |
| 125 | + ["soun", 1024], |
| 126 | + ]); |
| 127 | + expect(tracks[0]!.sync).toEqual([true, false, false, true, false]); |
| 128 | + expect(tracks[1]!.durations).toEqual([1024, 1024, 1024, 1024, 1024, 1024, 640]); |
| 129 | + |
| 130 | + const moov = stitched.moov; |
| 131 | + const after = (type: string, from = 0) => moov.indexOf(type, from, "latin1") + 4; |
| 132 | + // Only audio needs an edit list (version 1): it shows its media minus the 1024-sample |
| 133 | + // priming, (6784 - 1024) / 48000 s = 120 ms in the 1000-tick movie timescale. |
| 134 | + const audioElst = after("elst"); |
| 135 | + expect(moov.indexOf("elst", audioElst, "latin1")).toBe(-1); |
| 136 | + expect(audioElst).toBeGreaterThan(after("vide")); |
| 137 | + expect(Number(moov.readBigUInt64BE(audioElst + 8))).toBe(120); |
| 138 | + expect(Number(moov.readBigInt64BE(audioElst + 16))).toBe(1024); |
| 139 | + expect(moov.readUInt32BE(after("mdhd", audioElst) + 16)).toBe(6784); |
| 140 | + expect(moov.readUInt32BE(after("mvhd") + 16)).toBe(167); |
| 141 | + }); |
| 142 | + |
| 143 | + it("refuses a chunk whose video starts late, as B-frames make it", async () => { |
| 144 | + const chunks = [fakeChunk(0, [video([500, 120])]), fakeChunk(1, [video([480, 110], 512)])]; |
| 145 | + const layouts = await Promise.all(chunks.map((c) => readMp4Layout(memoryRange(c.file)))); |
| 146 | + expect(() => buildStitchedHeader(layouts)).toThrow("chunk 1 video starts at media time 512"); |
| 147 | + }); |
| 148 | +}); |
0 commit comments