From dd376e146eb2ccc8d6070bbe38085323076b87cd Mon Sep 17 00:00:00 2001 From: Tangent 128 Date: Sat, 12 Aug 2017 16:48:24 -0400 Subject: [PATCH] Implement encoding SimpleBlocks --- src/bin/stub.rs | 7 +++++++ src/webm.rs | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/bin/stub.rs b/src/bin/stub.rs index 617437c..c545f56 100644 --- a/src/bin/stub.rs +++ b/src/bin/stub.rs @@ -12,6 +12,13 @@ pub fn main() { encode_webm_element(WebmElement::Cluster, &mut cursor).unwrap(); encode_webm_element(WebmElement::Timecode(0), &mut cursor).unwrap(); + encode_webm_element(WebmElement::SimpleBlock { + track: 3, + flags: 0x0, + timecode: 123, + data: "Hello, World".as_bytes() + }, &mut cursor).unwrap(); + encode_webm_element(WebmElement::Cluster, &mut cursor).unwrap(); encode_webm_element(WebmElement::Timecode(1000), &mut cursor).unwrap(); diff --git a/src/webm.rs b/src/webm.rs index 03b2c96..7f2f7a4 100644 --- a/src/webm.rs +++ b/src/webm.rs @@ -1,5 +1,5 @@ -use std::io::{Error as IoError, ErrorKind, Result as IoResult, Write, Seek}; -use bytes::{BigEndian, ByteOrder}; +use std::io::{Cursor, Error as IoError, ErrorKind, Result as IoResult, Write, Seek}; +use bytes::{BigEndian, BufMut, ByteOrder}; use ebml::*; const SEGMENT_ID: u64 = 0x08538067; @@ -80,6 +80,33 @@ fn decode_simple_block(bytes: &[u8]) -> Result { } } +pub fn encode_simple_block(element: WebmElement, output: &mut T) -> IoResult<()> { + if let WebmElement::SimpleBlock{ + track, + timecode, + flags, + data + } = element { + // limiting number of tracks for now + if track > 31 { + return Err(IoError::new(ErrorKind::InvalidInput, WriteError::OutOfRange)); + } + let header_len = 1 + 2 + 1; + encode_tag_header(SIMPLE_BLOCK_ID, Varint::Value((header_len + data.len()) as u64), output)?; + + encode_varint(Varint::Value(track), output)?; + + let mut buffer = Cursor::new([0; 3]); + buffer.put_i16::(timecode); + buffer.put_u8(flags); + + output.write_all(&buffer.get_ref()[..])?; + output.write_all(data) + } else { + Err(IoError::new(ErrorKind::InvalidInput, WriteError::OutOfRange)) + } +} + pub fn encode_webm_element(element: WebmElement, output: &mut T) -> IoResult<()> { match element { WebmElement::EbmlHead => encode_element(EBML_HEAD_ID, output, |output| { @@ -90,6 +117,7 @@ pub fn encode_webm_element(element: WebmElement, output: &mut T WebmElement::Cues => Ok(()), WebmElement::Cluster => encode_tag_header(CLUSTER_ID, Varint::Unknown, output), WebmElement::Timecode(time) => encode_integer(TIMECODE_ID, time, output), + WebmElement::SimpleBlock {..} => encode_simple_block(element, output), _ => Err(IoError::new(ErrorKind::InvalidInput, WriteError::OutOfRange)) } }