Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Stream decoded PCM chunks directly to disk for Linux/Windows#23

Merged
sjoenk merged 3 commits intomainfrom
feature/19-implement-streaming-output-to-disk-for-linuxwindows
Feb 14, 2026
Merged

Stream decoded PCM chunks directly to disk for Linux/Windows#23
sjoenk merged 3 commits intomainfrom
feature/19-implement-streaming-output-to-disk-for-linuxwindows

Conversation

@sjoenk
Copy link
Owner

@sjoenk sjoenk commented Feb 14, 2026

Summary

  • Introduces a callback-based DecodeToPcmStream function on both Linux (GStreamer) and Windows (Media Foundation) that writes each decoded PCM chunk directly to the output file instead of accumulating all data in a std::vector<uint8_t> before writing
  • Rewrites ConvertToWav, ConvertToM4a (Linux), and TrimAudio (both platforms) to stream PCM to disk during decoding using placeholder WAV header + seek-back pattern
  • Adds a ~4 GB WAV size guard (kMaxWavDataSize) consistent with Android/iOS implementations

Test plan

  • Run integration tests on Linux (flutter test integration_test/)
  • Run integration tests on Windows (flutter test integration_test/)
  • Verify WAV conversion produces valid, playable output
  • Verify M4A conversion still works correctly on Linux
  • Verify trim operations (both WAV and M4A output) produce correct results
  • Profile peak memory usage with a large audio file to confirm reduction

Closes #19

…onversion

Introduce a callback-based DecodeToPcmStream function on both platforms
that writes each decoded PCM chunk directly to the output file instead
of accumulating all data in a std::vector before writing. This
drastically reduces peak memory usage for large audio files.

The existing DecodeToPcm now delegates to DecodeToPcmStream, preserving
the accumulation behavior for callers that still need all data in memory
(GetWaveform, Windows TrimAudio M4A branch).

Affected paths: ConvertToWav, ConvertToM4a (Linux), and TrimAudio on
both platforms. Adds a ~4 GB WAV size guard (kMaxWavDataSize).
@sjoenk sjoenk linked an issue Feb 14, 2026 that may be closed by this pull request
- Add try/catch in DecodeToPcmStream (Linux/Windows) to prevent
  GStreamer/Media Foundation resource leaks when the onChunk callback
  throws (e.g. WAV size overflow)
- Extract streamPcmToWav helper that encapsulates the placeholder-header,
  stream, seek-back, finalize pattern with proper file cleanup on error
- Simplify ConvertToWav, ConvertToM4a, and TrimAudio to use the helper,
  eliminating ~5x code duplication
- Add kMaxWavDataSize guard to Linux ConvertToM4a (was missing)
- Document why Windows TrimAudio M4A path uses full buffering
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements streaming-to-disk decoding for Linux and Windows audio conversion, eliminating the need to buffer entire decoded PCM audio in memory before writing. This follows the same approach previously implemented for Android (issue #14) and iOS/macOS (issue #18).

Changes:

  • Introduces DecodeToPcmStream callback-based API on Windows (Media Foundation) and Linux (GStreamer)
  • Refactors ConvertToWav, ConvertToM4a (Linux), and TrimAudio to use streaming writes with placeholder WAV headers
  • Adds 4 GB WAV size limit enforcement consistent with Android/iOS implementations

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
windows/audio_decoder_plugin.h Adds PcmInfo struct, streamPcmToWav, and DecodeToPcmStream method declarations; changes WriteWavHeader signature to accept std::ostream&
windows/audio_decoder_plugin.cpp Implements streaming decode with Media Foundation, refactors WAV/trim operations to stream data, adds WAV size limit
linux/audio_decoder_plugin.cc Implements streaming decode with GStreamer, refactors WAV/M4A/trim operations to stream data, adds WAV size limit

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

- Rename streamPcmToWav to StreamPcmToWav for consistency with project
  naming conventions (Linux + Windows)
- Add error check after file.write() in onChunk callback to detect
  write failures early
- Add error check after file.seekp(0) with cleanup on failure
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement streaming output to disk for Linux/Windows

2 participants