From 55921e4888522f2b5b7851c181ba4b40651198e6 Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Fri, 25 Nov 2022 08:47:46 +0100 Subject: [PATCH] Fix voice broadcast recording (#9617) --- .../audio/VoiceBroadcastRecorder.ts | 1 + .../audio/VoiceBroadcastRecorder-test.ts | 51 ++++++++++++------- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/voice-broadcast/audio/VoiceBroadcastRecorder.ts b/src/voice-broadcast/audio/VoiceBroadcastRecorder.ts index f521ed2105..ebd9bfc737 100644 --- a/src/voice-broadcast/audio/VoiceBroadcastRecorder.ts +++ b/src/voice-broadcast/audio/VoiceBroadcastRecorder.ts @@ -80,6 +80,7 @@ export class VoiceBroadcastRecorder const chunk = this.extractChunk(); this.currentChunkLength = 0; this.previousChunkEndTimePosition = 0; + this.headers = new Uint8Array(0); return chunk; } diff --git a/test/voice-broadcast/audio/VoiceBroadcastRecorder-test.ts b/test/voice-broadcast/audio/VoiceBroadcastRecorder-test.ts index 29d96235e0..d4011613b6 100644 --- a/test/voice-broadcast/audio/VoiceBroadcastRecorder-test.ts +++ b/test/voice-broadcast/audio/VoiceBroadcastRecorder-test.ts @@ -76,8 +76,27 @@ describe("VoiceBroadcastRecorder", () => { let voiceBroadcastRecorder: VoiceBroadcastRecorder; let onChunkRecorded: (chunk: ChunkRecordedPayload) => void; - const itShouldNotEmitAChunkRecordedEvent = () => { - it("should not emit a ChunkRecorded event", () => { + const simulateFirstChunk = (): void => { + voiceRecording.onDataAvailable(headers1); + voiceRecording.onDataAvailable(headers2); + // set recorder seconds to something greater than the test chunk length of 30 + // @ts-ignore + voiceRecording.recorderSeconds = 42; + voiceRecording.onDataAvailable(chunk1); + }; + + const expectOnFirstChunkRecorded = (): void => { + expect(onChunkRecorded).toHaveBeenNthCalledWith( + 1, + { + buffer: concat(headers1, headers2, chunk1), + length: 42, + }, + ); + }; + + const itShouldNotEmitAChunkRecordedEvent = (): void => { + it("should not emit a ChunkRecorded event", (): void => { expect(voiceRecording.emit).not.toHaveBeenCalledWith( VoiceBroadcastRecorderEvent.ChunkRecorded, expect.anything(), @@ -163,7 +182,7 @@ describe("VoiceBroadcastRecorder", () => { itShouldNotEmitAChunkRecordedEvent(); - describe("stop", () => { + describe("and calling stop", () => { let stopPayload: ChunkRecordedPayload; beforeEach(async () => { @@ -176,18 +195,22 @@ describe("VoiceBroadcastRecorder", () => { length: 23, }); }); + + describe("and calling start again and receiving some data", () => { + beforeEach(() => { + simulateFirstChunk(); + }); + + it("should emit the ChunkRecorded event for the first chunk", () => { + expectOnFirstChunkRecorded(); + }); + }); }); }); describe("when some chunks have been received", () => { beforeEach(() => { - // simulate first chunk - voiceRecording.onDataAvailable(headers1); - voiceRecording.onDataAvailable(headers2); - // set recorder seconds to something greater than the test chunk length of 30 - // @ts-ignore - voiceRecording.recorderSeconds = 42; - voiceRecording.onDataAvailable(chunk1); + simulateFirstChunk(); // simulate a second chunk voiceRecording.onDataAvailable(chunk2a); @@ -198,13 +221,7 @@ describe("VoiceBroadcastRecorder", () => { }); it("should emit ChunkRecorded events", () => { - expect(onChunkRecorded).toHaveBeenNthCalledWith( - 1, - { - buffer: concat(headers1, headers2, chunk1), - length: 42, - }, - ); + expectOnFirstChunkRecorded(); expect(onChunkRecorded).toHaveBeenNthCalledWith( 2,