#ifndef ASYNC_DECODER_H #define ASYNC_DECODER_H #include #include #include #include #include #include extern "C" { #include #include #include } // Job to be processed by worker thread struct DecodeJob { std::vector 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 { 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 running_{false}; std::atomic configured_{false}; // Job queue with synchronization std::queue jobQueue_; std::mutex queueMutex_; std::condition_variable queueCV_; // Flush synchronization std::atomic flushPending_{false}; // FFmpeg context (owned/accessed by worker thread after configure) AVCodecContext* codecCtx_; const AVCodec* codec_; }; #endif // ASYNC_DECODER_H