Class PlivoMediaStreamParser

Parses the Plivo Audio Stream WebSocket protocol.

Plivo sends JSON-encoded messages for stream lifecycle events (start, stop) and audio chunks (media). The audio payload is base64-encoded mu-law PCM, delivered in a payload field inside the media object.

Outgoing audio is wrapped in a playAudio JSON envelope, which is the format Plivo expects when the server streams audio back to the caller. No explicit connection acknowledgment is required after the handshake.

Implements

Constructors

Methods

  • Parse a raw WebSocket frame from Plivo's audio stream.

    Supported Plivo event types:

    • start -- stream established; stream_id maps to streamSid, call_uuid maps to callSid.
    • media -- audio chunk; media.payload contains base64-encoded mu-law PCM bytes.
    • stop -- stream ended (call terminated or stream explicitly closed).

    Any other event type is silently dropped by returning null. Malformed JSON or messages missing required fields (event, stream_id) also return null.

    Parameters

    • data: string | Buffer<ArrayBufferLike>

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

    Returns null | MediaStreamIncoming

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

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

    Plivo requires audio to be base64-encoded and wrapped in a playAudio JSON envelope. Unlike Twilio, the streamSid / stream_id is NOT included in the outbound message -- Plivo implicitly routes the audio to the caller on the same WebSocket connection.

    Parameters

    • audio: Buffer<ArrayBufferLike>

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

    • _streamSid: string

      Unused by Plivo's playAudio format (accepted for interface parity with other parsers).

    Returns string

    JSON string: { event: 'playAudio', media: { payload: '<base64>' } }