I have an application where I'm sharing both my desktop and my webcam to my Kurento Server (two different endpoints in the same pipeline), starting a recording endpoint for both and then alerting the client on the other hand that they're both ready to consume.
My issue comes with having two WebRtcPeerRecvonly peers on my client, if one doesn't finish before the other one makes a request to consume I either get videos of the same Desktop endpoint or two videos of the same Webcam endpoint.
webcam peer
initWebcamUser(id){
let options = {
onicecandidate: (candidate) => {
socket.emit('onWebcamIceCandidate',{
candidate : candidate,
socket_id : id,
});
}
};
webRtcWebcamPeer = new kurentoUtils.WebRtcPeer.WebRtcPeerRecvonly(options, function(error) {
this.generateOffer((error, offerSdp) => {
socket.emit('viewerWebcam',{
sdpOffer : offerSdp,
socket_id : id
});
});
});
}
and my desktop peer.
initDesktop(socket_id){
let options = {
onicecandidate: (candidate) => {
socket.emit('onDesktopIceCandidate',{
candidate : candidate,
socket_id : socket_id,
});
}
}
webRtcDesktopPeer = new kurentoUtils.WebRtcPeer.WebRtcPeerRecvonly(options, function(error) {
this.generateOffer((error, offerSdp) => {
socket.emit('viewerDesktop',{
sdpOffer : offerSdp,
socket_id : socket_id
});
});
});
}
I've come to the conclusion that they're both sharing the same kurentoUtils.WebRtcPeer as if I set a delay of 2 seconds before invoking initDesktop after calling initWebcamUser I get the correct streams 100% of the time.
I guess this boils down to the question of is there anyway to do this concurrently? Or should I set up a promise based system on when the WebcamPeer is complete, if so where would I put this in this process, when iceCandidates are added?
Edit: I feel it's important to note that I am assigning these peers to their respective 'participants' in my webcamViewerResponse / desktopViewerResponse respectively so they wouldn't be referenced from those temp webRtcWebcamPeer/webRtcDesktopPeer variables when I'm having this issue.
Thanks in advance.
If anyone is looking for an answer for this, I found a solution. Not the most elegant but it works 100% of the time.
endpoint.on('OnIceComponentStateChanged', function(event) {
if(event.state==='CONNECTED'){
//resolve your promise to continue on with your next connection here.
}
});