Skip to content
This repository has been archived by the owner on Oct 25, 2024. It is now read-only.

Commit

Permalink
Setting local description is considered as negotiating. (#486)
Browse files Browse the repository at this point in the history
When the impolite side receives offer after `setLocalDescription`, before it's
resolved, it sets both local description and remote description. Its
signaling state changes from stable to have-local-offer, to stable, to
have-remote-offer. It doesn't comply with the behavior of impolite side
described in perfect negotiation, which is ignoring the remote
description.

Test: P2P case WebRTC collision should be resolved.
  • Loading branch information
jianjunz authored Apr 23, 2021
1 parent 943e6b4 commit d7ab0bd
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/sdk/p2p/peerconnection-channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ class P2PPeerConnectionChannel extends EventDispatcher {
this._sendDataPromises = new Map(); // Key is data sequence number, value is an object has |resolve| and |reject|.
this._addedTrackIds = []; // Tracks that have been added after receiving remote SDP but before connection is established. Draining these messages when ICE connection state is connected.
this._isPolitePeer = localId < remoteId;
this._settingLocalSdp = false;
this._settingRemoteSdp = false;
this._disposed = false;
this._createPeerConnection();
this._sendSignalingMessage(SignalingType.UA, sysInfo);
Expand Down Expand Up @@ -416,10 +418,13 @@ class P2PPeerConnectionChannel extends EventDispatcher {
_onOffer(sdp) {
Logger.debug('About to set remote description. Signaling state: ' +
this._pc.signalingState);
if (this._pc.signalingState !== 'stable') {
if (this._pc.signalingState !== 'stable' || this._settingLocalSdp) {
if (this._isPolitePeer) {
Logger.debug('Rollback.');
this._pc.setLocalDescription();
this._settingLocalSdp = true;
this._pc.setLocalDescription().then(() => {
this._settingLocalSdp = false;
});
} else {
Logger.debug('Collision detected. Ignore this offer.');
return;
Expand All @@ -434,7 +439,9 @@ class P2PPeerConnectionChannel extends EventDispatcher {
sdp.sdp = this._setCodecOrder(sdp.sdp);
}
const sessionDescription = new RTCSessionDescription(sdp);
this._settingRemoteSdp = true;
this._pc.setRemoteDescription(sessionDescription).then(() => {
this._settingRemoteSdp = false;
this._createAndSendAnswer();
}, (error) => {
Logger.debug('Set remote description failed. Message: ' + error.message);
Expand All @@ -447,9 +454,11 @@ class P2PPeerConnectionChannel extends EventDispatcher {
this._pc.signalingState);
sdp.sdp = this._setRtpSenderOptions(sdp.sdp, this._config);
const sessionDescription = new RTCSessionDescription(sdp);
this._settingRemoteSdp = true;
this._pc.setRemoteDescription(new RTCSessionDescription(
sessionDescription)).then(() => {
Logger.debug('Set remote descripiton successfully.');
this._settingRemoteSdp = false;
this._drainPendingMessages();
}, (error) => {
Logger.debug('Set remote description failed. Message: ' + error.message);
Expand Down Expand Up @@ -548,7 +557,8 @@ class P2PPeerConnectionChannel extends EventDispatcher {
}

_onNegotiationneeded() {
if (this._pc.signalingState === 'stable') {
if (this._pc.signalingState === 'stable' && !this._pc._settingLocalSdp &&
!this._settingRemoteSdp) {
this._doNegotiate();
} else {
this._isNegotiationNeeded = true;
Expand Down Expand Up @@ -849,7 +859,9 @@ class P2PPeerConnectionChannel extends EventDispatcher {
this._pc.createOffer().then((desc) => {
desc.sdp = this._setRtpReceiverOptions(desc.sdp);
if (this._pc.signalingState === 'stable') {
this._settingLocalSdp = true;
return this._pc.setLocalDescription(desc).then(() => {
this._settingLocalSdp = false;
return this._sendSdp(this._pc.localDescription);
});
}
Expand All @@ -867,7 +879,10 @@ class P2PPeerConnectionChannel extends EventDispatcher {
this._pc.createAnswer().then((desc) => {
desc.sdp = this._setRtpReceiverOptions(desc.sdp);
this._logCurrentAndPendingLocalDescription();
return this._pc.setLocalDescription(desc);
this._settingLocalSdp = true;
return this._pc.setLocalDescription(desc).then(()=>{
this._settingLocalSdp = false;
});
}).then(()=>{
return this._sendSdp(this._pc.localDescription);
}).catch((e) => {
Expand Down

0 comments on commit d7ab0bd

Please sign in to comment.