Skip to content

Commit

Permalink
fix: fail sooner if ice connection goes to failed state (#3736)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcin-bazyl authored Aug 2, 2024
1 parent 7d9c68b commit 8892647
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ export default class MediaConnectionAwaiter {
return this.webrtcMediaConnection.getConnectionState() === ConnectionState.Connected;
}

/**
* Returns true if the connection is in an unrecoverable "failed" state
*
* @returns {boolean}
*/
private isFailed(): boolean {
return this.webrtcMediaConnection.getConnectionState() === ConnectionState.Failed;
}

/**
* Returns true if the ICE Gathering is completed, false otherwise.
*
Expand Down Expand Up @@ -83,6 +92,17 @@ export default class MediaConnectionAwaiter {
`Media:MediaConnectionAwaiter#connectionStateChange --> connection state: ${this.webrtcMediaConnection.getConnectionState()}`
);

if (this.isFailed()) {
LoggerProxy.logger.warn(
'Media:MediaConnectionAwaiter#connectionStateChange --> ICE failed, rejecting'
);
this.clearCallbacks();

this.defer.reject({
iceConnected: this.iceConnected,
});
}

if (!this.isConnected()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,47 @@ describe('MediaConnectionAwaiter', () => {
assert.calledThrice(mockMC.off);
});

it('rejects immediately if ice state is FAILED', async () => {
mockMC.getConnectionState.returns(ConnectionState.Connecting);
mockMC.getIceGatheringState.returns('gathering');

let promiseResolved = false;
let promiseRejected = false;

mediaConnectionAwaiter
.waitForMediaConnectionConnected()
.then(() => {
promiseResolved = true;
})
.catch((error) => {
promiseRejected = true;

const {iceConnected} = error;
assert.equal(iceConnected, false);
});

await testUtils.flushPromises();
assert.equal(promiseResolved, false);
assert.equal(promiseRejected, false);

// check the right listener was registered
assert.calledThrice(mockMC.on);
assert.equal(mockMC.on.getCall(0).args[0], Event.PEER_CONNECTION_STATE_CHANGED);
assert.equal(mockMC.on.getCall(1).args[0], Event.ICE_CONNECTION_STATE_CHANGED);
assert.equal(mockMC.on.getCall(2).args[0], Event.ICE_GATHERING_STATE_CHANGED);
const iceConnectionListener = mockMC.on.getCall(1).args[1];

mockMC.getConnectionState.returns(ConnectionState.Failed);
iceConnectionListener();

await testUtils.flushPromises();

assert.equal(promiseResolved, false);
assert.equal(promiseRejected, true);

assert.calledThrice(mockMC.off);
});

it('rejects after timeout if dtls state is not connected', async () => {
mockMC.getConnectionState.returns(ConnectionState.Connecting);
mockMC.getIceGatheringState.returns('gathering');
Expand Down

0 comments on commit 8892647

Please sign in to comment.