fix: ensure last audio chunk is sent before stopping MediaRecorder #3
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "fix/last-chunk-not-sent"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Fixes #N/A - The last audio chunk was not being sent to the server before the MediaRecorder stopped, resulting in incomplete transcriptions.
Problem
When stopRecording() was called, the ondataavailable handler was set AFTER calling requestData(). If the browser fires ondataavailable synchronously, the handler missed the final chunk.
Solution
Chrome-based browsers omit the WebM EBML header (1a45dfa3) in chunks after the first timeslice. Only raw Opus audio frames are included. Example from debug log: - chunk_0 first4=1a45dfa3 (valid WebM header) ✅ - chunk_1 first4=43c38172 (INVALID - raw Opus, no WebM container) The fix: 1. Extract the WebM header from chunk_0 (first 4096 bytes contain EBML + Segment + Tracks) 2. For each subsequent chunk that doesn't start with 1a45dfa3, prepend the header 3. Then decode the patched chunk to PCM WAV Also adds _patch_chunk_with_header() and _extract_webm_header() helper methods and improved debug logging with per-chunk first4 bytes.