Skip to content

Commit 16a6edd

Browse files
authored
Move capabilities API into submodule in medea-flutter-webrtc-native crate (#214, #210)
1 parent 2a689a9 commit 16a6edd

19 files changed

+1347
-1274
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//! Capabilities of an [RTP] endpoint.
2+
//!
3+
//! [RTP]: https://en.wikipedia.org/wiki/Real-time_Transport_Protocol
4+
5+
pub mod rtp_codec;
6+
pub mod rtp_header_extension_capability;
7+
8+
use libwebrtc_sys as sys;
9+
10+
pub use self::{
11+
rtp_codec::{
12+
RtcpFeedback, RtcpFeedbackMessageType, RtcpFeedbackType,
13+
RtpCodecCapability, ScalabilityMode, set_codec_preferences,
14+
},
15+
rtp_header_extension_capability::RtpHeaderExtensionCapability,
16+
};
17+
#[cfg(doc)]
18+
use crate::RtpParameters;
19+
use crate::api::{MediaType, WEBRTC};
20+
21+
/// Representation of the static capabilities of an endpoint.
22+
///
23+
/// Applications can use these capabilities to construct [`RtpParameters`].
24+
#[derive(Debug)]
25+
pub struct RtpCapabilities {
26+
/// Supported codecs.
27+
pub codecs: Vec<RtpCodecCapability>,
28+
29+
/// Supported [RTP] header extensions.
30+
///
31+
/// [RTP]: https://en.wikipedia.org/wiki/Real-time_Transport_Protocol
32+
pub header_extensions: Vec<RtpHeaderExtensionCapability>,
33+
}
34+
35+
impl From<sys::RtpCapabilities> for RtpCapabilities {
36+
fn from(value: sys::RtpCapabilities) -> Self {
37+
Self {
38+
codecs: value.codecs().into_iter().map(Into::into).collect(),
39+
header_extensions: value
40+
.header_extensions()
41+
.into_iter()
42+
.map(Into::into)
43+
.collect(),
44+
}
45+
}
46+
}
47+
48+
/// Returns the capabilities of an [RTP] sender of the provided [`MediaType`].
49+
///
50+
/// [RTP]: https://en.wikipedia.org/wiki/Real-time_Transport_Protocol
51+
#[must_use]
52+
pub fn get_rtp_sender_capabilities(kind: MediaType) -> RtpCapabilities {
53+
RtpCapabilities::from(
54+
WEBRTC
55+
.lock()
56+
.unwrap()
57+
.peer_connection_factory
58+
.get_rtp_sender_capabilities(kind.into()),
59+
)
60+
}
61+
62+
/// Returns the capabilities of an [RTP] receiver of the provided [`MediaType`].
63+
///
64+
/// [RTP]: https://en.wikipedia.org/wiki/Real-time_Transport_Protocol
65+
#[must_use]
66+
pub fn get_rtp_receiver_capabilities(kind: MediaType) -> RtpCapabilities {
67+
RtpCapabilities::from(
68+
WEBRTC
69+
.lock()
70+
.unwrap()
71+
.peer_connection_factory
72+
.get_rtp_receiver_capabilities(kind.into()),
73+
)
74+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
//! Static codec capabilities.
2+
3+
pub mod rtcp_feedback;
4+
pub mod scalability_mode;
5+
6+
use std::sync::Arc;
7+
8+
use libwebrtc_sys as sys;
9+
10+
pub use self::{
11+
rtcp_feedback::{RtcpFeedback, RtcpFeedbackMessageType, RtcpFeedbackType},
12+
scalability_mode::ScalabilityMode,
13+
};
14+
use crate::{RtpTransceiver, api::MediaType, frb_generated::RustOpaque};
15+
16+
/// Representation of static capabilities of an endpoint's implementation of a
17+
/// codec.
18+
#[derive(Debug)]
19+
pub struct RtpCodecCapability {
20+
/// Default payload type for the codec.
21+
///
22+
/// Mainly needed for codecs that have statically assigned payload types.
23+
pub preferred_payload_type: Option<i32>,
24+
25+
/// List of [`ScalabilityMode`]s supported by the video codec.
26+
pub scalability_modes: Vec<ScalabilityMode>,
27+
28+
/// Built [MIME "type/subtype"][0] string from `name` and `kind`.
29+
///
30+
/// [0]: https://en.wikipedia.org/wiki/Media_type
31+
pub mime_type: String,
32+
33+
/// Used to identify the codec. Equivalent to [MIME subtype][0].
34+
///
35+
/// [0]: https://en.wikipedia.org/wiki/Media_type#Subtypes
36+
pub name: String,
37+
38+
/// [`MediaType`] of this codec. Equivalent to [MIME] top-level type.
39+
///
40+
/// [MIME]: https://en.wikipedia.org/wiki/Media_type
41+
pub kind: MediaType,
42+
43+
/// If [`None`], the implementation default is used.
44+
pub clock_rate: Option<i32>,
45+
46+
/// Number of audio channels used.
47+
///
48+
/// [`None`] for video codecs.
49+
///
50+
/// If [`None`] for audio, the implementation default is used.
51+
pub num_channels: Option<i32>,
52+
53+
/// Codec-specific parameters that must be signaled to the remote party.
54+
///
55+
/// Corresponds to `a=fmtp` parameters in [SDP].
56+
///
57+
/// Contrary to ORTC, these parameters are named using all lowercase
58+
/// strings. This helps make the mapping to [SDP] simpler, if an application
59+
/// is using [SDP]. Boolean values are represented by the string "1".
60+
///
61+
/// [SDP]: https://en.wikipedia.org/wiki/Session_Description_Protocol
62+
pub parameters: Vec<(String, String)>,
63+
64+
/// Feedback mechanisms to be used for this codec.
65+
pub feedback: Vec<RtcpFeedback>,
66+
}
67+
68+
impl From<sys::RtpCodecCapability> for RtpCodecCapability {
69+
fn from(value: sys::RtpCodecCapability) -> Self {
70+
Self {
71+
preferred_payload_type: value.preferred_payload_type(),
72+
scalability_modes: value
73+
.scalability_modes()
74+
.into_iter()
75+
.map(Into::into)
76+
.collect(),
77+
mime_type: value.mime_type(),
78+
name: value.name(),
79+
kind: value.kind().into(),
80+
clock_rate: value.clock_rate(),
81+
num_channels: value.num_channels(),
82+
parameters: value.parameters().into_iter().collect(),
83+
feedback: value
84+
.rtcp_feedback()
85+
.into_iter()
86+
.map(Into::into)
87+
.collect(),
88+
}
89+
}
90+
}
91+
92+
/// Changes the preferred [`RtpTransceiver`] codecs to the provided
93+
/// [`Vec`]`<`[`RtpCodecCapability`]`>`.
94+
#[expect(clippy::needless_pass_by_value, reason = "FFI")]
95+
pub fn set_codec_preferences(
96+
transceiver: RustOpaque<Arc<RtpTransceiver>>,
97+
codecs: Vec<RtpCodecCapability>,
98+
) {
99+
transceiver.set_codec_preferences(codecs);
100+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
//! [RTCP] feedback message.
2+
//!
3+
//! [RTCP]: https://en.wikipedia.org/wiki/RTP_Control_Protocol
4+
5+
use libwebrtc_sys as sys;
6+
7+
/// [RTCP] feedback message intended to enable congestion control for
8+
/// interactive real-time traffic using [RTP].
9+
///
10+
/// [RTCP]: https://en.wikipedia.org/wiki/RTP_Control_Protocol
11+
/// [RTP]: https://en.wikipedia.org/wiki/Real-time_Transport_Protocol
12+
#[derive(Debug)]
13+
pub struct RtcpFeedback {
14+
/// Message type of this [`RtcpFeedback`].
15+
pub message_type: Option<RtcpFeedbackMessageType>,
16+
17+
/// Kind of this [`RtcpFeedback`].
18+
pub kind: RtcpFeedbackType,
19+
}
20+
21+
impl From<sys::RtcpFeedback> for RtcpFeedback {
22+
fn from(value: sys::RtcpFeedback) -> Self {
23+
Self {
24+
message_type: value.message_type().map(Into::into),
25+
kind: value.kind().into(),
26+
}
27+
}
28+
}
29+
30+
/// Possible types of an [`RtcpFeedback`].
31+
#[derive(Debug, Eq, Hash, PartialEq)]
32+
#[repr(i32)]
33+
pub enum RtcpFeedbackType {
34+
/// Codec control messages.
35+
Ccm,
36+
37+
/// Loss notification feedback.
38+
Lntf,
39+
40+
/// Negative acknowledgemen.
41+
Nack,
42+
43+
/// Receiver estimated maximum bitrate.
44+
Remb,
45+
46+
/// Transport wide congestion control.
47+
TransportCC,
48+
}
49+
50+
impl From<sys::RtcpFeedbackType> for RtcpFeedbackType {
51+
fn from(value: sys::RtcpFeedbackType) -> Self {
52+
match value {
53+
sys::RtcpFeedbackType::CCM => Self::Ccm,
54+
sys::RtcpFeedbackType::LNTF => Self::Lntf,
55+
sys::RtcpFeedbackType::NACK => Self::Nack,
56+
sys::RtcpFeedbackType::REMB => Self::Remb,
57+
sys::RtcpFeedbackType::TRANSPORT_CC => Self::TransportCC,
58+
_ => unreachable!(),
59+
}
60+
}
61+
}
62+
63+
/// Possible message types of an [`RtcpFeedback`], when is type is
64+
/// [`RtcpFeedbackType::Nack`] or [`RtcpFeedbackType::Ccm`].
65+
#[derive(Debug, Eq, Hash, PartialEq)]
66+
#[repr(i32)]
67+
pub enum RtcpFeedbackMessageType {
68+
/// Equivalent to `{ type: "nack", parameter: undefined }` in ORTC.
69+
GenericNACK,
70+
71+
/// Usable with [`RtcpFeedbackType::Nack`].
72+
Pli,
73+
74+
/// Usable with [`RtcpFeedbackType::Ccm`].
75+
Fir,
76+
}
77+
78+
impl From<sys::RtcpFeedbackMessageType> for RtcpFeedbackMessageType {
79+
fn from(value: sys::RtcpFeedbackMessageType) -> Self {
80+
match value {
81+
sys::RtcpFeedbackMessageType::GENERIC_NACK => Self::GenericNACK,
82+
sys::RtcpFeedbackMessageType::PLI => Self::Pli,
83+
sys::RtcpFeedbackMessageType::FIR => Self::Fir,
84+
_ => unreachable!(),
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)