Show human readable time to serde repr
Adds a new field to the serde struct that represents a human readable duration of time. This makes easier to debug the SCTE markers. This commit also simplifies a bit the code using Default trait and other simplifications. Fixed the calculation of the sizes of sections in the final SCTE struct. We were not calculating correctly the amount of bytes of the sections. Now we use the Recorder pattern which returns the exact amount of bytes written.
This commit is contained in:
parent
8b5287938d
commit
abc1d82912
6 changed files with 199 additions and 115 deletions
|
@ -14,21 +14,15 @@ pub trait SpliceCommand: TransportPacketWrite {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize))]
|
#[cfg_attr(feature = "serde", derive(Serialize))]
|
||||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
#[derive(Debug, Default, Clone, Copy, PartialEq)]
|
||||||
pub struct SpliceNull {}
|
pub struct SpliceNull {}
|
||||||
|
|
||||||
impl SpliceNull {
|
|
||||||
pub fn new() -> SpliceNull {
|
|
||||||
SpliceNull {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TransportPacketWrite for SpliceNull {
|
impl TransportPacketWrite for SpliceNull {
|
||||||
fn write_to<W>(&self, _: &mut W) -> anyhow::Result<()>
|
fn write_to<W>(&self, _: &mut W) -> anyhow::Result<u32>
|
||||||
where
|
where
|
||||||
W: io::Write,
|
W: io::Write,
|
||||||
{
|
{
|
||||||
Ok(())
|
Ok(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,7 +51,7 @@ impl TimeSignal {
|
||||||
|
|
||||||
impl TransportPacketWrite for TimeSignal {
|
impl TransportPacketWrite for TimeSignal {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn write_to<W>(&self, buffer: &mut W) -> anyhow::Result<()>
|
fn write_to<W>(&self, buffer: &mut W) -> anyhow::Result<u32>
|
||||||
where
|
where
|
||||||
W: Write,
|
W: Write,
|
||||||
{
|
{
|
||||||
|
@ -131,7 +125,7 @@ mod tests {
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
#[test]
|
#[test]
|
||||||
fn serialize_splice_null() -> Result<()> {
|
fn serialize_splice_null() -> Result<()> {
|
||||||
let splice_null = SpliceNull::new();
|
let splice_null = SpliceNull::default();
|
||||||
assert_json_eq!(serde_json::to_value(&splice_null)?, serde_json::json!({}));
|
assert_json_eq!(serde_json::to_value(&splice_null)?, serde_json::json!({}));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,7 +52,7 @@ pub(crate) trait SpliceDescriptorExt {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SpliceDescriptor {
|
impl SpliceDescriptor {
|
||||||
pub(crate) fn write_to<W>(&mut self, buffer: &mut W) -> anyhow::Result<()>
|
pub(crate) fn write_to<W>(&mut self, buffer: &mut W) -> anyhow::Result<u32>
|
||||||
where
|
where
|
||||||
W: io::Write,
|
W: io::Write,
|
||||||
{
|
{
|
||||||
|
@ -65,17 +65,6 @@ impl SpliceDescriptor {
|
||||||
SpliceDescriptor::Unknown(_, _, _) => unimplemented!(),
|
SpliceDescriptor::Unknown(_, _, _) => unimplemented!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn len(&self) -> u8 {
|
|
||||||
match self {
|
|
||||||
SpliceDescriptor::Avail => 0,
|
|
||||||
SpliceDescriptor::DTMF => 0,
|
|
||||||
SpliceDescriptor::Segmentation(segmentation) => segmentation.len(),
|
|
||||||
SpliceDescriptor::Time => 0,
|
|
||||||
SpliceDescriptor::Audio => 0,
|
|
||||||
SpliceDescriptor::Unknown(_, _, _) => 0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<SegmentationDescriptor> for SpliceDescriptor {
|
impl From<SegmentationDescriptor> for SpliceDescriptor {
|
||||||
|
|
|
@ -30,16 +30,18 @@ pub struct SegmentationDescriptor {
|
||||||
sub_segment_num: u8,
|
sub_segment_num: u8,
|
||||||
sub_segments_expected: u8,
|
sub_segments_expected: u8,
|
||||||
|
|
||||||
descriptor_length: u8,
|
descriptor_length: Option<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
mod serde_serialization {
|
mod serde_serialization {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::ticks_to_secs;
|
use crate::ticks_to_secs;
|
||||||
|
use crate::time::format_duration;
|
||||||
use ascii::AsciiStr;
|
use ascii::AsciiStr;
|
||||||
use serde::ser::{Error, Serialize, SerializeStruct, Serializer};
|
use serde::ser::{Error, Serialize, SerializeStruct, Serializer};
|
||||||
use std::fmt::{format, LowerHex};
|
use std::fmt::{format, LowerHex};
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
impl Serialize for SegmentationDescriptor {
|
impl Serialize for SegmentationDescriptor {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
@ -129,13 +131,12 @@ mod serde_serialization {
|
||||||
}
|
}
|
||||||
state.serialize_field("components", &self.components)?;
|
state.serialize_field("components", &self.components)?;
|
||||||
if self.segmentation_duration_flag {
|
if self.segmentation_duration_flag {
|
||||||
|
let duration_secs = ticks_to_secs(self.segmentation_duration);
|
||||||
|
state.serialize_field("segmentation_duration", &self.segmentation_duration)?;
|
||||||
|
state.serialize_field("segmentation_duration_secs", &duration_secs)?;
|
||||||
state.serialize_field(
|
state.serialize_field(
|
||||||
"segmentation_duration",
|
"segmentation_duration_human",
|
||||||
&ticks_to_secs(self.segmentation_duration),
|
&format_duration(Duration::from_secs_f64(duration_secs)).to_string(),
|
||||||
)?;
|
|
||||||
state.serialize_field(
|
|
||||||
"segmentation_duration_ticks",
|
|
||||||
&self.segmentation_duration,
|
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
state.serialize_field(
|
state.serialize_field(
|
||||||
|
@ -204,14 +205,14 @@ mod serde_serialization {
|
||||||
// TODO: serialize as struct when variant is MPU and MID
|
// TODO: serialize as struct when variant is MPU and MID
|
||||||
match self {
|
match self {
|
||||||
// if field is represented as a character, then show with textual representation
|
// if field is represented as a character, then show with textual representation
|
||||||
ISCI(v) | AdID(v) | TID(v) | AdID(v) | ADSInformation(v) | URI(v) | SCR(v) => {
|
ISCI(v) | AdID(v) | TID(v) | ADSInformation(v) | URI(v) | SCR(v) => {
|
||||||
serializer.serialize_str(v.as_str())
|
serializer.serialize_str(v.as_str())
|
||||||
}
|
}
|
||||||
// if field is represented as a number, then show as hex
|
// if field is represented as a number, then show as hex
|
||||||
ISAN(v) | EIDR(v) | UUID(v) => serializer.serialize_str(&format!("0x{:x}", v)),
|
ISAN(v) | EIDR(v) | UUID(v) => serializer.serialize_str(&format!("0x{:x}", v)),
|
||||||
ISANDeprecated(v) | AiringID(v) => serializer.serialize_str(&format!("0x{:x}", v)),
|
ISANDeprecated(v) | AiringID(v) => serializer.serialize_str(&format!("0x{:x}", v)),
|
||||||
// everything else show as hex
|
// everything else show as hex, we skip the first byte (which is the length)
|
||||||
_ => serializer.serialize_str(&format!("0x{}", hex::encode(data[1..].to_vec()))),
|
_ => serializer.serialize_str(&format!("0x{}", hex::encode(&data[1..]))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -286,11 +287,7 @@ impl SegmentationDescriptor {
|
||||||
self.sub_segments_expected = sub_segments_expected;
|
self.sub_segments_expected = sub_segments_expected;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn len(&self) -> u8 {
|
pub(crate) fn write_to<W>(&mut self, buffer: &mut W) -> anyhow::Result<u32>
|
||||||
self.descriptor_length
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn write_to<W>(&mut self, buffer: &mut W) -> anyhow::Result<()>
|
|
||||||
where
|
where
|
||||||
W: io::Write,
|
W: io::Write,
|
||||||
{
|
{
|
||||||
|
@ -349,13 +346,19 @@ impl SegmentationDescriptor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let descriptor_length = recorder.bytes_written() as u8;
|
||||||
|
|
||||||
|
// Actually write to the output buffer, now we know the total size we need to write out
|
||||||
let mut buffer = BitWriter::endian(buffer, BigEndian);
|
let mut buffer = BitWriter::endian(buffer, BigEndian);
|
||||||
buffer.write(8, self.splice_descriptor_tag())?;
|
buffer.write(8, self.splice_descriptor_tag())?;
|
||||||
buffer.write(8, recorder.bytes_written() as u8)?;
|
buffer.write(8, descriptor_length)?;
|
||||||
self.descriptor_length = recorder.bytes_written() as u8;
|
|
||||||
recorder.playback(&mut buffer)?;
|
recorder.playback(&mut buffer)?;
|
||||||
|
|
||||||
Ok(())
|
// This field is used when serializing the Segmentation Descriptor with serde
|
||||||
|
self.descriptor_length = Some(descriptor_length);
|
||||||
|
|
||||||
|
// This is the full size of the descriptor, which includes the 2 bytes of the tag and the length
|
||||||
|
Ok(descriptor_length as u32 + 2)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1249,8 +1252,9 @@ mod tests {
|
||||||
"archive_allowed_flag": true,
|
"archive_allowed_flag": true,
|
||||||
"device_restrictions": "None",
|
"device_restrictions": "None",
|
||||||
"components": [],
|
"components": [],
|
||||||
"segmentation_duration": 307.0,
|
"segmentation_duration": 27630000,
|
||||||
"segmentation_duration_ticks": 27630000,
|
"segmentation_duration_secs": 307.0,
|
||||||
|
"segmentation_duration_human": "5m 7s",
|
||||||
"segmentation_upid_type": "0x08",
|
"segmentation_upid_type": "0x08",
|
||||||
"segmentation_upid_type_name": "AiringID",
|
"segmentation_upid_type_name": "AiringID",
|
||||||
"segmentation_upid_length": 8,
|
"segmentation_upid_length": 8,
|
||||||
|
|
101
src/info.rs
101
src/info.rs
|
@ -134,14 +134,13 @@ where
|
||||||
pub fn into_encoded(mut self) -> anyhow::Result<SpliceInfoSection<C, EncodedData>> {
|
pub fn into_encoded(mut self) -> anyhow::Result<SpliceInfoSection<C, EncodedData>> {
|
||||||
// Write splice command to a temporary buffer
|
// Write splice command to a temporary buffer
|
||||||
let mut splice_data = Vec::new();
|
let mut splice_data = Vec::new();
|
||||||
self.state.splice_command.write_to(&mut splice_data)?;
|
let splice_command_length = self.state.splice_command.write_to(&mut splice_data)? as u16;
|
||||||
|
|
||||||
// Write the descriptors to a temporary buffer
|
// Write the descriptors to a temporary buffer
|
||||||
let mut descriptor_data = Vec::new();
|
let mut descriptor_data = Vec::new();
|
||||||
let mut descriptor_loop_length = 0;
|
let mut descriptor_loop_length = 0;
|
||||||
for descriptor in &mut self.state.descriptors {
|
for descriptor in &mut self.state.descriptors {
|
||||||
descriptor.write_to(&mut descriptor_data)?;
|
descriptor_loop_length += descriptor.write_to(&mut descriptor_data)? as u16;
|
||||||
descriptor_loop_length += descriptor.len() as u16;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start writing the final output to a temporary buffer
|
// Start writing the final output to a temporary buffer
|
||||||
|
@ -155,8 +154,9 @@ where
|
||||||
// We know the section length by computing all known fixed size elements from now plus the
|
// We know the section length by computing all known fixed size elements from now plus the
|
||||||
// splice command length and descriptors which are also known by now
|
// splice command length and descriptors which are also known by now
|
||||||
const FIXED_INFO_SIZE_BYTES: usize = (8 + 1 + 6 + 33 + 8 + 12 + 12 + 8 + 16 + 32) / 8;
|
const FIXED_INFO_SIZE_BYTES: usize = (8 + 1 + 6 + 33 + 8 + 12 + 12 + 8 + 16 + 32) / 8;
|
||||||
let mut section_length =
|
let mut section_length = (FIXED_INFO_SIZE_BYTES
|
||||||
(FIXED_INFO_SIZE_BYTES + splice_data.len() + descriptor_loop_length as usize) as u16;
|
+ splice_command_length as usize
|
||||||
|
+ descriptor_loop_length as usize) as u16;
|
||||||
if self.state.encrypted_packet {
|
if self.state.encrypted_packet {
|
||||||
section_length += 4;
|
section_length += 4;
|
||||||
}
|
}
|
||||||
|
@ -168,7 +168,6 @@ where
|
||||||
buffer.write(33, self.state.pts_adjustment)?;
|
buffer.write(33, self.state.pts_adjustment)?;
|
||||||
buffer.write(8, self.state.cw_index)?;
|
buffer.write(8, self.state.cw_index)?;
|
||||||
buffer.write(12, self.state.tier)?;
|
buffer.write(12, self.state.tier)?;
|
||||||
let splice_command_length = splice_data.len() as u16;
|
|
||||||
buffer.write(12, splice_command_length)?;
|
buffer.write(12, splice_command_length)?;
|
||||||
let splice_command_type = self.state.splice_command.splice_command_type();
|
let splice_command_type = self.state.splice_command.splice_command_type();
|
||||||
buffer.write(8, u8::from(splice_command_type))?;
|
buffer.write(8, u8::from(splice_command_type))?;
|
||||||
|
@ -282,8 +281,10 @@ impl From<EncryptionAlgorithm> for u8 {
|
||||||
mod serde_serialization {
|
mod serde_serialization {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::ticks_to_secs;
|
use crate::ticks_to_secs;
|
||||||
|
use crate::time::format_duration;
|
||||||
use serde::ser::{Serialize, SerializeStruct, Serializer};
|
use serde::ser::{Serialize, SerializeStruct, Serializer};
|
||||||
use std::fmt::LowerHex;
|
use std::fmt::LowerHex;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
impl<C> Serialize for SpliceInfoSection<C, EncodedData>
|
impl<C> Serialize for SpliceInfoSection<C, EncodedData>
|
||||||
where
|
where
|
||||||
|
@ -316,8 +317,13 @@ mod serde_serialization {
|
||||||
"encryption_algorithm",
|
"encryption_algorithm",
|
||||||
&u8::from(self.state.encryption_algorithm),
|
&u8::from(self.state.encryption_algorithm),
|
||||||
)?;
|
)?;
|
||||||
state.serialize_field("pts_adjustment", &ticks_to_secs(self.state.pts_adjustment))?;
|
state.serialize_field("pts_adjustment", &self.state.pts_adjustment)?;
|
||||||
state.serialize_field("pts_adjustment_ticks", &self.state.pts_adjustment)?;
|
let pts_adjustment_secs = ticks_to_secs(self.state.pts_adjustment);
|
||||||
|
state.serialize_field("pts_adjustment_secs", &pts_adjustment_secs)?;
|
||||||
|
state.serialize_field(
|
||||||
|
"pts_adjustment_human",
|
||||||
|
&format_duration(Duration::from_secs_f64(pts_adjustment_secs)).to_string(),
|
||||||
|
)?;
|
||||||
state.serialize_field("cw_index", &as_hex(self.state.cw_index))?;
|
state.serialize_field("cw_index", &as_hex(self.state.cw_index))?;
|
||||||
state.serialize_field("tier", &as_hex(self.state.tier))?;
|
state.serialize_field("tier", &as_hex(self.state.tier))?;
|
||||||
state.serialize_field("splice_command_length", &self.encoded.splice_command_length)?;
|
state.serialize_field("splice_command_length", &self.encoded.splice_command_length)?;
|
||||||
|
@ -350,7 +356,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn write_splice_null_as_base64() -> Result<()> {
|
fn write_splice_null_as_base64() -> Result<()> {
|
||||||
let splice = SpliceInfoSection::new(SpliceNull::new());
|
let splice = SpliceInfoSection::new(SpliceNull::default());
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
splice.into_encoded()?.to_base64(),
|
splice.into_encoded()?.to_base64(),
|
||||||
|
@ -362,7 +368,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn write_splice_null_as_hex() -> Result<()> {
|
fn write_splice_null_as_hex() -> Result<()> {
|
||||||
let splice = SpliceInfoSection::new(SpliceNull::new());
|
let splice = SpliceInfoSection::new(SpliceNull::default());
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
splice.into_encoded()?.to_hex(),
|
splice.into_encoded()?.to_hex(),
|
||||||
|
@ -372,8 +378,7 @@ mod tests {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
fn spec_14_1_example_time_signal() -> Result<SpliceInfoSection<TimeSignal, EncodedData>> {
|
||||||
fn compliance_spec_14_1_example_time_signal_as_base64() -> Result<()> {
|
|
||||||
let mut splice = SpliceInfoSection::new(TimeSignal::from(0x072bd0050u64));
|
let mut splice = SpliceInfoSection::new(TimeSignal::from(0x072bd0050u64));
|
||||||
splice.set_cw_index(0xff);
|
splice.set_cw_index(0xff);
|
||||||
|
|
||||||
|
@ -392,8 +397,14 @@ mod tests {
|
||||||
|
|
||||||
splice.add_descriptor(descriptor.into());
|
splice.add_descriptor(descriptor.into());
|
||||||
|
|
||||||
|
Ok(splice.into_encoded()?)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn compliance_spec_14_1_example_time_signal_as_base64() -> Result<()> {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
splice.into_encoded()?.to_base64(),
|
spec_14_1_example_time_signal()?.to_base64(),
|
||||||
|
// This example was encoded using the threefive Python library
|
||||||
"/DA2AAAAAAAA///wBQb+cr0AUAAgAh5DVUVJSAAAjn/PAAGlmbAICAAAAAAsoKGKNAIAmsm2waDx"
|
"/DA2AAAAAAAA///wBQb+cr0AUAAgAh5DVUVJSAAAjn/PAAGlmbAICAAAAAAsoKGKNAIAmsm2waDx"
|
||||||
.to_string()
|
.to_string()
|
||||||
);
|
);
|
||||||
|
@ -402,26 +413,9 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn compliance_spec_14_1_example_time_signal_as_hex() -> Result<()> {
|
fn compliance_spec_14_1_example_time_signal_as_hex() -> Result<()> {
|
||||||
let mut splice = SpliceInfoSection::new(TimeSignal::from(0x072bd0050u64));
|
|
||||||
splice.set_cw_index(0xff);
|
|
||||||
|
|
||||||
let mut descriptor = SegmentationDescriptor::default();
|
|
||||||
descriptor.set_segmentation_event_id(0x4800008e);
|
|
||||||
descriptor.set_program_segmentation_flag(true);
|
|
||||||
descriptor.set_segmentation_duration_flag(true);
|
|
||||||
descriptor.set_no_regional_blackout_flag(true);
|
|
||||||
descriptor.set_archive_allowed_flag(true);
|
|
||||||
descriptor.set_segmentation_duration(27630000);
|
|
||||||
descriptor.set_segmentation_upid(SegmentationUpid::AiringID(0x2ca0a18a));
|
|
||||||
descriptor.set_segmentation_type(SegmentationType::ProviderPlacementOpportunityStart);
|
|
||||||
descriptor.set_segment_num(2);
|
|
||||||
descriptor.set_sub_segment_num(154);
|
|
||||||
descriptor.set_sub_segments_expected(201);
|
|
||||||
|
|
||||||
splice.add_descriptor(descriptor.into());
|
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
splice.into_encoded()?.to_hex(),
|
spec_14_1_example_time_signal()?.to_hex(),
|
||||||
|
// This example was encoded using the threefive Python library
|
||||||
"0xfc3036000000000000fffff00506fe72bd00500020021e435545494800008e7fcf0001a599b00808000000002ca0a18a3402009ac9b6c1a0f1".to_string()
|
"0xfc3036000000000000fffff00506fe72bd00500020021e435545494800008e7fcf0001a599b00808000000002ca0a18a3402009ac9b6c1a0f1".to_string()
|
||||||
);
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -430,36 +424,19 @@ mod tests {
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
#[test]
|
#[test]
|
||||||
fn compliance_spec_14_1_example_time_signal_as_json() -> Result<()> {
|
fn compliance_spec_14_1_example_time_signal_as_json() -> Result<()> {
|
||||||
let mut splice = SpliceInfoSection::new(TimeSignal::from(0x072bd0050u64));
|
|
||||||
splice.set_cw_index(0xff);
|
|
||||||
|
|
||||||
let mut descriptor = SegmentationDescriptor::default();
|
|
||||||
descriptor.set_segmentation_event_id(0x4800008e);
|
|
||||||
descriptor.set_program_segmentation_flag(true);
|
|
||||||
descriptor.set_segmentation_duration_flag(true);
|
|
||||||
descriptor.set_no_regional_blackout_flag(true);
|
|
||||||
descriptor.set_archive_allowed_flag(true);
|
|
||||||
descriptor.set_segmentation_duration(27630000);
|
|
||||||
descriptor.set_segmentation_upid(SegmentationUpid::AiringID(0x2ca0a18a));
|
|
||||||
descriptor.set_segmentation_type(SegmentationType::ProviderPlacementOpportunityStart);
|
|
||||||
descriptor.set_segment_num(2);
|
|
||||||
descriptor.set_sub_segment_num(154);
|
|
||||||
descriptor.set_sub_segments_expected(201);
|
|
||||||
|
|
||||||
splice.add_descriptor(descriptor.into());
|
|
||||||
|
|
||||||
let expected_json: serde_json::Value = serde_json::from_str(
|
let expected_json: serde_json::Value = serde_json::from_str(
|
||||||
r#"{
|
r#"{
|
||||||
"table_id": "0xfc",
|
"table_id": "0xfc",
|
||||||
"section_syntax_indicator": false,
|
"section_syntax_indicator": false,
|
||||||
"private_indicator": false,
|
"private_indicator": false,
|
||||||
"sap_type": "0x3",
|
"sap_type": "0x3",
|
||||||
"section_length": 52,
|
"section_length": 54,
|
||||||
"protocol_version": 0,
|
"protocol_version": 0,
|
||||||
"encrypted_packet": false,
|
"encrypted_packet": false,
|
||||||
"encryption_algorithm": 0,
|
"encryption_algorithm": 0,
|
||||||
"pts_adjustment": 0.0,
|
"pts_adjustment": 0,
|
||||||
"pts_adjustment_ticks": 0,
|
"pts_adjustment_secs": 0.0,
|
||||||
|
"pts_adjustment_human": "0s",
|
||||||
"cw_index": "0xff",
|
"cw_index": "0xff",
|
||||||
"tier": "0xfff",
|
"tier": "0xfff",
|
||||||
"splice_command_length": 5,
|
"splice_command_length": 5,
|
||||||
|
@ -473,7 +450,7 @@ mod tests {
|
||||||
"pts_time": 21388.766756,
|
"pts_time": 21388.766756,
|
||||||
"pts_time_ticks": 1924989008
|
"pts_time_ticks": 1924989008
|
||||||
},
|
},
|
||||||
"descriptor_loop_length": 30,
|
"descriptor_loop_length": 32,
|
||||||
"descriptors": [
|
"descriptors": [
|
||||||
{
|
{
|
||||||
"name": "Segmentation Descriptor",
|
"name": "Segmentation Descriptor",
|
||||||
|
@ -490,8 +467,9 @@ mod tests {
|
||||||
"archive_allowed_flag": true,
|
"archive_allowed_flag": true,
|
||||||
"device_restrictions": "None",
|
"device_restrictions": "None",
|
||||||
"components": [],
|
"components": [],
|
||||||
"segmentation_duration": 307.0,
|
"segmentation_duration": 27630000,
|
||||||
"segmentation_duration_ticks": 27630000,
|
"segmentation_duration_secs": 307.0,
|
||||||
|
"segmentation_duration_human": "5m 7s",
|
||||||
"segmentation_upid_type": "0x08",
|
"segmentation_upid_type": "0x08",
|
||||||
"segmentation_upid_type_name": "AiringID",
|
"segmentation_upid_type_name": "AiringID",
|
||||||
"segmentation_upid_length": 8,
|
"segmentation_upid_length": 8,
|
||||||
|
@ -504,12 +482,12 @@ mod tests {
|
||||||
"sub_segments_expected": 201
|
"sub_segments_expected": 201
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"crc_32": "0x926218f0"
|
"crc_32": "0xb6c1a0f1"
|
||||||
}"#,
|
}"#,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
assert_json_eq!(
|
assert_json_eq!(
|
||||||
serde_json::to_value(&splice.into_encoded()?)?,
|
serde_json::to_value(&spec_14_1_example_time_signal()?)?,
|
||||||
expected_json
|
expected_json
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -519,7 +497,7 @@ mod tests {
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
#[test]
|
#[test]
|
||||||
fn serialize_as_json() -> Result<()> {
|
fn serialize_as_json() -> Result<()> {
|
||||||
let splice = SpliceInfoSection::new(SpliceNull::new());
|
let splice = SpliceInfoSection::new(SpliceNull::default());
|
||||||
|
|
||||||
assert_json_eq!(
|
assert_json_eq!(
|
||||||
serde_json::to_value(&splice.into_encoded()?)?,
|
serde_json::to_value(&splice.into_encoded()?)?,
|
||||||
|
@ -532,8 +510,9 @@ mod tests {
|
||||||
"protocol_version": 0,
|
"protocol_version": 0,
|
||||||
"encrypted_packet": false,
|
"encrypted_packet": false,
|
||||||
"encryption_algorithm": 0,
|
"encryption_algorithm": 0,
|
||||||
"pts_adjustment": 0.0,
|
"pts_adjustment": 0,
|
||||||
"pts_adjustment_ticks": 0,
|
"pts_adjustment_secs": 0.0,
|
||||||
|
"pts_adjustment_human": "0s",
|
||||||
"cw_index": "0x0",
|
"cw_index": "0x0",
|
||||||
"tier": "0xfff",
|
"tier": "0xfff",
|
||||||
"splice_command_length": 0,
|
"splice_command_length": 0,
|
||||||
|
|
|
@ -15,7 +15,7 @@ pub use commands::SpliceNull;
|
||||||
pub use info::{EncryptionAlgorithm, SAPType, SpliceInfoSection};
|
pub use info::{EncryptionAlgorithm, SAPType, SpliceInfoSection};
|
||||||
|
|
||||||
pub trait TransportPacketWrite {
|
pub trait TransportPacketWrite {
|
||||||
fn write_to<W>(&self, buffer: &mut W) -> anyhow::Result<()>
|
fn write_to<W>(&self, buffer: &mut W) -> anyhow::Result<u32>
|
||||||
where
|
where
|
||||||
W: io::Write;
|
W: io::Write;
|
||||||
}
|
}
|
||||||
|
|
138
src/time.rs
138
src/time.rs
|
@ -1,9 +1,9 @@
|
||||||
use crate::{ClockTimeExt, CueError, TransportPacketWrite};
|
use crate::{BytesWritten, ClockTimeExt, CueError, TransportPacketWrite};
|
||||||
use bitstream_io::{BigEndian, BitWrite, BitWriter};
|
use bitstream_io::{BigEndian, BitRecorder, BitWrite, BitWriter};
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
use serde::{Serialize, Serializer};
|
use serde::{Serialize, Serializer};
|
||||||
use std::io;
|
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
use std::{fmt, io};
|
||||||
|
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize))]
|
#[cfg_attr(feature = "serde", derive(Serialize))]
|
||||||
pub struct SpliceTime {
|
pub struct SpliceTime {
|
||||||
|
@ -57,19 +57,24 @@ impl SpliceTime {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TransportPacketWrite for SpliceTime {
|
impl TransportPacketWrite for SpliceTime {
|
||||||
fn write_to<W>(&self, buffer: &mut W) -> anyhow::Result<()>
|
fn write_to<W>(&self, buffer: &mut W) -> anyhow::Result<u32>
|
||||||
where
|
where
|
||||||
W: io::Write,
|
W: io::Write,
|
||||||
{
|
{
|
||||||
let mut buffer = BitWriter::endian(buffer, BigEndian);
|
let mut recorder = BitRecorder::<u32, BigEndian>::new();
|
||||||
buffer.write_bit(self.time_specified_flag)?;
|
|
||||||
|
recorder.write_bit(self.time_specified_flag)?;
|
||||||
if self.time_specified_flag {
|
if self.time_specified_flag {
|
||||||
buffer.write(6, 0x3f)?;
|
recorder.write(6, 0x3f)?;
|
||||||
buffer.write(33, self.pts_time)?;
|
recorder.write(33, self.pts_time)?;
|
||||||
} else {
|
} else {
|
||||||
buffer.write(7, 0x7f)?;
|
recorder.write(7, 0x7f)?;
|
||||||
}
|
}
|
||||||
Ok(())
|
|
||||||
|
let mut buffer = BitWriter::endian(buffer, BigEndian);
|
||||||
|
recorder.playback(&mut buffer)?;
|
||||||
|
|
||||||
|
Ok(recorder.bytes_written())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,3 +83,116 @@ impl From<Duration> for SpliceTime {
|
||||||
Self::from_ticks(duration.to_90k())
|
Self::from_ticks(duration.to_90k())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Copyright (c) 2016 The humantime Developers
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
/// Formats duration into a human-readable string
|
||||||
|
///
|
||||||
|
/// Note: this format is guaranteed to have same value when using
|
||||||
|
/// parse_duration, but we can change some details of the exact composition
|
||||||
|
/// of the value.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use std::time::Duration;
|
||||||
|
/// use humantime::format_duration;
|
||||||
|
///
|
||||||
|
/// let val1 = Duration::new(9420, 0);
|
||||||
|
/// assert_eq!(format_duration(val1).to_string(), "2h 37m");
|
||||||
|
/// let val2 = Duration::new(0, 32_000_000);
|
||||||
|
/// assert_eq!(format_duration(val2).to_string(), "32ms");
|
||||||
|
/// ```
|
||||||
|
pub(crate) fn format_duration(val: Duration) -> FormattedDuration {
|
||||||
|
FormattedDuration(val)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn item_plural(f: &mut fmt::Formatter, started: &mut bool, name: &str, value: u64) -> fmt::Result {
|
||||||
|
if value > 0 {
|
||||||
|
if *started {
|
||||||
|
f.write_str(" ")?;
|
||||||
|
}
|
||||||
|
write!(f, "{}{}", value, name)?;
|
||||||
|
if value > 1 {
|
||||||
|
f.write_str("s")?;
|
||||||
|
}
|
||||||
|
*started = true;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
fn item(f: &mut fmt::Formatter, started: &mut bool, name: &str, value: u32) -> fmt::Result {
|
||||||
|
if value > 0 {
|
||||||
|
if *started {
|
||||||
|
f.write_str(" ")?;
|
||||||
|
}
|
||||||
|
write!(f, "{}{}", value, name)?;
|
||||||
|
*started = true;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A wrapper type that allows you to Display a Duration
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub(crate) struct FormattedDuration(Duration);
|
||||||
|
|
||||||
|
impl FormattedDuration {
|
||||||
|
/// Returns a reference to the [`Duration`][] that is being formatted.
|
||||||
|
pub fn get_ref(&self) -> &Duration {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for FormattedDuration {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
let secs = self.0.as_secs();
|
||||||
|
let nanos = self.0.subsec_nanos();
|
||||||
|
|
||||||
|
if secs == 0 && nanos == 0 {
|
||||||
|
f.write_str("0s")?;
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let years = secs / 31_557_600; // 365.25d
|
||||||
|
let ydays = secs % 31_557_600;
|
||||||
|
let months = ydays / 2_630_016; // 30.44d
|
||||||
|
let mdays = ydays % 2_630_016;
|
||||||
|
let days = mdays / 86400;
|
||||||
|
let day_secs = mdays % 86400;
|
||||||
|
let hours = day_secs / 3600;
|
||||||
|
let minutes = day_secs % 3600 / 60;
|
||||||
|
let seconds = day_secs % 60;
|
||||||
|
|
||||||
|
let millis = nanos / 1_000_000;
|
||||||
|
let micros = nanos / 1000 % 1000;
|
||||||
|
let nanosec = nanos % 1000;
|
||||||
|
|
||||||
|
let started = &mut false;
|
||||||
|
item_plural(f, started, "year", years)?;
|
||||||
|
item_plural(f, started, "month", months)?;
|
||||||
|
item_plural(f, started, "day", days)?;
|
||||||
|
item(f, started, "h", hours as u32)?;
|
||||||
|
item(f, started, "m", minutes as u32)?;
|
||||||
|
item(f, started, "s", seconds as u32)?;
|
||||||
|
item(f, started, "milli", millis)?;
|
||||||
|
item(f, started, "us", micros)?;
|
||||||
|
item(f, started, "ns", nanosec)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue