scte35/src/lib.rs

70 lines
1.3 KiB
Rust
Raw Permalink Normal View History

use bitstream_io::{BigEndian, BitRecorder};
use std::io;
2022-05-01 22:08:28 +00:00
use std::time::Duration;
2022-05-01 12:55:05 +00:00
use thiserror::Error;
2022-04-23 22:05:28 +00:00
mod commands;
mod descriptors;
mod info;
2022-05-01 22:08:28 +00:00
mod time;
2022-04-23 22:05:28 +00:00
#[cfg(feature = "serde")]
mod serde;
2022-05-01 14:01:39 +00:00
pub use commands::SpliceNull;
pub use descriptors::*;
2022-05-01 14:01:39 +00:00
pub use info::{EncryptionAlgorithm, SAPType, SpliceInfoSection};
pub use time::SpliceTime;
2022-04-23 22:05:28 +00:00
2022-05-01 12:55:05 +00:00
#[derive(Error, Debug)]
#[error("Could not execute operation due to {0}")]
pub enum CueError {
2022-05-01 12:55:05 +00:00
Io(#[from] io::Error),
2022-04-23 22:05:28 +00:00
}
2022-05-01 22:08:28 +00:00
pub trait ClockTimeExt {
2022-05-02 09:04:45 +00:00
fn to_90k(&self) -> u64;
2022-05-01 22:08:28 +00:00
}
2022-05-03 14:53:28 +00:00
impl ClockTimeExt for u64 {
#[inline]
fn to_90k(&self) -> u64 {
*self
}
}
2022-05-01 22:08:28 +00:00
impl ClockTimeExt for Duration {
2022-05-03 14:53:28 +00:00
#[inline]
2022-05-02 09:04:45 +00:00
fn to_90k(&self) -> u64 {
(self.as_secs_f64() * 90_000.0).floor() as u64
2022-05-01 22:08:28 +00:00
}
}
trait BytesWritten {
fn bytes_written(&self) -> u32;
}
impl BytesWritten for BitRecorder<u32, BigEndian> {
#[inline]
fn bytes_written(&self) -> u32 {
self.written() / 8
}
}
2022-05-01 22:08:28 +00:00
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_clock_time() {
let duration = Duration::from_secs(1);
2022-05-02 09:04:45 +00:00
assert_eq!(duration.to_90k(), 90_000);
2022-05-01 22:08:28 +00:00
}
#[test]
fn test_spec_example() {
let time = Duration::from_secs_f64(21388.766756);
2022-05-02 09:04:45 +00:00
assert_eq!(time.to_90k(), 0x072bd0050);
}
2022-05-01 22:08:28 +00:00
}