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_UDP_MCAST | \
126 GST_RTSP_LOWER_TRANS_TCP
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 if (priv->permissions)
296 gst_rtsp_permissions_unref (priv->permissions);
298 g_ptr_array_unref (priv->streams);
300 g_list_free_full (priv->dynamic, gst_object_unref);
303 gst_object_unref (priv->pipeline);
305 gst_object_unref (priv->nettime);
306 gst_object_unref (priv->element);
308 g_object_unref (priv->pool);
309 g_mutex_clear (&priv->lock);
310 g_cond_clear (&priv->cond);
311 g_rec_mutex_clear (&priv->state_lock);
313 G_OBJECT_CLASS (gst_rtsp_media_parent_class)->finalize (obj);
317 gst_rtsp_media_get_property (GObject * object, guint propid,
318 GValue * value, GParamSpec * pspec)
320 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
324 g_value_set_object (value, media->priv->element);
327 g_value_set_boolean (value, gst_rtsp_media_is_shared (media));
330 g_value_set_boolean (value, gst_rtsp_media_is_reusable (media));
333 g_value_set_flags (value, gst_rtsp_media_get_protocols (media));
335 case PROP_EOS_SHUTDOWN:
336 g_value_set_boolean (value, gst_rtsp_media_is_eos_shutdown (media));
338 case PROP_BUFFER_SIZE:
339 g_value_set_uint (value, gst_rtsp_media_get_buffer_size (media));
341 case PROP_TIME_PROVIDER:
342 g_value_set_boolean (value, gst_rtsp_media_is_time_provider (media));
345 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
350 gst_rtsp_media_set_property (GObject * object, guint propid,
351 const GValue * value, GParamSpec * pspec)
353 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
357 media->priv->element = g_value_get_object (value);
358 gst_object_ref_sink (media->priv->element);
361 gst_rtsp_media_set_shared (media, g_value_get_boolean (value));
364 gst_rtsp_media_set_reusable (media, g_value_get_boolean (value));
367 gst_rtsp_media_set_protocols (media, g_value_get_flags (value));
369 case PROP_EOS_SHUTDOWN:
370 gst_rtsp_media_set_eos_shutdown (media, g_value_get_boolean (value));
372 case PROP_BUFFER_SIZE:
373 gst_rtsp_media_set_buffer_size (media, g_value_get_uint (value));
375 case PROP_TIME_PROVIDER:
376 gst_rtsp_media_use_time_provider (media, g_value_get_boolean (value));
379 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
383 /* must be called with state lock */
385 collect_media_stats (GstRTSPMedia * media)
387 GstRTSPMediaPrivate *priv = media->priv;
388 gint64 position, stop;
390 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
391 priv->status != GST_RTSP_MEDIA_STATUS_PREPARING)
394 priv->range.unit = GST_RTSP_RANGE_NPT;
396 GST_INFO ("collect media stats");
399 priv->range.min.type = GST_RTSP_TIME_NOW;
400 priv->range.min.seconds = -1;
401 priv->range_start = -1;
402 priv->range.max.type = GST_RTSP_TIME_END;
403 priv->range.max.seconds = -1;
404 priv->range_stop = -1;
406 GstRTSPMediaClass *klass;
409 klass = GST_RTSP_MEDIA_GET_CLASS (media);
411 /* get the position */
413 if (klass->query_position)
414 ret = klass->query_position (media, &position);
417 GST_INFO ("position query failed");
421 /* get the current segment stop */
423 if (klass->query_stop)
424 ret = klass->query_stop (media, &stop);
427 GST_INFO ("stop query failed");
431 GST_INFO ("stats: position %" GST_TIME_FORMAT ", stop %"
432 GST_TIME_FORMAT, GST_TIME_ARGS (position), GST_TIME_ARGS (stop));
434 if (position == -1) {
435 priv->range.min.type = GST_RTSP_TIME_NOW;
436 priv->range.min.seconds = -1;
437 priv->range_start = -1;
439 priv->range.min.type = GST_RTSP_TIME_SECONDS;
440 priv->range.min.seconds = ((gdouble) position) / GST_SECOND;
441 priv->range_start = position;
444 priv->range.max.type = GST_RTSP_TIME_END;
445 priv->range.max.seconds = -1;
446 priv->range_stop = -1;
448 priv->range.max.type = GST_RTSP_TIME_SECONDS;
449 priv->range.max.seconds = ((gdouble) stop) / GST_SECOND;
450 priv->range_stop = stop;
456 * gst_rtsp_media_new:
457 * @element: (transfer full): a #GstElement
459 * Create a new #GstRTSPMedia instance. @element is the bin element that
460 * provides the different streams. The #GstRTSPMedia object contains the
461 * element to produce RTP data for one or more related (audio/video/..)
464 * Ownership is taken of @element.
466 * Returns: a new #GstRTSPMedia object.
469 gst_rtsp_media_new (GstElement * element)
471 GstRTSPMedia *result;
473 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
475 result = g_object_new (GST_TYPE_RTSP_MEDIA, "element", element, NULL);
481 * gst_rtsp_media_get_element:
482 * @media: a #GstRTSPMedia
484 * Get the element that was used when constructing @media.
486 * Returns: a #GstElement. Unref after usage.
489 gst_rtsp_media_get_element (GstRTSPMedia * media)
491 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
493 return gst_object_ref (media->priv->element);
497 * gst_rtsp_media_take_pipeline:
498 * @media: a #GstRTSPMedia
499 * @pipeline: (transfer full): a #GstPipeline
501 * Set @pipeline as the #GstPipeline for @media. Ownership is
502 * taken of @pipeline.
505 gst_rtsp_media_take_pipeline (GstRTSPMedia * media, GstPipeline * pipeline)
507 GstRTSPMediaPrivate *priv;
509 GstNetTimeProvider *nettime;
511 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
512 g_return_if_fail (GST_IS_PIPELINE (pipeline));
516 g_mutex_lock (&priv->lock);
517 old = priv->pipeline;
518 priv->pipeline = GST_ELEMENT_CAST (pipeline);
519 nettime = priv->nettime;
520 priv->nettime = NULL;
521 g_mutex_unlock (&priv->lock);
524 gst_object_unref (old);
527 gst_object_unref (nettime);
529 gst_bin_add (GST_BIN_CAST (pipeline), priv->element);
533 * gst_rtsp_media_set_permissions:
534 * @media: a #GstRTSPMedia
535 * @permissions: a #GstRTSPPermissions
537 * Set @permissions on @media.
540 gst_rtsp_media_set_permissions (GstRTSPMedia * media,
541 GstRTSPPermissions * permissions)
543 GstRTSPMediaPrivate *priv;
545 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
549 g_mutex_lock (&priv->lock);
550 if (priv->permissions)
551 gst_rtsp_permissions_unref (priv->permissions);
552 if ((priv->permissions = permissions))
553 gst_rtsp_permissions_ref (permissions);
554 g_mutex_unlock (&priv->lock);
558 * gst_rtsp_media_get_permissions:
559 * @media: a #GstRTSPMedia
561 * Get the permissions object from @media.
563 * Returns: (transfer full): a #GstRTSPPermissions object, unref after usage.
566 gst_rtsp_media_get_permissions (GstRTSPMedia * media)
568 GstRTSPMediaPrivate *priv;
569 GstRTSPPermissions *result;
571 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
575 g_mutex_lock (&priv->lock);
576 if ((result = priv->permissions))
577 gst_rtsp_permissions_ref (result);
578 g_mutex_unlock (&priv->lock);
584 * gst_rtsp_media_set_shared:
585 * @media: a #GstRTSPMedia
586 * @shared: the new value
588 * Set or unset if the pipeline for @media can be shared will multiple clients.
589 * When @shared is %TRUE, client requests for this media will share the media
593 gst_rtsp_media_set_shared (GstRTSPMedia * media, gboolean shared)
595 GstRTSPMediaPrivate *priv;
597 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
601 g_mutex_lock (&priv->lock);
602 priv->shared = shared;
603 g_mutex_unlock (&priv->lock);
607 * gst_rtsp_media_is_shared:
608 * @media: a #GstRTSPMedia
610 * Check if the pipeline for @media can be shared between multiple clients.
612 * Returns: %TRUE if the media can be shared between clients.
615 gst_rtsp_media_is_shared (GstRTSPMedia * media)
617 GstRTSPMediaPrivate *priv;
620 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
624 g_mutex_lock (&priv->lock);
626 g_mutex_unlock (&priv->lock);
632 * gst_rtsp_media_set_reusable:
633 * @media: a #GstRTSPMedia
634 * @reusable: the new value
636 * Set or unset if the pipeline for @media can be reused after the pipeline has
640 gst_rtsp_media_set_reusable (GstRTSPMedia * media, gboolean reusable)
642 GstRTSPMediaPrivate *priv;
644 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
648 g_mutex_lock (&priv->lock);
649 priv->reusable = reusable;
650 g_mutex_unlock (&priv->lock);
654 * gst_rtsp_media_is_reusable:
655 * @media: a #GstRTSPMedia
657 * Check if the pipeline for @media can be reused after an unprepare.
659 * Returns: %TRUE if the media can be reused
662 gst_rtsp_media_is_reusable (GstRTSPMedia * media)
664 GstRTSPMediaPrivate *priv;
667 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
671 g_mutex_lock (&priv->lock);
672 res = priv->reusable;
673 g_mutex_unlock (&priv->lock);
679 do_set_protocols (GstRTSPStream * stream, GstRTSPLowerTrans * protocols)
681 gst_rtsp_stream_set_protocols (stream, *protocols);
685 * gst_rtsp_media_set_protocols:
686 * @media: a #GstRTSPMedia
687 * @protocols: the new flags
689 * Configure the allowed lower transport for @media.
692 gst_rtsp_media_set_protocols (GstRTSPMedia * media, GstRTSPLowerTrans protocols)
694 GstRTSPMediaPrivate *priv;
696 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
700 g_mutex_lock (&priv->lock);
701 priv->protocols = protocols;
702 g_ptr_array_foreach (priv->streams, (GFunc) do_set_protocols, &protocols);
703 g_mutex_unlock (&priv->lock);
707 * gst_rtsp_media_get_protocols:
708 * @media: a #GstRTSPMedia
710 * Get the allowed protocols of @media.
712 * Returns: a #GstRTSPLowerTrans
715 gst_rtsp_media_get_protocols (GstRTSPMedia * media)
717 GstRTSPMediaPrivate *priv;
718 GstRTSPLowerTrans res;
720 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media),
721 GST_RTSP_LOWER_TRANS_UNKNOWN);
725 g_mutex_lock (&priv->lock);
726 res = priv->protocols;
727 g_mutex_unlock (&priv->lock);
733 * gst_rtsp_media_set_eos_shutdown:
734 * @media: a #GstRTSPMedia
735 * @eos_shutdown: the new value
737 * Set or unset if an EOS event will be sent to the pipeline for @media before
741 gst_rtsp_media_set_eos_shutdown (GstRTSPMedia * media, gboolean eos_shutdown)
743 GstRTSPMediaPrivate *priv;
745 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
749 g_mutex_lock (&priv->lock);
750 priv->eos_shutdown = eos_shutdown;
751 g_mutex_unlock (&priv->lock);
755 * gst_rtsp_media_is_eos_shutdown:
756 * @media: a #GstRTSPMedia
758 * Check if the pipeline for @media will send an EOS down the pipeline before
761 * Returns: %TRUE if the media will send EOS before unpreparing.
764 gst_rtsp_media_is_eos_shutdown (GstRTSPMedia * media)
766 GstRTSPMediaPrivate *priv;
769 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
773 g_mutex_lock (&priv->lock);
774 res = priv->eos_shutdown;
775 g_mutex_unlock (&priv->lock);
781 * gst_rtsp_media_set_buffer_size:
782 * @media: a #GstRTSPMedia
783 * @size: the new value
785 * Set the kernel UDP buffer size.
788 gst_rtsp_media_set_buffer_size (GstRTSPMedia * media, guint size)
790 GstRTSPMediaPrivate *priv;
792 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
794 GST_LOG_OBJECT (media, "set buffer size %u", size);
798 g_mutex_lock (&priv->lock);
799 priv->buffer_size = size;
800 g_mutex_unlock (&priv->lock);
804 * gst_rtsp_media_get_buffer_size:
805 * @media: a #GstRTSPMedia
807 * Get the kernel UDP buffer size.
809 * Returns: the kernel UDP buffer size.
812 gst_rtsp_media_get_buffer_size (GstRTSPMedia * media)
814 GstRTSPMediaPrivate *priv;
817 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
821 g_mutex_unlock (&priv->lock);
822 res = priv->buffer_size;
823 g_mutex_unlock (&priv->lock);
829 * gst_rtsp_media_use_time_provider:
830 * @media: a #GstRTSPMedia
831 * @time_provider: if a #GstNetTimeProvider should be used
833 * Set @media to provide a #GstNetTimeProvider.
836 gst_rtsp_media_use_time_provider (GstRTSPMedia * media, gboolean time_provider)
838 GstRTSPMediaPrivate *priv;
840 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
844 g_mutex_lock (&priv->lock);
845 priv->time_provider = time_provider;
846 g_mutex_unlock (&priv->lock);
850 * gst_rtsp_media_is_time_provider:
851 * @media: a #GstRTSPMedia
853 * Check if @media can provide a #GstNetTimeProvider for its pipeline clock.
855 * Use gst_rtsp_media_get_time_provider() to get the network clock.
857 * Returns: %TRUE if @media can provide a #GstNetTimeProvider.
860 gst_rtsp_media_is_time_provider (GstRTSPMedia * media)
862 GstRTSPMediaPrivate *priv;
865 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
869 g_mutex_unlock (&priv->lock);
870 res = priv->time_provider;
871 g_mutex_unlock (&priv->lock);
877 * gst_rtsp_media_set_address_pool:
878 * @media: a #GstRTSPMedia
879 * @pool: a #GstRTSPAddressPool
881 * configure @pool to be used as the address pool of @media.
884 gst_rtsp_media_set_address_pool (GstRTSPMedia * media,
885 GstRTSPAddressPool * pool)
887 GstRTSPMediaPrivate *priv;
888 GstRTSPAddressPool *old;
890 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
894 GST_LOG_OBJECT (media, "set address pool %p", pool);
896 g_mutex_lock (&priv->lock);
897 if ((old = priv->pool) != pool)
898 priv->pool = pool ? g_object_ref (pool) : NULL;
901 g_ptr_array_foreach (priv->streams, (GFunc) gst_rtsp_stream_set_address_pool,
903 g_mutex_unlock (&priv->lock);
906 g_object_unref (old);
910 * gst_rtsp_media_get_address_pool:
911 * @media: a #GstRTSPMedia
913 * Get the #GstRTSPAddressPool used as the address pool of @media.
915 * Returns: (transfer full): the #GstRTSPAddressPool of @media. g_object_unref() after
919 gst_rtsp_media_get_address_pool (GstRTSPMedia * media)
921 GstRTSPMediaPrivate *priv;
922 GstRTSPAddressPool *result;
924 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
928 g_mutex_lock (&priv->lock);
929 if ((result = priv->pool))
930 g_object_ref (result);
931 g_mutex_unlock (&priv->lock);
937 * gst_rtsp_media_collect_streams:
938 * @media: a #GstRTSPMedia
940 * Find all payloader elements, they should be named pay%d in the
941 * element of @media, and create #GstRTSPStreams for them.
943 * Collect all dynamic elements, named dynpay%d, and add them to
944 * the list of dynamic elements.
947 gst_rtsp_media_collect_streams (GstRTSPMedia * media)
949 GstRTSPMediaPrivate *priv;
950 GstElement *element, *elem;
955 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
958 element = priv->element;
961 for (i = 0; have_elem; i++) {
966 name = g_strdup_printf ("pay%d", i);
967 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
968 GST_INFO ("found stream %d with payloader %p", i, elem);
970 /* take the pad of the payloader */
971 pad = gst_element_get_static_pad (elem, "src");
972 /* create the stream */
973 gst_rtsp_media_create_stream (media, elem, pad);
974 gst_object_unref (pad);
975 gst_object_unref (elem);
981 name = g_strdup_printf ("dynpay%d", i);
982 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
983 /* a stream that will dynamically create pads to provide RTP packets */
985 GST_INFO ("found dynamic element %d, %p", i, elem);
987 g_mutex_lock (&priv->lock);
988 priv->dynamic = g_list_prepend (priv->dynamic, elem);
989 g_mutex_unlock (&priv->lock);
998 * gst_rtsp_media_create_stream:
999 * @media: a #GstRTSPMedia
1000 * @payloader: a #GstElement
1001 * @srcpad: a source #GstPad
1003 * Create a new stream in @media that provides RTP data on @srcpad.
1004 * @srcpad should be a pad of an element inside @media->element.
1006 * Returns: (transfer none): a new #GstRTSPStream that remains valid for as long
1010 gst_rtsp_media_create_stream (GstRTSPMedia * media, GstElement * payloader,
1013 GstRTSPMediaPrivate *priv;
1014 GstRTSPStream *stream;
1019 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1020 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
1021 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1022 g_return_val_if_fail (GST_PAD_IS_SRC (pad), NULL);
1026 g_mutex_lock (&priv->lock);
1027 idx = priv->streams->len;
1029 GST_DEBUG ("media %p: creating stream with index %d", media, idx);
1031 name = g_strdup_printf ("src_%u", idx);
1032 srcpad = gst_ghost_pad_new (name, pad);
1033 gst_pad_set_active (srcpad, TRUE);
1034 gst_element_add_pad (priv->element, srcpad);
1037 stream = gst_rtsp_stream_new (idx, payloader, srcpad);
1039 gst_rtsp_stream_set_address_pool (stream, priv->pool);
1040 gst_rtsp_stream_set_protocols (stream, priv->protocols);
1042 g_ptr_array_add (priv->streams, stream);
1043 g_mutex_unlock (&priv->lock);
1045 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STREAM], 0, stream,
1052 gst_rtsp_media_remove_stream (GstRTSPMedia * media, GstRTSPStream * stream)
1054 GstRTSPMediaPrivate *priv;
1059 g_mutex_lock (&priv->lock);
1060 /* remove the ghostpad */
1061 srcpad = gst_rtsp_stream_get_srcpad (stream);
1062 gst_element_remove_pad (priv->element, srcpad);
1063 gst_object_unref (srcpad);
1064 /* now remove the stream */
1065 g_object_ref (stream);
1066 g_ptr_array_remove (priv->streams, stream);
1067 g_mutex_unlock (&priv->lock);
1069 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_REMOVED_STREAM], 0,
1072 g_object_unref (stream);
1076 * gst_rtsp_media_n_streams:
1077 * @media: a #GstRTSPMedia
1079 * Get the number of streams in this media.
1081 * Returns: The number of streams.
1084 gst_rtsp_media_n_streams (GstRTSPMedia * media)
1086 GstRTSPMediaPrivate *priv;
1089 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), 0);
1093 g_mutex_lock (&priv->lock);
1094 res = priv->streams->len;
1095 g_mutex_unlock (&priv->lock);
1101 * gst_rtsp_media_get_stream:
1102 * @media: a #GstRTSPMedia
1103 * @idx: the stream index
1105 * Retrieve the stream with index @idx from @media.
1107 * Returns: (transfer none): the #GstRTSPStream at index @idx or %NULL when a stream with
1108 * that index did not exist.
1111 gst_rtsp_media_get_stream (GstRTSPMedia * media, guint idx)
1113 GstRTSPMediaPrivate *priv;
1116 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1120 g_mutex_lock (&priv->lock);
1121 if (idx < priv->streams->len)
1122 res = g_ptr_array_index (priv->streams, idx);
1125 g_mutex_unlock (&priv->lock);
1131 * gst_rtsp_media_find_stream:
1132 * @media: a #GstRTSPMedia
1133 * @control: the control of the stream
1135 * Find a stream in @media with @control as the control uri.
1137 * Returns: (transfer none): the #GstRTSPStream with control uri @control
1138 * or %NULL when a stream with that control did not exist.
1141 gst_rtsp_media_find_stream (GstRTSPMedia * media, const gchar * control)
1143 GstRTSPMediaPrivate *priv;
1147 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1148 g_return_val_if_fail (control != NULL, NULL);
1154 g_mutex_lock (&priv->lock);
1155 for (i = 0; i < priv->streams->len; i++) {
1156 GstRTSPStream *test;
1158 test = g_ptr_array_index (priv->streams, i);
1159 if (gst_rtsp_stream_has_control (test, control)) {
1164 g_mutex_unlock (&priv->lock);
1170 * gst_rtsp_media_get_range_string:
1171 * @media: a #GstRTSPMedia
1172 * @play: for the PLAY request
1173 * @unit: the unit to use for the string
1175 * Get the current range as a string. @media must be prepared with
1176 * gst_rtsp_media_prepare ().
1178 * Returns: The range as a string, g_free() after usage.
1181 gst_rtsp_media_get_range_string (GstRTSPMedia * media, gboolean play,
1182 GstRTSPRangeUnit unit)
1184 GstRTSPMediaClass *klass;
1185 GstRTSPMediaPrivate *priv;
1187 GstRTSPTimeRange range;
1189 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1190 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1191 g_return_val_if_fail (klass->convert_range != NULL, FALSE);
1195 g_rec_mutex_lock (&priv->state_lock);
1196 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
1199 g_mutex_lock (&priv->lock);
1201 /* Update the range value with current position/duration */
1202 collect_media_stats (media);
1205 range = priv->range;
1207 if (!play && priv->n_active > 0) {
1208 range.min.type = GST_RTSP_TIME_NOW;
1209 range.min.seconds = -1;
1211 g_mutex_unlock (&priv->lock);
1212 g_rec_mutex_unlock (&priv->state_lock);
1214 if (!klass->convert_range (media, &range, unit))
1215 goto conversion_failed;
1217 result = gst_rtsp_range_to_string (&range);
1224 GST_WARNING ("media %p was not prepared", media);
1225 g_rec_mutex_unlock (&priv->state_lock);
1230 GST_WARNING ("range conversion to unit %d failed", unit);
1236 * gst_rtsp_media_seek:
1237 * @media: a #GstRTSPMedia
1238 * @range: a #GstRTSPTimeRange
1240 * Seek the pipeline of @media to @range. @media must be prepared with
1241 * gst_rtsp_media_prepare().
1243 * Returns: %TRUE on success.
1246 gst_rtsp_media_seek (GstRTSPMedia * media, GstRTSPTimeRange * range)
1248 GstRTSPMediaClass *klass;
1249 GstRTSPMediaPrivate *priv;
1252 GstClockTime start, stop;
1253 GstSeekType start_type, stop_type;
1256 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1258 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1259 g_return_val_if_fail (range != NULL, FALSE);
1260 g_return_val_if_fail (klass->convert_range != NULL, FALSE);
1264 g_rec_mutex_lock (&priv->state_lock);
1265 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
1268 /* Update the seekable state of the pipeline in case it changed */
1269 query = gst_query_new_seeking (GST_FORMAT_TIME);
1270 if (gst_element_query (priv->pipeline, query)) {
1275 gst_query_parse_seeking (query, &format, &seekable, &start, &end);
1276 priv->seekable = seekable;
1278 gst_query_unref (query);
1280 if (!priv->seekable)
1283 /* depends on the current playing state of the pipeline. We might need to
1284 * queue this until we get EOS. */
1285 flags = GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_KEY_UNIT;
1286 flags = GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT;
1288 start_type = stop_type = GST_SEEK_TYPE_NONE;
1290 if (!klass->convert_range (media, range, GST_RTSP_RANGE_NPT))
1292 gst_rtsp_range_get_times (range, &start, &stop);
1294 GST_INFO ("got %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1295 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1296 GST_INFO ("current %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1297 GST_TIME_ARGS (priv->range_start), GST_TIME_ARGS (priv->range_stop));
1299 if (priv->range_start == start)
1300 start = GST_CLOCK_TIME_NONE;
1301 else if (start != GST_CLOCK_TIME_NONE)
1302 start_type = GST_SEEK_TYPE_SET;
1304 if (priv->range_stop == stop)
1305 stop = GST_CLOCK_TIME_NONE;
1306 else if (stop != GST_CLOCK_TIME_NONE)
1307 stop_type = GST_SEEK_TYPE_SET;
1309 if (start != GST_CLOCK_TIME_NONE || stop != GST_CLOCK_TIME_NONE) {
1310 GST_INFO ("seeking to %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1311 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1313 res = gst_element_seek (priv->pipeline, 1.0, GST_FORMAT_TIME,
1314 flags, start_type, start, stop_type, stop);
1316 /* and block for the seek to complete */
1317 GST_INFO ("done seeking %d", res);
1318 gst_element_get_state (priv->pipeline, NULL, NULL, -1);
1319 GST_INFO ("prerolled again");
1321 collect_media_stats (media);
1323 GST_INFO ("no seek needed");
1326 g_rec_mutex_unlock (&priv->state_lock);
1333 g_rec_mutex_unlock (&priv->state_lock);
1334 GST_INFO ("media %p is not prepared", media);
1339 g_rec_mutex_unlock (&priv->state_lock);
1340 GST_INFO ("pipeline is not seekable");
1345 g_rec_mutex_unlock (&priv->state_lock);
1346 GST_WARNING ("conversion to npt not supported");
1352 gst_rtsp_media_set_status (GstRTSPMedia * media, GstRTSPMediaStatus status)
1354 GstRTSPMediaPrivate *priv = media->priv;
1356 g_mutex_lock (&priv->lock);
1357 priv->status = status;
1358 GST_DEBUG ("setting new status to %d", status);
1359 g_cond_broadcast (&priv->cond);
1360 g_mutex_unlock (&priv->lock);
1364 * gst_rtsp_media_get_status:
1365 * @media: a #GstRTSPMedia
1367 * Get the status of @media. When @media is busy preparing, this function waits
1368 * until @media is prepared or in error.
1370 * Returns: the status of @media.
1373 gst_rtsp_media_get_status (GstRTSPMedia * media)
1375 GstRTSPMediaPrivate *priv = media->priv;
1376 GstRTSPMediaStatus result;
1379 g_mutex_lock (&priv->lock);
1380 end_time = g_get_monotonic_time () + 20 * G_TIME_SPAN_SECOND;
1381 /* while we are preparing, wait */
1382 while (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING) {
1383 GST_DEBUG ("waiting for status change");
1384 if (!g_cond_wait_until (&priv->cond, &priv->lock, end_time)) {
1385 GST_DEBUG ("timeout, assuming error status");
1386 priv->status = GST_RTSP_MEDIA_STATUS_ERROR;
1389 /* could be success or error */
1390 result = priv->status;
1391 GST_DEBUG ("got status %d", result);
1392 g_mutex_unlock (&priv->lock);
1397 /* called with state-lock */
1399 default_handle_message (GstRTSPMedia * media, GstMessage * message)
1401 GstRTSPMediaPrivate *priv = media->priv;
1402 GstMessageType type;
1404 type = GST_MESSAGE_TYPE (message);
1407 case GST_MESSAGE_STATE_CHANGED:
1409 case GST_MESSAGE_BUFFERING:
1413 gst_message_parse_buffering (message, &percent);
1415 /* no state management needed for live pipelines */
1419 if (percent == 100) {
1420 /* a 100% message means buffering is done */
1421 priv->buffering = FALSE;
1422 /* if the desired state is playing, go back */
1423 if (priv->target_state == GST_STATE_PLAYING) {
1424 GST_INFO ("Buffering done, setting pipeline to PLAYING");
1425 gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
1427 GST_INFO ("Buffering done");
1430 /* buffering busy */
1431 if (priv->buffering == FALSE) {
1432 if (priv->target_state == GST_STATE_PLAYING) {
1433 /* we were not buffering but PLAYING, PAUSE the pipeline. */
1434 GST_INFO ("Buffering, setting pipeline to PAUSED ...");
1435 gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
1437 GST_INFO ("Buffering ...");
1440 priv->buffering = TRUE;
1444 case GST_MESSAGE_LATENCY:
1446 gst_bin_recalculate_latency (GST_BIN_CAST (priv->pipeline));
1449 case GST_MESSAGE_ERROR:
1454 gst_message_parse_error (message, &gerror, &debug);
1455 GST_WARNING ("%p: got error %s (%s)", media, gerror->message, debug);
1456 g_error_free (gerror);
1459 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
1462 case GST_MESSAGE_WARNING:
1467 gst_message_parse_warning (message, &gerror, &debug);
1468 GST_WARNING ("%p: got warning %s (%s)", media, gerror->message, debug);
1469 g_error_free (gerror);
1473 case GST_MESSAGE_ELEMENT:
1475 case GST_MESSAGE_STREAM_STATUS:
1477 case GST_MESSAGE_ASYNC_DONE:
1479 /* when we are dynamically adding pads, the addition of the udpsrc will
1480 * temporarily produce ASYNC_DONE messages. We have to ignore them and
1481 * wait for the final ASYNC_DONE after everything prerolled */
1482 GST_INFO ("%p: ignoring ASYNC_DONE", media);
1484 GST_INFO ("%p: got ASYNC_DONE", media);
1485 collect_media_stats (media);
1487 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
1488 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
1491 case GST_MESSAGE_EOS:
1492 GST_INFO ("%p: got EOS", media);
1494 if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARING) {
1495 GST_DEBUG ("shutting down after EOS");
1496 finish_unprepare (media);
1500 GST_INFO ("%p: got message type %d (%s)", media, type,
1501 gst_message_type_get_name (type));
1508 bus_message (GstBus * bus, GstMessage * message, GstRTSPMedia * media)
1510 GstRTSPMediaPrivate *priv = media->priv;
1511 GstRTSPMediaClass *klass;
1514 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1516 g_rec_mutex_lock (&priv->state_lock);
1517 if (klass->handle_message)
1518 ret = klass->handle_message (media, message);
1521 g_rec_mutex_unlock (&priv->state_lock);
1527 watch_destroyed (GstRTSPMedia * media)
1529 GST_DEBUG_OBJECT (media, "source destroyed");
1530 g_object_unref (media);
1533 /* called from streaming threads */
1535 pad_added_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
1537 GstRTSPMediaPrivate *priv = media->priv;
1538 GstRTSPStream *stream;
1540 /* FIXME, element is likely not a payloader, find the payloader here */
1541 stream = gst_rtsp_media_create_stream (media, element, pad);
1543 g_object_set_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream", stream);
1545 GST_INFO ("pad added %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
1547 g_rec_mutex_lock (&priv->state_lock);
1548 /* we will be adding elements below that will cause ASYNC_DONE to be
1549 * posted in the bus. We want to ignore those messages until the
1550 * pipeline really prerolled. */
1551 priv->adding = TRUE;
1553 /* join the element in the PAUSED state because this callback is
1554 * called from the streaming thread and it is PAUSED */
1555 gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
1556 priv->rtpbin, GST_STATE_PAUSED);
1558 priv->adding = FALSE;
1559 g_rec_mutex_unlock (&priv->state_lock);
1563 pad_removed_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
1565 GstRTSPMediaPrivate *priv = media->priv;
1566 GstRTSPStream *stream;
1568 stream = g_object_get_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream");
1572 GST_INFO ("pad removed %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
1574 g_rec_mutex_lock (&priv->state_lock);
1575 gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
1576 g_rec_mutex_unlock (&priv->state_lock);
1578 gst_rtsp_media_remove_stream (media, stream);
1582 remove_fakesink (GstRTSPMediaPrivate * priv)
1584 GstElement *fakesink;
1586 g_mutex_lock (&priv->lock);
1587 if ((fakesink = priv->fakesink))
1588 gst_object_ref (fakesink);
1589 priv->fakesink = NULL;
1590 g_mutex_unlock (&priv->lock);
1593 gst_bin_remove (GST_BIN (priv->pipeline), fakesink);
1594 gst_element_set_state (fakesink, GST_STATE_NULL);
1595 gst_object_unref (fakesink);
1596 GST_INFO ("removed fakesink");
1601 no_more_pads_cb (GstElement * element, GstRTSPMedia * media)
1603 GstRTSPMediaPrivate *priv = media->priv;
1605 GST_INFO ("no more pads");
1606 remove_fakesink (priv);
1609 typedef struct _DynPaySignalHandlers DynPaySignalHandlers;
1611 struct _DynPaySignalHandlers
1613 gulong pad_added_handler;
1614 gulong pad_removed_handler;
1615 gulong no_more_pads_handler;
1619 start_prepare (GstRTSPMedia * media)
1621 GstRTSPMediaPrivate *priv = media->priv;
1622 GstStateChangeReturn ret;
1626 /* link streams we already have, other streams might appear when we have
1627 * dynamic elements */
1628 for (i = 0; i < priv->streams->len; i++) {
1629 GstRTSPStream *stream;
1631 stream = g_ptr_array_index (priv->streams, i);
1633 gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
1634 priv->rtpbin, GST_STATE_NULL);
1637 for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
1638 GstElement *elem = walk->data;
1639 DynPaySignalHandlers *handlers = g_slice_new (DynPaySignalHandlers);
1641 GST_INFO ("adding callbacks for dynamic element %p", elem);
1643 handlers->pad_added_handler = g_signal_connect (elem, "pad-added",
1644 (GCallback) pad_added_cb, media);
1645 handlers->pad_removed_handler = g_signal_connect (elem, "pad-removed",
1646 (GCallback) pad_removed_cb, media);
1647 handlers->no_more_pads_handler = g_signal_connect (elem, "no-more-pads",
1648 (GCallback) no_more_pads_cb, media);
1650 g_object_set_data (G_OBJECT (elem), "gst-rtsp-dynpay-handlers", handlers);
1652 /* we add a fakesink here in order to make the state change async. We remove
1653 * the fakesink again in the no-more-pads callback. */
1654 priv->fakesink = gst_element_factory_make ("fakesink", "fakesink");
1655 gst_bin_add (GST_BIN (priv->pipeline), priv->fakesink);
1658 GST_INFO ("setting pipeline to PAUSED for media %p", media);
1659 /* first go to PAUSED */
1660 ret = gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
1661 priv->target_state = GST_STATE_PAUSED;
1664 case GST_STATE_CHANGE_SUCCESS:
1665 GST_INFO ("SUCCESS state change for media %p", media);
1666 priv->seekable = TRUE;
1668 case GST_STATE_CHANGE_ASYNC:
1669 GST_INFO ("ASYNC state change for media %p", media);
1670 priv->seekable = TRUE;
1672 case GST_STATE_CHANGE_NO_PREROLL:
1673 /* we need to go to PLAYING */
1674 GST_INFO ("NO_PREROLL state change: live media %p", media);
1675 /* FIXME we disable seeking for live streams for now. We should perform a
1676 * seeking query in preroll instead */
1677 priv->seekable = FALSE;
1678 priv->is_live = TRUE;
1679 ret = gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
1680 if (ret == GST_STATE_CHANGE_FAILURE)
1683 case GST_STATE_CHANGE_FAILURE:
1691 GST_WARNING ("failed to preroll pipeline");
1692 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
1698 * gst_rtsp_media_prepare:
1699 * @media: a #GstRTSPMedia
1700 * @thread: a #GstRTSPThread to run the bus handler or %NULL
1702 * Prepare @media for streaming. This function will create the objects
1703 * to manage the streaming. A pipeline must have been set on @media with
1704 * gst_rtsp_media_take_pipeline().
1706 * It will preroll the pipeline and collect vital information about the streams
1707 * such as the duration.
1709 * Returns: %TRUE on success.
1712 gst_rtsp_media_prepare (GstRTSPMedia * media, GstRTSPThread * thread)
1714 GstRTSPMediaPrivate *priv;
1715 GstRTSPMediaStatus status;
1719 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1720 g_return_val_if_fail (GST_IS_RTSP_THREAD (thread), FALSE);
1724 g_rec_mutex_lock (&priv->state_lock);
1725 priv->prepare_count++;
1727 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED)
1730 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
1733 if (priv->status != GST_RTSP_MEDIA_STATUS_UNPREPARED)
1734 goto not_unprepared;
1736 if (!priv->reusable && priv->reused)
1739 priv->rtpbin = gst_element_factory_make ("rtpbin", NULL);
1740 if (priv->rtpbin != NULL) {
1741 GstRTSPMediaClass *klass;
1742 gboolean success = TRUE;
1744 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1745 if (klass->setup_rtpbin)
1746 success = klass->setup_rtpbin (media, priv->rtpbin);
1748 if (success == FALSE) {
1749 gst_object_unref (priv->rtpbin);
1750 priv->rtpbin = NULL;
1753 if (priv->rtpbin == NULL)
1756 GST_INFO ("preparing media %p", media);
1758 /* reset some variables */
1759 priv->is_live = FALSE;
1760 priv->seekable = FALSE;
1761 priv->buffering = FALSE;
1762 priv->thread = thread;
1763 /* we're preparing now */
1764 priv->status = GST_RTSP_MEDIA_STATUS_PREPARING;
1766 bus = gst_pipeline_get_bus (GST_PIPELINE_CAST (priv->pipeline));
1768 /* add the pipeline bus to our custom mainloop */
1769 priv->source = gst_bus_create_watch (bus);
1770 gst_object_unref (bus);
1772 g_source_set_callback (priv->source, (GSourceFunc) bus_message,
1773 g_object_ref (media), (GDestroyNotify) watch_destroyed);
1775 priv->id = g_source_attach (priv->source, thread->context);
1777 /* add stuff to the bin */
1778 gst_bin_add (GST_BIN (priv->pipeline), priv->rtpbin);
1780 /* do remainder in context */
1781 source = g_idle_source_new ();
1782 g_source_set_callback (source, (GSourceFunc) start_prepare, media, NULL);
1783 g_source_attach (source, thread->context);
1784 g_source_unref (source);
1787 g_rec_mutex_unlock (&priv->state_lock);
1789 /* now wait for all pads to be prerolled, FIXME, we should somehow be
1790 * able to do this async so that we don't block the server thread. */
1791 status = gst_rtsp_media_get_status (media);
1792 if (status == GST_RTSP_MEDIA_STATUS_ERROR)
1795 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_PREPARED], 0, NULL);
1797 GST_INFO ("object %p is prerolled", media);
1804 GST_LOG ("media %p was prepared", media);
1805 g_rec_mutex_unlock (&priv->state_lock);
1811 GST_WARNING ("media %p was not unprepared", media);
1812 priv->prepare_count--;
1813 g_rec_mutex_unlock (&priv->state_lock);
1818 priv->prepare_count--;
1819 g_rec_mutex_unlock (&priv->state_lock);
1820 GST_WARNING ("can not reuse media %p", media);
1825 priv->prepare_count--;
1826 g_rec_mutex_unlock (&priv->state_lock);
1827 GST_WARNING ("no rtpbin element");
1828 g_warning ("failed to create element 'rtpbin', check your installation");
1833 GST_WARNING ("failed to preroll pipeline");
1834 gst_rtsp_media_unprepare (media);
1839 /* must be called with state-lock */
1841 finish_unprepare (GstRTSPMedia * media)
1843 GstRTSPMediaPrivate *priv = media->priv;
1847 GST_DEBUG ("shutting down");
1849 gst_element_set_state (priv->pipeline, GST_STATE_NULL);
1850 remove_fakesink (priv);
1852 for (i = 0; i < priv->streams->len; i++) {
1853 GstRTSPStream *stream;
1855 GST_INFO ("Removing elements of stream %d from pipeline", i);
1857 stream = g_ptr_array_index (priv->streams, i);
1859 gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
1862 /* remove the pad signal handlers */
1863 for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
1864 GstElement *elem = walk->data;
1865 DynPaySignalHandlers *handlers;
1868 g_object_steal_data (G_OBJECT (elem), "gst-rtsp-dynpay-handlers");
1869 g_assert (handlers != NULL);
1871 g_signal_handler_disconnect (G_OBJECT (elem), handlers->pad_added_handler);
1872 g_signal_handler_disconnect (G_OBJECT (elem),
1873 handlers->pad_removed_handler);
1874 g_signal_handler_disconnect (G_OBJECT (elem),
1875 handlers->no_more_pads_handler);
1877 g_slice_free (DynPaySignalHandlers, handlers);
1880 gst_bin_remove (GST_BIN (priv->pipeline), priv->rtpbin);
1881 priv->rtpbin = NULL;
1884 gst_object_unref (priv->nettime);
1885 priv->nettime = NULL;
1887 priv->reused = TRUE;
1888 priv->status = GST_RTSP_MEDIA_STATUS_UNPREPARED;
1890 /* when the media is not reusable, this will effectively unref the media and
1892 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_UNPREPARED], 0, NULL);
1894 /* the source has the last ref to the media */
1896 GST_DEBUG ("destroy source");
1897 g_source_destroy (priv->source);
1898 g_source_unref (priv->source);
1901 GST_DEBUG ("stop thread");
1902 gst_rtsp_thread_stop (priv->thread);
1906 /* called with state-lock */
1908 default_unprepare (GstRTSPMedia * media)
1910 GstRTSPMediaPrivate *priv = media->priv;
1912 if (priv->eos_shutdown) {
1913 GST_DEBUG ("sending EOS for shutdown");
1914 /* ref so that we don't disappear */
1915 gst_element_send_event (priv->pipeline, gst_event_new_eos ());
1916 /* we need to go to playing again for the EOS to propagate, normally in this
1917 * state, nothing is receiving data from us anymore so this is ok. */
1918 gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
1919 priv->status = GST_RTSP_MEDIA_STATUS_UNPREPARING;
1921 finish_unprepare (media);
1927 * gst_rtsp_media_unprepare:
1928 * @media: a #GstRTSPMedia
1930 * Unprepare @media. After this call, the media should be prepared again before
1931 * it can be used again. If the media is set to be non-reusable, a new instance
1934 * Returns: %TRUE on success.
1937 gst_rtsp_media_unprepare (GstRTSPMedia * media)
1939 GstRTSPMediaPrivate *priv;
1942 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1946 g_rec_mutex_lock (&priv->state_lock);
1947 if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARED)
1948 goto was_unprepared;
1950 priv->prepare_count--;
1951 if (priv->prepare_count > 0)
1954 GST_INFO ("unprepare media %p", media);
1955 priv->target_state = GST_STATE_NULL;
1958 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED) {
1959 GstRTSPMediaClass *klass;
1961 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1962 if (klass->unprepare)
1963 success = klass->unprepare (media);
1965 finish_unprepare (media);
1967 g_rec_mutex_unlock (&priv->state_lock);
1973 g_rec_mutex_unlock (&priv->state_lock);
1974 GST_INFO ("media %p was already unprepared", media);
1979 GST_INFO ("media %p still prepared %d times", media, priv->prepare_count);
1980 g_rec_mutex_unlock (&priv->state_lock);
1985 /* should be called with state-lock */
1987 get_clock_unlocked (GstRTSPMedia * media)
1989 if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED) {
1990 GST_DEBUG_OBJECT (media, "media was not prepared");
1993 return gst_pipeline_get_clock (GST_PIPELINE_CAST (media->priv->pipeline));
1997 * gst_rtsp_media_get_clock:
1998 * @media: a #GstRTSPMedia
2000 * Get the clock that is used by the pipeline in @media.
2002 * @media must be prepared before this method returns a valid clock object.
2004 * Returns: the #GstClock used by @media. unref after usage.
2007 gst_rtsp_media_get_clock (GstRTSPMedia * media)
2010 GstRTSPMediaPrivate *priv;
2012 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
2016 g_rec_mutex_lock (&priv->state_lock);
2017 clock = get_clock_unlocked (media);
2018 g_rec_mutex_unlock (&priv->state_lock);
2024 * gst_rtsp_media_get_base_time:
2025 * @media: a #GstRTSPMedia
2027 * Get the base_time that is used by the pipeline in @media.
2029 * @media must be prepared before this method returns a valid base_time.
2031 * Returns: the base_time used by @media.
2034 gst_rtsp_media_get_base_time (GstRTSPMedia * media)
2036 GstClockTime result;
2037 GstRTSPMediaPrivate *priv;
2039 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_CLOCK_TIME_NONE);
2043 g_rec_mutex_lock (&priv->state_lock);
2044 if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
2047 result = gst_element_get_base_time (media->priv->pipeline);
2048 g_rec_mutex_unlock (&priv->state_lock);
2055 g_rec_mutex_unlock (&priv->state_lock);
2056 GST_DEBUG_OBJECT (media, "media was not prepared");
2057 return GST_CLOCK_TIME_NONE;
2062 * gst_rtsp_media_get_time_provider:
2063 * @media: a #GstRTSPMedia
2064 * @address: an address or NULL
2065 * @port: a port or 0
2067 * Get the #GstNetTimeProvider for the clock used by @media. The time provider
2068 * will listen on @address and @port for client time requests.
2070 * Returns: the #GstNetTimeProvider of @media.
2072 GstNetTimeProvider *
2073 gst_rtsp_media_get_time_provider (GstRTSPMedia * media, const gchar * address,
2076 GstRTSPMediaPrivate *priv;
2077 GstNetTimeProvider *provider = NULL;
2079 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
2083 g_rec_mutex_lock (&priv->state_lock);
2084 if (priv->time_provider) {
2085 if ((provider = priv->nettime) == NULL) {
2088 if (priv->time_provider && (clock = get_clock_unlocked (media))) {
2089 provider = gst_net_time_provider_new (clock, address, port);
2090 gst_object_unref (clock);
2092 priv->nettime = provider;
2096 g_rec_mutex_unlock (&priv->state_lock);
2099 gst_object_ref (provider);
2105 * gst_rtsp_media_set_pipeline_state:
2106 * @media: a #GstRTSPMedia
2107 * @state: the target state of the pipeline
2109 * Set the state of the pipeline managed by @media to @state
2112 gst_rtsp_media_set_pipeline_state (GstRTSPMedia * media, GstState state)
2114 GstRTSPMediaPrivate *priv = media->priv;
2116 if (state == GST_STATE_NULL) {
2117 gst_rtsp_media_unprepare (media);
2119 GST_INFO ("state %s media %p", gst_element_state_get_name (state), media);
2120 priv->target_state = state;
2121 /* when we are buffering, don't update the state yet, this will be done
2122 * when buffering finishes */
2123 if (priv->buffering) {
2124 GST_INFO ("Buffering busy, delay state change");
2126 gst_element_set_state (priv->pipeline, state);
2132 * gst_rtsp_media_set_state:
2133 * @media: a #GstRTSPMedia
2134 * @state: the target state of the media
2135 * @transports: a #GPtrArray of #GstRTSPStreamTransport pointers
2137 * Set the state of @media to @state and for the transports in @transports.
2139 * @media must be prepared with gst_rtsp_media_prepare();
2141 * Returns: %TRUE on success.
2144 gst_rtsp_media_set_state (GstRTSPMedia * media, GstState state,
2145 GPtrArray * transports)
2147 GstRTSPMediaPrivate *priv;
2149 gboolean activate, deactivate, do_state;
2152 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2153 g_return_val_if_fail (transports != NULL, FALSE);
2157 g_rec_mutex_lock (&priv->state_lock);
2158 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
2161 /* NULL and READY are the same */
2162 if (state == GST_STATE_READY)
2163 state = GST_STATE_NULL;
2165 activate = deactivate = FALSE;
2167 GST_INFO ("going to state %s media %p", gst_element_state_get_name (state),
2171 case GST_STATE_NULL:
2172 case GST_STATE_PAUSED:
2173 /* we're going from PLAYING to PAUSED, READY or NULL, deactivate */
2174 if (priv->target_state == GST_STATE_PLAYING)
2177 case GST_STATE_PLAYING:
2178 /* we're going to PLAYING, activate */
2184 old_active = priv->n_active;
2186 for (i = 0; i < transports->len; i++) {
2187 GstRTSPStreamTransport *trans;
2189 /* we need a non-NULL entry in the array */
2190 trans = g_ptr_array_index (transports, i);
2195 if (gst_rtsp_stream_transport_set_active (trans, TRUE))
2197 } else if (deactivate) {
2198 if (gst_rtsp_stream_transport_set_active (trans, FALSE))
2203 /* we just activated the first media, do the playing state change */
2204 if (old_active == 0 && activate)
2206 /* if we have no more active media, do the downward state changes */
2207 else if (priv->n_active == 0)
2212 GST_INFO ("state %d active %d media %p do_state %d", state, priv->n_active,
2215 if (priv->target_state != state) {
2217 gst_rtsp_media_set_pipeline_state (media, state);
2219 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STATE], 0, state,
2223 /* remember where we are */
2224 if (state != GST_STATE_NULL && (state == GST_STATE_PAUSED ||
2225 old_active != priv->n_active))
2226 collect_media_stats (media);
2228 g_rec_mutex_unlock (&priv->state_lock);
2235 GST_WARNING ("media %p was not prepared", media);
2236 g_rec_mutex_unlock (&priv->state_lock);
2241 /* called with state-lock */
2243 default_convert_range (GstRTSPMedia * media, GstRTSPTimeRange * range,
2244 GstRTSPRangeUnit unit)
2246 return gst_rtsp_range_convert_units (range, unit);
2250 default_query_position (GstRTSPMedia * media, gint64 * position)
2252 return gst_element_query_position (media->priv->pipeline, GST_FORMAT_TIME,
2257 default_query_stop (GstRTSPMedia * media, gint64 * stop)
2262 query = gst_query_new_segment (GST_FORMAT_TIME);
2263 if ((res = gst_element_query (media->priv->pipeline, query))) {
2265 gst_query_parse_segment (query, NULL, &format, NULL, stop);
2266 if (format != GST_FORMAT_TIME)
2269 gst_query_unref (query);