WEBRTC Video call - Admit Reject Issue

Viewed 18

My P2P Video call using WEBRTC is working fine. I have a requirement now. I have only two participants. One is the host and another one is the normal user.

I have two scenarios

  1. When the normal user comes first to the room and when the host enters secondly, it should ask the host a prompt saying "Accept or Deny?" the normal user.

  2. When the admin comes first to the room and when the normal user enters secondly, it should ask the host a prompt saying "Accept or Deny?" the normal user.

    Basically prompt should ask the host to accept or deny the normal user regardless position of the host entering the room.

Host URL: https://abc.ngrok.io/?room=venice&name=Don&role=host

User URL: https://abc.ngrok.io/?room=venice&name=Don&role=user

Code

app.js

app.get("/", (req, res) => {
  res.render("room", { roomId: req.query.room, name: req.query.name, role: req.query.role });
  role = req.query.role;
});

io.on("connection", (socket) => {

  if (role == 'host') {
    host = socket.id
  }
  else if (role == 'user') {
    user = socket.id
  }

  function log() {
    var array = ['Message from server:'];
    array.push.apply(array, arguments);
    socket.emit('log', array);
  }

  socket.on("join-room", (roomId, userId, userName) => {
    var clientsInRoom = io.sockets.adapter.rooms[roomId];
    var numClients = clientsInRoom ? Object.keys(clientsInRoom.sockets).length : 0;
    console.log(numClients)
    if (numClients == 0) {
      users.create({ room: roomId, project: 'Glucolyf', 'participants': socket.id }, (err) => {
        if (err) throw err;

      })
      socket.join(roomId);
  
    }
    if (numClients == 1) {
      users.findOneAndUpdate({ room: roomId }, { $push: { participants: socket.id } }, { new: true }, (err) => {
        if (err) throw err;
      })
      socket.join(roomId);
      io.to(host).emit('permission', roomId);
    }
    socket.to(roomId).broadcast.emit("user-connected", userId, roomId);
  });
});

NOTE: It's a normal WEBRTCconfiguration. I'm finding host's socket ID and sending him the prompt with io.to(host).emit('permission', roomId);

script.js

navigator.mediaDevices
  .getUserMedia({
    audio: true,
    video: true,
  })
  .then((stream) => {
    myVideoStream = stream;
    addVideoStream(myVideo, stream);
    peer.on("call", (call) => {
      call.answer(stream);
      const video = document.createElement("video");
      video.playsInline = true;
      video.autoplay = true;
      call.on("stream", (userVideoStream) => {
        addVideoStream(video, userVideoStream);
      });

    });

    socket.on("user-connected", (userId) => {
      connectToNewUser(userId, stream);
    });
  });

socket.on("permission", async (id) => {
  var acceptsCall = confirm("New incoming call. Do you want to accept?");
  if (acceptsCall) {
    await updateUsers(acceptsCall).then(r => { // Updating DB collection with the permission
      console.log(r)
    })
  }
  else {
    await updateUsers(acceptsCall).then(r => {
      console.log(r)
    })
  }
})

const connectToNewUser = (userId, stream) => {
  setTimeout(async () => {
    await getUser(ROOM_ID).then(a => { // Checking the updated collection for permission

      if (a.accept == true) { // If true I'm adding
        uId = userId;
        const call = peer.call(userId, stream);
        const video = document.createElement("video");
        call.on("stream", (userVideoStream) => {
          addVideoStream(video, userVideoStream);
        });
        call.on("close", () => {
          video.remove();
        });

        peers[userId] = call;
      }
    })
  }, 3000)

};

NOTE: Prompting the host and updating the collection with the response. If true I'm adding the user.

Issue: It works sometimes but most time call is not connected even after the host selects admit in the prompt. Where I'm going wrong. Please help me out.

0 Answers
Related