ez_ffmpeg/rtmp/
gop.rs

1use std::collections::VecDeque;
2use bytes::Bytes;
3use rml_rtmp::time::RtmpTimestamp;
4
5#[derive(Clone)]
6pub(crate) enum FrameData {
7    Video {
8        timestamp: RtmpTimestamp,
9        data: Bytes,
10    },
11    Audio {
12        timestamp: RtmpTimestamp,
13        data: Bytes,
14    },
15}
16
17#[derive(Clone)]
18pub(crate) struct Gop {
19    datas: Vec<FrameData>,
20}
21
22impl Default for Gop {
23    fn default() -> Self {
24        Self::new()
25    }
26}
27
28impl Gop {
29    pub fn new() -> Self {
30        Self { datas: Vec::new() }
31    }
32
33    fn save_frame_data(&mut self, data: FrameData) {
34        self.datas.push(data);
35    }
36
37    pub fn get_frame_data(self) -> Vec<FrameData> {
38        self.datas
39    }
40
41    pub fn len(&self) -> usize {
42        self.datas.len()
43    }
44
45    pub fn is_empty(&self) -> bool {
46        self.len() == 0
47    }
48}
49
50#[derive(Clone)]
51pub struct Gops {
52    gops: VecDeque<Gop>,
53    size: usize,
54}
55
56impl Default for Gops {
57    fn default() -> Self {
58        Self::new(1)
59    }
60}
61
62impl Gops {
63    pub fn new(size: usize) -> Self {
64        Self {
65            gops: VecDeque::from([Gop::new()]),
66            size,
67        }
68    }
69
70    pub fn save_frame_data(&mut self, data: FrameData, is_key_frame: bool) {
71        if self.size == 0 {
72            return;
73        }
74
75        if is_key_frame {
76            if self.gops.len() == self.size {
77                self.gops.pop_front();
78            }
79            self.gops.push_back(Gop::new());
80        }
81
82        if let Some(gop) = self.gops.back_mut() {
83            gop.save_frame_data(data);
84        } else {
85            log::error!("should not be here!");
86        }
87    }
88
89    pub fn setted(&self) -> bool {
90        self.size != 0
91    }
92
93    pub fn get_gops(&self) -> VecDeque<Gop> {
94        self.gops.clone()
95    }
96}