Skip to content

API Reference parrot.integrations.liveavatar.broadcast.floor

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

parrot.integrations.liveavatar.broadcast.floor

View source

Speaking-floor authority and the moderated handoff barrier (FEAT-537).

Implements spec §2 "Moderation and exclusive speaking floor" — the half of Module 4 that decides who is allowed to talk, separately from the transport that carries their audio.

The barrier is the point of the whole module. A naive handoff sets speaker_lease_id = new and hopes the old browser stops; that leaves a window where two microphones reach Nova. Instead every transition runs:

  1. grant_floor → switching, speaker cleared, floor_epoch +1. From this instant nobody may send audio and every old-epoch frame is rejected.
  2. Tell the outgoing speaker's socket to stop capturing.
  3. BroadcastSession.switch_speaker — the producer cancels the in-flight turn, clears the software queue, interrupts the avatar and purges the native audio queue, then acknowledges.
  4. Only then commit_floor installs the new speaker.

If step 3 fails or times out, abort_floor leaves the floor idle and the caller gets a retryable error. Silence with a visible error is the correct outcome; two live speakers is not (spec §2, AC13).

Class BroadcastControlService

What the broadcast WebSocket route needs from the service layer.

Declared here, as a Protocol, so TASK-2960's transport can be written and tested against an explicit contract instead of prose, and so TASK-2961's concrete BroadcastService has a machine-checkable target. The handler never imports the concrete class — that would drag the optional LiveKit/Redis stack into every voice deployment.

Method BroadcastControlService.resolve_principal

Map an authenticated user plus an agent id to a scoped principal.

resolve_principal(user: Any, agent_id: str) -> Any

Parameter Type Default Description
user Any
agent_id str

Returns: Any

Method BroadcastControlService.get_descriptor

Return the current descriptor, or None.

get_descriptor(tenant_id: str, broadcast_id: str) -> Any

Parameter Type Default Description
tenant_id str
broadcast_id str

Returns: Any

Method BroadcastControlService.get_lease

Return one lease, or None.

get_lease(tenant_id: str, broadcast_id: str, lease_id: str) -> Any

Parameter Type Default Description
tenant_id str
broadcast_id str
lease_id str

Returns: Any

Method BroadcastControlService.heartbeat

Record a control-socket heartbeat for a lease.

heartbeat(tenant_id: str, broadcast_id: str, lease_id: str) -> None

Parameter Type Default Description
tenant_id str
broadcast_id str
lease_id str

Method BroadcastControlService.attach_control

Register a control socket so state changes can be pushed to it.

attach_control(tenant_id: str, broadcast_id: str, lease_id: str, send: Notifier) -> None

Parameter Type Default Description
tenant_id str
broadcast_id str
lease_id str
send Notifier

Method BroadcastControlService.detach_control

Deregister a control socket.

detach_control(tenant_id: str, broadcast_id: str, lease_id: str) -> None

Parameter Type Default Description
tenant_id str
broadcast_id str
lease_id str

Method BroadcastControlService.public_state

Return the browser-safe projection of the broadcast.

public_state(tenant_id: str, broadcast_id: str) -> Dict[str, Any]

Parameter Type Default Description
tenant_id str
broadcast_id str

Returns: Dict[str, Any]

Method BroadcastControlService.bind_speaker_socket

Bind the one microphone socket allowed to send audio.

bind_speaker_socket(tenant_id: str, broadcast_id: str, lease_id: str, socket_id: str, floor_epoch: int) -> bool

Parameter Type Default Description
tenant_id str
broadcast_id str
lease_id str
socket_id str
floor_epoch int

Returns: bool

Method BroadcastControlService.unbind_speaker_socket

Release a microphone-socket binding.

unbind_speaker_socket(tenant_id: str, broadcast_id: str, lease_id: str, socket_id: str) -> bool

Parameter Type Default Description
tenant_id str
broadcast_id str
lease_id str
socket_id str

Returns: bool

Method BroadcastControlService.voice_session

Return the broadcast-owned voice session on this worker, or None.

voice_session(tenant_id: str, broadcast_id: str) -> Any

Parameter Type Default Description
tenant_id str
broadcast_id str

Returns: Any

Method BroadcastControlService.media_session

Return the BroadcastSession on this worker, or None.

media_session(tenant_id: str, broadcast_id: str) -> Any

Parameter Type Default Description
tenant_id str
broadcast_id str

Returns: Any

Method BroadcastControlService.release_floor

Run the Finish-Speaking barrier back to the moderator.

release_floor(tenant_id: str, broadcast_id: str, speaker_lease_id: str) -> Any

Parameter Type Default Description
tenant_id str
broadcast_id str
speaker_lease_id str

Returns: Any

Attribute FLOOR_REVOKED

Attribute FLOOR_STATE

Class FloorCoordinator

Sequences registry state, socket notifications and the producer barrier.

