W3cubDocs

/DOM

RTCPeerConnection.setRemoteDescription

This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

Draft
This page is not complete.

The RTCPeerConnection.setRemoteDescription() method changes the remote description associated with the connection. This description specifies the properties of the remote end of the connection, including the media format. The method takes a single parameter—the session description—and it returns a Promise which is fulfilled once the description has been changed, asynchronously.

This is typically called after receiving an offer or answer from another peer over the signaling server. Keep in mind that if setRemoteDescription() is called while a connection is already in place, it means renegotiation is underway (possibly to adapt to changing network conditions). Because descriptions will be exchanged until the two peers agree on a configuration, the description submitted by calling setRemoteDescription() does not immediately take effect. Instead, the current connection configuration remains in place until negotiation is complete. Only then does the agreed-upon configuration take effect.

Syntax

aPromise = pc.setRemoteDescription(sessionDescription);

pc.setRemoteDescription(sessionDescription, successCallback, errorCallback); 

Parameters

sessionDescription
An RTCSessionDescriptionInit or RTCSessionDescription which specifies the configuration to be considered for the remote end of the connection.

The sessionDescription parameter is technically of type RTCSessionDescriptionInit, but because RTCSessionDescription serializes to be indistinguishable from RTCSessionDescriptionInit, you can also pass in an RTCSessionDescription. This lets you simplify code such as the following:

myPeerConnection.setRemoteDescription(new RTCSessionDescription(description)).then(function () {
  return createMyStream();
})

to simply be:

myPeerConnection.setRemoteDescription(description).then(function () {
  return createMyStream();
})

For this reason, the RTCSessionDescription() constructor is deprecated.

Return value

A Promise which is fulfilled once the value of RTCPeerConnection.remoteDescription is successfully changed or rejected if the change cannot be applied (for example, if the specified description is incompatible with one or both of the peers on the connection). The promise fulfillment handler receives no input parameters.

The process of changing descriptions actually involves intermediary steps handled by the WebRTC layer to ensure that an active connection can be changed without losing the connection if the change does not succeed. See Pending and current descriptions in WebRTC connectivity for more details on this process.

Deprecated parameters

In older code and documentation, you may see a callback-based version of this function used. This has been deprecated and its use is strongly discouraged. You should update any existing code to use the Promise-based version of setRemoteDescription() instead. The parameters for the older form of setRemoteDescription() are described below, to aid in updating existing code.

successCallback
A JavaScript Function which accepts no input parameters to be be called once the description has been successfully set. At that time, the offer can be sent to a remote peer via the signaling server.
errorCallback
A function matching the signautre RTCPeerConnectionErrorCallback which gets called if the description can't be set. It is passed a single DOMException object explaining why the request failed.

This deprecated form of the method returns instantaneously without waiting for the actual setting to be done: in case of success, the successCallback will be called; in case of failure, the errorCallback will be called.

Deprecated exceptions

When using the deprecated callback-based version of setRemoteDescription(), the following exceptions may occur:

InvalidStateError
The connection's signalingState is "closed", indicating that the connection is not currently open, so negotiation cannot take place.
InvalidSessionDescriptionError
The RTCSessionDescription specified by the sessionDescription parameter is invalid.

Example

Here we see a function which handles an offer received from the remote peer. This code is derived from the example and tutorial in the article Signaling and video calling; take a look at that for more details and a more in-depth explanation of what's going on.

function handleOffer(msg) {
  createMyPeerConnection();

  myPeerConnection.setRemoteDescription(msg.description).then(function () {
    return navigator.mediaDevices.getUserMedia(mediaConstraints);
  })
  .then(function(stream) {
    document.getElementById("local_video").srcObject = stream;
    return myPeerConnection.addStream(stream);
  })
  .then(function() {
    return myPeerConnection.createAnswer();
  })
  .then(function(answer) {
    return myPeerConnection.setLocalDescription(answer);
  })
  .then(function() {
    // Send the answer to the remote peer using the signaling server
  })
  .catch(handleGetUserMediaError);
}

After creating our RTCPeerConnection and saving it as myPeerConnection, we pass the description included in the received offer message, msg, directly into setRemoteDescription() to tell the user agent's WebRTC layer what configuration the caller has proposed using. When our promise fulfillment handler is called, indicating that this has been done, we create a stream, add it to the connection, then create an SDP answer and call setLocalConnection() to set that as the configuration at our end of the call before forwarding that answer to the caller.

Specifications

Browser compatibility

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) [1] (Yes) 22 (22) [2] No support (Yes) ?
Promise-based version 50.0 ? (Yes) (Yes) (Yes) (Yes)
RTCSessionDescriptionInit accepted ? ? 53 (53) ? ? ?
Errors represented by DOMException, not DOMError (Yes) (Yes) 58 (58) No support (Yes) No support
Feature Android Webview Chrome for Android Edge Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes)[1] (Yes)[1] (Yes) 22.0 (22) No support ? ?
Promise-based version 50.0 50.0 ? (Yes) (Yes) (Yes) (Yes)
RTCSessionDescriptionInit accepted ? ? ? 53.0 (53) ? ? ?
Errors represented by DOMException, not DOMError (Yes) (Yes) (Yes) 58.0 (58) No support (Yes) No support

[1] Though this method is not prefixed, the interface it belongs to was until Chrome 56.

[2] Though this method is not prefixed, the interface it belongs to is, until Firefox 44.

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/setRemoteDescription