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;
126 #define DEFAULT_SHARED FALSE
127 #define DEFAULT_SUSPEND_MODE GST_RTSP_SUSPEND_MODE_NONE
128 #define DEFAULT_REUSABLE FALSE
129 #define DEFAULT_PROFILES GST_RTSP_PROFILE_AVP
130 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | \
131 GST_RTSP_LOWER_TRANS_TCP
132 #define DEFAULT_EOS_SHUTDOWN FALSE
133 #define DEFAULT_BUFFER_SIZE 0x80000
134 #define DEFAULT_TIME_PROVIDER FALSE
136 /* define to dump received RTCP packets */
157 SIGNAL_REMOVED_STREAM,
165 GST_DEBUG_CATEGORY_STATIC (rtsp_media_debug);
166 #define GST_CAT_DEFAULT rtsp_media_debug
168 static void gst_rtsp_media_get_property (GObject * object, guint propid,
169 GValue * value, GParamSpec * pspec);
170 static void gst_rtsp_media_set_property (GObject * object, guint propid,
171 const GValue * value, GParamSpec * pspec);
172 static void gst_rtsp_media_finalize (GObject * obj);
174 static gboolean default_handle_message (GstRTSPMedia * media,
175 GstMessage * message);
176 static void finish_unprepare (GstRTSPMedia * media);
177 static gboolean default_unprepare (GstRTSPMedia * media);
178 static gboolean default_convert_range (GstRTSPMedia * media,
179 GstRTSPTimeRange * range, GstRTSPRangeUnit unit);
180 static gboolean default_query_position (GstRTSPMedia * media,
182 static gboolean default_query_stop (GstRTSPMedia * media, gint64 * stop);
183 static GstElement *default_create_rtpbin (GstRTSPMedia * media);
184 static gboolean default_setup_sdp (GstRTSPMedia * media, GstSDPMessage * sdp,
187 static gboolean wait_preroll (GstRTSPMedia * media);
189 static guint gst_rtsp_media_signals[SIGNAL_LAST] = { 0 };
191 #define C_ENUM(v) ((gint) v)
193 #define GST_TYPE_RTSP_SUSPEND_MODE (gst_rtsp_suspend_mode_get_type())
195 gst_rtsp_suspend_mode_get_type (void)
198 static const GEnumValue values[] = {
199 {C_ENUM (GST_RTSP_SUSPEND_MODE_NONE), "GST_RTSP_SUSPEND_MODE_NONE", "none"},
200 {C_ENUM (GST_RTSP_SUSPEND_MODE_PAUSE), "GST_RTSP_SUSPEND_MODE_PAUSE",
202 {C_ENUM (GST_RTSP_SUSPEND_MODE_RESET), "GST_RTSP_SUSPEND_MODE_RESET",
207 if (g_once_init_enter (&id)) {
208 GType tmp = g_enum_register_static ("GstRTSPSuspendMode", values);
209 g_once_init_leave (&id, tmp);
214 G_DEFINE_TYPE (GstRTSPMedia, gst_rtsp_media, G_TYPE_OBJECT);
217 gst_rtsp_media_class_init (GstRTSPMediaClass * klass)
219 GObjectClass *gobject_class;
221 g_type_class_add_private (klass, sizeof (GstRTSPMediaPrivate));
223 gobject_class = G_OBJECT_CLASS (klass);
225 gobject_class->get_property = gst_rtsp_media_get_property;
226 gobject_class->set_property = gst_rtsp_media_set_property;
227 gobject_class->finalize = gst_rtsp_media_finalize;
229 g_object_class_install_property (gobject_class, PROP_SHARED,
230 g_param_spec_boolean ("shared", "Shared",
231 "If this media pipeline can be shared", DEFAULT_SHARED,
232 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
234 g_object_class_install_property (gobject_class, PROP_SUSPEND_MODE,
235 g_param_spec_enum ("suspend-mode", "Suspend Mode",
236 "How to suspend the media in PAUSED", GST_TYPE_RTSP_SUSPEND_MODE,
237 DEFAULT_SUSPEND_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
239 g_object_class_install_property (gobject_class, PROP_REUSABLE,
240 g_param_spec_boolean ("reusable", "Reusable",
241 "If this media pipeline can be reused after an unprepare",
242 DEFAULT_REUSABLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
244 g_object_class_install_property (gobject_class, PROP_PROFILES,
245 g_param_spec_flags ("profiles", "Profiles",
246 "Allowed transfer profiles", GST_TYPE_RTSP_PROFILE,
247 DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
249 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
250 g_param_spec_flags ("protocols", "Protocols",
251 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
252 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
254 g_object_class_install_property (gobject_class, PROP_EOS_SHUTDOWN,
255 g_param_spec_boolean ("eos-shutdown", "EOS Shutdown",
256 "Send an EOS event to the pipeline before unpreparing",
257 DEFAULT_EOS_SHUTDOWN, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
259 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
260 g_param_spec_uint ("buffer-size", "Buffer Size",
261 "The kernel UDP buffer size to use", 0, G_MAXUINT,
262 DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
264 g_object_class_install_property (gobject_class, PROP_ELEMENT,
265 g_param_spec_object ("element", "The Element",
266 "The GstBin to use for streaming the media", GST_TYPE_ELEMENT,
267 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
269 g_object_class_install_property (gobject_class, PROP_TIME_PROVIDER,
270 g_param_spec_boolean ("time-provider", "Time Provider",
271 "Use a NetTimeProvider for clients",
272 DEFAULT_TIME_PROVIDER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
274 gst_rtsp_media_signals[SIGNAL_NEW_STREAM] =
275 g_signal_new ("new-stream", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
276 G_STRUCT_OFFSET (GstRTSPMediaClass, new_stream), NULL, NULL,
277 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_RTSP_STREAM);
279 gst_rtsp_media_signals[SIGNAL_REMOVED_STREAM] =
280 g_signal_new ("removed-stream", G_TYPE_FROM_CLASS (klass),
281 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPMediaClass, removed_stream),
282 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
283 GST_TYPE_RTSP_STREAM);
285 gst_rtsp_media_signals[SIGNAL_PREPARED] =
286 g_signal_new ("prepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
287 G_STRUCT_OFFSET (GstRTSPMediaClass, prepared), NULL, NULL,
288 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
290 gst_rtsp_media_signals[SIGNAL_UNPREPARED] =
291 g_signal_new ("unprepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
292 G_STRUCT_OFFSET (GstRTSPMediaClass, unprepared), NULL, NULL,
293 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
295 gst_rtsp_media_signals[SIGNAL_TARGET_STATE] =
296 g_signal_new ("target-state", G_TYPE_FROM_CLASS (klass),
297 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPMediaClass, new_state), NULL,
298 NULL, g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
300 gst_rtsp_media_signals[SIGNAL_NEW_STATE] =
301 g_signal_new ("new-state", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
302 G_STRUCT_OFFSET (GstRTSPMediaClass, new_state), NULL, NULL,
303 g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
305 GST_DEBUG_CATEGORY_INIT (rtsp_media_debug, "rtspmedia", 0, "GstRTSPMedia");
307 klass->handle_message = default_handle_message;
308 klass->unprepare = default_unprepare;
309 klass->convert_range = default_convert_range;
310 klass->query_position = default_query_position;
311 klass->query_stop = default_query_stop;
312 klass->create_rtpbin = default_create_rtpbin;
313 klass->setup_sdp = default_setup_sdp;
317 gst_rtsp_media_init (GstRTSPMedia * media)
319 GstRTSPMediaPrivate *priv = GST_RTSP_MEDIA_GET_PRIVATE (media);
323 priv->streams = g_ptr_array_new_with_free_func (g_object_unref);
324 g_mutex_init (&priv->lock);
325 g_cond_init (&priv->cond);
326 g_rec_mutex_init (&priv->state_lock);
328 priv->shared = DEFAULT_SHARED;
329 priv->suspend_mode = DEFAULT_SUSPEND_MODE;
330 priv->reusable = DEFAULT_REUSABLE;
331 priv->profiles = DEFAULT_PROFILES;
332 priv->protocols = DEFAULT_PROTOCOLS;
333 priv->eos_shutdown = DEFAULT_EOS_SHUTDOWN;
334 priv->buffer_size = DEFAULT_BUFFER_SIZE;
335 priv->time_provider = DEFAULT_TIME_PROVIDER;
339 gst_rtsp_media_finalize (GObject * obj)
341 GstRTSPMediaPrivate *priv;
344 media = GST_RTSP_MEDIA (obj);
347 GST_INFO ("finalize media %p", media);
349 if (priv->permissions)
350 gst_rtsp_permissions_unref (priv->permissions);
352 g_ptr_array_unref (priv->streams);
354 g_list_free_full (priv->dynamic, gst_object_unref);
357 gst_object_unref (priv->pipeline);
359 gst_object_unref (priv->nettime);
360 gst_object_unref (priv->element);
362 g_object_unref (priv->pool);
363 g_mutex_clear (&priv->lock);
364 g_cond_clear (&priv->cond);
365 g_rec_mutex_clear (&priv->state_lock);
367 G_OBJECT_CLASS (gst_rtsp_media_parent_class)->finalize (obj);
371 gst_rtsp_media_get_property (GObject * object, guint propid,
372 GValue * value, GParamSpec * pspec)
374 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
378 g_value_set_object (value, media->priv->element);
381 g_value_set_boolean (value, gst_rtsp_media_is_shared (media));
383 case PROP_SUSPEND_MODE:
384 g_value_set_enum (value, gst_rtsp_media_get_suspend_mode (media));
387 g_value_set_boolean (value, gst_rtsp_media_is_reusable (media));
390 g_value_set_flags (value, gst_rtsp_media_get_profiles (media));
393 g_value_set_flags (value, gst_rtsp_media_get_protocols (media));
395 case PROP_EOS_SHUTDOWN:
396 g_value_set_boolean (value, gst_rtsp_media_is_eos_shutdown (media));
398 case PROP_BUFFER_SIZE:
399 g_value_set_uint (value, gst_rtsp_media_get_buffer_size (media));
401 case PROP_TIME_PROVIDER:
402 g_value_set_boolean (value, gst_rtsp_media_is_time_provider (media));
405 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
410 gst_rtsp_media_set_property (GObject * object, guint propid,
411 const GValue * value, GParamSpec * pspec)
413 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
417 media->priv->element = g_value_get_object (value);
418 gst_object_ref_sink (media->priv->element);
421 gst_rtsp_media_set_shared (media, g_value_get_boolean (value));
423 case PROP_SUSPEND_MODE:
424 gst_rtsp_media_set_suspend_mode (media, g_value_get_enum (value));
427 gst_rtsp_media_set_reusable (media, g_value_get_boolean (value));
430 gst_rtsp_media_set_profiles (media, g_value_get_flags (value));
433 gst_rtsp_media_set_protocols (media, g_value_get_flags (value));
435 case PROP_EOS_SHUTDOWN:
436 gst_rtsp_media_set_eos_shutdown (media, g_value_get_boolean (value));
438 case PROP_BUFFER_SIZE:
439 gst_rtsp_media_set_buffer_size (media, g_value_get_uint (value));
441 case PROP_TIME_PROVIDER:
442 gst_rtsp_media_use_time_provider (media, g_value_get_boolean (value));
445 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
450 default_query_position (GstRTSPMedia * media, gint64 * position)
452 return gst_element_query_position (media->priv->pipeline, GST_FORMAT_TIME,
457 default_query_stop (GstRTSPMedia * media, gint64 * stop)
462 query = gst_query_new_segment (GST_FORMAT_TIME);
463 if ((res = gst_element_query (media->priv->pipeline, query))) {
465 gst_query_parse_segment (query, NULL, &format, NULL, stop);
466 if (format != GST_FORMAT_TIME)
469 gst_query_unref (query);
474 default_create_rtpbin (GstRTSPMedia * media)
478 rtpbin = gst_element_factory_make ("rtpbin", NULL);
483 /* must be called with state lock */
485 collect_media_stats (GstRTSPMedia * media)
487 GstRTSPMediaPrivate *priv = media->priv;
488 gint64 position, stop;
490 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
491 priv->status != GST_RTSP_MEDIA_STATUS_PREPARING)
494 priv->range.unit = GST_RTSP_RANGE_NPT;
496 GST_INFO ("collect media stats");
499 priv->range.min.type = GST_RTSP_TIME_NOW;
500 priv->range.min.seconds = -1;
501 priv->range_start = -1;
502 priv->range.max.type = GST_RTSP_TIME_END;
503 priv->range.max.seconds = -1;
504 priv->range_stop = -1;
506 GstRTSPMediaClass *klass;
509 klass = GST_RTSP_MEDIA_GET_CLASS (media);
511 /* get the position */
513 if (klass->query_position)
514 ret = klass->query_position (media, &position);
517 GST_INFO ("position query failed");
521 /* get the current segment stop */
523 if (klass->query_stop)
524 ret = klass->query_stop (media, &stop);
527 GST_INFO ("stop query failed");
531 GST_INFO ("stats: position %" GST_TIME_FORMAT ", stop %"
532 GST_TIME_FORMAT, GST_TIME_ARGS (position), GST_TIME_ARGS (stop));
534 if (position == -1) {
535 priv->range.min.type = GST_RTSP_TIME_NOW;
536 priv->range.min.seconds = -1;
537 priv->range_start = -1;
539 priv->range.min.type = GST_RTSP_TIME_SECONDS;
540 priv->range.min.seconds = ((gdouble) position) / GST_SECOND;
541 priv->range_start = position;
544 priv->range.max.type = GST_RTSP_TIME_END;
545 priv->range.max.seconds = -1;
546 priv->range_stop = -1;
548 priv->range.max.type = GST_RTSP_TIME_SECONDS;
549 priv->range.max.seconds = ((gdouble) stop) / GST_SECOND;
550 priv->range_stop = stop;
556 * gst_rtsp_media_new:
557 * @element: (transfer full): a #GstElement
559 * Create a new #GstRTSPMedia instance. @element is the bin element that
560 * provides the different streams. The #GstRTSPMedia object contains the
561 * element to produce RTP data for one or more related (audio/video/..)
564 * Ownership is taken of @element.
566 * Returns: a new #GstRTSPMedia object.
569 gst_rtsp_media_new (GstElement * element)
571 GstRTSPMedia *result;
573 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
575 result = g_object_new (GST_TYPE_RTSP_MEDIA, "element", element, NULL);
581 * gst_rtsp_media_get_element:
582 * @media: a #GstRTSPMedia
584 * Get the element that was used when constructing @media.
586 * Returns: (transfer full): a #GstElement. Unref after usage.
589 gst_rtsp_media_get_element (GstRTSPMedia * media)
591 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
593 return gst_object_ref (media->priv->element);
597 * gst_rtsp_media_take_pipeline:
598 * @media: a #GstRTSPMedia
599 * @pipeline: (transfer full): a #GstPipeline
601 * Set @pipeline as the #GstPipeline for @media. Ownership is
602 * taken of @pipeline.
605 gst_rtsp_media_take_pipeline (GstRTSPMedia * media, GstPipeline * pipeline)
607 GstRTSPMediaPrivate *priv;
609 GstNetTimeProvider *nettime;
611 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
612 g_return_if_fail (GST_IS_PIPELINE (pipeline));
616 g_mutex_lock (&priv->lock);
617 old = priv->pipeline;
618 priv->pipeline = GST_ELEMENT_CAST (pipeline);
619 nettime = priv->nettime;
620 priv->nettime = NULL;
621 g_mutex_unlock (&priv->lock);
624 gst_object_unref (old);
627 gst_object_unref (nettime);
629 gst_bin_add (GST_BIN_CAST (pipeline), priv->element);
633 * gst_rtsp_media_set_permissions:
634 * @media: a #GstRTSPMedia
635 * @permissions: a #GstRTSPPermissions
637 * Set @permissions on @media.
640 gst_rtsp_media_set_permissions (GstRTSPMedia * media,
641 GstRTSPPermissions * permissions)
643 GstRTSPMediaPrivate *priv;
645 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
649 g_mutex_lock (&priv->lock);
650 if (priv->permissions)
651 gst_rtsp_permissions_unref (priv->permissions);
652 if ((priv->permissions = permissions))
653 gst_rtsp_permissions_ref (permissions);
654 g_mutex_unlock (&priv->lock);
658 * gst_rtsp_media_get_permissions:
659 * @media: a #GstRTSPMedia
661 * Get the permissions object from @media.
663 * Returns: (transfer full): a #GstRTSPPermissions object, unref after usage.
666 gst_rtsp_media_get_permissions (GstRTSPMedia * media)
668 GstRTSPMediaPrivate *priv;
669 GstRTSPPermissions *result;
671 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
675 g_mutex_lock (&priv->lock);
676 if ((result = priv->permissions))
677 gst_rtsp_permissions_ref (result);
678 g_mutex_unlock (&priv->lock);
684 * gst_rtsp_media_set_suspend_mode:
685 * @media: a #GstRTSPMedia
686 * @mode: the new #GstRTSPSuspendMode
688 * Control how @ media will be suspended after the SDP has been generated and
689 * after a PAUSE request has been performed.
691 * Media must be unprepared when setting the suspend mode.
694 gst_rtsp_media_set_suspend_mode (GstRTSPMedia * media, GstRTSPSuspendMode mode)
696 GstRTSPMediaPrivate *priv;
698 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
702 g_rec_mutex_lock (&priv->state_lock);
703 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED)
705 priv->suspend_mode = mode;
706 g_rec_mutex_unlock (&priv->state_lock);
713 GST_WARNING ("media %p was prepared", media);
714 g_rec_mutex_unlock (&priv->state_lock);
719 * gst_rtsp_media_get_suspend_mode:
720 * @media: a #GstRTSPMedia
722 * Get how @media will be suspended.
724 * Returns: #GstRTSPSuspendMode.
727 gst_rtsp_media_get_suspend_mode (GstRTSPMedia * media)
729 GstRTSPMediaPrivate *priv;
730 GstRTSPSuspendMode res;
732 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_RTSP_SUSPEND_MODE_NONE);
736 g_rec_mutex_lock (&priv->state_lock);
737 res = priv->suspend_mode;
738 g_rec_mutex_unlock (&priv->state_lock);
744 * gst_rtsp_media_set_shared:
745 * @media: a #GstRTSPMedia
746 * @shared: the new value
748 * Set or unset if the pipeline for @media can be shared will multiple clients.
749 * When @shared is %TRUE, client requests for this media will share the media
753 gst_rtsp_media_set_shared (GstRTSPMedia * media, gboolean shared)
755 GstRTSPMediaPrivate *priv;
757 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
761 g_mutex_lock (&priv->lock);
762 priv->shared = shared;
763 g_mutex_unlock (&priv->lock);
767 * gst_rtsp_media_is_shared:
768 * @media: a #GstRTSPMedia
770 * Check if the pipeline for @media can be shared between multiple clients.
772 * Returns: %TRUE if the media can be shared between clients.
775 gst_rtsp_media_is_shared (GstRTSPMedia * media)
777 GstRTSPMediaPrivate *priv;
780 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
784 g_mutex_lock (&priv->lock);
786 g_mutex_unlock (&priv->lock);
792 * gst_rtsp_media_set_reusable:
793 * @media: a #GstRTSPMedia
794 * @reusable: the new value
796 * Set or unset if the pipeline for @media can be reused after the pipeline has
800 gst_rtsp_media_set_reusable (GstRTSPMedia * media, gboolean reusable)
802 GstRTSPMediaPrivate *priv;
804 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
808 g_mutex_lock (&priv->lock);
809 priv->reusable = reusable;
810 g_mutex_unlock (&priv->lock);
814 * gst_rtsp_media_is_reusable:
815 * @media: a #GstRTSPMedia
817 * Check if the pipeline for @media can be reused after an unprepare.
819 * Returns: %TRUE if the media can be reused
822 gst_rtsp_media_is_reusable (GstRTSPMedia * media)
824 GstRTSPMediaPrivate *priv;
827 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
831 g_mutex_lock (&priv->lock);
832 res = priv->reusable;
833 g_mutex_unlock (&priv->lock);
839 do_set_profiles (GstRTSPStream * stream, GstRTSPProfile * profiles)
841 gst_rtsp_stream_set_profiles (stream, *profiles);
845 * gst_rtsp_media_set_profiles:
846 * @media: a #GstRTSPMedia
847 * @profiles: the new flags
849 * Configure the allowed lower transport for @media.
852 gst_rtsp_media_set_profiles (GstRTSPMedia * media, GstRTSPProfile profiles)
854 GstRTSPMediaPrivate *priv;
856 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
860 g_mutex_lock (&priv->lock);
861 priv->profiles = profiles;
862 g_ptr_array_foreach (priv->streams, (GFunc) do_set_profiles, &profiles);
863 g_mutex_unlock (&priv->lock);
867 * gst_rtsp_media_get_profiles:
868 * @media: a #GstRTSPMedia
870 * Get the allowed profiles of @media.
872 * Returns: a #GstRTSPProfile
875 gst_rtsp_media_get_profiles (GstRTSPMedia * media)
877 GstRTSPMediaPrivate *priv;
880 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_RTSP_PROFILE_UNKNOWN);
884 g_mutex_lock (&priv->lock);
885 res = priv->profiles;
886 g_mutex_unlock (&priv->lock);
892 do_set_protocols (GstRTSPStream * stream, GstRTSPLowerTrans * protocols)
894 gst_rtsp_stream_set_protocols (stream, *protocols);
898 * gst_rtsp_media_set_protocols:
899 * @media: a #GstRTSPMedia
900 * @protocols: the new flags
902 * Configure the allowed lower transport for @media.
905 gst_rtsp_media_set_protocols (GstRTSPMedia * media, GstRTSPLowerTrans protocols)
907 GstRTSPMediaPrivate *priv;
909 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
913 g_mutex_lock (&priv->lock);
914 priv->protocols = protocols;
915 g_ptr_array_foreach (priv->streams, (GFunc) do_set_protocols, &protocols);
916 g_mutex_unlock (&priv->lock);
920 * gst_rtsp_media_get_protocols:
921 * @media: a #GstRTSPMedia
923 * Get the allowed protocols of @media.
925 * Returns: a #GstRTSPLowerTrans
928 gst_rtsp_media_get_protocols (GstRTSPMedia * media)
930 GstRTSPMediaPrivate *priv;
931 GstRTSPLowerTrans res;
933 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media),
934 GST_RTSP_LOWER_TRANS_UNKNOWN);
938 g_mutex_lock (&priv->lock);
939 res = priv->protocols;
940 g_mutex_unlock (&priv->lock);
946 * gst_rtsp_media_set_eos_shutdown:
947 * @media: a #GstRTSPMedia
948 * @eos_shutdown: the new value
950 * Set or unset if an EOS event will be sent to the pipeline for @media before
954 gst_rtsp_media_set_eos_shutdown (GstRTSPMedia * media, gboolean eos_shutdown)
956 GstRTSPMediaPrivate *priv;
958 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
962 g_mutex_lock (&priv->lock);
963 priv->eos_shutdown = eos_shutdown;
964 g_mutex_unlock (&priv->lock);
968 * gst_rtsp_media_is_eos_shutdown:
969 * @media: a #GstRTSPMedia
971 * Check if the pipeline for @media will send an EOS down the pipeline before
974 * Returns: %TRUE if the media will send EOS before unpreparing.
977 gst_rtsp_media_is_eos_shutdown (GstRTSPMedia * media)
979 GstRTSPMediaPrivate *priv;
982 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
986 g_mutex_lock (&priv->lock);
987 res = priv->eos_shutdown;
988 g_mutex_unlock (&priv->lock);
994 * gst_rtsp_media_set_buffer_size:
995 * @media: a #GstRTSPMedia
996 * @size: the new value
998 * Set the kernel UDP buffer size.
1001 gst_rtsp_media_set_buffer_size (GstRTSPMedia * media, guint size)
1003 GstRTSPMediaPrivate *priv;
1005 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1007 GST_LOG_OBJECT (media, "set buffer size %u", size);
1011 g_mutex_lock (&priv->lock);
1012 priv->buffer_size = size;
1013 g_mutex_unlock (&priv->lock);
1017 * gst_rtsp_media_get_buffer_size:
1018 * @media: a #GstRTSPMedia
1020 * Get the kernel UDP buffer size.
1022 * Returns: the kernel UDP buffer size.
1025 gst_rtsp_media_get_buffer_size (GstRTSPMedia * media)
1027 GstRTSPMediaPrivate *priv;
1030 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1034 g_mutex_unlock (&priv->lock);
1035 res = priv->buffer_size;
1036 g_mutex_unlock (&priv->lock);
1042 * gst_rtsp_media_use_time_provider:
1043 * @media: a #GstRTSPMedia
1044 * @time_provider: if a #GstNetTimeProvider should be used
1046 * Set @media to provide a #GstNetTimeProvider.
1049 gst_rtsp_media_use_time_provider (GstRTSPMedia * media, gboolean time_provider)
1051 GstRTSPMediaPrivate *priv;
1053 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1057 g_mutex_lock (&priv->lock);
1058 priv->time_provider = time_provider;
1059 g_mutex_unlock (&priv->lock);
1063 * gst_rtsp_media_is_time_provider:
1064 * @media: a #GstRTSPMedia
1066 * Check if @media can provide a #GstNetTimeProvider for its pipeline clock.
1068 * Use gst_rtsp_media_get_time_provider() to get the network clock.
1070 * Returns: %TRUE if @media can provide a #GstNetTimeProvider.
1073 gst_rtsp_media_is_time_provider (GstRTSPMedia * media)
1075 GstRTSPMediaPrivate *priv;
1078 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1082 g_mutex_unlock (&priv->lock);
1083 res = priv->time_provider;
1084 g_mutex_unlock (&priv->lock);
1090 * gst_rtsp_media_set_address_pool:
1091 * @media: a #GstRTSPMedia
1092 * @pool: a #GstRTSPAddressPool
1094 * configure @pool to be used as the address pool of @media.
1097 gst_rtsp_media_set_address_pool (GstRTSPMedia * media,
1098 GstRTSPAddressPool * pool)
1100 GstRTSPMediaPrivate *priv;
1101 GstRTSPAddressPool *old;
1103 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1107 GST_LOG_OBJECT (media, "set address pool %p", pool);
1109 g_mutex_lock (&priv->lock);
1110 if ((old = priv->pool) != pool)
1111 priv->pool = pool ? g_object_ref (pool) : NULL;
1114 g_ptr_array_foreach (priv->streams, (GFunc) gst_rtsp_stream_set_address_pool,
1116 g_mutex_unlock (&priv->lock);
1119 g_object_unref (old);
1123 * gst_rtsp_media_get_address_pool:
1124 * @media: a #GstRTSPMedia
1126 * Get the #GstRTSPAddressPool used as the address pool of @media.
1128 * Returns: (transfer full): the #GstRTSPAddressPool of @media. g_object_unref() after
1131 GstRTSPAddressPool *
1132 gst_rtsp_media_get_address_pool (GstRTSPMedia * media)
1134 GstRTSPMediaPrivate *priv;
1135 GstRTSPAddressPool *result;
1137 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1141 g_mutex_lock (&priv->lock);
1142 if ((result = priv->pool))
1143 g_object_ref (result);
1144 g_mutex_unlock (&priv->lock);
1150 * gst_rtsp_media_collect_streams:
1151 * @media: a #GstRTSPMedia
1153 * Find all payloader elements, they should be named pay\%d in the
1154 * element of @media, and create #GstRTSPStreams for them.
1156 * Collect all dynamic elements, named dynpay\%d, and add them to
1157 * the list of dynamic elements.
1160 gst_rtsp_media_collect_streams (GstRTSPMedia * media)
1162 GstRTSPMediaPrivate *priv;
1163 GstElement *element, *elem;
1168 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1171 element = priv->element;
1174 for (i = 0; have_elem; i++) {
1179 name = g_strdup_printf ("pay%d", i);
1180 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
1181 GST_INFO ("found stream %d with payloader %p", i, elem);
1183 /* take the pad of the payloader */
1184 pad = gst_element_get_static_pad (elem, "src");
1185 /* create the stream */
1186 gst_rtsp_media_create_stream (media, elem, pad);
1187 gst_object_unref (pad);
1188 gst_object_unref (elem);
1194 name = g_strdup_printf ("dynpay%d", i);
1195 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
1196 /* a stream that will dynamically create pads to provide RTP packets */
1198 GST_INFO ("found dynamic element %d, %p", i, elem);
1200 g_mutex_lock (&priv->lock);
1201 priv->dynamic = g_list_prepend (priv->dynamic, elem);
1202 g_mutex_unlock (&priv->lock);
1211 * gst_rtsp_media_create_stream:
1212 * @media: a #GstRTSPMedia
1213 * @payloader: a #GstElement
1214 * @srcpad: a source #GstPad
1216 * Create a new stream in @media that provides RTP data on @srcpad.
1217 * @srcpad should be a pad of an element inside @media->element.
1219 * Returns: (transfer none): a new #GstRTSPStream that remains valid for as long
1223 gst_rtsp_media_create_stream (GstRTSPMedia * media, GstElement * payloader,
1226 GstRTSPMediaPrivate *priv;
1227 GstRTSPStream *stream;
1232 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1233 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
1234 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1235 g_return_val_if_fail (GST_PAD_IS_SRC (pad), NULL);
1239 g_mutex_lock (&priv->lock);
1240 idx = priv->streams->len;
1242 GST_DEBUG ("media %p: creating stream with index %d", media, idx);
1244 name = g_strdup_printf ("src_%u", idx);
1245 srcpad = gst_ghost_pad_new (name, pad);
1246 gst_pad_set_active (srcpad, TRUE);
1247 gst_element_add_pad (priv->element, srcpad);
1250 stream = gst_rtsp_stream_new (idx, payloader, srcpad);
1252 gst_rtsp_stream_set_address_pool (stream, priv->pool);
1253 gst_rtsp_stream_set_protocols (stream, priv->protocols);
1255 g_ptr_array_add (priv->streams, stream);
1256 g_mutex_unlock (&priv->lock);
1258 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STREAM], 0, stream,
1265 gst_rtsp_media_remove_stream (GstRTSPMedia * media, GstRTSPStream * stream)
1267 GstRTSPMediaPrivate *priv;
1272 g_mutex_lock (&priv->lock);
1273 /* remove the ghostpad */
1274 srcpad = gst_rtsp_stream_get_srcpad (stream);
1275 gst_element_remove_pad (priv->element, srcpad);
1276 gst_object_unref (srcpad);
1277 /* now remove the stream */
1278 g_object_ref (stream);
1279 g_ptr_array_remove (priv->streams, stream);
1280 g_mutex_unlock (&priv->lock);
1282 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_REMOVED_STREAM], 0,
1285 g_object_unref (stream);
1289 * gst_rtsp_media_n_streams:
1290 * @media: a #GstRTSPMedia
1292 * Get the number of streams in this media.
1294 * Returns: The number of streams.
1297 gst_rtsp_media_n_streams (GstRTSPMedia * media)
1299 GstRTSPMediaPrivate *priv;
1302 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), 0);
1306 g_mutex_lock (&priv->lock);
1307 res = priv->streams->len;
1308 g_mutex_unlock (&priv->lock);
1314 * gst_rtsp_media_get_stream:
1315 * @media: a #GstRTSPMedia
1316 * @idx: the stream index
1318 * Retrieve the stream with index @idx from @media.
1320 * Returns: (transfer none): the #GstRTSPStream at index @idx or %NULL when a stream with
1321 * that index did not exist.
1324 gst_rtsp_media_get_stream (GstRTSPMedia * media, guint idx)
1326 GstRTSPMediaPrivate *priv;
1329 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1333 g_mutex_lock (&priv->lock);
1334 if (idx < priv->streams->len)
1335 res = g_ptr_array_index (priv->streams, idx);
1338 g_mutex_unlock (&priv->lock);
1344 * gst_rtsp_media_find_stream:
1345 * @media: a #GstRTSPMedia
1346 * @control: the control of the stream
1348 * Find a stream in @media with @control as the control uri.
1350 * Returns: (transfer none): the #GstRTSPStream with control uri @control
1351 * or %NULL when a stream with that control did not exist.
1354 gst_rtsp_media_find_stream (GstRTSPMedia * media, const gchar * control)
1356 GstRTSPMediaPrivate *priv;
1360 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1361 g_return_val_if_fail (control != NULL, NULL);
1367 g_mutex_lock (&priv->lock);
1368 for (i = 0; i < priv->streams->len; i++) {
1369 GstRTSPStream *test;
1371 test = g_ptr_array_index (priv->streams, i);
1372 if (gst_rtsp_stream_has_control (test, control)) {
1377 g_mutex_unlock (&priv->lock);
1382 /* called with state-lock */
1384 default_convert_range (GstRTSPMedia * media, GstRTSPTimeRange * range,
1385 GstRTSPRangeUnit unit)
1387 return gst_rtsp_range_convert_units (range, unit);
1391 * gst_rtsp_media_get_range_string:
1392 * @media: a #GstRTSPMedia
1393 * @play: for the PLAY request
1394 * @unit: the unit to use for the string
1396 * Get the current range as a string. @media must be prepared with
1397 * gst_rtsp_media_prepare ().
1399 * Returns: The range as a string, g_free() after usage.
1402 gst_rtsp_media_get_range_string (GstRTSPMedia * media, gboolean play,
1403 GstRTSPRangeUnit unit)
1405 GstRTSPMediaClass *klass;
1406 GstRTSPMediaPrivate *priv;
1408 GstRTSPTimeRange range;
1410 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1411 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1412 g_return_val_if_fail (klass->convert_range != NULL, FALSE);
1416 g_rec_mutex_lock (&priv->state_lock);
1417 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
1418 priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
1421 g_mutex_lock (&priv->lock);
1423 /* Update the range value with current position/duration */
1424 collect_media_stats (media);
1427 range = priv->range;
1429 if (!play && priv->n_active > 0) {
1430 range.min.type = GST_RTSP_TIME_NOW;
1431 range.min.seconds = -1;
1433 g_mutex_unlock (&priv->lock);
1434 g_rec_mutex_unlock (&priv->state_lock);
1436 if (!klass->convert_range (media, &range, unit))
1437 goto conversion_failed;
1439 result = gst_rtsp_range_to_string (&range);
1446 GST_WARNING ("media %p was not prepared", media);
1447 g_rec_mutex_unlock (&priv->state_lock);
1452 GST_WARNING ("range conversion to unit %d failed", unit);
1458 stream_update_blocked (GstRTSPStream * stream, GstRTSPMedia * media)
1460 gst_rtsp_stream_set_blocked (stream, media->priv->blocked);
1464 media_streams_set_blocked (GstRTSPMedia * media, gboolean blocked)
1466 GstRTSPMediaPrivate *priv = media->priv;
1468 GST_DEBUG ("media %p set blocked %d", media, blocked);
1469 priv->blocked = blocked;
1470 g_ptr_array_foreach (priv->streams, (GFunc) stream_update_blocked, media);
1474 * gst_rtsp_media_seek:
1475 * @media: a #GstRTSPMedia
1476 * @range: a #GstRTSPTimeRange
1478 * Seek the pipeline of @media to @range. @media must be prepared with
1479 * gst_rtsp_media_prepare().
1481 * Returns: %TRUE on success.
1484 gst_rtsp_media_seek (GstRTSPMedia * media, GstRTSPTimeRange * range)
1486 GstRTSPMediaClass *klass;
1487 GstRTSPMediaPrivate *priv;
1489 GstClockTime start, stop;
1490 GstSeekType start_type, stop_type;
1493 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1495 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1496 g_return_val_if_fail (range != NULL, FALSE);
1497 g_return_val_if_fail (klass->convert_range != NULL, FALSE);
1501 g_rec_mutex_lock (&priv->state_lock);
1502 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
1505 /* Update the seekable state of the pipeline in case it changed */
1506 query = gst_query_new_seeking (GST_FORMAT_TIME);
1507 if (gst_element_query (priv->pipeline, query)) {
1512 gst_query_parse_seeking (query, &format, &seekable, &start, &end);
1513 priv->seekable = seekable;
1515 gst_query_unref (query);
1517 if (!priv->seekable)
1520 start_type = stop_type = GST_SEEK_TYPE_NONE;
1522 if (!klass->convert_range (media, range, GST_RTSP_RANGE_NPT))
1524 gst_rtsp_range_get_times (range, &start, &stop);
1526 GST_INFO ("got %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1527 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1528 GST_INFO ("current %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1529 GST_TIME_ARGS (priv->range_start), GST_TIME_ARGS (priv->range_stop));
1531 if (priv->range_start == start)
1532 start = GST_CLOCK_TIME_NONE;
1533 else if (start != GST_CLOCK_TIME_NONE)
1534 start_type = GST_SEEK_TYPE_SET;
1536 if (priv->range_stop == stop)
1537 stop = GST_CLOCK_TIME_NONE;
1538 else if (stop != GST_CLOCK_TIME_NONE)
1539 stop_type = GST_SEEK_TYPE_SET;
1541 if (start != GST_CLOCK_TIME_NONE || stop != GST_CLOCK_TIME_NONE) {
1544 GST_INFO ("seeking to %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1545 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1547 priv->status = GST_RTSP_MEDIA_STATUS_PREPARING;
1549 media_streams_set_blocked (media, TRUE);
1551 /* depends on the current playing state of the pipeline. We might need to
1552 * queue this until we get EOS. */
1553 flags = GST_SEEK_FLAG_FLUSH;
1555 /* only set keyframe flag when modifying start */
1556 if (start_type != GST_SEEK_TYPE_NONE)
1557 flags |= GST_SEEK_FLAG_KEY_UNIT;
1559 /* FIXME, we only do forwards */
1560 res = gst_element_seek (priv->pipeline, 1.0, GST_FORMAT_TIME,
1561 flags, start_type, start, stop_type, stop);
1563 /* and block for the seek to complete */
1564 GST_INFO ("done seeking %d", res);
1565 g_rec_mutex_unlock (&priv->state_lock);
1567 /* wait until pipeline is prerolled again, this will also collect stats */
1568 if (!wait_preroll (media))
1569 goto preroll_failed;
1571 g_rec_mutex_lock (&priv->state_lock);
1572 GST_INFO ("prerolled again");
1574 GST_INFO ("no seek needed");
1577 g_rec_mutex_unlock (&priv->state_lock);
1584 g_rec_mutex_unlock (&priv->state_lock);
1585 GST_INFO ("media %p is not prepared", media);
1590 g_rec_mutex_unlock (&priv->state_lock);
1591 GST_INFO ("pipeline is not seekable");
1596 g_rec_mutex_unlock (&priv->state_lock);
1597 GST_WARNING ("conversion to npt not supported");
1602 GST_WARNING ("failed to preroll after seek");
1608 gst_rtsp_media_set_status (GstRTSPMedia * media, GstRTSPMediaStatus status)
1610 GstRTSPMediaPrivate *priv = media->priv;
1612 g_mutex_lock (&priv->lock);
1613 priv->status = status;
1614 GST_DEBUG ("setting new status to %d", status);
1615 g_cond_broadcast (&priv->cond);
1616 g_mutex_unlock (&priv->lock);
1620 * gst_rtsp_media_get_status:
1621 * @media: a #GstRTSPMedia
1623 * Get the status of @media. When @media is busy preparing, this function waits
1624 * until @media is prepared or in error.
1626 * Returns: the status of @media.
1629 gst_rtsp_media_get_status (GstRTSPMedia * media)
1631 GstRTSPMediaPrivate *priv = media->priv;
1632 GstRTSPMediaStatus result;
1635 g_mutex_lock (&priv->lock);
1636 end_time = g_get_monotonic_time () + 20 * G_TIME_SPAN_SECOND;
1637 /* while we are preparing, wait */
1638 while (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING) {
1639 GST_DEBUG ("waiting for status change");
1640 if (!g_cond_wait_until (&priv->cond, &priv->lock, end_time)) {
1641 GST_DEBUG ("timeout, assuming error status");
1642 priv->status = GST_RTSP_MEDIA_STATUS_ERROR;
1645 /* could be success or error */
1646 result = priv->status;
1647 GST_DEBUG ("got status %d", result);
1648 g_mutex_unlock (&priv->lock);
1654 stream_collect_blocking (GstRTSPStream * stream, gboolean * blocked)
1656 *blocked &= gst_rtsp_stream_is_blocking (stream);
1660 media_streams_blocking (GstRTSPMedia * media)
1662 gboolean blocking = TRUE;
1664 g_ptr_array_foreach (media->priv->streams, (GFunc) stream_collect_blocking,
1670 static GstStateChangeReturn
1671 set_state (GstRTSPMedia * media, GstState state)
1673 GstRTSPMediaPrivate *priv = media->priv;
1674 GstStateChangeReturn ret;
1676 GST_INFO ("set state to %s for media %p", gst_element_state_get_name (state),
1678 ret = gst_element_set_state (priv->pipeline, state);
1683 static GstStateChangeReturn
1684 set_target_state (GstRTSPMedia * media, GstState state, gboolean do_state)
1686 GstRTSPMediaPrivate *priv = media->priv;
1687 GstStateChangeReturn ret;
1689 GST_INFO ("set target state to %s for media %p",
1690 gst_element_state_get_name (state), media);
1691 priv->target_state = state;
1693 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_TARGET_STATE], 0,
1694 priv->target_state, NULL);
1697 ret = set_state (media, state);
1699 ret = GST_STATE_CHANGE_SUCCESS;
1704 /* called with state-lock */
1706 default_handle_message (GstRTSPMedia * media, GstMessage * message)
1708 GstRTSPMediaPrivate *priv = media->priv;
1709 GstMessageType type;
1711 type = GST_MESSAGE_TYPE (message);
1714 case GST_MESSAGE_STATE_CHANGED:
1716 case GST_MESSAGE_BUFFERING:
1720 gst_message_parse_buffering (message, &percent);
1722 /* no state management needed for live pipelines */
1726 if (percent == 100) {
1727 /* a 100% message means buffering is done */
1728 priv->buffering = FALSE;
1729 /* if the desired state is playing, go back */
1730 if (priv->target_state == GST_STATE_PLAYING) {
1731 GST_INFO ("Buffering done, setting pipeline to PLAYING");
1732 set_state (media, GST_STATE_PLAYING);
1734 GST_INFO ("Buffering done");
1737 /* buffering busy */
1738 if (priv->buffering == FALSE) {
1739 if (priv->target_state == GST_STATE_PLAYING) {
1740 /* we were not buffering but PLAYING, PAUSE the pipeline. */
1741 GST_INFO ("Buffering, setting pipeline to PAUSED ...");
1742 set_state (media, GST_STATE_PAUSED);
1744 GST_INFO ("Buffering ...");
1747 priv->buffering = TRUE;
1751 case GST_MESSAGE_LATENCY:
1753 gst_bin_recalculate_latency (GST_BIN_CAST (priv->pipeline));
1756 case GST_MESSAGE_ERROR:
1761 gst_message_parse_error (message, &gerror, &debug);
1762 GST_WARNING ("%p: got error %s (%s)", media, gerror->message, debug);
1763 g_error_free (gerror);
1766 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
1769 case GST_MESSAGE_WARNING:
1774 gst_message_parse_warning (message, &gerror, &debug);
1775 GST_WARNING ("%p: got warning %s (%s)", media, gerror->message, debug);
1776 g_error_free (gerror);
1780 case GST_MESSAGE_ELEMENT:
1782 const GstStructure *s;
1784 s = gst_message_get_structure (message);
1785 if (gst_structure_has_name (s, "GstRTSPStreamBlocking")) {
1786 GST_DEBUG ("media received blocking message");
1787 if (priv->blocked && media_streams_blocking (media)) {
1788 GST_DEBUG ("media is blocking");
1789 collect_media_stats (media);
1791 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
1792 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
1797 case GST_MESSAGE_STREAM_STATUS:
1799 case GST_MESSAGE_ASYNC_DONE:
1801 /* when we are dynamically adding pads, the addition of the udpsrc will
1802 * temporarily produce ASYNC_DONE messages. We have to ignore them and
1803 * wait for the final ASYNC_DONE after everything prerolled */
1804 GST_INFO ("%p: ignoring ASYNC_DONE", media);
1806 GST_INFO ("%p: got ASYNC_DONE", media);
1807 collect_media_stats (media);
1809 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
1810 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
1813 case GST_MESSAGE_EOS:
1814 GST_INFO ("%p: got EOS", media);
1816 if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARING) {
1817 GST_DEBUG ("shutting down after EOS");
1818 finish_unprepare (media);
1822 GST_INFO ("%p: got message type %d (%s)", media, type,
1823 gst_message_type_get_name (type));
1830 bus_message (GstBus * bus, GstMessage * message, GstRTSPMedia * media)
1832 GstRTSPMediaPrivate *priv = media->priv;
1833 GstRTSPMediaClass *klass;
1836 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1838 g_rec_mutex_lock (&priv->state_lock);
1839 if (klass->handle_message)
1840 ret = klass->handle_message (media, message);
1843 g_rec_mutex_unlock (&priv->state_lock);
1849 watch_destroyed (GstRTSPMedia * media)
1851 GST_DEBUG_OBJECT (media, "source destroyed");
1852 g_object_unref (media);
1856 find_payload_element (GstElement * payloader)
1858 GstElement *pay = NULL;
1860 if (GST_IS_BIN (payloader)) {
1862 GValue item = { 0 };
1864 iter = gst_bin_iterate_recurse (GST_BIN (payloader));
1865 while (gst_iterator_next (iter, &item) == GST_ITERATOR_OK) {
1866 GstElement *element = (GstElement *) g_value_get_object (&item);
1867 GstElementClass *eclass = GST_ELEMENT_GET_CLASS (element);
1871 gst_element_class_get_metadata (eclass, GST_ELEMENT_METADATA_KLASS);
1875 if (strstr (klass, "Payloader") && strstr (klass, "RTP")) {
1876 pay = gst_object_ref (element);
1877 g_value_unset (&item);
1880 g_value_unset (&item);
1882 gst_iterator_free (iter);
1884 pay = g_object_ref (payloader);
1890 /* called from streaming threads */
1892 pad_added_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
1894 GstRTSPMediaPrivate *priv = media->priv;
1895 GstRTSPStream *stream;
1898 /* find the real payload element */
1899 pay = find_payload_element (element);
1900 stream = gst_rtsp_media_create_stream (media, pay, pad);
1901 gst_object_unref (pay);
1903 g_object_set_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream", stream);
1905 GST_INFO ("pad added %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
1907 g_rec_mutex_lock (&priv->state_lock);
1908 /* we will be adding elements below that will cause ASYNC_DONE to be
1909 * posted in the bus. We want to ignore those messages until the
1910 * pipeline really prerolled. */
1911 priv->adding = TRUE;
1913 /* join the element in the PAUSED state because this callback is
1914 * called from the streaming thread and it is PAUSED */
1915 gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
1916 priv->rtpbin, GST_STATE_PAUSED);
1918 priv->adding = FALSE;
1919 g_rec_mutex_unlock (&priv->state_lock);
1923 pad_removed_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
1925 GstRTSPMediaPrivate *priv = media->priv;
1926 GstRTSPStream *stream;
1928 stream = g_object_get_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream");
1932 GST_INFO ("pad removed %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
1934 g_rec_mutex_lock (&priv->state_lock);
1935 gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
1936 g_rec_mutex_unlock (&priv->state_lock);
1938 gst_rtsp_media_remove_stream (media, stream);
1942 remove_fakesink (GstRTSPMediaPrivate * priv)
1944 GstElement *fakesink;
1946 g_mutex_lock (&priv->lock);
1947 if ((fakesink = priv->fakesink))
1948 gst_object_ref (fakesink);
1949 priv->fakesink = NULL;
1950 g_mutex_unlock (&priv->lock);
1953 gst_bin_remove (GST_BIN (priv->pipeline), fakesink);
1954 gst_element_set_state (fakesink, GST_STATE_NULL);
1955 gst_object_unref (fakesink);
1956 GST_INFO ("removed fakesink");
1961 no_more_pads_cb (GstElement * element, GstRTSPMedia * media)
1963 GstRTSPMediaPrivate *priv = media->priv;
1965 GST_INFO ("no more pads");
1966 remove_fakesink (priv);
1969 typedef struct _DynPaySignalHandlers DynPaySignalHandlers;
1971 struct _DynPaySignalHandlers
1973 gulong pad_added_handler;
1974 gulong pad_removed_handler;
1975 gulong no_more_pads_handler;
1979 start_preroll (GstRTSPMedia * media)
1981 GstRTSPMediaPrivate *priv = media->priv;
1982 GstStateChangeReturn ret;
1984 GST_INFO ("setting pipeline to PAUSED for media %p", media);
1985 /* first go to PAUSED */
1986 ret = set_target_state (media, GST_STATE_PAUSED, TRUE);
1989 case GST_STATE_CHANGE_SUCCESS:
1990 GST_INFO ("SUCCESS state change for media %p", media);
1991 priv->seekable = TRUE;
1993 case GST_STATE_CHANGE_ASYNC:
1994 GST_INFO ("ASYNC state change for media %p", media);
1995 priv->seekable = TRUE;
1997 case GST_STATE_CHANGE_NO_PREROLL:
1998 /* we need to go to PLAYING */
1999 GST_INFO ("NO_PREROLL state change: live media %p", media);
2000 /* FIXME we disable seeking for live streams for now. We should perform a
2001 * seeking query in preroll instead */
2002 priv->seekable = FALSE;
2003 priv->is_live = TRUE;
2004 /* start blocked to make sure nothing goes to the sink */
2005 media_streams_set_blocked (media, TRUE);
2006 ret = set_state (media, GST_STATE_PLAYING);
2007 if (ret == GST_STATE_CHANGE_FAILURE)
2010 case GST_STATE_CHANGE_FAILURE:
2018 GST_WARNING ("failed to preroll pipeline");
2024 wait_preroll (GstRTSPMedia * media)
2026 GstRTSPMediaStatus status;
2028 GST_DEBUG ("wait to preroll pipeline");
2030 /* wait until pipeline is prerolled */
2031 status = gst_rtsp_media_get_status (media);
2032 if (status == GST_RTSP_MEDIA_STATUS_ERROR)
2033 goto preroll_failed;
2039 GST_WARNING ("failed to preroll pipeline");
2045 start_prepare (GstRTSPMedia * media)
2047 GstRTSPMediaPrivate *priv = media->priv;
2051 /* link streams we already have, other streams might appear when we have
2052 * dynamic elements */
2053 for (i = 0; i < priv->streams->len; i++) {
2054 GstRTSPStream *stream;
2056 stream = g_ptr_array_index (priv->streams, i);
2058 gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
2059 priv->rtpbin, GST_STATE_NULL);
2062 for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
2063 GstElement *elem = walk->data;
2064 DynPaySignalHandlers *handlers = g_slice_new (DynPaySignalHandlers);
2066 GST_INFO ("adding callbacks for dynamic element %p", elem);
2068 handlers->pad_added_handler = g_signal_connect (elem, "pad-added",
2069 (GCallback) pad_added_cb, media);
2070 handlers->pad_removed_handler = g_signal_connect (elem, "pad-removed",
2071 (GCallback) pad_removed_cb, media);
2072 handlers->no_more_pads_handler = g_signal_connect (elem, "no-more-pads",
2073 (GCallback) no_more_pads_cb, media);
2075 g_object_set_data (G_OBJECT (elem), "gst-rtsp-dynpay-handlers", handlers);
2077 /* we add a fakesink here in order to make the state change async. We remove
2078 * the fakesink again in the no-more-pads callback. */
2079 priv->fakesink = gst_element_factory_make ("fakesink", "fakesink");
2080 gst_bin_add (GST_BIN (priv->pipeline), priv->fakesink);
2083 if (!start_preroll (media))
2084 goto preroll_failed;
2090 GST_WARNING ("failed to preroll pipeline");
2091 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2097 * gst_rtsp_media_prepare:
2098 * @media: a #GstRTSPMedia
2099 * @thread: a #GstRTSPThread to run the bus handler or %NULL
2101 * Prepare @media for streaming. This function will create the objects
2102 * to manage the streaming. A pipeline must have been set on @media with
2103 * gst_rtsp_media_take_pipeline().
2105 * It will preroll the pipeline and collect vital information about the streams
2106 * such as the duration.
2108 * Returns: %TRUE on success.
2111 gst_rtsp_media_prepare (GstRTSPMedia * media, GstRTSPThread * thread)
2113 GstRTSPMediaPrivate *priv;
2116 GstRTSPMediaClass *klass;
2118 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2119 g_return_val_if_fail (GST_IS_RTSP_THREAD (thread), FALSE);
2123 g_rec_mutex_lock (&priv->state_lock);
2124 priv->prepare_count++;
2126 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED ||
2127 priv->status == GST_RTSP_MEDIA_STATUS_SUSPENDED)
2130 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
2133 if (priv->status != GST_RTSP_MEDIA_STATUS_UNPREPARED)
2134 goto not_unprepared;
2136 if (!priv->reusable && priv->reused)
2139 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2141 if (!klass->create_rtpbin)
2142 goto no_create_rtpbin;
2144 priv->rtpbin = klass->create_rtpbin (media);
2145 if (priv->rtpbin != NULL) {
2146 gboolean success = TRUE;
2148 if (klass->setup_rtpbin)
2149 success = klass->setup_rtpbin (media, priv->rtpbin);
2151 if (success == FALSE) {
2152 gst_object_unref (priv->rtpbin);
2153 priv->rtpbin = NULL;
2156 if (priv->rtpbin == NULL)
2159 GST_INFO ("preparing media %p", media);
2161 /* reset some variables */
2162 priv->is_live = FALSE;
2163 priv->seekable = FALSE;
2164 priv->buffering = FALSE;
2165 priv->thread = thread;
2166 /* we're preparing now */
2167 priv->status = GST_RTSP_MEDIA_STATUS_PREPARING;
2169 bus = gst_pipeline_get_bus (GST_PIPELINE_CAST (priv->pipeline));
2171 /* add the pipeline bus to our custom mainloop */
2172 priv->source = gst_bus_create_watch (bus);
2173 gst_object_unref (bus);
2175 g_source_set_callback (priv->source, (GSourceFunc) bus_message,
2176 g_object_ref (media), (GDestroyNotify) watch_destroyed);
2178 priv->id = g_source_attach (priv->source, thread->context);
2180 /* add stuff to the bin */
2181 gst_bin_add (GST_BIN (priv->pipeline), priv->rtpbin);
2183 /* do remainder in context */
2184 source = g_idle_source_new ();
2185 g_source_set_callback (source, (GSourceFunc) start_prepare, media, NULL);
2186 g_source_attach (source, thread->context);
2187 g_source_unref (source);
2190 g_rec_mutex_unlock (&priv->state_lock);
2192 /* now wait for all pads to be prerolled, FIXME, we should somehow be
2193 * able to do this async so that we don't block the server thread. */
2194 if (!wait_preroll (media))
2195 goto preroll_failed;
2197 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_PREPARED], 0, NULL);
2199 GST_INFO ("object %p is prerolled", media);
2206 GST_LOG ("media %p was prepared", media);
2207 /* we are not going to use the giving thread, so stop it. */
2208 gst_rtsp_thread_stop (thread);
2209 g_rec_mutex_unlock (&priv->state_lock);
2215 GST_WARNING ("media %p was not unprepared", media);
2216 priv->prepare_count--;
2217 g_rec_mutex_unlock (&priv->state_lock);
2222 priv->prepare_count--;
2223 g_rec_mutex_unlock (&priv->state_lock);
2224 GST_WARNING ("can not reuse media %p", media);
2229 priv->prepare_count--;
2230 g_rec_mutex_unlock (&priv->state_lock);
2231 GST_ERROR ("no create_rtpbin function");
2232 g_critical ("no create_rtpbin vmethod function set");
2237 priv->prepare_count--;
2238 g_rec_mutex_unlock (&priv->state_lock);
2239 GST_WARNING ("no rtpbin element");
2240 g_warning ("failed to create element 'rtpbin', check your installation");
2245 GST_WARNING ("failed to preroll pipeline");
2246 gst_rtsp_media_unprepare (media);
2251 /* must be called with state-lock */
2253 finish_unprepare (GstRTSPMedia * media)
2255 GstRTSPMediaPrivate *priv = media->priv;
2259 GST_DEBUG ("shutting down");
2261 set_state (media, GST_STATE_NULL);
2262 remove_fakesink (priv);
2264 for (i = 0; i < priv->streams->len; i++) {
2265 GstRTSPStream *stream;
2267 GST_INFO ("Removing elements of stream %d from pipeline", i);
2269 stream = g_ptr_array_index (priv->streams, i);
2271 gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
2274 /* remove the pad signal handlers */
2275 for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
2276 GstElement *elem = walk->data;
2277 DynPaySignalHandlers *handlers;
2280 g_object_steal_data (G_OBJECT (elem), "gst-rtsp-dynpay-handlers");
2281 g_assert (handlers != NULL);
2283 g_signal_handler_disconnect (G_OBJECT (elem), handlers->pad_added_handler);
2284 g_signal_handler_disconnect (G_OBJECT (elem),
2285 handlers->pad_removed_handler);
2286 g_signal_handler_disconnect (G_OBJECT (elem),
2287 handlers->no_more_pads_handler);
2289 g_slice_free (DynPaySignalHandlers, handlers);
2292 gst_bin_remove (GST_BIN (priv->pipeline), priv->rtpbin);
2293 priv->rtpbin = NULL;
2296 gst_object_unref (priv->nettime);
2297 priv->nettime = NULL;
2299 priv->reused = TRUE;
2300 priv->status = GST_RTSP_MEDIA_STATUS_UNPREPARED;
2302 /* when the media is not reusable, this will effectively unref the media and
2304 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_UNPREPARED], 0, NULL);
2306 /* the source has the last ref to the media */
2308 GST_DEBUG ("destroy source");
2309 g_source_destroy (priv->source);
2310 g_source_unref (priv->source);
2313 GST_DEBUG ("stop thread");
2314 gst_rtsp_thread_stop (priv->thread);
2318 /* called with state-lock */
2320 default_unprepare (GstRTSPMedia * media)
2322 GstRTSPMediaPrivate *priv = media->priv;
2324 if (priv->eos_shutdown) {
2325 GST_DEBUG ("sending EOS for shutdown");
2326 /* ref so that we don't disappear */
2327 gst_element_send_event (priv->pipeline, gst_event_new_eos ());
2328 /* we need to go to playing again for the EOS to propagate, normally in this
2329 * state, nothing is receiving data from us anymore so this is ok. */
2330 set_state (media, GST_STATE_PLAYING);
2331 priv->status = GST_RTSP_MEDIA_STATUS_UNPREPARING;
2333 finish_unprepare (media);
2339 * gst_rtsp_media_unprepare:
2340 * @media: a #GstRTSPMedia
2342 * Unprepare @media. After this call, the media should be prepared again before
2343 * it can be used again. If the media is set to be non-reusable, a new instance
2346 * Returns: %TRUE on success.
2349 gst_rtsp_media_unprepare (GstRTSPMedia * media)
2351 GstRTSPMediaPrivate *priv;
2354 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2358 g_rec_mutex_lock (&priv->state_lock);
2359 if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARED)
2360 goto was_unprepared;
2362 priv->prepare_count--;
2363 if (priv->prepare_count > 0)
2366 GST_INFO ("unprepare media %p", media);
2367 set_target_state (media, GST_STATE_NULL, FALSE);
2370 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED) {
2371 GstRTSPMediaClass *klass;
2373 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2374 if (klass->unprepare)
2375 success = klass->unprepare (media);
2377 finish_unprepare (media);
2379 g_rec_mutex_unlock (&priv->state_lock);
2385 g_rec_mutex_unlock (&priv->state_lock);
2386 GST_INFO ("media %p was already unprepared", media);
2391 GST_INFO ("media %p still prepared %d times", media, priv->prepare_count);
2392 g_rec_mutex_unlock (&priv->state_lock);
2397 /* should be called with state-lock */
2399 get_clock_unlocked (GstRTSPMedia * media)
2401 if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED) {
2402 GST_DEBUG_OBJECT (media, "media was not prepared");
2405 return gst_pipeline_get_clock (GST_PIPELINE_CAST (media->priv->pipeline));
2409 * gst_rtsp_media_get_clock:
2410 * @media: a #GstRTSPMedia
2412 * Get the clock that is used by the pipeline in @media.
2414 * @media must be prepared before this method returns a valid clock object.
2416 * Returns: (transfer full): the #GstClock used by @media. unref after usage.
2419 gst_rtsp_media_get_clock (GstRTSPMedia * media)
2422 GstRTSPMediaPrivate *priv;
2424 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
2428 g_rec_mutex_lock (&priv->state_lock);
2429 clock = get_clock_unlocked (media);
2430 g_rec_mutex_unlock (&priv->state_lock);
2436 * gst_rtsp_media_get_base_time:
2437 * @media: a #GstRTSPMedia
2439 * Get the base_time that is used by the pipeline in @media.
2441 * @media must be prepared before this method returns a valid base_time.
2443 * Returns: the base_time used by @media.
2446 gst_rtsp_media_get_base_time (GstRTSPMedia * media)
2448 GstClockTime result;
2449 GstRTSPMediaPrivate *priv;
2451 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_CLOCK_TIME_NONE);
2455 g_rec_mutex_lock (&priv->state_lock);
2456 if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
2459 result = gst_element_get_base_time (media->priv->pipeline);
2460 g_rec_mutex_unlock (&priv->state_lock);
2467 g_rec_mutex_unlock (&priv->state_lock);
2468 GST_DEBUG_OBJECT (media, "media was not prepared");
2469 return GST_CLOCK_TIME_NONE;
2474 * gst_rtsp_media_get_time_provider:
2475 * @media: a #GstRTSPMedia
2476 * @address: an address or %NULL
2477 * @port: a port or 0
2479 * Get the #GstNetTimeProvider for the clock used by @media. The time provider
2480 * will listen on @address and @port for client time requests.
2482 * Returns: (transfer full): the #GstNetTimeProvider of @media.
2484 GstNetTimeProvider *
2485 gst_rtsp_media_get_time_provider (GstRTSPMedia * media, const gchar * address,
2488 GstRTSPMediaPrivate *priv;
2489 GstNetTimeProvider *provider = NULL;
2491 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
2495 g_rec_mutex_lock (&priv->state_lock);
2496 if (priv->time_provider) {
2497 if ((provider = priv->nettime) == NULL) {
2500 if (priv->time_provider && (clock = get_clock_unlocked (media))) {
2501 provider = gst_net_time_provider_new (clock, address, port);
2502 gst_object_unref (clock);
2504 priv->nettime = provider;
2508 g_rec_mutex_unlock (&priv->state_lock);
2511 gst_object_ref (provider);
2517 default_setup_sdp (GstRTSPMedia * media, GstSDPMessage * sdp, GstSDPInfo * info)
2519 return gst_rtsp_sdp_from_media (sdp, info, media);
2523 * gst_rtsp_media_setup_sdp:
2524 * @sdp: a #GstSDPMessage
2526 * @media: a #GstRTSPMedia
2528 * Add @media specific info to @sdp. @info is used to configure the connection
2529 * information in the SDP.
2531 * Returns: TRUE on success.
2534 gst_rtsp_media_setup_sdp (GstRTSPMedia * media, GstSDPMessage * sdp,
2537 GstRTSPMediaPrivate *priv;
2538 GstRTSPMediaClass *klass;
2541 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2542 g_return_val_if_fail (sdp != NULL, FALSE);
2543 g_return_val_if_fail (info != NULL, FALSE);
2547 g_rec_mutex_lock (&priv->state_lock);
2549 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2551 if (!klass->setup_sdp)
2554 res = klass->setup_sdp (media, sdp, info);
2556 g_rec_mutex_unlock (&priv->state_lock);
2563 g_rec_mutex_unlock (&priv->state_lock);
2564 GST_ERROR ("no setup_sdp function");
2565 g_critical ("no setup_sdp vmethod function set");
2571 * gst_rtsp_media_suspend:
2572 * @media: a #GstRTSPMedia
2574 * Suspend @media. The state of the pipeline managed by @media is set to
2575 * GST_STATE_NULL but all streams are kept. @media can be prepared again
2576 * with gst_rtsp_media_undo_reset()
2578 * @media must be prepared with gst_rtsp_media_prepare();
2580 * Returns: %TRUE on success.
2583 gst_rtsp_media_suspend (GstRTSPMedia * media)
2585 GstRTSPMediaPrivate *priv = media->priv;
2586 GstStateChangeReturn ret;
2588 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2590 GST_FIXME ("suspend for dynamic pipelines needs fixing");
2592 g_rec_mutex_lock (&priv->state_lock);
2593 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
2596 /* don't attempt to suspend when something is busy */
2597 if (priv->n_active > 0)
2600 switch (priv->suspend_mode) {
2601 case GST_RTSP_SUSPEND_MODE_NONE:
2602 GST_DEBUG ("media %p no suspend", media);
2604 case GST_RTSP_SUSPEND_MODE_PAUSE:
2605 GST_DEBUG ("media %p suspend to PAUSED", media);
2606 ret = set_target_state (media, GST_STATE_PAUSED, TRUE);
2607 if (ret == GST_STATE_CHANGE_FAILURE)
2610 case GST_RTSP_SUSPEND_MODE_RESET:
2611 GST_DEBUG ("media %p suspend to NULL", media);
2612 ret = set_target_state (media, GST_STATE_NULL, TRUE);
2613 if (ret == GST_STATE_CHANGE_FAILURE)
2619 /* let the streams do the state changes freely, if any */
2620 media_streams_set_blocked (media, FALSE);
2621 priv->status = GST_RTSP_MEDIA_STATUS_SUSPENDED;
2623 g_rec_mutex_unlock (&priv->state_lock);
2630 g_rec_mutex_unlock (&priv->state_lock);
2631 GST_WARNING ("media %p was not prepared", media);
2636 g_rec_mutex_unlock (&priv->state_lock);
2637 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2638 GST_WARNING ("failed changing pipeline's state for media %p", media);
2644 * gst_rtsp_media_unsuspend:
2645 * @media: a #GstRTSPMedia
2647 * Unsuspend @media if it was in a suspended state. This method does nothing
2648 * when the media was not in the suspended state.
2650 * Returns: %TRUE on success.
2653 gst_rtsp_media_unsuspend (GstRTSPMedia * media)
2655 GstRTSPMediaPrivate *priv = media->priv;
2657 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2659 g_rec_mutex_lock (&priv->state_lock);
2660 if (priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
2663 switch (priv->suspend_mode) {
2664 case GST_RTSP_SUSPEND_MODE_NONE:
2665 priv->status = GST_RTSP_MEDIA_STATUS_PREPARED;
2667 case GST_RTSP_SUSPEND_MODE_PAUSE:
2668 priv->status = GST_RTSP_MEDIA_STATUS_PREPARED;
2670 case GST_RTSP_SUSPEND_MODE_RESET:
2672 priv->status = GST_RTSP_MEDIA_STATUS_PREPARING;
2673 if (!start_preroll (media))
2675 g_rec_mutex_unlock (&priv->state_lock);
2677 if (!wait_preroll (media))
2678 goto preroll_failed;
2680 g_rec_mutex_lock (&priv->state_lock);
2686 g_rec_mutex_unlock (&priv->state_lock);
2693 g_rec_mutex_unlock (&priv->state_lock);
2694 GST_WARNING ("failed to preroll pipeline");
2695 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2700 GST_WARNING ("failed to preroll pipeline");
2705 /* must be called with state-lock */
2707 media_set_pipeline_state_locked (GstRTSPMedia * media, GstState state)
2709 GstRTSPMediaPrivate *priv = media->priv;
2711 if (state == GST_STATE_NULL) {
2712 gst_rtsp_media_unprepare (media);
2714 GST_INFO ("state %s media %p", gst_element_state_get_name (state), media);
2715 set_target_state (media, state, FALSE);
2716 /* when we are buffering, don't update the state yet, this will be done
2717 * when buffering finishes */
2718 if (priv->buffering) {
2719 GST_INFO ("Buffering busy, delay state change");
2721 if (state == GST_STATE_PLAYING)
2722 /* make sure pads are not blocking anymore when going to PLAYING */
2723 media_streams_set_blocked (media, FALSE);
2725 set_state (media, state);
2727 /* and suspend after pause */
2728 if (state == GST_STATE_PAUSED)
2729 gst_rtsp_media_suspend (media);
2735 * gst_rtsp_media_set_pipeline_state:
2736 * @media: a #GstRTSPMedia
2737 * @state: the target state of the pipeline
2739 * Set the state of the pipeline managed by @media to @state
2742 gst_rtsp_media_set_pipeline_state (GstRTSPMedia * media, GstState state)
2744 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
2746 g_rec_mutex_lock (&media->priv->state_lock);
2747 media_set_pipeline_state_locked (media, state);
2748 g_rec_mutex_unlock (&media->priv->state_lock);
2752 * gst_rtsp_media_set_state:
2753 * @media: a #GstRTSPMedia
2754 * @state: the target state of the media
2755 * @transports: (element-type GstRtspServer.RTSPStreamTransport): a #GPtrArray
2756 * of #GstRTSPStreamTransport pointers
2758 * Set the state of @media to @state and for the transports in @transports.
2760 * @media must be prepared with gst_rtsp_media_prepare();
2762 * Returns: %TRUE on success.
2765 gst_rtsp_media_set_state (GstRTSPMedia * media, GstState state,
2766 GPtrArray * transports)
2768 GstRTSPMediaPrivate *priv;
2770 gboolean activate, deactivate, do_state;
2773 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2774 g_return_val_if_fail (transports != NULL, FALSE);
2778 g_rec_mutex_lock (&priv->state_lock);
2779 if (priv->status == GST_RTSP_MEDIA_STATUS_ERROR)
2781 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
2782 priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
2785 /* NULL and READY are the same */
2786 if (state == GST_STATE_READY)
2787 state = GST_STATE_NULL;
2789 activate = deactivate = FALSE;
2791 GST_INFO ("going to state %s media %p", gst_element_state_get_name (state),
2795 case GST_STATE_NULL:
2796 case GST_STATE_PAUSED:
2797 /* we're going from PLAYING to PAUSED, READY or NULL, deactivate */
2798 if (priv->target_state == GST_STATE_PLAYING)
2801 case GST_STATE_PLAYING:
2802 /* we're going to PLAYING, activate */
2808 old_active = priv->n_active;
2810 for (i = 0; i < transports->len; i++) {
2811 GstRTSPStreamTransport *trans;
2813 /* we need a non-NULL entry in the array */
2814 trans = g_ptr_array_index (transports, i);
2819 if (gst_rtsp_stream_transport_set_active (trans, TRUE))
2821 } else if (deactivate) {
2822 if (gst_rtsp_stream_transport_set_active (trans, FALSE))
2827 /* we just activated the first media, do the playing state change */
2828 if (old_active == 0 && activate)
2830 /* if we have no more active media, do the downward state changes */
2831 else if (priv->n_active == 0)
2836 GST_INFO ("state %d active %d media %p do_state %d", state, priv->n_active,
2839 if (priv->target_state != state) {
2841 media_set_pipeline_state_locked (media, state);
2843 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STATE], 0, state,
2847 /* remember where we are */
2848 if (state != GST_STATE_NULL && (state == GST_STATE_PAUSED ||
2849 old_active != priv->n_active))
2850 collect_media_stats (media);
2852 g_rec_mutex_unlock (&priv->state_lock);
2859 GST_WARNING ("media %p was not prepared", media);
2860 g_rec_mutex_unlock (&priv->state_lock);
2865 GST_WARNING ("media %p in error status while changing to state %d",
2867 if (state == GST_STATE_NULL) {
2868 for (i = 0; i < transports->len; i++) {
2869 GstRTSPStreamTransport *trans;
2871 /* we need a non-NULL entry in the array */
2872 trans = g_ptr_array_index (transports, i);
2876 gst_rtsp_stream_transport_set_active (trans, FALSE);
2880 g_rec_mutex_unlock (&priv->state_lock);