@pproenca/node-webcodecs 0.1.1-alpha.0 → 0.1.1-alpha.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/README.md +75 -233
  2. package/binding.gyp +123 -0
  3. package/dist/audio-decoder.js +1 -2
  4. package/dist/audio-encoder.d.ts +4 -0
  5. package/dist/audio-encoder.js +28 -2
  6. package/dist/binding.d.ts +0 -2
  7. package/dist/binding.js +43 -125
  8. package/dist/control-message-queue.js +0 -1
  9. package/dist/demuxer.d.ts +7 -0
  10. package/dist/demuxer.js +9 -0
  11. package/dist/encoded-chunks.d.ts +16 -0
  12. package/dist/encoded-chunks.js +82 -2
  13. package/dist/image-decoder.js +4 -0
  14. package/dist/index.d.ts +11 -0
  15. package/dist/index.js +3 -1
  16. package/dist/native-types.d.ts +20 -0
  17. package/dist/platform.d.ts +1 -10
  18. package/dist/platform.js +1 -39
  19. package/dist/resource-manager.d.ts +1 -2
  20. package/dist/resource-manager.js +3 -17
  21. package/dist/types.d.ts +12 -0
  22. package/dist/video-decoder.d.ts +21 -0
  23. package/dist/video-decoder.js +74 -2
  24. package/dist/video-encoder.d.ts +22 -0
  25. package/dist/video-encoder.js +83 -8
  26. package/lib/audio-decoder.ts +1 -2
  27. package/lib/audio-encoder.ts +31 -2
  28. package/lib/binding.ts +45 -104
  29. package/lib/control-message-queue.ts +0 -1
  30. package/lib/demuxer.ts +10 -0
  31. package/lib/encoded-chunks.ts +90 -2
  32. package/lib/image-decoder.ts +5 -0
  33. package/lib/index.ts +3 -0
  34. package/lib/native-types.ts +22 -0
  35. package/lib/platform.ts +1 -41
  36. package/lib/resource-manager.ts +3 -19
  37. package/lib/types.ts +13 -0
  38. package/lib/video-decoder.ts +84 -2
  39. package/lib/video-encoder.ts +90 -8
  40. package/package.json +49 -32
  41. package/src/addon.cc +57 -0
  42. package/src/async_decode_worker.cc +241 -33
  43. package/src/async_decode_worker.h +55 -3
  44. package/src/async_encode_worker.cc +103 -35
  45. package/src/async_encode_worker.h +23 -4
  46. package/src/audio_data.cc +38 -15
  47. package/src/audio_data.h +1 -0
  48. package/src/audio_decoder.cc +24 -3
  49. package/src/audio_encoder.cc +55 -4
  50. package/src/common.cc +125 -17
  51. package/src/common.h +34 -4
  52. package/src/demuxer.cc +16 -2
  53. package/src/encoded_audio_chunk.cc +10 -0
  54. package/src/encoded_audio_chunk.h +2 -0
  55. package/src/encoded_video_chunk.h +1 -0
  56. package/src/error_builder.cc +0 -4
  57. package/src/image_decoder.cc +127 -90
  58. package/src/image_decoder.h +11 -4
  59. package/src/muxer.cc +1 -0
  60. package/src/test_video_generator.cc +3 -2
  61. package/src/video_decoder.cc +169 -19
  62. package/src/video_decoder.h +9 -11
  63. package/src/video_encoder.cc +389 -32
  64. package/src/video_encoder.h +15 -0
  65. package/src/video_filter.cc +22 -11
  66. package/src/video_frame.cc +160 -5
  67. package/src/warnings.cc +0 -4
  68. package/dist/audio-data.js.map +0 -1
  69. package/dist/audio-decoder.js.map +0 -1
  70. package/dist/audio-encoder.js.map +0 -1
  71. package/dist/binding.js.map +0 -1
  72. package/dist/codec-base.js.map +0 -1
  73. package/dist/control-message-queue.js.map +0 -1
  74. package/dist/demuxer.js.map +0 -1
  75. package/dist/encoded-chunks.js.map +0 -1
  76. package/dist/errors.js.map +0 -1
  77. package/dist/ffmpeg.d.ts +0 -21
  78. package/dist/ffmpeg.js +0 -112
  79. package/dist/image-decoder.js.map +0 -1
  80. package/dist/image-track-list.js.map +0 -1
  81. package/dist/image-track.js.map +0 -1
  82. package/dist/index.js.map +0 -1
  83. package/dist/is.js.map +0 -1
  84. package/dist/muxer.js.map +0 -1
  85. package/dist/native-types.js.map +0 -1
  86. package/dist/platform.js.map +0 -1
  87. package/dist/resource-manager.js.map +0 -1
  88. package/dist/test-video-generator.js.map +0 -1
  89. package/dist/transfer.js.map +0 -1
  90. package/dist/types.js.map +0 -1
  91. package/dist/video-decoder.js.map +0 -1
  92. package/dist/video-encoder.js.map +0 -1
  93. package/dist/video-filter.js.map +0 -1
  94. package/dist/video-frame.js.map +0 -1
  95. package/install/build.js +0 -51
  96. package/install/check.js +0 -192
  97. package/lib/ffmpeg.ts +0 -78
