client: expose connection
[platform/upstream/gstreamer.git] / gst / rtsp-server / rtsp-client.c
index 3f6cd0a..af07253 100644 (file)
 #include "rtsp-sdp.h"
 #include "rtsp-params.h"
 
+#define GST_RTSP_CLIENT_GET_PRIVATE(obj)  \
+   (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_CLIENT, GstRTSPClientPrivate))
+
+/* locking order:
+ * send_lock, lock, tunnels_lock
+ */
+
+struct _GstRTSPClientPrivate
+{
+  GMutex lock;                  /* protects everything else */
+  GMutex send_lock;
+  GstRTSPConnection *connection;
+  GstRTSPWatch *watch;
+  guint close_seq;
+  gchar *server_ip;
+  gboolean is_ipv6;
+  gboolean use_client_settings;
+
+  GstRTSPClientSendFunc send_func;      /* protected by send_lock */
+  gpointer send_data;           /* protected by send_lock */
+  GDestroyNotify send_notify;   /* protected by send_lock */
+
+  GstRTSPSessionPool *session_pool;
+  GstRTSPMountPoints *mount_points;
+  GstRTSPAuth *auth;
+
+  GstRTSPUrl *uri;
+  GstRTSPMedia *media;
+
+  GList *transports;
+  GList *sessions;
+};
+
 static GMutex tunnels_lock;
-static GHashTable *tunnels;
+static GHashTable *tunnels;     /* protected by tunnels_lock */
 
 #define DEFAULT_SESSION_POOL            NULL
 #define DEFAULT_MOUNT_POINTS            NULL
