Skip to content

Commit

Permalink
too many changes
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaSingh-02 committed Jul 12, 2024
1 parent 321d7a8 commit 3b27431
Show file tree
Hide file tree
Showing 16 changed files with 393 additions and 325 deletions.
7 changes: 1 addition & 6 deletions apps/client/src/app/(auth)/(protected)/childLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import React, { useState } from 'react';
import UserProvider, { useAuth } from '@/Context/index';
import { useRouter } from 'next/navigation';
import { VideoProvider, SocketProvider } from '@/Context';
import { PeerProvider } from '@/Context';
import { VideoProvider } from '@/Context';

export default function ChildLayout({ children }: { children: React.ReactNode }) {
const router = useRouter();
Expand All @@ -18,15 +17,11 @@ export default function ChildLayout({ children }: { children: React.ReactNode })

return (
<>
<PeerProvider>
<VideoProvider value={{ videoStatus, setVideoStatus }}>
<main className='w-[100%]'>
<SocketProvider>
<UserProvider>{children}</UserProvider>
</SocketProvider>
</main>
</VideoProvider>
</PeerProvider>
</>
);
}
3 changes: 2 additions & 1 deletion apps/client/src/app/(auth)/(protected)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export default function RootLayout({
}) {
return (
<>
<ChildLayout>{children}</ChildLayout>
{/* <ChildLayout>{children}</ChildLayout> */}
{children}
</>
);
}
109 changes: 109 additions & 0 deletions apps/client/src/app/(auth)/(protected)/receiver/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
'use client';
import React, { useEffect, useState, useCallback, useRef } from 'react';
import useSocket from '@/hooks/useSocket';
import { IMessage } from '../sender/page';

const Room = () => {
const videoRef1 = useRef<HTMLVideoElement | null>(null);
const videoRef2 = useRef<HTMLVideoElement | null>(null);
const socket = useSocket();
const [pc, setPc] = useState<RTCPeerConnection | null>(null);

useEffect(() => {
if (!socket) return;

socket.onopen = () => {
socket.send(
JSON.stringify({
type: 'receiver',
}),
);
};
const pc = new RTCPeerConnection();
setPc(pc);

socket.onmessage = async (event) => {
const message: IMessage = JSON.parse(event.data);
console.log("this is message", message);
if (message.type === 'create-offer') {
await pc.setRemoteDescription(new RTCSessionDescription(message.payload.remoteSdp));
const answer = await pc.createAnswer();
await pc.setLocalDescription(new RTCSessionDescription(answer));
socket.send(JSON.stringify({
type: "create-answer",
payload: {
remoteSdp: answer
}
}));
} else if (message.type === 'ice-candidate') {
pc.addIceCandidate(message.payload.candidate);
}
}
// getCameraStreamAndSend(pc);

}, [socket]);

const sendStream = async () => {
if (!socket) return;
socket.onmessage = async (event) => {
const message: IMessage = JSON.parse(event.data);
console.log("this is message", message);
if (message.type === 'create-offer' && pc) {
await pc.setRemoteDescription(message.payload.remoteSdp);
const answer = await pc.createAnswer();
await pc.setLocalDescription(answer);
socket.send(JSON.stringify({
type: "create-answer",
payload: {
remoteSdp: answer
}
}));
} else if (message.type === 'ice-candidate' && pc) {
pc.addIceCandidate(message.payload.candidate);
}
}

if(pc){
pc.onicecandidate = (e) => {
if (e.candidate) {
socket.send(JSON.stringify({
type: "ice-candidate",
payload: {
candidate: e.candidate
}
}))
}
}
getCameraStreamAndSend(pc);
}
};

const getCameraStreamAndSend = (pc: RTCPeerConnection) => {
navigator.mediaDevices.getUserMedia({ video: true }).then((stream) => {
if (videoRef1.current) {
videoRef1.current.srcObject = stream;
videoRef1.current.play();
}
stream.getTracks().forEach((track) => {
pc.addTrack(track);
});
});
pc.ontrack = (event: RTCTrackEvent) => {
console.log("this is video event", event);
if (videoRef2.current) {
videoRef2.current.srcObject = new MediaStream([event.track]);
videoRef2.current.play();
}
}
}

return (
<>
<video ref={videoRef1}></video>
<video ref={videoRef2}></video>
<button onClick={sendStream}>Send video</button>
</>
);
};

export default Room;
139 changes: 60 additions & 79 deletions apps/client/src/app/(auth)/(protected)/room/[rid]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,111 +1,92 @@
'use client';

import { useVideo, useUserContext, useSocket, usePeer } from '@/Context';
import React, { useEffect, useState, useCallback } from 'react';
import { useVideo, useUserContext, usePeer } from '@/Context';
import React, { useEffect, useState, useCallback, useRef } from 'react';
import { usePathname } from 'next/navigation';
import ReactPlayer from 'react-player';
import { BsCameraVideo, BsCameraVideoOff } from 'react-icons/bs';
import { configuration } from '@/lib/iceserver/iceserver';
import registerPeerConnectionLintners from '@/peerconnection/peerconnection';
import useSocket from '@/hooks/useSocket';

const Room = () => {
const [myVideo, setMyVideo] = useState<MediaStream>();
const [remoteEmailId, setRemoteEmailId] = useState();
const [localStream, setLocalStream] = useState<MediaStream>();
const [remoteStream, setRemoteStream] = useState<MediaStream>();
const { videoStatus, setVideoStatus } = useVideo();
const { user } = useUserContext();
let peerConnection: RTCPeerConnection;
let localStream: any;
const pathName: string = usePathname()!;
const rid = pathName.split('/')[2];

const videoRef1 = useRef<HTMLVideoElement | null>(null);
const videoRef2 = useRef<HTMLVideoElement | null>(null);

// Handles Video Closing
const closeVideoStream = () => {
if (myVideo) {
myVideo.getTracks().forEach((track) => track.stop());
setMyVideo(undefined);
if (localStream) {
localStream.getTracks().forEach((track) => track.stop());
setLocalStream(undefined);
}
};
let flag = false;
let data: any;
const createRoom = async() => {
peerConnection = new RTCPeerConnection(configuration);
registerPeerConnectionLintners(peerConnection);

const offer = await peerConnection.createOffer();
await peerConnection.setLocalDescription(offer);

const roomWithOffer = {
offer: {
type: offer.type,
sdp: offer.sdp
}
}
data = roomWithOffer;
const roomId = rid;
if(!peerConnection.currentRemoteDescription && data.answer){
const ans = new RTCSessionDescription(data.answer);
await peerConnection.setRemoteDescription(ans);
}
const toggleMyVideo = () => {
setVideoStatus(!videoStatus);
};

localStream.getTrack().forEach((track: any) => {
peerConnection.addTrack(track, localStream);
const sendVideo = () => {
navigator.mediaDevices.getUserMedia({ video: true, audio: true }).then((res) => {
setLocalStream(res);
});
};

const joinRoom = async() => {
const offer = data.offer;
await peerConnection.setRemoteDescription(offer);
const answer = await peerConnection.createAnswer();
await peerConnection.setLocalDescription(answer);

const roomWithAnswer = {
answer:{
type: answer.type,
sdp: answer.sdp
}
}
data = roomWithAnswer;
flag = true;
const offerOptions = {
offerToReceiveAudio: 1,
offerToReceiveVideo: 1,
};

const sendStream = {};
const call = async () => {
const videoTrack = localStream?.getVideoTracks()[0];
const audioTrack = localStream?.getAudioTracks()[0];
};

const handleCallAccepted = {};

const socket = useSocket();
const senderSide = ()=>{
if(!socket) return;

const toggleMyVideo = () => {
setVideoStatus(!videoStatus);
};
socket.onopen = ()=>{
socket.send(JSON.stringify({
type: "sender"
}))
}
}

const sendVideo = () => {
navigator.mediaDevices.getUserMedia({ video: true, audio: true }).then((res) => {
setMyVideo(res);
});
};

return (
// <>
// <div className='flex w-[100%] items-center h-screen '>
// <div className='w-[70%] flex justify-center items-center border-2 border-gray-800 h-[80%] rounded-2xl m-10'>
// {/* <h2>Connected to {remoteEmailId}</h2> */}
// {remoteStream && <ReactPlayer url={localStream} playing muted height={500} width={800} />}
// </div>
// <div className='w-[30%] flex flex-col justify-between py-10 border-2 px-7 border-gray-800 h-[90vh] m-10 rounded-2xl'>
// <div className='border-2 border-red-400 h-[250px] rounded-2xl flex justify-center items-center'>
// <ReactPlayer url={localStream} playing muted height={240} width={800} />
// </div>
// <div className='border-2 border-red-400 h-[400px] rounded-2xl flex justify-center items-center'>Details : coming soon...</div>
// <div className=' flex justify-center '>
// {/* <button
// onClick={toggleMyVideo}
// className={`px-4 py-2 flex justify-center rounded-md ${videoStatus ? 'bg-red-500' : 'bg-green-500'} mx-2 text-xl`}
// >
// {videoStatus ? <BsCameraVideoOff /> : <BsCameraVideo />}
// </button> */}
// <button onClick={sendVideo}>sendStream</button>
// </div>
// </div>
// </div>
// </>
<>
<div className='flex w-[100%] items-center h-screen '>
<div className='w-[70%] flex justify-center items-center border-2 border-gray-800 h-[80%] rounded-2xl m-10'>
{/* <h2>Connected to {remoteEmailId}</h2> */}
{remoteStream && <ReactPlayer url={myVideo} playing muted height={500} width={800} />}
</div>
<div className='w-[30%] flex flex-col justify-between py-10 border-2 px-7 border-gray-800 h-[90vh] m-10 rounded-2xl'>
<div className='border-2 border-red-400 h-[250px] rounded-2xl flex justify-center items-center'>
<ReactPlayer url={myVideo} playing muted height={240} width={800} />
</div>
<div className='border-2 border-red-400 h-[400px] rounded-2xl flex justify-center items-center'>Details : coming soon...</div>
<div className=' flex justify-center '>
{/* <button
onClick={toggleMyVideo}
className={`px-4 py-2 flex justify-center rounded-md ${videoStatus ? 'bg-red-500' : 'bg-green-500'} mx-2 text-xl`}
>
{videoStatus ? <BsCameraVideoOff /> : <BsCameraVideo />}
</button> */}
<button onClick={sendVideo}>sendStream</button>
</div>
</div>
</div>
<video ref={videoRef1}></video>
<video ref={videoRef2}></video>
<button>Send video</button>
<button>receive video</button>
</>
);
};
Expand Down
Loading

0 comments on commit 3b27431

Please sign in to comment.