Skip to content

API Reference parrot.integrations.liveavatar.room_audio_publisher

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.integrations.liveavatar.room_audio_publisher

View source

Headless LiveKit room audio publisher (FEAT-256 Module 1).

Joins the ai-parrot-owned LiveKit room as a headless participant (using the publish-capable agent_token from :func:~room_manager.LiveKitRoomManager.mint_room_tokens) and publishes a direct audio track fed with Supertonic PCM frames.

This is the core of the avatar-OFF path: when the avatar is disabled (or LiveAvatar has no credits), ai-parrot itself pushes audio directly into the room so the browser still hears the bot.

Audio format: 24 kHz mono 16-bit PCM (matches Supertonic output — no resampling).

Design constraints:

  • Keep-alive: the publisher is long-lived; do NOT use it as a one-shot context manager per turn (mirrors the keep-alive caveat in handlers/avatar.py).
  • Idempotent aclose: teardown never raises; safe to call multiple times.
  • No double audio: only ONE sink (publisher OR LiveAvatar WS) is ever active per session.

Usage::

publisher = await RoomAudioPublisher.start(tokens)
# ... per turn ...
await publisher.capture_pcm(pcm_bytes)
# ... on interrupt ...
await publisher.flush()
# ... on session end ...
await publisher.aclose()

Attribute FAILURE_CAPTURE

Type: str

Attribute FAILURE_DISCONNECTED

Type: str

Attribute FailureCallback

Alias LiveKitRoomTokens

Re-exported from parrot.integrations.liveavatar.models.LiveKitRoomTokens.

Attribute PresenceCallback

Class RoomAudioPublisher

Headless LiveKit participant that publishes a Supertonic audio track.

Created via :meth:start (class-method factory) which performs the async room connection and track publication. After creation the publisher is ready to receive PCM via :meth:capture_pcm.

Parameter Type Default Description
room object Connected livekit.rtc.Room.
source object livekit.rtc.AudioSource for pushing frames.
track object Published livekit.rtc.LocalAudioTrack.
audio_frame_cls type livekit.rtc.AudioFrame class (cached to avoid repeated lazy imports in the hot-path :meth:capture_pcm).
sample_rate int _SAMPLE_RATE PCM sample rate in Hz (default 24 000).
num_channels int _NUM_CHANNELS Number of PCM channels (default 1 — mono).
identity Optional[str] None This publisher's LiveKit participant identity.
track_sid Optional[str] None SID of the published track, when the SDK returned one.
on_failure Optional[FailureCallback] None Called once with a reason code when publication fails.
on_presence Optional['PresenceCallback'] None Called with (identity, present) on every remote participant join/leave. This connection is the only real LiveKit room handle the producer holds, so it is also the only place server-verified presence can be observed — which is what a lease's confirmed flag is required to mean.

Attributes:

Attribute Type Description
room The connected livekit.rtc.Room instance.
source The livekit.rtc.AudioSource that frames are pushed into.
track The published livekit.rtc.LocalAudioTrack.

Method RoomAudioPublisher.start

Connect to the LiveKit room and publish an audio track.

Two calling conventions:

  • Legacy — start(tokens) joins with tokens.agent_token and publishes agent-voice, exactly as FEAT-256 callers expect.
  • Broadcast — start(livekit_url=…, token=…, track_name="direct-voice") joins with a distinct direct-publisher token (TASK-2954). Sharing the avatar's fixed avatar-agent identity would make the two publishers evict each other in the room (spec §6).

start(tokens: Optional[LiveKitRoomTokens] = None, *, sample_rate: int = _SAMPLE_RATE, num_channels: int = _NUM_CHANNELS, livekit_url: Optional[str] = None, token: Optional[str] = None, track_name: str = 'agent-voice', queue_size_ms: int = 1000, on_failure: Optional[FailureCallback] = None, on_presence: Optional[PresenceCallback] = None) -> 'RoomAudioPublisher'

Parameter Type Default Description
tokens Optional[LiveKitRoomTokens] None Legacy room credentials. Mutually exclusive with livekit_url/token.
sample_rate int _SAMPLE_RATE PCM sample rate in Hz (default 24 000).
num_channels int _NUM_CHANNELS Number of PCM channels (default 1 — mono).
livekit_url Optional[str] None Room WebSocket URL, for the broadcast convention.
token Optional[str] None Publish-capable JWT for this publisher's own identity.
track_name str 'agent-voice' Published track name.
queue_size_ms int 1000 Native audio-source queue depth.
on_failure Optional[FailureCallback] None Called once with a reason code when publication fails or the room disconnects.
on_presence Optional[PresenceCallback] None Called with (identity, present) as remote participants join and leave the room.

Returns: 'RoomAudioPublisher' — A ready :class:RoomAudioPublisher instance.

Raises:

  • ValueError — If neither or both calling conventions are used.
  • ImportError — If the livekit realtime SDK is not installed.
  • Exception — If the room connection or track publication fails.

Method RoomAudioPublisher.capture_pcm

Push a block of raw PCM audio into the room audio track.

Wraps the bytes in a :class:livekit.rtc.AudioFrame and calls AudioSource.capture_frame. A no-op when closed or during a flush.

capture_pcm(pcm: bytes) -> None

Parameter Type Default Description
pcm bytes Raw 16-bit PCM bytes at the sample rate / channel count the publisher was created with (default: 24 kHz mono 16-bit).

Method RoomAudioPublisher.flush

Barge-in: drop in-flight audio and purge the native queue.

The pre-FEAT-537 implementation only toggled a Python-side flag, which left everything already handed to AudioSource to play out — so a "flushed" interrupt still spoke the previous turn (spec §6: "flush() is not a native queue purge in the verified baseline"). This now calls AudioSource.clear_queue() as well.

Idempotent and never raises; a no-op once closed.

flush() -> None

Method RoomAudioPublisher.wait_for_playout

Await the native queue draining at normal turn completion.

Call this only when a turn finished on its own. Spec §2: "never wait for stale audio during cancellation" — on an interrupt or handoff use :meth:flush, which discards that audio instead of waiting for it.

wait_for_playout(timeout_s: Optional[float] = None) -> bool

Parameter Type Default Description
timeout_s Optional[float] None Optional deadline in seconds.

Returns: bool — True when playout completed, False on timeout or when the SDK does not expose wait_for_playout.

Method RoomAudioPublisher.aclose

Disconnect from the room and release resources (idempotent).

Safe to call multiple times; subsequent calls are no-ops. Never raises — teardown errors are logged and suppressed.

aclose() -> None

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally