|
| 1 | +import { useCallback, useEffect, useRef } from 'react'; |
| 2 | +import { useCall } from '@stream-io/video-react-bindings'; |
| 3 | + |
| 4 | +type FullScreenBlurType = |
| 5 | + typeof import('@stream-io/video-filters-web').FullScreenBlur; |
| 6 | + |
| 7 | +const isFullScreenBlurPlatformSupported = (): boolean => { |
| 8 | + if ( |
| 9 | + typeof window === 'undefined' || |
| 10 | + typeof OffscreenCanvas === 'undefined' || |
| 11 | + typeof VideoFrame === 'undefined' || |
| 12 | + !window.WebGL2RenderingContext |
| 13 | + ) { |
| 14 | + return false; |
| 15 | + } |
| 16 | + |
| 17 | + try { |
| 18 | + const canvas = new OffscreenCanvas(1, 1); |
| 19 | + return !!canvas.getContext('webgl2', { |
| 20 | + alpha: false, |
| 21 | + antialias: false, |
| 22 | + desynchronized: true, |
| 23 | + }); |
| 24 | + } catch { |
| 25 | + return false; |
| 26 | + } |
| 27 | +}; |
| 28 | + |
| 29 | +export interface ModerationOptions { |
| 30 | + /** |
| 31 | + * How long the moderation effect should stay active before being disabled. |
| 32 | + * Set to `0` to keep it active indefinitely. Defaults to 5000 ms. |
| 33 | + */ |
| 34 | + duration?: number; |
| 35 | +} |
| 36 | + |
| 37 | +export const useModeration = (options?: ModerationOptions) => { |
| 38 | + const { duration = 5000 } = options || {}; |
| 39 | + |
| 40 | + const call = useCall(); |
| 41 | + |
| 42 | + const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null); |
| 43 | + const processorRef = useRef<InstanceType<FullScreenBlurType> | null>(null); |
| 44 | + const unregisterRef = useRef<(() => Promise<void>) | null>(null); |
| 45 | + |
| 46 | + const blurModulePromise = useRef<Promise<FullScreenBlurType> | null>(null); |
| 47 | + |
| 48 | + /** |
| 49 | + * Lazily loads and caches the video-filters-web module. |
| 50 | + */ |
| 51 | + const loadVideoFiltersWebModule = useCallback(() => { |
| 52 | + if (!blurModulePromise.current) { |
| 53 | + blurModulePromise.current = import('@stream-io/video-filters-web') |
| 54 | + .then((module) => module.FullScreenBlur) |
| 55 | + .catch((error) => { |
| 56 | + console.error('[moderation] Failed to import blur module:', error); |
| 57 | + throw error; |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + return blurModulePromise.current; |
| 62 | + }, []); |
| 63 | + |
| 64 | + const disableBlur = useCallback(() => { |
| 65 | + if (timeoutRef.current) { |
| 66 | + clearTimeout(timeoutRef.current); |
| 67 | + timeoutRef.current = null; |
| 68 | + } |
| 69 | + |
| 70 | + unregisterRef |
| 71 | + .current?.() |
| 72 | + .catch((err) => console.error('[moderation] unregister error:', err)); |
| 73 | + |
| 74 | + unregisterRef.current = null; |
| 75 | + }, []); |
| 76 | + |
| 77 | + const handleFallback = useCallback(async () => { |
| 78 | + try { |
| 79 | + await call?.camera.disable(); |
| 80 | + } catch (error) { |
| 81 | + console.error('[moderation] Failed to disable camera:', error); |
| 82 | + } |
| 83 | + }, [call]); |
| 84 | + |
| 85 | + useEffect(() => { |
| 86 | + if (!call) return; |
| 87 | + |
| 88 | + return call.on('call.moderation_warning', async () => { |
| 89 | + try { |
| 90 | + await loadVideoFiltersWebModule(); |
| 91 | + } catch (importErr) { |
| 92 | + console.error('[moderation] Failed to import blur module:', importErr); |
| 93 | + } |
| 94 | + }); |
| 95 | + }, [call, loadVideoFiltersWebModule]); |
| 96 | + |
| 97 | + useEffect(() => { |
| 98 | + if (!call) return; |
| 99 | + |
| 100 | + return call.on('call.moderation_blur', async () => { |
| 101 | + if (unregisterRef.current) return; |
| 102 | + |
| 103 | + let FullScreenBlurClass: FullScreenBlurType; |
| 104 | + |
| 105 | + try { |
| 106 | + FullScreenBlurClass = await loadVideoFiltersWebModule(); |
| 107 | + } catch (importErr) { |
| 108 | + console.error('[moderation] Failed to import blur module:', importErr); |
| 109 | + await handleFallback(); |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + if (!isFullScreenBlurPlatformSupported()) { |
| 114 | + console.warn('[moderation] Blur not supported on this platform'); |
| 115 | + await handleFallback(); |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + const { unregister } = call.camera.registerFilter((inputStream) => { |
| 120 | + unregisterRef.current = unregister; |
| 121 | + |
| 122 | + const outputPromise = new Promise<MediaStream>( |
| 123 | + async (resolve, reject) => { |
| 124 | + const [track] = inputStream.getVideoTracks(); |
| 125 | + |
| 126 | + let processor: InstanceType<FullScreenBlurType>; |
| 127 | + |
| 128 | + try { |
| 129 | + processor = new FullScreenBlurClass(track); |
| 130 | + processorRef.current = processor; |
| 131 | + |
| 132 | + const result = await processor.start(); |
| 133 | + const output = new MediaStream([result]); |
| 134 | + resolve(output); |
| 135 | + |
| 136 | + if (duration > 0) { |
| 137 | + timeoutRef.current = setTimeout(disableBlur, duration); |
| 138 | + } |
| 139 | + } catch (error) { |
| 140 | + reject(error); |
| 141 | + console.error('[moderation] Processor init failed:', error); |
| 142 | + |
| 143 | + await unregisterRef.current?.(); |
| 144 | + unregisterRef.current = null; |
| 145 | + processorRef.current = null; |
| 146 | + |
| 147 | + await handleFallback(); |
| 148 | + return; |
| 149 | + } |
| 150 | + }, |
| 151 | + ); |
| 152 | + |
| 153 | + return { |
| 154 | + output: outputPromise, |
| 155 | + stop: () => { |
| 156 | + if (processorRef.current) { |
| 157 | + processorRef.current.stop(); |
| 158 | + processorRef.current = null; |
| 159 | + } |
| 160 | + }, |
| 161 | + }; |
| 162 | + }); |
| 163 | + }); |
| 164 | + }, [call, loadVideoFiltersWebModule, disableBlur, handleFallback, duration]); |
| 165 | + |
| 166 | + useEffect(() => disableBlur, [disableBlur]); |
| 167 | +}; |
0 commit comments