client: add vmethod to configure media and streams
[platform/upstream/gstreamer.git] / gst / rtsp-server / rtsp-client.c
index c51c803..0822727 100644 (file)
@@ -107,6 +107,7 @@ enum
   SIGNAL_TEARDOWN_REQUEST,
   SIGNAL_SET_PARAMETER_REQUEST,
   SIGNAL_GET_PARAMETER_REQUEST,
+  SIGNAL_HANDLE_RESPONSE,
   SIGNAL_LAST
 };
 
@@ -126,12 +127,16 @@ static void client_session_finalized (GstRTSPClient * client,
     GstRTSPSession * session);
 static void unlink_session_transports (GstRTSPClient * client,
     GstRTSPSession * session, GstRTSPSessionMedia * sessmedia);
+static gboolean default_configure_client_media (GstRTSPClient * client,
+    GstRTSPMedia * media, GstRTSPStream * stream, GstRTSPContext * ctx);
 static gboolean default_configure_client_transport (GstRTSPClient * client,
-    GstRTSPClientState * state, GstRTSPTransport * ct);
+    GstRTSPContext * ctx, GstRTSPTransport * ct);
 static GstRTSPResult default_params_set (GstRTSPClient * client,
-    GstRTSPClientState * state);
+    GstRTSPContext * ctx);
 static GstRTSPResult default_params_get (GstRTSPClient * client,
-    GstRTSPClientState * state);
+    GstRTSPContext * ctx);
+static gchar *default_make_path_from_uri (GstRTSPClient * client,
+    const GstRTSPUrl * uri);
 
 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
 
@@ -149,9 +154,11 @@ gst_rtsp_client_class_init (GstRTSPClientClass * klass)
   gobject_class->finalize = gst_rtsp_client_finalize;
 
   klass->create_sdp = create_sdp;
+  klass->configure_client_media = default_configure_client_media;
   klass->configure_client_transport = default_configure_client_transport;
   klass->params_set = default_params_set;
   klass->params_get = default_params_get;
+  klass->make_path_from_uri = default_make_path_from_uri;
 
   g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
       g_param_spec_object ("session-pool", "Session Pool",
@@ -223,6 +230,12 @@ gst_rtsp_client_class_init (GstRTSPClientClass * klass)
           get_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
       G_TYPE_NONE, 1, G_TYPE_POINTER);
 
+  gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE] =
+      g_signal_new ("handle-response", G_TYPE_FROM_CLASS (klass),
+      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
+          handle_response), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
+      G_TYPE_NONE, 1, G_TYPE_POINTER);
+
   tunnels =
       g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
   g_mutex_init (&tunnels_lock);
@@ -341,6 +354,8 @@ gst_rtsp_client_finalize (GObject * obj)
     g_object_unref (priv->mount_points);
   if (priv->auth)
     g_object_unref (priv->auth);
+  if (priv->thread_pool)
+    g_object_unref (priv->thread_pool);
 
   if (priv->path)
     g_free (priv->path);
@@ -444,12 +459,12 @@ send_message (GstRTSPClient * client, GstRTSPSession * session,
 
 static void
 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
-    GstRTSPClientState * state)
+    GstRTSPContext * ctx)
 {
-  gst_rtsp_message_init_response (state->response, code,
-      gst_rtsp_status_as_text (code), state->request);
+  gst_rtsp_message_init_response (ctx->response, code,
+      gst_rtsp_status_as_text (code), ctx->request);
 
-  send_message (client, NULL, state->response, FALSE);
+  send_message (client, NULL, ctx->response, FALSE);
 }
 
 static gboolean
@@ -471,25 +486,20 @@ paths_are_equal (const gchar * path1, const gchar * path2, gint len2)
  * but is cached for when the same client (without breaking the connection) is
  * doing a setup for the exact same url. */
 static GstRTSPMedia *
-find_media (GstRTSPClient * client, GstRTSPClientState * state, gint * matched)
+find_media (GstRTSPClient * client, GstRTSPContext * ctx, gchar * path,
+    gint * matched)
 {
   GstRTSPClientPrivate *priv = client->priv;
   GstRTSPMediaFactory *factory;
   GstRTSPMedia *media;
-  gchar *path;
   gint path_len;
 
-  if (!priv->mount_points)
-    goto no_mount_points;
-
-  path = state->uri->abspath;
-
   /* find the longest matching factory for the uri first */
   if (!(factory = gst_rtsp_mount_points_match (priv->mount_points,
               path, matched)))
     goto no_factory;
 
-  state->factory = factory;
+  ctx->factory = factory;
 
   if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_ACCESS))
     goto no_factory_access;
@@ -517,13 +527,13 @@ find_media (GstRTSPClient * client, GstRTSPClientState * state, gint * matched)
     priv->media = NULL;
 
     /* prepare the media and add it to the pipeline */
-    if (!(media = gst_rtsp_media_factory_construct (factory, state->uri)))
+    if (!(media = gst_rtsp_media_factory_construct (factory, ctx->uri)))
       goto no_media;
 
-    state->media = media;
+    ctx->media = media;
 
     thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
-        GST_RTSP_THREAD_TYPE_MEDIA, state);
+        GST_RTSP_THREAD_TYPE_MEDIA, ctx);
     if (thread == NULL)
       goto no_thread;
 
@@ -537,12 +547,12 @@ find_media (GstRTSPClient * client, GstRTSPClientState * state, gint * matched)
   } else {
     /* we have seen this path before, used cached media */
     media = priv->media;
-    state->media = media;
+    ctx->media = media;
     GST_INFO ("reusing cached media %p for path %s", media, priv->path);
   }
 
   g_object_unref (factory);
-  state->factory = NULL;
+  ctx->factory = NULL;
 
   if (media)
     g_object_ref (media);
