SPB Git

Repository Public License

Support.swift 17.3 KB 285 lines swift

1// ORIX Convert — Packages/EngineMedia/Tests/EngineMediaTests/Support.swift2// Description: Test support: fixtures generated at test time with the bundled ffmpeg (testsrc2, sine, noise) and by3//              the engines themselves, plus helpers that convert, validate and collect warnings.4// Author: Simon-Pierre Boucher — contact@spboucher.ai5import Foundation6import Testing7import ConvertCore8@testable import EngineMedia910struct TestFailure: Error, CustomStringConvertible { let description: String }1112final class Collector<T: Sendable>: @unchecked Sendable {13    private let lock = NSLock()14    private var items: [T] = []15    func add(_ item: T) { lock.lock(); items.append(item); lock.unlock() }16    var all: [T] { lock.lock(); defer { lock.unlock() }; return items }17}1819struct Converted: Sendable {20    let output: ConversionOutput21    let url: URL22    let checks: [ValidationCheck]23    let warnings: [ConversionWarning]24    let progress: [Double?]25    let log: [String]26    var facts: [String: String] { output.facts }27    func hasWarning(_ code: String) -> Bool { warnings.contains { $0.code == code } }28}2930enum T {31    static let root: URL = {32        let url = FileManager.default.temporaryDirectory.appendingPathComponent("orix-enginemedia-tests-\(ProcessInfo.processInfo.processIdentifier)", isDirectory: true)33        try? FileManager.default.removeItem(at: url)34        try? FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)35        return url36    }()3738    static func newDirectory(_ label: String = "work") -> URL {39        let url = root.appendingPathComponent("\(label)-\(UUID().uuidString.prefix(8))", isDirectory: true)40        try? FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)41        return url42    }4344    /// Runs the bundled ffmpeg directly (fixture generation and independent measurements).45    @discardableResult46    static func ffmpeg(_ args: [String], timeout: TimeInterval = 300) async throws -> ProcessResult {47        let tool = try BundledTools.require("ffmpeg")48        let result = try await ProcessRunner.run(tool, arguments: ["-hide_banner", "-nostdin", "-y", "-loglevel", "error"] + args,49                                                 environment: ["SVT_LOG": "1"], timeout: timeout, captureLimit: 256 << 20)50        guard result.status == 0 else { throw TestFailure(description: "ffmpeg failed (\(result.status)): \(result.stderrTail(6))\nargs: \(args)") }51        return result52    }5354    static func engine(for path: ConversionPath) -> any ConversionEngine { path.engine == "audio" ? AudioEngine() : VideoEngine() }5556    /// inspect → preflight → convert → validate, in a fresh work directory.57    static func convert(_ source: URL, _ path: ConversionPath, options: [String: OptionValue] = [:], baseName: String? = nil,58                        expectValid: Bool = true, sourceLocation: SourceLocation = #_sourceLocation) async throws -> Converted {59        let engine = engine(for: path)60        let work = newDirectory("out")61        let request = ConversionRequest(source: source, sourceFormat: path.source, destinationFormat: path.destination, operation: path.operation,62                                        options: ConversionOptions(options), outputBaseName: baseName)63        let inspection = try await engine.inspect(source, format: path.source)64        let warnings = Collector<ConversionWarning>(), progress = Collector<Double?>(), log = Collector<String>()65        let context = ConversionContext(workDirectory: work, progress: { progress.add($0.fraction) }, log: { _, m in log.add(m) }, warn: { warnings.add($0) })66        let output = try await engine.convert(request, context: context)67        guard let url = output.files.first else { throw TestFailure(description: "no output file for \(path.id)") }68        #expect(url.path.hasPrefix(work.path), "output must be inside the work directory", sourceLocation: sourceLocation)69        let size = (try? url.resourceValues(forKeys: [.fileSizeKey]))?.fileSize ?? 070        #expect(size > 0, "empty output for \(path.id)", sourceLocation: sourceLocation)71        let checks = try await engine.validate(output, request: request, source: inspection)72        if expectValid {73            #expect(!checks.isEmpty, "no validation checks for \(path.id)", sourceLocation: sourceLocation)74            for check in checks where !check.passed {75                Issue.record("\(path.id) [\(options)]: validation failed — \(check.name): \(check.detail)", sourceLocation: sourceLocation)76            }77        }78        for f in progress.all.compactMap({ $0 }) where !(0...1).contains(f) {79            Issue.record("progress fraction out of range: \(f)", sourceLocation: sourceLocation)80        }81        return Converted(output: output, url: url, checks: checks, warnings: warnings.all, progress: progress.all, log: log.all)82    }8384    static func videoPath(_ s: FormatID, _ d: FormatID, _ op: String? = nil) throws -> ConversionPath {85        guard let p = VideoEngine().paths.first(where: { $0.source == s && $0.destination == d && $0.operation == op }) else {86            throw TestFailure(description: "video path \(s)>\(d) \(op ?? "") is not declared")87        }88        return p89    }9091    static func audioPath(_ s: FormatID, _ d: FormatID) throws -> ConversionPath {92        guard let p = AudioEngine().paths.first(where: { $0.source == s && $0.destination == d }) else {93            throw TestFailure(description: "audio path \(s)>\(d) is not declared")94        }95        return p96    }9798    /// MD5 of the decoded frames/samples of a stream, computed by ffmpeg independently of the engine.99    static func decodedMD5(_ url: URL, stream: String, extra: [String] = []) async throws -> String {100        let r = try await ffmpeg(["-i", "file:" + url.path, "-map", "0:\(stream)"] + extra + ["-f", "md5", "-"])101        return r.stdoutString.trimmingCharacters(in: .whitespacesAndNewlines)102    }103104    /// MD5 of the coded packets of a stream, copied without decoding. AAC is normalised to raw frames first, because105    /// transport streams wrap each frame in an ADTS header that MP4-family containers do not store.106    static func packetMD5(_ url: URL, stream: String, codec: String?) async throws -> String {107        let bsf = codec == "aac" ? ["-bsf:a", "aac_adtstoasc"] : []108        let r = try await ffmpeg(["-i", "file:" + url.path, "-map", "0:\(stream)", "-c", "copy"] + bsf + ["-f", "hash", "-hash", "md5", "-"])109        return r.stdoutString.trimmingCharacters(in: .whitespacesAndNewlines)110    }111112    /// Decoded audio as interleaved 16-bit samples.113    static func pcm16(_ url: URL, stream: String = "a:0") async throws -> [Int16] {114        let r = try await ffmpeg(["-i", "file:" + url.path, "-map", "0:\(stream)", "-c:a", "pcm_s16le", "-f", "s16le", "-"])115        return r.stdout.withUnsafeBytes { Array($0.bindMemory(to: Int16.self)) }116    }117118    static func probe(_ url: URL, countPackets: Bool = false) async throws -> MediaInfo { try await Probe.media(url, countPackets: countPackets) }119}120121/// Fixtures, generated once per test process.122actor Fixtures {123    static let shared = Fixtures()124    private var cache: [String: URL] = [:]125    private var inFlight: [String: Task<URL, Error>] = [:]126    let dir = T.newDirectory("fixtures")127128    private func make(_ key: String, _ body: @escaping @Sendable (URL) async throws -> URL) async throws -> URL {129        if let url = cache[key] { return url }130        if let task = inFlight[key] { return try await task.value }131        let dir = self.dir132        let task = Task { try await body(dir) }133        inFlight[key] = task134        defer { inFlight[key] = nil }135        let url = try await task.value136        cache[key] = url137        return url138    }139140    /// 3 s, 320×240, 30 fps H.264 + stereo AAC 48 kHz, with a title tag.141    func masterVideo() async throws -> URL {142        try await make("master.mp4") { dir in143            let url = dir.appendingPathComponent("master.mp4")144            try await T.ffmpeg(["-filter_complex", "testsrc2=size=320x240:rate=30:duration=3[v];sine=frequency=440:sample_rate=48000:duration=3,aformat=channel_layouts=stereo[a]",145                                "-map", "[v]", "-map", "[a]", "-c:v", "h264_videotoolbox", "-q:v", "65", "-g", "15", "-pix_fmt", "yuv420p", "-c:a", "aac_at", "-b:a", "128k",146                                "-metadata", "title=ORIX master clip", "-f", "mp4", "file:" + url.path])147            return url148        }149    }150151    /// A source in each container, with given codecs (default: the container's first codecs), made by the engine.152    func video(_ format: FormatID, videoCodec: String? = nil, audioCodec: String? = nil) async throws -> URL {153        if format == .mp4, videoCodec == nil, audioCodec == nil { return try await masterVideo() }154        let master = try await masterVideo()155        let key = "video-\(format.rawValue)-\(videoCodec ?? "default")-\(audioCodec ?? "default")"156        return try await make(key) { dir in157            var options: [String: OptionValue] = [:]158            if let v = videoCodec { options["video.codec"] = .string(v) }159            if let a = audioCodec { options["audio.codec"] = .string(a) }160            let out = try await T.convert(master, try T.videoPath(.mp4, format), options: options, baseName: key, expectValid: false)161            let ext = FormatCatalog.format(format)?.preferredExtension ?? format.rawValue162            let url = dir.appendingPathComponent(key).appendingPathExtension(ext)163            try FileManager.default.moveItem(at: out.url, to: url)164            return url165        }166    }167168    /// Matroska with H.264, two audio tracks (stereo + 5.1), SRT and ASS subtitle tracks, 2 chapters and tags.169    func richMKV() async throws -> URL {170        let master = try await masterVideo()171        return try await make("rich.mkv") { dir in172            let srt = dir.appendingPathComponent("subs.srt")173            try "1\n00:00:00,200 --> 00:00:01,000\nHello ORIX\n\n2\n00:00:01,200 --> 00:00:02,500\nSecond line\n\n".write(to: srt, atomically: true, encoding: .utf8)174            let ass = dir.appendingPathComponent("subs.ass")175            try """176            [Script Info]177            ScriptType: v4.00+178            PlayResX: 320179            PlayResY: 240180181            [V4+ Styles]182            Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding183            Style: Default,Helvetica,20,&H0000FFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,2,2,10,10,10,1184185            [Events]186            Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text187            Dialogue: 0,0:00:00.30,0:00:01.10,Default,,0,0,0,,Styled {\\b1}one{\\b0}188            Dialogue: 0,0:00:01.50,0:00:02.60,Default,,0,0,0,,Styled two189190            """.write(to: ass, atomically: true, encoding: .utf8)191            let meta = dir.appendingPathComponent("chapters.txt")192            try ";FFMETADATA1\ntitle=Rich fixture\nartist=ORIX Tests\n[CHAPTER]\nTIMEBASE=1/1000\nSTART=0\nEND=1500\ntitle=Opening\n[CHAPTER]\nTIMEBASE=1/1000\nSTART=1500\nEND=3000\ntitle=Ending\n"193                .write(to: meta, atomically: true, encoding: .utf8)194            let url = dir.appendingPathComponent("rich.mkv")195            try await T.ffmpeg(["-i", "file:" + master.path, "-f", "srt", "-i", "file:" + srt.path, "-f", "ass", "-i", "file:" + ass.path, "-f", "ffmetadata", "-i", "file:" + meta.path,196                                "-filter_complex", "sine=frequency=660:sample_rate=48000:duration=3,aformat=channel_layouts=5.1[s]",197                                "-map", "0:v:0", "-map", "0:a:0", "-map", "[s]", "-map", "1:0", "-map", "2:0", "-map_metadata", "3", "-map_chapters", "3",198                                "-c:v", "copy", "-c:a:0", "copy", "-c:a:1", "ac3", "-b:a:1", "448k", "-c:s:0", "subrip", "-c:s:1", "ass",199                                "-metadata:s:s:0", "language=eng", "-metadata:s:s:1", "language=fra", "-metadata:s:a:1", "language=fra",200                                "-f", "matroska", "file:" + url.path])201            return url202        }203    }204205    /// A container holding one SRT-derived text subtitle track, converted from `richMKV` by the engine.206    func subtitled(_ format: FormatID) async throws -> URL {207        let rich = try await richMKV()208        if format == .mkv { return rich }209        return try await make("subtitled-\(format.rawValue)") { dir in210            let out = try await T.convert(rich, try T.videoPath(.mkv, format), options: ["audio.tracks": .string("first")], baseName: "subtitled", expectValid: false)211            let url = dir.appendingPathComponent("subtitled").appendingPathExtension(FormatCatalog.format(format)?.preferredExtension ?? format.rawValue)212            try FileManager.default.moveItem(at: out.url, to: url)213            return url214        }215    }216217    /// HEVC Main 10 tagged BT.2020 / PQ (HDR10 signalling), 2 s.218    func hdrVideo() async throws -> URL {219        try await make("hdr.mp4") { dir in220            let url = dir.appendingPathComponent("hdr.mp4")221            try await T.ffmpeg(["-filter_complex", "testsrc2=size=320x240:rate=30:duration=2,format=p010le,setparams=color_primaries=bt2020:color_trc=smpte2084:colorspace=bt2020nc:range=tv[v]",222                                "-map", "[v]", "-c:v", "hevc_videotoolbox", "-q:v", "65", "-profile:v", "main10", "-tag:v", "hvc1", "-f", "mp4", "file:" + url.path])223            return url224        }225    }226227    /// 3 s stereo 44.1 kHz PCM: two sines plus noise, so that every sample matters. `bits` is 16 or 24.228    func masterAudio(bits: Int = 16) async throws -> URL {229        try await make("master\(bits).wav") { dir in230            let url = dir.appendingPathComponent("master\(bits).wav")231            try await T.ffmpeg(["-filter_complex",232                                "sine=frequency=440:sample_rate=44100:duration=3[a];sine=frequency=1000:sample_rate=44100:duration=3[b];anoisesrc=sample_rate=44100:duration=3:amplitude=0.05:seed=7,asplit[n1][n2];"233                                + "[a][n1]amix=inputs=2:normalize=0[l];[b][n2]amix=inputs=2:normalize=0[r];[l][r]join=inputs=2:channel_layout=stereo,volume=0.5[o]",234                                "-map", "[o]", "-c:a", bits == 24 ? "pcm_s24le" : "pcm_s16le", "-f", "wav", "file:" + url.path])235            return url236        }237    }238239    /// The master audio in each audio format, made by the engine from the 16-bit WAV.240    func audio(_ format: FormatID) async throws -> URL {241        let master = try await masterAudio()242        if format == .wav { return master }243        return try await make("audio-\(format.rawValue)") { dir in244            let out = try await T.convert(master, try T.audioPath(.wav, format), baseName: "audio-\(format.rawValue)", expectValid: false)245            let url = dir.appendingPathComponent("audio-\(format.rawValue)").appendingPathExtension(FormatCatalog.format(format)?.preferredExtension ?? format.rawValue)246            try FileManager.default.moveItem(at: out.url, to: url)247            return url248        }249    }250251    /// FLAC with a full set of tags and a PNG cover.252    func taggedFLAC() async throws -> URL {253        let master = try await masterAudio()254        return try await make("tagged.flac") { dir in255            let cover = dir.appendingPathComponent("cover.png")256            try await T.ffmpeg(["-filter_complex", "testsrc2=size=96x96:rate=1:duration=1[v]", "-map", "[v]", "-frames:v", "1", "-c:v", "png", "-f", "rawvideo", "file:" + cover.path])257            let url = dir.appendingPathComponent("tagged.flac")258            try await T.ffmpeg(["-i", "file:" + master.path, "-f", "png_pipe", "-i", "file:" + cover.path, "-map", "0:a", "-map", "1:v", "-c:a", "flac", "-c:v", "copy",259                                "-disposition:v:0", "attached_pic", "-metadata", "title=Test Title", "-metadata", "artist=Test Artist", "-metadata", "album=Test Album",260                                "-metadata", "date=2026", "-metadata", "track=7", "-metadata", "genre=Electronic", "-metadata", "comment=Made by tests",261                                "-f", "flac", "file:" + url.path])262            return url263        }264    }265266    /// A Standard MIDI File: piano note at 1.0–2.0 s, snare (channel 10) at 2.0–2.5 s, 120 bpm → 2.5 s long.267    func midi() async throws -> URL {268        try await make("notes.mid") { dir in269            var track: [UInt8] = []270            track += [0x00, 0xFF, 0x51, 0x03, 0x07, 0xA1, 0x20]          // tempo 500000 µs per quarter271            track += [0x00, 0xC0, 0x00]                                  // channel 1: acoustic grand piano272            track += [0x87, 0x40, 0x90, 60, 100]                         // +960 ticks = 2 quarters (1.0 s): note on C4273            track += [0x87, 0x40, 0x80, 60, 0]                           // +960 ticks (2.0 s): note off274            track += [0x00, 0x99, 38, 110]                               // channel 10: snare on275            track += [0x83, 0x60, 0x89, 38, 0]                           // +480 ticks (2.5 s): snare off276            track += [0x00, 0xFF, 0x2F, 0x00]                            // end of track277            var data: [UInt8] = [0x4D, 0x54, 0x68, 0x64, 0, 0, 0, 6, 0, 0, 0, 1, 0x01, 0xE0]   // format 0, 1 track, 480 tpq278            data += [0x4D, 0x54, 0x72, 0x6B, 0, 0, UInt8(track.count >> 8), UInt8(track.count & 0xFF)] + track279            let url = dir.appendingPathComponent("notes.mid")280            try Data(data).write(to: url)281            return url282        }283    }284}285
Files main
Menu

Theme