6

我在 linux 上使用 google chrome 21.x,webrtc 对等连接建立但无法接收任何远程视频流,给予对等连接“.onaddstream”的回调永远不会被调用,有人可以建议我需要在哪里看?

我正在粘贴我的整个代码,仍然无法接收远程视频流,也没有任何错误。

var peerConnCreated = false;
var peerConn = null;
var cameraOn = false;
var clientId = 0;
var svcName = "";
var clientIdRecvd = false;
var myname = "";
var hisname = "";
var myJsep;
var hisJsep;
var mySdp;
var hisSdp;

function login()
{
    var loginid = document.getElementById("login").value;
    var jsonText = {"clientid":clientId, "service":"rtc", "mtype": "online", "username": loginid};
    myname = loginid;
    socket.send(JSON.stringify(jsonText));
}

function iceCallback(canditate, moreToFollow)
{
    if(canditate) {
        console.log("ice canditate");
        var jsonText = {"clientid":clientId, "service":"rtc", "mtype": "canditate", "sndr": myname, "rcpt": hisname, 
            "label": canditate.label, "cand": canditate.toSdp()};
        socket.send(JSON.stringify(jsonText));
    }
}

function onSessionConnecting(message)
{
    console.log("session connecting ...");
}

function onRemoteStreamRemoved(event)
{
    console.log("remote stream removed");
    remotevid.src = "";
}

function onSessionOpened(message)
{
    console.log("session opened");
}

function onRemoteStreamAdded(event)
{
    console.log("remote stream added");
    remotevid.src = window.webkitURL.createObjectURL(event.stream);
    remotevid.style.opacity = 1;
}

function createPeerConnection()
{
    if (peerConnCreated) return;
    peerConn = new webkitPeerConnection00("STUN stun.l.google.com:19302", iceCallback); 
    peerConn.onconnecting = onSessionConnecting;
    peerConn.onopen = onSessionOpened;
    peerConn.onaddstream = onRemoteStreamAdded;
    peerConn.onremovestream = onRemoteStreamRemoved;
    console.log("peer connection created");
    peerConnCreated = true;
}

function turnOnCameraAndMic()
{
    navigator.webkitGetUserMedia({video:true, audio:true}, successCallback, errorCallback);
    function successCallback(stream) {
        sourcevid.style.opacity = 1;
        sourcevid.src = window.webkitURL.createObjectURL(stream);
        peerConn.addStream(stream);
        console.log("local stream added");
    }
    function errorCallback(error) {
        console.error('An error occurred: [CODE ' + error.code + ']');
    }
    cameraOn = true;
}

function dialUser(user)
{
    if (!peerConnCreated) createPeerConnection();
    hisname = user;
    var localOffer = peerConn.createOffer({has_audio:true, has_video:true});
    peerConn.setLocalDescription(peerConn.SDP_OFFER, localOffer);
    mySdp =  peerConn.localDescription;
    myJsep = mySdp.toSdp();
    var call = {"clientid":clientId, "service":"rtc", "mtype": "call", "sndr": myname, "rcpt": hisname, "jsepdata": myJsep};
    socket.send(JSON.stringify(call));
    console.log("sent offer");
    //console.log(myJsep);
    peerConn.startIce();
    console.log("ice started ");
}

