fork/rust-sdks

git clone https://git.julia.blue/fork/rust-sdks

aoife cassidychore(webrtc-sys): bump to m125_release (#505)a1b926e

main
4.5 KiB134 linesraw
1/*
2 * Copyright 2023 LiveKit
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 "api/environment/environment_factory.h"
20#include "api/video_codecs/sdp_video_format.h"
21#include "api/video_codecs/video_encoder.h"
22#include "api/video_codecs/video_encoder_factory_template.h"
23#include "livekit/objc_video_factory.h"
24#include "media/base/media_constants.h"
25#include "media/engine/simulcast_encoder_adapter.h"
26#include "rtc_base/logging.h"
27#if defined(RTC_USE_LIBAOM_AV1_ENCODER)
28#include "api/video_codecs/video_encoder_factory_template_libaom_av1_adapter.h"
29#endif
30#if defined(WEBRTC_USE_H264)
31#include "api/video_codecs/video_encoder_factory_template_open_h264_adapter.h"
32#endif
33#include "api/video_codecs/video_encoder_factory_template_libvpx_vp8_adapter.h"
34#include "api/video_codecs/video_encoder_factory_template_libvpx_vp9_adapter.h"
35
36#ifdef WEBRTC_ANDROID
37#include "livekit/android.h"
38#endif
39
40namespace livekit {
41
42using Factory = webrtc::VideoEncoderFactoryTemplate<
43    webrtc::LibvpxVp8EncoderTemplateAdapter,
44#if defined(WEBRTC_USE_H264)
45    webrtc::OpenH264EncoderTemplateAdapter,
46#endif
47#if defined(RTC_USE_LIBAOM_AV1_ENCODER)
48    webrtc::LibaomAv1EncoderTemplateAdapter,
49#endif
50    webrtc::LibvpxVp9EncoderTemplateAdapter>;
51
52VideoEncoderFactory::InternalFactory::InternalFactory() {
53#ifdef __APPLE__
54  factories_.push_back(livekit::CreateObjCVideoEncoderFactory());
55#endif
56
57#ifdef WEBRTC_ANDROID
58  factories_.push_back(CreateAndroidVideoEncoderFactory());
59#endif
60
61  // TODO(theomonnom): Add other HW encoders here
62}
63
64std::vector<webrtc::SdpVideoFormat>
65VideoEncoderFactory::InternalFactory::GetSupportedFormats() const {
66  std::vector<webrtc::SdpVideoFormat> formats = Factory().GetSupportedFormats();
67
68  for (const auto& factory : factories_) {
69    auto supported_formats = factory->GetSupportedFormats();
70    formats.insert(formats.end(), supported_formats.begin(),
71                   supported_formats.end());
72  }
73  return formats;
74}
75
76VideoEncoderFactory::CodecSupport
77VideoEncoderFactory::InternalFactory::QueryCodecSupport(
78    const webrtc::SdpVideoFormat& format,
79    absl::optional<std::string> scalability_mode) const {
80  auto original_format =
81      webrtc::FuzzyMatchSdpVideoFormat(Factory().GetSupportedFormats(), format);
82  return original_format
83             ? Factory().QueryCodecSupport(*original_format, scalability_mode)
84             : webrtc::VideoEncoderFactory::CodecSupport{.is_supported = false};
85}
86
87std::unique_ptr<webrtc::VideoEncoder>
88VideoEncoderFactory::InternalFactory::Create(
89    const webrtc::Environment& env, const webrtc::SdpVideoFormat& format) {
90  for (const auto& factory : factories_) {
91    for (const auto& supported_format : factory->GetSupportedFormats()) {
92      if (supported_format.IsSameCodec(format))
93        return factory->Create(env, format);
94    }
95  }
96
97  auto original_format =
98      webrtc::FuzzyMatchSdpVideoFormat(Factory().GetSupportedFormats(), format);
99
100  if (original_format) {
101    return Factory().Create(env, *original_format);
102  }
103
104  RTC_LOG(LS_ERROR) << "No VideoEncoder found for " << format.name;
105  return nullptr;
106}
107
108VideoEncoderFactory::VideoEncoderFactory() {
109  internal_factory_ = std::make_unique<InternalFactory>();
110}
111
112std::vector<webrtc::SdpVideoFormat> VideoEncoderFactory::GetSupportedFormats()
113    const {
114  return internal_factory_->GetSupportedFormats();
115}
116
117VideoEncoderFactory::CodecSupport VideoEncoderFactory::QueryCodecSupport(
118    const webrtc::SdpVideoFormat& format,
119    absl::optional<std::string> scalability_mode) const {
120  return internal_factory_->QueryCodecSupport(format, scalability_mode);
121}
122
123std::unique_ptr<webrtc::VideoEncoder> VideoEncoderFactory::Create(
124    const webrtc::Environment& env, const webrtc::SdpVideoFormat& format) {
125  std::unique_ptr<webrtc::VideoEncoder> encoder;
126  if (format.IsCodecInList(internal_factory_->GetSupportedFormats())) {
127    encoder = std::make_unique<webrtc::SimulcastEncoderAdapter>(
128        env, internal_factory_.get(), nullptr, format);
129  }
130
131  return encoder;
132}
133
134}  // namespace livekit