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 GstRTSPLowerTrans protocols;
87 gboolean eos_shutdown;
89 GstRTSPAddressPool *pool;
93 GRecMutex state_lock; /* locking order: state lock, lock */
94 GPtrArray *streams; /* protected by lock */
95 GList *dynamic; /* protected by lock */
96 GstRTSPMediaStatus status; /* protected by lock */
101 /* the pipeline for the media */
102 GstElement *pipeline;
103 GstElement *fakesink; /* protected by lock */
106 GstRTSPThread *thread;
108 gboolean time_provider;
109 GstNetTimeProvider *nettime;
114 GstState target_state;
116 /* RTP session manager */
119 /* the range of media */
120 GstRTSPTimeRange range; /* protected by lock */
121 GstClockTime range_start;
122 GstClockTime range_stop;
125 #define DEFAULT_SHARED FALSE
126 #define DEFAULT_SUSPEND_MODE GST_RTSP_SUSPEND_MODE_NONE
127 #define DEFAULT_REUSABLE FALSE
128 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | \
129 GST_RTSP_LOWER_TRANS_TCP
130 #define DEFAULT_EOS_SHUTDOWN FALSE
131 #define DEFAULT_BUFFER_SIZE 0x80000
132 #define DEFAULT_TIME_PROVIDER FALSE
134 /* define to dump received RTCP packets */
154 SIGNAL_REMOVED_STREAM,
161 GST_DEBUG_CATEGORY_STATIC (rtsp_media_debug);
162 #define GST_CAT_DEFAULT rtsp_media_debug
164 static void gst_rtsp_media_get_property (GObject * object, guint propid,
165 GValue * value, GParamSpec * pspec);
166 static void gst_rtsp_media_set_property (GObject * object, guint propid,
167 const GValue * value, GParamSpec * pspec);
168 static void gst_rtsp_media_finalize (GObject * obj);
170 static gboolean default_handle_message (GstRTSPMedia * media,
171 GstMessage * message);
172 static void finish_unprepare (GstRTSPMedia * media);
173 static gboolean default_unprepare (GstRTSPMedia * media);
174 static gboolean default_convert_range (GstRTSPMedia * media,
175 GstRTSPTimeRange * range, GstRTSPRangeUnit unit);
176 static gboolean default_query_position (GstRTSPMedia * media,
178 static gboolean default_query_stop (GstRTSPMedia * media, gint64 * stop);
180 static gboolean wait_preroll (GstRTSPMedia * media);
182 static guint gst_rtsp_media_signals[SIGNAL_LAST] = { 0 };
184 #define C_ENUM(v) ((gint) v)
186 #define GST_TYPE_RTSP_SUSPEND_MODE (gst_rtsp_suspend_mode_get_type())
188 gst_rtsp_suspend_mode_get_type (void)
191 static const GEnumValue values[] = {
192 {C_ENUM (GST_RTSP_SUSPEND_MODE_NONE), "GST_RTSP_SUSPEND_MODE_NONE", "none"},
193 {C_ENUM (GST_RTSP_SUSPEND_MODE_PAUSE), "GST_RTSP_SUSPEND_MODE_PAUSE",
195 {C_ENUM (GST_RTSP_SUSPEND_MODE_RESET), "GST_RTSP_SUSPEND_MODE_RESET",
200 if (g_once_init_enter (&id)) {
201 GType tmp = g_enum_register_static ("GstRTSPSuspendMode", values);
202 g_once_init_leave (&id, tmp);
207 G_DEFINE_TYPE (GstRTSPMedia, gst_rtsp_media, G_TYPE_OBJECT);
210 gst_rtsp_media_class_init (GstRTSPMediaClass * klass)
212 GObjectClass *gobject_class;
214 g_type_class_add_private (klass, sizeof (GstRTSPMediaPrivate));
216 gobject_class = G_OBJECT_CLASS (klass);
218 gobject_class->get_property = gst_rtsp_media_get_property;
219 gobject_class->set_property = gst_rtsp_media_set_property;
220 gobject_class->finalize = gst_rtsp_media_finalize;
222 g_object_class_install_property (gobject_class, PROP_SHARED,
223 g_param_spec_boolean ("shared", "Shared",
224 "If this media pipeline can be shared", DEFAULT_SHARED,
225 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
227 g_object_class_install_property (gobject_class, PROP_SUSPEND_MODE,
228 g_param_spec_enum ("suspend-mode", "Suspend Mode",
229 "How to suspend the media in PAUSED", GST_TYPE_RTSP_SUSPEND_MODE,
230 DEFAULT_SUSPEND_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
232 g_object_class_install_property (gobject_class, PROP_REUSABLE,
233 g_param_spec_boolean ("reusable", "Reusable",
234 "If this media pipeline can be reused after an unprepare",
235 DEFAULT_REUSABLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
237 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
238 g_param_spec_flags ("protocols", "Protocols",
239 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
240 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
242 g_object_class_install_property (gobject_class, PROP_EOS_SHUTDOWN,
243 g_param_spec_boolean ("eos-shutdown", "EOS Shutdown",
244 "Send an EOS event to the pipeline before unpreparing",
245 DEFAULT_EOS_SHUTDOWN, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
247 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
248 g_param_spec_uint ("buffer-size", "Buffer Size",
249 "The kernel UDP buffer size to use", 0, G_MAXUINT,
250 DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
252 g_object_class_install_property (gobject_class, PROP_ELEMENT,
253 g_param_spec_object ("element", "The Element",
254 "The GstBin to use for streaming the media", GST_TYPE_ELEMENT,
255 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
257 g_object_class_install_property (gobject_class, PROP_TIME_PROVIDER,
258 g_param_spec_boolean ("time-provider", "Time Provider",
259 "Use a NetTimeProvider for clients",
260 DEFAULT_TIME_PROVIDER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
262 gst_rtsp_media_signals[SIGNAL_NEW_STREAM] =
263 g_signal_new ("new-stream", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
264 G_STRUCT_OFFSET (GstRTSPMediaClass, new_stream), NULL, NULL,
265 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_RTSP_STREAM);
267 gst_rtsp_media_signals[SIGNAL_REMOVED_STREAM] =
268 g_signal_new ("removed-stream", G_TYPE_FROM_CLASS (klass),
269 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPMediaClass, removed_stream),
270 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
271 GST_TYPE_RTSP_STREAM);
273 gst_rtsp_media_signals[SIGNAL_PREPARED] =
274 g_signal_new ("prepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
275 G_STRUCT_OFFSET (GstRTSPMediaClass, prepared), NULL, NULL,
276 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
278 gst_rtsp_media_signals[SIGNAL_UNPREPARED] =
279 g_signal_new ("unprepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
280 G_STRUCT_OFFSET (GstRTSPMediaClass, unprepared), NULL, NULL,
281 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
283 gst_rtsp_media_signals[SIGNAL_NEW_STATE] =
284 g_signal_new ("new-state", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
285 G_STRUCT_OFFSET (GstRTSPMediaClass, new_state), NULL, NULL,
286 g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
288 GST_DEBUG_CATEGORY_INIT (rtsp_media_debug, "rtspmedia", 0, "GstRTSPMedia");
290 klass->handle_message = default_handle_message;
291 klass->unprepare = default_unprepare;
292 klass->convert_range = default_convert_range;
293 klass->query_position = default_query_position;
294 klass->query_stop = default_query_stop;
298 gst_rtsp_media_init (GstRTSPMedia * media)
300 GstRTSPMediaPrivate *priv = GST_RTSP_MEDIA_GET_PRIVATE (media);
304 priv->streams = g_ptr_array_new_with_free_func (g_object_unref);
305 g_mutex_init (&priv->lock);
306 g_cond_init (&priv->cond);
307 g_rec_mutex_init (&priv->state_lock);
309 priv->shared = DEFAULT_SHARED;
310 priv->suspend_mode = DEFAULT_SUSPEND_MODE;
311 priv->reusable = DEFAULT_REUSABLE;
312 priv->protocols = DEFAULT_PROTOCOLS;
313 priv->eos_shutdown = DEFAULT_EOS_SHUTDOWN;
314 priv->buffer_size = DEFAULT_BUFFER_SIZE;
315 priv->time_provider = DEFAULT_TIME_PROVIDER;
319 gst_rtsp_media_finalize (GObject * obj)
321 GstRTSPMediaPrivate *priv;
324 media = GST_RTSP_MEDIA (obj);
327 GST_INFO ("finalize media %p", media);
329 if (priv->permissions)
330 gst_rtsp_permissions_unref (priv->permissions);
332 g_ptr_array_unref (priv->streams);
334 g_list_free_full (priv->dynamic, gst_object_unref);
337 gst_object_unref (priv->pipeline);
339 gst_object_unref (priv->nettime);
340 gst_object_unref (priv->element);
342 g_object_unref (priv->pool);
343 g_mutex_clear (&priv->lock);
344 g_cond_clear (&priv->cond);
345 g_rec_mutex_clear (&priv->state_lock);
347 G_OBJECT_CLASS (gst_rtsp_media_parent_class)->finalize (obj);
351 gst_rtsp_media_get_property (GObject * object, guint propid,
352 GValue * value, GParamSpec * pspec)
354 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
358 g_value_set_object (value, media->priv->element);
361 g_value_set_boolean (value, gst_rtsp_media_is_shared (media));
363 case PROP_SUSPEND_MODE:
364 g_value_set_enum (value, gst_rtsp_media_get_suspend_mode (media));
367 g_value_set_boolean (value, gst_rtsp_media_is_reusable (media));
370 g_value_set_flags (value, gst_rtsp_media_get_protocols (media));
372 case PROP_EOS_SHUTDOWN:
373 g_value_set_boolean (value, gst_rtsp_media_is_eos_shutdown (media));
375 case PROP_BUFFER_SIZE:
376 g_value_set_uint (value, gst_rtsp_media_get_buffer_size (media));
378 case PROP_TIME_PROVIDER:
379 g_value_set_boolean (value, gst_rtsp_media_is_time_provider (media));
382 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
387 gst_rtsp_media_set_property (GObject * object, guint propid,
388 const GValue * value, GParamSpec * pspec)
390 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
394 media->priv->element = g_value_get_object (value);
395 gst_object_ref_sink (media->priv->element);
398 gst_rtsp_media_set_shared (media, g_value_get_boolean (value));
400 case PROP_SUSPEND_MODE:
401 gst_rtsp_media_set_suspend_mode (media, g_value_get_enum (value));
404 gst_rtsp_media_set_reusable (media, g_value_get_boolean (value));
407 gst_rtsp_media_set_protocols (media, g_value_get_flags (value));
409 case PROP_EOS_SHUTDOWN:
410 gst_rtsp_media_set_eos_shutdown (media, g_value_get_boolean (value));
412 case PROP_BUFFER_SIZE:
413 gst_rtsp_media_set_buffer_size (media, g_value_get_uint (value));
415 case PROP_TIME_PROVIDER:
416 gst_rtsp_media_use_time_provider (media, g_value_get_boolean (value));
419 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
424 default_query_position (GstRTSPMedia * media, gint64 * position)
426 return gst_element_query_position (media->priv->pipeline, GST_FORMAT_TIME,
431 default_query_stop (GstRTSPMedia * media, gint64 * stop)
436 query = gst_query_new_segment (GST_FORMAT_TIME);
437 if ((res = gst_element_query (media->priv->pipeline, query))) {
439 gst_query_parse_segment (query, NULL, &format, NULL, stop);
440 if (format != GST_FORMAT_TIME)
443 gst_query_unref (query);
447 /* must be called with state lock */
449 collect_media_stats (GstRTSPMedia * media)
451 GstRTSPMediaPrivate *priv = media->priv;
452 gint64 position, stop;
454 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
455 priv->status != GST_RTSP_MEDIA_STATUS_PREPARING)
458 priv->range.unit = GST_RTSP_RANGE_NPT;
460 GST_INFO ("collect media stats");
463 priv->range.min.type = GST_RTSP_TIME_NOW;
464 priv->range.min.seconds = -1;
465 priv->range_start = -1;
466 priv->range.max.type = GST_RTSP_TIME_END;
467 priv->range.max.seconds = -1;
468 priv->range_stop = -1;
470 GstRTSPMediaClass *klass;
473 klass = GST_RTSP_MEDIA_GET_CLASS (media);
475 /* get the position */
477 if (klass->query_position)
478 ret = klass->query_position (media, &position);
481 GST_INFO ("position query failed");
485 /* get the current segment stop */
487 if (klass->query_stop)
488 ret = klass->query_stop (media, &stop);
491 GST_INFO ("stop query failed");
495 GST_INFO ("stats: position %" GST_TIME_FORMAT ", stop %"
496 GST_TIME_FORMAT, GST_TIME_ARGS (position), GST_TIME_ARGS (stop));
498 if (position == -1) {
499 priv->range.min.type = GST_RTSP_TIME_NOW;
500 priv->range.min.seconds = -1;
501 priv->range_start = -1;
503 priv->range.min.type = GST_RTSP_TIME_SECONDS;
504 priv->range.min.seconds = ((gdouble) position) / GST_SECOND;
505 priv->range_start = position;
508 priv->range.max.type = GST_RTSP_TIME_END;
509 priv->range.max.seconds = -1;
510 priv->range_stop = -1;
512 priv->range.max.type = GST_RTSP_TIME_SECONDS;
513 priv->range.max.seconds = ((gdouble) stop) / GST_SECOND;
514 priv->range_stop = stop;
520 * gst_rtsp_media_new:
521 * @element: (transfer full): a #GstElement
523 * Create a new #GstRTSPMedia instance. @element is the bin element that
524 * provides the different streams. The #GstRTSPMedia object contains the
525 * element to produce RTP data for one or more related (audio/video/..)
528 * Ownership is taken of @element.
530 * Returns: a new #GstRTSPMedia object.
533 gst_rtsp_media_new (GstElement * element)
535 GstRTSPMedia *result;
537 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
539 result = g_object_new (GST_TYPE_RTSP_MEDIA, "element", element, NULL);
545 * gst_rtsp_media_get_element:
546 * @media: a #GstRTSPMedia
548 * Get the element that was used when constructing @media.
550 * Returns: (transfer full): a #GstElement. Unref after usage.
553 gst_rtsp_media_get_element (GstRTSPMedia * media)
555 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
557 return gst_object_ref (media->priv->element);
561 * gst_rtsp_media_take_pipeline:
562 * @media: a #GstRTSPMedia
563 * @pipeline: (transfer full): a #GstPipeline
565 * Set @pipeline as the #GstPipeline for @media. Ownership is
566 * taken of @pipeline.
569 gst_rtsp_media_take_pipeline (GstRTSPMedia * media, GstPipeline * pipeline)
571 GstRTSPMediaPrivate *priv;
573 GstNetTimeProvider *nettime;
575 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
576 g_return_if_fail (GST_IS_PIPELINE (pipeline));
580 g_mutex_lock (&priv->lock);
581 old = priv->pipeline;
582 priv->pipeline = GST_ELEMENT_CAST (pipeline);
583 nettime = priv->nettime;
584 priv->nettime = NULL;
585 g_mutex_unlock (&priv->lock);
588 gst_object_unref (old);
591 gst_object_unref (nettime);
593 gst_bin_add (GST_BIN_CAST (pipeline), priv->element);
597 * gst_rtsp_media_set_permissions:
598 * @media: a #GstRTSPMedia
599 * @permissions: a #GstRTSPPermissions
601 * Set @permissions on @media.
604 gst_rtsp_media_set_permissions (GstRTSPMedia * media,
605 GstRTSPPermissions * permissions)
607 GstRTSPMediaPrivate *priv;
609 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
613 g_mutex_lock (&priv->lock);
614 if (priv->permissions)
615 gst_rtsp_permissions_unref (priv->permissions);
616 if ((priv->permissions = permissions))
617 gst_rtsp_permissions_ref (permissions);
618 g_mutex_unlock (&priv->lock);
622 * gst_rtsp_media_get_permissions:
623 * @media: a #GstRTSPMedia
625 * Get the permissions object from @media.
627 * Returns: (transfer full): a #GstRTSPPermissions object, unref after usage.
630 gst_rtsp_media_get_permissions (GstRTSPMedia * media)
632 GstRTSPMediaPrivate *priv;
633 GstRTSPPermissions *result;
635 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
639 g_mutex_lock (&priv->lock);
640 if ((result = priv->permissions))
641 gst_rtsp_permissions_ref (result);
642 g_mutex_unlock (&priv->lock);
648 * gst_rtsp_media_set_suspend_mode:
649 * @media: a #GstRTSPMedia
650 * @mode: the new #GstRTSPSuspendMode
652 * Control how @ media will be suspended after the SDP has been generated and
653 * after a PAUSE request has been performed.
655 * Media must be unprepared when setting the suspend mode.
658 gst_rtsp_media_set_suspend_mode (GstRTSPMedia * media, GstRTSPSuspendMode mode)
660 GstRTSPMediaPrivate *priv;
662 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
666 g_rec_mutex_lock (&priv->state_lock);
667 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED)
669 priv->suspend_mode = mode;
670 g_rec_mutex_unlock (&priv->state_lock);
677 GST_WARNING ("media %p was prepared", media);
678 g_rec_mutex_unlock (&priv->state_lock);
683 * gst_rtsp_media_get_suspend_mode:
684 * @media: a #GstRTSPMedia
686 * Get how @media will be suspended.
688 * Returns: #GstRTSPSuspendMode.
691 gst_rtsp_media_get_suspend_mode (GstRTSPMedia * media)
693 GstRTSPMediaPrivate *priv;
694 GstRTSPSuspendMode res;
696 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_RTSP_SUSPEND_MODE_NONE);
700 g_rec_mutex_lock (&priv->state_lock);
701 res = priv->suspend_mode;
702 g_rec_mutex_unlock (&priv->state_lock);
708 * gst_rtsp_media_set_shared:
709 * @media: a #GstRTSPMedia
710 * @shared: the new value
712 * Set or unset if the pipeline for @media can be shared will multiple clients.
713 * When @shared is %TRUE, client requests for this media will share the media
717 gst_rtsp_media_set_shared (GstRTSPMedia * media, gboolean shared)
719 GstRTSPMediaPrivate *priv;
721 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
725 g_mutex_lock (&priv->lock);
726 priv->shared = shared;
727 g_mutex_unlock (&priv->lock);
731 * gst_rtsp_media_is_shared:
732 * @media: a #GstRTSPMedia
734 * Check if the pipeline for @media can be shared between multiple clients.
736 * Returns: %TRUE if the media can be shared between clients.
739 gst_rtsp_media_is_shared (GstRTSPMedia * media)
741 GstRTSPMediaPrivate *priv;
744 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
748 g_mutex_lock (&priv->lock);
750 g_mutex_unlock (&priv->lock);
756 * gst_rtsp_media_set_reusable:
757 * @media: a #GstRTSPMedia
758 * @reusable: the new value
760 * Set or unset if the pipeline for @media can be reused after the pipeline has
764 gst_rtsp_media_set_reusable (GstRTSPMedia * media, gboolean reusable)
766 GstRTSPMediaPrivate *priv;
768 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
772 g_mutex_lock (&priv->lock);
773 priv->reusable = reusable;
774 g_mutex_unlock (&priv->lock);
778 * gst_rtsp_media_is_reusable:
779 * @media: a #GstRTSPMedia
781 * Check if the pipeline for @media can be reused after an unprepare.
783 * Returns: %TRUE if the media can be reused
786 gst_rtsp_media_is_reusable (GstRTSPMedia * media)
788 GstRTSPMediaPrivate *priv;
791 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
795 g_mutex_lock (&priv->lock);
796 res = priv->reusable;
797 g_mutex_unlock (&priv->lock);
803 do_set_protocols (GstRTSPStream * stream, GstRTSPLowerTrans * protocols)
805 gst_rtsp_stream_set_protocols (stream, *protocols);
809 * gst_rtsp_media_set_protocols:
810 * @media: a #GstRTSPMedia
811 * @protocols: the new flags
813 * Configure the allowed lower transport for @media.
816 gst_rtsp_media_set_protocols (GstRTSPMedia * media, GstRTSPLowerTrans protocols)
818 GstRTSPMediaPrivate *priv;
820 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
824 g_mutex_lock (&priv->lock);
825 priv->protocols = protocols;
826 g_ptr_array_foreach (priv->streams, (GFunc) do_set_protocols, &protocols);
827 g_mutex_unlock (&priv->lock);
831 * gst_rtsp_media_get_protocols:
832 * @media: a #GstRTSPMedia
834 * Get the allowed protocols of @media.
836 * Returns: a #GstRTSPLowerTrans
839 gst_rtsp_media_get_protocols (GstRTSPMedia * media)
841 GstRTSPMediaPrivate *priv;
842 GstRTSPLowerTrans res;
844 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media),
845 GST_RTSP_LOWER_TRANS_UNKNOWN);
849 g_mutex_lock (&priv->lock);
850 res = priv->protocols;
851 g_mutex_unlock (&priv->lock);
857 * gst_rtsp_media_set_eos_shutdown:
858 * @media: a #GstRTSPMedia
859 * @eos_shutdown: the new value
861 * Set or unset if an EOS event will be sent to the pipeline for @media before
865 gst_rtsp_media_set_eos_shutdown (GstRTSPMedia * media, gboolean eos_shutdown)
867 GstRTSPMediaPrivate *priv;
869 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
873 g_mutex_lock (&priv->lock);
874 priv->eos_shutdown = eos_shutdown;
875 g_mutex_unlock (&priv->lock);
879 * gst_rtsp_media_is_eos_shutdown:
880 * @media: a #GstRTSPMedia
882 * Check if the pipeline for @media will send an EOS down the pipeline before
885 * Returns: %TRUE if the media will send EOS before unpreparing.
888 gst_rtsp_media_is_eos_shutdown (GstRTSPMedia * media)
890 GstRTSPMediaPrivate *priv;
893 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
897 g_mutex_lock (&priv->lock);
898 res = priv->eos_shutdown;
899 g_mutex_unlock (&priv->lock);
905 * gst_rtsp_media_set_buffer_size:
906 * @media: a #GstRTSPMedia
907 * @size: the new value
909 * Set the kernel UDP buffer size.
912 gst_rtsp_media_set_buffer_size (GstRTSPMedia * media, guint size)
914 GstRTSPMediaPrivate *priv;
916 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
918 GST_LOG_OBJECT (media, "set buffer size %u", size);
922 g_mutex_lock (&priv->lock);
923 priv->buffer_size = size;
924 g_mutex_unlock (&priv->lock);
928 * gst_rtsp_media_get_buffer_size:
929 * @media: a #GstRTSPMedia
931 * Get the kernel UDP buffer size.
933 * Returns: the kernel UDP buffer size.
936 gst_rtsp_media_get_buffer_size (GstRTSPMedia * media)
938 GstRTSPMediaPrivate *priv;
941 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
945 g_mutex_unlock (&priv->lock);
946 res = priv->buffer_size;
947 g_mutex_unlock (&priv->lock);
953 * gst_rtsp_media_use_time_provider:
954 * @media: a #GstRTSPMedia
955 * @time_provider: if a #GstNetTimeProvider should be used
957 * Set @media to provide a #GstNetTimeProvider.
960 gst_rtsp_media_use_time_provider (GstRTSPMedia * media, gboolean time_provider)
962 GstRTSPMediaPrivate *priv;
964 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
968 g_mutex_lock (&priv->lock);
969 priv->time_provider = time_provider;
970 g_mutex_unlock (&priv->lock);
974 * gst_rtsp_media_is_time_provider:
975 * @media: a #GstRTSPMedia
977 * Check if @media can provide a #GstNetTimeProvider for its pipeline clock.
979 * Use gst_rtsp_media_get_time_provider() to get the network clock.
981 * Returns: %TRUE if @media can provide a #GstNetTimeProvider.
984 gst_rtsp_media_is_time_provider (GstRTSPMedia * media)
986 GstRTSPMediaPrivate *priv;
989 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
993 g_mutex_unlock (&priv->lock);
994 res = priv->time_provider;
995 g_mutex_unlock (&priv->lock);
1001 * gst_rtsp_media_set_address_pool:
1002 * @media: a #GstRTSPMedia
1003 * @pool: a #GstRTSPAddressPool
1005 * configure @pool to be used as the address pool of @media.
1008 gst_rtsp_media_set_address_pool (GstRTSPMedia * media,
1009 GstRTSPAddressPool * pool)
1011 GstRTSPMediaPrivate *priv;
1012 GstRTSPAddressPool *old;
1014 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1018 GST_LOG_OBJECT (media, "set address pool %p", pool);
1020 g_mutex_lock (&priv->lock);
1021 if ((old = priv->pool) != pool)
1022 priv->pool = pool ? g_object_ref (pool) : NULL;
1025 g_ptr_array_foreach (priv->streams, (GFunc) gst_rtsp_stream_set_address_pool,
1027 g_mutex_unlock (&priv->lock);
1030 g_object_unref (old);
1034 * gst_rtsp_media_get_address_pool:
1035 * @media: a #GstRTSPMedia
1037 * Get the #GstRTSPAddressPool used as the address pool of @media.
1039 * Returns: (transfer full): the #GstRTSPAddressPool of @media. g_object_unref() after
1042 GstRTSPAddressPool *
1043 gst_rtsp_media_get_address_pool (GstRTSPMedia * media)
1045 GstRTSPMediaPrivate *priv;
1046 GstRTSPAddressPool *result;
1048 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1052 g_mutex_lock (&priv->lock);
1053 if ((result = priv->pool))
1054 g_object_ref (result);
1055 g_mutex_unlock (&priv->lock);
1061 * gst_rtsp_media_collect_streams:
1062 * @media: a #GstRTSPMedia
1064 * Find all payloader elements, they should be named pay\%d in the
1065 * element of @media, and create #GstRTSPStreams for them.
1067 * Collect all dynamic elements, named dynpay\%d, and add them to
1068 * the list of dynamic elements.
1071 gst_rtsp_media_collect_streams (GstRTSPMedia * media)
1073 GstRTSPMediaPrivate *priv;
1074 GstElement *element, *elem;
1079 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1082 element = priv->element;
1085 for (i = 0; have_elem; i++) {
1090 name = g_strdup_printf ("pay%d", i);
1091 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
1092 GST_INFO ("found stream %d with payloader %p", i, elem);
1094 /* take the pad of the payloader */
1095 pad = gst_element_get_static_pad (elem, "src");
1096 /* create the stream */
1097 gst_rtsp_media_create_stream (media, elem, pad);
1098 gst_object_unref (pad);
1099 gst_object_unref (elem);
1105 name = g_strdup_printf ("dynpay%d", i);
1106 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
1107 /* a stream that will dynamically create pads to provide RTP packets */
1109 GST_INFO ("found dynamic element %d, %p", i, elem);
1111 g_mutex_lock (&priv->lock);
1112 priv->dynamic = g_list_prepend (priv->dynamic, elem);
1113 g_mutex_unlock (&priv->lock);
1122 * gst_rtsp_media_create_stream:
1123 * @media: a #GstRTSPMedia
1124 * @payloader: a #GstElement
1125 * @srcpad: a source #GstPad
1127 * Create a new stream in @media that provides RTP data on @srcpad.
1128 * @srcpad should be a pad of an element inside @media->element.
1130 * Returns: (transfer none): a new #GstRTSPStream that remains valid for as long
1134 gst_rtsp_media_create_stream (GstRTSPMedia * media, GstElement * payloader,
1137 GstRTSPMediaPrivate *priv;
1138 GstRTSPStream *stream;
1143 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1144 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
1145 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1146 g_return_val_if_fail (GST_PAD_IS_SRC (pad), NULL);
1150 g_mutex_lock (&priv->lock);
1151 idx = priv->streams->len;
1153 GST_DEBUG ("media %p: creating stream with index %d", media, idx);
1155 name = g_strdup_printf ("src_%u", idx);
1156 srcpad = gst_ghost_pad_new (name, pad);
1157 gst_pad_set_active (srcpad, TRUE);
1158 gst_element_add_pad (priv->element, srcpad);
1161 stream = gst_rtsp_stream_new (idx, payloader, srcpad);
1163 gst_rtsp_stream_set_address_pool (stream, priv->pool);
1164 gst_rtsp_stream_set_protocols (stream, priv->protocols);
1166 g_ptr_array_add (priv->streams, stream);
1167 g_mutex_unlock (&priv->lock);
1169 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STREAM], 0, stream,
1176 gst_rtsp_media_remove_stream (GstRTSPMedia * media, GstRTSPStream * stream)
1178 GstRTSPMediaPrivate *priv;
1183 g_mutex_lock (&priv->lock);
1184 /* remove the ghostpad */
1185 srcpad = gst_rtsp_stream_get_srcpad (stream);
1186 gst_element_remove_pad (priv->element, srcpad);
1187 gst_object_unref (srcpad);
1188 /* now remove the stream */
1189 g_object_ref (stream);
1190 g_ptr_array_remove (priv->streams, stream);
1191 g_mutex_unlock (&priv->lock);
1193 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_REMOVED_STREAM], 0,
1196 g_object_unref (stream);
1200 * gst_rtsp_media_n_streams:
1201 * @media: a #GstRTSPMedia
1203 * Get the number of streams in this media.
1205 * Returns: The number of streams.
1208 gst_rtsp_media_n_streams (GstRTSPMedia * media)
1210 GstRTSPMediaPrivate *priv;
1213 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), 0);
1217 g_mutex_lock (&priv->lock);
1218 res = priv->streams->len;
1219 g_mutex_unlock (&priv->lock);
1225 * gst_rtsp_media_get_stream:
1226 * @media: a #GstRTSPMedia
1227 * @idx: the stream index
1229 * Retrieve the stream with index @idx from @media.
1231 * Returns: (transfer none): the #GstRTSPStream at index @idx or %NULL when a stream with
1232 * that index did not exist.
1235 gst_rtsp_media_get_stream (GstRTSPMedia * media, guint idx)
1237 GstRTSPMediaPrivate *priv;
1240 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1244 g_mutex_lock (&priv->lock);
1245 if (idx < priv->streams->len)
1246 res = g_ptr_array_index (priv->streams, idx);
1249 g_mutex_unlock (&priv->lock);
1255 * gst_rtsp_media_find_stream:
1256 * @media: a #GstRTSPMedia
1257 * @control: the control of the stream
1259 * Find a stream in @media with @control as the control uri.
1261 * Returns: (transfer none): the #GstRTSPStream with control uri @control
1262 * or %NULL when a stream with that control did not exist.
1265 gst_rtsp_media_find_stream (GstRTSPMedia * media, const gchar * control)
1267 GstRTSPMediaPrivate *priv;
1271 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1272 g_return_val_if_fail (control != NULL, NULL);
1278 g_mutex_lock (&priv->lock);
1279 for (i = 0; i < priv->streams->len; i++) {
1280 GstRTSPStream *test;
1282 test = g_ptr_array_index (priv->streams, i);
1283 if (gst_rtsp_stream_has_control (test, control)) {
1288 g_mutex_unlock (&priv->lock);
1293 /* called with state-lock */
1295 default_convert_range (GstRTSPMedia * media, GstRTSPTimeRange * range,
1296 GstRTSPRangeUnit unit)
1298 return gst_rtsp_range_convert_units (range, unit);
1302 * gst_rtsp_media_get_range_string:
1303 * @media: a #GstRTSPMedia
1304 * @play: for the PLAY request
1305 * @unit: the unit to use for the string
1307 * Get the current range as a string. @media must be prepared with
1308 * gst_rtsp_media_prepare ().
1310 * Returns: The range as a string, g_free() after usage.
1313 gst_rtsp_media_get_range_string (GstRTSPMedia * media, gboolean play,
1314 GstRTSPRangeUnit unit)
1316 GstRTSPMediaClass *klass;
1317 GstRTSPMediaPrivate *priv;
1319 GstRTSPTimeRange range;
1321 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1322 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1323 g_return_val_if_fail (klass->convert_range != NULL, FALSE);
1327 g_rec_mutex_lock (&priv->state_lock);
1328 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
1329 priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
1332 g_mutex_lock (&priv->lock);
1334 /* Update the range value with current position/duration */
1335 collect_media_stats (media);
1338 range = priv->range;
1340 if (!play && priv->n_active > 0) {
1341 range.min.type = GST_RTSP_TIME_NOW;
1342 range.min.seconds = -1;
1344 g_mutex_unlock (&priv->lock);
1345 g_rec_mutex_unlock (&priv->state_lock);
1347 if (!klass->convert_range (media, &range, unit))
1348 goto conversion_failed;
1350 result = gst_rtsp_range_to_string (&range);
1357 GST_WARNING ("media %p was not prepared", media);
1358 g_rec_mutex_unlock (&priv->state_lock);
1363 GST_WARNING ("range conversion to unit %d failed", unit);
1369 stream_update_blocked (GstRTSPStream * stream, GstRTSPMedia * media)
1371 gst_rtsp_stream_set_blocked (stream, media->priv->blocked);
1375 media_streams_set_blocked (GstRTSPMedia * media, gboolean blocked)
1377 GstRTSPMediaPrivate *priv = media->priv;
1379 GST_DEBUG ("media %p set blocked %d", media, blocked);
1380 priv->blocked = blocked;
1381 g_ptr_array_foreach (priv->streams, (GFunc) stream_update_blocked, media);
1385 * gst_rtsp_media_seek:
1386 * @media: a #GstRTSPMedia
1387 * @range: a #GstRTSPTimeRange
1389 * Seek the pipeline of @media to @range. @media must be prepared with
1390 * gst_rtsp_media_prepare().
1392 * Returns: %TRUE on success.
1395 gst_rtsp_media_seek (GstRTSPMedia * media, GstRTSPTimeRange * range)
1397 GstRTSPMediaClass *klass;
1398 GstRTSPMediaPrivate *priv;
1401 GstClockTime start, stop;
1402 GstSeekType start_type, stop_type;
1405 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1407 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1408 g_return_val_if_fail (range != NULL, FALSE);
1409 g_return_val_if_fail (klass->convert_range != NULL, FALSE);
1413 g_rec_mutex_lock (&priv->state_lock);
1414 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
1417 /* Update the seekable state of the pipeline in case it changed */
1418 query = gst_query_new_seeking (GST_FORMAT_TIME);
1419 if (gst_element_query (priv->pipeline, query)) {
1424 gst_query_parse_seeking (query, &format, &seekable, &start, &end);
1425 priv->seekable = seekable;
1427 gst_query_unref (query);
1429 if (!priv->seekable)
1432 /* depends on the current playing state of the pipeline. We might need to
1433 * queue this until we get EOS. */
1434 flags = GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT;
1436 start_type = stop_type = GST_SEEK_TYPE_NONE;
1438 if (!klass->convert_range (media, range, GST_RTSP_RANGE_NPT))
1440 gst_rtsp_range_get_times (range, &start, &stop);
1442 GST_INFO ("got %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1443 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1444 GST_INFO ("current %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1445 GST_TIME_ARGS (priv->range_start), GST_TIME_ARGS (priv->range_stop));
1447 if (priv->range_start == start)
1448 start = GST_CLOCK_TIME_NONE;
1449 else if (start != GST_CLOCK_TIME_NONE)
1450 start_type = GST_SEEK_TYPE_SET;
1452 if (priv->range_stop == stop)
1453 stop = GST_CLOCK_TIME_NONE;
1454 else if (stop != GST_CLOCK_TIME_NONE)
1455 stop_type = GST_SEEK_TYPE_SET;
1457 if (start != GST_CLOCK_TIME_NONE || stop != GST_CLOCK_TIME_NONE) {
1458 GST_INFO ("seeking to %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1459 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1461 priv->status = GST_RTSP_MEDIA_STATUS_PREPARING;
1463 media_streams_set_blocked (media, TRUE);
1465 res = gst_element_seek (priv->pipeline, 1.0, GST_FORMAT_TIME,
1466 flags, start_type, start, stop_type, stop);
1468 /* and block for the seek to complete */
1469 GST_INFO ("done seeking %d", res);
1470 g_rec_mutex_unlock (&priv->state_lock);
1472 /* wait until pipeline is prerolled again, this will also collect stats */
1473 if (!wait_preroll (media))
1474 goto preroll_failed;
1476 g_rec_mutex_lock (&priv->state_lock);
1477 GST_INFO ("prerolled again");
1479 GST_INFO ("no seek needed");
1482 g_rec_mutex_unlock (&priv->state_lock);
1489 g_rec_mutex_unlock (&priv->state_lock);
1490 GST_INFO ("media %p is not prepared", media);
1495 g_rec_mutex_unlock (&priv->state_lock);
1496 GST_INFO ("pipeline is not seekable");
1501 g_rec_mutex_unlock (&priv->state_lock);
1502 GST_WARNING ("conversion to npt not supported");
1507 GST_WARNING ("failed to preroll after seek");
1513 gst_rtsp_media_set_status (GstRTSPMedia * media, GstRTSPMediaStatus status)
1515 GstRTSPMediaPrivate *priv = media->priv;
1517 g_mutex_lock (&priv->lock);
1518 priv->status = status;
1519 GST_DEBUG ("setting new status to %d", status);
1520 g_cond_broadcast (&priv->cond);
1521 g_mutex_unlock (&priv->lock);
1525 * gst_rtsp_media_get_status:
1526 * @media: a #GstRTSPMedia
1528 * Get the status of @media. When @media is busy preparing, this function waits
1529 * until @media is prepared or in error.
1531 * Returns: the status of @media.
1534 gst_rtsp_media_get_status (GstRTSPMedia * media)
1536 GstRTSPMediaPrivate *priv = media->priv;
1537 GstRTSPMediaStatus result;
1540 g_mutex_lock (&priv->lock);
1541 end_time = g_get_monotonic_time () + 20 * G_TIME_SPAN_SECOND;
1542 /* while we are preparing, wait */
1543 while (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING) {
1544 GST_DEBUG ("waiting for status change");
1545 if (!g_cond_wait_until (&priv->cond, &priv->lock, end_time)) {
1546 GST_DEBUG ("timeout, assuming error status");
1547 priv->status = GST_RTSP_MEDIA_STATUS_ERROR;
1550 /* could be success or error */
1551 result = priv->status;
1552 GST_DEBUG ("got status %d", result);
1553 g_mutex_unlock (&priv->lock);
1559 stream_collect_blocking (GstRTSPStream * stream, gboolean * blocked)
1561 *blocked &= gst_rtsp_stream_is_blocking (stream);
1565 media_streams_blocking (GstRTSPMedia * media)
1567 gboolean blocking = TRUE;
1569 g_ptr_array_foreach (media->priv->streams, (GFunc) stream_collect_blocking,
1575 /* called with state-lock */
1577 default_handle_message (GstRTSPMedia * media, GstMessage * message)
1579 GstRTSPMediaPrivate *priv = media->priv;
1580 GstMessageType type;
1582 type = GST_MESSAGE_TYPE (message);
1585 case GST_MESSAGE_STATE_CHANGED:
1587 case GST_MESSAGE_BUFFERING:
1591 gst_message_parse_buffering (message, &percent);
1593 /* no state management needed for live pipelines */
1597 if (percent == 100) {
1598 /* a 100% message means buffering is done */
1599 priv->buffering = FALSE;
1600 /* if the desired state is playing, go back */
1601 if (priv->target_state == GST_STATE_PLAYING) {
1602 GST_INFO ("Buffering done, setting pipeline to PLAYING");
1603 gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
1605 GST_INFO ("Buffering done");
1608 /* buffering busy */
1609 if (priv->buffering == FALSE) {
1610 if (priv->target_state == GST_STATE_PLAYING) {
1611 /* we were not buffering but PLAYING, PAUSE the pipeline. */
1612 GST_INFO ("Buffering, setting pipeline to PAUSED ...");
1613 gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
1615 GST_INFO ("Buffering ...");
1618 priv->buffering = TRUE;
1622 case GST_MESSAGE_LATENCY:
1624 gst_bin_recalculate_latency (GST_BIN_CAST (priv->pipeline));
1627 case GST_MESSAGE_ERROR:
1632 gst_message_parse_error (message, &gerror, &debug);
1633 GST_WARNING ("%p: got error %s (%s)", media, gerror->message, debug);
1634 g_error_free (gerror);
1637 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
1640 case GST_MESSAGE_WARNING:
1645 gst_message_parse_warning (message, &gerror, &debug);
1646 GST_WARNING ("%p: got warning %s (%s)", media, gerror->message, debug);
1647 g_error_free (gerror);
1651 case GST_MESSAGE_ELEMENT:
1653 const GstStructure *s;
1655 s = gst_message_get_structure (message);
1656 if (gst_structure_has_name (s, "GstRTSPStreamBlocking")) {
1657 GST_DEBUG ("media received blocking message");
1658 if (priv->blocked && media_streams_blocking (media)) {
1659 GST_DEBUG ("media is blocking");
1660 collect_media_stats (media);
1662 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
1663 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
1668 case GST_MESSAGE_STREAM_STATUS:
1670 case GST_MESSAGE_ASYNC_DONE:
1672 /* when we are dynamically adding pads, the addition of the udpsrc will
1673 * temporarily produce ASYNC_DONE messages. We have to ignore them and
1674 * wait for the final ASYNC_DONE after everything prerolled */
1675 GST_INFO ("%p: ignoring ASYNC_DONE", media);
1677 GST_INFO ("%p: got ASYNC_DONE", media);
1678 collect_media_stats (media);
1680 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
1681 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
1684 case GST_MESSAGE_EOS:
1685 GST_INFO ("%p: got EOS", media);
1687 if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARING) {
1688 GST_DEBUG ("shutting down after EOS");
1689 finish_unprepare (media);
1693 GST_INFO ("%p: got message type %d (%s)", media, type,
1694 gst_message_type_get_name (type));
1701 bus_message (GstBus * bus, GstMessage * message, GstRTSPMedia * media)
1703 GstRTSPMediaPrivate *priv = media->priv;
1704 GstRTSPMediaClass *klass;
1707 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1709 g_rec_mutex_lock (&priv->state_lock);
1710 if (klass->handle_message)
1711 ret = klass->handle_message (media, message);
1714 g_rec_mutex_unlock (&priv->state_lock);
1720 watch_destroyed (GstRTSPMedia * media)
1722 GST_DEBUG_OBJECT (media, "source destroyed");
1723 g_object_unref (media);
1727 find_payload_element (GstElement * payloader)
1729 GstElement *pay = NULL;
1731 if (GST_IS_BIN (payloader)) {
1733 GValue item = { 0 };
1735 iter = gst_bin_iterate_recurse (GST_BIN (payloader));
1736 while (gst_iterator_next (iter, &item) == GST_ITERATOR_OK) {
1737 GstElement *element = (GstElement *) g_value_get_object (&item);
1738 GstElementClass *eclass = GST_ELEMENT_GET_CLASS (element);
1742 gst_element_class_get_metadata (eclass, GST_ELEMENT_METADATA_KLASS);
1746 if (strstr (klass, "Payloader") && strstr (klass, "RTP")) {
1747 pay = gst_object_ref (element);
1748 g_value_unset (&item);
1751 g_value_unset (&item);
1753 gst_iterator_free (iter);
1755 pay = g_object_ref (payloader);
1761 /* called from streaming threads */
1763 pad_added_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
1765 GstRTSPMediaPrivate *priv = media->priv;
1766 GstRTSPStream *stream;
1769 /* find the real payload element */
1770 pay = find_payload_element (element);
1771 stream = gst_rtsp_media_create_stream (media, pay, pad);
1772 gst_object_unref (pay);
1774 g_object_set_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream", stream);
1776 GST_INFO ("pad added %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
1778 g_rec_mutex_lock (&priv->state_lock);
1779 /* we will be adding elements below that will cause ASYNC_DONE to be
1780 * posted in the bus. We want to ignore those messages until the
1781 * pipeline really prerolled. */
1782 priv->adding = TRUE;
1784 /* join the element in the PAUSED state because this callback is
1785 * called from the streaming thread and it is PAUSED */
1786 gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
1787 priv->rtpbin, GST_STATE_PAUSED);
1789 priv->adding = FALSE;
1790 g_rec_mutex_unlock (&priv->state_lock);
1794 pad_removed_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
1796 GstRTSPMediaPrivate *priv = media->priv;
1797 GstRTSPStream *stream;
1799 stream = g_object_get_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream");
1803 GST_INFO ("pad removed %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
1805 g_rec_mutex_lock (&priv->state_lock);
1806 gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
1807 g_rec_mutex_unlock (&priv->state_lock);
1809 gst_rtsp_media_remove_stream (media, stream);
1813 remove_fakesink (GstRTSPMediaPrivate * priv)
1815 GstElement *fakesink;
1817 g_mutex_lock (&priv->lock);
1818 if ((fakesink = priv->fakesink))
1819 gst_object_ref (fakesink);
1820 priv->fakesink = NULL;
1821 g_mutex_unlock (&priv->lock);
1824 gst_bin_remove (GST_BIN (priv->pipeline), fakesink);
1825 gst_element_set_state (fakesink, GST_STATE_NULL);
1826 gst_object_unref (fakesink);
1827 GST_INFO ("removed fakesink");
1832 no_more_pads_cb (GstElement * element, GstRTSPMedia * media)
1834 GstRTSPMediaPrivate *priv = media->priv;
1836 GST_INFO ("no more pads");
1837 remove_fakesink (priv);
1840 typedef struct _DynPaySignalHandlers DynPaySignalHandlers;
1842 struct _DynPaySignalHandlers
1844 gulong pad_added_handler;
1845 gulong pad_removed_handler;
1846 gulong no_more_pads_handler;
1850 start_preroll (GstRTSPMedia * media)
1852 GstRTSPMediaPrivate *priv = media->priv;
1853 GstStateChangeReturn ret;
1855 GST_INFO ("setting pipeline to PAUSED for media %p", media);
1856 /* first go to PAUSED */
1857 ret = gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
1858 priv->target_state = GST_STATE_PAUSED;
1861 case GST_STATE_CHANGE_SUCCESS:
1862 GST_INFO ("SUCCESS state change for media %p", media);
1863 priv->seekable = TRUE;
1865 case GST_STATE_CHANGE_ASYNC:
1866 GST_INFO ("ASYNC state change for media %p", media);
1867 priv->seekable = TRUE;
1869 case GST_STATE_CHANGE_NO_PREROLL:
1870 /* we need to go to PLAYING */
1871 GST_INFO ("NO_PREROLL state change: live media %p", media);
1872 /* FIXME we disable seeking for live streams for now. We should perform a
1873 * seeking query in preroll instead */
1874 priv->seekable = FALSE;
1875 priv->is_live = TRUE;
1876 /* start blocked to make sure nothing goes to the sink */
1877 media_streams_set_blocked (media, TRUE);
1878 ret = gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
1879 if (ret == GST_STATE_CHANGE_FAILURE)
1882 case GST_STATE_CHANGE_FAILURE:
1890 GST_WARNING ("failed to preroll pipeline");
1896 wait_preroll (GstRTSPMedia * media)
1898 GstRTSPMediaStatus status;
1900 GST_DEBUG ("wait to preroll pipeline");
1902 /* wait until pipeline is prerolled */
1903 status = gst_rtsp_media_get_status (media);
1904 if (status == GST_RTSP_MEDIA_STATUS_ERROR)
1905 goto preroll_failed;
1911 GST_WARNING ("failed to preroll pipeline");
1917 start_prepare (GstRTSPMedia * media)
1919 GstRTSPMediaPrivate *priv = media->priv;
1923 /* link streams we already have, other streams might appear when we have
1924 * dynamic elements */
1925 for (i = 0; i < priv->streams->len; i++) {
1926 GstRTSPStream *stream;
1928 stream = g_ptr_array_index (priv->streams, i);
1930 gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
1931 priv->rtpbin, GST_STATE_NULL);
1934 for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
1935 GstElement *elem = walk->data;
1936 DynPaySignalHandlers *handlers = g_slice_new (DynPaySignalHandlers);
1938 GST_INFO ("adding callbacks for dynamic element %p", elem);
1940 handlers->pad_added_handler = g_signal_connect (elem, "pad-added",
1941 (GCallback) pad_added_cb, media);
1942 handlers->pad_removed_handler = g_signal_connect (elem, "pad-removed",
1943 (GCallback) pad_removed_cb, media);
1944 handlers->no_more_pads_handler = g_signal_connect (elem, "no-more-pads",
1945 (GCallback) no_more_pads_cb, media);
1947 g_object_set_data (G_OBJECT (elem), "gst-rtsp-dynpay-handlers", handlers);
1949 /* we add a fakesink here in order to make the state change async. We remove
1950 * the fakesink again in the no-more-pads callback. */
1951 priv->fakesink = gst_element_factory_make ("fakesink", "fakesink");
1952 gst_bin_add (GST_BIN (priv->pipeline), priv->fakesink);
1955 if (!start_preroll (media))
1956 goto preroll_failed;
1962 GST_WARNING ("failed to preroll pipeline");
1963 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
1969 * gst_rtsp_media_prepare:
1970 * @media: a #GstRTSPMedia
1971 * @thread: a #GstRTSPThread to run the bus handler or %NULL
1973 * Prepare @media for streaming. This function will create the objects
1974 * to manage the streaming. A pipeline must have been set on @media with
1975 * gst_rtsp_media_take_pipeline().
1977 * It will preroll the pipeline and collect vital information about the streams
1978 * such as the duration.
1980 * Returns: %TRUE on success.
1983 gst_rtsp_media_prepare (GstRTSPMedia * media, GstRTSPThread * thread)
1985 GstRTSPMediaPrivate *priv;
1989 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1990 g_return_val_if_fail (GST_IS_RTSP_THREAD (thread), FALSE);
1994 g_rec_mutex_lock (&priv->state_lock);
1995 priv->prepare_count++;
1997 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED ||
1998 priv->status == GST_RTSP_MEDIA_STATUS_SUSPENDED)
2001 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
2004 if (priv->status != GST_RTSP_MEDIA_STATUS_UNPREPARED)
2005 goto not_unprepared;
2007 if (!priv->reusable && priv->reused)
2010 priv->rtpbin = gst_element_factory_make ("rtpbin", NULL);
2011 if (priv->rtpbin != NULL) {
2012 GstRTSPMediaClass *klass;
2013 gboolean success = TRUE;
2015 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2016 if (klass->setup_rtpbin)
2017 success = klass->setup_rtpbin (media, priv->rtpbin);
2019 if (success == FALSE) {
2020 gst_object_unref (priv->rtpbin);
2021 priv->rtpbin = NULL;
2024 if (priv->rtpbin == NULL)
2027 GST_INFO ("preparing media %p", media);
2029 /* reset some variables */
2030 priv->is_live = FALSE;
2031 priv->seekable = FALSE;
2032 priv->buffering = FALSE;
2033 priv->thread = thread;
2034 /* we're preparing now */
2035 priv->status = GST_RTSP_MEDIA_STATUS_PREPARING;
2037 bus = gst_pipeline_get_bus (GST_PIPELINE_CAST (priv->pipeline));
2039 /* add the pipeline bus to our custom mainloop */
2040 priv->source = gst_bus_create_watch (bus);
2041 gst_object_unref (bus);
2043 g_source_set_callback (priv->source, (GSourceFunc) bus_message,
2044 g_object_ref (media), (GDestroyNotify) watch_destroyed);
2046 priv->id = g_source_attach (priv->source, thread->context);
2048 /* add stuff to the bin */
2049 gst_bin_add (GST_BIN (priv->pipeline), priv->rtpbin);
2051 /* do remainder in context */
2052 source = g_idle_source_new ();
2053 g_source_set_callback (source, (GSourceFunc) start_prepare, media, NULL);
2054 g_source_attach (source, thread->context);
2055 g_source_unref (source);
2058 g_rec_mutex_unlock (&priv->state_lock);
2060 /* now wait for all pads to be prerolled, FIXME, we should somehow be
2061 * able to do this async so that we don't block the server thread. */
2062 if (!wait_preroll (media))
2063 goto preroll_failed;
2065 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_PREPARED], 0, NULL);
2067 GST_INFO ("object %p is prerolled", media);
2074 GST_LOG ("media %p was prepared", media);
2075 g_rec_mutex_unlock (&priv->state_lock);
2081 GST_WARNING ("media %p was not unprepared", media);
2082 priv->prepare_count--;
2083 g_rec_mutex_unlock (&priv->state_lock);
2088 priv->prepare_count--;
2089 g_rec_mutex_unlock (&priv->state_lock);
2090 GST_WARNING ("can not reuse media %p", media);
2095 priv->prepare_count--;
2096 g_rec_mutex_unlock (&priv->state_lock);
2097 GST_WARNING ("no rtpbin element");
2098 g_warning ("failed to create element 'rtpbin', check your installation");
2103 GST_WARNING ("failed to preroll pipeline");
2104 gst_rtsp_media_unprepare (media);
2109 /* must be called with state-lock */
2111 finish_unprepare (GstRTSPMedia * media)
2113 GstRTSPMediaPrivate *priv = media->priv;
2117 GST_DEBUG ("shutting down");
2119 gst_element_set_state (priv->pipeline, GST_STATE_NULL);
2120 remove_fakesink (priv);
2122 for (i = 0; i < priv->streams->len; i++) {
2123 GstRTSPStream *stream;
2125 GST_INFO ("Removing elements of stream %d from pipeline", i);
2127 stream = g_ptr_array_index (priv->streams, i);
2129 gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
2132 /* remove the pad signal handlers */
2133 for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
2134 GstElement *elem = walk->data;
2135 DynPaySignalHandlers *handlers;
2138 g_object_steal_data (G_OBJECT (elem), "gst-rtsp-dynpay-handlers");
2139 g_assert (handlers != NULL);
2141 g_signal_handler_disconnect (G_OBJECT (elem), handlers->pad_added_handler);
2142 g_signal_handler_disconnect (G_OBJECT (elem),
2143 handlers->pad_removed_handler);
2144 g_signal_handler_disconnect (G_OBJECT (elem),
2145 handlers->no_more_pads_handler);
2147 g_slice_free (DynPaySignalHandlers, handlers);
2150 gst_bin_remove (GST_BIN (priv->pipeline), priv->rtpbin);
2151 priv->rtpbin = NULL;
2154 gst_object_unref (priv->nettime);
2155 priv->nettime = NULL;
2157 priv->reused = TRUE;
2158 priv->status = GST_RTSP_MEDIA_STATUS_UNPREPARED;
2160 /* when the media is not reusable, this will effectively unref the media and
2162 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_UNPREPARED], 0, NULL);
2164 /* the source has the last ref to the media */
2166 GST_DEBUG ("destroy source");
2167 g_source_destroy (priv->source);
2168 g_source_unref (priv->source);
2171 GST_DEBUG ("stop thread");
2172 gst_rtsp_thread_stop (priv->thread);
2176 /* called with state-lock */
2178 default_unprepare (GstRTSPMedia * media)
2180 GstRTSPMediaPrivate *priv = media->priv;
2182 if (priv->eos_shutdown) {
2183 GST_DEBUG ("sending EOS for shutdown");
2184 /* ref so that we don't disappear */
2185 gst_element_send_event (priv->pipeline, gst_event_new_eos ());
2186 /* we need to go to playing again for the EOS to propagate, normally in this
2187 * state, nothing is receiving data from us anymore so this is ok. */
2188 gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
2189 priv->status = GST_RTSP_MEDIA_STATUS_UNPREPARING;
2191 finish_unprepare (media);
2197 * gst_rtsp_media_unprepare:
2198 * @media: a #GstRTSPMedia
2200 * Unprepare @media. After this call, the media should be prepared again before
2201 * it can be used again. If the media is set to be non-reusable, a new instance
2204 * Returns: %TRUE on success.
2207 gst_rtsp_media_unprepare (GstRTSPMedia * media)
2209 GstRTSPMediaPrivate *priv;
2212 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2216 g_rec_mutex_lock (&priv->state_lock);
2217 if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARED)
2218 goto was_unprepared;
2220 priv->prepare_count--;
2221 if (priv->prepare_count > 0)
2224 GST_INFO ("unprepare media %p", media);
2225 priv->target_state = GST_STATE_NULL;
2228 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED) {
2229 GstRTSPMediaClass *klass;
2231 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2232 if (klass->unprepare)
2233 success = klass->unprepare (media);
2235 finish_unprepare (media);
2237 g_rec_mutex_unlock (&priv->state_lock);
2243 g_rec_mutex_unlock (&priv->state_lock);
2244 GST_INFO ("media %p was already unprepared", media);
2249 GST_INFO ("media %p still prepared %d times", media, priv->prepare_count);
2250 g_rec_mutex_unlock (&priv->state_lock);
2255 /* should be called with state-lock */
2257 get_clock_unlocked (GstRTSPMedia * media)
2259 if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED) {
2260 GST_DEBUG_OBJECT (media, "media was not prepared");
2263 return gst_pipeline_get_clock (GST_PIPELINE_CAST (media->priv->pipeline));
2267 * gst_rtsp_media_get_clock:
2268 * @media: a #GstRTSPMedia
2270 * Get the clock that is used by the pipeline in @media.
2272 * @media must be prepared before this method returns a valid clock object.
2274 * Returns: (transfer full): the #GstClock used by @media. unref after usage.
2277 gst_rtsp_media_get_clock (GstRTSPMedia * media)
2280 GstRTSPMediaPrivate *priv;
2282 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
2286 g_rec_mutex_lock (&priv->state_lock);
2287 clock = get_clock_unlocked (media);
2288 g_rec_mutex_unlock (&priv->state_lock);
2294 * gst_rtsp_media_get_base_time:
2295 * @media: a #GstRTSPMedia
2297 * Get the base_time that is used by the pipeline in @media.
2299 * @media must be prepared before this method returns a valid base_time.
2301 * Returns: the base_time used by @media.
2304 gst_rtsp_media_get_base_time (GstRTSPMedia * media)
2306 GstClockTime result;
2307 GstRTSPMediaPrivate *priv;
2309 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_CLOCK_TIME_NONE);
2313 g_rec_mutex_lock (&priv->state_lock);
2314 if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
2317 result = gst_element_get_base_time (media->priv->pipeline);
2318 g_rec_mutex_unlock (&priv->state_lock);
2325 g_rec_mutex_unlock (&priv->state_lock);
2326 GST_DEBUG_OBJECT (media, "media was not prepared");
2327 return GST_CLOCK_TIME_NONE;
2332 * gst_rtsp_media_get_time_provider:
2333 * @media: a #GstRTSPMedia
2334 * @address: an address or %NULL
2335 * @port: a port or 0
2337 * Get the #GstNetTimeProvider for the clock used by @media. The time provider
2338 * will listen on @address and @port for client time requests.
2340 * Returns: (transfer full): the #GstNetTimeProvider of @media.
2342 GstNetTimeProvider *
2343 gst_rtsp_media_get_time_provider (GstRTSPMedia * media, const gchar * address,
2346 GstRTSPMediaPrivate *priv;
2347 GstNetTimeProvider *provider = NULL;
2349 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
2353 g_rec_mutex_lock (&priv->state_lock);
2354 if (priv->time_provider) {
2355 if ((provider = priv->nettime) == NULL) {
2358 if (priv->time_provider && (clock = get_clock_unlocked (media))) {
2359 provider = gst_net_time_provider_new (clock, address, port);
2360 gst_object_unref (clock);
2362 priv->nettime = provider;
2366 g_rec_mutex_unlock (&priv->state_lock);
2369 gst_object_ref (provider);
2375 * gst_rtsp_media_suspend:
2376 * @media: a #GstRTSPMedia
2378 * Suspend @media. The state of the pipeline managed by @media is set to
2379 * GST_STATE_NULL but all streams are kept. @media can be prepared again
2380 * with gst_rtsp_media_undo_reset()
2382 * @media must be prepared with gst_rtsp_media_prepare();
2384 * Returns: %TRUE on success.
2387 gst_rtsp_media_suspend (GstRTSPMedia * media)
2389 GstRTSPMediaPrivate *priv = media->priv;
2390 GstStateChangeReturn ret;
2392 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2394 GST_FIXME ("suspend for dynamic pipelines needs fixing");
2396 g_rec_mutex_lock (&priv->state_lock);
2397 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
2400 /* don't attempt to suspend when something is busy */
2401 if (priv->n_active > 0)
2404 switch (priv->suspend_mode) {
2405 case GST_RTSP_SUSPEND_MODE_NONE:
2406 GST_DEBUG ("media %p no suspend", media);
2408 case GST_RTSP_SUSPEND_MODE_PAUSE:
2409 GST_DEBUG ("media %p suspend to PAUSED", media);
2410 priv->target_state = GST_STATE_PAUSED;
2411 ret = gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
2412 if (ret == GST_STATE_CHANGE_FAILURE)
2415 case GST_RTSP_SUSPEND_MODE_RESET:
2416 GST_DEBUG ("media %p suspend to NULL", media);
2417 priv->target_state = GST_STATE_NULL;
2418 ret = gst_element_set_state (priv->pipeline, GST_STATE_NULL);
2419 if (ret == GST_STATE_CHANGE_FAILURE)
2425 /* let the streams do the state changes freely, if any */
2426 media_streams_set_blocked (media, FALSE);
2427 priv->status = GST_RTSP_MEDIA_STATUS_SUSPENDED;
2429 g_rec_mutex_unlock (&priv->state_lock);
2436 g_rec_mutex_unlock (&priv->state_lock);
2437 GST_WARNING ("media %p was not prepared", media);
2442 g_rec_mutex_unlock (&priv->state_lock);
2443 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2444 GST_WARNING ("failed changing pipeline's state for media %p", media);
2450 * gst_rtsp_media_unsuspend:
2451 * @media: a #GstRTSPMedia
2453 * Unsuspend @media if it was in a suspended state. This method does nothing
2454 * when the media was not in the suspended state.
2456 * Returns: %TRUE on success.
2459 gst_rtsp_media_unsuspend (GstRTSPMedia * media)
2461 GstRTSPMediaPrivate *priv = media->priv;
2463 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2465 g_rec_mutex_lock (&priv->state_lock);
2466 if (priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
2469 switch (priv->suspend_mode) {
2470 case GST_RTSP_SUSPEND_MODE_NONE:
2471 priv->status = GST_RTSP_MEDIA_STATUS_PREPARED;
2473 case GST_RTSP_SUSPEND_MODE_PAUSE:
2474 priv->status = GST_RTSP_MEDIA_STATUS_PREPARED;
2476 case GST_RTSP_SUSPEND_MODE_RESET:
2478 priv->status = GST_RTSP_MEDIA_STATUS_PREPARING;
2479 if (!start_preroll (media))
2481 g_rec_mutex_unlock (&priv->state_lock);
2483 if (!wait_preroll (media))
2484 goto preroll_failed;
2486 g_rec_mutex_lock (&priv->state_lock);
2492 g_rec_mutex_unlock (&priv->state_lock);
2499 g_rec_mutex_unlock (&priv->state_lock);
2500 GST_WARNING ("failed to preroll pipeline");
2501 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2506 GST_WARNING ("failed to preroll pipeline");
2511 /* must be called with state-lock */
2513 media_set_pipeline_state_locked (GstRTSPMedia * media, GstState state)
2515 GstRTSPMediaPrivate *priv = media->priv;
2517 if (state == GST_STATE_NULL) {
2518 gst_rtsp_media_unprepare (media);
2520 GST_INFO ("state %s media %p", gst_element_state_get_name (state), media);
2521 priv->target_state = state;
2522 /* when we are buffering, don't update the state yet, this will be done
2523 * when buffering finishes */
2524 if (priv->buffering) {
2525 GST_INFO ("Buffering busy, delay state change");
2527 if (state == GST_STATE_PLAYING)
2528 /* make sure pads are not blocking anymore when going to PLAYING */
2529 media_streams_set_blocked (media, FALSE);
2531 gst_element_set_state (priv->pipeline, state);
2533 /* and suspend after pause */
2534 if (state == GST_STATE_PAUSED)
2535 gst_rtsp_media_suspend (media);
2541 * gst_rtsp_media_set_pipeline_state:
2542 * @media: a #GstRTSPMedia
2543 * @state: the target state of the pipeline
2545 * Set the state of the pipeline managed by @media to @state
2548 gst_rtsp_media_set_pipeline_state (GstRTSPMedia * media, GstState state)
2550 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
2552 g_rec_mutex_lock (&media->priv->state_lock);
2553 media_set_pipeline_state_locked (media, state);
2554 g_rec_mutex_unlock (&media->priv->state_lock);
2558 * gst_rtsp_media_set_state:
2559 * @media: a #GstRTSPMedia
2560 * @state: the target state of the media
2561 * @transports: (element-type GstRtspServer.RTSPStreamTransport): a #GPtrArray
2562 * of #GstRTSPStreamTransport pointers
2564 * Set the state of @media to @state and for the transports in @transports.
2566 * @media must be prepared with gst_rtsp_media_prepare();
2568 * Returns: %TRUE on success.
2571 gst_rtsp_media_set_state (GstRTSPMedia * media, GstState state,
2572 GPtrArray * transports)
2574 GstRTSPMediaPrivate *priv;
2576 gboolean activate, deactivate, do_state;
2579 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2580 g_return_val_if_fail (transports != NULL, FALSE);
2584 g_rec_mutex_lock (&priv->state_lock);
2585 if (priv->status == GST_RTSP_MEDIA_STATUS_ERROR)
2587 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
2590 /* NULL and READY are the same */
2591 if (state == GST_STATE_READY)
2592 state = GST_STATE_NULL;
2594 activate = deactivate = FALSE;
2596 GST_INFO ("going to state %s media %p", gst_element_state_get_name (state),
2600 case GST_STATE_NULL:
2601 case GST_STATE_PAUSED:
2602 /* we're going from PLAYING to PAUSED, READY or NULL, deactivate */
2603 if (priv->target_state == GST_STATE_PLAYING)
2606 case GST_STATE_PLAYING:
2607 /* we're going to PLAYING, activate */
2613 old_active = priv->n_active;
2615 for (i = 0; i < transports->len; i++) {
2616 GstRTSPStreamTransport *trans;
2618 /* we need a non-NULL entry in the array */
2619 trans = g_ptr_array_index (transports, i);
2624 if (gst_rtsp_stream_transport_set_active (trans, TRUE))
2626 } else if (deactivate) {
2627 if (gst_rtsp_stream_transport_set_active (trans, FALSE))
2632 /* we just activated the first media, do the playing state change */
2633 if (old_active == 0 && activate)
2635 /* if we have no more active media, do the downward state changes */
2636 else if (priv->n_active == 0)
2641 GST_INFO ("state %d active %d media %p do_state %d", state, priv->n_active,
2644 if (priv->target_state != state) {
2646 media_set_pipeline_state_locked (media, state);
2648 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STATE], 0, state,
2652 /* remember where we are */
2653 if (state != GST_STATE_NULL && (state == GST_STATE_PAUSED ||
2654 old_active != priv->n_active))
2655 collect_media_stats (media);
2657 g_rec_mutex_unlock (&priv->state_lock);
2664 GST_WARNING ("media %p was not prepared", media);
2665 g_rec_mutex_unlock (&priv->state_lock);
2670 GST_WARNING ("media %p in error status while changing to state %d",
2672 if (state == GST_STATE_NULL) {
2673 for (i = 0; i < transports->len; i++) {
2674 GstRTSPStreamTransport *trans;
2676 /* we need a non-NULL entry in the array */
2677 trans = g_ptr_array_index (transports, i);
2681 gst_rtsp_stream_transport_set_active (trans, FALSE);
2685 g_rec_mutex_unlock (&priv->state_lock);