<!-- Module User's Guide -->

<chapter>
	
	<title>&adminguide;</title>
	
	<section id="overview" xreflabel="Overview">
	<title>Overview</title>
	<para>
		This module is used by &osips; to communicate with RTPProxy, a media
		relay proxy used to make the communication between user agents behind
		NAT possible.
	</para>
	<para>
		This module is also used along with RTPProxy to record media streams
		between user agents or to play media to either &ua;c or &ua;s.
	</para>
	</section>


	<section>
	<title>Multiple RTPProxy usage</title>
	<para>
		Currently, the rtpproxy module can support multiple rtpproxies for
		balancing/distribution and control/selection purposes.
	</para>
	<para>
		The module allows the definition of several sets of rtpproxies - 
		load-balancing will be performed over a set and the user has the
		ability to choose what set should be used. The set is selected via
		its id - the id being defined along with the set. Refer to the 
		<quote>rtpproxy_sock</quote> module parameter definition for syntax
		description.
	</para>
	<para>
		The balancing inside a set is done automatically by the module based on
		the weight of each rtpproxy from the set. Note that if rtpproxy has weight
		0, it will be used only when no other rtpproxies  (with a different
		weight value than 0) respond. Default weight is 1.
	</para>
	<para>
		Starting with &osips; 2.1, engage_rtp_proxy(), unforce_rtp_proxy()
		and start_recording() functions have been fully replaced by
		rtpproxy_engage(), rtpproxy_unforce() and rtpproxy_start_recording().
	</para>
	<para>
		IMPORTANT: if you use multiple sets, make sure you use the same set for
		both rtpproxy_offer()/rtpproxy_answer() and rtpproxy_unforce()!!
	</para>
	</section>

	<section>
	<title>RTPProxy timeout notifications</title>
	<para>
		Nathelper module can also receive timeout notifications from multiple
		rtpproxies. RTPProxy can be configured to send notifications when
		a session doesn't receive any media for a configurable interval of
		time. The rtpproxy modules has implemented a listener for such
		notifications and when received it terminates the dialog at SIP
		level (send BYE to both ends), with the help of dialog module.
	</para>
	<para>
		In our tests with RTPProxy we observed some limitations and also
		provide a patch for it against git commit
		<quote>600c80493793bafd2d69427bc22fcb43faad98c5</quote>.
		It contains an addition and implements separate timeout parameters
		for the phases of session establishment and ongoing sessions.
		In the official code a single timeout parameter controls
		both session establishment and rtp timeout and the timeout
		notification is also sent in the call establishment phase.
		This is a problem since we want to detect rtp timeout fast, but also
		allow a longer period for call establishment.
	</para>
	<para>
		Note that RTPProxy version
		<ulink url='http://www.rtpproxy.org/post/v2release/'>v2.0.0</ulink>
		has integrated this feature upstream, therefore this patch is no
		longer needed.
	</para>
	<para>
		To enable timeout notification there are several steps that you must follow:
		<para>
			Start &osips; timeout detection by setting the <quote>rtpp_notify_socket</quote>
			module parameter in your configuration script. This is the socket where further
			notification will be received from rtpproxies. This socket must be a TCP or 
			UNIX socket. Also, for all the calls that require notification, the
			rtpproxy_engage(), rtpproxy_offer() and rtpproxy_answer() functions must
			be called with the <quote>n</quote> flag.
		</para>
		<para>
			Configure RTPProxy to use timeout notification by adding
			the following command line parameters:
			<itemizedlist>
				<listitem>
					<para><quote> -n timeout_socket</quote> - specifies
						where the notifications will be sent. This socket
						must be the same as <quote>rtpp_notify_socket</quote>
						&osips; module parameter. This parameter is mandatory.
					</para>
				</listitem>
				<listitem>
					<para><quote> -T ttl</quote> - limits the rtp session
						timeout to <quote>ttl</quote>. This parameter
						is optional and the default value is 60 seconds.
					</para>
				</listitem>
				<listitem>
					<para><quote> -W ttl</quote> - limits the session
						establishment timeout to <quote>ttl</quote>.
						This parameter is optional and the default value 
						is 60 seconds.
					</para>
				</listitem>
			</itemizedlist>
		</para>
			<para>
				All of the previous parameters can be used with the offical
				RTPProxy release, except for the last one. It has been
				added, together with other modifications to RTPProxy in order
				to work properly. The patch is located in the
				<emphasis>patches</emphasis> directory in the module.
			</para>
			<para>
				To get the patched version from git you must follow theese steps:
				<itemizedlist>
					<listitem>
						<para>
							Get the latest source code: <quote>git clone git://sippy.git.sourceforge.net/gitroot/sippy/rtpproxy</quote>
						</para>
					</listitem>
					<listitem>
						<para>
							Make a branch from the commit: <quote>git checkout
								-b branch_name 600c80493793bafd2d69427bc22fcb43faad98c5</quote>
						</para>
					</listitem>
					<listitem>
						<para>
							Patch RTPProxy: <quote>patch &lt;
								path_to_rtpproxy_patch</quote>
						</para>
					</listitem>
				</itemizedlist>
			</para>
			<para>
				The patched version can also be found at:
				https://opensips.org/pub/rtpproxy/
			</para>
	</para>
	</section>


	<section id="dependencies" xreflabel="Dependencies">
	<title>Dependencies</title>
	<section>
		<title>&osips; Modules</title>
		<para>
		The following modules must be loaded before this module:
			<itemizedlist>
			<listitem>
			<para>
				<emphasis>a database</emphasis> module - only if you want
				to load use a database table from where to load the rtp proxies
				sets.
			</para>
			</listitem>

			<listitem>
			<para>
				<emphasis>dialog</emphasis> module - if using the rtpproxy_engage
				functions or RTPProxy timeout notifications.
			</para>
			</listitem>

			</itemizedlist>
		</para>
	</section>
	<section>
		<title>External Libraries or Applications</title>
		<para>
		The following libraries or applications must be installed before 
		running &osips; with this module loaded:
			<itemizedlist>
			<listitem>
			<para>
				<emphasis>None</emphasis>.
			</para>
			</listitem>
		</itemizedlist>
		</para>
	</section>
	</section>

	<section id="exported_parameters" xreflabel="Exported Parameters">
	<title>Exported Parameters</title>
	<section id="param_rtpproxy_sock" xreflabel="rtpproxy_sock">
		<title><varname>rtpproxy_sock</varname> (string)</title>
		<para>
		Definition of socket(s) used to connect to (a set) RTPProxy. It may 
		specify a UNIX socket, an IPv4/IPv6 UDP socket or an IPv4/IPv6 TCP socket.
		If the protocol part (i.e. <quote>udp:</quote>) is missing, the socket is
		treated as a UNIX socket.
		</para>
		<para>
			The definition also supports to specify a different IP that will
			be advertised instead of the one returned by RTPProxy. This is
			useful when having multiple RTPProxy servers that are located
			behind NAT, and listen only on private intefaces, but need to
			advertise a public one.
		</para>
		<para>
		<emphasis>
			Default value is <quote>NONE</quote> (disabled).
		</emphasis>
		</para>
		<example>
		<title>Set <varname>rtpproxy_sock</varname> parameter</title>
		<programlisting format="linespecific">
