1use std::sync::Arc;
2
3use bytes::Bytes;
4use flowly::{DataFrame, EncodedFrame, Fourcc, Frame, FrameFlags, FrameSource};
5
6#[derive(Clone, Default, PartialEq)]
7pub struct Mp4FrameSource<S> {
8 pub original: S,
9 pub params: Vec<Bytes>,
10 pub codec: Fourcc,
11 pub width: u16,
12 pub height: u16,
13}
14
15impl<S: FrameSource> FrameSource for Mp4FrameSource<S> {
16 type Source = S;
17
18 fn source(&self) -> &Self::Source {
19 &self.original
20 }
21}
22
23#[derive(Clone)]
24pub struct Mp4Frame<S> {
25 source: Arc<Mp4FrameSource<S>>,
26 timestamp: u64,
27 offset: i32,
28 data: Bytes,
29 flags: FrameFlags,
30}
31
32impl<S> Mp4Frame<S> {
33 pub fn new(
34 source: Arc<Mp4FrameSource<S>>,
35 timestamp: u64,
36 offset: i32,
37 data: Bytes,
38 flags: FrameFlags,
39 ) -> Self {
40 Self {
41 source,
42 timestamp,
43 offset,
44 data,
45 flags,
46 }
47 }
48}
49
50impl<S: FrameSource> DataFrame for Mp4Frame<S> {
51 type Source = Arc<Mp4FrameSource<S>>;
52 type Chunk = Bytes;
53
54 fn source(&self) -> &Self::Source {
55 &self.source
56 }
57
58 fn chunks(&self) -> impl Send + Iterator<Item = <Self::Chunk as flowly::MemBlock>::Ref<'_>> {
59 std::iter::once(&self.data)
60 }
61
62 fn into_chunks(self) -> impl Send + Iterator<Item = Self::Chunk> {
63 std::iter::once(self.data)
64 }
65}
66
67impl<S: FrameSource> Frame for Mp4Frame<S> {
68 fn timestamp(&self) -> u64 {
69 self.timestamp
70 }
71
72 fn codec(&self) -> flowly::Fourcc {
73 self.source.codec
74 }
75
76 fn flags(&self) -> flowly::FrameFlags {
77 self.flags
78 }
79}
80
81impl<S: FrameSource> EncodedFrame for Mp4Frame<S> {
82 type Param = Bytes;
83
84 fn pts(&self) -> i64 {
85 self.timestamp as i64 + self.offset as i64
86 }
87
88 fn params(&self) -> impl Iterator<Item = &Self::Param> {
89 self.source.params.iter()
90 }
91}