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;
84 GstRTSPLowerTrans protocols;
86 gboolean eos_shutdown;
88 GstRTSPAddressPool *pool;
91 GRecMutex state_lock; /* locking order: state lock, lock */
92 GPtrArray *streams; /* protected by lock */
93 GList *dynamic; /* protected by lock */
94 GstRTSPMediaStatus status; /* protected by lock */
99 /* the pipeline for the media */
100 GstElement *pipeline;
101 GstElement *fakesink; /* protected by lock */
104 GstRTSPThread *thread;
106 gboolean time_provider;
107 GstNetTimeProvider *nettime;
112 GstState target_state;
114 /* RTP session manager */
117 /* the range of media */
118 GstRTSPTimeRange range; /* protected by lock */
119 GstClockTime range_start;
120 GstClockTime range_stop;
123 #define DEFAULT_SHARED FALSE
124 #define DEFAULT_REUSABLE FALSE
125 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_TCP
126 //#define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP_MCAST
127 #define DEFAULT_EOS_SHUTDOWN FALSE
128 #define DEFAULT_BUFFER_SIZE 0x80000
129 #define DEFAULT_TIME_PROVIDER FALSE
131 /* define to dump received RTCP packets */
150 SIGNAL_REMOVED_STREAM,
157 GST_DEBUG_CATEGORY_STATIC (rtsp_media_debug);
158 #define GST_CAT_DEFAULT rtsp_media_debug
160 static void gst_rtsp_media_get_property (GObject * object, guint propid,
161 GValue * value, GParamSpec * pspec);
162 static void gst_rtsp_media_set_property (GObject * object, guint propid,
163 const GValue * value, GParamSpec * pspec);
164 static void gst_rtsp_media_finalize (GObject * obj);
166 static gboolean default_handle_message (GstRTSPMedia * media,
167 GstMessage * message);
168 static void finish_unprepare (GstRTSPMedia * media);
169 static gboolean default_unprepare (GstRTSPMedia * media);
171 default_convert_range (GstRTSPMedia * media, GstRTSPTimeRange * range,
172 GstRTSPRangeUnit unit);
173 static gboolean default_query_position (GstRTSPMedia * media,
175 static gboolean default_query_stop (GstRTSPMedia * media, gint64 * stop);
177 static guint gst_rtsp_media_signals[SIGNAL_LAST] = { 0 };
179 G_DEFINE_TYPE (GstRTSPMedia, gst_rtsp_media, G_TYPE_OBJECT);
182 gst_rtsp_media_class_init (GstRTSPMediaClass * klass)
184 GObjectClass *gobject_class;
186 g_type_class_add_private (klass, sizeof (GstRTSPMediaPrivate));
188 gobject_class = G_OBJECT_CLASS (klass);
190 gobject_class->get_property = gst_rtsp_media_get_property;
191 gobject_class->set_property = gst_rtsp_media_set_property;
192 gobject_class->finalize = gst_rtsp_media_finalize;
194 g_object_class_install_property (gobject_class, PROP_SHARED,
195 g_param_spec_boolean ("shared", "Shared",
196 "If this media pipeline can be shared", DEFAULT_SHARED,
197 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
199 g_object_class_install_property (gobject_class, PROP_REUSABLE,
200 g_param_spec_boolean ("reusable", "Reusable",
201 "If this media pipeline can be reused after an unprepare",
202 DEFAULT_REUSABLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
204 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
205 g_param_spec_flags ("protocols", "Protocols",
206 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
207 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
209 g_object_class_install_property (gobject_class, PROP_EOS_SHUTDOWN,
210 g_param_spec_boolean ("eos-shutdown", "EOS Shutdown",
211 "Send an EOS event to the pipeline before unpreparing",
212 DEFAULT_EOS_SHUTDOWN, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
214 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
215 g_param_spec_uint ("buffer-size", "Buffer Size",
216 "The kernel UDP buffer size to use", 0, G_MAXUINT,
217 DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
219 g_object_class_install_property (gobject_class, PROP_ELEMENT,
220 g_param_spec_object ("element", "The Element",
221 "The GstBin to use for streaming the media", GST_TYPE_ELEMENT,
222 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
224 g_object_class_install_property (gobject_class, PROP_TIME_PROVIDER,
225 g_param_spec_boolean ("time-provider", "Time Provider",
226 "Use a NetTimeProvider for clients",
227 DEFAULT_TIME_PROVIDER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
229 gst_rtsp_media_signals[SIGNAL_NEW_STREAM] =
230 g_signal_new ("new-stream", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
231 G_STRUCT_OFFSET (GstRTSPMediaClass, new_stream), NULL, NULL,
232 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_RTSP_STREAM);
234 gst_rtsp_media_signals[SIGNAL_REMOVED_STREAM] =
235 g_signal_new ("removed-stream", G_TYPE_FROM_CLASS (klass),
236 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPMediaClass, removed_stream),
237 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
238 GST_TYPE_RTSP_STREAM);
240 gst_rtsp_media_signals[SIGNAL_PREPARED] =
241 g_signal_new ("prepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
242 G_STRUCT_OFFSET (GstRTSPMediaClass, prepared), NULL, NULL,
243 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
245 gst_rtsp_media_signals[SIGNAL_UNPREPARED] =
246 g_signal_new ("unprepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
247 G_STRUCT_OFFSET (GstRTSPMediaClass, unprepared), NULL, NULL,
248 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
250 gst_rtsp_media_signals[SIGNAL_NEW_STATE] =
251 g_signal_new ("new-state", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
252 G_STRUCT_OFFSET (GstRTSPMediaClass, new_state), NULL, NULL,
253 g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
255 GST_DEBUG_CATEGORY_INIT (rtsp_media_debug, "rtspmedia", 0, "GstRTSPMedia");
257 klass->handle_message = default_handle_message;
258 klass->unprepare = default_unprepare;
259 klass->convert_range = default_convert_range;
260 klass->query_position = default_query_position;
261 klass->query_stop = default_query_stop;
265 gst_rtsp_media_init (GstRTSPMedia * media)
267 GstRTSPMediaPrivate *priv = GST_RTSP_MEDIA_GET_PRIVATE (media);
271 priv->streams = g_ptr_array_new_with_free_func (g_object_unref);
272 g_mutex_init (&priv->lock);
273 g_cond_init (&priv->cond);
274 g_rec_mutex_init (&priv->state_lock);
276 priv->shared = DEFAULT_SHARED;
277 priv->reusable = DEFAULT_REUSABLE;
278 priv->protocols = DEFAULT_PROTOCOLS;
279 priv->eos_shutdown = DEFAULT_EOS_SHUTDOWN;
280 priv->buffer_size = DEFAULT_BUFFER_SIZE;
281 priv->time_provider = DEFAULT_TIME_PROVIDER;
285 gst_rtsp_media_finalize (GObject * obj)
287 GstRTSPMediaPrivate *priv;
290 media = GST_RTSP_MEDIA (obj);
293 GST_INFO ("finalize media %p", media);
295 g_ptr_array_unref (priv->streams);
297 g_list_free_full (priv->dynamic, gst_object_unref);
300 gst_object_unref (priv->pipeline);
302 gst_object_unref (priv->nettime);
303 gst_object_unref (priv->element);
305 g_object_unref (priv->pool);
306 g_mutex_clear (&priv->lock);
307 g_cond_clear (&priv->cond);
308 g_rec_mutex_clear (&priv->state_lock);
310 G_OBJECT_CLASS (gst_rtsp_media_parent_class)->finalize (obj);
314 gst_rtsp_media_get_property (GObject * object, guint propid,
315 GValue * value, GParamSpec * pspec)
317 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
321 g_value_set_object (value, media->priv->element);
324 g_value_set_boolean (value, gst_rtsp_media_is_shared (media));
327 g_value_set_boolean (value, gst_rtsp_media_is_reusable (media));
330 g_value_set_flags (value, gst_rtsp_media_get_protocols (media));
332 case PROP_EOS_SHUTDOWN:
333 g_value_set_boolean (value, gst_rtsp_media_is_eos_shutdown (media));
335 case PROP_BUFFER_SIZE:
336 g_value_set_uint (value, gst_rtsp_media_get_buffer_size (media));
338 case PROP_TIME_PROVIDER:
339 g_value_set_boolean (value, gst_rtsp_media_is_time_provider (media));
342 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
347 gst_rtsp_media_set_property (GObject * object, guint propid,
348 const GValue * value, GParamSpec * pspec)
350 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
354 media->priv->element = g_value_get_object (value);
355 gst_object_ref_sink (media->priv->element);
358 gst_rtsp_media_set_shared (media, g_value_get_boolean (value));
361 gst_rtsp_media_set_reusable (media, g_value_get_boolean (value));
364 gst_rtsp_media_set_protocols (media, g_value_get_flags (value));
366 case PROP_EOS_SHUTDOWN:
367 gst_rtsp_media_set_eos_shutdown (media, g_value_get_boolean (value));
369 case PROP_BUFFER_SIZE:
370 gst_rtsp_media_set_buffer_size (media, g_value_get_uint (value));
372 case PROP_TIME_PROVIDER:
373 gst_rtsp_media_use_time_provider (media, g_value_get_boolean (value));
376 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
380 /* must be called with state lock */
382 collect_media_stats (GstRTSPMedia * media)
384 GstRTSPMediaPrivate *priv = media->priv;
385 gint64 position, stop;
387 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
388 priv->status != GST_RTSP_MEDIA_STATUS_PREPARING)
391 priv->range.unit = GST_RTSP_RANGE_NPT;
393 GST_INFO ("collect media stats");
396 priv->range.min.type = GST_RTSP_TIME_NOW;
397 priv->range.min.seconds = -1;
398 priv->range_start = -1;
399 priv->range.max.type = GST_RTSP_TIME_END;
400 priv->range.max.seconds = -1;
401 priv->range_stop = -1;
403 GstRTSPMediaClass *klass;
406 klass = GST_RTSP_MEDIA_GET_CLASS (media);
408 /* get the position */
410 if (klass->query_position)
411 ret = klass->query_position (media, &position);
414 GST_INFO ("position query failed");
418 /* get the current segment stop */
420 if (klass->query_stop)
421 ret = klass->query_stop (media, &stop);
424 GST_INFO ("stop query failed");
428 GST_INFO ("stats: position %" GST_TIME_FORMAT ", stop %"
429 GST_TIME_FORMAT, GST_TIME_ARGS (position), GST_TIME_ARGS (stop));
431 if (position == -1) {
432 priv->range.min.type = GST_RTSP_TIME_NOW;
433 priv->range.min.seconds = -1;
434 priv->range_start = -1;
436 priv->range.min.type = GST_RTSP_TIME_SECONDS;
437 priv->range.min.seconds = ((gdouble) position) / GST_SECOND;
438 priv->range_start = position;
441 priv->range.max.type = GST_RTSP_TIME_END;
442 priv->range.max.seconds = -1;
443 priv->range_stop = -1;
445 priv->range.max.type = GST_RTSP_TIME_SECONDS;
446 priv->range.max.seconds = ((gdouble) stop) / GST_SECOND;
447 priv->range_stop = stop;
453 * gst_rtsp_media_new:
454 * @element: (transfer full): a #GstElement
456 * Create a new #GstRTSPMedia instance. @element is the bin element that
457 * provides the different streams. The #GstRTSPMedia object contains the
458 * element to produce RTP data for one or more related (audio/video/..)
461 * Ownership is taken of @element.
463 * Returns: a new #GstRTSPMedia object.
466 gst_rtsp_media_new (GstElement * element)
468 GstRTSPMedia *result;
470 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
472 result = g_object_new (GST_TYPE_RTSP_MEDIA, "element", element, NULL);
478 * gst_rtsp_media_get_element:
479 * @media: a #GstRTSPMedia
481 * Get the element that was used when constructing @media.
483 * Returns: a #GstElement. Unref after usage.
486 gst_rtsp_media_get_element (GstRTSPMedia * media)
488 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
490 return gst_object_ref (media->priv->element);
494 * gst_rtsp_media_take_pipeline:
495 * @media: a #GstRTSPMedia
496 * @pipeline: (transfer full): a #GstPipeline
498 * Set @pipeline as the #GstPipeline for @media. Ownership is
499 * taken of @pipeline.
502 gst_rtsp_media_take_pipeline (GstRTSPMedia * media, GstPipeline * pipeline)
504 GstRTSPMediaPrivate *priv;
506 GstNetTimeProvider *nettime;
508 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
509 g_return_if_fail (GST_IS_PIPELINE (pipeline));
513 g_mutex_lock (&priv->lock);
514 old = priv->pipeline;
515 priv->pipeline = GST_ELEMENT_CAST (pipeline);
516 nettime = priv->nettime;
517 priv->nettime = NULL;
518 g_mutex_unlock (&priv->lock);
521 gst_object_unref (old);
524 gst_object_unref (nettime);
526 gst_bin_add (GST_BIN_CAST (pipeline), priv->element);
530 * gst_rtsp_media_set_permissions:
531 * @media: a #GstRTSPMedia
532 * @permissions: a #GstRTSPPermissions
534 * Set @permissions on @media.
537 gst_rtsp_media_set_permissions (GstRTSPMedia * media,
538 GstRTSPPermissions * permissions)
540 GstRTSPMediaPrivate *priv;
542 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
546 g_mutex_lock (&priv->lock);
547 if (priv->permissions)
548 gst_rtsp_permissions_unref (priv->permissions);
549 if ((priv->permissions = permissions))
550 gst_rtsp_permissions_ref (permissions);
551 g_mutex_unlock (&priv->lock);
555 * gst_rtsp_media_get_permissions:
556 * @media: a #GstRTSPMedia
558 * Get the permissions object from @media.
560 * Returns: (transfer full): a #GstRTSPPermissions object, unref after usage.
563 gst_rtsp_media_get_permissions (GstRTSPMedia * media)
565 GstRTSPMediaPrivate *priv;
566 GstRTSPPermissions *result;
568 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
572 g_mutex_lock (&priv->lock);
573 if ((result = priv->permissions))
574 gst_rtsp_permissions_ref (result);
575 g_mutex_unlock (&priv->lock);
581 * gst_rtsp_media_set_shared:
582 * @media: a #GstRTSPMedia
583 * @shared: the new value
585 * Set or unset if the pipeline for @media can be shared will multiple clients.
586 * When @shared is %TRUE, client requests for this media will share the media
590 gst_rtsp_media_set_shared (GstRTSPMedia * media, gboolean shared)
592 GstRTSPMediaPrivate *priv;
594 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
598 g_mutex_lock (&priv->lock);
599 priv->shared = shared;
600 g_mutex_unlock (&priv->lock);
604 * gst_rtsp_media_is_shared:
605 * @media: a #GstRTSPMedia
607 * Check if the pipeline for @media can be shared between multiple clients.
609 * Returns: %TRUE if the media can be shared between clients.
612 gst_rtsp_media_is_shared (GstRTSPMedia * media)
614 GstRTSPMediaPrivate *priv;
617 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
621 g_mutex_lock (&priv->lock);
623 g_mutex_unlock (&priv->lock);
629 * gst_rtsp_media_set_reusable:
630 * @media: a #GstRTSPMedia
631 * @reusable: the new value
633 * Set or unset if the pipeline for @media can be reused after the pipeline has
637 gst_rtsp_media_set_reusable (GstRTSPMedia * media, gboolean reusable)
639 GstRTSPMediaPrivate *priv;
641 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
645 g_mutex_lock (&priv->lock);
646 priv->reusable = reusable;
647 g_mutex_unlock (&priv->lock);
651 * gst_rtsp_media_is_reusable:
652 * @media: a #GstRTSPMedia
654 * Check if the pipeline for @media can be reused after an unprepare.
656 * Returns: %TRUE if the media can be reused
659 gst_rtsp_media_is_reusable (GstRTSPMedia * media)
661 GstRTSPMediaPrivate *priv;
664 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
668 g_mutex_lock (&priv->lock);
669 res = priv->reusable;
670 g_mutex_unlock (&priv->lock);
676 * gst_rtsp_media_set_protocols:
677 * @media: a #GstRTSPMedia
678 * @protocols: the new flags
680 * Configure the allowed lower transport for @media.
683 gst_rtsp_media_set_protocols (GstRTSPMedia * media, GstRTSPLowerTrans protocols)
685 GstRTSPMediaPrivate *priv;
687 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
691 g_mutex_lock (&priv->lock);
692 priv->protocols = protocols;
693 g_mutex_unlock (&priv->lock);
697 * gst_rtsp_media_get_protocols:
698 * @media: a #GstRTSPMedia
700 * Get the allowed protocols of @media.
702 * Returns: a #GstRTSPLowerTrans
705 gst_rtsp_media_get_protocols (GstRTSPMedia * media)
707 GstRTSPMediaPrivate *priv;
708 GstRTSPLowerTrans res;
710 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media),
711 GST_RTSP_LOWER_TRANS_UNKNOWN);
715 g_mutex_lock (&priv->lock);
716 res = priv->protocols;
717 g_mutex_unlock (&priv->lock);
723 * gst_rtsp_media_set_eos_shutdown:
724 * @media: a #GstRTSPMedia
725 * @eos_shutdown: the new value
727 * Set or unset if an EOS event will be sent to the pipeline for @media before
731 gst_rtsp_media_set_eos_shutdown (GstRTSPMedia * media, gboolean eos_shutdown)
733 GstRTSPMediaPrivate *priv;
735 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
739 g_mutex_lock (&priv->lock);
740 priv->eos_shutdown = eos_shutdown;
741 g_mutex_unlock (&priv->lock);
745 * gst_rtsp_media_is_eos_shutdown:
746 * @media: a #GstRTSPMedia
748 * Check if the pipeline for @media will send an EOS down the pipeline before
751 * Returns: %TRUE if the media will send EOS before unpreparing.
754 gst_rtsp_media_is_eos_shutdown (GstRTSPMedia * media)
756 GstRTSPMediaPrivate *priv;
759 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
763 g_mutex_lock (&priv->lock);
764 res = priv->eos_shutdown;
765 g_mutex_unlock (&priv->lock);
771 * gst_rtsp_media_set_buffer_size:
772 * @media: a #GstRTSPMedia
773 * @size: the new value
775 * Set the kernel UDP buffer size.
778 gst_rtsp_media_set_buffer_size (GstRTSPMedia * media, guint size)
780 GstRTSPMediaPrivate *priv;
782 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
784 GST_LOG_OBJECT (media, "set buffer size %u", size);
788 g_mutex_lock (&priv->lock);
789 priv->buffer_size = size;
790 g_mutex_unlock (&priv->lock);
794 * gst_rtsp_media_get_buffer_size:
795 * @media: a #GstRTSPMedia
797 * Get the kernel UDP buffer size.
799 * Returns: the kernel UDP buffer size.
802 gst_rtsp_media_get_buffer_size (GstRTSPMedia * media)
804 GstRTSPMediaPrivate *priv;
807 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
811 g_mutex_unlock (&priv->lock);
812 res = priv->buffer_size;
813 g_mutex_unlock (&priv->lock);
819 * gst_rtsp_media_use_time_provider:
820 * @media: a #GstRTSPMedia
821 * @time_provider: if a #GstNetTimeProvider should be used
823 * Set @media to provide a #GstNetTimeProvider.
826 gst_rtsp_media_use_time_provider (GstRTSPMedia * media, gboolean time_provider)
828 GstRTSPMediaPrivate *priv;
830 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
834 g_mutex_lock (&priv->lock);
835 priv->time_provider = time_provider;
836 g_mutex_unlock (&priv->lock);
840 * gst_rtsp_media_is_time_provider:
841 * @media: a #GstRTSPMedia
843 * Check if @media can provide a #GstNetTimeProvider for its pipeline clock.
845 * Use gst_rtsp_media_get_time_provider() to get the network clock.
847 * Returns: %TRUE if @media can provide a #GstNetTimeProvider.
850 gst_rtsp_media_is_time_provider (GstRTSPMedia * media)
852 GstRTSPMediaPrivate *priv;
855 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
859 g_mutex_unlock (&priv->lock);
860 res = priv->time_provider;
861 g_mutex_unlock (&priv->lock);
867 * gst_rtsp_media_set_address_pool:
868 * @media: a #GstRTSPMedia
869 * @pool: a #GstRTSPAddressPool
871 * configure @pool to be used as the address pool of @media.
874 gst_rtsp_media_set_address_pool (GstRTSPMedia * media,
875 GstRTSPAddressPool * pool)
877 GstRTSPMediaPrivate *priv;
878 GstRTSPAddressPool *old;
880 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
884 GST_LOG_OBJECT (media, "set address pool %p", pool);
886 g_mutex_lock (&priv->lock);
887 if ((old = priv->pool) != pool)
888 priv->pool = pool ? g_object_ref (pool) : NULL;
891 g_ptr_array_foreach (priv->streams, (GFunc) gst_rtsp_stream_set_address_pool,
893 g_mutex_unlock (&priv->lock);
896 g_object_unref (old);
900 * gst_rtsp_media_get_address_pool:
901 * @media: a #GstRTSPMedia
903 * Get the #GstRTSPAddressPool used as the address pool of @media.
905 * Returns: (transfer full): the #GstRTSPAddressPool of @media. g_object_unref() after
909 gst_rtsp_media_get_address_pool (GstRTSPMedia * media)
911 GstRTSPMediaPrivate *priv;
912 GstRTSPAddressPool *result;
914 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
918 g_mutex_lock (&priv->lock);
919 if ((result = priv->pool))
920 g_object_ref (result);
921 g_mutex_unlock (&priv->lock);
927 * gst_rtsp_media_collect_streams:
928 * @media: a #GstRTSPMedia
930 * Find all payloader elements, they should be named pay%d in the
931 * element of @media, and create #GstRTSPStreams for them.
933 * Collect all dynamic elements, named dynpay%d, and add them to
934 * the list of dynamic elements.
937 gst_rtsp_media_collect_streams (GstRTSPMedia * media)
939 GstRTSPMediaPrivate *priv;
940 GstElement *element, *elem;
945 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
948 element = priv->element;
951 for (i = 0; have_elem; i++) {
956 name = g_strdup_printf ("pay%d", i);
957 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
958 GST_INFO ("found stream %d with payloader %p", i, elem);
960 /* take the pad of the payloader */
961 pad = gst_element_get_static_pad (elem, "src");
962 /* create the stream */
963 gst_rtsp_media_create_stream (media, elem, pad);
964 gst_object_unref (pad);
965 gst_object_unref (elem);
971 name = g_strdup_printf ("dynpay%d", i);
972 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
973 /* a stream that will dynamically create pads to provide RTP packets */
975 GST_INFO ("found dynamic element %d, %p", i, elem);
977 g_mutex_lock (&priv->lock);
978 priv->dynamic = g_list_prepend (priv->dynamic, elem);
979 g_mutex_unlock (&priv->lock);
988 * gst_rtsp_media_create_stream:
989 * @media: a #GstRTSPMedia
990 * @payloader: a #GstElement
991 * @srcpad: a source #GstPad
993 * Create a new stream in @media that provides RTP data on @srcpad.
994 * @srcpad should be a pad of an element inside @media->element.
996 * Returns: (transfer none): a new #GstRTSPStream that remains valid for as long
1000 gst_rtsp_media_create_stream (GstRTSPMedia * media, GstElement * payloader,
1003 GstRTSPMediaPrivate *priv;
1004 GstRTSPStream *stream;
1009 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1010 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
1011 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1012 g_return_val_if_fail (GST_PAD_IS_SRC (pad), NULL);
1016 g_mutex_lock (&priv->lock);
1017 idx = priv->streams->len;
1019 GST_DEBUG ("media %p: creating stream with index %d", media, idx);
1021 name = g_strdup_printf ("src_%u", idx);
1022 srcpad = gst_ghost_pad_new (name, pad);
1023 gst_pad_set_active (srcpad, TRUE);
1024 gst_element_add_pad (priv->element, srcpad);
1027 stream = gst_rtsp_stream_new (idx, payloader, srcpad);
1029 gst_rtsp_stream_set_address_pool (stream, priv->pool);
1031 g_ptr_array_add (priv->streams, stream);
1032 g_mutex_unlock (&priv->lock);
1034 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STREAM], 0, stream,
1041 gst_rtsp_media_remove_stream (GstRTSPMedia * media, GstRTSPStream * stream)
1043 GstRTSPMediaPrivate *priv;
1048 g_mutex_lock (&priv->lock);
1049 /* remove the ghostpad */
1050 srcpad = gst_rtsp_stream_get_srcpad (stream);
1051 gst_element_remove_pad (priv->element, srcpad);
1052 gst_object_unref (srcpad);
1053 /* now remove the stream */
1054 g_object_ref (stream);
1055 g_ptr_array_remove (priv->streams, stream);
1056 g_mutex_unlock (&priv->lock);
1058 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_REMOVED_STREAM], 0,
1061 g_object_unref (stream);
1065 * gst_rtsp_media_n_streams:
1066 * @media: a #GstRTSPMedia
1068 * Get the number of streams in this media.
1070 * Returns: The number of streams.
1073 gst_rtsp_media_n_streams (GstRTSPMedia * media)
1075 GstRTSPMediaPrivate *priv;
1078 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), 0);
1082 g_mutex_lock (&priv->lock);
1083 res = priv->streams->len;
1084 g_mutex_unlock (&priv->lock);
1090 * gst_rtsp_media_get_stream:
1091 * @media: a #GstRTSPMedia
1092 * @idx: the stream index
1094 * Retrieve the stream with index @idx from @media.
1096 * Returns: (transfer none): the #GstRTSPStream at index @idx or %NULL when a stream with
1097 * that index did not exist.
1100 gst_rtsp_media_get_stream (GstRTSPMedia * media, guint idx)
1102 GstRTSPMediaPrivate *priv;
1105 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1109 g_mutex_lock (&priv->lock);
1110 if (idx < priv->streams->len)
1111 res = g_ptr_array_index (priv->streams, idx);
1114 g_mutex_unlock (&priv->lock);
1120 * gst_rtsp_media_find_stream:
1121 * @media: a #GstRTSPMedia
1122 * @control: the control of the stream
1124 * Find a stream in @media with @control as the control uri.
1126 * Returns: (transfer none): the #GstRTSPStream with control uri @control
1127 * or %NULL when a stream with that control did not exist.
1130 gst_rtsp_media_find_stream (GstRTSPMedia * media, const gchar * control)
1132 GstRTSPMediaPrivate *priv;
1136 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1137 g_return_val_if_fail (control != NULL, NULL);
1143 g_mutex_lock (&priv->lock);
1144 for (i = 0; i < priv->streams->len; i++) {
1145 GstRTSPStream *test;
1147 test = g_ptr_array_index (priv->streams, i);
1148 if (gst_rtsp_stream_has_control (test, control)) {
1153 g_mutex_unlock (&priv->lock);
1159 * gst_rtsp_media_get_range_string:
1160 * @media: a #GstRTSPMedia
1161 * @play: for the PLAY request
1162 * @unit: the unit to use for the string
1164 * Get the current range as a string. @media must be prepared with
1165 * gst_rtsp_media_prepare ().
1167 * Returns: The range as a string, g_free() after usage.
1170 gst_rtsp_media_get_range_string (GstRTSPMedia * media, gboolean play,
1171 GstRTSPRangeUnit unit)
1173 GstRTSPMediaClass *klass;
1174 GstRTSPMediaPrivate *priv;
1176 GstRTSPTimeRange range;
1178 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1179 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1180 g_return_val_if_fail (klass->convert_range != NULL, FALSE);
1184 g_rec_mutex_lock (&priv->state_lock);
1185 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
1188 g_mutex_lock (&priv->lock);
1190 /* Update the range value with current position/duration */
1191 collect_media_stats (media);
1194 range = priv->range;
1196 if (!play && priv->n_active > 0) {
1197 range.min.type = GST_RTSP_TIME_NOW;
1198 range.min.seconds = -1;
1200 g_mutex_unlock (&priv->lock);
1201 g_rec_mutex_unlock (&priv->state_lock);
1203 if (!klass->convert_range (media, &range, unit))
1204 goto conversion_failed;
1206 result = gst_rtsp_range_to_string (&range);
1213 GST_WARNING ("media %p was not prepared", media);
1214 g_rec_mutex_unlock (&priv->state_lock);
1219 GST_WARNING ("range conversion to unit %d failed", unit);
1225 * gst_rtsp_media_seek:
1226 * @media: a #GstRTSPMedia
1227 * @range: a #GstRTSPTimeRange
1229 * Seek the pipeline of @media to @range. @media must be prepared with
1230 * gst_rtsp_media_prepare().
1232 * Returns: %TRUE on success.
1235 gst_rtsp_media_seek (GstRTSPMedia * media, GstRTSPTimeRange * range)
1237 GstRTSPMediaClass *klass;
1238 GstRTSPMediaPrivate *priv;
1241 GstClockTime start, stop;
1242 GstSeekType start_type, stop_type;
1244 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1246 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1247 g_return_val_if_fail (range != NULL, FALSE);
1248 g_return_val_if_fail (klass->convert_range != NULL, FALSE);
1252 g_rec_mutex_lock (&priv->state_lock);
1253 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
1256 if (!priv->seekable)
1259 /* depends on the current playing state of the pipeline. We might need to
1260 * queue this until we get EOS. */
1261 flags = GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_KEY_UNIT;
1263 start_type = stop_type = GST_SEEK_TYPE_NONE;
1265 if (!klass->convert_range (media, range, GST_RTSP_RANGE_NPT))
1267 gst_rtsp_range_get_times (range, &start, &stop);
1269 GST_INFO ("got %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1270 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1271 GST_INFO ("current %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1272 GST_TIME_ARGS (priv->range_start), GST_TIME_ARGS (priv->range_stop));
1274 if (priv->range_start == start)
1275 start = GST_CLOCK_TIME_NONE;
1276 else if (start != GST_CLOCK_TIME_NONE)
1277 start_type = GST_SEEK_TYPE_SET;
1279 if (priv->range_stop == stop)
1280 stop = GST_CLOCK_TIME_NONE;
1281 else if (stop != GST_CLOCK_TIME_NONE)
1282 stop_type = GST_SEEK_TYPE_SET;
1284 if (start != GST_CLOCK_TIME_NONE || stop != GST_CLOCK_TIME_NONE) {
1285 GST_INFO ("seeking to %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1286 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1288 res = gst_element_seek (priv->pipeline, 1.0, GST_FORMAT_TIME,
1289 flags, start_type, start, stop_type, stop);
1291 /* and block for the seek to complete */
1292 GST_INFO ("done seeking %d", res);
1293 gst_element_get_state (priv->pipeline, NULL, NULL, -1);
1294 GST_INFO ("prerolled again");
1296 collect_media_stats (media);
1298 GST_INFO ("no seek needed");
1301 g_rec_mutex_unlock (&priv->state_lock);
1308 g_rec_mutex_unlock (&priv->state_lock);
1309 GST_INFO ("media %p is not prepared", media);
1314 g_rec_mutex_unlock (&priv->state_lock);
1315 GST_INFO ("pipeline is not seekable");
1320 g_rec_mutex_unlock (&priv->state_lock);
1321 GST_WARNING ("conversion to npt not supported");
1327 gst_rtsp_media_set_status (GstRTSPMedia * media, GstRTSPMediaStatus status)
1329 GstRTSPMediaPrivate *priv = media->priv;
1331 g_mutex_lock (&priv->lock);
1332 priv->status = status;
1333 GST_DEBUG ("setting new status to %d", status);
1334 g_cond_broadcast (&priv->cond);
1335 g_mutex_unlock (&priv->lock);
1339 * gst_rtsp_media_get_status:
1340 * @media: a #GstRTSPMedia
1342 * Get the status of @media. When @media is busy preparing, this function waits
1343 * until @media is prepared or in error.
1345 * Returns: the status of @media.
1348 gst_rtsp_media_get_status (GstRTSPMedia * media)
1350 GstRTSPMediaPrivate *priv = media->priv;
1351 GstRTSPMediaStatus result;
1354 g_mutex_lock (&priv->lock);
1355 end_time = g_get_monotonic_time () + 20 * G_TIME_SPAN_SECOND;
1356 /* while we are preparing, wait */
1357 while (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING) {
1358 GST_DEBUG ("waiting for status change");
1359 if (!g_cond_wait_until (&priv->cond, &priv->lock, end_time)) {
1360 GST_DEBUG ("timeout, assuming error status");
1361 priv->status = GST_RTSP_MEDIA_STATUS_ERROR;
1364 /* could be success or error */
1365 result = priv->status;
1366 GST_DEBUG ("got status %d", result);
1367 g_mutex_unlock (&priv->lock);
1372 /* called with state-lock */
1374 default_handle_message (GstRTSPMedia * media, GstMessage * message)
1376 GstRTSPMediaPrivate *priv = media->priv;
1377 GstMessageType type;
1379 type = GST_MESSAGE_TYPE (message);
1382 case GST_MESSAGE_STATE_CHANGED:
1384 case GST_MESSAGE_BUFFERING:
1388 gst_message_parse_buffering (message, &percent);
1390 /* no state management needed for live pipelines */
1394 if (percent == 100) {
1395 /* a 100% message means buffering is done */
1396 priv->buffering = FALSE;
1397 /* if the desired state is playing, go back */
1398 if (priv->target_state == GST_STATE_PLAYING) {
1399 GST_INFO ("Buffering done, setting pipeline to PLAYING");
1400 gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
1402 GST_INFO ("Buffering done");
1405 /* buffering busy */
1406 if (priv->buffering == FALSE) {
1407 if (priv->target_state == GST_STATE_PLAYING) {
1408 /* we were not buffering but PLAYING, PAUSE the pipeline. */
1409 GST_INFO ("Buffering, setting pipeline to PAUSED ...");
1410 gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
1412 GST_INFO ("Buffering ...");
1415 priv->buffering = TRUE;
1419 case GST_MESSAGE_LATENCY:
1421 gst_bin_recalculate_latency (GST_BIN_CAST (priv->pipeline));
1424 case GST_MESSAGE_ERROR:
1429 gst_message_parse_error (message, &gerror, &debug);
1430 GST_WARNING ("%p: got error %s (%s)", media, gerror->message, debug);
1431 g_error_free (gerror);
1434 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
1437 case GST_MESSAGE_WARNING:
1442 gst_message_parse_warning (message, &gerror, &debug);
1443 GST_WARNING ("%p: got warning %s (%s)", media, gerror->message, debug);
1444 g_error_free (gerror);
1448 case GST_MESSAGE_ELEMENT:
1450 case GST_MESSAGE_STREAM_STATUS:
1452 case GST_MESSAGE_ASYNC_DONE:
1454 /* when we are dynamically adding pads, the addition of the udpsrc will
1455 * temporarily produce ASYNC_DONE messages. We have to ignore them and
1456 * wait for the final ASYNC_DONE after everything prerolled */
1457 GST_INFO ("%p: ignoring ASYNC_DONE", media);
1459 GST_INFO ("%p: got ASYNC_DONE", media);
1460 collect_media_stats (media);
1462 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
1463 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
1466 case GST_MESSAGE_EOS:
1467 GST_INFO ("%p: got EOS", media);
1469 if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARING) {
1470 GST_DEBUG ("shutting down after EOS");
1471 finish_unprepare (media);
1475 GST_INFO ("%p: got message type %d (%s)", media, type,
1476 gst_message_type_get_name (type));
1483 bus_message (GstBus * bus, GstMessage * message, GstRTSPMedia * media)
1485 GstRTSPMediaPrivate *priv = media->priv;
1486 GstRTSPMediaClass *klass;
1489 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1491 g_rec_mutex_lock (&priv->state_lock);
1492 if (klass->handle_message)
1493 ret = klass->handle_message (media, message);
1496 g_rec_mutex_unlock (&priv->state_lock);
1502 watch_destroyed (GstRTSPMedia * media)
1504 GST_DEBUG_OBJECT (media, "source destroyed");
1505 g_object_unref (media);
1508 /* called from streaming threads */
1510 pad_added_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
1512 GstRTSPMediaPrivate *priv = media->priv;
1513 GstRTSPStream *stream;
1515 /* FIXME, element is likely not a payloader, find the payloader here */
1516 stream = gst_rtsp_media_create_stream (media, element, pad);
1518 g_object_set_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream", stream);
1520 GST_INFO ("pad added %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
1522 g_rec_mutex_lock (&priv->state_lock);
1523 /* we will be adding elements below that will cause ASYNC_DONE to be
1524 * posted in the bus. We want to ignore those messages until the
1525 * pipeline really prerolled. */
1526 priv->adding = TRUE;
1528 /* join the element in the PAUSED state because this callback is
1529 * called from the streaming thread and it is PAUSED */
1530 gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
1531 priv->rtpbin, GST_STATE_PAUSED);
1533 priv->adding = FALSE;
1534 g_rec_mutex_unlock (&priv->state_lock);
1538 pad_removed_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
1540 GstRTSPMediaPrivate *priv = media->priv;
1541 GstRTSPStream *stream;
1543 stream = g_object_get_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream");
1547 GST_INFO ("pad removed %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
1549 g_rec_mutex_lock (&priv->state_lock);
1550 gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
1551 g_rec_mutex_unlock (&priv->state_lock);
1553 gst_rtsp_media_remove_stream (media, stream);
1557 remove_fakesink (GstRTSPMediaPrivate * priv)
1559 GstElement *fakesink;
1561 g_mutex_lock (&priv->lock);
1562 if ((fakesink = priv->fakesink))
1563 gst_object_ref (fakesink);
1564 priv->fakesink = NULL;
1565 g_mutex_unlock (&priv->lock);
1568 gst_bin_remove (GST_BIN (priv->pipeline), fakesink);
1569 gst_element_set_state (fakesink, GST_STATE_NULL);
1570 gst_object_unref (fakesink);
1571 GST_INFO ("removed fakesink");
1576 no_more_pads_cb (GstElement * element, GstRTSPMedia * media)
1578 GstRTSPMediaPrivate *priv = media->priv;
1580 GST_INFO ("no more pads");
1581 remove_fakesink (priv);
1584 typedef struct _DynPaySignalHandlers DynPaySignalHandlers;
1586 struct _DynPaySignalHandlers
1588 gulong pad_added_handler;
1589 gulong pad_removed_handler;
1590 gulong no_more_pads_handler;
1594 start_prepare (GstRTSPMedia * media)
1596 GstRTSPMediaPrivate *priv = media->priv;
1597 GstStateChangeReturn ret;
1601 /* link streams we already have, other streams might appear when we have
1602 * dynamic elements */
1603 for (i = 0; i < priv->streams->len; i++) {
1604 GstRTSPStream *stream;
1606 stream = g_ptr_array_index (priv->streams, i);
1608 gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
1609 priv->rtpbin, GST_STATE_NULL);
1612 for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
1613 GstElement *elem = walk->data;
1614 DynPaySignalHandlers *handlers = g_slice_new (DynPaySignalHandlers);
1616 GST_INFO ("adding callbacks for dynamic element %p", elem);
1618 handlers->pad_added_handler = g_signal_connect (elem, "pad-added",
1619 (GCallback) pad_added_cb, media);
1620 handlers->pad_removed_handler = g_signal_connect (elem, "pad-removed",
1621 (GCallback) pad_removed_cb, media);
1622 handlers->no_more_pads_handler = g_signal_connect (elem, "no-more-pads",
1623 (GCallback) no_more_pads_cb, media);
1625 g_object_set_data (G_OBJECT (elem), "gst-rtsp-dynpay-handlers", handlers);
1627 /* we add a fakesink here in order to make the state change async. We remove
1628 * the fakesink again in the no-more-pads callback. */
1629 priv->fakesink = gst_element_factory_make ("fakesink", "fakesink");
1630 gst_bin_add (GST_BIN (priv->pipeline), priv->fakesink);
1633 GST_INFO ("setting pipeline to PAUSED for media %p", media);
1634 /* first go to PAUSED */
1635 ret = gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
1636 priv->target_state = GST_STATE_PAUSED;
1639 case GST_STATE_CHANGE_SUCCESS:
1640 GST_INFO ("SUCCESS state change for media %p", media);
1641 priv->seekable = TRUE;
1643 case GST_STATE_CHANGE_ASYNC:
1644 GST_INFO ("ASYNC state change for media %p", media);
1645 priv->seekable = TRUE;
1647 case GST_STATE_CHANGE_NO_PREROLL:
1648 /* we need to go to PLAYING */
1649 GST_INFO ("NO_PREROLL state change: live media %p", media);
1650 /* FIXME we disable seeking for live streams for now. We should perform a
1651 * seeking query in preroll instead */
1652 priv->seekable = FALSE;
1653 priv->is_live = TRUE;
1654 ret = gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
1655 if (ret == GST_STATE_CHANGE_FAILURE)
1658 case GST_STATE_CHANGE_FAILURE:
1666 GST_WARNING ("failed to preroll pipeline");
1667 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
1673 * gst_rtsp_media_prepare:
1674 * @media: a #GstRTSPMedia
1675 * @thread: a #GstRTSPThread to run the bus handler or %NULL
1677 * Prepare @media for streaming. This function will create the objects
1678 * to manage the streaming. A pipeline must have been set on @media with
1679 * gst_rtsp_media_take_pipeline().
1681 * It will preroll the pipeline and collect vital information about the streams
1682 * such as the duration.
1684 * Returns: %TRUE on success.
1687 gst_rtsp_media_prepare (GstRTSPMedia * media, GstRTSPThread * thread)
1689 GstRTSPMediaPrivate *priv;
1690 GstRTSPMediaStatus status;
1694 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1695 g_return_val_if_fail (GST_IS_RTSP_THREAD (thread), FALSE);
1699 g_rec_mutex_lock (&priv->state_lock);
1700 priv->prepare_count++;
1702 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED)
1705 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
1708 if (priv->status != GST_RTSP_MEDIA_STATUS_UNPREPARED)
1709 goto not_unprepared;
1711 if (!priv->reusable && priv->reused)
1714 priv->rtpbin = gst_element_factory_make ("rtpbin", NULL);
1715 if (priv->rtpbin != NULL) {
1716 GstRTSPMediaClass *klass;
1717 gboolean success = TRUE;
1719 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1720 if (klass->setup_rtpbin)
1721 success = klass->setup_rtpbin (media, priv->rtpbin);
1723 if (success == FALSE) {
1724 gst_object_unref (priv->rtpbin);
1725 priv->rtpbin = NULL;
1728 if (priv->rtpbin == NULL)
1731 GST_INFO ("preparing media %p", media);
1733 /* reset some variables */
1734 priv->is_live = FALSE;
1735 priv->seekable = FALSE;
1736 priv->buffering = FALSE;
1737 priv->thread = thread;
1738 /* we're preparing now */
1739 priv->status = GST_RTSP_MEDIA_STATUS_PREPARING;
1741 bus = gst_pipeline_get_bus (GST_PIPELINE_CAST (priv->pipeline));
1743 /* add the pipeline bus to our custom mainloop */
1744 priv->source = gst_bus_create_watch (bus);
1745 gst_object_unref (bus);
1747 g_source_set_callback (priv->source, (GSourceFunc) bus_message,
1748 g_object_ref (media), (GDestroyNotify) watch_destroyed);
1750 priv->id = g_source_attach (priv->source, thread->context);
1752 /* add stuff to the bin */
1753 gst_bin_add (GST_BIN (priv->pipeline), priv->rtpbin);
1755 /* do remainder in context */
1756 source = g_idle_source_new ();
1757 g_source_set_callback (source, (GSourceFunc) start_prepare, media, NULL);
1758 g_source_attach (source, thread->context);
1759 g_source_unref (source);
1762 g_rec_mutex_unlock (&priv->state_lock);
1764 /* now wait for all pads to be prerolled, FIXME, we should somehow be
1765 * able to do this async so that we don't block the server thread. */
1766 status = gst_rtsp_media_get_status (media);
1767 if (status == GST_RTSP_MEDIA_STATUS_ERROR)
1770 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_PREPARED], 0, NULL);
1772 GST_INFO ("object %p is prerolled", media);
1779 GST_LOG ("media %p was prepared", media);
1780 g_rec_mutex_unlock (&priv->state_lock);
1786 GST_WARNING ("media %p was not unprepared", media);
1787 priv->prepare_count--;
1788 g_rec_mutex_unlock (&priv->state_lock);
1793 priv->prepare_count--;
1794 g_rec_mutex_unlock (&priv->state_lock);
1795 GST_WARNING ("can not reuse media %p", media);
1800 priv->prepare_count--;
1801 g_rec_mutex_unlock (&priv->state_lock);
1802 GST_WARNING ("no rtpbin element");
1803 g_warning ("failed to create element 'rtpbin', check your installation");
1808 GST_WARNING ("failed to preroll pipeline");
1809 gst_rtsp_media_unprepare (media);
1814 /* must be called with state-lock */
1816 finish_unprepare (GstRTSPMedia * media)
1818 GstRTSPMediaPrivate *priv = media->priv;
1822 GST_DEBUG ("shutting down");
1824 gst_element_set_state (priv->pipeline, GST_STATE_NULL);
1825 remove_fakesink (priv);
1827 for (i = 0; i < priv->streams->len; i++) {
1828 GstRTSPStream *stream;
1830 GST_INFO ("Removing elements of stream %d from pipeline", i);
1832 stream = g_ptr_array_index (priv->streams, i);
1834 gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
1837 /* remove the pad signal handlers */
1838 for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
1839 GstElement *elem = walk->data;
1840 DynPaySignalHandlers *handlers;
1843 g_object_steal_data (G_OBJECT (elem), "gst-rtsp-dynpay-handlers");
1844 g_assert (handlers != NULL);
1846 g_signal_handler_disconnect (G_OBJECT (elem), handlers->pad_added_handler);
1847 g_signal_handler_disconnect (G_OBJECT (elem),
1848 handlers->pad_removed_handler);
1849 g_signal_handler_disconnect (G_OBJECT (elem),
1850 handlers->no_more_pads_handler);
1852 g_slice_free (DynPaySignalHandlers, handlers);
1855 gst_bin_remove (GST_BIN (priv->pipeline), priv->rtpbin);
1856 priv->rtpbin = NULL;
1859 gst_object_unref (priv->nettime);
1860 priv->nettime = NULL;
1862 priv->reused = TRUE;
1863 priv->status = GST_RTSP_MEDIA_STATUS_UNPREPARED;
1865 /* when the media is not reusable, this will effectively unref the media and
1867 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_UNPREPARED], 0, NULL);
1869 /* the source has the last ref to the media */
1871 GST_DEBUG ("destroy source");
1872 g_source_destroy (priv->source);
1873 g_source_unref (priv->source);
1876 GST_DEBUG ("stop thread");
1877 gst_rtsp_thread_stop (priv->thread);
1881 /* called with state-lock */
1883 default_unprepare (GstRTSPMedia * media)
1885 GstRTSPMediaPrivate *priv = media->priv;
1887 if (priv->eos_shutdown) {
1888 GST_DEBUG ("sending EOS for shutdown");
1889 /* ref so that we don't disappear */
1890 gst_element_send_event (priv->pipeline, gst_event_new_eos ());
1891 /* we need to go to playing again for the EOS to propagate, normally in this
1892 * state, nothing is receiving data from us anymore so this is ok. */
1893 gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
1894 priv->status = GST_RTSP_MEDIA_STATUS_UNPREPARING;
1896 finish_unprepare (media);
1902 * gst_rtsp_media_unprepare:
1903 * @media: a #GstRTSPMedia
1905 * Unprepare @media. After this call, the media should be prepared again before
1906 * it can be used again. If the media is set to be non-reusable, a new instance
1909 * Returns: %TRUE on success.
1912 gst_rtsp_media_unprepare (GstRTSPMedia * media)
1914 GstRTSPMediaPrivate *priv;
1917 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1921 g_rec_mutex_lock (&priv->state_lock);
1922 if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARED)
1923 goto was_unprepared;
1925 priv->prepare_count--;
1926 if (priv->prepare_count > 0)
1929 GST_INFO ("unprepare media %p", media);
1930 priv->target_state = GST_STATE_NULL;
1933 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED) {
1934 GstRTSPMediaClass *klass;
1936 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1937 if (klass->unprepare)
1938 success = klass->unprepare (media);
1940 finish_unprepare (media);
1942 g_rec_mutex_unlock (&priv->state_lock);
1948 g_rec_mutex_unlock (&priv->state_lock);
1949 GST_INFO ("media %p was already unprepared", media);
1954 GST_INFO ("media %p still prepared %d times", media, priv->prepare_count);
1955 g_rec_mutex_unlock (&priv->state_lock);
1960 /* should be called with state-lock */
1962 get_clock_unlocked (GstRTSPMedia * media)
1964 if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED) {
1965 GST_DEBUG_OBJECT (media, "media was not prepared");
1968 return gst_pipeline_get_clock (GST_PIPELINE_CAST (media->priv->pipeline));
1972 * gst_rtsp_media_get_clock:
1973 * @media: a #GstRTSPMedia
1975 * Get the clock that is used by the pipeline in @media.
1977 * @media must be prepared before this method returns a valid clock object.
1979 * Returns: the #GstClock used by @media. unref after usage.
1982 gst_rtsp_media_get_clock (GstRTSPMedia * media)
1985 GstRTSPMediaPrivate *priv;
1987 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1991 g_rec_mutex_lock (&priv->state_lock);
1992 clock = get_clock_unlocked (media);
1993 g_rec_mutex_unlock (&priv->state_lock);
1999 * gst_rtsp_media_get_base_time:
2000 * @media: a #GstRTSPMedia
2002 * Get the base_time that is used by the pipeline in @media.
2004 * @media must be prepared before this method returns a valid base_time.
2006 * Returns: the base_time used by @media.
2009 gst_rtsp_media_get_base_time (GstRTSPMedia * media)
2011 GstClockTime result;
2012 GstRTSPMediaPrivate *priv;
2014 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_CLOCK_TIME_NONE);
2018 g_rec_mutex_lock (&priv->state_lock);
2019 if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
2022 result = gst_element_get_base_time (media->priv->pipeline);
2023 g_rec_mutex_unlock (&priv->state_lock);
2030 g_rec_mutex_unlock (&priv->state_lock);
2031 GST_DEBUG_OBJECT (media, "media was not prepared");
2032 return GST_CLOCK_TIME_NONE;
2037 * gst_rtsp_media_get_time_provider:
2038 * @media: a #GstRTSPMedia
2039 * @address: an address or NULL
2040 * @port: a port or 0
2042 * Get the #GstNetTimeProvider for the clock used by @media. The time provider
2043 * will listen on @address and @port for client time requests.
2045 * Returns: the #GstNetTimeProvider of @media.
2047 GstNetTimeProvider *
2048 gst_rtsp_media_get_time_provider (GstRTSPMedia * media, const gchar * address,
2051 GstRTSPMediaPrivate *priv;
2052 GstNetTimeProvider *provider = NULL;
2054 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
2058 g_rec_mutex_lock (&priv->state_lock);
2059 if (priv->time_provider) {
2060 if ((provider = priv->nettime) == NULL) {
2063 if (priv->time_provider && (clock = get_clock_unlocked (media))) {
2064 provider = gst_net_time_provider_new (clock, address, port);
2065 gst_object_unref (clock);
2067 priv->nettime = provider;
2071 g_rec_mutex_unlock (&priv->state_lock);
2074 gst_object_ref (provider);
2080 * gst_rtsp_media_set_pipeline_state:
2081 * @media: a #GstRTSPMedia
2082 * @state: the target state of the pipeline
2084 * Set the state of the pipeline managed by @media to @state
2087 gst_rtsp_media_set_pipeline_state (GstRTSPMedia * media, GstState state)
2089 GstRTSPMediaPrivate *priv = media->priv;
2091 if (state == GST_STATE_NULL) {
2092 gst_rtsp_media_unprepare (media);
2094 GST_INFO ("state %s media %p", gst_element_state_get_name (state), media);
2095 priv->target_state = state;
2096 /* when we are buffering, don't update the state yet, this will be done
2097 * when buffering finishes */
2098 if (priv->buffering) {
2099 GST_INFO ("Buffering busy, delay state change");
2101 gst_element_set_state (priv->pipeline, state);
2107 * gst_rtsp_media_set_state:
2108 * @media: a #GstRTSPMedia
2109 * @state: the target state of the media
2110 * @transports: a #GPtrArray of #GstRTSPStreamTransport pointers
2112 * Set the state of @media to @state and for the transports in @transports.
2114 * @media must be prepared with gst_rtsp_media_prepare();
2116 * Returns: %TRUE on success.
2119 gst_rtsp_media_set_state (GstRTSPMedia * media, GstState state,
2120 GPtrArray * transports)
2122 GstRTSPMediaPrivate *priv;
2124 gboolean activate, deactivate, do_state;
2127 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2128 g_return_val_if_fail (transports != NULL, FALSE);
2132 g_rec_mutex_lock (&priv->state_lock);
2133 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
2136 /* NULL and READY are the same */
2137 if (state == GST_STATE_READY)
2138 state = GST_STATE_NULL;
2140 activate = deactivate = FALSE;
2142 GST_INFO ("going to state %s media %p", gst_element_state_get_name (state),
2146 case GST_STATE_NULL:
2147 case GST_STATE_PAUSED:
2148 /* we're going from PLAYING to PAUSED, READY or NULL, deactivate */
2149 if (priv->target_state == GST_STATE_PLAYING)
2152 case GST_STATE_PLAYING:
2153 /* we're going to PLAYING, activate */
2159 old_active = priv->n_active;
2161 for (i = 0; i < transports->len; i++) {
2162 GstRTSPStreamTransport *trans;
2164 /* we need a non-NULL entry in the array */
2165 trans = g_ptr_array_index (transports, i);
2170 if (gst_rtsp_stream_transport_set_active (trans, TRUE))
2172 } else if (deactivate) {
2173 if (gst_rtsp_stream_transport_set_active (trans, FALSE))
2178 /* we just activated the first media, do the playing state change */
2179 if (old_active == 0 && activate)
2181 /* if we have no more active media, do the downward state changes */
2182 else if (priv->n_active == 0)
2187 GST_INFO ("state %d active %d media %p do_state %d", state, priv->n_active,
2190 if (priv->target_state != state) {
2192 gst_rtsp_media_set_pipeline_state (media, state);
2194 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STATE], 0, state,
2198 /* remember where we are */
2199 if (state != GST_STATE_NULL && (state == GST_STATE_PAUSED ||
2200 old_active != priv->n_active))
2201 collect_media_stats (media);
2203 g_rec_mutex_unlock (&priv->state_lock);
2210 GST_WARNING ("media %p was not prepared", media);
2211 g_rec_mutex_unlock (&priv->state_lock);
2216 /* called with state-lock */
2218 default_convert_range (GstRTSPMedia * media, GstRTSPTimeRange * range,
2219 GstRTSPRangeUnit unit)
2221 return gst_rtsp_range_convert_units (range, unit);
2225 default_query_position (GstRTSPMedia * media, gint64 * position)
2227 return gst_element_query_position (media->priv->pipeline, GST_FORMAT_TIME,
2232 default_query_stop (GstRTSPMedia * media, gint64 * stop)
2237 query = gst_query_new_segment (GST_FORMAT_TIME);
2238 if ((res = gst_element_query (media->priv->pipeline, query))) {
2240 gst_query_parse_segment (query, NULL, &format, NULL, stop);
2241 if (format != GST_FORMAT_TIME)
2244 gst_query_unref (query);