Repository navigation
Expand file tree
/
Copy pathasync_decoder.cpp
More file actions
500 lines (413 loc) · 14.9 KB
/
Copy pathasync_decoder.cpp
File metadata and controls
500 lines (413 loc) · 14.9 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#include "async_decoder.h"
#include "env_state.h"
#include "frame.h"
Napi::FunctionReference VideoDecoderAsync::constructor;
Napi::Object VideoDecoderAsync::Init(Napi::Env env, Napi::Object exports) {
Napi::Function func = DefineClass(env, "VideoDecoderAsync", {
InstanceMethod("configure", &VideoDecoderAsync::Configure),
InstanceMethod("decode", &VideoDecoderAsync::Decode),
InstanceMethod("flush", &VideoDecoderAsync::Flush),
InstanceMethod("reset", &VideoDecoderAsync::Reset),
InstanceMethod("close", &VideoDecoderAsync::Close),
});
constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
exports.Set("VideoDecoderAsync", func);
return exports;
}
VideoDecoderAsync::VideoDecoderAsync(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<VideoDecoderAsync>(info)
, codecCtx_(nullptr)
, codec_(nullptr) {
Napi::Env env = info.Env();
if (info.Length() < 2) {
Napi::TypeError::New(env, "Expected 2 callbacks").ThrowAsJavaScriptException();
return;
}
// Create thread-safe functions for callbacks
tsfnOutput_ = Napi::ThreadSafeFunction::New(
env,
info[0].As<Napi::Function>(),
"VideoDecoderAsyncOutput",
0, // Unlimited queue
1 // 1 initial thread
);
tsfnError_ = Napi::ThreadSafeFunction::New(
env,
info[1].As<Napi::Function>(),
"VideoDecoderAsyncError",
0,
1
);
// Don't hold the event loop open when idle; a pending flush() holds it
// via tsfnFlush_ instead, so awaited work still completes
tsfnOutput_.Unref(env);
tsfnError_.Unref(env);
// Fires on the JS thread after the worker finishes each job, so the
// in-flight ref count and event-loop ref are only touched on one thread
tsfnJobDone_ = Napi::ThreadSafeFunction::New(
env,
Napi::Function::New(env, [](const Napi::CallbackInfo&) {}),
"VideoDecoderAsyncJobDone",
0,
1
);
tsfnJobDone_.Unref(env);
}
// Hold the event loop open while jobs are in flight (JS thread only)
void VideoDecoderAsync::JobSubmitted(Napi::Env env) {
if (activeJobs_++ == 0) {
tsfnOutput_.Ref(env);
// pin the wrapper so queued job-done callbacks (which capture this)
// can never outlive the instance
Ref();
}
}
void VideoDecoderAsync::JobFinished(Napi::Env env) {
if (activeJobs_ > 0 && --activeJobs_ == 0) {
tsfnOutput_.Unref(env);
Unref();
}
}
VideoDecoderAsync::~VideoDecoderAsync() {
// Signal worker to stop
running_ = false;
queueCV_.notify_all();
// Wait for worker thread to finish
if (workerThread_.joinable()) {
workerThread_.join();
}
// Clean up FFmpeg resources
if (codecCtx_) {
avcodec_free_context(&codecCtx_);
}
// Release thread-safe functions unless env teardown already finalized them
if (!nwc_env_teardown.load()) {
if (tsfnOutput_) tsfnOutput_.Release();
if (tsfnError_) tsfnError_.Release();
if (tsfnFlush_) tsfnFlush_.Release();
if (tsfnJobDone_) tsfnJobDone_.Release();
}
}
void VideoDecoderAsync::Configure(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (!info[0].IsObject()) {
Napi::TypeError::New(env, "Config must be an object").ThrowAsJavaScriptException();
return;
}
Napi::Object config = info[0].As<Napi::Object>();
std::string codecName = config.Get("codec").As<Napi::String>().Utf8Value();
// For H.264 decoding, use the decoder not encoder
if (codecName == "libx264") {
codecName = "h264";
}
// For AV1, prefer libdav1d
if (codecName == "av1") {
codec_ = avcodec_find_decoder_by_name("libdav1d");
if (!codec_) {
codec_ = avcodec_find_decoder_by_name("libaom-av1");
}
if (!codec_) {
codec_ = avcodec_find_decoder(AV_CODEC_ID_AV1);
}
} else {
codec_ = avcodec_find_decoder_by_name(codecName.c_str());
}
if (!codec_) {
// Try by codec ID
if (codecName == "h264") {
codec_ = avcodec_find_decoder(AV_CODEC_ID_H264);
} else if (codecName == "vp8") {
codec_ = avcodec_find_decoder(AV_CODEC_ID_VP8);
} else if (codecName == "vp9") {
codec_ = avcodec_find_decoder(AV_CODEC_ID_VP9);
} else if (codecName == "hevc") {
codec_ = avcodec_find_decoder(AV_CODEC_ID_HEVC);
}
}
if (!codec_) {
Napi::Error::New(env, "Codec not found: " + codecName).ThrowAsJavaScriptException();
return;
}
codecCtx_ = avcodec_alloc_context3(codec_);
if (!codecCtx_) {
Napi::Error::New(env, "Failed to allocate codec context").ThrowAsJavaScriptException();
return;
}
// Set dimensions if provided
if (config.Has("width")) {
codecCtx_->width = config.Get("width").As<Napi::Number>().Int32Value();
}
if (config.Has("height")) {
codecCtx_->height = config.Get("height").As<Napi::Number>().Int32Value();
}
// Set extradata
if (config.Has("extradata")) {
Napi::Buffer<uint8_t> extradata = config.Get("extradata").As<Napi::Buffer<uint8_t>>();
codecCtx_->extradata_size = extradata.Length();
codecCtx_->extradata = (uint8_t*)av_malloc(extradata.Length() + AV_INPUT_BUFFER_PADDING_SIZE);
memcpy(codecCtx_->extradata, extradata.Data(), extradata.Length());
memset(codecCtx_->extradata + extradata.Length(), 0, AV_INPUT_BUFFER_PADDING_SIZE);
}
// 0 = auto-detect core count; default of 1 leaves multicore decode on the table
codecCtx_->thread_count = 0;
// Frame threading buffers ~thread_count frames before output; for
// latency-sensitive use (seeking, realtime) restrict to slice threading
if (config.Has("optimizeForLatency") &&
config.Get("optimizeForLatency").ToBoolean().Value()) {
codecCtx_->thread_type = FF_THREAD_SLICE;
codecCtx_->flags |= AV_CODEC_FLAG_LOW_DELAY;
}
// Open codec
int ret = avcodec_open2(codecCtx_, codec_, nullptr);
if (ret < 0) {
char errBuf[256];
av_strerror(ret, errBuf, sizeof(errBuf));
avcodec_free_context(&codecCtx_);
codecCtx_ = nullptr;
Napi::Error::New(env, std::string("Failed to open codec: ") + errBuf).ThrowAsJavaScriptException();
return;
}
configured_ = true;
// Start worker thread
running_ = true;
workerThread_ = std::thread(&VideoDecoderAsync::WorkerThread, this);
}
void VideoDecoderAsync::WorkerThread() {
while (running_) {
DecodeJob job;
{
std::unique_lock<std::mutex> lock(queueMutex_);
queueCV_.wait(lock, [this] {
return !jobQueue_.empty() || !running_;
});
if (!running_ && jobQueue_.empty()) {
break;
}
if (jobQueue_.empty()) {
continue;
}
job = std::move(jobQueue_.front());
jobQueue_.pop();
}
if (job.isFlush) {
ProcessFlush();
} else {
ProcessDecode(job);
}
tsfnJobDone_.NonBlockingCall([this](Napi::Env env, Napi::Function) {
JobFinished(env);
});
}
}
void VideoDecoderAsync::ProcessDecode(DecodeJob& job) {
if (!codecCtx_) {
return;
}
// Create packet
AVPacket* packet = av_packet_alloc();
packet->data = job.data.data();
packet->size = static_cast<int>(job.data.size());
packet->pts = job.timestamp;
packet->dts = job.timestamp;
packet->duration = job.duration;
if (job.isKeyframe) {
packet->flags |= AV_PKT_FLAG_KEY;
}
// Send packet to decoder
int ret = avcodec_send_packet(codecCtx_, packet);
if (ret < 0) {
char errBuf[256];
av_strerror(ret, errBuf, sizeof(errBuf));
DecodeResult result;
result.frame = nullptr;
result.isError = true;
result.errorMessage = std::string("Decode error: ") + errBuf;
tsfnError_.BlockingCall(&result, [](Napi::Env env, Napi::Function fn, DecodeResult* res) {
fn.Call({ Napi::String::New(env, res->errorMessage) });
});
av_packet_free(&packet);
return;
}
// Receive decoded frames
AVFrame* frame = av_frame_alloc();
while (ret >= 0) {
ret = avcodec_receive_frame(codecCtx_, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
} else if (ret < 0) {
char errBuf[256];
av_strerror(ret, errBuf, sizeof(errBuf));
DecodeResult result;
result.frame = nullptr;
result.isError = true;
result.errorMessage = std::string("Decode error: ") + errBuf;
tsfnError_.BlockingCall(&result, [](Napi::Env env, Napi::Function fn, DecodeResult* res) {
fn.Call({ Napi::String::New(env, res->errorMessage) });
});
break;
}
// Clone frame for output
AVFrame* outputFrame = av_frame_clone(frame);
DecodeResult* result = new DecodeResult();
result->frame = outputFrame;
result->timestamp = job.timestamp;
result->duration = job.duration;
result->isError = false;
result->isFlushComplete = false;
// Call JS callback
tsfnOutput_.BlockingCall(result, [](Napi::Env env, Napi::Function fn, DecodeResult* res) {
Napi::Object nativeFrame = VideoFrameNative::NewInstance(env, res->frame);
fn.Call({
nativeFrame,
Napi::Number::New(env, static_cast<double>(res->timestamp)),
Napi::Number::New(env, static_cast<double>(res->duration))
});
delete res;
});
av_frame_unref(frame);
}
av_frame_free(&frame);
av_packet_free(&packet);
}
void VideoDecoderAsync::ProcessFlush() {
if (!codecCtx_) {
flushPending_ = false;
return;
}
// Send NULL packet to flush
avcodec_send_packet(codecCtx_, nullptr);
AVFrame* frame = av_frame_alloc();
int ret;
while ((ret = avcodec_receive_frame(codecCtx_, frame)) >= 0) {
AVFrame* outputFrame = av_frame_clone(frame);
DecodeResult* result = new DecodeResult();
result->frame = outputFrame;
result->timestamp = frame->pts;
result->duration = NWC_FRAME_DURATION(frame);
result->isError = false;
result->isFlushComplete = false;
// Use NonBlockingCall to prevent deadlock in resource-constrained environments
// (CI, serverless, containers) where the JS event loop may be starved
tsfnOutput_.NonBlockingCall(result, [](Napi::Env env, Napi::Function fn, DecodeResult* res) {
Napi::Object nativeFrame = VideoFrameNative::NewInstance(env, res->frame);
fn.Call({
nativeFrame,
Napi::Number::New(env, static_cast<double>(res->timestamp)),
Napi::Number::New(env, static_cast<double>(res->duration))
});
delete res;
});
av_frame_unref(frame);
}
av_frame_free(&frame);
// Draining puts the codec in EOF state; per WebCodecs spec the decoder
// must accept new chunks after flush(), so reset it
avcodec_flush_buffers(codecCtx_);
// Signal flush complete using NonBlockingCall to prevent deadlock
if (tsfnFlush_) {
tsfnFlush_.NonBlockingCall([](Napi::Env env, Napi::Function fn) {
fn.Call({ env.Null() });
});
// Queued call still runs; releasing lets the event loop drain after it
tsfnFlush_.Release();
tsfnFlush_ = Napi::ThreadSafeFunction();
}
flushPending_ = false;
}
void VideoDecoderAsync::Decode(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (!configured_) {
Napi::Error::New(env, "Decoder not configured").ThrowAsJavaScriptException();
return;
}
Napi::Buffer<uint8_t> data = info[0].As<Napi::Buffer<uint8_t>>();
bool isKeyframe = info[1].As<Napi::Boolean>().Value();
int64_t timestamp = info[2].As<Napi::Number>().Int64Value();
int64_t duration = info[3].As<Napi::Number>().Int64Value();
// Copy data for async processing
DecodeJob job;
job.data.assign(data.Data(), data.Data() + data.Length());
job.isKeyframe = isKeyframe;
job.timestamp = timestamp;
job.duration = duration;
job.isFlush = false;
{
std::lock_guard<std::mutex> lock(queueMutex_);
jobQueue_.push(std::move(job));
}
queueCV_.notify_one();
JobSubmitted(env);
}
Napi::Value VideoDecoderAsync::Flush(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (!configured_) {
Napi::Function callback = info[0].As<Napi::Function>();
callback.Call({ env.Null() });
return env.Undefined();
}
// Create thread-safe function for flush callback
Napi::Function callback = info[0].As<Napi::Function>();
if (tsfnFlush_) tsfnFlush_.Release(); // stale handle from an unresolved earlier flush
tsfnFlush_ = Napi::ThreadSafeFunction::New(
env,
callback,
"VideoDecoderAsyncFlush",
0,
1
);
flushPending_ = true;
// Queue flush job
DecodeJob job;
job.isFlush = true;
{
std::lock_guard<std::mutex> lock(queueMutex_);
jobQueue_.push(std::move(job));
}
queueCV_.notify_one();
JobSubmitted(env);
return env.Undefined();
}
void VideoDecoderAsync::Reset(const Napi::CallbackInfo& info) {
// Clear queue
{
std::lock_guard<std::mutex> lock(queueMutex_);
while (!jobQueue_.empty()) {
jobQueue_.pop();
}
}
if (codecCtx_) {
avcodec_flush_buffers(codecCtx_);
}
}
void VideoDecoderAsync::Close(const Napi::CallbackInfo& info) {
// Stop worker thread
running_ = false;
queueCV_.notify_all();
if (workerThread_.joinable()) {
workerThread_.join();
}
// Clear queue
{
std::lock_guard<std::mutex> lock(queueMutex_);
while (!jobQueue_.empty()) {
jobQueue_.pop();
}
}
// Clean up FFmpeg
if (codecCtx_) {
avcodec_free_context(&codecCtx_);
codecCtx_ = nullptr;
}
// Worker is joined, so no more calls are queued; release now and null the
// handles so the destructor doesn't touch already-finalized functions
if (tsfnOutput_) { tsfnOutput_.Release(); tsfnOutput_ = Napi::ThreadSafeFunction(); }
if (tsfnError_) { tsfnError_.Release(); tsfnError_ = Napi::ThreadSafeFunction(); }
if (tsfnFlush_) { tsfnFlush_.Release(); tsfnFlush_ = Napi::ThreadSafeFunction(); }
if (tsfnJobDone_) { tsfnJobDone_.Release(); tsfnJobDone_ = Napi::ThreadSafeFunction(); }
if (activeJobs_ > 0) {
activeJobs_ = 0;
Unref(); // balance the in-flight pin; queued JobFinished sees 0 and skips
}
configured_ = false;
}