From 4f0a5d1eb49d5e7edccc289786be2e75664295f4 Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Mon, 9 Jan 2023 18:18:46 +0100 Subject: [PATCH] Fix broadcast last sequence number (#9858) --- .../models/VoiceBroadcastRecording.ts | 12 ++++++++++-- .../models/VoiceBroadcastRecording-test.ts | 10 ++++++---- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/voice-broadcast/models/VoiceBroadcastRecording.ts b/src/voice-broadcast/models/VoiceBroadcastRecording.ts index e0627731eb..a78dc8ccc3 100644 --- a/src/voice-broadcast/models/VoiceBroadcastRecording.ts +++ b/src/voice-broadcast/models/VoiceBroadcastRecording.ts @@ -60,13 +60,20 @@ export class VoiceBroadcastRecording { private state: VoiceBroadcastInfoState; private recorder: VoiceBroadcastRecorder; - private sequence = 1; private dispatcherRef: string; private chunkEvents = new VoiceBroadcastChunkEvents(); private chunkRelationHelper: RelationsHelper; private maxLength: number; private timeLeft: number; + /** + * Broadcast chunks have a sequence number to bring them in the correct order and to know if a message is missing. + * This variable holds the last sequence number. + * Starts with 0 because there is no chunk at the beginning of a broadcast. + * Will be incremented when a chunk message is created. + */ + private sequence = 0; + public constructor( public readonly infoEvent: MatrixEvent, private client: MatrixClient, @@ -268,7 +275,8 @@ export class VoiceBroadcastRecording event_id: this.infoEvent.getId(), }; content["io.element.voice_broadcast_chunk"] = { - sequence: this.sequence++, + /** Increment the last sequence number and use it for this message. Also see {@link sequence}. */ + sequence: ++this.sequence, }; await this.client.sendMessage(this.infoEvent.getRoomId(), content); diff --git a/test/voice-broadcast/models/VoiceBroadcastRecording-test.ts b/test/voice-broadcast/models/VoiceBroadcastRecording-test.ts index 21a8986bbd..58c5e7b0cd 100644 --- a/test/voice-broadcast/models/VoiceBroadcastRecording-test.ts +++ b/test/voice-broadcast/models/VoiceBroadcastRecording-test.ts @@ -254,12 +254,12 @@ describe("VoiceBroadcastRecording", () => { expect(voiceBroadcastRecording.getState()).toBe(VoiceBroadcastInfoState.Started); }); - describe("and calling stop()", () => { + describe("and calling stop", () => { beforeEach(() => { voiceBroadcastRecording.stop(); }); - itShouldSendAnInfoEvent(VoiceBroadcastInfoState.Stopped, 1); + itShouldSendAnInfoEvent(VoiceBroadcastInfoState.Stopped, 0); itShouldBeInState(VoiceBroadcastInfoState.Stopped); it("should emit a stopped state changed event", () => { @@ -351,6 +351,7 @@ describe("VoiceBroadcastRecording", () => { itShouldBeInState(VoiceBroadcastInfoState.Stopped); itShouldSendAVoiceMessage([23, 24, 25], 3, getMaxBroadcastLength(), 2); + itShouldSendAnInfoEvent(VoiceBroadcastInfoState.Stopped, 2); }); }); @@ -364,6 +365,7 @@ describe("VoiceBroadcastRecording", () => { }); itShouldSendAVoiceMessage([4, 5, 6], 3, 42, 1); + itShouldSendAnInfoEvent(VoiceBroadcastInfoState.Stopped, 1); }); describe.each([ @@ -375,7 +377,7 @@ describe("VoiceBroadcastRecording", () => { }); itShouldBeInState(VoiceBroadcastInfoState.Paused); - itShouldSendAnInfoEvent(VoiceBroadcastInfoState.Paused, 1); + itShouldSendAnInfoEvent(VoiceBroadcastInfoState.Paused, 0); it("should stop the recorder", () => { expect(mocked(voiceBroadcastRecorder.stop)).toHaveBeenCalled(); @@ -413,7 +415,7 @@ describe("VoiceBroadcastRecording", () => { }); itShouldBeInState(VoiceBroadcastInfoState.Resumed); - itShouldSendAnInfoEvent(VoiceBroadcastInfoState.Resumed, 1); + itShouldSendAnInfoEvent(VoiceBroadcastInfoState.Resumed, 0); it("should start the recorder", () => { expect(mocked(voiceBroadcastRecorder.start)).toHaveBeenCalled();