简体   繁体   English

局部变量到全局javascript

[英]local variable to global javascript

I have a problem with JavaScript, webRTC and Kurento. 我有JavaScript,webRTC和Kurento的问题。 I'm not able to solve it by myself. 我自己无法解决这个问题。 I'm trying to put the remote stream from a local variable in a global variable but I have some troubles. 我试图将远程流从局部变量放入全局变量,但我遇到了一些麻烦。 I try to explain all steps to bring the problem out: The first step, I have the Kurento webRtcEndpoint function: 我尝试解释所有解决问题的步骤:第一步,我有Kurento webRtcEndpoint函数:

webRtcPeer = kurentoUtils.WebRtcPeer.startRecvOnly(videoElement, onPlayOffer, onError);

It calls the function "onPlayOffer" that is: 它调用函数“onPlayOffer”,即:

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've edited the function processSdpAnswer to take the stream in this way: 我编辑了函数processSdpAnswer以这种方式获取流:

WebRtcPeer.prototype.processSdpAnswer = function(sdpAnswer, callbackEvent, successCallback) {
//WebRtcPeer.prototype.processSdpAnswer = function(sdpAnswer, 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);
    //Added the string below to create the callback
    callbackEvent(event.stream);
};

self.pc.setRemoteDescription(answer, function() {
    if (self.remoteVideo) {
        var stream = self.pc.getRemoteStreams()[0];
        //console.log('Kurento-Utils: Second self.pc');
        //console.log(self.pc)
        self.remoteVideo.src = URL.createObjectURL(stream);
    }
    if (successCallback) {
        successCallback();
    }
}, this.onerror);

So, in this case the callback is the function recordVideo, which is passed "event.stream" 因此,在这种情况下,回调是函数recordVideo,它传递给“event.stream”

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);}

So I expect that in the function "onPlayOffer" I may have the object localStream (declared globally) as a copy of stream (that is local). 所以我希望在函数“onPlayOffer”中我可以将对象localStream(全局声明)作为流的副本(即本地)。 The variable "stream" is correct, the variable "localStream" instead, is UNDEFINED. 变量“stream”是正确的,而变量“localStream”则是UNDEFINED。

Can you help me to understand why? 你能帮我理解为什么吗? I've read that maybe the problem will be the console, but I've tried to comment all console.log line without success. 我已经读过,问题可能是控制台,但我试图评论所有console.log行没有成功。 Can you help me? 你能帮助我吗? Thank you everybody! 谢谢大家!

(if anybody know a faster way to take the event.stream object globally, I will be thanks for the help!) (如果有人知道更快的方式来全局采用event.stream对象,我将感谢您的帮助!)

you are right, your problem is asynchronous, 你是对的,你的问题是异步的,

simplest way to correct it would be, putting whatever code/logic that has to follow the async call as an callback of that async call, 最简单的纠正方法是,将任何必须遵循异步调用的代码/逻辑作为异步调用的回调,

it can be done by changing 它可以通过改变来完成

    ...
    webRtcPeer.processSdpAnswer(sdpAnswer, recordVideo);

    console.log('DEBUG: ok, AGAIN, localStream: ');
    console.log(localStream);

    yield player.play();
    ...

into

    ...
    var callback = function (){
        console.log('DEBUG: ok, AGAIN, localStream: ');
        console.log(localStream);

        yield player.play();
    };
    webRtcPeer.processSdpAnswer(sdpAnswer, recordVideo.bind({callback: callback})); // through bind, we are setting the `this` value of the recordVideo.

and modify the recordVideo into 并将recordVideo修改为

function recordVideo(stream) {
    ...
    this.callback();    // extra line added.
}

You are missing a yield, and this is making the code below: 你错过了一个收益,这就是下面的代码:

webRtcPeer.processSdpAnswer(sdpAnswer, recordVideo);

to execute before recordVideo 在recordVideo之前执行

To solve it just put instead 解决它只是改为

yield webRtcPeer.processSdpAnswer(sdpAnswer, recordVideo);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM