pcr_measure/
pcr_measure.rs

1
2extern crate mpegts;
3
4use std::io::BufReader;
5use std::fs::File;
6use std::process;
7use std::env;
8
9use mpegts::parser::packet::parse_next_packets;
10use mpegts::writer::continuity_pcr::ContinuityPcr;
11
12const TS_PACKET_SIZE: usize = 188;
13const SYSTEM_CLOCK_FREQUENCY: usize = 27000000;
14
15fn main() {
16
17  if env::args().count() != 2 {
18    println!("ERROR: missing filepath argument.");
19    println!("usage:");
20    println!("       pcr_measure [filepath.ts]");
21    process::exit(0x0f00);
22  }
23
24  let path = env::args().last().unwrap();
25
26  let file = File::open(path).unwrap();
27  let mut stream = BufReader::new(file);
28
29  let mut ts_packet_count = 0;
30
31  let mut continuity_pcr = ContinuityPcr{streams: vec![]};
32
33  loop {
34    let packets = parse_next_packets(&mut stream).unwrap();
35
36    for packet in packets {
37      if packet.program_id == 0 {
38        continue;
39      }
40      if packet.adaptation_field.is_some() {
41        let af = packet.adaptation_field.unwrap();
42        if af.pcr.is_some() {
43          let pcr = af.pcr.unwrap();
44
45          let new_pcr = pcr.get();
46          let new_pcr_index = (ts_packet_count * TS_PACKET_SIZE) + 10;
47
48          match continuity_pcr.get(packet.program_id) {
49            None => {},
50            Some(pcr_stream) => {
51              let instant_bitrate = ((new_pcr_index as i64 - pcr_stream.index as i64) * 8 * SYSTEM_CLOCK_FREQUENCY as i64) as f64 / (new_pcr as i64 - pcr_stream.pcr as i64) as f64;
52
53              println!("{} bitrate = {:?}", packet.program_id, instant_bitrate as i64);
54            },
55          }
56
57          continuity_pcr.update(packet.program_id, new_pcr, new_pcr_index);
58        }
59      }
60
61      ts_packet_count += 1;
62    }
63  }
64}