...
# single rtpproxy with specific weight
modparam("rtpproxy", "rtpproxy_sock", "udp:localhost:22222=2")

# single rtpproxy with advertised address + weight
modparam("rtpproxy", "rtpproxy_sock", "udp:localhost:22222|8.8.8.8=2")

# multiple rtproxies for LB
modparam("rtpproxy", "rtpproxy_sock",
	"udp:localhost:22222 udp:localhost:22223 tcp:remote1:33422 tcp6:remote2:32322")

# multiple sets of multiple rtproxies
modparam("rtpproxy", "rtpproxy_sock", "1 == udp:localhost:22222 udp:localhost:22223")
modparam("rtpproxy", "rtpproxy_sock", "2 == udp:localhost:22223")
modparam("rtpproxy", "rtpproxy_sock", "2 == udp:localhost:22223|8.8.8.8")
...
</programlisting>
		</example>
	</section>
	<section id="param_rtpproxy_disable_tout" xreflabel="rtpproxy_disable_tout">
		<title><varname>rtpproxy_disable_tout</varname> (integer)</title>
		<para>
		Once RTPProxy was found unreachable and marked as disable, rtpproxy
		will not attempt to establish communication to RTPProxy for 
		rtpproxy_disable_tout seconds.
		</para>
		<para>
		<emphasis>
			Default value is <quote>60</quote>.
		</emphasis>
		</para>
		<example>
		<title>Set <varname>rtpproxy_disable_tout</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("rtpproxy", "rtpproxy_disable_tout", 20)
...
</programlisting>
		</example>
	</section>
	<section id="param_rtpproxy_timeout" xreflabel="rtpproxy_timeout">
		<title><varname>rtpproxy_timeout</varname> (string)</title>
		<para>
		Timeout value in waiting for reply from RTPProxy.
		</para>
		<para>
		<emphasis>
			Default value is <quote>1</quote>.
		</emphasis>
		</para>
		<example>
		<title>Set <varname>rtpproxy_timeout</varname> parameter to 200ms</title>
		<programlisting format="linespecific">
...
modparam("rtpproxy", "rtpproxy_timeout", "0.2")
...
</programlisting>
		</example>
	</section>
	<section id="param_rtpproxy_autobridge" xreflabel="rtpproxy_autobridge">
		<title><varname>rtpproxy_autobridge</varname> (integer)</title>
		<para>
		Enable auto-bridging feature. Does not properly function when doing serial/parallel forking!
		</para>
		<para>
		<emphasis>
			Default value is <quote>0</quote>.
		</emphasis>
		</para>
		<example>
		<title>Enable auto-bridging feature</title>
		<programlisting format="linespecific">
...
modparam("rtpproxy", "rtpproxy_autobridge", 1)
...
</programlisting>
		</example>
	</section>
	<section id="param_rtpproxy_retr" xreflabel="rtpproxy_retr">
		<title><varname>rtpproxy_retr</varname> (integer)</title>
		<para>
		How many times rtpproxy should retry to send and receive after
		timeout was generated.
		</para>
		<para>
		<emphasis>
			Default value is <quote>5</quote>.
		</emphasis>
		</para>
		<example>
		<title>Set <varname>rtpproxy_retr</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("rtpproxy", "rtpproxy_retr", 2)
...
</programlisting>
		</example>
	</section>

	<section id="param_default_set" xreflabel="default_set">
		<title><varname>default_set</varname> (integer)</title>
		<para>
			The parameter indicates the default RTPProxy set to be used when
			provisioning an engine in the config file without an explicit set,
			or when calling one of the <emphasis>rtpproxy_*()</emphasis>
			functions without an explicit set.
		</para>
		<para>
		<emphasis>
			Default value is set <quote>0</quote>.
		</emphasis>
		</para>
		<example>
		<title>Set <varname>default_set</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("rtpproxy", "default_set", 1)
