Removed video and added text

This commit is contained in:
Mitchell McCaffrey
2020-03-26 12:24:52 +11:00
parent 30e957e937
commit 54c7100c2c
13 changed files with 469 additions and 338 deletions
+6 -88
View File
@@ -1,18 +1,10 @@
import { useEffect, useState, useRef } from "react";
import { useEffect, useState } from "react";
import Peer from "peerjs";
const getUserMedia =
navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia;
function useSession(onConnectionOpen, onConnectionSync) {
const [peerId, setPeerId] = useState(null);
const [peer, setPeer] = useState(null);
const [connections, setConnections] = useState({});
const [streams, setStreams] = useState({});
// Keep a ref to the streams in order to clean them up on unmount
const streamsRef = useRef(streams);
function addConnection(connection) {
console.log("Adding connection", connection.peer);
@@ -28,59 +20,15 @@ function useSession(onConnectionOpen, onConnectionSync) {
});
}
function addStream(stream, id) {
console.log("Adding stream", id);
setStreams(prevStreams => {
console.log("Streams", {
...prevStreams,
[id]: stream
});
return {
...prevStreams,
[id]: stream
};
});
}
useEffect(() => {
console.log("Creating peer");
setPeer(new Peer());
return () => {
console.log("Cleaning up streams");
for (let stream of Object.values(streamsRef.current)) {
for (let track of stream.getTracks()) {
track.stop();
}
}
};
}, []);
// Update stream refs
useEffect(() => {
console.log("Syncing stream ref to stream state", streams);
streamsRef.current = streams;
}, [streams, streamsRef]);
useEffect(() => {
function handleOpen(id) {
console.log("Peer open", id);
setPeerId(id);
console.log("Getting user data");
getUserMedia(
{
video: {
frameRate: { ideal: 15, max: 20 },
width: { max: 256 },
height: { max: 144 }
},
audio: false
},
stream => {
addStream(stream, id);
}
);
}
function handleConnection(connection) {
@@ -90,7 +38,7 @@ function useSession(onConnectionOpen, onConnectionSync) {
if (metadata.sync) {
connection.send({
id: "sync",
data: Object.keys(connections)
data: { connections: Object.keys(connections) }
});
if (onConnectionSync) {
onConnectionSync(connection);
@@ -118,25 +66,6 @@ function useSession(onConnectionOpen, onConnectionSync) {
});
}
function handleCall(call) {
console.log("incoming call", call.peer);
call.answer(streams[peerId]);
call.on("stream", remoteStream => {
addStream(remoteStream, call.peer);
});
function removeStream() {
setStreams(prevStreams => {
const { [call.peer]: old, ...rest } = prevStreams;
return rest;
});
}
call.on("close", removeStream);
call.on("error", error => {
console.error("Media Connection error", error);
removeStream();
});
}
function handleError(error) {
console.error("Peer error", error);
}
@@ -147,23 +76,13 @@ function useSession(onConnectionOpen, onConnectionSync) {
peer.on("open", handleOpen);
peer.on("connection", handleConnection);
peer.on("call", handleCall);
peer.on("error", handleError);
return () => {
peer.removeListener("open", handleOpen);
peer.removeListener("connection", handleConnection);
peer.removeListener("call", handleCall);
peer.removeListener("error", handleError);
};
}, [peer, peerId, connections, onConnectionOpen, onConnectionSync, streams]);
function call(connectionId) {
console.log("Calling", connectionId);
const call = peer.call(connectionId, streams[peerId]);
call.on("stream", stream => {
addStream(stream, connectionId);
});
}
}, [peer, peerId, connections, onConnectionOpen, onConnectionSync]);
function connectTo(connectionId) {
console.log("Connecting to", connectionId);
@@ -177,7 +96,8 @@ function useSession(onConnectionOpen, onConnectionSync) {
connection.on("open", () => {
connection.on("data", data => {
if (data.id === "sync") {
for (let syncId of data.data) {
const { connections: syncConnections } = data.data;
for (let syncId of syncConnections) {
console.log("Syncing to", syncId);
if (connectionId === syncId || syncId in connections) {
continue;
@@ -186,7 +106,6 @@ function useSession(onConnectionOpen, onConnectionSync) {
metadata: { sync: false }
});
addConnection(syncConnection);
call(syncId);
}
}
});
@@ -194,10 +113,9 @@ function useSession(onConnectionOpen, onConnectionSync) {
onConnectionOpen(connection);
}
});
call(connectionId);
}
return { peer, peerId, streams, connections, connectTo };
return { peer, peerId, connections, connectTo };
}
export default useSession;