rtp: Add 'autoconnect' feature to the osmo_rtp_socket

The bound RTP socket will wait for incoming RTP packets and as soon as it
sees one, will 'connect' to it, so all replies will go to that sources and
incoming messages from other sources will be discarded. This obviously only
works once.

Change-Id: I5b54ca4296901fcf37794faf29e0b2acca27bd1b
Signed-off-by: Sylvain Munaut <tnt@246tNt.com>
diff --git a/include/osmocom/trau/osmo_ortp.h b/include/osmocom/trau/osmo_ortp.h
index 5fb2960..54afcf0 100644
--- a/include/osmocom/trau/osmo_ortp.h
+++ b/include/osmocom/trau/osmo_ortp.h
@@ -74,6 +74,7 @@
 struct osmo_rtp_socket *osmo_rtp_socket_create(void *talloc_ctx, unsigned int flags);
 int osmo_rtp_socket_bind(struct osmo_rtp_socket *rs, const char *ip, int port);
 int osmo_rtp_socket_connect(struct osmo_rtp_socket *rs, const char *ip, uint16_t port);
+int osmo_rtp_socket_autoconnect(struct osmo_rtp_socket *rs);
 int osmo_rtp_socket_set_pt(struct osmo_rtp_socket *rs, int payload_type);
 int osmo_rtp_socket_free(struct osmo_rtp_socket *rs);
 int osmo_rtp_skipped_frame(struct osmo_rtp_socket *rs, unsigned int duration);
diff --git a/src/trau/osmo_ortp.c b/src/trau/osmo_ortp.c
index 0eb1661..1d652cb 100644
--- a/src/trau/osmo_ortp.c
+++ b/src/trau/osmo_ortp.c
@@ -468,6 +468,28 @@
 		return osmo_rtp_socket_fdreg(rs);
 }
 
+/*! \brief Automatically associates a RTP socket with the first incoming UDP packet
+ *  \param[in] rs OsmoRTP socket
+ *
+ * The bound RTP socket will wait for incoming RTP packets and as soon as it
+ * sees one, will 'connect' to it, so all replies will go to that sources and
+ * incoming messages from other sources will be discarded. This obviously only
+ * works once.
+ *
+ *  \returns 0 on success, <0 in case of error.
+ */
+int osmo_rtp_socket_autoconnect(struct osmo_rtp_socket *rs)
+{
+	rtp_session_set_symmetric_rtp(rs->sess, 1);
+	rtp_session_set_connected_mode(rs->sess, 1);
+	rs->flags &= ~OSMO_RTP_F_DISABLED;
+
+	if (rs->flags & OSMO_RTP_F_POLL)
+		return 0;
+	else
+		return osmo_rtp_socket_fdreg(rs);
+}
+
 /*! \brief Increment timestamp on a RTP socket without sending any packet
  *  \param[in] rs OsmoRTP socket
  *  \param[in] duration duration in number of RTP clock ticks