...
</programlisting>
		</example>
	</section>
	<section id="param_nortpproxy_str" xreflabel="nortpproxy_str">
		<title><varname>nortpproxy_str</varname> (string)</title>
		<para>
		The parameter sets the SDP attribute used by rtpproxy to mark
		the packet SDP informations have already been mangled.
		</para>
		<para>
		If empty string, no marker will be added or checked.
		</para>
		<note><para>
		The string must be a complete SDP line, including the EOH (\r\n).
		</para></note>
		<para>
		<emphasis>
			Default value is <quote>a=nortpproxy:yes\r\n</quote>.
		</emphasis>
		</para>
		<example>
		<title>Set <varname>nortpproxy_str</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("rtpproxy", "nortpproxy_str", "a=sdpmangled:yes\r\n")
...
</programlisting>
		</example>
	</section>

	<section id="param_db_url" xreflabel="db_url">
		<title><varname>db_url</varname> (string)</title>
		<para>
			The database url. This parameter should be set if you want to 
			use a database table from where to load or reload definitions of
			socket(s) used to connect to (a set) RTPProxy. The record from
			the database table will be read at start up (added to the ones
			defined with the rtpproxy_sock module parameter) and when the MI command
			rtpproxy_reload is issued(the definitions will be replaced with the
			ones from the database table).
		</para>
		<para>
		<emphasis>
			Default value is <quote>NULL</quote>.
		</emphasis>
		</para>

		<example>
		<title>Set <varname>db_url</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("rtpproxy", "db_url", 
		"mysql://opensips:opensipsrw@192.168.2.132/opensips")
...

</programlisting>
		</example>
	</section>

	<section id="param_db_table" xreflabel="db_table">
		<title><varname>db_table</varname> (string)</title>
		<para>
			The name of the database table containing definitions of
			socket(s) used to connect to (a set) RTPProxy.
		</para>
		<para>
		<emphasis>
			Default value is <quote>rtpproxy_sockets</quote>.
		</emphasis>
		</para>

		<example>
		<title>Set <varname>db_table</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("rtpproxy", "db_table", "nh_sockets") 
...

</programlisting>
		</example>
	</section>

	<section id="param_rtpp_socket_col" xreflabel="rtpp_socket_col">
		<title><varname>rtpp_socket_col</varname> (string)</title>
		<para>
			The name rtpp socket column in the database table.
		</para>
		<para>
		<emphasis>
			Default value is <quote>rtpproxy_sock</quote>.
		</emphasis>
		</para>

		<example>
		<title>Set <varname>rtpp_socket_col</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("rtpproxy", "rtpp_socket_col", "rtpp_socket") 
...

</programlisting>
		</example>
	</section>

	<section id="param_set_id_col" xreflabel="set_id_col">
		<title><varname>set_id_col</varname> (string)</title>
		<para>
			The name set id column in the database table.
		</para>
		<para>
		<emphasis>
			Default value is <quote>set_id</quote>.
		</emphasis>
		</para>

		<example>
		<title>Set <varname>set_id</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("rtpproxy", "set_id_col", "rtpp_set_id") 
...

</programlisting>
		</example>
	</section>

	<section id="param_rtpp_notify_socket" xreflabel="rtpp_notify_socket">
		<title><varname>rtpp_notify_socket</varname> (string)</title>
		<para>
			The socket &osips; listens for notifications from RTPProxy.
			Currently &osips; can receive RTP timeout and DTMF events.
		</para>
		<para>
		<emphasis>
			Default value is <quote>NULL</quote> - no notifications are received.
		</emphasis>
		</para>

		<example>
		<title>Set <varname>rtpp_notify_socket</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("rtpproxy", "rtpp_notify_socket", "tcp:10.10.10.10:9999")

# use an UNIX socket
modparam("rtpproxy", "rtpp_notify_socket", "unix:/tmp/rtpproxy.unix")
# or
modparam("rtpproxy", "rtpp_notify_socket", "/tmp/rtpproxy.unix")
...

</programlisting>
		</example>
	</section>

	<section id="param_generated_sdp_port_min"
			xreflabel="generated_sdp_port_min">
		<title><varname>generated_sdp_port_min</varname> (integer)</title>
		<para>
			When RTPProxy module needs to generate an SDP body,
			use this value as the minimum value of the port.
		</para>

		<para>
		<emphasis>Default value is <quote>35000</quote>.</emphasis>
		</para>
		<example>
		<title>Set <varname>generated_sdp_port_min</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("rtpproxy", "generated_sdp_port_min", 10000)
...
		</programlisting>
		</example>
	</section>

	<section id="param_generated_sdp_port_max"
			xreflabel="generated_sdp_port_max">
		<title><varname>generated_sdp_port_max</varname> (integer)</title>
		<para>
			When RTPProxy module needs to generate an SDP body,
			use this value as the maximum value of the port.
		</para>

		<para>
		<emphasis>Default value is <quote>65000</quote>.</emphasis>
		</para>
		<example>
		<title>Set <varname>generated_sdp_port_max</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("rtpproxy", "generated_sdp_port_max", 30000)
...
		</programlisting>
		</example>
	</section>

	<section id="param_generated_sdp_media_ip"
			xreflabel="generated_sdp_media_ip">
		<title><varname>generated_sdp_media_ip</varname> (string)</title>
		<para>
			When RTPProxy module needs to generate an SDP body,
			use this value as the media_ip in the <emphasis>c=</emphasis>
			and the <emphasis>o=</emphasis>.
		</para>

		<para>
		<emphasis>Default value is <quote>127.0.0.1</quote>.</emphasis>
		</para>
		<example>
		<title>Set <varname>generated_sdp_media_ip</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("rtpproxy", "generated_sdp_media_ip", "10.0.0.1")
