Skip to content

Commit 371e166

Browse files
committed
fix: improve display VB + clear properly
1 parent 0e44d2c commit 371e166

File tree

7 files changed

+178
-218
lines changed

7 files changed

+178
-218
lines changed

src/components/footer/modals/webcam/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
updateShowVideoShareModal,
88
} from '../../../../store/slices/bottomIconsActivitySlice';
99
import { getInputMediaDevices } from '../../../../helpers/utils';
10-
import PreviewWebcam from './previewWebcam';
10+
import WebcamSettings from './webcamSettings';
1111
import { addVideoDevices } from '../../../../store/slices/roomSettingsSlice';
1212
import Modal from '../../../../helpers/ui/modal';
1313
import Dropdown from '../../../../helpers/ui/dropdown';
@@ -99,7 +99,7 @@ const ShareWebcamModal = ({
9999
</div>
100100
)}
101101
<div className="w-full">
102-
<PreviewWebcam deviceId={selectedWebcam} />
102+
<WebcamSettings deviceId={selectedWebcam} />
103103
</div>
104104
</Modal>
105105
)

src/components/footer/modals/webcam/previewWebcam.tsx

Lines changed: 0 additions & 93 deletions
This file was deleted.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import React, { useEffect, useRef } from 'react';
2+
import {
3+
createLocalVideoTrack,
4+
LocalVideoTrack,
5+
VideoPresets,
6+
} from 'livekit-client';
7+
8+
import { useAppSelector } from '../../../../store';
9+
import {
10+
createVirtualBackgroundProcessor,
11+
TwilioBackgroundProcessor,
12+
} from '../../../../helpers/libs/TrackProcessor';
13+
14+
interface WebcamPreviewProps {
15+
deviceId: string;
16+
}
17+
18+
const WebcamPreview = ({ deviceId }: WebcamPreviewProps) => {
19+
const videoRef = useRef<HTMLVideoElement>(null);
20+
const localVideoTrack = useRef<LocalVideoTrack | null>(null);
21+
22+
const virtualBackground = useAppSelector(
23+
(state) => state.bottomIconsActivity.virtualBackground,
24+
);
25+
26+
useEffect(() => {
27+
if (deviceId && videoRef.current) {
28+
// stop the previous track before creating a new one
29+
if (localVideoTrack.current) {
30+
localVideoTrack.current.detach();
31+
localVideoTrack.current.stop();
32+
}
33+
34+
let processor: TwilioBackgroundProcessor | undefined;
35+
if (virtualBackground.type !== 'none') {
36+
processor = createVirtualBackgroundProcessor(virtualBackground);
37+
}
38+
39+
createLocalVideoTrack({
40+
deviceId,
41+
resolution: VideoPresets.h720.resolution,
42+
processor,
43+
}).then((track) => {
44+
localVideoTrack.current = track;
45+
if (videoRef.current) {
46+
localVideoTrack.current.attach(videoRef.current);
47+
}
48+
});
49+
}
50+
51+
return () => {
52+
// stop track on component unmount
53+
if (localVideoTrack.current) {
54+
localVideoTrack.current.stopProcessor(false).then(() => {
55+
localVideoTrack.current?.detach();
56+
localVideoTrack.current?.stop();
57+
});
58+
}
59+
};
60+
}, [deviceId, virtualBackground]);
61+
62+
useEffect(() => {
63+
return () => {
64+
if (localVideoTrack.current) {
65+
localVideoTrack.current.stopProcessor(false).then(() => {
66+
localVideoTrack.current?.detach();
67+
localVideoTrack.current?.stop();
68+
});
69+
}
70+
};
71+
}, []);
72+
73+
return <video ref={videoRef} className="w-full h-full" autoPlay muted />;
74+
};
75+
76+
export default WebcamPreview;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import React from 'react';
2+
import { useTranslation } from 'react-i18next';
3+
import { isSupported } from '@twilio/video-processors';
4+
5+
import { useAppDispatch } from '../../../../store';
6+
import { updateVirtualBackground } from '../../../../store/slices/bottomIconsActivitySlice';
7+
import { BackgroundConfig } from '../../../../helpers/libs/TrackProcessor';
8+
import WebcamPreview from './webcamPreview';
9+
import BackgroundItems from './backgroundItems';
10+
11+
interface WebcamSettingsProps {
12+
deviceId: string;
13+
}
14+
15+
const WebcamSettings = ({ deviceId }: WebcamSettingsProps) => {
16+
const { t } = useTranslation();
17+
const dispatch = useAppDispatch();
18+
19+
const onSelectBg = (bg: BackgroundConfig) => {
20+
dispatch(updateVirtualBackground(bg));
21+
};
22+
23+
return (
24+
<div className="">
25+
<div className="w-full overflow-hidden rounded-lg relative bg-black h-64 3xl:h-80">
26+
<WebcamPreview deviceId={deviceId} />
27+
</div>
28+
{isSupported && (
29+
<>
30+
<div className="title text-xs md:text-sm leading-none text-Gray-700 dark:text-dark-text px-1 md:px-3 uppercase pt-5 3x:pt-8 pb-5">
31+
{t('footer.modal.chose-virtual-bg')}
32+
</div>
33+
<BackgroundItems onSelect={onSelectBg} />
34+
</>
35+
)}
36+
</div>
37+
);
38+
};
39+
40+
export default WebcamSettings;

src/components/landing/index.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { LoadingIcon } from '../../assets/Icons/Loading';
2525

2626
import MicrophoneIcon from './microphone';
2727
import WebcamIcon from './webcam';
28-
import WebcamPreview from './webcamPreview';
28+
import WebcamPreview from '../footer/modals/webcam/webcamPreview';
2929

3030
interface StartupJoinModalProps {
3131
setIsAppReady: Dispatch<boolean>;
@@ -181,7 +181,11 @@ const Landing = ({
181181
</div>
182182
<div className="wrapper bg-Gray-50 dark:bg-dark-secondary pt-4 sm:pt-8 3xl:pt-11 pb-4 sm:pb-10 3xl:pb-14 px-4 sm:px-8 3xl:px-12 flex flex-wrap">
183183
<div className="left relative z-20 bg-Gray-25 dark:bg-Gray-800 shadow-box1 border border-Gray-200 dark:border-Gray-700 p-2 w-full md:w-1/2 rounded-2xl mb-5 sm:mb-0">
184-
<WebcamPreview selectedVideoDevice={selectedVideoDevice} />
184+
<div className="camera bg-Gray-950 rounded-lg overflow-hidden w-full h-56 sm:h-72 3xl:h-80">
185+
{selectedVideoDevice !== '' && (
186+
<WebcamPreview deviceId={selectedVideoDevice} />
187+
)}
188+
</div>
185189
<div className="micro-cam-wrap flex justify-center py-5 gap-5 empty:hidden">
186190
{lockMicrophone ? (
187191
<div className="microphone-wrap relative cursor-not-allowed shadow-IconBox border border-Red-200 rounded-2xl h-11 w-11 flex items-center justify-center transition-all duration-300 text-Gray-950">

src/components/landing/webcamPreview.tsx

Lines changed: 0 additions & 69 deletions
This file was deleted.

0 commit comments

Comments
 (0)