I am trying to test/run/learn some WebRTC code and am having problem understanding this code from Mozilla:
...
var pc = new RTCPeerConnection();
...
pc.createOffer(function(offer) {
pc.setLocalDescription(new RTCSessionDescription(offer), function() {
// send the offer
}, error);
}, error);
The problem I have in understanding this is that pc.createOffer already returns an "offer" object with two properties: type and sdp. So, why is "new RTCSessionDescription(offer)" passed as an argument to pc.setLocalDescription and not "offer" itself as returned by pc.createOffer?
I read on RTCSessionDescription interface here. What did I miss?
Note that the MDN page you reference says "Draft. This page is not complete."
The code will work as written - because an RTCSessionDescription can effectively be cloned by passing one into another's constructor - but you're right that it is redundant, so I've updated the page to remove it. The object returned from createOffer will always suffice. Thanks for catching it.
Note that you still need to call new RTCSessionDescription in situations where the offer/answer comes in over the wire, for now.
But you'll be glad to know that that's being fixed as well soon: The spec recently changed, so you wont have to call the new RTCSessionDescription constructor ever. Much simpler. Note though that browsers like Firefox hasn't updated to allow this simpler syntax yet. This is just a convenience though, and there is no behavioral difference.
Update: adapter.js has a shim for this, so you can get rid of them now if you're using adapter.js.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With