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;
353 gint64 position, duration;
355 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
356 priv->status != GST_RTSP_MEDIA_STATUS_PREPARING)
359 priv->range.unit = GST_RTSP_RANGE_NPT;
361 GST_INFO ("collect media stats");
364 priv->range.min.type = GST_RTSP_TIME_NOW;
365 priv->range.min.seconds = -1;
366 priv->range_start = -1;
367 priv->range.max.type = GST_RTSP_TIME_END;
368 priv->range.max.seconds = -1;
369 priv->range_stop = -1;
371 /* get the position */
372 if (!gst_element_query_position (priv->pipeline, GST_FORMAT_TIME,
374 GST_INFO ("position query failed");
378 /* get the duration */
379 if (!gst_element_query_duration (priv->pipeline, GST_FORMAT_TIME,
381 GST_INFO ("duration query failed");
385 GST_INFO ("stats: position %" GST_TIME_FORMAT ", duration %"
386 GST_TIME_FORMAT, GST_TIME_ARGS (position), GST_TIME_ARGS (duration));
388 if (position == -1) {
389 priv->range.min.type = GST_RTSP_TIME_NOW;
390 priv->range.min.seconds = -1;
391 priv->range_start = -1;
393 priv->range.min.type = GST_RTSP_TIME_SECONDS;
394 priv->range.min.seconds = ((gdouble) position) / GST_SECOND;
395 priv->range_start = position;
397 if (duration == -1) {
398 priv->range.max.type = GST_RTSP_TIME_END;
399 priv->range.max.seconds = -1;
400 priv->range_stop = -1;
402 priv->range.max.type = GST_RTSP_TIME_SECONDS;
403 priv->range.max.seconds = ((gdouble) duration) / GST_SECOND;
404 priv->range_stop = duration;
410 * gst_rtsp_media_new:
411 * @element: (transfer full): a #GstElement
413 * Create a new #GstRTSPMedia instance. @element is the bin element that
414 * provides the different streams. The #GstRTSPMedia object contains the
415 * element to produce RTP data for one or more related (audio/video/..)
418 * Ownership is taken of @element.
420 * Returns: a new #GstRTSPMedia object.
423 gst_rtsp_media_new (GstElement * element)
425 GstRTSPMedia *result;
427 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
429 result = g_object_new (GST_TYPE_RTSP_MEDIA, "element", element, NULL);
435 * gst_rtsp_media_take_element:
436 * @media: a #GstRTSPMedia
437 * @pipeline: (transfer full): a #GstPipeline
439 * Set @pipeline as the #GstPipeline for @media. Ownership is
440 * taken of @pipeline.
443 gst_rtsp_media_take_pipeline (GstRTSPMedia * media, GstPipeline * pipeline)
445 GstRTSPMediaPrivate *priv;
447 GstNetTimeProvider *nettime;
449 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
450 g_return_if_fail (GST_IS_PIPELINE (pipeline));
454 g_mutex_lock (&priv->lock);
455 old = priv->pipeline;
456 priv->pipeline = GST_ELEMENT_CAST (pipeline);
457 nettime = priv->nettime;
458 priv->nettime = NULL;
459 g_mutex_unlock (&priv->lock);
462 gst_object_unref (old);
465 gst_object_unref (nettime);
467 gst_bin_add (GST_BIN_CAST (pipeline), priv->element);
471 * gst_rtsp_media_set_shared:
472 * @media: a #GstRTSPMedia
473 * @shared: the new value
475 * Set or unset if the pipeline for @media can be shared will multiple clients.
476 * When @shared is %TRUE, client requests for this media will share the media
480 gst_rtsp_media_set_shared (GstRTSPMedia * media, gboolean shared)
482 GstRTSPMediaPrivate *priv;
484 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
488 g_mutex_lock (&priv->lock);
489 priv->shared = shared;
490 g_mutex_unlock (&priv->lock);
494 * gst_rtsp_media_is_shared:
495 * @media: a #GstRTSPMedia
497 * Check if the pipeline for @media can be shared between multiple clients.
499 * Returns: %TRUE if the media can be shared between clients.
502 gst_rtsp_media_is_shared (GstRTSPMedia * media)
504 GstRTSPMediaPrivate *priv;
507 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
511 g_mutex_lock (&priv->lock);
513 g_mutex_unlock (&priv->lock);
519 * gst_rtsp_media_set_reusable:
520 * @media: a #GstRTSPMedia
521 * @reusable: the new value
523 * Set or unset if the pipeline for @media can be reused after the pipeline has
527 gst_rtsp_media_set_reusable (GstRTSPMedia * media, gboolean reusable)
529 GstRTSPMediaPrivate *priv;
531 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
535 g_mutex_lock (&priv->lock);
536 priv->reusable = reusable;
537 g_mutex_unlock (&priv->lock);
541 * gst_rtsp_media_is_reusable:
542 * @media: a #GstRTSPMedia
544 * Check if the pipeline for @media can be reused after an unprepare.
546 * Returns: %TRUE if the media can be reused
549 gst_rtsp_media_is_reusable (GstRTSPMedia * media)
551 GstRTSPMediaPrivate *priv;
554 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
558 g_mutex_lock (&priv->lock);
559 res = priv->reusable;
560 g_mutex_unlock (&priv->lock);
566 * gst_rtsp_media_set_protocols:
567 * @media: a #GstRTSPMedia
568 * @protocols: the new flags
570 * Configure the allowed lower transport for @media.
573 gst_rtsp_media_set_protocols (GstRTSPMedia * media, GstRTSPLowerTrans protocols)
575 GstRTSPMediaPrivate *priv;
577 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
581 g_mutex_lock (&priv->lock);
582 priv->protocols = protocols;
583 g_mutex_unlock (&priv->lock);
587 * gst_rtsp_media_get_protocols:
588 * @media: a #GstRTSPMedia
590 * Get the allowed protocols of @media.
592 * Returns: a #GstRTSPLowerTrans
595 gst_rtsp_media_get_protocols (GstRTSPMedia * media)
597 GstRTSPMediaPrivate *priv;
598 GstRTSPLowerTrans res;
600 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media),
601 GST_RTSP_LOWER_TRANS_UNKNOWN);
605 g_mutex_lock (&priv->lock);
606 res = priv->protocols;
607 g_mutex_unlock (&priv->lock);
613 * gst_rtsp_media_set_eos_shutdown:
614 * @media: a #GstRTSPMedia
615 * @eos_shutdown: the new value
617 * Set or unset if an EOS event will be sent to the pipeline for @media before
621 gst_rtsp_media_set_eos_shutdown (GstRTSPMedia * media, gboolean eos_shutdown)
623 GstRTSPMediaPrivate *priv;
625 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
629 g_mutex_lock (&priv->lock);
630 priv->eos_shutdown = eos_shutdown;
631 g_mutex_unlock (&priv->lock);
635 * gst_rtsp_media_is_eos_shutdown:
636 * @media: a #GstRTSPMedia
638 * Check if the pipeline for @media will send an EOS down the pipeline before
641 * Returns: %TRUE if the media will send EOS before unpreparing.
644 gst_rtsp_media_is_eos_shutdown (GstRTSPMedia * media)
646 GstRTSPMediaPrivate *priv;
649 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
653 g_mutex_lock (&priv->lock);
654 res = priv->eos_shutdown;
655 g_mutex_unlock (&priv->lock);
661 * gst_rtsp_media_set_buffer_size:
662 * @media: a #GstRTSPMedia
663 * @size: the new value
665 * Set the kernel UDP buffer size.
668 gst_rtsp_media_set_buffer_size (GstRTSPMedia * media, guint size)
670 GstRTSPMediaPrivate *priv;
672 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
674 GST_LOG_OBJECT (media, "set buffer size %u", size);
678 g_mutex_lock (&priv->lock);
679 priv->buffer_size = size;
680 g_mutex_unlock (&priv->lock);
684 * gst_rtsp_media_get_buffer_size:
685 * @media: a #GstRTSPMedia
687 * Get the kernel UDP buffer size.
689 * Returns: the kernel UDP buffer size.
692 gst_rtsp_media_get_buffer_size (GstRTSPMedia * media)
694 GstRTSPMediaPrivate *priv;
697 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
701 g_mutex_unlock (&priv->lock);
702 res = priv->buffer_size;
703 g_mutex_unlock (&priv->lock);
709 * gst_rtsp_media_use_time_provider:
710 * @media: a #GstRTSPMedia
712 * Set @media to provide a GstNetTimeProvider.
715 gst_rtsp_media_use_time_provider (GstRTSPMedia * media, gboolean time_provider)
717 GstRTSPMediaPrivate *priv;
719 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
723 g_mutex_lock (&priv->lock);
724 priv->time_provider = time_provider;
725 g_mutex_unlock (&priv->lock);
729 * gst_rtsp_media_is_time_provider:
730 * @media: a #GstRTSPMedia
732 * Check if @media can provide a #GstNetTimeProvider for its pipeline clock.
734 * Use gst_rtsp_media_get_time_provider() to get the network clock.
736 * Returns: %TRUE if @media can provide a #GstNetTimeProvider.
739 gst_rtsp_media_is_time_provider (GstRTSPMedia * media)
741 GstRTSPMediaPrivate *priv;
744 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
748 g_mutex_unlock (&priv->lock);
749 res = priv->time_provider;
750 g_mutex_unlock (&priv->lock);
756 * gst_rtsp_media_set_auth:
757 * @media: a #GstRTSPMedia
758 * @auth: a #GstRTSPAuth
760 * configure @auth to be used as the authentication manager of @media.
763 gst_rtsp_media_set_auth (GstRTSPMedia * media, GstRTSPAuth * auth)
765 GstRTSPMediaPrivate *priv;
768 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
772 GST_LOG_OBJECT (media, "set auth %p", auth);
774 g_mutex_lock (&priv->lock);
775 if ((old = priv->auth) != auth)
776 priv->auth = auth ? g_object_ref (auth) : NULL;
779 g_mutex_unlock (&priv->lock);
782 g_object_unref (old);
786 * gst_rtsp_media_get_auth:
787 * @media: a #GstRTSPMedia
789 * Get the #GstRTSPAuth used as the authentication manager of @media.
791 * Returns: (transfer full): the #GstRTSPAuth of @media. g_object_unref() after
795 gst_rtsp_media_get_auth (GstRTSPMedia * media)
797 GstRTSPMediaPrivate *priv;
800 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
804 g_mutex_lock (&priv->lock);
805 if ((result = priv->auth))
806 g_object_ref (result);
807 g_mutex_unlock (&priv->lock);
813 * gst_rtsp_media_set_address_pool:
814 * @media: a #GstRTSPMedia
815 * @pool: a #GstRTSPAddressPool
817 * configure @pool to be used as the address pool of @media.
820 gst_rtsp_media_set_address_pool (GstRTSPMedia * media,
821 GstRTSPAddressPool * pool)
823 GstRTSPMediaPrivate *priv;
824 GstRTSPAddressPool *old;
826 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
830 GST_LOG_OBJECT (media, "set address pool %p", pool);
832 g_mutex_lock (&priv->lock);
833 if ((old = priv->pool) != pool)
834 priv->pool = pool ? g_object_ref (pool) : NULL;
837 g_ptr_array_foreach (priv->streams, (GFunc) gst_rtsp_stream_set_address_pool,
839 g_mutex_unlock (&priv->lock);
842 g_object_unref (old);
846 * gst_rtsp_media_get_address_pool:
847 * @media: a #GstRTSPMedia
849 * Get the #GstRTSPAddressPool used as the address pool of @media.
851 * Returns: (transfer full): the #GstRTSPAddressPool of @media. g_object_unref() after
855 gst_rtsp_media_get_address_pool (GstRTSPMedia * media)
857 GstRTSPMediaPrivate *priv;
858 GstRTSPAddressPool *result;
860 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
864 g_mutex_lock (&priv->lock);
865 if ((result = priv->pool))
866 g_object_ref (result);
867 g_mutex_unlock (&priv->lock);
873 * gst_rtsp_media_collect_streams:
874 * @media: a #GstRTSPMedia
876 * Find all payloader elements, they should be named pay%d in the
877 * element of @media, and create #GstRTSPStreams for them.
879 * Collect all dynamic elements, named dynpay%d, and add them to
880 * the list of dynamic elements.
883 gst_rtsp_media_collect_streams (GstRTSPMedia * media)
885 GstRTSPMediaPrivate *priv;
886 GstElement *element, *elem;
891 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
894 element = priv->element;
897 for (i = 0; have_elem; i++) {
902 name = g_strdup_printf ("pay%d", i);
903 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
904 GST_INFO ("found stream %d with payloader %p", i, elem);
906 /* take the pad of the payloader */
907 pad = gst_element_get_static_pad (elem, "src");
908 /* create the stream */
909 gst_rtsp_media_create_stream (media, elem, pad);
910 gst_object_unref (pad);
911 gst_object_unref (elem);
917 name = g_strdup_printf ("dynpay%d", i);
918 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
919 /* a stream that will dynamically create pads to provide RTP packets */
921 GST_INFO ("found dynamic element %d, %p", i, elem);
923 g_mutex_lock (&priv->lock);
924 priv->dynamic = g_list_prepend (priv->dynamic, elem);
925 g_mutex_unlock (&priv->lock);
934 * gst_rtsp_media_create_stream:
935 * @media: a #GstRTSPMedia
936 * @payloader: a #GstElement
937 * @srcpad: a source #GstPad
939 * Create a new stream in @media that provides RTP data on @srcpad.
940 * @srcpad should be a pad of an element inside @media->element.
942 * Returns: (transfer none): a new #GstRTSPStream that remains valid for as long
946 gst_rtsp_media_create_stream (GstRTSPMedia * media, GstElement * payloader,
949 GstRTSPMediaPrivate *priv;
950 GstRTSPStream *stream;
955 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
956 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
957 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
958 g_return_val_if_fail (GST_PAD_IS_SRC (pad), NULL);
962 g_mutex_lock (&priv->lock);
963 idx = priv->streams->len;
965 GST_DEBUG ("media %p: creating stream with index %d", media, idx);
967 name = g_strdup_printf ("src_%u", idx);
968 srcpad = gst_ghost_pad_new (name, pad);
969 gst_pad_set_active (srcpad, TRUE);
970 gst_element_add_pad (priv->element, srcpad);
973 stream = gst_rtsp_stream_new (idx, payloader, srcpad);
975 gst_rtsp_stream_set_address_pool (stream, priv->pool);
977 g_ptr_array_add (priv->streams, stream);
978 g_mutex_unlock (&priv->lock);
980 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STREAM], 0, stream,
987 gst_rtsp_media_remove_stream (GstRTSPMedia * media, GstRTSPStream * stream)
989 GstRTSPMediaPrivate *priv;
994 g_mutex_lock (&priv->lock);
995 /* remove the ghostpad */
996 srcpad = gst_rtsp_stream_get_srcpad (stream);
997 gst_element_remove_pad (priv->element, srcpad);
998 gst_object_unref (srcpad);
999 /* now remove the stream */
1000 g_object_ref (stream);
1001 g_ptr_array_remove (priv->streams, stream);
1002 g_mutex_unlock (&priv->lock);
1004 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_REMOVED_STREAM], 0,
1007 g_object_unref (stream);
1011 * gst_rtsp_media_n_streams:
1012 * @media: a #GstRTSPMedia
1014 * Get the number of streams in this media.
1016 * Returns: The number of streams.
1019 gst_rtsp_media_n_streams (GstRTSPMedia * media)
1021 GstRTSPMediaPrivate *priv;
1024 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), 0);
1028 g_mutex_lock (&priv->lock);
1029 res = priv->streams->len;
1030 g_mutex_unlock (&priv->lock);
1036 * gst_rtsp_media_get_stream:
1037 * @media: a #GstRTSPMedia
1038 * @idx: the stream index
1040 * Retrieve the stream with index @idx from @media.
1042 * Returns: (transfer none): the #GstRTSPStream at index @idx or %NULL when a stream with
1043 * that index did not exist.
1046 gst_rtsp_media_get_stream (GstRTSPMedia * media, guint idx)
1048 GstRTSPMediaPrivate *priv;
1051 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1055 g_mutex_lock (&priv->lock);
1056 if (idx < priv->streams->len)
1057 res = g_ptr_array_index (priv->streams, idx);
1060 g_mutex_unlock (&priv->lock);
1066 * gst_rtsp_media_get_range_string:
1067 * @media: a #GstRTSPMedia
1068 * @play: for the PLAY request
1069 * @unit: the unit to use for the string
1071 * Get the current range as a string. @media must be prepared with
1072 * gst_rtsp_media_prepare ().
1074 * Returns: The range as a string, g_free() after usage.
1077 gst_rtsp_media_get_range_string (GstRTSPMedia * media, gboolean play,
1078 GstRTSPRangeUnit unit)
1080 GstRTSPMediaClass *klass;
1081 GstRTSPMediaPrivate *priv;
1083 GstRTSPTimeRange range;
1085 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1086 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1087 g_return_val_if_fail (klass->convert_range != NULL, FALSE);
1091 g_rec_mutex_lock (&priv->state_lock);
1092 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
1095 g_mutex_lock (&priv->lock);
1097 range = priv->range;
1099 if (!play && priv->n_active > 0) {
1100 range.min.type = GST_RTSP_TIME_NOW;
1101 range.min.seconds = -1;
1103 g_mutex_unlock (&priv->lock);
1104 g_rec_mutex_unlock (&priv->state_lock);
1106 if (!klass->convert_range (media, &range, unit)) {
1107 goto conversion_failed;
1110 result = gst_rtsp_range_to_string (&range);
1117 GST_WARNING ("media %p was not prepared", media);
1118 g_rec_mutex_unlock (&priv->state_lock);
1123 GST_WARNING ("range conversion to unit %d failed", unit);
1124 g_rec_mutex_unlock (&priv->state_lock);
1130 * gst_rtsp_media_seek:
1131 * @media: a #GstRTSPMedia
1132 * @range: a #GstRTSPTimeRange
1134 * Seek the pipeline of @media to @range. @media must be prepared with
1135 * gst_rtsp_media_prepare().
1137 * Returns: %TRUE on success.
1140 gst_rtsp_media_seek (GstRTSPMedia * media, GstRTSPTimeRange * range)
1142 GstRTSPMediaClass *klass;
1143 GstRTSPMediaPrivate *priv;
1146 GstClockTime start, stop;
1147 GstSeekType start_type, stop_type;
1149 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1151 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1152 g_return_val_if_fail (range != NULL, FALSE);
1153 g_return_val_if_fail (klass->convert_range != NULL, FALSE);
1157 g_rec_mutex_lock (&priv->state_lock);
1158 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
1161 if (!priv->seekable)
1164 /* depends on the current playing state of the pipeline. We might need to
1165 * queue this until we get EOS. */
1166 flags = GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_KEY_UNIT;
1168 start_type = stop_type = GST_SEEK_TYPE_NONE;
1170 if (!klass->convert_range (media, range, GST_RTSP_RANGE_NPT))
1172 gst_rtsp_range_get_times (range, &start, &stop);
1174 GST_INFO ("got %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1175 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1176 GST_INFO ("current %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1177 GST_TIME_ARGS (priv->range_start), GST_TIME_ARGS (priv->range_stop));
1179 if (priv->range_start == start)
1180 start = GST_CLOCK_TIME_NONE;
1181 else if (start != GST_CLOCK_TIME_NONE)
1182 start_type = GST_SEEK_TYPE_SET;
1184 if (priv->range_stop == stop)
1185 stop = GST_CLOCK_TIME_NONE;
1186 else if (stop != GST_CLOCK_TIME_NONE)
1187 stop_type = GST_SEEK_TYPE_SET;
1189 if (start != GST_CLOCK_TIME_NONE || stop != GST_CLOCK_TIME_NONE) {
1190 GST_INFO ("seeking to %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1191 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1193 res = gst_element_seek (priv->pipeline, 1.0, GST_FORMAT_TIME,
1194 flags, start_type, start, stop_type, stop);
1196 /* and block for the seek to complete */
1197 GST_INFO ("done seeking %d", res);
1198 gst_element_get_state (priv->pipeline, NULL, NULL, -1);
1199 GST_INFO ("prerolled again");
1201 collect_media_stats (media);
1203 GST_INFO ("no seek needed");
1206 g_rec_mutex_unlock (&priv->state_lock);
1213 g_rec_mutex_unlock (&priv->state_lock);
1214 GST_INFO ("media %p is not prepared", media);
1219 g_rec_mutex_unlock (&priv->state_lock);
1220 GST_INFO ("pipeline is not seekable");
1225 g_rec_mutex_unlock (&priv->state_lock);
1226 GST_WARNING ("conversion to npt not supported");
1232 gst_rtsp_media_set_status (GstRTSPMedia * media, GstRTSPMediaStatus status)
1234 GstRTSPMediaPrivate *priv = media->priv;
1236 g_mutex_lock (&priv->lock);
1237 priv->status = status;
1238 GST_DEBUG ("setting new status to %d", status);
1239 g_cond_broadcast (&priv->cond);
1240 g_mutex_unlock (&priv->lock);
1244 * gst_rtsp_media_get_status:
1245 * @media: a #GstRTSPMedia
1247 * Get the status of @media. When @media is busy preparing, this function waits
1248 * until @media is prepared or in error.
1250 * Returns: the status of @media.
1253 gst_rtsp_media_get_status (GstRTSPMedia * media)
1255 GstRTSPMediaPrivate *priv = media->priv;
1256 GstRTSPMediaStatus result;
1259 g_mutex_lock (&priv->lock);
1260 end_time = g_get_monotonic_time () + 20 * G_TIME_SPAN_SECOND;
1261 /* while we are preparing, wait */
1262 while (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING) {
1263 GST_DEBUG ("waiting for status change");
1264 if (!g_cond_wait_until (&priv->cond, &priv->lock, end_time)) {
1265 GST_DEBUG ("timeout, assuming error status");
1266 priv->status = GST_RTSP_MEDIA_STATUS_ERROR;
1269 /* could be success or error */
1270 result = priv->status;
1271 GST_DEBUG ("got status %d", result);
1272 g_mutex_unlock (&priv->lock);
1277 /* called with state-lock */
1279 default_handle_message (GstRTSPMedia * media, GstMessage * message)
1281 GstRTSPMediaPrivate *priv = media->priv;
1282 GstMessageType type;
1284 type = GST_MESSAGE_TYPE (message);
1287 case GST_MESSAGE_STATE_CHANGED:
1289 case GST_MESSAGE_BUFFERING:
1293 gst_message_parse_buffering (message, &percent);
1295 /* no state management needed for live pipelines */
1299 if (percent == 100) {
1300 /* a 100% message means buffering is done */
1301 priv->buffering = FALSE;
1302 /* if the desired state is playing, go back */
1303 if (priv->target_state == GST_STATE_PLAYING) {
1304 GST_INFO ("Buffering done, setting pipeline to PLAYING");
1305 gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
1307 GST_INFO ("Buffering done");
1310 /* buffering busy */
1311 if (priv->buffering == FALSE) {
1312 if (priv->target_state == GST_STATE_PLAYING) {
1313 /* we were not buffering but PLAYING, PAUSE the pipeline. */
1314 GST_INFO ("Buffering, setting pipeline to PAUSED ...");
1315 gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
1317 GST_INFO ("Buffering ...");
1320 priv->buffering = TRUE;
1324 case GST_MESSAGE_LATENCY:
1326 gst_bin_recalculate_latency (GST_BIN_CAST (priv->pipeline));
1329 case GST_MESSAGE_ERROR:
1334 gst_message_parse_error (message, &gerror, &debug);
1335 GST_WARNING ("%p: got error %s (%s)", media, gerror->message, debug);
1336 g_error_free (gerror);
1339 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
1342 case GST_MESSAGE_WARNING:
1347 gst_message_parse_warning (message, &gerror, &debug);
1348 GST_WARNING ("%p: got warning %s (%s)", media, gerror->message, debug);
1349 g_error_free (gerror);
1353 case GST_MESSAGE_ELEMENT:
1355 case GST_MESSAGE_STREAM_STATUS:
1357 case GST_MESSAGE_ASYNC_DONE:
1359 /* when we are dynamically adding pads, the addition of the udpsrc will
1360 * temporarily produce ASYNC_DONE messages. We have to ignore them and
1361 * wait for the final ASYNC_DONE after everything prerolled */
1362 GST_INFO ("%p: ignoring ASYNC_DONE", media);
1364 GST_INFO ("%p: got ASYNC_DONE", media);
1365 collect_media_stats (media);
1367 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
1368 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
1371 case GST_MESSAGE_EOS:
1372 GST_INFO ("%p: got EOS", media);
1374 if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARING) {
1375 GST_DEBUG ("shutting down after EOS");
1376 finish_unprepare (media);
1380 GST_INFO ("%p: got message type %d (%s)", media, type,
1381 gst_message_type_get_name (type));
1388 bus_message (GstBus * bus, GstMessage * message, GstRTSPMedia * media)
1390 GstRTSPMediaPrivate *priv = media->priv;
1391 GstRTSPMediaClass *klass;
1394 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1396 g_rec_mutex_lock (&priv->state_lock);
1397 if (klass->handle_message)
1398 ret = klass->handle_message (media, message);
1401 g_rec_mutex_unlock (&priv->state_lock);
1407 watch_destroyed (GstRTSPMedia * media)
1409 GST_DEBUG_OBJECT (media, "source destroyed");
1410 g_object_unref (media);
1413 /* called from streaming threads */
1415 pad_added_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
1417 GstRTSPMediaPrivate *priv = media->priv;
1418 GstRTSPStream *stream;
1420 /* FIXME, element is likely not a payloader, find the payloader here */
1421 stream = gst_rtsp_media_create_stream (media, element, pad);
1423 g_object_set_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream", stream);
1425 GST_INFO ("pad added %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
1427 g_rec_mutex_lock (&priv->state_lock);
1428 /* we will be adding elements below that will cause ASYNC_DONE to be
1429 * posted in the bus. We want to ignore those messages until the
1430 * pipeline really prerolled. */
1431 priv->adding = TRUE;
1433 /* join the element in the PAUSED state because this callback is
1434 * called from the streaming thread and it is PAUSED */
1435 gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
1436 priv->rtpbin, GST_STATE_PAUSED);
1438 priv->adding = FALSE;
1439 g_rec_mutex_unlock (&priv->state_lock);
1443 pad_removed_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
1445 GstRTSPMediaPrivate *priv = media->priv;
1446 GstRTSPStream *stream;
1448 stream = g_object_get_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream");
1452 GST_INFO ("pad removed %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
1454 g_rec_mutex_lock (&priv->state_lock);
1455 gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
1456 g_rec_mutex_unlock (&priv->state_lock);
1458 gst_rtsp_media_remove_stream (media, stream);
1462 remove_fakesink (GstRTSPMediaPrivate * priv)
1464 GstElement *fakesink;
1466 g_mutex_lock (&priv->lock);
1467 if ((fakesink = priv->fakesink))
1468 gst_object_ref (fakesink);
1469 priv->fakesink = NULL;
1470 g_mutex_unlock (&priv->lock);
1473 gst_bin_remove (GST_BIN (priv->pipeline), fakesink);
1474 gst_element_set_state (fakesink, GST_STATE_NULL);
1475 gst_object_unref (fakesink);
1476 GST_INFO ("removed fakesink");
1481 no_more_pads_cb (GstElement * element, GstRTSPMedia * media)
1483 GstRTSPMediaPrivate *priv = media->priv;
1485 GST_INFO ("no more pads");
1486 remove_fakesink (priv);
1489 typedef struct _DynPaySignalHandlers DynPaySignalHandlers;
1491 struct _DynPaySignalHandlers
1493 gulong pad_added_handler;
1494 gulong pad_removed_handler;
1495 gulong no_more_pads_handler;
1499 * gst_rtsp_media_prepare:
1500 * @media: a #GstRTSPMedia
1502 * Prepare @media for streaming. This function will create the objects
1503 * to manage the streaming. A pipeline must have been set on @media with
1504 * gst_rtsp_media_take_pipeline().
1506 * It will preroll the pipeline and collect vital information about the streams
1507 * such as the duration.
1509 * Returns: %TRUE on success.
1512 gst_rtsp_media_prepare (GstRTSPMedia * media)
1514 GstRTSPMediaPrivate *priv;
1515 GstStateChangeReturn ret;
1516 GstRTSPMediaStatus status;
1518 GstRTSPMediaClass *klass;
1522 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1526 g_rec_mutex_lock (&priv->state_lock);
1527 priv->prepare_count++;
1529 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED)
1532 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
1535 if (priv->status != GST_RTSP_MEDIA_STATUS_UNPREPARED)
1536 goto not_unprepared;
1538 if (!priv->reusable && priv->reused)
1541 priv->rtpbin = gst_element_factory_make ("rtpbin", NULL);
1542 if (priv->rtpbin == NULL)
1545 GST_INFO ("preparing media %p", media);
1547 /* reset some variables */
1548 priv->is_live = FALSE;
1549 priv->seekable = FALSE;
1550 priv->buffering = FALSE;
1551 /* we're preparing now */
1552 priv->status = GST_RTSP_MEDIA_STATUS_PREPARING;
1554 bus = gst_pipeline_get_bus (GST_PIPELINE_CAST (priv->pipeline));
1556 /* add the pipeline bus to our custom mainloop */
1557 priv->source = gst_bus_create_watch (bus);
1558 gst_object_unref (bus);
1560 g_source_set_callback (priv->source, (GSourceFunc) bus_message,
1561 g_object_ref (media), (GDestroyNotify) watch_destroyed);
1563 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1564 priv->id = g_source_attach (priv->source, klass->context);
1566 /* add stuff to the bin */
1567 gst_bin_add (GST_BIN (priv->pipeline), priv->rtpbin);
1569 /* link streams we already have, other streams might appear when we have
1570 * dynamic elements */
1571 for (i = 0; i < priv->streams->len; i++) {
1572 GstRTSPStream *stream;
1574 stream = g_ptr_array_index (priv->streams, i);
1576 gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
1577 priv->rtpbin, GST_STATE_NULL);
1580 for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
1581 GstElement *elem = walk->data;
1582 DynPaySignalHandlers *handlers = g_slice_new (DynPaySignalHandlers);
1584 GST_INFO ("adding callbacks for dynamic element %p", elem);
1586 handlers->pad_added_handler = g_signal_connect (elem, "pad-added",
1587 (GCallback) pad_added_cb, media);
1588 handlers->pad_removed_handler = g_signal_connect (elem, "pad-removed",
1589 (GCallback) pad_removed_cb, media);
1590 handlers->no_more_pads_handler = g_signal_connect (elem, "no-more-pads",
1591 (GCallback) no_more_pads_cb, media);
1593 g_object_set_data (G_OBJECT (elem), "gst-rtsp-dynpay-handlers", handlers);
1595 /* we add a fakesink here in order to make the state change async. We remove
1596 * the fakesink again in the no-more-pads callback. */
1597 priv->fakesink = gst_element_factory_make ("fakesink", "fakesink");
1598 gst_bin_add (GST_BIN (priv->pipeline), priv->fakesink);
1601 GST_INFO ("setting pipeline to PAUSED for media %p", media);
1602 /* first go to PAUSED */
1603 ret = gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
1604 priv->target_state = GST_STATE_PAUSED;
1607 case GST_STATE_CHANGE_SUCCESS:
1608 GST_INFO ("SUCCESS state change for media %p", media);
1609 priv->seekable = TRUE;
1611 case GST_STATE_CHANGE_ASYNC:
1612 GST_INFO ("ASYNC state change for media %p", media);
1613 priv->seekable = TRUE;
1615 case GST_STATE_CHANGE_NO_PREROLL:
1616 /* we need to go to PLAYING */
1617 GST_INFO ("NO_PREROLL state change: live media %p", media);
1618 /* FIXME we disable seeking for live streams for now. We should perform a
1619 * seeking query in preroll instead */
1620 priv->seekable = FALSE;
1621 priv->is_live = TRUE;
1622 ret = gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
1623 if (ret == GST_STATE_CHANGE_FAILURE)
1626 case GST_STATE_CHANGE_FAILURE:
1630 g_rec_mutex_unlock (&priv->state_lock);
1632 /* now wait for all pads to be prerolled, FIXME, we should somehow be
1633 * able to do this async so that we don't block the server thread. */
1634 status = gst_rtsp_media_get_status (media);
1635 if (status == GST_RTSP_MEDIA_STATUS_ERROR)
1638 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_PREPARED], 0, NULL);
1640 GST_INFO ("object %p is prerolled", media);
1647 GST_LOG ("media %p was prepared", media);
1648 g_rec_mutex_unlock (&priv->state_lock);
1654 GST_WARNING ("media %p was not unprepared", media);
1655 priv->prepare_count--;
1656 g_rec_mutex_unlock (&priv->state_lock);
1661 priv->prepare_count--;
1662 g_rec_mutex_unlock (&priv->state_lock);
1663 GST_WARNING ("can not reuse media %p", media);
1668 priv->prepare_count--;
1669 g_rec_mutex_unlock (&priv->state_lock);
1670 GST_WARNING ("no rtpbin element");
1671 g_warning ("failed to create element 'rtpbin', check your installation");
1676 GST_WARNING ("failed to preroll pipeline");
1677 gst_rtsp_media_unprepare (media);
1678 g_rec_mutex_unlock (&priv->state_lock);
1683 /* must be called with state-lock */
1685 finish_unprepare (GstRTSPMedia * media)
1687 GstRTSPMediaPrivate *priv = media->priv;
1691 GST_DEBUG ("shutting down");
1693 gst_element_set_state (priv->pipeline, GST_STATE_NULL);
1694 remove_fakesink (priv);
1696 for (i = 0; i < priv->streams->len; i++) {
1697 GstRTSPStream *stream;
1699 GST_INFO ("Removing elements of stream %d from pipeline", i);
1701 stream = g_ptr_array_index (priv->streams, i);
1703 gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
1706 /* remove the pad signal handlers */
1707 for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
1708 GstElement *elem = walk->data;
1709 DynPaySignalHandlers *handlers;
1712 g_object_steal_data (G_OBJECT (elem), "gst-rtsp-dynpay-handlers");
1713 g_assert (handlers != NULL);
1715 g_signal_handler_disconnect (G_OBJECT (elem), handlers->pad_added_handler);
1716 g_signal_handler_disconnect (G_OBJECT (elem),
1717 handlers->pad_removed_handler);
1718 g_signal_handler_disconnect (G_OBJECT (elem),
1719 handlers->no_more_pads_handler);
1721 g_slice_free (DynPaySignalHandlers, handlers);
1724 gst_bin_remove (GST_BIN (priv->pipeline), priv->rtpbin);
1725 priv->rtpbin = NULL;
1728 gst_object_unref (priv->nettime);
1729 priv->nettime = NULL;
1731 priv->reused = TRUE;
1732 priv->status = GST_RTSP_MEDIA_STATUS_UNPREPARED;
1734 /* when the media is not reusable, this will effectively unref the media and
1736 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_UNPREPARED], 0, NULL);
1738 /* the source has the last ref to the media */
1740 GST_DEBUG ("destroy source");
1741 g_source_destroy (priv->source);
1742 g_source_unref (priv->source);
1746 /* called with state-lock */
1748 default_unprepare (GstRTSPMedia * media)
1750 GstRTSPMediaPrivate *priv = media->priv;
1752 if (priv->eos_shutdown) {
1753 GST_DEBUG ("sending EOS for shutdown");
1754 /* ref so that we don't disappear */
1755 gst_element_send_event (priv->pipeline, gst_event_new_eos ());
1756 /* we need to go to playing again for the EOS to propagate, normally in this
1757 * state, nothing is receiving data from us anymore so this is ok. */
1758 gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
1759 priv->status = GST_RTSP_MEDIA_STATUS_UNPREPARING;
1761 finish_unprepare (media);
1767 * gst_rtsp_media_unprepare:
1768 * @media: a #GstRTSPMedia
1770 * Unprepare @media. After this call, the media should be prepared again before
1771 * it can be used again. If the media is set to be non-reusable, a new instance
1774 * Returns: %TRUE on success.
1777 gst_rtsp_media_unprepare (GstRTSPMedia * media)
1779 GstRTSPMediaPrivate *priv;
1782 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1786 g_rec_mutex_lock (&priv->state_lock);
1787 if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARED)
1788 goto was_unprepared;
1790 priv->prepare_count--;
1791 if (priv->prepare_count > 0)
1794 GST_INFO ("unprepare media %p", media);
1795 priv->target_state = GST_STATE_NULL;
1798 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED) {
1799 GstRTSPMediaClass *klass;
1801 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1802 if (klass->unprepare)
1803 success = klass->unprepare (media);
1805 finish_unprepare (media);
1807 g_rec_mutex_unlock (&priv->state_lock);
1813 g_rec_mutex_unlock (&priv->state_lock);
1814 GST_INFO ("media %p was already unprepared", media);
1819 GST_INFO ("media %p still prepared %d times", media, priv->prepare_count);
1820 g_rec_mutex_unlock (&priv->state_lock);
1825 /* should be called with state-lock */
1827 get_clock_unlocked (GstRTSPMedia * media)
1829 if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED) {
1830 GST_DEBUG_OBJECT (media, "media was not prepared");
1833 return gst_pipeline_get_clock (GST_PIPELINE_CAST (media->priv->pipeline));
1837 * gst_rtsp_media_get_clock:
1838 * @media: a #GstRTSPMedia
1840 * Get the clock that is used by the pipeline in @media.
1842 * @media must be prepared before this method returns a valid clock object.
1844 * Returns: the #GstClock used by @media. unref after usage.
1847 gst_rtsp_media_get_clock (GstRTSPMedia * media)
1850 GstRTSPMediaPrivate *priv;
1852 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1856 g_rec_mutex_lock (&priv->state_lock);
1857 clock = get_clock_unlocked (media);
1858 g_rec_mutex_unlock (&priv->state_lock);
1864 * gst_rtsp_media_get_base_time:
1865 * @media: a #GstRTSPMedia
1867 * Get the base_time that is used by the pipeline in @media.
1869 * @media must be prepared before this method returns a valid base_time.
1871 * Returns: the base_time used by @media.
1874 gst_rtsp_media_get_base_time (GstRTSPMedia * media)
1876 GstClockTime result;
1877 GstRTSPMediaPrivate *priv;
1879 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_CLOCK_TIME_NONE);
1883 g_rec_mutex_lock (&priv->state_lock);
1884 if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
1887 result = gst_element_get_base_time (media->priv->pipeline);
1888 g_rec_mutex_unlock (&priv->state_lock);
1895 g_rec_mutex_unlock (&priv->state_lock);
1896 GST_DEBUG_OBJECT (media, "media was not prepared");
1897 return GST_CLOCK_TIME_NONE;
1902 * gst_rtsp_media_get_time_provider:
1903 * @media: a #GstRTSPMedia
1904 * @address: an address or NULL
1905 * @port: a port or 0
1907 * Get the #GstNetTimeProvider for the clock used by @media. The time provider
1908 * will listen on @address and @port for client time requests.
1910 * Returns: the #GstNetTimeProvider of @media.
1912 GstNetTimeProvider *
1913 gst_rtsp_media_get_time_provider (GstRTSPMedia * media, const gchar * address,
1916 GstRTSPMediaPrivate *priv;
1917 GstNetTimeProvider *provider = NULL;
1919 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1923 g_rec_mutex_lock (&priv->state_lock);
1924 if (priv->time_provider) {
1925 if ((provider = priv->nettime) == NULL) {
1928 if (priv->time_provider && (clock = get_clock_unlocked (media))) {
1929 provider = gst_net_time_provider_new (clock, address, port);
1930 gst_object_unref (clock);
1932 priv->nettime = provider;
1936 g_rec_mutex_unlock (&priv->state_lock);
1939 gst_object_ref (provider);
1945 * gst_rtsp_media_set_state:
1946 * @media: a #GstRTSPMedia
1947 * @state: the target state of the media
1948 * @transports: a #GPtrArray of #GstRTSPStreamTransport pointers
1950 * Set the state of @media to @state and for the transports in @transports.
1952 * @media must be prepared with gst_rtsp_media_prepare();
1954 * Returns: %TRUE on success.
1957 gst_rtsp_media_set_state (GstRTSPMedia * media, GstState state,
1958 GPtrArray * transports)
1960 GstRTSPMediaPrivate *priv;
1962 gboolean activate, deactivate, do_state;
1965 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1966 g_return_val_if_fail (transports != NULL, FALSE);
1970 g_rec_mutex_lock (&priv->state_lock);
1971 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
1974 /* NULL and READY are the same */
1975 if (state == GST_STATE_READY)
1976 state = GST_STATE_NULL;
1978 activate = deactivate = FALSE;
1980 GST_INFO ("going to state %s media %p", gst_element_state_get_name (state),
1984 case GST_STATE_NULL:
1985 case GST_STATE_PAUSED:
1986 /* we're going from PLAYING to PAUSED, READY or NULL, deactivate */
1987 if (priv->target_state == GST_STATE_PLAYING)
1990 case GST_STATE_PLAYING:
1991 /* we're going to PLAYING, activate */
1997 old_active = priv->n_active;
1999 for (i = 0; i < transports->len; i++) {
2000 GstRTSPStreamTransport *trans;
2002 /* we need a non-NULL entry in the array */
2003 trans = g_ptr_array_index (transports, i);
2008 if (gst_rtsp_stream_transport_set_active (trans, TRUE))
2010 } else if (deactivate) {
2011 if (gst_rtsp_stream_transport_set_active (trans, FALSE))
2016 /* we just activated the first media, do the playing state change */
2017 if (old_active == 0 && activate)
2019 /* if we have no more active media, do the downward state changes */
2020 else if (priv->n_active == 0)
2025 GST_INFO ("state %d active %d media %p do_state %d", state, priv->n_active,
2028 if (priv->target_state != state) {
2030 if (state == GST_STATE_NULL) {
2031 gst_rtsp_media_unprepare (media);
2033 GST_INFO ("state %s media %p", gst_element_state_get_name (state),
2035 priv->target_state = state;
2036 /* when we are buffering, don't update the state yet, this will be done
2037 * when buffering finishes */
2038 if (priv->buffering) {
2039 GST_INFO ("Buffering busy, delay state change");
2041 gst_element_set_state (priv->pipeline, state);
2045 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STATE], 0, state,
2049 /* remember where we are */
2050 if (state != GST_STATE_NULL && (state == GST_STATE_PAUSED ||
2051 old_active != priv->n_active))
2052 collect_media_stats (media);
2054 g_rec_mutex_unlock (&priv->state_lock);
2061 GST_WARNING ("media %p was not prepared", media);
2062 g_rec_mutex_unlock (&priv->state_lock);
2067 /* called with state-lock */
2069 default_convert_range (GstRTSPMedia * media, GstRTSPTimeRange * range,
2070 GstRTSPRangeUnit unit)
2072 return gst_rtsp_range_convert_units (range, unit);