rtsp-client: No flush during Teardown.
[platform/upstream/gstreamer.git] / gst / rtsp-server / rtsp-client.c
index 4f1c3c9..c41d019 100644 (file)
@@ -1,5 +1,7 @@
 /* GStreamer
  * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
+ * Copyright (C) 2015 Centricular Ltd
+ *     Author: Sebastian Dröge <sebastian@centricular.com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -42,6 +44,8 @@
 #include <stdio.h>
 #include <string.h>
 
+#include <gst/sdp/gstmikey.h>
+
 #include "rtsp-client.h"
 #include "rtsp-sdp.h"
 #include "rtsp-params.h"
@@ -57,8 +61,10 @@ struct _GstRTSPClientPrivate
 {
   GMutex lock;                  /* protects everything else */
   GMutex send_lock;
+  GMutex watch_lock;
   GstRTSPConnection *connection;
   GstRTSPWatch *watch;
+  GMainContext *watch_context;
   guint close_seq;
   gchar *server_ip;
   gboolean is_ipv6;
@@ -68,6 +74,7 @@ struct _GstRTSPClientPrivate
   GDestroyNotify send_notify;   /* protected by send_lock */
 
   GstRTSPSessionPool *session_pool;
+  gulong session_removed_id;
   GstRTSPMountPoints *mount_points;
   GstRTSPAuth *auth;
   GstRTSPThreadPool *thread_pool;
@@ -77,21 +84,30 @@ struct _GstRTSPClientPrivate
   gchar *path;
   GstRTSPMedia *media;
 
-  GList *transports;
+  GHashTable *transports;
   GList *sessions;
+  guint sessions_cookie;
+
+  gboolean drop_backlog;
 };
 
 static GMutex tunnels_lock;
 static GHashTable *tunnels;     /* protected by tunnels_lock */
 
+/* FIXME make this configurable. We don't want to do this yet because it will
+ * be superceeded by a cache object later */
+#define WATCH_BACKLOG_SIZE              100
+
 #define DEFAULT_SESSION_POOL            NULL
 #define DEFAULT_MOUNT_POINTS            NULL
+#define DEFAULT_DROP_BACKLOG            TRUE
 
 enum
 {
   PROP_0,
   PROP_SESSION_POOL,
   PROP_MOUNT_POINTS,
+  PROP_DROP_BACKLOG,
   PROP_LAST
 };
 
@@ -108,6 +124,9 @@ enum
   SIGNAL_SET_PARAMETER_REQUEST,
   SIGNAL_GET_PARAMETER_REQUEST,
   SIGNAL_HANDLE_RESPONSE,
+  SIGNAL_SEND_MESSAGE,
+  SIGNAL_ANNOUNCE_REQUEST,
+  SIGNAL_RECORD_REQUEST,
   SIGNAL_LAST
 };
 
@@ -123,16 +142,20 @@ static void gst_rtsp_client_set_property (GObject * object, guint propid,
 static void gst_rtsp_client_finalize (GObject * obj);
 
 static GstSDPMessage *create_sdp (GstRTSPClient * client, GstRTSPMedia * media);
-static void client_session_finalized (GstRTSPClient * client,
-    GstRTSPSession * session);
-static void unlink_session_transports (GstRTSPClient * client,
-    GstRTSPSession * session, GstRTSPSessionMedia * sessmedia);
+static gboolean handle_sdp (GstRTSPClient * client, GstRTSPContext * ctx,
+    GstRTSPMedia * media, GstSDPMessage * sdp);
+static gboolean default_configure_client_media (GstRTSPClient * client,
+    GstRTSPMedia * media, GstRTSPStream * stream, GstRTSPContext * ctx);
 static gboolean default_configure_client_transport (GstRTSPClient * client,
     GstRTSPContext * ctx, GstRTSPTransport * ct);
 static GstRTSPResult default_params_set (GstRTSPClient * client,
     GstRTSPContext * ctx);
 static GstRTSPResult default_params_get (GstRTSPClient * client,
     GstRTSPContext * ctx);
+static gchar *default_make_path_from_uri (GstRTSPClient * client,
+    const GstRTSPUrl * uri);
+static void client_session_removed (GstRTSPSessionPool * pool,
+    GstRTSPSession * session, GstRTSPClient * client);
 
 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
 
@@ -150,9 +173,12 @@ gst_rtsp_client_class_init (GstRTSPClientClass * klass)
   gobject_class->finalize = gst_rtsp_client_finalize;
 
   klass->create_sdp = create_sdp;
+  klass->handle_sdp = handle_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",
@@ -166,69 +192,98 @@ gst_rtsp_client_class_init (GstRTSPClientClass * klass)
           GST_TYPE_RTSP_MOUNT_POINTS,
           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
+  g_object_class_install_property (gobject_class, PROP_DROP_BACKLOG,
+      g_param_spec_boolean ("drop-backlog", "Drop Backlog",
+          "Drop data when the backlog queue is full",
+          DEFAULT_DROP_BACKLOG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
   gst_rtsp_client_signals[SIGNAL_CLOSED] =
       g_signal_new ("closed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
       G_STRUCT_OFFSET (GstRTSPClientClass, closed), NULL, NULL,
-      g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
+      g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
 
   gst_rtsp_client_signals[SIGNAL_NEW_SESSION] =
       g_signal_new ("new-session", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
       G_STRUCT_OFFSET (GstRTSPClientClass, new_session), NULL, NULL,
-      g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_RTSP_SESSION);
+      g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_RTSP_SESSION);
 
   gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST] =
       g_signal_new ("options-request", G_TYPE_FROM_CLASS (klass),
       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, options_request),
-      NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
-      G_TYPE_POINTER);
+      NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
+      GST_TYPE_RTSP_CONTEXT);
 
   gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST] =
       g_signal_new ("describe-request", G_TYPE_FROM_CLASS (klass),
       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, describe_request),
-      NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
-      G_TYPE_POINTER);
+      NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
+      GST_TYPE_RTSP_CONTEXT);
 
   gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST] =
       g_signal_new ("setup-request", G_TYPE_FROM_CLASS (klass),
       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, setup_request),
-      NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
-      G_TYPE_POINTER);
+      NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
+      GST_TYPE_RTSP_CONTEXT);
 
   gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST] =
       g_signal_new ("play-request", G_TYPE_FROM_CLASS (klass),
       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, play_request),
-      NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
-      G_TYPE_POINTER);
+      NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
+      GST_TYPE_RTSP_CONTEXT);
 
   gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST] =
       g_signal_new ("pause-request", G_TYPE_FROM_CLASS (klass),
       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, pause_request),
-      NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
-      G_TYPE_POINTER);
+      NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
+      GST_TYPE_RTSP_CONTEXT);
 
   gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST] =
       g_signal_new ("teardown-request", G_TYPE_FROM_CLASS (klass),
       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, teardown_request),
-      NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
-      G_TYPE_POINTER);
+      NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
+      GST_TYPE_RTSP_CONTEXT);
 
   gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST] =
       g_signal_new ("set-parameter-request", G_TYPE_FROM_CLASS (klass),
       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
-          set_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
-      G_TYPE_NONE, 1, G_TYPE_POINTER);
+          set_parameter_request), NULL, NULL, g_cclosure_marshal_generic,
+      G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
 
   gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST] =
       g_signal_new ("get-parameter-request", G_TYPE_FROM_CLASS (klass),
       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
-          get_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
-      G_TYPE_NONE, 1, G_TYPE_POINTER);
+          get_parameter_request), NULL, NULL, g_cclosure_marshal_generic,
+      G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
 
   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);
+          handle_response), NULL, NULL, g_cclosure_marshal_generic,
+      G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
+
+  /**
+   * GstRTSPClient::send-message:
+   * @client: The RTSP client
+   * @session: (type GstRtspServer.RTSPSession): The session
+   * @message: (type GstRtsp.RTSPMessage): The message
+   */
+  gst_rtsp_client_signals[SIGNAL_SEND_MESSAGE] =
+      g_signal_new ("send-message", G_TYPE_FROM_CLASS (klass),
+      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
+          send_message), NULL, NULL, g_cclosure_marshal_generic,
+      G_TYPE_NONE, 2, GST_TYPE_RTSP_CONTEXT, G_TYPE_POINTER);
+
+  gst_rtsp_client_signals[SIGNAL_ANNOUNCE_REQUEST] =
+      g_signal_new ("announce-request", G_TYPE_FROM_CLASS (klass),
+      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, announce_request),
+      NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
+      GST_TYPE_RTSP_CONTEXT);
+
+  gst_rtsp_client_signals[SIGNAL_RECORD_REQUEST] =
+      g_signal_new ("record-request", G_TYPE_FROM_CLASS (klass),
+      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, record_request),
+      NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
+      GST_TYPE_RTSP_CONTEXT);
 
   tunnels =
       g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
@@ -246,82 +301,103 @@ gst_rtsp_client_init (GstRTSPClient * client)
 
   g_mutex_init (&priv->lock);
   g_mutex_init (&priv->send_lock);
+  g_mutex_init (&priv->watch_lock);
   priv->close_seq = 0;
+  priv->drop_backlog = DEFAULT_DROP_BACKLOG;
+  priv->transports =
+      g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL,
+      g_object_unref);
 }
 
 static GstRTSPFilterResult
-filter_session (GstRTSPSession * sess, GstRTSPSessionMedia * sessmedia,
+filter_session_media (GstRTSPSession * sess, GstRTSPSessionMedia * sessmedia,
     gpointer user_data)
 {
-  GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
-
   gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
-  unlink_session_transports (client, sess, sessmedia);
 
-  /* 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 */
-  gst_rtsp_session_filter (session, filter_session, client);
-}
-
-static void
 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
 {
   GstRTSPClientPrivate *priv = client->priv;
-  GList *walk;
 
-  for (walk = priv->sessions; walk; walk = g_list_next (walk)) {
-    GstRTSPSession *msession = (GstRTSPSession *) walk->data;
+  g_mutex_lock (&priv->lock);
+  /* check if we already know about this session */
+  if (g_list_find (priv->sessions, session) == NULL) {
+    GST_INFO ("watching session %p", session);
 
-    /* we already know about this session */
-    if (msession == session)
-      return;
-  }
+    priv->sessions = g_list_prepend (priv->sessions, g_object_ref (session));
+    priv->sessions_cookie++;
 
-  GST_INFO ("watching session %p", session);
+    /* connect removed session handler, it will be disconnected when the last
+     * session gets removed  */
+    if (priv->session_removed_id == 0)
+      priv->session_removed_id = g_signal_connect_data (priv->session_pool,
+          "session-removed", G_CALLBACK (client_session_removed),
+          g_object_ref (client), (GClosureNotify) g_object_unref, 0);
+  }
+  g_mutex_unlock (&priv->lock);
 
-  g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
-      client);
-  priv->sessions = g_list_prepend (priv->sessions, session);
+  return;
 }
 
+/* should be called with lock */
 static void
-client_unwatch_session (GstRTSPClient * client, GstRTSPSession * session)
+client_unwatch_session (GstRTSPClient * client, GstRTSPSession * session,
+    GList * link)
 {
   GstRTSPClientPrivate *priv = client->priv;
 
-  GST_INFO ("unwatching session %p", session);
+  GST_INFO ("client %p: unwatch session %p", client, session);
+
+  if (link == NULL) {
+    link = g_list_find (priv->sessions, session);
+    if (link == NULL)
+      return;
+  }
+
+  priv->sessions = g_list_delete_link (priv->sessions, link);
+  priv->sessions_cookie++;
+
+  /* if this was the last session, disconnect the handler.
+   * This will also drop the extra client ref */
+  if (!priv->sessions) {
+    g_signal_handler_disconnect (priv->session_pool, priv->session_removed_id);
+    priv->session_removed_id = 0;
+  }
 
-  g_object_weak_unref (G_OBJECT (session),
-      (GWeakNotify) client_session_finalized, client);
-  priv->sessions = g_list_remove (priv->sessions, session);
+  /* remove the session */
+  g_object_unref (session);
 }
 
