web_codecs/video/
frame.rs

1use std::{
2	ops::{Deref, DerefMut},
3	time::Duration,
4};
5
6use derive_more::From;
7
8use crate::Timestamp;
9
10use super::Dimensions;
11
12#[derive(Debug, From)]
13pub struct VideoFrame(web_sys::VideoFrame);
14
15impl VideoFrame {
16	pub fn timestamp(&self) -> Timestamp {
17		Timestamp::from_micros(self.0.timestamp().unwrap() as _)
18	}
19
20	pub fn duration(&self) -> Option<Duration> {
21		Some(Duration::from_micros(self.0.duration()? as _))
22	}
23
24	pub fn dimensions(&self) -> Dimensions {
25		Dimensions {
26			width: self.0.coded_width(),
27			height: self.0.coded_height(),
28		}
29	}
30}
31
32// Avoid closing the video frame on transfer by cloning it.
33impl From<VideoFrame> for web_sys::VideoFrame {
34	fn from(this: VideoFrame) -> Self {
35		this.0.clone().expect("detached")
36	}
37}
38
39impl Clone for VideoFrame {
40	fn clone(&self) -> Self {
41		Self(self.0.clone().expect("detached"))
42	}
43}
44
45impl Deref for VideoFrame {
46	type Target = web_sys::VideoFrame;
47
48	fn deref(&self) -> &Self::Target {
49		&self.0
50	}
51}
52
53impl DerefMut for VideoFrame {
54	fn deref_mut(&mut self) -> &mut Self::Target {
55		&mut self.0
56	}
57}
58
59// Make sure we close the frame on drop.
60impl Drop for VideoFrame {
61	fn drop(&mut self) {
62		self.0.close();
63	}
64}