2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
3 * Copyright (C) 2015 Centricular Ltd
4 * Author: Sebastian Dröge <sebastian@centricular.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * @short_description: The media pipeline
24 * @see_also: #GstRTSPMediaFactory, #GstRTSPStream, #GstRTSPSession,
25 * #GstRTSPSessionMedia
27 * a #GstRTSPMedia contains the complete GStreamer pipeline to manage the
28 * streaming to the clients. The actual data transfer is done by the
29 * #GstRTSPStream objects that are created and exposed by the #GstRTSPMedia.
31 * The #GstRTSPMedia is usually created from a #GstRTSPMediaFactory when the
32 * client does a DESCRIBE or SETUP of a resource.
34 * A media is created with gst_rtsp_media_new() that takes the element that will
35 * provide the streaming elements. For each of the streams, a new #GstRTSPStream
36 * object needs to be made with the gst_rtsp_media_create_stream() which takes
37 * the payloader element and the source pad that produces the RTP stream.
39 * The pipeline of the media is set to PAUSED with gst_rtsp_media_prepare(). The
40 * prepare method will add rtpbin and sinks and sources to send and receive RTP
41 * and RTCP packets from the clients. Each stream srcpad is connected to an
42 * input into the internal rtpbin.
44 * It is also possible to dynamically create #GstRTSPStream objects during the
45 * prepare phase. With gst_rtsp_media_get_status() you can check the status of
48 * After the media is prepared, it is ready for streaming. It will usually be
49 * managed in a session with gst_rtsp_session_manage_media(). See
50 * #GstRTSPSession and #GstRTSPSessionMedia.
52 * The state of the media can be controlled with gst_rtsp_media_set_state ().
53 * Seeking can be done with gst_rtsp_media_seek().
55 * With gst_rtsp_media_unprepare() the pipeline is stopped and shut down. When
56 * gst_rtsp_media_set_eos_shutdown() an EOS will be sent to the pipeline to
59 * With gst_rtsp_media_set_shared(), the media can be shared between multiple
60 * clients. With gst_rtsp_media_set_reusable() you can control if the pipeline
61 * can be prepared again after an unprepare.
63 * Last reviewed on 2013-07-11 (1.0.0)
70 #include <gst/app/gstappsrc.h>
71 #include <gst/app/gstappsink.h>
73 #include <gst/sdp/gstmikey.h>
74 #include <gst/rtp/gstrtppayloads.h>
76 #define AES_128_KEY_LEN 16
77 #define AES_256_KEY_LEN 32
79 #define HMAC_32_KEY_LEN 4
80 #define HMAC_80_KEY_LEN 10
82 #include "rtsp-media.h"
84 #define GST_RTSP_MEDIA_GET_PRIVATE(obj) \
85 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_MEDIA, GstRTSPMediaPrivate))
87 struct _GstRTSPMediaPrivate
92 /* protected by lock */
93 GstRTSPPermissions *permissions;
95 gboolean suspend_mode;
97 GstRTSPProfile profiles;
98 GstRTSPLowerTrans protocols;
100 gboolean eos_shutdown;
102 GstRTSPAddressPool *pool;
107 GRecMutex state_lock; /* locking order: state lock, lock */
108 GPtrArray *streams; /* protected by lock */
109 GList *dynamic; /* protected by lock */
110 GstRTSPMediaStatus status; /* protected by lock */
115 /* the pipeline for the media */
116 GstElement *pipeline;
117 GstElement *fakesink; /* protected by lock */
120 GstRTSPThread *thread;
122 gboolean time_provider;
123 GstNetTimeProvider *nettime;
128 GstState target_state;
130 /* RTP session manager */
133 /* the range of media */
134 GstRTSPTimeRange range; /* protected by lock */
135 GstClockTime range_start;
136 GstClockTime range_stop;
138 GList *payloads; /* protected by lock */
139 GstClockTime rtx_time; /* protected by lock */
142 #define DEFAULT_SHARED FALSE
143 #define DEFAULT_SUSPEND_MODE GST_RTSP_SUSPEND_MODE_NONE
144 #define DEFAULT_REUSABLE FALSE
145 #define DEFAULT_PROFILES GST_RTSP_PROFILE_AVP
146 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | \
147 GST_RTSP_LOWER_TRANS_TCP
148 #define DEFAULT_EOS_SHUTDOWN FALSE
149 #define DEFAULT_BUFFER_SIZE 0x80000
150 #define DEFAULT_TIME_PROVIDER FALSE
151 #define DEFAULT_RECORD FALSE
153 /* define to dump received RTCP packets */
175 SIGNAL_REMOVED_STREAM,
183 GST_DEBUG_CATEGORY_STATIC (rtsp_media_debug);
184 #define GST_CAT_DEFAULT rtsp_media_debug
186 static void gst_rtsp_media_get_property (GObject * object, guint propid,
187 GValue * value, GParamSpec * pspec);
188 static void gst_rtsp_media_set_property (GObject * object, guint propid,
189 const GValue * value, GParamSpec * pspec);
190 static void gst_rtsp_media_finalize (GObject * obj);
192 static gboolean default_handle_message (GstRTSPMedia * media,
193 GstMessage * message);
194 static void finish_unprepare (GstRTSPMedia * media);
195 static gboolean default_prepare (GstRTSPMedia * media, GstRTSPThread * thread);
196 static gboolean default_unprepare (GstRTSPMedia * media);
197 static gboolean default_suspend (GstRTSPMedia * media);
198 static gboolean default_unsuspend (GstRTSPMedia * media);
199 static gboolean default_convert_range (GstRTSPMedia * media,
200 GstRTSPTimeRange * range, GstRTSPRangeUnit unit);
201 static gboolean default_query_position (GstRTSPMedia * media,
203 static gboolean default_query_stop (GstRTSPMedia * media, gint64 * stop);
204 static GstElement *default_create_rtpbin (GstRTSPMedia * media);
205 static gboolean default_setup_sdp (GstRTSPMedia * media, GstSDPMessage * sdp,
207 static gboolean default_handle_sdp (GstRTSPMedia * media, GstSDPMessage * sdp);
209 static gboolean wait_preroll (GstRTSPMedia * media);
211 static guint gst_rtsp_media_signals[SIGNAL_LAST] = { 0 };
213 #define C_ENUM(v) ((gint) v)
215 #define GST_TYPE_RTSP_SUSPEND_MODE (gst_rtsp_suspend_mode_get_type())
217 gst_rtsp_suspend_mode_get_type (void)
220 static const GEnumValue values[] = {
221 {C_ENUM (GST_RTSP_SUSPEND_MODE_NONE), "GST_RTSP_SUSPEND_MODE_NONE", "none"},
222 {C_ENUM (GST_RTSP_SUSPEND_MODE_PAUSE), "GST_RTSP_SUSPEND_MODE_PAUSE",
224 {C_ENUM (GST_RTSP_SUSPEND_MODE_RESET), "GST_RTSP_SUSPEND_MODE_RESET",
229 if (g_once_init_enter (&id)) {
230 GType tmp = g_enum_register_static ("GstRTSPSuspendMode", values);
231 g_once_init_leave (&id, tmp);
236 G_DEFINE_TYPE (GstRTSPMedia, gst_rtsp_media, G_TYPE_OBJECT);
239 gst_rtsp_media_class_init (GstRTSPMediaClass * klass)
241 GObjectClass *gobject_class;
243 g_type_class_add_private (klass, sizeof (GstRTSPMediaPrivate));
245 gobject_class = G_OBJECT_CLASS (klass);
247 gobject_class->get_property = gst_rtsp_media_get_property;
248 gobject_class->set_property = gst_rtsp_media_set_property;
249 gobject_class->finalize = gst_rtsp_media_finalize;
251 g_object_class_install_property (gobject_class, PROP_SHARED,
252 g_param_spec_boolean ("shared", "Shared",
253 "If this media pipeline can be shared", DEFAULT_SHARED,
254 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
256 g_object_class_install_property (gobject_class, PROP_SUSPEND_MODE,
257 g_param_spec_enum ("suspend-mode", "Suspend Mode",
258 "How to suspend the media in PAUSED", GST_TYPE_RTSP_SUSPEND_MODE,
259 DEFAULT_SUSPEND_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
261 g_object_class_install_property (gobject_class, PROP_REUSABLE,
262 g_param_spec_boolean ("reusable", "Reusable",
263 "If this media pipeline can be reused after an unprepare",
264 DEFAULT_REUSABLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
266 g_object_class_install_property (gobject_class, PROP_PROFILES,
267 g_param_spec_flags ("profiles", "Profiles",
268 "Allowed transfer profiles", GST_TYPE_RTSP_PROFILE,
269 DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
271 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
272 g_param_spec_flags ("protocols", "Protocols",
273 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
274 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
276 g_object_class_install_property (gobject_class, PROP_EOS_SHUTDOWN,
277 g_param_spec_boolean ("eos-shutdown", "EOS Shutdown",
278 "Send an EOS event to the pipeline before unpreparing",
279 DEFAULT_EOS_SHUTDOWN, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
281 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
282 g_param_spec_uint ("buffer-size", "Buffer Size",
283 "The kernel UDP buffer size to use", 0, G_MAXUINT,
284 DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
286 g_object_class_install_property (gobject_class, PROP_ELEMENT,
287 g_param_spec_object ("element", "The Element",
288 "The GstBin to use for streaming the media", GST_TYPE_ELEMENT,
289 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
291 g_object_class_install_property (gobject_class, PROP_TIME_PROVIDER,
292 g_param_spec_boolean ("time-provider", "Time Provider",
293 "Use a NetTimeProvider for clients",
294 DEFAULT_TIME_PROVIDER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
296 g_object_class_install_property (gobject_class, PROP_RECORD,
297 g_param_spec_boolean ("record", "Record",
298 "If this media pipeline can be used for PLAY or RECORD",
299 DEFAULT_RECORD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
301 gst_rtsp_media_signals[SIGNAL_NEW_STREAM] =
302 g_signal_new ("new-stream", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
303 G_STRUCT_OFFSET (GstRTSPMediaClass, new_stream), NULL, NULL,
304 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_RTSP_STREAM);
306 gst_rtsp_media_signals[SIGNAL_REMOVED_STREAM] =
307 g_signal_new ("removed-stream", G_TYPE_FROM_CLASS (klass),
308 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPMediaClass, removed_stream),
309 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
310 GST_TYPE_RTSP_STREAM);
312 gst_rtsp_media_signals[SIGNAL_PREPARED] =
313 g_signal_new ("prepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
314 G_STRUCT_OFFSET (GstRTSPMediaClass, prepared), NULL, NULL,
315 g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
317 gst_rtsp_media_signals[SIGNAL_UNPREPARED] =
318 g_signal_new ("unprepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
319 G_STRUCT_OFFSET (GstRTSPMediaClass, unprepared), NULL, NULL,
320 g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
322 gst_rtsp_media_signals[SIGNAL_TARGET_STATE] =
323 g_signal_new ("target-state", G_TYPE_FROM_CLASS (klass),
324 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPMediaClass, target_state),
325 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_INT);
327 gst_rtsp_media_signals[SIGNAL_NEW_STATE] =
328 g_signal_new ("new-state", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
329 G_STRUCT_OFFSET (GstRTSPMediaClass, new_state), NULL, NULL,
330 g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_INT);
332 GST_DEBUG_CATEGORY_INIT (rtsp_media_debug, "rtspmedia", 0, "GstRTSPMedia");
334 klass->handle_message = default_handle_message;
335 klass->prepare = default_prepare;
336 klass->unprepare = default_unprepare;
337 klass->suspend = default_suspend;
338 klass->unsuspend = default_unsuspend;
339 klass->convert_range = default_convert_range;
340 klass->query_position = default_query_position;
341 klass->query_stop = default_query_stop;
342 klass->create_rtpbin = default_create_rtpbin;
343 klass->setup_sdp = default_setup_sdp;
344 klass->handle_sdp = default_handle_sdp;
348 gst_rtsp_media_init (GstRTSPMedia * media)
350 GstRTSPMediaPrivate *priv = GST_RTSP_MEDIA_GET_PRIVATE (media);
354 priv->streams = g_ptr_array_new_with_free_func (g_object_unref);
355 g_mutex_init (&priv->lock);
356 g_cond_init (&priv->cond);
357 g_rec_mutex_init (&priv->state_lock);
359 priv->shared = DEFAULT_SHARED;
360 priv->suspend_mode = DEFAULT_SUSPEND_MODE;
361 priv->reusable = DEFAULT_REUSABLE;
362 priv->profiles = DEFAULT_PROFILES;
363 priv->protocols = DEFAULT_PROTOCOLS;
364 priv->eos_shutdown = DEFAULT_EOS_SHUTDOWN;
365 priv->buffer_size = DEFAULT_BUFFER_SIZE;
366 priv->time_provider = DEFAULT_TIME_PROVIDER;
367 priv->record = DEFAULT_RECORD;
371 gst_rtsp_media_finalize (GObject * obj)
373 GstRTSPMediaPrivate *priv;
376 media = GST_RTSP_MEDIA (obj);
379 GST_INFO ("finalize media %p", media);
381 if (priv->permissions)
382 gst_rtsp_permissions_unref (priv->permissions);
384 g_ptr_array_unref (priv->streams);
386 g_list_free_full (priv->dynamic, gst_object_unref);
389 gst_object_unref (priv->pipeline);
391 gst_object_unref (priv->nettime);
392 gst_object_unref (priv->element);
394 g_object_unref (priv->pool);
396 g_list_free (priv->payloads);
397 g_mutex_clear (&priv->lock);
398 g_cond_clear (&priv->cond);
399 g_rec_mutex_clear (&priv->state_lock);
401 G_OBJECT_CLASS (gst_rtsp_media_parent_class)->finalize (obj);
405 gst_rtsp_media_get_property (GObject * object, guint propid,
406 GValue * value, GParamSpec * pspec)
408 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
412 g_value_set_object (value, media->priv->element);
415 g_value_set_boolean (value, gst_rtsp_media_is_shared (media));
417 case PROP_SUSPEND_MODE:
418 g_value_set_enum (value, gst_rtsp_media_get_suspend_mode (media));
421 g_value_set_boolean (value, gst_rtsp_media_is_reusable (media));
424 g_value_set_flags (value, gst_rtsp_media_get_profiles (media));
427 g_value_set_flags (value, gst_rtsp_media_get_protocols (media));
429 case PROP_EOS_SHUTDOWN:
430 g_value_set_boolean (value, gst_rtsp_media_is_eos_shutdown (media));
432 case PROP_BUFFER_SIZE:
433 g_value_set_uint (value, gst_rtsp_media_get_buffer_size (media));
435 case PROP_TIME_PROVIDER:
436 g_value_set_boolean (value, gst_rtsp_media_is_time_provider (media));
439 g_value_set_boolean (value, gst_rtsp_media_is_record (media));
442 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
447 gst_rtsp_media_set_property (GObject * object, guint propid,
448 const GValue * value, GParamSpec * pspec)
450 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
454 media->priv->element = g_value_get_object (value);
455 gst_object_ref_sink (media->priv->element);
458 gst_rtsp_media_set_shared (media, g_value_get_boolean (value));
460 case PROP_SUSPEND_MODE:
461 gst_rtsp_media_set_suspend_mode (media, g_value_get_enum (value));
464 gst_rtsp_media_set_reusable (media, g_value_get_boolean (value));
467 gst_rtsp_media_set_profiles (media, g_value_get_flags (value));
470 gst_rtsp_media_set_protocols (media, g_value_get_flags (value));
472 case PROP_EOS_SHUTDOWN:
473 gst_rtsp_media_set_eos_shutdown (media, g_value_get_boolean (value));
475 case PROP_BUFFER_SIZE:
476 gst_rtsp_media_set_buffer_size (media, g_value_get_uint (value));
478 case PROP_TIME_PROVIDER:
479 gst_rtsp_media_use_time_provider (media, g_value_get_boolean (value));
482 gst_rtsp_media_set_record (media, g_value_get_boolean (value));
485 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
493 } DoQueryPositionData;
496 do_query_position (GstRTSPStream * stream, DoQueryPositionData * data)
500 if (gst_rtsp_stream_query_position (stream, &tmp)) {
501 data->position = MAX (data->position, tmp);
507 default_query_position (GstRTSPMedia * media, gint64 * position)
509 GstRTSPMediaPrivate *priv;
510 DoQueryPositionData data;
517 g_ptr_array_foreach (priv->streams, (GFunc) do_query_position, &data);
519 *position = data.position;
531 do_query_stop (GstRTSPStream * stream, DoQueryStopData * data)
535 if (gst_rtsp_stream_query_stop (stream, &tmp)) {
536 data->stop = MAX (data->stop, tmp);
542 default_query_stop (GstRTSPMedia * media, gint64 * stop)
544 GstRTSPMediaPrivate *priv;
545 DoQueryStopData data;
552 g_ptr_array_foreach (priv->streams, (GFunc) do_query_stop, &data);
560 default_create_rtpbin (GstRTSPMedia * media)
564 rtpbin = gst_element_factory_make ("rtpbin", NULL);
569 /* must be called with state lock */
571 collect_media_stats (GstRTSPMedia * media)
573 GstRTSPMediaPrivate *priv = media->priv;
574 gint64 position = 0, stop = -1;
576 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
577 priv->status != GST_RTSP_MEDIA_STATUS_PREPARING)
580 priv->range.unit = GST_RTSP_RANGE_NPT;
582 GST_INFO ("collect media stats");
585 priv->range.min.type = GST_RTSP_TIME_NOW;
586 priv->range.min.seconds = -1;
587 priv->range_start = -1;
588 priv->range.max.type = GST_RTSP_TIME_END;
589 priv->range.max.seconds = -1;
590 priv->range_stop = -1;
592 GstRTSPMediaClass *klass;
595 klass = GST_RTSP_MEDIA_GET_CLASS (media);
597 /* get the position */
599 if (klass->query_position)
600 ret = klass->query_position (media, &position);
603 GST_INFO ("position query failed");
607 /* get the current segment stop */
609 if (klass->query_stop)
610 ret = klass->query_stop (media, &stop);
613 GST_INFO ("stop query failed");
617 GST_INFO ("stats: position %" GST_TIME_FORMAT ", stop %"
618 GST_TIME_FORMAT, GST_TIME_ARGS (position), GST_TIME_ARGS (stop));
620 if (position == -1) {
621 priv->range.min.type = GST_RTSP_TIME_NOW;
622 priv->range.min.seconds = -1;
623 priv->range_start = -1;
625 priv->range.min.type = GST_RTSP_TIME_SECONDS;
626 priv->range.min.seconds = ((gdouble) position) / GST_SECOND;
627 priv->range_start = position;
630 priv->range.max.type = GST_RTSP_TIME_END;
631 priv->range.max.seconds = -1;
632 priv->range_stop = -1;
634 priv->range.max.type = GST_RTSP_TIME_SECONDS;
635 priv->range.max.seconds = ((gdouble) stop) / GST_SECOND;
636 priv->range_stop = stop;
642 * gst_rtsp_media_new:
643 * @element: (transfer full): a #GstElement
645 * Create a new #GstRTSPMedia instance. @element is the bin element that
646 * provides the different streams. The #GstRTSPMedia object contains the
647 * element to produce RTP data for one or more related (audio/video/..)
650 * Ownership is taken of @element.
652 * Returns: (transfer full): a new #GstRTSPMedia object.
655 gst_rtsp_media_new (GstElement * element)
657 GstRTSPMedia *result;
659 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
661 result = g_object_new (GST_TYPE_RTSP_MEDIA, "element", element, NULL);
667 * gst_rtsp_media_get_element:
668 * @media: a #GstRTSPMedia
670 * Get the element that was used when constructing @media.
672 * Returns: (transfer full): a #GstElement. Unref after usage.
675 gst_rtsp_media_get_element (GstRTSPMedia * media)
677 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
679 return gst_object_ref (media->priv->element);
683 * gst_rtsp_media_take_pipeline:
684 * @media: a #GstRTSPMedia
685 * @pipeline: (transfer full): a #GstPipeline
687 * Set @pipeline as the #GstPipeline for @media. Ownership is
688 * taken of @pipeline.
691 gst_rtsp_media_take_pipeline (GstRTSPMedia * media, GstPipeline * pipeline)
693 GstRTSPMediaPrivate *priv;
695 GstNetTimeProvider *nettime;
697 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
698 g_return_if_fail (GST_IS_PIPELINE (pipeline));
702 g_mutex_lock (&priv->lock);
703 old = priv->pipeline;
704 priv->pipeline = GST_ELEMENT_CAST (pipeline);
705 nettime = priv->nettime;
706 priv->nettime = NULL;
707 g_mutex_unlock (&priv->lock);
710 gst_object_unref (old);
713 gst_object_unref (nettime);
715 gst_bin_add (GST_BIN_CAST (pipeline), priv->element);
719 * gst_rtsp_media_set_permissions:
720 * @media: a #GstRTSPMedia
721 * @permissions: (transfer none): a #GstRTSPPermissions
723 * Set @permissions on @media.
726 gst_rtsp_media_set_permissions (GstRTSPMedia * media,
727 GstRTSPPermissions * permissions)
729 GstRTSPMediaPrivate *priv;
731 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
735 g_mutex_lock (&priv->lock);
736 if (priv->permissions)
737 gst_rtsp_permissions_unref (priv->permissions);
738 if ((priv->permissions = permissions))
739 gst_rtsp_permissions_ref (permissions);
740 g_mutex_unlock (&priv->lock);
744 * gst_rtsp_media_get_permissions:
745 * @media: a #GstRTSPMedia
747 * Get the permissions object from @media.
749 * Returns: (transfer full): a #GstRTSPPermissions object, unref after usage.
752 gst_rtsp_media_get_permissions (GstRTSPMedia * media)
754 GstRTSPMediaPrivate *priv;
755 GstRTSPPermissions *result;
757 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
761 g_mutex_lock (&priv->lock);
762 if ((result = priv->permissions))
763 gst_rtsp_permissions_ref (result);
764 g_mutex_unlock (&priv->lock);
770 * gst_rtsp_media_set_suspend_mode:
771 * @media: a #GstRTSPMedia
772 * @mode: the new #GstRTSPSuspendMode
774 * Control how @ media will be suspended after the SDP has been generated and
775 * after a PAUSE request has been performed.
777 * Media must be unprepared when setting the suspend mode.
780 gst_rtsp_media_set_suspend_mode (GstRTSPMedia * media, GstRTSPSuspendMode mode)
782 GstRTSPMediaPrivate *priv;
784 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
788 g_rec_mutex_lock (&priv->state_lock);
789 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED)
791 priv->suspend_mode = mode;
792 g_rec_mutex_unlock (&priv->state_lock);
799 GST_WARNING ("media %p was prepared", media);
800 g_rec_mutex_unlock (&priv->state_lock);
805 * gst_rtsp_media_get_suspend_mode:
806 * @media: a #GstRTSPMedia
808 * Get how @media will be suspended.
810 * Returns: #GstRTSPSuspendMode.
813 gst_rtsp_media_get_suspend_mode (GstRTSPMedia * media)
815 GstRTSPMediaPrivate *priv;
816 GstRTSPSuspendMode res;
818 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_RTSP_SUSPEND_MODE_NONE);
822 g_rec_mutex_lock (&priv->state_lock);
823 res = priv->suspend_mode;
824 g_rec_mutex_unlock (&priv->state_lock);
830 * gst_rtsp_media_set_shared:
831 * @media: a #GstRTSPMedia
832 * @shared: the new value
834 * Set or unset if the pipeline for @media can be shared will multiple clients.
835 * When @shared is %TRUE, client requests for this media will share the media
839 gst_rtsp_media_set_shared (GstRTSPMedia * media, gboolean shared)
841 GstRTSPMediaPrivate *priv;
843 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
847 g_mutex_lock (&priv->lock);
848 priv->shared = shared;
849 g_mutex_unlock (&priv->lock);
853 * gst_rtsp_media_is_shared:
854 * @media: a #GstRTSPMedia
856 * Check if the pipeline for @media can be shared between multiple clients.
858 * Returns: %TRUE if the media can be shared between clients.
861 gst_rtsp_media_is_shared (GstRTSPMedia * media)
863 GstRTSPMediaPrivate *priv;
866 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
870 g_mutex_lock (&priv->lock);
872 g_mutex_unlock (&priv->lock);
878 * gst_rtsp_media_set_reusable:
879 * @media: a #GstRTSPMedia
880 * @reusable: the new value
882 * Set or unset if the pipeline for @media can be reused after the pipeline has
886 gst_rtsp_media_set_reusable (GstRTSPMedia * media, gboolean reusable)
888 GstRTSPMediaPrivate *priv;
890 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
894 g_mutex_lock (&priv->lock);
895 priv->reusable = reusable;
896 g_mutex_unlock (&priv->lock);
900 * gst_rtsp_media_is_reusable:
901 * @media: a #GstRTSPMedia
903 * Check if the pipeline for @media can be reused after an unprepare.
905 * Returns: %TRUE if the media can be reused
908 gst_rtsp_media_is_reusable (GstRTSPMedia * media)
910 GstRTSPMediaPrivate *priv;
913 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
917 g_mutex_lock (&priv->lock);
918 res = priv->reusable;
919 g_mutex_unlock (&priv->lock);
925 do_set_profiles (GstRTSPStream * stream, GstRTSPProfile * profiles)
927 gst_rtsp_stream_set_profiles (stream, *profiles);
931 * gst_rtsp_media_set_profiles:
932 * @media: a #GstRTSPMedia
933 * @profiles: the new flags
935 * Configure the allowed lower transport for @media.
938 gst_rtsp_media_set_profiles (GstRTSPMedia * media, GstRTSPProfile profiles)
940 GstRTSPMediaPrivate *priv;
942 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
946 g_mutex_lock (&priv->lock);
947 priv->profiles = profiles;
948 g_ptr_array_foreach (priv->streams, (GFunc) do_set_profiles, &profiles);
949 g_mutex_unlock (&priv->lock);
953 * gst_rtsp_media_get_profiles:
954 * @media: a #GstRTSPMedia
956 * Get the allowed profiles of @media.
958 * Returns: a #GstRTSPProfile
961 gst_rtsp_media_get_profiles (GstRTSPMedia * media)
963 GstRTSPMediaPrivate *priv;
966 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_RTSP_PROFILE_UNKNOWN);
970 g_mutex_lock (&priv->lock);
971 res = priv->profiles;
972 g_mutex_unlock (&priv->lock);
978 do_set_protocols (GstRTSPStream * stream, GstRTSPLowerTrans * protocols)
980 gst_rtsp_stream_set_protocols (stream, *protocols);
984 * gst_rtsp_media_set_protocols:
985 * @media: a #GstRTSPMedia
986 * @protocols: the new flags
988 * Configure the allowed lower transport for @media.
991 gst_rtsp_media_set_protocols (GstRTSPMedia * media, GstRTSPLowerTrans protocols)
993 GstRTSPMediaPrivate *priv;
995 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
999 g_mutex_lock (&priv->lock);
1000 priv->protocols = protocols;
1001 g_ptr_array_foreach (priv->streams, (GFunc) do_set_protocols, &protocols);
1002 g_mutex_unlock (&priv->lock);
1006 * gst_rtsp_media_get_protocols:
1007 * @media: a #GstRTSPMedia
1009 * Get the allowed protocols of @media.
1011 * Returns: a #GstRTSPLowerTrans
1014 gst_rtsp_media_get_protocols (GstRTSPMedia * media)
1016 GstRTSPMediaPrivate *priv;
1017 GstRTSPLowerTrans res;
1019 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media),
1020 GST_RTSP_LOWER_TRANS_UNKNOWN);
1024 g_mutex_lock (&priv->lock);
1025 res = priv->protocols;
1026 g_mutex_unlock (&priv->lock);
1032 * gst_rtsp_media_set_eos_shutdown:
1033 * @media: a #GstRTSPMedia
1034 * @eos_shutdown: the new value
1036 * Set or unset if an EOS event will be sent to the pipeline for @media before
1040 gst_rtsp_media_set_eos_shutdown (GstRTSPMedia * media, gboolean eos_shutdown)
1042 GstRTSPMediaPrivate *priv;
1044 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1048 g_mutex_lock (&priv->lock);
1049 priv->eos_shutdown = eos_shutdown;
1050 g_mutex_unlock (&priv->lock);
1054 * gst_rtsp_media_is_eos_shutdown:
1055 * @media: a #GstRTSPMedia
1057 * Check if the pipeline for @media will send an EOS down the pipeline before
1060 * Returns: %TRUE if the media will send EOS before unpreparing.
1063 gst_rtsp_media_is_eos_shutdown (GstRTSPMedia * media)
1065 GstRTSPMediaPrivate *priv;
1068 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1072 g_mutex_lock (&priv->lock);
1073 res = priv->eos_shutdown;
1074 g_mutex_unlock (&priv->lock);
1080 * gst_rtsp_media_set_buffer_size:
1081 * @media: a #GstRTSPMedia
1082 * @size: the new value
1084 * Set the kernel UDP buffer size.
1087 gst_rtsp_media_set_buffer_size (GstRTSPMedia * media, guint size)
1089 GstRTSPMediaPrivate *priv;
1091 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1093 GST_LOG_OBJECT (media, "set buffer size %u", size);
1097 g_mutex_lock (&priv->lock);
1098 priv->buffer_size = size;
1099 g_mutex_unlock (&priv->lock);
1103 * gst_rtsp_media_get_buffer_size:
1104 * @media: a #GstRTSPMedia
1106 * Get the kernel UDP buffer size.
1108 * Returns: the kernel UDP buffer size.
1111 gst_rtsp_media_get_buffer_size (GstRTSPMedia * media)
1113 GstRTSPMediaPrivate *priv;
1116 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1120 g_mutex_unlock (&priv->lock);
1121 res = priv->buffer_size;
1122 g_mutex_unlock (&priv->lock);
1128 * gst_rtsp_media_set_retransmission_time:
1129 * @media: a #GstRTSPMedia
1130 * @time: the new value
1132 * Set the amount of time to store retransmission packets.
1135 gst_rtsp_media_set_retransmission_time (GstRTSPMedia * media, GstClockTime time)
1137 GstRTSPMediaPrivate *priv;
1140 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1142 GST_LOG_OBJECT (media, "set retransmission time %" G_GUINT64_FORMAT, time);
1146 g_mutex_lock (&priv->lock);
1147 priv->rtx_time = time;
1148 for (i = 0; i < priv->streams->len; i++) {
1149 GstRTSPStream *stream = g_ptr_array_index (priv->streams, i);
1151 gst_rtsp_stream_set_retransmission_time (stream, time);
1155 g_object_set (priv->rtpbin, "do-retransmission", time > 0, NULL);
1156 g_mutex_unlock (&priv->lock);
1160 * gst_rtsp_media_get_retransmission_time:
1161 * @media: a #GstRTSPMedia
1163 * Get the amount of time to store retransmission data.
1165 * Returns: the amount of time to store retransmission data.
1168 gst_rtsp_media_get_retransmission_time (GstRTSPMedia * media)
1170 GstRTSPMediaPrivate *priv;
1173 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1177 g_mutex_unlock (&priv->lock);
1178 res = priv->rtx_time;
1179 g_mutex_unlock (&priv->lock);
1185 * gst_rtsp_media_use_time_provider:
1186 * @media: a #GstRTSPMedia
1187 * @time_provider: if a #GstNetTimeProvider should be used
1189 * Set @media to provide a #GstNetTimeProvider.
1192 gst_rtsp_media_use_time_provider (GstRTSPMedia * media, gboolean time_provider)
1194 GstRTSPMediaPrivate *priv;
1196 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1200 g_mutex_lock (&priv->lock);
1201 priv->time_provider = time_provider;
1202 g_mutex_unlock (&priv->lock);
1206 * gst_rtsp_media_is_time_provider:
1207 * @media: a #GstRTSPMedia
1209 * Check if @media can provide a #GstNetTimeProvider for its pipeline clock.
1211 * Use gst_rtsp_media_get_time_provider() to get the network clock.
1213 * Returns: %TRUE if @media can provide a #GstNetTimeProvider.
1216 gst_rtsp_media_is_time_provider (GstRTSPMedia * media)
1218 GstRTSPMediaPrivate *priv;
1221 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1225 g_mutex_unlock (&priv->lock);
1226 res = priv->time_provider;
1227 g_mutex_unlock (&priv->lock);
1233 * gst_rtsp_media_set_address_pool:
1234 * @media: a #GstRTSPMedia
1235 * @pool: (transfer none): a #GstRTSPAddressPool
1237 * configure @pool to be used as the address pool of @media.
1240 gst_rtsp_media_set_address_pool (GstRTSPMedia * media,
1241 GstRTSPAddressPool * pool)
1243 GstRTSPMediaPrivate *priv;
1244 GstRTSPAddressPool *old;
1246 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1250 GST_LOG_OBJECT (media, "set address pool %p", pool);
1252 g_mutex_lock (&priv->lock);
1253 if ((old = priv->pool) != pool)
1254 priv->pool = pool ? g_object_ref (pool) : NULL;
1257 g_ptr_array_foreach (priv->streams, (GFunc) gst_rtsp_stream_set_address_pool,
1259 g_mutex_unlock (&priv->lock);
1262 g_object_unref (old);
1266 * gst_rtsp_media_get_address_pool:
1267 * @media: a #GstRTSPMedia
1269 * Get the #GstRTSPAddressPool used as the address pool of @media.
1271 * Returns: (transfer full): the #GstRTSPAddressPool of @media. g_object_unref() after
1274 GstRTSPAddressPool *
1275 gst_rtsp_media_get_address_pool (GstRTSPMedia * media)
1277 GstRTSPMediaPrivate *priv;
1278 GstRTSPAddressPool *result;
1280 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1284 g_mutex_lock (&priv->lock);
1285 if ((result = priv->pool))
1286 g_object_ref (result);
1287 g_mutex_unlock (&priv->lock);
1293 _find_payload_types (GstRTSPMedia * media)
1296 GQueue queue = G_QUEUE_INIT;
1298 n = media->priv->streams->len;
1299 for (i = 0; i < n; i++) {
1300 GstRTSPStream *stream = g_ptr_array_index (media->priv->streams, i);
1301 guint pt = gst_rtsp_stream_get_pt (stream);
1303 g_queue_push_tail (&queue, GUINT_TO_POINTER (pt));
1310 _next_available_pt (GList * payloads)
1314 for (i = 96; i <= 127; i++) {
1315 GList *iter = g_list_find (payloads, GINT_TO_POINTER (i));
1317 return GPOINTER_TO_UINT (i);
1324 * gst_rtsp_media_collect_streams:
1325 * @media: a #GstRTSPMedia
1327 * Find all payloader elements, they should be named pay\%d in the
1328 * element of @media, and create #GstRTSPStreams for them.
1330 * Collect all dynamic elements, named dynpay\%d, and add them to
1331 * the list of dynamic elements.
1333 * Find all depayloader elements, they should be named depay\%d in the
1334 * element of @media, and create #GstRTSPStreams for them.
1337 gst_rtsp_media_collect_streams (GstRTSPMedia * media)
1339 GstRTSPMediaPrivate *priv;
1340 GstElement *element, *elem;
1345 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1348 element = priv->element;
1351 for (i = 0; have_elem; i++) {
1356 name = g_strdup_printf ("pay%d", i);
1357 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
1358 GST_INFO ("found stream %d with payloader %p", i, elem);
1360 /* take the pad of the payloader */
1361 pad = gst_element_get_static_pad (elem, "src");
1362 /* create the stream */
1363 gst_rtsp_media_create_stream (media, elem, pad);
1364 gst_object_unref (pad);
1365 gst_object_unref (elem);
1371 name = g_strdup_printf ("dynpay%d", i);
1372 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
1373 /* a stream that will dynamically create pads to provide RTP packets */
1374 GST_INFO ("found dynamic element %d, %p", i, elem);
1376 g_mutex_lock (&priv->lock);
1377 priv->dynamic = g_list_prepend (priv->dynamic, elem);
1378 g_mutex_unlock (&priv->lock);
1384 name = g_strdup_printf ("depay%d", i);
1385 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
1386 GST_INFO ("found stream %d with depayloader %p", i, elem);
1388 /* take the pad of the payloader */
1389 pad = gst_element_get_static_pad (elem, "sink");
1390 /* create the stream */
1391 gst_rtsp_media_create_stream (media, elem, pad);
1392 gst_object_unref (pad);
1393 gst_object_unref (elem);
1402 * gst_rtsp_media_create_stream:
1403 * @media: a #GstRTSPMedia
1404 * @payloader: a #GstElement
1407 * Create a new stream in @media that provides RTP data on @pad.
1408 * @pad should be a pad of an element inside @media->element.
1410 * Returns: (transfer none): a new #GstRTSPStream that remains valid for as long
1414 gst_rtsp_media_create_stream (GstRTSPMedia * media, GstElement * payloader,
1417 GstRTSPMediaPrivate *priv;
1418 GstRTSPStream *stream;
1423 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1424 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
1425 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1429 g_mutex_lock (&priv->lock);
1430 idx = priv->streams->len;
1432 GST_DEBUG ("media %p: creating stream with index %d", media, idx);
1434 if (GST_PAD_IS_SRC (pad))
1435 name = g_strdup_printf ("src_%u", idx);
1437 name = g_strdup_printf ("sink_%u", idx);
1439 ghostpad = gst_ghost_pad_new (name, pad);
1440 gst_pad_set_active (ghostpad, TRUE);
1441 gst_element_add_pad (priv->element, ghostpad);
1444 stream = gst_rtsp_stream_new (idx, payloader, ghostpad);
1446 gst_rtsp_stream_set_address_pool (stream, priv->pool);
1447 gst_rtsp_stream_set_profiles (stream, priv->profiles);
1448 gst_rtsp_stream_set_protocols (stream, priv->protocols);
1449 gst_rtsp_stream_set_retransmission_time (stream, priv->rtx_time);
1451 g_ptr_array_add (priv->streams, stream);
1453 if (GST_PAD_IS_SRC (pad)) {
1457 g_list_free (priv->payloads);
1458 priv->payloads = _find_payload_types (media);
1460 n = priv->streams->len;
1461 for (i = 0; i < n; i++) {
1462 GstRTSPStream *stream = g_ptr_array_index (priv->streams, i);
1463 guint rtx_pt = _next_available_pt (priv->payloads);
1466 GST_WARNING ("Ran out of space of dynamic payload types");
1470 gst_rtsp_stream_set_retransmission_pt (stream, rtx_pt);
1473 g_list_append (priv->payloads, GUINT_TO_POINTER (rtx_pt));
1476 g_mutex_unlock (&priv->lock);
1478 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STREAM], 0, stream,
1485 gst_rtsp_media_remove_stream (GstRTSPMedia * media, GstRTSPStream * stream)
1487 GstRTSPMediaPrivate *priv;
1492 g_mutex_lock (&priv->lock);
1493 /* remove the ghostpad */
1494 srcpad = gst_rtsp_stream_get_srcpad (stream);
1495 gst_element_remove_pad (priv->element, srcpad);
1496 gst_object_unref (srcpad);
1497 /* now remove the stream */
1498 g_object_ref (stream);
1499 g_ptr_array_remove (priv->streams, stream);
1500 g_mutex_unlock (&priv->lock);
1502 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_REMOVED_STREAM], 0,
1505 g_object_unref (stream);
1509 * gst_rtsp_media_n_streams:
1510 * @media: a #GstRTSPMedia
1512 * Get the number of streams in this media.
1514 * Returns: The number of streams.
1517 gst_rtsp_media_n_streams (GstRTSPMedia * media)
1519 GstRTSPMediaPrivate *priv;
1522 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), 0);
1526 g_mutex_lock (&priv->lock);
1527 res = priv->streams->len;
1528 g_mutex_unlock (&priv->lock);
1534 * gst_rtsp_media_get_stream:
1535 * @media: a #GstRTSPMedia
1536 * @idx: the stream index
1538 * Retrieve the stream with index @idx from @media.
1540 * Returns: (nullable) (transfer none): the #GstRTSPStream at index
1541 * @idx or %NULL when a stream with that index did not exist.
1544 gst_rtsp_media_get_stream (GstRTSPMedia * media, guint idx)
1546 GstRTSPMediaPrivate *priv;
1549 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1553 g_mutex_lock (&priv->lock);
1554 if (idx < priv->streams->len)
1555 res = g_ptr_array_index (priv->streams, idx);
1558 g_mutex_unlock (&priv->lock);
1564 * gst_rtsp_media_find_stream:
1565 * @media: a #GstRTSPMedia
1566 * @control: the control of the stream
1568 * Find a stream in @media with @control as the control uri.
1570 * Returns: (nullable) (transfer none): the #GstRTSPStream with
1571 * control uri @control or %NULL when a stream with that control did
1575 gst_rtsp_media_find_stream (GstRTSPMedia * media, const gchar * control)
1577 GstRTSPMediaPrivate *priv;
1581 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1582 g_return_val_if_fail (control != NULL, NULL);
1588 g_mutex_lock (&priv->lock);
1589 for (i = 0; i < priv->streams->len; i++) {
1590 GstRTSPStream *test;
1592 test = g_ptr_array_index (priv->streams, i);
1593 if (gst_rtsp_stream_has_control (test, control)) {
1598 g_mutex_unlock (&priv->lock);
1603 /* called with state-lock */
1605 default_convert_range (GstRTSPMedia * media, GstRTSPTimeRange * range,
1606 GstRTSPRangeUnit unit)
1608 return gst_rtsp_range_convert_units (range, unit);
1612 * gst_rtsp_media_get_range_string:
1613 * @media: a #GstRTSPMedia
1614 * @play: for the PLAY request
1615 * @unit: the unit to use for the string
1617 * Get the current range as a string. @media must be prepared with
1618 * gst_rtsp_media_prepare ().
1620 * Returns: (transfer full): The range as a string, g_free() after usage.
1623 gst_rtsp_media_get_range_string (GstRTSPMedia * media, gboolean play,
1624 GstRTSPRangeUnit unit)
1626 GstRTSPMediaClass *klass;
1627 GstRTSPMediaPrivate *priv;
1629 GstRTSPTimeRange range;
1631 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1632 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1633 g_return_val_if_fail (klass->convert_range != NULL, FALSE);
1637 g_rec_mutex_lock (&priv->state_lock);
1638 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
1639 priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
1642 g_mutex_lock (&priv->lock);
1644 /* Update the range value with current position/duration */
1645 collect_media_stats (media);
1648 range = priv->range;
1650 if (!play && priv->n_active > 0) {
1651 range.min.type = GST_RTSP_TIME_NOW;
1652 range.min.seconds = -1;
1654 g_mutex_unlock (&priv->lock);
1655 g_rec_mutex_unlock (&priv->state_lock);
1657 if (!klass->convert_range (media, &range, unit))
1658 goto conversion_failed;
1660 result = gst_rtsp_range_to_string (&range);
1667 GST_WARNING ("media %p was not prepared", media);
1668 g_rec_mutex_unlock (&priv->state_lock);
1673 GST_WARNING ("range conversion to unit %d failed", unit);
1679 stream_update_blocked (GstRTSPStream * stream, GstRTSPMedia * media)
1681 gst_rtsp_stream_set_blocked (stream, media->priv->blocked);
1685 media_streams_set_blocked (GstRTSPMedia * media, gboolean blocked)
1687 GstRTSPMediaPrivate *priv = media->priv;
1689 GST_DEBUG ("media %p set blocked %d", media, blocked);
1690 priv->blocked = blocked;
1691 g_ptr_array_foreach (priv->streams, (GFunc) stream_update_blocked, media);
1695 gst_rtsp_media_set_status (GstRTSPMedia * media, GstRTSPMediaStatus status)
1697 GstRTSPMediaPrivate *priv = media->priv;
1699 g_mutex_lock (&priv->lock);
1700 priv->status = status;
1701 GST_DEBUG ("setting new status to %d", status);
1702 g_cond_broadcast (&priv->cond);
1703 g_mutex_unlock (&priv->lock);
1707 * gst_rtsp_media_get_status:
1708 * @media: a #GstRTSPMedia
1710 * Get the status of @media. When @media is busy preparing, this function waits
1711 * until @media is prepared or in error.
1713 * Returns: the status of @media.
1716 gst_rtsp_media_get_status (GstRTSPMedia * media)
1718 GstRTSPMediaPrivate *priv = media->priv;
1719 GstRTSPMediaStatus result;
1722 g_mutex_lock (&priv->lock);
1723 end_time = g_get_monotonic_time () + 20 * G_TIME_SPAN_SECOND;
1724 /* while we are preparing, wait */
1725 while (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING) {
1726 GST_DEBUG ("waiting for status change");
1727 if (!g_cond_wait_until (&priv->cond, &priv->lock, end_time)) {
1728 GST_DEBUG ("timeout, assuming error status");
1729 priv->status = GST_RTSP_MEDIA_STATUS_ERROR;
1732 /* could be success or error */
1733 result = priv->status;
1734 GST_DEBUG ("got status %d", result);
1735 g_mutex_unlock (&priv->lock);
1741 * gst_rtsp_media_seek:
1742 * @media: a #GstRTSPMedia
1743 * @range: (transfer none): a #GstRTSPTimeRange
1745 * Seek the pipeline of @media to @range. @media must be prepared with
1746 * gst_rtsp_media_prepare().
1748 * Returns: %TRUE on success.
1751 gst_rtsp_media_seek (GstRTSPMedia * media, GstRTSPTimeRange * range)
1753 GstRTSPMediaClass *klass;
1754 GstRTSPMediaPrivate *priv;
1756 GstClockTime start, stop;
1757 GstSeekType start_type, stop_type;
1760 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1762 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1763 g_return_val_if_fail (range != NULL, FALSE);
1764 g_return_val_if_fail (klass->convert_range != NULL, FALSE);
1768 g_rec_mutex_lock (&priv->state_lock);
1769 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
1772 /* Update the seekable state of the pipeline in case it changed */
1773 if (gst_rtsp_media_is_record (media)) {
1774 /* TODO: Seeking for RECORD? */
1775 priv->seekable = FALSE;
1777 query = gst_query_new_seeking (GST_FORMAT_TIME);
1778 if (gst_element_query (priv->pipeline, query)) {
1783 gst_query_parse_seeking (query, &format, &seekable, &start, &end);
1784 priv->seekable = seekable;
1786 gst_query_unref (query);
1789 if (!priv->seekable)
1792 start_type = stop_type = GST_SEEK_TYPE_NONE;
1794 if (!klass->convert_range (media, range, GST_RTSP_RANGE_NPT))
1796 gst_rtsp_range_get_times (range, &start, &stop);
1798 GST_INFO ("got %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1799 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1800 GST_INFO ("current %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1801 GST_TIME_ARGS (priv->range_start), GST_TIME_ARGS (priv->range_stop));
1803 if (start != GST_CLOCK_TIME_NONE)
1804 start_type = GST_SEEK_TYPE_SET;
1806 if (priv->range_stop == stop)
1807 stop = GST_CLOCK_TIME_NONE;
1808 else if (stop != GST_CLOCK_TIME_NONE)
1809 stop_type = GST_SEEK_TYPE_SET;
1811 if (start != GST_CLOCK_TIME_NONE || stop != GST_CLOCK_TIME_NONE) {
1814 GST_INFO ("seeking to %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1815 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1817 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARING);
1819 media_streams_set_blocked (media, TRUE);
1821 /* depends on the current playing state of the pipeline. We might need to
1822 * queue this until we get EOS. */
1823 flags = GST_SEEK_FLAG_FLUSH;
1825 /* if range start was not supplied we must continue from current position.
1826 * but since we're doing a flushing seek, let us query the current position
1827 * so we end up at exactly the same position after the seek. */
1828 if (range->min.type == GST_RTSP_TIME_END) { /* Yepp, that's right! */
1830 gboolean ret = FALSE;
1832 if (klass->query_position)
1833 ret = klass->query_position (media, &position);
1836 GST_WARNING ("position query failed");
1838 GST_DEBUG ("doing accurate seek to %" GST_TIME_FORMAT,
1839 GST_TIME_ARGS (position));
1841 start_type = GST_SEEK_TYPE_SET;
1842 flags |= GST_SEEK_FLAG_ACCURATE;
1845 /* only set keyframe flag when modifying start */
1846 if (start_type != GST_SEEK_TYPE_NONE)
1847 flags |= GST_SEEK_FLAG_KEY_UNIT;
1850 /* FIXME, we only do forwards playback, no trick modes yet */
1851 res = gst_element_seek (priv->pipeline, 1.0, GST_FORMAT_TIME,
1852 flags, start_type, start, stop_type, stop);
1854 /* and block for the seek to complete */
1855 GST_INFO ("done seeking %d", res);
1856 g_rec_mutex_unlock (&priv->state_lock);
1858 /* wait until pipeline is prerolled again, this will also collect stats */
1859 if (!wait_preroll (media))
1860 goto preroll_failed;
1862 g_rec_mutex_lock (&priv->state_lock);
1863 GST_INFO ("prerolled again");
1865 GST_INFO ("no seek needed");
1868 g_rec_mutex_unlock (&priv->state_lock);
1875 g_rec_mutex_unlock (&priv->state_lock);
1876 GST_INFO ("media %p is not prepared", media);
1881 g_rec_mutex_unlock (&priv->state_lock);
1882 GST_INFO ("pipeline is not seekable");
1887 g_rec_mutex_unlock (&priv->state_lock);
1888 GST_WARNING ("conversion to npt not supported");
1893 GST_WARNING ("failed to preroll after seek");
1899 stream_collect_blocking (GstRTSPStream * stream, gboolean * blocked)
1901 *blocked &= gst_rtsp_stream_is_blocking (stream);
1905 media_streams_blocking (GstRTSPMedia * media)
1907 gboolean blocking = TRUE;
1909 g_ptr_array_foreach (media->priv->streams, (GFunc) stream_collect_blocking,
1915 static GstStateChangeReturn
1916 set_state (GstRTSPMedia * media, GstState state)
1918 GstRTSPMediaPrivate *priv = media->priv;
1919 GstStateChangeReturn ret;
1921 GST_INFO ("set state to %s for media %p", gst_element_state_get_name (state),
1923 ret = gst_element_set_state (priv->pipeline, state);
1928 static GstStateChangeReturn
1929 set_target_state (GstRTSPMedia * media, GstState state, gboolean do_state)
1931 GstRTSPMediaPrivate *priv = media->priv;
1932 GstStateChangeReturn ret;
1934 GST_INFO ("set target state to %s for media %p",
1935 gst_element_state_get_name (state), media);
1936 priv->target_state = state;
1938 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_TARGET_STATE], 0,
1939 priv->target_state, NULL);
1942 ret = set_state (media, state);
1944 ret = GST_STATE_CHANGE_SUCCESS;
1949 /* called with state-lock */
1951 default_handle_message (GstRTSPMedia * media, GstMessage * message)
1953 GstRTSPMediaPrivate *priv = media->priv;
1954 GstMessageType type;
1956 type = GST_MESSAGE_TYPE (message);
1959 case GST_MESSAGE_STATE_CHANGED:
1961 GstState old, new, pending;
1963 if (GST_MESSAGE_SRC (message) != GST_OBJECT (priv->pipeline))
1966 gst_message_parse_state_changed (message, &old, &new, &pending);
1968 GST_DEBUG ("%p: went from %s to %s (pending %s)", media,
1969 gst_element_state_get_name (old), gst_element_state_get_name (new),
1970 gst_element_state_get_name (pending));
1971 if (gst_rtsp_media_is_record (media)
1972 && old == GST_STATE_READY && new == GST_STATE_PAUSED) {
1973 GST_INFO ("%p: went to PAUSED, prepared now", media);
1974 collect_media_stats (media);
1976 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
1977 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
1982 case GST_MESSAGE_BUFFERING:
1986 gst_message_parse_buffering (message, &percent);
1988 /* no state management needed for live pipelines */
1992 if (percent == 100) {
1993 /* a 100% message means buffering is done */
1994 priv->buffering = FALSE;
1995 /* if the desired state is playing, go back */
1996 if (priv->target_state == GST_STATE_PLAYING) {
1997 GST_INFO ("Buffering done, setting pipeline to PLAYING");
1998 set_state (media, GST_STATE_PLAYING);
2000 GST_INFO ("Buffering done");
2003 /* buffering busy */
2004 if (priv->buffering == FALSE) {
2005 if (priv->target_state == GST_STATE_PLAYING) {
2006 /* we were not buffering but PLAYING, PAUSE the pipeline. */
2007 GST_INFO ("Buffering, setting pipeline to PAUSED ...");
2008 set_state (media, GST_STATE_PAUSED);
2010 GST_INFO ("Buffering ...");
2013 priv->buffering = TRUE;
2017 case GST_MESSAGE_LATENCY:
2019 gst_bin_recalculate_latency (GST_BIN_CAST (priv->pipeline));
2022 case GST_MESSAGE_ERROR:
2027 gst_message_parse_error (message, &gerror, &debug);
2028 GST_WARNING ("%p: got error %s (%s)", media, gerror->message, debug);
2029 g_error_free (gerror);
2032 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2035 case GST_MESSAGE_WARNING:
2040 gst_message_parse_warning (message, &gerror, &debug);
2041 GST_WARNING ("%p: got warning %s (%s)", media, gerror->message, debug);
2042 g_error_free (gerror);
2046 case GST_MESSAGE_ELEMENT:
2048 const GstStructure *s;
2050 s = gst_message_get_structure (message);
2051 if (gst_structure_has_name (s, "GstRTSPStreamBlocking")) {
2052 GST_DEBUG ("media received blocking message");
2053 if (priv->blocked && media_streams_blocking (media)) {
2054 GST_DEBUG ("media is blocking");
2055 collect_media_stats (media);
2057 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
2058 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
2063 case GST_MESSAGE_STREAM_STATUS:
2065 case GST_MESSAGE_ASYNC_DONE:
2067 /* when we are dynamically adding pads, the addition of the udpsrc will
2068 * temporarily produce ASYNC_DONE messages. We have to ignore them and
2069 * wait for the final ASYNC_DONE after everything prerolled */
2070 GST_INFO ("%p: ignoring ASYNC_DONE", media);
2072 GST_INFO ("%p: got ASYNC_DONE", media);
2073 collect_media_stats (media);
2075 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
2076 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
2079 case GST_MESSAGE_EOS:
2080 GST_INFO ("%p: got EOS", media);
2082 if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARING) {
2083 GST_DEBUG ("shutting down after EOS");
2084 finish_unprepare (media);
2088 GST_INFO ("%p: got message type %d (%s)", media, type,
2089 gst_message_type_get_name (type));
2096 bus_message (GstBus * bus, GstMessage * message, GstRTSPMedia * media)
2098 GstRTSPMediaPrivate *priv = media->priv;
2099 GstRTSPMediaClass *klass;
2102 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2104 g_rec_mutex_lock (&priv->state_lock);
2105 if (klass->handle_message)
2106 ret = klass->handle_message (media, message);
2109 g_rec_mutex_unlock (&priv->state_lock);
2115 watch_destroyed (GstRTSPMedia * media)
2117 GST_DEBUG_OBJECT (media, "source destroyed");
2118 g_object_unref (media);
2122 find_payload_element (GstElement * payloader)
2124 GstElement *pay = NULL;
2126 if (GST_IS_BIN (payloader)) {
2128 GValue item = { 0 };
2130 iter = gst_bin_iterate_recurse (GST_BIN (payloader));
2131 while (gst_iterator_next (iter, &item) == GST_ITERATOR_OK) {
2132 GstElement *element = (GstElement *) g_value_get_object (&item);
2133 GstElementClass *eclass = GST_ELEMENT_GET_CLASS (element);
2137 gst_element_class_get_metadata (eclass, GST_ELEMENT_METADATA_KLASS);
2141 if (strstr (klass, "Payloader") && strstr (klass, "RTP")) {
2142 pay = gst_object_ref (element);
2143 g_value_unset (&item);
2146 g_value_unset (&item);
2148 gst_iterator_free (iter);
2150 pay = g_object_ref (payloader);
2156 /* called from streaming threads */
2158 pad_added_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
2160 GstRTSPMediaPrivate *priv = media->priv;
2161 GstRTSPStream *stream;
2164 /* find the real payload element */
2165 pay = find_payload_element (element);
2166 stream = gst_rtsp_media_create_stream (media, pay, pad);
2167 gst_object_unref (pay);
2169 GST_INFO ("pad added %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
2171 g_rec_mutex_lock (&priv->state_lock);
2172 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARING)
2175 g_object_set_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream", stream);
2177 /* we will be adding elements below that will cause ASYNC_DONE to be
2178 * posted in the bus. We want to ignore those messages until the
2179 * pipeline really prerolled. */
2180 priv->adding = TRUE;
2182 /* join the element in the PAUSED state because this callback is
2183 * called from the streaming thread and it is PAUSED */
2184 if (!gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
2185 priv->rtpbin, GST_STATE_PAUSED)) {
2186 GST_WARNING ("failed to join bin element");
2189 priv->adding = FALSE;
2190 g_rec_mutex_unlock (&priv->state_lock);
2197 gst_rtsp_media_remove_stream (media, stream);
2198 g_rec_mutex_unlock (&priv->state_lock);
2199 GST_INFO ("ignore pad because we are not preparing");
2205 pad_removed_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
2207 GstRTSPMediaPrivate *priv = media->priv;
2208 GstRTSPStream *stream;
2210 stream = g_object_get_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream");
2214 GST_INFO ("pad removed %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
2216 g_rec_mutex_lock (&priv->state_lock);
2217 gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
2218 g_rec_mutex_unlock (&priv->state_lock);
2220 gst_rtsp_media_remove_stream (media, stream);
2224 remove_fakesink (GstRTSPMediaPrivate * priv)
2226 GstElement *fakesink;
2228 g_mutex_lock (&priv->lock);
2229 if ((fakesink = priv->fakesink))
2230 gst_object_ref (fakesink);
2231 priv->fakesink = NULL;
2232 g_mutex_unlock (&priv->lock);
2235 gst_bin_remove (GST_BIN (priv->pipeline), fakesink);
2236 gst_element_set_state (fakesink, GST_STATE_NULL);
2237 gst_object_unref (fakesink);
2238 GST_INFO ("removed fakesink");
2243 no_more_pads_cb (GstElement * element, GstRTSPMedia * media)
2245 GstRTSPMediaPrivate *priv = media->priv;
2247 GST_INFO ("no more pads");
2248 remove_fakesink (priv);
2251 typedef struct _DynPaySignalHandlers DynPaySignalHandlers;
2253 struct _DynPaySignalHandlers
2255 gulong pad_added_handler;
2256 gulong pad_removed_handler;
2257 gulong no_more_pads_handler;
2261 start_preroll (GstRTSPMedia * media)
2263 GstRTSPMediaPrivate *priv = media->priv;
2264 GstStateChangeReturn ret;
2266 GST_INFO ("setting pipeline to PAUSED for media %p", media);
2267 /* first go to PAUSED */
2268 ret = set_target_state (media, GST_STATE_PAUSED, TRUE);
2271 case GST_STATE_CHANGE_SUCCESS:
2272 GST_INFO ("SUCCESS state change for media %p", media);
2273 priv->seekable = TRUE;
2275 case GST_STATE_CHANGE_ASYNC:
2276 GST_INFO ("ASYNC state change for media %p", media);
2277 priv->seekable = TRUE;
2279 case GST_STATE_CHANGE_NO_PREROLL:
2280 /* we need to go to PLAYING */
2281 GST_INFO ("NO_PREROLL state change: live media %p", media);
2282 /* FIXME we disable seeking for live streams for now. We should perform a
2283 * seeking query in preroll instead */
2284 priv->seekable = FALSE;
2285 priv->is_live = TRUE;
2286 if (!gst_rtsp_media_is_record (media)) {
2287 /* start blocked to make sure nothing goes to the sink */
2288 media_streams_set_blocked (media, TRUE);
2290 ret = set_state (media, GST_STATE_PLAYING);
2291 if (ret == GST_STATE_CHANGE_FAILURE)
2294 case GST_STATE_CHANGE_FAILURE:
2302 GST_WARNING ("failed to preroll pipeline");
2308 wait_preroll (GstRTSPMedia * media)
2310 GstRTSPMediaStatus status;
2312 GST_DEBUG ("wait to preroll pipeline");
2314 /* wait until pipeline is prerolled */
2315 status = gst_rtsp_media_get_status (media);
2316 if (status == GST_RTSP_MEDIA_STATUS_ERROR)
2317 goto preroll_failed;
2323 GST_WARNING ("failed to preroll pipeline");
2329 start_prepare (GstRTSPMedia * media)
2331 GstRTSPMediaPrivate *priv = media->priv;
2335 /* link streams we already have, other streams might appear when we have
2336 * dynamic elements */
2337 for (i = 0; i < priv->streams->len; i++) {
2338 GstRTSPStream *stream;
2340 stream = g_ptr_array_index (priv->streams, i);
2342 if (!gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
2343 priv->rtpbin, GST_STATE_NULL)) {
2344 goto join_bin_failed;
2349 g_object_set (priv->rtpbin, "do-retransmission", priv->rtx_time > 0, NULL);
2351 for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
2352 GstElement *elem = walk->data;
2353 DynPaySignalHandlers *handlers = g_slice_new (DynPaySignalHandlers);
2355 GST_INFO ("adding callbacks for dynamic element %p", elem);
2357 handlers->pad_added_handler = g_signal_connect (elem, "pad-added",
2358 (GCallback) pad_added_cb, media);
2359 handlers->pad_removed_handler = g_signal_connect (elem, "pad-removed",
2360 (GCallback) pad_removed_cb, media);
2361 handlers->no_more_pads_handler = g_signal_connect (elem, "no-more-pads",
2362 (GCallback) no_more_pads_cb, media);
2364 g_object_set_data (G_OBJECT (elem), "gst-rtsp-dynpay-handlers", handlers);
2366 /* we add a fakesink here in order to make the state change async. We remove
2367 * the fakesink again in the no-more-pads callback. */
2368 priv->fakesink = gst_element_factory_make ("fakesink", "fakesink");
2369 gst_bin_add (GST_BIN (priv->pipeline), priv->fakesink);
2372 if (!start_preroll (media))
2373 goto preroll_failed;
2379 GST_WARNING ("failed to join bin element");
2380 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2385 GST_WARNING ("failed to preroll pipeline");
2386 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2392 default_prepare (GstRTSPMedia * media, GstRTSPThread * thread)
2394 GstRTSPMediaPrivate *priv;
2395 GstRTSPMediaClass *klass;
2397 GMainContext *context;
2402 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2404 if (!klass->create_rtpbin)
2405 goto no_create_rtpbin;
2407 priv->rtpbin = klass->create_rtpbin (media);
2408 if (priv->rtpbin != NULL) {
2409 gboolean success = TRUE;
2411 if (klass->setup_rtpbin)
2412 success = klass->setup_rtpbin (media, priv->rtpbin);
2414 if (success == FALSE) {
2415 gst_object_unref (priv->rtpbin);
2416 priv->rtpbin = NULL;
2419 if (priv->rtpbin == NULL)
2422 priv->thread = thread;
2423 context = (thread != NULL) ? (thread->context) : NULL;
2425 bus = gst_pipeline_get_bus (GST_PIPELINE_CAST (priv->pipeline));
2427 /* add the pipeline bus to our custom mainloop */
2428 priv->source = gst_bus_create_watch (bus);
2429 gst_object_unref (bus);
2431 g_source_set_callback (priv->source, (GSourceFunc) bus_message,
2432 g_object_ref (media), (GDestroyNotify) watch_destroyed);
2434 priv->id = g_source_attach (priv->source, context);
2436 /* add stuff to the bin */
2437 gst_bin_add (GST_BIN (priv->pipeline), priv->rtpbin);
2439 /* do remainder in context */
2440 source = g_idle_source_new ();
2441 g_source_set_callback (source, (GSourceFunc) start_prepare, media, NULL);
2442 g_source_attach (source, context);
2443 g_source_unref (source);
2450 GST_ERROR ("no create_rtpbin function");
2451 g_critical ("no create_rtpbin vmethod function set");
2456 GST_WARNING ("no rtpbin element");
2457 g_warning ("failed to create element 'rtpbin', check your installation");
2463 * gst_rtsp_media_prepare:
2464 * @media: a #GstRTSPMedia
2465 * @thread: (transfer full) (allow-none): a #GstRTSPThread to run the
2466 * bus handler or %NULL
2468 * Prepare @media for streaming. This function will create the objects
2469 * to manage the streaming. A pipeline must have been set on @media with
2470 * gst_rtsp_media_take_pipeline().
2472 * It will preroll the pipeline and collect vital information about the streams
2473 * such as the duration.
2475 * Returns: %TRUE on success.
2478 gst_rtsp_media_prepare (GstRTSPMedia * media, GstRTSPThread * thread)
2480 GstRTSPMediaPrivate *priv;
2481 GstRTSPMediaClass *klass;
2483 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2487 g_rec_mutex_lock (&priv->state_lock);
2488 priv->prepare_count++;
2490 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED ||
2491 priv->status == GST_RTSP_MEDIA_STATUS_SUSPENDED)
2494 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
2497 if (priv->status != GST_RTSP_MEDIA_STATUS_UNPREPARED)
2498 goto not_unprepared;
2500 if (!priv->reusable && priv->reused)
2503 GST_INFO ("preparing media %p", media);
2505 /* reset some variables */
2506 priv->is_live = FALSE;
2507 priv->seekable = FALSE;
2508 priv->buffering = FALSE;
2510 /* we're preparing now */
2511 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARING);
2513 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2514 if (klass->prepare) {
2515 if (!klass->prepare (media, thread))
2516 goto prepare_failed;
2520 g_rec_mutex_unlock (&priv->state_lock);
2522 /* now wait for all pads to be prerolled, FIXME, we should somehow be
2523 * able to do this async so that we don't block the server thread. */
2524 if (!wait_preroll (media))
2525 goto preroll_failed;
2527 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_PREPARED], 0, NULL);
2529 GST_INFO ("object %p is prerolled", media);
2536 /* we are not going to use the giving thread, so stop it. */
2538 gst_rtsp_thread_stop (thread);
2543 GST_LOG ("media %p was prepared", media);
2544 /* we are not going to use the giving thread, so stop it. */
2546 gst_rtsp_thread_stop (thread);
2547 g_rec_mutex_unlock (&priv->state_lock);
2553 /* we are not going to use the giving thread, so stop it. */
2555 gst_rtsp_thread_stop (thread);
2556 GST_WARNING ("media %p was not unprepared", media);
2557 priv->prepare_count--;
2558 g_rec_mutex_unlock (&priv->state_lock);
2563 /* we are not going to use the giving thread, so stop it. */
2565 gst_rtsp_thread_stop (thread);
2566 priv->prepare_count--;
2567 g_rec_mutex_unlock (&priv->state_lock);
2568 GST_WARNING ("can not reuse media %p", media);
2573 /* we are not going to use the giving thread, so stop it. */
2575 gst_rtsp_thread_stop (thread);
2576 priv->prepare_count--;
2577 g_rec_mutex_unlock (&priv->state_lock);
2578 GST_ERROR ("failed to prepare media");
2583 GST_WARNING ("failed to preroll pipeline");
2584 gst_rtsp_media_unprepare (media);
2589 /* must be called with state-lock */
2591 finish_unprepare (GstRTSPMedia * media)
2593 GstRTSPMediaPrivate *priv = media->priv;
2597 GST_DEBUG ("shutting down");
2599 /* release the lock on shutdown, otherwise pad_added_cb might try to
2600 * acquire the lock and then we deadlock */
2601 g_rec_mutex_unlock (&priv->state_lock);
2602 set_state (media, GST_STATE_NULL);
2603 g_rec_mutex_lock (&priv->state_lock);
2604 remove_fakesink (priv);
2606 for (i = 0; i < priv->streams->len; i++) {
2607 GstRTSPStream *stream;
2609 GST_INFO ("Removing elements of stream %d from pipeline", i);
2611 stream = g_ptr_array_index (priv->streams, i);
2613 gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
2616 /* remove the pad signal handlers */
2617 for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
2618 GstElement *elem = walk->data;
2619 DynPaySignalHandlers *handlers;
2622 g_object_steal_data (G_OBJECT (elem), "gst-rtsp-dynpay-handlers");
2623 g_assert (handlers != NULL);
2625 g_signal_handler_disconnect (G_OBJECT (elem), handlers->pad_added_handler);
2626 g_signal_handler_disconnect (G_OBJECT (elem),
2627 handlers->pad_removed_handler);
2628 g_signal_handler_disconnect (G_OBJECT (elem),
2629 handlers->no_more_pads_handler);
2631 g_slice_free (DynPaySignalHandlers, handlers);
2634 gst_bin_remove (GST_BIN (priv->pipeline), priv->rtpbin);
2635 priv->rtpbin = NULL;
2638 gst_object_unref (priv->nettime);
2639 priv->nettime = NULL;
2641 priv->reused = TRUE;
2642 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_UNPREPARED);
2644 /* when the media is not reusable, this will effectively unref the media and
2646 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_UNPREPARED], 0, NULL);
2648 /* the source has the last ref to the media */
2650 GST_DEBUG ("destroy source");
2651 g_source_destroy (priv->source);
2652 g_source_unref (priv->source);
2655 GST_DEBUG ("stop thread");
2656 gst_rtsp_thread_stop (priv->thread);
2660 /* called with state-lock */
2662 default_unprepare (GstRTSPMedia * media)
2664 GstRTSPMediaPrivate *priv = media->priv;
2666 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_UNPREPARING);
2668 if (priv->eos_shutdown) {
2669 GST_DEBUG ("sending EOS for shutdown");
2670 /* ref so that we don't disappear */
2671 gst_element_send_event (priv->pipeline, gst_event_new_eos ());
2672 /* we need to go to playing again for the EOS to propagate, normally in this
2673 * state, nothing is receiving data from us anymore so this is ok. */
2674 set_state (media, GST_STATE_PLAYING);
2676 finish_unprepare (media);
2682 * gst_rtsp_media_unprepare:
2683 * @media: a #GstRTSPMedia
2685 * Unprepare @media. After this call, the media should be prepared again before
2686 * it can be used again. If the media is set to be non-reusable, a new instance
2689 * Returns: %TRUE on success.
2692 gst_rtsp_media_unprepare (GstRTSPMedia * media)
2694 GstRTSPMediaPrivate *priv;
2697 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2701 g_rec_mutex_lock (&priv->state_lock);
2702 if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARED)
2703 goto was_unprepared;
2705 priv->prepare_count--;
2706 if (priv->prepare_count > 0)
2709 GST_INFO ("unprepare media %p", media);
2711 media_streams_set_blocked (media, FALSE);
2712 set_target_state (media, GST_STATE_NULL, FALSE);
2715 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED) {
2716 GstRTSPMediaClass *klass;
2718 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2719 if (klass->unprepare)
2720 success = klass->unprepare (media);
2722 finish_unprepare (media);
2724 g_rec_mutex_unlock (&priv->state_lock);
2730 g_rec_mutex_unlock (&priv->state_lock);
2731 GST_INFO ("media %p was already unprepared", media);
2736 GST_INFO ("media %p still prepared %d times", media, priv->prepare_count);
2737 g_rec_mutex_unlock (&priv->state_lock);
2742 /* should be called with state-lock */
2744 get_clock_unlocked (GstRTSPMedia * media)
2746 if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED) {
2747 GST_DEBUG_OBJECT (media, "media was not prepared");
2750 return gst_pipeline_get_clock (GST_PIPELINE_CAST (media->priv->pipeline));
2754 * gst_rtsp_media_get_clock:
2755 * @media: a #GstRTSPMedia
2757 * Get the clock that is used by the pipeline in @media.
2759 * @media must be prepared before this method returns a valid clock object.
2761 * Returns: (transfer full): the #GstClock used by @media. unref after usage.
2764 gst_rtsp_media_get_clock (GstRTSPMedia * media)
2767 GstRTSPMediaPrivate *priv;
2769 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
2773 g_rec_mutex_lock (&priv->state_lock);
2774 clock = get_clock_unlocked (media);
2775 g_rec_mutex_unlock (&priv->state_lock);
2781 * gst_rtsp_media_get_base_time:
2782 * @media: a #GstRTSPMedia
2784 * Get the base_time that is used by the pipeline in @media.
2786 * @media must be prepared before this method returns a valid base_time.
2788 * Returns: the base_time used by @media.
2791 gst_rtsp_media_get_base_time (GstRTSPMedia * media)
2793 GstClockTime result;
2794 GstRTSPMediaPrivate *priv;
2796 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_CLOCK_TIME_NONE);
2800 g_rec_mutex_lock (&priv->state_lock);
2801 if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
2804 result = gst_element_get_base_time (media->priv->pipeline);
2805 g_rec_mutex_unlock (&priv->state_lock);
2812 g_rec_mutex_unlock (&priv->state_lock);
2813 GST_DEBUG_OBJECT (media, "media was not prepared");
2814 return GST_CLOCK_TIME_NONE;
2819 * gst_rtsp_media_get_time_provider:
2820 * @media: a #GstRTSPMedia
2821 * @address: (allow-none): an address or %NULL
2822 * @port: a port or 0
2824 * Get the #GstNetTimeProvider for the clock used by @media. The time provider
2825 * will listen on @address and @port for client time requests.
2827 * Returns: (transfer full): the #GstNetTimeProvider of @media.
2829 GstNetTimeProvider *
2830 gst_rtsp_media_get_time_provider (GstRTSPMedia * media, const gchar * address,
2833 GstRTSPMediaPrivate *priv;
2834 GstNetTimeProvider *provider = NULL;
2836 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
2840 g_rec_mutex_lock (&priv->state_lock);
2841 if (priv->time_provider) {
2842 if ((provider = priv->nettime) == NULL) {
2845 if (priv->time_provider && (clock = get_clock_unlocked (media))) {
2846 provider = gst_net_time_provider_new (clock, address, port);
2847 gst_object_unref (clock);
2849 priv->nettime = provider;
2853 g_rec_mutex_unlock (&priv->state_lock);
2856 gst_object_ref (provider);
2862 default_setup_sdp (GstRTSPMedia * media, GstSDPMessage * sdp, GstSDPInfo * info)
2864 return gst_rtsp_sdp_from_media (sdp, info, media);
2868 * gst_rtsp_media_setup_sdp:
2869 * @media: a #GstRTSPMedia
2870 * @sdp: (transfer none): a #GstSDPMessage
2871 * @info: (transfer none): a #GstSDPInfo
2873 * Add @media specific info to @sdp. @info is used to configure the connection
2874 * information in the SDP.
2876 * Returns: TRUE on success.
2879 gst_rtsp_media_setup_sdp (GstRTSPMedia * media, GstSDPMessage * sdp,
2882 GstRTSPMediaPrivate *priv;
2883 GstRTSPMediaClass *klass;
2886 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2887 g_return_val_if_fail (sdp != NULL, FALSE);
2888 g_return_val_if_fail (info != NULL, FALSE);
2892 g_rec_mutex_lock (&priv->state_lock);
2894 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2896 if (!klass->setup_sdp)
2899 res = klass->setup_sdp (media, sdp, info);
2901 g_rec_mutex_unlock (&priv->state_lock);
2908 g_rec_mutex_unlock (&priv->state_lock);
2909 GST_ERROR ("no setup_sdp function");
2910 g_critical ("no setup_sdp vmethod function set");
2915 static const gchar *
2916 rtsp_get_attribute_for_pt (const GstSDPMedia * media, const gchar * name,
2925 if ((attr = gst_sdp_media_get_attribute_val_n (media, name, i)) == NULL)
2928 if (sscanf (attr, "%d ", &val) != 1)
2937 #define PARSE_INT(p, del, res) \
2940 p = strstr (p, del); \
2950 #define PARSE_STRING(p, del, res) \
2953 p = strstr (p, del); \
2965 #define SKIP_SPACES(p) \
2966 while (*p && g_ascii_isspace (*p)) \
2971 * <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
2974 parse_rtpmap (const gchar * rtpmap, gint * payload, gchar ** name,
2975 gint * rate, gchar ** params)
2979 p = (gchar *) rtpmap;
2981 PARSE_INT (p, " ", *payload);
2989 PARSE_STRING (p, "/", *name);
2990 if (*name == NULL) {
2991 GST_DEBUG ("no rate, name %s", p);
2992 /* no rate, assume -1 then, this is not supposed to happen but RealMedia
2993 * streams seem to omit the rate. */
3000 p = strstr (p, "/");
3018 * Mapping of caps to and from SDP fields:
3020 * a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
3021 * a=fmtp:<payload> <param>[=<value>];...
3024 media_to_caps (gint pt, const GstSDPMedia * media)
3027 const gchar *rtpmap;
3031 gchar *params = NULL;
3037 /* get and parse rtpmap */
3038 rtpmap = rtsp_get_attribute_for_pt (media, "rtpmap", pt);
3041 ret = parse_rtpmap (rtpmap, &payload, &name, &rate, ¶ms);
3043 g_warning ("error parsing rtpmap, ignoring");
3047 /* dynamic payloads need rtpmap or we fail */
3048 if (rtpmap == NULL && pt >= 96)
3051 /* check if we have a rate, if not, we need to look up the rate from the
3052 * default rates based on the payload types. */
3054 const GstRTPPayloadInfo *info;
3056 if (GST_RTP_PAYLOAD_IS_DYNAMIC (pt)) {
3057 /* dynamic types, use media and encoding_name */
3058 tmp = g_ascii_strdown (media->media, -1);
3059 info = gst_rtp_payload_info_for_name (tmp, name);
3062 /* static types, use payload type */
3063 info = gst_rtp_payload_info_for_pt (pt);
3067 if ((rate = info->clock_rate) == 0)
3070 /* we fail if we cannot find one */
3075 tmp = g_ascii_strdown (media->media, -1);
3076 caps = gst_caps_new_simple ("application/x-unknown",
3077 "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
3079 s = gst_caps_get_structure (caps, 0);
3081 gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);
3083 /* encoding name must be upper case */
3085 tmp = g_ascii_strup (name, -1);
3086 gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
3090 /* params must be lower case */
3091 if (params != NULL) {
3092 tmp = g_ascii_strdown (params, -1);
3093 gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
3097 /* parse optional fmtp: field */
3098 if ((fmtp = rtsp_get_attribute_for_pt (media, "fmtp", pt))) {
3104 /* p is now of the format <payload> <param>[=<value>];... */
3105 PARSE_INT (p, " ", payload);
3106 if (payload != -1 && payload == pt) {
3110 /* <param>[=<value>] are separated with ';' */
3111 pairs = g_strsplit (p, ";", 0);
3112 for (i = 0; pairs[i]; i++) {
3114 const gchar *val, *key;
3116 /* the key may not have a '=', the value can have other '='s */
3117 valpos = strstr (pairs[i], "=");
3119 /* we have a '=' and thus a value, remove the '=' with \0 */
3121 /* value is everything between '=' and ';'. We split the pairs at ;
3122 * boundaries so we can take the remainder of the value. Some servers
3123 * put spaces around the value which we strip off here. Alternatively
3124 * we could strip those spaces in the depayloaders should these spaces
3125 * actually carry any meaning in the future. */
3126 val = g_strstrip (valpos + 1);
3128 /* simple <param>;.. is translated into <param>=1;... */
3131 /* strip the key of spaces, convert key to lowercase but not the value. */
3132 key = g_strstrip (pairs[i]);
3133 if (strlen (key) > 1) {
3134 tmp = g_ascii_strdown (key, -1);
3135 gst_structure_set (s, tmp, G_TYPE_STRING, val, NULL);
3147 g_warning ("rtpmap type not given for dynamic payload %d", pt);
3152 g_warning ("rate unknown for payload type %d", pt);
3158 parse_keymgmt (const gchar * keymgmt, GstCaps * caps)
3160 gboolean res = FALSE;
3164 GstMIKEYMessage *msg;
3165 const GstMIKEYPayload *payload;
3166 const gchar *srtp_cipher;
3167 const gchar *srtp_auth;
3169 p = (gchar *) keymgmt;
3175 PARSE_STRING (p, " ", kmpid);
3176 if (!g_str_equal (kmpid, "mikey"))
3179 data = g_base64_decode (p, &size);
3183 msg = gst_mikey_message_new_from_data (data, size, NULL, NULL);
3188 srtp_cipher = "aes-128-icm";
3189 srtp_auth = "hmac-sha1-80";
3191 /* check the Security policy if any */
3192 if ((payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_SP, 0))) {
3193 GstMIKEYPayloadSP *p = (GstMIKEYPayloadSP *) payload;
3196 if (p->proto != GST_MIKEY_SEC_PROTO_SRTP)
3199 len = gst_mikey_payload_sp_get_n_params (payload);
3200 for (i = 0; i < len; i++) {
3201 const GstMIKEYPayloadSPParam *param =
3202 gst_mikey_payload_sp_get_param (payload, i);
3204 switch (param->type) {
3205 case GST_MIKEY_SP_SRTP_ENC_ALG:
3206 switch (param->val[0]) {
3208 srtp_cipher = "null";
3212 srtp_cipher = "aes-128-icm";
3218 case GST_MIKEY_SP_SRTP_ENC_KEY_LEN:
3219 switch (param->val[0]) {
3220 case AES_128_KEY_LEN:
3221 srtp_cipher = "aes-128-icm";
3223 case AES_256_KEY_LEN:
3224 srtp_cipher = "aes-256-icm";
3230 case GST_MIKEY_SP_SRTP_AUTH_ALG:
3231 switch (param->val[0]) {
3237 srtp_auth = "hmac-sha1-80";
3243 case GST_MIKEY_SP_SRTP_AUTH_KEY_LEN:
3244 switch (param->val[0]) {
3245 case HMAC_32_KEY_LEN:
3246 srtp_auth = "hmac-sha1-32";
3248 case HMAC_80_KEY_LEN:
3249 srtp_auth = "hmac-sha1-80";
3255 case GST_MIKEY_SP_SRTP_SRTP_ENC:
3257 case GST_MIKEY_SP_SRTP_SRTCP_ENC:
3265 if (!(payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_KEMAC, 0)))
3268 GstMIKEYPayloadKEMAC *p = (GstMIKEYPayloadKEMAC *) payload;
3269 const GstMIKEYPayload *sub;
3270 GstMIKEYPayloadKeyData *pkd;
3273 if (p->enc_alg != GST_MIKEY_ENC_NULL || p->mac_alg != GST_MIKEY_MAC_NULL)
3276 if (!(sub = gst_mikey_payload_kemac_get_sub (payload, 0)))
3279 if (sub->type != GST_MIKEY_PT_KEY_DATA)
3282 pkd = (GstMIKEYPayloadKeyData *) sub;
3284 gst_buffer_new_wrapped (g_memdup (pkd->key_data, pkd->key_len),
3286 gst_caps_set_simple (caps, "srtp-key", GST_TYPE_BUFFER, buf, NULL);
3289 gst_caps_set_simple (caps,
3290 "srtp-cipher", G_TYPE_STRING, srtp_cipher,
3291 "srtp-auth", G_TYPE_STRING, srtp_auth,
3292 "srtcp-cipher", G_TYPE_STRING, srtp_cipher,
3293 "srtcp-auth", G_TYPE_STRING, srtp_auth, NULL);
3297 gst_mikey_message_unref (msg);
3303 * Mapping SDP attributes to caps
3305 * prepend 'a-' to IANA registered sdp attributes names
3306 * (ie: not prefixed with 'x-') in order to avoid
3307 * collision with gstreamer standard caps properties names
3310 sdp_attributes_to_caps (GArray * attributes, GstCaps * caps)
3312 if (attributes->len > 0) {
3316 s = gst_caps_get_structure (caps, 0);
3318 for (i = 0; i < attributes->len; i++) {
3319 GstSDPAttribute *attr = &g_array_index (attributes, GstSDPAttribute, i);
3320 gchar *tofree, *key;
3324 /* skip some of the attribute we already handle */
3325 if (!strcmp (key, "fmtp"))
3327 if (!strcmp (key, "rtpmap"))
3329 if (!strcmp (key, "control"))
3331 if (!strcmp (key, "range"))
3333 if (g_str_equal (key, "key-mgmt")) {
3334 parse_keymgmt (attr->value, caps);
3338 /* string must be valid UTF8 */
3339 if (!g_utf8_validate (attr->value, -1, NULL))
3342 if (!g_str_has_prefix (key, "x-"))
3343 tofree = key = g_strdup_printf ("a-%s", key);
3347 GST_DEBUG ("adding caps: %s=%s", key, attr->value);
3348 gst_structure_set (s, key, G_TYPE_STRING, attr->value, NULL);
3355 default_handle_sdp (GstRTSPMedia * media, GstSDPMessage * sdp)
3357 GstRTSPMediaPrivate *priv = media->priv;
3360 medias_len = gst_sdp_message_medias_len (sdp);
3361 if (medias_len != priv->streams->len) {
3362 GST_ERROR ("%p: Media has more or less streams than SDP (%d /= %d)", media,
3363 priv->streams->len, medias_len);
3367 for (i = 0; i < medias_len; i++) {
3368 const gchar *proto, *media_type;
3369 const GstSDPMedia *sdp_media = gst_sdp_message_get_media (sdp, i);
3370 GstRTSPStream *stream;
3371 gint j, formats_len;
3372 const gchar *control;
3373 GstRTSPProfile profile, profiles;
3375 stream = g_ptr_array_index (priv->streams, i);
3377 /* TODO: Should we do something with the other SDP information? */
3380 proto = gst_sdp_media_get_proto (sdp_media);
3381 if (proto == NULL) {
3382 GST_ERROR ("%p: SDP media %d has no proto", media, i);
3386 if (g_str_equal (proto, "RTP/AVP")) {
3387 media_type = "application/x-rtp";
3388 profile = GST_RTSP_PROFILE_AVP;
3389 } else if (g_str_equal (proto, "RTP/SAVP")) {
3390 media_type = "application/x-srtp";
3391 profile = GST_RTSP_PROFILE_SAVP;
3392 } else if (g_str_equal (proto, "RTP/AVPF")) {
3393 media_type = "application/x-rtp";
3394 profile = GST_RTSP_PROFILE_AVPF;
3395 } else if (g_str_equal (proto, "RTP/SAVPF")) {
3396 media_type = "application/x-srtp";
3397 profile = GST_RTSP_PROFILE_SAVPF;
3399 GST_ERROR ("%p: unsupported profile '%s' for stream %d", media, proto, i);
3403 profiles = gst_rtsp_stream_get_profiles (stream);
3404 if ((profiles & profile) == 0) {
3405 GST_ERROR ("%p: unsupported profile '%s' for stream %d", media, proto, i);
3409 formats_len = gst_sdp_media_formats_len (sdp_media);
3410 for (j = 0; j < formats_len; j++) {
3415 pt = atoi (gst_sdp_media_get_format (sdp_media, j));
3417 GST_DEBUG (" looking at %d pt: %d", j, pt);
3420 caps = media_to_caps (pt, sdp_media);
3422 GST_WARNING (" skipping pt %d without caps", pt);
3426 /* do some tweaks */
3427 GST_DEBUG ("mapping sdp session level attributes to caps");
3428 sdp_attributes_to_caps (sdp->attributes, caps);
3429 GST_DEBUG ("mapping sdp media level attributes to caps");
3430 sdp_attributes_to_caps (sdp_media->attributes, caps);
3432 s = gst_caps_get_structure (caps, 0);
3433 gst_structure_set_name (s, media_type);
3435 gst_rtsp_stream_set_pt_map (stream, pt, caps);
3436 gst_caps_unref (caps);
3439 control = gst_sdp_media_get_attribute_val (sdp_media, "control");
3441 gst_rtsp_stream_set_control (stream, control);
3449 * gst_rtsp_media_handle_sdp:
3450 * @media: a #GstRTSPMedia
3451 * @sdp: (transfer none): a #GstSDPMessage
3453 * Configure an SDP on @media for receiving streams
3455 * Returns: TRUE on success.
3458 gst_rtsp_media_handle_sdp (GstRTSPMedia * media, GstSDPMessage * sdp)
3460 GstRTSPMediaPrivate *priv;
3461 GstRTSPMediaClass *klass;
3464 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
3465 g_return_val_if_fail (sdp != NULL, FALSE);
3469 g_rec_mutex_lock (&priv->state_lock);
3471 klass = GST_RTSP_MEDIA_GET_CLASS (media);
3473 if (!klass->handle_sdp)
3476 res = klass->handle_sdp (media, sdp);
3478 g_rec_mutex_unlock (&priv->state_lock);
3485 g_rec_mutex_unlock (&priv->state_lock);
3486 GST_ERROR ("no handle_sdp function");
3487 g_critical ("no handle_sdp vmethod function set");
3493 do_set_seqnum (GstRTSPStream * stream)
3496 seq_num = gst_rtsp_stream_get_current_seqnum (stream);
3497 gst_rtsp_stream_set_seqnum_offset (stream, seq_num + 1);
3500 /* call with state_lock */
3502 default_suspend (GstRTSPMedia * media)
3504 GstRTSPMediaPrivate *priv = media->priv;
3505 GstStateChangeReturn ret;
3507 switch (priv->suspend_mode) {
3508 case GST_RTSP_SUSPEND_MODE_NONE:
3509 GST_DEBUG ("media %p no suspend", media);
3511 case GST_RTSP_SUSPEND_MODE_PAUSE:
3512 GST_DEBUG ("media %p suspend to PAUSED", media);
3513 ret = set_target_state (media, GST_STATE_PAUSED, TRUE);
3514 if (ret == GST_STATE_CHANGE_FAILURE)
3517 case GST_RTSP_SUSPEND_MODE_RESET:
3518 GST_DEBUG ("media %p suspend to NULL", media);
3519 ret = set_target_state (media, GST_STATE_NULL, TRUE);
3520 if (ret == GST_STATE_CHANGE_FAILURE)
3522 /* Because payloader needs to set the sequence number as
3523 * monotonic, we need to preserve the sequence number
3524 * after pause. (otherwise going from pause to play, which
3525 * is actually from NULL to PLAY will create a new sequence
3527 g_ptr_array_foreach (priv->streams, (GFunc) do_set_seqnum, NULL);
3533 /* let the streams do the state changes freely, if any */
3534 media_streams_set_blocked (media, FALSE);
3541 GST_WARNING ("failed changing pipeline's state for media %p", media);
3547 * gst_rtsp_media_suspend:
3548 * @media: a #GstRTSPMedia
3550 * Suspend @media. The state of the pipeline managed by @media is set to
3551 * GST_STATE_NULL but all streams are kept. @media can be prepared again
3552 * with gst_rtsp_media_unsuspend()
3554 * @media must be prepared with gst_rtsp_media_prepare();
3556 * Returns: %TRUE on success.
3559 gst_rtsp_media_suspend (GstRTSPMedia * media)
3561 GstRTSPMediaPrivate *priv = media->priv;
3562 GstRTSPMediaClass *klass;
3564 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
3566 GST_FIXME ("suspend for dynamic pipelines needs fixing");
3568 g_rec_mutex_lock (&priv->state_lock);
3569 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
3572 /* don't attempt to suspend when something is busy */
3573 if (priv->n_active > 0)
3576 klass = GST_RTSP_MEDIA_GET_CLASS (media);
3577 if (klass->suspend) {
3578 if (!klass->suspend (media))
3579 goto suspend_failed;
3582 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_SUSPENDED);
3584 g_rec_mutex_unlock (&priv->state_lock);
3591 g_rec_mutex_unlock (&priv->state_lock);
3592 GST_WARNING ("media %p was not prepared", media);
3597 g_rec_mutex_unlock (&priv->state_lock);
3598 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
3599 GST_WARNING ("failed to suspend media %p", media);
3604 /* call with state_lock */
3606 default_unsuspend (GstRTSPMedia * media)
3608 GstRTSPMediaPrivate *priv = media->priv;
3610 switch (priv->suspend_mode) {
3611 case GST_RTSP_SUSPEND_MODE_NONE:
3612 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
3614 case GST_RTSP_SUSPEND_MODE_PAUSE:
3615 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
3617 case GST_RTSP_SUSPEND_MODE_RESET:
3619 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARING);
3620 if (!start_preroll (media))
3622 g_rec_mutex_unlock (&priv->state_lock);
3624 if (!wait_preroll (media))
3625 goto preroll_failed;
3627 g_rec_mutex_lock (&priv->state_lock);
3638 GST_WARNING ("failed to preroll pipeline");
3643 GST_WARNING ("failed to preroll pipeline");
3649 * gst_rtsp_media_unsuspend:
3650 * @media: a #GstRTSPMedia
3652 * Unsuspend @media if it was in a suspended state. This method does nothing
3653 * when the media was not in the suspended state.
3655 * Returns: %TRUE on success.
3658 gst_rtsp_media_unsuspend (GstRTSPMedia * media)
3660 GstRTSPMediaPrivate *priv = media->priv;
3661 GstRTSPMediaClass *klass;
3663 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
3665 g_rec_mutex_lock (&priv->state_lock);
3666 if (priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
3669 klass = GST_RTSP_MEDIA_GET_CLASS (media);
3670 if (klass->unsuspend) {
3671 if (!klass->unsuspend (media))
3672 goto unsuspend_failed;
3676 g_rec_mutex_unlock (&priv->state_lock);
3683 g_rec_mutex_unlock (&priv->state_lock);
3684 GST_WARNING ("failed to unsuspend media %p", media);
3685 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
3690 /* must be called with state-lock */
3692 media_set_pipeline_state_locked (GstRTSPMedia * media, GstState state)
3694 GstRTSPMediaPrivate *priv = media->priv;
3696 if (state == GST_STATE_NULL) {
3697 gst_rtsp_media_unprepare (media);
3699 GST_INFO ("state %s media %p", gst_element_state_get_name (state), media);
3700 set_target_state (media, state, FALSE);
3701 /* when we are buffering, don't update the state yet, this will be done
3702 * when buffering finishes */
3703 if (priv->buffering) {
3704 GST_INFO ("Buffering busy, delay state change");
3706 if (state == GST_STATE_PLAYING)
3707 /* make sure pads are not blocking anymore when going to PLAYING */
3708 media_streams_set_blocked (media, FALSE);
3710 set_state (media, state);
3712 /* and suspend after pause */
3713 if (state == GST_STATE_PAUSED)
3714 gst_rtsp_media_suspend (media);
3720 * gst_rtsp_media_set_pipeline_state:
3721 * @media: a #GstRTSPMedia
3722 * @state: the target state of the pipeline
3724 * Set the state of the pipeline managed by @media to @state
3727 gst_rtsp_media_set_pipeline_state (GstRTSPMedia * media, GstState state)
3729 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
3731 g_rec_mutex_lock (&media->priv->state_lock);
3732 media_set_pipeline_state_locked (media, state);
3733 g_rec_mutex_unlock (&media->priv->state_lock);
3737 * gst_rtsp_media_set_state:
3738 * @media: a #GstRTSPMedia
3739 * @state: the target state of the media
3740 * @transports: (transfer none) (element-type GstRtspServer.RTSPStreamTransport):
3741 * a #GPtrArray of #GstRTSPStreamTransport pointers
3743 * Set the state of @media to @state and for the transports in @transports.
3745 * @media must be prepared with gst_rtsp_media_prepare();
3747 * Returns: %TRUE on success.
3750 gst_rtsp_media_set_state (GstRTSPMedia * media, GstState state,
3751 GPtrArray * transports)
3753 GstRTSPMediaPrivate *priv;
3755 gboolean activate, deactivate, do_state;
3758 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
3759 g_return_val_if_fail (transports != NULL, FALSE);
3763 g_rec_mutex_lock (&priv->state_lock);
3764 if (priv->status == GST_RTSP_MEDIA_STATUS_ERROR)
3766 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
3767 priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
3770 /* NULL and READY are the same */
3771 if (state == GST_STATE_READY)
3772 state = GST_STATE_NULL;
3774 activate = deactivate = FALSE;
3776 GST_INFO ("going to state %s media %p, target state %s",
3777 gst_element_state_get_name (state), media,
3778 gst_element_state_get_name (priv->target_state));
3781 case GST_STATE_NULL:
3782 /* we're going from PLAYING or PAUSED to READY or NULL, deactivate */
3783 if (priv->target_state >= GST_STATE_PAUSED)
3786 case GST_STATE_PAUSED:
3787 /* we're going from PLAYING to PAUSED, READY or NULL, deactivate */
3788 if (priv->target_state == GST_STATE_PLAYING)
3791 case GST_STATE_PLAYING:
3792 /* we're going to PLAYING, activate */
3798 old_active = priv->n_active;
3800 GST_DEBUG ("%d transports, activate %d, deactivate %d", transports->len,
3801 activate, deactivate);
3802 for (i = 0; i < transports->len; i++) {
3803 GstRTSPStreamTransport *trans;
3805 /* we need a non-NULL entry in the array */
3806 trans = g_ptr_array_index (transports, i);
3811 if (gst_rtsp_stream_transport_set_active (trans, TRUE))
3813 } else if (deactivate) {
3814 if (gst_rtsp_stream_transport_set_active (trans, FALSE))
3819 /* we just activated the first media, do the playing state change */
3820 if (old_active == 0 && activate)
3822 /* if we have no more active media, do the downward state changes */
3823 else if (priv->n_active == 0)
3828 GST_INFO ("state %d active %d media %p do_state %d", state, priv->n_active,
3831 if (priv->target_state != state) {
3833 media_set_pipeline_state_locked (media, state);
3835 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STATE], 0, state,
3839 /* remember where we are */
3840 if (state != GST_STATE_NULL && (state == GST_STATE_PAUSED ||
3841 old_active != priv->n_active))
3842 collect_media_stats (media);
3844 g_rec_mutex_unlock (&priv->state_lock);
3851 GST_WARNING ("media %p was not prepared", media);
3852 g_rec_mutex_unlock (&priv->state_lock);
3857 GST_WARNING ("media %p in error status while changing to state %d",
3859 if (state == GST_STATE_NULL) {
3860 for (i = 0; i < transports->len; i++) {
3861 GstRTSPStreamTransport *trans;
3863 /* we need a non-NULL entry in the array */
3864 trans = g_ptr_array_index (transports, i);
3868 gst_rtsp_stream_transport_set_active (trans, FALSE);
3872 g_rec_mutex_unlock (&priv->state_lock);
3878 * gst_rtsp_media_set_record:
3879 * @media: a #GstRTSPMedia
3880 * @record: the new value
3882 * Set or unset if the pipeline for @media can be used for PLAY or RECORD
3886 gst_rtsp_media_set_record (GstRTSPMedia * media, gboolean record)
3888 GstRTSPMediaPrivate *priv;
3890 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
3894 g_mutex_lock (&priv->lock);
3895 priv->record = record;
3896 g_mutex_unlock (&priv->lock);
3900 * gst_rtsp_media_is_record:
3901 * @media: a #GstRTSPMedia
3903 * Check if the pipeline for @media can be used for PLAY or RECORD methods.
3905 * Returns: %TRUE if the media can be record between clients.
3908 gst_rtsp_media_is_record (GstRTSPMedia * media)
3910 GstRTSPMediaPrivate *priv;
3913 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
3917 g_mutex_lock (&priv->lock);
3919 g_mutex_unlock (&priv->lock);