parent
5ac014ff29
commit
4648fa3c8c
|
@ -396,7 +396,11 @@ export class VoiceBroadcastPlayback
|
|||
}
|
||||
|
||||
if (!this.playbacks.has(eventId)) {
|
||||
// set to buffering while loading the chunk data
|
||||
const currentState = this.getState();
|
||||
this.setState(VoiceBroadcastPlaybackState.Buffering);
|
||||
await this.loadPlayback(event);
|
||||
this.setState(currentState);
|
||||
}
|
||||
|
||||
const playback = this.playbacks.get(eventId);
|
||||
|
|
|
@ -18,6 +18,7 @@ import { mocked } from "jest-mock";
|
|||
import { screen } from "@testing-library/react";
|
||||
import userEvent from "@testing-library/user-event";
|
||||
import { MatrixClient, MatrixEvent, MatrixEventEvent, Room } from "matrix-js-sdk/src/matrix";
|
||||
import { defer } from "matrix-js-sdk/src/utils";
|
||||
|
||||
import { Playback, PlaybackState } from "../../../src/audio/Playback";
|
||||
import { PlaybackManager } from "../../../src/audio/PlaybackManager";
|
||||
|
@ -31,9 +32,10 @@ import {
|
|||
VoiceBroadcastPlaybackState,
|
||||
VoiceBroadcastRecording,
|
||||
} from "../../../src/voice-broadcast";
|
||||
import { filterConsole, flushPromises, stubClient } from "../../test-utils";
|
||||
import { filterConsole, flushPromises, flushPromisesWithFakeTimers, stubClient } from "../../test-utils";
|
||||
import { createTestPlayback } from "../../test-utils/audio";
|
||||
import { mkVoiceBroadcastChunkEvent, mkVoiceBroadcastInfoStateEvent } from "../utils/test-utils";
|
||||
import { LazyValue } from "../../../src/utils/LazyValue";
|
||||
|
||||
jest.mock("../../../src/utils/MediaEventHelper", () => ({
|
||||
MediaEventHelper: jest.fn(),
|
||||
|
@ -49,6 +51,7 @@ describe("VoiceBroadcastPlayback", () => {
|
|||
let playback: VoiceBroadcastPlayback;
|
||||
let onStateChanged: (state: VoiceBroadcastPlaybackState) => void;
|
||||
let chunk1Event: MatrixEvent;
|
||||
let deplayedChunk1Event: MatrixEvent;
|
||||
let chunk2Event: MatrixEvent;
|
||||
let chunk2BEvent: MatrixEvent;
|
||||
let chunk3Event: MatrixEvent;
|
||||
|
@ -58,6 +61,7 @@ describe("VoiceBroadcastPlayback", () => {
|
|||
const chunk1Data = new ArrayBuffer(2);
|
||||
const chunk2Data = new ArrayBuffer(3);
|
||||
const chunk3Data = new ArrayBuffer(3);
|
||||
let delayedChunk1Helper: MediaEventHelper;
|
||||
let chunk1Helper: MediaEventHelper;
|
||||
let chunk2Helper: MediaEventHelper;
|
||||
let chunk3Helper: MediaEventHelper;
|
||||
|
@ -97,8 +101,8 @@ describe("VoiceBroadcastPlayback", () => {
|
|||
};
|
||||
|
||||
const startPlayback = () => {
|
||||
beforeEach(async () => {
|
||||
await playback.start();
|
||||
beforeEach(() => {
|
||||
playback.start();
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -127,11 +131,36 @@ describe("VoiceBroadcastPlayback", () => {
|
|||
};
|
||||
};
|
||||
|
||||
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();
|
||||
};
|
||||
|
||||
const mkInfoEvent = (state: VoiceBroadcastInfoState) => {
|
||||
return mkVoiceBroadcastInfoStateEvent(roomId, state, userId, deviceId);
|
||||
};
|
||||
|
||||
const mkPlayback = async () => {
|
||||
const mkPlayback = async (fakeTimers = false): Promise<VoiceBroadcastPlayback> => {
|
||||
const playback = new VoiceBroadcastPlayback(
|
||||
infoEvent,
|
||||
client,
|
||||
|
@ -140,7 +169,7 @@ describe("VoiceBroadcastPlayback", () => {
|
|||
jest.spyOn(playback, "removeAllListeners");
|
||||
jest.spyOn(playback, "destroy");
|
||||
playback.on(VoiceBroadcastPlaybackEvent.StateChanged, onStateChanged);
|
||||
await flushPromises();
|
||||
fakeTimers ? await flushPromisesWithFakeTimers() : await flushPromises();
|
||||
return playback;
|
||||
};
|
||||
|
||||
|
@ -152,6 +181,7 @@ describe("VoiceBroadcastPlayback", () => {
|
|||
|
||||
const createChunkEvents = () => {
|
||||
chunk1Event = mkVoiceBroadcastChunkEvent(infoEvent.getId()!, userId, roomId, chunk1Length, 1);
|
||||
deplayedChunk1Event = mkVoiceBroadcastChunkEvent(infoEvent.getId()!, userId, roomId, chunk1Length, 1);
|
||||
chunk2Event = mkVoiceBroadcastChunkEvent(infoEvent.getId()!, userId, roomId, chunk2Length, 2);
|
||||
chunk2Event.setTxnId("tx-id-1");
|
||||
chunk2BEvent = mkVoiceBroadcastChunkEvent(infoEvent.getId()!, userId, roomId, chunk2Length, 2);
|
||||
|
@ -159,6 +189,7 @@ describe("VoiceBroadcastPlayback", () => {
|
|||
chunk3Event = mkVoiceBroadcastChunkEvent(infoEvent.getId()!, userId, roomId, chunk3Length, 3);
|
||||
|
||||
chunk1Helper = mkChunkHelper(chunk1Data);
|
||||
delayedChunk1Helper = mkDeplayedChunkHelper(chunk1Data);
|
||||
chunk2Helper = mkChunkHelper(chunk2Data);
|
||||
chunk3Helper = mkChunkHelper(chunk3Data);
|
||||
|
||||
|
@ -181,6 +212,7 @@ describe("VoiceBroadcastPlayback", () => {
|
|||
|
||||
mocked(MediaEventHelper).mockImplementation((event: MatrixEvent): any => {
|
||||
if (event === chunk1Event) return chunk1Helper;
|
||||
if (event === deplayedChunk1Event) return delayedChunk1Helper;
|
||||
if (event === chunk2Event) return chunk2Helper;
|
||||
if (event === chunk3Event) return chunk3Helper;
|
||||
});
|
||||
|
@ -488,11 +520,17 @@ describe("VoiceBroadcastPlayback", () => {
|
|||
|
||||
describe("when there is a stopped voice broadcast", () => {
|
||||
beforeEach(async () => {
|
||||
jest.useFakeTimers();
|
||||
infoEvent = mkInfoEvent(VoiceBroadcastInfoState.Stopped);
|
||||
createChunkEvents();
|
||||
setUpChunkEvents([chunk2Event, chunk1Event, chunk3Event]);
|
||||
room.addLiveEvents([infoEvent, chunk1Event, chunk2Event, chunk3Event]);
|
||||
playback = await mkPlayback();
|
||||
// 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();
|
||||
});
|
||||
|
||||
it("should expose the info event", () => {
|
||||
|
@ -504,6 +542,13 @@ describe("VoiceBroadcastPlayback", () => {
|
|||
describe("and calling start", () => {
|
||||
startPlayback();
|
||||
|
||||
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Buffering);
|
||||
|
||||
describe("and the first chunk data has been loaded", () => {
|
||||
beforeEach(async () => {
|
||||
await simulateFirstChunkArrived();
|
||||
});
|
||||
|
||||
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Playing);
|
||||
|
||||
it("should play the chunks beginning with the first one", () => {
|
||||
|
@ -525,7 +570,6 @@ describe("VoiceBroadcastPlayback", () => {
|
|||
|
||||
it("should update the time", () => {
|
||||
expect(playback.timeSeconds).toBe(11);
|
||||
expect(playback.timeLeftSeconds).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -660,10 +704,12 @@ describe("VoiceBroadcastPlayback", () => {
|
|||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("and calling toggle for the first time", () => {
|
||||
beforeEach(async () => {
|
||||
await playback.toggle();
|
||||
playback.toggle();
|
||||
await simulateFirstChunkArrived();
|
||||
});
|
||||
|
||||
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Playing);
|
||||
|
@ -693,7 +739,8 @@ describe("VoiceBroadcastPlayback", () => {
|
|||
describe("and calling toggle", () => {
|
||||
beforeEach(async () => {
|
||||
mocked(onStateChanged).mockReset();
|
||||
await playback.toggle();
|
||||
playback.toggle();
|
||||
await simulateFirstChunkArrived();
|
||||
});
|
||||
|
||||
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Playing);
|
||||
|
|
Loading…
Reference in New Issue