Re #1587:
 - Don't answer call replace request with 200 if the replaced call hasn't reach confirmed state and has UAS role.
 - Only apply accept_replace_in_early_state when call is in early state.



git-svn-id: https://svn.pjsip.org/repos/pjproject/trunk@4268 74dad513-b988-da41-8d7b-12977e46ad98
diff --git a/pjsip/src/pjsip-ua/sip_replaces.c b/pjsip/src/pjsip-ua/sip_replaces.c
index 3bda530..3879376 100644
--- a/pjsip/src/pjsip-ua/sip_replaces.c
+++ b/pjsip/src/pjsip-ua/sip_replaces.c
@@ -305,12 +305,19 @@
      * initiated by this UA, it returns a 481 (Call/Transaction Does Not
      * Exist) response to the new INVITE.
      */
-    if (inv->state <= PJSIP_INV_STATE_EARLY && inv->role != PJSIP_ROLE_UAC &&
-	pjsip_cfg()->endpt.accept_replace_in_early_state == PJ_FALSE)
+    if (inv->state <= PJSIP_INV_STATE_EARLY && inv->role != PJSIP_ROLE_UAC)
     {
-	code = PJSIP_SC_CALL_TSX_DOES_NOT_EXIST;
-	warn_text = "Found early INVITE session but not initiated by this UA";
-	goto on_return;
+	/* Really return 481 only if call haven't reached early state or
+	 * accept-replace-in-early-state (ticket #1587) is not allowed.
+	 */
+	if (inv->state != PJSIP_INV_STATE_EARLY ||
+	    pjsip_cfg()->endpt.accept_replace_in_early_state == PJ_FALSE)
+	{
+	    code = PJSIP_SC_CALL_TSX_DOES_NOT_EXIST;
+	    warn_text = "Found early INVITE session but not initiated by "
+			"this UA";
+	    goto on_return;
+	}
     }
 
 
diff --git a/pjsip/src/pjsua-lib/pjsua_call.c b/pjsip/src/pjsua-lib/pjsua_call.c
index a92874b..c374f0d 100644
--- a/pjsip/src/pjsua-lib/pjsua_call.c
+++ b/pjsip/src/pjsua-lib/pjsua_call.c
@@ -825,11 +825,20 @@
 	pjsua_var.ua_cfg.cb.on_call_replaced(replaced_call->index,
 					     call->index);
 
-    PJ_LOG(4,(THIS_FILE, "Answering replacement call %d with 200/OK",
-			 call->index));
+    if (replaced_call->inv->state <= PJSIP_INV_STATE_EARLY &&
+	replaced_call->inv->role != PJSIP_ROLE_UAC)
+    {
+	/* Replaced call is not in confirmed state yet and we are not
+	 * the call initiator, should not answer with 200 response here.
+	 */
+    } else {
+    	PJ_LOG(4,(THIS_FILE, "Answering replacement call %d with 200/OK",
+			     call->index));
 
-    /* Answer the new call with 200 response */
-    status = pjsip_inv_answer(call->inv, 200, NULL, NULL, &tdata);
+	/* Answer the new call with 200 response */
+	status = pjsip_inv_answer(call->inv, 200, NULL, NULL, &tdata);
+    }
+
     if (status == PJ_SUCCESS)
 	status = pjsip_inv_send_msg(call->inv, tdata);