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.
23 #include <gst/app/gstappsrc.h>
24 #include <gst/app/gstappsink.h>
26 #include "rtsp-media.h"
28 #define GST_RTSP_MEDIA_GET_PRIVATE(obj) \
29 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_MEDIA, GstRTSPMediaPrivate))
31 struct _GstRTSPMediaPrivate
36 /* protected by lock */
39 GstRTSPLowerTrans protocols;
41 gboolean eos_shutdown;
44 GstRTSPAddressPool *pool;
47 GRecMutex state_lock; /* locking order: state lock, lock */
48 GPtrArray *streams; /* protected by lock */
49 GList *dynamic; /* protected by lock */
50 GstRTSPMediaStatus status; /* protected by lock */
55 /* the pipeline for the media */
57 GstElement *fakesink; /* protected by lock */
61 gboolean time_provider;
62 GstNetTimeProvider *nettime;
67 GstState target_state;
69 /* RTP session manager */
72 /* the range of media */
73 GstRTSPTimeRange range; /* protected by lock */
74 GstClockTime range_start;
75 GstClockTime range_stop;
78 #define DEFAULT_SHARED FALSE
79 #define DEFAULT_REUSABLE FALSE
80 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_TCP
81 //#define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP_MCAST
82 #define DEFAULT_EOS_SHUTDOWN FALSE
83 #define DEFAULT_BUFFER_SIZE 0x80000
84 #define DEFAULT_TIME_PROVIDER FALSE
86 /* define to dump received RTCP packets */
105 SIGNAL_REMOVED_STREAM,
112 GST_DEBUG_CATEGORY_STATIC (rtsp_media_debug);
113 #define GST_CAT_DEFAULT rtsp_media_debug
115 static void gst_rtsp_media_get_property (GObject * object, guint propid,
116 GValue * value, GParamSpec * pspec);
117 static void gst_rtsp_media_set_property (GObject * object, guint propid,
118 const GValue * value, GParamSpec * pspec);
119 static void gst_rtsp_media_finalize (GObject * obj);
121 static gpointer do_loop (GstRTSPMediaClass * klass);
122 static gboolean default_handle_message (GstRTSPMedia * media,
123 GstMessage * message);
124 static void finish_unprepare (GstRTSPMedia * media);
125 static gboolean default_unprepare (GstRTSPMedia * media);
127 default_convert_range (GstRTSPMedia * media, GstRTSPTimeRange * range,
128 GstRTSPRangeUnit unit);
129 static gboolean default_query_position (GstRTSPMedia * media,
131 static gboolean default_query_stop (GstRTSPMedia * media, gint64 * stop);
133 static guint gst_rtsp_media_signals[SIGNAL_LAST] = { 0 };
135 G_DEFINE_TYPE (GstRTSPMedia, gst_rtsp_media, G_TYPE_OBJECT);
138 gst_rtsp_media_class_init (GstRTSPMediaClass * klass)
140 GObjectClass *gobject_class;
142 g_type_class_add_private (klass, sizeof (GstRTSPMediaPrivate));
144 gobject_class = G_OBJECT_CLASS (klass);
146 gobject_class->get_property = gst_rtsp_media_get_property;
147 gobject_class->set_property = gst_rtsp_media_set_property;
148 gobject_class->finalize = gst_rtsp_media_finalize;
150 g_object_class_install_property (gobject_class, PROP_SHARED,
151 g_param_spec_boolean ("shared", "Shared",
152 "If this media pipeline can be shared", DEFAULT_SHARED,
153 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
155 g_object_class_install_property (gobject_class, PROP_REUSABLE,
156 g_param_spec_boolean ("reusable", "Reusable",
157 "If this media pipeline can be reused after an unprepare",
158 DEFAULT_REUSABLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
160 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
161 g_param_spec_flags ("protocols", "Protocols",
162 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
163 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
165 g_object_class_install_property (gobject_class, PROP_EOS_SHUTDOWN,
166 g_param_spec_boolean ("eos-shutdown", "EOS Shutdown",
167 "Send an EOS event to the pipeline before unpreparing",
168 DEFAULT_EOS_SHUTDOWN, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
170 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
171 g_param_spec_uint ("buffer-size", "Buffer Size",
172 "The kernel UDP buffer size to use", 0, G_MAXUINT,
173 DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
175 g_object_class_install_property (gobject_class, PROP_ELEMENT,
176 g_param_spec_object ("element", "The Element",
177 "The GstBin to use for streaming the media", GST_TYPE_ELEMENT,
178 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
180 g_object_class_install_property (gobject_class, PROP_TIME_PROVIDER,
181 g_param_spec_boolean ("time-provider", "Time Provider",
182 "Use a NetTimeProvider for clients",
183 DEFAULT_TIME_PROVIDER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
185 gst_rtsp_media_signals[SIGNAL_NEW_STREAM] =
186 g_signal_new ("new-stream", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
187 G_STRUCT_OFFSET (GstRTSPMediaClass, new_stream), NULL, NULL,
188 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_RTSP_STREAM);
190 gst_rtsp_media_signals[SIGNAL_REMOVED_STREAM] =
191 g_signal_new ("removed-stream", G_TYPE_FROM_CLASS (klass),
192 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPMediaClass, removed_stream),
193 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
194 GST_TYPE_RTSP_STREAM);
196 gst_rtsp_media_signals[SIGNAL_PREPARED] =
197 g_signal_new ("prepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
198 G_STRUCT_OFFSET (GstRTSPMediaClass, prepared), NULL, NULL,
199 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
201 gst_rtsp_media_signals[SIGNAL_UNPREPARED] =
202 g_signal_new ("unprepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
203 G_STRUCT_OFFSET (GstRTSPMediaClass, unprepared), NULL, NULL,
204 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
206 gst_rtsp_media_signals[SIGNAL_NEW_STATE] =
207 g_signal_new ("new-state", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
208 G_STRUCT_OFFSET (GstRTSPMediaClass, new_state), NULL, NULL,
209 g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 0, G_TYPE_INT);
211 klass->context = g_main_context_new ();
212 klass->loop = g_main_loop_new (klass->context, TRUE);
214 GST_DEBUG_CATEGORY_INIT (rtsp_media_debug, "rtspmedia", 0, "GstRTSPMedia");
216 klass->thread = g_thread_new ("Bus Thread", (GThreadFunc) do_loop, klass);
218 klass->handle_message = default_handle_message;
219 klass->unprepare = default_unprepare;
220 klass->convert_range = default_convert_range;
221 klass->query_position = default_query_position;
222 klass->query_stop = default_query_stop;
226 gst_rtsp_media_init (GstRTSPMedia * media)
228 GstRTSPMediaPrivate *priv = GST_RTSP_MEDIA_GET_PRIVATE (media);
232 priv->streams = g_ptr_array_new_with_free_func (g_object_unref);
233 g_mutex_init (&priv->lock);
234 g_cond_init (&priv->cond);
235 g_rec_mutex_init (&priv->state_lock);
237 priv->shared = DEFAULT_SHARED;
238 priv->reusable = DEFAULT_REUSABLE;
239 priv->protocols = DEFAULT_PROTOCOLS;
240 priv->eos_shutdown = DEFAULT_EOS_SHUTDOWN;
241 priv->buffer_size = DEFAULT_BUFFER_SIZE;
242 priv->time_provider = DEFAULT_TIME_PROVIDER;
246 gst_rtsp_media_finalize (GObject * obj)
248 GstRTSPMediaPrivate *priv;
251 media = GST_RTSP_MEDIA (obj);
254 GST_INFO ("finalize media %p", media);
256 g_ptr_array_unref (priv->streams);
258 g_list_free_full (priv->dynamic, gst_object_unref);
261 gst_object_unref (priv->pipeline);
263 gst_object_unref (priv->nettime);
264 gst_object_unref (priv->element);
266 g_object_unref (priv->auth);
268 g_object_unref (priv->pool);
269 g_mutex_clear (&priv->lock);
270 g_cond_clear (&priv->cond);
271 g_rec_mutex_clear (&priv->state_lock);
273 G_OBJECT_CLASS (gst_rtsp_media_parent_class)->finalize (obj);
277 gst_rtsp_media_get_property (GObject * object, guint propid,
278 GValue * value, GParamSpec * pspec)
280 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
284 g_value_set_object (value, media->priv->element);
287 g_value_set_boolean (value, gst_rtsp_media_is_shared (media));
290 g_value_set_boolean (value, gst_rtsp_media_is_reusable (media));
293 g_value_set_flags (value, gst_rtsp_media_get_protocols (media));
295 case PROP_EOS_SHUTDOWN:
296 g_value_set_boolean (value, gst_rtsp_media_is_eos_shutdown (media));
298 case PROP_BUFFER_SIZE:
299 g_value_set_uint (value, gst_rtsp_media_get_buffer_size (media));
301 case PROP_TIME_PROVIDER:
302 g_value_set_boolean (value, gst_rtsp_media_is_time_provider (media));
305 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
310 gst_rtsp_media_set_property (GObject * object, guint propid,
311 const GValue * value, GParamSpec * pspec)
313 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
317 media->priv->element = g_value_get_object (value);
318 gst_object_ref_sink (media->priv->element);
321 gst_rtsp_media_set_shared (media, g_value_get_boolean (value));
324 gst_rtsp_media_set_reusable (media, g_value_get_boolean (value));
327 gst_rtsp_media_set_protocols (media, g_value_get_flags (value));
329 case PROP_EOS_SHUTDOWN:
330 gst_rtsp_media_set_eos_shutdown (media, g_value_get_boolean (value));
332 case PROP_BUFFER_SIZE:
333 gst_rtsp_media_set_buffer_size (media, g_value_get_uint (value));
335 case PROP_TIME_PROVIDER:
336 gst_rtsp_media_use_time_provider (media, g_value_get_boolean (value));
339 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
344 do_loop (GstRTSPMediaClass * klass)
346 GST_INFO ("enter mainloop");
347 g_main_loop_run (klass->loop);
348 GST_INFO ("exit mainloop");
353 /* must be called with state lock */
355 collect_media_stats (GstRTSPMedia * media)
357 GstRTSPMediaPrivate *priv = media->priv;
358 gint64 position, stop;
360 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
361 priv->status != GST_RTSP_MEDIA_STATUS_PREPARING)
364 priv->range.unit = GST_RTSP_RANGE_NPT;
366 GST_INFO ("collect media stats");
369 priv->range.min.type = GST_RTSP_TIME_NOW;
370 priv->range.min.seconds = -1;
371 priv->range_start = -1;
372 priv->range.max.type = GST_RTSP_TIME_END;
373 priv->range.max.seconds = -1;
374 priv->range_stop = -1;
376 GstRTSPMediaClass *klass;
379 klass = GST_RTSP_MEDIA_GET_CLASS (media);
381 /* get the position */
383 if (klass->query_position)
384 ret = klass->query_position (media, &position);
387 GST_INFO ("position query failed");
391 /* get the current segment stop */
393 if (klass->query_stop)
394 ret = klass->query_stop (media, &stop);
397 GST_INFO ("stop query failed");
401 GST_INFO ("stats: position %" GST_TIME_FORMAT ", stop %"
402 GST_TIME_FORMAT, GST_TIME_ARGS (position), GST_TIME_ARGS (stop));
404 if (position == -1) {
405 priv->range.min.type = GST_RTSP_TIME_NOW;
406 priv->range.min.seconds = -1;
407 priv->range_start = -1;
409 priv->range.min.type = GST_RTSP_TIME_SECONDS;
410 priv->range.min.seconds = ((gdouble) position) / GST_SECOND;
411 priv->range_start = position;
414 priv->range.max.type = GST_RTSP_TIME_END;
415 priv->range.max.seconds = -1;
416 priv->range_stop = -1;
418 priv->range.max.type = GST_RTSP_TIME_SECONDS;
419 priv->range.max.seconds = ((gdouble) stop) / GST_SECOND;
420 priv->range_stop = stop;
426 * gst_rtsp_media_new:
427 * @element: (transfer full): a #GstElement
429 * Create a new #GstRTSPMedia instance. @element is the bin element that
430 * provides the different streams. The #GstRTSPMedia object contains the
431 * element to produce RTP data for one or more related (audio/video/..)
434 * Ownership is taken of @element.
436 * Returns: a new #GstRTSPMedia object.
439 gst_rtsp_media_new (GstElement * element)
441 GstRTSPMedia *result;
443 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
445 result = g_object_new (GST_TYPE_RTSP_MEDIA, "element", element, NULL);
451 * gst_rtsp_media_get_element:
452 * @media: a #GstRTSPMedia
454 * Get the element that was used when constructing @media.
456 * Returns: a #GstElement. Unref after usage.
459 gst_rtsp_media_get_element (GstRTSPMedia * media)
461 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
463 return gst_object_ref (media->priv->element);
467 * gst_rtsp_media_take_pipeline:
468 * @media: a #GstRTSPMedia
469 * @pipeline: (transfer full): a #GstPipeline
471 * Set @pipeline as the #GstPipeline for @media. Ownership is
472 * taken of @pipeline.
475 gst_rtsp_media_take_pipeline (GstRTSPMedia * media, GstPipeline * pipeline)
477 GstRTSPMediaPrivate *priv;
479 GstNetTimeProvider *nettime;
481 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
482 g_return_if_fail (GST_IS_PIPELINE (pipeline));
486 g_mutex_lock (&priv->lock);
487 old = priv->pipeline;
488 priv->pipeline = GST_ELEMENT_CAST (pipeline);
489 nettime = priv->nettime;
490 priv->nettime = NULL;
491 g_mutex_unlock (&priv->lock);
494 gst_object_unref (old);
497 gst_object_unref (nettime);
499 gst_bin_add (GST_BIN_CAST (pipeline), priv->element);
503 * gst_rtsp_media_set_shared:
504 * @media: a #GstRTSPMedia
505 * @shared: the new value
507 * Set or unset if the pipeline for @media can be shared will multiple clients.
508 * When @shared is %TRUE, client requests for this media will share the media
512 gst_rtsp_media_set_shared (GstRTSPMedia * media, gboolean shared)
514 GstRTSPMediaPrivate *priv;
516 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
520 g_mutex_lock (&priv->lock);
521 priv->shared = shared;
522 g_mutex_unlock (&priv->lock);
526 * gst_rtsp_media_is_shared:
527 * @media: a #GstRTSPMedia
529 * Check if the pipeline for @media can be shared between multiple clients.
531 * Returns: %TRUE if the media can be shared between clients.
534 gst_rtsp_media_is_shared (GstRTSPMedia * media)
536 GstRTSPMediaPrivate *priv;
539 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
543 g_mutex_lock (&priv->lock);
545 g_mutex_unlock (&priv->lock);
551 * gst_rtsp_media_set_reusable:
552 * @media: a #GstRTSPMedia
553 * @reusable: the new value
555 * Set or unset if the pipeline for @media can be reused after the pipeline has
559 gst_rtsp_media_set_reusable (GstRTSPMedia * media, gboolean reusable)
561 GstRTSPMediaPrivate *priv;
563 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
567 g_mutex_lock (&priv->lock);
568 priv->reusable = reusable;
569 g_mutex_unlock (&priv->lock);
573 * gst_rtsp_media_is_reusable:
574 * @media: a #GstRTSPMedia
576 * Check if the pipeline for @media can be reused after an unprepare.
578 * Returns: %TRUE if the media can be reused
581 gst_rtsp_media_is_reusable (GstRTSPMedia * media)
583 GstRTSPMediaPrivate *priv;
586 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
590 g_mutex_lock (&priv->lock);
591 res = priv->reusable;
592 g_mutex_unlock (&priv->lock);
598 * gst_rtsp_media_set_protocols:
599 * @media: a #GstRTSPMedia
600 * @protocols: the new flags
602 * Configure the allowed lower transport for @media.
605 gst_rtsp_media_set_protocols (GstRTSPMedia * media, GstRTSPLowerTrans protocols)
607 GstRTSPMediaPrivate *priv;
609 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
613 g_mutex_lock (&priv->lock);
614 priv->protocols = protocols;
615 g_mutex_unlock (&priv->lock);
619 * gst_rtsp_media_get_protocols:
620 * @media: a #GstRTSPMedia
622 * Get the allowed protocols of @media.
624 * Returns: a #GstRTSPLowerTrans
627 gst_rtsp_media_get_protocols (GstRTSPMedia * media)
629 GstRTSPMediaPrivate *priv;
630 GstRTSPLowerTrans res;
632 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media),
633 GST_RTSP_LOWER_TRANS_UNKNOWN);
637 g_mutex_lock (&priv->lock);
638 res = priv->protocols;
639 g_mutex_unlock (&priv->lock);
645 * gst_rtsp_media_set_eos_shutdown:
646 * @media: a #GstRTSPMedia
647 * @eos_shutdown: the new value
649 * Set or unset if an EOS event will be sent to the pipeline for @media before
653 gst_rtsp_media_set_eos_shutdown (GstRTSPMedia * media, gboolean eos_shutdown)
655 GstRTSPMediaPrivate *priv;
657 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
661 g_mutex_lock (&priv->lock);
662 priv->eos_shutdown = eos_shutdown;
663 g_mutex_unlock (&priv->lock);
667 * gst_rtsp_media_is_eos_shutdown:
668 * @media: a #GstRTSPMedia
670 * Check if the pipeline for @media will send an EOS down the pipeline before
673 * Returns: %TRUE if the media will send EOS before unpreparing.
676 gst_rtsp_media_is_eos_shutdown (GstRTSPMedia * media)
678 GstRTSPMediaPrivate *priv;
681 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
685 g_mutex_lock (&priv->lock);
686 res = priv->eos_shutdown;
687 g_mutex_unlock (&priv->lock);
693 * gst_rtsp_media_set_buffer_size:
694 * @media: a #GstRTSPMedia
695 * @size: the new value
697 * Set the kernel UDP buffer size.
700 gst_rtsp_media_set_buffer_size (GstRTSPMedia * media, guint size)
702 GstRTSPMediaPrivate *priv;
704 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
706 GST_LOG_OBJECT (media, "set buffer size %u", size);
710 g_mutex_lock (&priv->lock);
711 priv->buffer_size = size;
712 g_mutex_unlock (&priv->lock);
716 * gst_rtsp_media_get_buffer_size:
717 * @media: a #GstRTSPMedia
719 * Get the kernel UDP buffer size.
721 * Returns: the kernel UDP buffer size.
724 gst_rtsp_media_get_buffer_size (GstRTSPMedia * media)
726 GstRTSPMediaPrivate *priv;
729 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
733 g_mutex_unlock (&priv->lock);
734 res = priv->buffer_size;
735 g_mutex_unlock (&priv->lock);
741 * gst_rtsp_media_use_time_provider:
742 * @media: a #GstRTSPMedia
744 * Set @media to provide a GstNetTimeProvider.
747 gst_rtsp_media_use_time_provider (GstRTSPMedia * media, gboolean time_provider)
749 GstRTSPMediaPrivate *priv;
751 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
755 g_mutex_lock (&priv->lock);
756 priv->time_provider = time_provider;
757 g_mutex_unlock (&priv->lock);
761 * gst_rtsp_media_is_time_provider:
762 * @media: a #GstRTSPMedia
764 * Check if @media can provide a #GstNetTimeProvider for its pipeline clock.
766 * Use gst_rtsp_media_get_time_provider() to get the network clock.
768 * Returns: %TRUE if @media can provide a #GstNetTimeProvider.
771 gst_rtsp_media_is_time_provider (GstRTSPMedia * media)
773 GstRTSPMediaPrivate *priv;
776 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
780 g_mutex_unlock (&priv->lock);
781 res = priv->time_provider;
782 g_mutex_unlock (&priv->lock);
788 * gst_rtsp_media_set_auth:
789 * @media: a #GstRTSPMedia
790 * @auth: a #GstRTSPAuth
792 * configure @auth to be used as the authentication manager of @media.
795 gst_rtsp_media_set_auth (GstRTSPMedia * media, GstRTSPAuth * auth)
797 GstRTSPMediaPrivate *priv;
800 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
804 GST_LOG_OBJECT (media, "set auth %p", auth);
806 g_mutex_lock (&priv->lock);
807 if ((old = priv->auth) != auth)
808 priv->auth = auth ? g_object_ref (auth) : NULL;
811 g_mutex_unlock (&priv->lock);
814 g_object_unref (old);
818 * gst_rtsp_media_get_auth:
819 * @media: a #GstRTSPMedia
821 * Get the #GstRTSPAuth used as the authentication manager of @media.
823 * Returns: (transfer full): the #GstRTSPAuth of @media. g_object_unref() after
827 gst_rtsp_media_get_auth (GstRTSPMedia * media)
829 GstRTSPMediaPrivate *priv;
832 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
836 g_mutex_lock (&priv->lock);
837 if ((result = priv->auth))
838 g_object_ref (result);
839 g_mutex_unlock (&priv->lock);
845 * gst_rtsp_media_set_address_pool:
846 * @media: a #GstRTSPMedia
847 * @pool: a #GstRTSPAddressPool
849 * configure @pool to be used as the address pool of @media.
852 gst_rtsp_media_set_address_pool (GstRTSPMedia * media,
853 GstRTSPAddressPool * pool)
855 GstRTSPMediaPrivate *priv;
856 GstRTSPAddressPool *old;
858 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
862 GST_LOG_OBJECT (media, "set address pool %p", pool);
864 g_mutex_lock (&priv->lock);
865 if ((old = priv->pool) != pool)
866 priv->pool = pool ? g_object_ref (pool) : NULL;
869 g_ptr_array_foreach (priv->streams, (GFunc) gst_rtsp_stream_set_address_pool,
871 g_mutex_unlock (&priv->lock);
874 g_object_unref (old);
878 * gst_rtsp_media_get_address_pool:
879 * @media: a #GstRTSPMedia
881 * Get the #GstRTSPAddressPool used as the address pool of @media.
883 * Returns: (transfer full): the #GstRTSPAddressPool of @media. g_object_unref() after
887 gst_rtsp_media_get_address_pool (GstRTSPMedia * media)
889 GstRTSPMediaPrivate *priv;
890 GstRTSPAddressPool *result;
892 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
896 g_mutex_lock (&priv->lock);
897 if ((result = priv->pool))
898 g_object_ref (result);
899 g_mutex_unlock (&priv->lock);
905 * gst_rtsp_media_collect_streams:
906 * @media: a #GstRTSPMedia
908 * Find all payloader elements, they should be named pay%d in the
909 * element of @media, and create #GstRTSPStreams for them.
911 * Collect all dynamic elements, named dynpay%d, and add them to
912 * the list of dynamic elements.
915 gst_rtsp_media_collect_streams (GstRTSPMedia * media)
917 GstRTSPMediaPrivate *priv;
918 GstElement *element, *elem;
923 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
926 element = priv->element;
929 for (i = 0; have_elem; i++) {
934 name = g_strdup_printf ("pay%d", i);
935 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
936 GST_INFO ("found stream %d with payloader %p", i, elem);
938 /* take the pad of the payloader */
939 pad = gst_element_get_static_pad (elem, "src");
940 /* create the stream */
941 gst_rtsp_media_create_stream (media, elem, pad);
942 gst_object_unref (pad);
943 gst_object_unref (elem);
949 name = g_strdup_printf ("dynpay%d", i);
950 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
951 /* a stream that will dynamically create pads to provide RTP packets */
953 GST_INFO ("found dynamic element %d, %p", i, elem);
955 g_mutex_lock (&priv->lock);
956 priv->dynamic = g_list_prepend (priv->dynamic, elem);
957 g_mutex_unlock (&priv->lock);
966 * gst_rtsp_media_create_stream:
967 * @media: a #GstRTSPMedia
968 * @payloader: a #GstElement
969 * @srcpad: a source #GstPad
971 * Create a new stream in @media that provides RTP data on @srcpad.
972 * @srcpad should be a pad of an element inside @media->element.
974 * Returns: (transfer none): a new #GstRTSPStream that remains valid for as long
978 gst_rtsp_media_create_stream (GstRTSPMedia * media, GstElement * payloader,
981 GstRTSPMediaPrivate *priv;
982 GstRTSPStream *stream;
987 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
988 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
989 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
990 g_return_val_if_fail (GST_PAD_IS_SRC (pad), NULL);
994 g_mutex_lock (&priv->lock);
995 idx = priv->streams->len;
997 GST_DEBUG ("media %p: creating stream with index %d", media, idx);
999 name = g_strdup_printf ("src_%u", idx);
1000 srcpad = gst_ghost_pad_new (name, pad);
1001 gst_pad_set_active (srcpad, TRUE);
1002 gst_element_add_pad (priv->element, srcpad);
1005 stream = gst_rtsp_stream_new (idx, payloader, srcpad);
1007 gst_rtsp_stream_set_address_pool (stream, priv->pool);
1009 g_ptr_array_add (priv->streams, stream);
1010 g_mutex_unlock (&priv->lock);
1012 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STREAM], 0, stream,
1019 gst_rtsp_media_remove_stream (GstRTSPMedia * media, GstRTSPStream * stream)
1021 GstRTSPMediaPrivate *priv;
1026 g_mutex_lock (&priv->lock);
1027 /* remove the ghostpad */
1028 srcpad = gst_rtsp_stream_get_srcpad (stream);
1029 gst_element_remove_pad (priv->element, srcpad);
1030 gst_object_unref (srcpad);
1031 /* now remove the stream */
1032 g_object_ref (stream);
1033 g_ptr_array_remove (priv->streams, stream);
1034 g_mutex_unlock (&priv->lock);
1036 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_REMOVED_STREAM], 0,
1039 g_object_unref (stream);
1043 * gst_rtsp_media_n_streams:
1044 * @media: a #GstRTSPMedia
1046 * Get the number of streams in this media.
1048 * Returns: The number of streams.
1051 gst_rtsp_media_n_streams (GstRTSPMedia * media)
1053 GstRTSPMediaPrivate *priv;
1056 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), 0);
1060 g_mutex_lock (&priv->lock);
1061 res = priv->streams->len;
1062 g_mutex_unlock (&priv->lock);
1068 * gst_rtsp_media_get_stream:
1069 * @media: a #GstRTSPMedia
1070 * @idx: the stream index
1072 * Retrieve the stream with index @idx from @media.
1074 * Returns: (transfer none): the #GstRTSPStream at index @idx or %NULL when a stream with
1075 * that index did not exist.
1078 gst_rtsp_media_get_stream (GstRTSPMedia * media, guint idx)
1080 GstRTSPMediaPrivate *priv;
1083 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1087 g_mutex_lock (&priv->lock);
1088 if (idx < priv->streams->len)
1089 res = g_ptr_array_index (priv->streams, idx);
1092 g_mutex_unlock (&priv->lock);
1098 * gst_rtsp_media_get_range_string:
1099 * @media: a #GstRTSPMedia
1100 * @play: for the PLAY request
1101 * @unit: the unit to use for the string
1103 * Get the current range as a string. @media must be prepared with
1104 * gst_rtsp_media_prepare ().
1106 * Returns: The range as a string, g_free() after usage.
1109 gst_rtsp_media_get_range_string (GstRTSPMedia * media, gboolean play,
1110 GstRTSPRangeUnit unit)
1112 GstRTSPMediaClass *klass;
1113 GstRTSPMediaPrivate *priv;
1115 GstRTSPTimeRange range;
1117 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1118 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1119 g_return_val_if_fail (klass->convert_range != NULL, FALSE);
1123 g_rec_mutex_lock (&priv->state_lock);
1124 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
1127 g_mutex_lock (&priv->lock);
1129 range = priv->range;
1131 if (!play && priv->n_active > 0) {
1132 range.min.type = GST_RTSP_TIME_NOW;
1133 range.min.seconds = -1;
1135 g_mutex_unlock (&priv->lock);
1136 g_rec_mutex_unlock (&priv->state_lock);
1138 if (!klass->convert_range (media, &range, unit))
1139 goto conversion_failed;
1141 result = gst_rtsp_range_to_string (&range);
1148 GST_WARNING ("media %p was not prepared", media);
1149 g_rec_mutex_unlock (&priv->state_lock);
1154 GST_WARNING ("range conversion to unit %d failed", unit);
1160 * gst_rtsp_media_seek:
1161 * @media: a #GstRTSPMedia
1162 * @range: a #GstRTSPTimeRange
1164 * Seek the pipeline of @media to @range. @media must be prepared with
1165 * gst_rtsp_media_prepare().
1167 * Returns: %TRUE on success.
1170 gst_rtsp_media_seek (GstRTSPMedia * media, GstRTSPTimeRange * range)
1172 GstRTSPMediaClass *klass;
1173 GstRTSPMediaPrivate *priv;
1176 GstClockTime start, stop;
1177 GstSeekType start_type, stop_type;
1179 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1181 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1182 g_return_val_if_fail (range != NULL, FALSE);
1183 g_return_val_if_fail (klass->convert_range != NULL, FALSE);
1187 g_rec_mutex_lock (&priv->state_lock);
1188 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
1191 if (!priv->seekable)
1194 /* depends on the current playing state of the pipeline. We might need to
1195 * queue this until we get EOS. */
1196 flags = GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_KEY_UNIT;
1198 start_type = stop_type = GST_SEEK_TYPE_NONE;
1200 if (!klass->convert_range (media, range, GST_RTSP_RANGE_NPT))
1202 gst_rtsp_range_get_times (range, &start, &stop);
1204 GST_INFO ("got %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1205 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1206 GST_INFO ("current %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1207 GST_TIME_ARGS (priv->range_start), GST_TIME_ARGS (priv->range_stop));
1209 if (priv->range_start == start)
1210 start = GST_CLOCK_TIME_NONE;
1211 else if (start != GST_CLOCK_TIME_NONE)
1212 start_type = GST_SEEK_TYPE_SET;
1214 if (priv->range_stop == stop)
1215 stop = GST_CLOCK_TIME_NONE;
1216 else if (stop != GST_CLOCK_TIME_NONE)
1217 stop_type = GST_SEEK_TYPE_SET;
1219 if (start != GST_CLOCK_TIME_NONE || stop != GST_CLOCK_TIME_NONE) {
1220 GST_INFO ("seeking to %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1221 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1223 res = gst_element_seek (priv->pipeline, 1.0, GST_FORMAT_TIME,
1224 flags, start_type, start, stop_type, stop);
1226 /* and block for the seek to complete */
1227 GST_INFO ("done seeking %d", res);
1228 gst_element_get_state (priv->pipeline, NULL, NULL, -1);
1229 GST_INFO ("prerolled again");
1231 collect_media_stats (media);
1233 GST_INFO ("no seek needed");
1236 g_rec_mutex_unlock (&priv->state_lock);
1243 g_rec_mutex_unlock (&priv->state_lock);
1244 GST_INFO ("media %p is not prepared", media);
1249 g_rec_mutex_unlock (&priv->state_lock);
1250 GST_INFO ("pipeline is not seekable");
1255 g_rec_mutex_unlock (&priv->state_lock);
1256 GST_WARNING ("conversion to npt not supported");
1262 gst_rtsp_media_set_status (GstRTSPMedia * media, GstRTSPMediaStatus status)
1264 GstRTSPMediaPrivate *priv = media->priv;
1266 g_mutex_lock (&priv->lock);
1267 priv->status = status;
1268 GST_DEBUG ("setting new status to %d", status);
1269 g_cond_broadcast (&priv->cond);
1270 g_mutex_unlock (&priv->lock);
1274 * gst_rtsp_media_get_status:
1275 * @media: a #GstRTSPMedia
1277 * Get the status of @media. When @media is busy preparing, this function waits
1278 * until @media is prepared or in error.
1280 * Returns: the status of @media.
1283 gst_rtsp_media_get_status (GstRTSPMedia * media)
1285 GstRTSPMediaPrivate *priv = media->priv;
1286 GstRTSPMediaStatus result;
1289 g_mutex_lock (&priv->lock);
1290 end_time = g_get_monotonic_time () + 20 * G_TIME_SPAN_SECOND;
1291 /* while we are preparing, wait */
1292 while (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING) {
1293 GST_DEBUG ("waiting for status change");
1294 if (!g_cond_wait_until (&priv->cond, &priv->lock, end_time)) {
1295 GST_DEBUG ("timeout, assuming error status");
1296 priv->status = GST_RTSP_MEDIA_STATUS_ERROR;
1299 /* could be success or error */
1300 result = priv->status;
1301 GST_DEBUG ("got status %d", result);
1302 g_mutex_unlock (&priv->lock);
1307 /* called with state-lock */
1309 default_handle_message (GstRTSPMedia * media, GstMessage * message)
1311 GstRTSPMediaPrivate *priv = media->priv;
1312 GstMessageType type;
1314 type = GST_MESSAGE_TYPE (message);
1317 case GST_MESSAGE_STATE_CHANGED:
1319 case GST_MESSAGE_BUFFERING:
1323 gst_message_parse_buffering (message, &percent);
1325 /* no state management needed for live pipelines */
1329 if (percent == 100) {
1330 /* a 100% message means buffering is done */
1331 priv->buffering = FALSE;
1332 /* if the desired state is playing, go back */
1333 if (priv->target_state == GST_STATE_PLAYING) {
1334 GST_INFO ("Buffering done, setting pipeline to PLAYING");
1335 gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
1337 GST_INFO ("Buffering done");
1340 /* buffering busy */
1341 if (priv->buffering == FALSE) {
1342 if (priv->target_state == GST_STATE_PLAYING) {
1343 /* we were not buffering but PLAYING, PAUSE the pipeline. */
1344 GST_INFO ("Buffering, setting pipeline to PAUSED ...");
1345 gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
1347 GST_INFO ("Buffering ...");
1350 priv->buffering = TRUE;
1354 case GST_MESSAGE_LATENCY:
1356 gst_bin_recalculate_latency (GST_BIN_CAST (priv->pipeline));
1359 case GST_MESSAGE_ERROR:
1364 gst_message_parse_error (message, &gerror, &debug);
1365 GST_WARNING ("%p: got error %s (%s)", media, gerror->message, debug);
1366 g_error_free (gerror);
1369 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
1372 case GST_MESSAGE_WARNING:
1377 gst_message_parse_warning (message, &gerror, &debug);
1378 GST_WARNING ("%p: got warning %s (%s)", media, gerror->message, debug);
1379 g_error_free (gerror);
1383 case GST_MESSAGE_ELEMENT:
1385 case GST_MESSAGE_STREAM_STATUS:
1387 case GST_MESSAGE_ASYNC_DONE:
1389 /* when we are dynamically adding pads, the addition of the udpsrc will
1390 * temporarily produce ASYNC_DONE messages. We have to ignore them and
1391 * wait for the final ASYNC_DONE after everything prerolled */
1392 GST_INFO ("%p: ignoring ASYNC_DONE", media);
1394 GST_INFO ("%p: got ASYNC_DONE", media);
1395 collect_media_stats (media);
1397 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
1398 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
1401 case GST_MESSAGE_EOS:
1402 GST_INFO ("%p: got EOS", media);
1404 if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARING) {
1405 GST_DEBUG ("shutting down after EOS");
1406 finish_unprepare (media);
1410 GST_INFO ("%p: got message type %d (%s)", media, type,
1411 gst_message_type_get_name (type));
1418 bus_message (GstBus * bus, GstMessage * message, GstRTSPMedia * media)
1420 GstRTSPMediaPrivate *priv = media->priv;
1421 GstRTSPMediaClass *klass;
1424 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1426 g_rec_mutex_lock (&priv->state_lock);
1427 if (klass->handle_message)
1428 ret = klass->handle_message (media, message);
1431 g_rec_mutex_unlock (&priv->state_lock);
1437 watch_destroyed (GstRTSPMedia * media)
1439 GST_DEBUG_OBJECT (media, "source destroyed");
1440 g_object_unref (media);
1443 /* called from streaming threads */
1445 pad_added_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
1447 GstRTSPMediaPrivate *priv = media->priv;
1448 GstRTSPStream *stream;
1450 /* FIXME, element is likely not a payloader, find the payloader here */
1451 stream = gst_rtsp_media_create_stream (media, element, pad);
1453 g_object_set_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream", stream);
1455 GST_INFO ("pad added %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
1457 g_rec_mutex_lock (&priv->state_lock);
1458 /* we will be adding elements below that will cause ASYNC_DONE to be
1459 * posted in the bus. We want to ignore those messages until the
1460 * pipeline really prerolled. */
1461 priv->adding = TRUE;
1463 /* join the element in the PAUSED state because this callback is
1464 * called from the streaming thread and it is PAUSED */
1465 gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
1466 priv->rtpbin, GST_STATE_PAUSED);
1468 priv->adding = FALSE;
1469 g_rec_mutex_unlock (&priv->state_lock);
1473 pad_removed_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
1475 GstRTSPMediaPrivate *priv = media->priv;
1476 GstRTSPStream *stream;
1478 stream = g_object_get_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream");
1482 GST_INFO ("pad removed %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
1484 g_rec_mutex_lock (&priv->state_lock);
1485 gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
1486 g_rec_mutex_unlock (&priv->state_lock);
1488 gst_rtsp_media_remove_stream (media, stream);
1492 remove_fakesink (GstRTSPMediaPrivate * priv)
1494 GstElement *fakesink;
1496 g_mutex_lock (&priv->lock);
1497 if ((fakesink = priv->fakesink))
1498 gst_object_ref (fakesink);
1499 priv->fakesink = NULL;
1500 g_mutex_unlock (&priv->lock);
1503 gst_bin_remove (GST_BIN (priv->pipeline), fakesink);
1504 gst_element_set_state (fakesink, GST_STATE_NULL);
1505 gst_object_unref (fakesink);
1506 GST_INFO ("removed fakesink");
1511 no_more_pads_cb (GstElement * element, GstRTSPMedia * media)
1513 GstRTSPMediaPrivate *priv = media->priv;
1515 GST_INFO ("no more pads");
1516 remove_fakesink (priv);
1519 typedef struct _DynPaySignalHandlers DynPaySignalHandlers;
1521 struct _DynPaySignalHandlers
1523 gulong pad_added_handler;
1524 gulong pad_removed_handler;
1525 gulong no_more_pads_handler;
1529 * gst_rtsp_media_prepare:
1530 * @media: a #GstRTSPMedia
1532 * Prepare @media for streaming. This function will create the objects
1533 * to manage the streaming. A pipeline must have been set on @media with
1534 * gst_rtsp_media_take_pipeline().
1536 * It will preroll the pipeline and collect vital information about the streams
1537 * such as the duration.
1539 * Returns: %TRUE on success.
1542 gst_rtsp_media_prepare (GstRTSPMedia * media)
1544 GstRTSPMediaPrivate *priv;
1545 GstStateChangeReturn ret;
1546 GstRTSPMediaStatus status;
1548 GstRTSPMediaClass *klass;
1552 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1556 g_rec_mutex_lock (&priv->state_lock);
1557 priv->prepare_count++;
1559 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED)
1562 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
1565 if (priv->status != GST_RTSP_MEDIA_STATUS_UNPREPARED)
1566 goto not_unprepared;
1568 if (!priv->reusable && priv->reused)
1571 priv->rtpbin = gst_element_factory_make ("rtpbin", NULL);
1572 if (priv->rtpbin == NULL)
1575 GST_INFO ("preparing media %p", media);
1577 /* reset some variables */
1578 priv->is_live = FALSE;
1579 priv->seekable = FALSE;
1580 priv->buffering = FALSE;
1581 /* we're preparing now */
1582 priv->status = GST_RTSP_MEDIA_STATUS_PREPARING;
1584 bus = gst_pipeline_get_bus (GST_PIPELINE_CAST (priv->pipeline));
1586 /* add the pipeline bus to our custom mainloop */
1587 priv->source = gst_bus_create_watch (bus);
1588 gst_object_unref (bus);
1590 g_source_set_callback (priv->source, (GSourceFunc) bus_message,
1591 g_object_ref (media), (GDestroyNotify) watch_destroyed);
1593 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1594 priv->id = g_source_attach (priv->source, klass->context);
1596 /* add stuff to the bin */
1597 gst_bin_add (GST_BIN (priv->pipeline), priv->rtpbin);
1599 /* link streams we already have, other streams might appear when we have
1600 * dynamic elements */
1601 for (i = 0; i < priv->streams->len; i++) {
1602 GstRTSPStream *stream;
1604 stream = g_ptr_array_index (priv->streams, i);
1606 gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
1607 priv->rtpbin, GST_STATE_NULL);
1610 for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
1611 GstElement *elem = walk->data;
1612 DynPaySignalHandlers *handlers = g_slice_new (DynPaySignalHandlers);
1614 GST_INFO ("adding callbacks for dynamic element %p", elem);
1616 handlers->pad_added_handler = g_signal_connect (elem, "pad-added",
1617 (GCallback) pad_added_cb, media);
1618 handlers->pad_removed_handler = g_signal_connect (elem, "pad-removed",
1619 (GCallback) pad_removed_cb, media);
1620 handlers->no_more_pads_handler = g_signal_connect (elem, "no-more-pads",
1621 (GCallback) no_more_pads_cb, media);
1623 g_object_set_data (G_OBJECT (elem), "gst-rtsp-dynpay-handlers", handlers);
1625 /* we add a fakesink here in order to make the state change async. We remove
1626 * the fakesink again in the no-more-pads callback. */
1627 priv->fakesink = gst_element_factory_make ("fakesink", "fakesink");
1628 gst_bin_add (GST_BIN (priv->pipeline), priv->fakesink);
1631 GST_INFO ("setting pipeline to PAUSED for media %p", media);
1632 /* first go to PAUSED */
1633 ret = gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
1634 priv->target_state = GST_STATE_PAUSED;
1637 case GST_STATE_CHANGE_SUCCESS:
1638 GST_INFO ("SUCCESS state change for media %p", media);
1639 priv->seekable = TRUE;
1641 case GST_STATE_CHANGE_ASYNC:
1642 GST_INFO ("ASYNC state change for media %p", media);
1643 priv->seekable = TRUE;
1645 case GST_STATE_CHANGE_NO_PREROLL:
1646 /* we need to go to PLAYING */
1647 GST_INFO ("NO_PREROLL state change: live media %p", media);
1648 /* FIXME we disable seeking for live streams for now. We should perform a
1649 * seeking query in preroll instead */
1650 priv->seekable = FALSE;
1651 priv->is_live = TRUE;
1652 ret = gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
1653 if (ret == GST_STATE_CHANGE_FAILURE)
1656 case GST_STATE_CHANGE_FAILURE:
1660 g_rec_mutex_unlock (&priv->state_lock);
1662 /* now wait for all pads to be prerolled, FIXME, we should somehow be
1663 * able to do this async so that we don't block the server thread. */
1664 status = gst_rtsp_media_get_status (media);
1665 if (status == GST_RTSP_MEDIA_STATUS_ERROR)
1668 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_PREPARED], 0, NULL);
1670 GST_INFO ("object %p is prerolled", media);
1677 GST_LOG ("media %p was prepared", media);
1678 g_rec_mutex_unlock (&priv->state_lock);
1684 GST_WARNING ("media %p was not unprepared", media);
1685 priv->prepare_count--;
1686 g_rec_mutex_unlock (&priv->state_lock);
1691 priv->prepare_count--;
1692 g_rec_mutex_unlock (&priv->state_lock);
1693 GST_WARNING ("can not reuse media %p", media);
1698 priv->prepare_count--;
1699 g_rec_mutex_unlock (&priv->state_lock);
1700 GST_WARNING ("no rtpbin element");
1701 g_warning ("failed to create element 'rtpbin', check your installation");
1706 GST_WARNING ("failed to preroll pipeline");
1707 gst_rtsp_media_unprepare (media);
1708 g_rec_mutex_unlock (&priv->state_lock);
1713 /* must be called with state-lock */
1715 finish_unprepare (GstRTSPMedia * media)
1717 GstRTSPMediaPrivate *priv = media->priv;
1721 GST_DEBUG ("shutting down");
1723 gst_element_set_state (priv->pipeline, GST_STATE_NULL);
1724 remove_fakesink (priv);
1726 for (i = 0; i < priv->streams->len; i++) {
1727 GstRTSPStream *stream;
1729 GST_INFO ("Removing elements of stream %d from pipeline", i);
1731 stream = g_ptr_array_index (priv->streams, i);
1733 gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
1736 /* remove the pad signal handlers */
1737 for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
1738 GstElement *elem = walk->data;
1739 DynPaySignalHandlers *handlers;
1742 g_object_steal_data (G_OBJECT (elem), "gst-rtsp-dynpay-handlers");
1743 g_assert (handlers != NULL);
1745 g_signal_handler_disconnect (G_OBJECT (elem), handlers->pad_added_handler);
1746 g_signal_handler_disconnect (G_OBJECT (elem),
1747 handlers->pad_removed_handler);
1748 g_signal_handler_disconnect (G_OBJECT (elem),
1749 handlers->no_more_pads_handler);
1751 g_slice_free (DynPaySignalHandlers, handlers);
1754 gst_bin_remove (GST_BIN (priv->pipeline), priv->rtpbin);
1755 priv->rtpbin = NULL;
1758 gst_object_unref (priv->nettime);
1759 priv->nettime = NULL;
1761 priv->reused = TRUE;
1762 priv->status = GST_RTSP_MEDIA_STATUS_UNPREPARED;
1764 /* when the media is not reusable, this will effectively unref the media and
1766 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_UNPREPARED], 0, NULL);
1768 /* the source has the last ref to the media */
1770 GST_DEBUG ("destroy source");
1771 g_source_destroy (priv->source);
1772 g_source_unref (priv->source);
1776 /* called with state-lock */
1778 default_unprepare (GstRTSPMedia * media)
1780 GstRTSPMediaPrivate *priv = media->priv;
1782 if (priv->eos_shutdown) {
1783 GST_DEBUG ("sending EOS for shutdown");
1784 /* ref so that we don't disappear */
1785 gst_element_send_event (priv->pipeline, gst_event_new_eos ());
1786 /* we need to go to playing again for the EOS to propagate, normally in this
1787 * state, nothing is receiving data from us anymore so this is ok. */
1788 gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
1789 priv->status = GST_RTSP_MEDIA_STATUS_UNPREPARING;
1791 finish_unprepare (media);
1797 * gst_rtsp_media_unprepare:
1798 * @media: a #GstRTSPMedia
1800 * Unprepare @media. After this call, the media should be prepared again before
1801 * it can be used again. If the media is set to be non-reusable, a new instance
1804 * Returns: %TRUE on success.
1807 gst_rtsp_media_unprepare (GstRTSPMedia * media)
1809 GstRTSPMediaPrivate *priv;
1812 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1816 g_rec_mutex_lock (&priv->state_lock);
1817 if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARED)
1818 goto was_unprepared;
1820 priv->prepare_count--;
1821 if (priv->prepare_count > 0)
1824 GST_INFO ("unprepare media %p", media);
1825 priv->target_state = GST_STATE_NULL;
1828 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED) {
1829 GstRTSPMediaClass *klass;
1831 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1832 if (klass->unprepare)
1833 success = klass->unprepare (media);
1835 finish_unprepare (media);
1837 g_rec_mutex_unlock (&priv->state_lock);
1843 g_rec_mutex_unlock (&priv->state_lock);
1844 GST_INFO ("media %p was already unprepared", media);
1849 GST_INFO ("media %p still prepared %d times", media, priv->prepare_count);
1850 g_rec_mutex_unlock (&priv->state_lock);
1855 /* should be called with state-lock */
1857 get_clock_unlocked (GstRTSPMedia * media)
1859 if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED) {
1860 GST_DEBUG_OBJECT (media, "media was not prepared");
1863 return gst_pipeline_get_clock (GST_PIPELINE_CAST (media->priv->pipeline));
1867 * gst_rtsp_media_get_clock:
1868 * @media: a #GstRTSPMedia
1870 * Get the clock that is used by the pipeline in @media.
1872 * @media must be prepared before this method returns a valid clock object.
1874 * Returns: the #GstClock used by @media. unref after usage.
1877 gst_rtsp_media_get_clock (GstRTSPMedia * media)
1880 GstRTSPMediaPrivate *priv;
1882 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1886 g_rec_mutex_lock (&priv->state_lock);
1887 clock = get_clock_unlocked (media);
1888 g_rec_mutex_unlock (&priv->state_lock);
1894 * gst_rtsp_media_get_base_time:
1895 * @media: a #GstRTSPMedia
1897 * Get the base_time that is used by the pipeline in @media.
1899 * @media must be prepared before this method returns a valid base_time.
1901 * Returns: the base_time used by @media.
1904 gst_rtsp_media_get_base_time (GstRTSPMedia * media)
1906 GstClockTime result;
1907 GstRTSPMediaPrivate *priv;
1909 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_CLOCK_TIME_NONE);
1913 g_rec_mutex_lock (&priv->state_lock);
1914 if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
1917 result = gst_element_get_base_time (media->priv->pipeline);
1918 g_rec_mutex_unlock (&priv->state_lock);
1925 g_rec_mutex_unlock (&priv->state_lock);
1926 GST_DEBUG_OBJECT (media, "media was not prepared");
1927 return GST_CLOCK_TIME_NONE;
1932 * gst_rtsp_media_get_time_provider:
1933 * @media: a #GstRTSPMedia
1934 * @address: an address or NULL
1935 * @port: a port or 0
1937 * Get the #GstNetTimeProvider for the clock used by @media. The time provider
1938 * will listen on @address and @port for client time requests.
1940 * Returns: the #GstNetTimeProvider of @media.
1942 GstNetTimeProvider *
1943 gst_rtsp_media_get_time_provider (GstRTSPMedia * media, const gchar * address,
1946 GstRTSPMediaPrivate *priv;
1947 GstNetTimeProvider *provider = NULL;
1949 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1953 g_rec_mutex_lock (&priv->state_lock);
1954 if (priv->time_provider) {
1955 if ((provider = priv->nettime) == NULL) {
1958 if (priv->time_provider && (clock = get_clock_unlocked (media))) {
1959 provider = gst_net_time_provider_new (clock, address, port);
1960 gst_object_unref (clock);
1962 priv->nettime = provider;
1966 g_rec_mutex_unlock (&priv->state_lock);
1969 gst_object_ref (provider);
1975 * gst_rtsp_media_set_state:
1976 * @media: a #GstRTSPMedia
1977 * @state: the target state of the media
1978 * @transports: a #GPtrArray of #GstRTSPStreamTransport pointers
1980 * Set the state of @media to @state and for the transports in @transports.
1982 * @media must be prepared with gst_rtsp_media_prepare();
1984 * Returns: %TRUE on success.
1987 gst_rtsp_media_set_state (GstRTSPMedia * media, GstState state,
1988 GPtrArray * transports)
1990 GstRTSPMediaPrivate *priv;
1992 gboolean activate, deactivate, do_state;
1995 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1996 g_return_val_if_fail (transports != NULL, FALSE);
2000 g_rec_mutex_lock (&priv->state_lock);
2001 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
2004 /* NULL and READY are the same */
2005 if (state == GST_STATE_READY)
2006 state = GST_STATE_NULL;
2008 activate = deactivate = FALSE;
2010 GST_INFO ("going to state %s media %p", gst_element_state_get_name (state),
2014 case GST_STATE_NULL:
2015 case GST_STATE_PAUSED:
2016 /* we're going from PLAYING to PAUSED, READY or NULL, deactivate */
2017 if (priv->target_state == GST_STATE_PLAYING)
2020 case GST_STATE_PLAYING:
2021 /* we're going to PLAYING, activate */
2027 old_active = priv->n_active;
2029 for (i = 0; i < transports->len; i++) {
2030 GstRTSPStreamTransport *trans;
2032 /* we need a non-NULL entry in the array */
2033 trans = g_ptr_array_index (transports, i);
2038 if (gst_rtsp_stream_transport_set_active (trans, TRUE))
2040 } else if (deactivate) {
2041 if (gst_rtsp_stream_transport_set_active (trans, FALSE))
2046 /* we just activated the first media, do the playing state change */
2047 if (old_active == 0 && activate)
2049 /* if we have no more active media, do the downward state changes */
2050 else if (priv->n_active == 0)
2055 GST_INFO ("state %d active %d media %p do_state %d", state, priv->n_active,
2058 if (priv->target_state != state) {
2060 if (state == GST_STATE_NULL) {
2061 gst_rtsp_media_unprepare (media);
2063 GST_INFO ("state %s media %p", gst_element_state_get_name (state),
2065 priv->target_state = state;
2066 /* when we are buffering, don't update the state yet, this will be done
2067 * when buffering finishes */
2068 if (priv->buffering) {
2069 GST_INFO ("Buffering busy, delay state change");
2071 gst_element_set_state (priv->pipeline, state);
2075 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STATE], 0, state,
2079 /* remember where we are */
2080 if (state != GST_STATE_NULL && (state == GST_STATE_PAUSED ||
2081 old_active != priv->n_active))
2082 collect_media_stats (media);
2084 g_rec_mutex_unlock (&priv->state_lock);
2091 GST_WARNING ("media %p was not prepared", media);
2092 g_rec_mutex_unlock (&priv->state_lock);
2097 /* called with state-lock */
2099 default_convert_range (GstRTSPMedia * media, GstRTSPTimeRange * range,
2100 GstRTSPRangeUnit unit)
2102 return gst_rtsp_range_convert_units (range, unit);
2106 default_query_position (GstRTSPMedia * media, gint64 * position)
2108 return gst_element_query_position (media->priv->pipeline, GST_FORMAT_TIME,
2113 default_query_stop (GstRTSPMedia * media, gint64 * stop)
2118 query = gst_query_new_segment (GST_FORMAT_TIME);
2119 if ((res = gst_element_query (media->priv->pipeline, query))) {
2121 gst_query_parse_segment (query, NULL, &format, NULL, stop);
2122 if (format != GST_FORMAT_TIME)
2125 gst_query_unref (query);