peerConnection.addIceCandidate 给出错误:无效的字符串 [英] peerConnection.addIceCandidate giving error: invalid string

查看:29
本文介绍了peerConnection.addIceCandidate 给出错误:无效的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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() 给了我这个错误:Uncaught SyntaxError:一个无效或非法的字符串被指定.

另外,peerConnection.setRemoteDescription(); 给了我这个错误:Uncaught TypeMismatchError:对象的类型与与该对象关联的参数的预期类型不兼容.

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.

这是我的代码:

服务器(在 CoffeeScript 中)

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

客户端(在 JavaScript 中)

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});
    });
};

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

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

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

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:如果您知道记录 WebRTC API 的良好来源(W3C 文档除外),请告诉我!

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

谢谢!

推荐答案

对于那个错误的重点是,ICE Candidates 必须在成功设置远程描述后才能添加.

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

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

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.

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

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

首先,您可以在 pc.addIceCandidate(candidate); 之前检查是否设置了 pc 的 remoteDescription.如果看到为null(或者undefined),可以在本地存储收到的ice候选,设置remoteDescription后添加(或者在offer中等待适时发送)

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.)

这篇关于peerConnection.addIceCandidate 给出错误:无效的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