Skip to content

Commit

Permalink
refactor h264 depacketizer
Browse files Browse the repository at this point in the history
* add a couple tests

* fix #4: put the whole video frame (including multiple NALs) into
  a single Bytes. This is simpler to use and speeds up the
  depacketize/h264_aac_writevideo benchmark. It's also a behavior
  change; now I include non-VUI data such as SPS/PPS/SEI into the
  data. I think this is a better default but it might be worth
  making customizable.
  • Loading branch information
scottlamb committed Jun 25, 2021
1 parent 4fe885f commit af7e8a7
Show file tree
Hide file tree
Showing 4 changed files with 388 additions and 224 deletions.
6 changes: 1 addition & 5 deletions benches/depacketize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use std::num::NonZeroU16;

use bytes::Buf;
use criterion::{criterion_group, criterion_main, Criterion};
use retina::client::{rtp::StrictSequenceChecker, Timeline};
use retina::codec::{CodecItem, Depacketizer};
Expand Down Expand Up @@ -78,10 +77,7 @@ fn criterion_benchmark(c: &mut Criterion) {
CodecItem::VideoFrame(v) => v,
_ => return,
};
let mut slices = [std::io::IoSlice::new(b""); 2];
let n = v.chunks_vectored(&mut slices);
assert_eq!(n, 2);
assert_eq!(w.write_vectored(&slices[..]).unwrap(), v.remaining());
w.write_all(&v.data()[..]).unwrap();
})
})
});
Expand Down
9 changes: 5 additions & 4 deletions examples/client/mp4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,16 +485,16 @@ impl<W: AsyncWrite + AsyncSeek + Send + Unpin> Mp4Writer<W> {
Ok(())
}

async fn video(&mut self, mut frame: retina::codec::VideoFrame) -> Result<(), failure::Error> {
async fn video(&mut self, frame: retina::codec::VideoFrame) -> Result<(), failure::Error> {
println!(
"{}: {}-byte video frame",
&frame.timestamp,
frame.remaining()
frame.data().remaining(),
);
if let Some(ref p) = frame.new_parameters {
bail!("parameters change unimplemented. new parameters: {:#?}", p);
}
let size = u32::try_from(frame.remaining())?;
let size = u32::try_from(frame.data().remaining())?;
self.video_trak
.add_sample(self.mdat_pos, size, frame.timestamp, frame.loss)?;
self.mdat_pos = self
Expand All @@ -505,7 +505,8 @@ impl<W: AsyncWrite + AsyncSeek + Send + Unpin> Mp4Writer<W> {
self.video_sync_sample_nums
.push(u32::try_from(self.video_trak.samples)?);
}
write_all_buf(&mut self.inner, &mut frame).await?;
let mut data = frame.into_data();
write_all_buf(&mut self.inner, &mut data).await?;
Ok(())
}

Expand Down
Loading

0 comments on commit af7e8a7

Please sign in to comment.