Skip to content

Call audio streaming

Call audio streaming sends the live audio of a call to a WebSocket server that you run. SIPRTC opens the connection to your server and delivers the audio as JSON messages. You use it to transcribe or analyse calls as they happen, record them in your own system, or put a voice bot on the call.

You start a stream from TinyML, the XML that controls a call. You can include it in the TinyML your application returns, or send it to a live call through the Calls API.

One-way (fork) Two-way (bidirectional)
TinyML <Start><Stream/></Start> <Connect><Stream/></Connect>
Audio to your server Yes Yes
Audio from your server played into the call No Yes
Tracks you can stream inbound_track, outbound_track or both_tracks inbound_track
What the call does next Moves on to the next TinyML verb while the stream runs Stays on the stream. Later verbs are not run.
Typical use Live transcription, analytics, call recording, agent assist Voice bots and AI agents

<Start><Stream> forks the audio. The call goes on to the next verb straight away, for example a <Dial> to an agent, and the stream keeps running in the background. Your server only listens; anything it sends is ignored.

Choose which audio you receive with the track attribute:

  • inbound_track (default): the audio SIPRTC receives from the other party on this call leg, for example the caller.
  • outbound_track: the audio SIPRTC sends to that party: prompts, or the voice of the person they are connected to.
  • both_tracks: both, as separate messages labelled inbound and outbound.
CallerSIPRTCYour WebSocket servercall audiocall moves on, e.g. to <Dial>connected, then startmedia every 20 msinbound / outbound tracklistens onlyanything it sends is ignoredcall hangs up or <Stop><Stream/>stop, then close (1000)
One-way stream — <Start><Stream>

<Connect><Stream> hands the call to your server. You receive the caller’s audio, and the audio you send back is played to the caller. You can also clear queued audio when the caller interrupts (barge-in), place marks to know when playback reaches a point, and send DTMF tones.

While the stream runs, the caller hears only the audio you send; when you send nothing, they hear silence. <Connect> never moves on to the next verb. To move the call somewhere else, for example to a queue when the bot hands over to a person, update the call through the Calls API with new TinyML that starts with <Stop><Stream/></Stop>. See Start and stop streams.

CallerSIPRTCYour WebSocket servere.g. a voice botconnected, then startspeaksmedia · the caller's audiomedia (your audio)then a markplays your audiomark echowhen playback reaches ittalks over the botcleardrops queued audio (barge-in)hand over to a personCalls API update with new TinyMLstarting with <Stop><Stream/>
Two-way stream — <Connect><Stream>
  1. The call runs <Start><Stream> or <Connect><Stream>. If the call has not been answered yet, SIPRTC answers it.
  2. SIPRTC connects to your wss:// URL.
  3. When the WebSocket handshake succeeds, SIPRTC sends a connected message and then a start message describing the call, the tracks and the audio format. Your status callback receives stream-started.
  4. SIPRTC sends a media message for every packet of call audio, normally every 20 ms per track.
  5. In a two-way stream, you send media, mark, clear and dtmf messages back.
  6. The stream ends when the call hangs up, when TinyML runs <Stop><Stream/></Stop>, or when your server closes the connection. If SIPRTC ends it, you receive a stop message and then a normal close (code 1000). Your status callback receives stream-stopped.

SIPRTC does not reconnect a stream. If the connection fails or drops, the stream is over. Start a new one with TinyML if you need to.

The message formats are in WebSocket protocol. Audio encodings and code samples are in Audio formats and examples. Status callbacks are in Stream status callbacks.

  • One stream per call leg. A leg that is already streaming, including a live transcription started with <Start><Transcription>, refuses a second stream. Stop the first one before you start another.
  • Mono audio. Each track is one channel. With both_tracks, the two tracks arrive as separate messages, not mixed together.
  • Encodings: audio/x-mulaw (8-bit G.711 μ-law, the default) or audio/L16 (16-bit linear PCM, little-endian).
  • Sample rates: 8000 Hz, or 16000 Hz for audio/L16 on calls whose codec carries wideband audio (for example G.722 or Opus). Phone network calls are 8000 Hz. Always read the rate from the start message.
  • Queued audio: a two-way stream queues up to 30 seconds of audio you send. Audio beyond that is dropped.
  • Use wss://. SIPRTC accepts ws:// and wss:// URLs. Use wss:// for anything other than local testing. Your server needs a certificate that a public certificate authority has issued for its hostname. Self-signed certificates are rejected.
  • Authenticate with the URL or the custom parameters. SIPRTC does not send an Authorization header or any custom headers when it connects. To check that a connection is genuine, put a hard-to-guess token in the URL path or query string (for example wss://bot.example.com/stream?token=...), or pass one in the stream’s custom parameters and check it in the start message before you trust the stream. User names and passwords in the URL (wss://user:pass@host) are not supported.
  • Source addresses. Connections come from SIPRTC’s media servers. Authenticate with a token as above rather than by allow-listing IP addresses.
  • Status callbacks carry no signature. Use an HTTPS callback URL with a secret in it, or credentials in the URL (https://user:pass@example.com/cb), which SIPRTC sends as HTTP basic authentication.
  • Keep the URL private. It may contain your token, so treat TinyML that contains it as a secret.