Skip to content

Commit

Permalink
remote video in progress 🚧
Browse files Browse the repository at this point in the history
  • Loading branch information
sinanptm committed Sep 30, 2024
1 parent 72a8d1c commit ca0605f
Show file tree
Hide file tree
Showing 7 changed files with 288 additions and 218 deletions.
50 changes: 16 additions & 34 deletions client/app/(patient)/video-section/[sectionId]/page.tsx
Original file line number Diff line number Diff line change
@@ -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<MediaStream | null>(null);
const [remoteStream, setRemoteStream] = useState<MediaStream | null>(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 <div className="flex items-center justify-center h-screen">Loading...</div>;

Expand All @@ -53,6 +31,10 @@ export default function PatientVideoCallPage() {

return (
<VideoChat
isMuted={isMuted}
isVideoOff={isVideoOff}
toggleMute={toggleMute}
toggleVideo={toggleVideo}
localStream={localStream}
remoteStream={remoteStream}
handleEndCall={handleEndCall}
Expand Down
50 changes: 16 additions & 34 deletions client/app/doctor/video-call/[sectionId]/page.tsx
Original file line number Diff line number Diff line change
@@ -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 { useGetSectionByIdDoctor } from '@/lib/hooks/video/useDoctor';
import { useVideoCall } from '@/lib/hooks/useVideoCall';

export default function DoctorVideoCallPage() {
const { sectionId } = useParams();
const [hasJoined, setHasJoined] = useState(false);
const [localStream, setLocalStream] = useState<MediaStream | null>(null);
const [remoteStream, setRemoteStream] = useState<MediaStream | null>(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 <div className="flex items-center justify-center h-screen">Loading...</div>;

Expand All @@ -53,6 +31,10 @@ export default function DoctorVideoCallPage() {

return (
<VideoChat
isMuted={isMuted}
isVideoOff={isVideoOff}
toggleMute={toggleMute}
toggleVideo={toggleVideo}
localStream={localStream}
remoteStream={remoteStream}
handleEndCall={handleEndCall}
Expand Down
19 changes: 10 additions & 9 deletions client/components/button/VideoSectionButtonPatient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ const VideoSectionButtonPatient = () => {
const handleClick = () => {
setIsModalOpen(true);
}

console.log(upcomingSections);

if (isLoading) return null


Expand All @@ -38,13 +37,15 @@ const VideoSectionButtonPatient = () => {
)}
<span className="sr-only">View Upcoming Video Calls</span>
</ButtonV2>
<VideoSectionsModel
open={isModalOpen}
setOpen={setIsModalOpen}
sections={upcomingSections!}
link="/video-section"
user='patient'
/>
{upcomingSections && (
<VideoSectionsModel
open={isModalOpen}
setOpen={setIsModalOpen}
sections={upcomingSections!}
link="/video-section"
user='patient'
/>
)}
</>
)
}
Expand Down
Loading

1 comment on commit ca0605f

@vercel
Copy link

@vercel vercel bot commented on ca0605f Sep 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.