/
VirtualBackground.jsx
103 lines (97 loc) · 3.3 KB
/
VirtualBackground.jsx
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
94
95
96
97
98
99
100
101
102
103
/**
* Please refer the following docs for more detals.
* https://www.100ms.live/docs/javascript/v2/how--to-guides/extend-capabilities/plugins/virtual-background
*/
import { useEffect, useRef, useState } from "react";
import { HMSVirtualBackgroundTypes } from "@100mslive/hms-virtual-background";
import {
selectIsAllowedToPublish,
selectIsLocalVideoPluginPresent,
selectLocalPeerRole,
selectLocalVideoTrackID,
useHMSActions,
useHMSStore,
} from "@100mslive/react-sdk";
import { VirtualBackgroundIcon } from "@100mslive/react-icons";
import { Loading, Tooltip } from "@100mslive/roomkit-react";
import IconButton from "../../IconButton";
import { useIsFeatureEnabled } from "../../components/hooks/useFeatures";
import { getRandomVirtualBackground } from "./vbutils";
import { FEATURE_LIST } from "../../common/constants";
export const VirtualBackground = () => {
const pluginRef = useRef(null);
const hmsActions = useHMSActions();
const isAllowedToPublish = useHMSStore(selectIsAllowedToPublish);
const role = useHMSStore(selectLocalPeerRole);
const [isVBLoading, setIsVBLoading] = useState(false);
const [isVBSupported, setIsVBSupported] = useState(false);
const localPeerVideoTrackID = useHMSStore(selectLocalVideoTrackID);
const isVBPresent = useHMSStore(selectIsLocalVideoPluginPresent("HMSVB"));
const isFeatureEnabled = useIsFeatureEnabled(FEATURE_LIST.VIDEO_PLUGINS);
async function createPlugin() {
if (!pluginRef.current) {
const { HMSVBPlugin } = await import("@100mslive/hms-virtual-background");
pluginRef.current = new HMSVBPlugin(
HMSVirtualBackgroundTypes.NONE,
HMSVirtualBackgroundTypes.NONE
);
}
}
useEffect(() => {
if (!localPeerVideoTrackID) {
return;
}
createPlugin().then(() => {
//check support of plugin
const pluginSupport = hmsActions.validateVideoPluginSupport(
pluginRef.current
);
setIsVBSupported(pluginSupport.isSupported);
});
}, [hmsActions, localPeerVideoTrackID]);
async function addPlugin() {
setIsVBLoading(true);
try {
await createPlugin();
window.HMS.virtualBackground = pluginRef.current;
const { background, backgroundType } = getRandomVirtualBackground();
await pluginRef.current.setBackground(background, backgroundType);
await hmsActions.addPluginToVideoTrack(
pluginRef.current,
Math.floor(role.publishParams.video.frameRate / 2)
);
} catch (err) {
console.error("add virtual background plugin failed", err);
}
setIsVBLoading(false);
}
async function removePlugin() {
if (pluginRef.current) {
await hmsActions.removePluginFromVideoTrack(pluginRef.current);
pluginRef.current = null;
}
}
if (!isAllowedToPublish.video || !isVBSupported || !isFeatureEnabled) {
return null;
}
return (
<Tooltip
title={
isVBLoading
? "Adding virtual background"
: `Turn ${!isVBPresent ? "on" : "off"} virtual background`
}
>
<IconButton
active={!isVBPresent}
disabled={isVBLoading}
onClick={() => {
!isVBPresent ? addPlugin() : removePlugin();
}}
data-testid="virtual_bg_btn"
>
{isVBLoading ? <Loading /> : <VirtualBackgroundIcon />}
</IconButton>
</Tooltip>
);
};