Commit f2b1cb43 authored by Shehan Liyanage's avatar Shehan Liyanage

add all changes

parent 14001e04
This diff is collapsed.
No preview for this file type
import React, { useEffect, useRef, useState } from 'react' import React, { useEffect, useRef, useState } from 'react'
import { Box } from '@mui/material' import { Box } from '@mui/material'
import { useTheme, type Theme } from '@mui/material/styles' import { useTheme, Theme } from '@mui/material/styles'
import { useRemoteUsers, usePublish, useRTCClient, useClientEvent } from 'agora-rtc-react' import { useRemoteUsers, usePublish, useRTCClient, useClientEvent } from 'agora-rtc-react'
import SymmetricGrid from '../SymmetricGrid/SymmetricGrid' import SymmetricGrid from '../SymmetricGrid/SymmetricGrid'
import PinnedGrid from '../PinnedGrid/PinnedGrid' import PinnedGrid from '../PinnedGrid/PinnedGrid'
...@@ -10,6 +10,7 @@ import type { ...@@ -10,6 +10,7 @@ import type {
IMicrophoneAudioTrack, IMicrophoneAudioTrack,
} from 'agora-rtc-sdk-ng' } from 'agora-rtc-sdk-ng'
import ControlCenter from '../ControlCenter/ControlCenter' import ControlCenter from '../ControlCenter/ControlCenter'
import { useSocketContext } from '../../context/SocketContext'
interface Props { interface Props {
setJoined: (x: boolean) => void setJoined: (x: boolean) => void
...@@ -19,6 +20,7 @@ interface Props { ...@@ -19,6 +20,7 @@ interface Props {
const MeetingLayout = ({ setJoined, localCameraTrack, localMicrophoneTrack }: Props) => { const MeetingLayout = ({ setJoined, localCameraTrack, localMicrophoneTrack }: Props) => {
const theme: Theme = useTheme() const theme: Theme = useTheme()
const { prediction } = useSocketContext()
const agoraEngine = useRTCClient() const agoraEngine = useRTCClient()
usePublish([localMicrophoneTrack, localCameraTrack]) usePublish([localMicrophoneTrack, localCameraTrack])
...@@ -100,8 +102,13 @@ const MeetingLayout = ({ setJoined, localCameraTrack, localMicrophoneTrack }: Pr ...@@ -100,8 +102,13 @@ const MeetingLayout = ({ setJoined, localCameraTrack, localMicrophoneTrack }: Pr
backgroundColor: '#363739', backgroundColor: '#363739',
borderRadius: '20px', borderRadius: '20px',
marginRight: '20px', marginRight: '20px',
display: 'flex',
alignItems: 'center',
color: 'white',
}} }}
></Box> >
{prediction}
</Box>
)} )}
<Box sx={{ height: '10%', width: '100%', display: 'flex', marginBottom: '40px' }}> <Box sx={{ height: '10%', width: '100%', display: 'flex', marginBottom: '40px' }}>
<ControlCenter <ControlCenter
......
import React, { createContext, useContext, useEffect } from 'react' import React, { createContext, useContext, useEffect, useRef, useState } from 'react'
import { useJoin, useLocalCameraTrack, useLocalMicrophoneTrack } from 'agora-rtc-react' import { useJoin, useLocalCameraTrack, useLocalMicrophoneTrack } from 'agora-rtc-react'
import type { IMicrophoneAudioTrack, ICameraVideoTrack } from 'agora-rtc-sdk-ng' import type { IMicrophoneAudioTrack, ICameraVideoTrack } from 'agora-rtc-sdk-ng'
import { Box } from '@mui/material' import { Box } from '@mui/material'
import { useTheme, type Theme } from '@mui/material/styles' import { useTheme, Theme } from '@mui/material/styles'
import MeetingLoading from '../../Components/MeetingLoading/MeetingLoading' import MeetingLoading from '../../Components/MeetingLoading/MeetingLoading'
import MeetingLayout from '../../Components/MeetingLayout/MeetingLayout' import MeetingLayout from '../../Components/MeetingLayout/MeetingLayout'
import { useParams } from 'react-router-dom' import { useParams } from 'react-router-dom'
import { useSocketContext } from '../../context/SocketContext'
interface AgoraContextType { interface AgoraContextType {
localCameraTrack: ICameraVideoTrack | null localCameraTrack: ICameraVideoTrack | null
...@@ -58,10 +59,38 @@ export const AgoraManager = ({ ...@@ -58,10 +59,38 @@ export const AgoraManager = ({
const { isLoading: isLoadingCam, localCameraTrack } = useLocalCameraTrack() const { isLoading: isLoadingCam, localCameraTrack } = useLocalCameraTrack()
const { isLoading: isLoadingMic, localMicrophoneTrack } = useLocalMicrophoneTrack() const { isLoading: isLoadingMic, localMicrophoneTrack } = useLocalMicrophoneTrack()
const { socket } = useSocketContext()
useEffect(() => { useEffect(() => {
localStorage.setItem('myUid', uid.toString()) localStorage.setItem('myUid', uid.toString())
}, [uid]) }, [uid])
useEffect(() => {
const sendFrames = () => {
if (localCameraTrack) {
const canvas = document.createElement('canvas')
const video = document.createElement('video')
video.srcObject = new MediaStream([localCameraTrack.getMediaStreamTrack()])
video.onloadedmetadata = () => {
canvas.width = video.videoWidth
canvas.height = video.videoHeight
const ctx = canvas.getContext('2d')
ctx?.drawImage(video, 0, 0, video.videoWidth, video.videoHeight)
const frame = canvas.toDataURL('image/jpeg', 0.5)
socket.emit('predictionVideo', frame)
}
}
}
if (localCameraTrack) {
const intervalId = setInterval(sendFrames, 2000)
return () => clearInterval(intervalId)
}
}, [localCameraTrack, socket])
useEffect(() => { useEffect(() => {
return () => { return () => {
localCameraTrack?.close() localCameraTrack?.close()
......
import React, { createContext, useContext, useEffect, useState } from 'react'
import { io, Socket } from 'socket.io-client'
interface SocketContextType {
socket: Socket
prediction: string
}
const SocketContext = createContext<SocketContextType | null>(null)
export const SocketProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
const socket: Socket = io('http://127.0.0.1:5000')
const [prediction, setPrediction] = useState('')
useEffect(() => {
socket.on('predictionVideo', (predictedVal: string) => {
setPrediction(predictedVal)
})
return () => {
socket.off('predictionVideo')
}
}, [socket])
return <SocketContext.Provider value={{ socket, prediction }}>{children}</SocketContext.Provider>
}
export const useSocketContext = () => {
const context = useContext(SocketContext)
if (!context) throw new Error('useSocketContext must be used within a SocketProvider')
return context
}
...@@ -3,12 +3,15 @@ import ReactDOM from 'react-dom/client' ...@@ -3,12 +3,15 @@ import ReactDOM from 'react-dom/client'
import './index.css' import './index.css'
import App from './App' import App from './App'
import { BrowserRouter } from 'react-router-dom' import { BrowserRouter } from 'react-router-dom'
import { SocketProvider } from './context/SocketContext'
const root = ReactDOM.createRoot(document.getElementById('root') ?? document.body) const root = ReactDOM.createRoot(document.getElementById('root') ?? document.body)
root.render( root.render(
<React.StrictMode> <React.StrictMode>
<BrowserRouter> <BrowserRouter>
<App /> <SocketProvider>
<App />
</SocketProvider>
</BrowserRouter> </BrowserRouter>
</React.StrictMode>, </React.StrictMode>,
) )
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment