xref: /webrtc/rtp/src/codecs/vp8/vp8_test.rs (revision 5d8fe953)
1 use super::*;
2 
3 #[test]
test_vp8_unmarshal() -> Result<()>4 fn test_vp8_unmarshal() -> Result<()> {
5     let mut pck = Vp8Packet::default();
6 
7     // Empty packet
8     let empty_bytes = Bytes::from_static(&[]);
9     let result = pck.depacketize(&empty_bytes);
10     assert!(result.is_err(), "Result should be err in case of error");
11 
12     // Payload smaller than header size
13     let small_bytes = Bytes::from_static(&[0x00, 0x11, 0x22]);
14     let result = pck.depacketize(&small_bytes);
15     assert!(result.is_err(), "Result should be err in case of error");
16 
17     // Payload smaller than header size
18     let small_bytes = Bytes::from_static(&[0x00, 0x11]);
19     let result = pck.depacketize(&small_bytes);
20     assert!(result.is_err(), "Result should be err in case of error");
21 
22     // Normal packet
23     let raw_bytes = Bytes::from_static(&[0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x90]);
24     let payload = pck.depacketize(&raw_bytes).expect("Normal packet");
25     assert!(!payload.is_empty(), "Payload must be not empty");
26 
27     // Header size, only X
28     let raw_bytes = Bytes::from_static(&[0x80, 0x00, 0x00, 0x00]);
29     let payload = pck.depacketize(&raw_bytes).expect("Only X");
30     assert!(!payload.is_empty(), "Payload must be not empty");
31     assert_eq!(pck.x, 1, "X must be 1");
32     assert_eq!(pck.i, 0, "I must be 0");
33     assert_eq!(pck.l, 0, "L must be 0");
34     assert_eq!(pck.t, 0, "T must be 0");
35     assert_eq!(pck.k, 0, "K must be 0");
36 
37     // Header size, X and I, PID 16bits
38     let raw_bytes = Bytes::from_static(&[0x80, 0x80, 0x81, 0x00, 0x00]);
39     let payload = pck.depacketize(&raw_bytes).expect("X and I, PID 16bits");
40     assert!(!payload.is_empty(), "Payload must be not empty");
41     assert_eq!(pck.x, 1, "X must be 1");
42     assert_eq!(pck.i, 1, "I must be 1");
43     assert_eq!(pck.l, 0, "L must be 0");
44     assert_eq!(pck.t, 0, "T must be 0");
45     assert_eq!(pck.k, 0, "K must be 0");
46 
47     // Header size, X and L
48     let raw_bytes = Bytes::from_static(&[0x80, 0x40, 0x00, 0x00]);
49     let payload = pck.depacketize(&raw_bytes).expect("X and L");
50     assert!(!payload.is_empty(), "Payload must be not empty");
51     assert_eq!(pck.x, 1, "X must be 1");
52     assert_eq!(pck.i, 0, "I must be 0");
53     assert_eq!(pck.l, 1, "L must be 1");
54     assert_eq!(pck.t, 0, "T must be 0");
55     assert_eq!(pck.k, 0, "K must be 0");
56 
57     // Header size, X and T
58     let raw_bytes = Bytes::from_static(&[0x80, 0x20, 0x00, 0x00]);
59     let payload = pck.depacketize(&raw_bytes).expect("X and T");
60     assert!(!payload.is_empty(), "Payload must be not empty");
61     assert_eq!(pck.x, 1, "X must be 1");
62     assert_eq!(pck.i, 0, "I must be 0");
63     assert_eq!(pck.l, 0, "L must be 0");
64     assert_eq!(pck.t, 1, "T must be 1");
65     assert_eq!(pck.k, 0, "K must be 0");
66 
67     // Header size, X and K
68     let raw_bytes = Bytes::from_static(&[0x80, 0x10, 0x00, 0x00]);
69     let payload = pck.depacketize(&raw_bytes).expect("X and K");
70     assert!(!payload.is_empty(), "Payload must be not empty");
71     assert_eq!(pck.x, 1, "X must be 1");
72     assert_eq!(pck.i, 0, "I must be 0");
73     assert_eq!(pck.l, 0, "L must be 0");
74     assert_eq!(pck.t, 0, "T must be 0");
75     assert_eq!(pck.k, 1, "K must be 1");
76 
77     // Header size, all flags and 8bit picture_id
78     let raw_bytes = Bytes::from_static(&[0xff, 0xff, 0x00, 0x00, 0x00, 0x00]);
79     let payload = pck
80         .depacketize(&raw_bytes)
81         .expect("all flags and 8bit picture_id");
82     assert!(!payload.is_empty(), "Payload must be not empty");
83     assert_eq!(pck.x, 1, "X must be 1");
84     assert_eq!(pck.i, 1, "I must be 1");
85     assert_eq!(pck.l, 1, "L must be 1");
86     assert_eq!(pck.t, 1, "T must be 1");
87     assert_eq!(pck.k, 1, "K must be 1");
88 
89     // Header size, all flags and 16bit picture_id
90     let raw_bytes = Bytes::from_static(&[0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00]);
91     let payload = pck
92         .depacketize(&raw_bytes)
93         .expect("all flags and 16bit picture_id");
94     assert!(!payload.is_empty(), "Payload must be not empty");
95     assert_eq!(pck.x, 1, "X must be 1");
96     assert_eq!(pck.i, 1, "I must be 1");
97     assert_eq!(pck.l, 1, "L must be 1");
98     assert_eq!(pck.t, 1, "T must be 1");
99     assert_eq!(pck.k, 1, "K must be 1");
100 
101     Ok(())
102 }
103 
104 #[test]
test_vp8_payload() -> Result<()>105 fn test_vp8_payload() -> Result<()> {
106     let tests = vec![
107         (
108             "WithoutPictureID",
109             Vp8Payloader::default(),
110             2,
111             vec![
112                 Bytes::from_static(&[0x90, 0x90, 0x90]),
113                 Bytes::from_static(&[0x91, 0x91]),
114             ],
115             vec![
116                 vec![
117                     Bytes::from_static(&[0x10, 0x90]),
118                     Bytes::from_static(&[0x00, 0x90]),
119                     Bytes::from_static(&[0x00, 0x90]),
120                 ],
121                 vec![
122                     Bytes::from_static(&[0x10, 0x91]),
123                     Bytes::from_static(&[0x00, 0x91]),
124                 ],
125             ],
126         ),
127         (
128             "WithPictureID_1byte",
129             Vp8Payloader {
130                 enable_picture_id: true,
131                 picture_id: 0x20,
132             },
133             5,
134             vec![
135                 Bytes::from_static(&[0x90, 0x90, 0x90]),
136                 Bytes::from_static(&[0x91, 0x91]),
137             ],
138             vec![
139                 vec![
140                     Bytes::from_static(&[0x90, 0x80, 0x20, 0x90, 0x90]),
141                     Bytes::from_static(&[0x80, 0x80, 0x20, 0x90]),
142                 ],
143                 vec![Bytes::from_static(&[0x90, 0x80, 0x21, 0x91, 0x91])],
144             ],
145         ),
146         (
147             "WithPictureID_2bytes",
148             Vp8Payloader {
149                 enable_picture_id: true,
150                 picture_id: 0x120,
151             },
152             6,
153             vec![
154                 Bytes::from_static(&[0x90, 0x90, 0x90]),
155                 Bytes::from_static(&[0x91, 0x91]),
156             ],
157             vec![
158                 vec![
159                     Bytes::from_static(&[0x90, 0x80, 0x81, 0x20, 0x90, 0x90]),
160                     Bytes::from_static(&[0x80, 0x80, 0x81, 0x20, 0x90]),
161                 ],
162                 vec![Bytes::from_static(&[0x90, 0x80, 0x81, 0x21, 0x91, 0x91])],
163             ],
164         ),
165     ];
166 
167     for (name, mut pck, mtu, payloads, expected) in tests {
168         for (i, payload) in payloads.iter().enumerate() {
169             let actual = pck.payload(mtu, payload)?;
170             assert_eq!(expected[i], actual, "{name}: Generated packet[{i}] differs");
171         }
172     }
173 
174     Ok(())
175 }
176 
177 #[test]
test_vp8_payload_eror() -> Result<()>178 fn test_vp8_payload_eror() -> Result<()> {
179     let mut pck = Vp8Payloader::default();
180     let empty = Bytes::from_static(&[]);
181     let payload = Bytes::from_static(&[0x90, 0x90, 0x90]);
182 
183     // Positive MTU, empty payload
184     let result = pck.payload(1, &empty)?;
185     assert!(result.is_empty(), "Generated payload should be empty");
186 
187     // Positive MTU, small payload
188     let result = pck.payload(1, &payload)?;
189     assert_eq!(result.len(), 0, "Generated payload should be empty");
190 
191     // Positive MTU, small payload
192     let result = pck.payload(2, &payload)?;
193     assert_eq!(
194         result.len(),
195         payload.len(),
196         "Generated payload should be the same size as original payload size"
197     );
198 
199     Ok(())
200 }
201 
202 #[test]
test_vp8_partition_head_checker_is_partition_head() -> Result<()>203 fn test_vp8_partition_head_checker_is_partition_head() -> Result<()> {
204     let vp8 = Vp8Packet::default();
205 
206     //"SmallPacket"
207     assert!(
208         !vp8.is_partition_head(&Bytes::from_static(&[0x00])),
209         "Small packet should not be the head of a new partition"
210     );
211 
212     //"SFlagON",
213     assert!(
214         vp8.is_partition_head(&Bytes::from_static(&[0x10, 0x00, 0x00, 0x00])),
215         "Packet with S flag should be the head of a new partition"
216     );
217 
218     //"SFlagOFF"
219     assert!(
220         !vp8.is_partition_head(&Bytes::from_static(&[0x00, 0x00, 0x00, 0x00])),
221         "Packet without S flag should not be the head of a new partition"
222     );
223 
224     Ok(())
225 }
226