|
| 1 | +import { |
| 2 | + StreamVideoParticipant, |
| 3 | + type VideoTrackType, |
| 4 | + getLogger, |
| 5 | +} from '@stream-io/video-client'; |
| 6 | +import React, { createContext, useContext, useRef, RefObject } from 'react'; |
| 7 | +import { NativeModules, findNodeHandle, Platform } from 'react-native'; |
| 8 | + |
| 9 | +const { StreamVideoReactNative } = NativeModules; |
| 10 | + |
| 11 | +type SnapshotContextType = { |
| 12 | + register: ( |
| 13 | + participant: StreamVideoParticipant, |
| 14 | + videoTrackType: VideoTrackType, |
| 15 | + ref: RefObject<any>, |
| 16 | + ) => void; |
| 17 | + deregister: ( |
| 18 | + participant: StreamVideoParticipant, |
| 19 | + videoTrackType: VideoTrackType, |
| 20 | + ) => void; |
| 21 | + take: ( |
| 22 | + participant: StreamVideoParticipant, |
| 23 | + videoTrackType: VideoTrackType, |
| 24 | + ) => Promise<string | null>; |
| 25 | +}; |
| 26 | + |
| 27 | +// Create the context with a default undefined value |
| 28 | +const SnapshotContext = createContext<SnapshotContextType | undefined>( |
| 29 | + undefined, |
| 30 | +); |
| 31 | + |
| 32 | +// Reference map type |
| 33 | +type RefMap = Map<string, RefObject<any>>; |
| 34 | + |
| 35 | +export const SnapshotProvider = ({ children }: React.PropsWithChildren<{}>) => { |
| 36 | + // Use a ref to store the map of participant IDs to their view refs |
| 37 | + const participantRefs = useRef<RefMap>(new Map()); |
| 38 | + |
| 39 | + // Register a participant's RTCView ref |
| 40 | + const register = ( |
| 41 | + participant: StreamVideoParticipant, |
| 42 | + videoTrackType: VideoTrackType, |
| 43 | + ref: RefObject<any>, |
| 44 | + ) => { |
| 45 | + if (ref && participant.userId) { |
| 46 | + participantRefs.current.set( |
| 47 | + `${participant.userId}-${videoTrackType}`, |
| 48 | + ref, |
| 49 | + ); |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + const deregister = ( |
| 54 | + participant: StreamVideoParticipant, |
| 55 | + videoTrackType: VideoTrackType, |
| 56 | + ) => { |
| 57 | + if (participant.userId) { |
| 58 | + participantRefs.current.delete(`${participant.userId}-${videoTrackType}`); |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + // Take a snapshot of a specific participant's view |
| 63 | + const take = async ( |
| 64 | + participant: StreamVideoParticipant, |
| 65 | + videoTrackType: VideoTrackType, |
| 66 | + ): Promise<string | null> => { |
| 67 | + try { |
| 68 | + if (Platform.OS !== 'ios') { |
| 69 | + throw new Error('SnapshotProvider is only supported on iOS'); |
| 70 | + } |
| 71 | + |
| 72 | + if (!participant?.userId) { |
| 73 | + getLogger(['SnapshotProvider'])( |
| 74 | + 'error', |
| 75 | + 'Cannot take snapshot: Invalid participant', |
| 76 | + ); |
| 77 | + return null; |
| 78 | + } |
| 79 | + |
| 80 | + const ref = participantRefs.current.get( |
| 81 | + `${participant.userId}-${videoTrackType}`, |
| 82 | + ); |
| 83 | + if (!ref || !ref.current) { |
| 84 | + getLogger(['SnapshotProvider'])( |
| 85 | + 'error', |
| 86 | + 'Cannot take snapshot: No registered view for this participant', |
| 87 | + ); |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | + // Get the native handle for the view |
| 92 | + const tag = findNodeHandle(ref.current); |
| 93 | + if (!tag) { |
| 94 | + getLogger(['SnapshotProvider'])( |
| 95 | + 'error', |
| 96 | + 'Cannot take snapshot: Cannot get native handle for view', |
| 97 | + ); |
| 98 | + return null; |
| 99 | + } |
| 100 | + |
| 101 | + // Take the snapshot using our native module |
| 102 | + const base64Image = await StreamVideoReactNative.captureRef(tag, { |
| 103 | + // format: 'jpg', |
| 104 | + // quality: 0.8, |
| 105 | + }); |
| 106 | + |
| 107 | + return base64Image; |
| 108 | + } catch (error) { |
| 109 | + getLogger(['SnapshotProvider'])( |
| 110 | + 'error', |
| 111 | + 'Error taking participant snapshot:', |
| 112 | + error, |
| 113 | + ); |
| 114 | + return null; |
| 115 | + } |
| 116 | + }; |
| 117 | + |
| 118 | + const value = { |
| 119 | + register, |
| 120 | + deregister, |
| 121 | + take, |
| 122 | + }; |
| 123 | + |
| 124 | + return ( |
| 125 | + <SnapshotContext.Provider value={value}> |
| 126 | + {children} |
| 127 | + </SnapshotContext.Provider> |
| 128 | + ); |
| 129 | +}; |
| 130 | + |
| 131 | +export const useSnapshot = (): SnapshotContextType => { |
| 132 | + const context = useContext(SnapshotContext); |
| 133 | + if (!context) { |
| 134 | + throw new Error('useSnapshot must be used within a SnapshotProvider'); |
| 135 | + } |
| 136 | + return context; |
| 137 | +}; |
0 commit comments