Class TelnyxMediaStreamParser

Parses the Telnyx media stream WebSocket protocol.

Telnyx sends JSON-encoded messages for stream lifecycle events (start, stop) and audio chunks (media). Unlike Twilio, Telnyx does NOT deliver DTMF events over the media stream WebSocket -- those arrive as HTTP webhooks to a separate endpoint and must be handled outside this parser.

Outgoing audio is sent as a raw binary Buffer (mu-law PCM bytes without any JSON envelope) because Telnyx accepts unframed binary WebSocket frames directly. No explicit connection acknowledgment is needed after the handshake.

Implements

Constructors

Methods

  • Parse a raw WebSocket frame from Telnyx's media stream.

    Supported Telnyx event types:

    • start -- stream established; stream_id maps to streamSid, call_control_id maps to callSid.
    • media -- audio chunk; media.chunk field contains base64-encoded mu-law bytes; only inbound track frames are returned (outbound echoes are discarded to prevent feedback loops).
    • stop -- stream ended (call terminated or stream explicitly closed).

    Any other event type (e.g., future Telnyx additions, DTMF attempts) is silently dropped by returning null.

    Parameters

    • data: string | Buffer<ArrayBufferLike>

      Raw WebSocket frame payload (JSON string or Buffer from Telnyx).

    Returns null | MediaStreamIncoming

    Normalised MediaStreamIncoming event, or null for outbound audio tracks, unknown event types, or malformed messages.

  • Encode mu-law audio for transmission back to Telnyx.

    Telnyx accepts raw binary WebSocket frames -- no JSON wrapping is needed. This is the key asymmetry in Telnyx's protocol: inbound is JSON, outbound is raw binary.

    Parameters

    • audio: Buffer<ArrayBufferLike>

      Raw mu-law PCM bytes to send to the caller.

    • _streamSid: string

      Unused by Telnyx binary framing (accepted for interface parity with other parsers).

    Returns Buffer<ArrayBufferLike>

    The audio Buffer unchanged, ready to send as a binary WS frame.