Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Malformed RTCConfiguration only in Chrome

I'm using WebRTC and realize it is not supported in all browsers. However, Chrome and Firefox do support it (in newer versions; I have the newest versions installed) as long as you have the correct prefix for certain variables. For instance, I have the following for PeerConnection for cross-browser support:

var PeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;

Now that it should be supported cross-browser, I have the following code:

var servers = {
iceservers: [
     {url: "stun:23.21.150.121"},
     {url: "stun:stun.1.google.com:19302"}
    ]   
};
var pc = PeerConnection(servers);

But in Chrome it gets an error on the last line (var pc = PeerConnection(servers);). The error is:

Failed to construct 'RTCPeerConnection': Malformed RTCConfiguration"}

Obviously, Chrome does not like my configuration parameter in the PeerConnection declaration. But my question is: why am I getting this error and how come only in Chrome? (FireFox works fine)

like image 830
chRyNaN Avatar asked Sep 22 '14 14:09

chRyNaN


2 Answers

Well, the solution is actually quiet simple. The servers object must be created with iceServers in camel case. Also, you forgot your new keyword when creating the connection but that is probably a typo in the question.

Like this:

var servers = {
iceServers: [
     {url: "stun:23.21.150.121"},
     {url: "stun:stun.1.google.com:19302"}
    ]   
};
var pc = new PeerConnection(servers);

Both all lower case and camel case work just fine in FireFox. So, changing it should not change how it works there but it must be camel case for it to work in Chrome.

like image 53
Benjamin Trent Avatar answered Nov 02 '22 06:11

Benjamin Trent


See Ben's answer. JavaScript is case-sensitive and 'iceServers' is the correct spelling from the mediacapture spec.

I wanted to clarify that all lower case 'iceservers' doesn't actually work in Firefox either, in that your STUN servers are being ignored. Firefox uses its default STUN server when it doesn't see one provided, so that's why it appears to work, but not using the STUN servers you think.

like image 2
jib Avatar answered Nov 02 '22 07:11

jib