...
		</programlisting>
		</example>
	</section>

</section>


	<section id="exported_functions" xreflabel="exported_functions">
	<title>Exported Functions</title>
	<section id="func_rtpproxy_engage" xreflabel="rtpproxy_engage()">
		<title>
			<function moreinfo="none">rtpproxy_engage([[flags][, [ip_address][, [set_id][, [sock_var][, ret_var]]]]])</function>
		</title>

		<para>
		Rewrites &sdp; body to ensure that media is passed through
		an &rtp; proxy. It uses the dialog module facilities to keep track
		when the rtpproxy session must be updated. Function must only be
		called for the initial INVITE
		and internally takes care of rewriting the body of 200 OKs and ACKs.
		Note that when used in bridge mode, this function might advertise wrong
		interfaces in &sdp; (due to the fact that &osips; is not aware of the RTPProxy
		configuration), so you might face an undefined behavior.
		</para>
		<para>Meaning of the parameters is as follows:</para>
		<itemizedlist>
		<listitem>
			<para>
			<emphasis>flags(string, optional)</emphasis> - flags to turn on some features.
			</para>
			<itemizedlist>
				<listitem><para>
				<emphasis>a</emphasis> - flags that UA from which message is
				received doesn't support symmetric RTP.
				</para></listitem>
				<listitem><para>
				<emphasis>l</emphasis> - force <quote>lookup</quote>, that is,
				only rewrite SDP when corresponding session is already exists 
				in the RTP proxy. By default is on when the session is to be
				completed (reply in non-swap or ACK in swap mode).
				</para></listitem>
				<listitem><para>
				<emphasis>k</emphasis> - only create RTPProxy session, but do
				not modify the SDP body. This is useful when you only want to
				inject some media, but do not want to engage RTPProxy in the
				entire call.
				</para></listitem>
				<listitem><para>
				<emphasis>i/e</emphasis> - when RTPProxy is used in bridge mode,
				these flags are used to indicate the direction of the media flow
				for the current request/reply. 'i' refers to the LAN (internal
				network) and corresponds to the first interface of RTPProxy (as
				specified by the -l parameter). 'e' refers to the WAN (external
				network) and corresponds to the second interface of RTPProxy.
				These flags should always be used together. For example, an
				INVITE (offer) that comes from the Internet (WAN) to goes to a
				local media server (LAN) should use the 'ei' flags. The answer
				should use the 'ie' flags. Depending on the scenario, the 'ii'
				and 'ee' combination are also supported. Only makes sense when
				RTPProxy is running in the bridge mode.
				</para><para>
				<emphasis>NOTE:</emphasis> when using RTPProxy in bridge mode,
				all sessions are considered asymmetric (as oposed to symmetric
				if used in normal mode). If you have symmetric clients (this
				is the most common scenario), you'll have to force the
				<emphasis>s</emphasis>!
				</para></listitem>
				<listitem><para>
				<emphasis>f</emphasis> - instructs rtpproxy to ignore marks 
				inserted by another rtpproxy in transit to indicate that the 
				session is already goes through another proxy. Allows creating 
				chain of proxies.
				</para></listitem>
				<listitem><para>
				<emphasis>r</emphasis> - flags that IP address in SDP should 
				be trusted. Without this flag, rtpproxy ignores address in 
				the SDP and uses source address of the SIP message as media 
				address which is passed to the RTP proxy.
				</para></listitem>
				<listitem><para>
				<emphasis>o</emphasis> - flags that IP from the origin 
				description (o=) should be also changed.
				</para></listitem>
				<listitem><para>
				<emphasis>c</emphasis> - flags to change the session-level 
				SDP connection (c=) IP if media-description also includes 
				connection information.
				</para></listitem>
				<listitem><para>
				<emphasis>s/w</emphasis> - flags that for the UA from which 
				message is received, support symmetric RTP must be forced.
				</para></listitem>
				<listitem><para>
				<emphasis>n[&lt;SOCKET&gt;]</emphasis> - flags that enables
				the notification timeout for the session. One can specify an
				optional "advertised" socket between the &lt; and &gt; tags.
				If the socket is not specified, the value of
				<emphasis>rtpp_notify_socket</emphasis> is used.
				</para></listitem>
				<listitem><para>
				<emphasis>d[NNN]</emphasis> - enables DTMF notifications for this
				call. One can optionally specify the payload type that DTMF will
				be used for this call - it it is not specified, RTPProxy uses the
				<emphasis>101</emphasis> pt. <emphasis>NOTE:</emphasis> this feature
				is currently only available in the RTPProxy
				<emphasis>rtpp_2_1_dtmf</emphasis> branch.
				</para></listitem>
				<listitem><para>
				<emphasis>tNN</emphasis> - can be used to specify a RTP
				ttl for the caller. The NN represents the timeout in seconds
				for that stream. This can be useful in music on hold scenarios
				where only one client is sending RTP.
				</para></listitem>
				<listitem><para>
				<emphasis>TNN</emphasis> - Similar to the <emphasis>tNN</emphasis>
				paramaeter, but used for tuning the calllee's ttl for RTP.
				</para></listitem>
				<listitem><para>
				<emphasis>zNN</emphasis> - requests the RTPproxy to perform
				re-packetization of RTP traffic coming from the UA which
				has sent the current message to increase or decrease payload
				size per each RTP packet forwarded if possible.  The NN is the
				target payload size in ms, for the most codecs its value should
				be in 10ms increments, however for some codecs the increment
				could differ (e.g. 30ms for GSM or 20ms for G.723).  The
				RTPproxy would select the closest value supported by the codec.
				This feature could be used for significantly reducing bandwith
				overhead for low bitrate codecs, for example with G.729 going
				from 10ms to 100ms saves two thirds of the network bandwith.
				</para></listitem>
			</itemizedlist>
		</listitem>
		<listitem><para>
		<emphasis>ip_address(string, optional)</emphasis> - new SDP IP address.
		</para></listitem>
		<listitem><para>
		<emphasis>set_id(int, optional)</emphasis> - the set used for this call.
		</para></listitem>
		<listitem><para>
		<emphasis>sock_var(var, optional)</emphasis> - variable used to store the RTPProxy
		socket chosen for this call. Note that the variable will only be populated in the
		initial request.
		</para></listitem>
		<listitem><para>
		<emphasis>ret_var(var, optional)</emphasis> - variable used to print the IP and port
		the RTPProxy server is using for this call. This is useful especially when using
		the <emphasis>rtp_cluster</emphasis>, which can advertise multiple servers behind it.
		The format of the value returned is <emphasis>IP:port</emphasis>.
		Note that the variable will only be populated in the initial request.
		</para></listitem>
		</itemizedlist>
		<para>
		This function can be used from REQUEST_ROUTE, FAILURE_ROUTE, BRANCH_ROUTE.
		</para>
		<example>
		<title><function>rtpproxy_engage</function> usage</title>
		<programlisting format="linespecific">
...
if (is_method("INVITE") &amp;&amp; has_totag()) {
	if ($var(setid) != 0) {
		rtpproxy_engage(,,$var(setid), $var(proxy));
		xlog("SCRIPT: RTPProxy server used is $var(proxy)\n");
	} else {
		rtpproxy_engage();
		xlog("SCRIPT: using default RTPProxy set\n");
	}
}
...
		</programlisting>
		</example>
		</section>

        <section id="func_rtpproxy_offer" xreflabel="rtpproxy_offer()">
                <title>
                <function moreinfo="none">rtpproxy_offer([[flags][, [ip_address][, [set_id][, [sock_var][, [ret_var][, [body_var]]]]]])</function>
                </title>
                <para>
                Rewrites &sdp; body to ensure that media is passed through
                an &rtp; proxy. To be invoked
		on INVITE for the cases the SDPs are in INVITE and 200 OK and on 200 OK
		when SDPs are in 200 OK and ACK.
			</para>
				<para>
					The function receives the same parameters as
					<function>rtpproxy_engage()</function>, as well as an extra
					parameter named <emphasis>body_var</emphasis> - this parameter
					is used as an in-out variable for the body that should be used
					to challenge &rtp; proxy server. If the variable is specified,
					it is the function uses its content as the body to challenge,
					and returns the resulted body in it. If not used, the message's
					body is used, and the outgoing body is changed.
				</para>
			<para>
		This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE,
		FAILURE_ROUTE, BRANCH_ROUTE.
                </para>
		<example>
		<title><function>rtpproxy_offer</function> usage</title>
		<programlisting format="linespecific">
route {
...
    if (is_method("INVITE")) {
        if (has_body("application/sdp")) {
            if (rtpproxy_offer())
                t_on_reply("1");
        } else {
            t_on_reply("2");
        }
    }
    if (is_method("ACK") &amp;&amp; has_body("application/sdp"))
        rtpproxy_answer();
...
}

onreply_route[1]
{
...
    if (has_body("application/sdp"))
        rtpproxy_answer();
...
}

onreply_route[2]
{
...
    if (has_body("application/sdp"))
        rtpproxy_offer();
...
}
</programlisting>
                </example>
	</section>
        <section id="func_rtpproxy_answer" xreflabel="rtpproxy_answer()">
                <title>
                <function moreinfo="none">rtpproxy_answer([[flags][, [ip_address][, [set_id][, [sock_var][, [ret_var][, [body_var]]]]]]])</function>
                </title>
		<para>
		Rewrites &sdp; body to ensure that media is passed through
		an &rtp; proxy. To be invoked
		on 200 OK for the cases the SDPs are in INVITE and 200 OK and on ACK
		when SDPs are in 200 OK and ACK.
		</para>
		<para>
			See <function>rtpproxy_offer()</function> function description
			above for the meaning of the parameters.
		</para>
		<para>
		This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE,
		FAILURE_ROUTE, BRANCH_ROUTE.
		</para>
		<example>
		<title><function>rtpproxy_answer</function> usage</title>
		<para>
		See rtpproxy_offer() function example above for example.
		</para>
		</example>
        </section>
	<section id="func_rtpproxy_unforce" xreflabel="rtpproxy_unforce()">
		<title>
		<function moreinfo="none">rtpproxy_unforce([[set_id][, sock_var]])</function>
		</title>
		<para>
		Tears down the RTPProxy session for the current call.
		</para>
		<para>Meaning of the parameters is as follows:</para>
		<itemizedlist>
			<listitem><para>
			<emphasis>set_id(int, optional)</emphasis> - the set used for this call.
			</para></listitem>
			<listitem><para>
			<emphasis>sock_var(var, optional)</emphasis> - variable used to store the RTPProxy
			socket chosen for this call.
			</para></listitem>
		</itemizedlist>
		<para>
		This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE, FAILURE_ROUTE, BRANCH_ROUTE.
		</para>
		<example>
		<title><function>rtpproxy_unforce</function> usage</title>
		<programlisting format="linespecific">
...
rtpproxy_unforce();
...
</programlisting>
		</example>
	</section>
	<section id="rtpproxy_stream2xxx">
	<title>
	    <function>rtpproxy_stream2uac(prompt_name, count[, [set_id][, sock_var]])</function>,
	    <function>rtpproxy_stream2uas(prompt_name, count[, [set_id][, sock_var]])</function>
	</title>
	<para>
	    Instruct the RTPproxy to stream prompt/announcement pre-encoded with
	    the makeann command from the RTPproxy distribution. The uac/uas
	    suffix selects who will hear the announcement relatively to the current
	    transaction - UAC or UAS. For example invoking the
	    <function>rtpproxy_stream2uac</function> in the request processing
	    block on ACK transaction will play the prompt to the UA that has
	    generated original INVITE and ACK while
	    <function>rtpproxy_stop_stream2uas</function> on 183 in reply
	    processing block will play the prompt to the UA that has generated 183.
	</para>
	<para>
	    Apart from generating announcements, another possible application
	    of this function is implementing music on hold (MOH) functionality.
	    When count is -1, the streaming will be in loop indefinitely until
	    the appropriate <function>rtpproxy_stop_stream2xxx</function> is issued.
	</para>
	<para>
	    In order to work correctly, functions require that the session in the
	    RTPproxy already exists. Also those functions don't alted SDP, so that
	    they are not substitute for calling <function>rtpproxy_offer</function>
	    or <function>rtpproxy_answer</function>.
	</para>
	<para>
	    This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE.
	</para>
	<para>Meaning of the parameters is as follows:</para>
	<itemizedlist>
	    <listitem>
		<para>
		    <emphasis>prompt_name</emphasis> (string) - name of the prompt to
		    stream.  Should be either absolute pathname or pathname
		    relative to the directory where RTPproxy runs.
		</para>
	    </listitem>
	    <listitem>
		<para>
		    <emphasis>count</emphasis> (int) - number of times the prompt
		    should be repeated.  The value of -1 means that it will
		    be streaming in loop indefinitely, until appropriate
		    <function>rtpproxy_stop_stream2xxx</function> is issued.
		</para>
	    </listitem>
			<listitem><para>
			<emphasis>set_id(int, optional)</emphasis> - the set used for this call.
			</para></listitem>
			<listitem><para>
			<emphasis>sock_var(var, optional)</emphasis> - variable used to store the RTPProxy
			socket chosen for this call.
			</para></listitem>
	</itemizedlist>
	<example>
	    <title><function>rtpproxy_stream2xxx</function> usage</title>
	    <programlisting>
...
    if (is_method("INVITE")) {
        rtpproxy_offer();
        if ($rb=~ "0\.0\.0\.0") {
            rtpproxy_stream2uas("/var/rtpproxy/prompts/music_on_hold", -1);
        } else {
            rtpproxy_stop_stream2uas();
        };
    };
...
	    </programlisting>
	</example>
	</section>
	<section id="rtpproxy_stop_stream2xxx">
	<title>
	    <function>rtpproxy_stop_stream2uac([[set_id][, sock_var]])</function>,
	    <function>rtpproxy_stop_stream2uas([[set_id][, sock_var]])</function>
	</title>
	<para>
	    Stop streaming of announcement/prompt/MOH started previously by the
	    respective <function>rtpproxy_stream2xxx</function>.  The uac/uas
	    suffix selects whose announcement relatively to tha current
	    transaction should be stopped - UAC or UAS.
	</para>
		<para>Meaning of the parameters is as follows:</para>
		<itemizedlist>
			<listitem><para>
			<emphasis>set_id(int, optional)</emphasis> - the set used for this call.
			</para></listitem>
			<listitem><para>
			<emphasis>sock_var(var, optional)</emphasis> - variable used to store the RTPProxy
			socket chosen for this call.
			</para></listitem>
		</itemizedlist>
	<para>
	    These functions can be used from REQUEST_ROUTE, ONREPLY_ROUTE.
	</para>
	</section>
	<section id="func_rtpproxy_start_recording" xreflabel="rtpproxy_start_recording()">
		<title>
		<function moreinfo="none">rtpproxy_start_recording([[set_id][, [sock_var][, [flags][, [destination][, mediastream]]]]])</function>
		</title>
		<para>
		This command will send a signal to the RTP-Proxy to record 
		the RTP stream on the RTP-Proxy.
		</para>
		<para>Meaning of the parameters is as follows:</para>
		<itemizedlist>
			<listitem><para>
			<emphasis>set_id(int, optional)</emphasis> - the set used for this call.
			</para></listitem>
			<listitem><para>
			<emphasis>sock_var(var, optional)</emphasis> - variable used to store the RTPProxy
			socket chosen for this call.
			</para></listitem>
			<listitem><para>
					<emphasis>flags(string, optional)</emphasis> - a list of flags passed to
					RTPProxy for the recording. Currently only <emphasis>s</emphasis>
					is supported, and it indicates that RTPProxy should record both
					audio legs in a single file. Note that this feature is available
					starting with RTPProxy 2.0.
			</para></listitem>
			<listitem><para>
					<emphasis>destination(string, optional)</emphasis> - the destination of
					the recording. If it has the  <emphasis>udp:IP:port</emphasis>
					format, RTPProxy sends the RTP stream to that <emphasis>IP:port</emphasis>
					remote destination. Otherwise, destination represents the name
					of the file in the recording directory.
			</para></listitem>
			<listitem><para>
					<emphasis>mediastream(int, optional)</emphasis> - this parameter is only used
					if the <emphasis>destination</emphasis> is specified, and represents
					the index of media stream to record/copy, starting from 1. If this parameter
					is missing, &osips; instructs RTPProxy to copy all the streams.
			</para></listitem>
		</itemizedlist>
		<para>
		This function can be used from REQUEST_ROUTE and ONREPLY_ROUTE.
		</para>
		<example>
		<title><function>rtpproxy_start_recording</function> usage</title>
		<programlisting format="linespecific">
...
rtpproxy_start_recording();

# copy RTP stream to a different listener
rtpproxy_start_recording(,,,"udp:127.0.0.1:60000");

# copy only first RTP stream (audio stream)
rtpproxy_start_recording(,,,"udp:127.0.0.1:60000", 1);
...
		</programlisting>
		</example>
	</section>
	<section id="func_rtpproxy_stats" xreflabel="rtpproxy_stats()">
		<title>
		<function moreinfo="none">rtpproxy_stats(up_pvar, down_var, sent_var, fail_var[, [set_id][, sock_var]])</function>
		</title>
		<para>
		This command gathers call RTP statistics from RTP-Proxy.
		</para>
		<para>Meaning of the parameters is as follows:</para>
		<itemizedlist>
			<listitem><para>
					<emphasis>up_var</emphasis> (var) - the variable used to return the
					packets sent by <emphasis>upstream</emphasis> for this call.
			</para></listitem>
			<listitem><para>
					<emphasis>down_var</emphasis> (var) - the variable used to return the
					packets sent by <emphasis>downstream</emphasis> for this call.
			</para></listitem>
			<listitem><para>
					<emphasis>sent_var</emphasis> (var) - the variable used to return the
					total number of packets sent for this call.
			</para></listitem>
			<listitem><para>
					<emphasis>up_var</emphasis> (var) - the variable used to return the
					number of failed packets for this call.
			</para></listitem>
			<listitem><para>
			<emphasis>set_id(int, optional)</emphasis> - the set used for this call.
			</para></listitem>
			<listitem><para>
			<emphasis>sock_var(var, optional)</emphasis> - variable used to store the RTPProxy
			socket chosen for this call.
			</para></listitem>
		</itemizedlist>
		<para>
			This function can be used from REQUEST_ROUTE, FAILURE_ROUTE, ONREPLY_ROUTE,
			BRANCH_ROUTE and LOCAL_ROUTE.
		</para>
		<example>
		<title><function>rtpproxy_stats</function> usage</title>
		<programlisting format="linespecific">
...
rtpproxy_stats($var(up),$var(down),$var(sent),$var(fail));
xlog("RTP statistics for $ci: up=$var(up) down=$var(down) sent=$var(sent) fail=$var(fail)\n");
...
		</programlisting>
		</example>
	</section>
	<section id="func_rtpproxy_all_stats" xreflabel="rtpproxy_all_stats()">
		<title>
		<function moreinfo="none">rtpproxy_all_stats(stats_avp[, [set_id][, sock_var]])</function>
		</title>
		<para>
			This command gathers all RTP statistics available from RTP-Proxy.
			All the returned values stored in an AVP that can be further read by
			indexing the AVP.
		</para>
		<para>
			This command is only available starting with RTPProxy 2.1 realease.
		</para>
		<para>Meaning of the parameters is as follows:</para>
		<itemizedlist>
			<listitem><para>
					<emphasis>stats_avp</emphasis> (var) - an AVP where the
					statistics will be stored. This AVP can be further
					indexed to get a specific statistic.
			</para></listitem>
			<listitem><para>
			<emphasis>set_id(int, optional)</emphasis> - the set used for this call.
			</para></listitem>
			<listitem><para>
			<emphasis>sock_var(var, optional)</emphasis> - variable used to store the RTPProxy
			socket chosen for this call.
			</para></listitem>
		</itemizedlist>
		<para>
			This function can be used from REQUEST_ROUTE, FAILURE_ROUTE, ONREPLY_ROUTE,
			BRANCH_ROUTE and LOCAL_ROUTE.
		</para>
		<para>
			Each statistic is stored at a specific index as it follows:
			<itemizedlist>
				<listitem><para><emphasis>ttl</emphasis> -
						<emphasis>$avp(ret)</emphasis> /
						<emphasis>$(avp(ret)[0])</emphasis>
				</para></listitem>
				<listitem><para><emphasis>pkts_ia</emphasis> -
						<emphasis>$(avp(ret)[1])</emphasis>
				</para></listitem>
				<listitem><para><emphasis>pkts_io</emphasis> -
						<emphasis>$(avp(ret)[2])</emphasis>
				</para></listitem>
				<listitem><para><emphasis>relayed</emphasis> -
						<emphasis>$(avp(ret)[3])</emphasis>
				</para></listitem>
				<listitem><para><emphasis>dropped</emphasis> -
						<emphasis>$(avp(ret)[4])</emphasis>
				</para></listitem>
				<listitem><para><emphasis>rtpa_set</emphasis> -
						<emphasis>$(avp(ret)[5])</emphasis>
				</para></listitem>
				<listitem><para><emphasis>rtpa_rcvd</emphasis> -
						<emphasis>$(avp(ret)[6])</emphasis>
				</para></listitem>
				<listitem><para><emphasis>rtpa_dups</emphasis> -
						<emphasis>$(avp(ret)[7])</emphasis>
				</para></listitem>
				<listitem><para><emphasis>rtpa_lost</emphasis> -
						<emphasis>$(avp(ret)[8])</emphasis>
				</para></listitem>
				<listitem><para><emphasis>rtpa_perrs</emphasis> -
						<emphasis>$(avp(ret)[9])</emphasis>
				</para></listitem>
			</itemizedlist>
		</para>
		<example>
		<title><function>rtpproxy_all_stats</function> usage</title>
		<programlisting format="linespecific">
...
rtpproxy_all_stats($avp(stats));
xlog("RTP statistics for $ci: dropped=$(avp(stats)[4])\n");
...
		</programlisting>
		</example>
	</section>
	</section>


	<section id="exported_mi_functions" xreflabel="Exported MI Functions">
	<title>Exported MI Functions</title>
		<section id="mi_rtpproxy_enable" xreflabel="rtpproxy_enable">
			<title><function moreinfo="none">rtpproxy_enable</function></title>
			<para>
			Enables/Disables a rtp proxy.
			</para>
			<para>Parameters:</para>
			<itemizedlist>
				<listitem><para>
					<emphasis>url</emphasis> - the rtp proxy url (exactly as defined in 
					the config file).
				</para></listitem>
				<listitem><para>
					<emphasis>enable</emphasis> - 1 - enable, 0 - disable.
					the config file).
				</para></listitem>
				<listitem><para>
					<emphasis>setid</emphasis> (optional) - the rtpproxy set ID (used
					for better indentification of the rtpproxy instance to be enabled,
					for example when a rtpproxy is used in multiple sets).
				</para></listitem>
			</itemizedlist>
			<para>
			NOTE: if a rtpproxy is defined multiple times (in the same or
			different set), all its instances will be enables/disabled IF
			no set ID provided (as second param).
			</para>
			<example>
			<title>
				<function moreinfo="none">rtpproxy_enable</function> usage</title>
			<programlisting format="linespecific">
