2.5 KiB
2.5 KiB
Audio Recording & Streaming Architecture
This project implements an asynchronous and real-time 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) utilizes theMediaRecorderAPI withaudio/webm;codecs=opus. - Transport: Binary chunks are transmitted via Phoenix Channels (
audio:lobby). - Persistence: The server appends incoming chunks to a
.webmfile located inpriv/static/uploads/. - Finalization: An
audio_endevent triggers an Oban job (RiverConnect.Workers.ProcessRecording) to verify the file and update the database status. - Serving: Static audio files are exposed via a dedicated
Plug.Staticentry inRiverConnectWeb.Endpoint.
2. Channel Protocol (AudioChannel)
audio_start:%{"id" => id}. Initializes the server-side recording state and creates the target file.audio_chunk:{:binary, payload}. Appends binary data to the file and broadcasts the chunk for real-time consumption.audio_end:%{"duration_ms" => ms, "id" => id}. Persists theAudioMessagerecord and enqueues the processing worker.
3. Real-time Streaming
- During an active recording session, the channel broadcasts
audio_chunkevents to all subscribers. - Subscribed clients use
AudioContext.decodeAudioDatato process and play the binary stream with minimal latency.
4. Database Schema (AudioMessage)
user_id: Identifies the sender (Guest or Authenticated).file_path: Relative URL to the static resource.duration_ms: Integer representing the length of the audio.status: State machine values ("processing","ready").- Note: The schema must
@derive {Jason.Encoder, ...}for binary serialization over the socket.
5. Environment & Infrastructure
- Windows Support:
mixcommands in background processes may require explicit PATH configuration for Erlang/Elixir binaries. - Static Configuration: Dynamic directories in
priv/staticrequire explicit inclusion in theEndpointconfiguration to be served correctly.
6. Implementation Notes
- Server Restarts: Modifications to
endpoint.exorUserSocketrequire a server restart to take effect. - Hardware Access: Web-based recording requires secure context (HTTPS/Localhost) and explicit browser microphone permissions.
- Latency Management: Real-time playback relies on prompt chunk processing; production environments may require a jitter buffer for consistency.