| 1 | // Copyright 2025 LiveKit, Inc. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | use crate::rtp_transceiver::RtpTransceiverDirection; |
| 16 | |
| 17 | #[derive(Debug, Copy, Clone, PartialEq, Eq)] |
| 18 | pub enum Priority { |
| 19 | VeryLow, |
| 20 | Low, |
| 21 | Medium, |
| 22 | High, |
| 23 | } |
| 24 | |
| 25 | /// Controls how the encoder degrades quality when bandwidth is constrained. |
| 26 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Default)] |
| 27 | pub enum DegradationPreference { |
| 28 | /// Maintain both framerate and resolution. Frames may be dropped before encoding |
| 29 | /// if necessary to avoid overusing network and encoder resources. |
| 30 | MaintainFramerateAndResolution, |
| 31 | /// Degrade framerate to maintain resolution. |
| 32 | MaintainFramerate, |
| 33 | /// Degrade resolution to maintain framerate. |
| 34 | MaintainResolution, |
| 35 | /// Balance between framerate and resolution degradation. |
| 36 | #[default] |
| 37 | Balanced, |
| 38 | } |
| 39 | |
| 40 | impl DegradationPreference { |
| 41 | /// Deprecated: Use `MaintainFramerateAndResolution` instead. |
| 42 | #[deprecated(since = "0.8.0", note = "Use MaintainFramerateAndResolution instead")] |
| 43 | #[allow(non_upper_case_globals)] |
| 44 | pub const Disabled: Self = Self::MaintainFramerateAndResolution; |
| 45 | } |
| 46 | |
| 47 | #[derive(Debug, Clone)] |
| 48 | pub struct RtpHeaderExtensionParameters { |
| 49 | pub uri: String, |
| 50 | pub id: i32, |
| 51 | pub encrypted: bool, |
| 52 | } |
| 53 | |
| 54 | #[derive(Debug, Clone, Default)] |
| 55 | pub struct RtpParameters { |
| 56 | pub codecs: Vec<RtpCodecParameters>, |
| 57 | pub header_extensions: Vec<RtpHeaderExtensionParameters>, |
| 58 | pub encodings: Vec<RtpEncodingParameters>, |
| 59 | pub rtcp: RtcpParameters, |
| 60 | /// Opaque token used by WebRTC to pair getParameters/setParameters calls. |
| 61 | /// Must be preserved when round-tripping through set_parameters(). |
| 62 | pub(crate) transaction_id: String, |
| 63 | pub(crate) mid: String, |
| 64 | pub(crate) has_degradation_preference: bool, |
| 65 | pub(crate) degradation_preference: i32, |
| 66 | } |
| 67 | |
| 68 | impl RtpParameters { |
| 69 | /// Sets the degradation preference for this RTP sender. |
| 70 | /// |
| 71 | /// This controls how the encoder trades off between resolution and framerate |
| 72 | /// when bandwidth is constrained. |
| 73 | pub fn set_degradation_preference(&mut self, preference: DegradationPreference) { |
| 74 | self.has_degradation_preference = true; |
| 75 | self.degradation_preference = match preference { |
| 76 | DegradationPreference::MaintainFramerateAndResolution => 0, |
| 77 | DegradationPreference::MaintainFramerate => 1, |
| 78 | DegradationPreference::MaintainResolution => 2, |
| 79 | DegradationPreference::Balanced => 3, |
| 80 | }; |
| 81 | } |
| 82 | |
| 83 | /// Gets the current degradation preference, if set. |
| 84 | pub fn degradation_preference(&self) -> Option<DegradationPreference> { |
| 85 | if !self.has_degradation_preference { |
| 86 | return None; |
| 87 | } |
| 88 | Some(match self.degradation_preference { |
| 89 | 0 => DegradationPreference::MaintainFramerateAndResolution, |
| 90 | 1 => DegradationPreference::MaintainFramerate, |
| 91 | 2 => DegradationPreference::MaintainResolution, |
| 92 | 3 => DegradationPreference::Balanced, |
| 93 | _ => DegradationPreference::Balanced, |
| 94 | }) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /// Mirrors webrtc_sys RtcpFeedback for round-trip fidelity. |
| 99 | #[derive(Debug, Clone, Default)] |
| 100 | pub(crate) struct CodecFeedback { |
| 101 | pub feedback_type: i32, |
| 102 | pub has_message_type: bool, |
| 103 | pub message_type: i32, |
| 104 | } |
| 105 | |
| 106 | #[derive(Debug, Clone, Default)] |
| 107 | pub struct RtpCodecParameters { |
| 108 | pub payload_type: u8, |
| 109 | pub mime_type: String, // read-only |
| 110 | pub clock_rate: Option<u64>, |
| 111 | pub channels: Option<u16>, |
| 112 | pub(crate) name: String, |
| 113 | pub(crate) kind: i32, |
| 114 | pub(crate) has_max_ptime: bool, |
| 115 | pub(crate) max_ptime: i32, |
| 116 | pub(crate) has_ptime: bool, |
| 117 | pub(crate) ptime: i32, |
| 118 | pub(crate) rtcp_feedback: Vec<CodecFeedback>, |
| 119 | pub(crate) parameters: Vec<(String, String)>, |
| 120 | } |
| 121 | |
| 122 | #[derive(Debug, Clone, Default)] |
| 123 | pub struct RtcpParameters { |
| 124 | pub cname: String, |
| 125 | pub reduced_size: bool, |
| 126 | pub(crate) mux: bool, |
| 127 | pub(crate) has_ssrc: bool, |
| 128 | pub(crate) ssrc: u32, |
| 129 | } |
| 130 | |
| 131 | #[derive(Debug, Clone)] |
| 132 | pub struct RtpEncodingParameters { |
| 133 | pub active: bool, |
| 134 | pub max_bitrate: Option<u64>, |
| 135 | pub max_framerate: Option<f64>, |
| 136 | pub priority: Priority, |
| 137 | pub rid: String, |
| 138 | pub scale_resolution_down_by: Option<f64>, |
| 139 | /// RTP scalability mode (e.g. "L3T3_KEY"). Required to enable true |
| 140 | /// SVC for codecs that support it (VP9, AV1). |
| 141 | pub scalability_mode: Option<String>, |
| 142 | /// Preserved for round-trip fidelity with WebRTC's getParameters/setParameters. |
| 143 | pub has_ssrc: bool, |
| 144 | pub ssrc: u32, |
| 145 | } |
| 146 | |
| 147 | #[derive(Debug, Clone)] |
| 148 | pub struct RtpCodecCapability { |
| 149 | pub channels: Option<u16>, |
| 150 | pub clock_rate: Option<u64>, |
| 151 | pub mime_type: String, |
| 152 | pub sdp_fmtp_line: Option<String>, |
| 153 | } |
| 154 | |
| 155 | #[derive(Debug, Clone)] |
| 156 | pub struct RtpHeaderExtensionCapability { |
| 157 | pub uri: String, |
| 158 | pub direction: RtpTransceiverDirection, |
| 159 | } |
| 160 | |
| 161 | #[derive(Debug, Clone)] |
| 162 | pub struct RtpCapabilities { |
| 163 | pub codecs: Vec<RtpCodecCapability>, |
| 164 | pub header_extensions: Vec<RtpHeaderExtensionCapability>, |
| 165 | } |
| 166 | |
| 167 | impl Default for RtpEncodingParameters { |
| 168 | fn default() -> Self { |
| 169 | Self { |
| 170 | active: true, |
| 171 | max_bitrate: None, |
| 172 | max_framerate: None, |
| 173 | priority: Priority::Low, |
| 174 | rid: String::default(), |
| 175 | scale_resolution_down_by: None, |
| 176 | scalability_mode: None, |
| 177 | has_ssrc: false, |
| 178 | ssrc: 0, |
| 179 | } |
| 180 | } |
| 181 | } |