Parameter Type Default Description
notifier Optional[Notifier] None Called as notifier(lease_id, frame) to reach one participant's control socket. Failures are swallowed — Redis remains authoritative and a browser that missed a notification recovers on its next poll (spec §2).
remote_barrier Optional[Callable[[str, str], Any]] None

Method FloorCoordinator.handoff

Move the floor, with the producer barrier in the middle.

handoff(registry: BroadcastRegistry, session: Any, *, tenant_id: str, broadcast_id: str, moderator_lease_id: str, target_lease_id: Optional[str], expected_version: int) -> HandoffResult

Parameter Type Default Description
registry BroadcastRegistry Shared state store.
session Any The owning :class:BroadcastSession, or None when the producer lives on another worker (the caller then relays the barrier).
tenant_id str Tenant scope.
broadcast_id str Broadcast to transition.
moderator_lease_id str The caller — must be the current moderator.
target_lease_id Optional[str] Lease to grant, or None to revoke (the floor returns to the moderator).
expected_version int Compare-and-set guard against a concurrent grant.

Returns: HandoffResult — The completed :class:HandoffResult.

Raises:

  • NotModerator — If the caller is not the current moderator.
  • StaleVersion — On a concurrent or already-switching conflict.
  • BroadcastError — With stale_floor_epoch when the producer does not acknowledge; the floor is left idle and the error is retryable.

Method FloorCoordinator.release

Finish Speaking / speaker departure — floor back to the moderator.

Uses the same barrier as a moderator grant, so a speaker releasing the floor cannot leave a shorter window than a revoke would.

release(registry: BroadcastRegistry, session: Any, *, tenant_id: str, broadcast_id: str, speaker_lease_id: str) -> HandoffResult

Parameter Type Default Description
registry BroadcastRegistry Shared state store.
session Any The owning :class:BroadcastSession, or None.
tenant_id str Tenant scope.
broadcast_id str Broadcast concerned.
speaker_lease_id str The lease giving the floor up.

Returns: HandoffResult — The completed :class:HandoffResult.

Raises:

  • NotSpeaker — If the caller does not hold the floor.
  • BroadcastError — On barrier failure.

Method FloorCoordinator.succeed_moderator

Complete the barrier a departure-driven election already opened.

release_viewer / elect_moderator open the barrier inside the registry (atomically, so no second moderator can appear), leaving the producer acknowledgement to the owning worker — this method.

succeed_moderator(registry: BroadcastRegistry, session: Any, *, tenant_id: str, broadcast_id: str, new_moderator_lease_id: str, floor_epoch: int, previous_speaker: Optional[str] = None) -> HandoffResult

Parameter Type Default Description
registry BroadcastRegistry Shared state store.
session Any The owning :class:BroadcastSession, or None.
tenant_id str Tenant scope.
broadcast_id str Broadcast concerned.
new_moderator_lease_id str The elected lease.
floor_epoch int Epoch the election opened.
previous_speaker Optional[str] None Lease to notify, if it is still connected.

Returns: HandoffResult — The completed :class:HandoffResult.

Raises:

  • BroadcastError — On barrier failure.

Class HandoffResult

Outcome of one completed floor transition.

Parameter Type Default Description
descriptor BroadcastDescriptor
previous_speaker_lease_id Optional[str]
floor_epoch int

Fields:

Field Type Default Description
descriptor BroadcastDescriptor Broadcast state after commit_floor.
previous_speaker_lease_id Optional[str] Who held the floor before, if anyone.
floor_epoch int The epoch the new speaker was installed under.

Attribute Notifier

Function validate_audio_authority

Reject microphone input that is not authorized right now.

Applied at ingress and again at the producer, immediately before any provider call. Deliberately strict about what does not authorize input: possessing the socket, being the moderator, or holding a valid LiveKit viewer JWT are all insufficient (spec §2).

The core fencing-tuple check (granted / right lease / right epoch / right socket) is delegated to :func:~.registry.validate_audio_authority so there is exactly one implementation of that rule. This wrapper adds the two things the registry version cannot see because it takes only a lease id: the socket binding stored on the lease, and control-connection freshness.

validate_audio_authority(descriptor: BroadcastDescriptor, lease: Optional[ViewerLease], *, floor_epoch: Optional[int], socket_id: str, now: Optional[float] = None) -> None

Parameter Type Default Description
descriptor BroadcastDescriptor Current broadcast state.
lease Optional[ViewerLease] The caller's lease, or None when it no longer exists.
floor_epoch Optional[int] Epoch the client claims to be sending under. None means the client omitted it, which is treated as stale rather than waved through — an old client must not bypass fencing by silence.
socket_id str The socket the frame arrived on.
now Optional[float] None Optional timestamp for the control-freshness check.

Raises:

  • FloorNotGranted — No floor is granted, the caller does not hold it, the lease has gone away, or its control connection is stale.
  • StaleFloorEpoch — The epoch is missing or superseded.
  • SpeakerConnectionExists — A different socket holds the binding.

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