@@ -12,22 +12,40 @@ extern "C" {
12
12
  #include <libswscale/swscale.h>
13
13
  }
14
14
 
15
+ #include "src/ffmpeg_raii.h"
16
+
15
17
  #include <napi.h>
16
18
 
17
19
  #include <atomic>
18
20
  #include <condition_variable>
21
+ #include <memory>
19
22
  #include <mutex>
20
23
  #include <queue>
24
+ #include <string>
21
25
  #include <thread>
22
26
  #include <vector>
23
27
 
24
28
  class VideoDecoder;
25
29
 
30
+ // Metadata config for decoded video frames (mirrors EncoderMetadataConfig pattern)
31
+ struct DecoderMetadataConfig {
32
+ int rotation = 0;
33
+ bool flip = false;
34
+ int display_width = 0;
35
+ int display_height = 0;
36
+ std::string color_primaries;
37
+ std::string color_transfer;
38
+ std::string color_matrix;
39
+ bool color_full_range = false;
40
+ bool has_color_space = false;
41
+ };
42
+
26
43
  struct DecodeTask {
27
44
  std::vector<uint8_t> data;
28
45
  int64_t timestamp;
29
46
  int64_t duration;
30
47
  bool is_key;
48
+ bool is_flush = false; // When true, flush the decoder instead of decoding
31
49
  };
32
50
 
33
51
  struct DecodedFrame {
@@ -55,8 +73,14 @@ class AsyncDecodeWorker {
55
73
  void Flush();
56
74
  void SetCodecContext(AVCodecContext* ctx, SwsContext* sws, int width,
57
75
  int height);
76
+ void SetMetadataConfig(const DecoderMetadataConfig& config);
58
77
  bool IsRunning() const { return running_.load(); }
59
78
  size_t QueueSize() const;
79
+ int GetPendingFrames() const { return pending_frames_->load(); }
80
+ // Get shared pending counter for TSFN callbacks to capture
81
+ std::shared_ptr<std::atomic<int>> GetPendingFramesPtr() const {
82
+ return pending_frames_;
83
+ }
60
84
 
61
85
  private:
62
86
  void WorkerThread();
@@ -70,16 +94,44 @@ class AsyncDecodeWorker {
70
94
  std::queue<DecodeTask> task_queue_;
71
95
  mutable std::mutex queue_mutex_; // mutable for const QueueSize()
72
96
  std::condition_variable queue_cv_;
97
+ std::mutex codec_mutex_; // Protects codec_context_, sws_context_, frame_, packet_, metadata_config_
73
98
  std::atomic<bool> running_{false};
74
99
  std::atomic<bool> flushing_{false};
100
+ std::atomic<int> processing_{0}; // Track tasks currently being processed
101
+ // DARWIN-X64 FIX: Guard against codec access during shutdown race window.
102
+ // Set to true after SetCodecContext, false at START of Stop().
103
+ // ProcessPacket checks this before accessing codec_context_.
104
+ std::atomic<bool> codec_valid_{false};
105
+ // Mutex to synchronize Stop() calls from Cleanup() and destructor
106
+ std::mutex stop_mutex_;
107
+ // Use shared_ptr for pending counter so TSFN callbacks can safely access it
108
+ // even after the worker object is destroyed. The shared_ptr is captured by
109
+ // the callback lambda, ensuring the atomic counter remains valid.
110
+ std::shared_ptr<std::atomic<int>> pending_frames_ =
111
+ std::make_shared<std::atomic<int>>(0);
75
112
 
76
113
  // FFmpeg contexts (owned by VideoDecoder, just references here)
77
114
  AVCodecContext* codec_context_;
78
- SwsContext* sws_context_;
79
- AVFrame* frame_;
80
- AVPacket* packet_;
115
+ ffmpeg::SwsContextPtr sws_context_; // RAII-managed, created lazily on first frame
116
+ ffmpeg::AVFramePtr frame_; // RAII-managed, owned by this worker
117
+ ffmpeg::AVPacketPtr packet_; // RAII-managed, owned by this worker
81
118
  int output_width_;
82
119
  int output_height_;
120
+
121
+ // Track last frame format/dimensions for sws_context recreation
122
+ AVPixelFormat last_frame_format_ = AV_PIX_FMT_NONE;
123
+ int last_frame_width_ = 0;
124
+ int last_frame_height_ = 0;
125
+
126
+ // Buffer pool for decoded frame data to reduce allocations
127
+ std::vector<std::vector<uint8_t>*> buffer_pool_;
128
+ std::mutex pool_mutex_;
129
+
130
+ // Decoder metadata for output frames
131
+ DecoderMetadataConfig metadata_config_;
132
+
133
+ std::vector<uint8_t>* AcquireBuffer(size_t size);
134
+ void ReleaseBuffer(std::vector<uint8_t>* buffer);
83
135
  };
84
136
 
85
137
  #endif // SRC_ASYNC_DECODE_WORKER_H_
@@ -5,10 +5,14 @@
5
5
 
6
6
  #include "src/async_encode_worker.h"
7
7
 
8
+ #include <chrono>
9
+ #include <cstdio>
10
+ #include <memory>
8
11
  #include <string>
9
12
  #include <utility>
10
13
  #include <vector>
11
14
 
15
+ #include "src/common.h"
12
16
  #include "src/encoded_video_chunk.h"
13
17
  #include "src/video_encoder.h"
14
18
 
@@ -44,32 +48,36 @@ AsyncEncodeWorker::AsyncEncodeWorker(VideoEncoder* /* encoder */,
44
48
 
45
49
  void AsyncEncodeWorker::SetCodecContext(AVCodecContext* ctx, SwsContext* sws,
46
50
  int width, int height) {
51
+ std::lock_guard<std::mutex> lock(codec_mutex_);
47
52
  codec_context_ = ctx;
48
53
  sws_context_ = sws;
49
54
  width_ = width;
50
55
  height_ = height;
51
- frame_ = av_frame_alloc();
56
+ frame_ = ffmpeg::make_frame();
52
57
  if (frame_) {
53
58
  frame_->format = AV_PIX_FMT_YUV420P;
54
59
  frame_->width = width;
55
60
  frame_->height = height;
56
- av_frame_get_buffer(frame_, 32);
61
+ int ret = av_frame_get_buffer(frame_.get(), 32);
62
+ if (ret < 0) {
63
+ frame_.reset(); // Clear on allocation failure
64
+ }
57
65
  }
58
- packet_ = av_packet_alloc();
66
+ packet_ = ffmpeg::make_packet();
67
+
68
+ // DARWIN-X64 FIX: Mark codec as valid only after successful initialization.
69
+ // ProcessFrame checks this flag to avoid accessing codec during shutdown.
70
+ codec_valid_.store(true, std::memory_order_release);
59
71
  }
60
72
 
61
73
  void AsyncEncodeWorker::SetMetadataConfig(const EncoderMetadataConfig& config) {
74
+ std::lock_guard<std::mutex> lock(codec_mutex_);
62
75
  metadata_config_ = config;
63
76
  }
64
77
 
65
78
  AsyncEncodeWorker::~AsyncEncodeWorker() {
66
79
  Stop();
67
- if (frame_) {
68
- av_frame_free(&frame_);
69
- }
70
- if (packet_) {
71
- av_packet_free(&packet_);
72
- }
80
+ // frame_ and packet_ are RAII-managed, automatically cleaned up
73
81
  }
74
82
 
75
83
  void AsyncEncodeWorker::Start() {
@@ -80,9 +88,26 @@ void AsyncEncodeWorker::Start() {
80
88
  }
81
89
 
82
90
  void AsyncEncodeWorker::Stop() {
91
+ // DARWIN-X64 FIX: Use stop_mutex_ to prevent double-stop race.
92
+ // Cleanup() and destructor may both call Stop().
93
+ std::lock_guard<std::mutex> stop_lock(stop_mutex_);
94
+
83
95
  if (!running_.load()) return;
84
96
 
85
- running_.store(false);
97
+ // DARWIN-X64 FIX: Invalidate codec FIRST, before signaling shutdown.
98
+ // This prevents ProcessFrame from accessing codec_context_ during the
99
+ // race window between setting running_=false and the worker thread exiting.
100
+ codec_valid_.store(false, std::memory_order_release);
101
+
102
+ {
103
+ // CRITICAL: Hold mutex while modifying condition predicate to prevent
104
+ // lost wakeup race on x86_64. Without mutex, there's a window where:
105
+ // 1. Worker checks predicate (running_==true), starts entering wait()
106
+ // 2. Main thread sets running_=false, calls notify_all()
107
+ // 3. Worker enters wait() after notification - blocked forever
108
+ std::lock_guard<std::mutex> lock(queue_mutex_);
109
+ running_.store(false, std::memory_order_release);
110
+ }
86
111
  queue_cv_.notify_all();
87
112
 
88
113
  if (worker_thread_.joinable()) {
@@ -110,11 +135,13 @@ void AsyncEncodeWorker::Flush() {
110
135
 
111
136
  flushing_.store(true);
112
137
 
113
- // Wait for queue to drain (including flush task)
138
+ // Wait for queue to drain AND all in-flight processing to complete
114
139
  {
115
140
  std::unique_lock<std::mutex> lock(queue_mutex_);
116
- queue_cv_.wait(lock,
117
- [this] { return task_queue_.empty() || !running_.load(); });
141
+ queue_cv_.wait(lock, [this] {
142
+ return (task_queue_.empty() && processing_.load() == 0) ||
143
+ !running_.load();
144
+ });
118
145
  }
119
146
 
120
147
  flushing_.store(false);
@@ -145,28 +172,46 @@ void AsyncEncodeWorker::WorkerThread() {
145
172
 
146
173
  task = std::move(task_queue_.front());
147
174
  task_queue_.pop();
175
+ processing_++; // Track that we're processing this task
148
176
  }
149
177
 
150
178
  ProcessFrame(task);
151
179
 
152
- if (task_queue_.empty()) {
153
- queue_cv_.notify_all();
180
+ // Decrement counter and notify under lock (fixes race condition).
181
+ {
182
+ std::lock_guard<std::mutex> lock(queue_mutex_);
183
+ processing_--;
184
+ if (task_queue_.empty() && processing_.load() == 0) {
185
+ queue_cv_.notify_all();
186
+ }
154
187
  }
155
188
  }
156
189
  }
157
190
 
158
191
  void AsyncEncodeWorker::ProcessFrame(const EncodeTask& task) {
192
+ // DARWIN-X64 FIX: Check codec_valid_ BEFORE acquiring mutex.
193
+ // During shutdown, Stop() sets codec_valid_=false before running_=false.
194
+ // This creates a window where the worker thread could still be running
195
+ // but the codec is being destroyed. Early exit prevents the race.
196
+ if (!codec_valid_.load(std::memory_order_acquire)) {
197
+ return;
198
+ }
199
+
200
+ std::lock_guard<std::mutex> lock(codec_mutex_);
159
201
  if (!codec_context_ || !sws_context_ || !frame_ || !packet_) {
160
202
  return;
161
203
  }
162
204
 
163
- // Handle flush task - send NULL frame to drain encoder
205
+ // Handle flush task - send NULL frame to drain encoder.
206
+ // Note: After this, the codec enters EOF mode and won't accept new frames.
207
+ // The VideoEncoder::Flush() method handles codec reinitialization after
208
+ // the worker drains to allow continued encoding per W3C WebCodecs spec.
164
209
  if (task.is_flush) {
165
210
  avcodec_send_frame(codec_context_, nullptr);
166
211
  // Drain all remaining packets
167
- while (avcodec_receive_packet(codec_context_, packet_) == 0) {
168
- EmitChunk(packet_);
169
- av_packet_unref(packet_);
212
+ while (avcodec_receive_packet(codec_context_, packet_.get()) == 0) {
213
+ EmitChunk(packet_.get());
214
+ av_packet_unref(packet_.get());
170
215
  }
171
216
  // Clear frame info map after flush
172
217
  frame_info_.clear();
@@ -193,21 +238,26 @@ void AsyncEncodeWorker::ProcessFrame(const EncodeTask& task) {
193
238
  frame_->quality = 0; // Let encoder decide
194
239
  }
195
240
 
196
- int ret = avcodec_send_frame(codec_context_, frame_);
241
+ int ret = avcodec_send_frame(codec_context_, frame_.get());
197
242
  if (ret < 0 && ret != AVERROR(EAGAIN)) {
198
243
  std::string error_msg = "Encode error: " + std::to_string(ret);
199
244
  error_tsfn_.NonBlockingCall(
200
245
  new std::string(error_msg),
201
246
  [](Napi::Env env, Napi::Function fn, std::string* msg) {
247
+ // If env is null, TSFN is closing during teardown. Just cleanup.
248
+ if (env == nullptr) {
249
+ delete msg;
250
+ return;
251
+ }
202
252
  fn.Call({Napi::Error::New(env, *msg).Value()});
203
253
  delete msg;
204
254
  });
205
255
  return;
206
256
  }
207
257
 
208
- while (avcodec_receive_packet(codec_context_, packet_) == 0) {
209
- EmitChunk(packet_);
210
- av_packet_unref(packet_);
258
+ while (avcodec_receive_packet(codec_context_, packet_.get()) == 0) {
259
+ EmitChunk(packet_.get());
260
+ av_packet_unref(packet_.get());
211
261
  }
212
262
  }
213
263
 
@@ -220,12 +270,14 @@ struct ChunkCallbackData {
220
270
  int64_t frame_index; // For SVC layer computation
221
271
  EncoderMetadataConfig metadata;
222
272
  std::vector<uint8_t> extradata; // Copy from codec_context at emit time
223
- std::atomic<int>* pending;
273
+ // Use shared_ptr to pending counter so it remains valid even if worker is
274
+ // destroyed before callback executes on main thread.
275
+ std::shared_ptr<std::atomic<int>> pending;
224
276
  };
225
277
 
226
278
  void AsyncEncodeWorker::EmitChunk(AVPacket* pkt) {
227
279
  // Increment pending count before async operation
228
- pending_chunks_.fetch_add(1);
280
+ pending_chunks_->fetch_add(1);
229
281
 
230
282
  // pkt->pts is the frame_index (set in ProcessFrame)
231
283
  int64_t frame_index = pkt->pts;
@@ -237,6 +289,7 @@ void AsyncEncodeWorker::EmitChunk(AVPacket* pkt) {
237
289
  if (it != frame_info_.end()) {
238
290
  timestamp = it->second.first;
239
291
  duration = it->second.second;
292
+ frame_info_.erase(it); // Clean up after use
240
293
  }
241
294
 
242
295
  // Create callback data with all info needed on main thread
@@ -254,17 +307,32 @@ void AsyncEncodeWorker::EmitChunk(AVPacket* pkt) {
254
307
  codec_context_->extradata,
255
308
  codec_context_->extradata + codec_context_->extradata_size);
256
309
  }
257
- cb_data->pending = &pending_chunks_;
310
+ cb_data->pending = pending_chunks_;
258
311
 
259
312
  output_tsfn_.NonBlockingCall(cb_data, [](Napi::Env env, Napi::Function fn,
260
313
  ChunkCallbackData* info) {
261
- // Create EncodedVideoChunk-like object (matches synchronous path)
262
- Napi::Object chunk = Napi::Object::New(env);
263
- chunk.Set("type", info->is_key ? "key" : "delta");
264
- chunk.Set("timestamp", Napi::Number::New(env, info->pts));
265
- chunk.Set("duration", Napi::Number::New(env, info->duration));
266
- chunk.Set("data", Napi::Buffer<uint8_t>::Copy(env, info->data.data(),
267
- info->data.size()));
314
+ // CRITICAL: If env is null, the TSFN is being destroyed (environment teardown).
315
+ // Must still clean up data and counters, then return to avoid crashing.
316
+ // NOTE: Do NOT access static variables (like counterQueue) here - they may
317
+ // already be destroyed due to static destruction order during process exit.
318
+ if (env == nullptr) {
319
+ info->pending->fetch_sub(1);
320
+ // Skip counterQueue-- : static may be destroyed during process exit
321
+ delete info;
322
+ return;
323
+ }
324
+
325
+ // Decrement pending count before any operations
326
+ info->pending->fetch_sub(1);
327
+ webcodecs::counterQueue--;
328
+
329
+ // Create native EncodedVideoChunk directly to avoid double-copy.
330
+ // The data is copied once into the chunk's internal buffer.
331
+ // Previously we created a plain JS object here, which the TS layer
332
+ // would wrap in a new EncodedVideoChunk, causing a second copy.
333
+ Napi::Object chunk = EncodedVideoChunk::CreateInstance(
334
+ env, info->is_key ? "key" : "delta", info->pts, info->duration,
335
+ info->data.data(), info->data.size());
268
336
 
269
337
  // Create metadata object matching sync path
270
338
  Napi::Object metadata = Napi::Object::New(env);
@@ -320,8 +388,8 @@ void AsyncEncodeWorker::EmitChunk(AVPacket* pkt) {
320
388
 
321
389
  fn.Call({chunk, metadata});
322
390
 
323
- // Decrement pending count after callback completes
324
- info->pending->fetch_sub(1);
391
+ // ChunkCallbackData is no longer tied to the buffer lifetime.
392
+ // Delete it now that the data has been copied into the EncodedVideoChunk.
325
393
  delete info;
326
394
  });
327
395
  }
@@ -12,11 +12,14 @@ extern "C" {
12
12
  #include <libswscale/swscale.h>
13
13
  }
14
14
 
15
+ #include "src/ffmpeg_raii.h"
16
+
15
17
  #include <napi.h>
16
18
 
17
19
  #include <atomic>
18
20
  #include <condition_variable>
19
21
  #include <map>
22
+ #include <memory>
20
23
  #include <mutex>
21
24
  #include <queue>
22
25
  #include <string>
@@ -78,7 +81,11 @@ class AsyncEncodeWorker {
78
81
  void Flush();
79
82
  bool IsRunning() const { return running_.load(); }
80
83
  size_t QueueSize() const;
81
- int GetPendingChunks() const { return pending_chunks_.load(); }
84
+ int GetPendingChunks() const { return pending_chunks_->load(); }
85
+ // Get shared pending counter for TSFN callbacks to capture
86
+ std::shared_ptr<std::atomic<int>> GetPendingChunksPtr() const {
87
+ return pending_chunks_;
88
+ }
82
89
  void SetCodecContext(AVCodecContext* ctx, SwsContext* sws, int width,
83
90
  int height);
84
91
  void SetMetadataConfig(const EncoderMetadataConfig& config);
@@ -94,16 +101,28 @@ class AsyncEncodeWorker {
94
101
  std::thread worker_thread_;
95
102
  std::queue<EncodeTask> task_queue_;
96
103
  mutable std::mutex queue_mutex_; // mutable for const QueueSize()
104
+ std::mutex codec_mutex_; // Protects codec_context_, sws_context_, frame_, packet_, metadata_config_
97
105
  std::condition_variable queue_cv_;
98
106
  std::atomic<bool> running_{false};
99
107
  std::atomic<bool> flushing_{false};
100
- std::atomic<int> pending_chunks_{0};
108
+ std::atomic<int> processing_{0}; // Track tasks currently being processed
109
+ // DARWIN-X64 FIX: Guard against codec access during shutdown race window.
110
+ // Set to true after SetCodecContext, false at START of Stop().
111
+ // ProcessFrame checks this before accessing codec_context_.
112
+ std::atomic<bool> codec_valid_{false};
113
+ // Mutex to synchronize Stop() calls from Cleanup() and destructor
114
+ std::mutex stop_mutex_;
115
+ // Use shared_ptr for pending counter so TSFN callbacks can safely access it
116
+ // even after the worker object is destroyed. The shared_ptr is captured by
117
+ // the callback lambda, ensuring the atomic counter remains valid.
118
+ std::shared_ptr<std::atomic<int>> pending_chunks_ =
119
+ std::make_shared<std::atomic<int>>(0);
101
120
 
102
121
  // FFmpeg contexts (owned by VideoEncoder, just references here)
103
122
  AVCodecContext* codec_context_;
104
123
  SwsContext* sws_context_;
105
- AVFrame* frame_;
106
- AVPacket* packet_;
124
+ ffmpeg::AVFramePtr frame_; // RAII-managed, owned by this worker
125
+ ffmpeg::AVPacketPtr packet_; // RAII-managed, owned by this worker
107
126
  int width_;
108
127
  int height_;
109
128
 
package/src/audio_data.cc CHANGED
@@ -8,6 +8,7 @@
8
8
  #include <vector>
9
9
 
10
10
  #include "src/common.h"
11
+ #include "src/ffmpeg_raii.h"
11
12
 
12
13
  extern "C" {
13
14
  #include <libavutil/channel_layout.h>
@@ -98,6 +99,7 @@ AudioData::AudioData(const Napi::CallbackInfo& info)
98
99
  number_of_channels_(0),
99
100
  timestamp_(0),
100
101
  closed_(false) {
102
+ webcodecs::counterAudioData++;
101
103
  Napi::Env env = info.Env();
102
104
 
103
105
  if (info.Length() < 1 || !info[0].IsObject()) {
@@ -197,6 +199,26 @@ AudioData::AudioData(const Napi::CallbackInfo& info)
197
199
  .ThrowAsJavaScriptException();
198
200
  return;
199
201
  }
202
+
203
+ // Inform V8 of external memory allocation for GC pressure calculation.
204
+ Napi::MemoryManagement::AdjustExternalMemory(
205
+ env, static_cast<int64_t>(data_.size()));
206
+ }
207
+
208
+ AudioData::~AudioData() {
209
+ webcodecs::counterAudioData--;
210
+ // Note: We intentionally DO NOT call AdjustExternalMemory here.
211
+ //
212
+ // Calling NAPI functions (including AdjustExternalMemory) from destructors
213
+ // during V8 shutdown is unsafe and causes crashes on Node.js 24+ due to
214
+ // race conditions with V8's ArrayBufferSweeper during Heap::TearDown().
215
+ // See: https://github.com/nodejs/node-addon-api/issues/1153
216
+ //
217
+ // The WebCodecs spec mandates that close() must be called for proper
218
+ // resource management. External memory tracking is handled exclusively
219
+ // in Close() to avoid shutdown crashes.
220
+ data_.clear();
221
+ data_.shrink_to_fit();
200
222
  }
201
223
 
202
224
  size_t AudioData::GetBytesPerSample() const {
@@ -503,8 +525,8 @@ void AudioData::CopyTo(const Napi::CallbackInfo& info) {
503
525
  return;
504
526
  }
505
527
 
506
- // Create resampler context.
507
- SwrContext* swr = swr_alloc();
528
+ // Create resampler context (RAII managed).
529
+ ffmpeg::SwrContextPtr swr(swr_alloc());
508
530
  if (!swr) {
509
531
  Napi::Error::New(env, "Failed to allocate SwrContext")
510
532
  .ThrowAsJavaScriptException();
@@ -517,18 +539,17 @@ void AudioData::CopyTo(const Napi::CallbackInfo& info) {
517
539
  av_channel_layout_default(&ch_layout, number_of_channels_);
518
540
 
519
541
  // Set input parameters.
520
- av_opt_set_chlayout(swr, "in_chlayout", &ch_layout, 0);
521
- av_opt_set_int(swr, "in_sample_rate", sample_rate_, 0);
522
- av_opt_set_sample_fmt(swr, "in_sample_fmt", src_fmt, 0);
542
+ av_opt_set_chlayout(swr.get(), "in_chlayout", &ch_layout, 0);
543
+ av_opt_set_int(swr.get(), "in_sample_rate", sample_rate_, 0);
544
+ av_opt_set_sample_fmt(swr.get(), "in_sample_fmt", src_fmt, 0);
523
545
 
524
546
  // Set output parameters.
525
- av_opt_set_chlayout(swr, "out_chlayout", &ch_layout, 0);
526
- av_opt_set_int(swr, "out_sample_rate", sample_rate_, 0);
527
- av_opt_set_sample_fmt(swr, "out_sample_fmt", dst_fmt, 0);
547
+ av_opt_set_chlayout(swr.get(), "out_chlayout", &ch_layout, 0);
548
+ av_opt_set_int(swr.get(), "out_sample_rate", sample_rate_, 0);
549
+ av_opt_set_sample_fmt(swr.get(), "out_sample_fmt", dst_fmt, 0);
528
550
 
529
- int ret = swr_init(swr);
551
+ int ret = swr_init(swr.get());
530
552
  if (ret < 0) {
531
- swr_free(&swr);
532
553
  av_channel_layout_uninit(&ch_layout);
533
554
  Napi::Error::New(env, "Failed to initialize SwrContext")
534
555
  .ThrowAsJavaScriptException();
@@ -566,9 +587,8 @@ void AudioData::CopyTo(const Napi::CallbackInfo& info) {
566
587
  temp_buffer.data() + c * frame_count * target_bytes_per_sample;
567
588
  }
568
589
 
569
- ret = swr_convert(swr, dst_data, frame_count, src_data, frame_count);
590
+ ret = swr_convert(swr.get(), dst_data, frame_count, src_data, frame_count);
570
591
  if (ret < 0) {
571
- swr_free(&swr);
572
592
  av_channel_layout_uninit(&ch_layout);
573
593
  Napi::Error::New(env, "swr_convert failed").ThrowAsJavaScriptException();
574
594
  return;
@@ -581,16 +601,15 @@ void AudioData::CopyTo(const Napi::CallbackInfo& info) {
581
601
  // Interleaved output: write directly to destination.
582
602
  dst_data[0] = dest_data;
583
603
 
584
- ret = swr_convert(swr, dst_data, frame_count, src_data, frame_count);
604
+ ret = swr_convert(swr.get(), dst_data, frame_count, src_data, frame_count);
585
605
  if (ret < 0) {
586
- swr_free(&swr);
587
606
  av_channel_layout_uninit(&ch_layout);
588
607
  Napi::Error::New(env, "swr_convert failed").ThrowAsJavaScriptException();
589
608
  return;
590
609
  }
591
610
  }
592
611
 
593
- swr_free(&swr);
612
+ // RAII handles swr cleanup
594
613
  av_channel_layout_uninit(&ch_layout);
595
614
  }
596
615
 
@@ -610,6 +629,10 @@ Napi::Value AudioData::Clone(const Napi::CallbackInfo& info) {
610
629
 
611
630
  void AudioData::Close(const Napi::CallbackInfo& info) {
612
631
  if (!closed_) {
632
+ if (!data_.empty()) {
633
+ Napi::MemoryManagement::AdjustExternalMemory(
634
+ info.Env(), -static_cast<int64_t>(data_.size()));
635
+ }
613
636
  data_.clear();
614
637
  data_.shrink_to_fit();
615
638
  closed_ = true;
package/src/audio_data.h CHANGED
@@ -20,6 +20,7 @@ class AudioData : public Napi::ObjectWrap<AudioData> {
20
20
  int64_t timestamp, const uint8_t* data,
21
21
  size_t data_size);
22
22
  explicit AudioData(const Napi::CallbackInfo& info);
23
+ ~AudioData();
23
24
 
24
25
  // Prevent copy and assignment.
25
26
  AudioData(const AudioData&) = delete;
@@ -50,6 +50,8 @@ AudioDecoder::AudioDecoder(const Napi::CallbackInfo& info)
50
50
  state_("unconfigured"),
51
51
  sample_rate_(0),
52
52
  number_of_channels_(0) {
53
+ // Track active decoder instance
54
+ webcodecs::counterAudioDecoders++;
53
55
  Napi::Env env = info.Env();
54
56
 
55
57
  if (info.Length() < 1 || !info[0].IsObject()) {
@@ -75,9 +77,29 @@ AudioDecoder::AudioDecoder(const Napi::CallbackInfo& info)
75
77
  error_callback_ = Napi::Persistent(init.Get("error").As<Napi::Function>());
76
78
  }
77
79
 
78
- AudioDecoder::~AudioDecoder() { Cleanup(); }
80
+ AudioDecoder::~AudioDecoder() {
81
+ // CRITICAL: Call Cleanup() first to ensure codec context is properly
82
+ // flushed before any further cleanup.
83
+ Cleanup();
84
+
85
+ // Now safe to disable FFmpeg logging.
86
+ webcodecs::ShutdownFFmpegLogging();
87
+
88
+ webcodecs::counterAudioDecoders--;
89
+ }
79
90
 
80
91
  void AudioDecoder::Cleanup() {
92
+ // DARWIN-X64 FIX: Flush codec internal buffers BEFORE destroying resources.
93
+ // Audio decoders may have internal queued frames. Flushing ensures they're
94
+ // drained before context destruction, preventing use-after-free.
95
+ // CRITICAL: Only flush if codec was successfully opened. avcodec_flush_buffers
96
+ // crashes on an unopened codec context (the internal codec pointer is NULL).
97
+ // NOTE: Order matters - flush must happen before resetting frame_/packet_/swr_
98
+ // to match VideoDecoder pattern and ensure codec internal state is consistent.
99
+ if (codec_context_ && avcodec_is_open(codec_context_.get())) {
100
+ avcodec_flush_buffers(codec_context_.get());
101
+ }
102
+
81
103
  frame_.reset();
82
104
  packet_.reset();
83
105
  swr_context_.reset();
@@ -236,9 +258,8 @@ void AudioDecoder::Close(const Napi::CallbackInfo& info) {
236
258
  Napi::Value AudioDecoder::Reset(const Napi::CallbackInfo& info) {
237
259
  Napi::Env env = info.Env();
238
260
 
261
+ // W3C spec: reset() is a no-op when closed (don't throw)
239
262
  if (state_ == "closed") {
240
- Napi::Error::New(env, "InvalidStateError: Cannot reset closed decoder")
241
- .ThrowAsJavaScriptException();
242
263
  return env.Undefined();
243
264
  }
244
265