xref: /webrtc/rtp/src/codecs/vp8/vp8_test.rs (revision 6ac0fffd)
1 use super::*;
2 
3 #[test]
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]
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!(
171                 expected[i], actual,
172                 "{}: Generated packet[{}] differs",
173                 name, i
174             );
175         }
176     }
177 
178     Ok(())
179 }
180 
181 #[test]
182 fn test_vp8_payload_eror() -> Result<()> {
183     let mut pck = Vp8Payloader::default();
184     let empty = Bytes::from_static(&[]);
185     let payload = Bytes::from_static(&[0x90, 0x90, 0x90]);
186 
187     // Positive MTU, empty payload
188     let result = pck.payload(1, &empty)?;
189     assert!(result.is_empty(), "Generated payload should be empty");
190 
191     // Positive MTU, small payload
192     let result = pck.payload(1, &payload)?;
193     assert_eq!(result.len(), 0, "Generated payload should be empty");
194 
195     // Positive MTU, small payload
196     let result = pck.payload(2, &payload)?;
197     assert_eq!(
198         result.len(),
199         payload.len(),
200         "Generated payload should be the same size as original payload size"
201     );
202 
203     Ok(())
204 }
205 
206 #[test]
207 fn test_vp8_partition_head_checker_is_partition_head() -> Result<()> {
208     let vp8 = Vp8Packet::default();
209 
210     //"SmallPacket"
211     assert!(
212         !vp8.is_partition_head(&Bytes::from_static(&[0x00])),
213         "Small packet should not be the head of a new partition"
214     );
215 
216     //"SFlagON",
217     assert!(
218         vp8.is_partition_head(&Bytes::from_static(&[0x10, 0x00, 0x00, 0x00])),
219         "Packet with S flag should be the head of a new partition"
220     );
221 
222     //"SFlagOFF"
223     assert!(
224         !vp8.is_partition_head(&Bytes::from_static(&[0x00, 0x00, 0x00, 0x00])),
225         "Packet without S flag should not be the head of a new partition"
226     );
227 
228     Ok(())
229 }
230