import { useMemo , useState } from "react" ; import { FlectChimeClient , RealtimeData , VideoTileState , VirtualBackgroundSegmentationType , NoiseSuppressionType , AttendeeState , TranscriptionScript } from "@dannadori/flect-amazon-chime-lib2" ; export type UseChimeClientProps = { RestAPIEndpoint : string ; } ; export type ChimeClientState = { meetingName : string | null ; meetingId : string | null ; userName : string | null ; attendeeId : string | null ; joinToken : string | null ; attendees : { [ attendeeId ] : AttendeeState ; } ; initialize : ( userId : string , idToken : string , accessToken : string , refreshToken : string ) => void ; initializeWithCode : ( codeToAccess : string ) => void ; isShareContent : boolean ; startShareContent : ( media : MediaStream ) => Promise < void > ; stopShareContent : ( ) => Promise < void > ; getMeetingInfo : ( meetingName : string , userName ? : string | null ) => Promise < void > ; createMeeting : ( meetingName : string , region : string ) => Promise < void > ; joinMeeting : ( meetingName : string , userName : string ) => Promise < void > ; enterMeeting : ( ) => Promise < void > ; leaveMeeting : ( ) => Promise < void > ; startLocalVideoTile : ( ) => void ; stopLocalVideoTile : ( ) => void ; setPreviewVideoElement : ( val : HTMLVideoElement ) => void ; setOutputAudioElement : ( val : HTMLAudioElement ) => void ; startPreview : ( ) => void ; stopPreview : ( ) => void ; bindVideoElement : ( tileId : number , videoElement : HTMLVideoElement ) => void ; unbindVideoElement : ( tileId : number ) => void ; audioInputDevice : string | MediaStream ; noiseSuppressionType : NoiseSuppressionType ; videoInputDevice : string | MediaStream ; virtualBackgroundSegmentationType : VirtualBackgroundSegmentationType ; virtualBackgroundEnable : boolean ; audioOutputDevice : string ; audioInputEnable : boolean ; isMuted : boolean ; noiseSuppressionEnable : boolean ; videoInputEnable : boolean ; audioOutputEnable : boolean ; setVideoInput : ( val : string | MediaStream | null ) => Promise < void > ; setVirtualBackgroundSegmentationType : ( val : VirtualBackgroundSegmentationType ) => Promise < void > ; setVirtualBackgroundEnable : ( val : boolean ) => Promise < void > ; setBackgroundImagePath : ( path : string ) => Promise < void > ; setAudioInput : ( val : string | MediaStream | null ) => Promise < void > ; setAudioOutput : ( val : string | null ) => Promise < void > ; setAudioInputEnable : ( val : boolean ) => Promise < void > ; setMute : ( val : boolean ) => Promise < void > ; setNoiseSuppressionEnable : ( val : boolean ) => Promise < void > ; setNoiseSuppressionType : ( value : NoiseSuppressionType ) => Promise < void > ; setVideoInputEnable : ( val : boolean ) => Promise < void > ; setAudioOutputEnable : ( val : boolean ) => Promise < void > ; pauseVideo : ( attendeeId : string , pause : boolean ) => Promise < void > ; getContentTiles : ( ) => VideoTileState [ ] ; getActiveSpeakerTile : ( ) => VideoTileState | null ; getAllTiles : ( ) => VideoTileState [ ] ; setBackgroundMusic : ( stream : MediaStream ) => void ; setBackgroundMusicVolume : ( volume : number ) => void ; getUserNameByAttendeeIdFromList : ( attendeeId : string ) => string ; sendChatData : ( text : string ) => void ; chatData : RealtimeData [ ] ; setTranscribeEnable : ( val : boolean ) => void ; setTranscribeLang : ( val : TranscribeLangs ) => Promise < void > ; transcribeEnable : boolean ; transcribeLang : TranscribeLangs ; transcriptionScripts : TranscriptionScript [ ] ; transcriptionPartialScript : TranscriptionScript | null ; } ; export const TranscribeLangs = { English_US : "en-US" , English_British : "en-GB" , English_Australian : "en-AU" , Spanish_US : "es-US" , French_Canadian : "fr-CA" , French : "fr-FR" , Italian : "it-IT" , German : "de-DE" , Portuguese_Brazilian : "pt-BR" , Japanese : "ja-JP" , Korean : "ko-KR" , Chinese_Mandarin : "zh-CN" , } as const ; export type TranscribeLangs = typeof TranscribeLangs [ keyof typeof TranscribeLangs ] ; export const useChimeClient = ( props ) => { const [ _lastUpdateTime , setLastUpdateTime ] = useState ( 0 ) ; const [ transcribeEnable , _setTranscribeEnable ] = useState ( false ) ; const [ transcribeLang , _setTranscribeLang ] = useState < TranscribeLangs > ( TranscribeLangs . Japanese ) ; const setTranscribeEnable = async ( val ) => { if ( val ) { await chimeClient . startTranscribe ( transcribeLang ) ; } else { await chimeClient . stopTranscribe ( ) ; } _setTranscribeEnable ( val ) ; } ; const setTranscribeLang = async ( val ) => { if ( transcribeEnable ) { await chimeClient . stopTranscribe ( ) ; await chimeClient . startTranscribe ( val ) ; } _setTranscribeLang ( val ) ; } ; const chimeClient = useMemo ( ( ) => { const c = new FlectChimeClient ( props . RestAPIEndpoint ) ; c . setFlectChimeClientListener ( { meetingStateUpdated : ( ) => { setLastUpdateTime ( new Date ( ) . getTime ( ) ) ; } , activeSpekaerUpdated : ( activeSpeakerId ) => { setLastUpdateTime ( new Date ( ) . getTime ( ) ) ; } , attendeesUpdated : ( attendeeList ) => { setLastUpdateTime ( new Date ( ) . getTime ( ) ) ; } , videoTileStateUpdated : ( videoTileStateList ) => { setLastUpdateTime ( new Date ( ) . getTime ( ) ) ; } , } ) ; c . setRealtimeSubscribeChatClientListener ( { chatDataUpdated : ( list ) => { console . log ( "chat data updated!" , list ) ; setLastUpdateTime ( new Date ( ) . getTime ( ) ) ; } , } ) ; c . setRealtimeSubscribeTranscriptionClientListener ( { transcriptionStatusUpdated : ( ) => { setLastUpdateTime ( new Date ( ) . getTime ( ) ) ; } , } ) ; return c ; } , [ ] ) ; const initialize = ( userId , idToken , accessToken , refreshToken ) => { chimeClient . init ( userId , idToken , accessToken , refreshToken ) ; setLastUpdateTime ( new Date ( ) . getTime ( ) ) ; } ; const initializeWithCode = ( codeToAccess ) => { chimeClient . initWithCode ( codeToAccess ) ; setLastUpdateTime ( new Date ( ) . getTime ( ) ) ; } ; const startShareContent = async ( media ) => { await chimeClient . startShareContent ( media ) ; setLastUpdateTime ( new Date ( ) . getTime ( ) ) ; } ; const stopShareContent = async ( ) => { await chimeClient . stopShareContent ( ) ; setLastUpdateTime ( new Date ( ) . getTime ( ) ) ; } ; /**
     * (A-) create meeting
     */ const getMeetingInfo = async ( meetingName , userName = null ) => { await chimeClient . getMeetingInfo ( meetingName , userName ) ; setLastUpdateTime ( new Date ( ) . getTime ( ) ) ; } ; /**
     * (A) create meeting
     */ const createMeeting = async ( meetingName , region ) => { await chimeClient . createMeeting ( meetingName , region ) ; setLastUpdateTime ( new Date ( ) . getTime ( ) ) ; } ; /**
     * (B) Join Meeting Room
     */ const joinMeeting = async ( meetingName , userName ) => { await chimeClient . joinMeeting ( meetingName , userName ) ; setLastUpdateTime ( new Date ( ) . getTime ( ) ) ; } ; /**
     * (C) Enter Meeting Room
     */ const enterMeeting = async ( ) => { await chimeClient . enterMeeting ( ) ; setLastUpdateTime ( new Date ( ) . getTime ( ) ) ; } ; /**
     * (D) leave meeting
     */ const leaveMeeting = async ( ) => { await chimeClient . leaveMeeting ( ) ; setLastUpdateTime ( new Date ( ) . getTime ( ) ) ; } ; const startLocalVideoTile = ( ) => { chimeClient . videoInputDeviceSetting ! . startLocalVideoTile ( ) ; } ; const stopLocalVideoTile = ( ) => { chimeClient . videoInputDeviceSetting ! . stopLocalVideoTile ( ) ; } ; const setPreviewVideoElement = ( val ) => { chimeClient . videoInputDeviceSetting ! . setPreviewVideoElement ( val ) ; } ; const setOutputAudioElement = ( val ) => { chimeClient . audioOutputDeviceSetting ! . setOutputAudioElement ( val ) ; } ; const startPreview = ( ) => { chimeClient . videoInputDeviceSetting ! . startPreview ( ) ; } ; const stopPreview = ( ) => { chimeClient . videoInputDeviceSetting ! . stopPreview ( ) ; } ; const bindVideoElement = ( tileId , videoElement ) => { chimeClient . meetingSession ! . audioVideo . bindVideoElement ( tileId , videoElement ) ; } ; const unbindVideoElement = ( tileId ) => { chimeClient . meetingSession ! . audioVideo . unbindVideoElement ( tileId ) ; } ; const setVideoInput = async ( val ) => { await chimeClient . videoInputDeviceSetting ! . setVideoInput ( val ) ; } ; const setVirtualBackgroundEnable = async ( val ) => { await chimeClient . videoInputDeviceSetting ! . setVirtualBackgrounEnable ( val ) ; } ; const setVirtualBackgroundSegmentationType = async ( val ) => { await chimeClient . videoInputDeviceSetting ! . setVirtualBackgroundSegmentationType ( val ) ; } ; const setBackgroundImagePath = async ( path ) => { await chimeClient . videoInputDeviceSetting ! . setBackgroundImagePath ( path ) ; } ; const setAudioInput = async ( val ) => { await chimeClient . audioInputDeviceSetting ! . setAudioInput ( val ) ; } ; const setAudioOutput = async ( val ) => { await chimeClient . audioOutputDeviceSetting ! . setAudioOutput ( val ) ; } ; const setAudioInputEnable = async ( val ) => { await chimeClient . audioInputDeviceSetting ! . setAudioInputEnable ( val ) ; setLastUpdateTime ( new Date ( ) . getTime ( ) ) ; } ; const setMute = async ( val ) => { if ( val ) { chimeClient . audioInputDeviceSetting ?. mute ( ) ; } else { chimeClient . audioInputDeviceSetting ?. unmute ( ) ; } setLastUpdateTime ( new Date ( ) . getTime ( ) ) ; } ; const setNoiseSuppressionEnable = async ( val ) => { await chimeClient . audioInputDeviceSetting ?. setNoiseSuppressionEnable ( val ) ; } ; const setNoiseSuppressionType = async ( value ) => { await chimeClient . audioInputDeviceSetting ?. setNoiseSuppressionType ( value ) ; } ; const setVideoInputEnable = async ( val ) => { await chimeClient . videoInputDeviceSetting ! . setVideoInputEnable ( val ) ; setLastUpdateTime ( new Date ( ) . getTime ( ) ) ; } ; const setAudioOutputEnable = async ( val ) => { await chimeClient . audioOutputDeviceSetting ! . setAudioOutputEnable ( val ) ; setLastUpdateTime ( new Date ( ) . getTime ( ) ) ; } ; const pauseVideo = async ( attendeeId , pause ) => { await chimeClient . setPauseVideo ( attendeeId , pause ) ; } ; const getContentTiles = ( ) => { return chimeClient . getContentTiles ( ) ; } ; const getActiveSpeakerTile = ( ) => { return chimeClient . getActiveSpeakerTile ( ) ; } ; const getAllTiles = ( ) => { return chimeClient . getAllTiles ( ) ; } ; const setBackgroundMusic = ( stream ) => { chimeClient . audioInputDeviceSetting ?. setBackgroundMusic ( stream ) ; } ; const setBackgroundMusicVolume = ( volume ) => { chimeClient . audioInputDeviceSetting ?. setBackgroundMusicVolume ( volume ) ; } ; const getUserNameByAttendeeIdFromList = ( attendeeId ) => { return chimeClient . getUserNameByAttendeeIdFromList ( attendeeId ) ; } ; const sendChatData = ( text ) => { chimeClient . chatClient ! . sendChatData ( text ) ; chimeClient . chatClient ?. chatData ; } ; const returnValue = { meetingName : chimeClient . meetingName , meetingId : chimeClient . meetingId , userName : chimeClient . userName , attendeeId : chimeClient . meetingSession ?. configuration . credentials ?. attendeeId || null , joinToken : chimeClient . joinToken , attendees : chimeClient . attendees , initialize , initializeWithCode , isShareContent : chimeClient . isShareContent , startShareContent , stopShareContent , getMeetingInfo , createMeeting , joinMeeting , enterMeeting , leaveMeeting , startLocalVideoTile , stopLocalVideoTile , setPreviewVideoElement , setOutputAudioElement , startPreview , stopPreview , bindVideoElement , unbindVideoElement , audioInputDevice : chimeClient . audioInputDeviceSetting ?. audioInput || "None" , noiseSuppressionType : chimeClient . audioInputDeviceSetting ?. voiceFocusSpec ?. variant || "None" , noiseSuppressionEnable : chimeClient . audioInputDeviceSetting ?. audioSuppressionEnable || false , videoInputDevice : chimeClient . videoInputDeviceSetting ?. videoInput || "None" , virtualBackgroundSegmentationType : chimeClient . videoInputDeviceSetting ?. virtualBackgroundSegmentationType || "None" , virtualBackgroundEnable : chimeClient . videoInputDeviceSetting ?. virtualBackgroundEnable || false , audioOutputDevice : chimeClient . audioOutputDeviceSetting ?. audioOutput || "None" , audioInputEnable : chimeClient . audioInputDeviceSetting ? chimeClient . audioInputDeviceSetting . audioInputEnable : false , isMuted : chimeClient . meetingSession ?. audioVideo . realtimeIsLocalAudioMuted ( ) || false , videoInputEnable : chimeClient . videoInputDeviceSetting ? chimeClient . videoInputDeviceSetting . videoInputEnable : false , audioOutputEnable : chimeClient . audioOutputDeviceSetting ? chimeClient . audioOutputDeviceSetting . audioOutputEnable : false , setAudioInput , setVideoInput , setVirtualBackgroundSegmentationType , setVirtualBackgroundEnable , setBackgroundImagePath , setAudioOutput , setAudioInputEnable , setMute , setNoiseSuppressionEnable , setNoiseSuppressionType , setVideoInputEnable , setAudioOutputEnable , pauseVideo , getContentTiles , getActiveSpeakerTile , getAllTiles , setBackgroundMusic , setBackgroundMusicVolume , getUserNameByAttendeeIdFromList , sendChatData , chatData : chimeClient . chatClient ?. chatData || [ ] , setTranscribeEnable , setTranscribeLang , transcribeEnable , transcribeLang , transcriptionScripts : chimeClient . transcriptionClient ?. transcriptionScripts || [ ] , transcriptionPartialScript : chimeClient . transcriptionClient ?. transcriptionPartialScript || null , } ; return returnValue ; } ;