use super::error::{BuilderError, BuilderResult};
use crate::descriptors::{SegmentationDescriptor, SpliceDescriptor};
use crate::types::{SpliceCommand, SpliceInfoSection};
#[derive(Debug)]
pub struct SpliceInfoSectionBuilder {
pts_adjustment: u64,
tier: u16,
cw_index: u8,
splice_command: Option<SpliceCommand>,
descriptors: Vec<SpliceDescriptor>,
}
impl SpliceInfoSectionBuilder {
pub fn new() -> Self {
Self {
pts_adjustment: 0,
tier: 0xFFF, cw_index: 0x00, splice_command: None,
descriptors: Vec::new(),
}
}
pub fn pts_adjustment(mut self, pts_adjustment: u64) -> Self {
self.pts_adjustment = pts_adjustment & 0x1_FFFF_FFFF; self
}
pub fn tier(mut self, tier: u16) -> Self {
self.tier = tier & 0xFFF; self
}
pub fn cw_index(mut self, cw_index: u8) -> Self {
self.cw_index = cw_index;
self
}
pub fn splice_command(mut self, command: SpliceCommand) -> Self {
self.splice_command = Some(command);
self
}
pub fn splice_null(mut self) -> Self {
self.splice_command = Some(SpliceCommand::SpliceNull);
self
}
pub fn splice_insert(mut self, insert: crate::types::SpliceInsert) -> Self {
self.splice_command = Some(SpliceCommand::SpliceInsert(insert));
self
}
pub fn time_signal(mut self, time_signal: crate::types::TimeSignal) -> Self {
self.splice_command = Some(SpliceCommand::TimeSignal(time_signal));
self
}
pub fn add_descriptor(mut self, descriptor: SpliceDescriptor) -> Self {
self.descriptors.push(descriptor);
self
}
pub fn add_segmentation_descriptor(mut self, descriptor: SegmentationDescriptor) -> Self {
self.descriptors
.push(SpliceDescriptor::Segmentation(descriptor));
self
}
pub fn build(self) -> BuilderResult<SpliceInfoSection> {
let splice_command = self
.splice_command
.ok_or(BuilderError::MissingRequiredField("splice_command"))?;
let splice_command_type: u8 = (&splice_command).into();
use crate::encoding::Encodable;
let mut descriptor_loop_length = 0u16;
for descriptor in &self.descriptors {
descriptor_loop_length += descriptor.encoded_size() as u16;
}
let mut section = SpliceInfoSection {
table_id: 0xFC, section_syntax_indicator: 0, private_indicator: 0, sap_type: 0x3, section_length: 0, protocol_version: 0, encrypted_packet: 0, encryption_algorithm: 0, pts_adjustment: self.pts_adjustment,
cw_index: self.cw_index,
tier: self.tier,
splice_command_length: 0, splice_command_type,
splice_command,
descriptor_loop_length: 0, splice_descriptors: self.descriptors,
alignment_stuffing_bits: Vec::new(), e_crc_32: None, crc_32: 0, };
section.splice_command_length = section.splice_command.encoded_size() as u16;
section.descriptor_loop_length = descriptor_loop_length;
section.section_length = (section.encoded_size() - 3) as u16;
Ok(section)
}
}
impl Default for SpliceInfoSectionBuilder {
fn default() -> Self {
Self::new()
}
}