-static void
-client_cleanup_session (GstRTSPClient * client, GstRTSPSession * session)
+static GstRTSPFilterResult
+cleanup_session (GstRTSPClient * client, GstRTSPSession * sess,
+    gpointer user_data)
 {
-  g_object_weak_unref (G_OBJECT (session),
-      (GWeakNotify) client_session_finalized, client);
-  client_unlink_session (client, session);
+  /* unlink all media managed in this session. This needs to happen
+   * without the client lock, so we really want to do it here. */
+  gst_rtsp_session_filter (sess, filter_session_media, client);
+
+  return GST_RTSP_FILTER_REMOVE;
 }
 
 static void
-client_cleanup_sessions (GstRTSPClient * client)
+clean_cached_media (GstRTSPClient * client, gboolean unprepare)
 {
   GstRTSPClientPrivate *priv = client->priv;
-  GList *sessions;
 
-  /* remove weak-ref from sessions */
-  for (sessions = priv->sessions; sessions; sessions = g_list_next (sessions)) {
-    client_cleanup_session (client, (GstRTSPSession *) sessions->data);
+  if (priv->path) {
+    g_free (priv->path);
+    priv->path = NULL;
+  }
+  if (priv->media) {
+    if (unprepare)
+      gst_rtsp_media_unprepare (priv->media);
+    g_object_unref (priv->media);
+    priv->media = NULL;
   }
-  g_list_free (priv->sessions);
-  priv->sessions = NULL;
 }
 
 /* A client is finalized when the connection is broken */
@@ -333,17 +409,29 @@ gst_rtsp_client_finalize (GObject * obj)
 
   GST_INFO ("finalize client %p", client);
 
+  if (priv->watch)
+    gst_rtsp_watch_set_flushing (priv->watch, TRUE);
   gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
 
   if (priv->watch)
     g_source_destroy ((GSource *) priv->watch);
 
-  client_cleanup_sessions (client);
+  if (priv->watch_context)
+    g_main_context_unref (priv->watch_context);
+
+  /* all sessions should have been removed by now. We keep a ref to
+   * the client object for the session removed handler. The ref is
+   * dropped when the last session is removed from the list. */
+  g_assert (priv->sessions == NULL);
+  g_assert (priv->session_removed_id == 0);
+
+  g_hash_table_unref (priv->transports);
 
   if (priv->connection)
     gst_rtsp_connection_free (priv->connection);
-  if (priv->session_pool)
+  if (priv->session_pool) {
     g_object_unref (priv->session_pool);
+  }
   if (priv->mount_points)
     g_object_unref (priv->mount_points);
   if (priv->auth)
@@ -351,16 +439,12 @@ gst_rtsp_client_finalize (GObject * obj)
   if (priv->thread_pool)
     g_object_unref (priv->thread_pool);
 
-  if (priv->path)
-    g_free (priv->path);
-  if (priv->media) {
-    gst_rtsp_media_unprepare (priv->media);
-    g_object_unref (priv->media);
-  }
+  clean_cached_media (client, TRUE);
 
   g_free (priv->server_ip);
   g_mutex_clear (&priv->lock);
   g_mutex_clear (&priv->send_lock);
+  g_mutex_clear (&priv->watch_lock);
 
   G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
 }
@@ -370,6 +454,7 @@ gst_rtsp_client_get_property (GObject * object, guint propid,
     GValue * value, GParamSpec * pspec)
 {
   GstRTSPClient *client = GST_RTSP_CLIENT (object);
+  GstRTSPClientPrivate *priv = client->priv;
 
   switch (propid) {
     case PROP_SESSION_POOL:
@@ -378,6 +463,9 @@ gst_rtsp_client_get_property (GObject * object, guint propid,
     case PROP_MOUNT_POINTS:
       g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
       break;
+    case PROP_DROP_BACKLOG:
+      g_value_set_boolean (value, priv->drop_backlog);
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
   }
@@ -388,6 +476,7 @@ gst_rtsp_client_set_property (GObject * object, guint propid,
     const GValue * value, GParamSpec * pspec)
 {
   GstRTSPClient *client = GST_RTSP_CLIENT (object);
+  GstRTSPClientPrivate *priv = client->priv;
 
   switch (propid) {
     case PROP_SESSION_POOL:
@@ -396,6 +485,11 @@ gst_rtsp_client_set_property (GObject * object, guint propid,
     case PROP_MOUNT_POINTS:
       gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
       break;
+    case PROP_DROP_BACKLOG:
+      g_mutex_lock (&priv->lock);
+      priv->drop_backlog = g_value_get_boolean (value);
+      g_mutex_unlock (&priv->lock);
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
   }
@@ -406,7 +500,7 @@ gst_rtsp_client_set_property (GObject * object, guint propid,
  *
  * Create a new #GstRTSPClient instance.
  *
- * Returns: a new #GstRTSPClient
+ * Returns: (transfer full): a new #GstRTSPClient
  */
 GstRTSPClient *
 gst_rtsp_client_new (void)
@@ -419,7 +513,7 @@ gst_rtsp_client_new (void)
 }
 
 static void
-send_message (GstRTSPClient * client, GstRTSPSession * session,
+send_message (GstRTSPClient * client, GstRTSPContext * ctx,
     GstRTSPMessage * message, gboolean close)
 {
   GstRTSPClientPrivate *priv = client->priv;
@@ -431,9 +525,9 @@ send_message (GstRTSPClient * client, GstRTSPSession * session,
   gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
 
   /* add the new session header for new session ids */
-  if (session) {
+  if (ctx->session) {
     gst_rtsp_message_take_header (message, GST_RTSP_HDR_SESSION,
-        gst_rtsp_session_get_header (session));
+        gst_rtsp_session_get_header (ctx->session));
   }
 
   if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
@@ -443,6 +537,9 @@ send_message (GstRTSPClient * client, GstRTSPSession * session,
   if (close)
     gst_rtsp_message_add_header (message, GST_RTSP_HDR_CONNECTION, "close");
 
+  g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SEND_MESSAGE],
+      0, ctx, message);
+
   g_mutex_lock (&priv->send_lock);
   if (priv->send_func)
     priv->send_func (client, message, close, priv->send_data);
@@ -458,7 +555,28 @@ send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
   gst_rtsp_message_init_response (ctx->response, code,
       gst_rtsp_status_as_text (code), ctx->request);
 
-  send_message (client, NULL, ctx->response, FALSE);
+  ctx->session = NULL;
+
+  send_message (client, ctx, ctx->response, FALSE);
+}
+
+static void
+send_option_not_supported_response (GstRTSPClient * client,
+    GstRTSPContext * ctx, const gchar * unsupported_options)
+{
+  GstRTSPStatusCode code = GST_RTSP_STS_OPTION_NOT_SUPPORTED;
+
+  gst_rtsp_message_init_response (ctx->response, code,
+      gst_rtsp_status_as_text (code), ctx->request);
+
+  if (unsupported_options != NULL) {
+    gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_UNSUPPORTED,
+        unsupported_options);
+  }
+
+  ctx->session = NULL;
+
+  send_message (client, ctx, ctx->response, FALSE);
 }
 
 static gboolean
@@ -507,18 +625,9 @@ find_media (GstRTSPClient * client, GstRTSPContext * ctx, gchar * path,
     path_len = strlen (path);
 
   if (!paths_are_equal (priv->path, path, path_len)) {
-    GstRTSPThread *thread;
-
     /* remove any previously cached values before we try to construct a new
      * media for uri */
-    if (priv->path)
-      g_free (priv->path);
-    priv->path = NULL;
-    if (priv->media) {
-      gst_rtsp_media_unprepare (priv->media);
-      g_object_unref (priv->media);
-    }
-    priv->media = NULL;
+    clean_cached_media (client, TRUE);
 
     /* prepare the media and add it to the pipeline */
     if (!(media = gst_rtsp_media_factory_construct (factory, ctx->uri)))
@@ -526,14 +635,19 @@ find_media (GstRTSPClient * client, GstRTSPContext * ctx, gchar * path,
 
     ctx->media = media;
 
-    thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
-        GST_RTSP_THREAD_TYPE_MEDIA, ctx);
-    if (thread == NULL)
-      goto no_thread;
+    if (!(gst_rtsp_media_get_transport_mode (media) &
+            GST_RTSP_TRANSPORT_MODE_RECORD)) {
+      GstRTSPThread *thread;
+
+      thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
+          GST_RTSP_THREAD_TYPE_MEDIA, ctx);
+      if (thread == NULL)
+        goto no_thread;
 
-    /* prepare the media */
-    if (!(gst_rtsp_media_prepare (media, thread)))
-      goto no_prepare;
+      /* prepare the media */
+      if (!gst_rtsp_media_prepare (media, thread))
+        goto no_prepare;
+    }
 
     /* now keep track of the uri and the media */
     priv->path = g_strndup (path, path_len);
@@ -608,6 +722,7 @@ do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
 {
   GstRTSPClientPrivate *priv = client->priv;
   GstRTSPMessage message = { 0 };
+  GstRTSPResult res = GST_RTSP_OK;
   GstMapInfo map_info;
   guint8 *data;
   guint usize;
@@ -622,7 +737,7 @@ do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
 
   g_mutex_lock (&priv->send_lock);
   if (priv->send_func)
-    priv->send_func (client, &message, FALSE, priv->send_data);
+    res = priv->send_func (client, &message, FALSE, priv->send_data);
   g_mutex_unlock (&priv->send_lock);
 
   gst_rtsp_message_steal_body (&message, &data, &usize);
@@ -630,96 +745,71 @@ do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
 
   gst_rtsp_message_unset (&message);
 
-  return TRUE;
-}
-
-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);
-
-  priv->transports = g_list_prepend (priv->transports, trans);
-
-  /* make sure our session can't expire */
-  gst_rtsp_session_prevent_expire (session);
+  return res == GST_RTSP_OK;
 }
 
-static void
-unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
-    GstRTSPStreamTransport * trans)
+/**
+ * gst_rtsp_client_close:
+ * @client: a #GstRTSPClient
+ *
+ * Close the connection of @client and remove all media it was managing.
+ *
+ * Since: 1.4
+ */
+void
+gst_rtsp_client_close (GstRTSPClient * client)
 {
   GstRTSPClientPrivate *priv = client->priv;
+  const gchar *tunnelid;
 
-  GST_DEBUG ("client %p: unlinking transport %p", client, trans);
-
-  gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
-
-  priv->transports = g_list_remove (priv->transports, trans);
-
-  /* our session can now expire */
-  gst_rtsp_session_allow_expire (session);
-}
-
-static void
-unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
-    GstRTSPSessionMedia * sessmedia)
-{
-  guint n_streams, i;
-
-  n_streams =
-      gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
-  for (i = 0; i < n_streams; i++) {
-    GstRTSPStreamTransport *trans;
-    const GstRTSPTransport *tr;
-
-    /* get the transport, if there is no transport configured, skip this stream */
-    trans = gst_rtsp_session_media_get_transport (sessmedia, i);
-    if (trans == NULL)
-      continue;
-
-    tr = gst_rtsp_stream_transport_get_transport (trans);
+  GST_DEBUG ("client %p: closing connection", client);
 
-    if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
-      /* for TCP, unlink the stream from the TCP connection of the client */
-      unlink_transport (client, session, trans);
+  if (priv->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 (priv->connection);
+  }
+
+  /* connection is now closed, destroy the watch which will also cause the
+   * closed signal to be emitted */
+  if (priv->watch) {
+    GST_DEBUG ("client %p: destroying watch", client);
+    g_source_destroy ((GSource *) priv->watch);
+    priv->watch = NULL;
+    gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
+    g_main_context_unref (priv->watch_context);
+    priv->watch_context = NULL;
   }
 }
 
-static void
-close_connection (GstRTSPClient * client)
+static gchar *
+default_make_path_from_uri (GstRTSPClient * client, const GstRTSPUrl * uri)
 {
-  GstRTSPClientPrivate *priv = client->priv;
-  const gchar *tunnelid;
-
-  GST_DEBUG ("client %p: closing connection", client);
+  gchar *path;
 
-  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);
-  }
+  if (uri->query)
+    path = g_strconcat (uri->abspath, "?", uri->query, NULL);
+  else
+    path = g_strdup (uri->abspath);
 
-  gst_rtsp_connection_close (priv->connection);
+  return path;
 }
 
 static gboolean
 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;
