-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
00b748c
commit 7413880
Showing
13 changed files
with
350 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import React, { createContext, useEffect, useRef, useState } from "react"; | ||
import io from "socket.io-client"; | ||
import Peer from "simple-peer"; | ||
|
||
const SocketContext = createContext(); | ||
|
||
const socket = io(process.env.REACT_APP_NODE_API); | ||
|
||
const ContextProvider = ({ children }) => { | ||
const [stream, serStream] = useState(null); | ||
const [me, setMe] = useState(""); | ||
const [callAccepted, setCallAccepted] = useState(false); | ||
const [callEnded, setcallEnded] = useState(false); | ||
const [name, setName] = useState(""); | ||
const [call, setCall] = useState(false) | ||
|
||
const myvideo = useRef(); | ||
const userVideo = useRef(); | ||
const connectionRef = useRef(); | ||
|
||
useEffect(() => { | ||
navigator.mediaDevices | ||
.getUserMedia({ video: false, audio: false }) | ||
.then((currentStream) => { | ||
serStream(currentStream); | ||
|
||
myvideo.current.srcObject = currentStream; | ||
}); | ||
|
||
socket.on("me", (id) => setMe(id)); | ||
socket.on("calluser", (from, callerName, signal) => { | ||
setCall({ isRecievedCall: true, from, name: callerName, signal }); | ||
}); | ||
}, []); | ||
|
||
const answerCall = () => { | ||
setCallAccepted(true); | ||
|
||
const peer = new Peer({ initiator: false, trickle: false, stream }); | ||
|
||
peer.on("signal", (data) => { | ||
socket.emit("answercall", { | ||
signal: data, | ||
to: call.from, | ||
}); | ||
}); | ||
|
||
peer.on("stream", (currentStream) => { | ||
userVideo.current.srcObject = currentStream; | ||
}); | ||
|
||
peer.signal(call.signal); | ||
|
||
connectionRef.current = peer; | ||
}; | ||
|
||
const callUser = (id) => { | ||
const peer = new Peer({ initiator: true, trickle: false, stream }); | ||
|
||
peer.on("signal", (data) => { | ||
socket.emit("calluser", { | ||
userToCall: id, | ||
signalData: data, | ||
from: me, | ||
name, | ||
}); | ||
}); | ||
|
||
peer.on("stream", (currentStream) => { | ||
userVideo.current.srcObject = currentStream; | ||
}); | ||
|
||
socket.on("callaccepted", (signal) => { | ||
setCallAccepted(true); | ||
|
||
peer.signal(signal); | ||
}); | ||
|
||
connectionRef.current = peer; | ||
}; | ||
|
||
const leaveCall = () => { | ||
setcallEnded(true); | ||
|
||
connectionRef.current.destroy(); | ||
|
||
window.location.reload(); | ||
}; | ||
|
||
return ( | ||
<SocketContext.Provider | ||
value={{ | ||
call, | ||
callAccepted, | ||
myvideo, | ||
userVideo, | ||
stream, | ||
name, | ||
setName, | ||
callEnded, | ||
me, | ||
callUser, | ||
leaveCall, | ||
answerCall, | ||
}} | ||
> | ||
{children} | ||
</SocketContext.Provider> | ||
); | ||
}; | ||
|
||
export {ContextProvider, SocketContext} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import React from 'react' | ||
|
||
const Notifications = () => { | ||
return ( | ||
<div>Notifications</div> | ||
) | ||
} | ||
|
||
export default Notifications |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import React from "react"; | ||
|
||
const Options = ({ children }) => { | ||
return ( | ||
<div> | ||
Options | ||
{children} | ||
</div> | ||
); | ||
}; | ||
|
||
export default Options; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React, { useContext } from "react"; | ||
import { SocketContext } from "../SocketContext"; | ||
|
||
const VideoPlayer = () => { | ||
const { name, callAccepted, myVideo, userVideo, callEnded, stream, call } = | ||
useContext(SocketContext); | ||
return ( | ||
<div className="flex justify-center gap-8 flex-col md:flex-row h-screen mt-10"> | ||
{stream && ( | ||
<div className="bg-white border-2 w-[22rem] h-[22rem]"> | ||
<h2 className="text-md">{name || "Name"}</h2> | ||
<video muted ref={myVideo} autoPlay /> | ||
</div> | ||
)} | ||
|
||
{callAccepted && !callEnded && ( | ||
<div className="bg-white border-2 w-[22rem] h-[22rem]"> | ||
<h2 className="text-md">{call?.name || "Name"}</h2> | ||
<video muted ref={userVideo} autoPlay /> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default VideoPlayer; |
Oops, something went wrong.