on("Signal") called twice
See original GitHub issueI’m trying to make a connection between two peers and after there’s a connection i want to add their streams. This is the main part of the code:
add a stream:
getMedia(peer) {
// Get media of current client
navigator.mediaDevices
.getUserMedia({ video: false, audio: true })
.then((stream) => {
peer.addStream(stream);
})
.catch(() => {});
}
callPeer method:
callPeer(peer, userToCall) {
peer.on("signal", (data) => {
this.socket.emit("call:offer", {
receiverId: userToCall,
signalData: data,
});
});
peer.on("connect", () => {
this.getMedia(peer);
console.log("peers connected");
});
peer.on("stream", (stream) => {
var audio = document.getElementById(userToCall);
audio.srcObject = stream;
audio.onloadedmetadata = function (e) {
audio.play();
};
});
}
acceptCall method:
acceptCall(peer, callData) {
peer.on("signal", (data) => {
this.socket.emit("call:accept", {
signalData: data,
receiverId: callData.senderId,
});
});
peer.on("stream", (stream) => {
var audio = document.getElementById(callData.senderId);
audio.srcObject = stream;
audio.onloadedmetadata = function (e) {
audio.play();
};
});
peer.on("connect", () => {
this.getMedia(peer);
console.log("peers connected");
});
peer.signal(callData.signalData);
}
The main problem is in acceptCall
method in this line: peer.signal(callData.signalData);
For some reason after i call this line, the peer.on("signal")
(acceptCall method) is being called twice everytime i’m trying to make a call.
Also, it happens in two scenarios, because of different reasons(from what i understand):
The first time it happens is the initial call, i investigated a bit, and turns out it was related to renegotiation.
so just adding a hacky if(!data.renegotiate)
inside the peer.on("signal")
(acceptCall method) solved it.
like this:
peer.on("signal", (data) => {
if (!data.renegotiate) {
this.socket.emit("call:accept", {
signalData: data,
receiverId: callData.senderId,
});
}
});
The second time happens again on the same line, when it tries to re perform the call signaling after addStream
called,
and i have no idea why it happens in this scenario.
but because of this, it tries to accept the call again(this.socket.emit("call:accept"...)
) which causes errors.
not sure what to do here, tried to add logs to the library index.js, but can’t find the place that calls the on("signal")
again.
thanks.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:24
Top GitHub Comments
anyway, i figured out what was the problem. everytime i called
acceptCall
it redefined the.on("signal')
, that’s why whensimple-peer
calledemit
it was being called twice. thanks for the help 😃signal
is expected to be called multiple times unless you specifytrickle: false
in peer configuration. This mechanism is called Trickle ICE and is designed to make connection establishment faster.