process_completed_stream

Function process_completed_stream 

Source
pub(crate) async fn process_completed_stream(
    capture: StreamCapture,
    pool: &PgPool,
    mp3_bitrate: u32,
) -> Result<(), Box<dyn Error + Send + Sync>>
Expand description

Finalizes one captured voice stream: MP3-encodes the audio and persists the row to PostgreSQL.

Called by the Tier 3 orchestrator when a VoiceEnd event fires (or an idle timeout elapses). The orchestrator is stubbed in run above so the function is unreferenced at build time today — the follow-up task will wire it into the session pool.

Two database writes happen:

  1. streams::insert_stream — creates the row with header metadata.
  2. streams::update_audio — fills in the MP3 blob, frame count, end timestamp, and decoded slow-data fields (text, DPRS lat/lon).

The split into insert + update mirrors the schema lifecycle documented in db/streams.rs: the row is visible to the HTTP API as soon as the header arrives, and its audio_mp3 field transitions from NULL to a populated blob when encoding completes.

§Errors

  • AMBE/MP3 decode errors (decoder::DecodeError) are wrapped in the returned boxed error and logged at warn level — one bad stream should not abort the capture loop.
  • SQL errors from the insert or update are bubbled up so the caller can distinguish transient database issues from bad audio.