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 */
111 GST_DEBUG_CATEGORY_STATIC (rtsp_media_debug);
112 #define GST_CAT_DEFAULT rtsp_media_debug
114 static void gst_rtsp_media_get_property (GObject * object, guint propid,
115 GValue * value, GParamSpec * pspec);
116 static void gst_rtsp_media_set_property (GObject * object, guint propid,
117 const GValue * value, GParamSpec * pspec);
118 static void gst_rtsp_media_finalize (GObject * obj);
120 static gpointer do_loop (GstRTSPMediaClass * klass);
121 static gboolean default_handle_message (GstRTSPMedia * media,
122 GstMessage * message);
123 static void finish_unprepare (GstRTSPMedia * media);
124 static gboolean default_unprepare (GstRTSPMedia * media);
126 static guint gst_rtsp_media_signals[SIGNAL_LAST] = { 0 };
128 G_DEFINE_TYPE (GstRTSPMedia, gst_rtsp_media, G_TYPE_OBJECT);
131 gst_rtsp_media_class_init (GstRTSPMediaClass * klass)
133 GObjectClass *gobject_class;
135 g_type_class_add_private (klass, sizeof (GstRTSPMediaPrivate));
137 gobject_class = G_OBJECT_CLASS (klass);
139 gobject_class->get_property = gst_rtsp_media_get_property;
140 gobject_class->set_property = gst_rtsp_media_set_property;
141 gobject_class->finalize = gst_rtsp_media_finalize;
143 g_object_class_install_property (gobject_class, PROP_SHARED,
144 g_param_spec_boolean ("shared", "Shared",
145 "If this media pipeline can be shared", DEFAULT_SHARED,
146 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
148 g_object_class_install_property (gobject_class, PROP_REUSABLE,
149 g_param_spec_boolean ("reusable", "Reusable",
150 "If this media pipeline can be reused after an unprepare",
151 DEFAULT_REUSABLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
153 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
154 g_param_spec_flags ("protocols", "Protocols",
155 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
156 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
158 g_object_class_install_property (gobject_class, PROP_EOS_SHUTDOWN,
159 g_param_spec_boolean ("eos-shutdown", "EOS Shutdown",
160 "Send an EOS event to the pipeline before unpreparing",
161 DEFAULT_EOS_SHUTDOWN, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
163 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
164 g_param_spec_uint ("buffer-size", "Buffer Size",
165 "The kernel UDP buffer size to use", 0, G_MAXUINT,
166 DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
168 g_object_class_install_property (gobject_class, PROP_ELEMENT,
169 g_param_spec_object ("element", "The Element",
170 "The GstBin to use for streaming the media", GST_TYPE_ELEMENT,
171 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
173 g_object_class_install_property (gobject_class, PROP_EOS_SHUTDOWN,
174 g_param_spec_boolean ("time-provider", "Time Provider",
175 "Use a NetTimeProvider for clients",
176 DEFAULT_TIME_PROVIDER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
178 gst_rtsp_media_signals[SIGNAL_NEW_STREAM] =
179 g_signal_new ("new-stream", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
180 G_STRUCT_OFFSET (GstRTSPMediaClass, new_stream), NULL, NULL,
181 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_RTSP_STREAM);
183 gst_rtsp_media_signals[SIGNAL_PREPARED] =
184 g_signal_new ("prepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
185 G_STRUCT_OFFSET (GstRTSPMediaClass, prepared), NULL, NULL,
186 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
188 gst_rtsp_media_signals[SIGNAL_UNPREPARED] =
189 g_signal_new ("unprepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
190 G_STRUCT_OFFSET (GstRTSPMediaClass, unprepared), NULL, NULL,
191 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
193 gst_rtsp_media_signals[SIGNAL_NEW_STATE] =
194 g_signal_new ("new-state", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
195 G_STRUCT_OFFSET (GstRTSPMediaClass, new_state), NULL, NULL,
196 g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 0, G_TYPE_INT);
198 klass->context = g_main_context_new ();
199 klass->loop = g_main_loop_new (klass->context, TRUE);
201 GST_DEBUG_CATEGORY_INIT (rtsp_media_debug, "rtspmedia", 0, "GstRTSPMedia");
203 klass->thread = g_thread_new ("Bus Thread", (GThreadFunc) do_loop, klass);
205 klass->handle_message = default_handle_message;
206 klass->unprepare = default_unprepare;
210 gst_rtsp_media_init (GstRTSPMedia * media)
212 GstRTSPMediaPrivate *priv = GST_RTSP_MEDIA_GET_PRIVATE (media);
216 priv->streams = g_ptr_array_new_with_free_func (g_object_unref);
217 g_mutex_init (&priv->lock);
218 g_cond_init (&priv->cond);
219 g_rec_mutex_init (&priv->state_lock);
221 priv->shared = DEFAULT_SHARED;
222 priv->reusable = DEFAULT_REUSABLE;
223 priv->protocols = DEFAULT_PROTOCOLS;
224 priv->eos_shutdown = DEFAULT_EOS_SHUTDOWN;
225 priv->buffer_size = DEFAULT_BUFFER_SIZE;
226 priv->time_provider = DEFAULT_TIME_PROVIDER;
230 gst_rtsp_media_finalize (GObject * obj)
232 GstRTSPMediaPrivate *priv;
235 media = GST_RTSP_MEDIA (obj);
238 GST_INFO ("finalize media %p", media);
240 g_ptr_array_unref (priv->streams);
242 g_list_free_full (priv->dynamic, gst_object_unref);
245 gst_object_unref (priv->pipeline);
247 gst_object_unref (priv->nettime);
248 gst_object_unref (priv->element);
250 g_object_unref (priv->auth);
252 g_object_unref (priv->pool);
253 g_mutex_clear (&priv->lock);
254 g_cond_clear (&priv->cond);
255 g_rec_mutex_clear (&priv->state_lock);
257 G_OBJECT_CLASS (gst_rtsp_media_parent_class)->finalize (obj);
261 gst_rtsp_media_get_property (GObject * object, guint propid,
262 GValue * value, GParamSpec * pspec)
264 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
268 g_value_set_object (value, media->priv->element);
271 g_value_set_boolean (value, gst_rtsp_media_is_shared (media));
274 g_value_set_boolean (value, gst_rtsp_media_is_reusable (media));
277 g_value_set_flags (value, gst_rtsp_media_get_protocols (media));
279 case PROP_EOS_SHUTDOWN:
280 g_value_set_boolean (value, gst_rtsp_media_is_eos_shutdown (media));
282 case PROP_BUFFER_SIZE:
283 g_value_set_uint (value, gst_rtsp_media_get_buffer_size (media));
285 case PROP_TIME_PROVIDER:
286 g_value_set_boolean (value, gst_rtsp_media_is_time_provider (media));
289 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
294 gst_rtsp_media_set_property (GObject * object, guint propid,
295 const GValue * value, GParamSpec * pspec)
297 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
301 media->priv->element = g_value_get_object (value);
304 gst_rtsp_media_set_shared (media, g_value_get_boolean (value));
307 gst_rtsp_media_set_reusable (media, g_value_get_boolean (value));
310 gst_rtsp_media_set_protocols (media, g_value_get_flags (value));
312 case PROP_EOS_SHUTDOWN:
313 gst_rtsp_media_set_eos_shutdown (media, g_value_get_boolean (value));
315 case PROP_BUFFER_SIZE:
316 gst_rtsp_media_set_buffer_size (media, g_value_get_uint (value));
318 case PROP_TIME_PROVIDER:
319 gst_rtsp_media_use_time_provider (media, g_value_get_boolean (value));
322 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
327 do_loop (GstRTSPMediaClass * klass)
329 GST_INFO ("enter mainloop");
330 g_main_loop_run (klass->loop);
331 GST_INFO ("exit mainloop");
336 /* must be called with state lock */
338 collect_media_stats (GstRTSPMedia * media)
340 GstRTSPMediaPrivate *priv = media->priv;
341 gint64 position, duration;
343 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
344 priv->status != GST_RTSP_MEDIA_STATUS_PREPARING)
347 priv->range.unit = GST_RTSP_RANGE_NPT;
349 GST_INFO ("collect media stats");
352 priv->range.min.type = GST_RTSP_TIME_NOW;
353 priv->range.min.seconds = -1;
354 priv->range_start = -1;
355 priv->range.max.type = GST_RTSP_TIME_END;
356 priv->range.max.seconds = -1;
357 priv->range_stop = -1;
359 /* get the position */
360 if (!gst_element_query_position (priv->pipeline, GST_FORMAT_TIME,
362 GST_INFO ("position query failed");
366 /* get the duration */
367 if (!gst_element_query_duration (priv->pipeline, GST_FORMAT_TIME,
369 GST_INFO ("duration query failed");
373 GST_INFO ("stats: position %" GST_TIME_FORMAT ", duration %"
374 GST_TIME_FORMAT, GST_TIME_ARGS (position), GST_TIME_ARGS (duration));
376 if (position == -1) {
377 priv->range.min.type = GST_RTSP_TIME_NOW;
378 priv->range.min.seconds = -1;
379 priv->range_start = -1;
381 priv->range.min.type = GST_RTSP_TIME_SECONDS;
382 priv->range.min.seconds = ((gdouble) position) / GST_SECOND;
383 priv->range_start = position;
385 if (duration == -1) {
386 priv->range.max.type = GST_RTSP_TIME_END;
387 priv->range.max.seconds = -1;
388 priv->range_stop = -1;
390 priv->range.max.type = GST_RTSP_TIME_SECONDS;
391 priv->range.max.seconds = ((gdouble) duration) / GST_SECOND;
392 priv->range_stop = duration;
398 * gst_rtsp_media_new:
399 * @element: (transfer full): a #GstElement
401 * Create a new #GstRTSPMedia instance. @element is the bin element that
402 * provides the different streams. The #GstRTSPMedia object contains the
403 * element to produce RTP data for one or more related (audio/video/..)
406 * Ownership is taken of @element.
408 * Returns: a new #GstRTSPMedia object.
411 gst_rtsp_media_new (GstElement * element)
413 GstRTSPMedia *result;
415 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
417 result = g_object_new (GST_TYPE_RTSP_MEDIA, "element", element, NULL);
423 * gst_rtsp_media_take_element:
424 * @media: a #GstRTSPMedia
425 * @pipeline: (transfer full): a #GstPipeline
427 * Set @pipeline as the #GstPipeline for @media. Ownership is
428 * taken of @pipeline.
431 gst_rtsp_media_take_pipeline (GstRTSPMedia * media, GstPipeline * pipeline)
433 GstRTSPMediaPrivate *priv;
435 GstNetTimeProvider *nettime;
437 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
438 g_return_if_fail (GST_IS_PIPELINE (pipeline));
442 g_mutex_lock (&priv->lock);
443 old = priv->pipeline;
444 priv->pipeline = GST_ELEMENT_CAST (pipeline);
445 nettime = priv->nettime;
446 priv->nettime = NULL;
447 g_mutex_unlock (&priv->lock);
450 gst_object_unref (old);
453 gst_object_unref (nettime);
455 gst_object_ref (priv->element);
456 gst_bin_add (GST_BIN_CAST (pipeline), priv->element);
460 * gst_rtsp_media_set_shared:
461 * @media: a #GstRTSPMedia
462 * @shared: the new value
464 * Set or unset if the pipeline for @media can be shared will multiple clients.
465 * When @shared is %TRUE, client requests for this media will share the media
469 gst_rtsp_media_set_shared (GstRTSPMedia * media, gboolean shared)
471 GstRTSPMediaPrivate *priv;
473 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
477 g_mutex_lock (&priv->lock);
478 priv->shared = shared;
479 g_mutex_unlock (&priv->lock);
483 * gst_rtsp_media_is_shared:
484 * @media: a #GstRTSPMedia
486 * Check if the pipeline for @media can be shared between multiple clients.
488 * Returns: %TRUE if the media can be shared between clients.
491 gst_rtsp_media_is_shared (GstRTSPMedia * media)
493 GstRTSPMediaPrivate *priv;
496 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
500 g_mutex_lock (&priv->lock);
502 g_mutex_unlock (&priv->lock);
508 * gst_rtsp_media_set_reusable:
509 * @media: a #GstRTSPMedia
510 * @reusable: the new value
512 * Set or unset if the pipeline for @media can be reused after the pipeline has
516 gst_rtsp_media_set_reusable (GstRTSPMedia * media, gboolean reusable)
518 GstRTSPMediaPrivate *priv;
520 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
524 g_mutex_lock (&priv->lock);
525 priv->reusable = reusable;
526 g_mutex_unlock (&priv->lock);
530 * gst_rtsp_media_is_reusable:
531 * @media: a #GstRTSPMedia
533 * Check if the pipeline for @media can be reused after an unprepare.
535 * Returns: %TRUE if the media can be reused
538 gst_rtsp_media_is_reusable (GstRTSPMedia * media)
540 GstRTSPMediaPrivate *priv;
543 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
547 g_mutex_lock (&priv->lock);
548 res = priv->reusable;
549 g_mutex_unlock (&priv->lock);
555 * gst_rtsp_media_set_protocols:
556 * @media: a #GstRTSPMedia
557 * @protocols: the new flags
559 * Configure the allowed lower transport for @media.
562 gst_rtsp_media_set_protocols (GstRTSPMedia * media, GstRTSPLowerTrans protocols)
564 GstRTSPMediaPrivate *priv;
566 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
570 g_mutex_lock (&priv->lock);
571 priv->protocols = protocols;
572 g_mutex_unlock (&priv->lock);
576 * gst_rtsp_media_get_protocols:
577 * @media: a #GstRTSPMedia
579 * Get the allowed protocols of @media.
581 * Returns: a #GstRTSPLowerTrans
584 gst_rtsp_media_get_protocols (GstRTSPMedia * media)
586 GstRTSPMediaPrivate *priv;
587 GstRTSPLowerTrans res;
589 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media),
590 GST_RTSP_LOWER_TRANS_UNKNOWN);
594 g_mutex_lock (&priv->lock);
595 res = priv->protocols;
596 g_mutex_unlock (&priv->lock);
602 * gst_rtsp_media_set_eos_shutdown:
603 * @media: a #GstRTSPMedia
604 * @eos_shutdown: the new value
606 * Set or unset if an EOS event will be sent to the pipeline for @media before
610 gst_rtsp_media_set_eos_shutdown (GstRTSPMedia * media, gboolean eos_shutdown)
612 GstRTSPMediaPrivate *priv;
614 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
618 g_mutex_lock (&priv->lock);
619 priv->eos_shutdown = eos_shutdown;
620 g_mutex_unlock (&priv->lock);
624 * gst_rtsp_media_is_eos_shutdown:
625 * @media: a #GstRTSPMedia
627 * Check if the pipeline for @media will send an EOS down the pipeline before
630 * Returns: %TRUE if the media will send EOS before unpreparing.
633 gst_rtsp_media_is_eos_shutdown (GstRTSPMedia * media)
635 GstRTSPMediaPrivate *priv;
638 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
642 g_mutex_lock (&priv->lock);
643 res = priv->eos_shutdown;
644 g_mutex_unlock (&priv->lock);
650 * gst_rtsp_media_set_buffer_size:
651 * @media: a #GstRTSPMedia
652 * @size: the new value
654 * Set the kernel UDP buffer size.
657 gst_rtsp_media_set_buffer_size (GstRTSPMedia * media, guint size)
659 GstRTSPMediaPrivate *priv;
661 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
663 GST_LOG_OBJECT (media, "set buffer size %u", size);
667 g_mutex_lock (&priv->lock);
668 priv->buffer_size = size;
669 g_mutex_unlock (&priv->lock);
673 * gst_rtsp_media_get_buffer_size:
674 * @media: a #GstRTSPMedia
676 * Get the kernel UDP buffer size.
678 * Returns: the kernel UDP buffer size.
681 gst_rtsp_media_get_buffer_size (GstRTSPMedia * media)
683 GstRTSPMediaPrivate *priv;
686 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
690 g_mutex_unlock (&priv->lock);
691 res = priv->buffer_size;
692 g_mutex_unlock (&priv->lock);
698 * gst_rtsp_media_use_time_provider:
699 * @media: a #GstRTSPMedia
701 * Set @media to provide a GstNetTimeProvider.
704 gst_rtsp_media_use_time_provider (GstRTSPMedia * media, gboolean time_provider)
706 GstRTSPMediaPrivate *priv;
708 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
712 g_mutex_lock (&priv->lock);
713 priv->time_provider = time_provider;
714 g_mutex_unlock (&priv->lock);
718 * gst_rtsp_media_is_time_provider:
719 * @media: a #GstRTSPMedia
721 * Check if @media can provide a #GstNetTimeProvider for its pipeline clock.
723 * Use gst_rtsp_media_get_time_provider() to get the network clock.
725 * Returns: %TRUE if @media can provide a #GstNetTimeProvider.
728 gst_rtsp_media_is_time_provider (GstRTSPMedia * media)
730 GstRTSPMediaPrivate *priv;
733 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
737 g_mutex_unlock (&priv->lock);
738 res = priv->time_provider;
739 g_mutex_unlock (&priv->lock);
745 * gst_rtsp_media_set_auth:
746 * @media: a #GstRTSPMedia
747 * @auth: a #GstRTSPAuth
749 * configure @auth to be used as the authentication manager of @media.
752 gst_rtsp_media_set_auth (GstRTSPMedia * media, GstRTSPAuth * auth)
754 GstRTSPMediaPrivate *priv;
757 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
761 GST_LOG_OBJECT (media, "set auth %p", auth);
763 g_mutex_lock (&priv->lock);
764 if ((old = priv->auth) != auth)
765 priv->auth = auth ? g_object_ref (auth) : NULL;
768 g_mutex_unlock (&priv->lock);
771 g_object_unref (old);
775 * gst_rtsp_media_get_auth:
776 * @media: a #GstRTSPMedia
778 * Get the #GstRTSPAuth used as the authentication manager of @media.
780 * Returns: (transfer full): the #GstRTSPAuth of @media. g_object_unref() after
784 gst_rtsp_media_get_auth (GstRTSPMedia * media)
786 GstRTSPMediaPrivate *priv;
789 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
793 g_mutex_lock (&priv->lock);
794 if ((result = priv->auth))
795 g_object_ref (result);
796 g_mutex_unlock (&priv->lock);
802 * gst_rtsp_media_set_address_pool:
803 * @media: a #GstRTSPMedia
804 * @pool: a #GstRTSPAddressPool
806 * configure @pool to be used as the address pool of @media.
809 gst_rtsp_media_set_address_pool (GstRTSPMedia * media,
810 GstRTSPAddressPool * pool)
812 GstRTSPMediaPrivate *priv;
813 GstRTSPAddressPool *old;
815 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
819 GST_LOG_OBJECT (media, "set address pool %p", pool);
821 g_mutex_lock (&priv->lock);
822 if ((old = priv->pool) != pool)
823 priv->pool = pool ? g_object_ref (pool) : NULL;
826 g_ptr_array_foreach (priv->streams, (GFunc) gst_rtsp_stream_set_address_pool,
828 g_mutex_unlock (&priv->lock);
831 g_object_unref (old);
835 * gst_rtsp_media_get_address_pool:
836 * @media: a #GstRTSPMedia
838 * Get the #GstRTSPAddressPool used as the address pool of @media.
840 * Returns: (transfer full): the #GstRTSPAddressPool of @media. g_object_unref() after
844 gst_rtsp_media_get_address_pool (GstRTSPMedia * media)
846 GstRTSPMediaPrivate *priv;
847 GstRTSPAddressPool *result;
849 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
853 g_mutex_lock (&priv->lock);
854 if ((result = priv->pool))
855 g_object_ref (result);
856 g_mutex_unlock (&priv->lock);
862 * gst_rtsp_media_collect_streams:
863 * @media: a #GstRTSPMedia
865 * Find all payloader elements, they should be named pay%d in the
866 * element of @media, and create #GstRTSPStreams for them.
868 * Collect all dynamic elements, named dynpay%d, and add them to
869 * the list of dynamic elements.
872 gst_rtsp_media_collect_streams (GstRTSPMedia * media)
874 GstRTSPMediaPrivate *priv;
875 GstElement *element, *elem;
880 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
883 element = priv->element;
886 for (i = 0; have_elem; i++) {
891 name = g_strdup_printf ("pay%d", i);
892 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
893 GST_INFO ("found stream %d with payloader %p", i, elem);
895 /* take the pad of the payloader */
896 pad = gst_element_get_static_pad (elem, "src");
897 /* create the stream */
898 gst_rtsp_media_create_stream (media, elem, pad);
899 gst_object_unref (pad);
900 gst_object_unref (elem);
906 name = g_strdup_printf ("dynpay%d", i);
907 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
908 /* a stream that will dynamically create pads to provide RTP packets */
910 GST_INFO ("found dynamic element %d, %p", i, elem);
912 g_mutex_lock (&priv->lock);
913 priv->dynamic = g_list_prepend (priv->dynamic, elem);
914 g_mutex_unlock (&priv->lock);
923 * gst_rtsp_media_create_stream:
924 * @media: a #GstRTSPMedia
925 * @payloader: a #GstElement
926 * @srcpad: a source #GstPad
928 * Create a new stream in @media that provides RTP data on @srcpad.
929 * @srcpad should be a pad of an element inside @media->element.
931 * Returns: (transfer none): a new #GstRTSPStream that remains valid for as long
935 gst_rtsp_media_create_stream (GstRTSPMedia * media, GstElement * payloader,
938 GstRTSPMediaPrivate *priv;
939 GstRTSPStream *stream;
944 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
945 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
946 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
947 g_return_val_if_fail (GST_PAD_IS_SRC (pad), NULL);
951 g_mutex_lock (&priv->lock);
952 idx = priv->streams->len;
954 GST_DEBUG ("media %p: creating stream with index %d", media, idx);
956 name = g_strdup_printf ("src_%u", idx);
957 srcpad = gst_ghost_pad_new (name, pad);
958 gst_pad_set_active (srcpad, TRUE);
959 gst_element_add_pad (priv->element, srcpad);
962 stream = gst_rtsp_stream_new (idx, payloader, srcpad);
964 gst_rtsp_stream_set_address_pool (stream, priv->pool);
966 g_ptr_array_add (priv->streams, stream);
967 g_mutex_unlock (&priv->lock);
969 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STREAM], 0, stream,
976 * gst_rtsp_media_n_streams:
977 * @media: a #GstRTSPMedia
979 * Get the number of streams in this media.
981 * Returns: The number of streams.
984 gst_rtsp_media_n_streams (GstRTSPMedia * media)
986 GstRTSPMediaPrivate *priv;
989 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), 0);
993 g_mutex_lock (&priv->lock);
994 res = priv->streams->len;
995 g_mutex_unlock (&priv->lock);
1001 * gst_rtsp_media_get_stream:
1002 * @media: a #GstRTSPMedia
1003 * @idx: the stream index
1005 * Retrieve the stream with index @idx from @media.
1007 * Returns: (transfer none): the #GstRTSPStream at index @idx or %NULL when a stream with
1008 * that index did not exist.
1011 gst_rtsp_media_get_stream (GstRTSPMedia * media, guint idx)
1013 GstRTSPMediaPrivate *priv;
1016 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1020 g_mutex_lock (&priv->lock);
1021 if (idx < priv->streams->len)
1022 res = g_ptr_array_index (priv->streams, idx);
1025 g_mutex_unlock (&priv->lock);
1031 * gst_rtsp_media_get_range_string:
1032 * @media: a #GstRTSPMedia
1033 * @play: for the PLAY request
1034 * @unit: the unit to use for the string
1036 * Get the current range as a string. @media must be prepared with
1037 * gst_rtsp_media_prepare ().
1039 * Returns: The range as a string, g_free() after usage.
1042 gst_rtsp_media_get_range_string (GstRTSPMedia * media, gboolean play,
1043 GstRTSPRangeUnit unit)
1045 GstRTSPMediaPrivate *priv;
1047 GstRTSPTimeRange range;
1049 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1053 g_rec_mutex_lock (&priv->state_lock);
1054 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
1057 g_mutex_lock (&priv->lock);
1059 range = priv->range;
1061 if (!play && priv->n_active > 0) {
1062 range.min.type = GST_RTSP_TIME_NOW;
1063 range.min.seconds = -1;
1065 g_mutex_unlock (&priv->lock);
1066 g_rec_mutex_unlock (&priv->state_lock);
1068 gst_rtsp_range_convert_units (&range, unit);
1070 result = gst_rtsp_range_to_string (&range);
1077 GST_WARNING ("media %p was not prepared", media);
1078 g_rec_mutex_unlock (&priv->state_lock);
1084 * gst_rtsp_media_seek:
1085 * @media: a #GstRTSPMedia
1086 * @range: a #GstRTSPTimeRange
1088 * Seek the pipeline of @media to @range. @media must be prepared with
1089 * gst_rtsp_media_prepare().
1091 * Returns: %TRUE on success.
1094 gst_rtsp_media_seek (GstRTSPMedia * media, GstRTSPTimeRange * range)
1096 GstRTSPMediaPrivate *priv;
1099 GstClockTime start, stop;
1100 GstSeekType start_type, stop_type;
1102 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1103 g_return_val_if_fail (range != NULL, FALSE);
1107 g_rec_mutex_lock (&priv->state_lock);
1108 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
1111 if (!priv->seekable)
1114 /* depends on the current playing state of the pipeline. We might need to
1115 * queue this until we get EOS. */
1116 flags = GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_KEY_UNIT;
1118 start_type = stop_type = GST_SEEK_TYPE_NONE;
1120 if (!gst_rtsp_range_get_times (range, &start, &stop))
1123 GST_INFO ("got %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1124 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1125 GST_INFO ("current %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1126 GST_TIME_ARGS (priv->range_start), GST_TIME_ARGS (priv->range_stop));
1128 if (priv->range_start == start)
1129 start = GST_CLOCK_TIME_NONE;
1130 else if (start != GST_CLOCK_TIME_NONE)
1131 start_type = GST_SEEK_TYPE_SET;
1133 if (priv->range_stop == stop)
1134 stop = GST_CLOCK_TIME_NONE;
1135 else if (stop != GST_CLOCK_TIME_NONE)
1136 stop_type = GST_SEEK_TYPE_SET;
1138 if (start != GST_CLOCK_TIME_NONE || stop != GST_CLOCK_TIME_NONE) {
1139 GST_INFO ("seeking to %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1140 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1142 res = gst_element_seek (priv->pipeline, 1.0, GST_FORMAT_TIME,
1143 flags, start_type, start, stop_type, stop);
1145 /* and block for the seek to complete */
1146 GST_INFO ("done seeking %d", res);
1147 gst_element_get_state (priv->pipeline, NULL, NULL, -1);
1148 GST_INFO ("prerolled again");
1150 collect_media_stats (media);
1152 GST_INFO ("no seek needed");
1155 g_rec_mutex_unlock (&priv->state_lock);
1162 g_rec_mutex_unlock (&priv->state_lock);
1163 GST_INFO ("media %p is not prepared", media);
1168 g_rec_mutex_unlock (&priv->state_lock);
1169 GST_INFO ("pipeline is not seekable");
1174 g_rec_mutex_unlock (&priv->state_lock);
1175 GST_WARNING ("seek unit %d not supported", range->unit);
1181 gst_rtsp_media_set_status (GstRTSPMedia * media, GstRTSPMediaStatus status)
1183 GstRTSPMediaPrivate *priv = media->priv;
1185 g_mutex_lock (&priv->lock);
1186 priv->status = status;
1187 GST_DEBUG ("setting new status to %d", status);
1188 g_cond_broadcast (&priv->cond);
1189 g_mutex_unlock (&priv->lock);
1193 * gst_rtsp_media_get_status:
1194 * @media: a #GstRTSPMedia
1196 * Get the status of @media. When @media is busy preparing, this function waits
1197 * until @media is prepared or in error.
1199 * Returns: the status of @media.
1202 gst_rtsp_media_get_status (GstRTSPMedia * media)
1204 GstRTSPMediaPrivate *priv = media->priv;
1205 GstRTSPMediaStatus result;
1208 g_mutex_lock (&priv->lock);
1209 end_time = g_get_monotonic_time () + 20 * G_TIME_SPAN_SECOND;
1210 /* while we are preparing, wait */
1211 while (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING) {
1212 GST_DEBUG ("waiting for status change");
1213 if (!g_cond_wait_until (&priv->cond, &priv->lock, end_time)) {
1214 GST_DEBUG ("timeout, assuming error status");
1215 priv->status = GST_RTSP_MEDIA_STATUS_ERROR;
1218 /* could be success or error */
1219 result = priv->status;
1220 GST_DEBUG ("got status %d", result);
1221 g_mutex_unlock (&priv->lock);
1226 /* called with state-lock */
1228 default_handle_message (GstRTSPMedia * media, GstMessage * message)
1230 GstRTSPMediaPrivate *priv = media->priv;
1231 GstMessageType type;
1233 type = GST_MESSAGE_TYPE (message);
1236 case GST_MESSAGE_STATE_CHANGED:
1238 case GST_MESSAGE_BUFFERING:
1242 gst_message_parse_buffering (message, &percent);
1244 /* no state management needed for live pipelines */
1248 if (percent == 100) {
1249 /* a 100% message means buffering is done */
1250 priv->buffering = FALSE;
1251 /* if the desired state is playing, go back */
1252 if (priv->target_state == GST_STATE_PLAYING) {
1253 GST_INFO ("Buffering done, setting pipeline to PLAYING");
1254 gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
1256 GST_INFO ("Buffering done");
1259 /* buffering busy */
1260 if (priv->buffering == FALSE) {
1261 if (priv->target_state == GST_STATE_PLAYING) {
1262 /* we were not buffering but PLAYING, PAUSE the pipeline. */
1263 GST_INFO ("Buffering, setting pipeline to PAUSED ...");
1264 gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
1266 GST_INFO ("Buffering ...");
1269 priv->buffering = TRUE;
1273 case GST_MESSAGE_LATENCY:
1275 gst_bin_recalculate_latency (GST_BIN_CAST (priv->pipeline));
1278 case GST_MESSAGE_ERROR:
1283 gst_message_parse_error (message, &gerror, &debug);
1284 GST_WARNING ("%p: got error %s (%s)", media, gerror->message, debug);
1285 g_error_free (gerror);
1288 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
1291 case GST_MESSAGE_WARNING:
1296 gst_message_parse_warning (message, &gerror, &debug);
1297 GST_WARNING ("%p: got warning %s (%s)", media, gerror->message, debug);
1298 g_error_free (gerror);
1302 case GST_MESSAGE_ELEMENT:
1304 case GST_MESSAGE_STREAM_STATUS:
1306 case GST_MESSAGE_ASYNC_DONE:
1308 /* when we are dynamically adding pads, the addition of the udpsrc will
1309 * temporarily produce ASYNC_DONE messages. We have to ignore them and
1310 * wait for the final ASYNC_DONE after everything prerolled */
1311 GST_INFO ("%p: ignoring ASYNC_DONE", media);
1313 GST_INFO ("%p: got ASYNC_DONE", media);
1314 collect_media_stats (media);
1316 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
1317 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
1320 case GST_MESSAGE_EOS:
1321 GST_INFO ("%p: got EOS", media);
1323 if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARING) {
1324 GST_DEBUG ("shutting down after EOS");
1325 finish_unprepare (media);
1329 GST_INFO ("%p: got message type %d (%s)", media, type,
1330 gst_message_type_get_name (type));
1337 bus_message (GstBus * bus, GstMessage * message, GstRTSPMedia * media)
1339 GstRTSPMediaPrivate *priv = media->priv;
1340 GstRTSPMediaClass *klass;
1343 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1345 g_rec_mutex_lock (&priv->state_lock);
1346 if (klass->handle_message)
1347 ret = klass->handle_message (media, message);
1350 g_rec_mutex_unlock (&priv->state_lock);
1356 watch_destroyed (GstRTSPMedia * media)
1358 GST_DEBUG_OBJECT (media, "source destroyed");
1359 g_object_unref (media);
1362 /* called from streaming threads */
1364 pad_added_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
1366 GstRTSPMediaPrivate *priv = media->priv;
1367 GstRTSPStream *stream;
1369 /* FIXME, element is likely not a payloader, find the payloader here */
1370 stream = gst_rtsp_media_create_stream (media, element, pad);
1372 GST_INFO ("pad added %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
1374 g_rec_mutex_lock (&priv->state_lock);
1375 /* we will be adding elements below that will cause ASYNC_DONE to be
1376 * posted in the bus. We want to ignore those messages until the
1377 * pipeline really prerolled. */
1378 priv->adding = TRUE;
1380 /* join the element in the PAUSED state because this callback is
1381 * called from the streaming thread and it is PAUSED */
1382 gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
1383 priv->rtpbin, GST_STATE_PAUSED);
1385 priv->adding = FALSE;
1386 g_rec_mutex_unlock (&priv->state_lock);
1390 no_more_pads_cb (GstElement * element, GstRTSPMedia * media)
1392 GstRTSPMediaPrivate *priv = media->priv;
1393 GstElement *fakesink;
1395 g_mutex_lock (&priv->lock);
1396 GST_INFO ("no more pads");
1397 if ((fakesink = priv->fakesink)) {
1398 gst_object_ref (fakesink);
1399 priv->fakesink = NULL;
1400 g_mutex_unlock (&priv->lock);
1402 gst_bin_remove (GST_BIN (priv->pipeline), fakesink);
1403 gst_element_set_state (fakesink, GST_STATE_NULL);
1404 gst_object_unref (fakesink);
1405 GST_INFO ("removed fakesink");
1410 * gst_rtsp_media_prepare:
1411 * @media: a #GstRTSPMedia
1413 * Prepare @media for streaming. This function will create the objects
1414 * to manage the streaming. A pipeline must have been set on @media with
1415 * gst_rtsp_media_take_pipeline().
1417 * It will preroll the pipeline and collect vital information about the streams
1418 * such as the duration.
1420 * Returns: %TRUE on success.
1423 gst_rtsp_media_prepare (GstRTSPMedia * media)
1425 GstRTSPMediaPrivate *priv;
1426 GstStateChangeReturn ret;
1427 GstRTSPMediaStatus status;
1429 GstRTSPMediaClass *klass;
1433 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1437 g_rec_mutex_lock (&priv->state_lock);
1438 priv->prepare_count++;
1440 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED)
1443 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
1446 if (priv->status != GST_RTSP_MEDIA_STATUS_UNPREPARED)
1447 goto not_unprepared;
1449 if (!priv->reusable && priv->reused)
1452 priv->rtpbin = gst_element_factory_make ("rtpbin", NULL);
1453 if (priv->rtpbin == NULL)
1456 GST_INFO ("preparing media %p", media);
1458 /* reset some variables */
1459 priv->is_live = FALSE;
1460 priv->seekable = FALSE;
1461 priv->buffering = FALSE;
1462 /* we're preparing now */
1463 priv->status = GST_RTSP_MEDIA_STATUS_PREPARING;
1465 bus = gst_pipeline_get_bus (GST_PIPELINE_CAST (priv->pipeline));
1467 /* add the pipeline bus to our custom mainloop */
1468 priv->source = gst_bus_create_watch (bus);
1469 gst_object_unref (bus);
1471 g_source_set_callback (priv->source, (GSourceFunc) bus_message,
1472 g_object_ref (media), (GDestroyNotify) watch_destroyed);
1474 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1475 priv->id = g_source_attach (priv->source, klass->context);
1477 /* add stuff to the bin */
1478 gst_bin_add (GST_BIN (priv->pipeline), priv->rtpbin);
1480 /* link streams we already have, other streams might appear when we have
1481 * dynamic elements */
1482 for (i = 0; i < priv->streams->len; i++) {
1483 GstRTSPStream *stream;
1485 stream = g_ptr_array_index (priv->streams, i);
1487 gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
1488 priv->rtpbin, GST_STATE_NULL);
1491 for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
1492 GstElement *elem = walk->data;
1494 GST_INFO ("adding callbacks for dynamic element %p", elem);
1496 g_signal_connect (elem, "pad-added", (GCallback) pad_added_cb, media);
1497 g_signal_connect (elem, "no-more-pads", (GCallback) no_more_pads_cb, media);
1499 /* we add a fakesink here in order to make the state change async. We remove
1500 * the fakesink again in the no-more-pads callback. */
1501 priv->fakesink = gst_element_factory_make ("fakesink", "fakesink");
1502 gst_bin_add (GST_BIN (priv->pipeline), priv->fakesink);
1505 GST_INFO ("setting pipeline to PAUSED for media %p", media);
1506 /* first go to PAUSED */
1507 ret = gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
1508 priv->target_state = GST_STATE_PAUSED;
1511 case GST_STATE_CHANGE_SUCCESS:
1512 GST_INFO ("SUCCESS state change for media %p", media);
1513 priv->seekable = TRUE;
1515 case GST_STATE_CHANGE_ASYNC:
1516 GST_INFO ("ASYNC state change for media %p", media);
1517 priv->seekable = TRUE;
1519 case GST_STATE_CHANGE_NO_PREROLL:
1520 /* we need to go to PLAYING */
1521 GST_INFO ("NO_PREROLL state change: live media %p", media);
1522 /* FIXME we disable seeking for live streams for now. We should perform a
1523 * seeking query in preroll instead */
1524 priv->seekable = FALSE;
1525 priv->is_live = TRUE;
1526 ret = gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
1527 if (ret == GST_STATE_CHANGE_FAILURE)
1530 case GST_STATE_CHANGE_FAILURE:
1534 g_rec_mutex_unlock (&priv->state_lock);
1536 /* now wait for all pads to be prerolled, FIXME, we should somehow be
1537 * able to do this async so that we don't block the server thread. */
1538 status = gst_rtsp_media_get_status (media);
1539 if (status == GST_RTSP_MEDIA_STATUS_ERROR)
1542 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_PREPARED], 0, NULL);
1544 GST_INFO ("object %p is prerolled", media);
1551 GST_LOG ("media %p was prepared", media);
1552 g_rec_mutex_unlock (&priv->state_lock);
1558 GST_WARNING ("media %p was not unprepared", media);
1559 priv->prepare_count--;
1560 g_rec_mutex_unlock (&priv->state_lock);
1565 priv->prepare_count--;
1566 g_rec_mutex_unlock (&priv->state_lock);
1567 GST_WARNING ("can not reuse media %p", media);
1572 priv->prepare_count--;
1573 g_rec_mutex_unlock (&priv->state_lock);
1574 GST_WARNING ("no rtpbin element");
1575 g_warning ("failed to create element 'rtpbin', check your installation");
1580 GST_WARNING ("failed to preroll pipeline");
1581 gst_rtsp_media_unprepare (media);
1582 g_rec_mutex_unlock (&priv->state_lock);
1587 /* must be called with state-lock */
1589 finish_unprepare (GstRTSPMedia * media)
1591 GstRTSPMediaPrivate *priv = media->priv;
1594 GST_DEBUG ("shutting down");
1596 gst_element_set_state (priv->pipeline, GST_STATE_NULL);
1598 for (i = 0; i < priv->streams->len; i++) {
1599 GstRTSPStream *stream;
1601 GST_INFO ("Removing elements of stream %d from pipeline", i);
1603 stream = g_ptr_array_index (priv->streams, i);
1605 gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
1607 g_ptr_array_set_size (priv->streams, 0);
1609 gst_bin_remove (GST_BIN (priv->pipeline), priv->rtpbin);
1610 priv->rtpbin = NULL;
1613 gst_object_unref (priv->nettime);
1614 priv->nettime = NULL;
1616 priv->reused = TRUE;
1617 priv->status = GST_RTSP_MEDIA_STATUS_UNPREPARED;
1619 /* when the media is not reusable, this will effectively unref the media and
1621 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_UNPREPARED], 0, NULL);
1623 /* the source has the last ref to the media */
1625 GST_DEBUG ("destroy source");
1626 g_source_destroy (priv->source);
1627 g_source_unref (priv->source);
1631 /* called with state-lock */
1633 default_unprepare (GstRTSPMedia * media)
1635 GstRTSPMediaPrivate *priv = media->priv;
1637 if (priv->eos_shutdown) {
1638 GST_DEBUG ("sending EOS for shutdown");
1639 /* ref so that we don't disappear */
1640 gst_element_send_event (priv->pipeline, gst_event_new_eos ());
1641 /* we need to go to playing again for the EOS to propagate, normally in this
1642 * state, nothing is receiving data from us anymore so this is ok. */
1643 gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
1644 priv->status = GST_RTSP_MEDIA_STATUS_UNPREPARING;
1646 finish_unprepare (media);
1652 * gst_rtsp_media_unprepare:
1653 * @media: a #GstRTSPMedia
1655 * Unprepare @media. After this call, the media should be prepared again before
1656 * it can be used again. If the media is set to be non-reusable, a new instance
1659 * Returns: %TRUE on success.
1662 gst_rtsp_media_unprepare (GstRTSPMedia * media)
1664 GstRTSPMediaPrivate *priv;
1667 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1671 g_rec_mutex_lock (&priv->state_lock);
1672 if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARED)
1673 goto was_unprepared;
1675 priv->prepare_count--;
1676 if (priv->prepare_count > 0)
1679 GST_INFO ("unprepare media %p", media);
1680 priv->target_state = GST_STATE_NULL;
1683 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED) {
1684 GstRTSPMediaClass *klass;
1686 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1687 if (klass->unprepare)
1688 success = klass->unprepare (media);
1690 finish_unprepare (media);
1692 g_rec_mutex_unlock (&priv->state_lock);
1698 g_rec_mutex_unlock (&priv->state_lock);
1699 GST_INFO ("media %p was already unprepared", media);
1704 GST_INFO ("media %p still prepared %d times", media, priv->prepare_count);
1705 g_rec_mutex_unlock (&priv->state_lock);
1710 /* should be called with state-lock */
1712 get_clock_unlocked (GstRTSPMedia * media)
1714 if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED) {
1715 GST_DEBUG_OBJECT (media, "media was not prepared");
1718 return gst_pipeline_get_clock (GST_PIPELINE_CAST (media->priv->pipeline));
1722 * gst_rtsp_media_get_clock:
1723 * @media: a #GstRTSPMedia
1725 * Get the clock that is used by the pipeline in @media.
1727 * @media must be prepared before this method returns a valid clock object.
1729 * Returns: the #GstClock used by @media. unref after usage.
1732 gst_rtsp_media_get_clock (GstRTSPMedia * media)
1735 GstRTSPMediaPrivate *priv;
1737 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1741 g_rec_mutex_lock (&priv->state_lock);
1742 clock = get_clock_unlocked (media);
1743 g_rec_mutex_unlock (&priv->state_lock);
1749 * gst_rtsp_media_get_base_time:
1750 * @media: a #GstRTSPMedia
1752 * Get the base_time that is used by the pipeline in @media.
1754 * @media must be prepared before this method returns a valid base_time.
1756 * Returns: the base_time used by @media.
1759 gst_rtsp_media_get_base_time (GstRTSPMedia * media)
1761 GstClockTime result;
1762 GstRTSPMediaPrivate *priv;
1764 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_CLOCK_TIME_NONE);
1768 g_rec_mutex_lock (&priv->state_lock);
1769 if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
1772 result = gst_element_get_base_time (media->priv->pipeline);
1773 g_rec_mutex_unlock (&priv->state_lock);
1780 g_rec_mutex_unlock (&priv->state_lock);
1781 GST_DEBUG_OBJECT (media, "media was not prepared");
1782 return GST_CLOCK_TIME_NONE;
1787 * gst_rtsp_media_get_time_provider:
1788 * @media: a #GstRTSPMedia
1789 * @address: an address or NULL
1790 * @port: a port or 0
1792 * Get the #GstNetTimeProvider for the clock used by @media. The time provider
1793 * will listen on @address and @port for client time requests.
1795 * Returns: the #GstNetTimeProvider of @media.
1797 GstNetTimeProvider *
1798 gst_rtsp_media_get_time_provider (GstRTSPMedia * media, const gchar * address,
1801 GstRTSPMediaPrivate *priv;
1802 GstNetTimeProvider *provider = NULL;
1804 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1808 g_rec_mutex_lock (&priv->state_lock);
1809 if (priv->time_provider) {
1810 if ((provider = priv->nettime) == NULL) {
1813 if (priv->time_provider && (clock = get_clock_unlocked (media))) {
1814 provider = gst_net_time_provider_new (clock, address, port);
1815 gst_object_unref (clock);
1817 priv->nettime = provider;
1821 g_rec_mutex_unlock (&priv->state_lock);
1824 gst_object_ref (provider);
1830 * gst_rtsp_media_set_state:
1831 * @media: a #GstRTSPMedia
1832 * @state: the target state of the media
1833 * @transports: a #GPtrArray of #GstRTSPStreamTransport pointers
1835 * Set the state of @media to @state and for the transports in @transports.
1837 * @media must be prepared with gst_rtsp_media_prepare();
1839 * Returns: %TRUE on success.
1842 gst_rtsp_media_set_state (GstRTSPMedia * media, GstState state,
1843 GPtrArray * transports)
1845 GstRTSPMediaPrivate *priv;
1847 gboolean activate, deactivate, do_state;
1850 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1851 g_return_val_if_fail (transports != NULL, FALSE);
1855 g_rec_mutex_lock (&priv->state_lock);
1856 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
1859 /* NULL and READY are the same */
1860 if (state == GST_STATE_READY)
1861 state = GST_STATE_NULL;
1863 activate = deactivate = FALSE;
1865 GST_INFO ("going to state %s media %p", gst_element_state_get_name (state),
1869 case GST_STATE_NULL:
1870 case GST_STATE_PAUSED:
1871 /* we're going from PLAYING to PAUSED, READY or NULL, deactivate */
1872 if (priv->target_state == GST_STATE_PLAYING)
1875 case GST_STATE_PLAYING:
1876 /* we're going to PLAYING, activate */
1882 old_active = priv->n_active;
1884 for (i = 0; i < transports->len; i++) {
1885 GstRTSPStreamTransport *trans;
1887 /* we need a non-NULL entry in the array */
1888 trans = g_ptr_array_index (transports, i);
1893 if (gst_rtsp_stream_transport_set_active (trans, TRUE))
1895 } else if (deactivate) {
1896 if (gst_rtsp_stream_transport_set_active (trans, FALSE))
1901 /* we just activated the first media, do the playing state change */
1902 if (old_active == 0 && activate)
1904 /* if we have no more active media, do the downward state changes */
1905 else if (priv->n_active == 0)
1910 GST_INFO ("state %d active %d media %p do_state %d", state, priv->n_active,
1913 if (priv->target_state != state) {
1915 if (state == GST_STATE_NULL) {
1916 gst_rtsp_media_unprepare (media);
1918 GST_INFO ("state %s media %p", gst_element_state_get_name (state),
1920 priv->target_state = state;
1921 /* when we are buffering, don't update the state yet, this will be done
1922 * when buffering finishes */
1923 if (priv->buffering) {
1924 GST_INFO ("Buffering busy, delay state change");
1926 gst_element_set_state (priv->pipeline, state);
1930 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STATE], 0, state,
1934 /* remember where we are */
1935 if (state != GST_STATE_NULL && (state == GST_STATE_PAUSED ||
1936 old_active != priv->n_active))
1937 collect_media_stats (media);
1939 g_rec_mutex_unlock (&priv->state_lock);
1946 GST_WARNING ("media %p was not prepared", media);
1947 g_rec_mutex_unlock (&priv->state_lock);