【问题标题】:peerConnection.addIceCandidate giving error: invalid stringpeerConnection.addIceCandidate 给出错误:无效字符串
【发布时间】:2013-06-25 03:49:09
【问题描述】:

我正在尝试实现仅语音的 WebRTC 应用程序。我在 Chrome Version 29.0.1547.0 dev 上运行它。我的应用使用 Socket.IO 作为信号机制。

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

另外,peerConnection.setRemoteDescription(); 给了我这个错误:Uncaught TypeMismatchError: The type of an object was incompatible with the expected type of the parameter associated to the object.

这是我的代码:

服务器(在 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

客户(在 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});
    });
};

HTML 只包含一个调用offer() 的按钮。

我可以确认ICECandidatesSessionDescriptions 正在成功地从一个客户端转移到另一个客户端。

我做错了什么?我应该如何修复这些错误以及任何其他错误,以便将音频从一个客户端传输到另一个客户端?

PS:如果您知道记录 WebRTC API 的良好来源(W3C 文档除外),请告诉我!

谢谢!

【问题讨论】:

  • 您能否粘贴您传递给 RTCIceCandidate 构造函数的 data.candidate 示例?
  • 我无法回答您的具体问题,但我发现WebRTC-Series of html5rocks 是关于该主题的一个很好的资源。
  • 我无法解决这个问题,即使在远程描述设置之前缓存远程候选冰。 RTCIceCandidate {sdpMLineIndex: 1, sdpMid: "", Candidate: "a=candidate:924013166 1 tcp 1509957375 192.168.57.1​​ 0 typ host generation 0 ↵"} app.js:13895 Uncaught SyntaxError:指定了无效或非法的字符串。

标签: html node.js websocket socket.io webrtc


【解决方案1】:

对于那个错误,关键是必须在成功设置远程描述后才能添加 ICE Candidates。

请注意,在(通过 Offerer)创建 Offer 之后,会立即生成 ice 候选者。因此,如果回答者在设置远程描述(理论上会在候选人之前到达)之前以某种方式接收到这些候选人,则会出现错误。

提供者也是如此。在添加任何候选冰之前必须设置远程描述。

我看到,在您的 javascript 代码中,您不能保证在添加候选冰之前设置远程描述。

首先你可以检查pc.addIceCandidate(candidate); 之前是否设置了电脑的remoteDescription。如果您看到它为空(或未定义),您可以在本地存储接收到的候选冰,然后在设置 remoteDescription 后添加它们(或等待提供者在适当的时间发送它们。)

【讨论】:

  • 您写道:which in theory would arrive antes candidates。我认为antes 是错字?你想说什么?
  • 误写了西班牙语单词。前注 = 之前。我已经更正了。
  • 所以,如果我做得对,请告诉我。 onicecandidate 由提供对等方调用,在 RTCPeerConnection 构建之后。然后回答节点运行addIceCandidate 与提供节点的候选人?
  • 对于以后可能遇到类似问题的读者,基本上答主需要使用pc.onicecandidate = function(event) { /* Get event.candidate and give it to the offerer */ }。在创建流之后但在创建答案之前执行此操作。然后对于提议者,将pc.addIceCandidate 与从回答者获得的每个 ICE 候选人一起使用(您需要某种方式在提议者和回答者之间传输此数据,但现在应该已经有了)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-21
  • 1970-01-01
  • 2011-11-15
  • 1970-01-01
  • 2017-10-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多