...
## disable a RTPProxy by URL only
$ opensips-cli -x mi rtpproxy_enable udp:192.168.2.133:8081 0
## disable a RTPProxy by URL and set ID (3)
$ opensips-cli -x mi rtpproxy_enable udp:192.168.2.133:8081 0 3
...
			</programlisting>
			</example>
		</section>
				
			<section id="mi_rtpproxy_show" xreflabel="rtpproxy_show">
			<title><function moreinfo="none">rtpproxy_show</function></title>
			<para>
			Displays all the rtp proxies and their information: set and 
			status (disabled or not, weight and recheck_ticks).
			</para>
			<para>
			No parameter.
			</para>
			<example>
			<title>
				<function moreinfo="none">rtpproxy_show</function> usage</title>
			<programlisting format="linespecific">
...
$ opensips-cli -x mi rtpproxy_show
...
			</programlisting>
			</example>
		</section>

		<section id="mi_rtpproxy_reload" xreflabel="rtpproxy_reload">
			<title><function moreinfo="none">rtpproxy_reload</function></title>
			<para>
			Reload rtp proxies sets from database. The function will delete all
			previous records and populate the list with the entries from the
			database table. The db_url parameter must be set if you want to use
			this command.
			</para>
			<para>
			No parameter.
			</para>
			<example>
			<title>
				<function moreinfo="none">rtpproxy_reload</function> usage</title>
			<programlisting format="linespecific">
