Skip to content

Add constructors, setters, adders & builders#27

Open
fengalin wants to merge 2 commits into
sdroege:mainfrom
fengalin:builders
Open

Add constructors, setters, adders & builders#27
fengalin wants to merge 2 commits into
sdroege:mainfrom
fengalin:builders

Conversation

@fengalin

@fengalin fengalin commented May 22, 2026

Copy link
Copy Markdown

This MR adds constructors, setters, adders & builders.

Constructors arguments include all non-optionals & non-collections attributes. String arguments use impl ToString.

When only one argument is optional (e.g. Attribute), an additional constructor with_... is implemented, otherwise setters / adders & builders are defined.

Adding an Attribute to a Session or a Media uses direct impl ToString arguments so we can write:

media.add_attribute("attr");
media.add_attribute_with_value("attr", "value");

instead of:

media.add_attribute(Attribute::new("attr"));
media.add_attribute(Attribute::new_with_value("attr"));

Same for other builder-less argument types: Connection, TimeZone & Key.

The second commit adds shortcuts to define the rtpmap attribute.

Example:

sdp_types::Session::builder(
    sdp_types::Origin::new(
        session_id,
        0,
        sdp_types::ADDRTYPE_IP4,
        sender_addr,
    ),
    "rtpbin2-precise-sync-example",
)
.connection(sdp_types::ADDRTYPE_IP4, outbound_addr)
.attribute("sendonly")
.media(
    sdp_types::Media::builder("audio", outbound_port, "RTP/AVP", pt)
        .rtpmap_with_params(pt, encoding_name, AUDIO_RATE, AUDIO_CHANNELS)
        .attribute_with_value("ts-refclk", refclk)
        .attribute_with_value("mediaclk", clksrc)
        .attribute(format!("ssrc:{ssrc} cname:{cname}"))
        .attribute("rtcp-mux")
        .build(),
)
.build()

Fixes #5

@fengalin

Copy link
Copy Markdown
Author

an rtpmap parser might be useful too

fengalin added 2 commits May 22, 2026 19:03
Constructors arguments include all non-optionals & non-collections attributes.
`String` arguments use `impl ToString`.

When only one argument is optional (e.g. `Attribute`), an additional constructor
`with_...` is implemented, otherwise setters / adders & builders are defined.

Adding an Attribute to a Session or a Media uses direct `impl ToString`
arguments so we can write:

```
media.add_attribute("attr");
media.add_attribute_with_value("attr", "value");
```

instead of:

```
media.add_attribute(Attribute::new("attr"));
media.add_attribute(Attribute::new_with_value("attr"));
```

Same for other builder-less argument types: `Connection`, `TimeZone` & `Key`.
Comment thread src/builders.rs
self
}

pub fn connection(

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

here and elsewhere I'm wondering why the builders don't take e.g. Connection directly here, but instead provide smaller setters

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.

Ah, I thought about mentioning it in the top comment, but then I forgot...

Connection, Attribute, Key and others have only two construction variants, so I thought it was more convenient and less verbose to include a direct setter in the builder. Ex:

    .connection(sdp_types::ADDRTYPE_IP4, outbound_addr)

instead of:

```rust
    .connection(sdp_types::Connection::new(sdp_types::ADDRTYPE_IP4, outbound_addr))

I could also use this for Origin btw.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

That makes sense but maybe a generic one based on the struct would also make sense (if you want to copy over a Connection from some other session or so)?

Comment thread src/builders.rs
}

/// Adds an "rtpmap" compliant attribute with the provided values.
pub fn rtpmap(mut self, pt: u8, encoding_name: impl AsRef<str>, clock_rate: u32) -> Self {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I wonder if this shouldn't be some kind of attribute builder

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.

As you wish. Related to previous comment.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I don't mean that this should be removed but should probably be some kind of specific attribute builder instead

@sdroege sdroege left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Looks good to me otherwise, thanks!

@fengalin

Copy link
Copy Markdown
Author

What do you think about this?

an rtpmap parser might be useful too

@sdroege

sdroege commented May 27, 2026

Copy link
Copy Markdown
Owner

an rtpmap parser might be useful too

Yes, see also #25

@fengalin

Copy link
Copy Markdown
Author

Maybe we should also add get_attributes_for_ssrc variants that would return the attributes as seen by current source, removing the need for the user to deal with ssrc:<ssrc> <attr> level and returning the most significant one. E.g. for ts-refclk, we want by order or priority, the ssrc-level, the media-level or the session-level attribute.

@fengalin

Copy link
Copy Markdown
Author

Just discussed with Tarun about my comment above. He already implemented the group attribute handling as part of this PR.

In a separate PR, we could define some kind of view / lens mechanism where a user gets a version of the SDP filtered for a specific SSRC.

@sdroege

sdroege commented May 29, 2026

Copy link
Copy Markdown
Owner

Sounds good to me. Let me know (both of you) when you want either/both of the PRs merged. From my point of view this is all ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add builders for Session, Media and other types

2 participants