Skip to content

Offline/WAL sync uploads decoded WAV instead of raw opus — 16x unnecessary bandwidth and storage #5363

Closed

Description

@omi-discord-vector

Problem

The live streaming path (BLE → WebSocket) correctly sends opus frames to the backend, which decodes server-side via opuslib. However, the offline/WAL sync path (/v1/sync-local-files) decodes opus → PCM on the phone, converts to WAV, then uploads the WAV file.

This means:

  • ~16x larger uploads (opus ~7 MB/hr vs WAV ~115 MB/hr at 16kHz mono 16-bit)
  • ~16x more GCS storage per conversation for audio chunks
  • Unnecessary phone CPU usage decoding opus before upload
  • Longer sync times on cellular/slow connections

For all-day recorders, this adds up to nearly 1 GB/day of raw PCM stored per user vs ~60 MB if kept as opus.

Root Cause

The two audio paths (live streaming vs offline sync) were built independently:

  • Live streaming ( WebSocket): accepts param, decodes server-side ✅
  • Offline sync (): expects WAV file uploads, app decodes on-device ❌

The backend already has the opus decoding infrastructure () — it's just not used in the sync path.

Proposed Fix

  1. App side ( / ): Upload raw opus frames directly instead of transcoding to WAV
  2. Backend side (): Accept opus-encoded files, decode server-side using existing infrastructure
  3. Storage: Store audio chunks as opus in GCS, decode to PCM only when needed (STT processing, share audio)

Impact

  • ~16x reduction in upload bandwidth for offline sync
  • ~16x reduction in GCS storage costs for audio
  • Faster sync times for users
  • Less battery drain on phone (no opus decode during upload)

Related

  • The "share audio" feature also serves WAV files — could serve AAC/opus for smaller downloads
  • Audio chunks in GCS are stored as raw PCM — could be opus at rest

Discovered via Discord discussion analyzing the codebase.

Activity

  1. added
    bug
    on Mar 6, 2026
  2. added
    p2
    on May 8, 2026
  3. aryanorastar commented on Aug 5, 2026

    @aryanorastar
    Collaborator

    Status check (2026-08-05) — upload half of this issue is already fixed on main

    Re-verified against current main (07494f52): the stated bug (“phone decodes opus → WAV then uploads WAV to /v1/sync-local-files”) no longer matches the shipping paths.

    What the code does now

    App (Flutter / shared WAL sync) writes length-prefixed opus frames to disk and uploads those .bin files via POST /v2/sync-local-files:

    • app/lib/services/wals/local_wal_sync.dart — flush builds length-prefixed frames → .bin
    • app/lib/services/wals/wal.dart — filenames like audio_*_opus_*_fsN_*.bin
    • app/lib/backend/http/api/conversations.dart — uploadLocalFilesV2 → v2/sync-local-files

    Native phone-mic / batch writers (iOS BaseBatchAudioWriter, Android BaseBatchAudioWriter + PhoneMicOpusEncoder) also emit the same length-prefixed .bin layout.

    Backend rejects non-.bin uploads and decodes server-side:

    • backend/utils/sync/files.py retrieve_file_paths / v2 equivalent — endswith('.bin') only
    • decode_opus_file_to_wav — opus → WAV for VAD/STT
    • GCS chunks are stored as .opus via upload_audio_chunk → encode_pcm_to_opus (after the server decode)

    Local WAV decode in the app (audio_player_utils._decodeOpusToWav) is for playback/share, not sync upload.

    What remains (separate from the 16× upload claim)

    Sync still does opus → WAV → PCM → opus on the server for STT then storage. That is CPU / latency waste, not a 16× wire/GCS blowup from client WAV uploads. Fixing that would be a new scoped change (“store original WAL opus framing without re-encode” or share AAC/opus) — not the original proposed app-side “stop uploading WAV.”

    Suggest closing this issue as fixed for the upload path, or retitling/splitting if maintainers want a follow-up for the server re-encode.

    — verified by @aryanorastar (no PR needed for the original claim)

  4. Git-on-my-level commented on Sep 9, 2026

    @Git-on-my-level
    Member

    Fixed by #12893 (merge commit d324a95): the WAL sync path now rejects decoded WAV and any non-framed name via assertWalSyncFilesAreFramedBins before /v2/sync-local-files, keeping uploads framed opus/PCM .bin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmobilep2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions