/* * Copyright 2023 LiveKit * * Licensed under the Apache License, Version 2.0 (the “License”); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an “AS IS” BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "livekit/video_encoder_factory.h" #include "api/environment/environment_factory.h" #include "api/video_codecs/sdp_video_format.h" #include "api/video_codecs/video_encoder.h" #include "api/video_codecs/video_encoder_factory_template.h" #include "livekit/objc_video_factory.h" #include "media/base/media_constants.h" #include "media/engine/simulcast_encoder_adapter.h" #include "rtc_base/logging.h" #if defined(RTC_USE_LIBAOM_AV1_ENCODER) #include "api/video_codecs/video_encoder_factory_template_libaom_av1_adapter.h" #endif #if defined(WEBRTC_USE_H264) #include "api/video_codecs/video_encoder_factory_template_open_h264_adapter.h" #endif #include "api/video_codecs/video_encoder_factory_template_libvpx_vp8_adapter.h" #include "api/video_codecs/video_encoder_factory_template_libvpx_vp9_adapter.h" #ifdef WEBRTC_ANDROID #include "livekit/android.h" #endif namespace livekit { using Factory = webrtc::VideoEncoderFactoryTemplate< webrtc::LibvpxVp8EncoderTemplateAdapter, #if defined(WEBRTC_USE_H264) webrtc::OpenH264EncoderTemplateAdapter, #endif #if defined(RTC_USE_LIBAOM_AV1_ENCODER) webrtc::LibaomAv1EncoderTemplateAdapter, #endif webrtc::LibvpxVp9EncoderTemplateAdapter>; VideoEncoderFactory::InternalFactory::InternalFactory() { #ifdef __APPLE__ factories_.push_back(livekit::CreateObjCVideoEncoderFactory()); #endif #ifdef WEBRTC_ANDROID factories_.push_back(CreateAndroidVideoEncoderFactory()); #endif // TODO(theomonnom): Add other HW encoders here } std::vector VideoEncoderFactory::InternalFactory::GetSupportedFormats() const { std::vector formats = Factory().GetSupportedFormats(); for (const auto& factory : factories_) { auto supported_formats = factory->GetSupportedFormats(); formats.insert(formats.end(), supported_formats.begin(), supported_formats.end()); } return formats; } VideoEncoderFactory::CodecSupport VideoEncoderFactory::InternalFactory::QueryCodecSupport( const webrtc::SdpVideoFormat& format, absl::optional scalability_mode) const { auto original_format = webrtc::FuzzyMatchSdpVideoFormat(Factory().GetSupportedFormats(), format); return original_format ? Factory().QueryCodecSupport(*original_format, scalability_mode) : webrtc::VideoEncoderFactory::CodecSupport{.is_supported = false}; } std::unique_ptr VideoEncoderFactory::InternalFactory::Create( const webrtc::Environment& env, const webrtc::SdpVideoFormat& format) { for (const auto& factory : factories_) { for (const auto& supported_format : factory->GetSupportedFormats()) { if (supported_format.IsSameCodec(format)) return factory->Create(env, format); } } auto original_format = webrtc::FuzzyMatchSdpVideoFormat(Factory().GetSupportedFormats(), format); if (original_format) { return Factory().Create(env, *original_format); } RTC_LOG(LS_ERROR) << "No VideoEncoder found for " << format.name; return nullptr; } VideoEncoderFactory::VideoEncoderFactory() { internal_factory_ = std::make_unique(); } std::vector VideoEncoderFactory::GetSupportedFormats() const { return internal_factory_->GetSupportedFormats(); } VideoEncoderFactory::CodecSupport VideoEncoderFactory::QueryCodecSupport( const webrtc::SdpVideoFormat& format, absl::optional scalability_mode) const { return internal_factory_->QueryCodecSupport(format, scalability_mode); } std::unique_ptr VideoEncoderFactory::Create( const webrtc::Environment& env, const webrtc::SdpVideoFormat& format) { std::unique_ptr encoder; if (format.IsCodecInList(internal_factory_->GetSupportedFormats())) { encoder = std::make_unique( env, internal_factory_.get(), nullptr, format); } return encoder; } } // namespace livekit