[Backport staging] Start voice broadcast recording while listening (#9659)

Co-authored-by: Michael Weimann <michaelw@matrix.org>
This commit is contained in:
ElementRobot 2022-12-01 20:34:45 +00:00 committed by GitHub
parent 1216580baf
commit f4b6719a28
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 135 additions and 40 deletions

View file

@ -584,6 +584,7 @@ export class MessageComposer extends React.Component<IProps, IState> {
setUpVoiceBroadcastPreRecording( setUpVoiceBroadcastPreRecording(
this.props.room, this.props.room,
MatrixClientPeg.get(), MatrixClientPeg.get(),
SdkContextClass.instance.voiceBroadcastPlaybacksStore,
VoiceBroadcastRecordingsStore.instance(), VoiceBroadcastRecordingsStore.instance(),
SdkContextClass.instance.voiceBroadcastPreRecordingStore, SdkContextClass.instance.voiceBroadcastPreRecordingStore,
); );

View file

@ -367,14 +367,14 @@ class PipView extends React.Component<IProps, IState> {
const pipMode = true; const pipMode = true;
let pipContent: CreatePipChildren | null = null; let pipContent: CreatePipChildren | null = null;
if (this.props.voiceBroadcastPreRecording) {
pipContent = this.createVoiceBroadcastPreRecordingPipContent(this.props.voiceBroadcastPreRecording);
}
if (this.props.voiceBroadcastPlayback) { if (this.props.voiceBroadcastPlayback) {
pipContent = this.createVoiceBroadcastPlaybackPipContent(this.props.voiceBroadcastPlayback); pipContent = this.createVoiceBroadcastPlaybackPipContent(this.props.voiceBroadcastPlayback);
} }
if (this.props.voiceBroadcastPreRecording) {
pipContent = this.createVoiceBroadcastPreRecordingPipContent(this.props.voiceBroadcastPreRecording);
}
if (this.props.voiceBroadcastRecording) { if (this.props.voiceBroadcastRecording) {
pipContent = this.createVoiceBroadcastRecordingPipContent(this.props.voiceBroadcastRecording); pipContent = this.createVoiceBroadcastRecordingPipContent(this.props.voiceBroadcastRecording);
} }

View file

@ -18,6 +18,7 @@ import { MatrixClient, Room, RoomMember } from "matrix-js-sdk/src/matrix";
import { TypedEventEmitter } from "matrix-js-sdk/src/models/typed-event-emitter"; import { TypedEventEmitter } from "matrix-js-sdk/src/models/typed-event-emitter";
import { IDestroyable } from "../../utils/IDestroyable"; import { IDestroyable } from "../../utils/IDestroyable";
import { VoiceBroadcastPlaybacksStore } from "../stores/VoiceBroadcastPlaybacksStore";
import { VoiceBroadcastRecordingsStore } from "../stores/VoiceBroadcastRecordingsStore"; import { VoiceBroadcastRecordingsStore } from "../stores/VoiceBroadcastRecordingsStore";
import { startNewVoiceBroadcastRecording } from "../utils/startNewVoiceBroadcastRecording"; import { startNewVoiceBroadcastRecording } from "../utils/startNewVoiceBroadcastRecording";
@ -34,6 +35,7 @@ export class VoiceBroadcastPreRecording
public room: Room, public room: Room,
public sender: RoomMember, public sender: RoomMember,
private client: MatrixClient, private client: MatrixClient,
private playbacksStore: VoiceBroadcastPlaybacksStore,
private recordingsStore: VoiceBroadcastRecordingsStore, private recordingsStore: VoiceBroadcastRecordingsStore,
) { ) {
super(); super();
@ -43,6 +45,7 @@ export class VoiceBroadcastPreRecording
await startNewVoiceBroadcastRecording( await startNewVoiceBroadcastRecording(
this.room, this.room,
this.client, this.client,
this.playbacksStore,
this.recordingsStore, this.recordingsStore,
); );
this.emit("dismiss", this); this.emit("dismiss", this);

View file

@ -18,6 +18,7 @@ import { MatrixClient, Room } from "matrix-js-sdk/src/matrix";
import { import {
checkVoiceBroadcastPreConditions, checkVoiceBroadcastPreConditions,
VoiceBroadcastPlaybacksStore,
VoiceBroadcastPreRecording, VoiceBroadcastPreRecording,
VoiceBroadcastPreRecordingStore, VoiceBroadcastPreRecordingStore,
VoiceBroadcastRecordingsStore, VoiceBroadcastRecordingsStore,
@ -26,6 +27,7 @@ import {
export const setUpVoiceBroadcastPreRecording = ( export const setUpVoiceBroadcastPreRecording = (
room: Room, room: Room,
client: MatrixClient, client: MatrixClient,
playbacksStore: VoiceBroadcastPlaybacksStore,
recordingsStore: VoiceBroadcastRecordingsStore, recordingsStore: VoiceBroadcastRecordingsStore,
preRecordingStore: VoiceBroadcastPreRecordingStore, preRecordingStore: VoiceBroadcastPreRecordingStore,
): VoiceBroadcastPreRecording | null => { ): VoiceBroadcastPreRecording | null => {
@ -39,7 +41,11 @@ export const setUpVoiceBroadcastPreRecording = (
const sender = room.getMember(userId); const sender = room.getMember(userId);
if (!sender) return null; if (!sender) return null;
const preRecording = new VoiceBroadcastPreRecording(room, sender, client, recordingsStore); // pause and clear current playback (if any)
playbacksStore.getCurrent()?.pause();
playbacksStore.clearCurrent();
const preRecording = new VoiceBroadcastPreRecording(room, sender, client, playbacksStore, recordingsStore);
preRecordingStore.setCurrent(preRecording); preRecordingStore.setCurrent(preRecording);
return preRecording; return preRecording;
}; };

View file

@ -24,6 +24,7 @@ import {
VoiceBroadcastRecordingsStore, VoiceBroadcastRecordingsStore,
VoiceBroadcastRecording, VoiceBroadcastRecording,
getChunkLength, getChunkLength,
VoiceBroadcastPlaybacksStore,
} from ".."; } from "..";
import { checkVoiceBroadcastPreConditions } from "./checkVoiceBroadcastPreConditions"; import { checkVoiceBroadcastPreConditions } from "./checkVoiceBroadcastPreConditions";
@ -80,17 +81,23 @@ const startBroadcast = async (
/** /**
* Starts a new Voice Broadcast Recording, if * Starts a new Voice Broadcast Recording, if
* - the user has the permissions to do so in the room * - the user has the permissions to do so in the room
* - the user is not already recording a voice broadcast
* - there is no other broadcast being recorded in the room, yet * - there is no other broadcast being recorded in the room, yet
* Sends a voice_broadcast_info state event and waits for the event to actually appear in the room state. * Sends a voice_broadcast_info state event and waits for the event to actually appear in the room state.
*/ */
export const startNewVoiceBroadcastRecording = async ( export const startNewVoiceBroadcastRecording = async (
room: Room, room: Room,
client: MatrixClient, client: MatrixClient,
playbacksStore: VoiceBroadcastPlaybacksStore,
recordingsStore: VoiceBroadcastRecordingsStore, recordingsStore: VoiceBroadcastRecordingsStore,
): Promise<VoiceBroadcastRecording | null> => { ): Promise<VoiceBroadcastRecording | null> => {
if (!checkVoiceBroadcastPreConditions(room, client, recordingsStore)) { if (!checkVoiceBroadcastPreConditions(room, client, recordingsStore)) {
return null; return null;
} }
// pause and clear current playback (if any)
playbacksStore.getCurrent()?.pause();
playbacksStore.clearCurrent();
return startBroadcast(room, client, recordingsStore); return startBroadcast(room, client, recordingsStore);
}; };

View file

@ -184,6 +184,7 @@ describe("PipView", () => {
room, room,
alice, alice,
client, client,
voiceBroadcastPlaybacksStore,
voiceBroadcastRecordingsStore, voiceBroadcastRecordingsStore,
); );
voiceBroadcastPreRecordingStore.setCurrent(voiceBroadcastPreRecording); voiceBroadcastPreRecordingStore.setCurrent(voiceBroadcastPreRecording);
@ -271,6 +272,19 @@ describe("PipView", () => {
}); });
}); });
describe("when there is a voice broadcast playback and pre-recording", () => {
beforeEach(() => {
startVoiceBroadcastPlayback(room);
setUpVoiceBroadcastPreRecording();
renderPip();
});
it("should render the voice broadcast pre-recording PiP", () => {
// check for the „Go live“ button
expect(screen.queryByText("Go live")).toBeInTheDocument();
});
});
describe("when there is a voice broadcast pre-recording", () => { describe("when there is a voice broadcast pre-recording", () => {
beforeEach(() => { beforeEach(() => {
setUpVoiceBroadcastPreRecording(); setUpVoiceBroadcastPreRecording();

View file

@ -21,6 +21,7 @@ import { act, render, RenderResult, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event"; import userEvent from "@testing-library/user-event";
import { import {
VoiceBroadcastPlaybacksStore,
VoiceBroadcastPreRecording, VoiceBroadcastPreRecording,
VoiceBroadcastPreRecordingPip, VoiceBroadcastPreRecordingPip,
VoiceBroadcastRecordingsStore, VoiceBroadcastRecordingsStore,
@ -42,6 +43,7 @@ jest.mock("../../../../src/components/views/avatars/RoomAvatar", () => ({
describe("VoiceBroadcastPreRecordingPip", () => { describe("VoiceBroadcastPreRecordingPip", () => {
let renderResult: RenderResult; let renderResult: RenderResult;
let preRecording: VoiceBroadcastPreRecording; let preRecording: VoiceBroadcastPreRecording;
let playbacksStore: VoiceBroadcastPlaybacksStore;
let recordingsStore: VoiceBroadcastRecordingsStore; let recordingsStore: VoiceBroadcastRecordingsStore;
let client: MatrixClient; let client: MatrixClient;
let room: Room; let room: Room;
@ -51,6 +53,7 @@ describe("VoiceBroadcastPreRecordingPip", () => {
client = stubClient(); client = stubClient();
room = new Room("!room@example.com", client, client.getUserId() || ""); room = new Room("!room@example.com", client, client.getUserId() || "");
sender = new RoomMember(room.roomId, client.getUserId() || ""); sender = new RoomMember(room.roomId, client.getUserId() || "");
playbacksStore = new VoiceBroadcastPlaybacksStore();
recordingsStore = new VoiceBroadcastRecordingsStore(); recordingsStore = new VoiceBroadcastRecordingsStore();
mocked(requestMediaPermissions).mockReturnValue(new Promise<MediaStream>((r) => { mocked(requestMediaPermissions).mockReturnValue(new Promise<MediaStream>((r) => {
r({ r({
@ -76,6 +79,7 @@ describe("VoiceBroadcastPreRecordingPip", () => {
room, room,
sender, sender,
client, client,
playbacksStore,
recordingsStore, recordingsStore,
); );
}); });

View file

@ -18,6 +18,7 @@ import { MatrixClient, Room, RoomMember } from "matrix-js-sdk/src/matrix";
import { import {
startNewVoiceBroadcastRecording, startNewVoiceBroadcastRecording,
VoiceBroadcastPlaybacksStore,
VoiceBroadcastPreRecording, VoiceBroadcastPreRecording,
VoiceBroadcastRecordingsStore, VoiceBroadcastRecordingsStore,
} from "../../../src/voice-broadcast"; } from "../../../src/voice-broadcast";
@ -30,6 +31,7 @@ describe("VoiceBroadcastPreRecording", () => {
let client: MatrixClient; let client: MatrixClient;
let room: Room; let room: Room;
let sender: RoomMember; let sender: RoomMember;
let playbacksStore: VoiceBroadcastPlaybacksStore;
let recordingsStore: VoiceBroadcastRecordingsStore; let recordingsStore: VoiceBroadcastRecordingsStore;
let preRecording: VoiceBroadcastPreRecording; let preRecording: VoiceBroadcastPreRecording;
let onDismiss: (voiceBroadcastPreRecording: VoiceBroadcastPreRecording) => void; let onDismiss: (voiceBroadcastPreRecording: VoiceBroadcastPreRecording) => void;
@ -38,12 +40,13 @@ describe("VoiceBroadcastPreRecording", () => {
client = stubClient(); client = stubClient();
room = new Room(roomId, client, client.getUserId() || ""); room = new Room(roomId, client, client.getUserId() || "");
sender = new RoomMember(roomId, client.getUserId() || ""); sender = new RoomMember(roomId, client.getUserId() || "");
playbacksStore = new VoiceBroadcastPlaybacksStore();
recordingsStore = new VoiceBroadcastRecordingsStore(); recordingsStore = new VoiceBroadcastRecordingsStore();
}); });
beforeEach(() => { beforeEach(() => {
onDismiss = jest.fn(); onDismiss = jest.fn();
preRecording = new VoiceBroadcastPreRecording(room, sender, client, recordingsStore); preRecording = new VoiceBroadcastPreRecording(room, sender, client, playbacksStore, recordingsStore);
preRecording.on("dismiss", onDismiss); preRecording.on("dismiss", onDismiss);
}); });
@ -56,6 +59,7 @@ describe("VoiceBroadcastPreRecording", () => {
expect(startNewVoiceBroadcastRecording).toHaveBeenCalledWith( expect(startNewVoiceBroadcastRecording).toHaveBeenCalledWith(
room, room,
client, client,
playbacksStore,
recordingsStore, recordingsStore,
); );
}); });

View file

@ -18,6 +18,7 @@ import { mocked } from "jest-mock";
import { MatrixClient, Room, RoomMember } from "matrix-js-sdk/src/matrix"; import { MatrixClient, Room, RoomMember } from "matrix-js-sdk/src/matrix";
import { import {
VoiceBroadcastPlaybacksStore,
VoiceBroadcastPreRecording, VoiceBroadcastPreRecording,
VoiceBroadcastPreRecordingStore, VoiceBroadcastPreRecordingStore,
VoiceBroadcastRecordingsStore, VoiceBroadcastRecordingsStore,
@ -31,6 +32,7 @@ describe("VoiceBroadcastPreRecordingStore", () => {
let client: MatrixClient; let client: MatrixClient;
let room: Room; let room: Room;
let sender: RoomMember; let sender: RoomMember;
let playbacksStore: VoiceBroadcastPlaybacksStore;
let recordingsStore: VoiceBroadcastRecordingsStore; let recordingsStore: VoiceBroadcastRecordingsStore;
let store: VoiceBroadcastPreRecordingStore; let store: VoiceBroadcastPreRecordingStore;
let preRecording1: VoiceBroadcastPreRecording; let preRecording1: VoiceBroadcastPreRecording;
@ -39,6 +41,7 @@ describe("VoiceBroadcastPreRecordingStore", () => {
client = stubClient(); client = stubClient();
room = new Room(roomId, client, client.getUserId() || ""); room = new Room(roomId, client, client.getUserId() || "");
sender = new RoomMember(roomId, client.getUserId() || ""); sender = new RoomMember(roomId, client.getUserId() || "");
playbacksStore = new VoiceBroadcastPlaybacksStore();
recordingsStore = new VoiceBroadcastRecordingsStore(); recordingsStore = new VoiceBroadcastRecordingsStore();
}); });
@ -46,7 +49,7 @@ describe("VoiceBroadcastPreRecordingStore", () => {
store = new VoiceBroadcastPreRecordingStore(); store = new VoiceBroadcastPreRecordingStore();
jest.spyOn(store, "emit"); jest.spyOn(store, "emit");
jest.spyOn(store, "removeAllListeners"); jest.spyOn(store, "removeAllListeners");
preRecording1 = new VoiceBroadcastPreRecording(room, sender, client, recordingsStore); preRecording1 = new VoiceBroadcastPreRecording(room, sender, client, playbacksStore, recordingsStore);
jest.spyOn(preRecording1, "off"); jest.spyOn(preRecording1, "off");
}); });
@ -117,7 +120,7 @@ describe("VoiceBroadcastPreRecordingStore", () => {
beforeEach(() => { beforeEach(() => {
mocked(store.emit).mockClear(); mocked(store.emit).mockClear();
mocked(preRecording1.off).mockClear(); mocked(preRecording1.off).mockClear();
preRecording2 = new VoiceBroadcastPreRecording(room, sender, client, recordingsStore); preRecording2 = new VoiceBroadcastPreRecording(room, sender, client, playbacksStore, recordingsStore);
store.setCurrent(preRecording2); store.setCurrent(preRecording2);
}); });

View file

@ -15,16 +15,20 @@ limitations under the License.
*/ */
import { mocked } from "jest-mock"; import { mocked } from "jest-mock";
import { MatrixClient, Room } from "matrix-js-sdk/src/matrix"; import { MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
import { import {
checkVoiceBroadcastPreConditions, checkVoiceBroadcastPreConditions,
VoiceBroadcastInfoState,
VoiceBroadcastPlayback,
VoiceBroadcastPlaybacksStore,
VoiceBroadcastPreRecording, VoiceBroadcastPreRecording,
VoiceBroadcastPreRecordingStore, VoiceBroadcastPreRecordingStore,
VoiceBroadcastRecordingsStore, VoiceBroadcastRecordingsStore,
} from "../../../src/voice-broadcast"; } from "../../../src/voice-broadcast";
import { setUpVoiceBroadcastPreRecording } from "../../../src/voice-broadcast/utils/setUpVoiceBroadcastPreRecording"; import { setUpVoiceBroadcastPreRecording } from "../../../src/voice-broadcast/utils/setUpVoiceBroadcastPreRecording";
import { mkRoomMemberJoinEvent, stubClient } from "../../test-utils"; import { mkRoomMemberJoinEvent, stubClient } from "../../test-utils";
import { mkVoiceBroadcastInfoStateEvent } from "./test-utils";
jest.mock("../../../src/voice-broadcast/utils/checkVoiceBroadcastPreConditions"); jest.mock("../../../src/voice-broadcast/utils/checkVoiceBroadcastPreConditions");
@ -34,11 +38,20 @@ describe("setUpVoiceBroadcastPreRecording", () => {
let userId: string; let userId: string;
let room: Room; let room: Room;
let preRecordingStore: VoiceBroadcastPreRecordingStore; let preRecordingStore: VoiceBroadcastPreRecordingStore;
let infoEvent: MatrixEvent;
let playback: VoiceBroadcastPlayback;
let playbacksStore: VoiceBroadcastPlaybacksStore;
let recordingsStore: VoiceBroadcastRecordingsStore; let recordingsStore: VoiceBroadcastRecordingsStore;
const itShouldReturnNull = () => { const itShouldReturnNull = () => {
it("should return null", () => { it("should return null", () => {
expect(setUpVoiceBroadcastPreRecording(room, client, recordingsStore, preRecordingStore)).toBeNull(); expect(setUpVoiceBroadcastPreRecording(
room,
client,
playbacksStore,
recordingsStore,
preRecordingStore,
)).toBeNull();
expect(checkVoiceBroadcastPreConditions).toHaveBeenCalledWith(room, client, recordingsStore); expect(checkVoiceBroadcastPreConditions).toHaveBeenCalledWith(room, client, recordingsStore);
}); });
}; };
@ -51,7 +64,16 @@ describe("setUpVoiceBroadcastPreRecording", () => {
userId = clientUserId; userId = clientUserId;
room = new Room(roomId, client, userId); room = new Room(roomId, client, userId);
infoEvent = mkVoiceBroadcastInfoStateEvent(
roomId,
VoiceBroadcastInfoState.Started,
client.getUserId()!,
client.getDeviceId()!,
);
preRecordingStore = new VoiceBroadcastPreRecordingStore(); preRecordingStore = new VoiceBroadcastPreRecordingStore();
playback = new VoiceBroadcastPlayback(infoEvent, client);
jest.spyOn(playback, "pause");
playbacksStore = new VoiceBroadcastPlaybacksStore();
recordingsStore = new VoiceBroadcastRecordingsStore(); recordingsStore = new VoiceBroadcastRecordingsStore();
}); });
@ -85,15 +107,25 @@ describe("setUpVoiceBroadcastPreRecording", () => {
itShouldReturnNull(); itShouldReturnNull();
}); });
describe("and there is a room member", () => { describe("and there is a room member and listening to another broadcast", () => {
beforeEach(() => { beforeEach(() => {
playbacksStore.setCurrent(playback);
room.currentState.setStateEvents([ room.currentState.setStateEvents([
mkRoomMemberJoinEvent(userId, roomId), mkRoomMemberJoinEvent(userId, roomId),
]); ]);
}); });
it("should create a voice broadcast pre-recording", () => { it("should pause the current playback and create a voice broadcast pre-recording", () => {
const result = setUpVoiceBroadcastPreRecording(room, client, recordingsStore, preRecordingStore); const result = setUpVoiceBroadcastPreRecording(
room,
client,
playbacksStore,
recordingsStore,
preRecordingStore,
);
expect(playback.pause).toHaveBeenCalled();
expect(playbacksStore.getCurrent()).toBeNull();
expect(checkVoiceBroadcastPreConditions).toHaveBeenCalledWith(room, client, recordingsStore); expect(checkVoiceBroadcastPreConditions).toHaveBeenCalledWith(room, client, recordingsStore);
expect(result).toBeInstanceOf(VoiceBroadcastPreRecording); expect(result).toBeInstanceOf(VoiceBroadcastPreRecording);
}); });

View file

@ -15,7 +15,7 @@ limitations under the License.
*/ */
import { mocked } from "jest-mock"; import { mocked } from "jest-mock";
import { EventType, MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix"; import { EventType, ISendEventResponse, MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
import Modal from "../../../src/Modal"; import Modal from "../../../src/Modal";
import { import {
@ -24,6 +24,8 @@ import {
VoiceBroadcastInfoState, VoiceBroadcastInfoState,
VoiceBroadcastRecordingsStore, VoiceBroadcastRecordingsStore,
VoiceBroadcastRecording, VoiceBroadcastRecording,
VoiceBroadcastPlaybacksStore,
VoiceBroadcastPlayback,
} from "../../../src/voice-broadcast"; } from "../../../src/voice-broadcast";
import { mkEvent, stubClient } from "../../test-utils"; import { mkEvent, stubClient } from "../../test-utils";
import { mkVoiceBroadcastInfoStateEvent } from "./test-utils"; import { mkVoiceBroadcastInfoStateEvent } from "./test-utils";
@ -38,6 +40,7 @@ describe("startNewVoiceBroadcastRecording", () => {
const roomId = "!room:example.com"; const roomId = "!room:example.com";
const otherUserId = "@other:example.com"; const otherUserId = "@other:example.com";
let client: MatrixClient; let client: MatrixClient;
let playbacksStore: VoiceBroadcastPlaybacksStore;
let recordingsStore: VoiceBroadcastRecordingsStore; let recordingsStore: VoiceBroadcastRecordingsStore;
let room: Room; let room: Room;
let infoEvent: MatrixEvent; let infoEvent: MatrixEvent;
@ -46,45 +49,50 @@ describe("startNewVoiceBroadcastRecording", () => {
beforeEach(() => { beforeEach(() => {
client = stubClient(); client = stubClient();
room = new Room(roomId, client, client.getUserId()); room = new Room(roomId, client, client.getUserId()!);
jest.spyOn(room.currentState, "maySendStateEvent"); jest.spyOn(room.currentState, "maySendStateEvent");
mocked(client.getRoom).mockImplementation((getRoomId: string) => { mocked(client.getRoom).mockImplementation((getRoomId: string) => {
if (getRoomId === roomId) { if (getRoomId === roomId) {
return room; return room;
} }
return null;
}); });
mocked(client.sendStateEvent).mockImplementation(( mocked(client.sendStateEvent).mockImplementation((
sendRoomId: string, sendRoomId: string,
eventType: string, eventType: string,
_content: any, content: any,
_stateKey: string, stateKey: string,
) => { ): Promise<ISendEventResponse> => {
if (sendRoomId === roomId && eventType === VoiceBroadcastInfoEventType) { if (sendRoomId === roomId && eventType === VoiceBroadcastInfoEventType) {
return Promise.resolve({ event_id: infoEvent.getId() }); return Promise.resolve({ event_id: infoEvent.getId()! });
} }
});
recordingsStore = { throw new Error("Unexpected sendStateEvent call");
setCurrent: jest.fn(), });
getCurrent: jest.fn(),
} as unknown as VoiceBroadcastRecordingsStore;
infoEvent = mkVoiceBroadcastInfoStateEvent( infoEvent = mkVoiceBroadcastInfoStateEvent(
roomId, roomId,
VoiceBroadcastInfoState.Started, VoiceBroadcastInfoState.Started,
client.getUserId(), client.getUserId()!,
client.getDeviceId(), client.getDeviceId()!,
); );
otherEvent = mkEvent({ otherEvent = mkEvent({
event: true, event: true,
type: EventType.RoomMember, type: EventType.RoomMember,
content: {}, content: {},
user: client.getUserId(), user: client.getUserId()!,
room: roomId, room: roomId,
skey: "", skey: "",
}); });
playbacksStore = new VoiceBroadcastPlaybacksStore();
recordingsStore = {
setCurrent: jest.fn(),
getCurrent: jest.fn(),
} as unknown as VoiceBroadcastRecordingsStore;
mocked(VoiceBroadcastRecording).mockImplementation(( mocked(VoiceBroadcastRecording).mockImplementation((
infoEvent: MatrixEvent, infoEvent: MatrixEvent,
client: MatrixClient, client: MatrixClient,
@ -106,22 +114,35 @@ describe("startNewVoiceBroadcastRecording", () => {
mocked(room.currentState.maySendStateEvent).mockReturnValue(true); mocked(room.currentState.maySendStateEvent).mockReturnValue(true);
}); });
describe("when there currently is no other broadcast", () => { describe("when currently listening to a broadcast and there is no recording", () => {
it("should create a new Voice Broadcast", async () => { let playback: VoiceBroadcastPlayback;
beforeEach(() => {
playback = new VoiceBroadcastPlayback(infoEvent, client);
jest.spyOn(playback, "pause");
playbacksStore.setCurrent(playback);
});
it("should stop listen to the current broadcast and create a new recording", async () => {
mocked(client.sendStateEvent).mockImplementation(async ( mocked(client.sendStateEvent).mockImplementation(async (
_roomId: string, _roomId: string,
_eventType: string, _eventType: string,
_content: any, _content: any,
_stateKey = "", _stateKey = "",
) => { ): Promise<ISendEventResponse> => {
setTimeout(() => { setTimeout(() => {
// emit state events after resolving the promise // emit state events after resolving the promise
room.currentState.setStateEvents([otherEvent]); room.currentState.setStateEvents([otherEvent]);
room.currentState.setStateEvents([infoEvent]); room.currentState.setStateEvents([infoEvent]);
}, 0); }, 0);
return { event_id: infoEvent.getId() }; return { event_id: infoEvent.getId()! };
}); });
const recording = await startNewVoiceBroadcastRecording(room, client, recordingsStore); const recording = await startNewVoiceBroadcastRecording(room, client, playbacksStore, recordingsStore);
expect(recording).not.toBeNull();
// expect to stop and clear the current playback
expect(playback.pause).toHaveBeenCalled();
expect(playbacksStore.getCurrent()).toBeNull();
expect(client.sendStateEvent).toHaveBeenCalledWith( expect(client.sendStateEvent).toHaveBeenCalledWith(
roomId, roomId,
@ -133,8 +154,8 @@ describe("startNewVoiceBroadcastRecording", () => {
}, },
client.getUserId(), client.getUserId(),
); );
expect(recording.infoEvent).toBe(infoEvent); expect(recording!.infoEvent).toBe(infoEvent);
expect(recording.start).toHaveBeenCalled(); expect(recording!.start).toHaveBeenCalled();
}); });
}); });
@ -144,7 +165,7 @@ describe("startNewVoiceBroadcastRecording", () => {
new VoiceBroadcastRecording(infoEvent, client), new VoiceBroadcastRecording(infoEvent, client),
); );
result = await startNewVoiceBroadcastRecording(room, client, recordingsStore); result = await startNewVoiceBroadcastRecording(room, client, playbacksStore, recordingsStore);
}); });
it("should not start a voice broadcast", () => { it("should not start a voice broadcast", () => {
@ -162,12 +183,12 @@ describe("startNewVoiceBroadcastRecording", () => {
mkVoiceBroadcastInfoStateEvent( mkVoiceBroadcastInfoStateEvent(
roomId, roomId,
VoiceBroadcastInfoState.Resumed, VoiceBroadcastInfoState.Resumed,
client.getUserId(), client.getUserId()!,
client.getDeviceId(), client.getDeviceId()!,
), ),
]); ]);
result = await startNewVoiceBroadcastRecording(room, client, recordingsStore); result = await startNewVoiceBroadcastRecording(room, client, playbacksStore, recordingsStore);
}); });
it("should not start a voice broadcast", () => { it("should not start a voice broadcast", () => {
@ -190,7 +211,7 @@ describe("startNewVoiceBroadcastRecording", () => {
), ),
]); ]);
result = await startNewVoiceBroadcastRecording(room, client, recordingsStore); result = await startNewVoiceBroadcastRecording(room, client, playbacksStore, recordingsStore);
}); });
it("should not start a voice broadcast", () => { it("should not start a voice broadcast", () => {
@ -206,7 +227,7 @@ describe("startNewVoiceBroadcastRecording", () => {
describe("when the current user is not allowed to send voice broadcast info state events", () => { describe("when the current user is not allowed to send voice broadcast info state events", () => {
beforeEach(async () => { beforeEach(async () => {
mocked(room.currentState.maySendStateEvent).mockReturnValue(false); mocked(room.currentState.maySendStateEvent).mockReturnValue(false);
result = await startNewVoiceBroadcastRecording(room, client, recordingsStore); result = await startNewVoiceBroadcastRecording(room, client, playbacksStore, recordingsStore);
}); });
it("should not start a voice broadcast", () => { it("should not start a voice broadcast", () => {