//! The media clock of a VIDEO node through the C ABI: what the node shows and reports follows one //! clock across play, pause, seek, loop, end and source replacement. A seeded script of commands //! runs on one node; the model keeps the media time as an interval and checks every state read //! and every paint against it. //! //! Commands and observations go through ot_video_* and scene paint. Only the clock is internal, //! because the ABI has no clock to inject: advance moves the wall clock by shifting the player's //! start time (as the loop rows in video_test.zig do), and pulls the audio clock by mixing the //! node's audio engine offline (no device opens). The wall clock still runs between commands, so //! the model's upper bound grows with the real time since its last anchor; the audio clock and a //! paused clock are exact. const std = @import("std"); const c = @import("context_abi_c"); const model = @import("model.zig"); const audio = @import("../audio.zig"); const video = @import("../video.zig"); /// The library under test: the opentui.h functions the model calls. const ot = struct { const abi = @import("../context-abi.zig"); const media = @import("../context-video-abi.zig"); pub const ContextHandle = abi.ContextHandle; pub const ot_context_create = abi.ot_context_create; pub const ot_context_destroy = abi.ot_context_destroy; pub const ot_session_create = abi.ot_session_create; pub const ot_session_attach_renderer = abi.ot_session_attach_renderer; pub const ot_session_read_output = abi.ot_session_read_output; pub const ot_session_complete_output = abi.ot_session_complete_output; pub const ot_scene_create_node = abi.ot_scene_create_node; pub const ot_scene_move_node = abi.ot_scene_move_node; pub const ot_scene_set_style = abi.ot_scene_set_style; pub const ot_scene_paint = abi.ot_scene_paint; pub const ot_scene_frame_commit = abi.ot_scene_frame_commit; pub const ot_scene_set_video = media.ot_scene_set_video; pub const ot_video_open = media.ot_video_open; pub const ot_video_play = media.ot_video_play; pub const ot_video_pause = media.ot_video_pause; pub const ot_video_seek = media.ot_video_seek; pub const ot_video_set_loop = media.ot_video_set_loop; pub const ot_video_set_muted = media.ot_video_set_muted; pub const ot_video_set_av_sync_offset = media.ot_video_set_av_sync_offset; pub const ot_video_set_max_fps = media.ot_video_set_max_fps; pub const ot_video_get_state = media.ot_video_get_state; }; /// The test clock: the player behind a node, the wall clock and the offline audio mixer. const clock = struct { fn player(context: *ot.ContextHandle, node: c.ot_handle) *video.Player { const renderable = context.core.raw().getRenderable(ot.abi.handleFromC(node)) catch unreachable; return renderable.scene_node.?.control.video.player.?; } fn now() i64 { return @intCast(std.Io.Clock.now(.awake, std.testing.io).toMicroseconds()); } /// Waits until the stream can play frames without an underrun. Returns true when the input /// ended first: the mix may then drain the stream and hand the clock to the wall. fn ready(value: *video.Video, frames: u32) !bool { for (0..5000) |_| { value.engine_lock.lockUncancelable(std.testing.io); var stats: audio.StreamStats = undefined; const known = value.audio_stream != 0 and audio.getStreamStats(value.audio_engine.?, value.audio_stream, &stats) == audio.Status.ok; const ended = value.audio_ended; const produced = value.audio_produced_frames; value.engine_lock.unlock(std.testing.io); if (known and stats.buffered_frames >= @max(frames, 12_000)) return false; if (known and ended and stats.frames_decoded >= produced) return true; std.Io.sleep(std.testing.io, .fromMilliseconds(1), .awake) catch {}; } return error.AudioStalled; } /// Mixes as a device would, in steps the queue holds. Returns whether the stream may have drained. fn mix(value: *video.Video, frames: u32, wait: bool) !bool { var output: [4800 * 2]f32 = undefined; var left = frames; var drained = false; while (left > 0) { const step: u32 = @min(left, 4800); if (wait and !drained) drained = try ready(value, step); if (value.mixOffline(output[0 .. step * 2], step) != audio.Status.ok) return error.MixFailed; left -= step; } return drained; } }; const Source = struct { path: []const u8, flags: u32 }; const sources = [_]Source{ .{ .path = "../core/src/tests/fixtures/video/testsrc-160x90-24fps-2s.mp4", .flags = 0 }, .{ .path = "../core/src/tests/fixtures/video/testsrc-160x90-24fps-2s.mp4", .flags = c.OT_VIDEO_OPEN_NO_AUDIO }, .{ .path = "../core/src/tests/fixtures/video/late-audio-160x90-0.5s.mp4", .flags = 0 }, }; /// One frame of the 24 fps fixtures, rounded up. const frame_us: i64 = 41_667; /// How far ahead of the clock a painted frame may be: the measured output latency. const ahead_us: i64 = 100_000; const rate: i64 = video.audio_sample_rate; /// How far before the end of a clip its audio may end. const drain_us: i64 = 300_000; const Op = union(enum) { open: u8, close, play, pause, /// A kind of target (see seekTarget) and a raw draw for its position. seek: struct { kind: u8, raw: u32 }, loop: u8, muted: bool, offset: i32, max_fps: u8, /// Media time to advance by, in microseconds. advance: u32, /// Presentation intervals of steady playback. tick: u8, paint, read, }; /// What the media time can be: [lo, hi], where hi grows with the real time since the anchor /// while the wall clock runs. const Time = struct { lo: i64 = 0, hi: i64 = 0, wall: bool = false, since: i64 = 0, fn exact(value: i64) Time { return .{ .lo = value, .hi = value }; } fn upper(self: Time, at: i64) i64 { return if (self.wall) self.hi + (at - self.since) else self.hi; } }; /// Which clock leads while playing: either after a mix that may have drained the audio. const Regime = enum { wall, audio, either }; const Model = struct { source: ?u8 = null, duration: i64 = 0, engine: bool = false, playing: bool = false, ended: bool = false, loop: bool = false, offset: i64 = 0, time: Time = .{}, regime: Regime = .wall, /// Set by an open or a seek to target: the next paint shows a frame between the target and /// the clock (a seek may decode its frame at once), or the last frame at the end. seek_frame: bool = false, target: i64 = 0, /// What the last read reported, until a seek or open; only a loop goes back past it. last: ?i64 = null, max_fps: i64 = 30, /// A pause near the end, which may also have ended or looped playback. pausing: bool = false, /// How far the offset fell since the last seek: the frame on screen is not decoded again. lowered: i64 = 0, fn anchor(self: *Model, value: i64, at: i64) void { self.time = Time.exact(value); self.ended = false; self.last = null; self.seek_frame = true; self.target = value; self.lowered = 0; self.regime = if (self.engine) .audio else .wall; if (self.playing) self.run(at); } /// Starts the clock: the wall clock from at, or the exact audio clock. Near the end the audio /// may have drained already, and the wall clock leads. fn run(self: *Model, at: i64) void { if (self.regime == .audio and self.time.hi >= self.duration - drain_us) self.regime = .either; self.time.since = at; self.time.wall = self.regime != .audio; } fn hold(self: *Model, at: i64) void { self.time.hi = self.time.upper(at); self.time.wall = false; } }; const Run = struct { context: *ot.ContextHandle, /// Whether the known blank frame after a seek to the end passes. gate: bool, session: c.ot_handle = undefined, node: c.ot_handle = undefined, model: Model = .{}, fn init(context: *ot.ContextHandle, gate: bool) !Run { var self: Run = .{ .context = context, .gate = gate }; var options = record(c.ot_session_options); options.chunk_size = 8192; options.span_capacity = 8; options.max_bytes = 1 << 20; try ok(ot.ot_session_create(context, &options, &self.session)); var renderer = record(c.ot_session_renderer_options); renderer.width = 8; renderer.height = 4; renderer.remote = 1; try ok(ot.ot_session_attach_renderer(context, &self.session, &renderer)); var root: c.ot_handle = undefined; try ok(ot.ot_scene_create_node(context, &self.session, c.OT_SCENE_ROOT, 1, &root)); try ok(ot.ot_scene_create_node(context, &self.session, c.OT_SCENE_VIDEO, 2, &self.node)); for ([_]u32{ c.OT_DIMENSION_WIDTH, c.OT_DIMENSION_HEIGHT }, [_]f32{ 8, 4 }) |kind, value| { try ok(ot.ot_scene_set_style(context, &self.node, c.OT_STYLE_DIMENSION, kind, c.OT_EDGE_NONE, c.OT_UNIT_POINT, value, c.OT_STYLE_DISABLE_FLEX_SHRINK)); } try ok(ot.ot_scene_move_node(context, &self.node, &root, 0)); try ok(ot.ot_scene_set_video(context, &self.node, c.OT_IMAGE_FILL, null)); return self; } fn player(self: *Run) *video.Player { return clock.player(self.context, self.node); } /// Waits until the audio thread applied the last control, so a mix sees it. fn settle(self: *Run) void { if (self.model.engine) self.player().video.syncAudio(); } /// Controls on a node without a source are rejected. fn closed(actual: c.ot_status) ?[]const u8 { model.count("rejected"); return expectStatus(actual, c.OT_INVALID_PHASE); } fn apply(self: *Run, command: Op) !?[]const u8 { const m = &self.model; switch (command) { .open => |index| { const source = sources[index]; var info: c.ot_video_info = undefined; try ok(ot.ot_video_open(self.context, &self.node, source.path.ptr, @intCast(source.path.len), source.flags, &info)); const engine = info.has_audio != 0 and source.flags == 0; if (engine) self.player().video.setAudioOffline(true); model.count(if (m.source == null) "open" else if (m.playing) "open replaces playing" else "open replaces"); m.* = .{ .source = index, .duration = info.duration_us, .engine = engine }; m.anchor(0, 0); var state: c.ot_video_state = undefined; try ok(ot.ot_video_get_state(self.context, &self.node, &state)); // A new source starts paused at 0 with no frame, whatever the old one showed. if (state.current_time_us != 0 or state.frame_pts_us != -1 or state.flags != 0) return "fresh-source"; m.last = 0; }, .close => { var info: c.ot_video_info = undefined; try ok(ot.ot_video_open(self.context, &self.node, null, 0, 0, &info)); m.* = .{}; model.count("close"); }, .play => { if (m.source == null) return closed(ot.ot_video_play(self.context, &self.node)); if (try self.read()) |failure| return failure; const at = clock.now(); if (expectStatus(ot.ot_video_play(self.context, &self.node), c.OT_OK)) |failure| return failure; self.settle(); if (m.playing) { model.count("play or pause again"); return null; } model.count(if (m.ended) "play after end" else "play"); m.playing = true; if (m.ended and !m.loop) m.anchor(0, at) else m.run(at); m.ended = false; }, .pause => { if (m.source == null) return closed(ot.ot_video_pause(self.context, &self.node)); if (try self.read()) |failure| return failure; if (expectStatus(ot.ot_video_pause(self.context, &self.node), c.OT_OK)) |failure| return failure; self.settle(); model.count(if (m.playing) "pause" else "play or pause again"); if (!m.playing) return null; // The pause's own tick may still end or loop playback: a read settles it. if (m.time.upper(clock.now()) >= m.duration) { m.pausing = true; return self.read(); } m.playing = false; m.hold(clock.now()); }, .seek => |value| { if (m.source == null) return closed(ot.ot_video_seek(self.context, &self.node, 0)); if (m.playing) if (try self.read()) |failure| return failure; const target = seekTarget(value.kind, value.raw, m.duration); const at = clock.now(); if (expectStatus(ot.ot_video_seek(self.context, &self.node, target), c.OT_OK)) |failure| return failure; self.settle(); model.count(if (m.playing) "seek playing" else "seek paused"); if (target >= m.duration or target < 0) model.count("seek outside the clip"); m.anchor(std.math.clamp(target, 0, m.duration), at); // The first read after a seek reports the target. var state: c.ot_video_state = undefined; return self.observe(&state, "seek-time"); }, .loop => |value| { if (m.source == null) return closed(ot.ot_video_set_loop(self.context, &self.node, value)); if (m.playing) if (try self.read()) |failure| return failure; if (expectStatus(ot.ot_video_set_loop(self.context, &self.node, value), if (value > 1) c.OT_INVALID_ARGUMENT else c.OT_OK)) |failure| return failure; if (value <= 1) m.loop = value == 1; model.count(if (value > 1) "rejected" else "loop"); }, .muted => |value| { if (m.source == null) return closed(ot.ot_video_set_muted(self.context, &self.node, @intFromBool(value))); if (expectStatus(ot.ot_video_set_muted(self.context, &self.node, @intFromBool(value)), c.OT_OK)) |failure| return failure; self.settle(); model.count("muted"); }, .offset => |value| { const offset: i64 = if (value == std.math.maxInt(i32)) video.max_av_sync_offset_us + 1 else value; if (m.source == null) return closed(ot.ot_video_set_av_sync_offset(self.context, &self.node, offset)); const valid = @abs(offset) <= video.max_av_sync_offset_us; if (expectStatus(ot.ot_video_set_av_sync_offset(self.context, &self.node, offset), if (valid) c.OT_OK else c.OT_INVALID_ARGUMENT)) |failure| return failure; if (valid) m.lowered += @max(0, m.offset - offset); if (valid) m.offset = offset; // A frame a seek already decoded keeps the old offset. if (valid) m.seek_frame = false; model.count(if (valid) "offset" else "rejected"); }, .max_fps => |value| { if (m.source == null) return closed(ot.ot_video_set_max_fps(self.context, &self.node, value)); const valid = value >= 1 and value <= 120; if (expectStatus(ot.ot_video_set_max_fps(self.context, &self.node, value), if (valid) c.OT_OK else c.OT_INVALID_ARGUMENT)) |failure| return failure; if (valid) m.max_fps = value; model.count(if (valid) "max fps" else "rejected"); }, .advance => |us| return self.advance(us), .tick => |intervals| return self.tick(intervals), .paint => return self.paint(), .read => { if (m.source == null) { var state: c.ot_video_state = undefined; return expectStatus(ot.ot_video_get_state(self.context, &self.node, &state), c.OT_INVALID_PHASE); } return self.read(); }, } return null; } /// Moves the clock that leads by us: the audio engine's when audio leads, else the wall's. fn advance(self: *Run, us: u32) !?[]const u8 { const m = &self.model; if (m.source == null) return null; var state: c.ot_video_state = undefined; if (try self.observe(&state, "clock")) |failure| return failure; const value = self.player(); const frames: i64 = @divTrunc(@as(i64, us) * rate, 1_000_000); if (state.flags & (c.OT_VIDEO_AUDIO | c.OT_VIDEO_BUFFERING) != 0) { const at = clock.now(); const drained = try clock.mix(value.video, @intCast(frames), true); // Audio time is the stream's base plus its played frames in whole microseconds, so the // frames move it by their length, or 1 us more. const step = @divTrunc(frames * 1_000_000, rate); m.time.hi += step + 1; if (drained) { // The stream may end within the mix; the wall clock then goes on from its end. model.count("advance audio to its end"); m.regime = .either; m.time.wall = true; m.time.since = at; } else { model.count("advance audio"); m.time.lo += step; } return null; } // A paused engine mixes nothing, so its clock holds too. if (m.engine and !m.playing) _ = try clock.mix(value.video, @intCast(frames), false); value.clock_start_us -= us; model.count(if (m.playing) "advance wall" else "advance paused"); if (m.playing) { m.time.lo += us; m.time.hi += us; } return null; } fn paint(self: *Run) !?[]const u8 { const m = &self.model; var request = record(c.ot_scene_frame_request); const black = [4]u16{ 0, 0, 0, 255 }; const window = [2]i64{ m.target, m.time.upper(clock.now()) }; try ok(ot.ot_scene_paint(self.context, &self.session, &black, 0, 0, &request)); if (request.kind != c.OT_SCENE_FRAME_DONE) return error.FrameNotDone; var result: u32 = undefined; try ok(ot.ot_scene_frame_commit(self.context, &self.session, &request, 0, &result)); var chunk: [4096]u8 = undefined; while (true) { var ticket: c.ot_output_ticket = undefined; try ok(ot.ot_session_read_output(self.context, &self.session, &chunk, chunk.len, &ticket)); if (ticket.byte_count == 0) break; try ok(ot.ot_session_complete_output(self.context, &self.session, &ticket, 1)); } if (m.source == null) return null; model.count("paint"); const expect = m.seek_frame; const was_playing = m.playing; const top = m.time.upper(clock.now()); var state: c.ot_video_state = undefined; if (try self.observe(&state, "clock")) |failure| return failure; if (state.flags & c.OT_VIDEO_PLAYING != 0 and state.frame_pts_us > @max(0, top + m.offset) + ahead_us + m.lowered) return "frame-ahead"; if (!expect) return null; m.seek_frame = false; // A tick at the end may have looped playback; the frame then follows the new position. if (was_playing and !m.ended and top >= m.duration) return null; model.count("frame after seek"); // A seek to the end shows the last frame, as playback that runs into the end does. If // playback ended since the seek, the frame lies between the target and the end. const end = window[0] >= m.duration; if (self.gate and (end or m.ended) and state.frame_pts_us == -1 and state.flags & c.OT_VIDEO_HAS_FRAME == 0) { // Known defect: a decode at the end shows no frame. model.count("gated no frame at end"); return null; } const lo = std.math.clamp(window[0] + m.offset, 0, m.duration - 1); const hi = if (end or m.ended) m.duration - 1 else std.math.clamp(top + m.offset, 0, m.duration - 1); if (state.frame_pts_us < 0 or state.frame_pts_us > hi or state.frame_pts_us <= lo - frame_us - 1) { if (self.gate and m.offset < 0 and state.frame_pts_us > hi and state.frame_pts_us <= top) { // Known defect: a seek positions the decoder at the clock, not at the clock plus a // negative offset, so it shows a frame between the two. model.count("gated seek past negative offset"); return null; } return "seek-frame"; } return null; } /// Intervals of playback as a renderer paints it: each moves the clock by one presentation /// interval, posts the next frame with a paint, lets the decode worker finish it, and /// presents it with a paint when due. fn tick(self: *Run, intervals: u8) !?[]const u8 { const m = &self.model; for (0..intervals) |index| { if (m.source == null) return null; const fps: i64 = @min(24, m.max_fps); const interval = @divTrunc(1_000_000 + fps - 1, fps); if (try self.advance(@intCast(interval))) |failure| return failure; if (try self.paint()) |failure| return failure; if (!m.playing) return null; self.player().video.drainPreparation() catch {}; const lo = m.time.lo; if (try self.paint()) |failure| return failure; // After a few intervals of steady playback the frame keeps up with the clock. if (index < 2 or !m.playing or m.regime == .either or m.time.upper(clock.now()) + 4 * interval >= m.duration) continue; var state: c.ot_video_state = undefined; try ok(ot.ot_video_get_state(self.context, &self.node, &state)); model.count("tick checked"); // The scheduler aims ahead by its measured preparation and output latency, so frames // may trail by that lead too; under load it grows. const due: i64 = @max(0, lo + m.offset); const lead: i64 = self.player().video.state.sync_lead_us; if (state.flags & c.OT_VIDEO_PLAYING == 0 or state.frame_pts_us >= due - 3 * @max(interval, frame_us) - lead) continue; if (self.gate and m.offset < 0) { // Known defect: with a negative offset every prepared frame looks stale and is dropped. model.count("gated frame behind negative offset"); return null; } return "frame-behind"; } return null; } fn read(self: *Run) !?[]const u8 { var state: c.ot_video_state = undefined; return self.observe(&state, "clock"); } /// Reads the state and checks it against the model, then narrows the model to it. fn observe(self: *Run, state: *c.ot_video_state, property: []const u8) !?[]const u8 { const m = &self.model; const at = clock.now(); try ok(ot.ot_video_get_state(self.context, &self.node, state)); const time = state.current_time_us; const flags = state.flags; const playing = flags & c.OT_VIDEO_PLAYING != 0; const ended = flags & c.OT_VIDEO_ENDED != 0; const leads = flags & (c.OT_VIDEO_AUDIO | c.OT_VIDEO_BUFFERING) != 0; if (flags & c.OT_VIDEO_ERROR != 0) return "error"; if ((playing and ended) or (leads and (!playing or !m.engine))) return "flags"; if (!m.playing) { // Paused or ended: the clock holds exactly where it stopped. if (playing or ended != m.ended) return "flags"; if (time < m.time.lo or time > m.time.hi) return if (m.time.lo == m.time.hi) "paused-hold" else property; model.count("read held"); m.time = Time.exact(time); m.last = time; return null; } const top = m.time.upper(clock.now()); const total = m.duration; if (!playing and !ended and m.pausing) { // The pause held the clock before the end, or after its tick looped. const wrapped = m.loop and top >= total and time < @min(m.time.lo, total); const lo = if (wrapped) @max(0, m.time.lo - total) else m.time.lo; if (time < lo or time > (if (wrapped) top - total else @min(top, total - 1))) return property; model.count("read held"); if (wrapped) { m.seek_frame = true; m.target = lo; } m.playing = false; m.pausing = false; m.time = Time.exact(time); m.last = time; return null; } m.pausing = false; if (!playing) { // Only reaching the end stops playback, at the end. if (!ended or m.loop or top < total or time != total) return "flags"; model.count("read reached end"); m.playing = false; m.ended = true; m.time = Time.exact(total); m.last = total; return null; } if (ended) return "flags"; const may_loop = m.loop and top >= total; if (m.last) |last| if (time < last and !may_loop) return "monotone"; var lo = m.time.lo; var hi = top; if (may_loop and time < @min(lo, total)) { // A loop restarts at the time that ran past the end; an audio source reopens its stream there. lo = @max(0, lo - total); hi = top - total; model.count("read looped"); m.regime = if (m.engine) .audio else .wall; m.seek_frame = true; m.target = lo; } if (time < lo or time > hi) return property; if (m.engine) { if (m.regime == .audio and !leads) return "clock-source"; if (!leads) m.regime = .wall; } else if (leads) return "flags"; model.count(if (leads) "read audio clock" else "read wall clock"); m.time = .{ .lo = time, .hi = time, .since = at, .wall = !leads }; m.last = time; return null; } }; fn expectStatus(actual: c.ot_status, expected: c.ot_status) ?[]const u8 { return if (actual == expected) null else "status"; } /// Seek targets: 0, inside the clip, within 50 ms of the end, the end, past it, and below 0. fn seekTarget(kind: u8, raw: u32, duration: i64) i64 { return switch (kind % 6) { 0 => 0, 1 => @mod(@as(i64, raw), duration), 2 => duration - 1 - @mod(@as(i64, raw), 50_000), 3 => duration, 4 => duration + 1 + @mod(@as(i64, raw), 1_000_000), else => -1 - @as(i64, raw), }; } const Harness = struct { gate: bool = true, pub const Command = Op; pub fn generate(_: *Harness, arena: std.mem.Allocator, random: std.Random, steps: usize) ![]Op { const commands = try arena.alloc(Op, steps); commands[0] = .{ .open = random.uintLessThan(u8, sources.len) }; for (commands[1..]) |*command| { command.* = switch (random.uintLessThan(u8, 100)) { 0...6 => .{ .open = random.uintLessThan(u8, sources.len) }, 7 => .close, 8...21 => .play, 22...29 => .pause, 30...43 => .{ .seek = .{ .kind = random.uintLessThan(u8, 6), .raw = random.int(u32) } }, 44...48 => .{ .loop = if (random.uintLessThan(u8, 8) == 0) 2 else random.uintLessThan(u8, 2) }, 49...50 => .{ .muted = random.boolean() }, 51...54 => .{ .offset = if (random.uintLessThan(u8, 6) == 0) std.math.maxInt(i32) else random.intRangeAtMost(i32, -120_000, 120_000) }, 55...56 => .{ .max_fps = random.uintLessThan(u8, 130) }, 57...71 => .{ .advance = ([_]u32{ 1_000, 17_000, 41_667, 100_000, 250_000, 700_000 })[random.uintLessThan(u8, 6)] }, 72...84 => .{ .tick = 1 + random.uintLessThan(u8, 6) }, 85...91 => .paint, else => .read, }; } return commands; } pub fn simplify(_: *Harness, arena: std.mem.Allocator, command: Op) ![]const Op { return switch (command) { .seek => |value| if (value.raw != 0) try arena.dupe(Op, &.{.{ .seek = .{ .kind = value.kind, .raw = 0 } }}) else &.{}, .advance => |us| if (us > 1_000) try arena.dupe(Op, &.{.{ .advance = 1_000 }}) else &.{}, .offset => |value| if (value != 0) try arena.dupe(Op, &.{.{ .offset = 0 }}) else &.{}, .tick => |intervals| if (intervals > 3) try arena.dupe(Op, &.{.{ .tick = 3 }}) else &.{}, else => &.{}, }; } pub fn format(_: *Harness, command: Op, writer: *std.Io.Writer) void { writer.print("{any}", .{command}) catch {}; } pub fn run(self: *Harness, _: std.mem.Allocator, commands: []const Op) !?model.Failure { var options = record(c.ot_context_options); options.object_capacity = 64; options.render_cells_max = 256; var context: ?*ot.ContextHandle = null; try ok(ot.ot_context_create(&options, &context)); defer std.testing.expectEqual(c.OT_OK, ot.ot_context_destroy(context.?)) catch unreachable; var state = try Run.init(context.?, self.gate); for (commands, 0..) |command, step| { const property = try state.apply(command) orelse continue; if (model.fail(property, step)) |failure| return failure; } return null; } }; fn record(comptime T: type) T { var value = std.mem.zeroes(T); value.struct_size = @sizeOf(T); value.abi_version = c.OT_CONTEXT_ABI_VERSION; return value; } fn ok(status: c.ot_status) !void { if (status != c.OT_OK) { std.debug.print("ABI status {d}\n", .{status}); return error.AbiStatus; } } test "Media clock model: play, pause, seek, loop and source replacement follow one clock" { var harness: Harness = .{}; try model.check(Harness, &harness, .{ .seeds = 64, .steps = 24 }); // A loop restarts at the time that ran past the end; the CI seeds reach no wall-clock loop. try std.testing.expectEqual(null, try harness.run(std.testing.allocator, &.{ .{ .open = 1 }, .{ .loop = 1 }, .{ .seek = .{ .kind = 2, .raw = 27_179 } }, .play, .{ .tick = 3 } })); } test "Media clock model: seeks to the end and negative A/V offsets show the wrong frame (known, gated)" { // Each row fails its property today. When a defect is fixed, its row passes and this test // fails with a message: delete the row and its gate. var harness: Harness = .{ .gate = false }; const rows = [_]struct { property: []const u8, trace: []const Op }{ // A paused seek to the end shows no frame (frame_pts -1); a seek to duration - 1 shows the // last frame, and playing into the end keeps it. .{ .property = "seek-frame", .trace = &.{ .{ .open = 0 }, .{ .seek = .{ .kind = 3, .raw = 0 } }, .paint } }, // A seek past the end clamps to the end and shows nothing, without audio too. .{ .property = "seek-frame", .trace = &.{ .{ .open = 1 }, .paint, .{ .seek = .{ .kind = 4, .raw = 0 } }, .paint } }, // Offset -11,149 us and a seek to 1,010,794: the frame due is 958,333, the decoder shows 1,000,000. .{ .property = "seek-frame", .trace = &.{ .{ .open = 2 }, .{ .offset = -11_149 }, .{ .seek = .{ .kind = 1, .raw = 3_427_010_794 } }, .paint } }, // A negative offset: prepared frames go stale before they are due, so playback drops frames // from about -15 ms and freezes further down; here the first frame stays while the clock runs. .{ .property = "frame-behind", .trace = &.{ .{ .open = 0 }, .{ .offset = -120_000 }, .play, .{ .tick = 6 }, .{ .tick = 6 } } }, }; for (rows) |row| { const failure = try harness.run(std.testing.allocator, row.trace) orelse { std.debug.print("a known defect is fixed: remove its row and its gate ({s})\n", .{row.property}); return error.KnownDefectFixed; }; try std.testing.expectEqualStrings(row.property, failure.property); } }