Skip to content

GrimOutlook/klv-uas

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

klv-uas

Crates.io Total Downloads docs.rs Crates.io Version GitHub Repo stars Crates.io License

A library for extracting KLV data from transport stream packet payloads. This library is not indented to be used for injecting KLV data into video streams.

WARNING

Currently, you must enable the feature ignore_incomplete. I want the user to understand that several of the datatypes cannot be parsed correctly quite yet, so they must manually allow this.

Example

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 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) => {
        continue
      },
    };

    break
  }

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

Goals

  • Support parsing all value types from KLV fields.
    • int
    • int8
    • int16
    • int32
    • uint
    • uint8
    • uint16
    • uint32
    • uint64
    • IMAPB
    • Byte
    • DLP
    • VLP
    • FLP
    • Set
    • 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

TEST_FILE="$HOME/Truck.ts" cargo run --features search --example klv_timestamp


Reference Material

  • A sample TS stream with KLV data can be found here.
  • The standards for KLV metadata can be found here. Find MISB ST 0107.X and click FILE. The current link is here but is likely to change.
  • The standards for the UAS Datalink Local Set can be found here. Find MISB ST 0601.X and click FILE. The current link is here but is likely to change.