> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tuco.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Stream FaceTime Call Audio

> Connect to a FaceTime call's streamUrl WebSocket to send your caller's microphone and hear the other side — 16-bit PCM both ways, with in-band call-phase frames.

> [!NOTE]
>
> **Live.** This is the audio channel for a call you placed with [Place a FaceTime Call](https://docs.tuco.ai/api-reference/endpoint/facetime-place-call.md). Open the `streamUrl` it returned and you get the call's audio both ways, so you can build your own call UI — a dialer, a softphone, a web app — with no Tuco call window.

## The URL

[Place a FaceTime Call](https://docs.tuco.ai/api-reference/endpoint/facetime-place-call.md) returns a `streamUrl`:

```
wss://app.tuco.ai/api/facetime/stream?token=eyJhbGciOi...
```

It's a **WebSocket** URL, one-time and per-call. The token in the query string is short-lived and scoped to that single call and workspace — it carries **no API key**, so it is safe to hand to a browser. WebSocket connections aren't subject to CORS, so you connect to it directly from your frontend; your API key never leaves your server.

Open it as soon as you have it — the other side's phone is already ringing.

## Protocol

The connection is **binary audio both ways, plus small JSON phase frames from the server**.

**Audio you send (client → call)** `binary`

Your caller's microphone, as **16-bit signed PCM, little-endian, 48,000 Hz, mono**. Send it in binary WebSocket frames (any reasonable chunk size — e.g. 20–40 ms of audio per frame).

**Audio you receive (call → client)** `binary`

The other side's audio, in the **same format** — 16-bit signed PCM, little-endian, 48,000 Hz, mono. Arrives as binary frames; play it as it comes.

**Phase frames you receive (call → client)** `text (JSON)`

Text frames announcing call state, so your UI can react without waiting on your server's webhooks:

```json theme={null}
{ "type": "phase", "phase": "ringing" }
{ "type": "phase", "phase": "connected" }
{ "type": "phase", "phase": "ended" }
```

`connected` means the other side answered; `ended` means the call is over (the connection closes right after). The same transitions are also delivered to your backend as [`call.ringing` / `call.answered` / `call.ended` webhooks](https://docs.tuco.ai/api-reference/message-webhooks#facetime-call-events) — use whichever fits.

> [!NOTE]
>
> **One listener per call.** Each call's `streamUrl` serves a single connection. A second attempt to connect to the same call is rejected with close code `4409`.

## Close codes

The server closes the socket with an app code (4000–4999) when something's wrong:

| Code | Meaning |
| - | - |
| `4401` | Token invalid or expired — place the call again to get a fresh `streamUrl`. |
| `4404` | The call or its line no longer exists. |
| `4409` | This call is already being streamed on another connection. |
| `1011` | The line couldn't be reached, or an internal error — place the call again. |
| `1000` / `1001` | Normal close (the call ended). |

## Browser example

A minimal two-way loop: capture the microphone, send it up, play what comes back, and react to phase frames.

```js theme={null}
// callDocId + streamUrl came from POST /api/facetime/call (made on your server)
const SAMPLE_RATE = 48000;
const ws = new WebSocket(streamUrl);
ws.binaryType = "arraybuffer";
const audio = new AudioContext({ sampleRate: SAMPLE_RATE });
let playAt = 0;

ws.onmessage = (ev) => {
  if (typeof ev.data === "string") {
    const msg = JSON.parse(ev.data);
    if (msg.type === "phase") setCallState(msg.phase); // "ringing" | "connected" | "ended"
    return;
  }
  // incoming audio: Int16LE PCM -> Float32 -> schedule for playback
  const view = new DataView(ev.data);
  const frame = new Float32Array(ev.data.byteLength / 2);
  for (let i = 0; i < frame.length; i++) frame[i] = view.getInt16(i * 2, true) / 32768;
  const buf = audio.createBuffer(1, frame.length, SAMPLE_RATE);
  buf.copyToChannel(frame, 0);
  const src = audio.createBufferSource();
  src.buffer = buf;
  src.connect(audio.destination);
  const now = audio.currentTime;
  if (playAt < now) playAt = now + 0.05; // small jitter buffer
  src.start(playAt);
  playAt += buf.duration;
};

ws.onopen = async () => {
  const mic = await navigator.mediaDevices.getUserMedia({
    audio: { channelCount: 1, sampleRate: SAMPLE_RATE, echoCancellation: true, noiseSuppression: true },
  });
  const node = audio.createScriptProcessor(2048, 1, 1);
  node.onaudioprocess = (e) => {
    if (ws.readyState !== WebSocket.OPEN) return;
    const input = e.inputBuffer.getChannelData(0);
    const out = new DataView(new ArrayBuffer(input.length * 2));
    for (let i = 0; i < input.length; i++) {
      const s = Math.max(-1, Math.min(1, input[i]));
      out.setInt16(i * 2, s < 0 ? s * 32768 : s * 32767, true); // Float32 -> Int16LE
    }
    ws.send(out.buffer);
  };
  audio.createMediaStreamSource(mic).connect(node);
  node.connect(audio.destination);
};

ws.onclose = (e) => setCallState(e.code >= 4000 ? "error" : "ended");
```

> [!TIP]
>
> To **mute**, just stop sending microphone frames (and optionally call [`POST /api/facetime/event`](https://docs.tuco.ai/api-reference/endpoint/facetime-end-call) with `action: "mute"`). To **hang up**, call that same endpoint with `action: "hangup"` — don't rely on only closing the socket, so the record is finalized cleanly.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.