Repository navigation
Expand file tree
/
Copy pathvideo_encoder_factory.cpp
More file actions
218 lines (199 loc) · 7.02 KB
/
Copy pathvideo_encoder_factory.cpp
File metadata and controls
218 lines (199 loc) · 7.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "video_encoder_factory.h"
#include <exception>
#include "avt/svt_av1_encoder.h"
#if !defined(MINIRTC_IOS)
#include "openh264/openh264_encoder.h"
#endif
#if defined(_WIN32) || defined(_WIN64)
#include "wmf/wmf_video_encoder.h"
#elif defined(__APPLE__)
#include "video_toolbox/video_toolbox_encoder.h"
#endif
#if USE_CUDA && (defined(_WIN32) || (defined(__linux__) && defined(__x86_64__)))
#define MINIRTC_HAS_CUDA_ENCODER 1
#include "nvcodec/nvidia_video_encoder.h"
#else
#define MINIRTC_HAS_CUDA_ENCODER 0
#endif
#if defined(__ANDROID__)
#include "android_codec_support.h"
#include "mediacodec/android_video_encoder.h"
#endif
#include "log.h"
namespace minirtc {
VideoEncoderFactory::VideoEncoderFactory() {}
VideoEncoderFactory::~VideoEncoderFactory() {}
std::unique_ptr<MediaCodec> VideoEncoderFactory::TryInitEncoder(
std::unique_ptr<MediaCodec> encoder, const MediaCodecConfig& config) {
if (!encoder) return nullptr;
std::string name = "requested encoder";
try {
name = encoder->GetEncoderName();
if (encoder->Init(config) == 0) return encoder;
LOG_WARN("Encoder [{}] initialization failed", name);
} catch (const std::exception& error) {
LOG_WARN("Encoder [{}] initialization threw: {}", name, error.what());
} catch (...) {
LOG_WARN("Encoder [{}] initialization threw an unknown exception", name);
}
return nullptr;
}
std::unique_ptr<MediaCodec> VideoEncoderFactory::CreateVideoEncoder(
std::shared_ptr<SystemClock> clock, bool hardware_acceleration,
VideoCodecType codec_type) {
#if defined(__ANDROID__)
if (hardware_acceleration && (codec_type == VideoCodecType::H264 ||
codec_type == VideoCodecType::AV1)) {
return std::make_unique<AndroidVideoEncoder>(clock, codec_type);
}
#endif
if (hardware_acceleration &&
(codec_type == VideoCodecType::H265 || codec_type == VideoCodecType::AV1)) {
#if MINIRTC_HAS_CUDA_ENCODER
if (CheckIsCudaEncodeSupported(codec_type))
return std::make_unique<NvidiaVideoEncoder>(clock, codec_type);
#endif
#if defined(_WIN32) || defined(_WIN64)
if (WmfVideoEncoder::IsSupported(codec_type))
return std::make_unique<WmfVideoEncoder>(clock, codec_type);
#elif defined(__APPLE__) && TARGET_OS_OSX
if (codec_type == VideoCodecType::H265 && CheckIsHardwareAccelerationSupported(codec_type))
return std::make_unique<VideoToolboxEncoder>(clock, codec_type);
#endif
}
if (codec_type == VideoCodecType::H265) return nullptr;
if (codec_type == VideoCodecType::AV1) {
LOG_INFO("Using the SVT-AV1 software encoder");
return std::make_unique<SvtAv1Encoder>(clock);
}
if (codec_type != VideoCodecType::H264) {
LOG_ERROR("Unsupported video codec type [{}]",
static_cast<int>(codec_type));
return nullptr;
}
#if defined(_WIN32) || defined(_WIN64)
if (hardware_acceleration) {
#if USE_CUDA
if (CheckIsCudaEncodeSupported()) {
return std::make_unique<NvidiaVideoEncoder>(clock);
}
#endif
return std::make_unique<WmfVideoEncoder>(clock, codec_type);
}
return std::make_unique<OpenH264Encoder>(clock);
#elif defined(MINIRTC_IOS)
if (!hardware_acceleration) {
LOG_ERROR("Software H.264 encoding is unavailable on iOS");
return nullptr;
}
return std::make_unique<VideoToolboxEncoder>(clock);
#elif defined(__APPLE__)
if (hardware_acceleration) {
return std::make_unique<VideoToolboxEncoder>(clock);
}
LOG_INFO("Hardware H.264 encoding disabled; using the OpenH264 encoder");
return std::make_unique<OpenH264Encoder>(clock);
#elif defined(__linux__) && defined(__aarch64__)
return std::make_unique<OpenH264Encoder>(OpenH264Encoder(clock));
#else
#if USE_CUDA
if (hardware_acceleration) {
if (CheckIsHardwareAccelerationSupported(VideoCodecType::H264)) {
return std::make_unique<NvidiaVideoEncoder>(clock);
} else {
return nullptr;
}
} else {
#endif
return std::make_unique<OpenH264Encoder>(OpenH264Encoder(clock));
#if USE_CUDA
}
#endif
#endif
}
std::unique_ptr<MediaCodec> VideoEncoderFactory::CreateInitializedVideoEncoder(
std::shared_ptr<SystemClock> clock, const MediaCodecConfig& config,
bool hardware_acceleration, VideoCodecType codec_type) {
#if defined(_WIN32) || defined(_WIN64)
if (codec_type == VideoCodecType::H264 ||
codec_type == VideoCodecType::H265 || codec_type == VideoCodecType::AV1) {
if (hardware_acceleration)
return CreateWindowsCaptureEncoder(clock, config, codec_type);
if (codec_type == VideoCodecType::H265) return nullptr;
return TryInitEncoder(CreateVideoEncoder(clock, false, codec_type), config);
}
#endif
auto encoder = TryInitEncoder(
CreateVideoEncoder(clock, hardware_acceleration, codec_type), config);
if (encoder) {
return encoder;
}
#if defined(MINIRTC_IOS)
// iOS has no software H.264 backend to fall back to.
return nullptr;
#else
if (!hardware_acceleration || codec_type == VideoCodecType::H265) {
return nullptr;
}
return TryInitEncoder(CreateVideoEncoder(clock, false, codec_type), config);
#endif
}
#ifdef _WIN32
std::unique_ptr<MediaCodec> VideoEncoderFactory::CreateWindowsDefaultEncoder(
std::shared_ptr<SystemClock> clock, const MediaCodecConfig& config, VideoCodecType codec_type) {
#if MINIRTC_HAS_CUDA_ENCODER
if (CheckIsCudaEncodeSupported(codec_type)) {
if (auto encoder = TryInitEncoder(std::make_unique<NvidiaVideoEncoder>(clock, codec_type), config))
return encoder;
}
#endif
return CreateWindowsFallbackEncoder(clock, config, codec_type);
}
std::unique_ptr<MediaCodec> VideoEncoderFactory::CreateWindowsFallbackEncoder(
std::shared_ptr<SystemClock> clock, const MediaCodecConfig& config,
VideoCodecType codec_type) {
if (auto encoder = TryInitEncoder(
std::make_unique<WmfVideoEncoder>(clock, codec_type), config)) {
return encoder;
}
return TryInitEncoder(CreateVideoEncoder(clock, false, codec_type), config);
}
#endif
bool VideoEncoderFactory::CheckIsHardwareAccelerationSupported(
VideoCodecType codec_type) {
if (codec_type == VideoCodecType::H265 || codec_type == VideoCodecType::AV1) {
#if MINIRTC_HAS_CUDA_ENCODER
if (CheckIsCudaEncodeSupported(codec_type)) return true;
#endif
#if defined(_WIN32) || defined(_WIN64)
return WmfVideoEncoder::IsSupported(codec_type);
#elif defined(__APPLE__) && TARGET_OS_OSX
return codec_type == VideoCodecType::H265 && VideoToolboxEncoder::IsHardwareHevcSupported();
#elif defined(__ANDROID__)
return codec_type == VideoCodecType::AV1 && !FindAndroidCodecs(codec_type, true).empty();
#else
return false;
#endif
}
#if defined(__ANDROID__)
return !FindAndroidCodecs(codec_type, true).empty();
#else
if (codec_type != VideoCodecType::H264) {
return false;
}
#if defined(_WIN32) || defined(_WIN64)
#if USE_CUDA
if (CheckIsCudaEncodeSupported()) return true;
#endif
return WmfVideoEncoder::IsSupported(codec_type);
#elif defined(__APPLE__)
return true;
#elif defined(__linux__) && (defined(__x86_64__) || defined(__amd64__)) && \
USE_CUDA
return CheckIsCudaEncodeSupported();
#else
return false;
#endif
#endif
}
} // namespace minirtc