+  gboolean keep_session;
 
   if (!ctx->session)
     goto no_session;
@@ -729,7 +819,8 @@ handle_teardown_request (GstRTSPClient * client, GstRTSPContext * ctx)
   if (!ctx->uri)
     goto no_uri;
 
-  path = ctx->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);
@@ -740,32 +831,31 @@ handle_teardown_request (GstRTSPClient * client, GstRTSPContext * ctx)
   if (path[matched] != '\0')
     goto no_aggregate;
 
+  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, ctx);
 
-  /* unlink the all TCP callbacks */
-  unlink_session_transports (client, session, sessmedia);
-
-  /* remove the session from the watched sessions */
-  client_unwatch_session (client, session);
-
   gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
 
   /* unmanage the media in the session, returns false if all media session
    * are torn down. */
-  if (!gst_rtsp_session_release_media (session, sessmedia)) {
-    /* remove the session */
-    gst_rtsp_session_pool_remove (priv->session_pool, session);
-  }
+  keep_session = gst_rtsp_session_release_media (session, sessmedia);
+
   /* construct the response now */
   code = GST_RTSP_STS_OK;
   gst_rtsp_message_init_response (ctx->response, code,
       gst_rtsp_status_as_text (code), ctx->request);
 
-  send_message (client, session, ctx->response, TRUE);
+  send_message (client, ctx, ctx->response, TRUE);
+
+  if (!keep_session) {
+    /* remove the session */
+    gst_rtsp_session_pool_remove (priv->session_pool, session);
+  }
 
   return TRUE;
 
@@ -786,6 +876,7 @@ not_found:
   {
     GST_ERROR ("client %p: no media for uri", client);
     send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
+    g_free (path);
     return FALSE;
   }
 no_aggregate:
@@ -793,6 +884,7 @@ no_aggregate:
     GST_ERROR ("client %p: no aggregate path %s", client, path);
     send_generic_response (client,
         GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
+    g_free (path);
     return FALSE;
   }
 }
@@ -837,7 +929,7 @@ handle_get_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
     if (res != GST_RTSP_OK)
       goto bad_request;
 
-    send_message (client, ctx->session, ctx->response, FALSE);
+    send_message (client, ctx, ctx->response, FALSE);
   }
 
   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
@@ -874,7 +966,7 @@ handle_set_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
     if (res != GST_RTSP_OK)
       goto bad_request;
 
-    send_message (client, ctx->session, ctx->response, FALSE);
+    send_message (client, ctx, ctx->response, FALSE);
   }
 
   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
@@ -895,10 +987,11 @@ static gboolean
 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 = ctx->session))
@@ -907,7 +1000,8 @@ handle_pause_request (GstRTSPClient * client, GstRTSPContext * ctx)
   if (!ctx->uri)
     goto no_uri;
 
-  path = ctx->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);
@@ -917,6 +1011,8 @@ handle_pause_request (GstRTSPClient * client, GstRTSPContext * ctx)
   if (path[matched] != '\0')
     goto no_aggregate;
 
+  g_free (path);
+
   ctx->sessmedia = sessmedia;
 
   rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
@@ -925,9 +1021,6 @@ handle_pause_request (GstRTSPClient * client, GstRTSPContext * ctx)
       rtspstate != GST_RTSP_STATE_RECORDING)
     goto invalid_state;
 
-  /* unlink the all TCP callbacks */
-  unlink_session_transports (client, session, sessmedia);
-
   /* then pause sending */
   gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
 
@@ -936,7 +1029,7 @@ handle_pause_request (GstRTSPClient * client, GstRTSPContext * ctx)
   gst_rtsp_message_init_response (ctx->response, code,
       gst_rtsp_status_as_text (code), ctx->request);
 
-  send_message (client, session, ctx->response, FALSE);
+  send_message (client, ctx, ctx->response, FALSE);
 
   /* the state is now READY */
   gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
@@ -962,6 +1055,7 @@ not_found:
   {
     GST_ERROR ("client %p: no media for uri", client);
     send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
+    g_free (path);
     return FALSE;
   }
 no_aggregate:
@@ -969,6 +1063,7 @@ no_aggregate:
     GST_ERROR ("client %p: no aggregate path %s", client, path);
     send_generic_response (client,
         GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
+    g_free (path);
     return FALSE;
   }
 invalid_state:
@@ -983,10 +1078,11 @@ invalid_state:
 /* 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)
+make_base_url (GstRTSPClient * client, GstRTSPUrl * url, const gchar * path)
 {
   GstRTSPUrl tmp;
-  gchar *result, *trail;
+  gchar *result;
+  const gchar *trail;
 
   /* check for trailing '/' and append one */
   trail = (path[strlen (path) - 1] != '/' ? "/" : "");
@@ -1006,18 +1102,17 @@ static gboolean
 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, *base_url;
+  gchar *str;
   GstRTSPTimeRange *range;
   GstRTSPResult res;
   GstRTSPState rtspstate;
   GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
-  gchar *path;
+  gchar *path, *rtpinfo;
   gint matched;
 
   if (!(session = ctx->session))
@@ -1026,7 +1121,8 @@ handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
   if (!(uri = ctx->uri))
     goto no_uri;
 
-  path = 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);
@@ -1036,68 +1132,43 @@ handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
   if (path[matched] != '\0')
     goto no_aggregate;
 
+  g_free (path);
+
   ctx->sessmedia = sessmedia;
   ctx->media = media = gst_rtsp_session_media_get_media (sessmedia);
 
+  if (!(gst_rtsp_media_get_transport_mode (media) &
+          GST_RTSP_TRANSPORT_MODE_PLAY))
+    goto unsupported_mode;
+
   /* 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 (ctx->request, GST_RTSP_HDR_RANGE, &str, 0);
   if (res == GST_RTSP_OK) {
     if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
+      GstRTSPMediaStatus media_status;
+
       /* we have a range, seek to the position */
       unit = range->unit;
       gst_rtsp_media_seek (media, range);
       gst_rtsp_range_free (range);
-    }
-  }
-
-  /* grab RTPInfo from the payloaders now */
-  rtpinfo = g_string_new ("");
-
-  base_url = make_base_url (client, uri, path);
 
-  n_streams = gst_rtsp_media_n_streams (media);
-  for (i = 0, infocount = 0; i < n_streams; i++) {
-    GstRTSPStreamTransport *trans;
-    GstRTSPStream *stream;
-    const GstRTSPTransport *tr;
-    guint rtptime, seq;
-
-    /* get the transport, if there is no transport configured, skip this stream */
-    trans = gst_rtsp_session_media_get_transport (sessmedia, i);
-    if (trans == NULL) {
-      GST_INFO ("stream %d is not configured", i);
-      continue;
-    }
-    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);
-    }
-
-    stream = gst_rtsp_stream_transport_get_stream (trans);
-    if (gst_rtsp_stream_get_rtpinfo (stream, &rtptime, &seq)) {
-      gchar *control;
-
-      if (infocount > 0)
-        g_string_append (rtpinfo, ", ");
-
-      control = gst_rtsp_stream_get_control (stream);
-      g_string_append_printf (rtpinfo, "url=%s%s;seq=%u;rtptime=%u",
-          base_url, control, seq, rtptime);
-      g_free (control);
-
-      infocount++;
-    } else {
-      GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
+      media_status = gst_rtsp_media_get_status (media);
+      if (media_status == GST_RTSP_MEDIA_STATUS_ERROR)
+        goto seek_failed;
     }
   }
-  g_free (base_url);
+
+  /* grab RTPInfo from the media now */
+  rtpinfo = gst_rtsp_session_media_get_rtpinfo (sessmedia);
 
   /* construct the response now */
   code = GST_RTSP_STS_OK;
@@ -1105,21 +1176,18 @@ handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
       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 (ctx->response, GST_RTSP_HDR_RTP_INFO, str);
-  } else {
-    g_string_free (rtpinfo, TRUE);
-  }
+  if (rtpinfo)
+    gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RTP_INFO,
+        rtpinfo);
 
   /* add the range */
   str = gst_rtsp_media_get_range_string (media, TRUE, unit);
   if (str)
     gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RANGE, str);
 
-  send_message (client, session, ctx->response, FALSE);
+  send_message (client, ctx, ctx->response, FALSE);
 
-  /* start playing after sending the request */
+  /* start playing after sending the response */
   gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
 
   gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
@@ -1152,6 +1220,7 @@ no_aggregate:
     GST_ERROR ("client %p: no aggregate path %s", client, path);
     send_generic_response (client,
         GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
+    g_free (path);
     return FALSE;
   }
 invalid_state:
@@ -1161,6 +1230,24 @@ invalid_state:
         ctx);
     return FALSE;
   }
+unsuspend_failed:
+  {
+    GST_ERROR ("client %p: unsuspend failed", client);
+    send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
+    return FALSE;
+  }
+seek_failed:
+  {
+    GST_ERROR ("client %p: seek failed", client);
+    send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
+    return FALSE;
+  }
+unsupported_mode:
+  {
+    GST_ERROR ("client %p: media does not support PLAY", client);
+    send_generic_response (client, GST_RTSP_STS_METHOD_NOT_ALLOWED, ctx);
+    return FALSE;
+  }
 }
 
 static void
@@ -1171,10 +1258,10 @@ do_keepalive (GstRTSPSession * session)
 }
 
 /* parse @transport and return a valid transport in @tr. only transports
- * from @supported are returned. Returns FALSE if no valid transport
+ * supported by @stream are returned. Returns FALSE if no valid transport
  * was found. */
 static gboolean
