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

Commit a9ec987

Browse files
jianjunztaste1981
authored andcommitted
Fire ended event when PeerConnection's connectionState changed to closed (#285)
or failed. This change is based on commit 21bdf18.
1 parent cb2a778 commit a9ec987

File tree

1 file changed

+38
-20
lines changed

1 file changed

+38
-20
lines changed

src/sdk/conference/channel.js

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
4747
// Timer for PeerConnection disconnected. Will stop connection after timer.
4848
this._disconnectTimer = null;
4949
this._ended = false;
50+
this._stopped = false;
5051
}
5152

5253
/**
@@ -367,24 +368,30 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
367368
}
368369

369370
_unpublish() {
370-
this._signaling.sendSignalingMessage('unpublish', {id: this._internalId})
371-
.catch((e) => {
372-
Logger.warning('MCU returns negative ack for unpublishing, ' + e);
373-
});
374-
if (this._pc && this._pc.signalingState !== 'closed') {
375-
this._pc.close();
371+
if (!this._stopped) {
372+
this._stopped = true;
373+
this._signaling.sendSignalingMessage('unpublish', {id: this._internalId})
374+
.catch((e) => {
375+
Logger.warning('MCU returns negative ack for unpublishing, ' + e);
376+
});
377+
if (this._pc && this._pc.signalingState !== 'closed') {
378+
this._pc.close();
379+
}
376380
}
377381
}
378382

379383
_unsubscribe() {
380-
this._signaling.sendSignalingMessage('unsubscribe', {
381-
id: this._internalId,
382-
})
383-
.catch((e) => {
384-
Logger.warning('MCU returns negative ack for unsubscribing, ' + e);
385-
});
386-
if (this._pc && this._pc.signalingState !== 'closed') {
387-
this._pc.close();
384+
if (!this._stopped) {
385+
this._stopped = true;
386+
this._signaling.sendSignalingMessage('unsubscribe', {
387+
id: this._internalId,
388+
})
389+
.catch((e) => {
390+
Logger.warning('MCU returns negative ack for unsubscribing, ' + e);
391+
});
392+
if (this._pc && this._pc.signalingState !== 'closed') {
393+
this._pc.close();
394+
}
388395
}
389396
}
390397

@@ -483,12 +490,18 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
483490
return;
484491
}
485492

486-
Logger.debug('ICE connection state changed to '
487-
+ event.currentTarget.iceConnectionState);
488-
if (event.currentTarget.iceConnectionState === 'closed'
489-
|| event.currentTarget.iceConnectionState === 'failed') {
490-
this._rejectPromise(
491-
new ConferenceError('ICE connection failed or closed.'));
493+
Logger.debug('ICE connection state changed to ' +
494+
event.currentTarget.iceConnectionState);
495+
if (event.currentTarget.iceConnectionState === 'closed' ||
496+
event.currentTarget.iceConnectionState === 'failed') {
497+
// Fire ended event if publication or subscription exists.
498+
this._fireEndedEventOnPublicationOrSubscription();
499+
}
500+
}
501+
502+
_onConnectionStateChange(event) {
503+
if (this._pc.connectionState === 'closed' ||
504+
this._pc.connectionState === 'failed') {
492505
// Fire ended event if publication or subscription exists.
493506
this._fireEndedEventOnPublicationOrSubscription();
494507
}
@@ -523,6 +536,9 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
523536
this._pc.oniceconnectionstatechange = (event) => {
524537
this._onIceConnectionStateChange.apply(this, [event]);
525538
};
539+
this._pc.onconnectionstatechange = (event) => {
540+
this._onConnectionStateChange.apply(this, [event]);
541+
};
526542
}
527543

528544
_getStats() {
@@ -598,6 +614,8 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
598614
error: error,
599615
});
600616
dispatcher.dispatchEvent(errorEvent);
617+
// Fire ended event when error occured
618+
this._fireEndedEventOnPublicationOrSubscription();
601619
}
602620

603621
_setCodecOrder(sdp, options) {

0 commit comments

Comments
 (0)