peerConnection.addIceCandidate giving error: inval

2020-02-29 03:39发布

I am trying to implement a Voice-only WebRTC app. I am running it on Chrome Version 29.0.1547.0 dev. My app uses Socket.IO for the signaling mechanism.

peerConnection.addIceCandidate() is giving me this error: Uncaught SyntaxError: An invalid or illegal string was specified.

and separately, peerConnection.setRemoteDescription(); is giving me this error: Uncaught TypeMismatchError: The type of an object was incompatible with the expected type of the parameter associated to the object.

Here's my code:

SERVER (in CoffeeScript)

app = require("express")()
server = require("http").createServer(app).listen(3000)
io = require("socket.io").listen(server)

app.get "/", (req, res) -> res.sendfile("index.html")
app.get "/client.js", (req, res) -> res.sendfile("client.js")

io.sockets.on "connection", (socket) ->
    socket.on "message", (data) ->
        socket.broadcast.emit "message", data

CLIENT (in JavaScript)

var socket = io.connect("http://localhost:3000");

var pc = new webkitRTCPeerConnection({
    "iceServers": [{"url": "stun:stun.l.google.com:19302"}]
});


navigator.getUserMedia = navigator.webkitGetUserMedia ||
    navigator.mozGetUserMedia;

navigator.getUserMedia({audio: true}, function (stream) {
    pc.addStream(stream);
}, function (error) { console.log(error); });


pc.onicecandidate = function (event) {
    if (!event || !event.candidate) return;
    socket.emit("message", {
        type: "iceCandidate",
        "candidate": event.candidate
    });
};


pc.onaddstream = function(event) {
    var audioElem = document.createElement("audio");
    audioElem.src = webkitURL.createObjectURL(event.stream);
    audioElem.autoplay = true;
    document.appendChild(audioElem);
    console.log("Got Remote Stream");
};


socket.on("message", function(data) {
    if (data.type === "iceCandidate") {
        console.log(data.candidate);

        candidate = new RTCIceCandidate(data.candidate);
        console.log(candidate);

        pc.addIceCandidate(candidate);

    } else if (data.type === "offer") {
        pc.setRemoteDescription(data.description);
        pc.createAnswer(function(description) {
            pc.setLocalDescription(description);
            socket.emit("message", {type: "answer", description: description});
        });
    } else if (data.type === "answer") {
        pc.setRemoteDescription(data.description);
    }
});


function offer() {
    pc.createOffer( function (description) {
        pc.setLocalDescription(description);
        socket.emit("message", {type: "offer", "description": description});
    });
};

The HTML just contains a button that calls offer().

I can confirm the ICECandidates and SessionDescriptions are transferring successfully from one client to the other.

What am I doing wrong? And how should I fix these and any other errors so that I can transfer audio from one client to the other?

PS: If you know about a good source documenting the WebRTC API (except the W3C documentation), please tell me about it!

Thanks!

1条回答
趁早两清
2楼-- · 2020-02-29 04:09

For that error the point is, ICE Candidates must be added only after successfully setting remote description.

Note that just after creating Offer (by Offerer), ice candidates are produced immediately. So, if somehow the answerer receives those candidates, before setting remote description (which in theory would arrive before candidates), you get error.

The same is true for offerer. It must set remote description before adding any ice candidate.

I see that in your javascript code you are not guaranteeing that remote description is set before adding ice candidates.

First of all you can check just before pc.addIceCandidate(candidate); if pc's remoteDescription is set. If you see that it is null (or undefined), you can locally store received ice candidates to add them after setting remoteDescription (or wait in offerer to send them in proper time.)

查看更多
登录 后发表回答