-parse_transport (const char *transport, GstRTSPLowerTrans supported,
+parse_transport (const char *transport, GstRTSPStream * stream,
     GstRTSPTransport * tr)
 {
   gint i;
@@ -1197,13 +1284,8 @@ parse_transport (const char *transport, GstRTSPLowerTrans supported,
       goto next;
     }
 
-    /* we have a transport, see if it's RTP/AVP */
-    if (tr->trans != GST_RTSP_TRANS_RTP || tr->profile != GST_RTSP_PROFILE_AVP) {
-      GST_WARNING ("invalid transport %s", transports[i]);
-      goto next;
-    }
-
-    if (!(tr->lower_transport & supported)) {
+    /* we have a transport, see if it's supported */
+    if (!gst_rtsp_stream_is_transport_supported (stream, tr)) {
       GST_WARNING ("unsupported transport %s", transports[i]);
       goto next;
     }
@@ -1222,11 +1304,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) {
@@ -1234,21 +1316,29 @@ 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
@@ -1300,6 +1390,33 @@ default_configure_client_transport (GstRTSPClient * client,
     ct->destination = g_strdup (url->host);
 
     if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
+      GSocket *sock;
+      GSocketAddress *addr;
+
+      sock = gst_rtsp_connection_get_read_socket (priv->connection);
+      if ((addr = g_socket_get_remote_address (sock, NULL))) {
+        /* our read port is the sender port of client */
+        ct->client_port.min =
+            g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
+        g_object_unref (addr);
+      }
+      if ((addr = g_socket_get_local_address (sock, NULL))) {
+        ct->server_port.max =
+            g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
+        g_object_unref (addr);
+      }
+      sock = gst_rtsp_connection_get_write_socket (priv->connection);
+      if ((addr = g_socket_get_remote_address (sock, NULL))) {
+        /* our write port is the receiver port of client */
+        ct->client_port.max =
+            g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
+        g_object_unref (addr);
+      }
+      if ((addr = g_socket_get_local_address (sock, NULL))) {
+        ct->server_port.min =
+            g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
+        g_object_unref (addr);
+      }
       /* check if the client selected channels for TCP */
       if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
         gst_rtsp_session_media_alloc_channels (ctx->sessmedia,
@@ -1318,8 +1435,8 @@ no_address:
 }
 
 static GstRTSPTransport *
-make_server_transport (GstRTSPClient * client, GstRTSPContext * ctx,
-    GstRTSPTransport * ct)
+make_server_transport (GstRTSPClient * client, GstRTSPMedia * media,
+    GstRTSPContext * ctx, GstRTSPTransport * ct)
 {
   GstRTSPTransport *st;
   GInetAddress *addr;
@@ -1331,6 +1448,8 @@ make_server_transport (GstRTSPClient * client, GstRTSPContext * ctx,
   st->trans = ct->trans;
   st->profile = ct->profile;
   st->lower_transport = ct->lower_transport;
+  st->mode_play = ct->mode_play;
+  st->mode_record = ct->mode_record;
 
   addr = g_inet_address_new_from_string (ct->destination);
 
@@ -1355,63 +1474,326 @@ make_server_transport (GstRTSPClient * client, GstRTSPContext * ctx,
       break;
     case GST_RTSP_LOWER_TRANS_TCP:
       st->interleaved = ct->interleaved;
+      st->client_port = ct->client_port;
+      st->server_port = ct->server_port;
     default:
       break;
   }
 
-  gst_rtsp_stream_get_ssrc (ctx->stream, &st->ssrc);
+  if ((gst_rtsp_media_get_transport_mode (media) &
+          GST_RTSP_TRANSPORT_MODE_PLAY))
+    gst_rtsp_stream_get_ssrc (ctx->stream, &st->ssrc);
 
   return st;
 }
 
+#define AES_128_KEY_LEN 16
+#define AES_256_KEY_LEN 32
+
+#define HMAC_32_KEY_LEN 4
+#define HMAC_80_KEY_LEN 10
+
 static gboolean
-handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
+mikey_apply_policy (GstCaps * caps, GstMIKEYMessage * msg, guint8 policy)
 {
-  GstRTSPClientPrivate *priv = client->priv;
-  GstRTSPResult res;
-  GstRTSPUrl *uri;
-  gchar *transport;
-  GstRTSPTransport *ct, *st;
-  GstRTSPLowerTrans supported;
-  GstRTSPStatusCode code;
-  GstRTSPSession *session;
-  GstRTSPStreamTransport *trans;
-  gchar *trans_str;
-  GstRTSPSessionMedia *sessmedia;
-  GstRTSPMedia *media;
-  GstRTSPStream *stream;
-  GstRTSPState rtspstate;
-  GstRTSPClientClass *klass;
-  gchar *path, *control;
-  gint matched;
+  const gchar *srtp_cipher;
+  const gchar *srtp_auth;
+  const GstMIKEYPayload *sp;
+  guint i;
 
-  if (!ctx->uri)
-    goto no_uri;
+  /* loop over Security policy until we find one containing policy */
+  for (i = 0;; i++) {
+    if ((sp = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_SP, i)) == NULL)
+      break;
 
-  uri = ctx->uri;
-  if (uri->query)
-    path = g_strconcat (uri->abspath, "?", uri->query, NULL);
-  else
-    path = g_strdup (uri->abspath);
+    if (((GstMIKEYPayloadSP *) sp)->policy == policy)
+      break;
+  }
 
-  /* parse the transport */
-  res =
-      gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_TRANSPORT,
-      &transport, 0);
-  if (res != GST_RTSP_OK)
-    goto no_transport;
+  /* the default ciphers */
+  srtp_cipher = "aes-128-icm";
+  srtp_auth = "hmac-sha1-80";
+
+  /* now override the defaults with what is in the Security Policy */
+  if (sp != NULL) {
+    guint len;
+
+    /* collect all the params and go over them */
+    len = gst_mikey_payload_sp_get_n_params (sp);
+    for (i = 0; i < len; i++) {
+      const GstMIKEYPayloadSPParam *param =
+          gst_mikey_payload_sp_get_param (sp, i);
+
+      switch (param->type) {
+        case GST_MIKEY_SP_SRTP_ENC_ALG:
+          switch (param->val[0]) {
+            case 0:
+              srtp_cipher = "null";
+              break;
+            case 2:
+            case 1:
+              srtp_cipher = "aes-128-icm";
+              break;
+            default:
+              break;
+          }
+          break;
+        case GST_MIKEY_SP_SRTP_ENC_KEY_LEN:
+          switch (param->val[0]) {
+            case AES_128_KEY_LEN:
+              srtp_cipher = "aes-128-icm";
+              break;
+            case AES_256_KEY_LEN:
+              srtp_cipher = "aes-256-icm";
+              break;
+            default:
+              break;
+          }
+          break;
+        case GST_MIKEY_SP_SRTP_AUTH_ALG:
+          switch (param->val[0]) {
+            case 0:
+              srtp_auth = "null";
+              break;
+            case 2:
+            case 1:
+              srtp_auth = "hmac-sha1-80";
+              break;
+            default:
+              break;
+          }
+          break;
+        case GST_MIKEY_SP_SRTP_AUTH_KEY_LEN:
+          switch (param->val[0]) {
+            case HMAC_32_KEY_LEN:
+              srtp_auth = "hmac-sha1-32";
+              break;
+            case HMAC_80_KEY_LEN:
+              srtp_auth = "hmac-sha1-80";
+              break;
+            default:
+              break;
+          }
+          break;
+        case GST_MIKEY_SP_SRTP_SRTP_ENC:
+          break;
+        case GST_MIKEY_SP_SRTP_SRTCP_ENC:
+          break;
+        default:
+          break;
+      }
+    }
+  }
+  /* now configure the SRTP parameters */
+  gst_caps_set_simple (caps,
+      "srtp-cipher", G_TYPE_STRING, srtp_cipher,
+      "srtp-auth", G_TYPE_STRING, srtp_auth,
+      "srtcp-cipher", G_TYPE_STRING, srtp_cipher,
+      "srtcp-auth", G_TYPE_STRING, srtp_auth, NULL);
 
-  /* we create the session after parsing stuff so that we don't make
-   * a session for malformed requests */
-  if (priv->session_pool == NULL)
-    goto no_pool;
+  return TRUE;
+}
 
-  session = ctx->session;
+static gboolean
+handle_mikey_data (GstRTSPClient * client, GstRTSPContext * ctx,
+    guint8 * data, gsize size)
+{
+  GstMIKEYMessage *msg;
+  guint i, n_cs;
+  GstCaps *caps = NULL;
+  GstMIKEYPayloadKEMAC *kemac;
+  const GstMIKEYPayloadKeyData *pkd;
+  GstBuffer *key;
+
+  /* the MIKEY message contains a CSB or crypto session bundle. It is a
+   * set of Crypto Sessions protected with the same master key.
+   * In the context of SRTP, an RTP and its RTCP stream is part of a
+   * crypto session */
+  if ((msg = gst_mikey_message_new_from_data (data, size, NULL, NULL)) == NULL)
+    goto parse_failed;
+
+  /* we can only handle SRTP crypto sessions for now */
+  if (msg->map_type != GST_MIKEY_MAP_TYPE_SRTP)
+    goto invalid_map_type;
+
+  /* get the number of crypto sessions. This maps SSRC to its
+   * security parameters */
+  n_cs = gst_mikey_message_get_n_cs (msg);
+  if (n_cs == 0)
+    goto no_crypto_sessions;
+
+  /* we also need keys */
+  if (!(kemac = (GstMIKEYPayloadKEMAC *) gst_mikey_message_find_payload
+          (msg, GST_MIKEY_PT_KEMAC, 0)))
+    goto no_keys;
+
+  /* we don't support encrypted keys */
+  if (kemac->enc_alg != GST_MIKEY_ENC_NULL
+      || kemac->mac_alg != GST_MIKEY_MAC_NULL)
+    goto unsupported_encryption;
+
+  /* get Key data sub-payload */
+  pkd = (const GstMIKEYPayloadKeyData *)
+      gst_mikey_payload_kemac_get_sub (&kemac->pt, 0);
+
+  key =
+      gst_buffer_new_wrapped (g_memdup (pkd->key_data, pkd->key_len),
+      pkd->key_len);
+
+  /* go over all crypto sessions and create the security policy for each
+   * SSRC */
+  for (i = 0; i < n_cs; i++) {
+    const GstMIKEYMapSRTP *map = gst_mikey_message_get_cs_srtp (msg, i);
+
+    caps = gst_caps_new_simple ("application/x-srtp",
+        "ssrc", G_TYPE_UINT, map->ssrc,
+        "roc", G_TYPE_UINT, map->roc, "srtp-key", GST_TYPE_BUFFER, key, NULL);
+    mikey_apply_policy (caps, msg, map->policy);
+
+    gst_rtsp_stream_update_crypto (ctx->stream, map->ssrc, caps);
+    gst_caps_unref (caps);
+  }
+  gst_mikey_message_unref (msg);
+  gst_buffer_unref (key);
 
-  if (session) {
-    g_object_ref (session);
-    /* get a handle to the configuration of the media in the session, this can
-     * return NULL if this is a new url to manage in this session. */
+  return TRUE;
+
+  /* ERRORS */
+parse_failed:
+  {
+    GST_DEBUG_OBJECT (client, "failed to parse MIKEY message");
+    return FALSE;
+  }
+invalid_map_type:
+  {
+    GST_DEBUG_OBJECT (client, "invalid map type %d", msg->map_type);
+    goto cleanup_message;
+  }
+no_crypto_sessions:
+  {
+    GST_DEBUG_OBJECT (client, "no crypto sessions");
+    goto cleanup_message;
+  }
+no_keys:
+  {
+    GST_DEBUG_OBJECT (client, "no keys found");
+    goto cleanup_message;
+  }
+unsupported_encryption:
+  {
+    GST_DEBUG_OBJECT (client, "unsupported key encryption");
+    goto cleanup_message;
+  }
+cleanup_message:
+  {
+    gst_mikey_message_unref (msg);
+    return FALSE;
+  }
+}
+
+#define IS_STRIP_CHAR(c) (g_ascii_isspace ((guchar)(c)) || ((c) == '\"'))
+
+static void
+strip_chars (gchar * str)
+{
+  gchar *s;
+  gsize len;
+
+  len = strlen (str);
+  while (len--) {
+    if (!IS_STRIP_CHAR (str[len]))
+      break;
+    str[len] = '\0';
+  }
+  for (s = str; *s && IS_STRIP_CHAR (*s); s++);
+  memmove (str, s, len + 1);
+}
+
+/* KeyMgmt = "KeyMgmt" ":" key-mgmt-spec 0*("," key-mgmt-spec)
+ * key-mgmt-spec = "prot" "=" KMPID ";" ["uri" "=" %x22 URI %x22 ";"]
+ */
+static gboolean
+handle_keymgmt (GstRTSPClient * client, GstRTSPContext * ctx, gchar * keymgmt)
+{
+  gchar **specs;
+  gint i, j;
+
+  specs = g_strsplit (keymgmt, ",", 0);
+  for (i = 0; specs[i]; i++) {
+    gchar **split;
+
+    split = g_strsplit (specs[i], ";", 0);
+    for (j = 0; split[j]; j++) {
+      g_strstrip (split[j]);
+      if (g_str_has_prefix (split[j], "prot=")) {
+        g_strstrip (split[j] + 5);
+        if (!g_str_equal (split[j] + 5, "mikey"))
+          break;
+        GST_DEBUG ("found mikey");
+      } else if (g_str_has_prefix (split[j], "uri=")) {
+        strip_chars (split[j] + 4);
+        GST_DEBUG ("found uri '%s'", split[j] + 4);
+      } else if (g_str_has_prefix (split[j], "data=")) {
+        guchar *data;
+        gsize size;
+        strip_chars (split[j] + 5);
+        GST_DEBUG ("found data '%s'", split[j] + 5);
+        data = g_base64_decode_inplace (split[j] + 5, &size);
+        handle_mikey_data (client, ctx, data, size);
+      }
+    }
+    g_strfreev (split);
+  }
+  g_strfreev (specs);
+  return TRUE;
+}
+
+static gboolean
+handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
+{
+  GstRTSPClientPrivate *priv = client->priv;
+  GstRTSPResult res;
+  GstRTSPUrl *uri;
+  gchar *transport, *keymgmt;
+  GstRTSPTransport *ct, *st;
+  GstRTSPStatusCode code;
+  GstRTSPSession *session;
+  GstRTSPStreamTransport *trans;
+  gchar *trans_str;
+  GstRTSPSessionMedia *sessmedia;
+  GstRTSPMedia *media;
+  GstRTSPStream *stream;
+  GstRTSPState rtspstate;
+  GstRTSPClientClass *klass;
+  gchar *path, *control = NULL;
+  gint matched;
+  gboolean new_session = FALSE;
+
+  if (!ctx->uri)
+    goto no_uri;
+
+  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 (ctx->request, GST_RTSP_HDR_TRANSPORT,
+      &transport, 0);
+  if (res != GST_RTSP_OK)
+    goto no_transport;
+
+  /* we create the session after parsing stuff so that we don't make
+   * a session for malformed requests */
+  if (priv->session_pool == NULL)
+    goto no_pool;
+
+  session = ctx->session;
+
+  if (session) {
+    g_object_ref (session);
+    /* get a handle to the configuration of the media in the session, this can
+     * return NULL if this is a new url to manage in this session. */
     sessmedia = gst_rtsp_session_get_media (session, path, &matched);
   } else {
     /* we need a new media configuration in this session */
@@ -1432,16 +1814,22 @@ handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
   if (media == NULL)
     goto media_not_found_no_reply;
 
-  if (path[matched] == '\0')
-    goto control_not_found;
+  if (path[matched] == '\0') {
+    if (gst_rtsp_media_n_streams (media) == 1) {
+      stream = gst_rtsp_media_get_stream (media, 0);
+    } else {
+      goto control_not_found;
+    }
+  } else {
+    /* path is what matched. */
+    path[matched] = '\0';
+    /* control is remainder */
+    control = &path[matched + 1];
 
-  /* path is what matched. We can modify the parsed uri in place */
-  path[matched] = '\0';
-  /* control is remainder */
-  control = &path[matched + 1];
+    /* find the stream now using the control part */
+    stream = gst_rtsp_media_find_stream (media, control);
+  }
 
-  /* find the stream now using the control part */
-  stream = gst_rtsp_media_find_stream (media, control);
   if (stream == NULL)
     goto stream_not_found;
 
@@ -1458,6 +1846,7 @@ handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
     /* make sure this client is closed when the session is closed */
     client_watch_session (client, session);
 
+    new_session = TRUE;
     /* signal new session */
     g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
         session);
@@ -1465,45 +1854,76 @@ handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
     ctx->session = session;
   }
 
+  if (!klass->configure_client_media (client, media, stream, ctx))
+    goto configure_media_failed_no_reply;
+
+  gst_rtsp_transport_new (&ct);
+
+  /* parse and find a usable supported transport */
+  if (!parse_transport (transport, stream, ct))
+    goto unsupported_transports;
+
+  if ((ct->mode_play
+          && !(gst_rtsp_media_get_transport_mode (media) &
+              GST_RTSP_TRANSPORT_MODE_PLAY)) || (ct->mode_record
+          && !(gst_rtsp_media_get_transport_mode (media) &
+              GST_RTSP_TRANSPORT_MODE_RECORD)))
+    goto unsupported_mode;
+
+  /* parse the keymgmt */
+  if (gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_KEYMGMT,
+          &keymgmt, 0) == GST_RTSP_OK) {
+    if (!handle_keymgmt (client, ctx, keymgmt))
+      goto keymgmt_error;
+  }
+
   if (sessmedia == NULL) {
     /* manage the media in our session now, if not done already  */
     sessmedia = gst_rtsp_session_manage_media (session, path, media);
     /* if we stil have no media, error */
     if (sessmedia == NULL)
       goto sessmedia_unavailable;
+
+    /* don't cache media anymore */
+    clean_cached_media (client, FALSE);
   } else {
     g_object_unref (media);
   }
 
   ctx->sessmedia = sessmedia;
 
-  /* set blocksize on this stream */
-  if (!handle_blocksize (media, stream, ctx->request))
-    goto invalid_blocksize;
-
-  gst_rtsp_transport_new (&ct);
-
-  /* our supported transports */
-  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, ctx, ct))
     goto unsupported_client_transport;
 
   /* set in the session media transport */
   trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
 
+  ctx->trans = trans;
+
+  /* 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);
 
+  if (ct->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
+    /* our callbacks to send data on this TCP connection */
+    gst_rtsp_stream_transport_set_callbacks (trans,
+        (GstRTSPSendFunc) do_send_data,
+        (GstRTSPSendFunc) do_send_data, client, NULL);
+
+    g_hash_table_insert (priv->transports,
+        GINT_TO_POINTER (ct->interleaved.min), trans);
+    g_object_ref (trans);
+    g_hash_table_insert (priv->transports,
+        GINT_TO_POINTER (ct->interleaved.max), trans);
+    g_object_ref (trans);
+  }
+
   /* create and serialize the server transport */
