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: (transfer full): 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: (transfer none): 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: (transfer none): 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 */
1197 GST_INFO ("found dynamic element %d, %p", i, elem);
1199 g_mutex_lock (&priv->lock);
1200 priv->dynamic = g_list_prepend (priv->dynamic, elem);
1201 g_mutex_unlock (&priv->lock);
1210 * gst_rtsp_media_create_stream:
1211 * @media: a #GstRTSPMedia
1212 * @payloader: a #GstElement
1213 * @srcpad: a source #GstPad
1215 * Create a new stream in @media that provides RTP data on @srcpad.
1216 * @srcpad should be a pad of an element inside @media->element.
1218 * Returns: (transfer none): a new #GstRTSPStream that remains valid for as long
1222 gst_rtsp_media_create_stream (GstRTSPMedia * media, GstElement * payloader,
1225 GstRTSPMediaPrivate *priv;
1226 GstRTSPStream *stream;
1231 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1232 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
1233 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1234 g_return_val_if_fail (GST_PAD_IS_SRC (pad), NULL);
1238 g_mutex_lock (&priv->lock);
1239 idx = priv->streams->len;
1241 GST_DEBUG ("media %p: creating stream with index %d", media, idx);
1243 name = g_strdup_printf ("src_%u", idx);
1244 srcpad = gst_ghost_pad_new (name, pad);
1245 gst_pad_set_active (srcpad, TRUE);
1246 gst_element_add_pad (priv->element, srcpad);
1249 stream = gst_rtsp_stream_new (idx, payloader, srcpad);
1251 gst_rtsp_stream_set_address_pool (stream, priv->pool);
1252 gst_rtsp_stream_set_profiles (stream, priv->profiles);
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: (transfer full): 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_set_status (GstRTSPMedia * media, GstRTSPMediaStatus status)
1476 GstRTSPMediaPrivate *priv = media->priv;
1478 g_mutex_lock (&priv->lock);
1479 priv->status = status;
1480 GST_DEBUG ("setting new status to %d", status);
1481 g_cond_broadcast (&priv->cond);
1482 g_mutex_unlock (&priv->lock);
1486 * gst_rtsp_media_get_status:
1487 * @media: a #GstRTSPMedia
1489 * Get the status of @media. When @media is busy preparing, this function waits
1490 * until @media is prepared or in error.
1492 * Returns: the status of @media.
1495 gst_rtsp_media_get_status (GstRTSPMedia * media)
1497 GstRTSPMediaPrivate *priv = media->priv;
1498 GstRTSPMediaStatus result;
1501 g_mutex_lock (&priv->lock);
1502 end_time = g_get_monotonic_time () + 20 * G_TIME_SPAN_SECOND;
1503 /* while we are preparing, wait */
1504 while (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING) {
1505 GST_DEBUG ("waiting for status change");
1506 if (!g_cond_wait_until (&priv->cond, &priv->lock, end_time)) {
1507 GST_DEBUG ("timeout, assuming error status");
1508 priv->status = GST_RTSP_MEDIA_STATUS_ERROR;
1511 /* could be success or error */
1512 result = priv->status;
1513 GST_DEBUG ("got status %d", result);
1514 g_mutex_unlock (&priv->lock);
1520 * gst_rtsp_media_seek:
1521 * @media: a #GstRTSPMedia
1522 * @range: (transfer none): a #GstRTSPTimeRange
1524 * Seek the pipeline of @media to @range. @media must be prepared with
1525 * gst_rtsp_media_prepare().
1527 * Returns: %TRUE on success.
1530 gst_rtsp_media_seek (GstRTSPMedia * media, GstRTSPTimeRange * range)
1532 GstRTSPMediaClass *klass;
1533 GstRTSPMediaPrivate *priv;
1535 GstClockTime start, stop;
1536 GstSeekType start_type, stop_type;
1539 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1541 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1542 g_return_val_if_fail (range != NULL, FALSE);
1543 g_return_val_if_fail (klass->convert_range != NULL, FALSE);
1547 g_rec_mutex_lock (&priv->state_lock);
1548 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
1551 /* Update the seekable state of the pipeline in case it changed */
1552 query = gst_query_new_seeking (GST_FORMAT_TIME);
1553 if (gst_element_query (priv->pipeline, query)) {
1558 gst_query_parse_seeking (query, &format, &seekable, &start, &end);
1559 priv->seekable = seekable;
1561 gst_query_unref (query);
1563 if (!priv->seekable)
1566 start_type = stop_type = GST_SEEK_TYPE_NONE;
1568 if (!klass->convert_range (media, range, GST_RTSP_RANGE_NPT))
1570 gst_rtsp_range_get_times (range, &start, &stop);
1572 GST_INFO ("got %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1573 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1574 GST_INFO ("current %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1575 GST_TIME_ARGS (priv->range_start), GST_TIME_ARGS (priv->range_stop));
1577 if (start != GST_CLOCK_TIME_NONE)
1578 start_type = GST_SEEK_TYPE_SET;
1580 if (priv->range_stop == stop)
1581 stop = GST_CLOCK_TIME_NONE;
1582 else if (stop != GST_CLOCK_TIME_NONE)
1583 stop_type = GST_SEEK_TYPE_SET;
1585 if (start != GST_CLOCK_TIME_NONE || stop != GST_CLOCK_TIME_NONE) {
1588 GST_INFO ("seeking to %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1589 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1591 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARING);
1593 media_streams_set_blocked (media, TRUE);
1595 /* depends on the current playing state of the pipeline. We might need to
1596 * queue this until we get EOS. */
1597 flags = GST_SEEK_FLAG_FLUSH;
1599 /* if range start was not supplied we must continue from current position.
1600 * but since we're doing a flushing seek, let us query the current position
1601 * so we end up at exactly the same position after the seek. */
1602 if (range->min.type == GST_RTSP_TIME_END) { /* Yepp, that's right! */
1604 gboolean ret = FALSE;
1606 if (klass->query_position)
1607 ret = klass->query_position (media, &position);
1610 GST_WARNING ("position query failed");
1612 GST_DEBUG ("doing accurate seek to %" GST_TIME_FORMAT,
1613 GST_TIME_ARGS (position));
1615 start_type = GST_SEEK_TYPE_SET;
1616 flags |= GST_SEEK_FLAG_ACCURATE;
1619 /* only set keyframe flag when modifying start */
1620 if (start_type != GST_SEEK_TYPE_NONE)
1621 flags |= GST_SEEK_FLAG_KEY_UNIT;
1624 /* FIXME, we only do forwards */
1625 res = gst_element_seek (priv->pipeline, 1.0, GST_FORMAT_TIME,
1626 flags, start_type, start, stop_type, stop);
1628 /* and block for the seek to complete */
1629 GST_INFO ("done seeking %d", res);
1630 g_rec_mutex_unlock (&priv->state_lock);
1632 /* wait until pipeline is prerolled again, this will also collect stats */
1633 if (!wait_preroll (media))
1634 goto preroll_failed;
1636 g_rec_mutex_lock (&priv->state_lock);
1637 GST_INFO ("prerolled again");
1639 GST_INFO ("no seek needed");
1642 g_rec_mutex_unlock (&priv->state_lock);
1649 g_rec_mutex_unlock (&priv->state_lock);
1650 GST_INFO ("media %p is not prepared", media);
1655 g_rec_mutex_unlock (&priv->state_lock);
1656 GST_INFO ("pipeline is not seekable");
1661 g_rec_mutex_unlock (&priv->state_lock);
1662 GST_WARNING ("conversion to npt not supported");
1667 GST_WARNING ("failed to preroll after seek");
1673 stream_collect_blocking (GstRTSPStream * stream, gboolean * blocked)
1675 *blocked &= gst_rtsp_stream_is_blocking (stream);
1679 media_streams_blocking (GstRTSPMedia * media)
1681 gboolean blocking = TRUE;
1683 g_ptr_array_foreach (media->priv->streams, (GFunc) stream_collect_blocking,
1689 static GstStateChangeReturn
1690 set_state (GstRTSPMedia * media, GstState state)
1692 GstRTSPMediaPrivate *priv = media->priv;
1693 GstStateChangeReturn ret;
1695 GST_INFO ("set state to %s for media %p", gst_element_state_get_name (state),
1697 ret = gst_element_set_state (priv->pipeline, state);
1702 static GstStateChangeReturn
1703 set_target_state (GstRTSPMedia * media, GstState state, gboolean do_state)
1705 GstRTSPMediaPrivate *priv = media->priv;
1706 GstStateChangeReturn ret;
1708 GST_INFO ("set target state to %s for media %p",
1709 gst_element_state_get_name (state), media);
1710 priv->target_state = state;
1712 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_TARGET_STATE], 0,
1713 priv->target_state, NULL);
1716 ret = set_state (media, state);
1718 ret = GST_STATE_CHANGE_SUCCESS;
1723 /* called with state-lock */
1725 default_handle_message (GstRTSPMedia * media, GstMessage * message)
1727 GstRTSPMediaPrivate *priv = media->priv;
1728 GstMessageType type;
1730 type = GST_MESSAGE_TYPE (message);
1733 case GST_MESSAGE_STATE_CHANGED:
1735 case GST_MESSAGE_BUFFERING:
1739 gst_message_parse_buffering (message, &percent);
1741 /* no state management needed for live pipelines */
1745 if (percent == 100) {
1746 /* a 100% message means buffering is done */
1747 priv->buffering = FALSE;
1748 /* if the desired state is playing, go back */
1749 if (priv->target_state == GST_STATE_PLAYING) {
1750 GST_INFO ("Buffering done, setting pipeline to PLAYING");
1751 set_state (media, GST_STATE_PLAYING);
1753 GST_INFO ("Buffering done");
1756 /* buffering busy */
1757 if (priv->buffering == FALSE) {
1758 if (priv->target_state == GST_STATE_PLAYING) {
1759 /* we were not buffering but PLAYING, PAUSE the pipeline. */
1760 GST_INFO ("Buffering, setting pipeline to PAUSED ...");
1761 set_state (media, GST_STATE_PAUSED);
1763 GST_INFO ("Buffering ...");
1766 priv->buffering = TRUE;
1770 case GST_MESSAGE_LATENCY:
1772 gst_bin_recalculate_latency (GST_BIN_CAST (priv->pipeline));
1775 case GST_MESSAGE_ERROR:
1780 gst_message_parse_error (message, &gerror, &debug);
1781 GST_WARNING ("%p: got error %s (%s)", media, gerror->message, debug);
1782 g_error_free (gerror);
1785 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
1788 case GST_MESSAGE_WARNING:
1793 gst_message_parse_warning (message, &gerror, &debug);
1794 GST_WARNING ("%p: got warning %s (%s)", media, gerror->message, debug);
1795 g_error_free (gerror);
1799 case GST_MESSAGE_ELEMENT:
1801 const GstStructure *s;
1803 s = gst_message_get_structure (message);
1804 if (gst_structure_has_name (s, "GstRTSPStreamBlocking")) {
1805 GST_DEBUG ("media received blocking message");
1806 if (priv->blocked && media_streams_blocking (media)) {
1807 GST_DEBUG ("media is blocking");
1808 collect_media_stats (media);
1810 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
1811 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
1816 case GST_MESSAGE_STREAM_STATUS:
1818 case GST_MESSAGE_ASYNC_DONE:
1820 /* when we are dynamically adding pads, the addition of the udpsrc will
1821 * temporarily produce ASYNC_DONE messages. We have to ignore them and
1822 * wait for the final ASYNC_DONE after everything prerolled */
1823 GST_INFO ("%p: ignoring ASYNC_DONE", media);
1825 GST_INFO ("%p: got ASYNC_DONE", media);
1826 collect_media_stats (media);
1828 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
1829 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
1832 case GST_MESSAGE_EOS:
1833 GST_INFO ("%p: got EOS", media);
1835 if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARING) {
1836 GST_DEBUG ("shutting down after EOS");
1837 finish_unprepare (media);
1841 GST_INFO ("%p: got message type %d (%s)", media, type,
1842 gst_message_type_get_name (type));
1849 bus_message (GstBus * bus, GstMessage * message, GstRTSPMedia * media)
1851 GstRTSPMediaPrivate *priv = media->priv;
1852 GstRTSPMediaClass *klass;
1855 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1857 g_rec_mutex_lock (&priv->state_lock);
1858 if (klass->handle_message)
1859 ret = klass->handle_message (media, message);
1862 g_rec_mutex_unlock (&priv->state_lock);
1868 watch_destroyed (GstRTSPMedia * media)
1870 GST_DEBUG_OBJECT (media, "source destroyed");
1871 g_object_unref (media);
1875 find_payload_element (GstElement * payloader)
1877 GstElement *pay = NULL;
1879 if (GST_IS_BIN (payloader)) {
1881 GValue item = { 0 };
1883 iter = gst_bin_iterate_recurse (GST_BIN (payloader));
1884 while (gst_iterator_next (iter, &item) == GST_ITERATOR_OK) {
1885 GstElement *element = (GstElement *) g_value_get_object (&item);
1886 GstElementClass *eclass = GST_ELEMENT_GET_CLASS (element);
1890 gst_element_class_get_metadata (eclass, GST_ELEMENT_METADATA_KLASS);
1894 if (strstr (klass, "Payloader") && strstr (klass, "RTP")) {
1895 pay = gst_object_ref (element);
1896 g_value_unset (&item);
1899 g_value_unset (&item);
1901 gst_iterator_free (iter);
1903 pay = g_object_ref (payloader);
1909 /* called from streaming threads */
1911 pad_added_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
1913 GstRTSPMediaPrivate *priv = media->priv;
1914 GstRTSPStream *stream;
1917 /* find the real payload element */
1918 pay = find_payload_element (element);
1919 stream = gst_rtsp_media_create_stream (media, pay, pad);
1920 gst_object_unref (pay);
1922 GST_INFO ("pad added %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
1924 g_rec_mutex_lock (&priv->state_lock);
1925 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARING)
1928 g_object_set_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream", stream);
1930 /* we will be adding elements below that will cause ASYNC_DONE to be
1931 * posted in the bus. We want to ignore those messages until the
1932 * pipeline really prerolled. */
1933 priv->adding = TRUE;
1935 /* join the element in the PAUSED state because this callback is
1936 * called from the streaming thread and it is PAUSED */
1937 if (!gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
1938 priv->rtpbin, GST_STATE_PAUSED)) {
1939 GST_WARNING ("failed to join bin element");
1942 priv->adding = FALSE;
1943 g_rec_mutex_unlock (&priv->state_lock);
1950 gst_rtsp_media_remove_stream (media, stream);
1951 g_rec_mutex_unlock (&priv->state_lock);
1952 GST_INFO ("ignore pad because we are not preparing");
1958 pad_removed_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
1960 GstRTSPMediaPrivate *priv = media->priv;
1961 GstRTSPStream *stream;
1963 stream = g_object_get_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream");
1967 GST_INFO ("pad removed %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
1969 g_rec_mutex_lock (&priv->state_lock);
1970 gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
1971 g_rec_mutex_unlock (&priv->state_lock);
1973 gst_rtsp_media_remove_stream (media, stream);
1977 remove_fakesink (GstRTSPMediaPrivate * priv)
1979 GstElement *fakesink;
1981 g_mutex_lock (&priv->lock);
1982 if ((fakesink = priv->fakesink))
1983 gst_object_ref (fakesink);
1984 priv->fakesink = NULL;
1985 g_mutex_unlock (&priv->lock);
1988 gst_bin_remove (GST_BIN (priv->pipeline), fakesink);
1989 gst_element_set_state (fakesink, GST_STATE_NULL);
1990 gst_object_unref (fakesink);
1991 GST_INFO ("removed fakesink");
1996 no_more_pads_cb (GstElement * element, GstRTSPMedia * media)
1998 GstRTSPMediaPrivate *priv = media->priv;
2000 GST_INFO ("no more pads");
2001 remove_fakesink (priv);
2004 typedef struct _DynPaySignalHandlers DynPaySignalHandlers;
2006 struct _DynPaySignalHandlers
2008 gulong pad_added_handler;
2009 gulong pad_removed_handler;
2010 gulong no_more_pads_handler;
2014 start_preroll (GstRTSPMedia * media)
2016 GstRTSPMediaPrivate *priv = media->priv;
2017 GstStateChangeReturn ret;
2019 GST_INFO ("setting pipeline to PAUSED for media %p", media);
2020 /* first go to PAUSED */
2021 ret = set_target_state (media, GST_STATE_PAUSED, TRUE);
2024 case GST_STATE_CHANGE_SUCCESS:
2025 GST_INFO ("SUCCESS state change for media %p", media);
2026 priv->seekable = TRUE;
2028 case GST_STATE_CHANGE_ASYNC:
2029 GST_INFO ("ASYNC state change for media %p", media);
2030 priv->seekable = TRUE;
2032 case GST_STATE_CHANGE_NO_PREROLL:
2033 /* we need to go to PLAYING */
2034 GST_INFO ("NO_PREROLL state change: live media %p", media);
2035 /* FIXME we disable seeking for live streams for now. We should perform a
2036 * seeking query in preroll instead */
2037 priv->seekable = FALSE;
2038 priv->is_live = TRUE;
2039 /* start blocked to make sure nothing goes to the sink */
2040 media_streams_set_blocked (media, TRUE);
2041 ret = set_state (media, GST_STATE_PLAYING);
2042 if (ret == GST_STATE_CHANGE_FAILURE)
2045 case GST_STATE_CHANGE_FAILURE:
2053 GST_WARNING ("failed to preroll pipeline");
2059 wait_preroll (GstRTSPMedia * media)
2061 GstRTSPMediaStatus status;
2063 GST_DEBUG ("wait to preroll pipeline");
2065 /* wait until pipeline is prerolled */
2066 status = gst_rtsp_media_get_status (media);
2067 if (status == GST_RTSP_MEDIA_STATUS_ERROR)
2068 goto preroll_failed;
2074 GST_WARNING ("failed to preroll pipeline");
2080 start_prepare (GstRTSPMedia * media)
2082 GstRTSPMediaPrivate *priv = media->priv;
2086 /* link streams we already have, other streams might appear when we have
2087 * dynamic elements */
2088 for (i = 0; i < priv->streams->len; i++) {
2089 GstRTSPStream *stream;
2091 stream = g_ptr_array_index (priv->streams, i);
2093 if (!gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
2094 priv->rtpbin, GST_STATE_NULL)) {
2095 goto join_bin_failed;
2099 for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
2100 GstElement *elem = walk->data;
2101 DynPaySignalHandlers *handlers = g_slice_new (DynPaySignalHandlers);
2103 GST_INFO ("adding callbacks for dynamic element %p", elem);
2105 handlers->pad_added_handler = g_signal_connect (elem, "pad-added",
2106 (GCallback) pad_added_cb, media);
2107 handlers->pad_removed_handler = g_signal_connect (elem, "pad-removed",
2108 (GCallback) pad_removed_cb, media);
2109 handlers->no_more_pads_handler = g_signal_connect (elem, "no-more-pads",
2110 (GCallback) no_more_pads_cb, media);
2112 g_object_set_data (G_OBJECT (elem), "gst-rtsp-dynpay-handlers", handlers);
2114 /* we add a fakesink here in order to make the state change async. We remove
2115 * the fakesink again in the no-more-pads callback. */
2116 priv->fakesink = gst_element_factory_make ("fakesink", "fakesink");
2117 gst_bin_add (GST_BIN (priv->pipeline), priv->fakesink);
2120 if (!start_preroll (media))
2121 goto preroll_failed;
2127 GST_WARNING ("failed to join bin element");
2128 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2133 GST_WARNING ("failed to preroll pipeline");
2134 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2140 * gst_rtsp_media_prepare:
2141 * @media: a #GstRTSPMedia
2142 * @thread: (transfer full): a #GstRTSPThread to run the bus handler or %NULL
2144 * Prepare @media for streaming. This function will create the objects
2145 * to manage the streaming. A pipeline must have been set on @media with
2146 * gst_rtsp_media_take_pipeline().
2148 * It will preroll the pipeline and collect vital information about the streams
2149 * such as the duration.
2151 * Returns: %TRUE on success.
2154 gst_rtsp_media_prepare (GstRTSPMedia * media, GstRTSPThread * thread)
2156 GstRTSPMediaPrivate *priv;
2159 GstRTSPMediaClass *klass;
2160 GMainContext *context;
2162 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2166 g_rec_mutex_lock (&priv->state_lock);
2167 priv->prepare_count++;
2169 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED ||
2170 priv->status == GST_RTSP_MEDIA_STATUS_SUSPENDED)
2173 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
2176 if (priv->status != GST_RTSP_MEDIA_STATUS_UNPREPARED)
2177 goto not_unprepared;
2179 if (!priv->reusable && priv->reused)
2182 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2184 if (!klass->create_rtpbin)
2185 goto no_create_rtpbin;
2187 priv->rtpbin = klass->create_rtpbin (media);
2188 if (priv->rtpbin != NULL) {
2189 gboolean success = TRUE;
2191 if (klass->setup_rtpbin)
2192 success = klass->setup_rtpbin (media, priv->rtpbin);
2194 if (success == FALSE) {
2195 gst_object_unref (priv->rtpbin);
2196 priv->rtpbin = NULL;
2199 if (priv->rtpbin == NULL)
2202 GST_INFO ("preparing media %p", media);
2204 /* reset some variables */
2205 priv->is_live = FALSE;
2206 priv->seekable = FALSE;
2207 priv->buffering = FALSE;
2208 priv->thread = thread;
2209 context = (thread != NULL) ? (thread->context) : NULL;
2211 /* we're preparing now */
2212 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARING);
2214 bus = gst_pipeline_get_bus (GST_PIPELINE_CAST (priv->pipeline));
2216 /* add the pipeline bus to our custom mainloop */
2217 priv->source = gst_bus_create_watch (bus);
2218 gst_object_unref (bus);
2220 g_source_set_callback (priv->source, (GSourceFunc) bus_message,
2221 g_object_ref (media), (GDestroyNotify) watch_destroyed);
2223 priv->id = g_source_attach (priv->source, context);
2225 /* add stuff to the bin */
2226 gst_bin_add (GST_BIN (priv->pipeline), priv->rtpbin);
2228 /* do remainder in context */
2229 source = g_idle_source_new ();
2230 g_source_set_callback (source, (GSourceFunc) start_prepare, media, NULL);
2231 g_source_attach (source, context);
2232 g_source_unref (source);
2235 g_rec_mutex_unlock (&priv->state_lock);
2237 /* now wait for all pads to be prerolled, FIXME, we should somehow be
2238 * able to do this async so that we don't block the server thread. */
2239 if (!wait_preroll (media))
2240 goto preroll_failed;
2242 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_PREPARED], 0, NULL);
2244 GST_INFO ("object %p is prerolled", media);
2251 GST_LOG ("media %p was prepared", media);
2252 /* we are not going to use the giving thread, so stop it. */
2254 gst_rtsp_thread_stop (thread);
2255 g_rec_mutex_unlock (&priv->state_lock);
2261 /* we are not going to use the giving thread, so stop it. */
2263 gst_rtsp_thread_stop (thread);
2264 GST_WARNING ("media %p was not unprepared", media);
2265 priv->prepare_count--;
2266 g_rec_mutex_unlock (&priv->state_lock);
2271 /* we are not going to use the giving thread, so stop it. */
2273 gst_rtsp_thread_stop (thread);
2274 priv->prepare_count--;
2275 g_rec_mutex_unlock (&priv->state_lock);
2276 GST_WARNING ("can not reuse media %p", media);
2281 /* we are not going to use the giving thread, so stop it. */
2283 gst_rtsp_thread_stop (thread);
2284 priv->prepare_count--;
2285 g_rec_mutex_unlock (&priv->state_lock);
2286 GST_ERROR ("no create_rtpbin function");
2287 g_critical ("no create_rtpbin vmethod function set");
2292 /* we are not going to use the giving thread, so stop it. */
2294 gst_rtsp_thread_stop (thread);
2295 priv->prepare_count--;
2296 g_rec_mutex_unlock (&priv->state_lock);
2297 GST_WARNING ("no rtpbin element");
2298 g_warning ("failed to create element 'rtpbin', check your installation");
2303 GST_WARNING ("failed to preroll pipeline");
2304 gst_rtsp_media_unprepare (media);
2309 /* must be called with state-lock */
2311 finish_unprepare (GstRTSPMedia * media)
2313 GstRTSPMediaPrivate *priv = media->priv;
2317 GST_DEBUG ("shutting down");
2319 /* release the lock on shutdown, otherwise pad_added_cb might try to
2320 * acquire the lock and then we deadlock */
2321 g_rec_mutex_unlock (&priv->state_lock);
2322 set_state (media, GST_STATE_NULL);
2323 g_rec_mutex_lock (&priv->state_lock);
2324 remove_fakesink (priv);
2326 for (i = 0; i < priv->streams->len; i++) {
2327 GstRTSPStream *stream;
2329 GST_INFO ("Removing elements of stream %d from pipeline", i);
2331 stream = g_ptr_array_index (priv->streams, i);
2333 gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
2336 /* remove the pad signal handlers */
2337 for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
2338 GstElement *elem = walk->data;
2339 DynPaySignalHandlers *handlers;
2342 g_object_steal_data (G_OBJECT (elem), "gst-rtsp-dynpay-handlers");
2343 g_assert (handlers != NULL);
2345 g_signal_handler_disconnect (G_OBJECT (elem), handlers->pad_added_handler);
2346 g_signal_handler_disconnect (G_OBJECT (elem),
2347 handlers->pad_removed_handler);
2348 g_signal_handler_disconnect (G_OBJECT (elem),
2349 handlers->no_more_pads_handler);
2351 g_slice_free (DynPaySignalHandlers, handlers);
2354 gst_bin_remove (GST_BIN (priv->pipeline), priv->rtpbin);
2355 priv->rtpbin = NULL;
2358 gst_object_unref (priv->nettime);
2359 priv->nettime = NULL;
2361 priv->reused = TRUE;
2362 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_UNPREPARED);
2364 /* when the media is not reusable, this will effectively unref the media and
2366 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_UNPREPARED], 0, NULL);
2368 /* the source has the last ref to the media */
2370 GST_DEBUG ("destroy source");
2371 g_source_destroy (priv->source);
2372 g_source_unref (priv->source);
2375 GST_DEBUG ("stop thread");
2376 gst_rtsp_thread_stop (priv->thread);
2380 /* called with state-lock */
2382 default_unprepare (GstRTSPMedia * media)
2384 GstRTSPMediaPrivate *priv = media->priv;
2386 if (priv->eos_shutdown) {
2387 GST_DEBUG ("sending EOS for shutdown");
2388 /* ref so that we don't disappear */
2389 gst_element_send_event (priv->pipeline, gst_event_new_eos ());
2390 /* we need to go to playing again for the EOS to propagate, normally in this
2391 * state, nothing is receiving data from us anymore so this is ok. */
2392 set_state (media, GST_STATE_PLAYING);
2394 finish_unprepare (media);
2400 * gst_rtsp_media_unprepare:
2401 * @media: a #GstRTSPMedia
2403 * Unprepare @media. After this call, the media should be prepared again before
2404 * it can be used again. If the media is set to be non-reusable, a new instance
2407 * Returns: %TRUE on success.
2410 gst_rtsp_media_unprepare (GstRTSPMedia * media)
2412 GstRTSPMediaPrivate *priv;
2415 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2419 g_rec_mutex_lock (&priv->state_lock);
2420 if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARED)
2421 goto was_unprepared;
2423 priv->prepare_count--;
2424 if (priv->prepare_count > 0)
2427 GST_INFO ("unprepare media %p", media);
2429 media_streams_set_blocked (media, FALSE);
2430 set_target_state (media, GST_STATE_NULL, FALSE);
2433 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_UNPREPARING);
2435 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED) {
2436 GstRTSPMediaClass *klass;
2438 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2439 if (klass->unprepare)
2440 success = klass->unprepare (media);
2442 finish_unprepare (media);
2444 g_rec_mutex_unlock (&priv->state_lock);
2450 g_rec_mutex_unlock (&priv->state_lock);
2451 GST_INFO ("media %p was already unprepared", media);
2456 GST_INFO ("media %p still prepared %d times", media, priv->prepare_count);
2457 g_rec_mutex_unlock (&priv->state_lock);
2462 /* should be called with state-lock */
2464 get_clock_unlocked (GstRTSPMedia * media)
2466 if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED) {
2467 GST_DEBUG_OBJECT (media, "media was not prepared");
2470 return gst_pipeline_get_clock (GST_PIPELINE_CAST (media->priv->pipeline));
2474 * gst_rtsp_media_get_clock:
2475 * @media: a #GstRTSPMedia
2477 * Get the clock that is used by the pipeline in @media.
2479 * @media must be prepared before this method returns a valid clock object.
2481 * Returns: (transfer full): the #GstClock used by @media. unref after usage.
2484 gst_rtsp_media_get_clock (GstRTSPMedia * media)
2487 GstRTSPMediaPrivate *priv;
2489 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
2493 g_rec_mutex_lock (&priv->state_lock);
2494 clock = get_clock_unlocked (media);
2495 g_rec_mutex_unlock (&priv->state_lock);
2501 * gst_rtsp_media_get_base_time:
2502 * @media: a #GstRTSPMedia
2504 * Get the base_time that is used by the pipeline in @media.
2506 * @media must be prepared before this method returns a valid base_time.
2508 * Returns: the base_time used by @media.
2511 gst_rtsp_media_get_base_time (GstRTSPMedia * media)
2513 GstClockTime result;
2514 GstRTSPMediaPrivate *priv;
2516 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_CLOCK_TIME_NONE);
2520 g_rec_mutex_lock (&priv->state_lock);
2521 if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
2524 result = gst_element_get_base_time (media->priv->pipeline);
2525 g_rec_mutex_unlock (&priv->state_lock);
2532 g_rec_mutex_unlock (&priv->state_lock);
2533 GST_DEBUG_OBJECT (media, "media was not prepared");
2534 return GST_CLOCK_TIME_NONE;
2539 * gst_rtsp_media_get_time_provider:
2540 * @media: a #GstRTSPMedia
2541 * @address: an address or %NULL
2542 * @port: a port or 0
2544 * Get the #GstNetTimeProvider for the clock used by @media. The time provider
2545 * will listen on @address and @port for client time requests.
2547 * Returns: (transfer full): the #GstNetTimeProvider of @media.
2549 GstNetTimeProvider *
2550 gst_rtsp_media_get_time_provider (GstRTSPMedia * media, const gchar * address,
2553 GstRTSPMediaPrivate *priv;
2554 GstNetTimeProvider *provider = NULL;
2556 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
2560 g_rec_mutex_lock (&priv->state_lock);
2561 if (priv->time_provider) {
2562 if ((provider = priv->nettime) == NULL) {
2565 if (priv->time_provider && (clock = get_clock_unlocked (media))) {
2566 provider = gst_net_time_provider_new (clock, address, port);
2567 gst_object_unref (clock);
2569 priv->nettime = provider;
2573 g_rec_mutex_unlock (&priv->state_lock);
2576 gst_object_ref (provider);
2582 default_setup_sdp (GstRTSPMedia * media, GstSDPMessage * sdp, GstSDPInfo * info)
2584 return gst_rtsp_sdp_from_media (sdp, info, media);
2588 * gst_rtsp_media_setup_sdp:
2589 * @media: a #GstRTSPMedia
2590 * @sdp: (transfer none): a #GstSDPMessage
2591 * @info: (transfer none): a #GstSDPInfo
2593 * Add @media specific info to @sdp. @info is used to configure the connection
2594 * information in the SDP.
2596 * Returns: TRUE on success.
2599 gst_rtsp_media_setup_sdp (GstRTSPMedia * media, GstSDPMessage * sdp,
2602 GstRTSPMediaPrivate *priv;
2603 GstRTSPMediaClass *klass;
2606 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2607 g_return_val_if_fail (sdp != NULL, FALSE);
2608 g_return_val_if_fail (info != NULL, FALSE);
2612 g_rec_mutex_lock (&priv->state_lock);
2614 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2616 if (!klass->setup_sdp)
2619 res = klass->setup_sdp (media, sdp, info);
2621 g_rec_mutex_unlock (&priv->state_lock);
2628 g_rec_mutex_unlock (&priv->state_lock);
2629 GST_ERROR ("no setup_sdp function");
2630 g_critical ("no setup_sdp vmethod function set");
2636 * gst_rtsp_media_suspend:
2637 * @media: a #GstRTSPMedia
2639 * Suspend @media. The state of the pipeline managed by @media is set to
2640 * GST_STATE_NULL but all streams are kept. @media can be prepared again
2641 * with gst_rtsp_media_unsuspend()
2643 * @media must be prepared with gst_rtsp_media_prepare();
2645 * Returns: %TRUE on success.
2648 gst_rtsp_media_suspend (GstRTSPMedia * media)
2650 GstRTSPMediaPrivate *priv = media->priv;
2651 GstStateChangeReturn ret;
2653 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2655 GST_FIXME ("suspend for dynamic pipelines needs fixing");
2657 g_rec_mutex_lock (&priv->state_lock);
2658 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
2661 /* don't attempt to suspend when something is busy */
2662 if (priv->n_active > 0)
2665 switch (priv->suspend_mode) {
2666 case GST_RTSP_SUSPEND_MODE_NONE:
2667 GST_DEBUG ("media %p no suspend", media);
2669 case GST_RTSP_SUSPEND_MODE_PAUSE:
2670 GST_DEBUG ("media %p suspend to PAUSED", media);
2671 ret = set_target_state (media, GST_STATE_PAUSED, TRUE);
2672 if (ret == GST_STATE_CHANGE_FAILURE)
2675 case GST_RTSP_SUSPEND_MODE_RESET:
2676 GST_DEBUG ("media %p suspend to NULL", media);
2677 ret = set_target_state (media, GST_STATE_NULL, TRUE);
2678 if (ret == GST_STATE_CHANGE_FAILURE)
2684 /* let the streams do the state changes freely, if any */
2685 media_streams_set_blocked (media, FALSE);
2686 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_SUSPENDED);
2688 g_rec_mutex_unlock (&priv->state_lock);
2695 g_rec_mutex_unlock (&priv->state_lock);
2696 GST_WARNING ("media %p was not prepared", media);
2701 g_rec_mutex_unlock (&priv->state_lock);
2702 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2703 GST_WARNING ("failed changing pipeline's state for media %p", media);
2709 * gst_rtsp_media_unsuspend:
2710 * @media: a #GstRTSPMedia
2712 * Unsuspend @media if it was in a suspended state. This method does nothing
2713 * when the media was not in the suspended state.
2715 * Returns: %TRUE on success.
2718 gst_rtsp_media_unsuspend (GstRTSPMedia * media)
2720 GstRTSPMediaPrivate *priv = media->priv;
2722 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2724 g_rec_mutex_lock (&priv->state_lock);
2725 if (priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
2728 switch (priv->suspend_mode) {
2729 case GST_RTSP_SUSPEND_MODE_NONE:
2730 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
2732 case GST_RTSP_SUSPEND_MODE_PAUSE:
2733 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
2735 case GST_RTSP_SUSPEND_MODE_RESET:
2737 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARING);
2738 if (!start_preroll (media))
2740 g_rec_mutex_unlock (&priv->state_lock);
2742 if (!wait_preroll (media))
2743 goto preroll_failed;
2745 g_rec_mutex_lock (&priv->state_lock);
2751 g_rec_mutex_unlock (&priv->state_lock);
2758 g_rec_mutex_unlock (&priv->state_lock);
2759 GST_WARNING ("failed to preroll pipeline");
2760 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2765 GST_WARNING ("failed to preroll pipeline");
2770 /* must be called with state-lock */
2772 media_set_pipeline_state_locked (GstRTSPMedia * media, GstState state)
2774 GstRTSPMediaPrivate *priv = media->priv;
2776 if (state == GST_STATE_NULL) {
2777 gst_rtsp_media_unprepare (media);
2779 GST_INFO ("state %s media %p", gst_element_state_get_name (state), media);
2780 set_target_state (media, state, FALSE);
2781 /* when we are buffering, don't update the state yet, this will be done
2782 * when buffering finishes */
2783 if (priv->buffering) {
2784 GST_INFO ("Buffering busy, delay state change");
2786 if (state == GST_STATE_PLAYING)
2787 /* make sure pads are not blocking anymore when going to PLAYING */
2788 media_streams_set_blocked (media, FALSE);
2790 set_state (media, state);
2792 /* and suspend after pause */
2793 if (state == GST_STATE_PAUSED)
2794 gst_rtsp_media_suspend (media);
2800 * gst_rtsp_media_set_pipeline_state:
2801 * @media: a #GstRTSPMedia
2802 * @state: the target state of the pipeline
2804 * Set the state of the pipeline managed by @media to @state
2807 gst_rtsp_media_set_pipeline_state (GstRTSPMedia * media, GstState state)
2809 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
2811 g_rec_mutex_lock (&media->priv->state_lock);
2812 media_set_pipeline_state_locked (media, state);
2813 g_rec_mutex_unlock (&media->priv->state_lock);
2817 * gst_rtsp_media_set_state:
2818 * @media: a #GstRTSPMedia
2819 * @state: the target state of the media
2820 * @transports: (transfer none) (element-type GstRtspServer.RTSPStreamTransport):
2821 * a #GPtrArray of #GstRTSPStreamTransport pointers
2823 * Set the state of @media to @state and for the transports in @transports.
2825 * @media must be prepared with gst_rtsp_media_prepare();
2827 * Returns: %TRUE on success.
2830 gst_rtsp_media_set_state (GstRTSPMedia * media, GstState state,
2831 GPtrArray * transports)
2833 GstRTSPMediaPrivate *priv;
2835 gboolean activate, deactivate, do_state;
2838 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2839 g_return_val_if_fail (transports != NULL, FALSE);
2843 g_rec_mutex_lock (&priv->state_lock);
2844 if (priv->status == GST_RTSP_MEDIA_STATUS_ERROR)
2846 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
2847 priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
2850 /* NULL and READY are the same */
2851 if (state == GST_STATE_READY)
2852 state = GST_STATE_NULL;
2854 activate = deactivate = FALSE;
2856 GST_INFO ("going to state %s media %p", gst_element_state_get_name (state),
2860 case GST_STATE_NULL:
2861 case GST_STATE_PAUSED:
2862 /* we're going from PLAYING to PAUSED, READY or NULL, deactivate */
2863 if (priv->target_state == GST_STATE_PLAYING)
2866 case GST_STATE_PLAYING:
2867 /* we're going to PLAYING, activate */
2873 old_active = priv->n_active;
2875 for (i = 0; i < transports->len; i++) {
2876 GstRTSPStreamTransport *trans;
2878 /* we need a non-NULL entry in the array */
2879 trans = g_ptr_array_index (transports, i);
2884 if (gst_rtsp_stream_transport_set_active (trans, TRUE))
2886 } else if (deactivate) {
2887 if (gst_rtsp_stream_transport_set_active (trans, FALSE))
2892 /* we just activated the first media, do the playing state change */
2893 if (old_active == 0 && activate)
2895 /* if we have no more active media, do the downward state changes */
2896 else if (priv->n_active == 0)
2901 GST_INFO ("state %d active %d media %p do_state %d", state, priv->n_active,
2904 if (priv->target_state != state) {
2906 media_set_pipeline_state_locked (media, state);
2908 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STATE], 0, state,
2912 /* remember where we are */
2913 if (state != GST_STATE_NULL && (state == GST_STATE_PAUSED ||
2914 old_active != priv->n_active))
2915 collect_media_stats (media);
2917 g_rec_mutex_unlock (&priv->state_lock);
2924 GST_WARNING ("media %p was not prepared", media);
2925 g_rec_mutex_unlock (&priv->state_lock);
2930 GST_WARNING ("media %p in error status while changing to state %d",
2932 if (state == GST_STATE_NULL) {
2933 for (i = 0; i < transports->len; i++) {
2934 GstRTSPStreamTransport *trans;
2936 /* we need a non-NULL entry in the array */
2937 trans = g_ptr_array_index (transports, i);
2941 gst_rtsp_stream_transport_set_active (trans, FALSE);
2945 g_rec_mutex_unlock (&priv->state_lock);