2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 * @short_description: The media pipeline
22 * @see_also: #GstRTSPMediaFactory, #GstRTSPStream, #GstRTSPSession,
23 * #GstRTSPSessionMedia
25 * a #GstRTSPMedia contains the complete GStreamer pipeline to manage the
26 * streaming to the clients. The actual data transfer is done by the
27 * #GstRTSPStream objects that are created and exposed by the #GstRTSPMedia.
29 * The #GstRTSPMedia is usually created from a #GstRTSPMediaFactory when the
30 * client does a DESCRIBE or SETUP of a resource.
32 * A media is created with gst_rtsp_media_new() that takes the element that will
33 * provide the streaming elements. For each of the streams, a new #GstRTSPStream
34 * object needs to be made with the gst_rtsp_media_create_stream() which takes
35 * the payloader element and the source pad that produces the RTP stream.
37 * The pipeline of the media is set to PAUSED with gst_rtsp_media_prepare(). The
38 * prepare method will add rtpbin and sinks and sources to send and receive RTP
39 * and RTCP packets from the clients. Each stream srcpad is connected to an
40 * input into the internal rtpbin.
42 * It is also possible to dynamically create #GstRTSPStream objects during the
43 * prepare phase. With gst_rtsp_media_get_status() you can check the status of
46 * After the media is prepared, it is ready for streaming. It will usually be
47 * managed in a session with gst_rtsp_session_manage_media(). See
48 * #GstRTSPSession and #GstRTSPSessionMedia.
50 * The state of the media can be controlled with gst_rtsp_media_set_state ().
51 * Seeking can be done with gst_rtsp_media_seek().
53 * With gst_rtsp_media_unprepare() the pipeline is stopped and shut down. When
54 * gst_rtsp_media_set_eos_shutdown() an EOS will be sent to the pipeline to
57 * With gst_rtsp_media_set_shared(), the media can be shared between multiple
58 * clients. With gst_rtsp_media_set_reusable() you can control if the pipeline
59 * can be prepared again after an unprepare.
61 * Last reviewed on 2013-07-11 (1.0.0)
67 #include <gst/app/gstappsrc.h>
68 #include <gst/app/gstappsink.h>
70 #include "rtsp-media.h"
72 #define GST_RTSP_MEDIA_GET_PRIVATE(obj) \
73 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_MEDIA, GstRTSPMediaPrivate))
75 struct _GstRTSPMediaPrivate
80 /* protected by lock */
81 GstRTSPPermissions *permissions;
83 gboolean suspend_mode;
85 GstRTSPProfile profiles;
86 GstRTSPLowerTrans protocols;
88 gboolean eos_shutdown;
90 GstRTSPAddressPool *pool;
94 GRecMutex state_lock; /* locking order: state lock, lock */
95 GPtrArray *streams; /* protected by lock */
96 GList *dynamic; /* protected by lock */
97 GstRTSPMediaStatus status; /* protected by lock */
102 /* the pipeline for the media */
103 GstElement *pipeline;
104 GstElement *fakesink; /* protected by lock */
107 GstRTSPThread *thread;
109 gboolean time_provider;
110 GstNetTimeProvider *nettime;
115 GstState target_state;
117 /* RTP session manager */
120 /* the range of media */
121 GstRTSPTimeRange range; /* protected by lock */
122 GstClockTime range_start;
123 GstClockTime range_stop;
126 #define DEFAULT_SHARED FALSE
127 #define DEFAULT_SUSPEND_MODE GST_RTSP_SUSPEND_MODE_NONE
128 #define DEFAULT_REUSABLE FALSE
129 #define DEFAULT_PROFILES GST_RTSP_PROFILE_AVP
130 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | \
131 GST_RTSP_LOWER_TRANS_TCP
132 #define DEFAULT_EOS_SHUTDOWN FALSE
133 #define DEFAULT_BUFFER_SIZE 0x80000
134 #define DEFAULT_TIME_PROVIDER FALSE
136 /* define to dump received RTCP packets */
157 SIGNAL_REMOVED_STREAM,
165 GST_DEBUG_CATEGORY_STATIC (rtsp_media_debug);
166 #define GST_CAT_DEFAULT rtsp_media_debug
168 static void gst_rtsp_media_get_property (GObject * object, guint propid,
169 GValue * value, GParamSpec * pspec);
170 static void gst_rtsp_media_set_property (GObject * object, guint propid,
171 const GValue * value, GParamSpec * pspec);
172 static void gst_rtsp_media_finalize (GObject * obj);
174 static gboolean default_handle_message (GstRTSPMedia * media,
175 GstMessage * message);
176 static void finish_unprepare (GstRTSPMedia * media);
177 static gboolean default_unprepare (GstRTSPMedia * media);
178 static gboolean default_convert_range (GstRTSPMedia * media,
179 GstRTSPTimeRange * range, GstRTSPRangeUnit unit);
180 static gboolean default_query_position (GstRTSPMedia * media,
182 static gboolean default_query_stop (GstRTSPMedia * media, gint64 * stop);
183 static GstElement *default_create_rtpbin (GstRTSPMedia * media);
184 static gboolean default_setup_sdp (GstRTSPMedia * media, GstSDPMessage * sdp,
187 static gboolean wait_preroll (GstRTSPMedia * media);
189 static guint gst_rtsp_media_signals[SIGNAL_LAST] = { 0 };
191 #define C_ENUM(v) ((gint) v)
193 #define GST_TYPE_RTSP_SUSPEND_MODE (gst_rtsp_suspend_mode_get_type())
195 gst_rtsp_suspend_mode_get_type (void)
198 static const GEnumValue values[] = {
199 {C_ENUM (GST_RTSP_SUSPEND_MODE_NONE), "GST_RTSP_SUSPEND_MODE_NONE", "none"},
200 {C_ENUM (GST_RTSP_SUSPEND_MODE_PAUSE), "GST_RTSP_SUSPEND_MODE_PAUSE",
202 {C_ENUM (GST_RTSP_SUSPEND_MODE_RESET), "GST_RTSP_SUSPEND_MODE_RESET",
207 if (g_once_init_enter (&id)) {
208 GType tmp = g_enum_register_static ("GstRTSPSuspendMode", values);
209 g_once_init_leave (&id, tmp);
214 G_DEFINE_TYPE (GstRTSPMedia, gst_rtsp_media, G_TYPE_OBJECT);
217 gst_rtsp_media_class_init (GstRTSPMediaClass * klass)
219 GObjectClass *gobject_class;
221 g_type_class_add_private (klass, sizeof (GstRTSPMediaPrivate));
223 gobject_class = G_OBJECT_CLASS (klass);
225 gobject_class->get_property = gst_rtsp_media_get_property;
226 gobject_class->set_property = gst_rtsp_media_set_property;
227 gobject_class->finalize = gst_rtsp_media_finalize;
229 g_object_class_install_property (gobject_class, PROP_SHARED,
230 g_param_spec_boolean ("shared", "Shared",
231 "If this media pipeline can be shared", DEFAULT_SHARED,
232 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
234 g_object_class_install_property (gobject_class, PROP_SUSPEND_MODE,
235 g_param_spec_enum ("suspend-mode", "Suspend Mode",
236 "How to suspend the media in PAUSED", GST_TYPE_RTSP_SUSPEND_MODE,
237 DEFAULT_SUSPEND_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
239 g_object_class_install_property (gobject_class, PROP_REUSABLE,
240 g_param_spec_boolean ("reusable", "Reusable",
241 "If this media pipeline can be reused after an unprepare",
242 DEFAULT_REUSABLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
244 #ifdef GST_TYPE_RTSP_PROFILE
245 g_object_class_install_property (gobject_class, PROP_PROFILES,
246 g_param_spec_flags ("profiles", "Profiles",
247 "Allowed transfer profiles", GST_TYPE_RTSP_PROFILE,
248 DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
251 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
252 g_param_spec_flags ("protocols", "Protocols",
253 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
254 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
256 g_object_class_install_property (gobject_class, PROP_EOS_SHUTDOWN,
257 g_param_spec_boolean ("eos-shutdown", "EOS Shutdown",
258 "Send an EOS event to the pipeline before unpreparing",
259 DEFAULT_EOS_SHUTDOWN, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
261 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
262 g_param_spec_uint ("buffer-size", "Buffer Size",
263 "The kernel UDP buffer size to use", 0, G_MAXUINT,
264 DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
266 g_object_class_install_property (gobject_class, PROP_ELEMENT,
267 g_param_spec_object ("element", "The Element",
268 "The GstBin to use for streaming the media", GST_TYPE_ELEMENT,
269 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
271 g_object_class_install_property (gobject_class, PROP_TIME_PROVIDER,
272 g_param_spec_boolean ("time-provider", "Time Provider",
273 "Use a NetTimeProvider for clients",
274 DEFAULT_TIME_PROVIDER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
276 gst_rtsp_media_signals[SIGNAL_NEW_STREAM] =
277 g_signal_new ("new-stream", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
278 G_STRUCT_OFFSET (GstRTSPMediaClass, new_stream), NULL, NULL,
279 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_RTSP_STREAM);
281 gst_rtsp_media_signals[SIGNAL_REMOVED_STREAM] =
282 g_signal_new ("removed-stream", G_TYPE_FROM_CLASS (klass),
283 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPMediaClass, removed_stream),
284 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
285 GST_TYPE_RTSP_STREAM);
287 gst_rtsp_media_signals[SIGNAL_PREPARED] =
288 g_signal_new ("prepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
289 G_STRUCT_OFFSET (GstRTSPMediaClass, prepared), NULL, NULL,
290 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
292 gst_rtsp_media_signals[SIGNAL_UNPREPARED] =
293 g_signal_new ("unprepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
294 G_STRUCT_OFFSET (GstRTSPMediaClass, unprepared), NULL, NULL,
295 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
297 gst_rtsp_media_signals[SIGNAL_TARGET_STATE] =
298 g_signal_new ("target-state", G_TYPE_FROM_CLASS (klass),
299 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPMediaClass, new_state), NULL,
300 NULL, g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
302 gst_rtsp_media_signals[SIGNAL_NEW_STATE] =
303 g_signal_new ("new-state", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
304 G_STRUCT_OFFSET (GstRTSPMediaClass, new_state), NULL, NULL,
305 g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
307 GST_DEBUG_CATEGORY_INIT (rtsp_media_debug, "rtspmedia", 0, "GstRTSPMedia");
309 klass->handle_message = default_handle_message;
310 klass->unprepare = default_unprepare;
311 klass->convert_range = default_convert_range;
312 klass->query_position = default_query_position;
313 klass->query_stop = default_query_stop;
314 klass->create_rtpbin = default_create_rtpbin;
315 klass->setup_sdp = default_setup_sdp;
319 gst_rtsp_media_init (GstRTSPMedia * media)
321 GstRTSPMediaPrivate *priv = GST_RTSP_MEDIA_GET_PRIVATE (media);
325 priv->streams = g_ptr_array_new_with_free_func (g_object_unref);
326 g_mutex_init (&priv->lock);
327 g_cond_init (&priv->cond);
328 g_rec_mutex_init (&priv->state_lock);
330 priv->shared = DEFAULT_SHARED;
331 priv->suspend_mode = DEFAULT_SUSPEND_MODE;
332 priv->reusable = DEFAULT_REUSABLE;
333 priv->profiles = DEFAULT_PROFILES;
334 priv->protocols = DEFAULT_PROTOCOLS;
335 priv->eos_shutdown = DEFAULT_EOS_SHUTDOWN;
336 priv->buffer_size = DEFAULT_BUFFER_SIZE;
337 priv->time_provider = DEFAULT_TIME_PROVIDER;
341 gst_rtsp_media_finalize (GObject * obj)
343 GstRTSPMediaPrivate *priv;
346 media = GST_RTSP_MEDIA (obj);
349 GST_INFO ("finalize media %p", media);
351 if (priv->permissions)
352 gst_rtsp_permissions_unref (priv->permissions);
354 g_ptr_array_unref (priv->streams);
356 g_list_free_full (priv->dynamic, gst_object_unref);
359 gst_object_unref (priv->pipeline);
361 gst_object_unref (priv->nettime);
362 gst_object_unref (priv->element);
364 g_object_unref (priv->pool);
365 g_mutex_clear (&priv->lock);
366 g_cond_clear (&priv->cond);
367 g_rec_mutex_clear (&priv->state_lock);
369 G_OBJECT_CLASS (gst_rtsp_media_parent_class)->finalize (obj);
373 gst_rtsp_media_get_property (GObject * object, guint propid,
374 GValue * value, GParamSpec * pspec)
376 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
380 g_value_set_object (value, media->priv->element);
383 g_value_set_boolean (value, gst_rtsp_media_is_shared (media));
385 case PROP_SUSPEND_MODE:
386 g_value_set_enum (value, gst_rtsp_media_get_suspend_mode (media));
389 g_value_set_boolean (value, gst_rtsp_media_is_reusable (media));
392 g_value_set_flags (value, gst_rtsp_media_get_profiles (media));
395 g_value_set_flags (value, gst_rtsp_media_get_protocols (media));
397 case PROP_EOS_SHUTDOWN:
398 g_value_set_boolean (value, gst_rtsp_media_is_eos_shutdown (media));
400 case PROP_BUFFER_SIZE:
401 g_value_set_uint (value, gst_rtsp_media_get_buffer_size (media));
403 case PROP_TIME_PROVIDER:
404 g_value_set_boolean (value, gst_rtsp_media_is_time_provider (media));
407 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
412 gst_rtsp_media_set_property (GObject * object, guint propid,
413 const GValue * value, GParamSpec * pspec)
415 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
419 media->priv->element = g_value_get_object (value);
420 gst_object_ref_sink (media->priv->element);
423 gst_rtsp_media_set_shared (media, g_value_get_boolean (value));
425 case PROP_SUSPEND_MODE:
426 gst_rtsp_media_set_suspend_mode (media, g_value_get_enum (value));
429 gst_rtsp_media_set_reusable (media, g_value_get_boolean (value));
432 gst_rtsp_media_set_profiles (media, g_value_get_flags (value));
435 gst_rtsp_media_set_protocols (media, g_value_get_flags (value));
437 case PROP_EOS_SHUTDOWN:
438 gst_rtsp_media_set_eos_shutdown (media, g_value_get_boolean (value));
440 case PROP_BUFFER_SIZE:
441 gst_rtsp_media_set_buffer_size (media, g_value_get_uint (value));
443 case PROP_TIME_PROVIDER:
444 gst_rtsp_media_use_time_provider (media, g_value_get_boolean (value));
447 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
452 default_query_position (GstRTSPMedia * media, gint64 * position)
454 return gst_element_query_position (media->priv->pipeline, GST_FORMAT_TIME,
459 default_query_stop (GstRTSPMedia * media, gint64 * stop)
464 query = gst_query_new_segment (GST_FORMAT_TIME);
465 if ((res = gst_element_query (media->priv->pipeline, query))) {
467 gst_query_parse_segment (query, NULL, &format, NULL, stop);
468 if (format != GST_FORMAT_TIME)
471 gst_query_unref (query);
476 default_create_rtpbin (GstRTSPMedia * media)
480 rtpbin = gst_element_factory_make ("rtpbin", NULL);
485 /* must be called with state lock */
487 collect_media_stats (GstRTSPMedia * media)
489 GstRTSPMediaPrivate *priv = media->priv;
490 gint64 position, stop;
492 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
493 priv->status != GST_RTSP_MEDIA_STATUS_PREPARING)
496 priv->range.unit = GST_RTSP_RANGE_NPT;
498 GST_INFO ("collect media stats");
501 priv->range.min.type = GST_RTSP_TIME_NOW;
502 priv->range.min.seconds = -1;
503 priv->range_start = -1;
504 priv->range.max.type = GST_RTSP_TIME_END;
505 priv->range.max.seconds = -1;
506 priv->range_stop = -1;
508 GstRTSPMediaClass *klass;
511 klass = GST_RTSP_MEDIA_GET_CLASS (media);
513 /* get the position */
515 if (klass->query_position)
516 ret = klass->query_position (media, &position);
519 GST_INFO ("position query failed");
523 /* get the current segment stop */
525 if (klass->query_stop)
526 ret = klass->query_stop (media, &stop);
529 GST_INFO ("stop query failed");
533 GST_INFO ("stats: position %" GST_TIME_FORMAT ", stop %"
534 GST_TIME_FORMAT, GST_TIME_ARGS (position), GST_TIME_ARGS (stop));
536 if (position == -1) {
537 priv->range.min.type = GST_RTSP_TIME_NOW;
538 priv->range.min.seconds = -1;
539 priv->range_start = -1;
541 priv->range.min.type = GST_RTSP_TIME_SECONDS;
542 priv->range.min.seconds = ((gdouble) position) / GST_SECOND;
543 priv->range_start = position;
546 priv->range.max.type = GST_RTSP_TIME_END;
547 priv->range.max.seconds = -1;
548 priv->range_stop = -1;
550 priv->range.max.type = GST_RTSP_TIME_SECONDS;
551 priv->range.max.seconds = ((gdouble) stop) / GST_SECOND;
552 priv->range_stop = stop;
558 * gst_rtsp_media_new:
559 * @element: (transfer full): a #GstElement
561 * Create a new #GstRTSPMedia instance. @element is the bin element that
562 * provides the different streams. The #GstRTSPMedia object contains the
563 * element to produce RTP data for one or more related (audio/video/..)
566 * Ownership is taken of @element.
568 * Returns: a new #GstRTSPMedia object.
571 gst_rtsp_media_new (GstElement * element)
573 GstRTSPMedia *result;
575 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
577 result = g_object_new (GST_TYPE_RTSP_MEDIA, "element", element, NULL);
583 * gst_rtsp_media_get_element:
584 * @media: a #GstRTSPMedia
586 * Get the element that was used when constructing @media.
588 * Returns: (transfer full): a #GstElement. Unref after usage.
591 gst_rtsp_media_get_element (GstRTSPMedia * media)
593 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
595 return gst_object_ref (media->priv->element);
599 * gst_rtsp_media_take_pipeline:
600 * @media: a #GstRTSPMedia
601 * @pipeline: (transfer full): a #GstPipeline
603 * Set @pipeline as the #GstPipeline for @media. Ownership is
604 * taken of @pipeline.
607 gst_rtsp_media_take_pipeline (GstRTSPMedia * media, GstPipeline * pipeline)
609 GstRTSPMediaPrivate *priv;
611 GstNetTimeProvider *nettime;
613 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
614 g_return_if_fail (GST_IS_PIPELINE (pipeline));
618 g_mutex_lock (&priv->lock);
619 old = priv->pipeline;
620 priv->pipeline = GST_ELEMENT_CAST (pipeline);
621 nettime = priv->nettime;
622 priv->nettime = NULL;
623 g_mutex_unlock (&priv->lock);
626 gst_object_unref (old);
629 gst_object_unref (nettime);
631 gst_bin_add (GST_BIN_CAST (pipeline), priv->element);
635 * gst_rtsp_media_set_permissions:
636 * @media: a #GstRTSPMedia
637 * @permissions: a #GstRTSPPermissions
639 * Set @permissions on @media.
642 gst_rtsp_media_set_permissions (GstRTSPMedia * media,
643 GstRTSPPermissions * permissions)
645 GstRTSPMediaPrivate *priv;
647 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
651 g_mutex_lock (&priv->lock);
652 if (priv->permissions)
653 gst_rtsp_permissions_unref (priv->permissions);
654 if ((priv->permissions = permissions))
655 gst_rtsp_permissions_ref (permissions);
656 g_mutex_unlock (&priv->lock);
660 * gst_rtsp_media_get_permissions:
661 * @media: a #GstRTSPMedia
663 * Get the permissions object from @media.
665 * Returns: (transfer full): a #GstRTSPPermissions object, unref after usage.
668 gst_rtsp_media_get_permissions (GstRTSPMedia * media)
670 GstRTSPMediaPrivate *priv;
671 GstRTSPPermissions *result;
673 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
677 g_mutex_lock (&priv->lock);
678 if ((result = priv->permissions))
679 gst_rtsp_permissions_ref (result);
680 g_mutex_unlock (&priv->lock);
686 * gst_rtsp_media_set_suspend_mode:
687 * @media: a #GstRTSPMedia
688 * @mode: the new #GstRTSPSuspendMode
690 * Control how @ media will be suspended after the SDP has been generated and
691 * after a PAUSE request has been performed.
693 * Media must be unprepared when setting the suspend mode.
696 gst_rtsp_media_set_suspend_mode (GstRTSPMedia * media, GstRTSPSuspendMode mode)
698 GstRTSPMediaPrivate *priv;
700 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
704 g_rec_mutex_lock (&priv->state_lock);
705 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED)
707 priv->suspend_mode = mode;
708 g_rec_mutex_unlock (&priv->state_lock);
715 GST_WARNING ("media %p was prepared", media);
716 g_rec_mutex_unlock (&priv->state_lock);
721 * gst_rtsp_media_get_suspend_mode:
722 * @media: a #GstRTSPMedia
724 * Get how @media will be suspended.
726 * Returns: #GstRTSPSuspendMode.
729 gst_rtsp_media_get_suspend_mode (GstRTSPMedia * media)
731 GstRTSPMediaPrivate *priv;
732 GstRTSPSuspendMode res;
734 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_RTSP_SUSPEND_MODE_NONE);
738 g_rec_mutex_lock (&priv->state_lock);
739 res = priv->suspend_mode;
740 g_rec_mutex_unlock (&priv->state_lock);
746 * gst_rtsp_media_set_shared:
747 * @media: a #GstRTSPMedia
748 * @shared: the new value
750 * Set or unset if the pipeline for @media can be shared will multiple clients.
751 * When @shared is %TRUE, client requests for this media will share the media
755 gst_rtsp_media_set_shared (GstRTSPMedia * media, gboolean shared)
757 GstRTSPMediaPrivate *priv;
759 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
763 g_mutex_lock (&priv->lock);
764 priv->shared = shared;
765 g_mutex_unlock (&priv->lock);
769 * gst_rtsp_media_is_shared:
770 * @media: a #GstRTSPMedia
772 * Check if the pipeline for @media can be shared between multiple clients.
774 * Returns: %TRUE if the media can be shared between clients.
777 gst_rtsp_media_is_shared (GstRTSPMedia * media)
779 GstRTSPMediaPrivate *priv;
782 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
786 g_mutex_lock (&priv->lock);
788 g_mutex_unlock (&priv->lock);
794 * gst_rtsp_media_set_reusable:
795 * @media: a #GstRTSPMedia
796 * @reusable: the new value
798 * Set or unset if the pipeline for @media can be reused after the pipeline has
802 gst_rtsp_media_set_reusable (GstRTSPMedia * media, gboolean reusable)
804 GstRTSPMediaPrivate *priv;
806 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
810 g_mutex_lock (&priv->lock);
811 priv->reusable = reusable;
812 g_mutex_unlock (&priv->lock);
816 * gst_rtsp_media_is_reusable:
817 * @media: a #GstRTSPMedia
819 * Check if the pipeline for @media can be reused after an unprepare.
821 * Returns: %TRUE if the media can be reused
824 gst_rtsp_media_is_reusable (GstRTSPMedia * media)
826 GstRTSPMediaPrivate *priv;
829 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
833 g_mutex_lock (&priv->lock);
834 res = priv->reusable;
835 g_mutex_unlock (&priv->lock);
841 do_set_profiles (GstRTSPStream * stream, GstRTSPProfile * profiles)
843 gst_rtsp_stream_set_profiles (stream, *profiles);
847 * gst_rtsp_media_set_profiles:
848 * @media: a #GstRTSPMedia
849 * @profiles: the new flags
851 * Configure the allowed lower transport for @media.
854 gst_rtsp_media_set_profiles (GstRTSPMedia * media, GstRTSPProfile profiles)
856 GstRTSPMediaPrivate *priv;
858 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
862 g_mutex_lock (&priv->lock);
863 priv->profiles = profiles;
864 g_ptr_array_foreach (priv->streams, (GFunc) do_set_profiles, &profiles);
865 g_mutex_unlock (&priv->lock);
869 * gst_rtsp_media_get_profiles:
870 * @media: a #GstRTSPMedia
872 * Get the allowed profiles of @media.
874 * Returns: a #GstRTSPProfile
877 gst_rtsp_media_get_profiles (GstRTSPMedia * media)
879 GstRTSPMediaPrivate *priv;
882 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_RTSP_PROFILE_UNKNOWN);
886 g_mutex_lock (&priv->lock);
887 res = priv->profiles;
888 g_mutex_unlock (&priv->lock);
894 do_set_protocols (GstRTSPStream * stream, GstRTSPLowerTrans * protocols)
896 gst_rtsp_stream_set_protocols (stream, *protocols);
900 * gst_rtsp_media_set_protocols:
901 * @media: a #GstRTSPMedia
902 * @protocols: the new flags
904 * Configure the allowed lower transport for @media.
907 gst_rtsp_media_set_protocols (GstRTSPMedia * media, GstRTSPLowerTrans protocols)
909 GstRTSPMediaPrivate *priv;
911 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
915 g_mutex_lock (&priv->lock);
916 priv->protocols = protocols;
917 g_ptr_array_foreach (priv->streams, (GFunc) do_set_protocols, &protocols);
918 g_mutex_unlock (&priv->lock);
922 * gst_rtsp_media_get_protocols:
923 * @media: a #GstRTSPMedia
925 * Get the allowed protocols of @media.
927 * Returns: a #GstRTSPLowerTrans
930 gst_rtsp_media_get_protocols (GstRTSPMedia * media)
932 GstRTSPMediaPrivate *priv;
933 GstRTSPLowerTrans res;
935 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media),
936 GST_RTSP_LOWER_TRANS_UNKNOWN);
940 g_mutex_lock (&priv->lock);
941 res = priv->protocols;
942 g_mutex_unlock (&priv->lock);
948 * gst_rtsp_media_set_eos_shutdown:
949 * @media: a #GstRTSPMedia
950 * @eos_shutdown: the new value
952 * Set or unset if an EOS event will be sent to the pipeline for @media before
956 gst_rtsp_media_set_eos_shutdown (GstRTSPMedia * media, gboolean eos_shutdown)
958 GstRTSPMediaPrivate *priv;
960 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
964 g_mutex_lock (&priv->lock);
965 priv->eos_shutdown = eos_shutdown;
966 g_mutex_unlock (&priv->lock);
970 * gst_rtsp_media_is_eos_shutdown:
971 * @media: a #GstRTSPMedia
973 * Check if the pipeline for @media will send an EOS down the pipeline before
976 * Returns: %TRUE if the media will send EOS before unpreparing.
979 gst_rtsp_media_is_eos_shutdown (GstRTSPMedia * media)
981 GstRTSPMediaPrivate *priv;
984 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
988 g_mutex_lock (&priv->lock);
989 res = priv->eos_shutdown;
990 g_mutex_unlock (&priv->lock);
996 * gst_rtsp_media_set_buffer_size:
997 * @media: a #GstRTSPMedia
998 * @size: the new value
1000 * Set the kernel UDP buffer size.
1003 gst_rtsp_media_set_buffer_size (GstRTSPMedia * media, guint size)
1005 GstRTSPMediaPrivate *priv;
1007 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1009 GST_LOG_OBJECT (media, "set buffer size %u", size);
1013 g_mutex_lock (&priv->lock);
1014 priv->buffer_size = size;
1015 g_mutex_unlock (&priv->lock);
1019 * gst_rtsp_media_get_buffer_size:
1020 * @media: a #GstRTSPMedia
1022 * Get the kernel UDP buffer size.
1024 * Returns: the kernel UDP buffer size.
1027 gst_rtsp_media_get_buffer_size (GstRTSPMedia * media)
1029 GstRTSPMediaPrivate *priv;
1032 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1036 g_mutex_unlock (&priv->lock);
1037 res = priv->buffer_size;
1038 g_mutex_unlock (&priv->lock);
1044 * gst_rtsp_media_use_time_provider:
1045 * @media: a #GstRTSPMedia
1046 * @time_provider: if a #GstNetTimeProvider should be used
1048 * Set @media to provide a #GstNetTimeProvider.
1051 gst_rtsp_media_use_time_provider (GstRTSPMedia * media, gboolean time_provider)
1053 GstRTSPMediaPrivate *priv;
1055 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1059 g_mutex_lock (&priv->lock);
1060 priv->time_provider = time_provider;
1061 g_mutex_unlock (&priv->lock);
1065 * gst_rtsp_media_is_time_provider:
1066 * @media: a #GstRTSPMedia
1068 * Check if @media can provide a #GstNetTimeProvider for its pipeline clock.
1070 * Use gst_rtsp_media_get_time_provider() to get the network clock.
1072 * Returns: %TRUE if @media can provide a #GstNetTimeProvider.
1075 gst_rtsp_media_is_time_provider (GstRTSPMedia * media)
1077 GstRTSPMediaPrivate *priv;
1080 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1084 g_mutex_unlock (&priv->lock);
1085 res = priv->time_provider;
1086 g_mutex_unlock (&priv->lock);
1092 * gst_rtsp_media_set_address_pool:
1093 * @media: a #GstRTSPMedia
1094 * @pool: a #GstRTSPAddressPool
1096 * configure @pool to be used as the address pool of @media.
1099 gst_rtsp_media_set_address_pool (GstRTSPMedia * media,
1100 GstRTSPAddressPool * pool)
1102 GstRTSPMediaPrivate *priv;
1103 GstRTSPAddressPool *old;
1105 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1109 GST_LOG_OBJECT (media, "set address pool %p", pool);
1111 g_mutex_lock (&priv->lock);
1112 if ((old = priv->pool) != pool)
1113 priv->pool = pool ? g_object_ref (pool) : NULL;
1116 g_ptr_array_foreach (priv->streams, (GFunc) gst_rtsp_stream_set_address_pool,
1118 g_mutex_unlock (&priv->lock);
1121 g_object_unref (old);
1125 * gst_rtsp_media_get_address_pool:
1126 * @media: a #GstRTSPMedia
1128 * Get the #GstRTSPAddressPool used as the address pool of @media.
1130 * Returns: (transfer full): the #GstRTSPAddressPool of @media. g_object_unref() after
1133 GstRTSPAddressPool *
1134 gst_rtsp_media_get_address_pool (GstRTSPMedia * media)
1136 GstRTSPMediaPrivate *priv;
1137 GstRTSPAddressPool *result;
1139 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1143 g_mutex_lock (&priv->lock);
1144 if ((result = priv->pool))
1145 g_object_ref (result);
1146 g_mutex_unlock (&priv->lock);
1152 * gst_rtsp_media_collect_streams:
1153 * @media: a #GstRTSPMedia
1155 * Find all payloader elements, they should be named pay\%d in the
1156 * element of @media, and create #GstRTSPStreams for them.
1158 * Collect all dynamic elements, named dynpay\%d, and add them to
1159 * the list of dynamic elements.
1162 gst_rtsp_media_collect_streams (GstRTSPMedia * media)
1164 GstRTSPMediaPrivate *priv;
1165 GstElement *element, *elem;
1170 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1173 element = priv->element;
1176 for (i = 0; have_elem; i++) {
1181 name = g_strdup_printf ("pay%d", i);
1182 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
1183 GST_INFO ("found stream %d with payloader %p", i, elem);
1185 /* take the pad of the payloader */
1186 pad = gst_element_get_static_pad (elem, "src");
1187 /* create the stream */
1188 gst_rtsp_media_create_stream (media, elem, pad);
1189 gst_object_unref (pad);
1190 gst_object_unref (elem);
1196 name = g_strdup_printf ("dynpay%d", i);
1197 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
1198 /* a stream that will dynamically create pads to provide RTP packets */
1200 GST_INFO ("found dynamic element %d, %p", i, elem);
1202 g_mutex_lock (&priv->lock);
1203 priv->dynamic = g_list_prepend (priv->dynamic, elem);
1204 g_mutex_unlock (&priv->lock);
1213 * gst_rtsp_media_create_stream:
1214 * @media: a #GstRTSPMedia
1215 * @payloader: a #GstElement
1216 * @srcpad: a source #GstPad
1218 * Create a new stream in @media that provides RTP data on @srcpad.
1219 * @srcpad should be a pad of an element inside @media->element.
1221 * Returns: (transfer none): a new #GstRTSPStream that remains valid for as long
1225 gst_rtsp_media_create_stream (GstRTSPMedia * media, GstElement * payloader,
1228 GstRTSPMediaPrivate *priv;
1229 GstRTSPStream *stream;
1234 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1235 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
1236 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1237 g_return_val_if_fail (GST_PAD_IS_SRC (pad), NULL);
1241 g_mutex_lock (&priv->lock);
1242 idx = priv->streams->len;
1244 GST_DEBUG ("media %p: creating stream with index %d", media, idx);
1246 name = g_strdup_printf ("src_%u", idx);
1247 srcpad = gst_ghost_pad_new (name, pad);
1248 gst_pad_set_active (srcpad, TRUE);
1249 gst_element_add_pad (priv->element, srcpad);
1252 stream = gst_rtsp_stream_new (idx, payloader, srcpad);
1254 gst_rtsp_stream_set_address_pool (stream, priv->pool);
1255 gst_rtsp_stream_set_protocols (stream, priv->protocols);
1257 g_ptr_array_add (priv->streams, stream);
1258 g_mutex_unlock (&priv->lock);
1260 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STREAM], 0, stream,
1267 gst_rtsp_media_remove_stream (GstRTSPMedia * media, GstRTSPStream * stream)
1269 GstRTSPMediaPrivate *priv;
1274 g_mutex_lock (&priv->lock);
1275 /* remove the ghostpad */
1276 srcpad = gst_rtsp_stream_get_srcpad (stream);
1277 gst_element_remove_pad (priv->element, srcpad);
1278 gst_object_unref (srcpad);
1279 /* now remove the stream */
1280 g_object_ref (stream);
1281 g_ptr_array_remove (priv->streams, stream);
1282 g_mutex_unlock (&priv->lock);
1284 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_REMOVED_STREAM], 0,
1287 g_object_unref (stream);
1291 * gst_rtsp_media_n_streams:
1292 * @media: a #GstRTSPMedia
1294 * Get the number of streams in this media.
1296 * Returns: The number of streams.
1299 gst_rtsp_media_n_streams (GstRTSPMedia * media)
1301 GstRTSPMediaPrivate *priv;
1304 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), 0);
1308 g_mutex_lock (&priv->lock);
1309 res = priv->streams->len;
1310 g_mutex_unlock (&priv->lock);
1316 * gst_rtsp_media_get_stream:
1317 * @media: a #GstRTSPMedia
1318 * @idx: the stream index
1320 * Retrieve the stream with index @idx from @media.
1322 * Returns: (transfer none): the #GstRTSPStream at index @idx or %NULL when a stream with
1323 * that index did not exist.
1326 gst_rtsp_media_get_stream (GstRTSPMedia * media, guint idx)
1328 GstRTSPMediaPrivate *priv;
1331 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1335 g_mutex_lock (&priv->lock);
1336 if (idx < priv->streams->len)
1337 res = g_ptr_array_index (priv->streams, idx);
1340 g_mutex_unlock (&priv->lock);
1346 * gst_rtsp_media_find_stream:
1347 * @media: a #GstRTSPMedia
1348 * @control: the control of the stream
1350 * Find a stream in @media with @control as the control uri.
1352 * Returns: (transfer none): the #GstRTSPStream with control uri @control
1353 * or %NULL when a stream with that control did not exist.
1356 gst_rtsp_media_find_stream (GstRTSPMedia * media, const gchar * control)
1358 GstRTSPMediaPrivate *priv;
1362 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1363 g_return_val_if_fail (control != NULL, NULL);
1369 g_mutex_lock (&priv->lock);
1370 for (i = 0; i < priv->streams->len; i++) {
1371 GstRTSPStream *test;
1373 test = g_ptr_array_index (priv->streams, i);
1374 if (gst_rtsp_stream_has_control (test, control)) {
1379 g_mutex_unlock (&priv->lock);
1384 /* called with state-lock */
1386 default_convert_range (GstRTSPMedia * media, GstRTSPTimeRange * range,
1387 GstRTSPRangeUnit unit)
1389 return gst_rtsp_range_convert_units (range, unit);
1393 * gst_rtsp_media_get_range_string:
1394 * @media: a #GstRTSPMedia
1395 * @play: for the PLAY request
1396 * @unit: the unit to use for the string
1398 * Get the current range as a string. @media must be prepared with
1399 * gst_rtsp_media_prepare ().
1401 * Returns: The range as a string, g_free() after usage.
1404 gst_rtsp_media_get_range_string (GstRTSPMedia * media, gboolean play,
1405 GstRTSPRangeUnit unit)
1407 GstRTSPMediaClass *klass;
1408 GstRTSPMediaPrivate *priv;
1410 GstRTSPTimeRange range;
1412 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1413 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1414 g_return_val_if_fail (klass->convert_range != NULL, FALSE);
1418 g_rec_mutex_lock (&priv->state_lock);
1419 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
1420 priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
1423 g_mutex_lock (&priv->lock);
1425 /* Update the range value with current position/duration */
1426 collect_media_stats (media);
1429 range = priv->range;
1431 if (!play && priv->n_active > 0) {
1432 range.min.type = GST_RTSP_TIME_NOW;
1433 range.min.seconds = -1;
1435 g_mutex_unlock (&priv->lock);
1436 g_rec_mutex_unlock (&priv->state_lock);
1438 if (!klass->convert_range (media, &range, unit))
1439 goto conversion_failed;
1441 result = gst_rtsp_range_to_string (&range);
1448 GST_WARNING ("media %p was not prepared", media);
1449 g_rec_mutex_unlock (&priv->state_lock);
1454 GST_WARNING ("range conversion to unit %d failed", unit);
1460 stream_update_blocked (GstRTSPStream * stream, GstRTSPMedia * media)
1462 gst_rtsp_stream_set_blocked (stream, media->priv->blocked);
1466 media_streams_set_blocked (GstRTSPMedia * media, gboolean blocked)
1468 GstRTSPMediaPrivate *priv = media->priv;
1470 GST_DEBUG ("media %p set blocked %d", media, blocked);
1471 priv->blocked = blocked;
1472 g_ptr_array_foreach (priv->streams, (GFunc) stream_update_blocked, media);
1476 * gst_rtsp_media_seek:
1477 * @media: a #GstRTSPMedia
1478 * @range: a #GstRTSPTimeRange
1480 * Seek the pipeline of @media to @range. @media must be prepared with
1481 * gst_rtsp_media_prepare().
1483 * Returns: %TRUE on success.
1486 gst_rtsp_media_seek (GstRTSPMedia * media, GstRTSPTimeRange * range)
1488 GstRTSPMediaClass *klass;
1489 GstRTSPMediaPrivate *priv;
1492 GstClockTime start, stop;
1493 GstSeekType start_type, stop_type;
1496 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1498 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1499 g_return_val_if_fail (range != NULL, FALSE);
1500 g_return_val_if_fail (klass->convert_range != NULL, FALSE);
1504 g_rec_mutex_lock (&priv->state_lock);
1505 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
1508 /* Update the seekable state of the pipeline in case it changed */
1509 query = gst_query_new_seeking (GST_FORMAT_TIME);
1510 if (gst_element_query (priv->pipeline, query)) {
1515 gst_query_parse_seeking (query, &format, &seekable, &start, &end);
1516 priv->seekable = seekable;
1518 gst_query_unref (query);
1520 if (!priv->seekable)
1523 /* depends on the current playing state of the pipeline. We might need to
1524 * queue this until we get EOS. */
1525 flags = GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT;
1527 start_type = stop_type = GST_SEEK_TYPE_NONE;
1529 if (!klass->convert_range (media, range, GST_RTSP_RANGE_NPT))
1531 gst_rtsp_range_get_times (range, &start, &stop);
1533 GST_INFO ("got %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1534 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1535 GST_INFO ("current %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1536 GST_TIME_ARGS (priv->range_start), GST_TIME_ARGS (priv->range_stop));
1538 if (priv->range_start == start)
1539 start = GST_CLOCK_TIME_NONE;
1540 else if (start != GST_CLOCK_TIME_NONE)
1541 start_type = GST_SEEK_TYPE_SET;
1543 if (priv->range_stop == stop)
1544 stop = GST_CLOCK_TIME_NONE;
1545 else if (stop != GST_CLOCK_TIME_NONE)
1546 stop_type = GST_SEEK_TYPE_SET;
1548 if (start != GST_CLOCK_TIME_NONE || stop != GST_CLOCK_TIME_NONE) {
1549 GST_INFO ("seeking to %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1550 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1552 priv->status = GST_RTSP_MEDIA_STATUS_PREPARING;
1554 media_streams_set_blocked (media, TRUE);
1556 res = gst_element_seek (priv->pipeline, 1.0, GST_FORMAT_TIME,
1557 flags, start_type, start, stop_type, stop);
1559 /* and block for the seek to complete */
1560 GST_INFO ("done seeking %d", res);
1561 g_rec_mutex_unlock (&priv->state_lock);
1563 /* wait until pipeline is prerolled again, this will also collect stats */
1564 if (!wait_preroll (media))
1565 goto preroll_failed;
1567 g_rec_mutex_lock (&priv->state_lock);
1568 GST_INFO ("prerolled again");
1570 GST_INFO ("no seek needed");
1573 g_rec_mutex_unlock (&priv->state_lock);
1580 g_rec_mutex_unlock (&priv->state_lock);
1581 GST_INFO ("media %p is not prepared", media);
1586 g_rec_mutex_unlock (&priv->state_lock);
1587 GST_INFO ("pipeline is not seekable");
1592 g_rec_mutex_unlock (&priv->state_lock);
1593 GST_WARNING ("conversion to npt not supported");
1598 GST_WARNING ("failed to preroll after seek");
1604 gst_rtsp_media_set_status (GstRTSPMedia * media, GstRTSPMediaStatus status)
1606 GstRTSPMediaPrivate *priv = media->priv;
1608 g_mutex_lock (&priv->lock);
1609 priv->status = status;
1610 GST_DEBUG ("setting new status to %d", status);
1611 g_cond_broadcast (&priv->cond);
1612 g_mutex_unlock (&priv->lock);
1616 * gst_rtsp_media_get_status:
1617 * @media: a #GstRTSPMedia
1619 * Get the status of @media. When @media is busy preparing, this function waits
1620 * until @media is prepared or in error.
1622 * Returns: the status of @media.
1625 gst_rtsp_media_get_status (GstRTSPMedia * media)
1627 GstRTSPMediaPrivate *priv = media->priv;
1628 GstRTSPMediaStatus result;
1631 g_mutex_lock (&priv->lock);
1632 end_time = g_get_monotonic_time () + 20 * G_TIME_SPAN_SECOND;
1633 /* while we are preparing, wait */
1634 while (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING) {
1635 GST_DEBUG ("waiting for status change");
1636 if (!g_cond_wait_until (&priv->cond, &priv->lock, end_time)) {
1637 GST_DEBUG ("timeout, assuming error status");
1638 priv->status = GST_RTSP_MEDIA_STATUS_ERROR;
1641 /* could be success or error */
1642 result = priv->status;
1643 GST_DEBUG ("got status %d", result);
1644 g_mutex_unlock (&priv->lock);
1650 stream_collect_blocking (GstRTSPStream * stream, gboolean * blocked)
1652 *blocked &= gst_rtsp_stream_is_blocking (stream);
1656 media_streams_blocking (GstRTSPMedia * media)
1658 gboolean blocking = TRUE;
1660 g_ptr_array_foreach (media->priv->streams, (GFunc) stream_collect_blocking,
1666 /* called with state-lock */
1668 default_handle_message (GstRTSPMedia * media, GstMessage * message)
1670 GstRTSPMediaPrivate *priv = media->priv;
1671 GstMessageType type;
1673 type = GST_MESSAGE_TYPE (message);
1676 case GST_MESSAGE_STATE_CHANGED:
1678 case GST_MESSAGE_BUFFERING:
1682 gst_message_parse_buffering (message, &percent);
1684 /* no state management needed for live pipelines */
1688 if (percent == 100) {
1689 /* a 100% message means buffering is done */
1690 priv->buffering = FALSE;
1691 /* if the desired state is playing, go back */
1692 if (priv->target_state == GST_STATE_PLAYING) {
1693 GST_INFO ("Buffering done, setting pipeline to PLAYING");
1694 gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
1696 GST_INFO ("Buffering done");
1699 /* buffering busy */
1700 if (priv->buffering == FALSE) {
1701 if (priv->target_state == GST_STATE_PLAYING) {
1702 /* we were not buffering but PLAYING, PAUSE the pipeline. */
1703 GST_INFO ("Buffering, setting pipeline to PAUSED ...");
1704 gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
1706 GST_INFO ("Buffering ...");
1709 priv->buffering = TRUE;
1713 case GST_MESSAGE_LATENCY:
1715 gst_bin_recalculate_latency (GST_BIN_CAST (priv->pipeline));
1718 case GST_MESSAGE_ERROR:
1723 gst_message_parse_error (message, &gerror, &debug);
1724 GST_WARNING ("%p: got error %s (%s)", media, gerror->message, debug);
1725 g_error_free (gerror);
1728 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
1731 case GST_MESSAGE_WARNING:
1736 gst_message_parse_warning (message, &gerror, &debug);
1737 GST_WARNING ("%p: got warning %s (%s)", media, gerror->message, debug);
1738 g_error_free (gerror);
1742 case GST_MESSAGE_ELEMENT:
1744 const GstStructure *s;
1746 s = gst_message_get_structure (message);
1747 if (gst_structure_has_name (s, "GstRTSPStreamBlocking")) {
1748 GST_DEBUG ("media received blocking message");
1749 if (priv->blocked && media_streams_blocking (media)) {
1750 GST_DEBUG ("media is blocking");
1751 collect_media_stats (media);
1753 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
1754 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
1759 case GST_MESSAGE_STREAM_STATUS:
1761 case GST_MESSAGE_ASYNC_DONE:
1763 /* when we are dynamically adding pads, the addition of the udpsrc will
1764 * temporarily produce ASYNC_DONE messages. We have to ignore them and
1765 * wait for the final ASYNC_DONE after everything prerolled */
1766 GST_INFO ("%p: ignoring ASYNC_DONE", media);
1768 GST_INFO ("%p: got ASYNC_DONE", media);
1769 collect_media_stats (media);
1771 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
1772 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
1775 case GST_MESSAGE_EOS:
1776 GST_INFO ("%p: got EOS", media);
1778 if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARING) {
1779 GST_DEBUG ("shutting down after EOS");
1780 finish_unprepare (media);
1784 GST_INFO ("%p: got message type %d (%s)", media, type,
1785 gst_message_type_get_name (type));
1792 bus_message (GstBus * bus, GstMessage * message, GstRTSPMedia * media)
1794 GstRTSPMediaPrivate *priv = media->priv;
1795 GstRTSPMediaClass *klass;
1798 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1800 g_rec_mutex_lock (&priv->state_lock);
1801 if (klass->handle_message)
1802 ret = klass->handle_message (media, message);
1805 g_rec_mutex_unlock (&priv->state_lock);
1811 watch_destroyed (GstRTSPMedia * media)
1813 GST_DEBUG_OBJECT (media, "source destroyed");
1814 g_object_unref (media);
1818 find_payload_element (GstElement * payloader)
1820 GstElement *pay = NULL;
1822 if (GST_IS_BIN (payloader)) {
1824 GValue item = { 0 };
1826 iter = gst_bin_iterate_recurse (GST_BIN (payloader));
1827 while (gst_iterator_next (iter, &item) == GST_ITERATOR_OK) {
1828 GstElement *element = (GstElement *) g_value_get_object (&item);
1829 GstElementClass *eclass = GST_ELEMENT_GET_CLASS (element);
1833 gst_element_class_get_metadata (eclass, GST_ELEMENT_METADATA_KLASS);
1837 if (strstr (klass, "Payloader") && strstr (klass, "RTP")) {
1838 pay = gst_object_ref (element);
1839 g_value_unset (&item);
1842 g_value_unset (&item);
1844 gst_iterator_free (iter);
1846 pay = g_object_ref (payloader);
1852 /* called from streaming threads */
1854 pad_added_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
1856 GstRTSPMediaPrivate *priv = media->priv;
1857 GstRTSPStream *stream;
1860 /* find the real payload element */
1861 pay = find_payload_element (element);
1862 stream = gst_rtsp_media_create_stream (media, pay, pad);
1863 gst_object_unref (pay);
1865 g_object_set_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream", stream);
1867 GST_INFO ("pad added %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
1869 g_rec_mutex_lock (&priv->state_lock);
1870 /* we will be adding elements below that will cause ASYNC_DONE to be
1871 * posted in the bus. We want to ignore those messages until the
1872 * pipeline really prerolled. */
1873 priv->adding = TRUE;
1875 /* join the element in the PAUSED state because this callback is
1876 * called from the streaming thread and it is PAUSED */
1877 gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
1878 priv->rtpbin, GST_STATE_PAUSED);
1880 priv->adding = FALSE;
1881 g_rec_mutex_unlock (&priv->state_lock);
1885 pad_removed_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
1887 GstRTSPMediaPrivate *priv = media->priv;
1888 GstRTSPStream *stream;
1890 stream = g_object_get_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream");
1894 GST_INFO ("pad removed %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
1896 g_rec_mutex_lock (&priv->state_lock);
1897 gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
1898 g_rec_mutex_unlock (&priv->state_lock);
1900 gst_rtsp_media_remove_stream (media, stream);
1904 remove_fakesink (GstRTSPMediaPrivate * priv)
1906 GstElement *fakesink;
1908 g_mutex_lock (&priv->lock);
1909 if ((fakesink = priv->fakesink))
1910 gst_object_ref (fakesink);
1911 priv->fakesink = NULL;
1912 g_mutex_unlock (&priv->lock);
1915 gst_bin_remove (GST_BIN (priv->pipeline), fakesink);
1916 gst_element_set_state (fakesink, GST_STATE_NULL);
1917 gst_object_unref (fakesink);
1918 GST_INFO ("removed fakesink");
1923 no_more_pads_cb (GstElement * element, GstRTSPMedia * media)
1925 GstRTSPMediaPrivate *priv = media->priv;
1927 GST_INFO ("no more pads");
1928 remove_fakesink (priv);
1931 typedef struct _DynPaySignalHandlers DynPaySignalHandlers;
1933 struct _DynPaySignalHandlers
1935 gulong pad_added_handler;
1936 gulong pad_removed_handler;
1937 gulong no_more_pads_handler;
1941 start_preroll (GstRTSPMedia * media)
1943 GstRTSPMediaPrivate *priv = media->priv;
1944 GstStateChangeReturn ret;
1946 GST_INFO ("setting pipeline to PAUSED for media %p", media);
1947 /* first go to PAUSED */
1948 priv->target_state = GST_STATE_PAUSED;
1949 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_TARGET_STATE], 0,
1950 priv->target_state, NULL);
1951 ret = gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
1954 case GST_STATE_CHANGE_SUCCESS:
1955 GST_INFO ("SUCCESS state change for media %p", media);
1956 priv->seekable = TRUE;
1958 case GST_STATE_CHANGE_ASYNC:
1959 GST_INFO ("ASYNC state change for media %p", media);
1960 priv->seekable = TRUE;
1962 case GST_STATE_CHANGE_NO_PREROLL:
1963 /* we need to go to PLAYING */
1964 GST_INFO ("NO_PREROLL state change: live media %p", media);
1965 /* FIXME we disable seeking for live streams for now. We should perform a
1966 * seeking query in preroll instead */
1967 priv->seekable = FALSE;
1968 priv->is_live = TRUE;
1969 /* start blocked to make sure nothing goes to the sink */
1970 media_streams_set_blocked (media, TRUE);
1971 ret = gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
1972 if (ret == GST_STATE_CHANGE_FAILURE)
1975 case GST_STATE_CHANGE_FAILURE:
1983 GST_WARNING ("failed to preroll pipeline");
1989 wait_preroll (GstRTSPMedia * media)
1991 GstRTSPMediaStatus status;
1993 GST_DEBUG ("wait to preroll pipeline");
1995 /* wait until pipeline is prerolled */
1996 status = gst_rtsp_media_get_status (media);
1997 if (status == GST_RTSP_MEDIA_STATUS_ERROR)
1998 goto preroll_failed;
2004 GST_WARNING ("failed to preroll pipeline");
2010 start_prepare (GstRTSPMedia * media)
2012 GstRTSPMediaPrivate *priv = media->priv;
2016 /* link streams we already have, other streams might appear when we have
2017 * dynamic elements */
2018 for (i = 0; i < priv->streams->len; i++) {
2019 GstRTSPStream *stream;
2021 stream = g_ptr_array_index (priv->streams, i);
2023 gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
2024 priv->rtpbin, GST_STATE_NULL);
2027 for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
2028 GstElement *elem = walk->data;
2029 DynPaySignalHandlers *handlers = g_slice_new (DynPaySignalHandlers);
2031 GST_INFO ("adding callbacks for dynamic element %p", elem);
2033 handlers->pad_added_handler = g_signal_connect (elem, "pad-added",
2034 (GCallback) pad_added_cb, media);
2035 handlers->pad_removed_handler = g_signal_connect (elem, "pad-removed",
2036 (GCallback) pad_removed_cb, media);
2037 handlers->no_more_pads_handler = g_signal_connect (elem, "no-more-pads",
2038 (GCallback) no_more_pads_cb, media);
2040 g_object_set_data (G_OBJECT (elem), "gst-rtsp-dynpay-handlers", handlers);
2042 /* we add a fakesink here in order to make the state change async. We remove
2043 * the fakesink again in the no-more-pads callback. */
2044 priv->fakesink = gst_element_factory_make ("fakesink", "fakesink");
2045 gst_bin_add (GST_BIN (priv->pipeline), priv->fakesink);
2048 if (!start_preroll (media))
2049 goto preroll_failed;
2055 GST_WARNING ("failed to preroll pipeline");
2056 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2062 * gst_rtsp_media_prepare:
2063 * @media: a #GstRTSPMedia
2064 * @thread: a #GstRTSPThread to run the bus handler or %NULL
2066 * Prepare @media for streaming. This function will create the objects
2067 * to manage the streaming. A pipeline must have been set on @media with
2068 * gst_rtsp_media_take_pipeline().
2070 * It will preroll the pipeline and collect vital information about the streams
2071 * such as the duration.
2073 * Returns: %TRUE on success.
2076 gst_rtsp_media_prepare (GstRTSPMedia * media, GstRTSPThread * thread)
2078 GstRTSPMediaPrivate *priv;
2081 GstRTSPMediaClass *klass;
2083 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2084 g_return_val_if_fail (GST_IS_RTSP_THREAD (thread), FALSE);
2088 g_rec_mutex_lock (&priv->state_lock);
2089 priv->prepare_count++;
2091 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED ||
2092 priv->status == GST_RTSP_MEDIA_STATUS_SUSPENDED)
2095 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
2098 if (priv->status != GST_RTSP_MEDIA_STATUS_UNPREPARED)
2099 goto not_unprepared;
2101 if (!priv->reusable && priv->reused)
2104 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2106 if (!klass->create_rtpbin)
2107 goto no_create_rtpbin;
2109 priv->rtpbin = klass->create_rtpbin (media);
2110 if (priv->rtpbin != NULL) {
2111 gboolean success = TRUE;
2113 if (klass->setup_rtpbin)
2114 success = klass->setup_rtpbin (media, priv->rtpbin);
2116 if (success == FALSE) {
2117 gst_object_unref (priv->rtpbin);
2118 priv->rtpbin = NULL;
2121 if (priv->rtpbin == NULL)
2124 GST_INFO ("preparing media %p", media);
2126 /* reset some variables */
2127 priv->is_live = FALSE;
2128 priv->seekable = FALSE;
2129 priv->buffering = FALSE;
2130 priv->thread = thread;
2131 /* we're preparing now */
2132 priv->status = GST_RTSP_MEDIA_STATUS_PREPARING;
2134 bus = gst_pipeline_get_bus (GST_PIPELINE_CAST (priv->pipeline));
2136 /* add the pipeline bus to our custom mainloop */
2137 priv->source = gst_bus_create_watch (bus);
2138 gst_object_unref (bus);
2140 g_source_set_callback (priv->source, (GSourceFunc) bus_message,
2141 g_object_ref (media), (GDestroyNotify) watch_destroyed);
2143 priv->id = g_source_attach (priv->source, thread->context);
2145 /* add stuff to the bin */
2146 gst_bin_add (GST_BIN (priv->pipeline), priv->rtpbin);
2148 /* do remainder in context */
2149 source = g_idle_source_new ();
2150 g_source_set_callback (source, (GSourceFunc) start_prepare, media, NULL);
2151 g_source_attach (source, thread->context);
2152 g_source_unref (source);
2155 g_rec_mutex_unlock (&priv->state_lock);
2157 /* now wait for all pads to be prerolled, FIXME, we should somehow be
2158 * able to do this async so that we don't block the server thread. */
2159 if (!wait_preroll (media))
2160 goto preroll_failed;
2162 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_PREPARED], 0, NULL);
2164 GST_INFO ("object %p is prerolled", media);
2171 GST_LOG ("media %p was prepared", media);
2172 g_rec_mutex_unlock (&priv->state_lock);
2178 GST_WARNING ("media %p was not unprepared", media);
2179 priv->prepare_count--;
2180 g_rec_mutex_unlock (&priv->state_lock);
2185 priv->prepare_count--;
2186 g_rec_mutex_unlock (&priv->state_lock);
2187 GST_WARNING ("can not reuse media %p", media);
2192 priv->prepare_count--;
2193 g_rec_mutex_unlock (&priv->state_lock);
2194 GST_ERROR ("no create_rtpbin function");
2195 g_critical ("no create_rtpbin vmethod function set");
2200 priv->prepare_count--;
2201 g_rec_mutex_unlock (&priv->state_lock);
2202 GST_WARNING ("no rtpbin element");
2203 g_warning ("failed to create element 'rtpbin', check your installation");
2208 GST_WARNING ("failed to preroll pipeline");
2209 gst_rtsp_media_unprepare (media);
2214 /* must be called with state-lock */
2216 finish_unprepare (GstRTSPMedia * media)
2218 GstRTSPMediaPrivate *priv = media->priv;
2222 GST_DEBUG ("shutting down");
2224 gst_element_set_state (priv->pipeline, GST_STATE_NULL);
2225 remove_fakesink (priv);
2227 for (i = 0; i < priv->streams->len; i++) {
2228 GstRTSPStream *stream;
2230 GST_INFO ("Removing elements of stream %d from pipeline", i);
2232 stream = g_ptr_array_index (priv->streams, i);
2234 gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
2237 /* remove the pad signal handlers */
2238 for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
2239 GstElement *elem = walk->data;
2240 DynPaySignalHandlers *handlers;
2243 g_object_steal_data (G_OBJECT (elem), "gst-rtsp-dynpay-handlers");
2244 g_assert (handlers != NULL);
2246 g_signal_handler_disconnect (G_OBJECT (elem), handlers->pad_added_handler);
2247 g_signal_handler_disconnect (G_OBJECT (elem),
2248 handlers->pad_removed_handler);
2249 g_signal_handler_disconnect (G_OBJECT (elem),
2250 handlers->no_more_pads_handler);
2252 g_slice_free (DynPaySignalHandlers, handlers);
2255 gst_bin_remove (GST_BIN (priv->pipeline), priv->rtpbin);
2256 priv->rtpbin = NULL;
2259 gst_object_unref (priv->nettime);
2260 priv->nettime = NULL;
2262 priv->reused = TRUE;
2263 priv->status = GST_RTSP_MEDIA_STATUS_UNPREPARED;
2265 /* when the media is not reusable, this will effectively unref the media and
2267 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_UNPREPARED], 0, NULL);
2269 /* the source has the last ref to the media */
2271 GST_DEBUG ("destroy source");
2272 g_source_destroy (priv->source);
2273 g_source_unref (priv->source);
2276 GST_DEBUG ("stop thread");
2277 gst_rtsp_thread_stop (priv->thread);
2281 /* called with state-lock */
2283 default_unprepare (GstRTSPMedia * media)
2285 GstRTSPMediaPrivate *priv = media->priv;
2287 if (priv->eos_shutdown) {
2288 GST_DEBUG ("sending EOS for shutdown");
2289 /* ref so that we don't disappear */
2290 gst_element_send_event (priv->pipeline, gst_event_new_eos ());
2291 /* we need to go to playing again for the EOS to propagate, normally in this
2292 * state, nothing is receiving data from us anymore so this is ok. */
2293 gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
2294 priv->status = GST_RTSP_MEDIA_STATUS_UNPREPARING;
2296 finish_unprepare (media);
2302 * gst_rtsp_media_unprepare:
2303 * @media: a #GstRTSPMedia
2305 * Unprepare @media. After this call, the media should be prepared again before
2306 * it can be used again. If the media is set to be non-reusable, a new instance
2309 * Returns: %TRUE on success.
2312 gst_rtsp_media_unprepare (GstRTSPMedia * media)
2314 GstRTSPMediaPrivate *priv;
2317 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2321 g_rec_mutex_lock (&priv->state_lock);
2322 if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARED)
2323 goto was_unprepared;
2325 priv->prepare_count--;
2326 if (priv->prepare_count > 0)
2329 GST_INFO ("unprepare media %p", media);
2330 priv->target_state = GST_STATE_NULL;
2331 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_TARGET_STATE], 0,
2332 priv->target_state, NULL);
2335 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED) {
2336 GstRTSPMediaClass *klass;
2338 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2339 if (klass->unprepare)
2340 success = klass->unprepare (media);
2342 finish_unprepare (media);
2344 g_rec_mutex_unlock (&priv->state_lock);
2350 g_rec_mutex_unlock (&priv->state_lock);
2351 GST_INFO ("media %p was already unprepared", media);
2356 GST_INFO ("media %p still prepared %d times", media, priv->prepare_count);
2357 g_rec_mutex_unlock (&priv->state_lock);
2362 /* should be called with state-lock */
2364 get_clock_unlocked (GstRTSPMedia * media)
2366 if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED) {
2367 GST_DEBUG_OBJECT (media, "media was not prepared");
2370 return gst_pipeline_get_clock (GST_PIPELINE_CAST (media->priv->pipeline));
2374 * gst_rtsp_media_get_clock:
2375 * @media: a #GstRTSPMedia
2377 * Get the clock that is used by the pipeline in @media.
2379 * @media must be prepared before this method returns a valid clock object.
2381 * Returns: (transfer full): the #GstClock used by @media. unref after usage.
2384 gst_rtsp_media_get_clock (GstRTSPMedia * media)
2387 GstRTSPMediaPrivate *priv;
2389 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
2393 g_rec_mutex_lock (&priv->state_lock);
2394 clock = get_clock_unlocked (media);
2395 g_rec_mutex_unlock (&priv->state_lock);
2401 * gst_rtsp_media_get_base_time:
2402 * @media: a #GstRTSPMedia
2404 * Get the base_time that is used by the pipeline in @media.
2406 * @media must be prepared before this method returns a valid base_time.
2408 * Returns: the base_time used by @media.
2411 gst_rtsp_media_get_base_time (GstRTSPMedia * media)
2413 GstClockTime result;
2414 GstRTSPMediaPrivate *priv;
2416 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_CLOCK_TIME_NONE);
2420 g_rec_mutex_lock (&priv->state_lock);
2421 if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
2424 result = gst_element_get_base_time (media->priv->pipeline);
2425 g_rec_mutex_unlock (&priv->state_lock);
2432 g_rec_mutex_unlock (&priv->state_lock);
2433 GST_DEBUG_OBJECT (media, "media was not prepared");
2434 return GST_CLOCK_TIME_NONE;
2439 * gst_rtsp_media_get_time_provider:
2440 * @media: a #GstRTSPMedia
2441 * @address: an address or %NULL
2442 * @port: a port or 0
2444 * Get the #GstNetTimeProvider for the clock used by @media. The time provider
2445 * will listen on @address and @port for client time requests.
2447 * Returns: (transfer full): the #GstNetTimeProvider of @media.
2449 GstNetTimeProvider *
2450 gst_rtsp_media_get_time_provider (GstRTSPMedia * media, const gchar * address,
2453 GstRTSPMediaPrivate *priv;
2454 GstNetTimeProvider *provider = NULL;
2456 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
2460 g_rec_mutex_lock (&priv->state_lock);
2461 if (priv->time_provider) {
2462 if ((provider = priv->nettime) == NULL) {
2465 if (priv->time_provider && (clock = get_clock_unlocked (media))) {
2466 provider = gst_net_time_provider_new (clock, address, port);
2467 gst_object_unref (clock);
2469 priv->nettime = provider;
2473 g_rec_mutex_unlock (&priv->state_lock);
2476 gst_object_ref (provider);
2482 default_setup_sdp (GstRTSPMedia * media, GstSDPMessage * sdp, GstSDPInfo * info)
2484 return gst_rtsp_sdp_from_media (sdp, info, media);
2488 * gst_rtsp_media_setup_sdp:
2489 * @sdp: a #GstSDPMessage
2491 * @media: a #GstRTSPMedia
2493 * Add @media specific info to @sdp. @info is used to configure the connection
2494 * information in the SDP.
2496 * Returns: TRUE on success.
2499 gst_rtsp_media_setup_sdp (GstRTSPMedia * media, GstSDPMessage * sdp,
2502 GstRTSPMediaPrivate *priv;
2503 GstRTSPMediaClass *klass;
2506 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2507 g_return_val_if_fail (sdp != NULL, FALSE);
2508 g_return_val_if_fail (info != NULL, FALSE);
2512 g_rec_mutex_lock (&priv->state_lock);
2514 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2516 if (!klass->setup_sdp)
2519 res = klass->setup_sdp (media, sdp, info);
2521 g_rec_mutex_unlock (&priv->state_lock);
2528 g_rec_mutex_unlock (&priv->state_lock);
2529 GST_ERROR ("no setup_sdp function");
2530 g_critical ("no setup_sdp vmethod function set");
2536 * gst_rtsp_media_suspend:
2537 * @media: a #GstRTSPMedia
2539 * Suspend @media. The state of the pipeline managed by @media is set to
2540 * GST_STATE_NULL but all streams are kept. @media can be prepared again
2541 * with gst_rtsp_media_undo_reset()
2543 * @media must be prepared with gst_rtsp_media_prepare();
2545 * Returns: %TRUE on success.
2548 gst_rtsp_media_suspend (GstRTSPMedia * media)
2550 GstRTSPMediaPrivate *priv = media->priv;
2551 GstStateChangeReturn ret;
2553 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2555 GST_FIXME ("suspend for dynamic pipelines needs fixing");
2557 g_rec_mutex_lock (&priv->state_lock);
2558 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
2561 /* don't attempt to suspend when something is busy */
2562 if (priv->n_active > 0)
2565 switch (priv->suspend_mode) {
2566 case GST_RTSP_SUSPEND_MODE_NONE:
2567 GST_DEBUG ("media %p no suspend", media);
2569 case GST_RTSP_SUSPEND_MODE_PAUSE:
2570 GST_DEBUG ("media %p suspend to PAUSED", media);
2571 priv->target_state = GST_STATE_PAUSED;
2572 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_TARGET_STATE], 0,
2573 priv->target_state, NULL);
2574 ret = gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
2575 if (ret == GST_STATE_CHANGE_FAILURE)
2578 case GST_RTSP_SUSPEND_MODE_RESET:
2579 GST_DEBUG ("media %p suspend to NULL", media);
2580 priv->target_state = GST_STATE_NULL;
2581 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_TARGET_STATE], 0,
2582 priv->target_state, NULL);
2583 ret = gst_element_set_state (priv->pipeline, GST_STATE_NULL);
2584 if (ret == GST_STATE_CHANGE_FAILURE)
2590 /* let the streams do the state changes freely, if any */
2591 media_streams_set_blocked (media, FALSE);
2592 priv->status = GST_RTSP_MEDIA_STATUS_SUSPENDED;
2594 g_rec_mutex_unlock (&priv->state_lock);
2601 g_rec_mutex_unlock (&priv->state_lock);
2602 GST_WARNING ("media %p was not prepared", media);
2607 g_rec_mutex_unlock (&priv->state_lock);
2608 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2609 GST_WARNING ("failed changing pipeline's state for media %p", media);
2615 * gst_rtsp_media_unsuspend:
2616 * @media: a #GstRTSPMedia
2618 * Unsuspend @media if it was in a suspended state. This method does nothing
2619 * when the media was not in the suspended state.
2621 * Returns: %TRUE on success.
2624 gst_rtsp_media_unsuspend (GstRTSPMedia * media)
2626 GstRTSPMediaPrivate *priv = media->priv;
2628 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2630 g_rec_mutex_lock (&priv->state_lock);
2631 if (priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
2634 switch (priv->suspend_mode) {
2635 case GST_RTSP_SUSPEND_MODE_NONE:
2636 priv->status = GST_RTSP_MEDIA_STATUS_PREPARED;
2638 case GST_RTSP_SUSPEND_MODE_PAUSE:
2639 priv->status = GST_RTSP_MEDIA_STATUS_PREPARED;
2641 case GST_RTSP_SUSPEND_MODE_RESET:
2643 priv->status = GST_RTSP_MEDIA_STATUS_PREPARING;
2644 if (!start_preroll (media))
2646 g_rec_mutex_unlock (&priv->state_lock);
2648 if (!wait_preroll (media))
2649 goto preroll_failed;
2651 g_rec_mutex_lock (&priv->state_lock);
2657 g_rec_mutex_unlock (&priv->state_lock);
2664 g_rec_mutex_unlock (&priv->state_lock);
2665 GST_WARNING ("failed to preroll pipeline");
2666 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2671 GST_WARNING ("failed to preroll pipeline");
2676 /* must be called with state-lock */
2678 media_set_pipeline_state_locked (GstRTSPMedia * media, GstState state)
2680 GstRTSPMediaPrivate *priv = media->priv;
2682 if (state == GST_STATE_NULL) {
2683 gst_rtsp_media_unprepare (media);
2685 GST_INFO ("state %s media %p", gst_element_state_get_name (state), media);
2686 priv->target_state = state;
2687 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_TARGET_STATE], 0,
2688 priv->target_state, NULL);
2689 /* when we are buffering, don't update the state yet, this will be done
2690 * when buffering finishes */
2691 if (priv->buffering) {
2692 GST_INFO ("Buffering busy, delay state change");
2694 if (state == GST_STATE_PLAYING)
2695 /* make sure pads are not blocking anymore when going to PLAYING */
2696 media_streams_set_blocked (media, FALSE);
2698 gst_element_set_state (priv->pipeline, state);
2700 /* and suspend after pause */
2701 if (state == GST_STATE_PAUSED)
2702 gst_rtsp_media_suspend (media);
2708 * gst_rtsp_media_set_pipeline_state:
2709 * @media: a #GstRTSPMedia
2710 * @state: the target state of the pipeline
2712 * Set the state of the pipeline managed by @media to @state
2715 gst_rtsp_media_set_pipeline_state (GstRTSPMedia * media, GstState state)
2717 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
2719 g_rec_mutex_lock (&media->priv->state_lock);
2720 media_set_pipeline_state_locked (media, state);
2721 g_rec_mutex_unlock (&media->priv->state_lock);
2725 * gst_rtsp_media_set_state:
2726 * @media: a #GstRTSPMedia
2727 * @state: the target state of the media
2728 * @transports: (element-type GstRtspServer.RTSPStreamTransport): a #GPtrArray
2729 * of #GstRTSPStreamTransport pointers
2731 * Set the state of @media to @state and for the transports in @transports.
2733 * @media must be prepared with gst_rtsp_media_prepare();
2735 * Returns: %TRUE on success.
2738 gst_rtsp_media_set_state (GstRTSPMedia * media, GstState state,
2739 GPtrArray * transports)
2741 GstRTSPMediaPrivate *priv;
2743 gboolean activate, deactivate, do_state;
2746 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2747 g_return_val_if_fail (transports != NULL, FALSE);
2751 g_rec_mutex_lock (&priv->state_lock);
2752 if (priv->status == GST_RTSP_MEDIA_STATUS_ERROR)
2754 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
2755 priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
2758 /* NULL and READY are the same */
2759 if (state == GST_STATE_READY)
2760 state = GST_STATE_NULL;
2762 activate = deactivate = FALSE;
2764 GST_INFO ("going to state %s media %p", gst_element_state_get_name (state),
2768 case GST_STATE_NULL:
2769 case GST_STATE_PAUSED:
2770 /* we're going from PLAYING to PAUSED, READY or NULL, deactivate */
2771 if (priv->target_state == GST_STATE_PLAYING)
2774 case GST_STATE_PLAYING:
2775 /* we're going to PLAYING, activate */
2781 old_active = priv->n_active;
2783 for (i = 0; i < transports->len; i++) {
2784 GstRTSPStreamTransport *trans;
2786 /* we need a non-NULL entry in the array */
2787 trans = g_ptr_array_index (transports, i);
2792 if (gst_rtsp_stream_transport_set_active (trans, TRUE))
2794 } else if (deactivate) {
2795 if (gst_rtsp_stream_transport_set_active (trans, FALSE))
2800 /* we just activated the first media, do the playing state change */
2801 if (old_active == 0 && activate)
2803 /* if we have no more active media, do the downward state changes */
2804 else if (priv->n_active == 0)
2809 GST_INFO ("state %d active %d media %p do_state %d", state, priv->n_active,
2812 if (priv->target_state != state) {
2814 media_set_pipeline_state_locked (media, state);
2816 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STATE], 0, state,
2820 /* remember where we are */
2821 if (state != GST_STATE_NULL && (state == GST_STATE_PAUSED ||
2822 old_active != priv->n_active))
2823 collect_media_stats (media);
2825 g_rec_mutex_unlock (&priv->state_lock);
2832 GST_WARNING ("media %p was not prepared", media);
2833 g_rec_mutex_unlock (&priv->state_lock);
2838 GST_WARNING ("media %p in error status while changing to state %d",
2840 if (state == GST_STATE_NULL) {
2841 for (i = 0; i < transports->len; i++) {
2842 GstRTSPStreamTransport *trans;
2844 /* we need a non-NULL entry in the array */
2845 trans = g_ptr_array_index (transports, i);
2849 gst_rtsp_stream_transport_set_active (trans, FALSE);
2853 g_rec_mutex_unlock (&priv->state_lock);