@@ -18,7 +18,7 @@ JsSIP.Session = function(ua) {
18
18
] ;
19
19
20
20
this . ua = ua ;
21
- this . status = null ;
21
+ this . status = JsSIP . c . SESSION_NULL ;
22
22
this . dialog = null ;
23
23
this . earlyDialogs = [ ] ;
24
24
this . mediaSession = null ;
@@ -64,6 +64,7 @@ JsSIP.Session.prototype.init_incoming = function(request) {
64
64
this . from_tag = request . from_tag ;
65
65
this . status = JsSIP . c . SESSION_INVITE_RECEIVED ;
66
66
this . id = request . call_id + this . from_tag ;
67
+ this . request = request ;
67
68
68
69
//Save the session into the ua sessions collection.
69
70
this . ua . sessions [ this . id ] = this ;
@@ -84,7 +85,7 @@ JsSIP.Session.prototype.connect = function(target, options) {
84
85
}
85
86
86
87
// Check Session Status
87
- if ( this . status !== null ) {
88
+ if ( this . status !== JsSIP . c . SESSION_NULL ) {
88
89
throw new JsSIP . exceptions . InvalidStateError ( ) ;
89
90
}
90
91
@@ -145,23 +146,6 @@ JsSIP.Session.prototype.connect = function(target, options) {
145
146
//Save the session into the ua sessions collection.
146
147
this . ua . sessions [ this . id ] = this ;
147
148
148
- /**
149
- * @private
150
- */
151
- this . cancel = function ( ) {
152
- if ( this . status === JsSIP . c . SESSION_INVITE_SENT ) {
153
- if ( this . received_100 ) {
154
- request . cancel ( ) ;
155
- } else {
156
- this . isCanceled = true ;
157
- }
158
- } else if ( this . status === JsSIP . c . SESSION_1XX_RECEIVED ) {
159
- request . cancel ( ) ;
160
- }
161
-
162
- this . failed ( 'local' , null , JsSIP . c . causes . CANCELED ) ;
163
- } ;
164
-
165
149
this . newSession ( 'local' , request , target ) ;
166
150
this . connecting ( 'local' , request , target ) ;
167
151
this . sendInitialRequest ( mediaType ) ;
@@ -455,18 +439,6 @@ JsSIP.Session.prototype.receiveInitialRequest = function(ua, request) {
455
439
session . mediaSession . startCallee ( onMediaSuccess , onMediaFailure , onSdpFailure , offer ) ;
456
440
} ;
457
441
458
- /**
459
- * Reject the call
460
- * @private
461
- */
462
- this . reject = function ( ) {
463
- if ( this . status === JsSIP . c . SESSION_WAITING_FOR_ANSWER ) {
464
- request . reply ( 486 ) ;
465
-
466
- this . failed ( 'local' , null , JsSIP . c . causes . REJECTED ) ;
467
- }
468
- } ;
469
-
470
442
// Fire 'call' event callback
471
443
this . newSession ( 'remote' , request ) ;
472
444
@@ -498,7 +470,7 @@ JsSIP.Session.prototype.receiveResponse = function(response) {
498
470
// Proceed to cancelation if the user requested.
499
471
if ( this . isCanceled ) {
500
472
if ( response . status_code >= 100 && response . status_code < 200 ) {
501
- this . request . cancel ( ) ;
473
+ this . request . cancel ( this . cancelReason ) ;
502
474
} else if ( response . status_code >= 200 && response . status_code < 299 ) {
503
475
this . acceptAndTerminate ( response ) ;
504
476
}
@@ -885,6 +857,66 @@ JsSIP.Session.prototype.terminate = function() {
885
857
this . close ( ) ;
886
858
} ;
887
859
860
+ /**
861
+ * Reject the incoming call
862
+ * Only valid for incoming Messages
863
+ *
864
+ * @param {Number } status_code
865
+ * @param {String } [reason_phrase]
866
+ */
867
+ JsSIP . Session . prototype . reject = function ( status_code , reason_phrase ) {
868
+ // Check Session Direction and Status
869
+ if ( this . direction !== 'incoming' ) {
870
+ throw new JsSIP . exceptions . InvalidMethodError ( ) ;
871
+ } else if ( this . status !== JsSIP . c . SESSION_WAITING_FOR_ANSWER ) {
872
+ throw new JsSIP . exceptions . InvalidStateError ( ) ;
873
+ }
874
+
875
+ if ( status_code ) {
876
+ if ( ( status_code < 300 || status_code >= 700 ) ) {
877
+ throw new JsSIP . exceptions . InvalidValueError ( ) ;
878
+ } else {
879
+ this . request . reply ( status_code , reason_phrase ) ;
880
+ }
881
+ } else {
882
+ this . request . reply ( 480 ) ;
883
+ }
884
+
885
+ this . failed ( 'local' , null , JsSIP . c . causes . REJECTED ) ;
886
+ } ;
887
+
888
+ /**
889
+ * Cancel the outgoing call
890
+ *
891
+ * @param {String } [reason]
892
+ */
893
+ JsSIP . Session . prototype . cancel = function ( reason ) {
894
+ // Check Session Direction
895
+ if ( this . direction !== 'outgoing' ) {
896
+ throw new JsSIP . exceptions . InvalidMethodError ( ) ;
897
+ }
898
+
899
+ // Check Session Status
900
+ if ( this . status === JsSIP . c . SESSION_NULL ) {
901
+ this . isCanceled = true ;
902
+ this . cancelReason = reason ;
903
+ } else if ( this . status === JsSIP . c . SESSION_INVITE_SENT ) {
904
+ if ( this . received_100 ) {
905
+ this . request . cancel ( reason ) ;
906
+ } else {
907
+ this . isCanceled = true ;
908
+ this . cancelReason = reason ;
909
+ }
910
+ } else if ( this . status === JsSIP . c . SESSION_1XX_RECEIVED ) {
911
+ this . request . cancel ( reason ) ;
912
+ } else {
913
+ throw new JsSIP . exceptions . InvalidStateError ( ) ;
914
+ }
915
+
916
+ this . failed ( 'local' , null , JsSIP . c . causes . CANCELED ) ;
917
+ } ;
918
+
919
+
888
920
889
921
/**
890
922
* Initial Request Sender
@@ -899,7 +931,7 @@ JsSIP.Session.prototype.sendInitialRequest = function(mediaType) {
899
931
request_sender = new JsSIP . RequestSender ( self , this . ua ) ;
900
932
901
933
function onMediaSuccess ( ) {
902
- if ( self . status === JsSIP . c . SESSION_TERMINATED ) {
934
+ if ( self . isCanceled || self . status === JsSIP . c . SESSION_TERMINATED ) {
903
935
self . mediaSession . close ( ) ;
904
936
return ;
905
937
}
0 commit comments