I'm facing an issue with JavaScript, webRTC, and Kurento that I can't seem to resolve on my own. My problem arises when trying to store the remote stream from a local variable into a global variable. I'll explain the steps leading to the issue: Firstly, the Kurento webRtcEndpoint function is called like this:
webRtcPeer = kurentoUtils.WebRtcPeer.startRecvOnly(videoElement, onPlayOffer, onError);
It triggers the "onPlayOffer" function which looks like:
function onPlayOffer(sdpOffer) {
co(function * () {
try {
if (!client) client = yield kurentoClient(args.ws_uri);
pipeline = yield client.create('MediaPipeline');
var webRtc = yield pipeline.create('WebRtcEndpoint');
var player = yield pipeline.create('PlayerEndpoint', { uri: args.file_uri });
yield player.connect(webRtc);
var sdpAnswer = yield webRtc.processOffer(sdpOffer);
webRtcPeer.processSdpAnswer(sdpAnswer, recordVideo);
console.log('DEBUG: ok, AGAIN, localStream: ');
console.log(localStream);
yield player.play();
I made changes to the processSdpAnswer function to handle the stream like this:
WebRtcPeer.prototype.processSdpAnswer = function(sdpAnswer, callbackEvent, successCallback) {
var answer = new RTCSessionDescription({
type : 'answer',
sdp : sdpAnswer,
});
console.log('Kurento-Utils: SDP answer received, setting remote description');
var self = this;
self.pc.onaddstream = function(event) {
var objectURL = URL.createObjectURL(event.stream);
callbackEvent(event.stream);
};
self.pc.setRemoteDescription(answer, function() {
if (self.remoteVideo) {
var stream = self.pc.getRemoteStreams()[0];
self.remoteVideo.src = URL.createObjectURL(stream);
}
if (successCallback) {
successCallback();
}
}, this.onerror);
In this scenario, the recordVideo function acts as a callback to store the stream globally:
function recordVideo(stream) {
console.log("DEBUG: called function recordVideo()");
localStream = stream;
console.log("DEBUG: Copied stream -> localStream:");
console.log(localStream);
console.log("DEBUG: the stream object contains:");
console.log(stream);}
However, when I check the value of "localStream" in the onPlayOffer function, it appears as UNDEFINED, while "stream" is correct. I'm unsure why this is happening. I've tried removing console.log statements as a possible solution, but it didn't work. Can someone please assist me in figuring out the issue? Any help is greatly appreciated!
(If anyone knows a quicker way to access the event.stream object globally, I would be grateful for any suggestions!)