Use a Bytes[Mut] for all Chunk data
This commit is contained in:
parent
5e2e1bcf83
commit
2400720d03
2 changed files with 13 additions and 22 deletions
33
src/chunk.rs
33
src/chunk.rs
|
@ -1,4 +1,4 @@
|
||||||
use bytes::{Buf, Bytes};
|
use bytes::{Buf, Bytes, BytesMut};
|
||||||
use futures3::prelude::*;
|
use futures3::prelude::*;
|
||||||
use std::{
|
use std::{
|
||||||
io::Cursor,
|
io::Cursor,
|
||||||
|
@ -15,10 +15,9 @@ pub struct ClusterHead {
|
||||||
pub keyframe: bool,
|
pub keyframe: bool,
|
||||||
pub start: u64,
|
pub start: u64,
|
||||||
pub end: u64,
|
pub end: u64,
|
||||||
/// space for a Cluster tag and a Timecode tag
|
/// a Cluster tag and a Timecode tag together take at most 15 bytes;
|
||||||
/// TODO: consider using a BytesMut here for simplicity
|
/// fortuitously, 15 bytes can be inlined in a Bytes handle even on 32-bit systems
|
||||||
bytes: [u8;16],
|
bytes: BytesMut,
|
||||||
bytes_used: u8
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ClusterHead {
|
impl ClusterHead {
|
||||||
|
@ -27,8 +26,7 @@ impl ClusterHead {
|
||||||
keyframe: false,
|
keyframe: false,
|
||||||
start: 0,
|
start: 0,
|
||||||
end: 0,
|
end: 0,
|
||||||
bytes: [0;16],
|
bytes: BytesMut::with_capacity(15),
|
||||||
bytes_used: 0
|
|
||||||
};
|
};
|
||||||
cluster_head.update_timecode(timecode);
|
cluster_head.update_timecode(timecode);
|
||||||
cluster_head
|
cluster_head
|
||||||
|
@ -37,11 +35,14 @@ impl ClusterHead {
|
||||||
let delta = self.end - self.start;
|
let delta = self.end - self.start;
|
||||||
self.start = timecode;
|
self.start = timecode;
|
||||||
self.end = self.start + delta;
|
self.end = self.start + delta;
|
||||||
let mut cursor = Cursor::new(self.bytes.as_mut());
|
let mut buffer = [0;15];
|
||||||
|
let mut cursor = Cursor::new(buffer.as_mut());
|
||||||
// buffer is sized so these should never fail
|
// buffer is sized so these should never fail
|
||||||
encode_webm_element(WebmElement::Cluster, &mut cursor).unwrap();
|
encode_webm_element(WebmElement::Cluster, &mut cursor).unwrap();
|
||||||
encode_webm_element(WebmElement::Timecode(timecode), &mut cursor).unwrap();
|
encode_webm_element(WebmElement::Timecode(timecode), &mut cursor).unwrap();
|
||||||
self.bytes_used = cursor.position() as u8;
|
self.bytes.clear();
|
||||||
|
let len = cursor.position() as usize;
|
||||||
|
self.bytes.extend_from_slice(&buffer[..len]);
|
||||||
}
|
}
|
||||||
pub fn observe_simpleblock_timecode(&mut self, timecode: i16) {
|
pub fn observe_simpleblock_timecode(&mut self, timecode: i16) {
|
||||||
let absolute_timecode = self.start + (timecode as u64);
|
let absolute_timecode = self.start + (timecode as u64);
|
||||||
|
@ -53,7 +54,7 @@ impl ClusterHead {
|
||||||
|
|
||||||
impl AsRef<[u8]> for ClusterHead {
|
impl AsRef<[u8]> for ClusterHead {
|
||||||
fn as_ref(&self) -> &[u8] {
|
fn as_ref(&self) -> &[u8] {
|
||||||
self.bytes[..self.bytes_used as usize].as_ref()
|
self.bytes.as_ref()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,22 +75,12 @@ impl Chunk {
|
||||||
pub fn into_bytes(self) -> Bytes {
|
pub fn into_bytes(self) -> Bytes {
|
||||||
match self {
|
match self {
|
||||||
Chunk::Headers {bytes, ..} => bytes,
|
Chunk::Headers {bytes, ..} => bytes,
|
||||||
Chunk::ClusterHead(cluster_head) => Bytes::from(cluster_head.as_ref()),
|
Chunk::ClusterHead(cluster_head) => cluster_head.bytes.freeze(),
|
||||||
Chunk::ClusterBody {bytes, ..} => bytes
|
Chunk::ClusterBody {bytes, ..} => bytes
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AsRef<[u8]> for Chunk {
|
|
||||||
fn as_ref(&self) -> &[u8] {
|
|
||||||
match self {
|
|
||||||
&Chunk::Headers {ref bytes, ..} => bytes.as_ref(),
|
|
||||||
&Chunk::ClusterHead(ref cluster_head) => cluster_head.as_ref(),
|
|
||||||
&Chunk::ClusterBody {ref bytes, ..} => bytes.as_ref()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum ChunkerState {
|
enum ChunkerState {
|
||||||
BuildingHeader(Cursor<Vec<u8>>),
|
BuildingHeader(Cursor<Vec<u8>>),
|
||||||
|
|
|
@ -44,6 +44,6 @@ pub fn run(args: &ArgMatches) -> Result<(), WebmetroError> {
|
||||||
}
|
}
|
||||||
|
|
||||||
Runtime::new().unwrap().block_on(chunk_stream.try_for_each(|chunk| {
|
Runtime::new().unwrap().block_on(chunk_stream.try_for_each(|chunk| {
|
||||||
ready(io::stdout().write_all(chunk.as_ref()).map_err(WebmetroError::from))
|
ready(io::stdout().write_all(&chunk.into_bytes()).map_err(WebmetroError::from))
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue