xref: /xiu/protocol/rtmp/src/cache/metadata.rs (revision fcc2ec9e)
1 use {
2     crate::{amf0::amf0_reader::Amf0Reader, amf0::Amf0ValueType},
3     bytes::BytesMut,
4     networkio::bytes_reader::BytesReader,
5 };
6 pub struct MetaData {
7     chunk_body: BytesMut,
8    // values: Vec<Amf0ValueType>,
9 }
10 
11 impl MetaData {
12     pub fn default() -> Self {
13         Self {
14             chunk_body: BytesMut::new(),
15             //values: Vec::new(),
16         }
17     }
18     //, values: Vec<Amf0ValueType>
19     pub fn save(&mut self, body: BytesMut) {
20         if self.is_metadata(body.clone()) {
21             self.chunk_body = body;
22         }
23     }
24 
25     pub fn is_metadata(&mut self, body: BytesMut) -> bool {
26         let reader = BytesReader::new(body);
27         let result = Amf0Reader::new(reader).read_all();
28 
29         let mut values: Vec<Amf0ValueType> = Vec::new();
30 
31         match result {
32             Ok(v) => {
33                 values.extend_from_slice(&v[..]);
34             }
35             Err(_) => return false,
36         }
37 
38         loop {
39             if values.len() < 2 {
40                 return false;
41             }
42 
43             match values.remove(0) {
44                 Amf0ValueType::UTF8String(str) => {
45                     if str != "@setDataFrame" {
46                         return false;
47                     }
48                 }
49                 _ => {
50                     return false;
51                 }
52             }
53 
54             match values.remove(0) {
55                 Amf0ValueType::UTF8String(str) => {
56                     if str != "onMetaData" {
57                         return false;
58                     }
59                 }
60                 _ => {
61                     return false;
62                 }
63             }
64             break;
65         }
66 
67         return true;
68     }
69 
70     pub fn get_chunk_body(&self) -> BytesMut {
71         return self.chunk_body.clone();
72     }
73 }
74