@@ -550,54 +560,51 @@ find_media (GstRTSPClient * client, GstRTSPClientState * state, gint * matched)
   return media;
 
   /* 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 %s", client, path);
-    send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
+    GST_ERROR ("client %p: no factory for path %s", client, path);
+    send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
     return NULL;
   }
 no_factory_access:
   {
-    GST_ERROR ("client %p: not authorized to see factory uri %s", client, path);
+    GST_ERROR ("client %p: not authorized to see factory path %s", client,
+        path);
+    /* error reply is already sent */
     return NULL;
   }
 not_authorized:
   {
-    GST_ERROR ("client %p: not authorized for factory uri %s", client, path);
+    GST_ERROR ("client %p: not authorized for factory path %s", client, path);
+    /* error reply is already sent */
     return NULL;
   }
 no_media:
   {
     GST_ERROR ("client %p: can't create media", client);
-    send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
+    send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
     g_object_unref (factory);
-    state->factory = NULL;
+    ctx->factory = NULL;
     return NULL;
   }
 no_thread:
   {
     GST_ERROR ("client %p: can't create thread", client);
-    send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
+    send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
     g_object_unref (media);
-    state->media = NULL;
+    ctx->media = NULL;
     g_object_unref (factory);
-    state->factory = NULL;
+    ctx->factory = NULL;
     return NULL;
   }
 no_prepare:
   {
     GST_ERROR ("client %p: can't prepare media", client);
-    send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
+    send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
     g_object_unref (media);
-    state->media = NULL;
+    ctx->media = NULL;
     g_object_unref (factory);
-    state->factory = NULL;
+    ctx->factory = NULL;
     return NULL;
   }
 }
@@ -710,25 +717,40 @@ close_connection (GstRTSPClient * client)
   gst_rtsp_connection_close (priv->connection);
 }
 
+static gchar *
+default_make_path_from_uri (GstRTSPClient * client, const GstRTSPUrl * uri)
+{
+  gchar *path;
+
+  if (uri->query)
+    path = g_strconcat (uri->abspath, "?", uri->query, NULL);
+  else
+    path = g_strdup (uri->abspath);
+
+  return path;
+}
+
 static gboolean
-handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
+handle_teardown_request (GstRTSPClient * client, GstRTSPContext * ctx)
 {
   GstRTSPClientPrivate *priv = client->priv;
+  GstRTSPClientClass *klass;
   GstRTSPSession *session;
   GstRTSPSessionMedia *sessmedia;
   GstRTSPStatusCode code;
-  const gchar *path;
+  gchar *path;
   gint matched;
 
-  if (!state->session)
+  if (!ctx->session)
     goto no_session;
 
-  session = state->session;
+  session = ctx->session;
 
-  if (!state->uri)
+  if (!ctx->uri)
     goto no_uri;
 
-  path = state->uri->abspath;
+  klass = GST_RTSP_CLIENT_GET_CLASS (client);
+  path = klass->make_path_from_uri (client, ctx->uri);
 
   /* get a handle to the configuration of the media in the session */
   sessmedia = gst_rtsp_session_get_media (session, path, &matched);
@@ -739,11 +761,13 @@ handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
   if (path[matched] != '\0')
     goto no_aggregate;
 
-  state->sessmedia = sessmedia;
+  g_free (path);
+
+  ctx->sessmedia = sessmedia;
 
   /* we emit the signal before closing the connection */
   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
-      0, state);
+      0, ctx);
 
   /* unlink the all TCP callbacks */
   unlink_session_transports (client, session, sessmedia);
@@ -761,10 +785,10 @@ handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
   }
   /* construct the response now */
   code = GST_RTSP_STS_OK;
-  gst_rtsp_message_init_response (state->response, code,
-      gst_rtsp_status_as_text (code), state->request);
+  gst_rtsp_message_init_response (ctx->response, code,
+      gst_rtsp_status_as_text (code), ctx->request);
 
-  send_message (client, session, state->response, TRUE);
+  send_message (client, session, ctx->response, TRUE);
 
   return TRUE;
 
@@ -772,75 +796,77 @@ handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
 no_session:
   {
     GST_ERROR ("client %p: no session", client);
-    send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
+    send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
     return FALSE;
   }
 no_uri:
   {
     GST_ERROR ("client %p: no uri supplied", client);
-    send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
+    send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
     return FALSE;
   }
 not_found:
   {
     GST_ERROR ("client %p: no media for uri", client);
-    send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
+    send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
+    g_free (path);
     return FALSE;
   }
 no_aggregate:
   {
     GST_ERROR ("client %p: no aggregate path %s", client, path);
     send_generic_response (client,
-        GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, state);
+        GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
+    g_free (path);
     return FALSE;
   }
 }
 
 static GstRTSPResult
-default_params_set (GstRTSPClient * client, GstRTSPClientState * state)
+default_params_set (GstRTSPClient * client, GstRTSPContext * ctx)
 {
   GstRTSPResult res;
 
-  res = gst_rtsp_params_set (client, state);
+  res = gst_rtsp_params_set (client, ctx);
 
   return res;
 }
 
 static GstRTSPResult
-default_params_get (GstRTSPClient * client, GstRTSPClientState * state)
+default_params_get (GstRTSPClient * client, GstRTSPContext * ctx)
 {
   GstRTSPResult res;
 
-  res = gst_rtsp_params_get (client, state);
+  res = gst_rtsp_params_get (client, ctx);
 
   return res;
 }
 
 static gboolean
-handle_get_param_request (GstRTSPClient * client, GstRTSPClientState * state)
+handle_get_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
 {
   GstRTSPResult res;
   guint8 *data;
   guint size;
 
-  res = gst_rtsp_message_get_body (state->request, &data, &size);
+  res = gst_rtsp_message_get_body (ctx->request, &data, &size);
   if (res != GST_RTSP_OK)
     goto bad_request;
 
   if (size == 0) {
     /* no body, keep-alive request */
-    send_generic_response (client, GST_RTSP_STS_OK, state);
+    send_generic_response (client, GST_RTSP_STS_OK, ctx);
   } else {
     /* there is a body, handle the params */
-    res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, state);
+    res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, ctx);
     if (res != GST_RTSP_OK)
       goto bad_request;
 
-    send_message (client, state->session, state->response, FALSE);
+    send_message (client, ctx->session, ctx->response, FALSE);
   }
 
   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
-      0, state);
+      0, ctx);
 
   return TRUE;
 
@@ -848,36 +874,36 @@ handle_get_param_request (GstRTSPClient * client, GstRTSPClientState * state)
 bad_request:
   {
     GST_ERROR ("client %p: bad request", client);
-    send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
+    send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
     return FALSE;
   }
 }
 
 static gboolean
-handle_set_param_request (GstRTSPClient * client, GstRTSPClientState * state)
+handle_set_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
 {
   GstRTSPResult res;
   guint8 *data;
   guint size;
 
-  res = gst_rtsp_message_get_body (state->request, &data, &size);
+  res = gst_rtsp_message_get_body (ctx->request, &data, &size);
   if (res != GST_RTSP_OK)
     goto bad_request;
 
   if (size == 0) {
     /* no body, keep-alive request */
-    send_generic_response (client, GST_RTSP_STS_OK, state);
+    send_generic_response (client, GST_RTSP_STS_OK, ctx);
   } else {
     /* there is a body, handle the params */
-    res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, state);
+    res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, ctx);
     if (res != GST_RTSP_OK)
       goto bad_request;
 
-    send_message (client, state->session, state->response, FALSE);
+    send_message (client, ctx->session, ctx->response, FALSE);
   }
 
   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
-      0, state);
+      0, ctx);
 
   return TRUE;
 
@@ -885,28 +911,30 @@ handle_set_param_request (GstRTSPClient * client, GstRTSPClientState * state)
 bad_request:
   {
     GST_ERROR ("client %p: bad request", client);
-    send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
+    send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
     return FALSE;
   }
 }
 
 static gboolean
-handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
+handle_pause_request (GstRTSPClient * client, GstRTSPContext * ctx)
 {
   GstRTSPSession *session;
+  GstRTSPClientClass *klass;
   GstRTSPSessionMedia *sessmedia;
   GstRTSPStatusCode code;
   GstRTSPState rtspstate;
-  const gchar *path;
+  gchar *path;
   gint matched;
 
-  if (!(session = state->session))
+  if (!(session = ctx->session))
     goto no_session;
 
-  if (!state->uri)
+  if (!ctx->uri)
     goto no_uri;
 
-  path = state->uri->abspath;
+  klass = GST_RTSP_CLIENT_GET_CLASS (client);
+  path = klass->make_path_from_uri (client, ctx->uri);
 
   /* get a handle to the configuration of the media in the session */
   sessmedia = gst_rtsp_session_get_media (session, path, &matched);
@@ -916,7 +944,9 @@ handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
   if (path[matched] != '\0')
     goto no_aggregate;
 
-  state->sessmedia = sessmedia;
+  g_free (path);
+
+  ctx->sessmedia = sessmedia;
 
   rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
   /* the session state must be playing or recording */
@@ -932,16 +962,15 @@ handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
 
   /* construct the response now */
   code = GST_RTSP_STS_OK;
-  gst_rtsp_message_init_response (state->response, code,
-      gst_rtsp_status_as_text (code), state->request);
+  gst_rtsp_message_init_response (ctx->response, code,
+      gst_rtsp_status_as_text (code), ctx->request);
 
-  send_message (client, session, state->response, FALSE);
+  send_message (client, session, ctx->response, FALSE);
 
   /* the state is now READY */
   gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
 
-  g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST],
-      0, state);
+  g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST], 0, ctx);
 
   return TRUE;
 
@@ -949,44 +978,70 @@ handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
 no_session:
   {
     GST_ERROR ("client %p: no seesion", client);
-    send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
+    send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
     return FALSE;
   }
 no_uri:
   {
     GST_ERROR ("client %p: no uri supplied", client);
-    send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
+    send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
     return FALSE;
   }
 not_found:
   {
     GST_ERROR ("client %p: no media for uri", client);
-    send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
+    send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
+    g_free (path);
     return FALSE;
   }
 no_aggregate:
   {
     GST_ERROR ("client %p: no aggregate path %s", client, path);
     send_generic_response (client,
-        GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, state);
+        GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
+    g_free (path);
     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);
+        ctx);
     return FALSE;
   }
 }
 
+/* convert @url and @path to a URL used as a content base for the factory
+ * located at @path */
+static gchar *
+make_base_url (GstRTSPClient * client, GstRTSPUrl * url, gchar * path)
+{
+  GstRTSPUrl tmp;
+  gchar *result, *trail;
+
+  /* check for trailing '/' and append one */
+  trail = (path[strlen (path) - 1] != '/' ? "/" : "");
+
+  tmp = *url;
+  tmp.user = NULL;
+  tmp.passwd = NULL;
+  tmp.abspath = g_strdup_printf ("%s%s", path, trail);
+  tmp.query = NULL;
+  result = gst_rtsp_url_get_request_uri (&tmp);
+  g_free (tmp.abspath);
+
+  return result;
+}
+
 static gboolean
-handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
+handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
 {
   GstRTSPSession *session;
+  GstRTSPClientClass *klass;
   GstRTSPSessionMedia *sessmedia;
   GstRTSPMedia *media;
   GstRTSPStatusCode code;
+  GstRTSPUrl *uri;
   GString *rtpinfo;
   guint n_streams, i, infocount;
   gchar *str;
@@ -994,16 +1049,17 @@ handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
   GstRTSPResult res;
   GstRTSPState rtspstate;
   GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
-  const gchar *path;
+  gchar *path;
   gint matched;
 
-  if (!(session = state->session))
+  if (!(session = ctx->session))
     goto no_session;
 
-  if (!state->uri)
+  if (!(uri = ctx->uri))
     goto no_uri;
 
-  path = state->uri->abspath;
+  klass = GST_RTSP_CLIENT_GET_CLASS (client);
+  path = klass->make_path_from_uri (client, uri);
 
   /* get a handle to the configuration of the media in the session */
   sessmedia = gst_rtsp_session_get_media (session, path, &matched);
@@ -1013,17 +1069,20 @@ handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
   if (path[matched] != '\0')
     goto no_aggregate;
 
-  state->sessmedia = sessmedia;
-  state->media = media = gst_rtsp_session_media_get_media (sessmedia);
+  ctx->sessmedia = sessmedia;
+  ctx->media = media = gst_rtsp_session_media_get_media (sessmedia);
 
   /* the session state must be playing or ready */
   rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
   if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
     goto invalid_state;
 
+  /* in play we first unsuspend, media could be suspended from SDP or PAUSED */
+  if (!gst_rtsp_media_unsuspend (media))
+    goto unsuspend_failed;
+
   /* parse the range header if we have one */
-  res =
-      gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_RANGE, &str, 0);
+  res = gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_RANGE, &str, 0);
   if (res == GST_RTSP_OK) {
     if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
       /* we have a range, seek to the position */
@@ -1041,7 +1100,6 @@ handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
     GstRTSPStreamTransport *trans;
     GstRTSPStream *stream;
     const GstRTSPTransport *tr;
-    gchar *uristr;
     guint rtptime, seq;
 
     /* get the transport, if there is no transport configured, skip this stream */
@@ -1059,46 +1117,51 @@ handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
 
     stream = gst_rtsp_stream_transport_get_stream (trans);
     if (gst_rtsp_stream_get_rtpinfo (stream, &rtptime, &seq)) {
+      const GstRTSPUrl *url;
+      gchar *url_str;
+
       if (infocount > 0)
         g_string_append (rtpinfo, ", ");
 
-      uristr = gst_rtsp_url_get_request_uri (state->uri);
-      g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u",
-          uristr, i, seq, rtptime);
-      g_free (uristr);
+      url = gst_rtsp_stream_transport_get_url (trans);
+      url_str = gst_rtsp_url_get_request_uri (url);
+      g_string_append_printf (rtpinfo, "url=%s;seq=%u;rtptime=%u",
+          url_str, seq, rtptime);
+      g_free (url_str);
 
       infocount++;
     } else {
       GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
     }
   }
+  g_free (path);
 
   /* construct the response now */
   code = GST_RTSP_STS_OK;
-  gst_rtsp_message_init_response (state->response, code,
-      gst_rtsp_status_as_text (code), state->request);
+  gst_rtsp_message_init_response (ctx->response, code,
+      gst_rtsp_status_as_text (code), ctx->request);
 
   /* add the RTP-Info header */
   if (infocount > 0) {
     str = g_string_free (rtpinfo, FALSE);
-    gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RTP_INFO, str);
+    gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RTP_INFO, str);
   } else {
     g_string_free (rtpinfo, TRUE);
   }
 
   /* add the range */
   str = gst_rtsp_media_get_range_string (media, TRUE, unit);
-  gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RANGE, str);
+  if (str)
+    gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RANGE, str);
 
-  send_message (client, session, state->response, FALSE);
+  send_message (client, session, ctx->response, FALSE);
 
   /* start playing after sending the request */
   gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
 
   gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
 
-  g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST],
-      0, state);
+  g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST], 0, ctx);
 
   return TRUE;
 
@@ -1106,33 +1169,42 @@ handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
 no_session:
   {
     GST_ERROR ("client %p: no session", client);
-    send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
+    send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
     return FALSE;
   }
 no_uri:
   {
     GST_ERROR ("client %p: no uri supplied", client);
-    send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
+    send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
     return FALSE;
   }
 not_found:
   {
     GST_ERROR ("client %p: media not found", client);
-    send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
+    send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
     return FALSE;
   }
 no_aggregate:
   {
     GST_ERROR ("client %p: no aggregate path %s", client, path);
     send_generic_response (client,
-        GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, state);
+        GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
+    g_free (path);
     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);
+        ctx);
+    g_free (path);
+    return FALSE;
+  }
+unsuspend_failed:
+  {
+    GST_ERROR ("client %p: unsuspend failed", client);
+    send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
+    g_free (path);
     return FALSE;
   }
 }
@@ -1196,11 +1268,11 @@ parse_transport (const char *transport, GstRTSPLowerTrans supported,
 }
 
 static gboolean
-handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
-    GstRTSPMessage * request)
+default_configure_client_media (GstRTSPClient * client, GstRTSPMedia * media,
+    GstRTSPStream * stream, GstRTSPContext * ctx)
 {
+  GstRTSPMessage *request = ctx->request;
   gchar *blocksize_str;
-  gboolean ret = TRUE;
 
   if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
           &blocksize_str, 0) == GST_RTSP_OK) {
@@ -1208,26 +1280,34 @@ handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
     gchar *end;
 
     blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
-    if (end == blocksize_str) {
-      GST_ERROR ("failed to parse blocksize");
-      ret = FALSE;
-    } else {
-      /* we don't want to change the mtu when this media
-       * can be shared because it impacts other clients */
-      if (gst_rtsp_media_is_shared (media))
-        return TRUE;
-
-      if (blocksize > G_MAXUINT)
-        blocksize = G_MAXUINT;
-      gst_rtsp_stream_set_mtu (stream, blocksize);
-    }
+    if (end == blocksize_str)
+      goto parse_failed;
+
+    /* we don't want to change the mtu when this media
+     * can be shared because it impacts other clients */
+    if (gst_rtsp_media_is_shared (media))
+      goto done;
+
+    if (blocksize > G_MAXUINT)
+      blocksize = G_MAXUINT;
+
+    gst_rtsp_stream_set_mtu (stream, blocksize);
+  }
+done:
+  return TRUE;
+
+  /* ERRORS */
+parse_failed:
+  {
+    GST_ERROR_OBJECT (client, "failed to parse blocksize");
+    send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
+    return FALSE;
   }
