1use std::borrow::Cow;
39
40use o_sfu_rfc::{rtp as rfc_rtp, webrtc as rfc_webrtc};
41pub use rfc_rtp::{HeaderExtensionId, Mid, PayloadType, Rid, Ssrc};
42
43use super::MediaKind;
44
45pub type MediaCodec = rfc_rtp::CodecName;
47
48pub type HeaderExtensionUri = rfc_webrtc::RtpHeaderExtensionUri;
50
51#[derive(Debug, Clone, PartialEq, Eq)]
56pub enum RtcpFeedbackKind {
57 Nack,
60 NackPli,
63 CcmFir,
66 GoogRemb,
68 TransportCc,
70 Other(String),
72}
73
74#[derive(Debug, Clone, PartialEq, Eq)]
76pub struct RtcpFeedback {
77 kind: RtcpFeedbackKind,
78 parameter: Option<String>,
79}
80
81impl RtcpFeedback {
82 #[must_use]
83 pub fn new(kind: RtcpFeedbackKind, parameter: Option<String>) -> Self {
84 Self { kind, parameter }
85 }
86
87 #[must_use]
88 pub fn kind(&self) -> &RtcpFeedbackKind {
89 &self.kind
90 }
91
92 #[must_use]
93 pub fn parameter(&self) -> Option<&str> {
94 self.parameter.as_deref()
95 }
96}
97
98#[derive(Debug, Clone, PartialEq, Eq)]
103pub enum CodecSetting {
104 RtxAssociation(PayloadType),
107 H264PacketizationMode(rfc_rtp::h264::PacketizationMode),
109 H264ProfileLevelId(String),
111 Vp9ProfileId(rfc_rtp::Vp9ProfileId),
113 UseInBandFec(bool),
115 Other { key: String, value: String },
117}
118
119impl CodecSetting {
120 #[must_use]
121 pub fn key(&self) -> &str {
122 match self {
123 Self::RtxAssociation(_) => rfc_rtp::fmtp::RTX_ASSOCIATION,
124 Self::H264PacketizationMode(_) => rfc_rtp::fmtp::H264_PACKETIZATION_MODE,
125 Self::H264ProfileLevelId(_) => rfc_rtp::fmtp::H264_PROFILE_LEVEL_ID,
126 Self::Vp9ProfileId(_) => rfc_rtp::fmtp::VP9_PROFILE_ID,
127 Self::UseInBandFec(_) => rfc_rtp::fmtp::OPUS_USE_IN_BAND_FEC,
128 Self::Other { key, .. } => key.as_str(),
129 }
130 }
131
132 #[must_use]
133 pub fn wire_value(&self) -> Cow<'_, str> {
134 match self {
135 Self::RtxAssociation(payload_type) => Cow::Owned(payload_type.value().to_string()),
136 Self::H264PacketizationMode(mode) => Cow::Owned(mode.fmtp_value().to_string()),
137 Self::H264ProfileLevelId(profile_level_id) => Cow::Borrowed(profile_level_id.as_str()),
138 Self::Vp9ProfileId(profile_id) => Cow::Owned(profile_id.value().to_string()),
139 Self::UseInBandFec(enabled) => Cow::Borrowed(if *enabled {
140 rfc_rtp::fmtp::VALUE_ENABLED
141 } else {
142 rfc_rtp::fmtp::VALUE_DISABLED
143 }),
144 Self::Other { value, .. } => Cow::Borrowed(value.as_str()),
145 }
146 }
147}
148
149#[derive(Debug, Clone, PartialEq, Eq)]
164pub struct HeaderExtension {
165 uri: HeaderExtensionUri,
166 id: HeaderExtensionId,
167}
168
169impl HeaderExtension {
170 #[must_use]
171 pub fn new(uri: impl Into<HeaderExtensionUri>, id: impl Into<HeaderExtensionId>) -> Self {
172 Self {
173 uri: uri.into(),
174 id: id.into(),
175 }
176 }
177
178 #[must_use]
179 pub fn uri_kind(&self) -> &HeaderExtensionUri {
180 &self.uri
181 }
182
183 #[must_use]
184 pub fn id(&self) -> HeaderExtensionId {
185 self.id
186 }
187
188 #[must_use]
189 pub fn uri(&self) -> &str {
190 self.uri.as_str()
191 }
192}
193
194#[derive(Debug, Clone, PartialEq, Eq)]
201pub struct MediaCodecCapability {
202 media_kind: MediaKind,
203 codec: MediaCodec,
204 clock_rate: u32,
205 payload_type: Option<PayloadType>,
206 channels: Option<u16>,
207 settings: Vec<CodecSetting>,
208 rtcp_feedback: Vec<RtcpFeedback>,
209}
210
211impl MediaCodecCapability {
212 #[must_use]
213 pub fn new(media_kind: MediaKind, codec: impl Into<MediaCodec>, clock_rate: u32) -> Self {
214 Self {
215 media_kind,
216 codec: codec.into(),
217 clock_rate,
218 payload_type: None,
219 channels: None,
220 settings: Vec::new(),
221 rtcp_feedback: Vec::new(),
222 }
223 }
224
225 #[must_use]
226 pub fn with_payload_type(mut self, payload_type: PayloadType) -> Self {
227 self.payload_type = Some(payload_type);
228 self
229 }
230
231 #[must_use]
232 pub fn with_channels(mut self, channels: u16) -> Self {
233 self.channels = Some(channels);
234 self
235 }
236
237 #[must_use]
238 pub fn with_setting(mut self, setting: CodecSetting) -> Self {
239 self.settings.push(setting);
240 self
241 }
242
243 #[must_use]
246 pub fn with_parameter(self, name: impl Into<String>, value: impl Into<String>) -> Self {
247 let setting = codec_setting_from_wire(&self.codec, name.into(), value.into());
248 self.with_setting(setting)
249 }
250
251 #[must_use]
252 pub fn with_rtcp_feedback(mut self, feedback: RtcpFeedback) -> Self {
253 self.rtcp_feedback.push(feedback);
254 self
255 }
256
257 #[must_use]
258 pub fn media_kind(&self) -> MediaKind {
259 self.media_kind
260 }
261
262 #[must_use]
263 pub fn codec(&self) -> &MediaCodec {
264 &self.codec
265 }
266
267 #[must_use]
268 pub fn codec_name(&self) -> &str {
269 self.codec.as_str()
270 }
271
272 #[must_use]
273 pub fn clock_rate(&self) -> u32 {
274 self.clock_rate
275 }
276
277 #[must_use]
278 pub fn payload_type_id(&self) -> Option<PayloadType> {
279 self.payload_type
280 }
281
282 #[must_use]
283 pub fn payload_type(&self) -> Option<u8> {
284 self.payload_type.map(PayloadType::value)
285 }
286
287 #[must_use]
288 pub fn channels(&self) -> Option<u16> {
289 self.channels
290 }
291
292 pub fn settings(&self) -> impl Iterator<Item = &CodecSetting> {
293 self.settings.iter()
294 }
295
296 pub fn parameters(&self) -> impl Iterator<Item = (String, String)> + '_ {
297 self.settings
298 .iter()
299 .map(|setting| (setting.key().to_owned(), setting.wire_value().into_owned()))
300 }
301
302 pub fn rtcp_feedback(&self) -> impl Iterator<Item = &RtcpFeedback> {
303 self.rtcp_feedback.iter()
304 }
305
306 #[must_use]
307 pub fn rtx_associated_payload_type_id(&self) -> Option<PayloadType> {
308 self.settings.iter().find_map(|setting| match setting {
309 CodecSetting::RtxAssociation(payload_type) => Some(*payload_type),
310 _ => None,
311 })
312 }
313
314 #[must_use]
315 pub fn rtx_associated_payload_type(&self) -> Option<u8> {
316 self.rtx_associated_payload_type_id()
317 .map(PayloadType::value)
318 }
319}
320
321#[derive(Debug, Clone, Default, PartialEq, Eq)]
323pub struct MediaCapabilities {
324 codecs: Vec<MediaCodecCapability>,
325 header_extensions: Vec<HeaderExtension>,
326}
327
328impl MediaCapabilities {
329 #[must_use]
330 pub fn new(codecs: Vec<MediaCodecCapability>, header_extensions: Vec<HeaderExtension>) -> Self {
331 Self {
332 codecs,
333 header_extensions,
334 }
335 }
336
337 pub fn codecs(&self) -> impl Iterator<Item = &MediaCodecCapability> {
338 self.codecs.iter()
339 }
340
341 pub fn header_extensions(&self) -> impl Iterator<Item = &HeaderExtension> {
342 self.header_extensions.iter()
343 }
344}
345
346#[derive(Debug, Clone, PartialEq, Eq)]
351pub struct MediaFormat {
352 media_kind: MediaKind,
353 codec: MediaCodec,
354 payload_type: PayloadType,
355 clock_rate: u32,
356 channels: Option<u16>,
357 settings: Vec<CodecSetting>,
358 rtcp_feedback: Vec<RtcpFeedback>,
359}
360
361impl MediaFormat {
362 #[must_use]
363 pub fn new(
364 media_kind: MediaKind,
365 codec: impl Into<MediaCodec>,
366 payload_type: PayloadType,
367 clock_rate: u32,
368 ) -> Self {
369 Self {
370 media_kind,
371 codec: codec.into(),
372 payload_type,
373 clock_rate,
374 channels: None,
375 settings: Vec::new(),
376 rtcp_feedback: Vec::new(),
377 }
378 }
379
380 #[must_use]
381 pub fn with_channels(mut self, channels: u16) -> Self {
382 self.channels = Some(channels);
383 self
384 }
385
386 #[must_use]
387 pub fn with_setting(mut self, setting: CodecSetting) -> Self {
388 self.settings.push(setting);
389 self
390 }
391
392 #[must_use]
393 pub fn with_parameter(self, name: impl Into<String>, value: impl Into<String>) -> Self {
394 let setting = codec_setting_from_wire(&self.codec, name.into(), value.into());
395 self.with_setting(setting)
396 }
397
398 #[must_use]
399 pub fn with_rtcp_feedback(mut self, feedback: RtcpFeedback) -> Self {
400 self.rtcp_feedback.push(feedback);
401 self
402 }
403
404 #[must_use]
405 pub fn media_kind(&self) -> MediaKind {
406 self.media_kind
407 }
408
409 #[must_use]
410 pub fn codec(&self) -> &MediaCodec {
411 &self.codec
412 }
413
414 #[must_use]
415 pub fn codec_name(&self) -> &str {
416 self.codec.as_str()
417 }
418
419 #[must_use]
420 pub fn payload_type_id(&self) -> PayloadType {
421 self.payload_type
422 }
423
424 #[must_use]
425 pub fn payload_type(&self) -> u8 {
426 self.payload_type.value()
427 }
428
429 #[must_use]
430 pub fn clock_rate(&self) -> u32 {
431 self.clock_rate
432 }
433
434 #[must_use]
435 pub fn channels(&self) -> Option<u16> {
436 self.channels
437 }
438
439 pub fn settings(&self) -> impl Iterator<Item = &CodecSetting> {
440 self.settings.iter()
441 }
442
443 pub fn parameters(&self) -> impl Iterator<Item = (String, String)> + '_ {
444 self.settings
445 .iter()
446 .map(|setting| (setting.key().to_owned(), setting.wire_value().into_owned()))
447 }
448
449 pub fn rtcp_feedback(&self) -> impl Iterator<Item = &RtcpFeedback> {
450 self.rtcp_feedback.iter()
451 }
452
453 #[must_use]
454 pub fn rtx_associated_payload_type_id(&self) -> Option<PayloadType> {
455 self.settings.iter().find_map(|setting| match setting {
456 CodecSetting::RtxAssociation(payload_type) => Some(*payload_type),
457 _ => None,
458 })
459 }
460
461 #[must_use]
462 pub fn rtx_associated_payload_type(&self) -> Option<u8> {
463 self.rtx_associated_payload_type_id()
464 .map(PayloadType::value)
465 }
466}
467
468#[derive(Debug, Clone, Default, PartialEq, Eq)]
491pub struct StreamBinding {
492 ssrc: Option<Ssrc>,
493 repair_ssrc: Option<Ssrc>,
494 rid: Option<Rid>,
495 payload_type: Option<PayloadType>,
496 max_bitrate: Option<u64>,
497}
498
499impl StreamBinding {
500 #[must_use]
501 pub fn new() -> Self {
502 Self::default()
503 }
504
505 #[must_use]
506 pub fn with_ssrc(mut self, ssrc: impl Into<Ssrc>) -> Self {
507 self.ssrc = Some(ssrc.into());
508 self
509 }
510
511 #[must_use]
515 pub fn with_repair_ssrc(mut self, repair_ssrc: impl Into<Ssrc>) -> Self {
516 self.repair_ssrc = Some(repair_ssrc.into());
517 self
518 }
519
520 #[must_use]
521 pub fn with_rid(mut self, rid: impl Into<Rid>) -> Self {
522 self.rid = Some(rid.into());
523 self
524 }
525
526 #[must_use]
527 pub fn with_payload_type(mut self, payload_type: PayloadType) -> Self {
528 self.payload_type = Some(payload_type);
529 self
530 }
531
532 #[must_use]
533 pub fn with_max_bitrate(mut self, max_bitrate: u64) -> Self {
534 self.max_bitrate = Some(max_bitrate);
535 self
536 }
537
538 #[must_use]
539 pub fn ssrc(&self) -> Option<u32> {
540 self.ssrc.map(Ssrc::value)
541 }
542
543 #[must_use]
544 pub fn repair_ssrc(&self) -> Option<u32> {
545 self.repair_ssrc.map(Ssrc::value)
546 }
547
548 #[must_use]
549 pub fn rid(&self) -> Option<&str> {
550 self.rid.as_ref().map(Rid::as_str)
551 }
552
553 #[must_use]
554 pub(super) fn with_payload_type_mapping(
555 mut self,
556 payload_types: &[(PayloadType, PayloadType)],
557 ) -> Self {
558 if let Some(payload_type) = self.payload_type {
559 let mapped_payload_type = payload_types
560 .iter()
561 .find_map(|(original, mapped)| (*original == payload_type).then_some(*mapped))
562 .unwrap_or(payload_type);
563 self.payload_type = Some(mapped_payload_type);
564 }
565 self
566 }
567
568 #[must_use]
569 pub fn payload_type_id(&self) -> Option<PayloadType> {
570 self.payload_type
571 }
572
573 #[must_use]
574 pub fn payload_type(&self) -> Option<u8> {
575 self.payload_type.map(PayloadType::value)
576 }
577
578 #[must_use]
579 pub fn max_bitrate(&self) -> Option<u64> {
580 self.max_bitrate
581 }
582}
583
584#[derive(Debug, Clone, Default, PartialEq, Eq)]
589pub struct MediaStream {
590 formats: Vec<MediaFormat>,
591 header_extensions: Vec<HeaderExtension>,
592 bindings: Vec<StreamBinding>,
593 mid: Option<Mid>,
594}
595
596impl MediaStream {
597 #[must_use]
598 pub fn new(
599 formats: Vec<MediaFormat>,
600 header_extensions: Vec<HeaderExtension>,
601 bindings: Vec<StreamBinding>,
602 ) -> Self {
603 Self {
604 formats,
605 header_extensions,
606 bindings,
607 mid: None,
608 }
609 }
610
611 #[must_use]
612 pub fn with_mid(mut self, mid: impl Into<Mid>) -> Self {
613 self.mid = Some(mid.into());
614 self
615 }
616
617 pub fn formats(&self) -> impl Iterator<Item = &MediaFormat> {
618 self.formats.iter()
619 }
620
621 pub fn header_extensions(&self) -> impl Iterator<Item = &HeaderExtension> {
622 self.header_extensions.iter()
623 }
624
625 pub fn bindings(&self) -> impl Iterator<Item = &StreamBinding> {
626 self.bindings.iter()
627 }
628
629 #[must_use]
630 pub fn mid(&self) -> Option<&str> {
631 self.mid.as_ref().map(Mid::as_str)
632 }
633}
634
635fn codec_setting_from_wire(codec: &MediaCodec, key: String, value: String) -> CodecSetting {
636 match key.as_str() {
637 rfc_rtp::fmtp::RTX_ASSOCIATION => value
638 .parse::<u8>()
639 .ok()
640 .and_then(PayloadType::try_new)
641 .map_or(
642 CodecSetting::Other { key, value },
643 CodecSetting::RtxAssociation,
644 ),
645 rfc_rtp::fmtp::H264_PACKETIZATION_MODE => value
646 .parse::<u8>()
647 .ok()
648 .and_then(rfc_rtp::h264::PacketizationMode::from_fmtp_value)
649 .map_or_else(
650 || CodecSetting::Other { key, value },
651 CodecSetting::H264PacketizationMode,
652 ),
653 rfc_rtp::fmtp::H264_PROFILE_LEVEL_ID => CodecSetting::H264ProfileLevelId(value),
654 rfc_rtp::fmtp::VP9_PROFILE_ID if codec == &MediaCodec::Vp9 => value
655 .parse::<u8>()
656 .ok()
657 .and_then(rfc_rtp::Vp9ProfileId::try_new)
658 .map_or(
659 CodecSetting::Other { key, value },
660 CodecSetting::Vp9ProfileId,
661 ),
662 rfc_rtp::fmtp::OPUS_USE_IN_BAND_FEC => match value.as_str() {
663 rfc_rtp::fmtp::VALUE_ENABLED | rfc_rtp::fmtp::VALUE_TRUE => {
664 CodecSetting::UseInBandFec(true)
665 }
666 rfc_rtp::fmtp::VALUE_DISABLED | rfc_rtp::fmtp::VALUE_FALSE => {
667 CodecSetting::UseInBandFec(false)
668 }
669 _ => CodecSetting::Other { key, value },
670 },
671 _ => CodecSetting::Other { key, value },
672 }
673}