Skip to content

Commit 4d8cd48

Browse files
author
Your New Name
committed
Camera: write nothing after the byte cap refuses a sample
- fill_to returns at once once CameoCap::Bytes is set: the refused picture already became the encoder's reference, so encoding the held frame again would write all-skipped samples the decoder renders from the last muxed picture — frames past the cap showing the wrong picture. seal keeps its Done path for capped clips. - Test: no_samples_are_written_after_the_byte_cap (a later tick writes no bytes and adds no frames; finish finalizes exactly the capped frames). deckcraft-media 48 + 20 passed.
1 parent aeb5dec commit 4d8cd48

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

‎crates/media/src/camera.rs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,13 @@ impl CameoRecorder {
333333
/// samples or the byte budget, keeping what is written, so an over-long slide keeps a
334334
/// truncated clip and the rest is written by the next pass.
335335
fn fill_to(&mut self, want: u64, budget: u64) -> crate::Result<()> {
336+
// A byte-capped clip already holds everything it may: the refused picture became the
337+
// encoder's reference, so encoding the same frame again would write all-skipped
338+
// samples the decoder renders from the last muxed picture — frames past the cap
339+
// showing the wrong picture. `seal` finalizes capped clips on the `Done` path below.
340+
if self.capped == Some(CameoCap::Bytes) {
341+
return Ok(());
342+
}
336343
if self.written >= want {
337344
if self.written >= self.target
338345
&& let Some(next) = self.next_image.take()
@@ -634,6 +641,33 @@ mod tests {
634641
assert!(small.bytes() <= budget, "the refused sample wrote nothing: {}", small.bytes());
635642
}
636643

644+
/// Once the byte cap refuses a sample, later passes write nothing more: the refused picture
645+
/// is the encoder's reference, so re-encoding the same frame would slip all-skipped samples
646+
/// past the cap that the decoder renders from the wrong picture.
647+
#[test]
648+
fn no_samples_are_written_after_the_byte_cap() {
649+
let mut rec = CameoRecorder::new((64, 64), 15, 30, 0).expect("recorder").with_max_bytes(8 << 10);
650+
let mut now = 0u64;
651+
for i in 0..1_000u64 {
652+
now = i * 67;
653+
rec.push(&frame(i, 64, 64, (i % 251) as u8), now).expect("push");
654+
rec.tick(MAX_SAMPLES_PER_CALL).expect("tick");
655+
if rec.capped() {
656+
break;
657+
}
658+
}
659+
assert_eq!(rec.cap(), Some(CameoCap::Bytes), "the byte budget ended the clip");
660+
let held = rec.track.bytes();
661+
let frames = rec.frames();
662+
// Later passes, and the close itself, must not encode the held frame again.
663+
rec.tick(MAX_SAMPLES_PER_CALL).expect("tick after the cap");
664+
assert_eq!(rec.track.bytes(), held, "no sample slipped past the cap");
665+
assert_eq!(rec.frames(), frames, "the frame count is frozen at the cap");
666+
let clip = rec.finish(now).expect("the capped clip finalizes");
667+
assert_eq!(clip.frames, frames, "the close wrote nothing after the cap");
668+
assert!(clip.mp4.len() <= MAX_CLIP_BYTES);
669+
}
670+
637671
/// A clip the byte budget stopped can never write another sample: sealing it returns the clip
638672
/// it has, never a pending recorder a caller would keep polling for nothing.
639673
#[test]

0 commit comments

Comments
 (0)