2022-03-01 09:43:32 +01:00
|
|
|
/*
|
2024-09-09 15:57:16 +02:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-03-01 09:43:32 +01:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 15:57:16 +02: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-03-01 09:43:32 +01:00
|
|
|
*/
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
import { mocked } from "jest-mock";
|
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2022-03-01 09:43:32 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
import { createAudioContext, decodeOgg } from "../../src/audio/compat";
|
2022-03-01 09:43:32 +01:00
|
|
|
import { Playback, PlaybackState } from "../../src/audio/Playback";
|
|
|
|
|
2023-04-27 12:02:20 +02:00
|
|
|
jest.mock("../../src/WorkerManager", () => ({
|
|
|
|
WorkerManager: jest.fn(() => ({
|
|
|
|
call: jest.fn().mockResolvedValue({ waveform: [0, 0, 1, 1] }),
|
|
|
|
})),
|
|
|
|
}));
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
jest.mock("../../src/audio/compat", () => ({
|
2022-03-01 09:43:32 +01:00
|
|
|
createAudioContext: jest.fn(),
|
|
|
|
decodeOgg: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("Playback", () => {
|
2022-03-01 09:43:32 +01:00
|
|
|
const mockAudioBufferSourceNode = {
|
|
|
|
addEventListener: jest.fn(),
|
|
|
|
connect: jest.fn(),
|
|
|
|
start: jest.fn(),
|
|
|
|
};
|
|
|
|
const mockAudioContext = {
|
|
|
|
decodeAudioData: jest.fn(),
|
|
|
|
suspend: jest.fn(),
|
|
|
|
resume: jest.fn(),
|
|
|
|
createBufferSource: jest.fn().mockReturnValue(mockAudioBufferSourceNode),
|
2022-11-02 09:46:42 +01:00
|
|
|
currentTime: 1337,
|
2022-03-01 09:43:32 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const mockAudioBuffer = {
|
|
|
|
duration: 99,
|
|
|
|
getChannelData: jest.fn(),
|
|
|
|
};
|
|
|
|
|
|
|
|
const mockChannelData = new Float32Array();
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-12-12 12:24:14 +01:00
|
|
|
jest.spyOn(logger, "error").mockRestore();
|
2022-03-01 09:43:32 +01:00
|
|
|
mockAudioBuffer.getChannelData.mockClear().mockReturnValue(mockChannelData);
|
2022-12-12 12:24:14 +01:00
|
|
|
mockAudioContext.decodeAudioData.mockReset().mockImplementation((_b, callback) => callback(mockAudioBuffer));
|
2022-03-01 09:43:32 +01:00
|
|
|
mockAudioContext.resume.mockClear().mockResolvedValue(undefined);
|
|
|
|
mockAudioContext.suspend.mockClear().mockResolvedValue(undefined);
|
|
|
|
mocked(decodeOgg).mockClear().mockResolvedValue(new ArrayBuffer(1));
|
|
|
|
mocked(createAudioContext).mockReturnValue(mockAudioContext as unknown as AudioContext);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("initialises correctly", () => {
|
2022-03-01 09:43:32 +01:00
|
|
|
const buffer = new ArrayBuffer(8);
|
|
|
|
|
|
|
|
const playback = new Playback(buffer);
|
2022-11-02 09:46:42 +01:00
|
|
|
playback.clockInfo.durationSeconds = mockAudioBuffer.duration;
|
2022-03-01 09:43:32 +01:00
|
|
|
|
|
|
|
expect(playback.sizeBytes).toEqual(8);
|
|
|
|
expect(playback.clockInfo).toBeTruthy();
|
2022-11-02 09:46:42 +01:00
|
|
|
expect(playback.liveData).toBe(playback.clockInfo.liveData);
|
|
|
|
expect(playback.timeSeconds).toBe(1337 % 99);
|
2022-03-01 09:43:32 +01:00
|
|
|
expect(playback.currentState).toEqual(PlaybackState.Decoding);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("toggles playback on from stopped state", async () => {
|
2022-03-01 09:43:32 +01:00
|
|
|
const buffer = new ArrayBuffer(8);
|
|
|
|
const playback = new Playback(buffer);
|
|
|
|
await playback.prepare();
|
|
|
|
// state is Stopped
|
|
|
|
await playback.toggle();
|
|
|
|
|
|
|
|
expect(mockAudioBufferSourceNode.start).toHaveBeenCalled();
|
|
|
|
expect(mockAudioContext.resume).toHaveBeenCalled();
|
|
|
|
expect(playback.currentState).toEqual(PlaybackState.Playing);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("toggles playback to paused from playing state", async () => {
|
2022-03-01 09:43:32 +01:00
|
|
|
const buffer = new ArrayBuffer(8);
|
|
|
|
const playback = new Playback(buffer);
|
|
|
|
await playback.prepare();
|
|
|
|
await playback.toggle();
|
|
|
|
expect(playback.currentState).toEqual(PlaybackState.Playing);
|
|
|
|
|
|
|
|
await playback.toggle();
|
|
|
|
|
|
|
|
expect(mockAudioContext.suspend).toHaveBeenCalled();
|
|
|
|
expect(playback.currentState).toEqual(PlaybackState.Paused);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("stop playbacks", async () => {
|
2022-03-01 09:43:32 +01:00
|
|
|
const buffer = new ArrayBuffer(8);
|
|
|
|
const playback = new Playback(buffer);
|
|
|
|
await playback.prepare();
|
|
|
|
await playback.toggle();
|
|
|
|
expect(playback.currentState).toEqual(PlaybackState.Playing);
|
|
|
|
|
|
|
|
await playback.stop();
|
|
|
|
|
|
|
|
expect(mockAudioContext.suspend).toHaveBeenCalled();
|
|
|
|
expect(playback.currentState).toEqual(PlaybackState.Stopped);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("prepare()", () => {
|
|
|
|
it("decodes audio data when not greater than 5mb", async () => {
|
2022-03-01 09:43:32 +01:00
|
|
|
const buffer = new ArrayBuffer(8);
|
|
|
|
|
|
|
|
const playback = new Playback(buffer);
|
|
|
|
|
|
|
|
await playback.prepare();
|
|
|
|
|
|
|
|
expect(mockAudioContext.decodeAudioData).toHaveBeenCalledTimes(1);
|
|
|
|
expect(mockAudioBuffer.getChannelData).toHaveBeenCalledWith(0);
|
|
|
|
|
|
|
|
// clock was updated
|
|
|
|
expect(playback.clockInfo.durationSeconds).toEqual(mockAudioBuffer.duration);
|
2022-11-02 09:46:42 +01:00
|
|
|
expect(playback.durationSeconds).toEqual(mockAudioBuffer.duration);
|
2022-03-01 09:43:32 +01:00
|
|
|
|
|
|
|
expect(playback.currentState).toEqual(PlaybackState.Stopped);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("tries to decode ogg when decodeAudioData fails", async () => {
|
2022-03-01 09:43:32 +01:00
|
|
|
// stub logger to keep console clean from expected error
|
2022-12-12 12:24:14 +01:00
|
|
|
jest.spyOn(logger, "error").mockReturnValue(undefined);
|
|
|
|
jest.spyOn(logger, "warn").mockReturnValue(undefined);
|
2022-03-01 09:43:32 +01:00
|
|
|
|
|
|
|
const buffer = new ArrayBuffer(8);
|
2022-12-12 12:24:14 +01:00
|
|
|
const decodingError = new Error("test");
|
|
|
|
mockAudioContext.decodeAudioData
|
|
|
|
.mockImplementationOnce((_b, _callback, error) => error(decodingError))
|
|
|
|
.mockImplementationOnce((_b, callback) => callback(mockAudioBuffer));
|
2022-03-01 09:43:32 +01:00
|
|
|
|
|
|
|
const playback = new Playback(buffer);
|
|
|
|
|
|
|
|
await playback.prepare();
|
|
|
|
|
|
|
|
expect(mockAudioContext.decodeAudioData).toHaveBeenCalledTimes(2);
|
|
|
|
expect(decodeOgg).toHaveBeenCalled();
|
|
|
|
|
|
|
|
// clock was updated
|
|
|
|
expect(playback.clockInfo.durationSeconds).toEqual(mockAudioBuffer.duration);
|
2022-11-02 09:46:42 +01:00
|
|
|
expect(playback.durationSeconds).toEqual(mockAudioBuffer.duration);
|
2022-03-01 09:43:32 +01:00
|
|
|
|
|
|
|
expect(playback.currentState).toEqual(PlaybackState.Stopped);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("does not try to re-decode audio", async () => {
|
2022-03-01 09:43:32 +01:00
|
|
|
const buffer = new ArrayBuffer(8);
|
|
|
|
const playback = new Playback(buffer);
|
|
|
|
await playback.prepare();
|
|
|
|
expect(playback.currentState).toEqual(PlaybackState.Stopped);
|
|
|
|
|
|
|
|
await playback.prepare();
|
|
|
|
|
|
|
|
// only called once in first prepare
|
|
|
|
expect(mockAudioContext.decodeAudioData).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|