Repository navigation
Expand file tree
/
Copy pathcapability_probe.cpp
More file actions
330 lines (263 loc) · 11.2 KB
/
Copy pathcapability_probe.cpp
File metadata and controls
330 lines (263 loc) · 11.2 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include "capability_probe.h"
#include "hw_accel.h"
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavutil/opt.h>
}
Napi::Object CapabilityProbe::Init(Napi::Env env, Napi::Object exports) {
Napi::Object probe = Napi::Object::New(env);
probe.Set("probeVideoEncoder", Napi::Function::New(env, ProbeVideoEncoder));
probe.Set("probeVideoDecoder", Napi::Function::New(env, ProbeVideoDecoder));
probe.Set("probeAudioEncoder", Napi::Function::New(env, ProbeAudioEncoder));
probe.Set("probeAudioDecoder", Napi::Function::New(env, ProbeAudioDecoder));
exports.Set("CapabilityProbe", probe);
return exports;
}
Napi::Value CapabilityProbe::ProbeVideoEncoder(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (!info[0].IsObject()) {
Napi::TypeError::New(env, "Config object required").ThrowAsJavaScriptException();
return env.Undefined();
}
Napi::Object config = info[0].As<Napi::Object>();
// Get required parameters
std::string codecName = config.Get("codec").As<Napi::String>().Utf8Value();
int width = config.Get("width").As<Napi::Number>().Int32Value();
int height = config.Get("height").As<Napi::Number>().Int32Value();
// Get optional hardware preference
std::string hwPrefStr = "no-preference";
if (config.Has("hardwareAcceleration") && config.Get("hardwareAcceleration").IsString()) {
hwPrefStr = config.Get("hardwareAcceleration").As<Napi::String>().Utf8Value();
}
HWAccel::Preference hwPref = HWAccel::parsePreference(hwPrefStr);
// Prepare result object
Napi::Object result = Napi::Object::New(env);
result.Set("supported", Napi::Boolean::New(env, false));
result.Set("hardwareAccelerated", Napi::Boolean::New(env, false));
// Try to find an encoder
HWAccel::EncoderInfo encInfo = HWAccel::selectEncoder(codecName, hwPref, width, height);
if (!encInfo.codec) {
result.Set("error", Napi::String::New(env, "No encoder found for codec: " + codecName));
return result;
}
// Try to actually open the codec
AVCodecContext* ctx = avcodec_alloc_context3(encInfo.codec);
if (!ctx) {
result.Set("error", Napi::String::New(env, "Failed to allocate codec context"));
return result;
}
ctx->width = width;
ctx->height = height;
ctx->time_base = {1, 1000000};
ctx->pix_fmt = encInfo.inputFormat;
// Set a default bitrate
ctx->bit_rate = 2000000;
// GOP size
ctx->gop_size = 30;
ctx->framerate = {30, 1};
ctx->max_b_frames = 0;
// Setup hardware device if needed
AVBufferRef* hwDeviceCtx = nullptr;
if (encInfo.hwType != HWAccel::Type::None) {
hwDeviceCtx = HWAccel::createHWDeviceContext(encInfo.hwType);
if (hwDeviceCtx) {
ctx->hw_device_ctx = av_buffer_ref(hwDeviceCtx);
result.Set("hardwareAccelerated", Napi::Boolean::New(env, true));
}
}
// Try to open the codec
int ret = avcodec_open2(ctx, encInfo.codec, nullptr);
if (ret >= 0) {
result.Set("supported", Napi::Boolean::New(env, true));
result.Set("encoderName", Napi::String::New(env, encInfo.codec->name));
// Report actual capabilities if available
if (ctx->coded_width > 0 && ctx->coded_height > 0) {
result.Set("codedWidth", Napi::Number::New(env, ctx->coded_width));
result.Set("codedHeight", Napi::Number::New(env, ctx->coded_height));
}
// avcodec_free_context below handles cleanup
} else if (encInfo.hwType != HWAccel::Type::None && hwPref != HWAccel::Preference::PreferHardware) {
// Hardware candidate failed to open (e.g. no GPU in a container);
// the config may still be encodable in software
avcodec_free_context(&ctx);
if (hwDeviceCtx) {
av_buffer_unref(&hwDeviceCtx);
hwDeviceCtx = nullptr;
}
result.Set("hardwareAccelerated", Napi::Boolean::New(env, false));
HWAccel::EncoderInfo swInfo = HWAccel::selectEncoder(codecName, HWAccel::Preference::PreferSoftware, width, height);
if (swInfo.codec) {
ctx = avcodec_alloc_context3(swInfo.codec);
ctx->width = width;
ctx->height = height;
ctx->time_base = {1, 1000000};
ctx->pix_fmt = swInfo.inputFormat;
ctx->bit_rate = 2000000;
ctx->gop_size = 30;
ctx->framerate = {30, 1};
ctx->max_b_frames = 0;
ret = avcodec_open2(ctx, swInfo.codec, nullptr);
if (ret >= 0) {
result.Set("supported", Napi::Boolean::New(env, true));
result.Set("encoderName", Napi::String::New(env, swInfo.codec->name));
} else {
char errBuf[256];
av_strerror(ret, errBuf, sizeof(errBuf));
result.Set("error", Napi::String::New(env, errBuf));
}
avcodec_free_context(&ctx);
}
return result;
} else {
char errBuf[256];
av_strerror(ret, errBuf, sizeof(errBuf));
result.Set("error", Napi::String::New(env, errBuf));
}
if (hwDeviceCtx) {
av_buffer_unref(&hwDeviceCtx);
}
avcodec_free_context(&ctx);
return result;
}
Napi::Value CapabilityProbe::ProbeVideoDecoder(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (!info[0].IsObject()) {
Napi::TypeError::New(env, "Config object required").ThrowAsJavaScriptException();
return env.Undefined();
}
Napi::Object config = info[0].As<Napi::Object>();
std::string codecName = config.Get("codec").As<Napi::String>().Utf8Value();
Napi::Object result = Napi::Object::New(env);
result.Set("supported", Napi::Boolean::New(env, false));
result.Set("hardwareAccelerated", Napi::Boolean::New(env, false));
// Find decoder
const AVCodec* codec = avcodec_find_decoder_by_name(codecName.c_str());
// Try common decoder names if not found
if (!codec) {
if (codecName == "h264" || codecName == "libx264") {
codec = avcodec_find_decoder(AV_CODEC_ID_H264);
} else if (codecName == "vp8" || codecName == "libvpx") {
codec = avcodec_find_decoder(AV_CODEC_ID_VP8);
} else if (codecName == "vp9" || codecName == "libvpx-vp9") {
codec = avcodec_find_decoder(AV_CODEC_ID_VP9);
} else if (codecName == "hevc" || codecName == "libx265") {
codec = avcodec_find_decoder(AV_CODEC_ID_HEVC);
} else if (codecName == "av1" || codecName == "libaom-av1" || codecName == "libdav1d") {
codec = avcodec_find_decoder(AV_CODEC_ID_AV1);
}
}
if (!codec) {
result.Set("error", Napi::String::New(env, "No decoder found for codec: " + codecName));
return result;
}
// Try to open the codec
AVCodecContext* ctx = avcodec_alloc_context3(codec);
if (!ctx) {
result.Set("error", Napi::String::New(env, "Failed to allocate codec context"));
return result;
}
// Set dimensions if provided
if (config.Has("width") && config.Get("width").IsNumber()) {
ctx->width = config.Get("width").As<Napi::Number>().Int32Value();
}
if (config.Has("height") && config.Get("height").IsNumber()) {
ctx->height = config.Get("height").As<Napi::Number>().Int32Value();
}
int ret = avcodec_open2(ctx, codec, nullptr);
if (ret >= 0) {
result.Set("supported", Napi::Boolean::New(env, true));
result.Set("decoderName", Napi::String::New(env, codec->name));
// avcodec_free_context below handles cleanup
} else {
char errBuf[256];
av_strerror(ret, errBuf, sizeof(errBuf));
result.Set("error", Napi::String::New(env, errBuf));
}
avcodec_free_context(&ctx);
return result;
}
Napi::Value CapabilityProbe::ProbeAudioEncoder(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (!info[0].IsObject()) {
Napi::TypeError::New(env, "Config object required").ThrowAsJavaScriptException();
return env.Undefined();
}
Napi::Object config = info[0].As<Napi::Object>();
std::string codecName = config.Get("codec").As<Napi::String>().Utf8Value();
Napi::Object result = Napi::Object::New(env);
result.Set("supported", Napi::Boolean::New(env, false));
// Find encoder
const AVCodec* codec = avcodec_find_encoder_by_name(codecName.c_str());
if (!codec) {
result.Set("error", Napi::String::New(env, "No encoder found for codec: " + codecName));
return result;
}
// Try to open
AVCodecContext* ctx = avcodec_alloc_context3(codec);
if (!ctx) {
result.Set("error", Napi::String::New(env, "Failed to allocate codec context"));
return result;
}
// Set required audio parameters
if (config.Has("sampleRate") && config.Get("sampleRate").IsNumber()) {
ctx->sample_rate = config.Get("sampleRate").As<Napi::Number>().Int32Value();
} else {
ctx->sample_rate = 48000;
}
if (config.Has("numberOfChannels") && config.Get("numberOfChannels").IsNumber()) {
int channels = config.Get("numberOfChannels").As<Napi::Number>().Int32Value();
av_channel_layout_default(&ctx->ch_layout, channels);
} else {
av_channel_layout_default(&ctx->ch_layout, 2);
}
// Set sample format - prefer float planar
ctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
ctx->bit_rate = 128000;
int ret = avcodec_open2(ctx, codec, nullptr);
if (ret >= 0) {
result.Set("supported", Napi::Boolean::New(env, true));
result.Set("encoderName", Napi::String::New(env, codec->name));
// avcodec_free_context below handles cleanup
} else {
char errBuf[256];
av_strerror(ret, errBuf, sizeof(errBuf));
result.Set("error", Napi::String::New(env, errBuf));
}
avcodec_free_context(&ctx);
return result;
}
Napi::Value CapabilityProbe::ProbeAudioDecoder(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (!info[0].IsObject()) {
Napi::TypeError::New(env, "Config object required").ThrowAsJavaScriptException();
return env.Undefined();
}
Napi::Object config = info[0].As<Napi::Object>();
std::string codecName = config.Get("codec").As<Napi::String>().Utf8Value();
Napi::Object result = Napi::Object::New(env);
result.Set("supported", Napi::Boolean::New(env, false));
// Find decoder
const AVCodec* codec = avcodec_find_decoder_by_name(codecName.c_str());
if (!codec) {
result.Set("error", Napi::String::New(env, "No decoder found for codec: " + codecName));
return result;
}
// Try to open
AVCodecContext* ctx = avcodec_alloc_context3(codec);
if (!ctx) {
result.Set("error", Napi::String::New(env, "Failed to allocate codec context"));
return result;
}
int ret = avcodec_open2(ctx, codec, nullptr);
if (ret >= 0) {
result.Set("supported", Napi::Boolean::New(env, true));
result.Set("decoderName", Napi::String::New(env, codec->name));
// avcodec_free_context below handles cleanup
} else {
char errBuf[256];
av_strerror(ret, errBuf, sizeof(errBuf));
result.Set("error", Napi::String::New(env, errBuf));
}
avcodec_free_context(&ctx);
return result;
}