-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcomponent.tsx
More file actions
93 lines (79 loc) · 3.35 KB
/
Copy pathcomponent.tsx
File metadata and controls
93 lines (79 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import * as React from 'react';
import { useEffect } from 'react';
import { BbbPluginSdk, ExternalVideoVolumeUiDataNames, PluginApi } from 'bigbluebutton-html-plugin-sdk';
import { DecreaseVolumeOnSpeakProps, ExternalVideoMeetingSubscription } from './types';
import MEETING_SUBSCRIPTION from './subscription';
const intervalBetweenIteration = 100; // In milliseconds
const totalIterationsToDecrease = 10;
const finalVolumeTarget = 0.2;
function DecreaseVolumeOnSpeak(
{ pluginUuid: uuid }: DecreaseVolumeOnSpeakProps,
): React.ReactElement {
BbbPluginSdk.initialize(uuid);
const pluginApi: PluginApi = BbbPluginSdk.getPluginApi(uuid);
const [isDecreasingVolume, setIsDecreasingVolume] = React.useState(false);
const volumeSetByUser = React.useRef(1);
const lastVolumeSetByMachine = React.useRef<number>(null);
const volumeSetByMachine = React.useRef<number[]>([]);
const externalVideoData = pluginApi.useCustomSubscription<ExternalVideoMeetingSubscription>(
MEETING_SUBSCRIPTION,
);
const { value: isMuted } = pluginApi.useUiData(ExternalVideoVolumeUiDataNames.IS_VOLUME_MUTED, {
value: false,
});
const setVolume = (volume: number) => {
pluginApi.uiCommands.externalVideo.volume.set({
volume,
});
volumeSetByMachine.current.push(volume);
};
const [isSomeoneTalking, setIsSomeoneTalking] = React.useState(false);
const talkingIndicatorResult = pluginApi.useTalkingIndicator();
const talkingIndicatorList = talkingIndicatorResult.data;
const volumeFromUiDataHook = pluginApi.useUiData(ExternalVideoVolumeUiDataNames
.CURRENT_VOLUME_VALUE, { value: 1 });
useEffect(() => {
if (
volumeSetByMachine.current.findIndex(
(vol) => volumeFromUiDataHook.value * 100 !== vol * 100,
) === -1
) {
volumeSetByUser.current = volumeFromUiDataHook.value;
}
}, [volumeFromUiDataHook]);
useEffect(() => {
if (talkingIndicatorList?.some((userVoice) => userVoice.talking) !== isSomeoneTalking) {
setIsSomeoneTalking(talkingIndicatorList?.some((userVoice) => userVoice.talking));
}
}, [talkingIndicatorResult]);
useEffect(() => {
if (isSomeoneTalking && !isDecreasingVolume
&& volumeSetByMachine.current.length < totalIterationsToDecrease
&& externalVideoData.data?.meeting[0]?.externalVideo?.playerPlaying
&& !isMuted) {
setIsDecreasingVolume(true);
const currentVolumeSetByUser = volumeSetByUser.current;
const partToSubtract = (currentVolumeSetByUser * (
1 - finalVolumeTarget)) / totalIterationsToDecrease;
for (let i = 0; i < totalIterationsToDecrease; i += 1) {
const vol = currentVolumeSetByUser - partToSubtract * i;
const volumeToSet = Math.floor((vol) * 100);
setTimeout(() => {
setVolume(volumeToSet / 100);
if (i === totalIterationsToDecrease - 1) {
lastVolumeSetByMachine.current = volumeToSet / 100;
setIsDecreasingVolume(false);
}
}, intervalBetweenIteration * i);
}
} else if (!isDecreasingVolume
&& !isSomeoneTalking && volumeSetByMachine.current.length >= totalIterationsToDecrease) {
volumeSetByMachine.current = [];
pluginApi.uiCommands.externalVideo.volume.set({
volume: volumeSetByUser.current,
});
}
}, [isSomeoneTalking, isDecreasingVolume]);
return null;
}
export default DecreaseVolumeOnSpeak;