@@ -527,12 +527,12 @@ export abstract class Session extends EventEmitter {
527
527
* @internal
528
528
*/
529
529
public close ( ) : void {
530
+ this . logger . log ( `Session[${ this . id } ].close` ) ;
531
+
530
532
if ( this . status === SessionStatus . STATUS_TERMINATED ) {
531
533
return ;
532
534
}
533
535
534
- this . logger . log ( "closing INVITE session " + this . id ) ;
535
-
536
536
// 1st Step. Terminate media.
537
537
if ( this . _sessionDescriptionHandler ) {
538
538
this . _sessionDescriptionHandler . close ( ) ;
@@ -564,7 +564,7 @@ export abstract class Session extends EventEmitter {
564
564
* they occur we ACK the response and send a BYE.
565
565
* Note that the BYE is sent in the dialog associated with the response
566
566
* which is not necessarily `this.dialog`. And, accordingly, the
567
- * session state is not transitioned to terminated.
567
+ * session state is not transitioned to terminated and session is not closed .
568
568
* @param inviteResponse - The response causing the error.
569
569
* @param statusCode - Status code for he reason phrase.
570
570
* @param reasonPhrase - Reason phrase for the BYE.
@@ -610,51 +610,76 @@ export abstract class Session extends EventEmitter {
610
610
throw new Error ( "Dialog undefined." ) ;
611
611
}
612
612
613
- // If the ACK doesn't have an offer/answer, nothing to be done.
614
- const body = getBody ( request . message ) ;
615
- if ( ! body ) {
616
- this . status = SessionStatus . STATUS_CONFIRMED ;
617
- return ;
618
- }
619
- if ( body . contentDisposition === "render" ) {
620
- this . renderbody = body . content ;
621
- this . rendertype = body . contentType ;
622
- this . status = SessionStatus . STATUS_CONFIRMED ;
623
- return ;
624
- }
625
- if ( body . contentDisposition !== "session" ) {
626
- this . status = SessionStatus . STATUS_CONFIRMED ;
627
- return ;
628
- }
629
-
630
- const options = {
631
- sessionDescriptionHandlerOptions : this . sessionDescriptionHandlerOptions ,
632
- sessionDescriptionHandlerModifiers : this . sessionDescriptionHandlerModifiers
633
- } ;
634
-
635
613
switch ( dialog . signalingState ) {
636
- case SignalingState . Initial :
614
+ case SignalingState . Initial : {
637
615
// State should never be reached as first reliable response must have answer/offer.
638
- throw new Error ( `Invalid signaling state ${ dialog . signalingState } .` ) ;
639
- case SignalingState . Stable :
640
- // Receved answer.
616
+ // So we must have never has sent an offer.
617
+ this . logger . error ( `Invalid signaling state ${ dialog . signalingState } .` ) ;
618
+ this . isFailed = true ;
619
+ const extraHeaders = [ "Reason: " + Utils . getReasonHeaderValue ( 488 , "Bad Media Description" ) ] ;
620
+ dialog . bye ( undefined , { extraHeaders } ) ;
621
+ this . stateTransition ( SessionState . Terminated ) ;
622
+ this . close ( ) ;
623
+ return ;
624
+ }
625
+ case SignalingState . Stable : {
626
+ // State we should be in.
627
+ // Either the ACK has the answer that got us here, or we were in this state prior to the ACK.
628
+ const body = getBody ( request . message ) ;
629
+ // If the ACK doesn't have an answer, nothing to be done.
630
+ if ( ! body ) {
631
+ this . status = SessionStatus . STATUS_CONFIRMED ;
632
+ return ;
633
+ }
634
+ if ( body . contentDisposition === "render" ) {
635
+ this . renderbody = body . content ;
636
+ this . rendertype = body . contentType ;
637
+ this . status = SessionStatus . STATUS_CONFIRMED ;
638
+ return ;
639
+ }
640
+ if ( body . contentDisposition !== "session" ) {
641
+ this . status = SessionStatus . STATUS_CONFIRMED ;
642
+ return ;
643
+ }
644
+ // Receved answer in ACK.
645
+ const options = {
646
+ sessionDescriptionHandlerOptions : this . sessionDescriptionHandlerOptions ,
647
+ sessionDescriptionHandlerModifiers : this . sessionDescriptionHandlerModifiers
648
+ } ;
641
649
this . setAnswer ( body , options )
642
650
. then ( ( ) => { this . status = SessionStatus . STATUS_CONFIRMED ; } )
643
- . catch ( ( error : any ) => {
644
- // FIXME: TODO - need to do something to handle this error
645
- this . logger . error ( error ) ;
651
+ . catch ( ( error : Error ) => {
652
+ this . logger . error ( error . message ) ;
653
+ this . isFailed = true ;
646
654
const extraHeaders = [ "Reason: " + Utils . getReasonHeaderValue ( 488 , "Bad Media Description" ) ] ;
647
- this . bye ( undefined , { extraHeaders } ) ;
655
+ dialog . bye ( undefined , { extraHeaders } ) ;
656
+ this . stateTransition ( SessionState . Terminated ) ;
648
657
this . close ( ) ;
649
- throw error ;
650
658
} ) ;
651
659
return ;
652
- case SignalingState . HaveLocalOffer :
653
- // State should never be reached as local offer would be answered by this ACK
654
- throw new Error ( `Invalid signaling state ${ dialog . signalingState } .` ) ;
655
- case SignalingState . HaveRemoteOffer :
660
+ }
661
+ case SignalingState . HaveLocalOffer : {
662
+ // State should never be reached as local offer would be answered by this ACK.
663
+ // So we must have received an ACK without an answer.
664
+ this . logger . error ( `Invalid signaling state ${ dialog . signalingState } .` ) ;
665
+ this . isFailed = true ;
666
+ const extraHeaders = [ "Reason: " + Utils . getReasonHeaderValue ( 488 , "Bad Media Description" ) ] ;
667
+ dialog . bye ( undefined , { extraHeaders } ) ;
668
+ this . stateTransition ( SessionState . Terminated ) ;
669
+ this . close ( ) ;
670
+ return ;
671
+ }
672
+ case SignalingState . HaveRemoteOffer : {
656
673
// State should never be reached as remote offer would be answered in first reliable response.
657
- throw new Error ( `Invalid signaling state ${ dialog . signalingState } .` ) ;
674
+ // So we must have never has sent an answer.
675
+ this . logger . error ( `Invalid signaling state ${ dialog . signalingState } .` ) ;
676
+ this . isFailed = true ;
677
+ const extraHeaders = [ "Reason: " + Utils . getReasonHeaderValue ( 488 , "Bad Media Description" ) ] ;
678
+ dialog . bye ( undefined , { extraHeaders } ) ;
679
+ this . stateTransition ( SessionState . Terminated ) ;
680
+ this . close ( ) ;
681
+ return ;
682
+ }
658
683
case SignalingState . Closed :
659
684
throw new Error ( `Invalid signaling state ${ dialog . signalingState } .` ) ;
660
685
default :
0 commit comments