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);
130 static guint gst_rtsp_media_signals[SIGNAL_LAST] = { 0 };
132 G_DEFINE_TYPE (GstRTSPMedia, gst_rtsp_media, G_TYPE_OBJECT);
135 gst_rtsp_media_class_init (GstRTSPMediaClass * klass)
137 GObjectClass *gobject_class;
139 g_type_class_add_private (klass, sizeof (GstRTSPMediaPrivate));
141 gobject_class = G_OBJECT_CLASS (klass);
143 gobject_class->get_property = gst_rtsp_media_get_property;
144 gobject_class->set_property = gst_rtsp_media_set_property;
145 gobject_class->finalize = gst_rtsp_media_finalize;
147 g_object_class_install_property (gobject_class, PROP_SHARED,
148 g_param_spec_boolean ("shared", "Shared",
149 "If this media pipeline can be shared", DEFAULT_SHARED,
150 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
152 g_object_class_install_property (gobject_class, PROP_REUSABLE,
153 g_param_spec_boolean ("reusable", "Reusable",
154 "If this media pipeline can be reused after an unprepare",
155 DEFAULT_REUSABLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
157 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
158 g_param_spec_flags ("protocols", "Protocols",
159 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
160 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
162 g_object_class_install_property (gobject_class, PROP_EOS_SHUTDOWN,
163 g_param_spec_boolean ("eos-shutdown", "EOS Shutdown",
164 "Send an EOS event to the pipeline before unpreparing",
165 DEFAULT_EOS_SHUTDOWN, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
167 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
168 g_param_spec_uint ("buffer-size", "Buffer Size",
169 "The kernel UDP buffer size to use", 0, G_MAXUINT,
170 DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
172 g_object_class_install_property (gobject_class, PROP_ELEMENT,
173 g_param_spec_object ("element", "The Element",
174 "The GstBin to use for streaming the media", GST_TYPE_ELEMENT,
175 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
177 g_object_class_install_property (gobject_class, PROP_EOS_SHUTDOWN,
178 g_param_spec_boolean ("time-provider", "Time Provider",
179 "Use a NetTimeProvider for clients",
180 DEFAULT_TIME_PROVIDER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
182 gst_rtsp_media_signals[SIGNAL_NEW_STREAM] =
183 g_signal_new ("new-stream", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
184 G_STRUCT_OFFSET (GstRTSPMediaClass, new_stream), NULL, NULL,
185 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_RTSP_STREAM);
187 gst_rtsp_media_signals[SIGNAL_REMOVED_STREAM] =
188 g_signal_new ("removed-stream", G_TYPE_FROM_CLASS (klass),
189 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPMediaClass, removed_stream),
190 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
191 GST_TYPE_RTSP_STREAM);
193 gst_rtsp_media_signals[SIGNAL_PREPARED] =
194 g_signal_new ("prepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
195 G_STRUCT_OFFSET (GstRTSPMediaClass, prepared), NULL, NULL,
196 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
198 gst_rtsp_media_signals[SIGNAL_UNPREPARED] =
199 g_signal_new ("unprepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
200 G_STRUCT_OFFSET (GstRTSPMediaClass, unprepared), NULL, NULL,
201 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
203 gst_rtsp_media_signals[SIGNAL_NEW_STATE] =
204 g_signal_new ("new-state", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
205 G_STRUCT_OFFSET (GstRTSPMediaClass, new_state), NULL, NULL,
206 g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 0, G_TYPE_INT);
208 klass->context = g_main_context_new ();
209 klass->loop = g_main_loop_new (klass->context, TRUE);
211 GST_DEBUG_CATEGORY_INIT (rtsp_media_debug, "rtspmedia", 0, "GstRTSPMedia");
213 klass->thread = g_thread_new ("Bus Thread", (GThreadFunc) do_loop, klass);
215 klass->handle_message = default_handle_message;
216 klass->unprepare = default_unprepare;
217 klass->convert_range = default_convert_range;
221 gst_rtsp_media_init (GstRTSPMedia * media)
223 GstRTSPMediaPrivate *priv = GST_RTSP_MEDIA_GET_PRIVATE (media);
227 priv->streams = g_ptr_array_new_with_free_func (g_object_unref);
228 g_mutex_init (&priv->lock);
229 g_cond_init (&priv->cond);
230 g_rec_mutex_init (&priv->state_lock);
232 priv->shared = DEFAULT_SHARED;
233 priv->reusable = DEFAULT_REUSABLE;
234 priv->protocols = DEFAULT_PROTOCOLS;
235 priv->eos_shutdown = DEFAULT_EOS_SHUTDOWN;
236 priv->buffer_size = DEFAULT_BUFFER_SIZE;
237 priv->time_provider = DEFAULT_TIME_PROVIDER;
241 gst_rtsp_media_finalize (GObject * obj)
243 GstRTSPMediaPrivate *priv;
246 media = GST_RTSP_MEDIA (obj);
249 GST_INFO ("finalize media %p", media);
251 g_ptr_array_unref (priv->streams);
253 g_list_free_full (priv->dynamic, gst_object_unref);
256 gst_object_unref (priv->pipeline);
258 gst_object_unref (priv->nettime);
259 gst_object_unref (priv->element);
261 g_object_unref (priv->auth);
263 g_object_unref (priv->pool);
264 g_mutex_clear (&priv->lock);
265 g_cond_clear (&priv->cond);
266 g_rec_mutex_clear (&priv->state_lock);
268 G_OBJECT_CLASS (gst_rtsp_media_parent_class)->finalize (obj);
272 gst_rtsp_media_get_property (GObject * object, guint propid,
273 GValue * value, GParamSpec * pspec)
275 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
279 g_value_set_object (value, media->priv->element);
282 g_value_set_boolean (value, gst_rtsp_media_is_shared (media));
285 g_value_set_boolean (value, gst_rtsp_media_is_reusable (media));
288 g_value_set_flags (value, gst_rtsp_media_get_protocols (media));
290 case PROP_EOS_SHUTDOWN:
291 g_value_set_boolean (value, gst_rtsp_media_is_eos_shutdown (media));
293 case PROP_BUFFER_SIZE:
294 g_value_set_uint (value, gst_rtsp_media_get_buffer_size (media));
296 case PROP_TIME_PROVIDER:
297 g_value_set_boolean (value, gst_rtsp_media_is_time_provider (media));
300 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
305 gst_rtsp_media_set_property (GObject * object, guint propid,
306 const GValue * value, GParamSpec * pspec)
308 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
312 media->priv->element = g_value_get_object (value);
313 gst_object_ref_sink (media->priv->element);
316 gst_rtsp_media_set_shared (media, g_value_get_boolean (value));
319 gst_rtsp_media_set_reusable (media, g_value_get_boolean (value));
322 gst_rtsp_media_set_protocols (media, g_value_get_flags (value));
324 case PROP_EOS_SHUTDOWN:
325 gst_rtsp_media_set_eos_shutdown (media, g_value_get_boolean (value));
327 case PROP_BUFFER_SIZE:
328 gst_rtsp_media_set_buffer_size (media, g_value_get_uint (value));
330 case PROP_TIME_PROVIDER:
331 gst_rtsp_media_use_time_provider (media, g_value_get_boolean (value));
334 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
339 do_loop (GstRTSPMediaClass * klass)
341 GST_INFO ("enter mainloop");
342 g_main_loop_run (klass->loop);
343 GST_INFO ("exit mainloop");
348 /* must be called with state lock */
350 collect_media_stats (GstRTSPMedia * media)
352 GstRTSPMediaPrivate *priv = media->priv;
354 gint64 position, stop;
356 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
357 priv->status != GST_RTSP_MEDIA_STATUS_PREPARING)
360 priv->range.unit = GST_RTSP_RANGE_NPT;
362 GST_INFO ("collect media stats");
365 priv->range.min.type = GST_RTSP_TIME_NOW;
366 priv->range.min.seconds = -1;
367 priv->range_start = -1;
368 priv->range.max.type = GST_RTSP_TIME_END;
369 priv->range.max.seconds = -1;
370 priv->range_stop = -1;
372 /* get the position */
373 if (!gst_element_query_position (priv->pipeline, GST_FORMAT_TIME,
375 GST_INFO ("position query failed");
379 /* get the current segment stop */
380 query = gst_query_new_segment (GST_FORMAT_TIME);
381 if (gst_element_query (priv->pipeline, query)) {
383 gst_query_parse_segment (query, NULL, &format, NULL, &stop);
384 if (format != GST_FORMAT_TIME)
387 GST_INFO ("segment query failed");
390 gst_query_unref (query);
392 GST_INFO ("stats: position %" GST_TIME_FORMAT ", stop %"
393 GST_TIME_FORMAT, GST_TIME_ARGS (position), GST_TIME_ARGS (stop));
395 if (position == -1) {
396 priv->range.min.type = GST_RTSP_TIME_NOW;
397 priv->range.min.seconds = -1;
398 priv->range_start = -1;
400 priv->range.min.type = GST_RTSP_TIME_SECONDS;
401 priv->range.min.seconds = ((gdouble) position) / GST_SECOND;
402 priv->range_start = position;
405 priv->range.max.type = GST_RTSP_TIME_END;
406 priv->range.max.seconds = -1;
407 priv->range_stop = -1;
409 priv->range.max.type = GST_RTSP_TIME_SECONDS;
410 priv->range.max.seconds = ((gdouble) stop) / GST_SECOND;
411 priv->range_stop = stop;
417 * gst_rtsp_media_new:
418 * @element: (transfer full): a #GstElement
420 * Create a new #GstRTSPMedia instance. @element is the bin element that
421 * provides the different streams. The #GstRTSPMedia object contains the
422 * element to produce RTP data for one or more related (audio/video/..)
425 * Ownership is taken of @element.
427 * Returns: a new #GstRTSPMedia object.
430 gst_rtsp_media_new (GstElement * element)
432 GstRTSPMedia *result;
434 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
436 result = g_object_new (GST_TYPE_RTSP_MEDIA, "element", element, NULL);
442 * gst_rtsp_media_get_element:
443 * @media: a #GstRTSPMedia
445 * Get the element that was used when constructing @media.
447 * Returns: a #GstElement. Unref after usage.
450 gst_rtsp_media_get_element (GstRTSPMedia * media)
452 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
454 return gst_object_ref (media->priv->element);
458 * gst_rtsp_media_take_pipeline:
459 * @media: a #GstRTSPMedia
460 * @pipeline: (transfer full): a #GstPipeline
462 * Set @pipeline as the #GstPipeline for @media. Ownership is
463 * taken of @pipeline.
466 gst_rtsp_media_take_pipeline (GstRTSPMedia * media, GstPipeline * pipeline)
468 GstRTSPMediaPrivate *priv;
470 GstNetTimeProvider *nettime;
472 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
473 g_return_if_fail (GST_IS_PIPELINE (pipeline));
477 g_mutex_lock (&priv->lock);
478 old = priv->pipeline;
479 priv->pipeline = GST_ELEMENT_CAST (pipeline);
480 nettime = priv->nettime;
481 priv->nettime = NULL;
482 g_mutex_unlock (&priv->lock);
485 gst_object_unref (old);
488 gst_object_unref (nettime);
490 gst_bin_add (GST_BIN_CAST (pipeline), priv->element);
494 * gst_rtsp_media_set_shared:
495 * @media: a #GstRTSPMedia
496 * @shared: the new value
498 * Set or unset if the pipeline for @media can be shared will multiple clients.
499 * When @shared is %TRUE, client requests for this media will share the media
503 gst_rtsp_media_set_shared (GstRTSPMedia * media, gboolean shared)
505 GstRTSPMediaPrivate *priv;
507 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
511 g_mutex_lock (&priv->lock);
512 priv->shared = shared;
513 g_mutex_unlock (&priv->lock);
517 * gst_rtsp_media_is_shared:
518 * @media: a #GstRTSPMedia
520 * Check if the pipeline for @media can be shared between multiple clients.
522 * Returns: %TRUE if the media can be shared between clients.
525 gst_rtsp_media_is_shared (GstRTSPMedia * media)
527 GstRTSPMediaPrivate *priv;
530 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
534 g_mutex_lock (&priv->lock);
536 g_mutex_unlock (&priv->lock);
542 * gst_rtsp_media_set_reusable:
543 * @media: a #GstRTSPMedia
544 * @reusable: the new value
546 * Set or unset if the pipeline for @media can be reused after the pipeline has
550 gst_rtsp_media_set_reusable (GstRTSPMedia * media, gboolean reusable)
552 GstRTSPMediaPrivate *priv;
554 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
558 g_mutex_lock (&priv->lock);
559 priv->reusable = reusable;
560 g_mutex_unlock (&priv->lock);
564 * gst_rtsp_media_is_reusable:
565 * @media: a #GstRTSPMedia
567 * Check if the pipeline for @media can be reused after an unprepare.
569 * Returns: %TRUE if the media can be reused
572 gst_rtsp_media_is_reusable (GstRTSPMedia * media)
574 GstRTSPMediaPrivate *priv;
577 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
581 g_mutex_lock (&priv->lock);
582 res = priv->reusable;
583 g_mutex_unlock (&priv->lock);
589 * gst_rtsp_media_set_protocols:
590 * @media: a #GstRTSPMedia
591 * @protocols: the new flags
593 * Configure the allowed lower transport for @media.
596 gst_rtsp_media_set_protocols (GstRTSPMedia * media, GstRTSPLowerTrans protocols)
598 GstRTSPMediaPrivate *priv;
600 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
604 g_mutex_lock (&priv->lock);
605 priv->protocols = protocols;
606 g_mutex_unlock (&priv->lock);
610 * gst_rtsp_media_get_protocols:
611 * @media: a #GstRTSPMedia
613 * Get the allowed protocols of @media.
615 * Returns: a #GstRTSPLowerTrans
618 gst_rtsp_media_get_protocols (GstRTSPMedia * media)
620 GstRTSPMediaPrivate *priv;
621 GstRTSPLowerTrans res;
623 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media),
624 GST_RTSP_LOWER_TRANS_UNKNOWN);
628 g_mutex_lock (&priv->lock);
629 res = priv->protocols;
630 g_mutex_unlock (&priv->lock);
636 * gst_rtsp_media_set_eos_shutdown:
637 * @media: a #GstRTSPMedia
638 * @eos_shutdown: the new value
640 * Set or unset if an EOS event will be sent to the pipeline for @media before
644 gst_rtsp_media_set_eos_shutdown (GstRTSPMedia * media, gboolean eos_shutdown)
646 GstRTSPMediaPrivate *priv;
648 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
652 g_mutex_lock (&priv->lock);
653 priv->eos_shutdown = eos_shutdown;
654 g_mutex_unlock (&priv->lock);
658 * gst_rtsp_media_is_eos_shutdown:
659 * @media: a #GstRTSPMedia
661 * Check if the pipeline for @media will send an EOS down the pipeline before
664 * Returns: %TRUE if the media will send EOS before unpreparing.
667 gst_rtsp_media_is_eos_shutdown (GstRTSPMedia * media)
669 GstRTSPMediaPrivate *priv;
672 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
676 g_mutex_lock (&priv->lock);
677 res = priv->eos_shutdown;
678 g_mutex_unlock (&priv->lock);
684 * gst_rtsp_media_set_buffer_size:
685 * @media: a #GstRTSPMedia
686 * @size: the new value
688 * Set the kernel UDP buffer size.
691 gst_rtsp_media_set_buffer_size (GstRTSPMedia * media, guint size)
693 GstRTSPMediaPrivate *priv;
695 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
697 GST_LOG_OBJECT (media, "set buffer size %u", size);
701 g_mutex_lock (&priv->lock);
702 priv->buffer_size = size;
703 g_mutex_unlock (&priv->lock);
707 * gst_rtsp_media_get_buffer_size:
708 * @media: a #GstRTSPMedia
710 * Get the kernel UDP buffer size.
712 * Returns: the kernel UDP buffer size.
715 gst_rtsp_media_get_buffer_size (GstRTSPMedia * media)
717 GstRTSPMediaPrivate *priv;
720 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
724 g_mutex_unlock (&priv->lock);
725 res = priv->buffer_size;
726 g_mutex_unlock (&priv->lock);
732 * gst_rtsp_media_use_time_provider:
733 * @media: a #GstRTSPMedia
735 * Set @media to provide a GstNetTimeProvider.
738 gst_rtsp_media_use_time_provider (GstRTSPMedia * media, gboolean time_provider)
740 GstRTSPMediaPrivate *priv;
742 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
746 g_mutex_lock (&priv->lock);
747 priv->time_provider = time_provider;
748 g_mutex_unlock (&priv->lock);
752 * gst_rtsp_media_is_time_provider:
753 * @media: a #GstRTSPMedia
755 * Check if @media can provide a #GstNetTimeProvider for its pipeline clock.
757 * Use gst_rtsp_media_get_time_provider() to get the network clock.
759 * Returns: %TRUE if @media can provide a #GstNetTimeProvider.
762 gst_rtsp_media_is_time_provider (GstRTSPMedia * media)
764 GstRTSPMediaPrivate *priv;
767 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
771 g_mutex_unlock (&priv->lock);
772 res = priv->time_provider;
773 g_mutex_unlock (&priv->lock);
779 * gst_rtsp_media_set_auth:
780 * @media: a #GstRTSPMedia
781 * @auth: a #GstRTSPAuth
783 * configure @auth to be used as the authentication manager of @media.
786 gst_rtsp_media_set_auth (GstRTSPMedia * media, GstRTSPAuth * auth)
788 GstRTSPMediaPrivate *priv;
791 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
795 GST_LOG_OBJECT (media, "set auth %p", auth);
797 g_mutex_lock (&priv->lock);
798 if ((old = priv->auth) != auth)
799 priv->auth = auth ? g_object_ref (auth) : NULL;
802 g_mutex_unlock (&priv->lock);
805 g_object_unref (old);
809 * gst_rtsp_media_get_auth:
810 * @media: a #GstRTSPMedia
812 * Get the #GstRTSPAuth used as the authentication manager of @media.
814 * Returns: (transfer full): the #GstRTSPAuth of @media. g_object_unref() after
818 gst_rtsp_media_get_auth (GstRTSPMedia * media)
820 GstRTSPMediaPrivate *priv;
823 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
827 g_mutex_lock (&priv->lock);
828 if ((result = priv->auth))
829 g_object_ref (result);
830 g_mutex_unlock (&priv->lock);
836 * gst_rtsp_media_set_address_pool:
837 * @media: a #GstRTSPMedia
838 * @pool: a #GstRTSPAddressPool
840 * configure @pool to be used as the address pool of @media.
843 gst_rtsp_media_set_address_pool (GstRTSPMedia * media,
844 GstRTSPAddressPool * pool)
846 GstRTSPMediaPrivate *priv;
847 GstRTSPAddressPool *old;
849 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
853 GST_LOG_OBJECT (media, "set address pool %p", pool);
855 g_mutex_lock (&priv->lock);
856 if ((old = priv->pool) != pool)
857 priv->pool = pool ? g_object_ref (pool) : NULL;
860 g_ptr_array_foreach (priv->streams, (GFunc) gst_rtsp_stream_set_address_pool,
862 g_mutex_unlock (&priv->lock);
865 g_object_unref (old);
869 * gst_rtsp_media_get_address_pool:
870 * @media: a #GstRTSPMedia
872 * Get the #GstRTSPAddressPool used as the address pool of @media.
874 * Returns: (transfer full): the #GstRTSPAddressPool of @media. g_object_unref() after
878 gst_rtsp_media_get_address_pool (GstRTSPMedia * media)
880 GstRTSPMediaPrivate *priv;
881 GstRTSPAddressPool *result;
883 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
887 g_mutex_lock (&priv->lock);
888 if ((result = priv->pool))
889 g_object_ref (result);
890 g_mutex_unlock (&priv->lock);
896 * gst_rtsp_media_collect_streams:
897 * @media: a #GstRTSPMedia
899 * Find all payloader elements, they should be named pay%d in the
900 * element of @media, and create #GstRTSPStreams for them.
902 * Collect all dynamic elements, named dynpay%d, and add them to
903 * the list of dynamic elements.
906 gst_rtsp_media_collect_streams (GstRTSPMedia * media)
908 GstRTSPMediaPrivate *priv;
909 GstElement *element, *elem;
914 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
917 element = priv->element;
920 for (i = 0; have_elem; i++) {
925 name = g_strdup_printf ("pay%d", i);
926 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
927 GST_INFO ("found stream %d with payloader %p", i, elem);
929 /* take the pad of the payloader */
930 pad = gst_element_get_static_pad (elem, "src");
931 /* create the stream */
932 gst_rtsp_media_create_stream (media, elem, pad);
933 gst_object_unref (pad);
934 gst_object_unref (elem);
940 name = g_strdup_printf ("dynpay%d", i);
941 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
942 /* a stream that will dynamically create pads to provide RTP packets */
944 GST_INFO ("found dynamic element %d, %p", i, elem);
946 g_mutex_lock (&priv->lock);
947 priv->dynamic = g_list_prepend (priv->dynamic, elem);
948 g_mutex_unlock (&priv->lock);
957 * gst_rtsp_media_create_stream:
958 * @media: a #GstRTSPMedia
959 * @payloader: a #GstElement
960 * @srcpad: a source #GstPad
962 * Create a new stream in @media that provides RTP data on @srcpad.
963 * @srcpad should be a pad of an element inside @media->element.
965 * Returns: (transfer none): a new #GstRTSPStream that remains valid for as long
969 gst_rtsp_media_create_stream (GstRTSPMedia * media, GstElement * payloader,
972 GstRTSPMediaPrivate *priv;
973 GstRTSPStream *stream;
978 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
979 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
980 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
981 g_return_val_if_fail (GST_PAD_IS_SRC (pad), NULL);
985 g_mutex_lock (&priv->lock);
986 idx = priv->streams->len;
988 GST_DEBUG ("media %p: creating stream with index %d", media, idx);
990 name = g_strdup_printf ("src_%u", idx);
991 srcpad = gst_ghost_pad_new (name, pad);
992 gst_pad_set_active (srcpad, TRUE);
993 gst_element_add_pad (priv->element, srcpad);
996 stream = gst_rtsp_stream_new (idx, payloader, srcpad);
998 gst_rtsp_stream_set_address_pool (stream, priv->pool);
1000 g_ptr_array_add (priv->streams, stream);
1001 g_mutex_unlock (&priv->lock);
1003 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STREAM], 0, stream,
1010 gst_rtsp_media_remove_stream (GstRTSPMedia * media, GstRTSPStream * stream)
1012 GstRTSPMediaPrivate *priv;
1017 g_mutex_lock (&priv->lock);
1018 /* remove the ghostpad */
1019 srcpad = gst_rtsp_stream_get_srcpad (stream);
1020 gst_element_remove_pad (priv->element, srcpad);
1021 gst_object_unref (srcpad);
1022 /* now remove the stream */
1023 g_object_ref (stream);
1024 g_ptr_array_remove (priv->streams, stream);
1025 g_mutex_unlock (&priv->lock);
1027 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_REMOVED_STREAM], 0,
1030 g_object_unref (stream);
1034 * gst_rtsp_media_n_streams:
1035 * @media: a #GstRTSPMedia
1037 * Get the number of streams in this media.
1039 * Returns: The number of streams.
1042 gst_rtsp_media_n_streams (GstRTSPMedia * media)
1044 GstRTSPMediaPrivate *priv;
1047 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), 0);
1051 g_mutex_lock (&priv->lock);
1052 res = priv->streams->len;
1053 g_mutex_unlock (&priv->lock);
1059 * gst_rtsp_media_get_stream:
1060 * @media: a #GstRTSPMedia
1061 * @idx: the stream index
1063 * Retrieve the stream with index @idx from @media.
1065 * Returns: (transfer none): the #GstRTSPStream at index @idx or %NULL when a stream with
1066 * that index did not exist.
1069 gst_rtsp_media_get_stream (GstRTSPMedia * media, guint idx)
1071 GstRTSPMediaPrivate *priv;
1074 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1078 g_mutex_lock (&priv->lock);
1079 if (idx < priv->streams->len)
1080 res = g_ptr_array_index (priv->streams, idx);
1083 g_mutex_unlock (&priv->lock);
1089 * gst_rtsp_media_get_range_string:
1090 * @media: a #GstRTSPMedia
1091 * @play: for the PLAY request
1092 * @unit: the unit to use for the string
1094 * Get the current range as a string. @media must be prepared with
1095 * gst_rtsp_media_prepare ().
1097 * Returns: The range as a string, g_free() after usage.
1100 gst_rtsp_media_get_range_string (GstRTSPMedia * media, gboolean play,
1101 GstRTSPRangeUnit unit)
1103 GstRTSPMediaClass *klass;
1104 GstRTSPMediaPrivate *priv;
1106 GstRTSPTimeRange range;
1108 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1109 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1110 g_return_val_if_fail (klass->convert_range != NULL, FALSE);
1114 g_rec_mutex_lock (&priv->state_lock);
1115 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
1118 g_mutex_lock (&priv->lock);
1120 range = priv->range;
1122 if (!play && priv->n_active > 0) {
1123 range.min.type = GST_RTSP_TIME_NOW;
1124 range.min.seconds = -1;
1126 g_mutex_unlock (&priv->lock);
1127 g_rec_mutex_unlock (&priv->state_lock);
1129 if (!klass->convert_range (media, &range, unit)) {
1130 goto conversion_failed;
1133 result = gst_rtsp_range_to_string (&range);
1140 GST_WARNING ("media %p was not prepared", media);
1141 g_rec_mutex_unlock (&priv->state_lock);
1146 GST_WARNING ("range conversion to unit %d failed", unit);
1147 g_rec_mutex_unlock (&priv->state_lock);
1153 * gst_rtsp_media_seek:
1154 * @media: a #GstRTSPMedia
1155 * @range: a #GstRTSPTimeRange
1157 * Seek the pipeline of @media to @range. @media must be prepared with
1158 * gst_rtsp_media_prepare().
1160 * Returns: %TRUE on success.
1163 gst_rtsp_media_seek (GstRTSPMedia * media, GstRTSPTimeRange * range)
1165 GstRTSPMediaClass *klass;
1166 GstRTSPMediaPrivate *priv;
1169 GstClockTime start, stop;
1170 GstSeekType start_type, stop_type;
1172 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1174 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1175 g_return_val_if_fail (range != NULL, FALSE);
1176 g_return_val_if_fail (klass->convert_range != NULL, FALSE);
1180 g_rec_mutex_lock (&priv->state_lock);
1181 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
1184 if (!priv->seekable)
1187 /* depends on the current playing state of the pipeline. We might need to
1188 * queue this until we get EOS. */
1189 flags = GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_KEY_UNIT;
1191 start_type = stop_type = GST_SEEK_TYPE_NONE;
1193 if (!klass->convert_range (media, range, GST_RTSP_RANGE_NPT))
1195 gst_rtsp_range_get_times (range, &start, &stop);
1197 GST_INFO ("got %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1198 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1199 GST_INFO ("current %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1200 GST_TIME_ARGS (priv->range_start), GST_TIME_ARGS (priv->range_stop));
1202 if (priv->range_start == start)
1203 start = GST_CLOCK_TIME_NONE;
1204 else if (start != GST_CLOCK_TIME_NONE)
1205 start_type = GST_SEEK_TYPE_SET;
1207 if (priv->range_stop == stop)
1208 stop = GST_CLOCK_TIME_NONE;
1209 else if (stop != GST_CLOCK_TIME_NONE)
1210 stop_type = GST_SEEK_TYPE_SET;
1212 if (start != GST_CLOCK_TIME_NONE || stop != GST_CLOCK_TIME_NONE) {
1213 GST_INFO ("seeking to %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1214 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1216 res = gst_element_seek (priv->pipeline, 1.0, GST_FORMAT_TIME,
1217 flags, start_type, start, stop_type, stop);
1219 /* and block for the seek to complete */
1220 GST_INFO ("done seeking %d", res);
1221 gst_element_get_state (priv->pipeline, NULL, NULL, -1);
1222 GST_INFO ("prerolled again");
1224 collect_media_stats (media);
1226 GST_INFO ("no seek needed");
1229 g_rec_mutex_unlock (&priv->state_lock);
1236 g_rec_mutex_unlock (&priv->state_lock);
1237 GST_INFO ("media %p is not prepared", media);
1242 g_rec_mutex_unlock (&priv->state_lock);
1243 GST_INFO ("pipeline is not seekable");
1248 g_rec_mutex_unlock (&priv->state_lock);
1249 GST_WARNING ("conversion to npt not supported");
1255 gst_rtsp_media_set_status (GstRTSPMedia * media, GstRTSPMediaStatus status)
1257 GstRTSPMediaPrivate *priv = media->priv;
1259 g_mutex_lock (&priv->lock);
1260 priv->status = status;
1261 GST_DEBUG ("setting new status to %d", status);
1262 g_cond_broadcast (&priv->cond);
1263 g_mutex_unlock (&priv->lock);
1267 * gst_rtsp_media_get_status:
1268 * @media: a #GstRTSPMedia
1270 * Get the status of @media. When @media is busy preparing, this function waits
1271 * until @media is prepared or in error.
1273 * Returns: the status of @media.
1276 gst_rtsp_media_get_status (GstRTSPMedia * media)
1278 GstRTSPMediaPrivate *priv = media->priv;
1279 GstRTSPMediaStatus result;
1282 g_mutex_lock (&priv->lock);
1283 end_time = g_get_monotonic_time () + 20 * G_TIME_SPAN_SECOND;
1284 /* while we are preparing, wait */
1285 while (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING) {
1286 GST_DEBUG ("waiting for status change");
1287 if (!g_cond_wait_until (&priv->cond, &priv->lock, end_time)) {
1288 GST_DEBUG ("timeout, assuming error status");
1289 priv->status = GST_RTSP_MEDIA_STATUS_ERROR;
1292 /* could be success or error */
1293 result = priv->status;
1294 GST_DEBUG ("got status %d", result);
1295 g_mutex_unlock (&priv->lock);
1300 /* called with state-lock */
1302 default_handle_message (GstRTSPMedia * media, GstMessage * message)
1304 GstRTSPMediaPrivate *priv = media->priv;
1305 GstMessageType type;
1307 type = GST_MESSAGE_TYPE (message);
1310 case GST_MESSAGE_STATE_CHANGED:
1312 case GST_MESSAGE_BUFFERING:
1316 gst_message_parse_buffering (message, &percent);
1318 /* no state management needed for live pipelines */
1322 if (percent == 100) {
1323 /* a 100% message means buffering is done */
1324 priv->buffering = FALSE;
1325 /* if the desired state is playing, go back */
1326 if (priv->target_state == GST_STATE_PLAYING) {
1327 GST_INFO ("Buffering done, setting pipeline to PLAYING");
1328 gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
1330 GST_INFO ("Buffering done");
1333 /* buffering busy */
1334 if (priv->buffering == FALSE) {
1335 if (priv->target_state == GST_STATE_PLAYING) {
1336 /* we were not buffering but PLAYING, PAUSE the pipeline. */
1337 GST_INFO ("Buffering, setting pipeline to PAUSED ...");
1338 gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
1340 GST_INFO ("Buffering ...");
1343 priv->buffering = TRUE;
1347 case GST_MESSAGE_LATENCY:
1349 gst_bin_recalculate_latency (GST_BIN_CAST (priv->pipeline));
1352 case GST_MESSAGE_ERROR:
1357 gst_message_parse_error (message, &gerror, &debug);
1358 GST_WARNING ("%p: got error %s (%s)", media, gerror->message, debug);
1359 g_error_free (gerror);
1362 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
1365 case GST_MESSAGE_WARNING:
1370 gst_message_parse_warning (message, &gerror, &debug);
1371 GST_WARNING ("%p: got warning %s (%s)", media, gerror->message, debug);
1372 g_error_free (gerror);
1376 case GST_MESSAGE_ELEMENT:
1378 case GST_MESSAGE_STREAM_STATUS:
1380 case GST_MESSAGE_ASYNC_DONE:
1382 /* when we are dynamically adding pads, the addition of the udpsrc will
1383 * temporarily produce ASYNC_DONE messages. We have to ignore them and
1384 * wait for the final ASYNC_DONE after everything prerolled */
1385 GST_INFO ("%p: ignoring ASYNC_DONE", media);
1387 GST_INFO ("%p: got ASYNC_DONE", media);
1388 collect_media_stats (media);
1390 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
1391 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
1394 case GST_MESSAGE_EOS:
1395 GST_INFO ("%p: got EOS", media);
1397 if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARING) {
1398 GST_DEBUG ("shutting down after EOS");
1399 finish_unprepare (media);
1403 GST_INFO ("%p: got message type %d (%s)", media, type,
1404 gst_message_type_get_name (type));
1411 bus_message (GstBus * bus, GstMessage * message, GstRTSPMedia * media)
1413 GstRTSPMediaPrivate *priv = media->priv;
1414 GstRTSPMediaClass *klass;
1417 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1419 g_rec_mutex_lock (&priv->state_lock);
1420 if (klass->handle_message)
1421 ret = klass->handle_message (media, message);
1424 g_rec_mutex_unlock (&priv->state_lock);
1430 watch_destroyed (GstRTSPMedia * media)
1432 GST_DEBUG_OBJECT (media, "source destroyed");
1433 g_object_unref (media);
1436 /* called from streaming threads */
1438 pad_added_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
1440 GstRTSPMediaPrivate *priv = media->priv;
1441 GstRTSPStream *stream;
1443 /* FIXME, element is likely not a payloader, find the payloader here */
1444 stream = gst_rtsp_media_create_stream (media, element, pad);
1446 g_object_set_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream", stream);
1448 GST_INFO ("pad added %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
1450 g_rec_mutex_lock (&priv->state_lock);
1451 /* we will be adding elements below that will cause ASYNC_DONE to be
1452 * posted in the bus. We want to ignore those messages until the
1453 * pipeline really prerolled. */
1454 priv->adding = TRUE;
1456 /* join the element in the PAUSED state because this callback is
1457 * called from the streaming thread and it is PAUSED */
1458 gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
1459 priv->rtpbin, GST_STATE_PAUSED);
1461 priv->adding = FALSE;
1462 g_rec_mutex_unlock (&priv->state_lock);
1466 pad_removed_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
1468 GstRTSPMediaPrivate *priv = media->priv;
1469 GstRTSPStream *stream;
1471 stream = g_object_get_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream");
1475 GST_INFO ("pad removed %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
1477 g_rec_mutex_lock (&priv->state_lock);
1478 gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
1479 g_rec_mutex_unlock (&priv->state_lock);
1481 gst_rtsp_media_remove_stream (media, stream);
1485 remove_fakesink (GstRTSPMediaPrivate * priv)
1487 GstElement *fakesink;
1489 g_mutex_lock (&priv->lock);
1490 if ((fakesink = priv->fakesink))
1491 gst_object_ref (fakesink);
1492 priv->fakesink = NULL;
1493 g_mutex_unlock (&priv->lock);
1496 gst_bin_remove (GST_BIN (priv->pipeline), fakesink);
1497 gst_element_set_state (fakesink, GST_STATE_NULL);
1498 gst_object_unref (fakesink);
1499 GST_INFO ("removed fakesink");
1504 no_more_pads_cb (GstElement * element, GstRTSPMedia * media)
1506 GstRTSPMediaPrivate *priv = media->priv;
1508 GST_INFO ("no more pads");
1509 remove_fakesink (priv);
1512 typedef struct _DynPaySignalHandlers DynPaySignalHandlers;
1514 struct _DynPaySignalHandlers
1516 gulong pad_added_handler;
1517 gulong pad_removed_handler;
1518 gulong no_more_pads_handler;
1522 * gst_rtsp_media_prepare:
1523 * @media: a #GstRTSPMedia
1525 * Prepare @media for streaming. This function will create the objects
1526 * to manage the streaming. A pipeline must have been set on @media with
1527 * gst_rtsp_media_take_pipeline().
1529 * It will preroll the pipeline and collect vital information about the streams
1530 * such as the duration.
1532 * Returns: %TRUE on success.
1535 gst_rtsp_media_prepare (GstRTSPMedia * media)
1537 GstRTSPMediaPrivate *priv;
1538 GstStateChangeReturn ret;
1539 GstRTSPMediaStatus status;
1541 GstRTSPMediaClass *klass;
1545 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1549 g_rec_mutex_lock (&priv->state_lock);
1550 priv->prepare_count++;
1552 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED)
1555 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
1558 if (priv->status != GST_RTSP_MEDIA_STATUS_UNPREPARED)
1559 goto not_unprepared;
1561 if (!priv->reusable && priv->reused)
1564 priv->rtpbin = gst_element_factory_make ("rtpbin", NULL);
1565 if (priv->rtpbin == NULL)
1568 GST_INFO ("preparing media %p", media);
1570 /* reset some variables */
1571 priv->is_live = FALSE;
1572 priv->seekable = FALSE;
1573 priv->buffering = FALSE;
1574 /* we're preparing now */
1575 priv->status = GST_RTSP_MEDIA_STATUS_PREPARING;
1577 bus = gst_pipeline_get_bus (GST_PIPELINE_CAST (priv->pipeline));
1579 /* add the pipeline bus to our custom mainloop */
1580 priv->source = gst_bus_create_watch (bus);
1581 gst_object_unref (bus);
1583 g_source_set_callback (priv->source, (GSourceFunc) bus_message,
1584 g_object_ref (media), (GDestroyNotify) watch_destroyed);
1586 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1587 priv->id = g_source_attach (priv->source, klass->context);
1589 /* add stuff to the bin */
1590 gst_bin_add (GST_BIN (priv->pipeline), priv->rtpbin);
1592 /* link streams we already have, other streams might appear when we have
1593 * dynamic elements */
1594 for (i = 0; i < priv->streams->len; i++) {
1595 GstRTSPStream *stream;
1597 stream = g_ptr_array_index (priv->streams, i);
1599 gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
1600 priv->rtpbin, GST_STATE_NULL);
1603 for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
1604 GstElement *elem = walk->data;
1605 DynPaySignalHandlers *handlers = g_slice_new (DynPaySignalHandlers);
1607 GST_INFO ("adding callbacks for dynamic element %p", elem);
1609 handlers->pad_added_handler = g_signal_connect (elem, "pad-added",
1610 (GCallback) pad_added_cb, media);
1611 handlers->pad_removed_handler = g_signal_connect (elem, "pad-removed",
1612 (GCallback) pad_removed_cb, media);
1613 handlers->no_more_pads_handler = g_signal_connect (elem, "no-more-pads",
1614 (GCallback) no_more_pads_cb, media);
1616 g_object_set_data (G_OBJECT (elem), "gst-rtsp-dynpay-handlers", handlers);
1618 /* we add a fakesink here in order to make the state change async. We remove
1619 * the fakesink again in the no-more-pads callback. */
1620 priv->fakesink = gst_element_factory_make ("fakesink", "fakesink");
1621 gst_bin_add (GST_BIN (priv->pipeline), priv->fakesink);
1624 GST_INFO ("setting pipeline to PAUSED for media %p", media);
1625 /* first go to PAUSED */
1626 ret = gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
1627 priv->target_state = GST_STATE_PAUSED;
1630 case GST_STATE_CHANGE_SUCCESS:
1631 GST_INFO ("SUCCESS state change for media %p", media);
1632 priv->seekable = TRUE;
1634 case GST_STATE_CHANGE_ASYNC:
1635 GST_INFO ("ASYNC state change for media %p", media);
1636 priv->seekable = TRUE;
1638 case GST_STATE_CHANGE_NO_PREROLL:
1639 /* we need to go to PLAYING */
1640 GST_INFO ("NO_PREROLL state change: live media %p", media);
1641 /* FIXME we disable seeking for live streams for now. We should perform a
1642 * seeking query in preroll instead */
1643 priv->seekable = FALSE;
1644 priv->is_live = TRUE;
1645 ret = gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
1646 if (ret == GST_STATE_CHANGE_FAILURE)
1649 case GST_STATE_CHANGE_FAILURE:
1653 g_rec_mutex_unlock (&priv->state_lock);
1655 /* now wait for all pads to be prerolled, FIXME, we should somehow be
1656 * able to do this async so that we don't block the server thread. */
1657 status = gst_rtsp_media_get_status (media);
1658 if (status == GST_RTSP_MEDIA_STATUS_ERROR)
1661 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_PREPARED], 0, NULL);
1663 GST_INFO ("object %p is prerolled", media);
1670 GST_LOG ("media %p was prepared", media);
1671 g_rec_mutex_unlock (&priv->state_lock);
1677 GST_WARNING ("media %p was not unprepared", media);
1678 priv->prepare_count--;
1679 g_rec_mutex_unlock (&priv->state_lock);
1684 priv->prepare_count--;
1685 g_rec_mutex_unlock (&priv->state_lock);
1686 GST_WARNING ("can not reuse media %p", media);
1691 priv->prepare_count--;
1692 g_rec_mutex_unlock (&priv->state_lock);
1693 GST_WARNING ("no rtpbin element");
1694 g_warning ("failed to create element 'rtpbin', check your installation");
1699 GST_WARNING ("failed to preroll pipeline");
1700 gst_rtsp_media_unprepare (media);
1701 g_rec_mutex_unlock (&priv->state_lock);
1706 /* must be called with state-lock */
1708 finish_unprepare (GstRTSPMedia * media)
1710 GstRTSPMediaPrivate *priv = media->priv;
1714 GST_DEBUG ("shutting down");
1716 gst_element_set_state (priv->pipeline, GST_STATE_NULL);
1717 remove_fakesink (priv);
1719 for (i = 0; i < priv->streams->len; i++) {
1720 GstRTSPStream *stream;
1722 GST_INFO ("Removing elements of stream %d from pipeline", i);
1724 stream = g_ptr_array_index (priv->streams, i);
1726 gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
1729 /* remove the pad signal handlers */
1730 for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
1731 GstElement *elem = walk->data;
1732 DynPaySignalHandlers *handlers;
1735 g_object_steal_data (G_OBJECT (elem), "gst-rtsp-dynpay-handlers");
1736 g_assert (handlers != NULL);
1738 g_signal_handler_disconnect (G_OBJECT (elem), handlers->pad_added_handler);
1739 g_signal_handler_disconnect (G_OBJECT (elem),
1740 handlers->pad_removed_handler);
1741 g_signal_handler_disconnect (G_OBJECT (elem),
1742 handlers->no_more_pads_handler);
1744 g_slice_free (DynPaySignalHandlers, handlers);
1747 gst_bin_remove (GST_BIN (priv->pipeline), priv->rtpbin);
1748 priv->rtpbin = NULL;
1751 gst_object_unref (priv->nettime);
1752 priv->nettime = NULL;
1754 priv->reused = TRUE;
1755 priv->status = GST_RTSP_MEDIA_STATUS_UNPREPARED;
1757 /* when the media is not reusable, this will effectively unref the media and
1759 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_UNPREPARED], 0, NULL);
1761 /* the source has the last ref to the media */
1763 GST_DEBUG ("destroy source");
1764 g_source_destroy (priv->source);
1765 g_source_unref (priv->source);
1769 /* called with state-lock */
1771 default_unprepare (GstRTSPMedia * media)
1773 GstRTSPMediaPrivate *priv = media->priv;
1775 if (priv->eos_shutdown) {
1776 GST_DEBUG ("sending EOS for shutdown");
1777 /* ref so that we don't disappear */
1778 gst_element_send_event (priv->pipeline, gst_event_new_eos ());
1779 /* we need to go to playing again for the EOS to propagate, normally in this
1780 * state, nothing is receiving data from us anymore so this is ok. */
1781 gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
1782 priv->status = GST_RTSP_MEDIA_STATUS_UNPREPARING;
1784 finish_unprepare (media);
1790 * gst_rtsp_media_unprepare:
1791 * @media: a #GstRTSPMedia
1793 * Unprepare @media. After this call, the media should be prepared again before
1794 * it can be used again. If the media is set to be non-reusable, a new instance
1797 * Returns: %TRUE on success.
1800 gst_rtsp_media_unprepare (GstRTSPMedia * media)
1802 GstRTSPMediaPrivate *priv;
1805 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1809 g_rec_mutex_lock (&priv->state_lock);
1810 if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARED)
1811 goto was_unprepared;
1813 priv->prepare_count--;
1814 if (priv->prepare_count > 0)
1817 GST_INFO ("unprepare media %p", media);
1818 priv->target_state = GST_STATE_NULL;
1821 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED) {
1822 GstRTSPMediaClass *klass;
1824 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1825 if (klass->unprepare)
1826 success = klass->unprepare (media);
1828 finish_unprepare (media);
1830 g_rec_mutex_unlock (&priv->state_lock);
1836 g_rec_mutex_unlock (&priv->state_lock);
1837 GST_INFO ("media %p was already unprepared", media);
1842 GST_INFO ("media %p still prepared %d times", media, priv->prepare_count);
1843 g_rec_mutex_unlock (&priv->state_lock);
1848 /* should be called with state-lock */
1850 get_clock_unlocked (GstRTSPMedia * media)
1852 if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED) {
1853 GST_DEBUG_OBJECT (media, "media was not prepared");
1856 return gst_pipeline_get_clock (GST_PIPELINE_CAST (media->priv->pipeline));
1860 * gst_rtsp_media_get_clock:
1861 * @media: a #GstRTSPMedia
1863 * Get the clock that is used by the pipeline in @media.
1865 * @media must be prepared before this method returns a valid clock object.
1867 * Returns: the #GstClock used by @media. unref after usage.
1870 gst_rtsp_media_get_clock (GstRTSPMedia * media)
1873 GstRTSPMediaPrivate *priv;
1875 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1879 g_rec_mutex_lock (&priv->state_lock);
1880 clock = get_clock_unlocked (media);
1881 g_rec_mutex_unlock (&priv->state_lock);
1887 * gst_rtsp_media_get_base_time:
1888 * @media: a #GstRTSPMedia
1890 * Get the base_time that is used by the pipeline in @media.
1892 * @media must be prepared before this method returns a valid base_time.
1894 * Returns: the base_time used by @media.
1897 gst_rtsp_media_get_base_time (GstRTSPMedia * media)
1899 GstClockTime result;
1900 GstRTSPMediaPrivate *priv;
1902 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_CLOCK_TIME_NONE);
1906 g_rec_mutex_lock (&priv->state_lock);
1907 if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
1910 result = gst_element_get_base_time (media->priv->pipeline);
1911 g_rec_mutex_unlock (&priv->state_lock);
1918 g_rec_mutex_unlock (&priv->state_lock);
1919 GST_DEBUG_OBJECT (media, "media was not prepared");
1920 return GST_CLOCK_TIME_NONE;
1925 * gst_rtsp_media_get_time_provider:
1926 * @media: a #GstRTSPMedia
1927 * @address: an address or NULL
1928 * @port: a port or 0
1930 * Get the #GstNetTimeProvider for the clock used by @media. The time provider
1931 * will listen on @address and @port for client time requests.
1933 * Returns: the #GstNetTimeProvider of @media.
1935 GstNetTimeProvider *
1936 gst_rtsp_media_get_time_provider (GstRTSPMedia * media, const gchar * address,
1939 GstRTSPMediaPrivate *priv;
1940 GstNetTimeProvider *provider = NULL;
1942 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1946 g_rec_mutex_lock (&priv->state_lock);
1947 if (priv->time_provider) {
1948 if ((provider = priv->nettime) == NULL) {
1951 if (priv->time_provider && (clock = get_clock_unlocked (media))) {
1952 provider = gst_net_time_provider_new (clock, address, port);
1953 gst_object_unref (clock);
1955 priv->nettime = provider;
1959 g_rec_mutex_unlock (&priv->state_lock);
1962 gst_object_ref (provider);
1968 * gst_rtsp_media_set_state:
1969 * @media: a #GstRTSPMedia
1970 * @state: the target state of the media
1971 * @transports: a #GPtrArray of #GstRTSPStreamTransport pointers
1973 * Set the state of @media to @state and for the transports in @transports.
1975 * @media must be prepared with gst_rtsp_media_prepare();
1977 * Returns: %TRUE on success.
1980 gst_rtsp_media_set_state (GstRTSPMedia * media, GstState state,
1981 GPtrArray * transports)
1983 GstRTSPMediaPrivate *priv;
1985 gboolean activate, deactivate, do_state;
1988 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1989 g_return_val_if_fail (transports != NULL, FALSE);
1993 g_rec_mutex_lock (&priv->state_lock);
1994 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
1997 /* NULL and READY are the same */
1998 if (state == GST_STATE_READY)
1999 state = GST_STATE_NULL;
2001 activate = deactivate = FALSE;
2003 GST_INFO ("going to state %s media %p", gst_element_state_get_name (state),
2007 case GST_STATE_NULL:
2008 case GST_STATE_PAUSED:
2009 /* we're going from PLAYING to PAUSED, READY or NULL, deactivate */
2010 if (priv->target_state == GST_STATE_PLAYING)
2013 case GST_STATE_PLAYING:
2014 /* we're going to PLAYING, activate */
2020 old_active = priv->n_active;
2022 for (i = 0; i < transports->len; i++) {
2023 GstRTSPStreamTransport *trans;
2025 /* we need a non-NULL entry in the array */
2026 trans = g_ptr_array_index (transports, i);
2031 if (gst_rtsp_stream_transport_set_active (trans, TRUE))
2033 } else if (deactivate) {
2034 if (gst_rtsp_stream_transport_set_active (trans, FALSE))
2039 /* we just activated the first media, do the playing state change */
2040 if (old_active == 0 && activate)
2042 /* if we have no more active media, do the downward state changes */
2043 else if (priv->n_active == 0)
2048 GST_INFO ("state %d active %d media %p do_state %d", state, priv->n_active,
2051 if (priv->target_state != state) {
2053 if (state == GST_STATE_NULL) {
2054 gst_rtsp_media_unprepare (media);
2056 GST_INFO ("state %s media %p", gst_element_state_get_name (state),
2058 priv->target_state = state;
2059 /* when we are buffering, don't update the state yet, this will be done
2060 * when buffering finishes */
2061 if (priv->buffering) {
2062 GST_INFO ("Buffering busy, delay state change");
2064 gst_element_set_state (priv->pipeline, state);
2068 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STATE], 0, state,
2072 /* remember where we are */
2073 if (state != GST_STATE_NULL && (state == GST_STATE_PAUSED ||
2074 old_active != priv->n_active))
2075 collect_media_stats (media);
2077 g_rec_mutex_unlock (&priv->state_lock);
2084 GST_WARNING ("media %p was not prepared", media);
2085 g_rec_mutex_unlock (&priv->state_lock);
2090 /* called with state-lock */
2092 default_convert_range (GstRTSPMedia * media, GstRTSPTimeRange * range,
2093 GstRTSPRangeUnit unit)
2095 return gst_rtsp_range_convert_units (range, unit);