BlueGit

ict rusty-teams

Files

rusty-teams / vendor / webrtc-sys / src / video_decoder_factory.cpp

video_decoder_factory.cpp

video_decoder_factory.cpp 6.4 KiB Download Raw
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_decoder_factory.h"
18
19#include <modules/video_coding/codecs/av1/av1_svc_config.h>
20#include "api/environment/environment.h"
21#include "api/video_codecs/av1_profile.h"
22#include "api/video_codecs/sdp_video_format.h"
23#include "livekit/objc_video_factory.h"
24#include "media/base/media_constants.h"
25#include "modules/video_coding/codecs/h264/include/h264.h"
26#include "modules/video_coding/codecs/vp8/include/vp8.h"
27#include "modules/video_coding/codecs/vp9/include/vp9.h"
28#include "rtc_base/logging.h"
29
30#if defined(RTC_DAV1D_IN_INTERNAL_DECODER_FACTORY)
31#include "modules/video_coding/codecs/av1/dav1d_decoder.h" // nogncheck
32#endif
33
34#ifdef WEBRTC_ANDROID
35#include "livekit/android.h"
36#endif
37
38#if defined(USE_NVIDIA_VIDEO_CODEC)
39#include "nvidia/nvidia_decoder_factory.h"
40#endif
41
42namespace livekit_ffi {
43
44namespace {
45// H264Decoder::IsSupported() only reflects the WEBRTC_USE_H264 build flag;
46// desktop prebuilts link an FFmpeg without the H.264 codec, which only
47// surfaces when Configure() fails at runtime ("FFmpeg H.264 decoder not
48// found"). Probe once so the SDP does not advertise decode support the
49// internal decoder cannot deliver.
50bool IsInternalH264DecoderAvailable() {
51 if (!webrtc::H264Decoder::IsSupported()) {
52 RTC_LOG(LS_WARNING) << "Internal H264 decoder not compiled in "
53 "(WEBRTC_USE_H264 off)";
54 return false;
55 }
56 auto decoder = webrtc::H264Decoder::Create();
57 if (!decoder) {
58 RTC_LOG(LS_WARNING) << "H264Decoder::Create() returned null";
59 return false;
60 }
61 webrtc::VideoDecoder::Settings settings;
62 settings.set_codec_type(webrtc::kVideoCodecH264);
63 if (!decoder->Configure(settings)) {
64 RTC_LOG(LS_WARNING) << "Internal H264 decoder failed to configure; "
65 "FFmpeg likely lacks the H.264 codec";
66 return false;
67 }
68 return true;
69}
70} // namespace
71
72VideoDecoderFactory::VideoDecoderFactory()
73 : internal_h264_decoder_works_(IsInternalH264DecoderAvailable()) {
74#ifdef __APPLE__
75 factories_.push_back(livekit_ffi::CreateObjCVideoDecoderFactory());
76#endif
77
78#ifdef WEBRTC_ANDROID
79 factories_.push_back(CreateAndroidVideoDecoderFactory());
80#endif
81
82#if defined(USE_NVIDIA_VIDEO_CODEC)
83 if (webrtc::NvidiaVideoDecoderFactory::IsSupported()) {
84 factories_.push_back(std::make_unique<webrtc::NvidiaVideoDecoderFactory>());
85 }
86#endif
87
88 if (!internal_h264_decoder_works_) {
89 RTC_LOG(LS_WARNING) << "Internal H264 decoder is unavailable, "
90 "not advertising its formats";
91 }
92}
93
94std::vector<webrtc::SdpVideoFormat> VideoDecoderFactory::GetSupportedFormats()
95 const {
96 std::vector<webrtc::SdpVideoFormat> formats;
97
98 for (const auto& factory : factories_) {
99 auto supported_formats = factory->GetSupportedFormats();
100 formats.insert(formats.end(), supported_formats.begin(),
101 supported_formats.end());
102 }
103
104 formats.push_back(webrtc::SdpVideoFormat(webrtc::kVp8CodecName));
105 for (const webrtc::SdpVideoFormat& format :
106 webrtc::SupportedVP9DecoderCodecs())
107 formats.push_back(format);
108 if (internal_h264_decoder_works_) {
109 for (const webrtc::SdpVideoFormat& h264_format :
110 webrtc::SupportedH264DecoderCodecs()) {
111 formats.push_back(h264_format);
112 }
113 }
114
115 formats.push_back(webrtc::SdpVideoFormat(
116 webrtc::SdpVideoFormat::AV1Profile0(),
117 webrtc::LibaomAv1EncoderSupportedScalabilityModes()));
118 return formats;
119}
120
121VideoDecoderFactory::CodecSupport VideoDecoderFactory::QueryCodecSupport(
122 const webrtc::SdpVideoFormat& format,
123 bool reference_scaling) const {
124 if (reference_scaling) {
125 webrtc::VideoCodecType codec =
126 webrtc::PayloadStringToCodecType(format.name);
127 if (codec != webrtc::kVideoCodecVP9 && codec != webrtc::kVideoCodecAV1) {
128 return {/*is_supported=*/false, /*is_power_efficient=*/false};
129 }
130 }
131
132 CodecSupport codec_support;
133 codec_support.is_supported = format.IsCodecInList(GetSupportedFormats());
134 return codec_support;
135}
136
137std::unique_ptr<webrtc::VideoDecoder> VideoDecoderFactory::Create(
138 const webrtc::Environment& env, const webrtc::SdpVideoFormat& format) {
139 for (const auto& factory : factories_) {
140 for (const auto& supported_format : factory->GetSupportedFormats()) {
141 if (supported_format.IsSameCodec(format))
142 return factory->Create(env, format);
143 }
144 }
145
146 // IsSameCodec treats H.264 packetization-modes as distinct codecs, so when
147 // the SFU sends mode=0 but the platform factory only advertises mode=1 the
148 // strict match above fails. Retry with the factory's packetization-mode so
149 // only that parameter is relaxed while the profile-level-id check is kept.
150 if (absl::EqualsIgnoreCase(format.name, webrtc::kH264CodecName)) {
151 for (const auto& factory : factories_) {
152 for (const auto& sf : factory->GetSupportedFormats()) {
153 if (!absl::EqualsIgnoreCase(sf.name, webrtc::kH264CodecName))
154 continue;
155 auto adjusted = format;
156 auto it = sf.parameters.find("packetization-mode");
157 if (it != sf.parameters.end())
158 adjusted.parameters["packetization-mode"] = it->second;
159 else
160 adjusted.parameters.erase("packetization-mode");
161 if (sf.IsSameCodec(adjusted))
162 return factory->Create(env, adjusted);
163 }
164 }
165 }
166
167 if (absl::EqualsIgnoreCase(format.name, webrtc::kVp8CodecName))
168 return webrtc::CreateVp8Decoder(env);
169 if (absl::EqualsIgnoreCase(format.name, webrtc::kVp9CodecName))
170 return webrtc::VP9Decoder::Create();
171 if (absl::EqualsIgnoreCase(format.name, webrtc::kH264CodecName) &&
172 internal_h264_decoder_works_)
173 return webrtc::H264Decoder::Create();
174
175
176#if defined(RTC_DAV1D_IN_INTERNAL_DECODER_FACTORY)
177 if (absl::EqualsIgnoreCase(format.name, webrtc::kAv1CodecName)) {
178 return webrtc::CreateDav1dDecoder();
179 }
180#endif
181
182
183 RTC_LOG(LS_ERROR) << "No VideoDecoder found for " << format.name;
184 return nullptr;
185}
186
187} // namespace livekit_ffi