@100mslive/roomkit-react 0.2.6-alpha.2 → 0.2.6-alpha.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,112 +0,0 @@
1
- import React, { useEffect, useRef, useState } from 'react';
2
- import { HMSVirtualBackgroundTypes } from '@100mslive/hms-virtual-background';
3
- import {
4
- selectIsLocalVideoEnabled,
5
- selectIsLocalVideoPluginPresent,
6
- selectLocalPeerRole,
7
- selectLocalVideoTrackID,
8
- useHMSActions,
9
- useHMSStore,
10
- } from '@100mslive/react-sdk';
11
- import { VirtualBackgroundIcon } from '@100mslive/react-icons';
12
- import { ActionTile } from '../../components/MoreSettings/ActionTile';
13
- import { Loading } from '../../../Loading';
14
- import { Tooltip } from '../../../Tooltip';
15
- import IconButton from '../../IconButton';
16
- import { getRandomVirtualBackground } from './vbutils';
17
-
18
- export const VirtualBackground = ({
19
- asActionTile = false,
20
- onVBClick = () => {
21
- return;
22
- },
23
- }) => {
24
- const pluginRef = useRef(null);
25
- const hmsActions = useHMSActions();
26
- const role = useHMSStore(selectLocalPeerRole);
27
- const isVideoOn = useHMSStore(selectIsLocalVideoEnabled);
28
- const [isVBLoading, setIsVBLoading] = useState(false);
29
- const [isVBSupported, setIsVBSupported] = useState(false);
30
- const [isVBOn, setIsVBOn] = useState(false);
31
- const localPeerVideoTrackID = useHMSStore(selectLocalVideoTrackID);
32
- const isVBPresent = useHMSStore(selectIsLocalVideoPluginPresent('HMSVB'));
33
-
34
- async function createPlugin() {
35
- if (!pluginRef.current) {
36
- const { HMSVBPlugin } = await import('@100mslive/hms-virtual-background');
37
- pluginRef.current = new HMSVBPlugin(HMSVirtualBackgroundTypes.NONE, HMSVirtualBackgroundTypes.NONE);
38
- }
39
- }
40
- useEffect(() => {
41
- if (!localPeerVideoTrackID) {
42
- return;
43
- }
44
- createPlugin().then(() => {
45
- //check support of plugin
46
- const pluginSupport = hmsActions.validateVideoPluginSupport(pluginRef.current);
47
- setIsVBSupported(pluginSupport.isSupported);
48
- });
49
- }, [hmsActions, localPeerVideoTrackID]);
50
-
51
- async function addPlugin() {
52
- setIsVBLoading(true);
53
- try {
54
- await createPlugin();
55
- window.HMS.virtualBackground = pluginRef.current;
56
- const { background, backgroundType } = getRandomVirtualBackground();
57
- await pluginRef.current.setBackground(background, backgroundType);
58
- await hmsActions.addPluginToVideoTrack(pluginRef.current, Math.floor(role.publishParams.video.frameRate / 2));
59
- } catch (err) {
60
- console.error('add virtual background plugin failed', err);
61
- }
62
- setIsVBLoading(false);
63
- }
64
-
65
- async function removePlugin() {
66
- if (pluginRef.current) {
67
- await hmsActions.removePluginFromVideoTrack(pluginRef.current);
68
- pluginRef.current = null;
69
- }
70
- }
71
-
72
- if (!isVBSupported || !isVideoOn) {
73
- return null;
74
- }
75
- if (asActionTile) {
76
- return (
77
- <ActionTile.Root
78
- data-testid="virtual_bg_btn"
79
- active={isVBPresent}
80
- disabled={isVBLoading}
81
- onClick={() => {
82
- setIsVBOn(!isVBOn);
83
- !isVBPresent ? addPlugin() : removePlugin();
84
- onVBClick();
85
- }}
86
- >
87
- <VirtualBackgroundIcon />
88
- <ActionTile.Title>Virtual Background</ActionTile.Title>
89
- </ActionTile.Root>
90
- );
91
- }
92
-
93
- return (
94
- <Tooltip
95
- boxCss={{ zIndex: '100' }}
96
- title={isVBLoading ? 'Adding virtual background' : `Turn ${!isVBPresent ? 'on' : 'off'} virtual background`}
97
- >
98
- <IconButton
99
- active={!isVBPresent}
100
- disabled={isVBLoading}
101
- onClick={() => {
102
- !isVBPresent ? addPlugin() : removePlugin();
103
- }}
104
- data-testid="virtual_bg_btn"
105
- >
106
- {isVBLoading ? <Loading /> : <VirtualBackgroundIcon />}
107
- </IconButton>
108
- </Tooltip>
109
- );
110
- };
111
-
112
- export default VirtualBackground;
@@ -1,66 +0,0 @@
1
- /* eslint-disable no-case-declarations */
2
- import { HMSVirtualBackgroundTypes } from '@100mslive/hms-virtual-background';
3
- export function getRandomVirtualBackground() {
4
- const backgroundList = [
5
- {
6
- background: HMSVirtualBackgroundTypes.BLUR,
7
- backgroundType: HMSVirtualBackgroundTypes.BLUR,
8
- },
9
- ];
10
-
11
- const images = [
12
- 'https://www.100ms.live/images/vb-1.jpeg',
13
- 'https://www.100ms.live/images/vb-2.jpg',
14
- 'https://www.100ms.live/images/vb-3.png',
15
- 'https://d2qi07yyjujoxr.cloudfront.net/webapp/vb/hms1.png',
16
- 'https://d2qi07yyjujoxr.cloudfront.net/webapp/vb/hms2.png',
17
- 'https://d2qi07yyjujoxr.cloudfront.net/webapp/vb/hms3.png',
18
- 'https://d2qi07yyjujoxr.cloudfront.net/webapp/vb/hms4.png',
19
- ].map(url => ({
20
- background: url,
21
- backgroundType: HMSVirtualBackgroundTypes.IMAGE,
22
- }));
23
-
24
- backgroundList.push(...images);
25
-
26
- /*
27
- //TODO: update with a better quality gif.
28
- const gifList = [
29
- {
30
- background: "https://www.100ms.live/images/vb-1.gif",
31
- backgroundType: HMSVirtualBackgroundTypes.GIF,
32
- },
33
- ];
34
- backgroundList.push(...gifList);
35
- */
36
-
37
- const videoList = [
38
- 'https://www.100ms.live/images/video-1.mp4',
39
- 'https://www.100ms.live/images/video-2.mp4',
40
- 'https://www.100ms.live/images/video-5.mp4',
41
- 'https://www.100ms.live/images/video-7.mp4',
42
- 'https://www.100ms.live/images/video-8.mp4',
43
- ].map(url => ({
44
- background: url,
45
- backgroundType: HMSVirtualBackgroundTypes.VIDEO,
46
- }));
47
- backgroundList.push(...videoList);
48
-
49
- const randomIdx = Math.floor(Math.random() * backgroundList.length);
50
- const virtualBackground = backgroundList[randomIdx];
51
- switch (virtualBackground.backgroundType) {
52
- case HMSVirtualBackgroundTypes.IMAGE:
53
- const img = document.createElement('img');
54
- img.alt = 'VB';
55
- img.src = backgroundList[randomIdx].background;
56
- virtualBackground.background = img;
57
- return virtualBackground;
58
- case HMSVirtualBackgroundTypes.VIDEO:
59
- const videoEl = document.createElement('video');
60
- videoEl.src = backgroundList[randomIdx].background;
61
- virtualBackground.background = videoEl;
62
- return virtualBackground;
63
- default:
64
- return virtualBackground;
65
- }
66
- }