rtsp-media: Make sure that sequence numbers are monotonic after pause
[platform/upstream/gstreamer.git] / gst / rtsp-server / rtsp-media.c
index fa9821a..6b81e25 100644 (file)
@@ -174,7 +174,10 @@ static void gst_rtsp_media_finalize (GObject * obj);
 static gboolean default_handle_message (GstRTSPMedia * media,
     GstMessage * message);
 static void finish_unprepare (GstRTSPMedia * media);
+static gboolean default_prepare (GstRTSPMedia * media, GstRTSPThread * thread);
 static gboolean default_unprepare (GstRTSPMedia * media);
+static gboolean default_suspend (GstRTSPMedia * media);
+static gboolean default_unsuspend (GstRTSPMedia * media);
 static gboolean default_convert_range (GstRTSPMedia * media,
     GstRTSPTimeRange * range, GstRTSPRangeUnit unit);
 static gboolean default_query_position (GstRTSPMedia * media,
@@ -285,27 +288,30 @@ gst_rtsp_media_class_init (GstRTSPMediaClass * klass)
   gst_rtsp_media_signals[SIGNAL_PREPARED] =
       g_signal_new ("prepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
       G_STRUCT_OFFSET (GstRTSPMediaClass, prepared), 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_media_signals[SIGNAL_UNPREPARED] =
       g_signal_new ("unprepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
       G_STRUCT_OFFSET (GstRTSPMediaClass, unprepared), 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_media_signals[SIGNAL_TARGET_STATE] =
       g_signal_new ("target-state", G_TYPE_FROM_CLASS (klass),
-      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPMediaClass, new_state), NULL,
-      NULL, g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
+      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPMediaClass, target_state),
+      NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_INT);
 
   gst_rtsp_media_signals[SIGNAL_NEW_STATE] =
       g_signal_new ("new-state", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
       G_STRUCT_OFFSET (GstRTSPMediaClass, new_state), NULL, NULL,
-      g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
+      g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_INT);
 
   GST_DEBUG_CATEGORY_INIT (rtsp_media_debug, "rtspmedia", 0, "GstRTSPMedia");
 
   klass->handle_message = default_handle_message;
+  klass->prepare = default_prepare;
   klass->unprepare = default_unprepare;
+  klass->suspend = default_suspend;
+  klass->unsuspend = default_unsuspend;
   klass->convert_range = default_convert_range;
   klass->query_position = default_query_position;
   klass->query_stop = default_query_stop;
@@ -446,28 +452,74 @@ gst_rtsp_media_set_property (GObject * object, guint propid,
   }
 }
 
+typedef struct
+{
+  gint64 position;
+  gboolean ret;
+} DoQueryPositionData;
+
+static void
+do_query_position (GstRTSPStream * stream, DoQueryPositionData * data)
+{
+  gint64 tmp;
+
+  if (gst_rtsp_stream_query_position (stream, &tmp)) {
+    data->position = MAX (data->position, tmp);
+    data->ret = TRUE;
+  }
+}
+
 static gboolean
 default_query_position (GstRTSPMedia * media, gint64 * position)
 {
-  return gst_element_query_position (media->priv->pipeline, GST_FORMAT_TIME,
-      position);
+  GstRTSPMediaPrivate *priv;
+  DoQueryPositionData data;
+
+  priv = media->priv;
+
+  data.position = -1;
+  data.ret = FALSE;
+
+  g_ptr_array_foreach (priv->streams, (GFunc) do_query_position, &data);
+
+  *position = data.position;
+
+  return data.ret;
+}
+
+typedef struct
+{
+  gint64 stop;
+  gboolean ret;
+} DoQueryStopData;
+
+static void
+do_query_stop (GstRTSPStream * stream, DoQueryStopData * data)
+{
+  gint64 tmp;
+
+  if (gst_rtsp_stream_query_stop (stream, &tmp)) {
+    data->stop = MAX (data->stop, tmp);
+    data->ret = TRUE;
+  }
 }
 
 static gboolean
 default_query_stop (GstRTSPMedia * media, gint64 * stop)
 {
-  GstQuery *query;
-  gboolean res;
+  GstRTSPMediaPrivate *priv;
+  DoQueryStopData data;
 
-  query = gst_query_new_segment (GST_FORMAT_TIME);
-  if ((res = gst_element_query (media->priv->pipeline, query))) {
-    GstFormat format;
-    gst_query_parse_segment (query, NULL, &format, NULL, stop);
-    if (format != GST_FORMAT_TIME)
-      *stop = -1;
-  }
-  gst_query_unref (query);
-  return res;
+  priv = media->priv;
+
+  data.stop = -1;
+  data.ret = FALSE;
+
+  g_ptr_array_foreach (priv->streams, (GFunc) do_query_stop, &data);
+
+  *stop = data.stop;
+
+  return data.ret;
 }
 
 static GstElement *