-  st = make_server_transport (client, ctx, ct);
+  st = make_server_transport (client, media, ctx, ct);
   trans_str = gst_rtsp_transport_as_text (st);
   gst_rtsp_transport_free (st);
 
@@ -1516,7 +1936,7 @@ handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
       trans_str);
   g_free (trans_str);
 
-  send_message (client, session, ctx->response, FALSE);
+  send_message (client, ctx, ctx->response, FALSE);
 
   /* update the state */
   rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
@@ -1548,86 +1968,98 @@ no_transport:
   {
     GST_ERROR ("client %p: no transport", client);
     send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
-    g_free (path);
-    return FALSE;
+    goto cleanup_path;
   }
 no_pool:
   {
     GST_ERROR ("client %p: no session pool configured", client);
     send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
-    g_free (path);
-    return FALSE;
+    goto cleanup_path;
   }
 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;
+    goto cleanup_path;
   }
 media_not_found:
   {
     GST_ERROR ("client %p: media '%s' not found", client, path);
     send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
-    g_free (path);
-    return FALSE;
+    goto cleanup_path;
   }
 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;
+    goto cleanup_path;
   }
 stream_not_found:
   {
-    GST_ERROR ("client %p: stream '%s' not found", client, control);
+    GST_ERROR ("client %p: stream '%s' not found", client,
+        GST_STR_NULL (control));
     send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
     g_object_unref (media);
-    g_free (path);
-    return FALSE;
+    goto cleanup_path;
   }
 service_unavailable:
   {
     GST_ERROR ("client %p: can't create session", client);
     send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
     g_object_unref (media);
-    g_free (path);
-    return FALSE;
+    goto cleanup_path;
   }
 sessmedia_unavailable:
   {
     GST_ERROR ("client %p: can't create session media", client);
     send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
     g_object_unref (media);
-    g_object_unref (session);
-    g_free (path);
-    return FALSE;
+    goto cleanup_session;
   }
-invalid_blocksize:
+configure_media_failed_no_reply:
   {
-    GST_ERROR ("client %p: invalid blocksize", client);
-    send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
-    g_object_unref (session);
-    g_free (path);
-    return FALSE;
+    GST_ERROR ("client %p: configure_media failed", client);
+    /* error reply is already sent */
+    goto cleanup_session;
   }
 unsupported_transports:
   {
     GST_ERROR ("client %p: unsupported transports", client);
     send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
-    gst_rtsp_transport_free (ct);
-    g_object_unref (session);
-    g_free (path);
-    return FALSE;
+    goto cleanup_transport;
   }
 unsupported_client_transport:
   {
     GST_ERROR ("client %p: unsupported client transport", client);
     send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
+    goto cleanup_transport;
+  }
+unsupported_mode:
+  {
+    GST_ERROR ("client %p: unsupported mode (media play: %d, media record: %d, "
+        "mode play: %d, mode record: %d)", client,
+        ! !(gst_rtsp_media_get_transport_mode (media) &
+            GST_RTSP_TRANSPORT_MODE_PLAY),
+        ! !(gst_rtsp_media_get_transport_mode (media) &
+            GST_RTSP_TRANSPORT_MODE_RECORD), ct->mode_play, ct->mode_record);
+    send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
+    goto cleanup_transport;
+  }
+keymgmt_error:
+  {
+    GST_ERROR ("client %p: keymgmt error", client);
+    send_generic_response (client, GST_RTSP_STS_KEY_MANAGEMENT_FAILURE, ctx);
+    goto cleanup_transport;
+  }
+  {
+  cleanup_transport:
     gst_rtsp_transport_free (ct);
+  cleanup_session:
+    if (new_session)
+      gst_rtsp_session_pool_remove (priv->session_pool, session);
     g_object_unref (session);
+  cleanup_path:
     g_free (path);
     return FALSE;
   }
@@ -1640,6 +2072,8 @@ create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
   GstSDPMessage *sdp;
   GstSDPInfo info;
   const gchar *proto;
+  guint64 session_id_tmp;
+  gchar session_id[21];
 
   gst_sdp_message_new (&sdp);
 
@@ -1651,7 +2085,11 @@ create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
   else
     proto = "IP4";
 
-  gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
+  session_id_tmp = (((guint64) g_random_int ()) << 32) | g_random_int ();
+  g_snprintf (session_id, sizeof (session_id), "%" G_GUINT64_FORMAT,
+      session_id_tmp);
+
+  gst_sdp_message_set_origin (sdp, "-", session_id, "1", "IN", proto,
       priv->server_ip);
 
   gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
@@ -1665,7 +2103,7 @@ create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
   info.server_ip = priv->server_ip;
 
   /* create an SDP for the media object */
-  if (!gst_rtsp_sdp_from_media (sdp, &info, media))
+  if (!gst_rtsp_media_setup_sdp (media, sdp, &info))
     goto no_sdp;
 
   return sdp;
@@ -1696,94 +2134,396 @@ handle_describe_request (GstRTSPClient * client, GstRTSPContext * ctx)
   if (!ctx->uri)
     goto no_uri;
 
-  /* check what kind of format is accepted, we don't really do anything with it
-   * and always return SDP for now. */
-  for (i = 0; i++;) {
-    gchar *accept;
+  /* check what kind of format is accepted, we don't really do anything with it
+   * and always return SDP for now. */
+  for (i = 0;; i++) {
+    gchar *accept;
+
+    res =
+        gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_ACCEPT,
+        &accept, i);
+    if (res == GST_RTSP_ENOTIMPL)
+      break;
+
+    if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
+      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, ctx, path, NULL)))
+    goto no_media;
+
+  if (!(gst_rtsp_media_get_transport_mode (media) &
+          GST_RTSP_TRANSPORT_MODE_PLAY))
+    goto unsupported_mode;
+
+  /* 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 (ctx->response, GST_RTSP_STS_OK,
+      gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
+
+  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 = make_base_url (client, ctx->uri, path);
+  g_free (path);
+
+  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 (ctx->response, (guint8 *) str, strlen (str));
+  gst_sdp_message_free (sdp);
+
+  send_message (client, ctx, ctx->response, FALSE);
+
+  g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
+      0, ctx);
+
+  return TRUE;
+
+  /* ERRORS */
+no_uri:
+  {
+    GST_ERROR ("client %p: no uri", client);
+    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;
+  }
+unsupported_mode:
+  {
+    GST_ERROR ("client %p: media does not support DESCRIBE", client);
+    send_generic_response (client, GST_RTSP_STS_METHOD_NOT_ALLOWED, ctx);
+    g_free (path);
+    g_object_unref (media);
+    return FALSE;
+  }
+no_sdp:
+  {
+    GST_ERROR ("client %p: can't create SDP", client);
+    send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
+    g_free (path);
+    g_object_unref (media);
+    return FALSE;
+  }
+}
+
+static gboolean
+handle_sdp (GstRTSPClient * client, GstRTSPContext * ctx, GstRTSPMedia * media,
+    GstSDPMessage * sdp)
+{
+  GstRTSPClientPrivate *priv = client->priv;
+  GstRTSPThread *thread;
+
+  /* create an SDP for the media object */
+  if (!gst_rtsp_media_handle_sdp (media, sdp))
+    goto unhandled_sdp;
+
+  thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
+      GST_RTSP_THREAD_TYPE_MEDIA, ctx);
+  if (thread == NULL)
+    goto no_thread;
+
+  /* prepare the media */
+  if (!gst_rtsp_media_prepare (media, thread))
+    goto no_prepare;
+
+  return TRUE;
+
+  /* ERRORS */
+unhandled_sdp:
+  {
+    GST_ERROR ("client %p: could not handle SDP", client);
+    return FALSE;
+  }
+no_thread:
+  {
+    GST_ERROR ("client %p: can't create thread", client);
+    return FALSE;
+  }
+no_prepare:
+  {
+    GST_ERROR ("client %p: can't prepare media", client);
+    return FALSE;
+  }
+}
+
+static gboolean
+handle_announce_request (GstRTSPClient * client, GstRTSPContext * ctx)
+{
+  GstRTSPClientPrivate *priv = client->priv;
+  GstRTSPClientClass *klass;
+  GstSDPResult sres;
+  GstSDPMessage *sdp;
+  GstRTSPMedia *media;
+  gchar *path, *cont = NULL;
+  guint8 *data;
+  guint size;
+
+  klass = GST_RTSP_CLIENT_GET_CLASS (client);
+
+  if (!ctx->uri)
+    goto no_uri;
+
+  if (!priv->mount_points)
+    goto no_mount_points;
+
+  /* check if reply is SDP */
+  gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_CONTENT_TYPE, &cont,
+      0);
+  /* could not be set but since the request returned OK, we assume it
+   * was SDP, else check it. */
+  if (cont) {
+    if (g_ascii_strcasecmp (cont, "application/sdp") != 0)
+      goto wrong_content_type;
+  }
+
+  /* get message body and parse as SDP */
+  gst_rtsp_message_get_body (ctx->request, &data, &size);
+  if (data == NULL || size == 0)
+    goto no_message;
+
+  GST_DEBUG ("client %p: parse SDP...", client);
+  gst_sdp_message_new (&sdp);
+  sres = gst_sdp_message_parse_buffer (data, size, sdp);
+  if (sres != GST_SDP_OK)
+    goto sdp_parse_failed;
+
+  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, ctx, path, NULL)))
+    goto no_media;
+
+  if (!(gst_rtsp_media_get_transport_mode (media) &
+          GST_RTSP_TRANSPORT_MODE_RECORD))
+    goto unsupported_mode;
+
+  /* Tell client subclass about the media */
+  if (!klass->handle_sdp (client, ctx, media, sdp))
+    goto unhandled_sdp;
+
+  /* we suspend after the announce */
+  gst_rtsp_media_suspend (media);
+  g_object_unref (media);
+
+  gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
+      gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
+
+  send_message (client, ctx, ctx->response, FALSE);
+
+  g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_ANNOUNCE_REQUEST],
+      0, ctx);
+
+  gst_sdp_message_free (sdp);
+  g_free (path);
+  return TRUE;
+
+no_uri:
+  {
+    GST_ERROR ("client %p: no uri", client);
+    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);
+    gst_sdp_message_free (sdp);
+    return FALSE;
+  }
+wrong_content_type:
+  {
+    GST_ERROR ("client %p: unknown content type", client);
+    send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
+    return FALSE;
+  }
+no_message:
+  {
+    GST_ERROR ("client %p: can't find SDP message", client);
+    send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
+    return FALSE;
+  }
+sdp_parse_failed:
+  {
+    GST_ERROR ("client %p: failed to parse SDP message", client);
+    send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
+    gst_sdp_message_free (sdp);
+    return FALSE;
+  }
+no_media:
+  {
+    GST_ERROR ("client %p: no media", client);
+    g_free (path);
+    /* error reply is already sent */
+    gst_sdp_message_free (sdp);
+    return FALSE;
+  }
+unsupported_mode:
+  {
+    GST_ERROR ("client %p: media does not support ANNOUNCE", client);
+    send_generic_response (client, GST_RTSP_STS_METHOD_NOT_ALLOWED, ctx);
+    g_free (path);
+    g_object_unref (media);
+    gst_sdp_message_free (sdp);
+    return FALSE;
+  }
+unhandled_sdp:
+  {
+    GST_ERROR ("client %p: can't handle SDP", client);
+    send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_MEDIA_TYPE, ctx);
+    g_free (path);
+    g_object_unref (media);
+    gst_sdp_message_free (sdp);
+    return FALSE;
+  }
+}
+
+static gboolean
+handle_record_request (GstRTSPClient * client, GstRTSPContext * ctx)
+{
+  GstRTSPSession *session;
+  GstRTSPClientClass *klass;
+  GstRTSPSessionMedia *sessmedia;
+  GstRTSPMedia *media;
+  GstRTSPUrl *uri;
+  GstRTSPState rtspstate;
+  gchar *path;
+  gint matched;
+
+  if (!(session = ctx->session))
+    goto no_session;
+
+  if (!(uri = ctx->uri))
+    goto no_uri;
+
+  klass = GST_RTSP_CLIENT_GET_CLASS (client);
+  path = klass->make_path_from_uri (client, uri);
 
-    res =
-        gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_ACCEPT,
-        &accept, i);
-    if (res == GST_RTSP_ENOTIMPL)
-      break;
+  /* get a handle to the configuration of the media in the session */
+  sessmedia = gst_rtsp_session_get_media (session, path, &matched);
+  if (!sessmedia)
+    goto not_found;
 
-    if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
-      break;
-  }
+  if (path[matched] != '\0')
+    goto no_aggregate;
 
-  if (!priv->mount_points)
-    goto no_mount_points;
+  g_free (path);
 
-  if (!(path = gst_rtsp_mount_points_make_path (priv->mount_points, ctx->uri)))
-    goto no_path;
+  ctx->sessmedia = sessmedia;
+  ctx->media = media = gst_rtsp_session_media_get_media (sessmedia);
 
-  /* find the media object for the uri */
-  if (!(media = find_media (client, ctx, path, NULL)))
-    goto no_media;
+  if (!(gst_rtsp_media_get_transport_mode (media) &
+          GST_RTSP_TRANSPORT_MODE_RECORD))
+    goto unsupported_mode;
 
-  /* create an SDP for the media object on this client */
-  if (!(sdp = klass->create_sdp (client, media)))
-    goto no_sdp;
+  /* 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;
 
-  g_object_unref (media);
+  /* in play we first unsuspend, media could be suspended from SDP or PAUSED */
+  if (!gst_rtsp_media_unsuspend (media))
+    goto unsuspend_failed;
 
   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 (ctx->response, GST_RTSP_HDR_CONTENT_TYPE,
-      "application/sdp");
-
-  /* content base for some clients that might screw up creating the setup uri */
-  str = make_base_url (client, ctx->uri, path);
-  g_free (path);
-
-  GST_INFO ("adding content-base: %s", str);
-  gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_CONTENT_BASE, str);
+  send_message (client, ctx, ctx->response, FALSE);
 
-  /* add SDP to the response body */
-  str = gst_sdp_message_as_text (sdp);
-  gst_rtsp_message_take_body (ctx->response, (guint8 *) str, strlen (str));
-  gst_sdp_message_free (sdp);
+  /* start playing after sending the response */
+  gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
 
-  send_message (client, ctx->session, ctx->response, FALSE);
+  gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
 
-  g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
-      0, ctx);
+  g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_RECORD_REQUEST], 0,
+      ctx);
 
   return TRUE;
 
   /* ERRORS */
+no_session:
+  {
+    GST_ERROR ("client %p: no session", client);
+    send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
+    return FALSE;
+  }
 no_uri:
   {
-    GST_ERROR ("client %p: no uri", client);
+    GST_ERROR ("client %p: no uri supplied", client);
     send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
     return FALSE;
   }
-no_mount_points:
+not_found:
   {
-    GST_ERROR ("client %p: no mount points configured", client);
+    GST_ERROR ("client %p: media not found", client);
     send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
     return FALSE;
   }
-no_path:
+no_aggregate:
   {
-    GST_ERROR ("client %p: can't find path for url", client);
-    send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
+    GST_ERROR ("client %p: no aggregate path %s", client, path);
+    send_generic_response (client,
+        GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
+    g_free (path);
     return FALSE;
   }
-no_media:
+unsupported_mode:
   {
-    GST_ERROR ("client %p: no media", client);
-    g_free (path);
-    /* error reply is already sent */
+    GST_ERROR ("client %p: media does not support RECORD", client);
+    send_generic_response (client, GST_RTSP_STS_METHOD_NOT_ALLOWED, ctx);
     return FALSE;
   }
-no_sdp:
+invalid_state:
   {
-    GST_ERROR ("client %p: can't create SDP", client);
+    GST_ERROR ("client %p: not PLAYING or READY", client);
+    send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
+        ctx);
+    return FALSE;
+  }
+unsuspend_failed:
+  {
+    GST_ERROR ("client %p: unsuspend failed", client);
     send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
-    g_free (path);
-    g_object_unref (media);
     return FALSE;
   }
 }
@@ -1809,7 +2549,7 @@ handle_options_request (GstRTSPClient * client, GstRTSPContext * ctx)
   gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_PUBLIC, str);
   g_free (str);
 
-  send_message (client, ctx->session, ctx->response, FALSE);
+  send_message (client, ctx, ctx->response, FALSE);
 
   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
       0, ctx);
@@ -1844,22 +2584,71 @@ sanitize_uri (GstRTSPUrl * uri)
   *d = '\0';
 }
 
+/* is called when the session is removed from its session pool. */
 static void
-client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
+client_session_removed (GstRTSPSessionPool * pool, GstRTSPSession * session,
+    GstRTSPClient * client)
 {
   GstRTSPClientPrivate *priv = client->priv;
 
-  GST_INFO ("client %p: session %p finished", client, session);
+  GST_INFO ("client %p: session %p removed", client, session);
 
-  /* unlink all media managed in this session */
-  client_unlink_session (client, session);
+  g_mutex_lock (&priv->lock);
+  if (priv->watch != NULL)
+    gst_rtsp_watch_set_send_backlog (priv->watch, 0, 0);
+  client_unwatch_session (client, session, NULL);
+  if (priv->watch != NULL)
+    gst_rtsp_watch_set_send_backlog (priv->watch, 0, WATCH_BACKLOG_SIZE);
+  g_mutex_unlock (&priv->lock);
+}
 
