Merge branch 'master' into fixes

This commit is contained in:
Taichi Kato 2020-05-31 11:17:06 +08:00 committed by GitHub
commit 480e417f16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 83 deletions

View File

@ -39,7 +39,6 @@ var VideoChat = {
localVideo: document.getElementById("local-video"), localVideo: document.getElementById("local-video"),
peerConnections: new Map(), peerConnections: new Map(),
recognition: undefined, recognition: undefined,
answerCreatedUUIDs: [], // HACKY workaround: currently, onAnswer seems to be triggering twice and we don't know why
// Call to getUserMedia (provided by adapter.js for cross browser compatibility) // Call to getUserMedia (provided by adapter.js for cross browser compatibility)
// asking for access to both the video and audio streams. If the request is // asking for access to both the video and audio streams. If the request is
@ -109,12 +108,9 @@ var VideoChat = {
VideoChat.localVideo.srcObject = stream; VideoChat.localVideo.srcObject = stream;
// Now we're ready to join the chat room. // Now we're ready to join the chat room.
VideoChat.socket.emit("join", roomHash); VideoChat.socket.emit("join", roomHash);
// Receive its own uuid
VideoChat.socket.on("uuid", (uuid) => (VideoChat.uuid = uuid));
// Add listeners to the websocket // Add listeners to the websocket
VideoChat.socket.on("full", chatRoomFull); VideoChat.socket.on("full", chatRoomFull);
VideoChat.socket.on("offer", VideoChat.onOffer); VideoChat.socket.on("offer", VideoChat.onOffer);
VideoChat.socket.on("ready", VideoChat.readyToCall);
VideoChat.socket.on("willInitiateCall", VideoChat.call); VideoChat.socket.on("willInitiateCall", VideoChat.call);
// Set up listeners on the socket // Set up listeners on the socket
VideoChat.socket.on("candidate", VideoChat.onCandidate); VideoChat.socket.on("candidate", VideoChat.onCandidate);
@ -125,17 +121,8 @@ var VideoChat = {
); );
}, },
// When we are ready to call, enable the Call button.
readyToCall: function (event) {
logIt("readyToCall");
},
call: function (uuid, room) { call: function (uuid, room) {
logIt("Initiating call"); logIt("Initiating call with " + uuid);
VideoChat.startCall(uuid);
},
// Set up a callback to run when we have the ephemeral token to use Twilio's TURN server.
startCall: function (uuid) {
VideoChat.socket.on( VideoChat.socket.on(
"token", "token",
VideoChat.establishConnection(uuid, function (a) { VideoChat.establishConnection(uuid, function (a) {
@ -151,10 +138,9 @@ var VideoChat = {
return; return;
} }
console.log("establishing connection to", uuid); console.log("establishing connection to", uuid);
// Initialise localICEcandidates for node uuid with empty array
VideoChat.localICECandidates[uuid] = []; // initialise uuid with empty array VideoChat.localICECandidates[uuid] = [];
VideoChat.connected.set(uuid, false); VideoChat.connected.set(uuid, false);
// Set up a new RTCPeerConnection using the token's iceServers. // Set up a new RTCPeerConnection using the token's iceServers.
VideoChat.peerConnections.set( VideoChat.peerConnections.set(
uuid, uuid,
@ -214,8 +200,6 @@ var VideoChat = {
case "connected": case "connected":
logIt("connected"); logIt("connected");
// Once connected we no longer have a need for the signaling server, so disconnect // Once connected we no longer have a need for the signaling server, so disconnect
// VideoChat.socket.off("token");
// VideoChat.socket.off("offer");
break; break;
case "disconnected": case "disconnected":
logIt("disconnected"); logIt("disconnected");
@ -274,12 +258,7 @@ var VideoChat = {
// Create an offer that contains the media capabilities of the browser. // Create an offer that contains the media capabilities of the browser.
createOffer: function (uuid) { createOffer: function (uuid) {
console.log( logIt(`createOffer to ${uuid} >>> Creating offer...`);
">>> Creating offer to UUID: ",
uuid,
". my UUID is",
VideoChat.socket.id
);
VideoChat.peerConnections.get(uuid).createOffer( VideoChat.peerConnections.get(uuid).createOffer(
function (offer) { function (offer) {
// If the offer is created successfully, set it as the local description // If the offer is created successfully, set it as the local description
@ -303,22 +282,11 @@ var VideoChat = {
createAnswer: function (offer, uuid) { createAnswer: function (offer, uuid) {
logIt("createAnswer"); logIt("createAnswer");
rtcOffer = new RTCSessionDescription(JSON.parse(offer)); rtcOffer = new RTCSessionDescription(JSON.parse(offer));
console.log( logIt(`>>> Creating answer to ${uuid}`);
"createAnswer: setting remote description of " +
uuid +
" on " +
VideoChat.socket.id
);
VideoChat.peerConnections.get(uuid).setRemoteDescription(rtcOffer); VideoChat.peerConnections.get(uuid).setRemoteDescription(rtcOffer);
VideoChat.peerConnections.get(uuid).createAnswer( VideoChat.peerConnections.get(uuid).createAnswer(
function (answer) { function (answer) {
VideoChat.peerConnections.get(uuid).setLocalDescription(answer); VideoChat.peerConnections.get(uuid).setLocalDescription(answer);
console.log(
">>> Creating answer to UUID: ",
uuid,
". my UUID is",
VideoChat.socket.id
);
VideoChat.socket.emit("answer", JSON.stringify(answer), roomHash, uuid); VideoChat.socket.emit("answer", JSON.stringify(answer), roomHash, uuid);
}, },
function (err) { function (err) {
@ -344,24 +312,9 @@ var VideoChat = {
// When an answer is received, add it to the peerConnection as the remote description. // When an answer is received, add it to the peerConnection as the remote description.
onAnswer: function (answer, uuid) { onAnswer: function (answer, uuid) {
console.log( logIt(`onAnswer <<< Received answer from ${uuid}`);
"onAnswer <<< Received answer",
uuid,
". my UUID is",
VideoChat.socket.id
);
// if (!VideoChat.answerCreatedUUIDs.includes(uuid)) {
VideoChat.answerCreatedUUIDs.push(uuid);
// logIt("onAnswer <<< Received answer" + "");
var rtcAnswer = new RTCSessionDescription(JSON.parse(answer)); var rtcAnswer = new RTCSessionDescription(JSON.parse(answer));
// Set remote description of RTCSession // Set remote description of RTCSession
console.log(
"onAnswer: setting remote description of " +
uuid +
" on " +
VideoChat.socket.id
);
VideoChat.peerConnections.get(uuid).setRemoteDescription(rtcAnswer); VideoChat.peerConnections.get(uuid).setRemoteDescription(rtcAnswer);
// The caller now knows that the callee is ready to accept new ICE candidates, so sending the buffer over // The caller now knows that the callee is ready to accept new ICE candidates, so sending the buffer over
VideoChat.localICECandidates[uuid].forEach((candidate) => { VideoChat.localICECandidates[uuid].forEach((candidate) => {
@ -378,9 +331,7 @@ var VideoChat = {
// Called when a stream is added to the peer connection // Called when a stream is added to the peer connection
onAddStream: function (event, uuid) { onAddStream: function (event, uuid) {
logIt( logIt("onAddStream <<< Received new stream from remote. Adding it...");
"onAddStream <<< Received new stream from remote. Adding it..." + event
);
// Create new remote video source in wrapper // Create new remote video source in wrapper
// Create a <video> node // Create a <video> node
var node = document.createElement("video"); var node = document.createElement("video");

View File

@ -9,14 +9,7 @@ var twilio = require("twilio")(twillioAccountSID, twillioAuthToken);
var express = require("express"); var express = require("express");
var app = express(); var app = express();
const fs = require("fs"); const fs = require("fs");
var http = require("https").createServer( var http = require("http").createServer(app);
{
key: fs.readFileSync("/Users/khushjammu/certs/privkey.pem"),
cert: fs.readFileSync("/Users/khushjammu/certs/cert.pem"),
},
app
);
// var http = require("http").createServer(app);
var io = require("socket.io")(http); var io = require("socket.io")(http);
var path = require("path"); var path = require("path");
var public = path.join(__dirname, "public"); var public = path.join(__dirname, "public");
@ -86,28 +79,11 @@ io.on("connection", function (socket) {
var numClients = typeof clients !== "undefined" ? clients.length : 0; var numClients = typeof clients !== "undefined" ? clients.length : 0;
if (numClients === 0) { if (numClients === 0) {
socket.join(room); socket.join(room);
twilio.tokens.create(function (err, response) { } else if (numClients < 5) {
if (err) {
logIt(err, room);
} else {
logIt("Token generated. Returning it to the browser client", room);
socket.emit("token", response);
// Existing callers initiates call with user
}
});
} else if (numClients < 4) {
socket.join(room); socket.join(room);
logIt("Connected clients", room);
for (var clientId in clients.sockets) {
logIt("ID: " + clientId, room);
}
// When the client is not the first to join the room, all clients are ready. // When the client is not the first to join the room, all clients are ready.
logIt("Broadcasting ready message", room); logIt("Broadcasting ready message", room);
socket.broadcast.to(room).emit("willInitiateCall", socket.id, room); socket.broadcast.to(room).emit("willInitiateCall", socket.id, room);
// socket.emit("uuid", socket.id);
socket.emit("ready", room).to(room);
socket.broadcast.to(room).emit("ready", room);
} else { } else {
logIt( logIt(
"room already full with " + numClients + " people in the room.", "room already full with " + numClients + " people in the room.",
@ -157,7 +133,7 @@ io.on("connection", function (socket) {
}); });
// Listen for Heroku port, otherwise just use 3000 // Listen for Heroku port, otherwise just use 3000
var port = process.env.PORT || 443; var port = process.env.PORT || 3000;
http.listen(port, function () { http.listen(port, function () {
console.log("http://localhost:" + port); console.log("http://localhost:" + port);
}); });