//handle the message from the sip server
//There is a new connection from our peer so turn on the camera 
//and relay the stream to peer.
function handleRtcMessage(request)
{
    var sessionRequest = eval('(' + request + ')');
    switch(sessionRequest.mtype) 
    {
        case 'online':
            console.log("new user online");
            var newuser = sessionRequest.username;
            var li = document.createElement("li");
            var name = document.createTextNode(newuser);
            li.appendChild(name);
            li.onclick = function() { dialUser(newuser); };
            document.getElementById("Contact List").appendChild(li);
            break;

        case 'call':
            console.log("recvng call");
            alert("Incoming call ...");
            if (!peerConnCreated) createPeerConnection();
            peerConn.setRemoteDescription(peerConn.SDP_OFFER, new SessionDescription(sessionRequest.jsepdata));
            hisname = sessionRequest.sndr;
            var remoteOffer = peerConn.remoteDescription;
            //console.log("remoteOffer" + remoteOffer.toSdp());
            var localAnswer = peerConn.createAnswer(remoteOffer.toSdp(), {has_audio:true, has_video:true}); 
            peerConn.setLocalDescription(peerConn.SDP_ANSWER, localAnswer);
            var jsonText = {"clientid":clientId,"service":"rtc", "mtype": "pickup", "sndr" :myname, "rcpt": hisname, "jsepdata": localAnswer.toSdp()};
            socket.send(JSON.stringify(jsonText));
            console.log("sent answer");
            //console.log(localAnswer.toSdp());
            peerConn.startIce();
            if (!cameraOn) turnOnCameraAndMic();
            break;

        case 'pickup':
            console.log("recvd pickup");
            peerConn.setRemoteDescription(peerConn.SDP_ANSWER, new SessionDescription(sessionRequest.jsepdata));
            hisname = sessionRequest.sndr;
            if (!cameraOn) turnOnCameraAndMic();
            break;

        case 'canditate':
            console.log("recvd canditate");
            var canditate = new IceCandidate(sessionRequest.label, sessionRequest.cand);
            peerConn.processIceMessage(canditate);
            break;

        case 'bye':
            console.log("recvd bye");
            break;
    }
}

//open the websocket  to the antkorp webserver
var socket = new WebSocket('ws://bldsvrub:9981');
var sourcevid = null;
var remotevid = null;

socket.onopen = function () {
    console.log("websocket opened");
    sourcevid = document.getElementById("sourcevid");
    remotevid = document.getElementById("remotevid");
};

socket.onmessage = function (event) { 
    if (!clientIdRecvd) {
        var reqObj = eval('(' + event.data + ')');
        clientId = reqObj.clientid;
        svcName  = reqObj.service;
        clientIdRecvd = true;
    } else {
        //hookup the new handler to process session requests
        handleRtcMessage(event.data);
    }
};

socket.onclose = function (event) { socket = null; };
4

4 回答 4

13

上面粘贴的代码包含一个小错误,应在生成 answer 或 offer 之前将流添加到对等连接,即应在任何 setlocalDescription 或 setRemoteDescription 调用之前调用“addStream”。

于 2012-08-08T10:44:50.603 回答
5

许多 WebRTC 演示:

例如一对一的 WebRTC 音频/视频/屏幕通话:

笔记:

这个问题太老了。这就是为什么我不认为我应该在这里添加一个工作片段代码片段的原因。上面的链接回答了所有问题。

但是,如果您是 NEW-WebRTC 用户并且遇到类似问题,那么这里有一些提示:

  • 在创建对等点之前,请确保两个对等点都准备好​​握手。
  • 就绪意味着,双方都可以访问媒体流(音频和/或视频)
  • 第一个对等点应该启动 RTCPeerConnection 对象,调用“addStream”并创建报价描述。
  • 第二个对等点应该从第一个对等点接收 OFFER-SDP。
  • 第二个对等点应该启动 RTCPeerConnection 对象,在创建 ANSWER-description 之前调用“addStream”和 setRemoteDescription。
  • 第二个对等方应该创建 ANSWER-SDP。
  • 第一个对等点应该得到 ANSWER-SDP 和 set-Remote-Descriptions。
  • ICE-candidate-pairs 应该与上述过程并行交换。

你可以在这里找到一些教程:

记住

此答案针对 WebRTC-1.0。它没有回答 WebRTC-1.1 (ORTC) 或更新版本。

于 2012-08-03T12:36:12.730 回答
3

onaddstream 应在收到包含至少一个流的答案时调用。如果您没有收到回调,请确保 setLocal 和 setRemoteDescription 都已被调用并成功。

于 2012-08-18T04:06:17.160 回答
0

我发现在我的情况下,如果不回复另一个视频,我将无法接收视频。

我解决了一个假视频流:

            let w = 640;
            let h = 480;
            let canvas: any = Object.assign(document.createElement("canvas"), { w, h });
            canvas.getContext('2d').fillRect(0, 0, w, h);
            let blackStream = canvas.captureStream();
            outgoingStream.addTrack(blackStream.getVideoTracks()[0]);
于 2018-08-16T18:20:55.260 回答