-  /* remove the session */
-  if (!(priv->sessions = g_list_remove (priv->sessions, session))) {
-    GST_INFO ("client %p: all sessions finalized, close the connection",
-        client);
-    close_connection (client);
+/* Returns TRUE if there are no Require headers, otherwise returns FALSE
+ * and also returns a newly-allocated string of (comma-separated) unsupported
+ * options in the unsupported_reqs variable .
+ *
+ * There may be multiple Require headers, but we must send one single
+ * Unsupported header with all the unsupported options as response. If
+ * an incoming Require header contained a comma-separated list of options
+ * GstRtspConnection will already have split that list up into multiple
+ * headers.
+ *
+ * TODO: allow the application to decide what features are supported
+ */
+static gboolean
+check_request_requirements (GstRTSPMessage * msg, gchar ** unsupported_reqs)
+{
+  GstRTSPResult res;
+  GPtrArray *arr = NULL;
+  gchar *reqs = NULL;
+  gint i;
+
+  i = 0;
+  do {
+    res = gst_rtsp_message_get_header (msg, GST_RTSP_HDR_REQUIRE, &reqs, i++);
+
+    if (res == GST_RTSP_ENOTIMPL)
+      break;
+
+    if (arr == NULL)
+      arr = g_ptr_array_new_with_free_func ((GDestroyNotify) g_free);
+
+    g_ptr_array_add (arr, g_strdup (reqs));
   }
+  while (TRUE);
+
+  /* if we don't have any Require headers at all, all is fine */
+  if (i == 1)
+    return TRUE;
+
+  /* otherwise we've now processed at all the Require headers */
+  g_ptr_array_add (arr, NULL);
+
+  /* for now we don't commit to supporting anything, so will just report
+   * all of the required options as unsupported */
+  *unsupported_reqs = g_strjoinv (", ", (gchar **) arr->pdata);
+
+  g_ptr_array_unref (arr);
+  return FALSE;
 }
 
 static void
@@ -1874,6 +2663,7 @@ handle_request (GstRTSPClient * client, GstRTSPMessage * request)
   GstRTSPSession *session = NULL;
   GstRTSPContext sctx = { NULL }, *ctx;
   GstRTSPMessage response = { 0 };
+  gchar *unsupported_reqs = NULL;
   gchar *sessid;
 
   if (!(ctx = gst_rtsp_context_get_current ())) {
@@ -1891,10 +2681,12 @@ handle_request (GstRTSPClient * client, GstRTSPMessage * request)
     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;
@@ -1904,8 +2696,35 @@ handle_request (GstRTSPClient * client, GstRTSPMessage * request)
   /* 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);
@@ -1932,6 +2751,41 @@ handle_request (GstRTSPClient * client, GstRTSPMessage * request)
   if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
     goto not_authorized;
 
+  /* handle any 'Require' headers */
+  if (!check_request_requirements (ctx->request, &unsupported_reqs))
+    goto unsupported_requirement;
+
+  /* the backlog must be unlimited while processing requests.
+   * the causes of this are two cases of deadlocks while streaming over TCP:
+   *
+   * 1. consider the scenario where the media pipeline's streaming thread
+   * is blocking in the appsink (taking the appsink's preroll lock) because
+   * the backlog is full. when a PAUSE request is received by the RTSP
+   * client thread then the the state of the session media ought to change
+   * to PAUSED. while most elements in the pipeline can change state this
+   * can never happen for the appsink since its preroll lock is taken by
+   * another thread.
+   *
+   * 2. consider the scenario where the media pipeline's streaming thread
+   * is blocking in the appsink new_sample callback (taking the send lock
+   * in RTSP client) because the backlog is full. when e.g. a GET request
+   * is received by the RTSP client thread then a response ought to be sent
+   * but this can never happen since it requires taking the send lock
+   * already taken by another thread.
+   *
+   * the reason that the backlog is never emptied is that the source used
+   * for dequeing messages from the backlog is never dispatched because it
+   * is attached to the same mainloop as the source receving RTSP requests and
+   * therefore run by the RTSP client thread which is alreayd blocking.
+   *
+   * without significant changes the easiest way to cope with this is to
+   * not block indefinitely when the backlog is full, but rather let the
+   * backlog grow in size. this in effect means that there can not be any
+   * upper boundary on its size.
+   */
+  if (priv->watch != NULL)
+    gst_rtsp_watch_set_send_backlog (priv->watch, 0, 0);
+
   /* now see what is asked and dispatch to a dedicated handler */
   switch (method) {
     case GST_RTSP_OPTIONS:
@@ -1959,14 +2813,25 @@ handle_request (GstRTSPClient * client, GstRTSPMessage * request)
       handle_get_param_request (client, ctx);
       break;
     case GST_RTSP_ANNOUNCE:
+      handle_announce_request (client, ctx);
+      break;
     case GST_RTSP_RECORD:
+      handle_record_request (client, ctx);
+      break;
     case GST_RTSP_REDIRECT:
+      if (priv->watch != NULL)
+        gst_rtsp_watch_set_send_backlog (priv->watch, 0, WATCH_BACKLOG_SIZE);
       goto not_implemented;
     case GST_RTSP_INVALID:
     default:
+      if (priv->watch != NULL)
+        gst_rtsp_watch_set_send_backlog (priv->watch, 0, WATCH_BACKLOG_SIZE);
       goto bad_request;
   }
 
+  if (priv->watch != NULL)
+    gst_rtsp_watch_set_send_backlog (priv->watch, 0, WATCH_BACKLOG_SIZE);
+
 done:
   if (ctx == &sctx)
     gst_rtsp_context_pop_current (ctx);
@@ -2008,6 +2873,14 @@ not_authorized:
     /* error reply is already sent */
     goto done;
   }
+unsupported_requirement:
+  {
+    GST_ERROR ("client %p: Required option is not supported (%s)", client,
+        unsupported_reqs);
+    send_option_not_supported_response (client, ctx, unsupported_reqs);
+    g_free (unsupported_reqs);
+    goto done;
+  }
 not_implemented:
   {
     GST_ERROR ("client %p: method %d not implemented", client, method);
@@ -2092,54 +2965,53 @@ handle_data (GstRTSPClient * client, GstRTSPMessage * message)
   GstRTSPClientPrivate *priv = client->priv;
   GstRTSPResult res;
   guint8 channel;
-  GList *walk;
   guint8 *data;
   guint size;
   GstBuffer *buffer;
-  gboolean handled;
+  GstRTSPStreamTransport *trans;
 
   /* find the stream for this message */
   res = gst_rtsp_message_parse_data (message, &channel);
   if (res != GST_RTSP_OK)
     return;
 
+  gst_rtsp_message_get_body (message, &data, &size);
+  if (size < 2)
+    goto invalid_length;
+
   gst_rtsp_message_steal_body (message, &data, &size);
 
-  buffer = gst_buffer_new_wrapped (data, size);
+  /* Strip trailing \0 (which GstRTSPConnection adds) */
+  --size;
 
-  handled = FALSE;
-  for (walk = priv->transports; walk; walk = g_list_next (walk)) {
-    GstRTSPStreamTransport *trans;
-    GstRTSPStream *stream;
-    const GstRTSPTransport *tr;
+  buffer = gst_buffer_new_wrapped (data, size);
 
-    trans = walk->data;
+  trans =
+      g_hash_table_lookup (priv->transports, GINT_TO_POINTER ((gint) channel));
+  if (trans) {
+    /* dispatch to the stream based on the channel number */
+    GST_LOG_OBJECT (client, "%u bytes of data on channel %u", size, channel);
+    gst_rtsp_stream_transport_recv_data (trans, channel, buffer);
+  } else {
+    GST_DEBUG_OBJECT (client, "received %u bytes of data for "
+        "unknown channel %u", size, channel);
+    gst_buffer_unref (buffer);
+  }
 
-    tr = gst_rtsp_stream_transport_get_transport (trans);
-    stream = gst_rtsp_stream_transport_get_stream (trans);
+  return;
 
-    /* check for TCP transport */
-    if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
-      /* dispatch to the stream based on the channel number */
-      if (tr->interleaved.min == channel) {
-        gst_rtsp_stream_recv_rtp (stream, buffer);
-        handled = TRUE;
-        break;
-      } else if (tr->interleaved.max == channel) {
-        gst_rtsp_stream_recv_rtcp (stream, buffer);
-        handled = TRUE;
-        break;
-      }
-    }
+/* ERRORS */
+invalid_length:
+  {
+    GST_DEBUG ("client %p: Short message received, ignoring", client);
+    return;
   }
-  if (!handled)
-    gst_buffer_unref (buffer);
 }
 
 /**
  * gst_rtsp_client_set_session_pool:
  * @client: a #GstRTSPClient
- * @pool: a #GstRTSPSessionPool
+ * @pool: (transfer none): a #GstRTSPSessionPool
  *
  * Set @pool as the sessionpool for @client which it will use to find
  * or allocate sessions. the sessionpool is usually inherited from the server
@@ -2162,8 +3034,14 @@ gst_rtsp_client_set_session_pool (GstRTSPClient * client,
   g_mutex_lock (&priv->lock);
   old = priv->session_pool;
   priv->session_pool = pool;
+
+  if (priv->session_removed_id) {
+    g_signal_handler_disconnect (old, priv->session_removed_id);
+    priv->session_removed_id = 0;
+  }
   g_mutex_unlock (&priv->lock);
 
+  /* FIXME, should remove all sessions from the old pool for this client */
   if (old)
     g_object_unref (old);
 }
@@ -2197,7 +3075,7 @@ gst_rtsp_client_get_session_pool (GstRTSPClient * client)
 /**
  * gst_rtsp_client_set_mount_points:
  * @client: a #GstRTSPClient
- * @mounts: a #GstRTSPMountPoints
+ * @mounts: (transfer none): a #GstRTSPMountPoints
  *
  * Set @mounts as the mount points for @client which it will use to map urls
  * to media streams. These mount points are usually inherited from the server that
@@ -2255,7 +3133,7 @@ gst_rtsp_client_get_mount_points (GstRTSPClient * client)
 /**
  * gst_rtsp_client_set_auth:
  * @client: a #GstRTSPClient
- * @auth: a #GstRTSPAuth
+ * @auth: (transfer none): a #GstRTSPAuth
  *
  * configure @auth to be used as the authentication manager of @client.
  */
@@ -2312,7 +3190,7 @@ gst_rtsp_client_get_auth (GstRTSPClient * client)
 /**
  * gst_rtsp_client_set_thread_pool:
  * @client: a #GstRTSPClient
- * @pool: a #GstRTSPThreadPool
+ * @pool: (transfer none): a #GstRTSPThreadPool
  *
  * configure @pool to be used as the thread pool of @client.
  */
