Skip to content

Commit

Permalink
Writer.payload_params: fix bug returning all payload params
Browse files Browse the repository at this point in the history
A filtered iterator is now returned. The invariant of mid resolution
in the session is now documented.
  • Loading branch information
david-flok committed Nov 20, 2023
1 parent e3bcbac commit 2712128
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@
//! let writer = rtc.writer(mid).unwrap();
//!
//! // Get the payload type (pt) for the wanted codec.
//! let pt = writer.payload_params()[0].pt();
//! let pt = writer.payload_params().nth(0).unwrap().pt();
//!
//! // Write the data
//! let wallclock = todo!(); // Absolute time of the data
Expand Down
17 changes: 15 additions & 2 deletions src/media/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ pub struct Writer<'a> {
}

impl<'a> Writer<'a> {
/// Create a new writer object.
///
/// The `mid` parameter is required to have a corresponding media in `self.session`.
pub(crate) fn new(session: &'a mut Session, mid: Mid) -> Self {
Writer {
session,
Expand All @@ -33,8 +36,14 @@ impl<'a> Writer<'a> {
/// Get the configured payload parameters for the `mid` this writer is for.
///
/// For the [`Writer::write()`] call, the `pt` must be set correctly.
pub fn payload_params(&self) -> &[PayloadParams] {
&self.session.codec_config
pub fn payload_params(&self) -> impl Iterator<Item = &PayloadParams> {
// This unwrap is OK due to the invariant of self.mid being resolvable
let media = self.session.media_by_mid(self.mid).unwrap();
self.session
.codec_config
.params()
.iter()
.filter(|p| media.remote_pts().contains(&p.pt))
}

/// Match the given parameters to the configured parameters for this [`Media`].
Expand Down Expand Up @@ -103,6 +112,7 @@ impl<'a> Writer<'a> {
rtp_time: MediaTime,
data: impl Into<Vec<u8>>,
) -> Result<(), RtcError> {
// This (indirect) unwrap is OK due to the invariant of self.mid being resolvable
let media = media_by_mid_mut(&mut self.session.medias, self.mid);

if !self.session.codec_config.has_pt(pt) {
Expand Down Expand Up @@ -198,6 +208,9 @@ impl<'a> Writer<'a> {
}
}

/// Get a &mut Media in a slice for a `mid`.
///
/// `mid` must be resolvable or panic will ensue.
fn media_by_mid_mut(medias: &mut [Media], mid: Mid) -> &mut Media {
medias.iter_mut().find(|m| m.mid() == mid).unwrap()
}

0 comments on commit 2712128

Please sign in to comment.