4

我对 JavaScript、webRTC 和 Kurento 有疑问。我自己无法解决。我正在尝试将来自局部变量的远程流放在全局变量中,但我遇到了一些麻烦。我尝试解释解决问题的所有步骤: 第一步,我有 Kurento webRtcEndpoint 函数:

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

它调用函数“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();

我已经编辑了函数 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);

所以,在这种情况下,回调是函数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);}

所以我希望在函数“onPlayOffer”中,我可能将对象 localStream(全局声明)作为流的副本(即本地)。变量“stream”是正确的,而变量“localStream”是未定义的。

你能帮我理解为什么吗?我读过也许问题出在控制台上,但我试图评论所有 console.log 行但没有成功。你能帮助我吗?谢谢大家!

(如果有人知道一种更快的方法来全局获取 event.stream 对象,我将感谢您的帮助!)

4

2 回答 2

2

你是对的,你的问题是异步的,

纠正它的最简单方法是将必须遵循异步调用的任何代码/逻辑作为该异步调用的回调,

可以通过改变来完成

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

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

    yield player.play();
    ...

进入

    ...
    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.

并将recordVideo修改为

function recordVideo(stream) {
    ...
    this.callback();    // extra line added.
}
于 2015-04-14T02:02:17.457 回答
1

你错过了一个收益,这使得下面的代码:

webRtcPeer.processSdpAnswer(sdpAnswer, recordVideo);

在recordVideo之前执行

要解决它只是把

yield webRtcPeer.processSdpAnswer(sdpAnswer, recordVideo);
于 2015-04-14T08:23:15.933 回答