Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(voice): mark stream as ended #10455

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 33 additions & 17 deletions packages/voice/__tests__/AudioReceiveStream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,27 @@ describe('AudioReceiveStream', () => {
await wait(200);
stream.push(DUMMY_BUFFER);
expect(stream.readable).toEqual(true);
stream.push(null);
await wait(200);
expect(stream.readable).toEqual(false);
});

// TODO: Fix this test
// test('AfterSilence end behavior', async () => {
// const duration = 100;
// const increment = 20;

// const stream = new AudioReceiveStream({ end: { behavior: EndBehaviorType.AfterSilence, duration: 100 } });
// stream.resume();
test('AfterSilence end behavior', async () => {
const duration = 100;
const increment = 20;

// for (let i = increment; i < duration / 2; i += increment) {
// await stepSilence(stream, increment);
// }
const stream = new AudioReceiveStream({ end: { behavior: EndBehaviorType.AfterSilence, duration } });
stream.resume();

// stream.push(DUMMY_BUFFER);
for (let step = increment; step < duration / 2; step += increment) {
await stepSilence(stream, increment);
}

// for (let i = increment; i < duration; i += increment) {
// await stepSilence(stream, increment);
// }
stream.push(DUMMY_BUFFER);

// await wait(increment);
// expect(stream.readableEnded).toEqual(true);
// });
await wait(duration);
expect(stream.readableEnded).toEqual(true);
});

test('AfterInactivity end behavior', async () => {
const duration = 100;
Expand All @@ -71,4 +69,22 @@ describe('AudioReceiveStream', () => {

expect(stream.readableEnded).toEqual(true);
});

test('Stream ends after pushing null', async () => {
const stream = new AudioReceiveStream({ end: { behavior: EndBehaviorType.AfterInactivity, duration: 100 } });
stream.resume();

stream.push(DUMMY_BUFFER);

expect(stream.readable).toEqual(true);
expect(stream.readableEnded).toEqual(false);
expect(stream.destroyed).toEqual(false);

stream.push(null);
await wait(50);

expect(stream.readable).toEqual(false);
expect(stream.readableEnded).toEqual(true);
expect(stream.destroyed).toEqual(true);
});
});
6 changes: 6 additions & 0 deletions packages/voice/src/receive/AudioReceiveStream.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Buffer } from 'node:buffer';
import { nextTick } from 'node:process';
import { Readable, type ReadableOptions } from 'node:stream';
import { SILENCE_FRAME } from '../audio/AudioPlayer';

Expand Down Expand Up @@ -74,6 +75,11 @@ export class AudioReceiveStream extends Readable {
this.renewEndTimeout(this.end);
}

if (buffer === null) {
// null marks EOF for stream
nextTick(() => this.destroy());
vladfrangu marked this conversation as resolved.
Show resolved Hide resolved
}

return super.push(buffer);
}

Expand Down