简体   繁体   English

peerConnection.addIceCandidate给出错误:无效的字符串

[英]peerConnection.addIceCandidate giving error: invalid string

I am trying to implement a Voice-only WebRTC app. 我正在尝试实现仅限语音的WebRTC应用。 I am running it on Chrome Version 29.0.1547.0 dev . 我在Chrome Version 29.0.1547.0 dev上运行它。 My app uses Socket.IO for the signaling mechanism. 我的应用程序使用Socket.IO作为信令机制。

peerConnection.addIceCandidate() is giving me this error: Uncaught SyntaxError: An invalid or illegal string was specified. peerConnection.addIceCandidate()给我这个错误: Uncaught SyntaxError: An invalid or illegal string was specified.

and separately, peerConnection.setRemoteDescription(); 另外, 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. 给我这个错误: 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) SERVER (在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) CLIENT (用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() . HTML只包含一个调用offer()的按钮。

I can confirm the ICECandidates and SessionDescriptions are transferring successfully from one client to the other. 我可以确认ICECandidatesSessionDescriptions正在从一个客户端成功传输到另一个客户端。

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! PS:如果您了解记录WebRTC API的良好来源(W3C文档除外),请告诉我相关信息!

Thanks! 谢谢!

For that error the point is, ICE Candidates must be added only after successfully setting remote description. 对于该错误,重点是,只有在成功设置远程描述后才能添加ICE Candidates。

Note that just after creating Offer (by Offerer), ice candidates are produced immediately. 请注意,在创建Offer(由Offerer)之后,立即生成冰候选者。 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. 我在你的javascript代码中看到你并不保证在添加冰候选者之前设置了远程描述。

First of all you can check just before pc.addIceCandidate(candidate); 首先,你可以在pc.addIceCandidate(candidate);之前检查pc.addIceCandidate(candidate); if pc's remoteDescription is set. 如果设置了pc的remoteDescription。 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.) 如果您看到它为null(或未定义),则可以在设置remoteDescription后在本地存储已接收的冰候选者(或等待提交者在适当的时间发送它们)。

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

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