Skip to content

Commit

Permalink
Merge pull request #3 from jensenn/fix/clock_reference_write
Browse files Browse the repository at this point in the history
Fix write functions for the ClockReference
  • Loading branch information
sile committed Jan 11, 2023
2 parents 0263326 + 7ebc356 commit 8e988e4
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl ClockReference {

pub(crate) fn write_pcr_to<W: Write>(&self, mut writer: W) -> Result<()> {
let base = self.0 / 300;
let extension = self.0 & 0b1_1111_1111;
let extension = self.0 % 300;

let n = (base << 15) | extension;
track_io!(writer.write_uint::<BigEndian>(n, 6))?;
Expand Down Expand Up @@ -147,7 +147,7 @@ impl ClockReference {

pub(crate) fn write_escr_to<W: Write>(&self, mut writer: W) -> Result<()> {
let base = self.0 / 300;
let extension = self.0 & 0b1_1111_1111;
let extension = self.0 % 300;

let marker = 1;
let base0 = base & ((1 << 15) - 1);
Expand All @@ -170,3 +170,26 @@ impl From<Timestamp> for ClockReference {
ClockReference(f.0 * 300)
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn pcr_conversion() {
let cr = ClockReference::new(10000).unwrap();
let mut buf = Vec::new();
cr.write_pcr_to(&mut buf).unwrap();
let new_cr = ClockReference::read_pcr_from(&buf[..]).unwrap();
assert_eq!(cr, new_cr);
}

#[test]
fn escr_conversion() {
let cr = ClockReference::new(10000).unwrap();
let mut buf = Vec::new();
cr.write_escr_to(&mut buf).unwrap();
let new_cr = ClockReference::read_escr_from(&buf[..]).unwrap();
assert_eq!(cr, new_cr);
}
}

0 comments on commit 8e988e4

Please sign in to comment.