add interfaces for raw data stream handling
[nihav.git] / nihav-core / src / frame.rs
index 5499de1001b40b36446bec2cd8d99902bcf24d1e..70a054d3655842788839385122f1d8f2a2bc6b98 100644 (file)
@@ -390,6 +390,17 @@ impl NABufferType {
             _ => 0,
         }
     }
+    /// Truncates audio frame duration if possible.
+    pub fn truncate_audio(&mut self, len: usize) {
+        match *self {
+            NABufferType::AudioU8(ref mut ab)     => ab.truncate(len),
+            NABufferType::AudioI16(ref mut ab)    => ab.truncate(len),
+            NABufferType::AudioI32(ref mut ab)    => ab.truncate(len),
+            NABufferType::AudioF32(ref mut ab)    => ab.truncate(len),
+            NABufferType::AudioPacked(ref mut ab) => ab.truncate(len),
+            _ => {},
+        };
+    }
     /// Returns the distance between starts of two channels.
     pub fn get_audio_stride(&self) -> usize {
         match *self {
@@ -741,7 +752,7 @@ impl<T:Copy> NAVideoBufferPool<T> {
     }
     /// Clears the pool from all frames.
     pub fn reset(&mut self) {
-        self.pool.truncate(0);
+        self.pool.clear();
     }
 }
 
@@ -1433,6 +1444,37 @@ impl fmt::Display for NAPacket {
     }
 }
 
+/// Packet with a piece of data for a raw stream.
+pub struct NARawData {
+    stream:         NAStreamRef,
+    buffer:         NABufferRef<Vec<u8>>,
+}
+
+impl NARawData {
+    /// Constructs a new `NARawData` instance.
+    pub fn new(stream: NAStreamRef, vec: Vec<u8>) -> Self {
+        Self { stream, buffer: NABufferRef::new(vec) }
+    }
+    /// Constructs a new `NARawData` instance reusing a buffer reference.
+    pub fn new_from_refbuf(stream: NAStreamRef, buffer: NABufferRef<Vec<u8>>) -> Self {
+        Self { stream, buffer }
+    }
+    /// Returns information about the stream this data belongs to.
+    pub fn get_stream(&self) -> NAStreamRef { self.stream.clone() }
+    /// Returns a reference to packet data.
+    pub fn get_buffer(&self) -> NABufferRef<Vec<u8>> { self.buffer.clone() }
+    /// Assigns raw data to a new stream.
+    pub fn reassign(&mut self, stream: NAStreamRef) {
+        self.stream = stream;
+    }
+}
+
+impl fmt::Display for NARawData {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        write!(f, "[raw data for {} size {}]", self.stream, self.buffer.len())
+    }
+}
+
 #[cfg(test)]
 mod test {
     use super::*;