-  return ret;
 }
 
 static gboolean
 default_configure_client_transport (GstRTSPClient * client,
-    GstRTSPClientState * state, GstRTSPTransport * ct)
+    GstRTSPContext * ctx, GstRTSPTransport * ct)
 {
   GstRTSPClientPrivate *priv = client->priv;
 
@@ -1241,7 +1321,7 @@ default_configure_client_transport (GstRTSPClient * client,
     if (ct->destination && use_client_settings) {
       GstRTSPAddress *addr;
 
-      addr = gst_rtsp_stream_reserve_address (state->stream, ct->destination,
+      addr = gst_rtsp_stream_reserve_address (ctx->stream, ct->destination,
           ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
 
       if (addr == NULL)
@@ -1254,7 +1334,7 @@ default_configure_client_transport (GstRTSPClient * client,
 
       family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
 
-      addr = gst_rtsp_stream_get_multicast_address (state->stream, family);
+      addr = gst_rtsp_stream_get_multicast_address (ctx->stream, family);
       if (addr == NULL)
         goto no_address;
 
@@ -1276,7 +1356,7 @@ default_configure_client_transport (GstRTSPClient * client,
     if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
       /* check if the client selected channels for TCP */
       if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
-        gst_rtsp_session_media_alloc_channels (state->sessmedia,
+        gst_rtsp_session_media_alloc_channels (ctx->sessmedia,
             &ct->interleaved);
       }
     }
@@ -1292,7 +1372,7 @@ no_address:
 }
 
 static GstRTSPTransport *
-make_server_transport (GstRTSPClient * client, GstRTSPClientState * state,
+make_server_transport (GstRTSPClient * client, GstRTSPContext * ctx,
     GstRTSPTransport * ct)
 {
   GstRTSPTransport *st;
@@ -1320,7 +1400,7 @@ make_server_transport (GstRTSPClient * client, GstRTSPClientState * state,
   switch (st->lower_transport) {
     case GST_RTSP_LOWER_TRANS_UDP:
       st->client_port = ct->client_port;
-      gst_rtsp_stream_get_server_port (state->stream, &st->server_port, family);
+      gst_rtsp_stream_get_server_port (ctx->stream, &st->server_port, family);
       break;
     case GST_RTSP_LOWER_TRANS_UDP_MCAST:
       st->port = ct->port;
@@ -1333,13 +1413,13 @@ make_server_transport (GstRTSPClient * client, GstRTSPClientState * state,
       break;
   }
 
-  gst_rtsp_stream_get_ssrc (state->stream, &st->ssrc);
+  gst_rtsp_stream_get_ssrc (ctx->stream, &st->ssrc);
 
   return st;
 }
 
 static gboolean
-handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
+handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
 {
   GstRTSPClientPrivate *priv = client->priv;
   GstRTSPResult res;
@@ -1359,15 +1439,16 @@ handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
   gchar *path, *control;
   gint matched;
 
-  if (!state->uri)
+  if (!ctx->uri)
     goto no_uri;
 
-  uri = state->uri;
-  path = uri->abspath;
+  uri = ctx->uri;
+  klass = GST_RTSP_CLIENT_GET_CLASS (client);
+  path = klass->make_path_from_uri (client, uri);
 
   /* parse the transport */
   res =
-      gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_TRANSPORT,
+      gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_TRANSPORT,
       &transport, 0);
   if (res != GST_RTSP_OK)
     goto no_transport;
@@ -1377,7 +1458,7 @@ handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
   if (priv->session_pool == NULL)
     goto no_pool;
 
-  session = state->session;
+  session = ctx->session;
 
   if (session) {
     g_object_ref (session);
@@ -1392,16 +1473,21 @@ handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
   /* we have no session media, find one and manage it */
   if (sessmedia == NULL) {
     /* get a handle to the configuration of the media in the session */
-    media = find_media (client, state, &matched);
+    media = find_media (client, ctx, path, &matched);
   } else {
     if ((media = gst_rtsp_session_media_get_media (sessmedia)))
       g_object_ref (media);
+    else
+      goto media_not_found;
   }
   /* no media, not found then */
   if (media == NULL)
-    goto media_not_found;
+    goto media_not_found_no_reply;
+
+  if (path[matched] == '\0')
+    goto control_not_found;
 
-  /* path is what matched. We can modify the parsed uri in place */
+  /* path is what matched. */
   path[matched] = '\0';
   /* control is remainder */
   control = &path[matched + 1];
@@ -1412,8 +1498,8 @@ handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
     goto stream_not_found;
 
   /* now we have a uri identifying a valid media and stream */
-  state->stream = stream;
-  state->media = media;
+  ctx->stream = stream;
+  ctx->media = media;
 
   if (session == NULL) {
     /* create a session if this fails we probably reached our session limit or
@@ -1428,7 +1514,7 @@ handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
     g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
         session);
 
-    state->session = session;
+    ctx->session = session;
   }
 
   if (sessmedia == NULL) {
@@ -1441,49 +1527,50 @@ handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
     g_object_unref (media);
   }
 
-  state->sessmedia = sessmedia;
+  ctx->sessmedia = sessmedia;
 
-  /* set blocksize on this stream */
-  if (!handle_blocksize (media, stream, state->request))
-    goto invalid_blocksize;
+  if (!klass->configure_client_media (client, media, stream, ctx))
+    goto configure_media_failed_no_reply;
 
   gst_rtsp_transport_new (&ct);
 
   /* our supported transports */
-  supported = GST_RTSP_LOWER_TRANS_UDP |
-      GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP;
+  supported = gst_rtsp_stream_get_protocols (stream);
 
   /* parse and find a usable supported transport */
   if (!parse_transport (transport, supported, ct))
     goto unsupported_transports;
 
   /* update the client transport */
-  klass = GST_RTSP_CLIENT_GET_CLASS (client);
-  if (!klass->configure_client_transport (client, state, ct))
+  if (!klass->configure_client_transport (client, ctx, ct))
     goto unsupported_client_transport;
 
   /* set in the session media transport */
   trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
 
+  /* configure the url used to set this transport, this we will use when
+   * generating the response for the PLAY request */
+  gst_rtsp_stream_transport_set_url (trans, uri);
+
   /* configure keepalive for this transport */
   gst_rtsp_stream_transport_set_keepalive (trans,
       (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
 
   /* create and serialize the server transport */
-  st = make_server_transport (client, state, ct);
+  st = make_server_transport (client, ctx, ct);
   trans_str = gst_rtsp_transport_as_text (st);
   gst_rtsp_transport_free (st);
 
   /* construct the response now */
   code = GST_RTSP_STS_OK;
-  gst_rtsp_message_init_response (state->response, code,
-      gst_rtsp_status_as_text (code), state->request);
+  gst_rtsp_message_init_response (ctx->response, code,
+      gst_rtsp_status_as_text (code), ctx->request);
 
-  gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_TRANSPORT,
+  gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_TRANSPORT,
       trans_str);
   g_free (trans_str);
 
-  send_message (client, session, state->response, FALSE);
+  send_message (client, session, ctx->response, FALSE);
 
   /* update the state */
   rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
@@ -1498,9 +1585,9 @@ handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
       break;
   }
   g_object_unref (session);
+  g_free (path);
 
-  g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST],
-      0, state);
+  g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST], 0, ctx);
 
   return TRUE;
 
@@ -1508,70 +1595,94 @@ handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
 no_uri:
   {
     GST_ERROR ("client %p: no uri", client);
-    send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
+    send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
     return FALSE;
   }
 no_transport:
   {
     GST_ERROR ("client %p: no transport", client);
-    send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
+    send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
+    g_free (path);
     return FALSE;
   }
 no_pool:
   {
     GST_ERROR ("client %p: no session pool configured", client);
-    send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
+    send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
+    g_free (path);
+    return FALSE;
+  }
+media_not_found_no_reply:
+  {
+    GST_ERROR ("client %p: media '%s' not found", client, path);
+    g_free (path);
+    /* error reply is already sent */
     return FALSE;
   }
 media_not_found:
   {
     GST_ERROR ("client %p: media '%s' not found", client, path);
-    send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
+    send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
+    g_free (path);
+    return FALSE;
+  }
+control_not_found:
+  {
+    GST_ERROR ("client %p: no control in path '%s'", client, path);
+    send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
+    g_object_unref (media);
+    g_free (path);
     return FALSE;
   }
 stream_not_found:
   {
     GST_ERROR ("client %p: stream '%s' not found", client, control);
-    send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
+    send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
     g_object_unref (media);
+    g_free (path);
     return FALSE;
   }
 service_unavailable:
   {
     GST_ERROR ("client %p: can't create session", client);
-    send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
+    send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
     g_object_unref (media);
+    g_free (path);
     return FALSE;
   }
 sessmedia_unavailable:
   {
     GST_ERROR ("client %p: can't create session media", client);
-    send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
+    send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
     g_object_unref (media);
     g_object_unref (session);
+    g_free (path);
     return FALSE;
   }
-invalid_blocksize:
+configure_media_failed_no_reply:
   {
-    GST_ERROR ("client %p: invalid blocksize", client);
-    send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
+    GST_ERROR ("client %p: configure_media failed", client);
     g_object_unref (session);
+    g_free (path);
+    /* error reply is already sent */
     return FALSE;
   }
 unsupported_transports:
   {
     GST_ERROR ("client %p: unsupported transports", client);
-    send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
+    send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
     gst_rtsp_transport_free (ct);
     g_object_unref (session);
+    g_free (path);
     return FALSE;
   }
 unsupported_client_transport:
   {
     GST_ERROR ("client %p: unsupported client transport", client);
-    send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
+    send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
     gst_rtsp_transport_free (ct);
     g_object_unref (session);
+    g_free (path);
     return FALSE;
   }
 }
@@ -1624,18 +1735,19 @@ no_sdp:
 
 /* for the describe we must generate an SDP */
 static gboolean
-handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state)
+handle_describe_request (GstRTSPClient * client, GstRTSPContext * ctx)
 {
+  GstRTSPClientPrivate *priv = client->priv;
   GstRTSPResult res;
   GstSDPMessage *sdp;
-  guint i, str_len;
-  gchar *str, *content_base;
+  guint i;
+  gchar *path, *str;
   GstRTSPMedia *media;
   GstRTSPClientClass *klass;
 
   klass = GST_RTSP_CLIENT_GET_CLASS (client);
 
-  if (!state->uri)
+  if (!ctx->uri)
     goto no_uri;
 
   /* check what kind of format is accepted, we don't really do anything with it
@@ -1644,7 +1756,7 @@ handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state)
     gchar *accept;
 
     res =
-        gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_ACCEPT,
+        gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_ACCEPT,
         &accept, i);
     if (res == GST_RTSP_ENOTIMPL)
       break;
@@ -1653,52 +1765,46 @@ handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state)
       break;
   }
 
+  if (!priv->mount_points)
+    goto no_mount_points;
+
+  if (!(path = gst_rtsp_mount_points_make_path (priv->mount_points, ctx->uri)))
+    goto no_path;
+
   /* find the media object for the uri */
-  if (!(media = find_media (client, state, NULL)))
+  if (!(media = find_media (client, ctx, path, NULL)))
     goto no_media;
 
   /* create an SDP for the media object on this client */
   if (!(sdp = klass->create_sdp (client, media)))
     goto no_sdp;
 
+  /* we suspend after the describe */
+  gst_rtsp_media_suspend (media);
   g_object_unref (media);
 
-  gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
-      gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
+  gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
+      gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
 
-  gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_TYPE,
+  gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_TYPE,
       "application/sdp");
 
   /* content base for some clients that might screw up creating the setup uri */
-  str = gst_rtsp_url_get_request_uri (state->uri);
-  str_len = strlen (str);
-
-  /* check for trailing '/' and append one */
-  if (str[str_len - 1] != '/') {
-    content_base = g_malloc (str_len + 2);
-    memcpy (content_base, str, str_len);
-    content_base[str_len] = '/';
-    content_base[str_len + 1] = '\0';
-    g_free (str);
-  } else {
-    content_base = str;
-  }
+  str = make_base_url (client, ctx->uri, path);
+  g_free (path);
 
-  GST_INFO ("adding content-base: %s", content_base);
-
-  gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_BASE,
-      content_base);
-  g_free (content_base);
+  GST_INFO ("adding content-base: %s", str);
+  gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_CONTENT_BASE, str);
 
   /* add SDP to the response body */
   str = gst_sdp_message_as_text (sdp);
-  gst_rtsp_message_take_body (state->response, (guint8 *) str, strlen (str));
+  gst_rtsp_message_take_body (ctx->response, (guint8 *) str, strlen (str));
   gst_sdp_message_free (sdp);
 
-  send_message (client, state->session, state->response, FALSE);
+  send_message (client, ctx->session, ctx->response, FALSE);
 
   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
-      0, state);
+      0, ctx);
 
   return TRUE;
 
@@ -1706,26 +1812,40 @@ handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state)
 no_uri:
   {
     GST_ERROR ("client %p: no uri", client);
-    send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
+    send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
+    return FALSE;
+  }
+no_mount_points:
+  {
+    GST_ERROR ("client %p: no mount points configured", client);
+    send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
+    return FALSE;
+  }
+no_path:
+  {
+    GST_ERROR ("client %p: can't find path for url", client);
+    send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
     return FALSE;
   }
 no_media:
   {
     GST_ERROR ("client %p: no media", client);
+    g_free (path);
     /* 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);
+    send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
+    g_free (path);
     g_object_unref (media);
     return FALSE;
   }
 }
 
 static gboolean
-handle_options_request (GstRTSPClient * client, GstRTSPClientState * state)
+handle_options_request (GstRTSPClient * client, GstRTSPContext * ctx)
 {
   GstRTSPMethod options;
   gchar *str;
@@ -1739,16 +1859,16 @@ handle_options_request (GstRTSPClient * client, GstRTSPClientState * state)
 
   str = gst_rtsp_options_as_text (options);
 
-  gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
-      gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
+  gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
+      gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
 
-  gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_PUBLIC, str);
+  gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_PUBLIC, str);
   g_free (str);
 
-  send_message (client, state->session, state->response, FALSE);
+  send_message (client, ctx->session, ctx->response, FALSE);
 
   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
-      0, state);
+      0, ctx);
 
   return TRUE;
 }
@@ -1798,69 +1918,6 @@ client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
   }
 }
 
-static GPrivate current_state;
-
-/**
- * gst_rtsp_client_state_get_current:
- *
- * Get the current #GstRTSPClientState. This object is retrieved from the
- * current thread that is handling the request for a client.
- *
- * Returns: a #GstRTSPClientState
- */
-GstRTSPClientState *
-gst_rtsp_client_state_get_current (void)
-{
-  GSList *l;
-
-  l = g_private_get (&current_state);
-  if (l == NULL)
-    return NULL;
-
-  return (GstRTSPClientState *) (l->data);
-
-}
-
-/**
- * gst_rtsp_client_state_push_current:
- * @state: a ##GstRTSPClientState
- *
- * Pushes @state onto the state stack. The current
- * state can then be received using gst_rtsp_client_state_get_current().
- **/
-void
-gst_rtsp_client_state_push_current (GstRTSPClientState * state)
-{
-  GSList *l;
-
-  g_return_if_fail (state != NULL);
-
-  l = g_private_get (&current_state);
-  l = g_slist_prepend (l, state);
-  g_private_set (&current_state, l);
-}
-
-/**
- * gst_rtsp_client_state_pop_current:
- * @state: a #GstRTSPClientState
- *
- * Pops @state off the state stack (verifying that @state
- * is on the top of the stack).
- **/
-void
-gst_rtsp_client_state_pop_current (GstRTSPClientState * state)
-{
-  GSList *l;
-
-  l = g_private_get (&current_state);
-
-  g_return_if_fail (l != NULL);
-  g_return_if_fail (l->data == state);
-
-  l = g_slist_delete_link (l, l);
-  g_private_set (&current_state, l);
-}
-
 static void
 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
 {
@@ -1871,36 +1928,69 @@ handle_request (GstRTSPClient * client, GstRTSPMessage * request)
   GstRTSPVersion version;
   GstRTSPResult res;
   GstRTSPSession *session = NULL;
-  GstRTSPClientState state = { NULL };
+  GstRTSPContext sctx = { NULL }, *ctx;
   GstRTSPMessage response = { 0 };
   gchar *sessid;
 
-  state.conn = priv->connection;
-  state.client = client;
-  state.request = request;
-  state.response = &response;
-  state.auth = priv->auth;
-  gst_rtsp_client_state_push_current (&state);
+  if (!(ctx = gst_rtsp_context_get_current ())) {
+    ctx = &sctx;
+    ctx->auth = priv->auth;
+    gst_rtsp_context_push_current (ctx);
+  }
+
+  ctx->conn = priv->connection;
+  ctx->client = client;
+  ctx->request = request;
+  ctx->response = &response;
 
   if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
     gst_rtsp_message_dump (request);
   }
 
-  GST_INFO ("client %p: received a request", client);
-
   gst_rtsp_message_parse_request (request, &method, &uristr, &version);
 
+  GST_INFO ("client %p: received a request %s %s %s", client,
+      gst_rtsp_method_as_text (method), uristr,
+      gst_rtsp_version_as_text (version));
+
   /* we can only handle 1.0 requests */
   if (version != GST_RTSP_VERSION_1_0)
     goto not_supported;
 
-  state.method = method;
+  ctx->method = method;
 
   /* we always try to parse the url first */
   if (strcmp (uristr, "*") == 0) {
     /* special case where we have * as uri, keep uri = NULL */
-  } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK)
-    goto bad_request;
+  } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK) {
+    /* check if the uristr is an absolute path <=> scheme and host information
+     * is missing */
+    gchar *scheme;
+
+    scheme = g_uri_parse_scheme (uristr);
+    if (scheme == NULL && g_str_has_prefix (uristr, "/")) {
+      gchar *absolute_uristr = NULL;
+
+      GST_WARNING_OBJECT (client, "request doesn't contain absolute url");
+      if (priv->server_ip == NULL) {
+        GST_WARNING_OBJECT (client, "host information missing");
+        goto bad_request;
+      }
+
+      absolute_uristr =
+          g_strdup_printf ("rtsp://%s%s", priv->server_ip, uristr);
+
+      GST_DEBUG_OBJECT (client, "absolute url: %s", absolute_uristr);
+      if (gst_rtsp_url_parse (absolute_uristr, &uri) != GST_RTSP_OK) {
+        g_free (absolute_uristr);
+        goto bad_request;
+      }
+      g_free (absolute_uristr);
+    } else {
+      g_free (scheme);
+      goto bad_request;
+    }
+  }
 
   /* get the session if there is any */
   res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
@@ -1921,8 +2011,8 @@ handle_request (GstRTSPClient * client, GstRTSPMessage * request)
   /* sanitize the uri */
   if (uri)
     sanitize_uri (uri);
-  state.uri = uri;
-  state.session = session;
+  ctx->uri = uri;
+  ctx->session = session;
 
   if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
     goto not_authorized;
@@ -1930,28 +2020,28 @@ handle_request (GstRTSPClient * client, GstRTSPMessage * request)
   /* now see what is asked and dispatch to a dedicated handler */
   switch (method) {
     case GST_RTSP_OPTIONS:
-      handle_options_request (client, &state);
+      handle_options_request (client, ctx);
       break;
     case GST_RTSP_DESCRIBE:
-      handle_describe_request (client, &state);
+      handle_describe_request (client, ctx);
       break;
     case GST_RTSP_SETUP:
-      handle_setup_request (client, &state);
+      handle_setup_request (client, ctx);
       break;
     case GST_RTSP_PLAY:
-      handle_play_request (client, &state);
+      handle_play_request (client, ctx);
       break;
     case GST_RTSP_PAUSE:
-      handle_pause_request (client, &state);
+      handle_pause_request (client, ctx);
       break;
     case GST_RTSP_TEARDOWN:
-      handle_teardown_request (client, &state);
+      handle_teardown_request (client, ctx);
       break;
     case GST_RTSP_SET_PARAMETER:
-      handle_set_param_request (client, &state);
+      handle_set_param_request (client, ctx);
       break;
     case GST_RTSP_GET_PARAMETER:
-      handle_get_param_request (client, &state);
+      handle_get_param_request (client, ctx);
       break;
     case GST_RTSP_ANNOUNCE:
     case GST_RTSP_RECORD:
@@ -1963,7 +2053,8 @@ handle_request (GstRTSPClient * client, GstRTSPMessage * request)
   }
 
 done:
