Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/codec/packet/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,22 @@ impl Packet {
}
}

#[inline]
pub fn read(&mut self, format: &mut format::context::Input) -> Result<(), Error> {
unsafe {
match av_read_frame(format.as_mut_ptr(), self.as_mut_ptr()) {
0 => Ok(()),
e => Err(Error::from(e)),
}
/// Read a packet from the input format context.
///
/// # Safety
/// this is unsafe because you can potentially "read" a packet twice like so:
/// ```rust,ignore
/// let mut packet = Packet::empty();
/// packet.read(&mut ictx)?;
/// packet.read(&mut ictx)?;
///
/// ```
/// it is recommended to use the input packet read api's
#[inline]
pub unsafe fn read(&mut self, format: &mut format::context::Input) -> Result<(), Error> {
match av_read_frame(format.as_mut_ptr(), self.as_mut_ptr()) {
0 => Ok(()),
e => Err(Error::from(e)),
}
}

Expand Down
15 changes: 14 additions & 1 deletion src/format/context/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::ffi::CString;
use std::mem;
use std::ops::{Deref, DerefMut};

use crate::packet::Mut;

use super::common::Context;
use super::destructor;
use ffi::*;
Expand Down Expand Up @@ -136,6 +138,17 @@ impl Input {
}
}
}

#[inline]
pub fn next_packet(&mut self) -> Result<Packet, Error> {
let mut packet = Packet::empty();
unsafe {
match av_read_frame(self.as_mut_ptr(), packet.as_mut_ptr()) {
0 => Ok(packet),
e => Err(Error::from(e)),
}
}
}
}

impl Deref for Input {
Expand Down Expand Up @@ -169,7 +182,7 @@ impl<'a> Iterator for PacketIter<'a> {
let mut packet = Packet::empty();

loop {
match packet.read(self.context) {
match unsafe { packet.read(self.context) } {
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I never used the iterator API tho if I had to guess I'd this can leak as well..

Ok(..) => unsafe {
return Some((
Stream::wrap(mem::transmute_copy(&self.context), packet.stream()),
Expand Down