Debian Codegraph @

debian/missing-sources/av-data/src/packet.rs

raw
1 //! Packets definitions.
2
3 #![allow(dead_code)]
4
5 use crate::timeinfo::TimeInfo;
6 use std::io::{Read, Result, Write};
7
8 /// Packet with compressed data.
9 #[derive(Default, Debug, Clone)]
10 pub struct Packet {
11 /// Packet data.
12 pub data: Vec<u8>,
13 /// Packet position in the stream.
14 ///
15 /// If `None`, the packet is not associated to a stream.
16 pub pos: Option<usize>,
17 /// Type of stream the packet is associated to.
18 pub stream_index: isize,
19 /// Packet timestamp information.
20 pub t: TimeInfo,
21
22 /// Tells whether a packet contains a keyframe.
23 pub is_key: bool,
24 /// Tells whether a packet is corrupted.
25 pub is_corrupted: bool,
26 }
27
28 impl Packet {
29 /// Creates a new empty `Packet` of a determined capacity.
30 pub fn with_capacity(capacity: usize) -> Self {
31 Packet {
32 data: Vec::with_capacity(capacity),
33 t: TimeInfo::default(),
34 pos: None,
35 stream_index: -1,
36 is_key: false,
37 is_corrupted: false,
38 }
39 }
40
41 /// Creates a zero-initalized `Packet` of a determined capacity.
42 pub fn zeroed(size: usize) -> Self {
43 Packet {
44 data: vec![0; size],
45 t: TimeInfo::default(),
46 pos: None,
47 stream_index: -1,
48 is_key: false,
49 is_corrupted: false,
50 }
51 }
52
53 /// Creates a new empty `Packet`.
54 pub fn new() -> Self {
55 Self::with_capacity(0)
56 }
57 }
58
59 /// Used to read a packet from a source.
60 pub trait ReadPacket: Read {
61 /// Reads a packet from a source.
62 fn get_packet(&mut self, len: usize) -> Result<Packet> {
63 let mut pkt = Packet::zeroed(len);
64 self.read_exact(pkt.data.as_mut_slice())?;
65 Ok(pkt)
66 }
67 }
68
69 /// Used to write a packet into a source.
70 pub trait WritePacket: Write {
71 /// Writes a packet into a source.
72 fn put_packet(&mut self, pkt: Packet) -> Result<()> {
73 self.write_all(pkt.data.as_slice())
74 }
75 }
76
77 impl<R: Read + ?Sized> ReadPacket for R {}
78 impl<W: Write + ?Sized> WritePacket for W {}
79
80 use std::sync::Arc;
81
82 /// A specialized type for a thread-safe reference-counting pointer `Packet`.
83 pub type ArcPacket = Arc<Packet>;
84
85 #[cfg(test)]
86 mod test {
87 use super::*;
88 use std::io::Cursor;
89
90 #[test]
91 fn read_packet() {
92 let data: Vec<u8> = (0..128).collect();
93 let mut buf = Cursor::new(data.clone());
94
95 match buf.get_packet(64) {
96 Ok(pkt) => assert_eq!(pkt.data, &data[..64]),
97 _ => unreachable!(),
98 }
99 }
100
101 /*#[test]
102 fn test_new(){
103 let pkt = Packet::new();
104 assert_eq!(0, pkt.data.len());
105 }*/
106
107 #[test]
108 fn write_packet() {
109 let size = 1024;
110 let mut buf = Cursor::new(Vec::with_capacity(size));
111
112 let mut pkt = Packet::with_capacity(size);
113
114 for i in 0..size {
115 pkt.data.push(i as u8);
116 }
117
118 buf.put_packet(pkt).unwrap();
119
120 let vec = buf.into_inner();
121
122 for (i, elem) in vec.iter().enumerate().take(size) {
123 println!("{}", elem);
124 assert!(*elem == i as u8);
125 }
126 }
127 }