Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Add voice broadcast playback pip
Browse files Browse the repository at this point in the history
  • Loading branch information
weeman1337 committed Nov 18, 2022
1 parent 2a29090 commit b3a11e1
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 29 deletions.
70 changes: 48 additions & 22 deletions src/components/views/voip/PipView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@ import { CallStore } from "../../../stores/CallStore";
import {
useCurrentVoiceBroadcastPreRecording,
useCurrentVoiceBroadcastRecording,
VoiceBroadcastPlayback,
VoiceBroadcastPlaybackBody,
VoiceBroadcastPreRecording,
VoiceBroadcastPreRecordingPip,
VoiceBroadcastRecording,
VoiceBroadcastRecordingPip,
} from '../../../voice-broadcast';
import { useCurrentVoiceBroadcastPlayback } from '../../../voice-broadcast/hooks/useCurrentVoiceBroadcastPlayback';

const SHOW_CALL_IN_STATES = [
CallState.Connected,
Expand All @@ -57,6 +60,7 @@ const SHOW_CALL_IN_STATES = [
interface IProps {
voiceBroadcastRecording?: Optional<VoiceBroadcastRecording>;
voiceBroadcastPreRecording?: Optional<VoiceBroadcastPreRecording>;
voiceBroadcastPlayback?: Optional<VoiceBroadcastPlayback>;
}

interface IState {
Expand All @@ -77,6 +81,11 @@ interface IState {
moving: boolean;
}

type CreatePipContent = (object: {
onStartMoving?: (event: React.MouseEvent<Element, MouseEvent>) => void;
onResize?: (event: Event) => void;
}) => JSX.Element;

const getRoomAndAppForWidget = (widgetId: string, roomId: string): [Room | null, IApp | null] => {
if (!widgetId) return [null, null];
if (!roomId) return [null, null];
Expand Down Expand Up @@ -328,9 +337,42 @@ class PipView extends React.Component<IProps, IState> {
this.setState({ showWidgetInPip, persistentWidgetId, persistentRoomId });
}

private createVoiceBroadcastPlaybackPipContent(): CreatePipContent {
return ({ onStartMoving }) => <div onMouseDown={onStartMoving}>
<VoiceBroadcastPlaybackBody
playback={this.props.voiceBroadcastPlayback}
pip={true}
/>
</div>;
}

private createVoiceBroadcastPreRecordingPipContent(): CreatePipContent {
return ({ onStartMoving }) => <div onMouseDown={onStartMoving}>
<VoiceBroadcastPreRecordingPip
voiceBroadcastPreRecording={this.props.voiceBroadcastPreRecording}
/>
</div>;
}

private createVoiceBroadcastRecordingPipContent(): CreatePipContent {
return ({ onStartMoving }) => <div onMouseDown={onStartMoving}>
<VoiceBroadcastRecordingPip
recording={this.props.voiceBroadcastRecording}
/>
</div>;
}

public render() {
const pipMode = true;
let pipContent;
let pipContent: CreatePipContent;

if (this.props.voiceBroadcastPreRecording) {
pipContent = this.createVoiceBroadcastPreRecordingPipContent();
} else if (this.props.voiceBroadcastPlayback) {
pipContent = this.createVoiceBroadcastPlaybackPipContent();
} else if (this.props.voiceBroadcastRecording) {
pipContent = this.createVoiceBroadcastRecordingPipContent();
}

if (this.state.primaryCall) {
// get a ref to call inside the current scope
Expand Down Expand Up @@ -375,26 +417,6 @@ class PipView extends React.Component<IProps, IState> {
</div>;
}

if (this.props.voiceBroadcastPreRecording) {
// get a ref to pre-recording inside the current scope
const preRecording = this.props.voiceBroadcastPreRecording;
pipContent = ({ onStartMoving }) => <div onMouseDown={onStartMoving}>
<VoiceBroadcastPreRecordingPip
voiceBroadcastPreRecording={preRecording}
/>
</div>;
}

if (this.props.voiceBroadcastRecording) {
// get a ref to recording inside the current scope
const recording = this.props.voiceBroadcastRecording;
pipContent = ({ onStartMoving }) => <div onMouseDown={onStartMoving}>
<VoiceBroadcastRecordingPip
recording={recording}
/>
</div>;
}

if (!!pipContent) {
return <PictureInPictureDragger
className="mx_LegacyCallPreview"
Expand All @@ -420,9 +442,13 @@ const PipViewHOC: React.FC<IProps> = (props) => {
const voiceBroadcastRecordingsStore = sdkContext.voiceBroadcastRecordingsStore;
const { currentVoiceBroadcastRecording } = useCurrentVoiceBroadcastRecording(voiceBroadcastRecordingsStore);

const voiceBroadcastPlaybacksStore = sdkContext.voiceBroadcastPlaybacksStore;
const { voiceBroadcastPlayback } = useCurrentVoiceBroadcastPlayback(voiceBroadcastPlaybacksStore);

return <PipView
voiceBroadcastRecording={currentVoiceBroadcastRecording}
voiceBroadcastPlayback={voiceBroadcastPlayback}
voiceBroadcastPreRecording={currentVoiceBroadcastPreRecording}
voiceBroadcastRecording={currentVoiceBroadcastRecording}
{...props}
/>;
};
Expand Down
14 changes: 13 additions & 1 deletion src/contexts/SDKContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ import TypingStore from "../stores/TypingStore";
import { WidgetLayoutStore } from "../stores/widgets/WidgetLayoutStore";
import { WidgetPermissionStore } from "../stores/widgets/WidgetPermissionStore";
import WidgetStore from "../stores/WidgetStore";
import { VoiceBroadcastPreRecordingStore, VoiceBroadcastRecordingsStore } from "../voice-broadcast";
import {
VoiceBroadcastPlaybacksStore,
VoiceBroadcastPreRecordingStore,
VoiceBroadcastRecordingsStore,
} from "../voice-broadcast";

export const SDKContext = createContext<SdkContextClass>(undefined);
SDKContext.displayName = "SDKContext";
Expand Down Expand Up @@ -66,6 +70,7 @@ export class SdkContextClass {
protected _TypingStore?: TypingStore;
protected _VoiceBroadcastRecordingsStore?: VoiceBroadcastRecordingsStore;
protected _VoiceBroadcastPreRecordingStore?: VoiceBroadcastPreRecordingStore;
protected _VoiceBroadcastPlaybacksStore?: VoiceBroadcastPlaybacksStore;

/**
* Automatically construct stores which need to be created eagerly so they can register with
Expand Down Expand Up @@ -158,4 +163,11 @@ export class SdkContextClass {
}
return this._VoiceBroadcastPreRecordingStore;
}

public get voiceBroadcastPlaybacksStore(): VoiceBroadcastPlaybacksStore {
if (!this._VoiceBroadcastPlaybacksStore) {
this._VoiceBroadcastPlaybacksStore = VoiceBroadcastPlaybacksStore.instance();
}
return this._VoiceBroadcastPlaybacksStore;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ limitations under the License.
*/

import React from "react";
import classNames from "classnames";

import {
VoiceBroadcastControl,
Expand All @@ -31,10 +32,12 @@ import Clock from "../../../components/views/audio_messages/Clock";
import SeekBar from "../../../components/views/audio_messages/SeekBar";

interface VoiceBroadcastPlaybackBodyProps {
pip?: boolean;
playback: VoiceBroadcastPlayback;
}

export const VoiceBroadcastPlaybackBody: React.FC<VoiceBroadcastPlaybackBodyProps> = ({
pip = false,
playback,
}) => {
const {
Expand Down Expand Up @@ -76,8 +79,13 @@ export const VoiceBroadcastPlaybackBody: React.FC<VoiceBroadcastPlaybackBodyProp
/>;
}

const classes = classNames({
mx_VoiceBroadcastBody: true,
["mx_VoiceBroadcastBody--pip"]: pip,
});

return (
<div className="mx_VoiceBroadcastBody">
<div className={classes}>
<VoiceBroadcastHeader
live={liveness}
microphoneLabel={sender?.name}
Expand Down
44 changes: 44 additions & 0 deletions src/voice-broadcast/hooks/useCurrentVoiceBroadcastPlayback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
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 { useState } from "react";

import { useTypedEventEmitter } from "../../hooks/useEventEmitter";
import { VoiceBroadcastPlayback } from "../models/VoiceBroadcastPlayback";
import {
VoiceBroadcastPlaybacksStore,
VoiceBroadcastPlaybacksStoreEvent,
} from "../stores/VoiceBroadcastPlaybacksStore";

export const useCurrentVoiceBroadcastPlayback = (
voiceBroadcastPlaybackStore: VoiceBroadcastPlaybacksStore,
) => {
const [voiceBroadcastPlayback, setVoiceBroadcastPlayback] = useState(
voiceBroadcastPlaybackStore.getCurrent(),
);

useTypedEventEmitter(
voiceBroadcastPlaybackStore,
VoiceBroadcastPlaybacksStoreEvent.CurrentChanged,
(playback: VoiceBroadcastPlayback) => {
setVoiceBroadcastPlayback(playback);
},
);

return {
voiceBroadcastPlayback,
};
};
21 changes: 16 additions & 5 deletions src/voice-broadcast/stores/VoiceBroadcastPlaybacksStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ export class VoiceBroadcastPlaybacksStore
this.emit(VoiceBroadcastPlaybacksStoreEvent.CurrentChanged, current);
}

public clearCurrent(): void {
if (this.current === null) return;

this.current = null;
this.emit(VoiceBroadcastPlaybacksStoreEvent.CurrentChanged, null);
}

public getCurrent(): VoiceBroadcastPlayback {
return this.current;
}
Expand Down Expand Up @@ -80,11 +87,15 @@ export class VoiceBroadcastPlaybacksStore
state: VoiceBroadcastPlaybackState,
playback: VoiceBroadcastPlayback,
): void => {
if ([
VoiceBroadcastPlaybackState.Buffering,
VoiceBroadcastPlaybackState.Playing,
].includes(state)) {
this.pauseExcept(playback);
switch (state) {
case VoiceBroadcastPlaybackState.Buffering:
case VoiceBroadcastPlaybackState.Playing:
this.pauseExcept(playback);
this.setCurrent(playback);
break;
case VoiceBroadcastPlaybackState.Stopped:
this.clearCurrent();
break;
}
};

Expand Down

0 comments on commit b3a11e1

Please sign in to comment.