Download mobile/modules/engine/src/services/AudioPlaybackService.ts from Anirudh1950/MentraOS: direct link, hf CLI and curl.
- Browser
- Download file 42.8 kB
-
https://huggingface.co/Anirudh1950/MentraOS/resolve/main/mobile/modules/engine/src/services/AudioPlaybackService.ts
- Command line
-
hf download hf://Anirudh1950/MentraOS/mobile/modules/engine/src/services/AudioPlaybackService.ts
-
curl -L -o AudioPlaybackService.ts https://huggingface.co/Anirudh1950/MentraOS/resolve/main/mobile/modules/engine/src/services/AudioPlaybackService.ts
42.8 kB
import {createAudioPlayer, AudioPlayer, AudioStatus, setAudioModeAsync} from "expo-audio"
import BluetoothSdk from "@mentra/bluetooth-sdk/internal"
import {BgTimer} from "../utils/timers"
import {setAudioCloudUplinkSuppressed} from "./AudioCloudUplink"
import {SILENT_AUDIO_SOURCE} from "./audioPlaybackAssets"
const RESTORE_GLASSES_VOLUME_AFTER_PLAYBACK = false
interface AudioPlayRequest {
startPositionMs?: number
startupTimeoutMs?: number
requestId: string
audioUrl: string
appId?: string
volume?: number
stopOtherAudio?: boolean
/**
* Suppress microphone audio sent to cloud STT while this audio is audible.
* Opt-in and off by default: the suppression is global to the uplink, so a
* caller that sets this silences STT for every running app, not just itself.
* No platform path sets it today (see AudioCloudUplink and OS-1741).
*/
suppressCloudUplink?: boolean
}
export type AudioPlaybackCompletionReason = "completed" | "interrupted" | "error"
type AudioPlaybackCompletion = (
requestId: string,
success: boolean,
error: string | null,
duration: number | null,
reason: AudioPlaybackCompletionReason,
) => void
interface PlaybackState {
startPositionMs: number
startupTimer: number | null
hasStarted: boolean
requestId: string
audioUrl: string
uplinkSuppressionId: string
appId?: string
startTime: number
completed: boolean // Guard against double callbacks
suppressCloudUplink: boolean
onComplete: AudioPlaybackCompletion
}
interface PendingPlaybackState {
startPositionMs: number
cancelled: boolean
audioUrl: string
appId?: string
createdAt: number
}
/** Open request for a live PCM output stream (miniapp speaker.createStream). */
export interface AudioStreamOpenRequest {
streamId: string
appId: string
/** PCM sample rate in Hz (16000 / 24000 / 48000). */
sampleRate: number
/** Channel count. v1 is mono. */
channels: number
volume?: number
/**
* Interrupt other audio when this stream starts: at open, or at the first
* write for a prewarmed stream. Default true.
*/
stopOtherAudio?: boolean
/**
* Open the native stream now but treat it as silent until its first write:
* it doesn't interrupt other audio, doesn't count as playing, and other
* audio leaves it alone.
*/
prewarm?: boolean
/**
* Audio queued before playback starts, and the playout cushion after that.
* Both platforms honor it; native defaults to 500 ms.
*/
jitterMs?: number
/**
* Called when the stream ends for any reason (drained after close, aborted,
* interrupted by other audio, or native error). `started` is false when it
* ended before any audio was written.
*/
onEnded: (
streamId: string,
success: boolean,
error: string | null,
durationMs: number | null,
started: boolean,
) => void
}
/** Cloud speech streamed from a URL straight into the native PCM player. */
export interface AudioPcmUrlRequest {
requestId: string
appId: string
/** Raw signed 16-bit little-endian mono PCM. */
url: string
sampleRate: number
volume?: number
jitterMs?: number
stopOtherAudio?: boolean
/** Same opt-in, uplink-wide mute as {@link AudioPlayRequest.suppressCloudUplink}. */
suppressCloudUplink?: boolean
}
/** One live PCM stream session, backed by native streaming audio. */
interface StreamState {
/** False until the first write (prewarmed streams only). Unstarted streams are inert. */
started: boolean
streamId: string
appId: string
startTime: number
ended: boolean // Guard against double onEnded
stopOtherAudio: boolean
/** Cloud-uplink suppression source held while this stream is audible, if any. */
uplinkSuppressionId?: string
onEnded: AudioStreamOpenRequest["onEnded"]
}
class AudioPlaybackService {
private static instance: AudioPlaybackService | null = null
// Reuse a single AudioPlayer to avoid AudioTrack exhaustion
// Creating new ExoPlayer instances per request leads to -12 ENOMEM errors
private player: AudioPlayer | null = null
// The source currently loaded into the reusable player. Keep object identity
// so a delayed tail cleanup from an older request cannot unload a newer one.
private loadedPlayback: PlaybackState | null = null
private currentPlayback: PlaybackState | null = null
// Requests that entered play() but have not yet reached the native player.
// Keeping these addressable prevents a cancelled request from starting after
// an async audio-mode or stream-cleanup step finishes.
private pendingPlaybacks = new Map<string, PendingPlaybackState>()
// Live PCM streams (speaker.createStream), keyed by streamId. The runtime
// enforces one per app; stopOtherAudio interrupts across apps.
private streams = new Map<string, StreamState>()
private audioModeConfigured: boolean = false
private preparing?: Promise<void>
// Debounce timer for notifying native that audio stopped
// Prevents mic toggle flicker when playing back-to-back audio
// Uses BgTimer to work reliably when app is backgrounded on Android
private audioStopDebounceTimer: number | null = null
// Playback that opted into suppression stays suppressed briefly while its
// A2DP tail drains. Track those completed sources so a later unsuppressed
// sound can resume listening immediately instead of inheriting that gate.
private tailUplinkSuppressions = new Set<string>()
// Original glasses media volume captured before we bump it for A2DP playback.
private glassesVolumeRestoreLevel: number | null = null
private static readonly AUDIO_STOP_DEBOUNCE_MS = 1500
private static readonly A2DP_TAIL_DRAIN_MS = 700
/** Break accidental same-app replay storms without affecting deliberate later replays. */
private static readonly DUPLICATE_PLAY_WINDOW_MS = 750
/** If glasses report step volume at or below this, bump to FLOOR before A2DP playback. */
private static readonly GLASSES_VOLUME_LOW_THRESHOLD = 2
private static readonly GLASSES_VOLUME_FLOOR = 9
private constructor() {}
/**
* Configure audio mode for playback, re-asserting it on EVERY play.
*
* This must run before each play — not just once — because other parts of the
* app put the shared iOS AVAudioSession into a record-oriented category and
* never restore it. In particular the phone mic (PhoneMic.swift) switches the
* session to `.playAndRecord` (sometimes `.voiceChat`/earpiece routing) while
* capturing and only deactivates it on stop, leaving the category sticky. A
* miniapp like the Recorder records and then immediately plays back through
* this service; if we kept the old "configure once" guard, playback would land
* in the leftover `.playAndRecord` session and be inaudible on the phone
* speaker (the symptom: the progress bar runs but no sound comes out). Calling
* setAudioModeAsync with no `allowsRecording` maps to category `.playback`,
* reclaiming the speaker route. `audioModeConfigured` is kept only to gate the
* one-time log.
*/
private async ensureAudioModeConfigured(): Promise<void> {
try {
await setAudioModeAsync({
shouldPlayInBackground: true,
playsInSilentMode: true,
interruptionMode: "duckOthers",
})
if (!this.audioModeConfigured) {
this.audioModeConfigured = true
console.log("AUDIO: Audio mode configured for playback")
}
} catch (error) {
console.error("AUDIO: Failed to configure audio mode:", error)
// Don't block playback if audio mode config fails
}
}
public static getInstance(): AudioPlaybackService {
if (!AudioPlaybackService.instance) {
AudioPlaybackService.instance = new AudioPlaybackService()
}
return AudioPlaybackService.instance
}
/**
* Configure the audio session and create the shared player ahead of the
* next sound (e.g. on a wake-word detection), without playing anything or
* acquiring the microphone.
*/
public prepareForPlayback(): Promise<void> {
if (!this.preparing)
this.preparing = this.ensureAudioModeConfigured()
.then(() => {
this.ensurePlayer()
})
.finally(() => {
this.preparing = undefined
})
return this.preparing
}
/**
* Ensure we have a reusable player instance. The default 500 ms status
* interval only paces position updates: expo-audio reports play/pause and
* completion from player events on both platforms.
*/
private ensurePlayer(): AudioPlayer {
if (!this.player) {
console.log("AUDIO: Creating reusable AudioPlayer instance")
this.player = createAudioPlayer(null)
// Add status listener once - it will handle all playback completions
this.player.addListener("playbackStatusUpdate", (status: AudioStatus) => {
this.onPlaybackStatusUpdate(status)
})
}
return this.player
}
private async getGlassesMediaVolumeWithTiming() {
const startedAt = Date.now()
try {
const result = await BluetoothSdk.getGlassesMediaVolume()
console.log(`AUDIO: Glasses media volume read completed in ${Date.now() - startedAt}ms`)
return result
} catch (error) {
console.warn(
`AUDIO: Glasses media volume read failed after ${Date.now() - startedAt}ms:`,
error instanceof Error ? error.message : String(error),
)
throw error
}
}
private async setGlassesMediaVolumeWithTiming(level: number) {
const startedAt = Date.now()
try {
const result = await BluetoothSdk.setGlassesMediaVolume(level)
console.log(`AUDIO: Glasses media volume set(${level}) completed in ${Date.now() - startedAt}ms`)
return result
} catch (error) {
console.warn(
`AUDIO: Glasses media volume set(${level}) failed after ${Date.now() - startedAt}ms:`,
error instanceof Error ? error.message : String(error),
)
throw error
}
}
/**
* Mentra Live: raise glasses media volume over BLE when it is very low so A2DP prompts are audible.
* Fail-open on unsupported devices, timeouts, or errors.
*/
private async ensureGlassesMediaVolumeForA2dp(playback: PlaybackState): Promise<void> {
if (this.glassesVolumeRestoreLevel !== null) {
console.log(
`AUDIO: Keeping bumped glasses media volume at ${AudioPlaybackService.GLASSES_VOLUME_FLOOR} during ongoing playback`,
)
return
}
try {
const raw = await this.getGlassesMediaVolumeWithTiming()
const level = Number(raw.level)
const statusCode = Number(raw.statusCode)
if (!Number.isFinite(level)) {
console.log("AUDIO: Received glasses media volume response without numeric level:", JSON.stringify(raw))
return
}
const k900S = Number.isFinite(statusCode) && statusCode >= 0 ? ` K900_S=${statusCode}` : ""
console.log(`AUDIO: Glasses media step volume (wearable knob, 0-15 scale): ${level}/15.${k900S}`)
if (level > AudioPlaybackService.GLASSES_VOLUME_LOW_THRESHOLD) {
return
}
if (this.currentPlayback !== playback || playback.completed) {
console.log("AUDIO: Skipping glasses volume bump; playback already ended")
return
}
console.log(`AUDIO: Raising glasses media volume (was ${level})`)
await this.setGlassesMediaVolumeWithTiming(AudioPlaybackService.GLASSES_VOLUME_FLOOR)
if (this.currentPlayback === playback && !playback.completed) {
this.glassesVolumeRestoreLevel = level
} else if (RESTORE_GLASSES_VOLUME_AFTER_PLAYBACK) {
console.log("AUDIO: Restoring glasses volume immediately; playback ended during volume bump")
await this.setGlassesMediaVolumeWithTiming(level)
} else {
console.log("AUDIO: Leaving bumped glasses media volume unchanged after playback")
}
} catch (e) {
const msg = e instanceof Error ? e.message : String(e)
console.warn("AUDIO: Skipping glasses volume bump:", msg)
}
}
/**
* Restore the user's original glasses media step volume after A2DP playback finishes.
*/
private async restoreGlassesMediaVolume(): Promise<void> {
const restoreLevel = this.glassesVolumeRestoreLevel
if (restoreLevel === null) {
return
}
if (!RESTORE_GLASSES_VOLUME_AFTER_PLAYBACK) {
this.glassesVolumeRestoreLevel = null
console.log("AUDIO: Leaving bumped glasses media volume unchanged after playback")
return
}
try {
console.log(`AUDIO: Restoring glasses media volume to ${restoreLevel}`)
await this.setGlassesMediaVolumeWithTiming(restoreLevel)
} catch (e) {
const msg = e instanceof Error ? e.message : String(e)
console.warn(`AUDIO: Failed to restore glasses volume to ${restoreLevel}:`, msg)
} finally {
this.glassesVolumeRestoreLevel = null
}
}
private clearStartupTimer(playback: PlaybackState): void {
if (playback.startupTimer === null) return
BgTimer.clearTimeout(playback.startupTimer)
playback.startupTimer = null
}
private unloadPlaybackSource(playback: PlaybackState, reason: string): void {
this.clearStartupTimer(playback)
if (this.loadedPlayback !== playback) return
const player = this.player
if (!player) {
this.loadedPlayback = null
return
}
try {
player.pause()
} catch (e) {
console.warn(`AUDIO: Error pausing ${playback.requestId} during ${reason}:`, e)
}
try {
// replace(null) fails native argument casting on iOS, while remove()
// destroys the ExoPlayer that Android must reuse to avoid AudioTrack
// exhaustion. Replacing with bundled silence detaches the previous media
// item on both platforms and leaves the native player alive.
player.replace(SILENT_AUDIO_SOURCE)
console.log(`AUDIO: Cleared source for ${playback.requestId} during ${reason}`)
} catch (e) {
console.warn(`AUDIO: Error clearing ${playback.requestId} during ${reason}:`, e)
} finally {
if (this.loadedPlayback === playback) this.loadedPlayback = null
}
}
private clearFinishedPlayerAfterTail(playback: PlaybackState): void {
if (playback.suppressCloudUplink) {
this.tailUplinkSuppressions.add(playback.uplinkSuppressionId)
}
BgTimer.setTimeout(() => {
// ExoPlayer's completion event precedes the final A2DP audio reaching the
// glasses. Keep cloud STT gated until that audible tail has drained, then
// accept fresh microphone audio immediately without restarting LC3.
if (playback.suppressCloudUplink && this.tailUplinkSuppressions.delete(playback.uplinkSuppressionId)) {
setAudioCloudUplinkSuppressed(playback.uplinkSuppressionId, false)
}
this.unloadPlaybackSource(playback, `${AudioPlaybackService.A2DP_TAIL_DRAIN_MS}ms A2DP tail drain`)
}, AudioPlaybackService.A2DP_TAIL_DRAIN_MS)
}
private releaseTailUplinkSuppressions(): void {
for (const sourceId of this.tailUplinkSuppressions) {
setAudioCloudUplinkSuppressed(sourceId, false)
}
this.tailUplinkSuppressions.clear()
}
private failPlayback(playback: PlaybackState, message: string): void {
if (this.currentPlayback !== playback || playback.completed) return
console.error(`AUDIO: Playback failed for ${playback.requestId}: ${message}`)
playback.completed = true
this.currentPlayback = null
if (playback.suppressCloudUplink) {
setAudioCloudUplinkSuppressed(playback.uplinkSuppressionId, false)
}
this.unloadPlaybackSource(playback, "playback failure")
this.notifyAudioStopDebounced()
void this.restoreGlassesMediaVolume()
// Unload and finish bookkeeping before the caller can start offline TTS.
playback.onComplete(playback.requestId, false, message, null, "error")
}
/**
* Play audio from a URL.
* Returns a promise that resolves with playback result when audio finishes or errors.
*/
public async play(request: AudioPlayRequest, onComplete: AudioPlaybackCompletion): Promise<void> {
const {requestId, audioUrl, appId, volume = 1.0, stopOtherAudio = true, suppressCloudUplink = false} = request
const startPositionMs = Number.isFinite(request.startPositionMs) ? Math.max(0, request.startPositionMs!) : 0
const now = Date.now()
const activeDuplicate =
this.currentPlayback &&
!this.currentPlayback.completed &&
this.currentPlayback.appId === appId &&
this.currentPlayback.audioUrl === audioUrl &&
this.currentPlayback.startPositionMs === startPositionMs &&
now - this.currentPlayback.startTime < AudioPlaybackService.DUPLICATE_PLAY_WINDOW_MS
const pendingDuplicate = [...this.pendingPlaybacks.values()].some(
(candidate) =>
!candidate.cancelled &&
candidate.appId === appId &&
candidate.audioUrl === audioUrl &&
candidate.startPositionMs === startPositionMs &&
now - candidate.createdAt < AudioPlaybackService.DUPLICATE_PLAY_WINDOW_MS,
)
if (activeDuplicate || pendingDuplicate) {
console.warn(`AUDIO: Suppressed duplicate play request ${requestId}${appId ? ` from ${appId}` : ""}`)
onComplete(requestId, false, "Duplicate audio request suppressed", 0, "error")
return
}
const pending: PendingPlaybackState = {cancelled: false, audioUrl, appId, createdAt: now, startPositionMs}
this.pendingPlaybacks.set(requestId, pending)
const loggedUrl = audioUrl.startsWith("data:")
? `${audioUrl.split(",")[0]},[bundled audio]`
: /^https?:/i.test(audioUrl)
? audioUrl.split("?")[0]
: audioUrl
console.log(`AUDIO: Play request ${requestId}${appId ? ` from ${appId}` : ""}: ${loggedUrl}`)
try {
// Ensure audio mode is configured for background playback
await this.ensureAudioModeConfigured()
if (pending.cancelled) {
onComplete(requestId, true, null, 0, "interrupted")
return
}
// URL playback reuses one native player, so replacing its source always
// interrupts the previous URL even when stopOtherAudio is false. Notify
// that request before replacing it so its promise cannot hang. The option
// still controls whether separate PCM streams are interrupted below.
if (this.currentPlayback && !this.currentPlayback.completed) {
console.log(`AUDIO: Interrupting current URL playback for new request`)
this.interruptCurrentPlayback(true)
}
// Live PCM streams count as "other audio" too.
if (stopOtherAudio) {
await this.abortAllStreams()
}
if (pending.cancelled) {
onComplete(requestId, true, null, 0, "interrupted")
return
}
if (!suppressCloudUplink) {
this.releaseTailUplinkSuppressions()
}
// Get or create the reusable player
const player = this.ensurePlayer()
// Set volume
player.volume = Math.max(0, Math.min(1, volume))
// Store the new playback state
const playback: PlaybackState = {
startPositionMs,
startupTimer: null,
hasStarted: false,
requestId,
audioUrl,
uplinkSuppressionId: `url:${requestId}`,
appId,
startTime: Date.now(),
completed: false,
suppressCloudUplink,
onComplete,
}
this.currentPlayback = playback
this.pendingPlaybacks.delete(requestId)
if (playback.suppressCloudUplink) {
setAudioCloudUplinkSuppressed(playback.uplinkSuppressionId, true)
}
// Replace the source and play
// Using replace() reuses the existing ExoPlayer/AudioTrack instead of creating new ones
if (startPositionMs > 0) player.pause()
player.replace({uri: audioUrl})
this.loadedPlayback = playback
if (startPositionMs > 0) await player.seekTo(startPositionMs / 1000)
// A newer play/stop may have replaced this request while seeking.
if (this.currentPlayback !== playback || playback.completed) return
player.play()
// Mentra Live volume reads can block up to 5s when the glasses don't
// answer. Do not put that in front of playback; guard it against late
// completion so a stale volume response cannot bump after this request is
// already over.
void this.ensureGlassesMediaVolumeForA2dp(playback)
// Notify native that our app is playing audio
// Used to suspend LC3 mic during audio playback to avoid MCU overload
// Cancel any pending "stop" notification first (handles back-to-back audio)
if (this.audioStopDebounceTimer !== null) {
BgTimer.clearTimeout(this.audioStopDebounceTimer)
this.audioStopDebounceTimer = null
}
BluetoothSdk.setOwnAppAudioPlaying(true).catch((e) => {
console.warn("AUDIO: Failed to notify native of audio start:", e)
})
console.log(`AUDIO: Requested native playback for ${requestId}`)
const startupTimeoutMs = request.startupTimeoutMs
if (
this.currentPlayback === playback &&
!playback.completed &&
!playback.hasStarted &&
startupTimeoutMs !== undefined &&
Number.isFinite(startupTimeoutMs) &&
startupTimeoutMs > 0
) {
playback.startupTimer = BgTimer.setTimeout(() => {
playback.startupTimer = null
if (playback.hasStarted) return
this.failPlayback(playback, `Playback did not start within ${startupTimeoutMs}ms`)
}, startupTimeoutMs)
}
} catch (error) {
if (suppressCloudUplink) {
setAudioCloudUplinkSuppressed(`url:${requestId}`, false)
}
const failedPlayback = this.currentPlayback?.requestId === requestId ? this.currentPlayback : null
if (failedPlayback) {
failedPlayback.completed = true
this.currentPlayback = null
this.unloadPlaybackSource(failedPlayback, "playback start failure")
}
if (pending.cancelled) {
onComplete(requestId, true, null, 0, "interrupted")
return
}
const errorMessage = error instanceof Error ? error.message : "Unknown error loading audio"
console.error(`AUDIO: Failed to play ${requestId}:`, errorMessage)
void this.restoreGlassesMediaVolume()
onComplete(requestId, false, errorMessage, null, "error")
} finally {
if (this.pendingPlaybacks.get(requestId) === pending) {
this.pendingPlaybacks.delete(requestId)
}
}
}
/**
* Cancel one URL playback without touching audio owned by another request.
* Pending requests are prevented from reaching the native player; active
* requests are paused synchronously before their completion callback fires.
*/
public cancelPlayback(requestId: string): void {
const pending = this.pendingPlaybacks.get(requestId)
if (pending) pending.cancelled = true
// Cloud speech plays as a PCM stream keyed by its request id.
if (this.streams.has(requestId)) void this.abortStream(requestId)
if (this.currentPlayback?.requestId === requestId && !this.currentPlayback.completed) {
this.interruptCurrentPlayback()
}
}
/**
* Interrupt current playback and notify its callback
*/
private interruptCurrentPlayback(preserveGlassesMediaVolume: boolean = false): void {
if (!this.currentPlayback || this.currentPlayback.completed) return
const playback = this.currentPlayback
playback.completed = true
this.currentPlayback = null
if (playback.suppressCloudUplink) {
setAudioCloudUplinkSuppressed(playback.uplinkSuppressionId, false)
}
// Pausing alone leaves the URI available to OS/Bluetooth media Play.
// Unload it unless a newer request has already replaced this source.
this.unloadPlaybackSource(playback, "interruption")
// Notify native that our app stopped playing audio (debounced)
this.notifyAudioStopDebounced()
if (!preserveGlassesMediaVolume) {
void this.restoreGlassesMediaVolume()
}
// Notify that playback was interrupted
const elapsedMs = Date.now() - playback.startTime
playback.onComplete(playback.requestId, true, null, elapsedMs, "interrupted")
console.log(`AUDIO: Interrupted ${playback.requestId} after ${elapsedMs}ms`)
}
/**
* Handle playback status updates from expo-audio
*/
private onPlaybackStatusUpdate(status: AudioStatus): void {
const playback = this.currentPlayback
// Guard against callbacks for unknown or completed playbacks
if (!playback || playback.completed) {
return
}
if (status.playing && !status.isBuffering && status.currentTime * 1000 > playback.startPositionMs) {
playback.hasStarted = true
this.clearStartupTimer(playback)
}
// Check if playback finished
if (status.didJustFinish) {
const durationMs = (status.duration || 0) * 1000 // expo-audio uses seconds
console.log(`AUDIO: Playback finished for ${playback.requestId}, duration: ${durationMs}ms`)
playback.completed = true
this.clearStartupTimer(playback)
// ExoPlayer can report finished before an A2DP sink has audibly drained
// the last buffered audio. Pausing immediately can clip the tail on
// Mentra Live; defer cleanup and guard it against a newer loaded source.
this.clearFinishedPlayerAfterTail(playback)
this.currentPlayback = null
playback.onComplete(playback.requestId, true, null, durationMs, "completed")
// Notify native that our app stopped playing audio (debounced)
this.notifyAudioStopDebounced()
void this.restoreGlassesMediaVolume()
return
}
// iOS "failed" is terminal even during startup. Android's unloaded idle
// status can also occur during source replacement, so retain its grace period.
const wentIdle = status.playbackState === "idle" && !status.isBuffering && !status.isLoaded
const elapsedMs = Date.now() - playback.startTime
if (status.playbackState === "failed" || (wentIdle && elapsedMs > 1500)) {
this.failPlayback(
playback,
wentIdle ? "Playback failed (player went idle)" : "Playback failed (native player failed)",
)
}
}
/**
* Notify native that audio stopped, with debouncing to prevent mic toggle flicker
* when playing back-to-back audio files
*/
private notifyAudioStopDebounced(): void {
// Clear any existing timer
if (this.audioStopDebounceTimer !== null) {
BgTimer.clearTimeout(this.audioStopDebounceTimer)
}
// Set a new timer - if new audio starts within this window, the timer gets cancelled
// Uses BgTimer to work reliably when app is backgrounded on Android
this.audioStopDebounceTimer = BgTimer.setTimeout(() => {
this.audioStopDebounceTimer = null
// URL playback and live PCM streams may overlap when stopOtherAudio is false.
// A URL that ends must not clear the native playback signal while another
// source is still active, or the mic watchdog can interrupt that source.
if (this.isPlaying()) return
BluetoothSdk.setOwnAppAudioPlaying(false).catch((e) => {
console.warn("AUDIO: Failed to notify native of audio stop:", e)
})
}, AudioPlaybackService.AUDIO_STOP_DEBOUNCE_MS)
}
// ── live PCM output streams (miniapp speaker.createStream) ────────────────
/**
* Open a live PCM stream session. Chunks are pushed with writeStreamChunk
* into native streaming audio (AudioTrack on Android, AVAudioEngine on iOS),
* following the media route such as A2DP to connected glasses.
*/
public async openStream(request: AudioStreamOpenRequest): Promise<void> {
const {streamId, appId, sampleRate, channels, volume = 1.0, stopOtherAudio = true, jitterMs} = request
const prewarm = request.prewarm === true
console.log(
`AUDIO: Stream open ${streamId} from ${appId}: rate=${sampleRate} ch=${channels}` +
(jitterMs === undefined ? "" : ` jitterMs=${jitterMs}`) +
(prewarm ? " prewarm" : ""),
)
await this.ensureAudioModeConfigured()
// A prewarmed stream is silent until its first write, so it interrupts
// other audio then, not now.
if (stopOtherAudio && !prewarm) await this.stopOtherAudio(streamId)
await BluetoothSdk.pcmStreamOpen(streamId, sampleRate, channels, Math.max(0, Math.min(1, volume)), jitterMs)
const stream: StreamState = {
streamId,
appId,
startTime: Date.now(),
ended: false,
started: false,
stopOtherAudio,
onEnded: request.onEnded,
}
this.streams.set(streamId, stream)
if (!prewarm) this.startStreamAudio(stream)
}
/** Interrupt URL playback and every stream that is audible, sparing `exceptStreamId`. */
private async stopOtherAudio(exceptStreamId: string): Promise<void> {
if (this.currentPlayback && !this.currentPlayback.completed) {
console.log("AUDIO: Interrupting current playback for new stream")
this.interruptCurrentPlayback(true)
}
await this.abortAllStreams(exceptStreamId)
}
private startStreamAudio(stream: StreamState, suppressCloudUplink = false): void {
if (stream.started) return
stream.started = true
if (suppressCloudUplink) {
stream.uplinkSuppressionId = `stream:${stream.streamId}`
setAudioCloudUplinkSuppressed(stream.uplinkSuppressionId, true)
} else {
this.releaseTailUplinkSuppressions()
}
// Cancel any pending "stopped" notification and mark our app as playing
// audio. Streams suppress cloud STT only when the caller opts in.
if (this.audioStopDebounceTimer !== null) {
BgTimer.clearTimeout(this.audioStopDebounceTimer)
this.audioStopDebounceTimer = null
}
BluetoothSdk.setOwnAppAudioPlaying(true).catch((e) => {
console.warn("AUDIO: Failed to notify native of stream audio start:", e)
})
// Guarded against the stream ending mid-read, same as the play() path.
void this.ensureGlassesMediaVolumeForA2dpStream(stream)
}
/**
* Append base64 PCM to an open stream. Resolves with the host-side backlog
* in ms; the native write blocks while the backlog is above the
* backpressure ceiling, so awaited writes self-throttle to realtime.
*/
public async writeStreamChunk(streamId: string, base64: string): Promise<{bufferedMs: number}> {
const stream = this.streams.get(streamId)
if (!stream || stream.ended) {
throw new Error(`stream ${streamId} is not open`)
}
const firstWrite = !stream.started
if (firstWrite) {
// A prewarmed stream becomes audible now: interrupt other audio first.
if (stream.stopOtherAudio) await this.stopOtherAudio(streamId)
if (stream.ended) throw new Error(`stream ${streamId} is not open`)
}
try {
const result = await BluetoothSdk.pcmStreamWrite(streamId, base64)
if (firstWrite) this.startStreamAudio(stream)
return result
} catch (error) {
const message = error instanceof Error ? error.message : String(error)
if (firstWrite && /not open/i.test(message)) {
// Native closed this prewarmed stream when the audio route changed
// before it played. That's an ordinary end, not a playback error: the
// caller sees "not open" and opens a fresh stream on the current route.
this.endStream(stream, true, null, 0)
throw new Error(`stream ${streamId} is not open`)
}
this.endStream(stream, false, message, Date.now() - stream.startTime)
throw error
}
}
/** Drain the backlog, then finish. Resolves with the total played duration. */
public async closeStream(streamId: string): Promise<{durationMs?: number}> {
const stream = this.streams.get(streamId)
if (!stream || stream.ended) {
throw new Error(`stream ${streamId} is not open`)
}
try {
const result = await BluetoothSdk.pcmStreamClose(streamId)
this.endStream(stream, true, null, result?.durationMs ?? Date.now() - stream.startTime)
return result ?? {}
} catch (error) {
const message = error instanceof Error ? error.message : String(error)
this.endStream(stream, false, message, Date.now() - stream.startTime)
throw error
}
}
/** Stop immediately, dropping the backlog. Safe on unknown/ended ids. */
public async abortStream(streamId: string): Promise<void> {
const stream = this.streams.get(streamId)
await BluetoothSdk.pcmStreamAbort(streamId).catch((e) => {
console.warn(`AUDIO: Stream abort ${streamId} native call failed:`, e)
})
if (stream) {
this.endStream(stream, true, null, Date.now() - stream.startTime)
}
}
/**
* Abort every audible stream (optionally sparing one about to replace them).
* Prewarmed streams that haven't received audio are not playing anything, so
* they are left alone unless `includeUnstarted` is set.
*/
private async abortAllStreams(exceptStreamId?: string, includeUnstarted = false): Promise<void> {
const streamIds = [...this.streams.values()]
.filter((stream) => stream.streamId !== exceptStreamId && (includeUnstarted || stream.started))
.map((stream) => stream.streamId)
await Promise.all(streamIds.map((streamId) => this.abortStream(streamId)))
}
/** Terminal bookkeeping for a stream: dedup, unregister, notify. */
private endStream(stream: StreamState, success: boolean, error: string | null, durationMs: number | null): void {
if (stream.ended) return
stream.ended = true
this.streams.delete(stream.streamId)
const suppressionId = stream.uplinkSuppressionId
if (suppressionId) {
// Keep the uplink muted while the A2DP tail drains, like URL playback.
this.tailUplinkSuppressions.add(suppressionId)
BgTimer.setTimeout(() => {
if (this.tailUplinkSuppressions.delete(suppressionId)) setAudioCloudUplinkSuppressed(suppressionId, false)
}, AudioPlaybackService.A2DP_TAIL_DRAIN_MS)
}
// Only signal "audio stopped" when nothing else is making noise.
if (stream.started && !this.isPlaying()) {
this.notifyAudioStopDebounced()
void this.restoreGlassesMediaVolume()
}
stream.onEnded(stream.streamId, success, error, durationMs, stream.started)
}
/**
* Stream cloud speech from `url` (raw 16-bit little-endian mono PCM) into the
* native PCM player as it downloads. The audio never crosses into
* JavaScript. `onComplete` fires once: `completed` after the whole body
* played, `interrupted` after cancelPlayback / stopForApp / other audio, or
* `error` on an HTTP or network failure (anything already played stays
* played).
*/
public async playPcmUrl(request: AudioPcmUrlRequest, onComplete: AudioPlaybackCompletion): Promise<void> {
const {requestId, appId, url, sampleRate, volume = 1.0, jitterMs, stopOtherAudio = true} = request
console.log(
`AUDIO: PCM URL ${requestId} from ${appId}: rate=${sampleRate}` +
(jitterMs === undefined ? "" : ` jitterMs=${jitterMs}`),
)
let settled = false
// Stays false unless native reports the whole body played; an abort ends the stream first.
let playedToEnd = false
const finish: AudioPlaybackCompletion = (id, success, error, duration, reason) => {
if (settled) return
settled = true
onComplete(id, success, error, duration, reason)
}
const stream: StreamState = {
streamId: requestId,
appId,
startTime: Date.now(),
ended: false,
started: false,
stopOtherAudio,
onEnded: (id, success, error, durationMs) =>
finish(id, success, error, durationMs, success ? (playedToEnd ? "completed" : "interrupted") : "error"),
}
this.streams.set(requestId, stream)
// Speech counts as playing from the request on, so stop/interrupt reach it.
this.startStreamAudio(stream, request.suppressCloudUplink === true)
try {
await this.ensureAudioModeConfigured()
if (stream.ended) return
if (stopOtherAudio) await this.stopOtherAudio(requestId)
if (stream.ended) return
const result = await BluetoothSdk.pcmStreamPlayUrl(
requestId,
url,
sampleRate,
Math.max(0, Math.min(1, volume)),
jitterMs,
)
playedToEnd = true
this.endStream(stream, true, null, result?.durationMs ?? Date.now() - stream.startTime)
} catch (error) {
// An abort already ended the stream as interrupted; ignore its rejection.
if (stream.ended) return
// Keep the native code (http_error, unsupported_format, network_error,
// "not open") so the caller can tell a refused request from a dropped one.
const code = (error as {code?: unknown} | null)?.code
const text = error instanceof Error ? error.message : String(error)
const message = typeof code === "string" && code && !text.includes(code) ? `${code}: ${text}` : text
console.warn(`AUDIO: PCM URL ${requestId} failed: ${message}`)
this.endStream(stream, false, message, Date.now() - stream.startTime)
}
}
/** Stream twin of ensureGlassesMediaVolumeForA2dp, guarded by stream.ended. */
private async ensureGlassesMediaVolumeForA2dpStream(stream: StreamState): Promise<void> {
if (this.glassesVolumeRestoreLevel !== null) return
try {
const raw = await this.getGlassesMediaVolumeWithTiming()
const level = Number(raw.level)
if (!Number.isFinite(level) || level > AudioPlaybackService.GLASSES_VOLUME_LOW_THRESHOLD) return
if (stream.ended) return
console.log(`AUDIO: Raising glasses media volume for stream (was ${level})`)
await this.setGlassesMediaVolumeWithTiming(AudioPlaybackService.GLASSES_VOLUME_FLOOR)
if (!stream.ended) {
this.glassesVolumeRestoreLevel = level
} else {
await this.setGlassesMediaVolumeWithTiming(level)
}
} catch (e) {
console.warn("AUDIO: Skipping glasses volume bump for stream:", e instanceof Error ? e.message : String(e))
}
}
/**
* Stop playback for a specific app.
* If appId is not provided, stops all playback.
*
* Prewarmed streams that haven't received audio are kept (speaker.stop() has
* nothing of theirs to stop) unless `includeUnstarted` is set, which teardown
* paths use so no native stream outlives its miniapp.
*/
public async stopForApp(appId?: string, options: {includeUnstarted?: boolean} = {}): Promise<void> {
// Settle URL playback before awaiting the native stream aborts below. A
// request still configuring audio has not reached the player yet, so the
// stop must cancel it here; and a play() issued while those aborts run is
// newer than this stop, so it must not be interrupted when they return.
this.cancelPendingPlaybacks(appId)
if (this.currentPlayback && !this.currentPlayback.completed && (!appId || this.currentPlayback.appId === appId)) {
console.log(`AUDIO: Stopping playback for app ${appId || "(all)"}`)
this.interruptCurrentPlayback()
}
// Abort this app's live PCM streams too (miniapp stop/disconnect path).
const streamIds = [...this.streams.values()]
.filter((stream) => (!appId || stream.appId === appId) && (options.includeUnstarted || stream.started))
.map((stream) => stream.streamId)
await Promise.all(streamIds.map((streamId) => this.abortStream(streamId)))
}
/**
* Stop all audio playback
*/
public async stopAll(): Promise<void> {
this.cancelPendingPlaybacks()
if (this.currentPlayback && !this.currentPlayback.completed) {
console.log("AUDIO: Stopping all playback")
this.interruptCurrentPlayback()
}
await this.abortAllStreams(undefined, true)
}
/** Keep requests that have not reached the native player from starting later. */
private cancelPendingPlaybacks(appId?: string): void {
for (const pending of this.pendingPlaybacks.values()) {
if (!appId || pending.appId === appId) pending.cancelled = true
}
}
/**
* Check if audio is currently playing
*/
public isPlaying(): boolean {
return (
(this.currentPlayback !== null && !this.currentPlayback.completed) ||
[...this.streams.values()].some((stream) => stream.started && !stream.ended)
)
}
/**
* Get current playback app IDs (all active)
*/
public getActiveAppIds(): string[] {
const appIds = new Set<string>()
if (this.currentPlayback && !this.currentPlayback.completed && this.currentPlayback.appId) {
appIds.add(this.currentPlayback.appId)
}
for (const stream of this.streams.values()) {
if (stream.started && !stream.ended) appIds.add(stream.appId)
}
return [...appIds]
}
/**
* Get number of active playbacks
*/
public getActiveCount(): number {
const audibleStreams = [...this.streams.values()].filter((stream) => stream.started).length
return (this.currentPlayback && !this.currentPlayback.completed ? 1 : 0) + audibleStreams
}
/**
* Release the player entirely (call when app is shutting down)
*/
public release(): void {
void this.abortAllStreams(undefined, true)
if (this.currentPlayback && !this.currentPlayback.completed) {
this.interruptCurrentPlayback()
} else {
void this.restoreGlassesMediaVolume()
}
if (this.player) {
try {
this.player.remove()
console.log("AUDIO: Released AudioPlayer")
} catch (error) {
console.error("AUDIO: Error releasing player:", error)
}
this.player = null
this.loadedPlayback = null
}
}
}
const audioPlaybackService = AudioPlaybackService.getInstance()
export default audioPlaybackService
import {createAudioPlayer, AudioPlayer, AudioStatus, setAudioModeAsync} from "expo-audio"
import BluetoothSdk from "@mentra/bluetooth-sdk/internal"
import {BgTimer} from "../utils/timers"
import {setAudioCloudUplinkSuppressed} from "./AudioCloudUplink"
import {SILENT_AUDIO_SOURCE} from "./audioPlaybackAssets"
const RESTORE_GLASSES_VOLUME_AFTER_PLAYBACK = false
interface AudioPlayRequest {
startPositionMs?: number
startupTimeoutMs?: number
requestId: string
audioUrl: string
appId?: string
volume?: number
stopOtherAudio?: boolean
/**
* Suppress microphone audio sent to cloud STT while this audio is audible.
* Opt-in and off by default: the suppression is global to the uplink, so a
* caller that sets this silences STT for every running app, not just itself.
* No platform path sets it today (see AudioCloudUplink and OS-1741).
*/
suppressCloudUplink?: boolean
}
export type AudioPlaybackCompletionReason = "completed" | "interrupted" | "error"
type AudioPlaybackCompletion = (
requestId: string,
success: boolean,
error: string | null,
duration: number | null,
reason: AudioPlaybackCompletionReason,
) => void
interface PlaybackState {
startPositionMs: number
startupTimer: number | null
hasStarted: boolean
requestId: string
audioUrl: string
uplinkSuppressionId: string
appId?: string
startTime: number
completed: boolean // Guard against double callbacks
suppressCloudUplink: boolean
onComplete: AudioPlaybackCompletion
}
interface PendingPlaybackState {
startPositionMs: number
cancelled: boolean
audioUrl: string
appId?: string
createdAt: number
}
/** Open request for a live PCM output stream (miniapp speaker.createStream). */
export interface AudioStreamOpenRequest {
streamId: string
appId: string
/** PCM sample rate in Hz (16000 / 24000 / 48000). */
sampleRate: number
/** Channel count. v1 is mono. */
channels: number
volume?: number
/**
* Interrupt other audio when this stream starts: at open, or at the first
* write for a prewarmed stream. Default true.
*/
stopOtherAudio?: boolean
/**
* Open the native stream now but treat it as silent until its first write:
* it doesn't interrupt other audio, doesn't count as playing, and other
* audio leaves it alone.
*/
prewarm?: boolean
/**
* Audio queued before playback starts, and the playout cushion after that.
* Both platforms honor it; native defaults to 500 ms.
*/
jitterMs?: number
/**
* Called when the stream ends for any reason (drained after close, aborted,
* interrupted by other audio, or native error). `started` is false when it
* ended before any audio was written.
*/
onEnded: (
streamId: string,
success: boolean,
error: string | null,
durationMs: number | null,
started: boolean,
) => void
}
/** Cloud speech streamed from a URL straight into the native PCM player. */
export interface AudioPcmUrlRequest {
requestId: string
appId: string