Skip to content

Commit 1e54824

Browse files
committed
Reorganize Packet struct interface and add in-page info
Firstly, we add info about whether a given packet is the last one ending inside a page or the first one starting. Secondly, we redesign the packet struct contents and make most of them private, only accessible through functions. This serves the purpose to make such additions doable in the future in a backwards compatible fashion, as adding fields to a struct full of public only fields is not backwards compatible. This smart idea is also recommended by the rust API guidelines [1]. [1]: https://rust-lang-nursery.github.io/api-guidelines/future-proofing.html#structs-have-private-fields-c-struct-private
1 parent c30a0eb commit 1e54824

File tree

5 files changed

+54
-15
lines changed

5 files changed

+54
-15
lines changed

examples/dump-all.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ fn print_u8_slice(arr :&[u8]) {
4848

4949
fn dump_pck_info(p :&Packet, ctr :usize) {
5050
println!("Packet: serial 0x{:08x}, data {:08} large, first {: >5}, last {: >5}, absgp 0x{:016x} nth {}",
51-
p.stream_serial, p.data.len(), p.first_packet, p.last_packet,
52-
p.absgp_page, ctr);
51+
p.stream_serial(), p.data.len(), p.first_in_page(), p.last_in_page(),
52+
p.absgp_page(), ctr);
5353
print_u8_slice(&p.data);
5454
}
5555

examples/format-info.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ fn main() {
2222

2323
fn dump_pck_info(p :&Packet) {
2424
println!("Packet: serial 0x{:08x}, data {:08} large, first {: >5}, last {: >5}, absgp 0x{:016x}",
25-
p.stream_serial, p.data.len(), p.first_packet, p.last_packet,
26-
p.absgp_page);
25+
p.stream_serial(), p.data.len(), p.first_in_page(), p.last_in_page(),
26+
p.absgp_page());
2727
}
2828

2929
fn run() -> Result<(), std::io::Error> {

examples/repack.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,19 @@ fn run() -> Result<(), std::io::Error> {
4646
let r = btry!(pck_rdr.read_packet());
4747
match r {
4848
Some(pck) => {
49-
let inf = if pck.last_packet {
50-
PacketWriteEndInfo::NormalPacket
51-
} else {
49+
let inf = if pck.last_in_stream() {
5250
PacketWriteEndInfo::EndStream
51+
} else if pck.last_in_page() {
52+
PacketWriteEndInfo::EndPage
53+
} else {
54+
PacketWriteEndInfo::NormalPacket
5355
};
56+
let stream_serial = pck.stream_serial();
57+
let absgp_page = pck.absgp_page();
5458
btry!(pck_wtr.write_packet(pck.data.into_boxed_slice(),
55-
pck.stream_serial,
59+
stream_serial,
5660
inf,
57-
pck.absgp_page));
61+
absgp_page));
5862
},
5963
// End of stream
6064
None => break,

src/lib.rs

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,49 @@ Every logical bitstream is identified by the serial number its pages have stored
4747
pub struct Packet {
4848
/// The data the `Packet` contains
4949
pub data :Vec<u8>,
50+
/// `true` iff this packet is the first one in the page.
51+
first_packet_pg :bool,
5052
/// `true` iff this packet is the first one in the logical bitstream.
51-
pub first_packet :bool,
53+
first_packet_stream :bool,
54+
/// `true` iff this packet is the last one in the page.
55+
last_packet_pg :bool,
5256
/// `true` iff this packet is the last one in the logical bitstream
53-
pub last_packet :bool,
57+
last_packet_stream :bool,
5458
/// Absolute granule position of the last page the packet was in.
5559
/// The meaning of the absolute granule position is defined by the codec.
56-
pub absgp_page :u64,
60+
absgp_page :u64,
5761
/// Serial number. Uniquely identifying the logical bitstream.
58-
pub stream_serial :u32,
62+
stream_serial :u32,
5963
/*/// Packet counter
6064
/// Why u64? There are MAX_U32 pages, and every page has up to 128 packets. u32 wouldn't be sufficient here...
6165
pub sequence_num :u64,*/ // TODO perhaps add this later on...
6266
}
67+
68+
impl Packet {
69+
/// Returns whether the packet is the first one starting in the page
70+
pub fn first_in_page(&self) -> bool {
71+
self.first_packet_pg
72+
}
73+
/// Returns whether the packet is the first one of the entire stream
74+
pub fn first_in_stream(&self) -> bool {
75+
self.first_packet_stream
76+
}
77+
/// Returns whether the packet is the last one starting in the page
78+
pub fn last_in_page(&self) -> bool {
79+
self.last_packet_pg
80+
}
81+
/// Returns whether the packet is the last one of the entire stream
82+
pub fn last_in_stream(&self) -> bool {
83+
self.last_packet_stream
84+
}
85+
/// Returns the absolute granule position of the page the packet ended in.
86+
///
87+
/// The meaning of the absolute granule position is defined by the codec.
88+
pub fn absgp_page(&self) -> u64 {
89+
self.absgp_page
90+
}
91+
/// Returns the serial number that uniquely identifies the logical bitstream.
92+
pub fn stream_serial(&self) -> u32 {
93+
self.stream_serial
94+
}
95+
}

src/reading.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,10 @@ impl BasePacketReader {
400400

401401
return Some(Packet {
402402
data: packet_content,
403-
first_packet: first_pck_overall,
404-
last_packet: last_pck_overall,
403+
first_packet_pg: first_pck_in_pg,
404+
first_packet_stream: first_pck_overall,
405+
last_packet_pg: last_pck_in_pg,
406+
last_packet_stream: last_pck_overall,
405407
absgp_page: pg_info.bi.absgp,
406408
stream_serial: str_serial,
407409
});

0 commit comments

Comments
 (0)