use std::{thread, time::Duration, time::Instant};
use crate::{
DecodedSurface, DecoderOptions, EncodedAccessUnit, MacOsDecoder, PixelFormat, VideoCodec,
VideoDecoder, VideoTimestamp,
};
const H264_KEYFRAME: &[u8] = &[
0x00, 0x00, 0x00, 0x01, 0x27, 0x64, 0x00, 0x0b, 0xac, 0x57, 0x0c, 0x11, 0xe0, 0x87, 0x40, 0x00,
0x00, 0x00, 0x01, 0x28, 0xee, 0x3c, 0xb0, 0x00, 0x00, 0x00, 0x01, 0x06, 0x05, 0x32, 0x47, 0x56,
0x4a, 0xdc, 0x5c, 0x4c, 0x43, 0x3f, 0x94, 0xef, 0xc5, 0x11, 0x3c, 0xd1, 0x43, 0xa8, 0x01, 0x00,
0x00, 0x03, 0x00, 0x01, 0x03, 0x00, 0x00, 0x03, 0x00, 0x01, 0x02, 0x00, 0x02, 0x87, 0xff, 0x0b,
0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x01, 0x22, 0x0c, 0x03, 0x89, 0x24, 0x01,
0x0d, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x01, 0x25, 0xb8, 0x20, 0x1f, 0xde, 0x08,
0xe5, 0x4c, 0xff, 0xbf, 0xfe, 0xcc, 0xc7, 0xc7, 0xb0, 0x5f, 0x40, 0xfc, 0x98, 0xba, 0x00, 0x27,
0xe3, 0xb9, 0xda, 0x25, 0x4e, 0x5e, 0xe8, 0x4e, 0xc9, 0x76, 0x50, 0x53, 0x57, 0x77, 0x76, 0x35,
0x73, 0x15, 0x00, 0x01, 0x5f, 0xe8, 0x13, 0x46, 0xe6, 0x06, 0xb4, 0x64, 0x2c, 0xaf, 0x24, 0xbd,
0x5d, 0xd0, 0x16, 0xaf, 0x63, 0x48, 0x9c, 0xa3, 0x51, 0x30, 0x46, 0x56, 0xa8,
];
const H265_KEYFRAME: &[u8] = &[
0x00, 0x00, 0x00, 0x01, 0x40, 0x01, 0x0c, 0x01, 0xff, 0xff, 0x01, 0x60, 0x00, 0x00, 0x03, 0x00,
0xb0, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x3c, 0x17, 0x02, 0x40, 0x00, 0x00, 0x00, 0x01,
0x42, 0x01, 0x01, 0x01, 0x60, 0x00, 0x00, 0x03, 0x00, 0xb0, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03,
0x00, 0x3c, 0xa0, 0x14, 0x20, 0x20, 0x70, 0x8f, 0x88, 0x17, 0xb9, 0x16, 0x54, 0x40, 0x00, 0x00,
0x00, 0x01, 0x44, 0x01, 0xc0, 0x72, 0xf4, 0x53, 0x64, 0x00, 0x00, 0x00, 0x01, 0x4e, 0x01, 0x05,
0x32, 0x47, 0x56, 0x4a, 0xdc, 0x5c, 0x4c, 0x43, 0x3f, 0x94, 0xef, 0xc5, 0x11, 0x3c, 0xd1, 0x43,
0xa8, 0x01, 0x00, 0x00, 0x03, 0x00, 0x01, 0x03, 0x00, 0x00, 0x03, 0x00, 0x01, 0x02, 0x00, 0x01,
0x0e, 0x00, 0x0b, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x01, 0x2c, 0x0c, 0x03,
0x89, 0x24, 0x01, 0x0d, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x01, 0x28, 0x01, 0xaf,
0x31, 0x0b, 0x03, 0x9a, 0xe8, 0x85, 0xe8, 0x7f, 0xfc, 0x1e, 0xda, 0xdb, 0x98, 0x97, 0x9c, 0xac,
0xd7, 0x7f, 0x1e, 0xb6, 0x80, 0x91, 0x79, 0x41, 0xba, 0xbb, 0x99, 0xbc, 0x17, 0x02, 0xf8, 0x49,
0xaf, 0xb6, 0x57, 0xcd, 0x04, 0x47, 0xfb, 0x85, 0x6d, 0x10, 0xa0,
];
#[test]
#[ignore = "requires access to macOS VideoToolbox hardware media services"]
fn decodes_h264_to_an_iosurface() {
decode_fixture(VideoCodec::H264, H264_KEYFRAME);
}
#[test]
#[ignore = "requires access to macOS VideoToolbox hardware media services"]
fn decodes_h265_to_an_iosurface() {
decode_fixture(VideoCodec::H265, H265_KEYFRAME);
}
fn decode_fixture(codec: VideoCodec, data: &[u8]) {
let mut decoder = MacOsDecoder::new(DecoderOptions::default()).unwrap();
decoder
.submit(EncodedAccessUnit::new(
codec,
data.to_vec(),
VideoTimestamp::from_rtp(0),
true,
))
.unwrap();
let deadline = Instant::now() + Duration::from_secs(2);
while Instant::now() < deadline {
if let Some(frame) = decoder.latest_frame() {
assert_eq!(frame.dimensions().width, 128);
assert_eq!(frame.dimensions().height, 128);
assert_eq!(frame.surface.pixel_format(), PixelFormat::Nv12VideoRange);
assert!(frame.surface.is_io_surface_backed());
return;
}
thread::sleep(Duration::from_millis(2));
}
panic!(
"VideoToolbox produced no frame; stats={:?}",
decoder.stats()
);
}