feat(voice): add output device selection - #1711
Merged
Merged
Conversation
🦋 Changeset detectedLatest commit: d7a8c82 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
agents
@cloudflare/ai-chat
@cloudflare/codemode
create-think
hono-agents
@cloudflare/shell
@cloudflare/think
@cloudflare/voice
@cloudflare/worker-bundler
commit: |
Separate non-fatal speaker routing failures from the global voice error channel so applications can keep call failures distinct from output selection warnings. This adds dedicated client and React state for output device errors, routes the example warning beside the speaker picker, and documents the new behavior. Also harden playback output setup against async races: rapid sink switches now preserve the latest requested device, concurrent playback initialization shares one media element setup, and teardown during startup no longer leaves orphaned audio routing. Expanded tests cover unsupported sink selection, out-of-order switches, setup/teardown races, and React hook propagation. Co-authored-by: Cursor <cursoragent@cursor.com>
threepointone
force-pushed
the
audio-picker
branch
from
June 12, 2026 08:49
559844c to
d7a8c82
Compare
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds output device selection for
@cloudflare/voiceplayback and wires the voice-agent example to use it. Fixes #1550.Why
Users need call audio to follow the speaker selected in their app UI instead of always playing through the system default output. The SDK previously played decoded audio directly to
AudioContext.destination, which left consumers no path to applyHTMLMediaElement.setSinkId().The implementation routes SDK playback through a hidden
HTMLAudioElement, then applies sink selection to that element. This avoids relying onAudioContext.setSinkId(), which has weaker cross-browser support, while keeping playback alive on the default or previous output when sink selection is unsupported or rejected.Public API Surface
outputDeviceId?: stringtoVoiceClientOptions, inherited byuseVoiceAgent()options:VoiceClient.setOutputDevice(outputDeviceId?: string): Promise<void>to update the active client without reconnecting the call.VoiceClient.outputDeviceErrorand anoutputdeviceerrorevent for non-fatal speaker-routing failures.outputDeviceErrortouseVoiceAgent()so React apps can render speaker-routing warnings separately from call/connection errors.undefinedand"default"as default output.Architectural Changes
Playback now flows through
AudioBufferSourceNode -> MediaStreamAudioDestinationNode -> hidden HTMLAudioElementinstead of connecting assistant audio directly toAudioContext.destination. The hidden media element becomes the output-routing boundary for browser sink selection. If that setup fails, playback falls back toAudioContext.destination.useVoiceAgent()appliesoutputDeviceIdchanges to the existingVoiceClient, so changing speakers does not reconnect the WebSocket or restart the call.Output-device failures are intentionally separated from the global
errorchannel because playback can continue even when routing to a selected speaker fails. Apps can showoutputDeviceErrornear a speaker picker while keeping globalerrorreserved for call, mic, connection, and server failures.Reliability and Browser Behavior
startCall()pre-warm and early audio chunks share one hidden media element setup.setOutputDevice()calls by preserving the latest requested device even whensetSinkId()promises resolve out of order.setSinkId()is unsupported or rejected.Code Changes
VoiceClientplayback output management and cleanup.VoiceClient.useVoiceAgent()to apply output device changes without reconnecting and exposeoutputDeviceError.@cloudflare/voice.Validation
pnpm --filter @cloudflare/voice testpnpm --filter @cloudflare/voice test:reactpnpm run buildpnpm run check