# RTPProxy control
route[ENC_SRTP] {
	add_rr_param(";rm=1");
	$avp(rtpproxy_offer_flags) = "replace-origin replace-session-connection ICE=force SRTP AVP";
	$avp(rtpproxy_answer_flags) = "replace-origin replace-session-connection ICE=remove RTP AVP";
}

route[DEC_SRTP] {
	add_rr_param(";rm=2");
	$avp(rtpproxy_offer_flags) = "replace-origin replace-session-connection ICE=remove RTP AVP";
	$avp(rtpproxy_answer_flags) = "replace-origin replace-session-connection ICE=force SRTP AVP";
}

route[ENC_WS_RTP] {
	add_rr_param(";rm=3");
	$avp(rtpproxy_offer_flags) = "replace-origin replace-session-connection ICE=force RTP AVPF";
	$avp(rtpproxy_answer_flags) = "replace-origin replace-session-connection ICE=remove RTP AVP";
}

route[DEC_WS_RTP] {
	add_rr_param(";rm=4");
	$avp(rtpproxy_offer_flags) = "replace-origin replace-session-connection ICE=remove RTP AVP";
	$avp(rtpproxy_answer_flags) = "replace-origin replace-session-connection ICE=force RTP AVPF";
}

route[ENC_WSS_RTP] {
	add_rr_param(";rm=5");
	$avp(rtpproxy_offer_flags) = "replace-origin replace-session-connection ICE=force SRTP AVPF DTLS=passive";
	$avp(rtpproxy_answer_flags) = "replace-origin replace-session-connection ICE=remove RTP AVP";
}

route[DEC_WSS_RTP] {
	add_rr_param(";rm=6");
	$avp(rtpproxy_offer_flags) = "replace-origin replace-session-connection ICE=remove RTP AVP";
	$avp(rtpproxy_answer_flags) = "replace-origin replace-session-connection ICE=force SRTP AVPF DTLS=passive";
}

route[ENC_RTP] {
	add_rr_param(";rm=7");
	$avp(rtpproxy_offer_flags) = "replace-origin replace-session-connection ICE=force RTP AVP";
	$avp(rtpproxy_answer_flags) = "replace-origin replace-session-connection ICE=remove RTP AVP";
}

route[DEC_RTP] {
	add_rr_param(";rm=8");
	$avp(rtpproxy_offer_flags) = "replace-origin replace-session-connection ICE=remove RTP AVP";
	$avp(rtpproxy_answer_flags) = "replace-origin replace-session-connection ICE=force RTP AVP";
}

# RTPProxy control
route[NATMANAGE] {
#!ifdef WITH_DEBUG
	if (is_request())
		xlog("REQUEST: $rm $ru ($si:$sp, $ci)\n");
	else
		xlog("REPLY: $rs $rr ($rm, $si:$sp, $ci)\n");

	if (is_direction("downstream"))
		xlog("    downstream\n");
	else
		xlog("    upstream\n");

	xlog("    Offer: $avp(rtpproxy_offer_flags)\n");
	xlog("    Answer: $avp(rtpproxy_answer_flags)\n");
	if (isflagset(FLT_MOBILE_ORIG)) {
		xlog("    mo\n");
	} else {
		xlog("    mt\n");
	}
#!endif
	if ((is_reply() && ($T_req($tt) != $null)) || (is_request() && has_totag())) {
		xlog("L_DBG", "Request had ToTag.");
		#if((is_request() && !check_route_param("rm=")) || (is_reply() && !isflagset(FLT_RTP))) {
		if(!check_route_param("rm=") && !isflagset(FLT_RTP)) {
			xlog("L_DBG", "No RM Param\n");
			return;
		}
		if (is_request()) {
			if (isflagset(FLT_MOBILE_ORIG) && is_direction("downstream")) {
				xlog("L_DBG", "1) add_contact_alias();");
				add_contact_alias();
			} else if (!isflagset(FLT_MOBILE_ORIG) && is_direction("upstream")) {
				xlog("L_DBG", "2) add_contact_alias();");
				add_contact_alias();
			}
		} else {
			if (!isflagset(FLT_MOBILE_ORIG) && is_direction("downstream")) {
				xlog("L_DBG", "1) ADD_contact_alias();");
				add_contact_alias();
			} else if (isflagset(FLT_MOBILE_ORIG) && is_direction("downstream")) {
				xlog("L_DBG", "2) ADD_contact_alias();");
				add_contact_alias();
			}
		}
	} else {
		if (is_reply() && !isflagset(FLT_MOBILE_ORIG)) {
			xlog("L_DBG", "3) ADD_contact_alias();");
			add_contact_alias();
		}
	}

	if (isflagset(FLT_MOBILE_ORIG)) {
		$avp(setid) = 1;
		$avp(extra_id) = "mo";
	} else {
		$avp(setid) = 2;
		$avp(extra_id) = "mt";
	}

	if(!t_is_set("onreply_route")) t_on_reply("NAT_REPLY");
	if(!t_is_set("failure_route")) t_on_failure("NATMANAGE");

	if (is_method("BYE") || t_is_failure_route()) {
		rtpengine_manage();
		return;
	}

	setflag(FLT_RTP);

	if (!has_body("application/sdp"))
		return;

#!ifdef REMOVE_BITALIGNED_AMR
	route(REMOVE_BITALIGNED);
#!endif

#!ifndef FORCE_RTPRELAY
	if (!isflagset(FLT_NAT) || !check_route_param("rm="))
		return;
#!endif

	if ((is_reply() && ($T_req($tt) != $null)) || (is_request() && has_totag())) {
		rtpengine_manage();
	} else {
		if ($avp(rtpproxy_offer_flags) == $null)
			return;
		if ($avp(rtpproxy_answer_flags) == $null)
			return;

		if (is_request()) {
			rtpengine_manage($avp(rtpproxy_offer_flags));		
		} else {
			rtpengine_manage($avp(rtpproxy_answer_flags));
		}
	}
}

# manage incoming replies
onreply_route[NAT_REPLY] {
	# In case of 1xx and 2xx do NAT
	if(status=~"[12][0-9][0-9]")
		route(NATMANAGE);
}

route[REMOVE_BITALIGNED] {
	if (sdp_get_line_startswith("$avp(mline)", "m=")) {
		# xlog("m-line: $avp(mline)\n");
		sdp_get("$avp(sdp)");
		# xlog("\n$avp(sdp)\n");
		$var(x) = -1;
		$var(remove) = "";
		$var(codec) = $(avp(mline){s.select,$var(x), });
		# xlog("$$var(codec) => $var(codec)\n");
		while ($(var(codec){s.int}) > 0) {
			# xlog("$var(x)) $$var(codec) => $var(codec)\n");
			$var(s) = "a=fmtp:"+$var(codec);
			# xlog("$$var(s) => $var(s)\n");
			$var(fmtp) = $(avp(sdp){line.sw,$var(s)});
			# xlog("$$var(fmtp) => $var(fmtp)\n");
			if ($var(fmtp) =~ "a=fmtp:"+$var(codec)+" mode-change-capability.*") {
				# xlog("Match: $var(codec)\n");
				if ($var(remove) == "") {
					$var(remove) = $var(codec);
				} else {
					$var(remove) = $var(remove)+","+$var(codec);
				}
			}
			$var(codec) = $(avp(mline){s.select,$var(x), });
			$var(x) = $var(x) - 1;
		}
		# xlog("$$var(remove) => $var(remove)\n");
		if ($var(remove) != "") {
			sdp_remove_codecs_by_id($var(remove), "audio");
			msg_apply_changes();
		}
	}
}

