| 1 | /* |
| 2 | * Copyright 2025 LiveKit, Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "livekit/rtp_parameters.h" |
| 18 | |
| 19 | namespace livekit_ffi { |
| 20 | |
| 21 | webrtc::RtcpFeedback to_native_rtcp_feedback(RtcpFeedback feedback) { |
| 22 | webrtc::RtcpFeedback native{}; |
| 23 | native.type = static_cast<webrtc::RtcpFeedbackType>(feedback.feedback_type); |
| 24 | if (feedback.has_message_type) |
| 25 | native.message_type = |
| 26 | static_cast<webrtc::RtcpFeedbackMessageType>(feedback.message_type); |
| 27 | |
| 28 | return native; |
| 29 | } |
| 30 | |
| 31 | webrtc::RtpCodecCapability to_native_rtp_codec_capability( |
| 32 | RtpCodecCapability capability) { |
| 33 | webrtc::RtpCodecCapability native{}; |
| 34 | // native.mime_type(); IGNORED |
| 35 | |
| 36 | native.name = capability.name.c_str(); |
| 37 | native.kind = static_cast<webrtc::MediaType>(capability.kind); |
| 38 | |
| 39 | if (capability.has_clock_rate) |
| 40 | native.clock_rate = capability.clock_rate; |
| 41 | |
| 42 | if (capability.has_preferred_payload_type) |
| 43 | native.preferred_payload_type = capability.preferred_payload_type; |
| 44 | |
| 45 | if (capability.has_num_channels) |
| 46 | native.num_channels = capability.num_channels; |
| 47 | |
| 48 | for (auto feedback : capability.rtcp_feedback) |
| 49 | native.rtcp_feedback.push_back(to_native_rtcp_feedback(feedback)); |
| 50 | |
| 51 | for (auto pair : capability.parameters) |
| 52 | native.parameters.insert(std::pair(pair.key, pair.value)); |
| 53 | |
| 54 | return native; |
| 55 | } |
| 56 | |
| 57 | webrtc::RtpHeaderExtensionCapability to_native_rtp_header_extension_capability( |
| 58 | RtpHeaderExtensionCapability header) { |
| 59 | webrtc::RtpHeaderExtensionCapability native{}; |
| 60 | native.uri = header.uri.c_str(); |
| 61 | |
| 62 | if (header.has_preferred_id) |
| 63 | native.preferred_id = header.preferred_id; |
| 64 | |
| 65 | native.preferred_encrypt = header.preferred_encrypt; |
| 66 | native.direction = |
| 67 | static_cast<webrtc::RtpTransceiverDirection>(header.direction); |
| 68 | |
| 69 | return native; |
| 70 | } |
| 71 | |
| 72 | webrtc::RtpExtension to_native_rtp_extension(RtpExtension ext) { |
| 73 | webrtc::RtpExtension native{}; |
| 74 | native.uri = ext.uri.c_str(); |
| 75 | native.id = ext.id; |
| 76 | native.encrypt = ext.encrypt; |
| 77 | return native; |
| 78 | } |
| 79 | |
| 80 | webrtc::RtpFecParameters to_rtp_fec_parameters(RtpFecParameters fec) { |
| 81 | webrtc::RtpFecParameters native{}; |
| 82 | |
| 83 | if (fec.has_ssrc) |
| 84 | native.ssrc = fec.ssrc; |
| 85 | |
| 86 | native.mechanism = static_cast<webrtc::FecMechanism>(fec.mechanism); |
| 87 | return native; |
| 88 | } |
| 89 | |
| 90 | webrtc::RtpRtxParameters to_rtp_rtx_parameters(RtpRtxParameters rtx) { |
| 91 | webrtc::RtpRtxParameters native{}; |
| 92 | |
| 93 | if (rtx.has_ssrc) |
| 94 | native.ssrc = rtx.ssrc; |
| 95 | return native; |
| 96 | } |
| 97 | |
| 98 | webrtc::RtpEncodingParameters to_native_rtp_encoding_paramters( |
| 99 | RtpEncodingParameters parameters) { |
| 100 | webrtc::RtpEncodingParameters native{}; |
| 101 | native.rid = parameters.rid.c_str(); |
| 102 | |
| 103 | if (parameters.has_ssrc) |
| 104 | native.ssrc = parameters.ssrc; |
| 105 | |
| 106 | native.active = parameters.active; |
| 107 | if (parameters.has_max_framerate) |
| 108 | native.max_framerate = parameters.max_framerate; |
| 109 | |
| 110 | native.adaptive_ptime = parameters.adaptive_ptime; |
| 111 | if (parameters.has_max_bitrate_bps) |
| 112 | native.max_bitrate_bps = parameters.max_bitrate_bps; |
| 113 | |
| 114 | if (parameters.has_min_bitrate_bps) |
| 115 | native.min_bitrate_bps = parameters.min_bitrate_bps; |
| 116 | |
| 117 | native.bitrate_priority = parameters.bitrate_priority; |
| 118 | native.network_priority = |
| 119 | static_cast<webrtc::Priority>(parameters.network_priority); |
| 120 | |
| 121 | if (parameters.has_scalability_mode) |
| 122 | native.scalability_mode = parameters.scalability_mode.c_str(); |
| 123 | |
| 124 | if (parameters.has_num_temporal_layers) |
| 125 | native.num_temporal_layers = parameters.num_temporal_layers; |
| 126 | |
| 127 | if (parameters.has_scale_resolution_down_by) |
| 128 | native.scale_resolution_down_by = parameters.scale_resolution_down_by; |
| 129 | return native; |
| 130 | } |
| 131 | |
| 132 | webrtc::RtpCodecParameters to_native_rtp_codec_parameters( |
| 133 | RtpCodecParameters params) { |
| 134 | webrtc::RtpCodecParameters native{}; |
| 135 | native.name = params.name.c_str(); |
| 136 | native.kind = static_cast<webrtc::MediaType>(params.kind); |
| 137 | native.payload_type = params.payload_type; |
| 138 | |
| 139 | for (auto pair : params.parameters) |
| 140 | native.parameters.insert(std::pair(pair.key, pair.value)); |
| 141 | |
| 142 | for (auto feedback : params.rtcp_feedback) |
| 143 | native.rtcp_feedback.push_back(to_native_rtcp_feedback(feedback)); |
| 144 | |
| 145 | if (params.has_num_channels) |
| 146 | native.num_channels = params.num_channels; |
| 147 | |
| 148 | if (params.has_clock_rate) |
| 149 | native.clock_rate = params.clock_rate; |
| 150 | |
| 151 | return native; |
| 152 | } |
| 153 | |
| 154 | webrtc::RtpCapabilities to_rtp_capabilities(RtpCapabilities capabilities) { |
| 155 | webrtc::RtpCapabilities native{}; |
| 156 | for (auto codec : capabilities.codecs) |
| 157 | native.codecs.push_back(to_native_rtp_codec_capability(codec)); |
| 158 | |
| 159 | for (auto header : capabilities.header_extensions) |
| 160 | native.header_extensions.push_back( |
| 161 | to_native_rtp_header_extension_capability(header)); |
| 162 | |
| 163 | for (auto fec : capabilities.fec) |
| 164 | native.fec.push_back(static_cast<webrtc::FecMechanism>(fec)); |
| 165 | |
| 166 | return native; |
| 167 | } |
| 168 | |
| 169 | webrtc::RtcpParameters to_native_rtcp_paramaters(RtcpParameters params) { |
| 170 | webrtc::RtcpParameters native{}; |
| 171 | if (params.has_ssrc) |
| 172 | native.ssrc = params.ssrc; |
| 173 | |
| 174 | native.mux = params.mux; |
| 175 | native.cname = params.cname.c_str(); |
| 176 | native.reduced_size = params.reduced_size; |
| 177 | return native; |
| 178 | } |
| 179 | |
| 180 | webrtc::RtpParameters to_native_rtp_parameters(RtpParameters params) { |
| 181 | webrtc::RtpParameters native{}; |
| 182 | native.transaction_id = params.transaction_id.c_str(); |
| 183 | native.mid = params.mid.c_str(); |
| 184 | |
| 185 | for (auto codec : params.codecs) |
| 186 | native.codecs.push_back(to_native_rtp_codec_parameters(codec)); |
| 187 | |
| 188 | for (auto header : params.header_extensions) |
| 189 | native.header_extensions.push_back(to_native_rtp_extension(header)); |
| 190 | |
| 191 | for (auto encoding : params.encodings) |
| 192 | native.encodings.push_back(to_native_rtp_encoding_paramters(encoding)); |
| 193 | |
| 194 | native.rtcp = to_native_rtcp_paramaters(params.rtcp); |
| 195 | |
| 196 | if (params.has_degradation_preference) |
| 197 | native.degradation_preference = static_cast<webrtc::DegradationPreference>( |
| 198 | params.degradation_preference); |
| 199 | |
| 200 | return native; |
| 201 | } |
| 202 | |
| 203 | RtcpFeedback to_rust_rtcp_feedback(webrtc::RtcpFeedback feedback) { |
| 204 | RtcpFeedback rust{}; |
| 205 | rust.feedback_type = static_cast<RtcpFeedbackType>(feedback.type); |
| 206 | |
| 207 | if (feedback.message_type.has_value()) { |
| 208 | rust.has_message_type = true; |
| 209 | rust.message_type = |
| 210 | static_cast<RtcpFeedbackMessageType>(feedback.message_type.value()); |
| 211 | } |
| 212 | |
| 213 | return rust; |
| 214 | } |
| 215 | |
| 216 | RtpCodecCapability to_rust_rtp_codec_capability( |
| 217 | webrtc::RtpCodecCapability capability) { |
| 218 | RtpCodecCapability rust{}; |
| 219 | rust.mime_type = capability.mime_type(); |
| 220 | rust.name = capability.name; |
| 221 | rust.kind = static_cast<MediaType>(capability.kind); |
| 222 | |
| 223 | if (capability.clock_rate.has_value()) { |
| 224 | rust.has_clock_rate = true; |
| 225 | rust.clock_rate = capability.clock_rate.value(); |
| 226 | } |
| 227 | |
| 228 | if (capability.preferred_payload_type.has_value()) { |
| 229 | rust.has_preferred_payload_type = true; |
| 230 | rust.preferred_payload_type = capability.preferred_payload_type.value(); |
| 231 | } |
| 232 | |
| 233 | if (capability.num_channels) { |
| 234 | rust.has_num_channels = true; |
| 235 | rust.num_channels = capability.num_channels.value(); |
| 236 | } |
| 237 | |
| 238 | for (auto feedback : capability.rtcp_feedback) |
| 239 | rust.rtcp_feedback.push_back(to_rust_rtcp_feedback(feedback)); |
| 240 | |
| 241 | for (auto param : capability.parameters) |
| 242 | rust.parameters.push_back(StringKeyValue{param.first, param.second}); |
| 243 | |
| 244 | return rust; |
| 245 | } |
| 246 | |
| 247 | RtpHeaderExtensionCapability to_rust_rtp_header_extension_capability( |
| 248 | webrtc::RtpHeaderExtensionCapability header) { |
| 249 | RtpHeaderExtensionCapability rust{}; |
| 250 | rust.uri = header.uri; |
| 251 | if (header.preferred_id.has_value()) { |
| 252 | rust.has_preferred_id = true; |
| 253 | rust.preferred_id = header.preferred_id.value(); |
| 254 | } |
| 255 | |
| 256 | rust.preferred_encrypt = header.preferred_encrypt; |
| 257 | rust.direction = static_cast<RtpTransceiverDirection>(header.direction); |
| 258 | return rust; |
| 259 | } |
| 260 | |
| 261 | RtpExtension to_rust_rtp_extension(webrtc::RtpExtension ext) { |
| 262 | RtpExtension rust{}; |
| 263 | rust.uri = ext.uri; |
| 264 | rust.id = ext.id; |
| 265 | rust.encrypt = ext.encrypt; |
| 266 | return rust; |
| 267 | } |
| 268 | |
| 269 | RtpFecParameters to_rust_rtp_fec_parameters(webrtc::RtpFecParameters fec) { |
| 270 | RtpFecParameters rust{}; |
| 271 | if (fec.ssrc.has_value()) { |
| 272 | rust.has_ssrc = true; |
| 273 | rust.ssrc = fec.ssrc.value(); |
| 274 | } |
| 275 | |
| 276 | rust.mechanism = static_cast<FecMechanism>(rust.mechanism); |
| 277 | return rust; |
| 278 | } |
| 279 | |
| 280 | RtpRtxParameters to_rust_rtp_rtx_parameters(webrtc::RtpRtxParameters param) { |
| 281 | RtpRtxParameters rust{}; |
| 282 | if (param.ssrc.has_value()) { |
| 283 | rust.has_ssrc = param.ssrc.has_value(); |
| 284 | rust.ssrc = param.ssrc.value(); |
| 285 | } |
| 286 | return rust; |
| 287 | } |
| 288 | |
| 289 | RtpEncodingParameters to_rust_rtp_encoding_parameters( |
| 290 | webrtc::RtpEncodingParameters params) { |
| 291 | RtpEncodingParameters rust{}; |
| 292 | if (params.ssrc.has_value()) { |
| 293 | rust.has_ssrc = params.ssrc.has_value(); |
| 294 | rust.ssrc = params.ssrc.value(); |
| 295 | } |
| 296 | |
| 297 | rust.bitrate_priority = params.bitrate_priority; |
| 298 | rust.network_priority = static_cast<Priority>(params.network_priority); |
| 299 | if (params.max_bitrate_bps.has_value()) { |
| 300 | rust.has_max_bitrate_bps = true; |
| 301 | rust.max_bitrate_bps = params.max_bitrate_bps.value(); |
| 302 | } |
| 303 | |
| 304 | if (params.min_bitrate_bps.has_value()) { |
| 305 | rust.has_min_bitrate_bps = true; |
| 306 | rust.min_bitrate_bps = params.min_bitrate_bps.value(); |
| 307 | } |
| 308 | |
| 309 | if (params.max_framerate.has_value()) { |
| 310 | rust.has_max_framerate = true; |
| 311 | rust.max_framerate = params.max_framerate.value(); |
| 312 | } |
| 313 | |
| 314 | if (params.num_temporal_layers.has_value()) { |
| 315 | rust.has_num_temporal_layers = true; |
| 316 | rust.num_temporal_layers = params.num_temporal_layers.value(); |
| 317 | } |
| 318 | |
| 319 | if (params.scale_resolution_down_by.has_value()) { |
| 320 | rust.has_scale_resolution_down_by = true; |
| 321 | rust.scale_resolution_down_by = params.scale_resolution_down_by.value(); |
| 322 | } |
| 323 | |
| 324 | if (params.scalability_mode.has_value()) { |
| 325 | rust.has_scalability_mode = true; |
| 326 | rust.scalability_mode = params.scalability_mode.value(); |
| 327 | } |
| 328 | |
| 329 | rust.active = params.active; |
| 330 | rust.rid = params.rid; |
| 331 | rust.adaptive_ptime = params.adaptive_ptime; |
| 332 | return rust; |
| 333 | } |
| 334 | |
| 335 | RtpCodecParameters to_rust_rtp_codec_parameters( |
| 336 | webrtc::RtpCodecParameters params) { |
| 337 | RtpCodecParameters rust{}; |
| 338 | rust.mime_type = params.mime_type(); |
| 339 | rust.name = params.name; |
| 340 | rust.kind = static_cast<MediaType>(params.kind); |
| 341 | rust.payload_type = params.payload_type; |
| 342 | if (params.clock_rate.has_value()) { |
| 343 | rust.has_clock_rate = true; |
| 344 | rust.clock_rate = params.clock_rate.value(); |
| 345 | } |
| 346 | |
| 347 | if (params.num_channels.has_value()) { |
| 348 | rust.has_num_channels = true; |
| 349 | rust.num_channels = params.num_channels.value(); |
| 350 | } |
| 351 | |
| 352 | for (auto feedback : params.rtcp_feedback) |
| 353 | rust.rtcp_feedback.push_back(to_rust_rtcp_feedback(feedback)); |
| 354 | |
| 355 | for (auto pair : params.parameters) |
| 356 | rust.parameters.push_back(StringKeyValue{pair.first, pair.second}); |
| 357 | |
| 358 | return rust; |
| 359 | } |
| 360 | |
| 361 | RtpCapabilities to_rust_rtp_capabilities(webrtc::RtpCapabilities capabilities) { |
| 362 | RtpCapabilities rust{}; |
| 363 | for (auto codec : capabilities.codecs) |
| 364 | rust.codecs.push_back(to_rust_rtp_codec_capability(codec)); |
| 365 | |
| 366 | for (auto header : capabilities.header_extensions) |
| 367 | rust.header_extensions.push_back( |
| 368 | to_rust_rtp_header_extension_capability(header)); |
| 369 | |
| 370 | for (auto fec : capabilities.fec) |
| 371 | rust.fec.push_back(static_cast<FecMechanism>(fec)); |
| 372 | |
| 373 | return rust; |
| 374 | } |
| 375 | |
| 376 | RtcpParameters to_rust_rtcp_parameters(webrtc::RtcpParameters params) { |
| 377 | RtcpParameters rust{}; |
| 378 | if (params.ssrc.has_value()) { |
| 379 | rust.has_ssrc = true; |
| 380 | rust.ssrc = params.ssrc.value(); |
| 381 | } |
| 382 | |
| 383 | rust.cname = params.cname; |
| 384 | rust.reduced_size = params.reduced_size; |
| 385 | rust.mux = params.mux; |
| 386 | return rust; |
| 387 | } |
| 388 | |
| 389 | RtpParameters to_rust_rtp_parameters(webrtc::RtpParameters params) { |
| 390 | RtpParameters rust{}; |
| 391 | rust.transaction_id = params.transaction_id; |
| 392 | rust.mid = params.mid; |
| 393 | |
| 394 | for (auto codec : params.codecs) |
| 395 | rust.codecs.push_back(to_rust_rtp_codec_parameters(codec)); |
| 396 | |
| 397 | for (auto header : params.header_extensions) |
| 398 | rust.header_extensions.push_back(to_rust_rtp_extension(header)); |
| 399 | |
| 400 | for (auto encoding : params.encodings) |
| 401 | rust.encodings.push_back(to_rust_rtp_encoding_parameters(encoding)); |
| 402 | |
| 403 | rust.rtcp = to_rust_rtcp_parameters(params.rtcp); |
| 404 | |
| 405 | if (params.degradation_preference.has_value()) { |
| 406 | rust.has_degradation_preference = true; |
| 407 | rust.degradation_preference = static_cast<DegradationPreference>( |
| 408 | params.degradation_preference.value()); |
| 409 | } |
| 410 | |
| 411 | return rust; |
| 412 | } |
| 413 | |
| 414 | } // namespace livekit_ffi |