use super::*;
use std::io::Cursor;
const SC: [u8; 4] = [0x00, 0x00, 0x00, 0x01];
const NON_FIRST_SLICE: [u8; 3] = [0x01, 0x40, 0xAA];
const SPS: [u8; 3] = [0x67, 0x42, 0xC0];
const FIRST_IDR_SLICE: [u8; 3] = [0x65, 0x88, 0x84];
fn push(out: &mut Vec<u8>, nal: &[u8]) {
out.extend_from_slice(&SC);
out.extend_from_slice(nal);
}
#[test]
fn a_stream_of_non_first_slices_is_refused() {
let mut stream = vec![];
for _ in 0..100_000 {
push(&mut stream, &NON_FIRST_SLICE);
}
let mut reader = H26xSampleReader::new(Cursor::new(stream), 4096, false);
assert!(matches!(reader.next_sample(), Err(Error::ErrBufferFull)));
}
#[test]
fn reading_resumes_after_an_oversized_access_unit() {
let extra = 903;
let mut stream = vec![];
for _ in 0..(MAX_ACCESS_UNIT_NALS + 1 + extra) {
push(&mut stream, &NON_FIRST_SLICE);
}
push(&mut stream, &SPS);
push(&mut stream, &FIRST_IDR_SLICE);
let mut reader = H26xSampleReader::new(Cursor::new(stream), 4096, false);
assert!(matches!(reader.next_sample(), Err(Error::ErrBufferFull)));
let rest = reader.next_sample().expect("the remaining slices");
assert!(rest.timed);
assert_eq!(extra * (SC.len() + NON_FIRST_SLICE.len()), rest.data.len());
let mut picture = vec![];
push(&mut picture, &SPS);
push(&mut picture, &FIRST_IDR_SLICE);
let next = reader.next_sample().expect("the next picture");
assert!(next.timed);
assert_eq!(Bytes::from(picture), next.data);
assert!(matches!(reader.next_sample(), Err(Error::ErrIoEOF)));
}