@@ -485,7 +537,7 @@ static void
 collect_media_stats (GstRTSPMedia * media)
 {
   GstRTSPMediaPrivate *priv = media->priv;
-  gint64 position, stop;
+  gint64 position = 0, stop = -1;
 
   if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
       priv->status != GST_RTSP_MEDIA_STATUS_PREPARING)
@@ -1194,7 +1246,6 @@ gst_rtsp_media_collect_streams (GstRTSPMedia * media)
     name = g_strdup_printf ("dynpay%d", i);
     if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
       /* a stream that will dynamically create pads to provide RTP packets */
-
       GST_INFO ("found dynamic element %d, %p", i, elem);
 
       g_mutex_lock (&priv->lock);
@@ -1318,8 +1369,8 @@ gst_rtsp_media_n_streams (GstRTSPMedia * media)
  *
  * Retrieve the stream with index @idx from @media.
  *
- * Returns: (transfer none): the #GstRTSPStream at index @idx or %NULL when a stream with
- * that index did not exist.
+ * Returns: (nullable) (transfer none): the #GstRTSPStream at index
+ * @idx or %NULL when a stream with that index did not exist.
  */
 GstRTSPStream *
 gst_rtsp_media_get_stream (GstRTSPMedia * media, guint idx)
@@ -1348,8 +1399,9 @@ gst_rtsp_media_get_stream (GstRTSPMedia * media, guint idx)
  *
  * Find a stream in @media with @control as the control uri.
  *
- * Returns: (transfer none): the #GstRTSPStream with control uri @control
- * or %NULL when a stream with that control did not exist.
+ * Returns: (nullable) (transfer none): the #GstRTSPStream with
+ * control uri @control or %NULL when a stream with that control did
+ * not exist.
  */
 GstRTSPStream *
 gst_rtsp_media_find_stream (GstRTSPMedia * media, const gchar * control)
@@ -1471,6 +1523,52 @@ media_streams_set_blocked (GstRTSPMedia * media, gboolean blocked)
   g_ptr_array_foreach (priv->streams, (GFunc) stream_update_blocked, media);
 }
 
+static void
+gst_rtsp_media_set_status (GstRTSPMedia * media, GstRTSPMediaStatus status)
+{
+  GstRTSPMediaPrivate *priv = media->priv;
+
+  g_mutex_lock (&priv->lock);
+  priv->status = status;
+  GST_DEBUG ("setting new status to %d", status);
+  g_cond_broadcast (&priv->cond);
+  g_mutex_unlock (&priv->lock);
+}
+
+/**
+ * gst_rtsp_media_get_status:
+ * @media: a #GstRTSPMedia
+ *
+ * Get the status of @media. When @media is busy preparing, this function waits
+ * until @media is prepared or in error.
+ *
+ * Returns: the status of @media.
+ */
+GstRTSPMediaStatus
+gst_rtsp_media_get_status (GstRTSPMedia * media)
+{
+  GstRTSPMediaPrivate *priv = media->priv;
+  GstRTSPMediaStatus result;
+  gint64 end_time;
+
+  g_mutex_lock (&priv->lock);
+  end_time = g_get_monotonic_time () + 20 * G_TIME_SPAN_SECOND;
+  /* while we are preparing, wait */
+  while (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING) {
+    GST_DEBUG ("waiting for status change");
+    if (!g_cond_wait_until (&priv->cond, &priv->lock, end_time)) {
+      GST_DEBUG ("timeout, assuming error status");
+      priv->status = GST_RTSP_MEDIA_STATUS_ERROR;
+    }
+  }
+  /* could be success or error */
+  result = priv->status;
+  GST_DEBUG ("got status %d", result);
+  g_mutex_unlock (&priv->lock);
+
+  return result;
+}
+
 /**
  * gst_rtsp_media_seek:
  * @media: a #GstRTSPMedia
@@ -1543,7 +1641,7 @@ gst_rtsp_media_seek (GstRTSPMedia * media, GstRTSPTimeRange * range)
     GST_INFO ("seeking to %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
         GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
 
-    priv->status = GST_RTSP_MEDIA_STATUS_PREPARING;
+    gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARING);
     if (priv->blocked)
       media_streams_set_blocked (media, TRUE);
 
@@ -1576,7 +1674,7 @@ gst_rtsp_media_seek (GstRTSPMedia * media, GstRTSPTimeRange * range)
         flags |= GST_SEEK_FLAG_KEY_UNIT;
     }
 
-    /* FIXME, we only do forwards */
+    /* FIXME, we only do forwards playback, no trick modes yet */
     res = gst_element_seek (priv->pipeline, 1.0, GST_FORMAT_TIME,
         flags, start_type, start, stop_type, stop);
 
@@ -1625,52 +1723,6 @@ preroll_failed:
 }
 
 static void
-gst_rtsp_media_set_status (GstRTSPMedia * media, GstRTSPMediaStatus status)
-{
-  GstRTSPMediaPrivate *priv = media->priv;
-
-  g_mutex_lock (&priv->lock);
-  priv->status = status;
-  GST_DEBUG ("setting new status to %d", status);
-  g_cond_broadcast (&priv->cond);
-  g_mutex_unlock (&priv->lock);
-}
-
-/**
- * gst_rtsp_media_get_status:
- * @media: a #GstRTSPMedia
- *
- * Get the status of @media. When @media is busy preparing, this function waits
- * until @media is prepared or in error.
- *
- * Returns: the status of @media.
- */
-GstRTSPMediaStatus
-gst_rtsp_media_get_status (GstRTSPMedia * media)
-{
-  GstRTSPMediaPrivate *priv = media->priv;
-  GstRTSPMediaStatus result;
-  gint64 end_time;
-
-  g_mutex_lock (&priv->lock);
-  end_time = g_get_monotonic_time () + 20 * G_TIME_SPAN_SECOND;
-  /* while we are preparing, wait */
-  while (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING) {
-    GST_DEBUG ("waiting for status change");
-    if (!g_cond_wait_until (&priv->cond, &priv->lock, end_time)) {
-      GST_DEBUG ("timeout, assuming error status");
-      priv->status = GST_RTSP_MEDIA_STATUS_ERROR;
-    }
-  }
-  /* could be success or error */
-  result = priv->status;
-  GST_DEBUG ("got status %d", result);
-  g_mutex_unlock (&priv->lock);
-
-  return result;
-}
-
-static void
 stream_collect_blocking (GstRTSPStream * stream, gboolean * blocked)
 {
   *blocked &= gst_rtsp_stream_is_blocking (stream);
@@ -1920,11 +1972,14 @@ pad_added_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
   stream = gst_rtsp_media_create_stream (media, pay, pad);
   gst_object_unref (pay);
 
-  g_object_set_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream", stream);
-
   GST_INFO ("pad added %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
 
   g_rec_mutex_lock (&priv->state_lock);
+  if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARING)
+    goto not_preparing;
+
+  g_object_set_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream", stream);
+
   /* we will be adding elements below that will cause ASYNC_DONE to be
    * posted in the bus. We want to ignore those messages until the
    * pipeline really prerolled. */
@@ -1932,11 +1987,24 @@ pad_added_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
 
   /* join the element in the PAUSED state because this callback is
    * called from the streaming thread and it is PAUSED */
-  gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
-      priv->rtpbin, GST_STATE_PAUSED);
+  if (!gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
+          priv->rtpbin, GST_STATE_PAUSED)) {
+    GST_WARNING ("failed to join bin element");
+  }
 
   priv->adding = FALSE;
   g_rec_mutex_unlock (&priv->state_lock);
+
+  return;
+
+  /* ERRORS */
+not_preparing:
+  {
+    gst_rtsp_media_remove_stream (media, stream);
+    g_rec_mutex_unlock (&priv->state_lock);
+    GST_INFO ("ignore pad because we are not preparing");
+    return;
+  }
 }
 
 static void
@@ -2075,8 +2143,10 @@ start_prepare (GstRTSPMedia * media)
 
     stream = g_ptr_array_index (priv->streams, i);
 
-    gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
-        priv->rtpbin, GST_STATE_NULL);
+    if (!gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
+            priv->rtpbin, GST_STATE_NULL)) {
+      goto join_bin_failed;
+    }
   }
 
   for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
@@ -2105,6 +2175,12 @@ start_prepare (GstRTSPMedia * media)
 
   return FALSE;
 
+join_bin_failed:
+  {
+    GST_WARNING ("failed to join bin element");
+    gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
+    return FALSE;
+  }
 preroll_failed:
   {
     GST_WARNING ("failed to preroll pipeline");
@@ -2113,49 +2189,17 @@ preroll_failed:
   }
 }
 
-/**
- * gst_rtsp_media_prepare:
- * @media: a #GstRTSPMedia
- * @thread: (transfer full): a #GstRTSPThread to run the bus handler or %NULL
- *
- * Prepare @media for streaming. This function will create the objects
- * to manage the streaming. A pipeline must have been set on @media with
- * gst_rtsp_media_take_pipeline().
- *
- * It will preroll the pipeline and collect vital information about the streams
- * such as the duration.
- *
- * Returns: %TRUE on success.
- */
-gboolean
-gst_rtsp_media_prepare (GstRTSPMedia * media, GstRTSPThread * thread)
+static gboolean
+default_prepare (GstRTSPMedia * media, GstRTSPThread * thread)
 {
   GstRTSPMediaPrivate *priv;
+  GstRTSPMediaClass *klass;
   GstBus *bus;
+  GMainContext *context;
   GSource *source;
-  GstRTSPMediaClass *klass;
-
-  g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
-  g_return_val_if_fail (GST_IS_RTSP_THREAD (thread), FALSE);
 
   priv = media->priv;
 
-  g_rec_mutex_lock (&priv->state_lock);
-  priv->prepare_count++;
-
-  if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED ||
-      priv->status == GST_RTSP_MEDIA_STATUS_SUSPENDED)
-    goto was_prepared;
-
-  if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
-    goto wait_status;
-
-  if (priv->status != GST_RTSP_MEDIA_STATUS_UNPREPARED)
-    goto not_unprepared;
-
-  if (!priv->reusable && priv->reused)
-    goto is_reused;
-
   klass = GST_RTSP_MEDIA_GET_CLASS (media);
 
   if (!klass->create_rtpbin)
@@ -2176,15 +2220,8 @@ gst_rtsp_media_prepare (GstRTSPMedia * media, GstRTSPThread * thread)
   if (priv->rtpbin == NULL)
     goto no_rtpbin;
 
-  GST_INFO ("preparing media %p", media);
-
-  /* reset some variables */
-  priv->is_live = FALSE;
-  priv->seekable = FALSE;
-  priv->buffering = FALSE;
   priv->thread = thread;
-  /* we're preparing now */
-  priv->status = GST_RTSP_MEDIA_STATUS_PREPARING;
+  context = (thread != NULL) ? (thread->context) : NULL;
 
   bus = gst_pipeline_get_bus (GST_PIPELINE_CAST (priv->pipeline));
 
@@ -2195,7 +2232,7 @@ gst_rtsp_media_prepare (GstRTSPMedia * media, GstRTSPThread * thread)
   g_source_set_callback (priv->source, (GSourceFunc) bus_message,
       g_object_ref (media), (GDestroyNotify) watch_destroyed);
 
-  priv->id = g_source_attach (priv->source, thread->context);
+  priv->id = g_source_attach (priv->source, context);
 
   /* add stuff to the bin */
   gst_bin_add (GST_BIN (priv->pipeline), priv->rtpbin);
@@ -2203,9 +2240,83 @@ gst_rtsp_media_prepare (GstRTSPMedia * media, GstRTSPThread * thread)
   /* do remainder in context */
   source = g_idle_source_new ();
   g_source_set_callback (source, (GSourceFunc) start_prepare, media, NULL);
-  g_source_attach (source, thread->context);
+  g_source_attach (source, context);
   g_source_unref (source);
 
+  return TRUE;
+
+  /* ERRORS */
+no_create_rtpbin:
+  {
+    GST_ERROR ("no create_rtpbin function");
+    g_critical ("no create_rtpbin vmethod function set");
+    return FALSE;
+  }
+no_rtpbin:
+  {
+    GST_WARNING ("no rtpbin element");
+    g_warning ("failed to create element 'rtpbin', check your installation");
+    return FALSE;
+  }
+}
+
+/**
+ * gst_rtsp_media_prepare:
+ * @media: a #GstRTSPMedia
+ * @thread: (transfer full) (allow-none): a #GstRTSPThread to run the
+ *   bus handler or %NULL
+ *
+ * Prepare @media for streaming. This function will create the objects
+ * to manage the streaming. A pipeline must have been set on @media with
+ * gst_rtsp_media_take_pipeline().
+ *
+ * It will preroll the pipeline and collect vital information about the streams
+ * such as the duration.
+ *
+ * Returns: %TRUE on success.
+ */
+gboolean
+gst_rtsp_media_prepare (GstRTSPMedia * media, GstRTSPThread * thread)
+{
+  GstRTSPMediaPrivate *priv;
+  GstRTSPMediaClass *klass;
+
+  g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
+
+  priv = media->priv;
+
+  g_rec_mutex_lock (&priv->state_lock);
+  priv->prepare_count++;
+
+  if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED ||
+      priv->status == GST_RTSP_MEDIA_STATUS_SUSPENDED)
+    goto was_prepared;
+
+  if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
+    goto is_preparing;
+
+  if (priv->status != GST_RTSP_MEDIA_STATUS_UNPREPARED)
+    goto not_unprepared;
+
+  if (!priv->reusable && priv->reused)
+    goto is_reused;
+
+  GST_INFO ("preparing media %p", media);
+
+  /* reset some variables */
+  priv->is_live = FALSE;
+  priv->seekable = FALSE;
+  priv->buffering = FALSE;
+
+  /* we're preparing now */
+  gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARING);
+
+  klass = GST_RTSP_MEDIA_GET_CLASS (media);
+  if (klass->prepare) {
+    if (!klass->prepare (media, thread))
+      goto prepare_failed;
+  }
+
 wait_status:
   g_rec_mutex_unlock (&priv->state_lock);
 
@@ -2221,17 +2332,28 @@ wait_status:
   return TRUE;
 
   /* OK */
+is_preparing:
+  {
+    /* we are not going to use the giving thread, so stop it. */
+    if (thread)
+      gst_rtsp_thread_stop (thread);
+    goto wait_status;
+  }
 was_prepared:
   {
     GST_LOG ("media %p was prepared", media);
     /* we are not going to use the giving thread, so stop it. */
-    gst_rtsp_thread_stop (thread);
+    if (thread)
+      gst_rtsp_thread_stop (thread);
     g_rec_mutex_unlock (&priv->state_lock);
     return TRUE;
   }
   /* ERRORS */
 not_unprepared:
   {
+    /* we are not going to use the giving thread, so stop it. */
+    if (thread)
+      gst_rtsp_thread_stop (thread);
     GST_WARNING ("media %p was not unprepared", media);
     priv->prepare_count--;
     g_rec_mutex_unlock (&priv->state_lock);
@@ -2239,25 +2361,22 @@ not_unprepared:
   }
 is_reused:
   {
+    /* we are not going to use the giving thread, so stop it. */
+    if (thread)
+      gst_rtsp_thread_stop (thread);
     priv->prepare_count--;
     g_rec_mutex_unlock (&priv->state_lock);
     GST_WARNING ("can not reuse media %p", media);
     return FALSE;
   }
-no_create_rtpbin:
-  {
-    priv->prepare_count--;
-    g_rec_mutex_unlock (&priv->state_lock);
-    GST_ERROR ("no create_rtpbin function");
-    g_critical ("no create_rtpbin vmethod function set");
-    return FALSE;
-  }
-no_rtpbin:
+prepare_failed:
   {
+    /* we are not going to use the giving thread, so stop it. */
+    if (thread)
+      gst_rtsp_thread_stop (thread);
     priv->prepare_count--;
     g_rec_mutex_unlock (&priv->state_lock);
-    GST_WARNING ("no rtpbin element");
-    g_warning ("failed to create element 'rtpbin', check your installation");
+    GST_ERROR ("failed to prepare media");
     return FALSE;
   }
 preroll_failed:
@@ -2278,7 +2397,11 @@ finish_unprepare (GstRTSPMedia * media)
 
   GST_DEBUG ("shutting down");
 
+  /* release the lock on shutdown, otherwise pad_added_cb might try to
+   * acquire the lock and then we deadlock */
+  g_rec_mutex_unlock (&priv->state_lock);
   set_state (media, GST_STATE_NULL);
+  g_rec_mutex_lock (&priv->state_lock);
   remove_fakesink (priv);
 
   for (i = 0; i < priv->streams->len; i++) {
@@ -2317,7 +2440,7 @@ finish_unprepare (GstRTSPMedia * media)
   priv->nettime = NULL;
 
   priv->reused = TRUE;
-  priv->status = GST_RTSP_MEDIA_STATUS_UNPREPARED;
+  gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_UNPREPARED);
 
   /* when the media is not reusable, this will effectively unref the media and
    * recreate it */
@@ -2348,7 +2471,6 @@ default_unprepare (GstRTSPMedia * media)
     /* we need to go to playing again for the EOS to propagate, normally in this
      * state, nothing is receiving data from us anymore so this is ok. */
     set_state (media, GST_STATE_PLAYING);
-    priv->status = GST_RTSP_MEDIA_STATUS_UNPREPARING;
   } else {
     finish_unprepare (media);
   }
@@ -2384,9 +2506,13 @@ gst_rtsp_media_unprepare (GstRTSPMedia * media)
     goto is_busy;
 
   GST_INFO ("unprepare media %p", media);
+  if (priv->blocked)
+    media_streams_set_blocked (media, FALSE);
   set_target_state (media, GST_STATE_NULL, FALSE);
   success = TRUE;
 
+  gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_UNPREPARING);
+
   if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED) {
     GstRTSPMediaClass *klass;
 
@@ -2493,7 +2619,7 @@ not_prepared:
 /**
  * gst_rtsp_media_get_time_provider:
  * @media: a #GstRTSPMedia
- * @address: an address or %NULL
+ * @address: (allow-none): an address or %NULL
  * @port: a port or 0
  *
  * Get the #GstNetTimeProvider for the clock used by @media. The time provider
@@ -2587,36 +2713,21 @@ no_setup_sdp:
   }
 }
 
-/**
- * gst_rtsp_media_suspend:
- * @media: a #GstRTSPMedia
- *
- * Suspend @media. The state of the pipeline managed by @media is set to
- * GST_STATE_NULL but all streams are kept. @media can be prepared again
- * with gst_rtsp_media_undo_reset()
- *
- * @media must be prepared with gst_rtsp_media_prepare();
- *
- * Returns: %TRUE on success.
- */
+static void
+do_set_seqnum (GstRTSPStream * stream)
+{
+  guint16 seq_num;
+  seq_num = gst_rtsp_stream_get_current_seqnum (stream);
+  gst_rtsp_stream_set_seqnum_offset (stream, seq_num + 1);
+}
+
+/* call with state_lock */
 gboolean
-gst_rtsp_media_suspend (GstRTSPMedia * media)
+default_suspend (GstRTSPMedia * media)
 {
   GstRTSPMediaPrivate *priv = media->priv;
   GstStateChangeReturn ret;
 
-  g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
-
-  GST_FIXME ("suspend for dynamic pipelines needs fixing");
-
-  g_rec_mutex_lock (&priv->state_lock);
-  if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
-    goto not_prepared;
-
-  /* don't attempt to suspend when something is busy */
-  if (priv->n_active > 0)
-    goto done;
-
   switch (priv->suspend_mode) {
     case GST_RTSP_SUSPEND_MODE_NONE:
       GST_DEBUG ("media %p no suspend", media);
@@ -2632,64 +2743,104 @@ gst_rtsp_media_suspend (GstRTSPMedia * media)
       ret = set_target_state (media, GST_STATE_NULL, TRUE);
       if (ret == GST_STATE_CHANGE_FAILURE)
         goto state_failed;
+      /* Because payloader needs to set the sequence number as
+       * monotonic, we need to preserve the sequence number
+       * after pause. (otherwise going from pause to play,  which
+       * is actually from NULL to PLAY will create a new sequence
+       * number. */
+      g_ptr_array_foreach (priv->streams, (GFunc) do_set_seqnum, NULL);
       break;
     default:
       break;
   }
+
   /* let the streams do the state changes freely, if any */
   media_streams_set_blocked (media, FALSE);
-  priv->status = GST_RTSP_MEDIA_STATUS_SUSPENDED;
-done:
-  g_rec_mutex_unlock (&priv->state_lock);
 
   return TRUE;
 
   /* ERRORS */
-not_prepared:
-  {
-    g_rec_mutex_unlock (&priv->state_lock);
-    GST_WARNING ("media %p was not prepared", media);
-    return FALSE;
-  }
 state_failed:
   {
-    g_rec_mutex_unlock (&priv->state_lock);
-    gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
     GST_WARNING ("failed changing pipeline's state for media %p", media);
     return FALSE;
   }
 }
 
 /**
- * gst_rtsp_media_unsuspend:
+ * gst_rtsp_media_suspend:
  * @media: a #GstRTSPMedia
  *
- * Unsuspend @media if it was in a suspended state. This method does nothing
- * when the media was not in the suspended state.
+ * Suspend @media. The state of the pipeline managed by @media is set to
+ * GST_STATE_NULL but all streams are kept. @media can be prepared again
+ * with gst_rtsp_media_unsuspend()
+ *
+ * @media must be prepared with gst_rtsp_media_prepare();
  *
  * Returns: %TRUE on success.
  */
 gboolean
-gst_rtsp_media_unsuspend (GstRTSPMedia * media)
+gst_rtsp_media_suspend (GstRTSPMedia * media)
 {
   GstRTSPMediaPrivate *priv = media->priv;
+  GstRTSPMediaClass *klass;
 
   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
 
+  GST_FIXME ("suspend for dynamic pipelines needs fixing");
+
   g_rec_mutex_lock (&priv->state_lock);
-  if (priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
+  if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
+    goto not_prepared;
+
+  /* don't attempt to suspend when something is busy */
+  if (priv->n_active > 0)
     goto done;
 
+  klass = GST_RTSP_MEDIA_GET_CLASS (media);
+  if (klass->suspend) {
+    if (!klass->suspend (media))
+      goto suspend_failed;
+  }
+
+  gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_SUSPENDED);
+done:
+  g_rec_mutex_unlock (&priv->state_lock);
+
+  return TRUE;
+
+  /* ERRORS */
+not_prepared:
+  {
+    g_rec_mutex_unlock (&priv->state_lock);
+    GST_WARNING ("media %p was not prepared", media);
+    return FALSE;
+  }
+suspend_failed:
+  {
+    g_rec_mutex_unlock (&priv->state_lock);
+    gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
+    GST_WARNING ("failed to suspend media %p", media);
+    return FALSE;
+  }
+}
+
+/* call with state_lock */
+gboolean
+default_unsuspend (GstRTSPMedia * media)
+{
+  GstRTSPMediaPrivate *priv = media->priv;
+
   switch (priv->suspend_mode) {
     case GST_RTSP_SUSPEND_MODE_NONE:
-      priv->status = GST_RTSP_MEDIA_STATUS_PREPARED;
+      gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
       break;
     case GST_RTSP_SUSPEND_MODE_PAUSE:
-      priv->status = GST_RTSP_MEDIA_STATUS_PREPARED;
+      gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
       break;
     case GST_RTSP_SUSPEND_MODE_RESET:
     {
-      priv->status = GST_RTSP_MEDIA_STATUS_PREPARING;
+      gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARING);
       if (!start_preroll (media))
         goto start_failed;
       g_rec_mutex_unlock (&priv->state_lock);
@@ -2702,17 +2853,13 @@ gst_rtsp_media_unsuspend (GstRTSPMedia * media)
     default:
       break;
   }
-done:
-  g_rec_mutex_unlock (&priv->state_lock);
 
   return TRUE;
 
   /* ERRORS */
 start_failed:
   {
-    g_rec_mutex_unlock (&priv->state_lock);
     GST_WARNING ("failed to preroll pipeline");
-    gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
     return FALSE;
   }
 preroll_failed:
@@ -2722,6 +2869,48 @@ preroll_failed:
   }
 }
 
+/**
+ * gst_rtsp_media_unsuspend:
+ * @media: a #GstRTSPMedia
+ *
+ * Unsuspend @media if it was in a suspended state. This method does nothing
+ * when the media was not in the suspended state.
+ *
+ * Returns: %TRUE on success.
+ */
+gboolean
+gst_rtsp_media_unsuspend (GstRTSPMedia * media)
+{
+  GstRTSPMediaPrivate *priv = media->priv;
+  GstRTSPMediaClass *klass;
+
+  g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
+
+  g_rec_mutex_lock (&priv->state_lock);
+  if (priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
+    goto done;
+
+  klass = GST_RTSP_MEDIA_GET_CLASS (media);
+  if (klass->unsuspend) {
+    if (!klass->unsuspend (media))
+      goto unsuspend_failed;
+  }
+
+done:
+  g_rec_mutex_unlock (&priv->state_lock);
+
+  return TRUE;
+
+  /* ERRORS */
+unsuspend_failed:
+  {
+    g_rec_mutex_unlock (&priv->state_lock);
+    GST_WARNING ("failed to unsuspend media %p", media);
+    gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
+    return FALSE;
+  }
+}
+
 /* must be called with state-lock */
 static void
 media_set_pipeline_state_locked (GstRTSPMedia * media, GstState state)