2.6 KiB
2.6 KiB
trigger, description
| trigger | description |
|---|---|
| model_decision | When working on the voxer-like audio recording, chunking, streaming and playback setup |
Audio Recording & Streaming Architecture
This project implements a "Voxer-like" audio messaging system using Phoenix Channels for binary streaming and Oban for background processing.
1. Components & Data Flow
- Capture:
AudioRecorderJS Hook (assets/js/hooks/audio_recorder.js) usesMediaRecorderwithaudio/webm;codecs=opus. - Transport: Raw binary chunks are pushed via Phoenix Channels (
audio:lobby). - Persistence: Server appends chunks to a
.webmfile inpriv/static/uploads/. - Processing:
audio_endevent triggers an Oban job (RiverConnect.Workers.ProcessRecording) to finalize and update status. - Serving: Files are served from
/uploads/via a dedicatedPlug.Staticentry inRiverConnectWeb.Endpoint.
2. Channel Protocol (AudioChannel)
audio_start:%{"id" => id}. Initializes the recording session and creates a temp file.audio_chunk:{:binary, payload}. Appends binary to the file and broadcasts to other listeners for live playback.audio_end:%{"duration_ms" => ms, "id" => id}. Creates theAudioMessageDB record and enqueues the Oban worker.
3. Real-time Playback
- While a recording is in progress, the channel broadcasts
audio_chunkevents. - The receiving
AudioRecorderhook usesAudioContext.decodeAudioDatato play these chunks immediately, providing a "near-realtime" walkie-talkie experience.
4. Database Schema (AudioMessage)
user_id: String (Guest ID or User ID).file_path: Relative path (e.g.,/uploads/uuid.webm).duration_ms: Total recording duration.status:"processing"|"ready".- Note: The schema must
@derive {Jason.Encoder, ...}to be sent over Phoenix Channels.
5. Environment Constraints (Windows)
- Commands like
mixmay need to be executed viacmd /cor with explicit PATH toerl.exeandmix.batin backgrounds tasks. - Static files in
priv/static/uploadsmust be exposed explicitly inendpoint.exand may require a server restart to be served correctly after initial creation.
6. Development Tips
- Restart Required: Adding new sockets or
Plug.Staticrules inendpoint.exrequires a fullmix phx.serverrestart. - Permissions: Ensure the browser has microphone permissions for the specific port (default 4000/4005).
- Streaming Jitter: Basic playback uses
decodeAudioData. For production-grade streaming without gaps, a jitter buffer/audio scheduler would be required.