Skip to content

Commit e5f9a2b

Browse files
committed
docs: archive historical plan documents from merged branches
1 parent 135a265 commit e5f9a2b

2 files changed

Lines changed: 968 additions & 0 deletions

File tree

Lines changed: 336 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,336 @@
1+
# Code Review Fixes Implementation Plan
2+
3+
> **Execution:** Use `/dev-workflow:execute-plan docs/plans/2025-01-01-code-review-fixes.md` to implement task-by-task.
4+
5+
**Goal:** Address 4 issues identified in code review: extradata padding, error checking, unused utilities, and code duplication.
6+
7+
**Architecture:** Minimal changes following existing codebase patterns. Each fix is isolated and can be verified independently.
8+
9+
**Tech Stack:** C++17, FFmpeg API, Node.js N-API
10+
11+
---
12+
13+
## Task Groups
14+
15+
| Task Group | Tasks | Rationale |
16+
|------------|-------|-----------|
17+
| Group 1 | 1, 2, 3 | Independent fixes in different files, no overlap |
18+
| Group 2 | 4 | Code review (depends on all fixes) |
19+
20+
---
21+
22+
### Task 1: Fix muxer extradata padding
23+
24+
**Files:**
25+
- Modify: `src/muxer.cc:160-168, 217-227`
26+
27+
**Step 1: Review correct pattern** (1 min)
28+
29+
The fix uses `av_mallocz()` instead of `av_malloc()` to zero-initialize padding bytes. This is the standard FFmpeg pattern for extradata allocation.
30+
31+
**Step 2: Fix AddVideoTrack extradata allocation** (2 min)
32+
33+
In `src/muxer.cc`, change line 160 from `av_malloc` to `av_mallocz`:
34+
35+
```cpp
36+
// Before (line 160):
37+
stream->codecpar->extradata =
38+
static_cast<uint8_t*>(av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE));
39+
40+
// After:
41+
stream->codecpar->extradata =
42+
static_cast<uint8_t*>(av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE));
43+
```
44+
45+
**Step 3: Fix AddAudioTrack extradata allocation** (2 min)
46+
47+
In `src/muxer.cc`, change line 217 from `av_malloc` to `av_mallocz`:
48+
49+
```cpp
50+
// Before (line 217):
51+
stream->codecpar->extradata =
52+
static_cast<uint8_t*>(av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE));
53+
54+
// After:
55+
stream->codecpar->extradata =
56+
static_cast<uint8_t*>(av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE));
57+
```
58+
59+
**Step 4: Build and verify** (1 min)
60+
61+
```bash
62+
npm run build:native
63+
```
64+
65+
Expected: Build succeeds with no errors.
66+
67+
**Step 5: Run muxer tests** (30 sec)
68+
69+
```bash
70+
npx vitest run test/golden/muxer-integration.test.ts -v
71+
```
72+
73+
Expected: All tests pass.
74+
75+
**Step 6: Commit** (30 sec)
76+
77+
```bash
78+
git add src/muxer.cc && git commit -m "fix(muxer): zero-initialize extradata padding bytes
79+
80+
Use av_mallocz() instead of av_malloc() to ensure AV_INPUT_BUFFER_PADDING_SIZE
81+
bytes are zeroed as required by FFmpeg API."
82+
```
83+
84+
---
85+
86+
### Task 2: Add av_frame_get_buffer error check in video_encoder
87+
88+
**Files:**
89+
- Modify: `src/video_encoder.cc:350-355`
90+
91+
**Step 1: Review correct pattern from audio_encoder** (1 min)
92+
93+
Reference implementation at `src/audio_encoder.cc:253-257`:
94+
```cpp
95+
ret = av_frame_get_buffer(frame_.get(), 0);
96+
if (ret < 0) {
97+
Cleanup();
98+
throw Napi::Error::New(env, "Could not allocate frame buffer");
99+
}
100+
```
101+
102+
**Step 2: Add error check to video_encoder** (3 min)
103+
104+
In `src/video_encoder.cc`, locate the `Configure` method around line 350. Replace the unchecked call:
105+
106+
```cpp
107+
// Before (line 352):
108+
av_frame_get_buffer(frame_.get(), kFrameBufferAlignment);
109+
110+
// After:
111+
int frame_ret = av_frame_get_buffer(frame_.get(), kFrameBufferAlignment);
112+
if (frame_ret < 0) {
113+
Cleanup();
114+
char errbuf[AV_ERROR_MAX_STRING_SIZE];
115+
av_strerror(frame_ret, errbuf, sizeof(errbuf));
116+
throw Napi::Error::New(env, std::string("Could not allocate video frame buffer: ") + errbuf);
117+
}
118+
```
119+
120+
**Step 3: Build and verify** (1 min)
121+
122+
```bash
123+
npm run build:native
124+
```
125+
126+
Expected: Build succeeds with no errors.
127+
128+
**Step 4: Run encoder tests** (30 sec)
129+
130+
```bash
131+
npx vitest run test/golden/video-encoder.test.ts -v
132+
```
133+
134+
Expected: All tests pass.
135+
136+
**Step 5: Commit** (30 sec)
137+
138+
```bash
139+
git add src/video_encoder.cc && git commit -m "fix(video-encoder): check av_frame_get_buffer return value
140+
141+
Add error handling for frame buffer allocation failure, matching the
142+
pattern used in audio_encoder.cc."
143+
```
144+
145+
---
146+
147+
### Task 3: Remove unused utilities from common.cc
148+
149+
**Files:**
150+
- Modify: `src/common.cc:21-26, 347-350`
151+
- Modify: `src/common.h` (remove declarations)
152+
153+
**Step 1: Remove FreeCallback from common.cc** (2 min)
154+
155+
In `src/common.cc`, delete lines 21-26:
156+
157+
```cpp
158+
// DELETE these lines:
159+
// FreeCallback for consistent buffer deallocation (following sharp pattern).
160+
// Default implementation uses delete[]. Can be overridden for platform-specific
161+
// memory management (e.g., Windows mixed runtime scenarios).
162+
std::function<void(void*, uint8_t*)> FreeCallback = [](void*, uint8_t* data) {
163+
delete[] data;
164+
};
165+
```
166+
167+
**Step 2: Remove TrimEnd from common.cc** (2 min)
168+
169+
In `src/common.cc`, delete lines 347-350:
170+
171+
```cpp
172+
// DELETE these lines:
173+
std::string TrimEnd(const std::string& str) {
174+
size_t end = str.find_last_not_of(" \t\n\r\f\v");
175+
return (end == std::string::npos) ? "" : str.substr(0, end + 1);
176+
}
177+
```
178+
179+
**Step 3: Remove declarations from common.h** (2 min)
180+
181+
In `src/common.h`, find and delete the declarations for both utilities:
182+
183+
```cpp
184+
// DELETE FreeCallback declaration (around line 121-127):
185+
// FreeCallback for consistent buffer deallocation.
186+
extern std::function<void(void*, uint8_t*)> FreeCallback;
187+
188+
// DELETE TrimEnd declaration (around line 133):
189+
std::string TrimEnd(const std::string& str);
190+
```
191+
192+
**Step 4: Verify no usages exist** (30 sec)
193+
194+
```bash
195+
grep -r "FreeCallback\|TrimEnd" src/ --include="*.cc" --include="*.h" | grep -v "^Binary"
196+
```
197+
198+
Expected: No matches (confirming these are unused).
199+
200+
**Step 5: Build and verify** (1 min)
201+
202+
```bash
203+
npm run build:native
204+
```
205+
206+
Expected: Build succeeds with no errors.
207+
208+
**Step 6: Run all tests** (1 min)
209+
210+
```bash
211+
npm run test-fast
212+
```
213+
214+
Expected: All tests pass.
215+
216+
**Step 7: Commit** (30 sec)
217+
218+
```bash
219+
git add src/common.cc src/common.h && git commit -m "refactor(common): remove unused FreeCallback and TrimEnd utilities
220+
221+
These utilities were added following Sharp patterns but were never used.
222+
Removing to follow YAGNI principle."
223+
```
224+
225+
---
226+
227+
### Task 4: Extract ComputeTemporalLayerId to common.h
228+
229+
**Files:**
230+
- Modify: `src/common.h` (add declaration)
231+
- Modify: `src/common.cc` (add implementation)
232+
- Modify: `src/video_encoder.cc:21-34` (remove duplicate, add include if needed)
233+
- Modify: `src/async_encode_worker.cc:21-34` (remove duplicate, add include if needed)
234+
235+
**Step 1: Add declaration to common.h** (2 min)
236+
237+
In `src/common.h`, add near other utility function declarations:
238+
239+
```cpp
240+
// Compute temporal layer ID for SVC encoding patterns.
241+
// L1T2: alternating pattern [0, 1, 0, 1, ...]
242+
// L1T3: pyramid pattern [0, 2, 1, 2, 0, 2, 1, 2, ...]
243+
int ComputeTemporalLayerId(int64_t frame_index, int temporal_layer_count);
244+
```
245+
246+
**Step 2: Add implementation to common.cc** (3 min)
247+
248+
In `src/common.cc`, add at the end (before closing namespace if any):
249+
250+
```cpp
251+
int ComputeTemporalLayerId(int64_t frame_index, int temporal_layer_count) {
252+
if (temporal_layer_count <= 1) return 0;
253+
254+
if (temporal_layer_count == 2) {
255+
// L1T2: alternating pattern [0, 1, 0, 1, ...]
256+
return (frame_index % 2 == 0) ? 0 : 1;
257+
}
258+
259+
// L1T3: pyramid pattern [0, 2, 1, 2, 0, 2, 1, 2, ...]
260+
int pos = frame_index % 4;
261+
if (pos == 0) return 0; // Base layer
262+
if (pos == 2) return 1; // Middle layer
263+
return 2; // Enhancement layer (pos 1, 3)
264+
}
265+
```
266+
267+
**Step 3: Remove duplicate from video_encoder.cc** (2 min)
268+
269+
In `src/video_encoder.cc`, delete lines 21-34 (the `ComputeTemporalLayerId` function). Verify `#include "common.h"` exists at the top of the file.
270+
271+
**Step 4: Remove duplicate from async_encode_worker.cc** (2 min)
272+
273+
In `src/async_encode_worker.cc`, delete lines 21-34 (the `ComputeTemporalLayerId` function and comment). Verify `#include "common.h"` exists at the top of the file.
274+
275+
**Step 5: Build and verify** (1 min)
276+
277+
```bash
278+
npm run build:native
279+
```
280+
281+
Expected: Build succeeds with no errors.
282+
283+
**Step 6: Run encoder tests** (30 sec)
284+
285+
```bash
286+
npx vitest run test/golden/video-encoder.test.ts test/golden/video-encoder-async.test.ts -v
287+
```
288+
289+
Expected: All tests pass.
290+
291+
**Step 7: Commit** (30 sec)
292+
293+
```bash
294+
git add src/common.h src/common.cc src/video_encoder.cc src/async_encode_worker.cc && git commit -m "refactor(encoder): extract ComputeTemporalLayerId to common.h
295+
296+
Move duplicated function to shared utility header following DRY principle.
297+
Function is used by both video_encoder.cc and async_encode_worker.cc for
298+
SVC temporal layer calculations."
299+
```
300+
301+
---
302+
303+
### Task 5: Code Review
304+
305+
**Step 1: Run full test suite** (2 min)
306+
307+
```bash
308+
npm test
309+
```
310+
311+
Expected: All tests pass.
312+
313+
**Step 2: Run linters** (1 min)
314+
315+
```bash
316+
npm run lint
317+
```
318+
319+
Expected: No lint errors.
320+
321+
**Step 3: Review changes** (2 min)
322+
323+
```bash
324+
git log --oneline -4
325+
git diff HEAD~4 --stat
326+
```
327+
328+
Verify 4 commits were made with proper messages.
329+
330+
**Step 4: Mark complete** (30 sec)
331+
332+
All code review issues have been addressed:
333+
1. Muxer extradata padding - Fixed with av_mallocz
334+
2. av_frame_get_buffer error check - Added error handling
335+
3. Unused utilities - Removed FreeCallback and TrimEnd
336+
4. Code duplication - Extracted ComputeTemporalLayerId to common.h

0 commit comments

Comments
 (0)