From 70910ac3fdc02c7b7727a4a294f55134e9f5141d Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Mon, 29 Apr 2019 13:15:59 +0200 Subject: [PATCH] switch NAStream reference to Arc --- nihav-core/src/demuxers/mod.rs | 23 +++++++++++------------ nihav-core/src/frame.rs | 9 ++++++--- nihav-realmedia/src/demuxers/mod.rs | 1 - nihav-realmedia/src/demuxers/realmedia.rs | 8 ++++---- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/nihav-core/src/demuxers/mod.rs b/nihav-core/src/demuxers/mod.rs index 48529d4..b3546fe 100644 --- a/nihav-core/src/demuxers/mod.rs +++ b/nihav-core/src/demuxers/mod.rs @@ -1,4 +1,3 @@ -use std::rc::Rc; pub use crate::frame::*; pub use crate::io::byteio::*; @@ -23,12 +22,12 @@ pub trait DemuxCore<'a> { } pub trait NAPacketReader { - fn read_packet(&mut self, str: Rc, ts: NATimeInfo, keyframe: bool, size: usize) -> DemuxerResult; + fn read_packet(&mut self, str: NAStreamRef, ts: NATimeInfo, keyframe: bool, size: usize) -> DemuxerResult; fn fill_packet(&mut self, pkt: &mut NAPacket) -> DemuxerResult<()>; } impl<'a> NAPacketReader for ByteReader<'a> { - fn read_packet(&mut self, str: Rc, ts: NATimeInfo, kf: bool, size: usize) -> DemuxerResult { + fn read_packet(&mut self, str: NAStreamRef, ts: NATimeInfo, kf: bool, size: usize) -> DemuxerResult { let mut buf: Vec = Vec::with_capacity(size); if buf.capacity() < size { return Err(DemuxerError::MemoryError); } buf.resize(size, 0); @@ -47,7 +46,7 @@ impl<'a> NAPacketReader for ByteReader<'a> { } pub struct StreamManager { - streams: Vec>, + streams: Vec, ignored: Vec, no_ign: bool, } @@ -66,18 +65,18 @@ impl StreamManager { let stream_num = self.streams.len(); let mut str = stream.clone(); str.set_num(stream_num); - self.streams.push(Rc::new(str)); + self.streams.push(str.into_ref()); self.ignored.push(false); Some(stream_num) } - pub fn get_stream(&self, idx: usize) -> Option> { + pub fn get_stream(&self, idx: usize) -> Option { if idx < self.streams.len() { Some(self.streams[idx].clone()) } else { None } } - pub fn get_stream_by_id(&self, id: u32) -> Option> { + pub fn get_stream_by_id(&self, id: u32) -> Option { for i in 0..self.streams.len() { if self.streams[i].get_id() == id { return Some(self.streams[i].clone()); @@ -117,18 +116,18 @@ impl StreamManager { } pub struct StreamIter<'a> { - streams: &'a Vec>, + streams: &'a Vec, pos: usize, } impl<'a> StreamIter<'a> { - pub fn new(streams: &'a Vec>) -> Self { + pub fn new(streams: &'a Vec) -> Self { StreamIter { streams: streams, pos: 0 } } } impl<'a> Iterator for StreamIter<'a> { - type Item = Rc; + type Item = NAStreamRef; fn next(&mut self) -> Option { if self.pos >= self.streams.len() { return None; } @@ -150,10 +149,10 @@ impl<'a> Demuxer<'a> { streams: str, } } - pub fn get_stream(&self, idx: usize) -> Option> { + pub fn get_stream(&self, idx: usize) -> Option { self.streams.get_stream(idx) } - pub fn get_stream_by_id(&self, id: u32) -> Option> { + pub fn get_stream_by_id(&self, id: u32) -> Option { self.streams.get_stream_by_id(id) } pub fn get_num_streams(&self) -> usize { diff --git a/nihav-core/src/frame.rs b/nihav-core/src/frame.rs index bc3216b..d97850b 100644 --- a/nihav-core/src/frame.rs +++ b/nihav-core/src/frame.rs @@ -739,6 +739,8 @@ pub struct NAStream { tb_den: u32, } +pub type NAStreamRef = Arc; + pub fn reduce_timebase(tb_num: u32, tb_den: u32) -> (u32, u32) { if tb_num == 0 { return (tb_num, tb_den); } if (tb_den % tb_num) == 0 { return (1, tb_den / tb_num); } @@ -769,6 +771,7 @@ impl NAStream { self.tb_num = n; self.tb_den = d; } + pub fn into_ref(self) -> NAStreamRef { Arc::new(self) } } impl fmt::Display for NAStream { @@ -779,7 +782,7 @@ impl fmt::Display for NAStream { #[allow(dead_code)] pub struct NAPacket { - stream: Rc, + stream: NAStreamRef, ts: NATimeInfo, buffer: NABufferRef>, keyframe: bool, @@ -787,12 +790,12 @@ pub struct NAPacket { } impl NAPacket { - pub fn new(str: Rc, ts: NATimeInfo, kf: bool, vec: Vec) -> Self { + pub fn new(str: NAStreamRef, ts: NATimeInfo, kf: bool, vec: Vec) -> Self { // let mut vec: Vec = Vec::new(); // vec.resize(size, 0); NAPacket { stream: str, ts: ts, keyframe: kf, buffer: NABufferRef::new(vec) } } - pub fn get_stream(&self) -> Rc { self.stream.clone() } + pub fn get_stream(&self) -> NAStreamRef { self.stream.clone() } pub fn get_time_information(&self) -> NATimeInfo { self.ts } pub fn get_pts(&self) -> Option { self.ts.get_pts() } pub fn get_dts(&self) -> Option { self.ts.get_dts() } diff --git a/nihav-realmedia/src/demuxers/mod.rs b/nihav-realmedia/src/demuxers/mod.rs index 5acf1f4..ebe9ee3 100644 --- a/nihav-realmedia/src/demuxers/mod.rs +++ b/nihav-realmedia/src/demuxers/mod.rs @@ -1,4 +1,3 @@ -use std::rc::Rc; use nihav_core::demuxers::*; macro_rules! validate { diff --git a/nihav-realmedia/src/demuxers/realmedia.rs b/nihav-realmedia/src/demuxers/realmedia.rs index 4b6ecef..f3f73b9 100644 --- a/nihav-realmedia/src/demuxers/realmedia.rs +++ b/nihav-realmedia/src/demuxers/realmedia.rs @@ -136,7 +136,7 @@ impl RMAudioStream { } RMAudioStream { deint: deint, iinfo: iinfo, buf: buf, sub_packet: 0 } } - fn read_apackets(&mut self, queued_packets: &mut Vec, src: &mut ByteReader, stream: Rc, ts: u32, keyframe: bool, payload_size: usize) -> DemuxerResult { + fn read_apackets(&mut self, queued_packets: &mut Vec, src: &mut ByteReader, stream: NAStreamRef, ts: u32, keyframe: bool, payload_size: usize) -> DemuxerResult { let (tb_num, tb_den) = stream.get_timebase(); let ts = NATimeInfo::new(Some(ts as u64), None, None, tb_num, tb_den); @@ -379,7 +379,7 @@ fn read_14or30(src: &mut ByteReader) -> DemuxerResult<(bool, u32)> { } } -fn read_video_buf(src: &mut ByteReader, stream: Rc, ts: u32, keyframe: bool, frame_size: usize) -> DemuxerResult { +fn read_video_buf(src: &mut ByteReader, stream: NAStreamRef, ts: u32, keyframe: bool, frame_size: usize) -> DemuxerResult { let size = (frame_size as usize) + 9; let mut vec: Vec = Vec::with_capacity(size); vec.resize(size, 0); @@ -392,7 +392,7 @@ fn read_video_buf(src: &mut ByteReader, stream: Rc, ts: u32, keyframe: Ok(NAPacket::new(stream, ts, keyframe, vec)) } -fn read_multiple_frame(src: &mut ByteReader, stream: Rc, keyframe: bool, skip_mtype: bool) -> DemuxerResult { +fn read_multiple_frame(src: &mut ByteReader, stream: NAStreamRef, keyframe: bool, skip_mtype: bool) -> DemuxerResult { if !skip_mtype { let mtype = src.read_byte()?; validate!(mtype == 0xC0); @@ -554,7 +554,7 @@ println!(" got ainfo {:?}", ainfo); Ok(()) } #[allow(unused_variables)] - fn parse_packet_payload(src: &mut ByteReader, rmstream: &mut RMStreamType, stream: Rc, slice_buf: &mut Vec, queued_pkts: &mut Vec, keyframe: bool, ts: u32, payload_size: usize) -> DemuxerResult { + fn parse_packet_payload(src: &mut ByteReader, rmstream: &mut RMStreamType, stream: NAStreamRef, slice_buf: &mut Vec, queued_pkts: &mut Vec, keyframe: bool, ts: u32, payload_size: usize) -> DemuxerResult { match rmstream { RMStreamType::Video(ref mut vstr) => { -- 2.39.5