2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 * @short_description: The media pipeline
22 * @see_also: #GstRTSPMediaFactory, #GstRTSPStream, #GstRTSPSession,
23 * #GstRTSPSessionMedia
25 * a #GstRTSPMedia contains the complete GStreamer pipeline to manage the
26 * streaming to the clients. The actual data transfer is done by the
27 * #GstRTSPStream objects that are created and exposed by the #GstRTSPMedia.
29 * The #GstRTSPMedia is usually created from a #GstRTSPMediaFactory when the
30 * client does a DESCRIBE or SETUP of a resource.
32 * A media is created with gst_rtsp_media_new() that takes the element that will
33 * provide the streaming elements. For each of the streams, a new #GstRTSPStream
34 * object needs to be made with the gst_rtsp_media_create_stream() which takes
35 * the payloader element and the source pad that produces the RTP stream.
37 * The pipeline of the media is set to PAUSED with gst_rtsp_media_prepare(). The
38 * prepare method will add rtpbin and sinks and sources to send and receive RTP
39 * and RTCP packets from the clients. Each stream srcpad is connected to an
40 * input into the internal rtpbin.
42 * It is also possible to dynamically create #GstRTSPStream objects during the
43 * prepare phase. With gst_rtsp_media_get_status() you can check the status of
46 * After the media is prepared, it is ready for streaming. It will usually be
47 * managed in a session with gst_rtsp_session_manage_media(). See
48 * #GstRTSPSession and #GstRTSPSessionMedia.
50 * The state of the media can be controlled with gst_rtsp_media_set_state ().
51 * Seeking can be done with gst_rtsp_media_seek().
53 * With gst_rtsp_media_unprepare() the pipeline is stopped and shut down. When
54 * gst_rtsp_media_set_eos_shutdown() an EOS will be sent to the pipeline to
57 * With gst_rtsp_media_set_shared(), the media can be shared between multiple
58 * clients. With gst_rtsp_media_set_reusable() you can control if the pipeline
59 * can be prepared again after an unprepare.
61 * Last reviewed on 2013-07-11 (1.0.0)
67 #include <gst/app/gstappsrc.h>
68 #include <gst/app/gstappsink.h>
70 #include "rtsp-media.h"
72 #define GST_RTSP_MEDIA_GET_PRIVATE(obj) \
73 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_MEDIA, GstRTSPMediaPrivate))
75 struct _GstRTSPMediaPrivate
80 /* protected by lock */
81 GstRTSPPermissions *permissions;
83 gboolean suspend_mode;
85 GstRTSPProfile profiles;
86 GstRTSPLowerTrans protocols;
88 gboolean eos_shutdown;
90 GstRTSPAddressPool *pool;
94 GRecMutex state_lock; /* locking order: state lock, lock */
95 GPtrArray *streams; /* protected by lock */
96 GList *dynamic; /* protected by lock */
97 GstRTSPMediaStatus status; /* protected by lock */
102 /* the pipeline for the media */
103 GstElement *pipeline;
104 GstElement *fakesink; /* protected by lock */
107 GstRTSPThread *thread;
109 gboolean time_provider;
110 GstNetTimeProvider *nettime;
115 GstState target_state;
117 /* RTP session manager */
120 /* the range of media */
121 GstRTSPTimeRange range; /* protected by lock */
122 GstClockTime range_start;
123 GstClockTime range_stop;
125 GList *payloads; /* protected by lock */
126 GstClockTime rtx_time; /* protected by lock */
129 #define DEFAULT_SHARED FALSE
130 #define DEFAULT_SUSPEND_MODE GST_RTSP_SUSPEND_MODE_NONE
131 #define DEFAULT_REUSABLE FALSE
132 #define DEFAULT_PROFILES GST_RTSP_PROFILE_AVP
133 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | \
134 GST_RTSP_LOWER_TRANS_TCP
135 #define DEFAULT_EOS_SHUTDOWN FALSE
136 #define DEFAULT_BUFFER_SIZE 0x80000
137 #define DEFAULT_TIME_PROVIDER FALSE
139 /* define to dump received RTCP packets */
160 SIGNAL_REMOVED_STREAM,
168 GST_DEBUG_CATEGORY_STATIC (rtsp_media_debug);
169 #define GST_CAT_DEFAULT rtsp_media_debug
171 static void gst_rtsp_media_get_property (GObject * object, guint propid,
172 GValue * value, GParamSpec * pspec);
173 static void gst_rtsp_media_set_property (GObject * object, guint propid,
174 const GValue * value, GParamSpec * pspec);
175 static void gst_rtsp_media_finalize (GObject * obj);
177 static gboolean default_handle_message (GstRTSPMedia * media,
178 GstMessage * message);
179 static void finish_unprepare (GstRTSPMedia * media);
180 static gboolean default_prepare (GstRTSPMedia * media, GstRTSPThread * thread);
181 static gboolean default_unprepare (GstRTSPMedia * media);
182 static gboolean default_suspend (GstRTSPMedia * media);
183 static gboolean default_unsuspend (GstRTSPMedia * media);
184 static gboolean default_convert_range (GstRTSPMedia * media,
185 GstRTSPTimeRange * range, GstRTSPRangeUnit unit);
186 static gboolean default_query_position (GstRTSPMedia * media,
188 static gboolean default_query_stop (GstRTSPMedia * media, gint64 * stop);
189 static GstElement *default_create_rtpbin (GstRTSPMedia * media);
190 static gboolean default_setup_sdp (GstRTSPMedia * media, GstSDPMessage * sdp,
193 static gboolean wait_preroll (GstRTSPMedia * media);
195 static guint gst_rtsp_media_signals[SIGNAL_LAST] = { 0 };
197 #define C_ENUM(v) ((gint) v)
199 #define GST_TYPE_RTSP_SUSPEND_MODE (gst_rtsp_suspend_mode_get_type())
201 gst_rtsp_suspend_mode_get_type (void)
204 static const GEnumValue values[] = {
205 {C_ENUM (GST_RTSP_SUSPEND_MODE_NONE), "GST_RTSP_SUSPEND_MODE_NONE", "none"},
206 {C_ENUM (GST_RTSP_SUSPEND_MODE_PAUSE), "GST_RTSP_SUSPEND_MODE_PAUSE",
208 {C_ENUM (GST_RTSP_SUSPEND_MODE_RESET), "GST_RTSP_SUSPEND_MODE_RESET",
213 if (g_once_init_enter (&id)) {
214 GType tmp = g_enum_register_static ("GstRTSPSuspendMode", values);
215 g_once_init_leave (&id, tmp);
220 G_DEFINE_TYPE (GstRTSPMedia, gst_rtsp_media, G_TYPE_OBJECT);
223 gst_rtsp_media_class_init (GstRTSPMediaClass * klass)
225 GObjectClass *gobject_class;
227 g_type_class_add_private (klass, sizeof (GstRTSPMediaPrivate));
229 gobject_class = G_OBJECT_CLASS (klass);
231 gobject_class->get_property = gst_rtsp_media_get_property;
232 gobject_class->set_property = gst_rtsp_media_set_property;
233 gobject_class->finalize = gst_rtsp_media_finalize;
235 g_object_class_install_property (gobject_class, PROP_SHARED,
236 g_param_spec_boolean ("shared", "Shared",
237 "If this media pipeline can be shared", DEFAULT_SHARED,
238 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
240 g_object_class_install_property (gobject_class, PROP_SUSPEND_MODE,
241 g_param_spec_enum ("suspend-mode", "Suspend Mode",
242 "How to suspend the media in PAUSED", GST_TYPE_RTSP_SUSPEND_MODE,
243 DEFAULT_SUSPEND_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
245 g_object_class_install_property (gobject_class, PROP_REUSABLE,
246 g_param_spec_boolean ("reusable", "Reusable",
247 "If this media pipeline can be reused after an unprepare",
248 DEFAULT_REUSABLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
250 g_object_class_install_property (gobject_class, PROP_PROFILES,
251 g_param_spec_flags ("profiles", "Profiles",
252 "Allowed transfer profiles", GST_TYPE_RTSP_PROFILE,
253 DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
255 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
256 g_param_spec_flags ("protocols", "Protocols",
257 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
258 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
260 g_object_class_install_property (gobject_class, PROP_EOS_SHUTDOWN,
261 g_param_spec_boolean ("eos-shutdown", "EOS Shutdown",
262 "Send an EOS event to the pipeline before unpreparing",
263 DEFAULT_EOS_SHUTDOWN, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
265 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
266 g_param_spec_uint ("buffer-size", "Buffer Size",
267 "The kernel UDP buffer size to use", 0, G_MAXUINT,
268 DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
270 g_object_class_install_property (gobject_class, PROP_ELEMENT,
271 g_param_spec_object ("element", "The Element",
272 "The GstBin to use for streaming the media", GST_TYPE_ELEMENT,
273 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
275 g_object_class_install_property (gobject_class, PROP_TIME_PROVIDER,
276 g_param_spec_boolean ("time-provider", "Time Provider",
277 "Use a NetTimeProvider for clients",
278 DEFAULT_TIME_PROVIDER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
280 gst_rtsp_media_signals[SIGNAL_NEW_STREAM] =
281 g_signal_new ("new-stream", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
282 G_STRUCT_OFFSET (GstRTSPMediaClass, new_stream), NULL, NULL,
283 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_RTSP_STREAM);
285 gst_rtsp_media_signals[SIGNAL_REMOVED_STREAM] =
286 g_signal_new ("removed-stream", G_TYPE_FROM_CLASS (klass),
287 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPMediaClass, removed_stream),
288 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
289 GST_TYPE_RTSP_STREAM);
291 gst_rtsp_media_signals[SIGNAL_PREPARED] =
292 g_signal_new ("prepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
293 G_STRUCT_OFFSET (GstRTSPMediaClass, prepared), NULL, NULL,
294 g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
296 gst_rtsp_media_signals[SIGNAL_UNPREPARED] =
297 g_signal_new ("unprepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
298 G_STRUCT_OFFSET (GstRTSPMediaClass, unprepared), NULL, NULL,
299 g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
301 gst_rtsp_media_signals[SIGNAL_TARGET_STATE] =
302 g_signal_new ("target-state", G_TYPE_FROM_CLASS (klass),
303 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPMediaClass, target_state),
304 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_INT);
306 gst_rtsp_media_signals[SIGNAL_NEW_STATE] =
307 g_signal_new ("new-state", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
308 G_STRUCT_OFFSET (GstRTSPMediaClass, new_state), NULL, NULL,
309 g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_INT);
311 GST_DEBUG_CATEGORY_INIT (rtsp_media_debug, "rtspmedia", 0, "GstRTSPMedia");
313 klass->handle_message = default_handle_message;
314 klass->prepare = default_prepare;
315 klass->unprepare = default_unprepare;
316 klass->suspend = default_suspend;
317 klass->unsuspend = default_unsuspend;
318 klass->convert_range = default_convert_range;
319 klass->query_position = default_query_position;
320 klass->query_stop = default_query_stop;
321 klass->create_rtpbin = default_create_rtpbin;
322 klass->setup_sdp = default_setup_sdp;
326 gst_rtsp_media_init (GstRTSPMedia * media)
328 GstRTSPMediaPrivate *priv = GST_RTSP_MEDIA_GET_PRIVATE (media);
332 priv->streams = g_ptr_array_new_with_free_func (g_object_unref);
333 g_mutex_init (&priv->lock);
334 g_cond_init (&priv->cond);
335 g_rec_mutex_init (&priv->state_lock);
337 priv->shared = DEFAULT_SHARED;
338 priv->suspend_mode = DEFAULT_SUSPEND_MODE;
339 priv->reusable = DEFAULT_REUSABLE;
340 priv->profiles = DEFAULT_PROFILES;
341 priv->protocols = DEFAULT_PROTOCOLS;
342 priv->eos_shutdown = DEFAULT_EOS_SHUTDOWN;
343 priv->buffer_size = DEFAULT_BUFFER_SIZE;
344 priv->time_provider = DEFAULT_TIME_PROVIDER;
348 gst_rtsp_media_finalize (GObject * obj)
350 GstRTSPMediaPrivate *priv;
353 media = GST_RTSP_MEDIA (obj);
356 GST_INFO ("finalize media %p", media);
358 if (priv->permissions)
359 gst_rtsp_permissions_unref (priv->permissions);
361 g_ptr_array_unref (priv->streams);
363 g_list_free_full (priv->dynamic, gst_object_unref);
366 gst_object_unref (priv->pipeline);
368 gst_object_unref (priv->nettime);
369 gst_object_unref (priv->element);
371 g_object_unref (priv->pool);
373 g_list_free (priv->payloads);
374 g_mutex_clear (&priv->lock);
375 g_cond_clear (&priv->cond);
376 g_rec_mutex_clear (&priv->state_lock);
378 G_OBJECT_CLASS (gst_rtsp_media_parent_class)->finalize (obj);
382 gst_rtsp_media_get_property (GObject * object, guint propid,
383 GValue * value, GParamSpec * pspec)
385 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
389 g_value_set_object (value, media->priv->element);
392 g_value_set_boolean (value, gst_rtsp_media_is_shared (media));
394 case PROP_SUSPEND_MODE:
395 g_value_set_enum (value, gst_rtsp_media_get_suspend_mode (media));
398 g_value_set_boolean (value, gst_rtsp_media_is_reusable (media));
401 g_value_set_flags (value, gst_rtsp_media_get_profiles (media));
404 g_value_set_flags (value, gst_rtsp_media_get_protocols (media));
406 case PROP_EOS_SHUTDOWN:
407 g_value_set_boolean (value, gst_rtsp_media_is_eos_shutdown (media));
409 case PROP_BUFFER_SIZE:
410 g_value_set_uint (value, gst_rtsp_media_get_buffer_size (media));
412 case PROP_TIME_PROVIDER:
413 g_value_set_boolean (value, gst_rtsp_media_is_time_provider (media));
416 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
421 gst_rtsp_media_set_property (GObject * object, guint propid,
422 const GValue * value, GParamSpec * pspec)
424 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
428 media->priv->element = g_value_get_object (value);
429 gst_object_ref_sink (media->priv->element);
432 gst_rtsp_media_set_shared (media, g_value_get_boolean (value));
434 case PROP_SUSPEND_MODE:
435 gst_rtsp_media_set_suspend_mode (media, g_value_get_enum (value));
438 gst_rtsp_media_set_reusable (media, g_value_get_boolean (value));
441 gst_rtsp_media_set_profiles (media, g_value_get_flags (value));
444 gst_rtsp_media_set_protocols (media, g_value_get_flags (value));
446 case PROP_EOS_SHUTDOWN:
447 gst_rtsp_media_set_eos_shutdown (media, g_value_get_boolean (value));
449 case PROP_BUFFER_SIZE:
450 gst_rtsp_media_set_buffer_size (media, g_value_get_uint (value));
452 case PROP_TIME_PROVIDER:
453 gst_rtsp_media_use_time_provider (media, g_value_get_boolean (value));
456 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
464 } DoQueryPositionData;
467 do_query_position (GstRTSPStream * stream, DoQueryPositionData * data)
471 if (gst_rtsp_stream_query_position (stream, &tmp)) {
472 data->position = MAX (data->position, tmp);
478 default_query_position (GstRTSPMedia * media, gint64 * position)
480 GstRTSPMediaPrivate *priv;
481 DoQueryPositionData data;
488 g_ptr_array_foreach (priv->streams, (GFunc) do_query_position, &data);
490 *position = data.position;
502 do_query_stop (GstRTSPStream * stream, DoQueryStopData * data)
506 if (gst_rtsp_stream_query_stop (stream, &tmp)) {
507 data->stop = MAX (data->stop, tmp);
513 default_query_stop (GstRTSPMedia * media, gint64 * stop)
515 GstRTSPMediaPrivate *priv;
516 DoQueryStopData data;
523 g_ptr_array_foreach (priv->streams, (GFunc) do_query_stop, &data);
531 default_create_rtpbin (GstRTSPMedia * media)
535 rtpbin = gst_element_factory_make ("rtpbin", NULL);
540 /* must be called with state lock */
542 collect_media_stats (GstRTSPMedia * media)
544 GstRTSPMediaPrivate *priv = media->priv;
545 gint64 position = 0, stop = -1;
547 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
548 priv->status != GST_RTSP_MEDIA_STATUS_PREPARING)
551 priv->range.unit = GST_RTSP_RANGE_NPT;
553 GST_INFO ("collect media stats");
556 priv->range.min.type = GST_RTSP_TIME_NOW;
557 priv->range.min.seconds = -1;
558 priv->range_start = -1;
559 priv->range.max.type = GST_RTSP_TIME_END;
560 priv->range.max.seconds = -1;
561 priv->range_stop = -1;
563 GstRTSPMediaClass *klass;
566 klass = GST_RTSP_MEDIA_GET_CLASS (media);
568 /* get the position */
570 if (klass->query_position)
571 ret = klass->query_position (media, &position);
574 GST_INFO ("position query failed");
578 /* get the current segment stop */
580 if (klass->query_stop)
581 ret = klass->query_stop (media, &stop);
584 GST_INFO ("stop query failed");
588 GST_INFO ("stats: position %" GST_TIME_FORMAT ", stop %"
589 GST_TIME_FORMAT, GST_TIME_ARGS (position), GST_TIME_ARGS (stop));
591 if (position == -1) {
592 priv->range.min.type = GST_RTSP_TIME_NOW;
593 priv->range.min.seconds = -1;
594 priv->range_start = -1;
596 priv->range.min.type = GST_RTSP_TIME_SECONDS;
597 priv->range.min.seconds = ((gdouble) position) / GST_SECOND;
598 priv->range_start = position;
601 priv->range.max.type = GST_RTSP_TIME_END;
602 priv->range.max.seconds = -1;
603 priv->range_stop = -1;
605 priv->range.max.type = GST_RTSP_TIME_SECONDS;
606 priv->range.max.seconds = ((gdouble) stop) / GST_SECOND;
607 priv->range_stop = stop;
613 * gst_rtsp_media_new:
614 * @element: (transfer full): a #GstElement
616 * Create a new #GstRTSPMedia instance. @element is the bin element that
617 * provides the different streams. The #GstRTSPMedia object contains the
618 * element to produce RTP data for one or more related (audio/video/..)
621 * Ownership is taken of @element.
623 * Returns: (transfer full): a new #GstRTSPMedia object.
626 gst_rtsp_media_new (GstElement * element)
628 GstRTSPMedia *result;
630 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
632 result = g_object_new (GST_TYPE_RTSP_MEDIA, "element", element, NULL);
638 * gst_rtsp_media_get_element:
639 * @media: a #GstRTSPMedia
641 * Get the element that was used when constructing @media.
643 * Returns: (transfer full): a #GstElement. Unref after usage.
646 gst_rtsp_media_get_element (GstRTSPMedia * media)
648 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
650 return gst_object_ref (media->priv->element);
654 * gst_rtsp_media_take_pipeline:
655 * @media: a #GstRTSPMedia
656 * @pipeline: (transfer full): a #GstPipeline
658 * Set @pipeline as the #GstPipeline for @media. Ownership is
659 * taken of @pipeline.
662 gst_rtsp_media_take_pipeline (GstRTSPMedia * media, GstPipeline * pipeline)
664 GstRTSPMediaPrivate *priv;
666 GstNetTimeProvider *nettime;
668 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
669 g_return_if_fail (GST_IS_PIPELINE (pipeline));
673 g_mutex_lock (&priv->lock);
674 old = priv->pipeline;
675 priv->pipeline = GST_ELEMENT_CAST (pipeline);
676 nettime = priv->nettime;
677 priv->nettime = NULL;
678 g_mutex_unlock (&priv->lock);
681 gst_object_unref (old);
684 gst_object_unref (nettime);
686 gst_bin_add (GST_BIN_CAST (pipeline), priv->element);
690 * gst_rtsp_media_set_permissions:
691 * @media: a #GstRTSPMedia
692 * @permissions: (transfer none): a #GstRTSPPermissions
694 * Set @permissions on @media.
697 gst_rtsp_media_set_permissions (GstRTSPMedia * media,
698 GstRTSPPermissions * permissions)
700 GstRTSPMediaPrivate *priv;
702 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
706 g_mutex_lock (&priv->lock);
707 if (priv->permissions)
708 gst_rtsp_permissions_unref (priv->permissions);
709 if ((priv->permissions = permissions))
710 gst_rtsp_permissions_ref (permissions);
711 g_mutex_unlock (&priv->lock);
715 * gst_rtsp_media_get_permissions:
716 * @media: a #GstRTSPMedia
718 * Get the permissions object from @media.
720 * Returns: (transfer full): a #GstRTSPPermissions object, unref after usage.
723 gst_rtsp_media_get_permissions (GstRTSPMedia * media)
725 GstRTSPMediaPrivate *priv;
726 GstRTSPPermissions *result;
728 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
732 g_mutex_lock (&priv->lock);
733 if ((result = priv->permissions))
734 gst_rtsp_permissions_ref (result);
735 g_mutex_unlock (&priv->lock);
741 * gst_rtsp_media_set_suspend_mode:
742 * @media: a #GstRTSPMedia
743 * @mode: the new #GstRTSPSuspendMode
745 * Control how @ media will be suspended after the SDP has been generated and
746 * after a PAUSE request has been performed.
748 * Media must be unprepared when setting the suspend mode.
751 gst_rtsp_media_set_suspend_mode (GstRTSPMedia * media, GstRTSPSuspendMode mode)
753 GstRTSPMediaPrivate *priv;
755 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
759 g_rec_mutex_lock (&priv->state_lock);
760 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED)
762 priv->suspend_mode = mode;
763 g_rec_mutex_unlock (&priv->state_lock);
770 GST_WARNING ("media %p was prepared", media);
771 g_rec_mutex_unlock (&priv->state_lock);
776 * gst_rtsp_media_get_suspend_mode:
777 * @media: a #GstRTSPMedia
779 * Get how @media will be suspended.
781 * Returns: #GstRTSPSuspendMode.
784 gst_rtsp_media_get_suspend_mode (GstRTSPMedia * media)
786 GstRTSPMediaPrivate *priv;
787 GstRTSPSuspendMode res;
789 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_RTSP_SUSPEND_MODE_NONE);
793 g_rec_mutex_lock (&priv->state_lock);
794 res = priv->suspend_mode;
795 g_rec_mutex_unlock (&priv->state_lock);
801 * gst_rtsp_media_set_shared:
802 * @media: a #GstRTSPMedia
803 * @shared: the new value
805 * Set or unset if the pipeline for @media can be shared will multiple clients.
806 * When @shared is %TRUE, client requests for this media will share the media
810 gst_rtsp_media_set_shared (GstRTSPMedia * media, gboolean shared)
812 GstRTSPMediaPrivate *priv;
814 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
818 g_mutex_lock (&priv->lock);
819 priv->shared = shared;
820 g_mutex_unlock (&priv->lock);
824 * gst_rtsp_media_is_shared:
825 * @media: a #GstRTSPMedia
827 * Check if the pipeline for @media can be shared between multiple clients.
829 * Returns: %TRUE if the media can be shared between clients.
832 gst_rtsp_media_is_shared (GstRTSPMedia * media)
834 GstRTSPMediaPrivate *priv;
837 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
841 g_mutex_lock (&priv->lock);
843 g_mutex_unlock (&priv->lock);
849 * gst_rtsp_media_set_reusable:
850 * @media: a #GstRTSPMedia
851 * @reusable: the new value
853 * Set or unset if the pipeline for @media can be reused after the pipeline has
857 gst_rtsp_media_set_reusable (GstRTSPMedia * media, gboolean reusable)
859 GstRTSPMediaPrivate *priv;
861 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
865 g_mutex_lock (&priv->lock);
866 priv->reusable = reusable;
867 g_mutex_unlock (&priv->lock);
871 * gst_rtsp_media_is_reusable:
872 * @media: a #GstRTSPMedia
874 * Check if the pipeline for @media can be reused after an unprepare.
876 * Returns: %TRUE if the media can be reused
879 gst_rtsp_media_is_reusable (GstRTSPMedia * media)
881 GstRTSPMediaPrivate *priv;
884 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
888 g_mutex_lock (&priv->lock);
889 res = priv->reusable;
890 g_mutex_unlock (&priv->lock);
896 do_set_profiles (GstRTSPStream * stream, GstRTSPProfile * profiles)
898 gst_rtsp_stream_set_profiles (stream, *profiles);
902 * gst_rtsp_media_set_profiles:
903 * @media: a #GstRTSPMedia
904 * @profiles: the new flags
906 * Configure the allowed lower transport for @media.
909 gst_rtsp_media_set_profiles (GstRTSPMedia * media, GstRTSPProfile profiles)
911 GstRTSPMediaPrivate *priv;
913 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
917 g_mutex_lock (&priv->lock);
918 priv->profiles = profiles;
919 g_ptr_array_foreach (priv->streams, (GFunc) do_set_profiles, &profiles);
920 g_mutex_unlock (&priv->lock);
924 * gst_rtsp_media_get_profiles:
925 * @media: a #GstRTSPMedia
927 * Get the allowed profiles of @media.
929 * Returns: a #GstRTSPProfile
932 gst_rtsp_media_get_profiles (GstRTSPMedia * media)
934 GstRTSPMediaPrivate *priv;
937 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_RTSP_PROFILE_UNKNOWN);
941 g_mutex_lock (&priv->lock);
942 res = priv->profiles;
943 g_mutex_unlock (&priv->lock);
949 do_set_protocols (GstRTSPStream * stream, GstRTSPLowerTrans * protocols)
951 gst_rtsp_stream_set_protocols (stream, *protocols);
955 * gst_rtsp_media_set_protocols:
956 * @media: a #GstRTSPMedia
957 * @protocols: the new flags
959 * Configure the allowed lower transport for @media.
962 gst_rtsp_media_set_protocols (GstRTSPMedia * media, GstRTSPLowerTrans protocols)
964 GstRTSPMediaPrivate *priv;
966 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
970 g_mutex_lock (&priv->lock);
971 priv->protocols = protocols;
972 g_ptr_array_foreach (priv->streams, (GFunc) do_set_protocols, &protocols);
973 g_mutex_unlock (&priv->lock);
977 * gst_rtsp_media_get_protocols:
978 * @media: a #GstRTSPMedia
980 * Get the allowed protocols of @media.
982 * Returns: a #GstRTSPLowerTrans
985 gst_rtsp_media_get_protocols (GstRTSPMedia * media)
987 GstRTSPMediaPrivate *priv;
988 GstRTSPLowerTrans res;
990 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media),
991 GST_RTSP_LOWER_TRANS_UNKNOWN);
995 g_mutex_lock (&priv->lock);
996 res = priv->protocols;
997 g_mutex_unlock (&priv->lock);
1003 * gst_rtsp_media_set_eos_shutdown:
1004 * @media: a #GstRTSPMedia
1005 * @eos_shutdown: the new value
1007 * Set or unset if an EOS event will be sent to the pipeline for @media before
1011 gst_rtsp_media_set_eos_shutdown (GstRTSPMedia * media, gboolean eos_shutdown)
1013 GstRTSPMediaPrivate *priv;
1015 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1019 g_mutex_lock (&priv->lock);
1020 priv->eos_shutdown = eos_shutdown;
1021 g_mutex_unlock (&priv->lock);
1025 * gst_rtsp_media_is_eos_shutdown:
1026 * @media: a #GstRTSPMedia
1028 * Check if the pipeline for @media will send an EOS down the pipeline before
1031 * Returns: %TRUE if the media will send EOS before unpreparing.
1034 gst_rtsp_media_is_eos_shutdown (GstRTSPMedia * media)
1036 GstRTSPMediaPrivate *priv;
1039 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1043 g_mutex_lock (&priv->lock);
1044 res = priv->eos_shutdown;
1045 g_mutex_unlock (&priv->lock);
1051 * gst_rtsp_media_set_buffer_size:
1052 * @media: a #GstRTSPMedia
1053 * @size: the new value
1055 * Set the kernel UDP buffer size.
1058 gst_rtsp_media_set_buffer_size (GstRTSPMedia * media, guint size)
1060 GstRTSPMediaPrivate *priv;
1062 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1064 GST_LOG_OBJECT (media, "set buffer size %u", size);
1068 g_mutex_lock (&priv->lock);
1069 priv->buffer_size = size;
1070 g_mutex_unlock (&priv->lock);
1074 * gst_rtsp_media_get_buffer_size:
1075 * @media: a #GstRTSPMedia
1077 * Get the kernel UDP buffer size.
1079 * Returns: the kernel UDP buffer size.
1082 gst_rtsp_media_get_buffer_size (GstRTSPMedia * media)
1084 GstRTSPMediaPrivate *priv;
1087 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1091 g_mutex_unlock (&priv->lock);
1092 res = priv->buffer_size;
1093 g_mutex_unlock (&priv->lock);
1099 * gst_rtsp_media_set_retransmission_time:
1100 * @media: a #GstRTSPMedia
1101 * @time: the new value
1103 * Set the amount of time to store retransmission packets.
1106 gst_rtsp_media_set_retransmission_time (GstRTSPMedia * media, GstClockTime time)
1108 GstRTSPMediaPrivate *priv;
1111 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1113 GST_LOG_OBJECT (media, "set retransmission time %" G_GUINT64_FORMAT, time);
1117 g_mutex_lock (&priv->lock);
1118 priv->rtx_time = time;
1119 for (i = 0; i < priv->streams->len; i++) {
1120 GstRTSPStream *stream = g_ptr_array_index (priv->streams, i);
1122 gst_rtsp_stream_set_retransmission_time (stream, time);
1126 g_object_set (priv->rtpbin, "do-retransmission", time > 0, NULL);
1127 g_mutex_unlock (&priv->lock);
1131 * gst_rtsp_media_get_retransmission_time:
1132 * @media: a #GstRTSPMedia
1134 * Get the amount of time to store retransmission data.
1136 * Returns: the amount of time to store retransmission data.
1139 gst_rtsp_media_get_retransmission_time (GstRTSPMedia * media)
1141 GstRTSPMediaPrivate *priv;
1144 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1148 g_mutex_unlock (&priv->lock);
1149 res = priv->rtx_time;
1150 g_mutex_unlock (&priv->lock);
1156 * gst_rtsp_media_use_time_provider:
1157 * @media: a #GstRTSPMedia
1158 * @time_provider: if a #GstNetTimeProvider should be used
1160 * Set @media to provide a #GstNetTimeProvider.
1163 gst_rtsp_media_use_time_provider (GstRTSPMedia * media, gboolean time_provider)
1165 GstRTSPMediaPrivate *priv;
1167 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1171 g_mutex_lock (&priv->lock);
1172 priv->time_provider = time_provider;
1173 g_mutex_unlock (&priv->lock);
1177 * gst_rtsp_media_is_time_provider:
1178 * @media: a #GstRTSPMedia
1180 * Check if @media can provide a #GstNetTimeProvider for its pipeline clock.
1182 * Use gst_rtsp_media_get_time_provider() to get the network clock.
1184 * Returns: %TRUE if @media can provide a #GstNetTimeProvider.
1187 gst_rtsp_media_is_time_provider (GstRTSPMedia * media)
1189 GstRTSPMediaPrivate *priv;
1192 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1196 g_mutex_unlock (&priv->lock);
1197 res = priv->time_provider;
1198 g_mutex_unlock (&priv->lock);
1204 * gst_rtsp_media_set_address_pool:
1205 * @media: a #GstRTSPMedia
1206 * @pool: (transfer none): a #GstRTSPAddressPool
1208 * configure @pool to be used as the address pool of @media.
1211 gst_rtsp_media_set_address_pool (GstRTSPMedia * media,
1212 GstRTSPAddressPool * pool)
1214 GstRTSPMediaPrivate *priv;
1215 GstRTSPAddressPool *old;
1217 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1221 GST_LOG_OBJECT (media, "set address pool %p", pool);
1223 g_mutex_lock (&priv->lock);
1224 if ((old = priv->pool) != pool)
1225 priv->pool = pool ? g_object_ref (pool) : NULL;
1228 g_ptr_array_foreach (priv->streams, (GFunc) gst_rtsp_stream_set_address_pool,
1230 g_mutex_unlock (&priv->lock);
1233 g_object_unref (old);
1237 * gst_rtsp_media_get_address_pool:
1238 * @media: a #GstRTSPMedia
1240 * Get the #GstRTSPAddressPool used as the address pool of @media.
1242 * Returns: (transfer full): the #GstRTSPAddressPool of @media. g_object_unref() after
1245 GstRTSPAddressPool *
1246 gst_rtsp_media_get_address_pool (GstRTSPMedia * media)
1248 GstRTSPMediaPrivate *priv;
1249 GstRTSPAddressPool *result;
1251 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1255 g_mutex_lock (&priv->lock);
1256 if ((result = priv->pool))
1257 g_object_ref (result);
1258 g_mutex_unlock (&priv->lock);
1264 _find_payload_types (GstRTSPMedia * media)
1269 n = media->priv->streams->len;
1270 for (i = 0; i < n; i++) {
1271 GstRTSPStream *stream = g_ptr_array_index (media->priv->streams, i);
1272 guint pt = gst_rtsp_stream_get_pt (stream);
1274 ret = g_list_append (ret, GUINT_TO_POINTER (pt));
1281 _next_available_pt (GList * payloads)
1285 for (i = 96; i <= 127; i++) {
1286 GList *iter = g_list_find (payloads, GINT_TO_POINTER (i));
1288 return GPOINTER_TO_UINT (i);
1295 * gst_rtsp_media_collect_streams:
1296 * @media: a #GstRTSPMedia
1298 * Find all payloader elements, they should be named pay\%d in the
1299 * element of @media, and create #GstRTSPStreams for them.
1301 * Collect all dynamic elements, named dynpay\%d, and add them to
1302 * the list of dynamic elements.
1305 gst_rtsp_media_collect_streams (GstRTSPMedia * media)
1307 GstRTSPMediaPrivate *priv;
1308 GstElement *element, *elem;
1313 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1316 element = priv->element;
1319 for (i = 0; have_elem; i++) {
1324 name = g_strdup_printf ("pay%d", i);
1325 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
1326 GST_INFO ("found stream %d with payloader %p", i, elem);
1328 /* take the pad of the payloader */
1329 pad = gst_element_get_static_pad (elem, "src");
1330 /* create the stream */
1331 gst_rtsp_media_create_stream (media, elem, pad);
1332 gst_object_unref (pad);
1333 gst_object_unref (elem);
1339 name = g_strdup_printf ("dynpay%d", i);
1340 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
1341 /* a stream that will dynamically create pads to provide RTP packets */
1342 GST_INFO ("found dynamic element %d, %p", i, elem);
1344 g_mutex_lock (&priv->lock);
1345 priv->dynamic = g_list_prepend (priv->dynamic, elem);
1346 g_mutex_unlock (&priv->lock);
1355 * gst_rtsp_media_create_stream:
1356 * @media: a #GstRTSPMedia
1357 * @payloader: a #GstElement
1358 * @srcpad: a source #GstPad
1360 * Create a new stream in @media that provides RTP data on @srcpad.
1361 * @srcpad should be a pad of an element inside @media->element.
1363 * Returns: (transfer none): a new #GstRTSPStream that remains valid for as long
1367 gst_rtsp_media_create_stream (GstRTSPMedia * media, GstElement * payloader,
1370 GstRTSPMediaPrivate *priv;
1371 GstRTSPStream *stream;
1377 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1378 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
1379 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1380 g_return_val_if_fail (GST_PAD_IS_SRC (pad), NULL);
1384 g_mutex_lock (&priv->lock);
1385 idx = priv->streams->len;
1387 GST_DEBUG ("media %p: creating stream with index %d", media, idx);
1389 name = g_strdup_printf ("src_%u", idx);
1390 srcpad = gst_ghost_pad_new (name, pad);
1391 gst_pad_set_active (srcpad, TRUE);
1392 gst_element_add_pad (priv->element, srcpad);
1395 stream = gst_rtsp_stream_new (idx, payloader, srcpad);
1397 gst_rtsp_stream_set_address_pool (stream, priv->pool);
1398 gst_rtsp_stream_set_profiles (stream, priv->profiles);
1399 gst_rtsp_stream_set_protocols (stream, priv->protocols);
1400 gst_rtsp_stream_set_retransmission_time (stream, priv->rtx_time);
1402 g_ptr_array_add (priv->streams, stream);
1405 g_list_free (priv->payloads);
1406 priv->payloads = _find_payload_types (media);
1408 n = priv->streams->len;
1409 for (i = 0; i < n; i++) {
1410 GstRTSPStream *stream = g_ptr_array_index (priv->streams, i);
1411 guint rtx_pt = _next_available_pt (priv->payloads);
1414 /* FIXME: ran out of space of dynamic payload types */
1418 gst_rtsp_stream_set_retransmission_pt (stream, rtx_pt);
1420 priv->payloads = g_list_append (priv->payloads, GUINT_TO_POINTER (rtx_pt));
1422 g_mutex_unlock (&priv->lock);
1424 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STREAM], 0, stream,
1431 gst_rtsp_media_remove_stream (GstRTSPMedia * media, GstRTSPStream * stream)
1433 GstRTSPMediaPrivate *priv;
1438 g_mutex_lock (&priv->lock);
1439 /* remove the ghostpad */
1440 srcpad = gst_rtsp_stream_get_srcpad (stream);
1441 gst_element_remove_pad (priv->element, srcpad);
1442 gst_object_unref (srcpad);
1443 /* now remove the stream */
1444 g_object_ref (stream);
1445 g_ptr_array_remove (priv->streams, stream);
1446 g_mutex_unlock (&priv->lock);
1448 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_REMOVED_STREAM], 0,
1451 g_object_unref (stream);
1455 * gst_rtsp_media_n_streams:
1456 * @media: a #GstRTSPMedia
1458 * Get the number of streams in this media.
1460 * Returns: The number of streams.
1463 gst_rtsp_media_n_streams (GstRTSPMedia * media)
1465 GstRTSPMediaPrivate *priv;
1468 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), 0);
1472 g_mutex_lock (&priv->lock);
1473 res = priv->streams->len;
1474 g_mutex_unlock (&priv->lock);
1480 * gst_rtsp_media_get_stream:
1481 * @media: a #GstRTSPMedia
1482 * @idx: the stream index
1484 * Retrieve the stream with index @idx from @media.
1486 * Returns: (nullable) (transfer none): the #GstRTSPStream at index
1487 * @idx or %NULL when a stream with that index did not exist.
1490 gst_rtsp_media_get_stream (GstRTSPMedia * media, guint idx)
1492 GstRTSPMediaPrivate *priv;
1495 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1499 g_mutex_lock (&priv->lock);
1500 if (idx < priv->streams->len)
1501 res = g_ptr_array_index (priv->streams, idx);
1504 g_mutex_unlock (&priv->lock);
1510 * gst_rtsp_media_find_stream:
1511 * @media: a #GstRTSPMedia
1512 * @control: the control of the stream
1514 * Find a stream in @media with @control as the control uri.
1516 * Returns: (nullable) (transfer none): the #GstRTSPStream with
1517 * control uri @control or %NULL when a stream with that control did
1521 gst_rtsp_media_find_stream (GstRTSPMedia * media, const gchar * control)
1523 GstRTSPMediaPrivate *priv;
1527 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1528 g_return_val_if_fail (control != NULL, NULL);
1534 g_mutex_lock (&priv->lock);
1535 for (i = 0; i < priv->streams->len; i++) {
1536 GstRTSPStream *test;
1538 test = g_ptr_array_index (priv->streams, i);
1539 if (gst_rtsp_stream_has_control (test, control)) {
1544 g_mutex_unlock (&priv->lock);
1549 /* called with state-lock */
1551 default_convert_range (GstRTSPMedia * media, GstRTSPTimeRange * range,
1552 GstRTSPRangeUnit unit)
1554 return gst_rtsp_range_convert_units (range, unit);
1558 * gst_rtsp_media_get_range_string:
1559 * @media: a #GstRTSPMedia
1560 * @play: for the PLAY request
1561 * @unit: the unit to use for the string
1563 * Get the current range as a string. @media must be prepared with
1564 * gst_rtsp_media_prepare ().
1566 * Returns: (transfer full): The range as a string, g_free() after usage.
1569 gst_rtsp_media_get_range_string (GstRTSPMedia * media, gboolean play,
1570 GstRTSPRangeUnit unit)
1572 GstRTSPMediaClass *klass;
1573 GstRTSPMediaPrivate *priv;
1575 GstRTSPTimeRange range;
1577 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1578 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1579 g_return_val_if_fail (klass->convert_range != NULL, FALSE);
1583 g_rec_mutex_lock (&priv->state_lock);
1584 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
1585 priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
1588 g_mutex_lock (&priv->lock);
1590 /* Update the range value with current position/duration */
1591 collect_media_stats (media);
1594 range = priv->range;
1596 if (!play && priv->n_active > 0) {
1597 range.min.type = GST_RTSP_TIME_NOW;
1598 range.min.seconds = -1;
1600 g_mutex_unlock (&priv->lock);
1601 g_rec_mutex_unlock (&priv->state_lock);
1603 if (!klass->convert_range (media, &range, unit))
1604 goto conversion_failed;
1606 result = gst_rtsp_range_to_string (&range);
1613 GST_WARNING ("media %p was not prepared", media);
1614 g_rec_mutex_unlock (&priv->state_lock);
1619 GST_WARNING ("range conversion to unit %d failed", unit);
1625 stream_update_blocked (GstRTSPStream * stream, GstRTSPMedia * media)
1627 gst_rtsp_stream_set_blocked (stream, media->priv->blocked);
1631 media_streams_set_blocked (GstRTSPMedia * media, gboolean blocked)
1633 GstRTSPMediaPrivate *priv = media->priv;
1635 GST_DEBUG ("media %p set blocked %d", media, blocked);
1636 priv->blocked = blocked;
1637 g_ptr_array_foreach (priv->streams, (GFunc) stream_update_blocked, media);
1641 gst_rtsp_media_set_status (GstRTSPMedia * media, GstRTSPMediaStatus status)
1643 GstRTSPMediaPrivate *priv = media->priv;
1645 g_mutex_lock (&priv->lock);
1646 priv->status = status;
1647 GST_DEBUG ("setting new status to %d", status);
1648 g_cond_broadcast (&priv->cond);
1649 g_mutex_unlock (&priv->lock);
1653 * gst_rtsp_media_get_status:
1654 * @media: a #GstRTSPMedia
1656 * Get the status of @media. When @media is busy preparing, this function waits
1657 * until @media is prepared or in error.
1659 * Returns: the status of @media.
1662 gst_rtsp_media_get_status (GstRTSPMedia * media)
1664 GstRTSPMediaPrivate *priv = media->priv;
1665 GstRTSPMediaStatus result;
1668 g_mutex_lock (&priv->lock);
1669 end_time = g_get_monotonic_time () + 20 * G_TIME_SPAN_SECOND;
1670 /* while we are preparing, wait */
1671 while (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING) {
1672 GST_DEBUG ("waiting for status change");
1673 if (!g_cond_wait_until (&priv->cond, &priv->lock, end_time)) {
1674 GST_DEBUG ("timeout, assuming error status");
1675 priv->status = GST_RTSP_MEDIA_STATUS_ERROR;
1678 /* could be success or error */
1679 result = priv->status;
1680 GST_DEBUG ("got status %d", result);
1681 g_mutex_unlock (&priv->lock);
1687 * gst_rtsp_media_seek:
1688 * @media: a #GstRTSPMedia
1689 * @range: (transfer none): a #GstRTSPTimeRange
1691 * Seek the pipeline of @media to @range. @media must be prepared with
1692 * gst_rtsp_media_prepare().
1694 * Returns: %TRUE on success.
1697 gst_rtsp_media_seek (GstRTSPMedia * media, GstRTSPTimeRange * range)
1699 GstRTSPMediaClass *klass;
1700 GstRTSPMediaPrivate *priv;
1702 GstClockTime start, stop;
1703 GstSeekType start_type, stop_type;
1706 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1708 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1709 g_return_val_if_fail (range != NULL, FALSE);
1710 g_return_val_if_fail (klass->convert_range != NULL, FALSE);
1714 g_rec_mutex_lock (&priv->state_lock);
1715 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
1718 /* Update the seekable state of the pipeline in case it changed */
1719 query = gst_query_new_seeking (GST_FORMAT_TIME);
1720 if (gst_element_query (priv->pipeline, query)) {
1725 gst_query_parse_seeking (query, &format, &seekable, &start, &end);
1726 priv->seekable = seekable;
1728 gst_query_unref (query);
1730 if (!priv->seekable)
1733 start_type = stop_type = GST_SEEK_TYPE_NONE;
1735 if (!klass->convert_range (media, range, GST_RTSP_RANGE_NPT))
1737 gst_rtsp_range_get_times (range, &start, &stop);
1739 GST_INFO ("got %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1740 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1741 GST_INFO ("current %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1742 GST_TIME_ARGS (priv->range_start), GST_TIME_ARGS (priv->range_stop));
1744 if (start != GST_CLOCK_TIME_NONE)
1745 start_type = GST_SEEK_TYPE_SET;
1747 if (priv->range_stop == stop)
1748 stop = GST_CLOCK_TIME_NONE;
1749 else if (stop != GST_CLOCK_TIME_NONE)
1750 stop_type = GST_SEEK_TYPE_SET;
1752 if (start != GST_CLOCK_TIME_NONE || stop != GST_CLOCK_TIME_NONE) {
1755 GST_INFO ("seeking to %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1756 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1758 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARING);
1760 media_streams_set_blocked (media, TRUE);
1762 /* depends on the current playing state of the pipeline. We might need to
1763 * queue this until we get EOS. */
1764 flags = GST_SEEK_FLAG_FLUSH;
1766 /* if range start was not supplied we must continue from current position.
1767 * but since we're doing a flushing seek, let us query the current position
1768 * so we end up at exactly the same position after the seek. */
1769 if (range->min.type == GST_RTSP_TIME_END) { /* Yepp, that's right! */
1771 gboolean ret = FALSE;
1773 if (klass->query_position)
1774 ret = klass->query_position (media, &position);
1777 GST_WARNING ("position query failed");
1779 GST_DEBUG ("doing accurate seek to %" GST_TIME_FORMAT,
1780 GST_TIME_ARGS (position));
1782 start_type = GST_SEEK_TYPE_SET;
1783 flags |= GST_SEEK_FLAG_ACCURATE;
1786 /* only set keyframe flag when modifying start */
1787 if (start_type != GST_SEEK_TYPE_NONE)
1788 flags |= GST_SEEK_FLAG_KEY_UNIT;
1791 /* FIXME, we only do forwards playback, no trick modes yet */
1792 res = gst_element_seek (priv->pipeline, 1.0, GST_FORMAT_TIME,
1793 flags, start_type, start, stop_type, stop);
1795 /* and block for the seek to complete */
1796 GST_INFO ("done seeking %d", res);
1797 g_rec_mutex_unlock (&priv->state_lock);
1799 /* wait until pipeline is prerolled again, this will also collect stats */
1800 if (!wait_preroll (media))
1801 goto preroll_failed;
1803 g_rec_mutex_lock (&priv->state_lock);
1804 GST_INFO ("prerolled again");
1806 GST_INFO ("no seek needed");
1809 g_rec_mutex_unlock (&priv->state_lock);
1816 g_rec_mutex_unlock (&priv->state_lock);
1817 GST_INFO ("media %p is not prepared", media);
1822 g_rec_mutex_unlock (&priv->state_lock);
1823 GST_INFO ("pipeline is not seekable");
1828 g_rec_mutex_unlock (&priv->state_lock);
1829 GST_WARNING ("conversion to npt not supported");
1834 GST_WARNING ("failed to preroll after seek");
1840 stream_collect_blocking (GstRTSPStream * stream, gboolean * blocked)
1842 *blocked &= gst_rtsp_stream_is_blocking (stream);
1846 media_streams_blocking (GstRTSPMedia * media)
1848 gboolean blocking = TRUE;
1850 g_ptr_array_foreach (media->priv->streams, (GFunc) stream_collect_blocking,
1856 static GstStateChangeReturn
1857 set_state (GstRTSPMedia * media, GstState state)
1859 GstRTSPMediaPrivate *priv = media->priv;
1860 GstStateChangeReturn ret;
1862 GST_INFO ("set state to %s for media %p", gst_element_state_get_name (state),
1864 ret = gst_element_set_state (priv->pipeline, state);
1869 static GstStateChangeReturn
1870 set_target_state (GstRTSPMedia * media, GstState state, gboolean do_state)
1872 GstRTSPMediaPrivate *priv = media->priv;
1873 GstStateChangeReturn ret;
1875 GST_INFO ("set target state to %s for media %p",
1876 gst_element_state_get_name (state), media);
1877 priv->target_state = state;
1879 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_TARGET_STATE], 0,
1880 priv->target_state, NULL);
1883 ret = set_state (media, state);
1885 ret = GST_STATE_CHANGE_SUCCESS;
1890 /* called with state-lock */
1892 default_handle_message (GstRTSPMedia * media, GstMessage * message)
1894 GstRTSPMediaPrivate *priv = media->priv;
1895 GstMessageType type;
1897 type = GST_MESSAGE_TYPE (message);
1900 case GST_MESSAGE_STATE_CHANGED:
1902 case GST_MESSAGE_BUFFERING:
1906 gst_message_parse_buffering (message, &percent);
1908 /* no state management needed for live pipelines */
1912 if (percent == 100) {
1913 /* a 100% message means buffering is done */
1914 priv->buffering = FALSE;
1915 /* if the desired state is playing, go back */
1916 if (priv->target_state == GST_STATE_PLAYING) {
1917 GST_INFO ("Buffering done, setting pipeline to PLAYING");
1918 set_state (media, GST_STATE_PLAYING);
1920 GST_INFO ("Buffering done");
1923 /* buffering busy */
1924 if (priv->buffering == FALSE) {
1925 if (priv->target_state == GST_STATE_PLAYING) {
1926 /* we were not buffering but PLAYING, PAUSE the pipeline. */
1927 GST_INFO ("Buffering, setting pipeline to PAUSED ...");
1928 set_state (media, GST_STATE_PAUSED);
1930 GST_INFO ("Buffering ...");
1933 priv->buffering = TRUE;
1937 case GST_MESSAGE_LATENCY:
1939 gst_bin_recalculate_latency (GST_BIN_CAST (priv->pipeline));
1942 case GST_MESSAGE_ERROR:
1947 gst_message_parse_error (message, &gerror, &debug);
1948 GST_WARNING ("%p: got error %s (%s)", media, gerror->message, debug);
1949 g_error_free (gerror);
1952 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
1955 case GST_MESSAGE_WARNING:
1960 gst_message_parse_warning (message, &gerror, &debug);
1961 GST_WARNING ("%p: got warning %s (%s)", media, gerror->message, debug);
1962 g_error_free (gerror);
1966 case GST_MESSAGE_ELEMENT:
1968 const GstStructure *s;
1970 s = gst_message_get_structure (message);
1971 if (gst_structure_has_name (s, "GstRTSPStreamBlocking")) {
1972 GST_DEBUG ("media received blocking message");
1973 if (priv->blocked && media_streams_blocking (media)) {
1974 GST_DEBUG ("media is blocking");
1975 collect_media_stats (media);
1977 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
1978 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
1983 case GST_MESSAGE_STREAM_STATUS:
1985 case GST_MESSAGE_ASYNC_DONE:
1987 /* when we are dynamically adding pads, the addition of the udpsrc will
1988 * temporarily produce ASYNC_DONE messages. We have to ignore them and
1989 * wait for the final ASYNC_DONE after everything prerolled */
1990 GST_INFO ("%p: ignoring ASYNC_DONE", media);
1992 GST_INFO ("%p: got ASYNC_DONE", media);
1993 collect_media_stats (media);
1995 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
1996 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
1999 case GST_MESSAGE_EOS:
2000 GST_INFO ("%p: got EOS", media);
2002 if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARING) {
2003 GST_DEBUG ("shutting down after EOS");
2004 finish_unprepare (media);
2008 GST_INFO ("%p: got message type %d (%s)", media, type,
2009 gst_message_type_get_name (type));
2016 bus_message (GstBus * bus, GstMessage * message, GstRTSPMedia * media)
2018 GstRTSPMediaPrivate *priv = media->priv;
2019 GstRTSPMediaClass *klass;
2022 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2024 g_rec_mutex_lock (&priv->state_lock);
2025 if (klass->handle_message)
2026 ret = klass->handle_message (media, message);
2029 g_rec_mutex_unlock (&priv->state_lock);
2035 watch_destroyed (GstRTSPMedia * media)
2037 GST_DEBUG_OBJECT (media, "source destroyed");
2038 g_object_unref (media);
2042 find_payload_element (GstElement * payloader)
2044 GstElement *pay = NULL;
2046 if (GST_IS_BIN (payloader)) {
2048 GValue item = { 0 };
2050 iter = gst_bin_iterate_recurse (GST_BIN (payloader));
2051 while (gst_iterator_next (iter, &item) == GST_ITERATOR_OK) {
2052 GstElement *element = (GstElement *) g_value_get_object (&item);
2053 GstElementClass *eclass = GST_ELEMENT_GET_CLASS (element);
2057 gst_element_class_get_metadata (eclass, GST_ELEMENT_METADATA_KLASS);
2061 if (strstr (klass, "Payloader") && strstr (klass, "RTP")) {
2062 pay = gst_object_ref (element);
2063 g_value_unset (&item);
2066 g_value_unset (&item);
2068 gst_iterator_free (iter);
2070 pay = g_object_ref (payloader);
2076 /* called from streaming threads */
2078 pad_added_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
2080 GstRTSPMediaPrivate *priv = media->priv;
2081 GstRTSPStream *stream;
2084 /* find the real payload element */
2085 pay = find_payload_element (element);
2086 stream = gst_rtsp_media_create_stream (media, pay, pad);
2087 gst_object_unref (pay);
2089 GST_INFO ("pad added %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
2091 g_rec_mutex_lock (&priv->state_lock);
2092 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARING)
2095 g_object_set_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream", stream);
2097 /* we will be adding elements below that will cause ASYNC_DONE to be
2098 * posted in the bus. We want to ignore those messages until the
2099 * pipeline really prerolled. */
2100 priv->adding = TRUE;
2102 /* join the element in the PAUSED state because this callback is
2103 * called from the streaming thread and it is PAUSED */
2104 if (!gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
2105 priv->rtpbin, GST_STATE_PAUSED)) {
2106 GST_WARNING ("failed to join bin element");
2109 priv->adding = FALSE;
2110 g_rec_mutex_unlock (&priv->state_lock);
2117 gst_rtsp_media_remove_stream (media, stream);
2118 g_rec_mutex_unlock (&priv->state_lock);
2119 GST_INFO ("ignore pad because we are not preparing");
2125 pad_removed_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
2127 GstRTSPMediaPrivate *priv = media->priv;
2128 GstRTSPStream *stream;
2130 stream = g_object_get_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream");
2134 GST_INFO ("pad removed %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
2136 g_rec_mutex_lock (&priv->state_lock);
2137 gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
2138 g_rec_mutex_unlock (&priv->state_lock);
2140 gst_rtsp_media_remove_stream (media, stream);
2144 remove_fakesink (GstRTSPMediaPrivate * priv)
2146 GstElement *fakesink;
2148 g_mutex_lock (&priv->lock);
2149 if ((fakesink = priv->fakesink))
2150 gst_object_ref (fakesink);
2151 priv->fakesink = NULL;
2152 g_mutex_unlock (&priv->lock);
2155 gst_bin_remove (GST_BIN (priv->pipeline), fakesink);
2156 gst_element_set_state (fakesink, GST_STATE_NULL);
2157 gst_object_unref (fakesink);
2158 GST_INFO ("removed fakesink");
2163 no_more_pads_cb (GstElement * element, GstRTSPMedia * media)
2165 GstRTSPMediaPrivate *priv = media->priv;
2167 GST_INFO ("no more pads");
2168 remove_fakesink (priv);
2171 typedef struct _DynPaySignalHandlers DynPaySignalHandlers;
2173 struct _DynPaySignalHandlers
2175 gulong pad_added_handler;
2176 gulong pad_removed_handler;
2177 gulong no_more_pads_handler;
2181 start_preroll (GstRTSPMedia * media)
2183 GstRTSPMediaPrivate *priv = media->priv;
2184 GstStateChangeReturn ret;
2186 GST_INFO ("setting pipeline to PAUSED for media %p", media);
2187 /* first go to PAUSED */
2188 ret = set_target_state (media, GST_STATE_PAUSED, TRUE);
2191 case GST_STATE_CHANGE_SUCCESS:
2192 GST_INFO ("SUCCESS state change for media %p", media);
2193 priv->seekable = TRUE;
2195 case GST_STATE_CHANGE_ASYNC:
2196 GST_INFO ("ASYNC state change for media %p", media);
2197 priv->seekable = TRUE;
2199 case GST_STATE_CHANGE_NO_PREROLL:
2200 /* we need to go to PLAYING */
2201 GST_INFO ("NO_PREROLL state change: live media %p", media);
2202 /* FIXME we disable seeking for live streams for now. We should perform a
2203 * seeking query in preroll instead */
2204 priv->seekable = FALSE;
2205 priv->is_live = TRUE;
2206 /* start blocked to make sure nothing goes to the sink */
2207 media_streams_set_blocked (media, TRUE);
2208 ret = set_state (media, GST_STATE_PLAYING);
2209 if (ret == GST_STATE_CHANGE_FAILURE)
2212 case GST_STATE_CHANGE_FAILURE:
2220 GST_WARNING ("failed to preroll pipeline");
2226 wait_preroll (GstRTSPMedia * media)
2228 GstRTSPMediaStatus status;
2230 GST_DEBUG ("wait to preroll pipeline");
2232 /* wait until pipeline is prerolled */
2233 status = gst_rtsp_media_get_status (media);
2234 if (status == GST_RTSP_MEDIA_STATUS_ERROR)
2235 goto preroll_failed;
2241 GST_WARNING ("failed to preroll pipeline");
2247 start_prepare (GstRTSPMedia * media)
2249 GstRTSPMediaPrivate *priv = media->priv;
2253 /* link streams we already have, other streams might appear when we have
2254 * dynamic elements */
2255 for (i = 0; i < priv->streams->len; i++) {
2256 GstRTSPStream *stream;
2258 stream = g_ptr_array_index (priv->streams, i);
2260 if (!gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
2261 priv->rtpbin, GST_STATE_NULL)) {
2262 goto join_bin_failed;
2267 g_object_set (priv->rtpbin, "do-retransmission", priv->rtx_time > 0, NULL);
2269 for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
2270 GstElement *elem = walk->data;
2271 DynPaySignalHandlers *handlers = g_slice_new (DynPaySignalHandlers);
2273 GST_INFO ("adding callbacks for dynamic element %p", elem);
2275 handlers->pad_added_handler = g_signal_connect (elem, "pad-added",
2276 (GCallback) pad_added_cb, media);
2277 handlers->pad_removed_handler = g_signal_connect (elem, "pad-removed",
2278 (GCallback) pad_removed_cb, media);
2279 handlers->no_more_pads_handler = g_signal_connect (elem, "no-more-pads",
2280 (GCallback) no_more_pads_cb, media);
2282 g_object_set_data (G_OBJECT (elem), "gst-rtsp-dynpay-handlers", handlers);
2284 /* we add a fakesink here in order to make the state change async. We remove
2285 * the fakesink again in the no-more-pads callback. */
2286 priv->fakesink = gst_element_factory_make ("fakesink", "fakesink");
2287 gst_bin_add (GST_BIN (priv->pipeline), priv->fakesink);
2290 if (!start_preroll (media))
2291 goto preroll_failed;
2297 GST_WARNING ("failed to join bin element");
2298 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2303 GST_WARNING ("failed to preroll pipeline");
2304 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2310 default_prepare (GstRTSPMedia * media, GstRTSPThread * thread)
2312 GstRTSPMediaPrivate *priv;
2313 GstRTSPMediaClass *klass;
2315 GMainContext *context;
2320 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2322 if (!klass->create_rtpbin)
2323 goto no_create_rtpbin;
2325 priv->rtpbin = klass->create_rtpbin (media);
2326 if (priv->rtpbin != NULL) {
2327 gboolean success = TRUE;
2329 if (klass->setup_rtpbin)
2330 success = klass->setup_rtpbin (media, priv->rtpbin);
2332 if (success == FALSE) {
2333 gst_object_unref (priv->rtpbin);
2334 priv->rtpbin = NULL;
2337 if (priv->rtpbin == NULL)
2340 priv->thread = thread;
2341 context = (thread != NULL) ? (thread->context) : NULL;
2343 bus = gst_pipeline_get_bus (GST_PIPELINE_CAST (priv->pipeline));
2345 /* add the pipeline bus to our custom mainloop */
2346 priv->source = gst_bus_create_watch (bus);
2347 gst_object_unref (bus);
2349 g_source_set_callback (priv->source, (GSourceFunc) bus_message,
2350 g_object_ref (media), (GDestroyNotify) watch_destroyed);
2352 priv->id = g_source_attach (priv->source, context);
2354 /* add stuff to the bin */
2355 gst_bin_add (GST_BIN (priv->pipeline), priv->rtpbin);
2357 /* do remainder in context */
2358 source = g_idle_source_new ();
2359 g_source_set_callback (source, (GSourceFunc) start_prepare, media, NULL);
2360 g_source_attach (source, context);
2361 g_source_unref (source);
2368 GST_ERROR ("no create_rtpbin function");
2369 g_critical ("no create_rtpbin vmethod function set");
2374 GST_WARNING ("no rtpbin element");
2375 g_warning ("failed to create element 'rtpbin', check your installation");
2381 * gst_rtsp_media_prepare:
2382 * @media: a #GstRTSPMedia
2383 * @thread: (transfer full) (allow-none): a #GstRTSPThread to run the
2384 * bus handler or %NULL
2386 * Prepare @media for streaming. This function will create the objects
2387 * to manage the streaming. A pipeline must have been set on @media with
2388 * gst_rtsp_media_take_pipeline().
2390 * It will preroll the pipeline and collect vital information about the streams
2391 * such as the duration.
2393 * Returns: %TRUE on success.
2396 gst_rtsp_media_prepare (GstRTSPMedia * media, GstRTSPThread * thread)
2398 GstRTSPMediaPrivate *priv;
2399 GstRTSPMediaClass *klass;
2401 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2405 g_rec_mutex_lock (&priv->state_lock);
2406 priv->prepare_count++;
2408 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED ||
2409 priv->status == GST_RTSP_MEDIA_STATUS_SUSPENDED)
2412 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
2415 if (priv->status != GST_RTSP_MEDIA_STATUS_UNPREPARED)
2416 goto not_unprepared;
2418 if (!priv->reusable && priv->reused)
2421 GST_INFO ("preparing media %p", media);
2423 /* reset some variables */
2424 priv->is_live = FALSE;
2425 priv->seekable = FALSE;
2426 priv->buffering = FALSE;
2428 /* we're preparing now */
2429 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARING);
2431 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2432 if (klass->prepare) {
2433 if (!klass->prepare (media, thread))
2434 goto prepare_failed;
2438 g_rec_mutex_unlock (&priv->state_lock);
2440 /* now wait for all pads to be prerolled, FIXME, we should somehow be
2441 * able to do this async so that we don't block the server thread. */
2442 if (!wait_preroll (media))
2443 goto preroll_failed;
2445 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_PREPARED], 0, NULL);
2447 GST_INFO ("object %p is prerolled", media);
2454 /* we are not going to use the giving thread, so stop it. */
2456 gst_rtsp_thread_stop (thread);
2461 GST_LOG ("media %p was prepared", media);
2462 /* we are not going to use the giving thread, so stop it. */
2464 gst_rtsp_thread_stop (thread);
2465 g_rec_mutex_unlock (&priv->state_lock);
2471 /* we are not going to use the giving thread, so stop it. */
2473 gst_rtsp_thread_stop (thread);
2474 GST_WARNING ("media %p was not unprepared", media);
2475 priv->prepare_count--;
2476 g_rec_mutex_unlock (&priv->state_lock);
2481 /* we are not going to use the giving thread, so stop it. */
2483 gst_rtsp_thread_stop (thread);
2484 priv->prepare_count--;
2485 g_rec_mutex_unlock (&priv->state_lock);
2486 GST_WARNING ("can not reuse media %p", media);
2491 /* we are not going to use the giving thread, so stop it. */
2493 gst_rtsp_thread_stop (thread);
2494 priv->prepare_count--;
2495 g_rec_mutex_unlock (&priv->state_lock);
2496 GST_ERROR ("failed to prepare media");
2501 GST_WARNING ("failed to preroll pipeline");
2502 gst_rtsp_media_unprepare (media);
2507 /* must be called with state-lock */
2509 finish_unprepare (GstRTSPMedia * media)
2511 GstRTSPMediaPrivate *priv = media->priv;
2515 GST_DEBUG ("shutting down");
2517 /* release the lock on shutdown, otherwise pad_added_cb might try to
2518 * acquire the lock and then we deadlock */
2519 g_rec_mutex_unlock (&priv->state_lock);
2520 set_state (media, GST_STATE_NULL);
2521 g_rec_mutex_lock (&priv->state_lock);
2522 remove_fakesink (priv);
2524 for (i = 0; i < priv->streams->len; i++) {
2525 GstRTSPStream *stream;
2527 GST_INFO ("Removing elements of stream %d from pipeline", i);
2529 stream = g_ptr_array_index (priv->streams, i);
2531 gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
2534 /* remove the pad signal handlers */
2535 for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
2536 GstElement *elem = walk->data;
2537 DynPaySignalHandlers *handlers;
2540 g_object_steal_data (G_OBJECT (elem), "gst-rtsp-dynpay-handlers");
2541 g_assert (handlers != NULL);
2543 g_signal_handler_disconnect (G_OBJECT (elem), handlers->pad_added_handler);
2544 g_signal_handler_disconnect (G_OBJECT (elem),
2545 handlers->pad_removed_handler);
2546 g_signal_handler_disconnect (G_OBJECT (elem),
2547 handlers->no_more_pads_handler);
2549 g_slice_free (DynPaySignalHandlers, handlers);
2552 gst_bin_remove (GST_BIN (priv->pipeline), priv->rtpbin);
2553 priv->rtpbin = NULL;
2556 gst_object_unref (priv->nettime);
2557 priv->nettime = NULL;
2559 priv->reused = TRUE;
2560 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_UNPREPARED);
2562 /* when the media is not reusable, this will effectively unref the media and
2564 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_UNPREPARED], 0, NULL);
2566 /* the source has the last ref to the media */
2568 GST_DEBUG ("destroy source");
2569 g_source_destroy (priv->source);
2570 g_source_unref (priv->source);
2573 GST_DEBUG ("stop thread");
2574 gst_rtsp_thread_stop (priv->thread);
2578 /* called with state-lock */
2580 default_unprepare (GstRTSPMedia * media)
2582 GstRTSPMediaPrivate *priv = media->priv;
2584 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_UNPREPARING);
2586 if (priv->eos_shutdown) {
2587 GST_DEBUG ("sending EOS for shutdown");
2588 /* ref so that we don't disappear */
2589 gst_element_send_event (priv->pipeline, gst_event_new_eos ());
2590 /* we need to go to playing again for the EOS to propagate, normally in this
2591 * state, nothing is receiving data from us anymore so this is ok. */
2592 set_state (media, GST_STATE_PLAYING);
2594 finish_unprepare (media);
2600 * gst_rtsp_media_unprepare:
2601 * @media: a #GstRTSPMedia
2603 * Unprepare @media. After this call, the media should be prepared again before
2604 * it can be used again. If the media is set to be non-reusable, a new instance
2607 * Returns: %TRUE on success.
2610 gst_rtsp_media_unprepare (GstRTSPMedia * media)
2612 GstRTSPMediaPrivate *priv;
2615 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2619 g_rec_mutex_lock (&priv->state_lock);
2620 if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARED)
2621 goto was_unprepared;
2623 priv->prepare_count--;
2624 if (priv->prepare_count > 0)
2627 GST_INFO ("unprepare media %p", media);
2629 media_streams_set_blocked (media, FALSE);
2630 set_target_state (media, GST_STATE_NULL, FALSE);
2633 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED) {
2634 GstRTSPMediaClass *klass;
2636 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2637 if (klass->unprepare)
2638 success = klass->unprepare (media);
2640 finish_unprepare (media);
2642 g_rec_mutex_unlock (&priv->state_lock);
2648 g_rec_mutex_unlock (&priv->state_lock);
2649 GST_INFO ("media %p was already unprepared", media);
2654 GST_INFO ("media %p still prepared %d times", media, priv->prepare_count);
2655 g_rec_mutex_unlock (&priv->state_lock);
2660 /* should be called with state-lock */
2662 get_clock_unlocked (GstRTSPMedia * media)
2664 if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED) {
2665 GST_DEBUG_OBJECT (media, "media was not prepared");
2668 return gst_pipeline_get_clock (GST_PIPELINE_CAST (media->priv->pipeline));
2672 * gst_rtsp_media_get_clock:
2673 * @media: a #GstRTSPMedia
2675 * Get the clock that is used by the pipeline in @media.
2677 * @media must be prepared before this method returns a valid clock object.
2679 * Returns: (transfer full): the #GstClock used by @media. unref after usage.
2682 gst_rtsp_media_get_clock (GstRTSPMedia * media)
2685 GstRTSPMediaPrivate *priv;
2687 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
2691 g_rec_mutex_lock (&priv->state_lock);
2692 clock = get_clock_unlocked (media);
2693 g_rec_mutex_unlock (&priv->state_lock);
2699 * gst_rtsp_media_get_base_time:
2700 * @media: a #GstRTSPMedia
2702 * Get the base_time that is used by the pipeline in @media.
2704 * @media must be prepared before this method returns a valid base_time.
2706 * Returns: the base_time used by @media.
2709 gst_rtsp_media_get_base_time (GstRTSPMedia * media)
2711 GstClockTime result;
2712 GstRTSPMediaPrivate *priv;
2714 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_CLOCK_TIME_NONE);
2718 g_rec_mutex_lock (&priv->state_lock);
2719 if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
2722 result = gst_element_get_base_time (media->priv->pipeline);
2723 g_rec_mutex_unlock (&priv->state_lock);
2730 g_rec_mutex_unlock (&priv->state_lock);
2731 GST_DEBUG_OBJECT (media, "media was not prepared");
2732 return GST_CLOCK_TIME_NONE;
2737 * gst_rtsp_media_get_time_provider:
2738 * @media: a #GstRTSPMedia
2739 * @address: (allow-none): an address or %NULL
2740 * @port: a port or 0
2742 * Get the #GstNetTimeProvider for the clock used by @media. The time provider
2743 * will listen on @address and @port for client time requests.
2745 * Returns: (transfer full): the #GstNetTimeProvider of @media.
2747 GstNetTimeProvider *
2748 gst_rtsp_media_get_time_provider (GstRTSPMedia * media, const gchar * address,
2751 GstRTSPMediaPrivate *priv;
2752 GstNetTimeProvider *provider = NULL;
2754 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
2758 g_rec_mutex_lock (&priv->state_lock);
2759 if (priv->time_provider) {
2760 if ((provider = priv->nettime) == NULL) {
2763 if (priv->time_provider && (clock = get_clock_unlocked (media))) {
2764 provider = gst_net_time_provider_new (clock, address, port);
2765 gst_object_unref (clock);
2767 priv->nettime = provider;
2771 g_rec_mutex_unlock (&priv->state_lock);
2774 gst_object_ref (provider);
2780 default_setup_sdp (GstRTSPMedia * media, GstSDPMessage * sdp, GstSDPInfo * info)
2782 return gst_rtsp_sdp_from_media (sdp, info, media);
2786 * gst_rtsp_media_setup_sdp:
2787 * @media: a #GstRTSPMedia
2788 * @sdp: (transfer none): a #GstSDPMessage
2789 * @info: (transfer none): a #GstSDPInfo
2791 * Add @media specific info to @sdp. @info is used to configure the connection
2792 * information in the SDP.
2794 * Returns: TRUE on success.
2797 gst_rtsp_media_setup_sdp (GstRTSPMedia * media, GstSDPMessage * sdp,
2800 GstRTSPMediaPrivate *priv;
2801 GstRTSPMediaClass *klass;
2804 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2805 g_return_val_if_fail (sdp != NULL, FALSE);
2806 g_return_val_if_fail (info != NULL, FALSE);
2810 g_rec_mutex_lock (&priv->state_lock);
2812 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2814 if (!klass->setup_sdp)
2817 res = klass->setup_sdp (media, sdp, info);
2819 g_rec_mutex_unlock (&priv->state_lock);
2826 g_rec_mutex_unlock (&priv->state_lock);
2827 GST_ERROR ("no setup_sdp function");
2828 g_critical ("no setup_sdp vmethod function set");
2834 do_set_seqnum (GstRTSPStream * stream)
2837 seq_num = gst_rtsp_stream_get_current_seqnum (stream);
2838 gst_rtsp_stream_set_seqnum_offset (stream, seq_num + 1);
2841 /* call with state_lock */
2843 default_suspend (GstRTSPMedia * media)
2845 GstRTSPMediaPrivate *priv = media->priv;
2846 GstStateChangeReturn ret;
2848 switch (priv->suspend_mode) {
2849 case GST_RTSP_SUSPEND_MODE_NONE:
2850 GST_DEBUG ("media %p no suspend", media);
2852 case GST_RTSP_SUSPEND_MODE_PAUSE:
2853 GST_DEBUG ("media %p suspend to PAUSED", media);
2854 ret = set_target_state (media, GST_STATE_PAUSED, TRUE);
2855 if (ret == GST_STATE_CHANGE_FAILURE)
2858 case GST_RTSP_SUSPEND_MODE_RESET:
2859 GST_DEBUG ("media %p suspend to NULL", media);
2860 ret = set_target_state (media, GST_STATE_NULL, TRUE);
2861 if (ret == GST_STATE_CHANGE_FAILURE)
2863 /* Because payloader needs to set the sequence number as
2864 * monotonic, we need to preserve the sequence number
2865 * after pause. (otherwise going from pause to play, which
2866 * is actually from NULL to PLAY will create a new sequence
2868 g_ptr_array_foreach (priv->streams, (GFunc) do_set_seqnum, NULL);
2874 /* let the streams do the state changes freely, if any */
2875 media_streams_set_blocked (media, FALSE);
2882 GST_WARNING ("failed changing pipeline's state for media %p", media);
2888 * gst_rtsp_media_suspend:
2889 * @media: a #GstRTSPMedia
2891 * Suspend @media. The state of the pipeline managed by @media is set to
2892 * GST_STATE_NULL but all streams are kept. @media can be prepared again
2893 * with gst_rtsp_media_unsuspend()
2895 * @media must be prepared with gst_rtsp_media_prepare();
2897 * Returns: %TRUE on success.
2900 gst_rtsp_media_suspend (GstRTSPMedia * media)
2902 GstRTSPMediaPrivate *priv = media->priv;
2903 GstRTSPMediaClass *klass;
2905 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2907 GST_FIXME ("suspend for dynamic pipelines needs fixing");
2909 g_rec_mutex_lock (&priv->state_lock);
2910 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
2913 /* don't attempt to suspend when something is busy */
2914 if (priv->n_active > 0)
2917 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2918 if (klass->suspend) {
2919 if (!klass->suspend (media))
2920 goto suspend_failed;
2923 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_SUSPENDED);
2925 g_rec_mutex_unlock (&priv->state_lock);
2932 g_rec_mutex_unlock (&priv->state_lock);
2933 GST_WARNING ("media %p was not prepared", media);
2938 g_rec_mutex_unlock (&priv->state_lock);
2939 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2940 GST_WARNING ("failed to suspend media %p", media);
2945 /* call with state_lock */
2947 default_unsuspend (GstRTSPMedia * media)
2949 GstRTSPMediaPrivate *priv = media->priv;
2951 switch (priv->suspend_mode) {
2952 case GST_RTSP_SUSPEND_MODE_NONE:
2953 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
2955 case GST_RTSP_SUSPEND_MODE_PAUSE:
2956 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
2958 case GST_RTSP_SUSPEND_MODE_RESET:
2960 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARING);
2961 if (!start_preroll (media))
2963 g_rec_mutex_unlock (&priv->state_lock);
2965 if (!wait_preroll (media))
2966 goto preroll_failed;
2968 g_rec_mutex_lock (&priv->state_lock);
2979 GST_WARNING ("failed to preroll pipeline");
2984 GST_WARNING ("failed to preroll pipeline");
2990 * gst_rtsp_media_unsuspend:
2991 * @media: a #GstRTSPMedia
2993 * Unsuspend @media if it was in a suspended state. This method does nothing
2994 * when the media was not in the suspended state.
2996 * Returns: %TRUE on success.
2999 gst_rtsp_media_unsuspend (GstRTSPMedia * media)
3001 GstRTSPMediaPrivate *priv = media->priv;
3002 GstRTSPMediaClass *klass;
3004 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
3006 g_rec_mutex_lock (&priv->state_lock);
3007 if (priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
3010 klass = GST_RTSP_MEDIA_GET_CLASS (media);
3011 if (klass->unsuspend) {
3012 if (!klass->unsuspend (media))
3013 goto unsuspend_failed;
3017 g_rec_mutex_unlock (&priv->state_lock);
3024 g_rec_mutex_unlock (&priv->state_lock);
3025 GST_WARNING ("failed to unsuspend media %p", media);
3026 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
3031 /* must be called with state-lock */
3033 media_set_pipeline_state_locked (GstRTSPMedia * media, GstState state)
3035 GstRTSPMediaPrivate *priv = media->priv;
3037 if (state == GST_STATE_NULL) {
3038 gst_rtsp_media_unprepare (media);
3040 GST_INFO ("state %s media %p", gst_element_state_get_name (state), media);
3041 set_target_state (media, state, FALSE);
3042 /* when we are buffering, don't update the state yet, this will be done
3043 * when buffering finishes */
3044 if (priv->buffering) {
3045 GST_INFO ("Buffering busy, delay state change");
3047 if (state == GST_STATE_PLAYING)
3048 /* make sure pads are not blocking anymore when going to PLAYING */
3049 media_streams_set_blocked (media, FALSE);
3051 set_state (media, state);
3053 /* and suspend after pause */
3054 if (state == GST_STATE_PAUSED)
3055 gst_rtsp_media_suspend (media);
3061 * gst_rtsp_media_set_pipeline_state:
3062 * @media: a #GstRTSPMedia
3063 * @state: the target state of the pipeline
3065 * Set the state of the pipeline managed by @media to @state
3068 gst_rtsp_media_set_pipeline_state (GstRTSPMedia * media, GstState state)
3070 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
3072 g_rec_mutex_lock (&media->priv->state_lock);
3073 media_set_pipeline_state_locked (media, state);
3074 g_rec_mutex_unlock (&media->priv->state_lock);
3078 * gst_rtsp_media_set_state:
3079 * @media: a #GstRTSPMedia
3080 * @state: the target state of the media
3081 * @transports: (transfer none) (element-type GstRtspServer.RTSPStreamTransport):
3082 * a #GPtrArray of #GstRTSPStreamTransport pointers
3084 * Set the state of @media to @state and for the transports in @transports.
3086 * @media must be prepared with gst_rtsp_media_prepare();
3088 * Returns: %TRUE on success.
3091 gst_rtsp_media_set_state (GstRTSPMedia * media, GstState state,
3092 GPtrArray * transports)
3094 GstRTSPMediaPrivate *priv;
3096 gboolean activate, deactivate, do_state;
3099 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
3100 g_return_val_if_fail (transports != NULL, FALSE);
3104 g_rec_mutex_lock (&priv->state_lock);
3105 if (priv->status == GST_RTSP_MEDIA_STATUS_ERROR)
3107 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
3108 priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
3111 /* NULL and READY are the same */
3112 if (state == GST_STATE_READY)
3113 state = GST_STATE_NULL;
3115 activate = deactivate = FALSE;
3117 GST_INFO ("going to state %s media %p, target state %s",
3118 gst_element_state_get_name (state), media,
3119 gst_element_state_get_name (priv->target_state));
3122 case GST_STATE_NULL:
3123 /* we're going from PLAYING or PAUSED to READY or NULL, deactivate */
3124 if (priv->target_state >= GST_STATE_PAUSED)
3127 case GST_STATE_PAUSED:
3128 /* we're going from PLAYING to PAUSED, READY or NULL, deactivate */
3129 if (priv->target_state == GST_STATE_PLAYING)
3132 case GST_STATE_PLAYING:
3133 /* we're going to PLAYING, activate */
3139 old_active = priv->n_active;
3141 GST_DEBUG ("%d transports, activate %d, deactivate %d", transports->len,
3142 activate, deactivate);
3143 for (i = 0; i < transports->len; i++) {
3144 GstRTSPStreamTransport *trans;
3146 /* we need a non-NULL entry in the array */
3147 trans = g_ptr_array_index (transports, i);
3152 if (gst_rtsp_stream_transport_set_active (trans, TRUE))
3154 } else if (deactivate) {
3155 if (gst_rtsp_stream_transport_set_active (trans, FALSE))
3160 /* we just activated the first media, do the playing state change */
3161 if (old_active == 0 && activate)
3163 /* if we have no more active media, do the downward state changes */
3164 else if (priv->n_active == 0)
3169 GST_INFO ("state %d active %d media %p do_state %d", state, priv->n_active,
3172 if (priv->target_state != state) {
3174 media_set_pipeline_state_locked (media, state);
3176 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STATE], 0, state,
3180 /* remember where we are */
3181 if (state != GST_STATE_NULL && (state == GST_STATE_PAUSED ||
3182 old_active != priv->n_active))
3183 collect_media_stats (media);
3185 g_rec_mutex_unlock (&priv->state_lock);
3192 GST_WARNING ("media %p was not prepared", media);
3193 g_rec_mutex_unlock (&priv->state_lock);
3198 GST_WARNING ("media %p in error status while changing to state %d",
3200 if (state == GST_STATE_NULL) {
3201 for (i = 0; i < transports->len; i++) {
3202 GstRTSPStreamTransport *trans;
3204 /* we need a non-NULL entry in the array */
3205 trans = g_ptr_array_index (transports, i);
3209 gst_rtsp_stream_transport_set_active (trans, FALSE);
3213 g_rec_mutex_unlock (&priv->state_lock);