3

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?

Sunny
  • 9,245
  • 10
  • 49
  • 79

1 Answers1

3

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.

jib
  • 40,579
  • 17
  • 100
  • 158
  • Thanks a lot for the clarification. Regarding your statement: "Note that you still need to call new RTCSessionDescription in situations where the offer/answer comes in over the wire, for now." I understand that this is being fixed as well. But I was wondering how does/did any webRTC interface/method know/knew that the offer/answer SDP passed to it has come "over the wire." – Sunny Apr 11 '16 at 03:30
  • One more question. Is RTCSessionDescription.toJSON() a "standard?" I know it is only for convenience but would be nice to know how reliable it is to avoid express conversion of SDP to JSON before sending it over the signalling channel. – Sunny Apr 11 '16 at 03:42
  • sorry, the second question is answered in the documentation. .toJSON is supported in Chrome, Firefox and Opera. – Sunny Apr 11 '16 at 03:49
  • 1
    @Sam `RTCSessionDescription` is a built-in class in the browser (log the `offer` produced by `createOffer` to console and you see `[object RTCSessionDescription]`), in contrast to the plain JavaScript object you get after serializing over a connection. Browser methods can tell the difference. – jib Apr 11 '16 at 03:54
  • can you please take a look at [this](http://stackoverflow.com/questions/36839080/am-getting-ice-candidates-only-with-sdpmid-audio-but-not-for-video) It concerns generation of ICE candidates. Thanks! – Sunny Apr 25 '16 at 11:40