| 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 webrtc_sys::{rtp_parameters as sys_rp, webrtc as sys_webrtc}; |
| 16 | |
| 17 | use crate::rtp_parameters::*; |
| 18 | |
| 19 | impl From<sys_webrtc::ffi::Priority> for Priority { |
| 20 | fn from(value: sys_webrtc::ffi::Priority) -> Self { |
| 21 | match value { |
| 22 | sys_webrtc::ffi::Priority::VeryLow => Self::VeryLow, |
| 23 | sys_webrtc::ffi::Priority::Low => Self::Low, |
| 24 | sys_webrtc::ffi::Priority::Medium => Self::Medium, |
| 25 | sys_webrtc::ffi::Priority::High => Self::High, |
| 26 | _ => panic!("unknown Priority"), |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | impl From<sys_rp::ffi::RtpExtension> for RtpHeaderExtensionParameters { |
| 32 | fn from(value: sys_rp::ffi::RtpExtension) -> Self { |
| 33 | Self { uri: value.uri, id: value.id, encrypted: value.encrypt } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | impl From<sys_rp::ffi::RtpParameters> for RtpParameters { |
| 38 | fn from(value: sys_rp::ffi::RtpParameters) -> Self { |
| 39 | Self { |
| 40 | codecs: value.codecs.into_iter().map(Into::into).collect(), |
| 41 | header_extensions: value.header_extensions.into_iter().map(Into::into).collect(), |
| 42 | encodings: value.encodings.into_iter().map(Into::into).collect(), |
| 43 | rtcp: value.rtcp.into(), |
| 44 | transaction_id: value.transaction_id, |
| 45 | mid: value.mid, |
| 46 | has_degradation_preference: value.has_degradation_preference, |
| 47 | degradation_preference: value.degradation_preference.repr, |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | impl From<sys_rp::ffi::RtpCodecParameters> for RtpCodecParameters { |
| 53 | fn from(value: sys_rp::ffi::RtpCodecParameters) -> Self { |
| 54 | Self { |
| 55 | mime_type: value.mime_type, |
| 56 | payload_type: value.payload_type as u8, |
| 57 | clock_rate: value.has_clock_rate.then_some(value.clock_rate as u64), |
| 58 | channels: value.has_num_channels.then_some(value.num_channels as u16), |
| 59 | name: value.name, |
| 60 | kind: value.kind.repr, |
| 61 | has_max_ptime: value.has_max_ptime, |
| 62 | max_ptime: value.max_ptime, |
| 63 | has_ptime: value.has_ptime, |
| 64 | ptime: value.ptime, |
| 65 | rtcp_feedback: value |
| 66 | .rtcp_feedback |
| 67 | .into_iter() |
| 68 | .map(|f| CodecFeedback { |
| 69 | feedback_type: f.feedback_type.repr, |
| 70 | has_message_type: f.has_message_type, |
| 71 | message_type: f.message_type.repr, |
| 72 | }) |
| 73 | .collect(), |
| 74 | parameters: value.parameters.into_iter().map(|kv| (kv.key, kv.value)).collect(), |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | impl From<sys_rp::ffi::RtcpParameters> for RtcpParameters { |
| 80 | fn from(value: sys_rp::ffi::RtcpParameters) -> Self { |
| 81 | Self { |
| 82 | cname: value.cname, |
| 83 | reduced_size: value.reduced_size, |
| 84 | mux: value.mux, |
| 85 | has_ssrc: value.has_ssrc, |
| 86 | ssrc: value.ssrc, |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | impl From<sys_rp::ffi::RtpEncodingParameters> for RtpEncodingParameters { |
| 92 | fn from(value: sys_rp::ffi::RtpEncodingParameters) -> Self { |
| 93 | Self { |
| 94 | active: value.active, |
| 95 | max_bitrate: value.has_max_bitrate_bps.then_some(value.max_bitrate_bps as u64), |
| 96 | max_framerate: value.has_max_framerate.then_some(value.max_framerate), |
| 97 | priority: value.network_priority.into(), |
| 98 | rid: value.rid, |
| 99 | scale_resolution_down_by: value |
| 100 | .has_scale_resolution_down_by |
| 101 | .then_some(value.scale_resolution_down_by), |
| 102 | scalability_mode: value.has_scalability_mode.then_some(value.scalability_mode), |
| 103 | has_ssrc: value.has_ssrc, |
| 104 | ssrc: value.ssrc, |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | impl From<sys_rp::ffi::RtpCodecCapability> for RtpCodecCapability { |
| 110 | fn from(value: sys_rp::ffi::RtpCodecCapability) -> Self { |
| 111 | Self { |
| 112 | channels: value.has_num_channels.then_some(value.num_channels as u16), |
| 113 | mime_type: value.mime_type, |
| 114 | clock_rate: value.has_clock_rate.then_some(value.clock_rate as u64), |
| 115 | sdp_fmtp_line: { |
| 116 | let parameters: Vec<String> = value |
| 117 | .parameters |
| 118 | .into_iter() |
| 119 | .map(|key_value| { |
| 120 | if !key_value.key.is_empty() { |
| 121 | format!("{}={}", key_value.key, key_value.value) |
| 122 | } else { |
| 123 | key_value.value |
| 124 | } |
| 125 | }) |
| 126 | .collect(); |
| 127 | |
| 128 | if !parameters.is_empty() { |
| 129 | Some(parameters.join(";")) |
| 130 | } else { |
| 131 | None |
| 132 | } |
| 133 | }, |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | impl From<sys_rp::ffi::RtpHeaderExtensionCapability> for RtpHeaderExtensionCapability { |
| 139 | fn from(value: sys_rp::ffi::RtpHeaderExtensionCapability) -> Self { |
| 140 | Self { direction: value.direction.into(), uri: value.uri } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | impl From<sys_rp::ffi::RtpCapabilities> for RtpCapabilities { |
| 145 | fn from(value: sys_rp::ffi::RtpCapabilities) -> Self { |
| 146 | Self { |
| 147 | codecs: value.codecs.into_iter().map(Into::into).collect(), |
| 148 | header_extensions: value.header_extensions.into_iter().map(Into::into).collect(), |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | impl From<Priority> for sys_webrtc::ffi::Priority { |
| 154 | fn from(value: Priority) -> Self { |
| 155 | match value { |
| 156 | Priority::VeryLow => Self::VeryLow, |
| 157 | Priority::Low => Self::Low, |
| 158 | Priority::Medium => Self::Medium, |
| 159 | Priority::High => Self::High, |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | impl From<RtpHeaderExtensionParameters> for sys_rp::ffi::RtpExtension { |
| 165 | fn from(value: RtpHeaderExtensionParameters) -> Self { |
| 166 | Self { uri: value.uri, id: value.id, encrypt: value.encrypted } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | impl From<RtpParameters> for sys_rp::ffi::RtpParameters { |
| 171 | fn from(value: RtpParameters) -> Self { |
| 172 | let degradation_preference = |
| 173 | sys_rp::ffi::DegradationPreference { repr: value.degradation_preference }; |
| 174 | Self { |
| 175 | codecs: value.codecs.into_iter().map(Into::into).collect(), |
| 176 | header_extensions: value.header_extensions.into_iter().map(Into::into).collect(), |
| 177 | encodings: value.encodings.into_iter().map(Into::into).collect(), |
| 178 | rtcp: value.rtcp.into(), |
| 179 | transaction_id: value.transaction_id, |
| 180 | mid: value.mid, |
| 181 | has_degradation_preference: value.has_degradation_preference, |
| 182 | degradation_preference, |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | impl From<RtpCodecParameters> for sys_rp::ffi::RtpCodecParameters { |
| 188 | fn from(value: RtpCodecParameters) -> Self { |
| 189 | let kind = sys_webrtc::ffi::MediaType { repr: value.kind }; |
| 190 | Self { |
| 191 | payload_type: value.payload_type as i32, |
| 192 | mime_type: value.mime_type, |
| 193 | has_clock_rate: value.clock_rate.is_some(), |
| 194 | clock_rate: value.clock_rate.unwrap_or_default() as i32, |
| 195 | has_num_channels: value.channels.is_some(), |
| 196 | num_channels: value.channels.unwrap_or_default() as i32, |
| 197 | name: value.name, |
| 198 | kind, |
| 199 | has_max_ptime: value.has_max_ptime, |
| 200 | max_ptime: value.max_ptime, |
| 201 | has_ptime: value.has_ptime, |
| 202 | ptime: value.ptime, |
| 203 | rtcp_feedback: value |
| 204 | .rtcp_feedback |
| 205 | .into_iter() |
| 206 | .map(|f| { |
| 207 | let feedback_type = sys_rp::ffi::RtcpFeedbackType { repr: f.feedback_type }; |
| 208 | let message_type = |
| 209 | sys_rp::ffi::RtcpFeedbackMessageType { repr: f.message_type }; |
| 210 | sys_rp::ffi::RtcpFeedback { |
| 211 | feedback_type, |
| 212 | has_message_type: f.has_message_type, |
| 213 | message_type, |
| 214 | } |
| 215 | }) |
| 216 | .collect(), |
| 217 | parameters: value |
| 218 | .parameters |
| 219 | .into_iter() |
| 220 | .map(|(key, value)| sys_rp::ffi::StringKeyValue { key, value }) |
| 221 | .collect(), |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | impl From<RtcpParameters> for sys_rp::ffi::RtcpParameters { |
| 227 | fn from(value: RtcpParameters) -> Self { |
| 228 | Self { |
| 229 | cname: value.cname, |
| 230 | reduced_size: value.reduced_size, |
| 231 | has_ssrc: value.has_ssrc, |
| 232 | ssrc: value.ssrc, |
| 233 | mux: value.mux, |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | impl From<RtpEncodingParameters> for sys_rp::ffi::RtpEncodingParameters { |
| 239 | fn from(value: RtpEncodingParameters) -> Self { |
| 240 | Self { |
| 241 | active: value.active, |
| 242 | has_max_bitrate_bps: value.max_bitrate.is_some(), |
| 243 | max_bitrate_bps: value.max_bitrate.unwrap_or_default() as i32, |
| 244 | has_max_framerate: value.max_framerate.is_some(), |
| 245 | max_framerate: value.max_framerate.unwrap_or_default(), |
| 246 | network_priority: value.priority.into(), |
| 247 | rid: value.rid, |
| 248 | has_scale_resolution_down_by: value.scale_resolution_down_by.is_some(), |
| 249 | scale_resolution_down_by: value.scale_resolution_down_by.unwrap_or_default(), |
| 250 | adaptive_ptime: false, |
| 251 | bitrate_priority: sys_rp::DEFAULT_BITRATE_PRIORITY, |
| 252 | has_min_bitrate_bps: false, |
| 253 | min_bitrate_bps: 0, |
| 254 | has_num_temporal_layers: false, |
| 255 | num_temporal_layers: 0, |
| 256 | has_scalability_mode: value.scalability_mode.is_some(), |
| 257 | scalability_mode: value.scalability_mode.unwrap_or_default(), |
| 258 | has_ssrc: value.has_ssrc, |
| 259 | ssrc: value.ssrc, |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | impl From<RtpCodecCapability> for sys_rp::ffi::RtpCodecCapability { |
| 265 | fn from(value: RtpCodecCapability) -> Self { |
| 266 | let mime_type: Vec<&str> = value.mime_type.split('/').collect(); |
| 267 | let kind = match mime_type[0] { |
| 268 | "audio" => sys_webrtc::ffi::MediaType::Audio, |
| 269 | "video" => sys_webrtc::ffi::MediaType::Video, |
| 270 | _ => panic!("invalid media type"), |
| 271 | }; |
| 272 | let name = mime_type[1].to_string(); |
| 273 | |
| 274 | Self { |
| 275 | name, |
| 276 | kind, |
| 277 | has_clock_rate: value.clock_rate.is_some(), |
| 278 | clock_rate: value.clock_rate.unwrap_or_default() as i32, |
| 279 | has_num_channels: value.channels.is_some(), |
| 280 | num_channels: value.channels.unwrap_or_default() as i32, |
| 281 | parameters: { |
| 282 | value |
| 283 | .sdp_fmtp_line |
| 284 | .map(|sdp_fmtp_line| { |
| 285 | sdp_fmtp_line |
| 286 | .split(';') |
| 287 | .map(|v| { |
| 288 | let key_value: Vec<&str> = v.split('=').collect(); |
| 289 | if key_value.len() == 2 { |
| 290 | sys_rp::ffi::StringKeyValue { |
| 291 | key: key_value[0].to_string(), |
| 292 | value: key_value[1].to_string(), |
| 293 | } |
| 294 | } else { |
| 295 | sys_rp::ffi::StringKeyValue { |
| 296 | key: "".to_string(), |
| 297 | value: key_value[0].to_string(), |
| 298 | } |
| 299 | } |
| 300 | }) |
| 301 | .collect() |
| 302 | }) |
| 303 | .unwrap_or_default() |
| 304 | }, |
| 305 | // Ignore |
| 306 | mime_type: String::default(), // !! |
| 307 | has_preferred_payload_type: false, |
| 308 | preferred_payload_type: 0, |
| 309 | rtcp_feedback: Vec::default(), |
| 310 | } |
| 311 | } |
| 312 | } |