@@ -2451,9 +3329,9 @@ gst_rtsp_client_get_connection (GstRTSPClient * client)
 /**
  * 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
+ * @func: (scope notified): a #GstRTSPClientSendFunc
+ * @user_data: (closure): user data passed to @func
+ * @notify: (allow-none): 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
@@ -2489,7 +3367,7 @@ gst_rtsp_client_set_send_func (GstRTSPClient * client,
 /**
  * gst_rtsp_client_handle_message:
  * @client: a #GstRTSPClient
- * @message: an #GstRTSPMessage
+ * @message: (transfer none): an #GstRTSPMessage
  *
  * Let the client handle @message.
  *
@@ -2521,8 +3399,9 @@ gst_rtsp_client_handle_message (GstRTSPClient * client,
 /**
  * gst_rtsp_client_send_message:
  * @client: a #GstRTSPClient
- * @session: a #GstRTSPSession to send the message to or %NULL
- * @message: The #GstRTSPMessage to send
+ * @session: (allow-none) (transfer none): a #GstRTSPSession to send
+ *   the message to or %NULL
+ * @message: (transfer none): The #GstRTSPMessage to send
  *
  * Send a message message to the remote end. @message must be a
  * #GST_RTSP_MESSAGE_REQUEST or a #GST_RTSP_MESSAGE_RESPONSE.
@@ -2531,12 +3410,31 @@ GstRTSPResult
 gst_rtsp_client_send_message (GstRTSPClient * client, GstRTSPSession * session,
     GstRTSPMessage * message)
 {
+  GstRTSPContext sctx = { NULL }
+  , *ctx;
+  GstRTSPClientPrivate *priv;
+
   g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
   g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
   g_return_val_if_fail (message->type == GST_RTSP_MESSAGE_REQUEST ||
       message->type == GST_RTSP_MESSAGE_RESPONSE, GST_RTSP_EINVAL);
 
-  send_message (client, session, message, FALSE);
+  priv = client->priv;
+
+  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->session = session;
+
+  send_message (client, ctx, message, FALSE);
+
+  if (ctx == &sctx)
+    gst_rtsp_context_pop_current (ctx);
 
   return GST_RTSP_OK;
 }
@@ -2546,11 +3444,42 @@ do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
     gboolean close, gpointer user_data)
 {
   GstRTSPClientPrivate *priv = client->priv;
+  GstRTSPResult ret;
+  GTimeVal time;
+
+  time.tv_sec = 1;
+  time.tv_usec = 0;
+
+  do {
+    /* send the response and store the seq number so we can wait until it's
+     * written to the client to close the connection */
+    ret =
+        gst_rtsp_watch_send_message (priv->watch, message,
+        close ? &priv->close_seq : NULL);
+    if (ret == GST_RTSP_OK)
+      break;
+
+    if (ret != GST_RTSP_ENOMEM)
+      goto error;
+
+    /* drop backlog */
+    if (priv->drop_backlog)
+      break;
+
+    /* queue was full, wait for more space */
+    GST_DEBUG_OBJECT (client, "waiting for backlog");
+    ret = gst_rtsp_watch_wait_backlog (priv->watch, &time);
+    GST_DEBUG_OBJECT (client, "Resend due to backlog full");
+  } while (ret != GST_RTSP_EINTR);
 
-  /* 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);
+  return ret;
+
+  /* ERRORS */
+error:
+  {
+    GST_DEBUG_OBJECT (client, "got error %d", ret);
+    return ret;
+  }
 }
 
 static GstRTSPResult
@@ -2567,8 +3496,9 @@ message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
   GstRTSPClientPrivate *priv = client->priv;
 
   if (priv->close_seq && priv->close_seq == cseq) {
+    GST_INFO ("client %p: send close message", client);
     priv->close_seq = 0;
-    close_connection (client);
+    gst_rtsp_client_close (client);
   }
 
   return GST_RTSP_OK;
@@ -2590,7 +3520,10 @@ closed (GstRTSPWatch * watch, gpointer user_data)
     g_mutex_unlock (&tunnels_lock);
   }
 
+  gst_rtsp_watch_set_flushing (watch, TRUE);
+  g_mutex_lock (&priv->watch_lock);
   gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
+  g_mutex_unlock (&priv->watch_lock);
 
   return GST_RTSP_OK;
 }
@@ -2662,28 +3595,6 @@ tunnel_existed:
   }
 }
 
-static GstRTSPStatusCode
-tunnel_start (GstRTSPWatch * watch, gpointer user_data)
-{
-  GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
-  GstRTSPClientPrivate *priv = client->priv;
-
-  GST_INFO ("client %p: tunnel start (connection %p)", client,
-      priv->connection);
-
-  if (!remember_tunnel (client))
-    goto tunnel_error;
-
-  return GST_RTSP_STS_OK;
-
-  /* ERRORS */
-tunnel_error:
-  {
-    GST_ERROR ("client %p: error starting tunnel", client);
-    return GST_RTSP_STS_SERVICE_UNAVAILABLE;
-  }
-}
-
 static GstRTSPResult
 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
 {
@@ -2699,66 +3610,119 @@ tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
   return GST_RTSP_OK;
 }
 
-static GstRTSPResult
-tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
+static gboolean
+handle_tunnel (GstRTSPClient * client)
 {
-  const gchar *tunnelid;
-  GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
   GstRTSPClientPrivate *priv = client->priv;
   GstRTSPClient *oclient;
   GstRTSPClientPrivate *opriv;
+  const gchar *tunnelid;
 
-  GST_INFO ("client %p: tunnel complete", client);
-
-  /* find previous tunnel */
   tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
   if (tunnelid == NULL)
     goto no_tunnelid;
 
+  /* check for previous tunnel */
   g_mutex_lock (&tunnels_lock);
-  if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
-    goto no_tunnel;
+  oclient = g_hash_table_lookup (tunnels, tunnelid);
 
-  /* remove the old client from the table. ref before because removing it will
-   * remove the ref to it. */
-  g_object_ref (oclient);
-  g_hash_table_remove (tunnels, tunnelid);
+  if (oclient == NULL) {
+    /* no previous tunnel, remember tunnel */
+    g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
+    g_mutex_unlock (&tunnels_lock);
 
-  opriv = oclient->priv;
+    GST_INFO ("client %p: no previous tunnel found, remembering tunnel (%p)",
+        client, priv->connection);
+  } else {
+    /* merge both tunnels into the first client */
+    /* remove the old client from the table. ref before because removing it will
+     * remove the ref to it. */
+    g_object_ref (oclient);
+    g_hash_table_remove (tunnels, tunnelid);
+    g_mutex_unlock (&tunnels_lock);
 
-  if (opriv->watch == NULL)
-    goto tunnel_closed;
-  g_mutex_unlock (&tunnels_lock);
+    opriv = oclient->priv;
 
-  GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
-      opriv->connection, priv->connection);
+    g_mutex_lock (&opriv->watch_lock);
+    if (opriv->watch == NULL)
+      goto tunnel_closed;
 
-  /* merge the tunnels into the first client */
-  gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
-  gst_rtsp_watch_reset (opriv->watch);
-  g_object_unref (oclient);
+    GST_INFO ("client %p: found previous tunnel %p (old %p, new %p)", client,
+        oclient, opriv->connection, priv->connection);
 
-  return GST_RTSP_OK;
+    gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
+    gst_rtsp_watch_reset (priv->watch);
+    gst_rtsp_watch_reset (opriv->watch);
+    g_mutex_unlock (&opriv->watch_lock);
+    g_object_unref (oclient);
+
+    /* the old client owns the tunnel now, the new one will be freed */
+    g_source_destroy ((GSource *) priv->watch);
+    priv->watch = NULL;
+    gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
+  }
+
+  return TRUE;
 
   /* ERRORS */
 no_tunnelid:
   {
     GST_ERROR ("client %p: no tunnelid provided", client);
-    return GST_RTSP_ERROR;
-  }
-no_tunnel:
-  {
-    g_mutex_unlock (&tunnels_lock);
-    GST_ERROR ("client %p: tunnel session %s not found", client, tunnelid);
-    return GST_RTSP_ERROR;
+    return FALSE;
   }
 tunnel_closed:
   {
-    g_mutex_unlock (&tunnels_lock);
     GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
+    g_mutex_unlock (&opriv->watch_lock);
     g_object_unref (oclient);
+    return FALSE;
+  }
+}
+
+static GstRTSPStatusCode
+tunnel_get (GstRTSPWatch * watch, gpointer user_data)
+{
+  GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
+
+  GST_INFO ("client %p: tunnel get (connection %p)", client,
+      client->priv->connection);
+
+  if (!handle_tunnel (client)) {
+    return GST_RTSP_STS_SERVICE_UNAVAILABLE;
+  }
+
+  return GST_RTSP_STS_OK;
+}
+
+static GstRTSPResult
+tunnel_post (GstRTSPWatch * watch, gpointer user_data)
+{
+  GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
+
+  GST_INFO ("client %p: tunnel post (connection %p)", client,
+      client->priv->connection);
+
+  if (!handle_tunnel (client)) {
     return GST_RTSP_ERROR;
   }
+
+  return GST_RTSP_OK;
+}
+
+static GstRTSPResult
+tunnel_http_response (GstRTSPWatch * watch, GstRTSPMessage * request,
+    GstRTSPMessage * response, gpointer user_data)
+{
+  GstRTSPClientClass *klass;
+
+  GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
+  klass = GST_RTSP_CLIENT_GET_CLASS (client);
+
+  if (klass->tunnel_http_response) {
+    klass->tunnel_http_response (client, request, response);
+  }
+
+  return GST_RTSP_OK;
 }
 
 static GstRTSPWatchFuncs watch_funcs = {
@@ -2766,10 +3730,11 @@ static GstRTSPWatchFuncs watch_funcs = {
   message_sent,
   closed,
   error,
-  tunnel_start,
-  tunnel_complete,
+  tunnel_get,
+  tunnel_post,
   error_full,
-  tunnel_lost
+  tunnel_lost,
+  tunnel_http_response
 };
 
 static void
@@ -2779,6 +3744,8 @@ client_watch_notify (GstRTSPClient * client)
 
   GST_INFO ("client %p: watch destroyed", client);
   priv->watch = NULL;
+  /* remove all sessions and so drop the extra client ref */
+  gst_rtsp_client_session_filter (client, cleanup_session, NULL);
   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
   g_object_unref (client);
 }
@@ -2789,7 +3756,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
@@ -2808,17 +3775,18 @@ gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
   g_return_val_if_fail (priv->connection != NULL, 0);
   g_return_val_if_fail (priv->watch == NULL, 0);
 
+  /* make sure noone will free the context before the watch is destroyed */
+  priv->watch_context = g_main_context_ref (context);
+
   /* create watch for the connection and attach */
   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_rtsp_watch_set_send_backlog (priv->watch, 0, WATCH_BACKLOG_SIZE);
 
-  GST_INFO ("attaching to context %p", context);
+  GST_INFO ("client %p: attaching to context %p", client, context);
   res = gst_rtsp_watch_attach (priv->watch, context);
 
   return res;
@@ -2827,7 +3795,7 @@ gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
 /**
  * gst_rtsp_client_session_filter:
  * @client: a #GstRTSPClient
- * @func: (scope call): a callback
+ * @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
@@ -2843,6 +3811,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.
@@ -2853,24 +3823,50 @@ gst_rtsp_client_session_filter (GstRTSPClient * client,
 {
   GstRTSPClientPrivate *priv;
   GList *result, *walk, *next;
+  GHashTable *visited;
+  guint cookie;
 
   g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
-  g_return_val_if_fail (func != NULL, NULL);
 
   priv = client->priv;
 
   result = NULL;
+  if (func)
+    visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
 
   g_mutex_lock (&priv->lock);
+restart:
+  cookie = priv->sessions_cookie;
   for (walk = priv->sessions; walk; walk = next) {
     GstRTSPSession *sess = walk->data;
+    GstRTSPFilterResult res;
+    gboolean changed;
 
     next = g_list_next (walk);
 
-    switch (func (client, sess, user_data)) {
+    if (func) {
+      /* only visit each session once */
+      if (g_hash_table_contains (visited, sess))
+        continue;
+
+      g_hash_table_add (visited, g_object_ref (sess));
+      g_mutex_unlock (&priv->lock);
+
+      res = func (client, sess, user_data);
+
+      g_mutex_lock (&priv->lock);
+    } else
+      res = GST_RTSP_FILTER_REF;
+
+    changed = (cookie != priv->sessions_cookie);
+
+    switch (res) {
       case GST_RTSP_FILTER_REMOVE:
-        /* stop watching the session and pretent it went away */
-        client_cleanup_session (client, sess);
+        /* stop watching the session and pretend it went away, if the list was
+         * changed, we can't use the current list position, try to see if we
+         * still have the session */
+        client_unwatch_session (client, sess, changed ? NULL : walk);
+        cookie = priv->sessions_cookie;
         break;
       case GST_RTSP_FILTER_REF:
         result = g_list_prepend (result, g_object_ref (sess));
@@ -2879,8 +3875,13 @@ gst_rtsp_client_session_filter (GstRTSPClient * client,
       default:
         break;
     }
+    if (changed)
+      goto restart;
   }
   g_mutex_unlock (&priv->lock);
 
+  if (func)
+    g_hash_table_unref (visited);
+
   return result;
 }