-  gst_rtsp_client_state_pop_current (&state);
+  if (ctx == &sctx)
+    gst_rtsp_context_pop_current (ctx);
   if (session)
     g_object_unref (session);
   if (uri)
@@ -1975,36 +2066,107 @@ not_supported:
   {
     GST_ERROR ("client %p: version %d not supported", client, version);
     send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
-        &state);
+        ctx);
     goto done;
   }
 bad_request:
   {
     GST_ERROR ("client %p: bad request", client);
-    send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
+    send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
     goto done;
   }
 no_pool:
   {
     GST_ERROR ("client %p: no pool configured", client);
-    send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
+    send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
     goto done;
   }
 session_not_found:
   {
     GST_ERROR ("client %p: session not found", client);
-    send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
+    send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
     goto done;
   }
 not_authorized:
   {
     GST_ERROR ("client %p: not allowed", client);
+    /* error reply is already sent */
     goto done;
   }
 not_implemented:
   {
     GST_ERROR ("client %p: method %d not implemented", client, method);
-    send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &state);
+    send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, ctx);
+    goto done;
+  }
+}
+
+
+static void
+handle_response (GstRTSPClient * client, GstRTSPMessage * response)
+{
+  GstRTSPClientPrivate *priv = client->priv;
+  GstRTSPResult res;
+  GstRTSPSession *session = NULL;
+  GstRTSPContext sctx = { NULL }, *ctx;
+  gchar *sessid;
+
+  if (!(ctx = gst_rtsp_context_get_current ())) {
+    ctx = &sctx;
+    ctx->auth = priv->auth;
+    gst_rtsp_context_push_current (ctx);
+  }
+
+  ctx->conn = priv->connection;
+  ctx->client = client;
+  ctx->request = NULL;
+  ctx->uri = NULL;
+  ctx->method = GST_RTSP_INVALID;
+  ctx->response = response;
+
+  if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
+    gst_rtsp_message_dump (response);
+  }
+
+  GST_INFO ("client %p: received a response", client);
+
+  /* get the session if there is any */
+  res =
+      gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION, &sessid, 0);
+  if (res == GST_RTSP_OK) {
+    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 (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);
+  }
+
+  ctx->session = session;
+
+  g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE],
+      0, ctx);
+
+done:
+  if (ctx == &sctx)
+    gst_rtsp_context_pop_current (ctx);
+  if (session)
+    g_object_unref (session);
+  return;
+
+no_pool:
+  {
+    GST_ERROR ("client %p: no pool configured", client);
+    goto done;
+  }
+session_not_found:
+  {
+    GST_ERROR ("client %p: session not found", client);
     goto done;
   }
 }
@@ -2430,6 +2592,7 @@ gst_rtsp_client_handle_message (GstRTSPClient * client,
       handle_request (client, message);
       break;
     case GST_RTSP_MESSAGE_RESPONSE:
+      handle_response (client, message);
       break;
     case GST_RTSP_MESSAGE_DATA:
       handle_data (client, message);
@@ -2711,7 +2874,7 @@ client_watch_notify (GstRTSPClient * client)
  * @context: (allow-none): a #GMainContext
  *
  * Attaches @client to @context. When the mainloop for @context is run, the
- * client will be dispatched. When @context is NULL, the default context will be
+ * client will be dispatched. When @context is %NULL, the default context will be
  * used).
  *
  * This function should be called when the client properties and urls are fully
@@ -2748,8 +2911,8 @@ gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
 
 /**
  * gst_rtsp_client_session_filter:
- * @client: a #GstRTSPclient
- * @func: (scope call): a callback
+ * @client: a #GstRTSPClient
+ * @func: (scope call) (allow-none): a callback
  * @user_data: user data passed to @func
  *
  * Call @func for each session managed by @client. The result value of @func
@@ -2765,6 +2928,8 @@ gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
  * will also be added with an additional ref to the result #GList of this
  * function..
  *
+ * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each session.
+ *
  * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
  * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
  * element in the #GList should be unreffed before the list is freed.
@@ -2777,7 +2942,6 @@ gst_rtsp_client_session_filter (GstRTSPClient * client,
   GList *result, *walk, *next;
 
   g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
-  g_return_val_if_fail (func != NULL, NULL);
 
   priv = client->priv;
 
@@ -2786,10 +2950,16 @@ gst_rtsp_client_session_filter (GstRTSPClient * client,
   g_mutex_lock (&priv->lock);
   for (walk = priv->sessions; walk; walk = next) {
     GstRTSPSession *sess = walk->data;
+    GstRTSPFilterResult res;
 
     next = g_list_next (walk);
 
-    switch (func (client, sess, user_data)) {
+    if (func)
+      res = func (client, sess, user_data);
+    else
+      res = GST_RTSP_FILTER_REF;
+
+    switch (res) {
       case GST_RTSP_FILTER_REMOVE:
         /* stop watching the session and pretent it went away */
         client_cleanup_session (client, sess);