MentraOS / mobile /modules /engine /src /services /__tests__ /AudioPlaybackService.test.ts
Anirudh1950's picture
Add files using upload-large-folder tool
8a29801 verified
Raw History Blame Contribute Delete
35.3 kB
/// <reference types="bun-types" />
import {afterEach, beforeEach, describe, expect, mock, spyOn, test} from "bun:test"
import {
emitLc3Frame,
pcmStreamAbort,
pcmStreamClose,
pcmStreamOpen,
pcmStreamPlayUrl,
pcmStreamWrite,
resetAudioTestMocks,
sendAudioFrame,
setOwnAppAudioPlaying,
} from "./audioTestMocks"
import {reactNative, reactNativeAppState} from "./reactNativeTestMock"
const setAudioModeAsync = mock(async () => {})
const tailTimerCallbacks: Array<() => void> = []
const startupTimerCallbacks = new Map<number, () => void>()
let nextStartupTimerId = 2
const silentAudioSource = 9001
const audioPlayer = {
addListener: mock(() => ({remove: () => {}})),
pause: mock(() => {}),
play: mock(() => {}),
remove: mock(() => {}),
replace: mock(() => {}),
seekTo: mock(async (_seconds: number) => {}),
volume: 1,
}
mock.module("expo-audio", () => ({
createAudioPlayer: () => audioPlayer,
setAudioModeAsync,
}))
mock.module("../audioPlaybackAssets", () => ({
SILENT_AUDIO_SOURCE: silentAudioSource,
}))
reactNative.Platform = {OS: "android"}
reactNativeAppState.currentState = "active"
mock.module("../../utils/timers", () => ({
BgTimer: {
clearTimeout: (timerId: number) => {
startupTimerCallbacks.delete(timerId)
},
setTimeout: (callback: () => void, delayMs: number) => {
if (delayMs === 5000) {
const timerId = nextStartupTimerId++
startupTimerCallbacks.set(timerId, callback)
return timerId
}
if (delayMs === 700) {
tailTimerCallbacks.push(callback)
return 1
}
callback()
return 1
},
},
}))
const {startAudioCloudUplink, stopAudioCloudUplink} = require("../AudioCloudUplink")
const audioPlaybackService = require("../AudioPlaybackService").default
describe("AudioPlaybackService live PCM streams", () => {
beforeEach(async () => {
await audioPlaybackService.stopAll()
stopAudioCloudUplink()
resetAudioTestMocks()
reactNativeAppState.currentState = "active"
tailTimerCallbacks.length = 0
startupTimerCallbacks.clear()
startAudioCloudUplink()
setAudioModeAsync.mockClear()
setAudioModeAsync.mockImplementation(async () => {})
audioPlayer.pause.mockClear()
audioPlayer.play.mockClear()
audioPlayer.remove.mockClear()
audioPlayer.replace.mockClear()
audioPlayer.seekTo.mockClear()
audioPlayer.seekTo.mockImplementation(async () => {})
})
afterEach(async () => {
await audioPlaybackService.stopAll()
stopAudioCloudUplink()
})
test("a prepared PCM cue is silent and only marks playback active on its first write", async () => {
await audioPlaybackService.openStream({
streamId: "prepared-pcm",
appId: "cue-app",
sampleRate: 48000,
channels: 1,
volume: 0.7,
jitterMs: 80,
prewarm: true,
stopOtherAudio: false,
onEnded: mock(() => {}),
})
expect(audioPlaybackService.isPlaying()).toBe(false)
expect(audioPlaybackService.getActiveAppIds()).toEqual([])
expect(setOwnAppAudioPlaying).not.toHaveBeenCalled()
expect(pcmStreamWrite).not.toHaveBeenCalled()
await audioPlaybackService.writeStreamChunk("prepared-pcm", "AAAA")
expect(audioPlaybackService.isPlaying()).toBe(true)
expect(setOwnAppAudioPlaying).toHaveBeenCalledWith(true)
await audioPlaybackService.abortStream("prepared-pcm")
})
test("stalled cloud playback unloads its source before reporting one startup error", async () => {
const completed = mock(() => {
expect(audioPlayer.replace).toHaveBeenLastCalledWith(silentAudioSource)
expect(audioPlaybackService.isPlaying()).toBe(false)
})
await audioPlaybackService.play(
{requestId: "stalled", audioUrl: "https://example.test/stalled", startupTimeoutMs: 5000},
completed,
)
audioPlaybackService.onPlaybackStatusUpdate({playing: true, isBuffering: true, currentTime: 0})
audioPlaybackService.onPlaybackStatusUpdate({playing: true, isBuffering: false, currentTime: 0})
expect(startupTimerCallbacks.size).toBe(1)
const timeout = [...startupTimerCallbacks.values()][0]
timeout()
timeout()
expect(completed).toHaveBeenCalledTimes(1)
expect(completed).toHaveBeenCalledWith("stalled", false, "Playback did not start within 5000ms", null, "error")
expect(setOwnAppAudioPlaying).toHaveBeenLastCalledWith(false)
})
test("audible progress clears the startup deadline without timing out a long answer", async () => {
const completed = mock(() => {})
await audioPlaybackService.play(
{requestId: "started", audioUrl: "https://example.test/started", startupTimeoutMs: 5000},
completed,
)
const timeout = [...startupTimerCallbacks.values()][0]
audioPlaybackService.onPlaybackStatusUpdate({playing: true, isBuffering: false, currentTime: 0.5})
expect(startupTimerCallbacks.size).toBe(0)
timeout()
expect(completed).not.toHaveBeenCalled()
expect(audioPlaybackService.isPlaying()).toBe(true)
})
test("cancelling stalled speech clears its deadline and a stale timeout cannot stop new audio", async () => {
const completed = mock(() => {})
await audioPlaybackService.play(
{requestId: "cancelled", audioUrl: "https://example.test/cancelled", startupTimeoutMs: 5000},
completed,
)
const timeout = [...startupTimerCallbacks.values()][0]
audioPlaybackService.cancelPlayback("cancelled")
expect(startupTimerCallbacks.size).toBe(0)
await audioPlaybackService.play({requestId: "new", audioUrl: "file://new.wav"}, () => {})
timeout()
expect(completed).toHaveBeenCalledTimes(1)
expect(completed).toHaveBeenCalledWith("cancelled", true, null, expect.any(Number), "interrupted")
expect(audioPlayer.replace).toHaveBeenLastCalledWith({uri: "file://new.wav"})
expect(audioPlaybackService.isPlaying()).toBe(true)
})
test("ordinary audio does not receive a speech startup deadline", async () => {
await audioPlaybackService.play({requestId: "ordinary", audioUrl: "https://example.test/music"}, () => {})
expect(startupTimerCallbacks.size).toBe(0)
})
test("native completion clears the startup deadline before draining the audio tail", async () => {
const completed = mock(() => {})
await audioPlaybackService.play(
{requestId: "finished", audioUrl: "https://example.test/finished", startupTimeoutMs: 5000},
completed,
)
const timeout = [...startupTimerCallbacks.values()][0]
audioPlaybackService.onPlaybackStatusUpdate({didJustFinish: true, duration: 0.1})
expect(startupTimerCallbacks.size).toBe(0)
timeout()
expect(completed).toHaveBeenCalledTimes(1)
tailTimerCallbacks.shift()?.()
expect(completed).toHaveBeenCalledTimes(1)
expect(completed).toHaveBeenCalledWith("finished", true, null, 100, "completed")
})
test("native failure clears the startup deadline and cannot report twice", async () => {
const completed = mock(() => {})
await audioPlaybackService.play(
{requestId: "failed", audioUrl: "https://example.test/failed", startupTimeoutMs: 5000},
completed,
)
const timeout = [...startupTimerCallbacks.values()][0]
audioPlaybackService.onPlaybackStatusUpdate({playbackState: "failed"})
expect(startupTimerCallbacks.size).toBe(0)
timeout()
expect(completed).toHaveBeenCalledTimes(1)
expect(completed).toHaveBeenCalledWith("failed", false, "Playback failed (native player failed)", null, "error")
})
test("seeks before playing and permits a new position in the same recording", async () => {
await audioPlaybackService.play(
{requestId: "seek-one", audioUrl: "file://seek.wav", startPositionMs: 5000},
() => {},
)
expect(audioPlayer.seekTo).toHaveBeenLastCalledWith(5)
await audioPlaybackService.play(
{requestId: "seek-two", audioUrl: "file://seek.wav", startPositionMs: 9000},
() => {},
)
expect(audioPlayer.seekTo).toHaveBeenLastCalledWith(9)
expect(audioPlayer.play).toHaveBeenCalledTimes(2)
})
test("a stopped pending seek cannot restart playback", async () => {
let completeSeek!: () => void
audioPlayer.seekTo.mockImplementation(
() =>
new Promise<void>((resolve) => {
completeSeek = resolve
}),
)
const playing = audioPlaybackService.play(
{requestId: "seek-stop", audioUrl: "file://seek-stop.wav", startPositionMs: 1000},
() => {},
)
while (!completeSeek) await Promise.resolve()
await audioPlaybackService.stopAll()
completeSeek()
await playing
expect(audioPlayer.play).not.toHaveBeenCalled()
})
test("plays a cold Android URL without a silent PCM warmup", async () => {
await audioPlaybackService.play({audioUrl: "https://example.test/click.wav", requestId: "click"}, () => {})
expect(pcmStreamOpen).not.toHaveBeenCalled()
expect(pcmStreamWrite).not.toHaveBeenCalled()
expect(pcmStreamAbort).not.toHaveBeenCalled()
expect(pcmStreamClose).not.toHaveBeenCalled()
expect(audioPlayer.play).toHaveBeenCalledTimes(1)
})
test("plays a background URL without a silent PCM warmup", async () => {
reactNativeAppState.currentState = "background"
await audioPlaybackService.play(
{audioUrl: "https://example.test/background.wav", requestId: "background"},
() => {},
)
audioPlaybackService.cancelPlayback("background")
expect(pcmStreamOpen).not.toHaveBeenCalled()
expect(pcmStreamWrite).not.toHaveBeenCalled()
expect(pcmStreamAbort).not.toHaveBeenCalled()
expect(audioPlayer.play).toHaveBeenCalledTimes(1)
})
test("opens a cold live PCM stream without a silent warmup", async () => {
const coldNow = Date.now() + 10_000
const dateNow = spyOn(Date, "now").mockReturnValue(coldNow)
try {
await audioPlaybackService.openStream({
appId: "com.example.call",
channels: 1,
onEnded: () => {},
sampleRate: 24_000,
streamId: "stream-cold",
volume: 0.75,
})
} finally {
dateNow.mockRestore()
}
expect(pcmStreamOpen).toHaveBeenCalledTimes(1)
expect(pcmStreamWrite).not.toHaveBeenCalled()
expect(pcmStreamAbort).not.toHaveBeenCalled()
expect(pcmStreamOpen).toHaveBeenNthCalledWith(1, "stream-cold", 24_000, 1, 0.75, undefined)
})
test("forwards a requested jitter budget to native", async () => {
await audioPlaybackService.openStream({
appId: "com.example.call",
channels: 1,
jitterMs: 120,
onEnded: () => {},
sampleRate: 16_000,
streamId: "stream-realtime",
})
expect(pcmStreamOpen).toHaveBeenCalledWith("stream-realtime", 16_000, 1, 1, 120)
})
test("opens, writes, drains, and reports active stream state", async () => {
const onEnded = mock(() => {})
await audioPlaybackService.openStream({
appId: "com.example.call",
channels: 1,
onEnded,
sampleRate: 24_000,
streamId: "stream-1",
volume: 0.75,
})
expect(pcmStreamOpen).toHaveBeenCalledWith("stream-1", 24_000, 1, 0.75, undefined)
emitLc3Frame([1])
expect(sendAudioFrame).toHaveBeenCalledTimes(1)
expect(audioPlaybackService.isPlaying()).toBe(true)
expect(audioPlaybackService.getActiveAppIds()).toEqual(["com.example.call"])
expect(audioPlaybackService.getActiveCount()).toBe(1)
await expect(audioPlaybackService.writeStreamChunk("stream-1", "AAAA")).resolves.toEqual({bufferedMs: 120})
await expect(audioPlaybackService.closeStream("stream-1")).resolves.toEqual({durationMs: 1_500})
expect(pcmStreamWrite).toHaveBeenCalledWith("stream-1", "AAAA")
expect(pcmStreamClose).toHaveBeenCalledWith("stream-1")
expect(onEnded).toHaveBeenCalledWith("stream-1", true, null, 1_500, true)
emitLc3Frame([2])
expect(sendAudioFrame).toHaveBeenCalledTimes(2)
expect(audioPlaybackService.isPlaying()).toBe(false)
})
test("opening stopOtherAudio stream aborts the previous stream exactly once", async () => {
const firstEnded = mock(() => {})
await audioPlaybackService.openStream({
appId: "app-one",
channels: 1,
onEnded: firstEnded,
sampleRate: 16_000,
streamId: "first",
})
await audioPlaybackService.openStream({
appId: "app-two",
channels: 1,
onEnded: () => {},
sampleRate: 16_000,
stopOtherAudio: true,
streamId: "second",
})
expect(pcmStreamAbort).toHaveBeenCalledWith("first")
expect(firstEnded).toHaveBeenCalledTimes(1)
expect(audioPlaybackService.getActiveAppIds()).toEqual(["app-two"])
})
test("a native write failure terminates bookkeeping and surfaces the error", async () => {
const onEnded = mock(() => {})
await audioPlaybackService.openStream({
appId: "com.example.call",
channels: 1,
onEnded,
sampleRate: 16_000,
streamId: "broken",
})
pcmStreamWrite.mockImplementationOnce(async () => {
throw new Error("native output failed")
})
await expect(audioPlaybackService.writeStreamChunk("broken", "AAAA")).rejects.toThrow("native output failed")
expect(onEnded).toHaveBeenCalledWith("broken", false, "native output failed", expect.any(Number), true)
expect(audioPlaybackService.getActiveCount()).toBe(0)
})
test("stopForApp leaves another miniapp's stream running", async () => {
await audioPlaybackService.openStream({
appId: "app-one",
channels: 1,
onEnded: () => {},
sampleRate: 16_000,
stopOtherAudio: false,
streamId: "one",
})
await audioPlaybackService.openStream({
appId: "app-two",
channels: 1,
onEnded: () => {},
sampleRate: 16_000,
stopOtherAudio: false,
streamId: "two",
})
await audioPlaybackService.stopForApp("app-one")
expect(pcmStreamAbort).toHaveBeenCalledWith("one")
expect(pcmStreamAbort).not.toHaveBeenCalledWith("two")
expect(audioPlaybackService.getActiveAppIds()).toEqual(["app-two"])
})
test("replacing URL playback reports an explicit interruption", async () => {
const firstComplete = mock(() => {})
await audioPlaybackService.play(
{requestId: "first-url", audioUrl: "file://first.wav", appId: "app-one", stopOtherAudio: false},
firstComplete,
)
await audioPlaybackService.play(
{requestId: "second-url", audioUrl: "file://second.wav", appId: "app-two", stopOtherAudio: false},
() => {},
)
expect(firstComplete).toHaveBeenCalledTimes(1)
expect(firstComplete).toHaveBeenCalledWith("first-url", true, null, expect.any(Number), "interrupted")
expect(audioPlayer.replace.mock.calls).toEqual([
[{uri: "file://first.wav"}],
[silentAudioSource],
[{uri: "file://second.wav"}],
])
expect(audioPlayer.remove).not.toHaveBeenCalled()
})
test("suppresses a rapid duplicate from the same miniapp before it can create an interruption storm", async () => {
const firstComplete = mock(() => {})
const duplicateComplete = mock(() => {})
await audioPlaybackService.play(
{requestId: "processing-one", audioUrl: "file://processing.wav", appId: "com.mentra.ai"},
firstComplete,
)
await audioPlaybackService.play(
{requestId: "processing-two", audioUrl: "file://processing.wav", appId: "com.mentra.ai"},
duplicateComplete,
)
expect(firstComplete).not.toHaveBeenCalled()
expect(duplicateComplete).toHaveBeenCalledWith(
"processing-two",
false,
"Duplicate audio request suppressed",
0,
"error",
)
expect(audioPlayer.replace).toHaveBeenCalledTimes(1)
expect(audioPlayer.remove).not.toHaveBeenCalled()
})
test("keeps native audio marked active when URL playback finishes beside a live PCM stream", async () => {
await audioPlaybackService.openStream({
appId: "stream-app",
channels: 1,
onEnded: () => {},
sampleRate: 16_000,
stopOtherAudio: false,
streamId: "ongoing-stream",
})
await audioPlaybackService.play(
{requestId: "short-url", audioUrl: "file://short.wav", appId: "url-app", stopOtherAudio: false},
() => {},
)
setOwnAppAudioPlaying.mockClear()
const playbackStatusTarget = audioPlaybackService as unknown as {
onPlaybackStatusUpdate(status: {didJustFinish: boolean; duration: number}): void
}
playbackStatusTarget.onPlaybackStatusUpdate({didJustFinish: true, duration: 1})
expect(audioPlaybackService.getActiveAppIds()).toEqual(["stream-app"])
expect(setOwnAppAudioPlaying).not.toHaveBeenCalledWith(false)
await audioPlaybackService.closeStream("ongoing-stream")
expect(setOwnAppAudioPlaying).toHaveBeenCalledWith(false)
})
test("only suppresses cloud STT when the caller opts in", async () => {
await audioPlaybackService.play(
{requestId: "sound-effect", audioUrl: "file://click.wav", appId: "app-one"},
() => {},
)
emitLc3Frame([1])
expect(sendAudioFrame).toHaveBeenCalledTimes(1)
audioPlaybackService.cancelPlayback("sound-effect")
await audioPlaybackService.play(
{
requestId: "spoken-tts",
audioUrl: "file://speech.wav",
appId: "app-one",
suppressCloudUplink: true,
},
() => {},
)
emitLc3Frame([2])
expect(sendAudioFrame).toHaveBeenCalledTimes(1)
audioPlaybackService.cancelPlayback("spoken-tts")
emitLc3Frame([3])
expect(sendAudioFrame).toHaveBeenCalledTimes(2)
})
test("releases a completed TTS tail gate when a live PCM cue starts", async () => {
const onComplete = mock(() => {})
await audioPlaybackService.play(
{
requestId: "spoken-tts",
audioUrl: "file://speech.wav",
appId: "app-one",
suppressCloudUplink: true,
},
onComplete,
)
const playbackStatusTarget = audioPlaybackService as unknown as {
onPlaybackStatusUpdate(status: {didJustFinish: boolean; duration: number}): void
}
playbackStatusTarget.onPlaybackStatusUpdate({didJustFinish: true, duration: 1})
expect(onComplete).toHaveBeenCalledWith("spoken-tts", true, null, 1_000, "completed")
emitLc3Frame([1])
expect(sendAudioFrame).toHaveBeenCalledTimes(0)
await audioPlaybackService.openStream({
appId: "app-one",
channels: 1,
onEnded: () => {},
sampleRate: 16_000,
streamId: "cue-stream",
})
emitLc3Frame([2])
expect(sendAudioFrame).toHaveBeenCalledTimes(1)
for (const callback of tailTimerCallbacks.splice(0)) callback()
})
test("unloads a completed URL after its A2DP tail so media play cannot replay it", async () => {
await audioPlaybackService.play(
{requestId: "finished-tts", audioUrl: "file://speech.wav", appId: "app-one"},
() => {},
)
const playbackStatusTarget = audioPlaybackService as unknown as {
onPlaybackStatusUpdate(status: {didJustFinish: boolean; duration: number}): void
}
playbackStatusTarget.onPlaybackStatusUpdate({didJustFinish: true, duration: 1})
expect(audioPlayer.replace).toHaveBeenLastCalledWith({uri: "file://speech.wav"})
for (const callback of tailTimerCallbacks.splice(0)) callback()
expect(audioPlayer.replace).toHaveBeenLastCalledWith(silentAudioSource)
expect(audioPlayer.remove).not.toHaveBeenCalled()
})
test("does not let an older tail timer unload a newer completed URL", async () => {
const playbackStatusTarget = audioPlaybackService as unknown as {
onPlaybackStatusUpdate(status: {didJustFinish: boolean; duration: number}): void
}
await audioPlaybackService.play({requestId: "first-tts", audioUrl: "file://first.wav"}, () => {})
playbackStatusTarget.onPlaybackStatusUpdate({didJustFinish: true, duration: 1})
await audioPlaybackService.play({requestId: "second-tts", audioUrl: "file://second.wav"}, () => {})
playbackStatusTarget.onPlaybackStatusUpdate({didJustFinish: true, duration: 1})
const firstTail = tailTimerCallbacks.shift()
firstTail?.()
expect(audioPlayer.replace).toHaveBeenLastCalledWith({uri: "file://second.wav"})
const secondTail = tailTimerCallbacks.shift()
secondTail?.()
expect(audioPlayer.replace).toHaveBeenLastCalledWith(silentAudioSource)
expect(audioPlayer.remove).not.toHaveBeenCalled()
})
test("cancels active URL playback immediately by request id", async () => {
const onComplete = mock(() => {})
await audioPlaybackService.play(
{requestId: "active-tts", audioUrl: "file://speech.wav", appId: "app-one"},
onComplete,
)
audioPlaybackService.cancelPlayback("active-tts")
expect(audioPlayer.pause).toHaveBeenCalledTimes(1)
expect(audioPlayer.replace).toHaveBeenLastCalledWith(silentAudioSource)
expect(audioPlayer.remove).not.toHaveBeenCalled()
expect(onComplete).toHaveBeenCalledWith("active-tts", true, null, expect.any(Number), "interrupted")
expect(audioPlaybackService.isPlaying()).toBe(false)
})
test("unloads URL playback when its owning miniapp stops", async () => {
await audioPlaybackService.play(
{requestId: "stopped-tts", audioUrl: "file://speech.wav", appId: "app-one"},
() => {},
)
await audioPlaybackService.stopForApp("app-one")
expect(audioPlayer.replace).toHaveBeenLastCalledWith(silentAudioSource)
expect(audioPlayer.remove).not.toHaveBeenCalled()
expect(audioPlaybackService.isPlaying()).toBe(false)
})
test("unloads a source when native playback start fails", async () => {
const onComplete = mock(() => {})
audioPlayer.play.mockImplementationOnce(() => {
throw new Error("native start failed")
})
await audioPlaybackService.play(
{requestId: "failed-tts", audioUrl: "file://speech.wav", appId: "app-one"},
onComplete,
)
expect(audioPlayer.replace).toHaveBeenLastCalledWith(silentAudioSource)
expect(audioPlayer.remove).not.toHaveBeenCalled()
expect(onComplete).toHaveBeenCalledWith("failed-tts", false, "native start failed", null, "error")
expect(audioPlaybackService.isPlaying()).toBe(false)
})
test("unloads a source when the native player falls idle with an error", async () => {
const onComplete = mock(() => {})
await audioPlaybackService.play(
{requestId: "idle-tts", audioUrl: "file://speech.wav", appId: "app-one"},
onComplete,
)
const dateNow = spyOn(Date, "now").mockReturnValue(Date.now() + 2_000)
try {
const playbackStatusTarget = audioPlaybackService as unknown as {
onPlaybackStatusUpdate(status: {
didJustFinish: boolean
duration: number
isBuffering: boolean
isLoaded: boolean
playbackState: string
}): void
}
playbackStatusTarget.onPlaybackStatusUpdate({
didJustFinish: false,
duration: 0,
isBuffering: false,
isLoaded: false,
playbackState: "idle",
})
} finally {
dateNow.mockRestore()
}
expect(audioPlayer.replace).toHaveBeenLastCalledWith(silentAudioSource)
expect(audioPlayer.remove).not.toHaveBeenCalled()
expect(onComplete).toHaveBeenCalledWith("idle-tts", false, "Playback failed (player went idle)", null, "error")
expect(audioPlaybackService.isPlaying()).toBe(false)
})
test("prevents a cancelled pending URL request from starting", async () => {
let finishAudioSetup: (() => void) | undefined
setAudioModeAsync.mockImplementationOnce(
() =>
new Promise<void>((resolve) => {
finishAudioSetup = resolve
}),
)
const onComplete = mock(() => {})
const playPromise = audioPlaybackService.play(
{requestId: "pending-tts", audioUrl: "file://speech.wav", appId: "app-one"},
onComplete,
)
audioPlaybackService.cancelPlayback("pending-tts")
finishAudioSetup?.()
await playPromise
expect(audioPlayer.replace).not.toHaveBeenCalled()
expect(audioPlayer.play).not.toHaveBeenCalled()
expect(onComplete).toHaveBeenCalledWith("pending-tts", true, null, 0, "interrupted")
expect(audioPlaybackService.isPlaying()).toBe(false)
})
test("stopForApp prevents that app's still-starting URL from playing", async () => {
let finishAudioSetup: (() => void) | undefined
setAudioModeAsync.mockImplementationOnce(
() =>
new Promise<void>((resolve) => {
finishAudioSetup = resolve
}),
)
const onComplete = mock(() => {})
const playPromise = audioPlaybackService.play(
{requestId: "starting-cue", audioUrl: "file://cue.mp3", appId: "app-one"},
onComplete,
)
await audioPlaybackService.stopForApp("app-one")
finishAudioSetup?.()
await playPromise
expect(audioPlayer.replace).not.toHaveBeenCalled()
expect(audioPlayer.play).not.toHaveBeenCalled()
expect(onComplete).toHaveBeenCalledWith("starting-cue", true, null, 0, "interrupted")
})
test("stopForApp leaves another app's still-starting URL alone", async () => {
let finishAudioSetup: (() => void) | undefined
setAudioModeAsync.mockImplementationOnce(
() =>
new Promise<void>((resolve) => {
finishAudioSetup = resolve
}),
)
const playPromise = audioPlaybackService.play(
{requestId: "other-app-cue", audioUrl: "file://other.mp3", appId: "app-two"},
() => {},
)
await audioPlaybackService.stopForApp("app-one")
finishAudioSetup?.()
await playPromise
expect(audioPlayer.play).toHaveBeenCalledTimes(1)
})
test("a URL requested while stopForApp aborts streams is not stopped by it", async () => {
await audioPlaybackService.openStream({
appId: "app-one",
channels: 1,
onEnded: () => {},
sampleRate: 16_000,
streamId: "answer-stream",
})
let finishStreamAbort: (() => void) | undefined
pcmStreamAbort.mockImplementationOnce(
() =>
new Promise<void>((resolve) => {
finishStreamAbort = resolve
}),
)
const onComplete = mock(() => {})
const stopping = audioPlaybackService.stopForApp("app-one")
await audioPlaybackService.play(
{requestId: "after-stop", audioUrl: "file://after-stop.mp3", appId: "app-one", stopOtherAudio: false},
onComplete,
)
finishStreamAbort?.()
await stopping
expect(audioPlayer.play).toHaveBeenCalledTimes(1)
expect(onComplete).not.toHaveBeenCalled()
expect(audioPlaybackService.isPlaying()).toBe(true)
})
test("prepares playback without starting sound or suppressing the microphone", async () => {
await Promise.all([audioPlaybackService.prepareForPlayback(), audioPlaybackService.prepareForPlayback()])
expect(audioPlayer.play).not.toHaveBeenCalled()
expect(audioPlayer.replace).not.toHaveBeenCalled()
expect(setOwnAppAudioPlaying).not.toHaveBeenCalled()
expect(audioPlaybackService.isPlaying()).toBe(false)
})
test("a prewarmed stream survives other audio and speaker.stop() until it plays", async () => {
const prewarmEnded = mock(() => {})
await audioPlaybackService.openStream({
streamId: "cue",
appId: "voice-app",
sampleRate: 48000,
channels: 1,
jitterMs: 80,
prewarm: true,
onEnded: prewarmEnded,
})
// Another app's sound and this app's own stop leave the idle stream alone.
await audioPlaybackService.openStream({
streamId: "other",
appId: "other-app",
sampleRate: 16000,
channels: 1,
onEnded: () => {},
})
await audioPlaybackService.play({requestId: "thinking", appId: "voice-app", audioUrl: "file://think.mp3"}, () => {})
await audioPlaybackService.stopForApp("voice-app")
expect(pcmStreamAbort).not.toHaveBeenCalledWith("cue")
expect(prewarmEnded).not.toHaveBeenCalled()
// Its first write makes it audible and, by default, stops other audio then.
await audioPlaybackService.openStream({
streamId: "other",
appId: "other-app",
sampleRate: 16000,
channels: 1,
stopOtherAudio: false,
onEnded: () => {},
})
pcmStreamAbort.mockClear()
await audioPlaybackService.writeStreamChunk("cue", "AAAA")
expect(pcmStreamAbort).toHaveBeenCalledWith("other")
expect(audioPlaybackService.getActiveAppIds()).toEqual(["voice-app"])
await audioPlaybackService.abortStream("cue")
expect(prewarmEnded).toHaveBeenCalledWith("cue", true, null, expect.any(Number), true)
})
test("a prewarmed stream closed by a route change ends quietly and reports not open", async () => {
const onEnded = mock(() => {})
await audioPlaybackService.openStream({
streamId: "stale",
appId: "voice-app",
sampleRate: 48000,
channels: 1,
prewarm: true,
onEnded,
})
pcmStreamWrite.mockImplementationOnce(async () => {
throw new Error("pcm stream stale is not open")
})
await expect(audioPlaybackService.writeStreamChunk("stale", "AAAA")).rejects.toThrow("not open")
expect(onEnded).toHaveBeenCalledWith("stale", true, null, 0, false)
expect(setOwnAppAudioPlaying).not.toHaveBeenCalledWith(true)
expect(audioPlaybackService.isPlaying()).toBe(false)
})
test("teardown includes prewarmed streams", async () => {
await audioPlaybackService.openStream({
streamId: "orphan",
appId: "voice-app",
sampleRate: 48000,
channels: 1,
prewarm: true,
onEnded: () => {},
})
await audioPlaybackService.stopForApp("voice-app", {includeUnstarted: true})
expect(pcmStreamAbort).toHaveBeenCalledWith("orphan")
})
})
describe("AudioPlaybackService cloud speech from a PCM URL", () => {
beforeEach(async () => {
await audioPlaybackService.stopAll()
resetAudioTestMocks()
pcmStreamPlayUrl.mockClear()
pcmStreamPlayUrl.mockImplementation(async () => ({durationMs: 2_000}))
})
afterEach(async () => {
await audioPlaybackService.stopAll()
})
test("plays through the native URL stream and reports completion", async () => {
const onComplete = mock(() => {})
await audioPlaybackService.playPcmUrl(
{
requestId: "tts-1",
appId: "app",
url: "https://runtime.test/tts",
sampleRate: 24000,
volume: 0.5,
jitterMs: 250,
},
onComplete,
)
expect(pcmStreamPlayUrl).toHaveBeenCalledWith("tts-1", "https://runtime.test/tts", 24000, 0.5, 250)
expect(setOwnAppAudioPlaying).toHaveBeenCalledWith(true)
expect(onComplete).toHaveBeenCalledWith("tts-1", true, null, 2_000, "completed")
expect(audioPlaybackService.isPlaying()).toBe(false)
})
test("counts as playing while it streams, and cancelPlayback interrupts it", async () => {
let reject!: (error: Error) => void
pcmStreamPlayUrl.mockImplementationOnce(
() =>
new Promise((_resolve, rejectPlay) => {
reject = rejectPlay
}),
)
const onComplete = mock(() => {})
const playing = audioPlaybackService.playPcmUrl(
{requestId: "tts-2", appId: "app", url: "https://runtime.test/tts", sampleRate: 24000},
onComplete,
)
await new Promise((resolve) => setTimeout(resolve, 0))
expect(audioPlaybackService.isPlaying()).toBe(true)
expect(audioPlaybackService.getActiveAppIds()).toEqual(["app"])
audioPlaybackService.cancelPlayback("tts-2")
await new Promise((resolve) => setTimeout(resolve, 0))
expect(pcmStreamAbort).toHaveBeenCalledWith("tts-2")
reject(new Error("aborted"))
await playing
expect(onComplete).toHaveBeenCalledTimes(1)
expect(onComplete).toHaveBeenCalledWith("tts-2", true, null, expect.any(Number), "interrupted")
})
test("a dropped connection ends the stream with an error", async () => {
pcmStreamPlayUrl.mockImplementationOnce(async () => {
throw new Error("The network connection was lost.")
})
const onComplete = mock(() => {})
await audioPlaybackService.playPcmUrl(
{requestId: "tts-3", appId: "app", url: "https://runtime.test/tts", sampleRate: 24000},
onComplete,
)
expect(onComplete).toHaveBeenCalledWith(
"tts-3",
false,
"The network connection was lost.",
expect.any(Number),
"error",
)
expect(audioPlaybackService.isPlaying()).toBe(false)
})
test("keeps the native error code in the reported error", async () => {
pcmStreamPlayUrl.mockImplementationOnce(async () => {
throw Object.assign(new Error("HTTP 401"), {code: "http_error"})
})
const onComplete = mock(() => {})
await audioPlaybackService.playPcmUrl(
{requestId: "tts-6", appId: "app", url: "https://runtime.test/tts", sampleRate: 24000},
onComplete,
)
expect(onComplete).toHaveBeenCalledWith("tts-6", false, "http_error: HTTP 401", expect.any(Number), "error")
})
test("other audio interrupts it like any stream", async () => {
pcmStreamPlayUrl.mockImplementationOnce(() => new Promise(() => {}))
const onComplete = mock(() => {})
void audioPlaybackService.playPcmUrl(
{requestId: "tts-4", appId: "app", url: "https://runtime.test/tts", sampleRate: 24000},
onComplete,
)
await new Promise((resolve) => setTimeout(resolve, 0))
await audioPlaybackService.play({requestId: "chime", appId: "other", audioUrl: "file://chime.wav"}, () => {})
expect(onComplete).toHaveBeenCalledWith("tts-4", true, null, expect.any(Number), "interrupted")
})
test("the opt-in uplink mute holds while it plays and releases after the tail", async () => {
startAudioCloudUplink()
let finish!: () => void
pcmStreamPlayUrl.mockImplementationOnce(
() =>
new Promise((resolve) => {
finish = () => resolve({durationMs: 1_000})
}),
)
const playing = audioPlaybackService.playPcmUrl(
{requestId: "tts-5", appId: "app", url: "https://runtime.test/tts", sampleRate: 24000, suppressCloudUplink: true},
() => {},
)
await new Promise((resolve) => setTimeout(resolve, 0))
emitLc3Frame([1])
expect(sendAudioFrame).not.toHaveBeenCalled()
finish()
await playing
for (const callback of tailTimerCallbacks.splice(0)) callback()
emitLc3Frame([2])
expect(sendAudioFrame).toHaveBeenCalledTimes(1)
stopAudioCloudUplink()
})
})