| 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/video_encoder_factory.h" |
| 18 | |
| 19 | #include <algorithm> |
| 20 | #include <atomic> |
| 21 | #include <cctype> |
| 22 | #include <cstdlib> |
| 23 | #include <optional> |
| 24 | #include <string_view> |
| 25 | #include <utility> |
| 26 | |
| 27 | #include "api/video/video_frame.h" |
| 28 | #include "modules/video_coding/include/video_error_codes.h" |
| 29 | |
| 30 | #include "api/environment/environment_factory.h" |
| 31 | #include "api/video_codecs/sdp_video_format.h" |
| 32 | #include "api/video_codecs/video_encoder.h" |
| 33 | #include "api/video_codecs/video_encoder_factory_template.h" |
| 34 | #include "livekit/encoded_video_frame_buffer.h" |
| 35 | #include "livekit/objc_video_factory.h" |
| 36 | #include "livekit/passthrough_video_encoder.h" |
| 37 | #include "livekit/webrtc.h" |
| 38 | #include "media/base/media_constants.h" |
| 39 | #include "media/engine/simulcast_encoder_adapter.h" |
| 40 | #include "rtc_base/logging.h" |
| 41 | #include "rust/cxx.h" |
| 42 | #if defined(RTC_USE_LIBAOM_AV1_ENCODER) |
| 43 | #include "api/video_codecs/video_encoder_factory_template_libaom_av1_adapter.h" |
| 44 | #endif |
| 45 | #if defined(WEBRTC_USE_H264) |
| 46 | #include "api/video_codecs/video_encoder_factory_template_open_h264_adapter.h" |
| 47 | #endif |
| 48 | #include "api/video_codecs/video_encoder_factory_template_libvpx_vp8_adapter.h" |
| 49 | #include "api/video_codecs/video_encoder_factory_template_libvpx_vp9_adapter.h" |
| 50 | |
| 51 | #ifdef WEBRTC_ANDROID |
| 52 | #include "livekit/android.h" |
| 53 | #endif |
| 54 | |
| 55 | #if defined(USE_NVIDIA_VIDEO_CODEC) |
| 56 | #include "nvidia/nvidia_encoder_factory.h" |
| 57 | #endif |
| 58 | |
| 59 | #if defined(USE_VAAPI_VIDEO_CODEC) |
| 60 | #include "vaapi/vaapi_encoder_factory.h" |
| 61 | #endif |
| 62 | |
| 63 | #if defined(USE_JETSON_VIDEO_CODEC) |
| 64 | #include "jetson/jetson_encoder_factory.h" |
| 65 | #endif |
| 66 | |
| 67 | #if defined(USE_MF_VIDEO_CODEC) |
| 68 | #include "mf/mf_encoder_factory.h" |
| 69 | #endif |
| 70 | |
| 71 | namespace livekit_ffi { |
| 72 | |
| 73 | namespace { |
| 74 | |
| 75 | constexpr char kBackendParameter[] = "x-livekit-video-encoder-backend"; |
| 76 | constexpr char kPreferredHwEncoderEnv[] = "LIVEKIT_PREFERRED_HW_ENCODER"; |
| 77 | |
| 78 | enum class PreferredHwEncoder { |
| 79 | kNvenc, |
| 80 | kVaapi, |
| 81 | }; |
| 82 | |
| 83 | struct PreferredHwEncoderConfig { |
| 84 | PreferredHwEncoder encoder = PreferredHwEncoder::kNvenc; |
| 85 | bool explicitly_set = false; |
| 86 | }; |
| 87 | |
| 88 | PreferredHwEncoderConfig GetPreferredHwEncoderConfig() { |
| 89 | const char* preferred_encoder = std::getenv(kPreferredHwEncoderEnv); |
| 90 | if (!preferred_encoder) { |
| 91 | return {}; |
| 92 | } |
| 93 | |
| 94 | std::string_view preferred_encoder_view(preferred_encoder); |
| 95 | if (preferred_encoder_view == "nvenc") { |
| 96 | return {PreferredHwEncoder::kNvenc, true}; |
| 97 | } |
| 98 | if (preferred_encoder_view == "vaapi") { |
| 99 | return {PreferredHwEncoder::kVaapi, true}; |
| 100 | } |
| 101 | |
| 102 | RTC_LOG(LS_WARNING) << "Ignoring invalid LIVEKIT_PREFERRED_HW_ENCODER=\"" |
| 103 | << preferred_encoder |
| 104 | << "\"; expected \"nvenc\" or \"vaapi\"."; |
| 105 | return {}; |
| 106 | } |
| 107 | |
| 108 | const char* BackendName(VideoEncoderBackend backend) { |
| 109 | switch (backend) { |
| 110 | case VideoEncoderBackend::Auto: |
| 111 | return "auto"; |
| 112 | case VideoEncoderBackend::Software: |
| 113 | return "software"; |
| 114 | case VideoEncoderBackend::Hardware: |
| 115 | return "hardware"; |
| 116 | case VideoEncoderBackend::Nvenc: |
| 117 | return "nvenc"; |
| 118 | case VideoEncoderBackend::Vaapi: |
| 119 | return "vaapi"; |
| 120 | case VideoEncoderBackend::VideoToolbox: |
| 121 | return "videotoolbox"; |
| 122 | case VideoEncoderBackend::PreEncoded: |
| 123 | return "preencoded"; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | std::optional<VideoEncoderBackend> BackendFromFormat( |
| 128 | const webrtc::SdpVideoFormat& format) { |
| 129 | auto it = format.parameters.find(kBackendParameter); |
| 130 | if (it == format.parameters.end()) { |
| 131 | return std::nullopt; |
| 132 | } |
| 133 | |
| 134 | if (it->second == BackendName(VideoEncoderBackend::Software)) { |
| 135 | return VideoEncoderBackend::Software; |
| 136 | } |
| 137 | if (it->second == BackendName(VideoEncoderBackend::Hardware)) { |
| 138 | return VideoEncoderBackend::Hardware; |
| 139 | } |
| 140 | if (it->second == BackendName(VideoEncoderBackend::Nvenc)) { |
| 141 | return VideoEncoderBackend::Nvenc; |
| 142 | } |
| 143 | if (it->second == BackendName(VideoEncoderBackend::Vaapi)) { |
| 144 | return VideoEncoderBackend::Vaapi; |
| 145 | } |
| 146 | if (it->second == BackendName(VideoEncoderBackend::VideoToolbox)) { |
| 147 | return VideoEncoderBackend::VideoToolbox; |
| 148 | } |
| 149 | if (it->second == BackendName(VideoEncoderBackend::PreEncoded)) { |
| 150 | return VideoEncoderBackend::PreEncoded; |
| 151 | } |
| 152 | |
| 153 | return std::nullopt; |
| 154 | } |
| 155 | |
| 156 | webrtc::SdpVideoFormat StripBackendParameter( |
| 157 | const webrtc::SdpVideoFormat& format) { |
| 158 | webrtc::SdpVideoFormat stripped = format; |
| 159 | stripped.parameters.erase(kBackendParameter); |
| 160 | return stripped; |
| 161 | } |
| 162 | |
| 163 | webrtc::SdpVideoFormat WithBackend( |
| 164 | const webrtc::SdpVideoFormat& format, |
| 165 | VideoEncoderBackend backend) { |
| 166 | webrtc::SdpVideoFormat tagged = format; |
| 167 | tagged.parameters[kBackendParameter] = BackendName(backend); |
| 168 | return tagged; |
| 169 | } |
| 170 | |
| 171 | bool IsSpecificHardwareBackend(VideoEncoderBackend backend) { |
| 172 | return backend == VideoEncoderBackend::Nvenc || |
| 173 | backend == VideoEncoderBackend::Vaapi || |
| 174 | backend == VideoEncoderBackend::VideoToolbox; |
| 175 | } |
| 176 | |
| 177 | bool BackendMatches(VideoEncoderBackend requested, VideoEncoderBackend actual) { |
| 178 | return requested == actual || |
| 179 | (requested == VideoEncoderBackend::Hardware && |
| 180 | (actual == VideoEncoderBackend::Hardware || |
| 181 | IsSpecificHardwareBackend(actual))); |
| 182 | } |
| 183 | |
| 184 | bool IsAutomaticFallbackBackend(VideoEncoderBackend backend) { |
| 185 | return backend != VideoEncoderBackend::PreEncoded; |
| 186 | } |
| 187 | |
| 188 | bool EqualsIgnoreAsciiCase(std::string_view a, std::string_view b) { |
| 189 | return a.size() == b.size() && |
| 190 | std::equal(a.begin(), a.end(), b.begin(), [](char x, char y) { |
| 191 | return std::tolower(static_cast<unsigned char>(x)) == |
| 192 | std::tolower(static_cast<unsigned char>(y)); |
| 193 | }); |
| 194 | } |
| 195 | |
| 196 | bool IsSameCodecName(std::string_view a, std::string_view b) { |
| 197 | if (EqualsIgnoreAsciiCase(a, b)) { |
| 198 | return true; |
| 199 | } |
| 200 | auto is_h265 = [](std::string_view name) { |
| 201 | return EqualsIgnoreAsciiCase(name, "H265") || |
| 202 | EqualsIgnoreAsciiCase(name, "HEVC"); |
| 203 | }; |
| 204 | return is_h265(a) && is_h265(b); |
| 205 | } |
| 206 | |
| 207 | // The pass-through backend forwards pre-encoded bytes, so SDP profile |
| 208 | // parameters do not constrain it: match it by codec name only. Real |
| 209 | // encoder backends keep exact profile matching. |
| 210 | bool FormatSupportedByBackendFactory(VideoEncoderBackend backend, |
| 211 | const webrtc::SdpVideoFormat& supported, |
| 212 | const webrtc::SdpVideoFormat& requested) { |
| 213 | if (backend == VideoEncoderBackend::PreEncoded) { |
| 214 | return IsSameCodecName(supported.name, requested.name); |
| 215 | } |
| 216 | return supported.IsSameCodec(requested); |
| 217 | } |
| 218 | |
| 219 | void AddBackendFactory( |
| 220 | std::vector<VideoEncoderBackendFactory>& factories, |
| 221 | VideoEncoderBackend backend, |
| 222 | std::unique_ptr<webrtc::VideoEncoderFactory> factory) { |
| 223 | factories.push_back(VideoEncoderBackendFactory{backend, std::move(factory)}); |
| 224 | } |
| 225 | |
| 226 | void AddJetsonFactory( |
| 227 | std::vector<VideoEncoderBackendFactory>& factories) { |
| 228 | #if defined(USE_JETSON_VIDEO_CODEC) |
| 229 | if (webrtc::JetsonVideoEncoderFactory::IsSupported()) { |
| 230 | AddBackendFactory( |
| 231 | factories, |
| 232 | VideoEncoderBackend::Hardware, |
| 233 | std::make_unique<webrtc::JetsonVideoEncoderFactory>()); |
| 234 | return; |
| 235 | } |
| 236 | #else |
| 237 | (void)factories; |
| 238 | #endif |
| 239 | } |
| 240 | |
| 241 | void AddNvencFactory( |
| 242 | std::vector<VideoEncoderBackendFactory>& factories, |
| 243 | bool preferred) { |
| 244 | #if defined(USE_NVIDIA_VIDEO_CODEC) |
| 245 | if (webrtc::NvidiaVideoEncoderFactory::IsSupported()) { |
| 246 | AddBackendFactory( |
| 247 | factories, |
| 248 | VideoEncoderBackend::Nvenc, |
| 249 | std::make_unique<webrtc::NvidiaVideoEncoderFactory>()); |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | if (preferred) { |
| 254 | RTC_LOG(LS_WARNING) |
| 255 | << "LIVEKIT_PREFERRED_HW_ENCODER=nvenc requested, but NVENC " |
| 256 | "is unavailable; falling back to other encoders."; |
| 257 | } |
| 258 | #else |
| 259 | (void)factories; |
| 260 | if (preferred) { |
| 261 | RTC_LOG(LS_WARNING) |
| 262 | << "LIVEKIT_PREFERRED_HW_ENCODER=nvenc requested, but NVENC support " |
| 263 | "is not compiled in; falling back to other encoders."; |
| 264 | } |
| 265 | #endif |
| 266 | } |
| 267 | |
| 268 | void AddVaapiFactory( |
| 269 | std::vector<VideoEncoderBackendFactory>& factories, |
| 270 | bool preferred) { |
| 271 | #if defined(USE_VAAPI_VIDEO_CODEC) |
| 272 | if (webrtc::VAAPIVideoEncoderFactory::IsSupported()) { |
| 273 | AddBackendFactory( |
| 274 | factories, |
| 275 | VideoEncoderBackend::Vaapi, |
| 276 | std::make_unique<webrtc::VAAPIVideoEncoderFactory>()); |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | if (preferred) { |
| 281 | RTC_LOG(LS_WARNING) |
| 282 | << "LIVEKIT_PREFERRED_HW_ENCODER=vaapi requested, but VAAPI " |
| 283 | "is unavailable; falling back to other encoders."; |
| 284 | } |
| 285 | #else |
| 286 | (void)factories; |
| 287 | if (preferred) { |
| 288 | RTC_LOG(LS_WARNING) |
| 289 | << "LIVEKIT_PREFERRED_HW_ENCODER=vaapi requested, but VAAPI support " |
| 290 | "is not compiled in; falling back to other encoders."; |
| 291 | } |
| 292 | #endif |
| 293 | } |
| 294 | |
| 295 | } // namespace |
| 296 | |
| 297 | using Factory = webrtc::VideoEncoderFactoryTemplate< |
| 298 | webrtc::LibvpxVp8EncoderTemplateAdapter, |
| 299 | #if defined(WEBRTC_USE_H264) |
| 300 | webrtc::OpenH264EncoderTemplateAdapter, |
| 301 | #endif |
| 302 | #if defined(RTC_USE_LIBAOM_AV1_ENCODER) |
| 303 | webrtc::LibaomAv1EncoderTemplateAdapter, |
| 304 | #endif |
| 305 | webrtc::LibvpxVp9EncoderTemplateAdapter>; |
| 306 | |
| 307 | rust::Vec<VideoEncoderBackend> video_encoder_backend_list() { |
| 308 | rust::Vec<VideoEncoderBackend> backends; |
| 309 | backends.push_back(VideoEncoderBackend::Auto); |
| 310 | backends.push_back(VideoEncoderBackend::Software); |
| 311 | backends.push_back(VideoEncoderBackend::PreEncoded); |
| 312 | |
| 313 | bool has_hardware_backend = false; |
| 314 | bool hardware_backend_listed = false; |
| 315 | |
| 316 | #ifdef __APPLE__ |
| 317 | backends.push_back(VideoEncoderBackend::VideoToolbox); |
| 318 | has_hardware_backend = true; |
| 319 | #endif |
| 320 | |
| 321 | #ifdef WEBRTC_ANDROID |
| 322 | backends.push_back(VideoEncoderBackend::Hardware); |
| 323 | has_hardware_backend = true; |
| 324 | hardware_backend_listed = true; |
| 325 | #endif |
| 326 | |
| 327 | #if defined(USE_JETSON_VIDEO_CODEC) |
| 328 | if (webrtc::JetsonVideoEncoderFactory::IsSupported()) { |
| 329 | has_hardware_backend = true; |
| 330 | } |
| 331 | #endif |
| 332 | |
| 333 | #if defined(USE_NVIDIA_VIDEO_CODEC) |
| 334 | if (webrtc::NvidiaVideoEncoderFactory::IsSupported()) { |
| 335 | backends.push_back(VideoEncoderBackend::Nvenc); |
| 336 | has_hardware_backend = true; |
| 337 | } |
| 338 | #endif |
| 339 | |
| 340 | #if defined(USE_VAAPI_VIDEO_CODEC) |
| 341 | if (webrtc::VAAPIVideoEncoderFactory::IsSupported()) { |
| 342 | backends.push_back(VideoEncoderBackend::Vaapi); |
| 343 | has_hardware_backend = true; |
| 344 | } |
| 345 | #endif |
| 346 | |
| 347 | #if defined(USE_MF_VIDEO_CODEC) |
| 348 | if (webrtc::MFVideoEncoderFactory::IsSupported()) { |
| 349 | has_hardware_backend = true; |
| 350 | } |
| 351 | #endif |
| 352 | |
| 353 | if (has_hardware_backend && !hardware_backend_listed) { |
| 354 | backends.push_back(VideoEncoderBackend::Hardware); |
| 355 | } |
| 356 | |
| 357 | return backends; |
| 358 | } |
| 359 | |
| 360 | VideoEncoderFactory::InternalFactory::InternalFactory() { |
| 361 | AddBackendFactory( |
| 362 | factories_, |
| 363 | VideoEncoderBackend::PreEncoded, |
| 364 | std::make_unique<livekit_ffi::PassthroughVideoEncoderFactory>()); |
| 365 | |
| 366 | #ifdef __APPLE__ |
| 367 | AddBackendFactory( |
| 368 | factories_, |
| 369 | VideoEncoderBackend::VideoToolbox, |
| 370 | livekit_ffi::CreateObjCVideoEncoderFactory()); |
| 371 | #endif |
| 372 | |
| 373 | #ifdef WEBRTC_ANDROID |
| 374 | AddBackendFactory( |
| 375 | factories_, |
| 376 | VideoEncoderBackend::Hardware, |
| 377 | CreateAndroidVideoEncoderFactory()); |
| 378 | #endif |
| 379 | |
| 380 | AddJetsonFactory(factories_); |
| 381 | |
| 382 | const PreferredHwEncoderConfig preferred_hw_encoder = |
| 383 | GetPreferredHwEncoderConfig(); |
| 384 | if (preferred_hw_encoder.encoder == PreferredHwEncoder::kVaapi) { |
| 385 | AddVaapiFactory(factories_, preferred_hw_encoder.explicitly_set); |
| 386 | AddNvencFactory(factories_, false); |
| 387 | } else { |
| 388 | AddNvencFactory(factories_, preferred_hw_encoder.explicitly_set); |
| 389 | AddVaapiFactory(factories_, false); |
| 390 | } |
| 391 | |
| 392 | #if defined(USE_MF_VIDEO_CODEC) |
| 393 | if (webrtc::MFVideoEncoderFactory::IsSupported()) { |
| 394 | AddBackendFactory( |
| 395 | factories_, |
| 396 | VideoEncoderBackend::Hardware, |
| 397 | std::make_unique<webrtc::MFVideoEncoderFactory>()); |
| 398 | } |
| 399 | #endif |
| 400 | } |
| 401 | |
| 402 | std::vector<webrtc::SdpVideoFormat> |
| 403 | VideoEncoderFactory::InternalFactory::GetSupportedFormats() const { |
| 404 | std::vector<webrtc::SdpVideoFormat> formats = Factory().GetSupportedFormats(); |
| 405 | |
| 406 | for (const auto& backend_factory : factories_) { |
| 407 | if (backend_factory.backend == VideoEncoderBackend::PreEncoded) { |
| 408 | continue; |
| 409 | } |
| 410 | auto supported_formats = backend_factory.factory->GetSupportedFormats(); |
| 411 | formats.insert(formats.end(), supported_formats.begin(), |
| 412 | supported_formats.end()); |
| 413 | } |
| 414 | |
| 415 | // The pass-through factory would otherwise advertise codecs no real |
| 416 | // encoder implements (e.g. H265 on desktops); a normal session |
| 417 | // negotiating such a codec would end up with a sender that cannot create |
| 418 | // an encoder. Only advertise pass-through formats for codecs some real |
| 419 | // encoder already supports. |
| 420 | const size_t real_format_count = formats.size(); |
| 421 | for (const auto& backend_factory : factories_) { |
| 422 | if (backend_factory.backend != VideoEncoderBackend::PreEncoded) { |
| 423 | continue; |
| 424 | } |
| 425 | for (const auto& format : backend_factory.factory->GetSupportedFormats()) { |
| 426 | const bool codec_available = std::any_of( |
| 427 | formats.begin(), formats.begin() + real_format_count, |
| 428 | [&](const webrtc::SdpVideoFormat& existing) { |
| 429 | return IsSameCodecName(existing.name, format.name); |
| 430 | }); |
| 431 | if (codec_available) { |
| 432 | formats.push_back(format); |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | return formats; |
| 437 | } |
| 438 | |
| 439 | std::vector<webrtc::SdpVideoFormat> |
| 440 | VideoEncoderFactory::InternalFactory::GetImplementations() const { |
| 441 | std::vector<webrtc::SdpVideoFormat> formats; |
| 442 | for (const auto& backend_factory : factories_) { |
| 443 | if (backend_factory.backend == VideoEncoderBackend::PreEncoded) { |
| 444 | continue; |
| 445 | } |
| 446 | for (const auto& format : backend_factory.factory->GetImplementations()) { |
| 447 | formats.push_back(WithBackend(format, backend_factory.backend)); |
| 448 | if (IsSpecificHardwareBackend(backend_factory.backend)) { |
| 449 | formats.push_back(WithBackend(format, VideoEncoderBackend::Hardware)); |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | for (const auto& format : Factory().GetImplementations()) { |
| 455 | formats.push_back(WithBackend(format, VideoEncoderBackend::Software)); |
| 456 | } |
| 457 | |
| 458 | return formats; |
| 459 | } |
| 460 | |
| 461 | VideoEncoderFactory::CodecSupport |
| 462 | VideoEncoderFactory::InternalFactory::QueryCodecSupport( |
| 463 | const webrtc::SdpVideoFormat& format, |
| 464 | std::optional<std::string> scalability_mode) const { |
| 465 | auto requested_backend = BackendFromFormat(format); |
| 466 | auto stripped_format = StripBackendParameter(format); |
| 467 | if (requested_backend == VideoEncoderBackend::Software) { |
| 468 | auto original_format = |
| 469 | webrtc::FuzzyMatchSdpVideoFormat(Factory().GetSupportedFormats(), |
| 470 | stripped_format); |
| 471 | auto support = |
| 472 | original_format |
| 473 | ? Factory().QueryCodecSupport(*original_format, scalability_mode) |
| 474 | : webrtc::VideoEncoderFactory::CodecSupport{.is_supported = false}; |
| 475 | if (support.is_supported) { |
| 476 | return support; |
| 477 | } |
| 478 | } else if (requested_backend && |
| 479 | *requested_backend != VideoEncoderBackend::Software && |
| 480 | *requested_backend != VideoEncoderBackend::Auto) { |
| 481 | for (const auto& backend_factory : factories_) { |
| 482 | if (!BackendMatches(*requested_backend, backend_factory.backend)) { |
| 483 | continue; |
| 484 | } |
| 485 | |
| 486 | for (const auto& supported_format : |
| 487 | backend_factory.factory->GetSupportedFormats()) { |
| 488 | if (FormatSupportedByBackendFactory(backend_factory.backend, |
| 489 | supported_format, |
| 490 | stripped_format)) { |
| 491 | return webrtc::VideoEncoderFactory::CodecSupport{ |
| 492 | .is_supported = true, |
| 493 | .is_power_efficient = true, |
| 494 | }; |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | auto original_format = |
| 501 | webrtc::FuzzyMatchSdpVideoFormat(Factory().GetSupportedFormats(), |
| 502 | stripped_format); |
| 503 | auto support = |
| 504 | original_format |
| 505 | ? Factory().QueryCodecSupport(*original_format, scalability_mode) |
| 506 | : webrtc::VideoEncoderFactory::CodecSupport{.is_supported = false}; |
| 507 | if (support.is_supported) { |
| 508 | return support; |
| 509 | } |
| 510 | |
| 511 | for (const auto& backend_factory : factories_) { |
| 512 | if (!IsAutomaticFallbackBackend(backend_factory.backend)) { |
| 513 | continue; |
| 514 | } |
| 515 | for (const auto& supported_format : |
| 516 | backend_factory.factory->GetSupportedFormats()) { |
| 517 | if (stripped_format.IsSameCodec(supported_format)) { |
| 518 | return webrtc::VideoEncoderFactory::CodecSupport{ |
| 519 | .is_supported = true, |
| 520 | .is_power_efficient = true, |
| 521 | }; |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | return support; |
| 527 | } |
| 528 | |
| 529 | std::unique_ptr<webrtc::VideoEncoder> |
| 530 | VideoEncoderFactory::InternalFactory::Create( |
| 531 | const webrtc::Environment& env, |
| 532 | const webrtc::SdpVideoFormat& format) { |
| 533 | auto requested_backend = BackendFromFormat(format); |
| 534 | auto stripped_format = StripBackendParameter(format); |
| 535 | bool requested_backend_unavailable = false; |
| 536 | |
| 537 | if (requested_backend == VideoEncoderBackend::Software) { |
| 538 | auto original_format = |
| 539 | webrtc::FuzzyMatchSdpVideoFormat(Factory().GetSupportedFormats(), |
| 540 | stripped_format); |
| 541 | if (original_format) { |
| 542 | auto encoder = Factory().Create(env, *original_format); |
| 543 | if (encoder) { |
| 544 | return encoder; |
| 545 | } |
| 546 | } |
| 547 | requested_backend_unavailable = true; |
| 548 | } else if (requested_backend && |
| 549 | *requested_backend != VideoEncoderBackend::Auto) { |
| 550 | for (const auto& backend_factory : factories_) { |
| 551 | if (!BackendMatches(*requested_backend, backend_factory.backend)) { |
| 552 | continue; |
| 553 | } |
| 554 | |
| 555 | for (const auto& supported_format : |
| 556 | backend_factory.factory->GetSupportedFormats()) { |
| 557 | if (FormatSupportedByBackendFactory(backend_factory.backend, |
| 558 | supported_format, |
| 559 | stripped_format)) { |
| 560 | auto encoder = backend_factory.factory->Create(env, stripped_format); |
| 561 | if (encoder) { |
| 562 | return encoder; |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | // A real encoder cannot consume the pre-encoded native frame buffers |
| 569 | // this session produces, so falling back would yield a silently broken |
| 570 | // sender. Fail loudly instead. |
| 571 | if (*requested_backend == VideoEncoderBackend::PreEncoded) { |
| 572 | RTC_LOG(LS_ERROR) |
| 573 | << "Pre-encoded pass-through encoder is unavailable for " |
| 574 | << stripped_format.name |
| 575 | << "; refusing to fall back to a real encoder."; |
| 576 | return nullptr; |
| 577 | } |
| 578 | |
| 579 | requested_backend_unavailable = true; |
| 580 | } |
| 581 | |
| 582 | if (requested_backend_unavailable) { |
| 583 | RTC_LOG(LS_WARNING) << "Requested video encoder backend " |
| 584 | << BackendName(*requested_backend) |
| 585 | << " is unavailable for " << stripped_format.name |
| 586 | << "; falling back to another compatible encoder."; |
| 587 | } |
| 588 | |
| 589 | for (const auto& backend_factory : factories_) { |
| 590 | if (!IsAutomaticFallbackBackend(backend_factory.backend)) { |
| 591 | continue; |
| 592 | } |
| 593 | for (const auto& supported_format : |
| 594 | backend_factory.factory->GetSupportedFormats()) { |
| 595 | if (supported_format.IsSameCodec(stripped_format)) |
| 596 | return backend_factory.factory->Create(env, stripped_format); |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | auto original_format = |
| 601 | webrtc::FuzzyMatchSdpVideoFormat(Factory().GetSupportedFormats(), |
| 602 | stripped_format); |
| 603 | |
| 604 | if (original_format) { |
| 605 | return Factory().Create(env, *original_format); |
| 606 | } |
| 607 | |
| 608 | RTC_LOG(LS_ERROR) << "No VideoEncoder found for " << stripped_format.name; |
| 609 | return nullptr; |
| 610 | } |
| 611 | |
| 612 | VideoEncoderFactory::VideoEncoderFactory() { |
| 613 | internal_factory_ = std::make_unique<InternalFactory>(); |
| 614 | } |
| 615 | |
| 616 | std::vector<webrtc::SdpVideoFormat> VideoEncoderFactory::GetSupportedFormats() |
| 617 | const { |
| 618 | return internal_factory_->GetSupportedFormats(); |
| 619 | } |
| 620 | |
| 621 | std::vector<webrtc::SdpVideoFormat> VideoEncoderFactory::GetImplementations() |
| 622 | const { |
| 623 | return internal_factory_->GetImplementations(); |
| 624 | } |
| 625 | |
| 626 | VideoEncoderFactory::CodecSupport VideoEncoderFactory::QueryCodecSupport( |
| 627 | const webrtc::SdpVideoFormat& format, |
| 628 | std::optional<std::string> scalability_mode) const { |
| 629 | return internal_factory_->QueryCodecSupport(format, scalability_mode); |
| 630 | } |
| 631 | |
| 632 | namespace { |
| 633 | |
| 634 | // Real encoders can never consume pre-encoded access units, but frames |
| 635 | // carrying an EncodedVideoFrameBuffer can still reach one in the window |
| 636 | // between stream startup and the sender's encoder selector switching onto |
| 637 | // the pass-through backend. Some platform encoders blind-cast native |
| 638 | // buffers (macOS ObjCVideoEncoder casts to ObjCFrameBuffer and retains a |
| 639 | // garbage pointer), so forwarding such a frame is a crash, not a graceful |
| 640 | // failure. Drop it instead: the selector switches shortly after, and the |
| 641 | // pass-through encoder requests a fresh keyframe when it takes over. |
| 642 | class EncodedFrameGuardEncoder final : public webrtc::VideoEncoder { |
| 643 | public: |
| 644 | explicit EncodedFrameGuardEncoder( |
| 645 | std::unique_ptr<webrtc::VideoEncoder> encoder) |
| 646 | : encoder_(std::move(encoder)) {} |
| 647 | |
| 648 | void SetFecControllerOverride( |
| 649 | webrtc::FecControllerOverride* fec_controller_override) override { |
| 650 | encoder_->SetFecControllerOverride(fec_controller_override); |
| 651 | } |
| 652 | |
| 653 | int InitEncode(const webrtc::VideoCodec* codec_settings, |
| 654 | const Settings& settings) override { |
| 655 | return encoder_->InitEncode(codec_settings, settings); |
| 656 | } |
| 657 | |
| 658 | int32_t RegisterEncodeCompleteCallback( |
| 659 | webrtc::EncodedImageCallback* callback) override { |
| 660 | return encoder_->RegisterEncodeCompleteCallback(callback); |
| 661 | } |
| 662 | |
| 663 | int32_t Release() override { return encoder_->Release(); } |
| 664 | |
| 665 | int32_t Encode( |
| 666 | const webrtc::VideoFrame& frame, |
| 667 | const std::vector<webrtc::VideoFrameType>* frame_types) override { |
| 668 | if (livekit::EncodedVideoFrameBuffer::FromNative( |
| 669 | frame.video_frame_buffer().get())) { |
| 670 | static std::atomic<bool> logged{false}; |
| 671 | if (!logged.exchange(true)) { |
| 672 | RTC_LOG(LS_WARNING) |
| 673 | << "Dropping pre-encoded access unit sent to a non pass-through " |
| 674 | "encoder; waiting for the sender to switch onto the " |
| 675 | "pass-through backend"; |
| 676 | } |
| 677 | return WEBRTC_VIDEO_CODEC_OK; |
| 678 | } |
| 679 | return encoder_->Encode(frame, frame_types); |
| 680 | } |
| 681 | |
| 682 | void SetRates(const RateControlParameters& parameters) override { |
| 683 | encoder_->SetRates(parameters); |
| 684 | } |
| 685 | |
| 686 | void OnPacketLossRateUpdate(float packet_loss_rate) override { |
| 687 | encoder_->OnPacketLossRateUpdate(packet_loss_rate); |
| 688 | } |
| 689 | |
| 690 | void OnRttUpdate(int64_t rtt_ms) override { encoder_->OnRttUpdate(rtt_ms); } |
| 691 | |
| 692 | void OnLossNotification(const LossNotification& loss_notification) override { |
| 693 | encoder_->OnLossNotification(loss_notification); |
| 694 | } |
| 695 | |
| 696 | EncoderInfo GetEncoderInfo() const override { |
| 697 | return encoder_->GetEncoderInfo(); |
| 698 | } |
| 699 | |
| 700 | private: |
| 701 | std::unique_ptr<webrtc::VideoEncoder> encoder_; |
| 702 | }; |
| 703 | |
| 704 | } // namespace |
| 705 | |
| 706 | std::unique_ptr<webrtc::VideoEncoder> VideoEncoderFactory::Create( |
| 707 | const webrtc::Environment& env, |
| 708 | const webrtc::SdpVideoFormat& format) { |
| 709 | std::unique_ptr<webrtc::VideoEncoder> encoder; |
| 710 | if (format.IsCodecInList(internal_factory_->GetSupportedFormats())) { |
| 711 | encoder = std::make_unique<webrtc::SimulcastEncoderAdapter>( |
| 712 | env, internal_factory_.get(), nullptr, format); |
| 713 | } |
| 714 | |
| 715 | if (encoder && |
| 716 | BackendFromFormat(format) != VideoEncoderBackend::PreEncoded) { |
| 717 | encoder = std::make_unique<EncodedFrameGuardEncoder>(std::move(encoder)); |
| 718 | } |
| 719 | |
| 720 | return encoder; |
| 721 | } |
| 722 | |
| 723 | } // namespace livekit_ffi |