-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- still should not work without a fixing upstream LK: livekit/components-js#1042 livekit/components-js#1043
- Loading branch information
Showing
6 changed files
with
204 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
Copyright 2024 New Vector Ltd. | ||
SPDX-License-Identifier: AGPL-3.0-only | ||
Please see LICENSE in the repository root for full details. | ||
*/ | ||
|
||
import { | ||
BackgroundBlur as backgroundBlur, | ||
BackgroundOptions, | ||
ProcessorWrapper, | ||
} from "@livekit/track-processors"; | ||
import { | ||
createContext, | ||
FC, | ||
useCallback, | ||
useContext, | ||
useEffect, | ||
useRef, | ||
useState, | ||
} from "react"; | ||
import { logger } from "matrix-js-sdk/src/logger"; | ||
import { LocalVideoTrack } from "livekit-client"; | ||
|
||
import { | ||
backgroundBlur as backgroundBlurSettings, | ||
useSetting, | ||
} from "../settings/settings"; | ||
|
||
type ProcessorState = { | ||
supported: boolean | undefined; | ||
processor: undefined | ProcessorWrapper<BackgroundOptions>; | ||
/** | ||
* Call this method to try to initialize a processor. | ||
* This only needs to happen if supported is undefined. | ||
* If the backgroundBlur setting is set to true this does not need to be called | ||
* and the processorState.supported will update automatically to the correct value. | ||
*/ | ||
checkSupported: () => void; | ||
}; | ||
const ProcessorContext = createContext<ProcessorState | undefined>(undefined); | ||
|
||
export const useTrackProcessor = (): ProcessorState | undefined => | ||
useContext(ProcessorContext); | ||
|
||
export const useTrackProcessorSync = ( | ||
videoTrack: LocalVideoTrack | null, | ||
): void => { | ||
const { processor } = useTrackProcessor() || {}; | ||
useEffect(() => { | ||
if (processor && !videoTrack?.getProcessor()) { | ||
void videoTrack?.setProcessor(processor); | ||
} | ||
if (!processor && videoTrack?.getProcessor()) { | ||
void videoTrack?.stopProcessor(); | ||
} | ||
}, [processor, videoTrack]); | ||
}; | ||
|
||
interface Props { | ||
children: JSX.Element; | ||
} | ||
export const ProcessorProvider: FC<Props> = ({ children }) => { | ||
// The setting the user wants to have | ||
const [blurActivated] = useSetting(backgroundBlurSettings); | ||
|
||
// If `ProcessorState.supported` is undefined the user can activate that we want | ||
// to have it at least checked (this is useful to show the settings menu properly) | ||
// We dont want to try initializing the blur if the user is not even looking at the setting | ||
const [shouldCheckSupport, setShouldCheckSupport] = useState(blurActivated); | ||
|
||
// Cache the processor so we only need to initialize it once. | ||
const blur = useRef<ProcessorWrapper<BackgroundOptions> | undefined>( | ||
undefined, | ||
); | ||
|
||
const checkSupported = useCallback(() => { | ||
setShouldCheckSupport(true); | ||
}, []); | ||
// This is the actual state exposed through the context | ||
const [processorState, setProcessorState] = useState<ProcessorState>(() => ({ | ||
supported: false, | ||
processor: undefined, | ||
checkSupported, | ||
})); | ||
|
||
useEffect(() => { | ||
if (!shouldCheckSupport) return; | ||
try { | ||
if (!blur.current) blur.current = backgroundBlur(15, { delegate: "GPU" }); | ||
setProcessorState({ | ||
checkSupported, | ||
supported: true, | ||
processor: blurActivated ? blur.current : undefined, | ||
}); | ||
} catch (e) { | ||
setProcessorState({ | ||
checkSupported, | ||
supported: false, | ||
processor: undefined, | ||
}); | ||
logger.error("disable background blur", e); | ||
} | ||
}, [blurActivated, checkSupported, shouldCheckSupport]); | ||
|
||
return ( | ||
<ProcessorContext.Provider value={processorState}> | ||
{children} | ||
</ProcessorContext.Provider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.