Skip to content

Commit

Permalink
Reader for initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
GrimOutlook committed Jul 29, 2024
1 parent 275a487 commit 0389d01
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ strum_macros = "0.26.4"
env_logger = "0.11.3"
itertools = "0.13.0"
test-case = "3.3.1"
ts-analyzer = { path = "../ts-analyzer" }
ts-analyzer = "0.2.1"

[features]
log = ["dep:log"]
Expand Down
68 changes: 38 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,47 @@ indented to be used for injecting KLV data into video streams.
extern crate klv_uas;

use std::env;
use klv_uas::tag::Tag;
use ts_analyzer::reader::TSReader;
use std::fs::File;
use std::io::BufReader;
use klv_uas::klv_packet::KlvPacket;

fn main() {
env_logger::init();
let filename = env::var("TEST_FILE").expect("Environment variable not set");

let f = File::open(filename.clone()).expect("Couldn't open file");
let mut reader = TSReader::new(f).expect("Transport Stream file contains no SYNC bytes.");

let klv: KlvPacket;
loop {
// Get a payload from the reader. The `unchecked` in the method name means that if an error
// is hit then `Some(payload)` is returned rather than `Ok(Some(payload))` in order to reduce
// `.unwrap()` (or other) calls.
let payload = reader.next_payload_unchecked()
// Assume that a payload was found in the file and was successfully parsed.
.expect("No valid payload found");

// Try to parse a UAS LS KLV packet from the payload that was found. This will likely only
// work if you have the `search` feature enabled as the UAS LS KLV record does not start at
// the first byte of the payload.
klv = match KlvPacket::from_bytes(payload) {
Ok(klv) => klv,
Err(e) => {
println!("Error {:?}", e);
continue
},
};

break
}

println!("Timestamp of KLV packet: {}", klv.precision_time_stamp());
env_logger::init();
let filename = env::var("TEST_FILE").expect("Environment variable not set");

let f = File::open(filename.clone()).expect("Couldn't open file");
let buf_reader = BufReader::new(f);
let mut reader = TSReader::new(&*filename, buf_reader).expect("Transport Stream file contains no SYNC bytes.");

reader.add_tracked_pid(258);

let klv;
loop {
// Get a payload from the reader. The `unchecked` in the method name means that if an error
// is hit then `Some(payload)` is returned rather than `Ok(Some(payload))` in order to reduce
// `.unwrap()` (or other) calls.
let payload = match reader.next_payload() {
Ok(payload) => payload.expect("Payload is None"),
Err(e) => panic!("Could not get payload due to error: {}", e),
};

// Try to parse a UAS LS KLV packet from the payload that was found. This will likely only
// work if you have the `search` feature enabled as the UAS LS KLV record does not start at
// the first byte of the payload.
klv = match KlvPacket::from_bytes(payload) {
Ok(klv) => klv,
Err(e) => {
println!("Error {:?}", e);
continue
},
};

break
}

println!("Timestamp of KLV packet: {:?}", klv.get(Tag::PrecisionTimeStamp).unwrap());
}
```

Expand All @@ -73,6 +78,9 @@ fn main() {
- [ ] FLP
- [ ] Set
- [x] UTF8
- Support converting all types of KLV value to actual values.
- Such as converting KLV Tag 5 (Platform Heading Angle) to the actual floating point angle represented by the integer
value stored in the KLV value.

### Testing

Expand Down
6 changes: 4 additions & 2 deletions examples/klv_timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ use std::env;
use klv_uas::tag::Tag;
use ts_analyzer::reader::TSReader;
use std::fs::File;
use std::io::BufReader;
use klv_uas::klv_packet::KlvPacket;

fn main() {
env_logger::init();
let filename = env::var("TEST_FILE").expect("Environment variable not set");

let f = File::open(filename.clone()).expect("Couldn't open file");
let mut reader = TSReader::new(f).expect("Transport Stream file contains no SYNC bytes.");
let buf_reader = BufReader::new(f);
let mut reader = TSReader::new(&*filename, buf_reader).expect("Transport Stream file contains no SYNC bytes.");

reader.add_tracked_pid(258);

Expand All @@ -21,7 +23,7 @@ fn main() {
// is hit then `Some(payload)` is returned rather than `Ok(Some(payload))` in order to reduce
// `.unwrap()` (or other) calls.
let payload = match reader.next_payload() {
Ok(payload) => payload.expect("Payload is None"),
Ok(payload) => payload.expect("No payloads found in reader"),
Err(e) => panic!("Could not get payload due to error: {}", e),
};

Expand Down
2 changes: 1 addition & 1 deletion src/klv_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ mod tests {
return packet_from_value(vec![0x03, 0x02, b'I', b'D']) // Mission ID is 'ID'.
}

#[test_case(packet_1, 46955, Some("ID".into()))]
#[test_case(packet_1, 47467, Some("ID".into()))]
fn from_bytes(packet: fn () -> Vec<u8>, checksum: u16, mission_id: Option<Arc<str>>) {
let bytes = packet();
let packet = KlvPacket::from_bytes(bytes.into()).unwrap();
Expand Down

0 comments on commit 0389d01

Please sign in to comment.