2022-10-14 12:13:17 +08:00
|
|
|
/*
|
2024-09-09 21:57:16 +08:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-10-14 12:13:17 +08:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 21:57:16 +08:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-10-14 12:13:17 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
import { mocked } from "jest-mock";
|
2024-10-15 00:11:58 +08:00
|
|
|
import { screen } from "jest-matrix-react";
|
2023-01-02 20:21:33 +08:00
|
|
|
import userEvent from "@testing-library/user-event";
|
2023-02-01 20:32:49 +08:00
|
|
|
import { MatrixClient, MatrixEvent, MatrixEventEvent, Room } from "matrix-js-sdk/src/matrix";
|
2023-02-07 17:14:28 +08:00
|
|
|
import { defer } from "matrix-js-sdk/src/utils";
|
2022-10-14 12:13:17 +08:00
|
|
|
|
2024-10-15 21:57:26 +08:00
|
|
|
import { Playback, PlaybackState } from "../../../../src/audio/Playback";
|
|
|
|
import { PlaybackManager } from "../../../../src/audio/PlaybackManager";
|
|
|
|
import { SdkContextClass } from "../../../../src/contexts/SDKContext";
|
|
|
|
import { MediaEventHelper } from "../../../../src/utils/MediaEventHelper";
|
2022-10-14 12:13:17 +08:00
|
|
|
import {
|
2022-10-17 22:31:22 +08:00
|
|
|
VoiceBroadcastInfoState,
|
2022-11-16 23:13:59 +08:00
|
|
|
VoiceBroadcastLiveness,
|
2022-10-14 12:13:17 +08:00
|
|
|
VoiceBroadcastPlayback,
|
|
|
|
VoiceBroadcastPlaybackEvent,
|
|
|
|
VoiceBroadcastPlaybackState,
|
2023-01-02 20:21:33 +08:00
|
|
|
VoiceBroadcastRecording,
|
2024-10-15 21:57:26 +08:00
|
|
|
} from "../../../../src/voice-broadcast";
|
2023-03-25 03:39:24 +08:00
|
|
|
import {
|
|
|
|
filterConsole,
|
|
|
|
flushPromises,
|
|
|
|
flushPromisesWithFakeTimers,
|
|
|
|
stubClient,
|
|
|
|
waitEnoughCyclesForModal,
|
2024-10-15 21:57:26 +08:00
|
|
|
} from "../../../test-utils";
|
|
|
|
import { createTestPlayback } from "../../../test-utils/audio";
|
2022-11-07 22:19:49 +08:00
|
|
|
import { mkVoiceBroadcastChunkEvent, mkVoiceBroadcastInfoStateEvent } from "../utils/test-utils";
|
2024-10-15 21:57:26 +08:00
|
|
|
import { LazyValue } from "../../../../src/utils/LazyValue";
|
2022-10-14 22:48:54 +08:00
|
|
|
|
2024-10-15 21:57:26 +08:00
|
|
|
jest.mock("../../../../src/utils/MediaEventHelper", () => ({
|
2022-10-14 22:48:54 +08:00
|
|
|
MediaEventHelper: jest.fn(),
|
|
|
|
}));
|
2022-10-14 12:13:17 +08:00
|
|
|
|
|
|
|
describe("VoiceBroadcastPlayback", () => {
|
|
|
|
const userId = "@user:example.com";
|
2022-11-07 22:19:49 +08:00
|
|
|
let deviceId: string;
|
2022-10-14 12:13:17 +08:00
|
|
|
const roomId = "!room:example.com";
|
2022-12-17 02:23:29 +08:00
|
|
|
let room: Room;
|
2022-10-14 22:48:54 +08:00
|
|
|
let client: MatrixClient;
|
2022-10-14 12:13:17 +08:00
|
|
|
let infoEvent: MatrixEvent;
|
|
|
|
let playback: VoiceBroadcastPlayback;
|
|
|
|
let onStateChanged: (state: VoiceBroadcastPlaybackState) => void;
|
2022-10-14 22:48:54 +08:00
|
|
|
let chunk1Event: MatrixEvent;
|
2023-02-07 17:14:28 +08:00
|
|
|
let deplayedChunk1Event: MatrixEvent;
|
2022-10-14 22:48:54 +08:00
|
|
|
let chunk2Event: MatrixEvent;
|
2022-11-09 19:17:54 +08:00
|
|
|
let chunk2BEvent: MatrixEvent;
|
2022-10-17 23:35:13 +08:00
|
|
|
let chunk3Event: MatrixEvent;
|
2022-11-04 18:50:19 +08:00
|
|
|
const chunk1Length = 2300;
|
|
|
|
const chunk2Length = 4200;
|
|
|
|
const chunk3Length = 6900;
|
2022-10-14 22:48:54 +08:00
|
|
|
const chunk1Data = new ArrayBuffer(2);
|
|
|
|
const chunk2Data = new ArrayBuffer(3);
|
2022-10-17 23:35:13 +08:00
|
|
|
const chunk3Data = new ArrayBuffer(3);
|
2023-02-07 17:14:28 +08:00
|
|
|
let delayedChunk1Helper: MediaEventHelper;
|
2022-10-14 22:48:54 +08:00
|
|
|
let chunk1Helper: MediaEventHelper;
|
|
|
|
let chunk2Helper: MediaEventHelper;
|
2022-10-17 23:35:13 +08:00
|
|
|
let chunk3Helper: MediaEventHelper;
|
2022-10-14 22:48:54 +08:00
|
|
|
let chunk1Playback: Playback;
|
|
|
|
let chunk2Playback: Playback;
|
2022-10-17 23:35:13 +08:00
|
|
|
let chunk3Playback: Playback;
|
2023-01-24 00:32:04 +08:00
|
|
|
let middleOfSecondChunk!: number;
|
|
|
|
let middleOfThirdChunk!: number;
|
2022-10-14 12:13:17 +08:00
|
|
|
|
2023-01-02 20:21:33 +08:00
|
|
|
const queryConfirmListeningDialog = () => {
|
|
|
|
return screen.queryByText(
|
|
|
|
"If you start listening to this live broadcast, your current live broadcast recording will be ended.",
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-10-14 12:13:17 +08:00
|
|
|
const itShouldSetTheStateTo = (state: VoiceBroadcastPlaybackState) => {
|
|
|
|
it(`should set the state to ${state}`, () => {
|
|
|
|
expect(playback.getState()).toBe(state);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const itShouldEmitAStateChangedEvent = (state: VoiceBroadcastPlaybackState) => {
|
|
|
|
it(`should emit a ${state} state changed event`, () => {
|
2022-10-24 22:06:58 +08:00
|
|
|
expect(mocked(onStateChanged)).toHaveBeenCalledWith(state, playback);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-11-16 23:13:59 +08:00
|
|
|
const itShouldHaveLiveness = (liveness: VoiceBroadcastLiveness): void => {
|
|
|
|
it(`should have liveness ${liveness}`, () => {
|
|
|
|
expect(playback.getLiveness()).toBe(liveness);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-10-24 22:06:58 +08:00
|
|
|
const startPlayback = () => {
|
2023-02-07 17:14:28 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
playback.start();
|
2022-10-24 22:06:58 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const pausePlayback = () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
playback.pause();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const stopPlayback = () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
playback.stop();
|
2022-10-14 12:13:17 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-10-14 22:48:54 +08:00
|
|
|
const mkChunkHelper = (data: ArrayBuffer): MediaEventHelper => {
|
|
|
|
return {
|
|
|
|
sourceBlob: {
|
2022-11-07 22:19:49 +08:00
|
|
|
cachedValue: new Blob(),
|
2022-10-14 22:48:54 +08:00
|
|
|
done: false,
|
|
|
|
value: {
|
|
|
|
// @ts-ignore
|
|
|
|
arrayBuffer: jest.fn().mockResolvedValue(data),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-02-07 17:14:28 +08:00
|
|
|
const mkDeplayedChunkHelper = (data: ArrayBuffer): MediaEventHelper => {
|
|
|
|
const deferred = defer<LazyValue<Blob>>();
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
deferred.resolve({
|
|
|
|
// @ts-ignore
|
|
|
|
arrayBuffer: jest.fn().mockResolvedValue(data),
|
|
|
|
});
|
|
|
|
}, 7500);
|
|
|
|
|
|
|
|
return {
|
|
|
|
sourceBlob: {
|
|
|
|
cachedValue: new Blob(),
|
|
|
|
done: false,
|
|
|
|
// @ts-ignore
|
|
|
|
value: deferred.promise,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const simulateFirstChunkArrived = async (): Promise<void> => {
|
|
|
|
jest.advanceTimersByTime(10000);
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
};
|
|
|
|
|
2022-10-17 22:31:22 +08:00
|
|
|
const mkInfoEvent = (state: VoiceBroadcastInfoState) => {
|
2022-11-07 22:19:49 +08:00
|
|
|
return mkVoiceBroadcastInfoStateEvent(roomId, state, userId, deviceId);
|
2022-10-17 22:31:22 +08:00
|
|
|
};
|
|
|
|
|
2023-02-07 17:14:28 +08:00
|
|
|
const mkPlayback = async (fakeTimers = false): Promise<VoiceBroadcastPlayback> => {
|
2023-01-02 20:21:33 +08:00
|
|
|
const playback = new VoiceBroadcastPlayback(
|
|
|
|
infoEvent,
|
|
|
|
client,
|
|
|
|
SdkContextClass.instance.voiceBroadcastRecordingsStore,
|
|
|
|
);
|
2022-10-17 22:31:22 +08:00
|
|
|
jest.spyOn(playback, "removeAllListeners");
|
2022-11-29 01:45:50 +08:00
|
|
|
jest.spyOn(playback, "destroy");
|
2022-10-17 22:31:22 +08:00
|
|
|
playback.on(VoiceBroadcastPlaybackEvent.StateChanged, onStateChanged);
|
2024-10-16 23:43:07 +08:00
|
|
|
if (fakeTimers) {
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
} else {
|
|
|
|
await flushPromises();
|
|
|
|
}
|
2022-10-17 22:31:22 +08:00
|
|
|
return playback;
|
|
|
|
};
|
|
|
|
|
|
|
|
const setUpChunkEvents = (chunkEvents: MatrixEvent[]) => {
|
2022-11-07 22:19:49 +08:00
|
|
|
mocked(client.relations).mockResolvedValueOnce({
|
|
|
|
events: chunkEvents,
|
|
|
|
});
|
2022-10-17 22:31:22 +08:00
|
|
|
};
|
|
|
|
|
2022-12-17 02:23:29 +08:00
|
|
|
const createChunkEvents = () => {
|
|
|
|
chunk1Event = mkVoiceBroadcastChunkEvent(infoEvent.getId()!, userId, roomId, chunk1Length, 1);
|
2023-02-07 17:14:28 +08:00
|
|
|
deplayedChunk1Event = mkVoiceBroadcastChunkEvent(infoEvent.getId()!, userId, roomId, chunk1Length, 1);
|
2022-12-17 02:23:29 +08:00
|
|
|
chunk2Event = mkVoiceBroadcastChunkEvent(infoEvent.getId()!, userId, roomId, chunk2Length, 2);
|
2022-11-09 19:17:54 +08:00
|
|
|
chunk2Event.setTxnId("tx-id-1");
|
2022-12-17 02:23:29 +08:00
|
|
|
chunk2BEvent = mkVoiceBroadcastChunkEvent(infoEvent.getId()!, userId, roomId, chunk2Length, 2);
|
2022-11-09 19:17:54 +08:00
|
|
|
chunk2BEvent.setTxnId("tx-id-1");
|
2022-12-17 02:23:29 +08:00
|
|
|
chunk3Event = mkVoiceBroadcastChunkEvent(infoEvent.getId()!, userId, roomId, chunk3Length, 3);
|
2022-10-14 22:48:54 +08:00
|
|
|
|
|
|
|
chunk1Helper = mkChunkHelper(chunk1Data);
|
2023-02-07 17:14:28 +08:00
|
|
|
delayedChunk1Helper = mkDeplayedChunkHelper(chunk1Data);
|
2022-10-14 22:48:54 +08:00
|
|
|
chunk2Helper = mkChunkHelper(chunk2Data);
|
2022-10-17 23:35:13 +08:00
|
|
|
chunk3Helper = mkChunkHelper(chunk3Data);
|
2022-10-14 22:48:54 +08:00
|
|
|
|
|
|
|
chunk1Playback = createTestPlayback();
|
|
|
|
chunk2Playback = createTestPlayback();
|
2022-10-17 23:35:13 +08:00
|
|
|
chunk3Playback = createTestPlayback();
|
2022-10-14 22:48:54 +08:00
|
|
|
|
2023-01-24 00:32:04 +08:00
|
|
|
middleOfSecondChunk = (chunk1Length + chunk2Length / 2) / 1000;
|
|
|
|
middleOfThirdChunk = (chunk1Length + chunk2Length + chunk3Length / 2) / 1000;
|
|
|
|
|
2022-10-14 22:48:54 +08:00
|
|
|
jest.spyOn(PlaybackManager.instance, "createPlaybackInstance").mockImplementation(
|
|
|
|
(buffer: ArrayBuffer, _waveForm?: number[]) => {
|
|
|
|
if (buffer === chunk1Data) return chunk1Playback;
|
|
|
|
if (buffer === chunk2Data) return chunk2Playback;
|
2022-10-17 23:35:13 +08:00
|
|
|
if (buffer === chunk3Data) return chunk3Playback;
|
2022-11-07 22:19:49 +08:00
|
|
|
|
|
|
|
throw new Error("unexpected buffer");
|
2022-10-14 22:48:54 +08:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-11-04 18:48:08 +08:00
|
|
|
mocked(MediaEventHelper).mockImplementation((event: MatrixEvent): any => {
|
2022-10-14 22:48:54 +08:00
|
|
|
if (event === chunk1Event) return chunk1Helper;
|
2023-02-07 17:14:28 +08:00
|
|
|
if (event === deplayedChunk1Event) return delayedChunk1Helper;
|
2022-10-14 22:48:54 +08:00
|
|
|
if (event === chunk2Event) return chunk2Helper;
|
2022-10-17 23:35:13 +08:00
|
|
|
if (event === chunk3Event) return chunk3Helper;
|
2022-10-14 22:48:54 +08:00
|
|
|
});
|
2022-12-17 02:23:29 +08:00
|
|
|
};
|
2022-10-14 12:13:17 +08:00
|
|
|
|
2023-01-24 18:20:26 +08:00
|
|
|
filterConsole(
|
|
|
|
// expected for some tests
|
|
|
|
"Unable to load broadcast playback",
|
|
|
|
);
|
2023-01-24 00:32:04 +08:00
|
|
|
|
2022-10-14 12:13:17 +08:00
|
|
|
beforeEach(() => {
|
2022-12-17 02:23:29 +08:00
|
|
|
client = stubClient();
|
|
|
|
deviceId = client.getDeviceId() || "";
|
|
|
|
room = new Room(roomId, client, client.getSafeUserId());
|
|
|
|
mocked(client.getRoom).mockImplementation((roomId: string): Room | null => {
|
|
|
|
if (roomId === room.roomId) return room;
|
|
|
|
return null;
|
|
|
|
});
|
2022-10-14 12:13:17 +08:00
|
|
|
onStateChanged = jest.fn();
|
|
|
|
});
|
|
|
|
|
2023-01-02 20:21:33 +08:00
|
|
|
afterEach(async () => {
|
|
|
|
SdkContextClass.instance.voiceBroadcastPlaybacksStore.getCurrent()?.stop();
|
|
|
|
SdkContextClass.instance.voiceBroadcastPlaybacksStore.clearCurrent();
|
|
|
|
await SdkContextClass.instance.voiceBroadcastRecordingsStore.getCurrent()?.stop();
|
|
|
|
SdkContextClass.instance.voiceBroadcastRecordingsStore.clearCurrent();
|
2022-11-07 22:19:49 +08:00
|
|
|
playback.destroy();
|
|
|
|
});
|
|
|
|
|
2022-10-26 20:54:44 +08:00
|
|
|
describe(`when there is a ${VoiceBroadcastInfoState.Resumed} broadcast without chunks yet`, () => {
|
2022-11-07 22:19:49 +08:00
|
|
|
beforeEach(async () => {
|
|
|
|
infoEvent = mkInfoEvent(VoiceBroadcastInfoState.Resumed);
|
2022-12-17 02:23:29 +08:00
|
|
|
createChunkEvents();
|
|
|
|
room.addLiveEvents([infoEvent]);
|
2022-11-07 22:19:49 +08:00
|
|
|
playback = await mkPlayback();
|
2022-10-17 23:35:13 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("and calling start", () => {
|
2022-10-24 22:06:58 +08:00
|
|
|
startPlayback();
|
2022-10-17 23:35:13 +08:00
|
|
|
|
2022-12-16 17:55:54 +08:00
|
|
|
itShouldHaveLiveness("live");
|
2022-11-16 23:13:59 +08:00
|
|
|
|
2022-10-17 23:35:13 +08:00
|
|
|
it("should be in buffering state", () => {
|
|
|
|
expect(playback.getState()).toBe(VoiceBroadcastPlaybackState.Buffering);
|
|
|
|
});
|
|
|
|
|
2022-11-04 18:50:19 +08:00
|
|
|
it("should have duration 0", () => {
|
|
|
|
expect(playback.durationSeconds).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should be at time 0", () => {
|
|
|
|
expect(playback.timeSeconds).toBe(0);
|
|
|
|
});
|
|
|
|
|
2022-10-24 22:06:58 +08:00
|
|
|
describe("and calling stop", () => {
|
|
|
|
stopPlayback();
|
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Stopped);
|
|
|
|
|
|
|
|
describe("and calling pause", () => {
|
|
|
|
pausePlayback();
|
|
|
|
// stopped voice broadcasts cannot be paused
|
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Stopped);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and calling pause", () => {
|
|
|
|
pausePlayback();
|
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Paused);
|
|
|
|
});
|
|
|
|
|
2022-10-17 23:35:13 +08:00
|
|
|
describe("and receiving the first chunk", () => {
|
|
|
|
beforeEach(() => {
|
2022-12-17 02:23:29 +08:00
|
|
|
room.relations.aggregateChildEvent(chunk1Event);
|
2022-10-17 23:35:13 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Playing);
|
2022-11-16 23:13:59 +08:00
|
|
|
itShouldHaveLiveness("live");
|
2022-10-17 23:35:13 +08:00
|
|
|
|
2022-11-04 18:50:19 +08:00
|
|
|
it("should update the duration", () => {
|
|
|
|
expect(playback.durationSeconds).toBe(2.3);
|
|
|
|
});
|
|
|
|
|
2022-10-17 23:35:13 +08:00
|
|
|
it("should play the first chunk", () => {
|
|
|
|
expect(chunk1Playback.play).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2023-02-01 20:32:49 +08:00
|
|
|
|
|
|
|
describe("and receiving the first undecryptable chunk", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.spyOn(chunk1Event, "isDecryptionFailure").mockReturnValue(true);
|
|
|
|
room.relations.aggregateChildEvent(chunk1Event);
|
|
|
|
});
|
|
|
|
|
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Error);
|
|
|
|
|
|
|
|
it("should not update the duration", () => {
|
|
|
|
expect(playback.durationSeconds).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and the chunk is decrypted", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(chunk1Event.isDecryptionFailure).mockReturnValue(false);
|
|
|
|
chunk1Event.emit(MatrixEventEvent.Decrypted, chunk1Event);
|
|
|
|
});
|
|
|
|
|
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Paused);
|
|
|
|
|
|
|
|
it("should not update the duration", () => {
|
|
|
|
expect(playback.durationSeconds).toBe(2.3);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-10-17 23:35:13 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-10-26 20:54:44 +08:00
|
|
|
describe(`when there is a ${VoiceBroadcastInfoState.Resumed} voice broadcast with some chunks`, () => {
|
2022-11-07 22:19:49 +08:00
|
|
|
beforeEach(async () => {
|
|
|
|
mocked(client.relations).mockResolvedValueOnce({ events: [] });
|
|
|
|
infoEvent = mkInfoEvent(VoiceBroadcastInfoState.Resumed);
|
2022-12-17 02:23:29 +08:00
|
|
|
createChunkEvents();
|
|
|
|
setUpChunkEvents([chunk2Event, chunk1Event]);
|
|
|
|
room.addLiveEvents([infoEvent, chunk1Event, chunk2Event]);
|
|
|
|
room.relations.aggregateChildEvent(chunk2Event);
|
|
|
|
room.relations.aggregateChildEvent(chunk1Event);
|
2022-11-07 22:19:49 +08:00
|
|
|
playback = await mkPlayback();
|
2022-10-14 12:13:17 +08:00
|
|
|
});
|
|
|
|
|
2022-11-09 19:17:54 +08:00
|
|
|
it("durationSeconds should have the length of the known chunks", () => {
|
|
|
|
expect(playback.durationSeconds).toEqual(6.5);
|
|
|
|
});
|
|
|
|
|
2023-01-24 18:20:26 +08:00
|
|
|
describe("and starting a playback with a broken chunk", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
mocked(chunk2Playback.prepare).mockRejectedValue("Error decoding chunk");
|
|
|
|
await playback.start();
|
|
|
|
});
|
|
|
|
|
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Error);
|
|
|
|
|
|
|
|
it("start() should keep it in the error state)", async () => {
|
|
|
|
await playback.start();
|
|
|
|
expect(playback.getState()).toBe(VoiceBroadcastPlaybackState.Error);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("stop() should keep it in the error state)", () => {
|
|
|
|
playback.stop();
|
|
|
|
expect(playback.getState()).toBe(VoiceBroadcastPlaybackState.Error);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("toggle() should keep it in the error state)", async () => {
|
|
|
|
await playback.toggle();
|
|
|
|
expect(playback.getState()).toBe(VoiceBroadcastPlaybackState.Error);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("pause() should keep it in the error state)", () => {
|
|
|
|
playback.pause();
|
|
|
|
expect(playback.getState()).toBe(VoiceBroadcastPlaybackState.Error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-11-09 19:17:54 +08:00
|
|
|
describe("and an event with the same transaction Id occurs", () => {
|
|
|
|
beforeEach(() => {
|
2022-12-17 02:23:29 +08:00
|
|
|
room.addLiveEvents([chunk2BEvent]);
|
|
|
|
room.relations.aggregateChildEvent(chunk2BEvent);
|
2022-11-09 19:17:54 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("durationSeconds should not change", () => {
|
|
|
|
expect(playback.durationSeconds).toEqual(6.5);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-10-17 22:31:22 +08:00
|
|
|
describe("and calling start", () => {
|
2022-10-24 22:06:58 +08:00
|
|
|
startPlayback();
|
2022-10-14 12:13:17 +08:00
|
|
|
|
2022-10-17 22:31:22 +08:00
|
|
|
it("should play the last chunk", () => {
|
2022-11-29 01:45:50 +08:00
|
|
|
expect(playback.getState()).toBe(VoiceBroadcastPlaybackState.Playing);
|
2022-10-17 23:35:13 +08:00
|
|
|
// assert that the last chunk is played first
|
2022-10-17 22:31:22 +08:00
|
|
|
expect(chunk2Playback.play).toHaveBeenCalled();
|
|
|
|
expect(chunk1Playback.play).not.toHaveBeenCalled();
|
|
|
|
});
|
2022-10-17 23:35:13 +08:00
|
|
|
|
2022-12-28 17:32:49 +08:00
|
|
|
describe(
|
|
|
|
"and receiving a stop info event with last_chunk_sequence = 2 and " +
|
|
|
|
"the playback of the last available chunk ends",
|
|
|
|
() => {
|
|
|
|
beforeEach(() => {
|
|
|
|
const stoppedEvent = mkVoiceBroadcastInfoStateEvent(
|
|
|
|
roomId,
|
|
|
|
VoiceBroadcastInfoState.Stopped,
|
|
|
|
client.getSafeUserId(),
|
|
|
|
client.deviceId!,
|
|
|
|
infoEvent,
|
|
|
|
2,
|
|
|
|
);
|
|
|
|
room.addLiveEvents([stoppedEvent]);
|
|
|
|
room.relations.aggregateChildEvent(stoppedEvent);
|
|
|
|
chunk2Playback.emit(PlaybackState.Stopped);
|
|
|
|
});
|
2022-10-17 23:35:13 +08:00
|
|
|
|
2022-12-28 17:32:49 +08:00
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Stopped);
|
|
|
|
},
|
|
|
|
);
|
2022-10-17 23:35:13 +08:00
|
|
|
|
2022-12-28 17:32:49 +08:00
|
|
|
describe(
|
|
|
|
"and receiving a stop info event with last_chunk_sequence = 3 and " +
|
|
|
|
"the playback of the last available chunk ends",
|
|
|
|
() => {
|
2022-10-17 23:35:13 +08:00
|
|
|
beforeEach(() => {
|
2022-12-28 17:32:49 +08:00
|
|
|
const stoppedEvent = mkVoiceBroadcastInfoStateEvent(
|
|
|
|
roomId,
|
|
|
|
VoiceBroadcastInfoState.Stopped,
|
|
|
|
client.getSafeUserId(),
|
|
|
|
client.deviceId!,
|
|
|
|
infoEvent,
|
|
|
|
3,
|
|
|
|
);
|
|
|
|
room.addLiveEvents([stoppedEvent]);
|
|
|
|
room.relations.aggregateChildEvent(stoppedEvent);
|
|
|
|
chunk2Playback.emit(PlaybackState.Stopped);
|
2022-10-17 23:35:13 +08:00
|
|
|
});
|
|
|
|
|
2022-12-28 17:32:49 +08:00
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Buffering);
|
2022-10-17 23:35:13 +08:00
|
|
|
|
2022-12-28 17:32:49 +08:00
|
|
|
describe("and the next chunk arrives", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
room.addLiveEvents([chunk3Event]);
|
|
|
|
room.relations.aggregateChildEvent(chunk3Event);
|
|
|
|
});
|
|
|
|
|
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Playing);
|
|
|
|
|
|
|
|
it("should play the next chunk", () => {
|
|
|
|
expect(chunk3Playback.play).toHaveBeenCalled();
|
|
|
|
});
|
2022-10-17 23:35:13 +08:00
|
|
|
});
|
2022-12-28 17:32:49 +08:00
|
|
|
},
|
|
|
|
);
|
2022-11-29 01:45:50 +08:00
|
|
|
|
|
|
|
describe("and the info event is deleted", () => {
|
|
|
|
beforeEach(() => {
|
2024-03-25 20:21:02 +08:00
|
|
|
infoEvent.makeRedacted(new MatrixEvent({}), room);
|
2022-11-29 01:45:50 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should stop and destroy the playback", () => {
|
|
|
|
expect(playback.getState()).toBe(VoiceBroadcastPlaybackState.Stopped);
|
|
|
|
expect(playback.destroy).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2022-10-14 12:13:17 +08:00
|
|
|
});
|
2023-01-02 20:21:33 +08:00
|
|
|
|
|
|
|
describe("and currently recording a broadcast", () => {
|
|
|
|
let recording: VoiceBroadcastRecording;
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
recording = new VoiceBroadcastRecording(
|
|
|
|
mkVoiceBroadcastInfoStateEvent(
|
|
|
|
roomId,
|
|
|
|
VoiceBroadcastInfoState.Started,
|
|
|
|
client.getSafeUserId(),
|
|
|
|
client.deviceId,
|
|
|
|
),
|
|
|
|
client,
|
|
|
|
);
|
|
|
|
jest.spyOn(recording, "stop");
|
|
|
|
SdkContextClass.instance.voiceBroadcastRecordingsStore.setCurrent(recording);
|
|
|
|
playback.start();
|
2023-03-25 03:39:24 +08:00
|
|
|
await waitEnoughCyclesForModal();
|
2023-01-02 20:21:33 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should display a confirm modal", () => {
|
|
|
|
expect(queryConfirmListeningDialog()).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when confirming the dialog", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await userEvent.click(screen.getByText("Yes, end my recording"));
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should stop the recording", () => {
|
|
|
|
expect(recording.stop).toHaveBeenCalled();
|
|
|
|
expect(SdkContextClass.instance.voiceBroadcastRecordingsStore.getCurrent()).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not start the playback", () => {
|
|
|
|
expect(playback.getState()).toBe(VoiceBroadcastPlaybackState.Playing);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when not confirming the dialog", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await userEvent.click(screen.getByText("No"));
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not stop the recording", () => {
|
|
|
|
expect(recording.stop).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should start the playback", () => {
|
|
|
|
expect(playback.getState()).toBe(VoiceBroadcastPlaybackState.Stopped);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-10-14 12:13:17 +08:00
|
|
|
});
|
|
|
|
|
2022-10-17 22:31:22 +08:00
|
|
|
describe("when there is a stopped voice broadcast", () => {
|
2022-11-07 22:19:49 +08:00
|
|
|
beforeEach(async () => {
|
2023-02-07 17:14:28 +08:00
|
|
|
jest.useFakeTimers();
|
2022-10-17 22:31:22 +08:00
|
|
|
infoEvent = mkInfoEvent(VoiceBroadcastInfoState.Stopped);
|
2022-12-17 02:23:29 +08:00
|
|
|
createChunkEvents();
|
2023-02-07 17:14:28 +08:00
|
|
|
// use delayed first chunk here to simulate loading time
|
|
|
|
setUpChunkEvents([chunk2Event, deplayedChunk1Event, chunk3Event]);
|
|
|
|
room.addLiveEvents([infoEvent, deplayedChunk1Event, chunk2Event, chunk3Event]);
|
|
|
|
playback = await mkPlayback(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.useRealTimers();
|
2022-10-14 22:48:54 +08:00
|
|
|
});
|
|
|
|
|
2022-11-07 22:19:49 +08:00
|
|
|
it("should expose the info event", () => {
|
|
|
|
expect(playback.infoEvent).toBe(infoEvent);
|
|
|
|
});
|
2022-10-14 12:13:17 +08:00
|
|
|
|
2022-11-07 22:19:49 +08:00
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Stopped);
|
2022-10-14 22:48:54 +08:00
|
|
|
|
2022-11-07 22:19:49 +08:00
|
|
|
describe("and calling start", () => {
|
|
|
|
startPlayback();
|
2022-10-14 22:48:54 +08:00
|
|
|
|
2023-02-07 17:14:28 +08:00
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Buffering);
|
2022-10-14 22:48:54 +08:00
|
|
|
|
2023-02-07 17:14:28 +08:00
|
|
|
describe("and the first chunk data has been loaded", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await simulateFirstChunkArrived();
|
2023-01-02 20:21:33 +08:00
|
|
|
});
|
|
|
|
|
2023-02-07 17:14:28 +08:00
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Playing);
|
2022-10-14 22:48:54 +08:00
|
|
|
|
2023-02-07 17:14:28 +08:00
|
|
|
it("should play the chunks beginning with the first one", () => {
|
|
|
|
// assert that the first chunk is being played
|
|
|
|
expect(chunk1Playback.play).toHaveBeenCalled();
|
|
|
|
expect(chunk2Playback.play).not.toHaveBeenCalled();
|
2023-02-03 20:49:43 +08:00
|
|
|
});
|
|
|
|
|
2023-02-07 17:14:28 +08:00
|
|
|
describe("and calling start again", () => {
|
|
|
|
it("should not play the first chunk a second time", () => {
|
|
|
|
expect(chunk1Playback.play).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
2023-02-03 20:49:43 +08:00
|
|
|
});
|
|
|
|
|
2023-02-07 17:14:28 +08:00
|
|
|
describe("and the chunk playback progresses", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
chunk1Playback.clockInfo.liveData.update([11]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should update the time", () => {
|
|
|
|
expect(playback.timeSeconds).toBe(11);
|
|
|
|
});
|
2022-11-07 22:19:49 +08:00
|
|
|
});
|
2022-10-14 22:48:54 +08:00
|
|
|
|
2023-02-07 17:14:28 +08:00
|
|
|
describe("and the chunk playback progresses across the actual time", () => {
|
|
|
|
// This can be the case if the meta data is out of sync with the actual audio data.
|
2022-11-07 22:19:49 +08:00
|
|
|
|
2023-02-07 17:14:28 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
chunk1Playback.clockInfo.liveData.update([15]);
|
|
|
|
});
|
2022-10-17 22:31:22 +08:00
|
|
|
|
2023-02-07 17:14:28 +08:00
|
|
|
it("should update the time", () => {
|
|
|
|
expect(playback.timeSeconds).toBe(15);
|
|
|
|
expect(playback.timeLeftSeconds).toBe(0);
|
|
|
|
});
|
2022-11-07 22:19:49 +08:00
|
|
|
});
|
|
|
|
|
2023-02-07 17:14:28 +08:00
|
|
|
describe("and skipping to the middle of the second chunk", () => {
|
|
|
|
const middleOfSecondChunk = (chunk1Length + chunk2Length / 2) / 1000;
|
2022-10-17 22:31:22 +08:00
|
|
|
|
2022-11-04 18:50:19 +08:00
|
|
|
beforeEach(async () => {
|
2023-02-07 17:14:28 +08:00
|
|
|
await playback.skipTo(middleOfSecondChunk);
|
2022-11-04 18:50:19 +08:00
|
|
|
});
|
|
|
|
|
2023-02-07 17:14:28 +08:00
|
|
|
it("should play the second chunk", () => {
|
|
|
|
expect(chunk1Playback.stop).toHaveBeenCalled();
|
|
|
|
expect(chunk1Playback.destroy).toHaveBeenCalled();
|
|
|
|
expect(chunk2Playback.play).toHaveBeenCalled();
|
2022-11-04 18:50:19 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should update the time", () => {
|
2023-02-07 17:14:28 +08:00
|
|
|
expect(playback.timeSeconds).toBe(middleOfSecondChunk);
|
2022-11-04 18:50:19 +08:00
|
|
|
});
|
|
|
|
|
2023-02-07 17:14:28 +08:00
|
|
|
describe("and skipping to the start", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await playback.skipTo(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should play the first chunk", () => {
|
|
|
|
expect(chunk2Playback.stop).toHaveBeenCalled();
|
|
|
|
expect(chunk2Playback.destroy).toHaveBeenCalled();
|
|
|
|
expect(chunk1Playback.play).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should update the time", () => {
|
|
|
|
expect(playback.timeSeconds).toBe(0);
|
|
|
|
});
|
|
|
|
});
|
2023-01-24 00:32:04 +08:00
|
|
|
});
|
|
|
|
|
2023-02-07 17:14:28 +08:00
|
|
|
describe("and skipping multiple times", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
return Promise.all([
|
|
|
|
playback.skipTo(middleOfSecondChunk),
|
|
|
|
playback.skipTo(middleOfThirdChunk),
|
|
|
|
playback.skipTo(0),
|
|
|
|
]);
|
|
|
|
});
|
2023-01-24 00:32:04 +08:00
|
|
|
|
2023-02-07 17:14:28 +08:00
|
|
|
it("should only skip to the first and last position", () => {
|
|
|
|
expect(chunk1Playback.stop).toHaveBeenCalled();
|
|
|
|
expect(chunk1Playback.destroy).toHaveBeenCalled();
|
|
|
|
expect(chunk2Playback.play).toHaveBeenCalled();
|
2023-01-24 00:32:04 +08:00
|
|
|
|
2023-02-07 17:14:28 +08:00
|
|
|
expect(chunk3Playback.play).not.toHaveBeenCalled();
|
2023-01-24 00:32:04 +08:00
|
|
|
|
2023-02-07 17:14:28 +08:00
|
|
|
expect(chunk2Playback.stop).toHaveBeenCalled();
|
|
|
|
expect(chunk2Playback.destroy).toHaveBeenCalled();
|
|
|
|
expect(chunk1Playback.play).toHaveBeenCalled();
|
|
|
|
});
|
2022-11-07 22:19:49 +08:00
|
|
|
});
|
2022-11-04 18:50:19 +08:00
|
|
|
|
2023-02-07 17:14:28 +08:00
|
|
|
describe("and the first chunk ends", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
chunk1Playback.emit(PlaybackState.Stopped);
|
|
|
|
});
|
2022-12-07 17:37:30 +08:00
|
|
|
|
2023-02-07 17:14:28 +08:00
|
|
|
it("should play until the end", () => {
|
|
|
|
// assert first chunk was unloaded
|
|
|
|
expect(chunk1Playback.destroy).toHaveBeenCalled();
|
2022-11-04 18:50:19 +08:00
|
|
|
|
2023-02-07 17:14:28 +08:00
|
|
|
// assert that the second chunk is being played
|
|
|
|
expect(chunk2Playback.play).toHaveBeenCalled();
|
2022-11-04 18:50:19 +08:00
|
|
|
|
2023-02-07 17:14:28 +08:00
|
|
|
// simulate end of second and third chunk
|
|
|
|
chunk2Playback.emit(PlaybackState.Stopped);
|
|
|
|
chunk3Playback.emit(PlaybackState.Stopped);
|
2022-11-04 18:50:19 +08:00
|
|
|
|
2023-02-07 17:14:28 +08:00
|
|
|
// assert that the entire playback is now in stopped state
|
|
|
|
expect(playback.getState()).toBe(VoiceBroadcastPlaybackState.Stopped);
|
|
|
|
});
|
|
|
|
});
|
2022-12-05 16:43:47 +08:00
|
|
|
|
2023-02-07 17:14:28 +08:00
|
|
|
describe("and calling pause", () => {
|
|
|
|
pausePlayback();
|
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Paused);
|
|
|
|
itShouldEmitAStateChangedEvent(VoiceBroadcastPlaybackState.Paused);
|
2022-12-20 00:05:34 +08:00
|
|
|
});
|
|
|
|
|
2023-02-07 17:14:28 +08:00
|
|
|
describe("and calling stop", () => {
|
|
|
|
stopPlayback();
|
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Stopped);
|
|
|
|
|
|
|
|
it("should stop the playback", () => {
|
|
|
|
expect(chunk1Playback.stop).toHaveBeenCalled();
|
2022-12-05 16:43:47 +08:00
|
|
|
});
|
|
|
|
|
2023-02-07 17:14:28 +08:00
|
|
|
describe("and skipping to somewhere in the middle of the first chunk", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
mocked(chunk1Playback.play).mockClear();
|
|
|
|
await playback.skipTo(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not start the playback", () => {
|
|
|
|
expect(chunk1Playback.play).not.toHaveBeenCalled();
|
|
|
|
});
|
2022-12-05 16:43:47 +08:00
|
|
|
});
|
|
|
|
});
|
2022-11-04 18:50:19 +08:00
|
|
|
|
2023-02-07 17:14:28 +08:00
|
|
|
describe("and calling destroy", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
playback.destroy();
|
|
|
|
});
|
2022-10-14 22:48:54 +08:00
|
|
|
|
2023-02-07 17:14:28 +08:00
|
|
|
it("should call removeAllListeners", () => {
|
|
|
|
expect(playback.removeAllListeners).toHaveBeenCalled();
|
|
|
|
});
|
2022-10-24 22:06:58 +08:00
|
|
|
|
2023-02-07 17:14:28 +08:00
|
|
|
it("should call destroy on the playbacks", () => {
|
|
|
|
expect(chunk1Playback.destroy).toHaveBeenCalled();
|
|
|
|
expect(chunk2Playback.destroy).toHaveBeenCalled();
|
|
|
|
});
|
2022-10-24 22:06:58 +08:00
|
|
|
});
|
2022-11-07 22:19:49 +08:00
|
|
|
});
|
|
|
|
});
|
2022-11-01 01:35:02 +08:00
|
|
|
|
2022-11-07 22:19:49 +08:00
|
|
|
describe("and calling toggle for the first time", () => {
|
|
|
|
beforeEach(async () => {
|
2023-02-07 17:14:28 +08:00
|
|
|
playback.toggle();
|
|
|
|
await simulateFirstChunkArrived();
|
2022-10-14 22:48:54 +08:00
|
|
|
});
|
|
|
|
|
2022-11-07 22:19:49 +08:00
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Playing);
|
|
|
|
|
|
|
|
describe("and calling toggle a second time", () => {
|
2022-10-14 22:48:54 +08:00
|
|
|
beforeEach(async () => {
|
|
|
|
await playback.toggle();
|
|
|
|
});
|
|
|
|
|
2022-11-07 22:19:49 +08:00
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Paused);
|
2022-10-14 22:48:54 +08:00
|
|
|
|
2022-11-07 22:19:49 +08:00
|
|
|
describe("and calling toggle a third time", () => {
|
2022-10-14 22:48:54 +08:00
|
|
|
beforeEach(async () => {
|
|
|
|
await playback.toggle();
|
|
|
|
});
|
|
|
|
|
2022-11-07 22:19:49 +08:00
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Playing);
|
2022-10-14 22:48:54 +08:00
|
|
|
});
|
|
|
|
});
|
2022-11-07 22:19:49 +08:00
|
|
|
});
|
2022-10-14 22:48:54 +08:00
|
|
|
|
2022-11-07 22:19:49 +08:00
|
|
|
describe("and calling stop", () => {
|
|
|
|
stopPlayback();
|
2022-10-14 22:48:54 +08:00
|
|
|
|
2022-11-07 22:19:49 +08:00
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Stopped);
|
2022-10-14 22:48:54 +08:00
|
|
|
|
2022-11-07 22:19:49 +08:00
|
|
|
describe("and calling toggle", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
mocked(onStateChanged).mockReset();
|
2023-02-07 17:14:28 +08:00
|
|
|
playback.toggle();
|
|
|
|
await simulateFirstChunkArrived();
|
2022-10-17 22:31:22 +08:00
|
|
|
});
|
2022-11-07 22:19:49 +08:00
|
|
|
|
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Playing);
|
|
|
|
itShouldEmitAStateChangedEvent(VoiceBroadcastPlaybackState.Playing);
|
2022-10-14 22:48:54 +08:00
|
|
|
});
|
2022-10-14 12:13:17 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|