@@ -48,6 +48,7 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
4848 // Timer for PeerConnection disconnected. Will stop connection after timer.
4949 this . _disconnectTimer = null ;
5050 this . _ended = false ;
51+ this . _stopped = false ;
5152 }
5253
5354 /**
@@ -407,24 +408,30 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
407408 }
408409
409410 _unpublish ( ) {
410- this . _signaling . sendSignalingMessage ( 'unpublish' , { id : this . _internalId } )
411- . catch ( ( e ) => {
412- Logger . warning ( 'MCU returns negative ack for unpublishing, ' + e ) ;
413- } ) ;
414- if ( this . _pc && this . _pc . signalingState !== 'closed' ) {
415- this . _pc . close ( ) ;
411+ if ( ! this . _stopped ) {
412+ this . _stopped = true ;
413+ this . _signaling . sendSignalingMessage ( 'unpublish' , { id : this . _internalId } )
414+ . catch ( ( e ) => {
415+ Logger . warning ( 'MCU returns negative ack for unpublishing, ' + e ) ;
416+ } ) ;
417+ if ( this . _pc && this . _pc . signalingState !== 'closed' ) {
418+ this . _pc . close ( ) ;
419+ }
416420 }
417421 }
418422
419423 _unsubscribe ( ) {
420- this . _signaling . sendSignalingMessage ( 'unsubscribe' , {
421- id : this . _internalId ,
422- } )
423- . catch ( ( e ) => {
424- Logger . warning ( 'MCU returns negative ack for unsubscribing, ' + e ) ;
425- } ) ;
426- if ( this . _pc && this . _pc . signalingState !== 'closed' ) {
427- this . _pc . close ( ) ;
424+ if ( ! this . _stopped ) {
425+ this . _stopped = true ;
426+ this . _signaling . sendSignalingMessage ( 'unsubscribe' , {
427+ id : this . _internalId ,
428+ } )
429+ . catch ( ( e ) => {
430+ Logger . warning ( 'MCU returns negative ack for unsubscribing, ' + e ) ;
431+ } ) ;
432+ if ( this . _pc && this . _pc . signalingState !== 'closed' ) {
433+ this . _pc . close ( ) ;
434+ }
428435 }
429436 }
430437
@@ -528,10 +535,23 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
528535 if ( event . currentTarget . iceConnectionState === 'closed' ||
529536 event . currentTarget . iceConnectionState === 'failed' ) {
530537 if ( event . currentTarget . iceConnectionState === 'failed' ) {
531- this . _handleError ( 'ICE connection failed.' ) ;
538+ this . _handleError ( 'connection failed.' ) ;
539+ } else {
540+ // Fire ended event if publication or subscription exists.
541+ this . _fireEndedEventOnPublicationOrSubscription ( ) ;
542+ }
543+ }
544+ }
545+
546+ _onConnectionStateChange ( event ) {
547+ if ( this . _pc . connectionState === 'closed' ||
548+ this . _pc . connectionState === 'failed' ) {
549+ if ( this . _pc . connectionState === 'failed' ) {
550+ this . _handleError ( 'connection failed.' ) ;
551+ } else {
552+ // Fire ended event if publication or subscription exists.
553+ this . _fireEndedEventOnPublicationOrSubscription ( ) ;
532554 }
533- // Fire ended event if publication or subscription exists.
534- this . _fireEndedEventOnPublicationOrSubscription ( ) ;
535555 }
536556 }
537557
@@ -564,6 +584,9 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
564584 this . _pc . oniceconnectionstatechange = ( event ) => {
565585 this . _onIceConnectionStateChange . apply ( this , [ event ] ) ;
566586 } ;
587+ this . _pc . onconnectionstatechange = ( event ) => {
588+ this . _onConnectionStateChange . apply ( this , [ event ] ) ;
589+ } ;
567590 }
568591
569592 _getStats ( ) {
@@ -645,6 +668,8 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
645668 error : error ,
646669 } ) ;
647670 dispatcher . dispatchEvent ( errorEvent ) ;
671+ // Fire ended event when error occured
672+ this . _fireEndedEventOnPublicationOrSubscription ( ) ;
648673 }
649674
650675 _setCodecOrder ( sdp , options ) {
0 commit comments