Repository navigation
Expand file tree
/
Copy pathasync_decoder.h
More file actions
87 lines (71 loc) · 2.23 KB
/
Copy pathasync_decoder.h
File metadata and controls
87 lines (71 loc) · 2.23 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
#ifndef ASYNC_DECODER_H
#define ASYNC_DECODER_H
#include <napi.h>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <thread>
#include <atomic>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavutil/frame.h>
#include <libavutil/imgutils.h>
}
// Job to be processed by worker thread
struct DecodeJob {
std::vector<uint8_t> data;
bool isKeyframe;
int64_t timestamp;
int64_t duration;
bool isFlush;
};
// Result from worker thread back to JS
struct DecodeResult {
AVFrame* frame; // Ownership transferred to callback
int64_t timestamp;
int64_t duration;
bool isError;
std::string errorMessage;
bool isFlushComplete;
};
class VideoDecoderAsync : public Napi::ObjectWrap<VideoDecoderAsync> {
public:
static Napi::Object Init(Napi::Env env, Napi::Object exports);
VideoDecoderAsync(const Napi::CallbackInfo& info);
~VideoDecoderAsync();
private:
static Napi::FunctionReference constructor;
// JavaScript-facing methods
void Configure(const Napi::CallbackInfo& info);
void Decode(const Napi::CallbackInfo& info);
Napi::Value Flush(const Napi::CallbackInfo& info);
void Reset(const Napi::CallbackInfo& info);
void Close(const Napi::CallbackInfo& info);
// Worker thread entry point
void WorkerThread();
// Process a single decode job (runs on worker thread)
void ProcessDecode(DecodeJob& job);
void ProcessFlush();
// Thread-safe functions for callbacks to JS
Napi::ThreadSafeFunction tsfnOutput_;
Napi::ThreadSafeFunction tsfnError_;
Napi::ThreadSafeFunction tsfnFlush_;
// Worker thread
std::thread workerThread_;
Napi::ThreadSafeFunction tsfnJobDone_;
int activeJobs_ = 0;
void JobSubmitted(Napi::Env env);
void JobFinished(Napi::Env env);
std::atomic<bool> running_{false};
std::atomic<bool> configured_{false};
// Job queue with synchronization
std::queue<DecodeJob> jobQueue_;
std::mutex queueMutex_;
std::condition_variable queueCV_;
// Flush synchronization
std::atomic<bool> flushPending_{false};
// FFmpeg context (owned/accessed by worker thread after configure)
AVCodecContext* codecCtx_;
const AVCodec* codec_;
};
#endif // ASYNC_DECODER_H