0

我为一个学生项目编写了一个小的 webRTC 示例。目的是,一个客户端(学生)通过 peerConnection 将他的视频和音频流发送到第二个客户端(导师)。沟通有效,导师得到了 SDP_OFFER 和所有这些东西。它还获得了 remoteVideo src 的 blob。但是视频输出是黑色的。在导师方面,我收到以下错误:

未捕获的错误:SYNTAX_ERR:DOM 异常 12
doAnswer processSignalingMessage
ws.onmessage

我希望有人有一个想法。谢谢!

有我的代码:

<!DOCTYPE html>  
<html>
    <head>
        <title>PeerConnection with an websocket</title>
    </head>
    <script>
    //variables
    var pc;
    var ws;
    var name; 

    // Local stream generation
    function gotStream(s) {
        var url = webkitURL.createObjectURL(s);
        document.getElementById("localView").src = url;
        localStream = s;
        console.log("gotStream" + s)
        console.log("Started showing loval stream. url = " + url); 
    }
    function gotStreamFailed(error) {
        console.log("Failed to get access to local media. Error code was " 
                    + error.code + ".");
    }
    function getUserMedia() {
        try {
            navigator.webkitGetUserMedia({audio:true, video:true}, 
                                        gotStream, gotStreamFailed);
        console.log("Requested access to local media");
        } catch (e) {
            console.log(e, "getUserMedia error");
        }
    }

    //peerConnection generation
    function genreatePeerConnection(){
        try{
            pc = new webkitPeerConnection00("STUN stun.l.google.com:19302", onIceCandidate);
            console.log("new peerConnection!");
            pc.onaddstream = onAddStream;
            pc.onremovestream = onRemoveStream;
        }catch (e){
            console.log(e, "generatePeerConnection error");
        }
    }
    function onAddStream(e) {
        var stream = e.stream;
        var url = webkitURL.createObjectURL(stream);
        try{
            document.getElementById("remoteView").src = url;
            console.log("Started showing remote stream. url = " + url);
        }catch (e){
            console.log(e, "no Video error...");
        }

    }
    function onRemoveStream(e) {
        document.getElementById("remoteView").src = "";
        trace("Stopped showing remote stream");
    }
    function onIceCandidate(candidate, moreToFollow) {
        console.log("candidate: " + candidate);
        if (candidate) {
            sendMessage({type: 'candidate',
                        label: candidate.label, candidate: candidate.toSdp()});
        }
    }
    function startCall() {
        console.log("adding stream" + localStream);
        pc.addStream(localStream);
        doCall();
    }
    function doCall() {
        console.log("Send offer to peer");
        var offer = pc.createOffer({audio:true, video:true});
        pc.setLocalDescription(pc.SDP_OFFER, offer);
        sendMessage({type: 'offer', sdp: offer.toSdp()});
        pc.startIce();
    }
    function doAnswer() {
        console.log("Send answer to peer");
        var offer = pc.remoteDescription;
        var answer = pc.createAnswer(offer.toSdp(),{audio:true, video:true});
        pc.setLocalDescription(pc.SDP_ANSWER, answer);
        sendMessage({type: 'answer', sdp: answer.toSdp()});
        pc.startIce();
    }
    function processSignalingMessage(message) {
        var msg = JSON.parse(message.data);

        if (msg.type === 'offer') {
            console.log("Test 1 - offer" + msg.sdp);
            pc.setRemoteDescription(pc.SDP_OFFER, new SessionDescription(msg.sdp));
            doAnswer();
        } else if (msg.type === 'answer') {
            pc.setRemoteDescription(pc.SDP_ANSWER, new SessionDescription(msg.sdp));
        } else if (msg.type === 'candidate') {
            console.log("Candidate...");
            var candidate = new IceCandidate(msg.label, msg.candidate);
            pc.processIceMessage(candidate);
        } else if (msg.type === 'bye') {
            onRemoteHangup();
        }
    }

    //Websocket connection 
    function openWs(){
        genreatePeerConnection();
        ws = new WebSocket("ws://10.68.0.88:10081");
        ws.onopen = function() {
                        console.log("Openned websocket connection");
                    }
        ws.onmessage = function (evt){
                            console.log("you got mail: " + evt);
                            processSignalingMessage(evt);               
                       }
    }

    //sendMessage to WS
    function sendMessage(msg){
        var stringMsg = JSON.stringify(msg);
        ws.send(stringMsg);        
    }
    function sendName(){
        name = document.getElementById("name").value;
        ws.send("name: " + name);
    }
    </script>
    <body>
        name<input id="name"/><button id="sendName" onclick="sendName();">send </button>

        <video id="localView" width="352" height="288" autoplay></video>
        <video id="remoteView" width="352" height="288" autoplay></video>
        <button id="call" onclick="startCall();">Call</button>
        <button id="connect" onclick="openWs();">Connection</button>
        <button id="getMedia" onclick="getUserMedia();">Cam</button>
    </body>
</html>
4

2 回答 2

0

仔细查看以下示例:http ://www.html5rocks.com/en/tutorials/webrtc/basics/

我认为您可能缺少一些内容,例如停止被调用方在 processSignalingMessage() 中发送报价并将 createPeerConnection() 放在 pc.addStream 之前的 startCall() 中。祝你好运!

于 2012-09-05T21:57:18.127 回答
0

非常感谢你的例子!我不知道为什么我这么盲目,但通过这个例子我发现了我的错误。我在 doCall() 函数中忘记了以下行:

pc.setLocalDescription(pc.SDP_OFFER, offer);

我不知道我怎么会错过这个,但也许我只见树木不见森林。现在它起作用了!谢谢 :)

于 2012-09-18T11:32:50.573 回答