/* Copyright 2022 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import { mocked } from "jest-mock"; import { EventType, MatrixClient, MatrixEvent, RelationType } from "matrix-js-sdk/src/matrix"; import { Relations } from "matrix-js-sdk/src/models/relations"; import { Playback, PlaybackState } from "../../../src/audio/Playback"; import { PlaybackManager } from "../../../src/audio/PlaybackManager"; import { getReferenceRelationsForEvent } from "../../../src/events"; import { RelationsHelperEvent } from "../../../src/events/RelationsHelper"; import { MediaEventHelper } from "../../../src/utils/MediaEventHelper"; import { VoiceBroadcastInfoEventType, VoiceBroadcastInfoState, VoiceBroadcastPlayback, VoiceBroadcastPlaybackEvent, VoiceBroadcastPlaybackState, } from "../../../src/voice-broadcast"; import { mkEvent, stubClient } from "../../test-utils"; import { createTestPlayback } from "../../test-utils/audio"; import { mkVoiceBroadcastChunkEvent } from "../utils/test-utils"; jest.mock("../../../src/events/getReferenceRelationsForEvent", () => ({ getReferenceRelationsForEvent: jest.fn(), })); jest.mock("../../../src/utils/MediaEventHelper", () => ({ MediaEventHelper: jest.fn(), })); describe("VoiceBroadcastPlayback", () => { const userId = "@user:example.com"; const roomId = "!room:example.com"; let client: MatrixClient; let infoEvent: MatrixEvent; let playback: VoiceBroadcastPlayback; let onStateChanged: (state: VoiceBroadcastPlaybackState) => void; let chunk1Event: MatrixEvent; let chunk2Event: MatrixEvent; let chunk3Event: MatrixEvent; const chunk1Data = new ArrayBuffer(2); const chunk2Data = new ArrayBuffer(3); const chunk3Data = new ArrayBuffer(3); let chunk1Helper: MediaEventHelper; let chunk2Helper: MediaEventHelper; let chunk3Helper: MediaEventHelper; let chunk1Playback: Playback; let chunk2Playback: Playback; let chunk3Playback: Playback; 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`, () => { expect(mocked(onStateChanged)).toHaveBeenCalledWith(state, playback); }); }; const startPlayback = () => { beforeEach(async () => { await playback.start(); }); }; const pausePlayback = () => { beforeEach(() => { playback.pause(); }); }; const stopPlayback = () => { beforeEach(() => { playback.stop(); }); }; const mkChunkHelper = (data: ArrayBuffer): MediaEventHelper => { return { sourceBlob: { cachedValue: null, done: false, value: { // @ts-ignore arrayBuffer: jest.fn().mockResolvedValue(data), }, }, }; }; const mkInfoEvent = (state: VoiceBroadcastInfoState) => { return mkEvent({ event: true, type: VoiceBroadcastInfoEventType, user: userId, room: roomId, content: { state, }, }); }; const mkPlayback = () => { const playback = new VoiceBroadcastPlayback(infoEvent, client); jest.spyOn(playback, "removeAllListeners"); playback.on(VoiceBroadcastPlaybackEvent.StateChanged, onStateChanged); return playback; }; const setUpChunkEvents = (chunkEvents: MatrixEvent[]) => { const relations = new Relations(RelationType.Reference, EventType.RoomMessage, client); jest.spyOn(relations, "getRelations").mockReturnValue(chunkEvents); mocked(getReferenceRelationsForEvent).mockReturnValue(relations); }; beforeAll(() => { client = stubClient(); chunk1Event = mkVoiceBroadcastChunkEvent(userId, roomId, 23, 1); chunk2Event = mkVoiceBroadcastChunkEvent(userId, roomId, 23, 2); chunk3Event = mkVoiceBroadcastChunkEvent(userId, roomId, 23, 3); chunk1Helper = mkChunkHelper(chunk1Data); chunk2Helper = mkChunkHelper(chunk2Data); chunk3Helper = mkChunkHelper(chunk3Data); chunk1Playback = createTestPlayback(); chunk2Playback = createTestPlayback(); chunk3Playback = createTestPlayback(); jest.spyOn(PlaybackManager.instance, "createPlaybackInstance").mockImplementation( (buffer: ArrayBuffer, _waveForm?: number[]) => { if (buffer === chunk1Data) return chunk1Playback; if (buffer === chunk2Data) return chunk2Playback; if (buffer === chunk3Data) return chunk3Playback; }, ); mocked(MediaEventHelper).mockImplementation((event: MatrixEvent) => { if (event === chunk1Event) return chunk1Helper; if (event === chunk2Event) return chunk2Helper; if (event === chunk3Event) return chunk3Helper; }); }); beforeEach(() => { jest.clearAllMocks(); onStateChanged = jest.fn(); }); describe(`when there is a ${VoiceBroadcastInfoState.Resumed} broadcast without chunks yet`, () => { beforeEach(() => { infoEvent = mkInfoEvent(VoiceBroadcastInfoState.Resumed); playback = mkPlayback(); setUpChunkEvents([]); }); describe("and calling start", () => { startPlayback(); it("should be in buffering state", () => { expect(playback.getState()).toBe(VoiceBroadcastPlaybackState.Buffering); }); 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); }); describe("and receiving the first chunk", () => { beforeEach(() => { // TODO Michael W: Use RelationsHelper // @ts-ignore playback.chunkRelationHelper.emit(RelationsHelperEvent.Add, chunk1Event); }); itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Playing); it("should play the first chunk", () => { expect(chunk1Playback.play).toHaveBeenCalled(); }); }); }); }); describe(`when there is a ${VoiceBroadcastInfoState.Resumed} voice broadcast with some chunks`, () => { beforeEach(() => { infoEvent = mkInfoEvent(VoiceBroadcastInfoState.Resumed); playback = mkPlayback(); setUpChunkEvents([chunk2Event, chunk1Event]); }); describe("and calling start", () => { startPlayback(); it("should play the last chunk", () => { // assert that the last chunk is played first expect(chunk2Playback.play).toHaveBeenCalled(); expect(chunk1Playback.play).not.toHaveBeenCalled(); }); describe("and the playback of the last chunk ended", () => { beforeEach(() => { chunk2Playback.emit(PlaybackState.Stopped); }); itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Buffering); describe("and the next chunk arrived", () => { beforeEach(() => { // TODO Michael W: Use RelationsHelper // @ts-ignore playback.chunkRelationHelper.emit(RelationsHelperEvent.Add, chunk3Event); }); itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Playing); it("should play the next chunk", () => { expect(chunk3Playback.play).toHaveBeenCalled(); }); }); }); }); }); describe("when there is a stopped voice broadcast", () => { beforeEach(() => { infoEvent = mkInfoEvent(VoiceBroadcastInfoState.Stopped); playback = mkPlayback(); }); describe("and there are some chunks", () => { beforeEach(() => { setUpChunkEvents([chunk2Event, chunk1Event]); }); it("should expose the info event", () => { expect(playback.infoEvent).toBe(infoEvent); }); itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Stopped); describe("and calling start", () => { startPlayback(); itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Playing); 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(); // simulate end of first chunk chunk1Playback.emit(PlaybackState.Stopped); // assert that the second chunk is being played expect(chunk2Playback.play).toHaveBeenCalled(); // simulate end of second chunk chunk2Playback.emit(PlaybackState.Stopped); // assert that the entire playback is now in stopped state expect(playback.getState()).toBe(VoiceBroadcastPlaybackState.Stopped); }); describe("and calling pause", () => { pausePlayback(); itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Paused); itShouldEmitAStateChangedEvent(VoiceBroadcastPlaybackState.Paused); }); describe("and calling stop", () => { stopPlayback(); itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Stopped); }); describe("and calling destroy", () => { beforeEach(() => { playback.destroy(); }); it("should call removeAllListeners", () => { expect(playback.removeAllListeners).toHaveBeenCalled(); }); it("should call destroy on the playbacks", () => { expect(chunk1Playback.destroy).toHaveBeenCalled(); expect(chunk2Playback.destroy).toHaveBeenCalled(); }); }); }); describe("and calling toggle for the first time", () => { beforeEach(async () => { await playback.toggle(); }); itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Playing); describe("and calling toggle a second time", () => { beforeEach(async () => { await playback.toggle(); }); itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Paused); describe("and calling toggle a third time", () => { beforeEach(async () => { await playback.toggle(); }); itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Playing); }); }); }); describe("and calling stop", () => { stopPlayback(); itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Stopped); describe("and calling toggle", () => { beforeEach(async () => { mocked(onStateChanged).mockReset(); await playback.toggle(); }); itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Playing); itShouldEmitAStateChangedEvent(VoiceBroadcastPlaybackState.Playing); }); }); }); }); });