|
| 1 | +import XCTest |
| 2 | +import CoreMedia |
| 3 | +@testable import AetherEngine |
| 4 | + |
| 5 | +/// Issue #89: the software AudioDecoder stamped each CMSampleBuffer with its container-quantized PTS. |
| 6 | +/// For frame durations that are not an integer number of container ticks (1536-sample AC-3 @ 44.1 kHz |
| 7 | +/// = 34.83 ms in a 1 ms MKV timebase) consecutive buffers no longer abut, and |
| 8 | +/// AVSampleBufferAudioRenderer clicks at every frame (~29 Hz, a continuous crackle). AudioClockAnchor |
| 9 | +/// stamps from a running sample count so buffers abut to the sample, re-anchoring only on a real |
| 10 | +/// (> 100 ms) source discontinuity. |
| 11 | +final class AudioClockAnchorTests: XCTestCase { |
| 12 | + |
| 13 | + /// Container-quantized PTS the demuxer hands us: MKV carries a 1 ms timebase, so the per-packet |
| 14 | + /// PTS is the frame's true time rounded to the nearest millisecond. |
| 15 | + private func containerPTS(frame n: Int, samplesPerFrame: Int, sampleRate: Int32) -> CMTime { |
| 16 | + let seconds = Double(n * samplesPerFrame) / Double(sampleRate) |
| 17 | + let ms = Int64((seconds * 1000).rounded()) |
| 18 | + return CMTimeMake(value: ms, timescale: 1000) |
| 19 | + } |
| 20 | + |
| 21 | + /// Drive the anchor exactly as AudioDecoder.emitPending does: resolve, then commit on success. |
| 22 | + @discardableResult |
| 23 | + private func runStream(_ anchor: inout AudioClockAnchor, |
| 24 | + ptsList: [CMTime], |
| 25 | + samplesPerFrame: Int, |
| 26 | + sampleRate: Int32) -> [CMTime] { |
| 27 | + var out: [CMTime] = [] |
| 28 | + for pts in ptsList { |
| 29 | + let r = anchor.resolve(startPTS: pts, sampleRate: sampleRate) |
| 30 | + anchor.commit(pts: r.pts, reanchor: r.reanchor, sampleCount: samplesPerFrame) |
| 31 | + out.append(r.pts) |
| 32 | + } |
| 33 | + return out |
| 34 | + } |
| 35 | + |
| 36 | + // MARK: - The crackle bug |
| 37 | + |
| 38 | + func testConsecutiveBuffersAbutToTheSample_441kHzAC3() { |
| 39 | + let rate: Int32 = 44100 |
| 40 | + let spf = 1536 // AC-3 frame |
| 41 | + var anchor = AudioClockAnchor() |
| 42 | + let ptsList = (0..<10).map { containerPTS(frame: $0, samplesPerFrame: spf, sampleRate: rate) } |
| 43 | + |
| 44 | + let out = runStream(&anchor, ptsList: ptsList, samplesPerFrame: spf, sampleRate: rate) |
| 45 | + |
| 46 | + let expectedStep = Double(spf) / Double(rate) // 34.8299... ms, the true frame length |
| 47 | + for n in 1..<out.count { |
| 48 | + let delta = CMTimeGetSeconds(CMTimeSubtract(out[n], out[n - 1])) |
| 49 | + XCTAssertEqual(delta, expectedStep, accuracy: 1e-6, |
| 50 | + "buffer \(n) must abut to the sample; got \(delta * 1000) ms, expected \(expectedStep * 1000) ms") |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + func testFirstBufferAnchorsToItsStartPTS() { |
| 55 | + var anchor = AudioClockAnchor() |
| 56 | + let start = CMTimeMake(value: 5000, timescale: 1000) |
| 57 | + let r = anchor.resolve(startPTS: start, sampleRate: 44100) |
| 58 | + XCTAssertTrue(r.reanchor) |
| 59 | + XCTAssertEqual(r.pts, start) |
| 60 | + } |
| 61 | + |
| 62 | + // MARK: - Jitter is absorbed, real discontinuities re-anchor |
| 63 | + |
| 64 | + func testSmallContainerJitterIsAbsorbed() { |
| 65 | + let rate: Int32 = 44100 |
| 66 | + let spf = 1536 |
| 67 | + var anchor = AudioClockAnchor() |
| 68 | + |
| 69 | + let r0 = anchor.resolve(startPTS: .zero, sampleRate: rate) |
| 70 | + anchor.commit(pts: r0.pts, reanchor: r0.reanchor, sampleCount: spf) |
| 71 | + |
| 72 | + // True next boundary is 34.83 ms; the container rounds it to 35 ms. The 0.17 ms jitter is what |
| 73 | + // produced the per-frame click, so it must be absorbed (predicted used, not the rounded PTS). |
| 74 | + let jittery = CMTimeMake(value: 35, timescale: 1000) |
| 75 | + let r1 = anchor.resolve(startPTS: jittery, sampleRate: rate) |
| 76 | + XCTAssertFalse(r1.reanchor, "sub-threshold container rounding must not re-anchor") |
| 77 | + XCTAssertEqual(CMTimeGetSeconds(r1.pts), Double(spf) / Double(rate), accuracy: 1e-6, |
| 78 | + "buffer must be stamped at the sample-accurate predicted time, not the rounded container PTS") |
| 79 | + } |
| 80 | + |
| 81 | + func testRealDiscontinuityReanchors() { |
| 82 | + let rate: Int32 = 44100 |
| 83 | + let spf = 1536 |
| 84 | + var anchor = AudioClockAnchor() |
| 85 | + let ptsList = (0..<5).map { containerPTS(frame: $0, samplesPerFrame: spf, sampleRate: rate) } |
| 86 | + runStream(&anchor, ptsList: ptsList, samplesPerFrame: spf, sampleRate: rate) |
| 87 | + |
| 88 | + let seek = CMTimeMake(value: 60_000, timescale: 1000) // a 60 s jump dwarfs container jitter |
| 89 | + let r = anchor.resolve(startPTS: seek, sampleRate: rate) |
| 90 | + XCTAssertTrue(r.reanchor) |
| 91 | + XCTAssertEqual(r.pts, seek, "a real source discontinuity must be honoured, not papered over") |
| 92 | + } |
| 93 | + |
| 94 | + func testResetReanchorsNextBuffer() { |
| 95 | + let rate: Int32 = 44100 |
| 96 | + let spf = 1536 |
| 97 | + var anchor = AudioClockAnchor() |
| 98 | + let ptsList = (0..<5).map { containerPTS(frame: $0, samplesPerFrame: spf, sampleRate: rate) } |
| 99 | + runStream(&anchor, ptsList: ptsList, samplesPerFrame: spf, sampleRate: rate) |
| 100 | + |
| 101 | + anchor.reset() |
| 102 | + |
| 103 | + // 174 ms sits right on the predicted clock (5 * 1536 / 44100 = 174.1 ms); without reset it would |
| 104 | + // be absorbed as a continuation. After a flush it must re-anchor to its own PTS instead. |
| 105 | + let afterFlush = CMTimeMake(value: 174, timescale: 1000) |
| 106 | + let r = anchor.resolve(startPTS: afterFlush, sampleRate: rate) |
| 107 | + XCTAssertTrue(r.reanchor, "flush must drop the anchor so the post-seek buffer re-anchors") |
| 108 | + XCTAssertEqual(r.pts, afterFlush) |
| 109 | + } |
| 110 | + |
| 111 | + // MARK: - The clean case stays clean, and dropped buffers don't drift the clock |
| 112 | + |
| 113 | + func test48kHzAC3StaysGapless() { |
| 114 | + let rate: Int32 = 48000 |
| 115 | + let spf = 1536 // exactly 32 ms, always was gapless |
| 116 | + var anchor = AudioClockAnchor() |
| 117 | + let ptsList = (0..<10).map { containerPTS(frame: $0, samplesPerFrame: spf, sampleRate: rate) } |
| 118 | + |
| 119 | + let out = runStream(&anchor, ptsList: ptsList, samplesPerFrame: spf, sampleRate: rate) |
| 120 | + |
| 121 | + let expectedStep = Double(spf) / Double(rate) |
| 122 | + for n in 1..<out.count { |
| 123 | + let delta = CMTimeGetSeconds(CMTimeSubtract(out[n], out[n - 1])) |
| 124 | + XCTAssertEqual(delta, expectedStep, accuracy: 1e-6) |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + func testDroppedBufferDoesNotAdvanceClock() { |
| 129 | + let rate: Int32 = 44100 |
| 130 | + let spf = 1536 |
| 131 | + var anchor = AudioClockAnchor() |
| 132 | + |
| 133 | + // Frame 0 emits and commits: clock now holds 1536 samples. |
| 134 | + let r0 = anchor.resolve(startPTS: .zero, sampleRate: rate) |
| 135 | + anchor.commit(pts: r0.pts, reanchor: r0.reanchor, sampleCount: spf) |
| 136 | + |
| 137 | + // Frame 1 resolves but its CMSampleBuffer creation fails, so it is never committed. |
| 138 | + _ = anchor.resolve(startPTS: containerPTS(frame: 1, samplesPerFrame: spf, sampleRate: rate), sampleRate: rate) |
| 139 | + |
| 140 | + // Frame 2 must predict off the still-1536 sample count (one frame in), proving the dropped |
| 141 | + // buffer injected no phantom samples. |
| 142 | + let r2 = anchor.resolve(startPTS: containerPTS(frame: 2, samplesPerFrame: spf, sampleRate: rate), sampleRate: rate) |
| 143 | + XCTAssertFalse(r2.reanchor) |
| 144 | + XCTAssertEqual(CMTimeGetSeconds(r2.pts), Double(spf) / Double(rate), accuracy: 1e-6, |
| 145 | + "an uncommitted (dropped) buffer must not advance the sample clock") |
| 146 | + } |
| 147 | +} |
0 commit comments