2022-10-14 12:13:17 +08:00
|
|
|
/*
|
|
|
|
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";
|
2022-10-14 22:48:54 +08:00
|
|
|
import { EventType, MatrixClient, MatrixEvent, MsgType, RelationType } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { Relations } from "matrix-js-sdk/src/models/relations";
|
2022-10-14 12:13:17 +08:00
|
|
|
|
2022-10-14 22:48:54 +08:00
|
|
|
import { Playback, PlaybackState } from "../../../src/audio/Playback";
|
|
|
|
import { PlaybackManager } from "../../../src/audio/PlaybackManager";
|
|
|
|
import { getReferenceRelationsForEvent } from "../../../src/events";
|
2022-10-17 23:35:13 +08:00
|
|
|
import { RelationsHelperEvent } from "../../../src/events/RelationsHelper";
|
2022-10-14 22:48:54 +08:00
|
|
|
import { MediaEventHelper } from "../../../src/utils/MediaEventHelper";
|
2022-10-14 12:13:17 +08:00
|
|
|
import {
|
2022-10-14 22:48:54 +08:00
|
|
|
VoiceBroadcastChunkEventType,
|
2022-10-14 12:13:17 +08:00
|
|
|
VoiceBroadcastInfoEventType,
|
2022-10-17 22:31:22 +08:00
|
|
|
VoiceBroadcastInfoState,
|
2022-10-14 12:13:17 +08:00
|
|
|
VoiceBroadcastPlayback,
|
|
|
|
VoiceBroadcastPlaybackEvent,
|
|
|
|
VoiceBroadcastPlaybackState,
|
|
|
|
} from "../../../src/voice-broadcast";
|
2022-10-14 22:48:54 +08:00
|
|
|
import { mkEvent, stubClient } from "../../test-utils";
|
|
|
|
import { createTestPlayback } from "../../test-utils/audio";
|
|
|
|
|
|
|
|
jest.mock("../../../src/events/getReferenceRelationsForEvent", () => ({
|
|
|
|
getReferenceRelationsForEvent: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
jest.mock("../../../src/utils/MediaEventHelper", () => ({
|
|
|
|
MediaEventHelper: jest.fn(),
|
|
|
|
}));
|
2022-10-14 12:13:17 +08:00
|
|
|
|
|
|
|
describe("VoiceBroadcastPlayback", () => {
|
|
|
|
const userId = "@user:example.com";
|
|
|
|
const roomId = "!room:example.com";
|
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 chunk0Event: MatrixEvent;
|
|
|
|
let chunk1Event: MatrixEvent;
|
|
|
|
let chunk2Event: MatrixEvent;
|
2022-10-17 23:35:13 +08:00
|
|
|
let chunk3Event: MatrixEvent;
|
2022-10-14 22:48:54 +08:00
|
|
|
const chunk0Data = new ArrayBuffer(1);
|
|
|
|
const chunk1Data = new ArrayBuffer(2);
|
|
|
|
const chunk2Data = new ArrayBuffer(3);
|
2022-10-17 23:35:13 +08:00
|
|
|
const chunk3Data = new ArrayBuffer(3);
|
2022-10-14 22:48:54 +08:00
|
|
|
let chunk0Helper: MediaEventHelper;
|
|
|
|
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 chunk0Playback: Playback;
|
|
|
|
let chunk1Playback: Playback;
|
|
|
|
let chunk2Playback: Playback;
|
2022-10-17 23:35:13 +08:00
|
|
|
let chunk3Playback: Playback;
|
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`, () => {
|
|
|
|
expect(mocked(onStateChanged)).toHaveBeenCalledWith(state);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-10-14 22:48:54 +08:00
|
|
|
const mkChunkEvent = (sequence: number) => {
|
|
|
|
return mkEvent({
|
|
|
|
event: true,
|
|
|
|
user: client.getUserId(),
|
|
|
|
room: roomId,
|
|
|
|
type: EventType.RoomMessage,
|
|
|
|
content: {
|
|
|
|
msgtype: MsgType.Audio,
|
|
|
|
[VoiceBroadcastChunkEventType]: {
|
|
|
|
sequence,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const mkChunkHelper = (data: ArrayBuffer): MediaEventHelper => {
|
|
|
|
return {
|
|
|
|
sourceBlob: {
|
|
|
|
cachedValue: null,
|
|
|
|
done: false,
|
|
|
|
value: {
|
|
|
|
// @ts-ignore
|
|
|
|
arrayBuffer: jest.fn().mockResolvedValue(data),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-10-17 22:31:22 +08:00
|
|
|
const mkInfoEvent = (state: VoiceBroadcastInfoState) => {
|
|
|
|
return mkEvent({
|
2022-10-14 12:13:17 +08:00
|
|
|
event: true,
|
|
|
|
type: VoiceBroadcastInfoEventType,
|
|
|
|
user: userId,
|
|
|
|
room: roomId,
|
2022-10-17 22:31:22 +08:00
|
|
|
content: {
|
|
|
|
state,
|
|
|
|
},
|
2022-10-14 12:13:17 +08:00
|
|
|
});
|
2022-10-17 22:31:22 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
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();
|
2022-10-14 22:48:54 +08:00
|
|
|
|
|
|
|
// crap event to test 0 as first sequence number
|
|
|
|
chunk0Event = mkChunkEvent(0);
|
|
|
|
chunk1Event = mkChunkEvent(1);
|
|
|
|
chunk2Event = mkChunkEvent(2);
|
2022-10-17 23:35:13 +08:00
|
|
|
chunk3Event = mkChunkEvent(3);
|
2022-10-14 22:48:54 +08:00
|
|
|
|
|
|
|
chunk0Helper = mkChunkHelper(chunk0Data);
|
|
|
|
chunk1Helper = mkChunkHelper(chunk1Data);
|
|
|
|
chunk2Helper = mkChunkHelper(chunk2Data);
|
2022-10-17 23:35:13 +08:00
|
|
|
chunk3Helper = mkChunkHelper(chunk3Data);
|
2022-10-14 22:48:54 +08:00
|
|
|
|
|
|
|
chunk0Playback = createTestPlayback();
|
|
|
|
chunk1Playback = createTestPlayback();
|
|
|
|
chunk2Playback = createTestPlayback();
|
2022-10-17 23:35:13 +08:00
|
|
|
chunk3Playback = createTestPlayback();
|
2022-10-14 22:48:54 +08:00
|
|
|
|
|
|
|
jest.spyOn(PlaybackManager.instance, "createPlaybackInstance").mockImplementation(
|
|
|
|
(buffer: ArrayBuffer, _waveForm?: number[]) => {
|
|
|
|
if (buffer === chunk0Data) return chunk0Playback;
|
|
|
|
if (buffer === chunk1Data) return chunk1Playback;
|
|
|
|
if (buffer === chunk2Data) return chunk2Playback;
|
2022-10-17 23:35:13 +08:00
|
|
|
if (buffer === chunk3Data) return chunk3Playback;
|
2022-10-14 22:48:54 +08:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
mocked(MediaEventHelper).mockImplementation((event: MatrixEvent) => {
|
|
|
|
if (event === chunk0Event) return chunk0Helper;
|
|
|
|
if (event === chunk1Event) return chunk1Helper;
|
|
|
|
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-10-14 12:13:17 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-10-17 22:31:22 +08:00
|
|
|
jest.clearAllMocks();
|
2022-10-14 12:13:17 +08:00
|
|
|
onStateChanged = jest.fn();
|
|
|
|
});
|
|
|
|
|
2022-10-17 23:35:13 +08:00
|
|
|
describe("when there is a running broadcast without chunks yet", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
infoEvent = mkInfoEvent(VoiceBroadcastInfoState.Running);
|
|
|
|
playback = mkPlayback();
|
|
|
|
setUpChunkEvents([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and calling start", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await playback.start();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should be in buffering state", () => {
|
|
|
|
expect(playback.getState()).toBe(VoiceBroadcastPlaybackState.Buffering);
|
|
|
|
});
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-10-17 22:31:22 +08:00
|
|
|
describe("when there is a running voice broadcast with some chunks", () => {
|
2022-10-14 12:13:17 +08:00
|
|
|
beforeEach(() => {
|
2022-10-17 22:31:22 +08:00
|
|
|
infoEvent = mkInfoEvent(VoiceBroadcastInfoState.Running);
|
|
|
|
playback = mkPlayback();
|
|
|
|
setUpChunkEvents([chunk2Event, chunk0Event, chunk1Event]);
|
2022-10-14 12:13:17 +08:00
|
|
|
});
|
|
|
|
|
2022-10-17 22:31:22 +08:00
|
|
|
describe("and calling start", () => {
|
2022-10-14 22:48:54 +08:00
|
|
|
beforeEach(async () => {
|
|
|
|
await playback.start();
|
2022-10-14 12:13:17 +08:00
|
|
|
});
|
|
|
|
|
2022-10-17 22:31:22 +08:00
|
|
|
it("should play the last chunk", () => {
|
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
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
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-10-14 12:13:17 +08:00
|
|
|
beforeEach(() => {
|
2022-10-17 22:31:22 +08:00
|
|
|
infoEvent = mkInfoEvent(VoiceBroadcastInfoState.Stopped);
|
|
|
|
playback = mkPlayback();
|
2022-10-14 22:48:54 +08:00
|
|
|
});
|
|
|
|
|
2022-10-17 22:31:22 +08:00
|
|
|
describe("and there is only a 0 sequence event", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
setUpChunkEvents([chunk0Event]);
|
|
|
|
});
|
2022-10-14 12:13:17 +08:00
|
|
|
|
2022-10-17 22:31:22 +08:00
|
|
|
describe("and calling start", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await playback.start();
|
|
|
|
});
|
|
|
|
|
2022-10-17 23:35:13 +08:00
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Buffering);
|
2022-10-17 22:31:22 +08:00
|
|
|
});
|
2022-10-14 22:48:54 +08:00
|
|
|
});
|
2022-10-14 12:13:17 +08:00
|
|
|
|
2022-10-17 22:31:22 +08:00
|
|
|
describe("and there are some chunks", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
setUpChunkEvents([chunk2Event, chunk0Event, chunk1Event]);
|
2022-10-14 12:13:17 +08:00
|
|
|
});
|
|
|
|
|
2022-10-17 22:31:22 +08:00
|
|
|
it("should expose the info event", () => {
|
|
|
|
expect(playback.infoEvent).toBe(infoEvent);
|
|
|
|
});
|
2022-10-14 22:48:54 +08:00
|
|
|
|
2022-10-17 23:35:13 +08:00
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Stopped);
|
2022-10-14 22:48:54 +08:00
|
|
|
|
2022-10-17 22:31:22 +08:00
|
|
|
describe("and calling start", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await playback.start();
|
|
|
|
});
|
2022-10-14 22:48:54 +08:00
|
|
|
|
2022-10-17 22:31:22 +08:00
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Playing);
|
2022-10-14 22:48:54 +08:00
|
|
|
|
2022-10-17 22:31:22 +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();
|
2022-10-14 22:48:54 +08:00
|
|
|
|
2022-10-17 22:31:22 +08:00
|
|
|
// simulate end of first chunk
|
|
|
|
chunk1Playback.emit(PlaybackState.Stopped);
|
2022-10-14 22:48:54 +08:00
|
|
|
|
2022-10-17 22:31:22 +08:00
|
|
|
// 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);
|
2022-10-14 22:48:54 +08:00
|
|
|
});
|
|
|
|
|
2022-10-17 22:31:22 +08:00
|
|
|
describe("and calling pause", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
playback.pause();
|
|
|
|
});
|
2022-10-14 12:13:17 +08:00
|
|
|
|
2022-10-17 22:31:22 +08:00
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Paused);
|
|
|
|
itShouldEmitAStateChangedEvent(VoiceBroadcastPlaybackState.Paused);
|
|
|
|
});
|
2022-10-14 22:48:54 +08:00
|
|
|
});
|
|
|
|
|
2022-10-17 22:31:22 +08:00
|
|
|
describe("and calling toggle for the first time", () => {
|
2022-10-14 22:48:54 +08:00
|
|
|
beforeEach(async () => {
|
|
|
|
await playback.toggle();
|
|
|
|
});
|
|
|
|
|
2022-10-17 22:31:22 +08:00
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Playing);
|
2022-10-14 22:48:54 +08:00
|
|
|
|
2022-10-17 22:31:22 +08:00
|
|
|
describe("and calling toggle a second time", () => {
|
2022-10-14 22:48:54 +08:00
|
|
|
beforeEach(async () => {
|
|
|
|
await playback.toggle();
|
|
|
|
});
|
|
|
|
|
2022-10-17 22:31:22 +08:00
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Paused);
|
|
|
|
|
|
|
|
describe("and calling toggle a third time", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await playback.toggle();
|
|
|
|
});
|
|
|
|
|
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Playing);
|
|
|
|
});
|
2022-10-14 22:48:54 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-10-17 22:31:22 +08:00
|
|
|
describe("and calling stop", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
playback.stop();
|
|
|
|
});
|
2022-10-14 22:48:54 +08:00
|
|
|
|
2022-10-17 22:31:22 +08:00
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Stopped);
|
2022-10-14 22:48:54 +08:00
|
|
|
|
2022-10-17 22:31:22 +08:00
|
|
|
describe("and calling toggle", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
mocked(onStateChanged).mockReset();
|
|
|
|
await playback.toggle();
|
|
|
|
});
|
2022-10-14 22:48:54 +08:00
|
|
|
|
2022-10-17 22:31:22 +08:00
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Playing);
|
|
|
|
itShouldEmitAStateChangedEvent(VoiceBroadcastPlaybackState.Playing);
|
|
|
|
});
|
2022-10-14 22:48:54 +08:00
|
|
|
});
|
2022-10-14 12:13:17 +08:00
|
|
|
|
2022-10-17 22:31:22 +08:00
|
|
|
describe("and calling destroy", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
playback.destroy();
|
|
|
|
});
|
2022-10-14 22:48:54 +08:00
|
|
|
|
2022-10-17 22:31:22 +08:00
|
|
|
it("should call removeAllListeners", () => {
|
|
|
|
expect(playback.removeAllListeners).toHaveBeenCalled();
|
|
|
|
});
|
2022-10-14 22:48:54 +08:00
|
|
|
});
|
2022-10-14 12:13:17 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|