...
$ opensips-cli -x mi rtpproxy_reload
...
			</programlisting>
			</example>
		</section>


	</section>

	<section id="exported_events" xreflabel="Exported Events">
	<title>Exported Events</title>
	<section id="event_E_RTPPROXY_STATUS" xreflabel="E_RTPPROXY_STATUS">
		<title>
		<function moreinfo="none">E_RTPPROXY_STATUS</function>
		</title>
		<para>
			This event is raised when a RTPProxy server changes it's status to
			enabled/disabled.
		</para>
		<para>Parameters:</para>
		<itemizedlist>
			<listitem><para>
				<emphasis>socket</emphasis> - the socket that identifies the 
				RTPProxy instance.
			</para></listitem>
			<listitem><para>
				<emphasis>status</emphasis> - <emphasis>active</emphasis> if
				the RTPProxy instance responds to probing or
				<emphasis>inactive</emphasis> if the instance was deactivated.
			</para></listitem>
		</itemizedlist>
	</section>
	<section id="event_E_RTPPROXY_DTMF" xreflabel="E_RTPPROXY_DTMF">
		<title>
		<function moreinfo="none">E_RTPPROXY_DTMF</function>
		</title>
		<para>
			This event is raised when a RTPProxy server sends a DTMF
			notification to OpenSIPS. In order to catch RFC 2833/4733
			DTMF events, you need to provide the <emphasis>d</emphasis>
			flag to <emphasis>rtpproxy_offer()</emphasis>/
			<emphasis>rtpproxy_answer()</emphasis>.
		</para>
		<para>Parameters:</para>
		<itemizedlist>
			<listitem><para>
				<emphasis>digit</emphasis> - the digit pressed.
			</para></listitem>
			<listitem><para>
				<emphasis>duration</emphasis> - the duration of the event.
			</para></listitem>
			<listitem><para>
				<emphasis>volume</emphasis> - the volume of the event.
			</para></listitem>
			<listitem><para>
				<emphasis>id</emphasis> - represents the identifier of
				the call for which that event was received.
			</para></listitem>
			<listitem><para>
				<emphasis>is_callid</emphasis> - is <emphasis>0</emphasis>
				if the <emphasis>id</emphasis> parameter represents the
				Dialog ID, or <emphasis>1</emphasis> if it is a callid.
			</para></listitem>
			<listitem><para>
				<emphasis>stream</emphasis> - indicates the stream index
				of the RTPProxy session. It is normally 0 if the caller
				sent the DTMF, or 1 if the callee sent it.
			</para></listitem>
		</itemizedlist>
	</section>
</section>

</chapter>