@@ -79,6 +112,8 @@ gst_rtsp_client_class_init (GstRTSPClientClass * klass)
 {
   GObjectClass *gobject_class;
 
+  g_type_class_add_private (klass, sizeof (GstRTSPClientPrivate));
+
   gobject_class = G_OBJECT_CLASS (klass);
 
   gobject_class->get_property = gst_rtsp_client_get_property;
@@ -173,38 +208,51 @@ gst_rtsp_client_class_init (GstRTSPClientClass * klass)
 static void
 gst_rtsp_client_init (GstRTSPClient * client)
 {
-  client->use_client_settings = DEFAULT_USE_CLIENT_SETTINGS;
-  client->close_response_seq = 0;
+  GstRTSPClientPrivate *priv = GST_RTSP_CLIENT_GET_PRIVATE (client);
+
+  client->priv = priv;
+
+  g_mutex_init (&priv->lock);
+  g_mutex_init (&priv->send_lock);
+  priv->use_client_settings = DEFAULT_USE_CLIENT_SETTINGS;
+  priv->close_seq = 0;
+}
+
+static GstRTSPFilterResult
+filter_session (GstRTSPSession * sess, GstRTSPSessionMedia * media,
+    gpointer user_data)
+{
+  GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
+
+  gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
+  unlink_session_transports (client, sess, media);
+
+  /* unmanage the media in the session */
+  return GST_RTSP_FILTER_REMOVE;
 }
 
 static void
 client_unlink_session (GstRTSPClient * client, GstRTSPSession * session)
 {
   /* unlink all media managed in this session */
-  while (session->medias) {
-    GstRTSPSessionMedia *media = session->medias->data;
-
-    gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
-    unlink_session_transports (client, session, media);
-    /* unmanage the media in the session. this will modify session->medias */
-    gst_rtsp_session_release_media (session, media);
-  }
+  gst_rtsp_session_filter (session, filter_session, client);
 }
 
 static void
 client_cleanup_sessions (GstRTSPClient * client)
 {
+  GstRTSPClientPrivate *priv = client->priv;
   GList *sessions;
 
   /* remove weak-ref from sessions */
-  for (sessions = client->sessions; sessions; sessions = g_list_next (sessions)) {
+  for (sessions = priv->sessions; sessions; sessions = g_list_next (sessions)) {
     GstRTSPSession *session = (GstRTSPSession *) sessions->data;
     g_object_weak_unref (G_OBJECT (session),
         (GWeakNotify) client_session_finalized, client);
     client_unlink_session (client, session);
   }
-  g_list_free (client->sessions);
-  client->sessions = NULL;
+  g_list_free (priv->sessions);
+  priv->sessions = NULL;
 }
 
 /* A client is finalized when the connection is broken */
@@ -212,30 +260,36 @@ static void
 gst_rtsp_client_finalize (GObject * obj)
 {
   GstRTSPClient *client = GST_RTSP_CLIENT (obj);
+  GstRTSPClientPrivate *priv = client->priv;
 
   GST_INFO ("finalize client %p", client);
 
-  if (client->watch)
-    g_source_destroy ((GSource *) client->watch);
+  gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
+
+  if (priv->watch)
+    g_source_destroy ((GSource *) priv->watch);
 
   client_cleanup_sessions (client);
 
-  gst_rtsp_connection_free (client->connection);
-  if (client->session_pool)
-    g_object_unref (client->session_pool);
-  if (client->mount_points)
-    g_object_unref (client->mount_points);
-  if (client->auth)
-    g_object_unref (client->auth);
+  if (priv->connection)
+    gst_rtsp_connection_free (priv->connection);
+  if (priv->session_pool)
+    g_object_unref (priv->session_pool);
+  if (priv->mount_points)
+    g_object_unref (priv->mount_points);
+  if (priv->auth)
+    g_object_unref (priv->auth);
 
-  if (client->uri)
-    gst_rtsp_url_free (client->uri);
-  if (client->media) {
-    gst_rtsp_media_unprepare (client->media);
-    g_object_unref (client->media);
+  if (priv->uri)
+    gst_rtsp_url_free (priv->uri);
+  if (priv->media) {
+    gst_rtsp_media_unprepare (priv->media);
+    g_object_unref (priv->media);
   }
 
-  g_free (client->server_ip);
+  g_free (priv->server_ip);
+  g_mutex_clear (&priv->lock);
+  g_mutex_clear (&priv->send_lock);
 
   G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
 }
@@ -305,6 +359,8 @@ static void
 send_response (GstRTSPClient * client, GstRTSPSession * session,
     GstRTSPMessage * response, gboolean close)
 {
+  GstRTSPClientPrivate *priv = client->priv;
+
   gst_rtsp_message_add_header (response, GST_RTSP_HDR_SERVER,
       "GStreamer RTSP server");
 
@@ -321,14 +377,14 @@ send_response (GstRTSPClient * client, GstRTSPSession * session,
     gst_rtsp_message_dump (response);
   }
 
-  if (close) {
-    gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONNECTION,
-        "close");
-  }
-  /* send the response and store the seq number so we can wait until it's
-   * written to the client to close the connection */
-  gst_rtsp_watch_send_message (client->watch, response, close ?
-      &client->close_response_seq : NULL);
+  if (close)
+    gst_rtsp_message_add_header (response, GST_RTSP_HDR_CONNECTION, "close");
+
+  g_mutex_lock (&priv->send_lock);
+  if (priv->send_func)
+    priv->send_func (client, response, close, priv->send_data);
+  g_mutex_unlock (&priv->send_lock);
+
   gst_rtsp_message_unset (response);
 }
 
@@ -376,38 +432,40 @@ compare_uri (const GstRTSPUrl * uri1, const GstRTSPUrl * uri2)
 static GstRTSPMedia *
 find_media (GstRTSPClient * client, GstRTSPClientState * state)
 {
+  GstRTSPClientPrivate *priv = client->priv;
   GstRTSPMediaFactory *factory;
   GstRTSPMedia *media;
   GstRTSPAuth *auth;
 
-  if (!compare_uri (client->uri, state->uri)) {
+  if (!compare_uri (priv->uri, state->uri)) {
     /* remove any previously cached values before we try to construct a new
      * media for uri */
-    if (client->uri)
-      gst_rtsp_url_free (client->uri);
-    client->uri = NULL;
-    if (client->media) {
-      gst_rtsp_media_unprepare (client->media);
-      g_object_unref (client->media);
+    if (priv->uri)
+      gst_rtsp_url_free (priv->uri);
+    priv->uri = NULL;
+    if (priv->media) {
+      gst_rtsp_media_unprepare (priv->media);
+      g_object_unref (priv->media);
     }
-    client->media = NULL;
+    priv->media = NULL;
 
-    if (!client->mount_points)
+    if (!priv->mount_points)
       goto no_mount_points;
 
     /* find the factory for the uri first */
     if (!(factory =
-            gst_rtsp_mount_points_find_factory (client->mount_points,
+            gst_rtsp_mount_points_find_factory (priv->mount_points,
                 state->uri)))
       goto no_factory;
 
-    state->factory = factory;
-
     /* check if we have access to the factory */
     if ((auth = gst_rtsp_media_factory_get_auth (factory))) {
+      state->factory = factory;
+
       if (!gst_rtsp_auth_check (auth, client, 0, state))
         goto not_allowed;
 
+      state->factory = NULL;
       g_object_unref (auth);
     }
 
@@ -417,22 +475,18 @@ find_media (GstRTSPClient * client, GstRTSPClientState * state)
 
     g_object_unref (factory);
     factory = NULL;
-    state->factory = NULL;
-
-    /* set ipv6 on the media before preparing */
-    media->is_ipv6 = client->is_ipv6;
-    state->media = media;
 
     /* prepare the media */
     if (!(gst_rtsp_media_prepare (media)))
       goto no_prepare;
 
     /* now keep track of the uri and the media */
-    client->uri = gst_rtsp_url_copy (state->uri);
-    client->media = media;
+    priv->uri = gst_rtsp_url_copy (state->uri);
+    priv->media = media;
+    state->media = media;
   } else {
     /* we have seen this uri before, used cached media */
-    media = client->media;
+    media = priv->media;
     state->media = media;
     GST_INFO ("reusing cached media %p", media);
   }
@@ -445,29 +499,35 @@ find_media (GstRTSPClient * client, GstRTSPClientState * state)
   /* ERRORS */
 no_mount_points:
   {
+    GST_ERROR ("client %p: no mount points configured", client);
     send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
     return NULL;
   }
 no_factory:
   {
+    GST_ERROR ("client %p: no factory for uri", client);
     send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
     return NULL;
   }
 not_allowed:
   {
+    GST_ERROR ("client %p: unauthorized request", client);
     handle_unauthorized_request (client, auth, state);
     g_object_unref (factory);
+    state->factory = NULL;
     g_object_unref (auth);
     return NULL;
   }
 no_media:
   {
+    GST_ERROR ("client %p: can't create media", client);
     send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
     g_object_unref (factory);
     return NULL;
   }
 no_prepare:
   {
+    GST_ERROR ("client %p: can't prepare media", client);
     send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
     g_object_unref (media);
     return NULL;
@@ -477,6 +537,7 @@ no_prepare:
 static gboolean
 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
 {
+  GstRTSPClientPrivate *priv = client->priv;
   GstRTSPMessage message = { 0 };
   GstMapInfo map_info;
   guint8 *data;
@@ -490,9 +551,10 @@ do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
 
   gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
 
-  /* FIXME, client->watch could have been finalized here, we need to keep an
-   * extra refcount to the watch.  */
-  gst_rtsp_watch_send_message (client->watch, &message, NULL);
+  g_mutex_lock (&priv->send_lock);
+  if (priv->send_func)
+    priv->send_func (client, &message, FALSE, priv->send_data);
+  g_mutex_unlock (&priv->send_lock);
 
   gst_rtsp_message_steal_body (&message, &data, &usize);
   gst_buffer_unmap (buffer, &map_info);
@@ -506,12 +568,15 @@ static void
 link_transport (GstRTSPClient * client, GstRTSPSession * session,
     GstRTSPStreamTransport * trans)
 {
+  GstRTSPClientPrivate *priv = client->priv;
+
   GST_DEBUG ("client %p: linking transport %p", client, trans);
+
   gst_rtsp_stream_transport_set_callbacks (trans,
       (GstRTSPSendFunc) do_send_data,
       (GstRTSPSendFunc) do_send_data, client, NULL);
 
-  client->transports = g_list_prepend (client->transports, trans);
+  priv->transports = g_list_prepend (priv->transports, trans);
 
   /* make sure our session can't expire */
   gst_rtsp_session_prevent_expire (session);
@@ -521,10 +586,13 @@ static void
 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
     GstRTSPStreamTransport * trans)
 {
+  GstRTSPClientPrivate *priv = client->priv;
+
   GST_DEBUG ("client %p: unlinking transport %p", client, trans);
+
   gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
 
-  client->transports = g_list_remove (client->transports, trans);
+  priv->transports = g_list_remove (priv->transports, trans);
 
   /* our session can now expire */
   gst_rtsp_session_allow_expire (session);
@@ -536,17 +604,18 @@ unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
 {
   guint n_streams, i;
 
-  n_streams = gst_rtsp_media_n_streams (media->media);
+  n_streams =
+      gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (media));
   for (i = 0; i < n_streams; i++) {
     GstRTSPStreamTransport *trans;
-    GstRTSPTransport *tr;
+    const GstRTSPTransport *tr;
 
     /* get the transport, if there is no transport configured, skip this stream */
     trans = gst_rtsp_session_media_get_transport (media, i);
     if (trans == NULL)
       continue;
 
-    tr = trans->transport;
+    tr = gst_rtsp_stream_transport_get_transport (trans);
 
     if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
       /* for TCP, unlink the stream from the TCP connection of the client */
@@ -558,23 +627,25 @@ unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
 static void
 close_connection (GstRTSPClient * client)
 {
+  GstRTSPClientPrivate *priv = client->priv;
   const gchar *tunnelid;
 
   GST_DEBUG ("client %p: closing connection", client);
 
-  if ((tunnelid = gst_rtsp_connection_get_tunnelid (client->connection))) {
+  if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
     g_mutex_lock (&tunnels_lock);
     /* remove from tunnelids */
     g_hash_table_remove (tunnels, tunnelid);
     g_mutex_unlock (&tunnels_lock);
   }
 
-  gst_rtsp_connection_close (client->connection);
+  gst_rtsp_connection_close (priv->connection);
 }
 
 static gboolean
 handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
 {
+  GstRTSPClientPrivate *priv = client->priv;
   GstRTSPSession *session;
   GstRTSPSessionMedia *media;
   GstRTSPStatusCode code;
@@ -597,7 +668,7 @@ handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
   /* remove the session from the watched sessions */
   g_object_weak_unref (G_OBJECT (session),
       (GWeakNotify) client_session_finalized, client);
-  client->sessions = g_list_remove (client->sessions, session);
+  priv->sessions = g_list_remove (priv->sessions, session);
 
   gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
 
@@ -605,7 +676,7 @@ handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
    * are torn down. */
   if (!gst_rtsp_session_release_media (session, media)) {
     /* remove the session */
-    gst_rtsp_session_pool_remove (client->session_pool, session);
+    gst_rtsp_session_pool_remove (priv->session_pool, session);
   }
   /* construct the response now */
   code = GST_RTSP_STS_OK;
@@ -623,11 +694,13 @@ handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
   /* ERRORS */
 no_session:
   {
+    GST_ERROR ("client %p: no session", client);
     send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
     return FALSE;
   }
 not_found:
   {
+    GST_ERROR ("client %p: no media for uri", client);
     send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
     return FALSE;
   }
@@ -664,6 +737,7 @@ handle_get_param_request (GstRTSPClient * client, GstRTSPClientState * state)
   /* ERRORS */
 bad_request:
   {
+    GST_ERROR ("client %p: bad request", client);
     send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
     return FALSE;
   }
@@ -700,6 +774,7 @@ handle_set_param_request (GstRTSPClient * client, GstRTSPClientState * state)
   /* ERRORS */
 bad_request:
   {
+    GST_ERROR ("client %p: bad request", client);
     send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
     return FALSE;
   }
@@ -711,6 +786,7 @@ handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
   GstRTSPSession *session;
   GstRTSPSessionMedia *media;
   GstRTSPStatusCode code;
+  GstRTSPState rtspstate;
 
   if (!(session = state->session))
     goto no_session;
@@ -722,9 +798,10 @@ handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
 
   state->sessmedia = media;
 
+  rtspstate = gst_rtsp_session_media_get_rtsp_state (media);
   /* the session state must be playing or recording */
-  if (media->state != GST_RTSP_STATE_PLAYING &&
-      media->state != GST_RTSP_STATE_RECORDING)
+  if (rtspstate != GST_RTSP_STATE_PLAYING &&
+      rtspstate != GST_RTSP_STATE_RECORDING)
     goto invalid_state;
 
   /* unlink the all TCP callbacks */
@@ -741,7 +818,7 @@ handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
   send_response (client, session, state->response, FALSE);
 
   /* the state is now READY */
-  media->state = GST_RTSP_STATE_READY;
+  gst_rtsp_session_media_set_rtsp_state (media, GST_RTSP_STATE_READY);
 
   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST],
       0, state);
@@ -751,16 +828,19 @@ handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
   /* ERRORS */
 no_session:
   {
+    GST_ERROR ("client %p: no seesion", client);
     send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
     return FALSE;
   }
 not_found:
   {
+    GST_ERROR ("client %p: no media for uri", client);
     send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
     return FALSE;
   }
 invalid_state:
   {
+    GST_ERROR ("client %p: not PLAYING or RECORDING", client);
     send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
         state);
     return FALSE;
@@ -778,6 +858,8 @@ handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
   gchar *str;
   GstRTSPTimeRange *range;
   GstRTSPResult res;
+  GstRTSPState rtspstate;
+  GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
 
   if (!(session = state->session))
     goto no_session;
@@ -790,8 +872,8 @@ handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
   state->sessmedia = media;
 
   /* the session state must be playing or ready */
-  if (media->state != GST_RTSP_STATE_PLAYING &&
-      media->state != GST_RTSP_STATE_READY)
+  rtspstate = gst_rtsp_session_media_get_rtsp_state (media);
+  if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
     goto invalid_state;
 
   /* parse the range header if we have one */
@@ -800,7 +882,8 @@ handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
   if (res == GST_RTSP_OK) {
     if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
       /* we have a range, seek to the position */
-      gst_rtsp_media_seek (media->media, range);
+      gst_rtsp_media_seek (gst_rtsp_session_media_get_media (media), range);
+      unit = range->unit;
       gst_rtsp_range_free (range);
     }
   }
@@ -808,10 +891,12 @@ handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
   /* grab RTPInfo from the payloaders now */
   rtpinfo = g_string_new ("");
 
-  n_streams = gst_rtsp_media_n_streams (media->media);
+  n_streams =
+      gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (media));
   for (i = 0, infocount = 0; i < n_streams; i++) {
     GstRTSPStreamTransport *trans;
-    GstRTSPTransport *tr;
+    GstRTSPStream *stream;
+    const GstRTSPTransport *tr;
     gchar *uristr;
     guint rtptime, seq;
 
@@ -821,14 +906,15 @@ handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
       GST_INFO ("stream %d is not configured", i);
       continue;
     }
-    tr = trans->transport;
+    tr = gst_rtsp_stream_transport_get_transport (trans);
 
     if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
       /* for TCP, link the stream to the TCP connection of the client */
       link_transport (client, session, trans);
     }
 
-    if (gst_rtsp_stream_get_rtpinfo (trans->stream, &rtptime, &seq)) {
+    stream = gst_rtsp_stream_transport_get_stream (trans);
+    if (gst_rtsp_stream_get_rtpinfo (stream, &rtptime, &seq)) {
       if (infocount > 0)
         g_string_append (rtpinfo, ", ");
 
@@ -857,7 +943,9 @@ handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
   }
 
   /* add the range */
-  str = gst_rtsp_media_get_range_string (media->media, TRUE);
+  str =
+      gst_rtsp_media_get_range_string (gst_rtsp_session_media_get_media (media),
+      TRUE, unit);
   gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RANGE, str);
 
   send_response (client, session, state->response, FALSE);
@@ -865,7 +953,7 @@ handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
   /* start playing after sending the request */
   gst_rtsp_session_media_set_state (media, GST_STATE_PLAYING);
 
-  media->state = GST_RTSP_STATE_PLAYING;
+  gst_rtsp_session_media_set_rtsp_state (media, GST_RTSP_STATE_PLAYING);
 
   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST],
       0, state);
@@ -875,16 +963,19 @@ handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
   /* ERRORS */
 no_session:
   {
+    GST_ERROR ("client %p: no session", client);
     send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
     return FALSE;
   }
 not_found:
   {
+    GST_ERROR ("client %p: media not found", client);
     send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
     return FALSE;
   }
 invalid_state:
   {
+    GST_ERROR ("client %p: not PLAYING or READY", client);
     send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
         state);
     return FALSE;
@@ -983,9 +1074,21 @@ static gboolean
 configure_client_transport (GstRTSPClient * client, GstRTSPClientState * state,
     GstRTSPTransport * ct)
 {
+  GstRTSPClientPrivate *priv = client->priv;
+
   /* we have a valid transport now, set the destination of the client. */
   if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
-    if (ct->destination == NULL || !client->use_client_settings) {
+    if (ct->destination && priv->use_client_settings) {
+      GstRTSPAddress *addr;
+
+      addr = gst_rtsp_stream_reserve_address (state->stream, ct->destination,
+          ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
+
+      if (addr == NULL)
+        goto no_address;
+
+      gst_rtsp_address_free (addr);
+    } else {
       GstRTSPAddress *addr;
 
       addr = gst_rtsp_stream_get_address (state->stream);
@@ -997,11 +1100,13 @@ configure_client_transport (GstRTSPClient * client, GstRTSPClientState * state,
       ct->port.min = addr->port;
       ct->port.max = addr->port + addr->n_ports - 1;
       ct->ttl = addr->ttl;
+
+      gst_rtsp_address_free (addr);
     }
   } else {
     GstRTSPUrl *url;
 
-    url = gst_rtsp_connection_get_url (client->connection);
+    url = gst_rtsp_connection_get_url (priv->connection);
     g_free (ct->destination);
     ct->destination = g_strdup (url->host);
 
@@ -1039,7 +1144,7 @@ make_server_transport (GstRTSPClient * client, GstRTSPClientState * state,
   switch (st->lower_transport) {
     case GST_RTSP_LOWER_TRANS_UDP:
       st->client_port = ct->client_port;
-      st->server_port = state->stream->server_port;
+      gst_rtsp_stream_get_server_port (state->stream, &st->server_port);
       break;
     case GST_RTSP_LOWER_TRANS_UDP_MCAST:
       st->port = ct->port;
@@ -1052,8 +1157,7 @@ make_server_transport (GstRTSPClient * client, GstRTSPClientState * state,
       break;
   }
 
-  if (state->stream->session)
-    g_object_get (state->stream->session, "internal-ssrc", &st->ssrc, NULL);
+  gst_rtsp_stream_get_ssrc (state->stream, &st->ssrc);
 
   return st;
 }
@@ -1061,6 +1165,7 @@ make_server_transport (GstRTSPClient * client, GstRTSPClientState * state,
 static gboolean
 handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
 {
+  GstRTSPClientPrivate *priv = client->priv;
   GstRTSPResult res;
   GstRTSPUrl *uri;
   gchar *transport;
@@ -1074,6 +1179,7 @@ handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
   GstRTSPSessionMedia *sessmedia;
   GstRTSPMedia *media;
   GstRTSPStream *stream;
+  GstRTSPState rtspstate;
 
   uri = state->uri;
 
@@ -1112,7 +1218,7 @@ handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
 
   /* we create the session after parsing stuff so that we don't make
    * a session for malformed requests */
-  if (client->session_pool == NULL)
+  if (priv->session_pool == NULL)
     goto no_pool;
 
   session = state->session;
@@ -1125,7 +1231,7 @@ handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
   } else {
     /* create a session if this fails we probably reached our session limit or
      * something. */
-    if (!(session = gst_rtsp_session_pool_create (client->session_pool)))
+    if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
       goto service_unavailable;
 
     state->session = session;
@@ -1148,7 +1254,7 @@ handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
     goto not_found;
 
   state->sessmedia = sessmedia;
-  state->media = media = sessmedia->media;
+  state->media = media = gst_rtsp_session_media_get_media (sessmedia);
 
   /* now get the stream */
   stream = gst_rtsp_media_get_stream (media, streamid);
@@ -1189,14 +1295,15 @@ handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
   send_response (client, session, state->response, FALSE);
 
   /* update the state */
-  switch (sessmedia->state) {
+  rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
+  switch (rtspstate) {
     case GST_RTSP_STATE_PLAYING:
     case GST_RTSP_STATE_RECORDING:
     case GST_RTSP_STATE_READY:
       /* no state change */
       break;
     default:
-      sessmedia->state = GST_RTSP_STATE_READY;
+      gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
       break;
   }
   g_object_unref (session);
@@ -1209,11 +1316,13 @@ handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
   /* ERRORS */
 bad_request:
   {
+    GST_ERROR ("client %p: bad request", client);
     send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
     return FALSE;
   }
 not_found:
   {
+    GST_ERROR ("client %p: media not found", client);
     send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
     g_object_unref (session);
     gst_rtsp_transport_free (ct);
@@ -1221,6 +1330,7 @@ not_found:
   }
 invalid_blocksize:
   {
+    GST_ERROR ("client %p: invalid blocksize", client);
     send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
     g_object_unref (session);
     gst_rtsp_transport_free (ct);
@@ -1228,6 +1338,7 @@ invalid_blocksize:
   }
 unsupported_client_transport:
   {
+    GST_ERROR ("client %p: unsupported client transport", client);
     send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
     g_object_unref (session);
     gst_rtsp_transport_free (ct);
@@ -1235,23 +1346,27 @@ unsupported_client_transport:
   }
 no_transport:
   {
+    GST_ERROR ("client %p: no transport", client);
     send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
     return FALSE;
   }
 unsupported_transports:
   {
+    GST_ERROR ("client %p: unsupported transports", client);
     send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
     gst_rtsp_transport_free (ct);
     return FALSE;
   }
 no_pool:
   {
-    send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
+    GST_ERROR ("client %p: no session pool configured", client);
+    send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
     gst_rtsp_transport_free (ct);
     return FALSE;
   }
 service_unavailable:
   {
+    GST_ERROR ("client %p: can't create session", client);
     send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
     gst_rtsp_transport_free (ct);
     return FALSE;
@@ -1261,6 +1376,7 @@ service_unavailable:
 static GstSDPMessage *
 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
 {
+  GstRTSPClientPrivate *priv = client->priv;
   GstSDPMessage *sdp;
   GstSDPInfo info;
   const gchar *proto;
@@ -1270,13 +1386,13 @@ create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
   /* some standard things first */
   gst_sdp_message_set_version (sdp, "0");
 
-  if (client->is_ipv6)
+  if (priv->is_ipv6)
     proto = "IP6";
   else
     proto = "IP4";
 
   gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
-      client->server_ip);
+      priv->server_ip);
 
   gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
   gst_sdp_message_set_information (sdp, "rtsp-server");
@@ -1286,7 +1402,7 @@ create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
   gst_sdp_message_add_attribute (sdp, "control", "*");
 
   info.server_proto = proto;
-  info.server_ip = g_strdup (client->server_ip);
+  info.server_ip = g_strdup (priv->server_ip);
 
   /* create an SDP for the media object */
   if (!gst_rtsp_sdp_from_media (sdp, &info, media))
@@ -1299,6 +1415,7 @@ create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
   /* ERRORS */
 no_sdp:
   {
+    GST_ERROR ("client %p: could not create SDP", client);
     g_free (info.server_ip);
     gst_sdp_message_free (sdp);
     return NULL;
@@ -1385,11 +1502,13 @@ handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state)
   /* ERRORS */
 no_media:
   {
+    GST_ERROR ("client %p: no media", client);
     /* error reply is already sent */
     return FALSE;
   }
 no_sdp:
   {
+    GST_ERROR ("client %p: can't create SDP", client);
     send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
     g_object_unref (media);
     return FALSE;
@@ -1455,13 +1574,15 @@ sanitize_uri (GstRTSPUrl * uri)
 static void
 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
 {
+  GstRTSPClientPrivate *priv = client->priv;
+
   GST_INFO ("client %p: session %p finished", client, session);
 
   /* unlink all media managed in this session */
   client_unlink_session (client, session);
 
   /* remove the session */
-  if (!(client->sessions = g_list_remove (client->sessions, session))) {
+  if (!(priv->sessions = g_list_remove (priv->sessions, session))) {
     GST_INFO ("client %p: all sessions finalized, close the connection",
         client);
     close_connection (client);
@@ -1471,9 +1592,10 @@ client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
 static void
 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
 {
+  GstRTSPClientPrivate *priv = client->priv;
   GList *walk;
 
-  for (walk = client->sessions; walk; walk = g_list_next (walk)) {
+  for (walk = priv->sessions; walk; walk = g_list_next (walk)) {
     GstRTSPSession *msession = (GstRTSPSession *) walk->data;
 
     /* we already know about this session */
@@ -1485,7 +1607,7 @@ client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
 
   g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
       client);
-  client->sessions = g_list_prepend (client->sessions, session);
+  priv->sessions = g_list_prepend (priv->sessions, session);
 
   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
       session);
@@ -1494,12 +1616,13 @@ client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
 static void
 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
 {
+  GstRTSPClientPrivate *priv = client->priv;
   GstRTSPMethod method;
   const gchar *uristr;
-  GstRTSPUrl *uri;
+  GstRTSPUrl *uri = NULL;
   GstRTSPVersion version;
   GstRTSPResult res;
-  GstRTSPSession *session;
+  GstRTSPSession *session = NULL;
   GstRTSPClientState state = { NULL };
   GstRTSPMessage response = { 0 };
   gchar *sessid;
@@ -1515,45 +1638,39 @@ handle_request (GstRTSPClient * client, GstRTSPMessage * request)
 
   gst_rtsp_message_parse_request (request, &method, &uristr, &version);
 
-  if (version != GST_RTSP_VERSION_1_0) {
-    /* we can only handle 1.0 requests */
-    send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
-        &state);
-    return;
-  }
+  /* we can only handle 1.0 requests */
+  if (version != GST_RTSP_VERSION_1_0)
+    goto not_supported;
+
   state.method = method;
 
   /* we always try to parse the url first */
-  if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK) {
-    send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
-    return;
-  }
-
-  /* sanitize the uri */
-  sanitize_uri (uri);
-  state.uri = uri;
+  if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK)
+    goto bad_request;
 
   /* get the session if there is any */
   res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
   if (res == GST_RTSP_OK) {
-    if (client->session_pool == NULL)
+    if (priv->session_pool == NULL)
       goto no_pool;
 
     /* we had a session in the request, find it again */
-    if (!(session = gst_rtsp_session_pool_find (client->session_pool, sessid)))
+    if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
       goto session_not_found;
 
     /* we add the session to the client list of watched sessions. When a session
      * disappears because it times out, we will be notified. If all sessions are
      * gone, we will close the connection */
     client_watch_session (client, session);
-  } else
-    session = NULL;
+  }
 
+  /* sanitize the uri */
+  sanitize_uri (uri);
+  state.uri = uri;
   state.session = session;
 
-  if (client->auth) {
-    if (!gst_rtsp_auth_check (client->auth, client, 0, &state))
+  if (priv->auth) {
+    if (!gst_rtsp_auth_check (priv->auth, client, 0, &state))
       goto not_authorized;
   }
 
@@ -1586,40 +1703,63 @@ handle_request (GstRTSPClient * client, GstRTSPMessage * request)
     case GST_RTSP_ANNOUNCE:
     case GST_RTSP_RECORD:
     case GST_RTSP_REDIRECT:
-      send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &state);
-      break;
+      goto not_implemented;
     case GST_RTSP_INVALID:
     default:
-      send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
-      break;
+      goto bad_request;
   }
+
+done:
   if (session)
     g_object_unref (session);
-
-  gst_rtsp_url_free (uri);
+  if (uri)
+    gst_rtsp_url_free (uri);
   return;
 
   /* ERRORS */
+not_supported:
+  {
+    GST_ERROR ("client %p: version %d not supported", client, version);
+    send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
+        &state);
+    goto done;
+  }
+bad_request:
+  {
+    GST_ERROR ("client %p: bad request", client);
+    send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
+    goto done;
+  }
 no_pool:
   {
-    send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, &state);
-    return;
+    GST_ERROR ("client %p: no pool configured", client);
+    send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
+    goto done;
   }
 session_not_found:
   {
+    GST_ERROR ("client %p: session not found", client);
     send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
-    return;
+    goto done;
   }
 not_authorized:
   {
-    handle_unauthorized_request (client, client->auth, &state);
-    return;
+    GST_ERROR ("client %p: not allowed", client);
+    handle_unauthorized_request (client, priv->auth, &state);
+    goto done;
+  }
+not_implemented:
+  {
+    GST_ERROR ("client %p: method %d not implemented", client, method);
+    send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &state);
+    goto done;
   }
 }
 
 static void
 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
 {
+  GstRTSPClientPrivate *priv = client->priv;
   GstRTSPResult res;
   guint8 channel;
   GList *walk;
@@ -1638,16 +1778,15 @@ handle_data (GstRTSPClient * client, GstRTSPMessage * message)
   buffer = gst_buffer_new_wrapped (data, size);
 
   handled = FALSE;
-  for (walk = client->transports; walk; walk = g_list_next (walk)) {
+  for (walk = priv->transports; walk; walk = g_list_next (walk)) {
     GstRTSPStreamTransport *trans;
     GstRTSPStream *stream;
-    GstRTSPTransport *tr;
+    const GstRTSPTransport *tr;
 
     trans = walk->data;
 
-    /* we only add clients with a transport to the list */
-    tr = trans->transport;
-    stream = trans->stream;
+    tr = gst_rtsp_stream_transport_get_transport (trans);
+    stream = gst_rtsp_stream_transport_get_stream (trans);
 
     /* check for TCP transport */
     if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
@@ -1681,15 +1820,22 @@ gst_rtsp_client_set_session_pool (GstRTSPClient * client,
     GstRTSPSessionPool * pool)
 {
   GstRTSPSessionPool *old;
+  GstRTSPClientPrivate *priv;
 
-  old = client->session_pool;
-  if (old != pool) {
-    if (pool)
-      g_object_ref (pool);
-    client->session_pool = pool;
-    if (old)
-      g_object_unref (old);
-  }
+  g_return_if_fail (GST_IS_RTSP_CLIENT (client));
+
+  priv = client->priv;
+
+  if (pool)
+    g_object_ref (pool);
+
+  g_mutex_lock (&priv->lock);
+  old = priv->session_pool;
+  priv->session_pool = pool;
+  g_mutex_unlock (&priv->lock);
+
+  if (old)
+    g_object_unref (old);
 }
 
 /**
@@ -1703,51 +1849,17 @@ gst_rtsp_client_set_session_pool (GstRTSPClient * client,
 GstRTSPSessionPool *
 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
 {
+  GstRTSPClientPrivate *priv;
   GstRTSPSessionPool *result;
 
-  if ((result = client->session_pool))
-    g_object_ref (result);
-
-  return result;
-}
-
-/**
- * gst_rtsp_client_set_server:
- * @client: a #GstRTSPClient
- * @server: a #GstRTSPServer
- *
- * Set @server as the server that created @client.
- */
-void
-gst_rtsp_client_set_server (GstRTSPClient * client, GstRTSPServer * server)
-{
-  GstRTSPServer *old;
-
-  old = client->server;
-  if (old != server) {
-    if (server)
-      g_object_ref (server);
-    client->server = server;
-    if (old)
-      g_object_unref (old);
-  }
-}
+  g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
 
-/**
- * gst_rtsp_client_get_server:
- * @client: a #GstRTSPClient
- *
- * Get the #GstRTSPServer object that @client was created from.
- *
- * Returns: (transfer full): a #GstRTSPServer, unref after usage.
- */
-GstRTSPServer *
-gst_rtsp_client_get_server (GstRTSPClient * client)
-{
-  GstRTSPServer *result;
+  priv = client->priv;
 
-  if ((result = client->server))
+  g_mutex_lock (&priv->lock);
+  if ((result = priv->session_pool))
     g_object_ref (result);
+  g_mutex_unlock (&priv->lock);
 
   return result;
 }
@@ -1765,17 +1877,23 @@ void
 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
     GstRTSPMountPoints * mounts)
 {
+  GstRTSPClientPrivate *priv;
   GstRTSPMountPoints *old;
 
-  old = client->mount_points;
+  g_return_if_fail (GST_IS_RTSP_CLIENT (client));
 
-  if (old != mounts) {
-    if (mounts)
-      g_object_ref (mounts);
-    client->mount_points = mounts;
-    if (old)
-      g_object_unref (old);
-  }
+  priv = client->priv;
+
+  if (mounts)
+    g_object_ref (mounts);
+
+  g_mutex_lock (&priv->lock);
+  old = priv->mount_points;
+  priv->mount_points = mounts;
+  g_mutex_unlock (&priv->lock);
+
+  if (old)
+    g_object_unref (old);
 }
 
 /**
@@ -1789,10 +1907,17 @@ gst_rtsp_client_set_mount_points (GstRTSPClient * client,
 GstRTSPMountPoints *
 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
 {
+  GstRTSPClientPrivate *priv;
   GstRTSPMountPoints *result;
 
-  if ((result = client->mount_points))
+  g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
+
+  priv = client->priv;
+
+  g_mutex_lock (&priv->lock);
+  if ((result = priv->mount_points))
     g_object_ref (result);
+  g_mutex_unlock (&priv->lock);
 
   return result;
 }
@@ -1810,7 +1935,15 @@ void
 gst_rtsp_client_set_use_client_settings (GstRTSPClient * client,
     gboolean use_client_settings)
 {
-  client->use_client_settings = use_client_settings;
+  GstRTSPClientPrivate *priv;
+
+  g_return_if_fail (GST_IS_RTSP_CLIENT (client));
+
+  priv = client->priv;
+
+  g_mutex_lock (&priv->lock);
+  priv->use_client_settings = use_client_settings;
+  g_mutex_unlock (&priv->lock);
 }
 
 /**
@@ -1823,7 +1956,18 @@ gst_rtsp_client_set_use_client_settings (GstRTSPClient * client,
 gboolean
 gst_rtsp_client_get_use_client_settings (GstRTSPClient * client)
 {
-  return client->use_client_settings;
+  GstRTSPClientPrivate *priv;
+  gboolean res;
+
+  g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
+
+  priv = client->priv;
+
+  g_mutex_lock (&priv->lock);
+  res = priv->use_client_settings;
+  g_mutex_unlock (&priv->lock);
+
+  return res;
 }
 
 /**
@@ -1836,19 +1980,23 @@ gst_rtsp_client_get_use_client_settings (GstRTSPClient * client)
 void
 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
 {
+  GstRTSPClientPrivate *priv;
   GstRTSPAuth *old;
 
   g_return_if_fail (GST_IS_RTSP_CLIENT (client));
 
-  old = client->auth;
+  priv = client->priv;
 
-  if (old != auth) {
-    if (auth)
-      g_object_ref (auth);
-    client->auth = auth;
-    if (old)
-      g_object_unref (old);
-  }
+  if (auth)
+    g_object_ref (auth);
+
+  g_mutex_lock (&priv->lock);
+  old = priv->auth;
+  priv->auth = auth;
+  g_mutex_unlock (&priv->lock);
+
+  if (old)
+    g_object_unref (old);
 }
 
 
@@ -1864,21 +2012,115 @@ gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
 GstRTSPAuth *
 gst_rtsp_client_get_auth (GstRTSPClient * client)
 {
+  GstRTSPClientPrivate *priv;
   GstRTSPAuth *result;
 
   g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
 
-  if ((result = client->auth))
+  priv = client->priv;
+
+  g_mutex_lock (&priv->lock);
+  if ((result = priv->auth))
     g_object_ref (result);
+  g_mutex_unlock (&priv->lock);
 
   return result;
 }
 
-static GstRTSPResult
-message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
-    gpointer user_data)
+/**
+ * gst_rtsp_client_get_uri:
+ * @client: a #GstRTSPClient
+ *
+ * Get the #GstRTSPUrl of @client.
+ *
+ * Returns: (transfer full): the #GstRTSPUrl of @client. Free with
+ * gst_rtsp_url_free () after usage.
+ */
+GstRTSPUrl *
+gst_rtsp_client_get_uri (GstRTSPClient * client)
 {
-  GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
+  GstRTSPClientPrivate *priv;
+  GstRTSPUrl *result = NULL;
+
+  g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
+
+  priv = client->priv;
+
+  g_mutex_lock (&priv->lock);
+  if (priv->uri != NULL)
+    result = gst_rtsp_url_copy (priv->uri);
+  g_mutex_unlock (&priv->lock);
+
+  return result;
+}
+
+/**
+ * gst_rtsp_client_get_connection:
+ * @client: a #GstRTSPClient
+ *
+ * Get the #GstRTSPConnection of @client.
+ *
+ * Returns: (transfer none): the #GstRTSPConnection of @client.
+ * The connection object returned remains valid until the client is freed.
+ */
+GstRTSPConnection *
+gst_rtsp_client_get_connection (GstRTSPClient * client)
+{
+  g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
+
+  return client->priv->connection;
+}
+
+/**
+ * gst_rtsp_client_set_send_func:
+ * @client: a #GstRTSPClient
+ * @func: a #GstRTSPClientSendFunc
+ * @user_data: user data passed to @func
+ * @notify: called when @user_data is no longer in use
+ *
+ * Set @func as the callback that will be called when a new message needs to be
+ * sent to the client. @user_data is passed to @func and @notify is called when
+ * @user_data is no longer in use.
+ */
+void
+gst_rtsp_client_set_send_func (GstRTSPClient * client,
+    GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
+{
+  GstRTSPClientPrivate *priv;
+  GDestroyNotify old_notify;
+  gpointer old_data;
+
+  g_return_if_fail (GST_IS_RTSP_CLIENT (client));
+
+  priv = client->priv;
+
+  g_mutex_lock (&priv->send_lock);
+  priv->send_func = func;
+  old_notify = priv->send_notify;
+  old_data = priv->send_data;
+  priv->send_notify = notify;
+  priv->send_data = user_data;
+  g_mutex_unlock (&priv->send_lock);
+
+  if (old_notify)
+    old_notify (old_data);
+}
+
+/**
+ * gst_rtsp_client_handle_message:
+ * @client: a #GstRTSPClient
+ * @message: an #GstRTSPMessage
+ *
+ * Let the client handle @message.
+ *
+ * Returns: a #GstRTSPResult.
+ */
+GstRTSPResult
+gst_rtsp_client_handle_message (GstRTSPClient * client,
+    GstRTSPMessage * message)
+{
+  g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
+  g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
 
   switch (message->type) {
     case GST_RTSP_MESSAGE_REQUEST:
@@ -1896,13 +2138,32 @@ message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
 }
 
 static GstRTSPResult
+do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
+    gboolean close, gpointer user_data)
+{
+  GstRTSPClientPrivate *priv = client->priv;
+
+  /* send the response and store the seq number so we can wait until it's
+   * written to the client to close the connection */
+  return gst_rtsp_watch_send_message (priv->watch, message, close ?
+      &priv->close_seq : NULL);
+}
+
+static GstRTSPResult
+message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
+    gpointer user_data)
+{
+  return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
+}
+
+static GstRTSPResult
 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
 {
-  GstRTSPClient *client;
+  GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
+  GstRTSPClientPrivate *priv = client->priv;
 
-  client = GST_RTSP_CLIENT (user_data);
-  if (client->close_response_seq && client->close_response_seq == cseq) {
-    client->close_response_seq = 0;
+  if (priv->close_seq && priv->close_seq == cseq) {
+    priv->close_seq = 0;
     close_connection (client);
   }
 
@@ -1913,17 +2174,20 @@ static GstRTSPResult
 closed (GstRTSPWatch * watch, gpointer user_data)
 {
   GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
+  GstRTSPClientPrivate *priv = client->priv;
   const gchar *tunnelid;
 
   GST_INFO ("client %p: connection closed", client);
 
-  if ((tunnelid = gst_rtsp_connection_get_tunnelid (client->connection))) {
+  if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
     g_mutex_lock (&tunnels_lock);
     /* remove from tunnelids */
     g_hash_table_remove (tunnels, tunnelid);
     g_mutex_unlock (&tunnels_lock);
   }
 
+  gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
+
   return GST_RTSP_OK;
 }
 
@@ -1959,10 +2223,11 @@ error_full (GstRTSPWatch * watch, GstRTSPResult result,
 static gboolean
 remember_tunnel (GstRTSPClient * client)
 {
+  GstRTSPClientPrivate *priv = client->priv;
   const gchar *tunnelid;
 
   /* store client in the pending tunnels */
-  tunnelid = gst_rtsp_connection_get_tunnelid (client->connection);
+  tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
   if (tunnelid == NULL)
     goto no_tunnelid;
 
@@ -1996,12 +2261,11 @@ tunnel_existed:
 static GstRTSPStatusCode
 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
 {
-  GstRTSPClient *client;
-
-  client = GST_RTSP_CLIENT (user_data);
+  GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
+  GstRTSPClientPrivate *priv = client->priv;
 
   GST_INFO ("client %p: tunnel start (connection %p)", client,
-      client->connection);
+      priv->connection);
 
   if (!remember_tunnel (client))
     goto tunnel_error;
@@ -2019,12 +2283,11 @@ tunnel_error:
 static GstRTSPResult
 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
 {
-  GstRTSPClient *client;
-
-  client = GST_RTSP_CLIENT (user_data);
+  GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
+  GstRTSPClientPrivate *priv = client->priv;
 
-  GST_INFO ("client %p: tunnel lost (connection %p)", client,
-      client->connection);
+  GST_WARNING ("client %p: tunnel lost (connection %p)", client,
+      priv->connection);
 
   /* ignore error, it'll only be a problem when the client does a POST again */
   remember_tunnel (client);
@@ -2037,12 +2300,14 @@ tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
 {
   const gchar *tunnelid;
   GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
+  GstRTSPClientPrivate *priv = client->priv;
   GstRTSPClient *oclient;
+  GstRTSPClientPrivate *opriv;
 
   GST_INFO ("client %p: tunnel complete", client);
 
   /* find previous tunnel */
-  tunnelid = gst_rtsp_connection_get_tunnelid (client->connection);
+  tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
   if (tunnelid == NULL)
     goto no_tunnelid;
 
@@ -2055,16 +2320,18 @@ tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
   g_object_ref (oclient);
   g_hash_table_remove (tunnels, tunnelid);
 
-  if (oclient->watch == NULL)
+  opriv = oclient->priv;
+
+  if (opriv->watch == NULL)
     goto tunnel_closed;
   g_mutex_unlock (&tunnels_lock);
 
   GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
-      oclient->connection, client->connection);
+      opriv->connection, priv->connection);
 
   /* merge the tunnels into the first client */
-  gst_rtsp_connection_do_tunnel (oclient->connection, client->connection);
-  gst_rtsp_watch_reset (oclient->watch);
+  gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
+  gst_rtsp_watch_reset (opriv->watch);
   g_object_unref (oclient);
 
   return GST_RTSP_OK;
@@ -2072,19 +2339,19 @@ tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
   /* ERRORS */
 no_tunnelid:
   {
-    GST_INFO ("client %p: no tunnelid provided", client);
+    GST_ERROR ("client %p: no tunnelid provided", client);
     return GST_RTSP_ERROR;
   }
 no_tunnel:
   {
     g_mutex_unlock (&tunnels_lock);
-    GST_INFO ("client %p: tunnel session %s not found", client, tunnelid);
+    GST_ERROR ("client %p: tunnel session %s not found", client, tunnelid);
     return GST_RTSP_ERROR;
   }
 tunnel_closed:
   {
     g_mutex_unlock (&tunnels_lock);
-    GST_INFO ("client %p: tunnel session %s was closed", client, tunnelid);
+    GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
     g_object_unref (oclient);
     return GST_RTSP_ERROR;
   }
@@ -2104,8 +2371,10 @@ static GstRTSPWatchFuncs watch_funcs = {
 static void
 client_watch_notify (GstRTSPClient * client)
 {
+  GstRTSPClientPrivate *priv = client->priv;
+
   GST_INFO ("client %p: watch destroyed", client);
-  client->watch = NULL;
+  priv->watch = NULL;
   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
   g_object_unref (client);
 }
@@ -2114,36 +2383,37 @@ static gboolean
 setup_client (GstRTSPClient * client, GSocket * socket,
     GstRTSPConnection * conn, GError ** error)
 {
+  GstRTSPClientPrivate *priv = client->priv;
   GSocket *read_socket;
   GSocketAddress *address;
   GstRTSPUrl *url;
 
   read_socket = gst_rtsp_connection_get_read_socket (conn);
-  client->is_ipv6 = g_socket_get_family (socket) == G_SOCKET_FAMILY_IPV6;
+  priv->is_ipv6 = g_socket_get_family (socket) == G_SOCKET_FAMILY_IPV6;
 
   if (!(address = g_socket_get_remote_address (read_socket, error)))
     goto no_address;
 
-  g_free (client->server_ip);
+  g_free (priv->server_ip);
   /* keep the original ip that the client connected to */
   if (G_IS_INET_SOCKET_ADDRESS (address)) {
     GInetAddress *iaddr;
 
     iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
 
-    client->server_ip = g_inet_address_to_string (iaddr);
+    priv->server_ip = g_inet_address_to_string (iaddr);
     g_object_unref (address);
   } else {
-    client->server_ip = g_strdup ("unknown");
+    priv->server_ip = g_strdup ("unknown");
   }
 
   GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
-      client->server_ip, client->is_ipv6);
+      priv->server_ip, priv->is_ipv6);
 
   url = gst_rtsp_connection_get_url (conn);
   GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
 
-  client->connection = conn;
+  priv->connection = conn;
 
   return TRUE;
 
@@ -2176,6 +2446,9 @@ gst_rtsp_client_use_socket (GstRTSPClient * client, GSocket * socket,
   GstRTSPConnection *conn;
   GstRTSPResult res;
 
+  g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
+  g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
+
   GST_RTSP_CHECK (gst_rtsp_connection_create_from_socket (socket, ip, port,
           initial_buffer, &conn), no_connection);
 
@@ -2211,6 +2484,9 @@ gst_rtsp_client_accept (GstRTSPClient * client, GSocket * socket,
   GstRTSPConnection *conn;
   GstRTSPResult res;
 
+  g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
+  g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
+
   /* a new client connected. */
   GST_RTSP_CHECK (gst_rtsp_connection_accept (socket, &conn, cancellable),
       accept_failed);
@@ -2245,18 +2521,25 @@ accept_failed:
 guint
 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
 {
+  GstRTSPClientPrivate *priv;
   guint res;
 
   g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
-  g_return_val_if_fail (client->watch == NULL, 0);
+  priv = client->priv;
+  g_return_val_if_fail (priv->watch == NULL, 0);
 
   /* create watch for the connection and attach */
-  client->watch = gst_rtsp_watch_new (client->connection, &watch_funcs,
+  priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
       g_object_ref (client), (GDestroyNotify) client_watch_notify);
+  gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
+      (GDestroyNotify) gst_rtsp_watch_unref);
+
+  /* FIXME make this configurable. We don't want to do this yet because it will
+   * be superceeded by a cache object later */
+  gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
 
   GST_INFO ("attaching to context %p", context);
-  res = gst_rtsp_watch_attach (client->watch, context);
-  gst_rtsp_watch_unref (client->watch);
+  res = gst_rtsp_watch_attach (priv->watch, context);
 
   return res;
 }