From ca0605fa391fa60b702c0705b53625037460bd3a Mon Sep 17 00:00:00 2001 From: Sinan Date: Mon, 30 Sep 2024 18:05:51 +0530 Subject: [PATCH] =?UTF-8?q?remote=20video=20in=20progress=20=F0=9F=9A=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../video-section/[sectionId]/page.tsx | 50 ++--- .../doctor/video-call/[sectionId]/page.tsx | 50 ++--- .../button/VideoSectionButtonPatient.tsx | 19 +- .../page-components/video/VideoChat.tsx | 200 ++++++++---------- client/lib/hooks/useVideoCall.ts | 94 ++++++++ client/lib/webrtc/createPeerConnection.ts | 80 ++++--- client/types/index.ts | 13 +- 7 files changed, 288 insertions(+), 218 deletions(-) create mode 100644 client/lib/hooks/useVideoCall.ts diff --git a/client/app/(patient)/video-section/[sectionId]/page.tsx b/client/app/(patient)/video-section/[sectionId]/page.tsx index 64b34221..56d7d3a2 100644 --- a/client/app/(patient)/video-section/[sectionId]/page.tsx +++ b/client/app/(patient)/video-section/[sectionId]/page.tsx @@ -1,49 +1,27 @@ 'use client' -import { useEffect, useState } from 'react'; import { useParams } from 'next/navigation'; import JoinPage from './Join-page'; import VideoChat from '@/components/page-components/video/VideoChat'; -import createPeerConnection from '@/lib/webrtc/createPeerConnection'; import { useGetSectionByIdPatient } from '@/lib/hooks/video/usePatient'; +import { useVideoCall } from '@/lib/hooks/useVideoCall'; export default function PatientVideoCallPage() { const { sectionId } = useParams(); - const [hasJoined, setHasJoined] = useState(false); - const [localStream, setLocalStream] = useState(null); - const [remoteStream, setRemoteStream] = useState(null); const { data, isLoading } = useGetSectionByIdPatient(sectionId as string); const section = data?.section; - useEffect(() => { - if (hasJoined && section && localStream) { - createPeerConnection(section.roomId ?? "id", 'patient', localStream).then(connection => { - if (connection) { - setRemoteStream(connection.remoteStream); - return () => connection.peerConnection.close(); - } - }); - } - }, [hasJoined, section, localStream]); - - const handleJoin = async () => { - try { - const stream = await navigator.mediaDevices.getUserMedia({ video: true, audio: true }); - setLocalStream(stream); - setHasJoined(true); - } catch (error) { - console.error('Error accessing media devices:', error); - } - }; - - const handleEndCall = () => { - if (localStream) { - localStream.getTracks().forEach(track => track.stop()); - } - setLocalStream(null); - setRemoteStream(null); - setHasJoined(false); - }; + const { + hasJoined, + localStream, + remoteStream, + handleJoin, + handleEndCall, + isMuted, + isVideoOff, + toggleMute, + toggleVideo + } = useVideoCall(section, 'patient'); if (isLoading) return
Loading...
; @@ -53,6 +31,10 @@ export default function PatientVideoCallPage() { return ( (null); - const [remoteStream, setRemoteStream] = useState(null); const { data, isLoading } = useGetSectionByIdDoctor(sectionId as string); const section = data?.section; - useEffect(() => { - if (hasJoined && section && localStream) { - createPeerConnection(section.roomId ?? "id", 'doctor', localStream).then(connection => { - if (connection) { - setRemoteStream(connection.remoteStream); - return () => connection.peerConnection.close(); - } - }); - } - }, [hasJoined, section, localStream]); - - const handleJoin = async () => { - try { - const stream = await navigator.mediaDevices.getUserMedia({ video: true, audio: true }); - setLocalStream(stream); - setHasJoined(true); - } catch (error) { - console.error('Error accessing media devices:', error); - } - }; - - const handleEndCall = () => { - if (localStream) { - localStream.getTracks().forEach(track => track.stop()); - } - setLocalStream(null); - setRemoteStream(null); - setHasJoined(false); - }; + const { + hasJoined, + localStream, + remoteStream, + handleJoin, + handleEndCall, + isMuted, + isVideoOff, + toggleMute, + toggleVideo + } = useVideoCall(section, 'doctor'); if (isLoading) return
Loading...
; @@ -53,6 +31,10 @@ export default function DoctorVideoCallPage() { return ( { const handleClick = () => { setIsModalOpen(true); } - - console.log(upcomingSections); + if (isLoading) return null @@ -38,13 +37,15 @@ const VideoSectionButtonPatient = () => { )} View Upcoming Video Calls - + {upcomingSections && ( + + )} ) } diff --git a/client/components/page-components/video/VideoChat.tsx b/client/components/page-components/video/VideoChat.tsx index 1f618fa6..de1a05b4 100644 --- a/client/components/page-components/video/VideoChat.tsx +++ b/client/components/page-components/video/VideoChat.tsx @@ -1,67 +1,45 @@ -'use client' - -import { useState, useEffect, useRef } from 'react' -import { Mic, MicOff, Video, VideoOff, PhoneOff } from 'lucide-react' -import { Button } from "@/components/ui/button" -import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar" -import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip" - -interface VideoChatProps { - localStream: MediaStream | null - remoteStream: MediaStream | null - handleEndCall: () => void - isDoctor: boolean - selfAvatar: string - remoteAvatar: string -} +import { useRef, useEffect } from 'react'; +import { Mic, MicOff, Video, VideoOff, PhoneOff } from 'lucide-react'; +import { Button } from "@/components/ui/button"; +import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; +import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip"; +import { VideoChatProps } from '@/types'; export default function VideoChat({ localStream, remoteStream, handleEndCall, + toggleMute, + toggleVideo, + isMuted, + isVideoOff, isDoctor, selfAvatar, remoteAvatar }: VideoChatProps) { - const [isMuted, setIsMuted] = useState(false) - const [isVideoOff, setIsVideoOff] = useState(false) - const [showControls, setShowControls] = useState(true) - const localVideoRef = useRef(null) - const remoteVideoRef = useRef(null) - + const localVideoRef = useRef(null); + const remoteVideoRef = useRef(null); useEffect(() => { if (localVideoRef.current && localStream) { - localVideoRef.current.srcObject = localStream + localVideoRef.current.srcObject = localStream; } - }, [localStream]) + }, [localStream, isVideoOff]); + useEffect(() => { if (remoteVideoRef.current && remoteStream) { - remoteVideoRef.current.srcObject = remoteStream + console.log('Setting remote stream'); + remoteVideoRef.current.srcObject = remoteStream; + } else { + console.log('Remote stream is not available'); } - }, [remoteStream]) - - const toggleMute = () => { - if (localStream) { - localStream.getAudioTracks().forEach(track => track.enabled = !track.enabled) - setIsMuted(!isMuted) - } - } - - const toggleVideo = () => { - if (localStream) { - localStream.getVideoTracks().forEach(track => track.enabled = !track.enabled) - setIsVideoOff(!isVideoOff) - } - } - - const toggleControls = () => { - setShowControls(!showControls) - } + }, [remoteStream]); + return ( -
-
+
+ {/* Remote Video */} +
{remoteStream ? (