Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add dts extractor #61 #81

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
34 changes: 34 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ rand = "0.8.3"
rtsp-types = "0.1.0"
sdp-types = "0.1.4"
smallvec = { version = "1.6.1", features = ["union"] }
test-case = "3.3.1"
thiserror = "1.0.25"
time = "0.1.43"
tokio = { version = "1.11.0", features = ["macros", "net", "rt", "time"] }
Expand Down
43 changes: 40 additions & 3 deletions examples/client/src/mp4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ struct TrakTracker {
}

impl TrakTracker {
fn add_sample(
fn add_audio_sample(
&mut self,
sample_description_index: u32,
byte_pos: u32,
Expand Down Expand Up @@ -178,6 +178,43 @@ impl TrakTracker {
Ok(())
}

fn add_video_sample(
&mut self,
sample_description_index: u32,
byte_pos: u32,
size: u32,
timestamp: retina::VideoTimestamp,
loss: u16,
allow_loss: bool,
) -> Result<(), Error> {
if self.samples > 0 && loss > 0 && !allow_loss {
bail!("Lost {} RTP packets mid-stream", loss);
}
self.samples += 1;
if self.next_pos != Some(byte_pos)
|| self.chunks.last().map(|c| c.sample_description_index)
!= Some(sample_description_index)
{
self.chunks.push(Chunk {
first_sample_number: self.samples,
byte_pos,
sample_description_index,
});
}
self.sizes.push(size);
self.next_pos = Some(byte_pos + size);
if let Some(last_pts) = self.last_pts.replace(timestamp.pts()) {
let duration = timestamp.pts().checked_sub(last_pts).unwrap();
self.tot_duration += u64::try_from(duration).unwrap();
let duration = u32::try_from(duration)?;
match self.durations.last_mut() {
Some((s, d)) if *d == duration => *s += 1,
_ => self.durations.push((1, duration)),
}
}
Ok(())
}

fn finish(&mut self) {
if self.last_pts.is_some() {
self.durations.push((1, 0));
Expand Down Expand Up @@ -571,7 +608,7 @@ impl<W: AsyncWrite + AsyncSeek + Send + Unpin> Mp4Writer<W> {
};
self.cur_video_params_sample_description_index = Some(sample_description_index);
let size = u32::try_from(frame.data().remaining())?;
self.video_trak.add_sample(
self.video_trak.add_video_sample(
sample_description_index,
self.mdat_pos,
size,
Expand All @@ -597,7 +634,7 @@ impl<W: AsyncWrite + AsyncSeek + Send + Unpin> Mp4Writer<W> {
frame.data().remaining()
);
let size = u32::try_from(frame.data().remaining())?;
self.audio_trak.add_sample(
self.audio_trak.add_audio_sample(
/* sample_description_index */ 1,
self.mdat_pos,
size,
Expand Down
4 changes: 2 additions & 2 deletions src/client/timeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl Timeline {
self.max_forward_jump_secs
));
}
self.timestamp = timestamp.timestamp;
self.timestamp = timestamp.pts;
Ok(timestamp)
}

Expand Down Expand Up @@ -117,7 +117,7 @@ impl Timeline {
}
Ok((
Timestamp {
timestamp,
pts: timestamp,
clock_rate: self.clock_rate,
start,
},
Expand Down
16 changes: 8 additions & 8 deletions src/codec/aac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,11 +565,11 @@ impl Depacketizer {
"Got {au_headers_count}-AU packet while fragment in progress"
));
}
if (pkt.timestamp().timestamp as u16) != frag.rtp_timestamp {
if (pkt.timestamp().pts as u16) != frag.rtp_timestamp {
return Err(format!(
"Timestamp changed from 0x{:04x} to 0x{:04x} mid-fragment",
frag.rtp_timestamp,
pkt.timestamp().timestamp as u16
pkt.timestamp().pts as u16
));
}
let au_header = u16::from_be_bytes([payload[2], payload[3]]);
Expand Down Expand Up @@ -704,7 +704,7 @@ impl Depacketizer {
let mut buf = BytesMut::with_capacity(size);
buf.extend_from_slice(&payload[agg.data_off..]);
self.state = DepacketizerState::Fragmented(Fragment {
rtp_timestamp: agg.pkt.timestamp().timestamp as u16,
rtp_timestamp: agg.pkt.timestamp().pts as u16,
loss: agg.loss,
loss_since_mark: agg.loss_since_mark,
size: size as u16,
Expand Down Expand Up @@ -804,7 +804,7 @@ mod tests {
Some("streamtype=5;profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3;config=1188"),
).unwrap();
let timestamp = crate::Timestamp {
timestamp: 42,
pts: 42,
clock_rate: NonZeroU32::new(48_000).unwrap(),
start: 0,
};
Expand Down Expand Up @@ -1000,7 +1000,7 @@ mod tests {
Some("streamtype=5;profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3;config=1188"),
).unwrap();
let timestamp = crate::Timestamp {
timestamp: 42,
pts: 42,
clock_rate: NonZeroU32::new(48_000).unwrap(),
start: 0,
};
Expand Down Expand Up @@ -1104,7 +1104,7 @@ mod tests {
Some("streamtype=5;profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3;config=1188"),
).unwrap();
let timestamp = crate::Timestamp {
timestamp: 42,
pts: 42,
clock_rate: NonZeroU32::new(48_000).unwrap(),
start: 0,
};
Expand Down Expand Up @@ -1210,7 +1210,7 @@ mod tests {
Some("streamtype=5;profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3;config=1188"),
).unwrap();
let timestamp = crate::Timestamp {
timestamp: 42,
pts: 42,
clock_rate: NonZeroU32::new(48_000).unwrap(),
start: 0,
};
Expand Down Expand Up @@ -1319,7 +1319,7 @@ mod tests {
Some("streamtype=5;profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3;config=1188"),
).unwrap();
let timestamp = crate::Timestamp {
timestamp: 42,
pts: 42,
clock_rate: NonZeroU32::new(48_000).unwrap(),
start: 0,
};
Expand Down
Loading
Loading