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

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

问题描述

我试图实施仅限语音的WebRTC应用。我在Chrome 版本29.0.1547.0 dev 上运行它。我的应用程序使用Socket.IO作为信号机制。



peerConnection.addIceCandidate()给我这个错误: 未捕获的SyntaxError:指定了一个无效或非法的字符串。



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

SERVER (在CoffeeScript中)

($$ p
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ app $ require $($ express) 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.onconnection,(socket) - >
socket.onmessage,(data) - >
socket.broadcast.emitmessage,data

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

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

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

pc.addIceCandidate(candidate);

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


函数offer(){
pc.createOffer(函数(描述){
pc.setLocalDescription(description);
socket.emit(消息,{type:offer,description:description});
});
};

HTML仅包含一个调用 offer()

我可以确认 ICECandidates SessionDescriptions 成功从一个客户转移到另一个客户。



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



PS:如果您知道记录WebRTC API(除了W3C文档),请告诉我吧!



请注意,在创建Offer(by Offerer)之后,ICE Candidates必须在成功设置远程描述后添加。 ,冰候选人立即生产。因此,如果答疑者以某种方式收到这些候选人,那么在设置远程描述之前(理论上这将在候选人之前到达),您会收到错误消息。

同样适用于提供者。在添加候选冰块之前,它必须设置远程描述。



我发现在你的javascript代码中,你不能保证在添加候选冰块之前设置了远程描述。 >

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

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!

解决方案

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

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

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