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;
104 GstRTSPTransportMode transport_mode;
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 */
140 guint latency; /* protected by lock */
143 #define DEFAULT_SHARED FALSE
144 #define DEFAULT_SUSPEND_MODE GST_RTSP_SUSPEND_MODE_NONE
145 #define DEFAULT_REUSABLE FALSE
146 #define DEFAULT_PROFILES GST_RTSP_PROFILE_AVP
147 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | \
148 GST_RTSP_LOWER_TRANS_TCP
149 #define DEFAULT_EOS_SHUTDOWN FALSE
150 #define DEFAULT_BUFFER_SIZE 0x80000
151 #define DEFAULT_TIME_PROVIDER FALSE
152 #define DEFAULT_LATENCY 200
153 #define DEFAULT_TRANSPORT_MODE GST_RTSP_TRANSPORT_MODE_PLAY
155 /* define to dump received RTCP packets */
178 SIGNAL_REMOVED_STREAM,
186 GST_DEBUG_CATEGORY_STATIC (rtsp_media_debug);
187 #define GST_CAT_DEFAULT rtsp_media_debug
189 static void gst_rtsp_media_get_property (GObject * object, guint propid,
190 GValue * value, GParamSpec * pspec);
191 static void gst_rtsp_media_set_property (GObject * object, guint propid,
192 const GValue * value, GParamSpec * pspec);
193 static void gst_rtsp_media_finalize (GObject * obj);
195 static gboolean default_handle_message (GstRTSPMedia * media,
196 GstMessage * message);
197 static void finish_unprepare (GstRTSPMedia * media);
198 static gboolean default_prepare (GstRTSPMedia * media, GstRTSPThread * thread);
199 static gboolean default_unprepare (GstRTSPMedia * media);
200 static gboolean default_suspend (GstRTSPMedia * media);
201 static gboolean default_unsuspend (GstRTSPMedia * media);
202 static gboolean default_convert_range (GstRTSPMedia * media,
203 GstRTSPTimeRange * range, GstRTSPRangeUnit unit);
204 static gboolean default_query_position (GstRTSPMedia * media,
206 static gboolean default_query_stop (GstRTSPMedia * media, gint64 * stop);
207 static GstElement *default_create_rtpbin (GstRTSPMedia * media);
208 static gboolean default_setup_sdp (GstRTSPMedia * media, GstSDPMessage * sdp,
210 static gboolean default_handle_sdp (GstRTSPMedia * media, GstSDPMessage * sdp);
212 static gboolean wait_preroll (GstRTSPMedia * media);
214 static guint gst_rtsp_media_signals[SIGNAL_LAST] = { 0 };
216 #define C_ENUM(v) ((gint) v)
219 gst_rtsp_suspend_mode_get_type (void)
222 static const GEnumValue values[] = {
223 {C_ENUM (GST_RTSP_SUSPEND_MODE_NONE), "GST_RTSP_SUSPEND_MODE_NONE", "none"},
224 {C_ENUM (GST_RTSP_SUSPEND_MODE_PAUSE), "GST_RTSP_SUSPEND_MODE_PAUSE",
226 {C_ENUM (GST_RTSP_SUSPEND_MODE_RESET), "GST_RTSP_SUSPEND_MODE_RESET",
231 if (g_once_init_enter (&id)) {
232 GType tmp = g_enum_register_static ("GstRTSPSuspendMode", values);
233 g_once_init_leave (&id, tmp);
238 #define C_FLAGS(v) ((guint) v)
241 gst_rtsp_transport_mode_get_type (void)
244 static const GFlagsValue values[] = {
245 {C_FLAGS (GST_RTSP_TRANSPORT_MODE_PLAY), "GST_RTSP_TRANSPORT_MODE_PLAY",
247 {C_FLAGS (GST_RTSP_TRANSPORT_MODE_RECORD), "GST_RTSP_TRANSPORT_MODE_RECORD",
252 if (g_once_init_enter (&id)) {
253 GType tmp = g_flags_register_static ("GstRTSPTransportMode", values);
254 g_once_init_leave (&id, tmp);
259 G_DEFINE_TYPE (GstRTSPMedia, gst_rtsp_media, G_TYPE_OBJECT);
262 gst_rtsp_media_class_init (GstRTSPMediaClass * klass)
264 GObjectClass *gobject_class;
266 g_type_class_add_private (klass, sizeof (GstRTSPMediaPrivate));
268 gobject_class = G_OBJECT_CLASS (klass);
270 gobject_class->get_property = gst_rtsp_media_get_property;
271 gobject_class->set_property = gst_rtsp_media_set_property;
272 gobject_class->finalize = gst_rtsp_media_finalize;
274 g_object_class_install_property (gobject_class, PROP_SHARED,
275 g_param_spec_boolean ("shared", "Shared",
276 "If this media pipeline can be shared", DEFAULT_SHARED,
277 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
279 g_object_class_install_property (gobject_class, PROP_SUSPEND_MODE,
280 g_param_spec_enum ("suspend-mode", "Suspend Mode",
281 "How to suspend the media in PAUSED", GST_TYPE_RTSP_SUSPEND_MODE,
282 DEFAULT_SUSPEND_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
284 g_object_class_install_property (gobject_class, PROP_REUSABLE,
285 g_param_spec_boolean ("reusable", "Reusable",
286 "If this media pipeline can be reused after an unprepare",
287 DEFAULT_REUSABLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
289 g_object_class_install_property (gobject_class, PROP_PROFILES,
290 g_param_spec_flags ("profiles", "Profiles",
291 "Allowed transfer profiles", GST_TYPE_RTSP_PROFILE,
292 DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
294 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
295 g_param_spec_flags ("protocols", "Protocols",
296 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
297 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
299 g_object_class_install_property (gobject_class, PROP_EOS_SHUTDOWN,
300 g_param_spec_boolean ("eos-shutdown", "EOS Shutdown",
301 "Send an EOS event to the pipeline before unpreparing",
302 DEFAULT_EOS_SHUTDOWN, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
304 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
305 g_param_spec_uint ("buffer-size", "Buffer Size",
306 "The kernel UDP buffer size to use", 0, G_MAXUINT,
307 DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
309 g_object_class_install_property (gobject_class, PROP_ELEMENT,
310 g_param_spec_object ("element", "The Element",
311 "The GstBin to use for streaming the media", GST_TYPE_ELEMENT,
312 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
314 g_object_class_install_property (gobject_class, PROP_TIME_PROVIDER,
315 g_param_spec_boolean ("time-provider", "Time Provider",
316 "Use a NetTimeProvider for clients",
317 DEFAULT_TIME_PROVIDER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
319 g_object_class_install_property (gobject_class, PROP_LATENCY,
320 g_param_spec_uint ("latency", "Latency",
321 "Latency used for receiving media in milliseconds", 0, G_MAXUINT,
322 DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
324 g_object_class_install_property (gobject_class, PROP_TRANSPORT_MODE,
325 g_param_spec_flags ("transport-mode", "Transport Mode",
326 "If this media pipeline can be used for PLAY or RECORD",
327 GST_TYPE_RTSP_TRANSPORT_MODE, DEFAULT_TRANSPORT_MODE,
328 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
330 gst_rtsp_media_signals[SIGNAL_NEW_STREAM] =
331 g_signal_new ("new-stream", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
332 G_STRUCT_OFFSET (GstRTSPMediaClass, new_stream), NULL, NULL,
333 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_RTSP_STREAM);
335 gst_rtsp_media_signals[SIGNAL_REMOVED_STREAM] =
336 g_signal_new ("removed-stream", G_TYPE_FROM_CLASS (klass),
337 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPMediaClass, removed_stream),
338 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
339 GST_TYPE_RTSP_STREAM);
341 gst_rtsp_media_signals[SIGNAL_PREPARED] =
342 g_signal_new ("prepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
343 G_STRUCT_OFFSET (GstRTSPMediaClass, prepared), NULL, NULL,
344 g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
346 gst_rtsp_media_signals[SIGNAL_UNPREPARED] =
347 g_signal_new ("unprepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
348 G_STRUCT_OFFSET (GstRTSPMediaClass, unprepared), NULL, NULL,
349 g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
351 gst_rtsp_media_signals[SIGNAL_TARGET_STATE] =
352 g_signal_new ("target-state", G_TYPE_FROM_CLASS (klass),
353 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPMediaClass, target_state),
354 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_INT);
356 gst_rtsp_media_signals[SIGNAL_NEW_STATE] =
357 g_signal_new ("new-state", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
358 G_STRUCT_OFFSET (GstRTSPMediaClass, new_state), NULL, NULL,
359 g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_INT);
361 GST_DEBUG_CATEGORY_INIT (rtsp_media_debug, "rtspmedia", 0, "GstRTSPMedia");
363 klass->handle_message = default_handle_message;
364 klass->prepare = default_prepare;
365 klass->unprepare = default_unprepare;
366 klass->suspend = default_suspend;
367 klass->unsuspend = default_unsuspend;
368 klass->convert_range = default_convert_range;
369 klass->query_position = default_query_position;
370 klass->query_stop = default_query_stop;
371 klass->create_rtpbin = default_create_rtpbin;
372 klass->setup_sdp = default_setup_sdp;
373 klass->handle_sdp = default_handle_sdp;
377 gst_rtsp_media_init (GstRTSPMedia * media)
379 GstRTSPMediaPrivate *priv = GST_RTSP_MEDIA_GET_PRIVATE (media);
383 priv->streams = g_ptr_array_new_with_free_func (g_object_unref);
384 g_mutex_init (&priv->lock);
385 g_cond_init (&priv->cond);
386 g_rec_mutex_init (&priv->state_lock);
388 priv->shared = DEFAULT_SHARED;
389 priv->suspend_mode = DEFAULT_SUSPEND_MODE;
390 priv->reusable = DEFAULT_REUSABLE;
391 priv->profiles = DEFAULT_PROFILES;
392 priv->protocols = DEFAULT_PROTOCOLS;
393 priv->eos_shutdown = DEFAULT_EOS_SHUTDOWN;
394 priv->buffer_size = DEFAULT_BUFFER_SIZE;
395 priv->time_provider = DEFAULT_TIME_PROVIDER;
396 priv->transport_mode = DEFAULT_TRANSPORT_MODE;
400 gst_rtsp_media_finalize (GObject * obj)
402 GstRTSPMediaPrivate *priv;
405 media = GST_RTSP_MEDIA (obj);
408 GST_INFO ("finalize media %p", media);
410 if (priv->permissions)
411 gst_rtsp_permissions_unref (priv->permissions);
413 g_ptr_array_unref (priv->streams);
415 g_list_free_full (priv->dynamic, gst_object_unref);
418 gst_object_unref (priv->pipeline);
420 gst_object_unref (priv->nettime);
421 gst_object_unref (priv->element);
423 g_object_unref (priv->pool);
425 g_list_free (priv->payloads);
426 g_mutex_clear (&priv->lock);
427 g_cond_clear (&priv->cond);
428 g_rec_mutex_clear (&priv->state_lock);
430 G_OBJECT_CLASS (gst_rtsp_media_parent_class)->finalize (obj);
434 gst_rtsp_media_get_property (GObject * object, guint propid,
435 GValue * value, GParamSpec * pspec)
437 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
441 g_value_set_object (value, media->priv->element);
444 g_value_set_boolean (value, gst_rtsp_media_is_shared (media));
446 case PROP_SUSPEND_MODE:
447 g_value_set_enum (value, gst_rtsp_media_get_suspend_mode (media));
450 g_value_set_boolean (value, gst_rtsp_media_is_reusable (media));
453 g_value_set_flags (value, gst_rtsp_media_get_profiles (media));
456 g_value_set_flags (value, gst_rtsp_media_get_protocols (media));
458 case PROP_EOS_SHUTDOWN:
459 g_value_set_boolean (value, gst_rtsp_media_is_eos_shutdown (media));
461 case PROP_BUFFER_SIZE:
462 g_value_set_uint (value, gst_rtsp_media_get_buffer_size (media));
464 case PROP_TIME_PROVIDER:
465 g_value_set_boolean (value, gst_rtsp_media_is_time_provider (media));
468 g_value_set_uint (value, gst_rtsp_media_get_latency (media));
470 case PROP_TRANSPORT_MODE:
471 g_value_set_flags (value, gst_rtsp_media_get_transport_mode (media));
474 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
479 gst_rtsp_media_set_property (GObject * object, guint propid,
480 const GValue * value, GParamSpec * pspec)
482 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
486 media->priv->element = g_value_get_object (value);
487 gst_object_ref_sink (media->priv->element);
490 gst_rtsp_media_set_shared (media, g_value_get_boolean (value));
492 case PROP_SUSPEND_MODE:
493 gst_rtsp_media_set_suspend_mode (media, g_value_get_enum (value));
496 gst_rtsp_media_set_reusable (media, g_value_get_boolean (value));
499 gst_rtsp_media_set_profiles (media, g_value_get_flags (value));
502 gst_rtsp_media_set_protocols (media, g_value_get_flags (value));
504 case PROP_EOS_SHUTDOWN:
505 gst_rtsp_media_set_eos_shutdown (media, g_value_get_boolean (value));
507 case PROP_BUFFER_SIZE:
508 gst_rtsp_media_set_buffer_size (media, g_value_get_uint (value));
510 case PROP_TIME_PROVIDER:
511 gst_rtsp_media_use_time_provider (media, g_value_get_boolean (value));
514 gst_rtsp_media_set_latency (media, g_value_get_uint (value));
516 case PROP_TRANSPORT_MODE:
517 gst_rtsp_media_set_transport_mode (media, g_value_get_flags (value));
520 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
528 } DoQueryPositionData;
531 do_query_position (GstRTSPStream * stream, DoQueryPositionData * data)
535 if (gst_rtsp_stream_query_position (stream, &tmp)) {
536 data->position = MAX (data->position, tmp);
542 default_query_position (GstRTSPMedia * media, gint64 * position)
544 GstRTSPMediaPrivate *priv;
545 DoQueryPositionData data;
552 g_ptr_array_foreach (priv->streams, (GFunc) do_query_position, &data);
554 *position = data.position;
566 do_query_stop (GstRTSPStream * stream, DoQueryStopData * data)
570 if (gst_rtsp_stream_query_stop (stream, &tmp)) {
571 data->stop = MAX (data->stop, tmp);
577 default_query_stop (GstRTSPMedia * media, gint64 * stop)
579 GstRTSPMediaPrivate *priv;
580 DoQueryStopData data;
587 g_ptr_array_foreach (priv->streams, (GFunc) do_query_stop, &data);
595 default_create_rtpbin (GstRTSPMedia * media)
599 rtpbin = gst_element_factory_make ("rtpbin", NULL);
604 /* must be called with state lock */
606 collect_media_stats (GstRTSPMedia * media)
608 GstRTSPMediaPrivate *priv = media->priv;
609 gint64 position = 0, stop = -1;
611 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
612 priv->status != GST_RTSP_MEDIA_STATUS_PREPARING)
615 priv->range.unit = GST_RTSP_RANGE_NPT;
617 GST_INFO ("collect media stats");
620 priv->range.min.type = GST_RTSP_TIME_NOW;
621 priv->range.min.seconds = -1;
622 priv->range_start = -1;
623 priv->range.max.type = GST_RTSP_TIME_END;
624 priv->range.max.seconds = -1;
625 priv->range_stop = -1;
627 GstRTSPMediaClass *klass;
630 klass = GST_RTSP_MEDIA_GET_CLASS (media);
632 /* get the position */
634 if (klass->query_position)
635 ret = klass->query_position (media, &position);
638 GST_INFO ("position query failed");
642 /* get the current segment stop */
644 if (klass->query_stop)
645 ret = klass->query_stop (media, &stop);
648 GST_INFO ("stop query failed");
652 GST_INFO ("stats: position %" GST_TIME_FORMAT ", stop %"
653 GST_TIME_FORMAT, GST_TIME_ARGS (position), GST_TIME_ARGS (stop));
655 if (position == -1) {
656 priv->range.min.type = GST_RTSP_TIME_NOW;
657 priv->range.min.seconds = -1;
658 priv->range_start = -1;
660 priv->range.min.type = GST_RTSP_TIME_SECONDS;
661 priv->range.min.seconds = ((gdouble) position) / GST_SECOND;
662 priv->range_start = position;
665 priv->range.max.type = GST_RTSP_TIME_END;
666 priv->range.max.seconds = -1;
667 priv->range_stop = -1;
669 priv->range.max.type = GST_RTSP_TIME_SECONDS;
670 priv->range.max.seconds = ((gdouble) stop) / GST_SECOND;
671 priv->range_stop = stop;
677 * gst_rtsp_media_new:
678 * @element: (transfer full): a #GstElement
680 * Create a new #GstRTSPMedia instance. @element is the bin element that
681 * provides the different streams. The #GstRTSPMedia object contains the
682 * element to produce RTP data for one or more related (audio/video/..)
685 * Ownership is taken of @element.
687 * Returns: (transfer full): a new #GstRTSPMedia object.
690 gst_rtsp_media_new (GstElement * element)
692 GstRTSPMedia *result;
694 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
696 result = g_object_new (GST_TYPE_RTSP_MEDIA, "element", element, NULL);
702 * gst_rtsp_media_get_element:
703 * @media: a #GstRTSPMedia
705 * Get the element that was used when constructing @media.
707 * Returns: (transfer full): a #GstElement. Unref after usage.
710 gst_rtsp_media_get_element (GstRTSPMedia * media)
712 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
714 return gst_object_ref (media->priv->element);
718 * gst_rtsp_media_take_pipeline:
719 * @media: a #GstRTSPMedia
720 * @pipeline: (transfer full): a #GstPipeline
722 * Set @pipeline as the #GstPipeline for @media. Ownership is
723 * taken of @pipeline.
726 gst_rtsp_media_take_pipeline (GstRTSPMedia * media, GstPipeline * pipeline)
728 GstRTSPMediaPrivate *priv;
730 GstNetTimeProvider *nettime;
732 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
733 g_return_if_fail (GST_IS_PIPELINE (pipeline));
737 g_mutex_lock (&priv->lock);
738 old = priv->pipeline;
739 priv->pipeline = GST_ELEMENT_CAST (pipeline);
740 nettime = priv->nettime;
741 priv->nettime = NULL;
742 g_mutex_unlock (&priv->lock);
745 gst_object_unref (old);
748 gst_object_unref (nettime);
750 gst_bin_add (GST_BIN_CAST (pipeline), priv->element);
754 * gst_rtsp_media_set_permissions:
755 * @media: a #GstRTSPMedia
756 * @permissions: (transfer none): a #GstRTSPPermissions
758 * Set @permissions on @media.
761 gst_rtsp_media_set_permissions (GstRTSPMedia * media,
762 GstRTSPPermissions * permissions)
764 GstRTSPMediaPrivate *priv;
766 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
770 g_mutex_lock (&priv->lock);
771 if (priv->permissions)
772 gst_rtsp_permissions_unref (priv->permissions);
773 if ((priv->permissions = permissions))
774 gst_rtsp_permissions_ref (permissions);
775 g_mutex_unlock (&priv->lock);
779 * gst_rtsp_media_get_permissions:
780 * @media: a #GstRTSPMedia
782 * Get the permissions object from @media.
784 * Returns: (transfer full): a #GstRTSPPermissions object, unref after usage.
787 gst_rtsp_media_get_permissions (GstRTSPMedia * media)
789 GstRTSPMediaPrivate *priv;
790 GstRTSPPermissions *result;
792 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
796 g_mutex_lock (&priv->lock);
797 if ((result = priv->permissions))
798 gst_rtsp_permissions_ref (result);
799 g_mutex_unlock (&priv->lock);
805 * gst_rtsp_media_set_suspend_mode:
806 * @media: a #GstRTSPMedia
807 * @mode: the new #GstRTSPSuspendMode
809 * Control how @ media will be suspended after the SDP has been generated and
810 * after a PAUSE request has been performed.
812 * Media must be unprepared when setting the suspend mode.
815 gst_rtsp_media_set_suspend_mode (GstRTSPMedia * media, GstRTSPSuspendMode mode)
817 GstRTSPMediaPrivate *priv;
819 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
823 g_rec_mutex_lock (&priv->state_lock);
824 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED)
826 priv->suspend_mode = mode;
827 g_rec_mutex_unlock (&priv->state_lock);
834 GST_WARNING ("media %p was prepared", media);
835 g_rec_mutex_unlock (&priv->state_lock);
840 * gst_rtsp_media_get_suspend_mode:
841 * @media: a #GstRTSPMedia
843 * Get how @media will be suspended.
845 * Returns: #GstRTSPSuspendMode.
848 gst_rtsp_media_get_suspend_mode (GstRTSPMedia * media)
850 GstRTSPMediaPrivate *priv;
851 GstRTSPSuspendMode res;
853 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_RTSP_SUSPEND_MODE_NONE);
857 g_rec_mutex_lock (&priv->state_lock);
858 res = priv->suspend_mode;
859 g_rec_mutex_unlock (&priv->state_lock);
865 * gst_rtsp_media_set_shared:
866 * @media: a #GstRTSPMedia
867 * @shared: the new value
869 * Set or unset if the pipeline for @media can be shared will multiple clients.
870 * When @shared is %TRUE, client requests for this media will share the media
874 gst_rtsp_media_set_shared (GstRTSPMedia * media, gboolean shared)
876 GstRTSPMediaPrivate *priv;
878 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
882 g_mutex_lock (&priv->lock);
883 priv->shared = shared;
884 g_mutex_unlock (&priv->lock);
888 * gst_rtsp_media_is_shared:
889 * @media: a #GstRTSPMedia
891 * Check if the pipeline for @media can be shared between multiple clients.
893 * Returns: %TRUE if the media can be shared between clients.
896 gst_rtsp_media_is_shared (GstRTSPMedia * media)
898 GstRTSPMediaPrivate *priv;
901 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
905 g_mutex_lock (&priv->lock);
907 g_mutex_unlock (&priv->lock);
913 * gst_rtsp_media_set_reusable:
914 * @media: a #GstRTSPMedia
915 * @reusable: the new value
917 * Set or unset if the pipeline for @media can be reused after the pipeline has
921 gst_rtsp_media_set_reusable (GstRTSPMedia * media, gboolean reusable)
923 GstRTSPMediaPrivate *priv;
925 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
929 g_mutex_lock (&priv->lock);
930 priv->reusable = reusable;
931 g_mutex_unlock (&priv->lock);
935 * gst_rtsp_media_is_reusable:
936 * @media: a #GstRTSPMedia
938 * Check if the pipeline for @media can be reused after an unprepare.
940 * Returns: %TRUE if the media can be reused
943 gst_rtsp_media_is_reusable (GstRTSPMedia * media)
945 GstRTSPMediaPrivate *priv;
948 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
952 g_mutex_lock (&priv->lock);
953 res = priv->reusable;
954 g_mutex_unlock (&priv->lock);
960 do_set_profiles (GstRTSPStream * stream, GstRTSPProfile * profiles)
962 gst_rtsp_stream_set_profiles (stream, *profiles);
966 * gst_rtsp_media_set_profiles:
967 * @media: a #GstRTSPMedia
968 * @profiles: the new flags
970 * Configure the allowed lower transport for @media.
973 gst_rtsp_media_set_profiles (GstRTSPMedia * media, GstRTSPProfile profiles)
975 GstRTSPMediaPrivate *priv;
977 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
981 g_mutex_lock (&priv->lock);
982 priv->profiles = profiles;
983 g_ptr_array_foreach (priv->streams, (GFunc) do_set_profiles, &profiles);
984 g_mutex_unlock (&priv->lock);
988 * gst_rtsp_media_get_profiles:
989 * @media: a #GstRTSPMedia
991 * Get the allowed profiles of @media.
993 * Returns: a #GstRTSPProfile
996 gst_rtsp_media_get_profiles (GstRTSPMedia * media)
998 GstRTSPMediaPrivate *priv;
1001 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_RTSP_PROFILE_UNKNOWN);
1005 g_mutex_lock (&priv->lock);
1006 res = priv->profiles;
1007 g_mutex_unlock (&priv->lock);
1013 do_set_protocols (GstRTSPStream * stream, GstRTSPLowerTrans * protocols)
1015 gst_rtsp_stream_set_protocols (stream, *protocols);
1019 * gst_rtsp_media_set_protocols:
1020 * @media: a #GstRTSPMedia
1021 * @protocols: the new flags
1023 * Configure the allowed lower transport for @media.
1026 gst_rtsp_media_set_protocols (GstRTSPMedia * media, GstRTSPLowerTrans protocols)
1028 GstRTSPMediaPrivate *priv;
1030 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1034 g_mutex_lock (&priv->lock);
1035 priv->protocols = protocols;
1036 g_ptr_array_foreach (priv->streams, (GFunc) do_set_protocols, &protocols);
1037 g_mutex_unlock (&priv->lock);
1041 * gst_rtsp_media_get_protocols:
1042 * @media: a #GstRTSPMedia
1044 * Get the allowed protocols of @media.
1046 * Returns: a #GstRTSPLowerTrans
1049 gst_rtsp_media_get_protocols (GstRTSPMedia * media)
1051 GstRTSPMediaPrivate *priv;
1052 GstRTSPLowerTrans res;
1054 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media),
1055 GST_RTSP_LOWER_TRANS_UNKNOWN);
1059 g_mutex_lock (&priv->lock);
1060 res = priv->protocols;
1061 g_mutex_unlock (&priv->lock);
1067 * gst_rtsp_media_set_eos_shutdown:
1068 * @media: a #GstRTSPMedia
1069 * @eos_shutdown: the new value
1071 * Set or unset if an EOS event will be sent to the pipeline for @media before
1075 gst_rtsp_media_set_eos_shutdown (GstRTSPMedia * media, gboolean eos_shutdown)
1077 GstRTSPMediaPrivate *priv;
1079 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1083 g_mutex_lock (&priv->lock);
1084 priv->eos_shutdown = eos_shutdown;
1085 g_mutex_unlock (&priv->lock);
1089 * gst_rtsp_media_is_eos_shutdown:
1090 * @media: a #GstRTSPMedia
1092 * Check if the pipeline for @media will send an EOS down the pipeline before
1095 * Returns: %TRUE if the media will send EOS before unpreparing.
1098 gst_rtsp_media_is_eos_shutdown (GstRTSPMedia * media)
1100 GstRTSPMediaPrivate *priv;
1103 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1107 g_mutex_lock (&priv->lock);
1108 res = priv->eos_shutdown;
1109 g_mutex_unlock (&priv->lock);
1115 * gst_rtsp_media_set_buffer_size:
1116 * @media: a #GstRTSPMedia
1117 * @size: the new value
1119 * Set the kernel UDP buffer size.
1122 gst_rtsp_media_set_buffer_size (GstRTSPMedia * media, guint size)
1124 GstRTSPMediaPrivate *priv;
1126 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1128 GST_LOG_OBJECT (media, "set buffer size %u", size);
1132 g_mutex_lock (&priv->lock);
1133 priv->buffer_size = size;
1134 g_mutex_unlock (&priv->lock);
1138 * gst_rtsp_media_get_buffer_size:
1139 * @media: a #GstRTSPMedia
1141 * Get the kernel UDP buffer size.
1143 * Returns: the kernel UDP buffer size.
1146 gst_rtsp_media_get_buffer_size (GstRTSPMedia * media)
1148 GstRTSPMediaPrivate *priv;
1151 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1155 g_mutex_unlock (&priv->lock);
1156 res = priv->buffer_size;
1157 g_mutex_unlock (&priv->lock);
1163 * gst_rtsp_media_set_retransmission_time:
1164 * @media: a #GstRTSPMedia
1165 * @time: the new value
1167 * Set the amount of time to store retransmission packets.
1170 gst_rtsp_media_set_retransmission_time (GstRTSPMedia * media, GstClockTime time)
1172 GstRTSPMediaPrivate *priv;
1175 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1177 GST_LOG_OBJECT (media, "set retransmission time %" G_GUINT64_FORMAT, time);
1181 g_mutex_lock (&priv->lock);
1182 priv->rtx_time = time;
1183 for (i = 0; i < priv->streams->len; i++) {
1184 GstRTSPStream *stream = g_ptr_array_index (priv->streams, i);
1186 gst_rtsp_stream_set_retransmission_time (stream, time);
1190 g_object_set (priv->rtpbin, "do-retransmission", time > 0, NULL);
1191 g_mutex_unlock (&priv->lock);
1195 * gst_rtsp_media_get_retransmission_time:
1196 * @media: a #GstRTSPMedia
1198 * Get the amount of time to store retransmission data.
1200 * Returns: the amount of time to store retransmission data.
1203 gst_rtsp_media_get_retransmission_time (GstRTSPMedia * media)
1205 GstRTSPMediaPrivate *priv;
1208 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1212 g_mutex_unlock (&priv->lock);
1213 res = priv->rtx_time;
1214 g_mutex_unlock (&priv->lock);
1220 * gst_rtsp_media_set_latncy:
1221 * @media: a #GstRTSPMedia
1222 * @latency: latency in milliseconds
1224 * Configure the latency used for receiving media.
1227 gst_rtsp_media_set_latency (GstRTSPMedia * media, guint latency)
1229 GstRTSPMediaPrivate *priv;
1231 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1233 GST_LOG_OBJECT (media, "set latency %ums", latency);
1237 g_mutex_lock (&priv->lock);
1238 priv->latency = latency;
1240 g_object_set (priv->rtpbin, "latency", latency, NULL);
1241 g_mutex_unlock (&priv->lock);
1245 * gst_rtsp_media_get_latency:
1246 * @media: a #GstRTSPMedia
1248 * Get the latency that is used for receiving media.
1250 * Returns: latency in milliseconds
1253 gst_rtsp_media_get_latency (GstRTSPMedia * media)
1255 GstRTSPMediaPrivate *priv;
1258 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1262 g_mutex_unlock (&priv->lock);
1263 res = priv->latency;
1264 g_mutex_unlock (&priv->lock);
1270 * gst_rtsp_media_use_time_provider:
1271 * @media: a #GstRTSPMedia
1272 * @time_provider: if a #GstNetTimeProvider should be used
1274 * Set @media to provide a #GstNetTimeProvider.
1277 gst_rtsp_media_use_time_provider (GstRTSPMedia * media, gboolean time_provider)
1279 GstRTSPMediaPrivate *priv;
1281 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1285 g_mutex_lock (&priv->lock);
1286 priv->time_provider = time_provider;
1287 g_mutex_unlock (&priv->lock);
1291 * gst_rtsp_media_is_time_provider:
1292 * @media: a #GstRTSPMedia
1294 * Check if @media can provide a #GstNetTimeProvider for its pipeline clock.
1296 * Use gst_rtsp_media_get_time_provider() to get the network clock.
1298 * Returns: %TRUE if @media can provide a #GstNetTimeProvider.
1301 gst_rtsp_media_is_time_provider (GstRTSPMedia * media)
1303 GstRTSPMediaPrivate *priv;
1306 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1310 g_mutex_unlock (&priv->lock);
1311 res = priv->time_provider;
1312 g_mutex_unlock (&priv->lock);
1318 * gst_rtsp_media_set_address_pool:
1319 * @media: a #GstRTSPMedia
1320 * @pool: (transfer none): a #GstRTSPAddressPool
1322 * configure @pool to be used as the address pool of @media.
1325 gst_rtsp_media_set_address_pool (GstRTSPMedia * media,
1326 GstRTSPAddressPool * pool)
1328 GstRTSPMediaPrivate *priv;
1329 GstRTSPAddressPool *old;
1331 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1335 GST_LOG_OBJECT (media, "set address pool %p", pool);
1337 g_mutex_lock (&priv->lock);
1338 if ((old = priv->pool) != pool)
1339 priv->pool = pool ? g_object_ref (pool) : NULL;
1342 g_ptr_array_foreach (priv->streams, (GFunc) gst_rtsp_stream_set_address_pool,
1344 g_mutex_unlock (&priv->lock);
1347 g_object_unref (old);
1351 * gst_rtsp_media_get_address_pool:
1352 * @media: a #GstRTSPMedia
1354 * Get the #GstRTSPAddressPool used as the address pool of @media.
1356 * Returns: (transfer full): the #GstRTSPAddressPool of @media. g_object_unref() after
1359 GstRTSPAddressPool *
1360 gst_rtsp_media_get_address_pool (GstRTSPMedia * media)
1362 GstRTSPMediaPrivate *priv;
1363 GstRTSPAddressPool *result;
1365 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1369 g_mutex_lock (&priv->lock);
1370 if ((result = priv->pool))
1371 g_object_ref (result);
1372 g_mutex_unlock (&priv->lock);
1378 _find_payload_types (GstRTSPMedia * media)
1381 GQueue queue = G_QUEUE_INIT;
1383 n = media->priv->streams->len;
1384 for (i = 0; i < n; i++) {
1385 GstRTSPStream *stream = g_ptr_array_index (media->priv->streams, i);
1386 guint pt = gst_rtsp_stream_get_pt (stream);
1388 g_queue_push_tail (&queue, GUINT_TO_POINTER (pt));
1395 _next_available_pt (GList * payloads)
1399 for (i = 96; i <= 127; i++) {
1400 GList *iter = g_list_find (payloads, GINT_TO_POINTER (i));
1402 return GPOINTER_TO_UINT (i);
1409 * gst_rtsp_media_collect_streams:
1410 * @media: a #GstRTSPMedia
1412 * Find all payloader elements, they should be named pay\%d in the
1413 * element of @media, and create #GstRTSPStreams for them.
1415 * Collect all dynamic elements, named dynpay\%d, and add them to
1416 * the list of dynamic elements.
1418 * Find all depayloader elements, they should be named depay\%d in the
1419 * element of @media, and create #GstRTSPStreams for them.
1422 gst_rtsp_media_collect_streams (GstRTSPMedia * media)
1424 GstRTSPMediaPrivate *priv;
1425 GstElement *element, *elem;
1429 gboolean more_elem_remaining = TRUE;
1430 GstRTSPTransportMode mode = 0;
1432 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1435 element = priv->element;
1438 for (i = 0; more_elem_remaining; i++) {
1441 more_elem_remaining = FALSE;
1443 name = g_strdup_printf ("pay%d", i);
1444 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
1445 GST_INFO ("found stream %d with payloader %p", i, elem);
1447 /* take the pad of the payloader */
1448 pad = gst_element_get_static_pad (elem, "src");
1449 /* create the stream */
1450 gst_rtsp_media_create_stream (media, elem, pad);
1451 gst_object_unref (pad);
1452 gst_object_unref (elem);
1455 more_elem_remaining = TRUE;
1456 mode |= GST_RTSP_TRANSPORT_MODE_PLAY;
1460 name = g_strdup_printf ("dynpay%d", i);
1461 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
1462 /* a stream that will dynamically create pads to provide RTP packets */
1463 GST_INFO ("found dynamic element %d, %p", i, elem);
1465 g_mutex_lock (&priv->lock);
1466 priv->dynamic = g_list_prepend (priv->dynamic, elem);
1467 g_mutex_unlock (&priv->lock);
1470 more_elem_remaining = TRUE;
1471 mode |= GST_RTSP_TRANSPORT_MODE_PLAY;
1475 name = g_strdup_printf ("depay%d", i);
1476 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
1477 GST_INFO ("found stream %d with depayloader %p", i, elem);
1479 /* take the pad of the payloader */
1480 pad = gst_element_get_static_pad (elem, "sink");
1481 /* create the stream */
1482 gst_rtsp_media_create_stream (media, elem, pad);
1483 gst_object_unref (pad);
1484 gst_object_unref (elem);
1487 more_elem_remaining = TRUE;
1488 mode |= GST_RTSP_TRANSPORT_MODE_RECORD;
1494 if (priv->transport_mode != mode)
1495 GST_WARNING ("found different mode than expected (0x%02x != 0x%02d)",
1496 priv->transport_mode, mode);
1501 * gst_rtsp_media_create_stream:
1502 * @media: a #GstRTSPMedia
1503 * @payloader: a #GstElement
1506 * Create a new stream in @media that provides RTP data on @pad.
1507 * @pad should be a pad of an element inside @media->element.
1509 * Returns: (transfer none): a new #GstRTSPStream that remains valid for as long
1513 gst_rtsp_media_create_stream (GstRTSPMedia * media, GstElement * payloader,
1516 GstRTSPMediaPrivate *priv;
1517 GstRTSPStream *stream;
1522 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1523 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
1524 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1528 g_mutex_lock (&priv->lock);
1529 idx = priv->streams->len;
1531 GST_DEBUG ("media %p: creating stream with index %d", media, idx);
1533 if (GST_PAD_IS_SRC (pad))
1534 name = g_strdup_printf ("src_%u", idx);
1536 name = g_strdup_printf ("sink_%u", idx);
1538 ghostpad = gst_ghost_pad_new (name, pad);
1539 gst_pad_set_active (ghostpad, TRUE);
1540 gst_element_add_pad (priv->element, ghostpad);
1543 stream = gst_rtsp_stream_new (idx, payloader, ghostpad);
1545 gst_rtsp_stream_set_address_pool (stream, priv->pool);
1546 gst_rtsp_stream_set_profiles (stream, priv->profiles);
1547 gst_rtsp_stream_set_protocols (stream, priv->protocols);
1548 gst_rtsp_stream_set_retransmission_time (stream, priv->rtx_time);
1550 g_ptr_array_add (priv->streams, stream);
1552 if (GST_PAD_IS_SRC (pad)) {
1556 g_list_free (priv->payloads);
1557 priv->payloads = _find_payload_types (media);
1559 n = priv->streams->len;
1560 for (i = 0; i < n; i++) {
1561 GstRTSPStream *stream = g_ptr_array_index (priv->streams, i);
1562 guint rtx_pt = _next_available_pt (priv->payloads);
1565 GST_WARNING ("Ran out of space of dynamic payload types");
1569 gst_rtsp_stream_set_retransmission_pt (stream, rtx_pt);
1572 g_list_append (priv->payloads, GUINT_TO_POINTER (rtx_pt));
1575 g_mutex_unlock (&priv->lock);
1577 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STREAM], 0, stream,
1584 gst_rtsp_media_remove_stream (GstRTSPMedia * media, GstRTSPStream * stream)
1586 GstRTSPMediaPrivate *priv;
1591 g_mutex_lock (&priv->lock);
1592 /* remove the ghostpad */
1593 srcpad = gst_rtsp_stream_get_srcpad (stream);
1594 gst_element_remove_pad (priv->element, srcpad);
1595 gst_object_unref (srcpad);
1596 /* now remove the stream */
1597 g_object_ref (stream);
1598 g_ptr_array_remove (priv->streams, stream);
1599 g_mutex_unlock (&priv->lock);
1601 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_REMOVED_STREAM], 0,
1604 g_object_unref (stream);
1608 * gst_rtsp_media_n_streams:
1609 * @media: a #GstRTSPMedia
1611 * Get the number of streams in this media.
1613 * Returns: The number of streams.
1616 gst_rtsp_media_n_streams (GstRTSPMedia * media)
1618 GstRTSPMediaPrivate *priv;
1621 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), 0);
1625 g_mutex_lock (&priv->lock);
1626 res = priv->streams->len;
1627 g_mutex_unlock (&priv->lock);
1633 * gst_rtsp_media_get_stream:
1634 * @media: a #GstRTSPMedia
1635 * @idx: the stream index
1637 * Retrieve the stream with index @idx from @media.
1639 * Returns: (nullable) (transfer none): the #GstRTSPStream at index
1640 * @idx or %NULL when a stream with that index did not exist.
1643 gst_rtsp_media_get_stream (GstRTSPMedia * media, guint idx)
1645 GstRTSPMediaPrivate *priv;
1648 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1652 g_mutex_lock (&priv->lock);
1653 if (idx < priv->streams->len)
1654 res = g_ptr_array_index (priv->streams, idx);
1657 g_mutex_unlock (&priv->lock);
1663 * gst_rtsp_media_find_stream:
1664 * @media: a #GstRTSPMedia
1665 * @control: the control of the stream
1667 * Find a stream in @media with @control as the control uri.
1669 * Returns: (nullable) (transfer none): the #GstRTSPStream with
1670 * control uri @control or %NULL when a stream with that control did
1674 gst_rtsp_media_find_stream (GstRTSPMedia * media, const gchar * control)
1676 GstRTSPMediaPrivate *priv;
1680 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1681 g_return_val_if_fail (control != NULL, NULL);
1687 g_mutex_lock (&priv->lock);
1688 for (i = 0; i < priv->streams->len; i++) {
1689 GstRTSPStream *test;
1691 test = g_ptr_array_index (priv->streams, i);
1692 if (gst_rtsp_stream_has_control (test, control)) {
1697 g_mutex_unlock (&priv->lock);
1702 /* called with state-lock */
1704 default_convert_range (GstRTSPMedia * media, GstRTSPTimeRange * range,
1705 GstRTSPRangeUnit unit)
1707 return gst_rtsp_range_convert_units (range, unit);
1711 * gst_rtsp_media_get_range_string:
1712 * @media: a #GstRTSPMedia
1713 * @play: for the PLAY request
1714 * @unit: the unit to use for the string
1716 * Get the current range as a string. @media must be prepared with
1717 * gst_rtsp_media_prepare ().
1719 * Returns: (transfer full): The range as a string, g_free() after usage.
1722 gst_rtsp_media_get_range_string (GstRTSPMedia * media, gboolean play,
1723 GstRTSPRangeUnit unit)
1725 GstRTSPMediaClass *klass;
1726 GstRTSPMediaPrivate *priv;
1728 GstRTSPTimeRange range;
1730 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1731 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1732 g_return_val_if_fail (klass->convert_range != NULL, FALSE);
1736 g_rec_mutex_lock (&priv->state_lock);
1737 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
1738 priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
1741 g_mutex_lock (&priv->lock);
1743 /* Update the range value with current position/duration */
1744 collect_media_stats (media);
1747 range = priv->range;
1749 if (!play && priv->n_active > 0) {
1750 range.min.type = GST_RTSP_TIME_NOW;
1751 range.min.seconds = -1;
1753 g_mutex_unlock (&priv->lock);
1754 g_rec_mutex_unlock (&priv->state_lock);
1756 if (!klass->convert_range (media, &range, unit))
1757 goto conversion_failed;
1759 result = gst_rtsp_range_to_string (&range);
1766 GST_WARNING ("media %p was not prepared", media);
1767 g_rec_mutex_unlock (&priv->state_lock);
1772 GST_WARNING ("range conversion to unit %d failed", unit);
1778 stream_update_blocked (GstRTSPStream * stream, GstRTSPMedia * media)
1780 gst_rtsp_stream_set_blocked (stream, media->priv->blocked);
1784 media_streams_set_blocked (GstRTSPMedia * media, gboolean blocked)
1786 GstRTSPMediaPrivate *priv = media->priv;
1788 GST_DEBUG ("media %p set blocked %d", media, blocked);
1789 priv->blocked = blocked;
1790 g_ptr_array_foreach (priv->streams, (GFunc) stream_update_blocked, media);
1794 gst_rtsp_media_set_status (GstRTSPMedia * media, GstRTSPMediaStatus status)
1796 GstRTSPMediaPrivate *priv = media->priv;
1798 g_mutex_lock (&priv->lock);
1799 priv->status = status;
1800 GST_DEBUG ("setting new status to %d", status);
1801 g_cond_broadcast (&priv->cond);
1802 g_mutex_unlock (&priv->lock);
1806 * gst_rtsp_media_get_status:
1807 * @media: a #GstRTSPMedia
1809 * Get the status of @media. When @media is busy preparing, this function waits
1810 * until @media is prepared or in error.
1812 * Returns: the status of @media.
1815 gst_rtsp_media_get_status (GstRTSPMedia * media)
1817 GstRTSPMediaPrivate *priv = media->priv;
1818 GstRTSPMediaStatus result;
1821 g_mutex_lock (&priv->lock);
1822 end_time = g_get_monotonic_time () + 20 * G_TIME_SPAN_SECOND;
1823 /* while we are preparing, wait */
1824 while (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING) {
1825 GST_DEBUG ("waiting for status change");
1826 if (!g_cond_wait_until (&priv->cond, &priv->lock, end_time)) {
1827 GST_DEBUG ("timeout, assuming error status");
1828 priv->status = GST_RTSP_MEDIA_STATUS_ERROR;
1831 /* could be success or error */
1832 result = priv->status;
1833 GST_DEBUG ("got status %d", result);
1834 g_mutex_unlock (&priv->lock);
1840 * gst_rtsp_media_seek:
1841 * @media: a #GstRTSPMedia
1842 * @range: (transfer none): a #GstRTSPTimeRange
1844 * Seek the pipeline of @media to @range. @media must be prepared with
1845 * gst_rtsp_media_prepare().
1847 * Returns: %TRUE on success.
1850 gst_rtsp_media_seek (GstRTSPMedia * media, GstRTSPTimeRange * range)
1852 GstRTSPMediaClass *klass;
1853 GstRTSPMediaPrivate *priv;
1855 GstClockTime start, stop;
1856 GstSeekType start_type, stop_type;
1859 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1861 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1862 g_return_val_if_fail (range != NULL, FALSE);
1863 g_return_val_if_fail (klass->convert_range != NULL, FALSE);
1867 g_rec_mutex_lock (&priv->state_lock);
1868 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
1871 /* Update the seekable state of the pipeline in case it changed */
1872 if ((priv->transport_mode & GST_RTSP_TRANSPORT_MODE_RECORD)) {
1873 /* TODO: Seeking for RECORD? */
1874 priv->seekable = FALSE;
1876 query = gst_query_new_seeking (GST_FORMAT_TIME);
1877 if (gst_element_query (priv->pipeline, query)) {
1882 gst_query_parse_seeking (query, &format, &seekable, &start, &end);
1883 priv->seekable = seekable;
1885 gst_query_unref (query);
1888 if (!priv->seekable)
1891 start_type = stop_type = GST_SEEK_TYPE_NONE;
1893 if (!klass->convert_range (media, range, GST_RTSP_RANGE_NPT))
1895 gst_rtsp_range_get_times (range, &start, &stop);
1897 GST_INFO ("got %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1898 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1899 GST_INFO ("current %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1900 GST_TIME_ARGS (priv->range_start), GST_TIME_ARGS (priv->range_stop));
1902 if (start != GST_CLOCK_TIME_NONE)
1903 start_type = GST_SEEK_TYPE_SET;
1905 if (priv->range_stop == stop)
1906 stop = GST_CLOCK_TIME_NONE;
1907 else if (stop != GST_CLOCK_TIME_NONE)
1908 stop_type = GST_SEEK_TYPE_SET;
1910 if (start != GST_CLOCK_TIME_NONE || stop != GST_CLOCK_TIME_NONE) {
1913 GST_INFO ("seeking to %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1914 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1916 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARING);
1918 media_streams_set_blocked (media, TRUE);
1920 /* depends on the current playing state of the pipeline. We might need to
1921 * queue this until we get EOS. */
1922 flags = GST_SEEK_FLAG_FLUSH;
1924 /* if range start was not supplied we must continue from current position.
1925 * but since we're doing a flushing seek, let us query the current position
1926 * so we end up at exactly the same position after the seek. */
1927 if (range->min.type == GST_RTSP_TIME_END) { /* Yepp, that's right! */
1929 gboolean ret = FALSE;
1931 if (klass->query_position)
1932 ret = klass->query_position (media, &position);
1935 GST_WARNING ("position query failed");
1937 GST_DEBUG ("doing accurate seek to %" GST_TIME_FORMAT,
1938 GST_TIME_ARGS (position));
1940 start_type = GST_SEEK_TYPE_SET;
1941 flags |= GST_SEEK_FLAG_ACCURATE;
1944 /* only set keyframe flag when modifying start */
1945 if (start_type != GST_SEEK_TYPE_NONE)
1946 flags |= GST_SEEK_FLAG_KEY_UNIT;
1949 /* FIXME, we only do forwards playback, no trick modes yet */
1950 res = gst_element_seek (priv->pipeline, 1.0, GST_FORMAT_TIME,
1951 flags, start_type, start, stop_type, stop);
1953 /* and block for the seek to complete */
1954 GST_INFO ("done seeking %d", res);
1958 g_rec_mutex_unlock (&priv->state_lock);
1960 /* wait until pipeline is prerolled again, this will also collect stats */
1961 if (!wait_preroll (media))
1962 goto preroll_failed;
1964 g_rec_mutex_lock (&priv->state_lock);
1965 GST_INFO ("prerolled again");
1967 GST_INFO ("no seek needed");
1970 g_rec_mutex_unlock (&priv->state_lock);
1977 g_rec_mutex_unlock (&priv->state_lock);
1978 GST_INFO ("media %p is not prepared", media);
1983 g_rec_mutex_unlock (&priv->state_lock);
1984 GST_INFO ("pipeline is not seekable");
1989 g_rec_mutex_unlock (&priv->state_lock);
1990 GST_WARNING ("conversion to npt not supported");
1995 g_rec_mutex_unlock (&priv->state_lock);
1996 GST_INFO ("seeking failed");
1997 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2002 GST_WARNING ("failed to preroll after seek");
2008 stream_collect_blocking (GstRTSPStream * stream, gboolean * blocked)
2010 *blocked &= gst_rtsp_stream_is_blocking (stream);
2014 media_streams_blocking (GstRTSPMedia * media)
2016 gboolean blocking = TRUE;
2018 g_ptr_array_foreach (media->priv->streams, (GFunc) stream_collect_blocking,
2024 static GstStateChangeReturn
2025 set_state (GstRTSPMedia * media, GstState state)
2027 GstRTSPMediaPrivate *priv = media->priv;
2028 GstStateChangeReturn ret;
2030 GST_INFO ("set state to %s for media %p", gst_element_state_get_name (state),
2032 ret = gst_element_set_state (priv->pipeline, state);
2037 static GstStateChangeReturn
2038 set_target_state (GstRTSPMedia * media, GstState state, gboolean do_state)
2040 GstRTSPMediaPrivate *priv = media->priv;
2041 GstStateChangeReturn ret;
2043 GST_INFO ("set target state to %s for media %p",
2044 gst_element_state_get_name (state), media);
2045 priv->target_state = state;
2047 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_TARGET_STATE], 0,
2048 priv->target_state, NULL);
2051 ret = set_state (media, state);
2053 ret = GST_STATE_CHANGE_SUCCESS;
2058 /* called with state-lock */
2060 default_handle_message (GstRTSPMedia * media, GstMessage * message)
2062 GstRTSPMediaPrivate *priv = media->priv;
2063 GstMessageType type;
2065 type = GST_MESSAGE_TYPE (message);
2068 case GST_MESSAGE_STATE_CHANGED:
2070 GstState old, new, pending;
2072 if (GST_MESSAGE_SRC (message) != GST_OBJECT (priv->pipeline))
2075 gst_message_parse_state_changed (message, &old, &new, &pending);
2077 GST_DEBUG ("%p: went from %s to %s (pending %s)", media,
2078 gst_element_state_get_name (old), gst_element_state_get_name (new),
2079 gst_element_state_get_name (pending));
2080 if ((priv->transport_mode & GST_RTSP_TRANSPORT_MODE_RECORD)
2081 && old == GST_STATE_READY && new == GST_STATE_PAUSED) {
2082 GST_INFO ("%p: went to PAUSED, prepared now", media);
2083 collect_media_stats (media);
2085 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
2086 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
2091 case GST_MESSAGE_BUFFERING:
2095 gst_message_parse_buffering (message, &percent);
2097 /* no state management needed for live pipelines */
2101 if (percent == 100) {
2102 /* a 100% message means buffering is done */
2103 priv->buffering = FALSE;
2104 /* if the desired state is playing, go back */
2105 if (priv->target_state == GST_STATE_PLAYING) {
2106 GST_INFO ("Buffering done, setting pipeline to PLAYING");
2107 set_state (media, GST_STATE_PLAYING);
2109 GST_INFO ("Buffering done");
2112 /* buffering busy */
2113 if (priv->buffering == FALSE) {
2114 if (priv->target_state == GST_STATE_PLAYING) {
2115 /* we were not buffering but PLAYING, PAUSE the pipeline. */
2116 GST_INFO ("Buffering, setting pipeline to PAUSED ...");
2117 set_state (media, GST_STATE_PAUSED);
2119 GST_INFO ("Buffering ...");
2122 priv->buffering = TRUE;
2126 case GST_MESSAGE_LATENCY:
2128 gst_bin_recalculate_latency (GST_BIN_CAST (priv->pipeline));
2131 case GST_MESSAGE_ERROR:
2136 gst_message_parse_error (message, &gerror, &debug);
2137 GST_WARNING ("%p: got error %s (%s)", media, gerror->message, debug);
2138 g_error_free (gerror);
2141 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2144 case GST_MESSAGE_WARNING:
2149 gst_message_parse_warning (message, &gerror, &debug);
2150 GST_WARNING ("%p: got warning %s (%s)", media, gerror->message, debug);
2151 g_error_free (gerror);
2155 case GST_MESSAGE_ELEMENT:
2157 const GstStructure *s;
2159 s = gst_message_get_structure (message);
2160 if (gst_structure_has_name (s, "GstRTSPStreamBlocking")) {
2161 GST_DEBUG ("media received blocking message");
2162 if (priv->blocked && media_streams_blocking (media)) {
2163 GST_DEBUG ("media is blocking");
2164 collect_media_stats (media);
2166 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
2167 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
2172 case GST_MESSAGE_STREAM_STATUS:
2174 case GST_MESSAGE_ASYNC_DONE:
2176 /* when we are dynamically adding pads, the addition of the udpsrc will
2177 * temporarily produce ASYNC_DONE messages. We have to ignore them and
2178 * wait for the final ASYNC_DONE after everything prerolled */
2179 GST_INFO ("%p: ignoring ASYNC_DONE", media);
2181 GST_INFO ("%p: got ASYNC_DONE", media);
2182 collect_media_stats (media);
2184 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
2185 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
2188 case GST_MESSAGE_EOS:
2189 GST_INFO ("%p: got EOS", media);
2191 if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARING) {
2192 GST_DEBUG ("shutting down after EOS");
2193 finish_unprepare (media);
2197 GST_INFO ("%p: got message type %d (%s)", media, type,
2198 gst_message_type_get_name (type));
2205 bus_message (GstBus * bus, GstMessage * message, GstRTSPMedia * media)
2207 GstRTSPMediaPrivate *priv = media->priv;
2208 GstRTSPMediaClass *klass;
2211 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2213 g_rec_mutex_lock (&priv->state_lock);
2214 if (klass->handle_message)
2215 ret = klass->handle_message (media, message);
2218 g_rec_mutex_unlock (&priv->state_lock);
2224 watch_destroyed (GstRTSPMedia * media)
2226 GST_DEBUG_OBJECT (media, "source destroyed");
2227 g_object_unref (media);
2231 find_payload_element (GstElement * payloader)
2233 GstElement *pay = NULL;
2235 if (GST_IS_BIN (payloader)) {
2237 GValue item = { 0 };
2239 iter = gst_bin_iterate_recurse (GST_BIN (payloader));
2240 while (gst_iterator_next (iter, &item) == GST_ITERATOR_OK) {
2241 GstElement *element = (GstElement *) g_value_get_object (&item);
2242 GstElementClass *eclass = GST_ELEMENT_GET_CLASS (element);
2246 gst_element_class_get_metadata (eclass, GST_ELEMENT_METADATA_KLASS);
2250 if (strstr (klass, "Payloader") && strstr (klass, "RTP")) {
2251 pay = gst_object_ref (element);
2252 g_value_unset (&item);
2255 g_value_unset (&item);
2257 gst_iterator_free (iter);
2259 pay = g_object_ref (payloader);
2265 /* called from streaming threads */
2267 pad_added_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
2269 GstRTSPMediaPrivate *priv = media->priv;
2270 GstRTSPStream *stream;
2273 /* find the real payload element */
2274 pay = find_payload_element (element);
2275 stream = gst_rtsp_media_create_stream (media, pay, pad);
2276 gst_object_unref (pay);
2278 GST_INFO ("pad added %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
2280 g_rec_mutex_lock (&priv->state_lock);
2281 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARING)
2284 g_object_set_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream", stream);
2286 /* we will be adding elements below that will cause ASYNC_DONE to be
2287 * posted in the bus. We want to ignore those messages until the
2288 * pipeline really prerolled. */
2289 priv->adding = TRUE;
2291 /* join the element in the PAUSED state because this callback is
2292 * called from the streaming thread and it is PAUSED */
2293 if (!gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
2294 priv->rtpbin, GST_STATE_PAUSED)) {
2295 GST_WARNING ("failed to join bin element");
2298 priv->adding = FALSE;
2299 g_rec_mutex_unlock (&priv->state_lock);
2306 gst_rtsp_media_remove_stream (media, stream);
2307 g_rec_mutex_unlock (&priv->state_lock);
2308 GST_INFO ("ignore pad because we are not preparing");
2314 pad_removed_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
2316 GstRTSPMediaPrivate *priv = media->priv;
2317 GstRTSPStream *stream;
2319 stream = g_object_get_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream");
2323 GST_INFO ("pad removed %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
2325 g_rec_mutex_lock (&priv->state_lock);
2326 gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
2327 g_rec_mutex_unlock (&priv->state_lock);
2329 gst_rtsp_media_remove_stream (media, stream);
2333 remove_fakesink (GstRTSPMediaPrivate * priv)
2335 GstElement *fakesink;
2337 g_mutex_lock (&priv->lock);
2338 if ((fakesink = priv->fakesink))
2339 gst_object_ref (fakesink);
2340 priv->fakesink = NULL;
2341 g_mutex_unlock (&priv->lock);
2344 gst_bin_remove (GST_BIN (priv->pipeline), fakesink);
2345 gst_element_set_state (fakesink, GST_STATE_NULL);
2346 gst_object_unref (fakesink);
2347 GST_INFO ("removed fakesink");
2352 no_more_pads_cb (GstElement * element, GstRTSPMedia * media)
2354 GstRTSPMediaPrivate *priv = media->priv;
2356 GST_INFO ("no more pads");
2357 remove_fakesink (priv);
2360 typedef struct _DynPaySignalHandlers DynPaySignalHandlers;
2362 struct _DynPaySignalHandlers
2364 gulong pad_added_handler;
2365 gulong pad_removed_handler;
2366 gulong no_more_pads_handler;
2370 start_preroll (GstRTSPMedia * media)
2372 GstRTSPMediaPrivate *priv = media->priv;
2373 GstStateChangeReturn ret;
2375 GST_INFO ("setting pipeline to PAUSED for media %p", media);
2376 /* first go to PAUSED */
2377 ret = set_target_state (media, GST_STATE_PAUSED, TRUE);
2380 case GST_STATE_CHANGE_SUCCESS:
2381 GST_INFO ("SUCCESS state change for media %p", media);
2382 priv->seekable = TRUE;
2384 case GST_STATE_CHANGE_ASYNC:
2385 GST_INFO ("ASYNC state change for media %p", media);
2386 priv->seekable = TRUE;
2388 case GST_STATE_CHANGE_NO_PREROLL:
2389 /* we need to go to PLAYING */
2390 GST_INFO ("NO_PREROLL state change: live media %p", media);
2391 /* FIXME we disable seeking for live streams for now. We should perform a
2392 * seeking query in preroll instead */
2393 priv->seekable = FALSE;
2394 priv->is_live = TRUE;
2395 if (!(priv->transport_mode & GST_RTSP_TRANSPORT_MODE_RECORD)) {
2396 /* start blocked to make sure nothing goes to the sink */
2397 media_streams_set_blocked (media, TRUE);
2399 ret = set_state (media, GST_STATE_PLAYING);
2400 if (ret == GST_STATE_CHANGE_FAILURE)
2403 case GST_STATE_CHANGE_FAILURE:
2411 GST_WARNING ("failed to preroll pipeline");
2417 wait_preroll (GstRTSPMedia * media)
2419 GstRTSPMediaStatus status;
2421 GST_DEBUG ("wait to preroll pipeline");
2423 /* wait until pipeline is prerolled */
2424 status = gst_rtsp_media_get_status (media);
2425 if (status == GST_RTSP_MEDIA_STATUS_ERROR)
2426 goto preroll_failed;
2432 GST_WARNING ("failed to preroll pipeline");
2438 start_prepare (GstRTSPMedia * media)
2440 GstRTSPMediaPrivate *priv = media->priv;
2444 /* link streams we already have, other streams might appear when we have
2445 * dynamic elements */
2446 for (i = 0; i < priv->streams->len; i++) {
2447 GstRTSPStream *stream;
2449 stream = g_ptr_array_index (priv->streams, i);
2451 if (!gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
2452 priv->rtpbin, GST_STATE_NULL)) {
2453 goto join_bin_failed;
2458 g_object_set (priv->rtpbin, "do-retransmission", priv->rtx_time > 0, NULL);
2460 for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
2461 GstElement *elem = walk->data;
2462 DynPaySignalHandlers *handlers = g_slice_new (DynPaySignalHandlers);
2464 GST_INFO ("adding callbacks for dynamic element %p", elem);
2466 handlers->pad_added_handler = g_signal_connect (elem, "pad-added",
2467 (GCallback) pad_added_cb, media);
2468 handlers->pad_removed_handler = g_signal_connect (elem, "pad-removed",
2469 (GCallback) pad_removed_cb, media);
2470 handlers->no_more_pads_handler = g_signal_connect (elem, "no-more-pads",
2471 (GCallback) no_more_pads_cb, media);
2473 g_object_set_data (G_OBJECT (elem), "gst-rtsp-dynpay-handlers", handlers);
2475 /* we add a fakesink here in order to make the state change async. We remove
2476 * the fakesink again in the no-more-pads callback. */
2477 priv->fakesink = gst_element_factory_make ("fakesink", "fakesink");
2478 gst_bin_add (GST_BIN (priv->pipeline), priv->fakesink);
2481 if (!start_preroll (media))
2482 goto preroll_failed;
2488 GST_WARNING ("failed to join bin element");
2489 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2494 GST_WARNING ("failed to preroll pipeline");
2495 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2501 default_prepare (GstRTSPMedia * media, GstRTSPThread * thread)
2503 GstRTSPMediaPrivate *priv;
2504 GstRTSPMediaClass *klass;
2506 GMainContext *context;
2511 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2513 if (!klass->create_rtpbin)
2514 goto no_create_rtpbin;
2516 priv->rtpbin = klass->create_rtpbin (media);
2517 if (priv->rtpbin != NULL) {
2518 gboolean success = TRUE;
2520 g_object_set (priv->rtpbin, "latency", priv->latency, NULL);
2522 if (klass->setup_rtpbin)
2523 success = klass->setup_rtpbin (media, priv->rtpbin);
2525 if (success == FALSE) {
2526 gst_object_unref (priv->rtpbin);
2527 priv->rtpbin = NULL;
2530 if (priv->rtpbin == NULL)
2533 priv->thread = thread;
2534 context = (thread != NULL) ? (thread->context) : NULL;
2536 bus = gst_pipeline_get_bus (GST_PIPELINE_CAST (priv->pipeline));
2538 /* add the pipeline bus to our custom mainloop */
2539 priv->source = gst_bus_create_watch (bus);
2540 gst_object_unref (bus);
2542 g_source_set_callback (priv->source, (GSourceFunc) bus_message,
2543 g_object_ref (media), (GDestroyNotify) watch_destroyed);
2545 priv->id = g_source_attach (priv->source, context);
2547 /* add stuff to the bin */
2548 gst_bin_add (GST_BIN (priv->pipeline), priv->rtpbin);
2550 /* do remainder in context */
2551 source = g_idle_source_new ();
2552 g_source_set_callback (source, (GSourceFunc) start_prepare, media, NULL);
2553 g_source_attach (source, context);
2554 g_source_unref (source);
2561 GST_ERROR ("no create_rtpbin function");
2562 g_critical ("no create_rtpbin vmethod function set");
2567 GST_WARNING ("no rtpbin element");
2568 g_warning ("failed to create element 'rtpbin', check your installation");
2574 * gst_rtsp_media_prepare:
2575 * @media: a #GstRTSPMedia
2576 * @thread: (transfer full) (allow-none): a #GstRTSPThread to run the
2577 * bus handler or %NULL
2579 * Prepare @media for streaming. This function will create the objects
2580 * to manage the streaming. A pipeline must have been set on @media with
2581 * gst_rtsp_media_take_pipeline().
2583 * It will preroll the pipeline and collect vital information about the streams
2584 * such as the duration.
2586 * Returns: %TRUE on success.
2589 gst_rtsp_media_prepare (GstRTSPMedia * media, GstRTSPThread * thread)
2591 GstRTSPMediaPrivate *priv;
2592 GstRTSPMediaClass *klass;
2594 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2598 g_rec_mutex_lock (&priv->state_lock);
2599 priv->prepare_count++;
2601 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED ||
2602 priv->status == GST_RTSP_MEDIA_STATUS_SUSPENDED)
2605 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
2608 if (priv->status != GST_RTSP_MEDIA_STATUS_UNPREPARED)
2609 goto not_unprepared;
2611 if (!priv->reusable && priv->reused)
2614 GST_INFO ("preparing media %p", media);
2616 /* reset some variables */
2617 priv->is_live = FALSE;
2618 priv->seekable = FALSE;
2619 priv->buffering = FALSE;
2621 /* we're preparing now */
2622 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARING);
2624 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2625 if (klass->prepare) {
2626 if (!klass->prepare (media, thread))
2627 goto prepare_failed;
2631 g_rec_mutex_unlock (&priv->state_lock);
2633 /* now wait for all pads to be prerolled, FIXME, we should somehow be
2634 * able to do this async so that we don't block the server thread. */
2635 if (!wait_preroll (media))
2636 goto preroll_failed;
2638 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_PREPARED], 0, NULL);
2640 GST_INFO ("object %p is prerolled", media);
2647 /* we are not going to use the giving thread, so stop it. */
2649 gst_rtsp_thread_stop (thread);
2654 GST_LOG ("media %p was prepared", media);
2655 /* we are not going to use the giving thread, so stop it. */
2657 gst_rtsp_thread_stop (thread);
2658 g_rec_mutex_unlock (&priv->state_lock);
2664 /* we are not going to use the giving thread, so stop it. */
2666 gst_rtsp_thread_stop (thread);
2667 GST_WARNING ("media %p was not unprepared", media);
2668 priv->prepare_count--;
2669 g_rec_mutex_unlock (&priv->state_lock);
2674 /* we are not going to use the giving thread, so stop it. */
2676 gst_rtsp_thread_stop (thread);
2677 priv->prepare_count--;
2678 g_rec_mutex_unlock (&priv->state_lock);
2679 GST_WARNING ("can not reuse media %p", media);
2684 /* we are not going to use the giving thread, so stop it. */
2686 gst_rtsp_thread_stop (thread);
2687 priv->prepare_count--;
2688 g_rec_mutex_unlock (&priv->state_lock);
2689 GST_ERROR ("failed to prepare media");
2694 GST_WARNING ("failed to preroll pipeline");
2695 gst_rtsp_media_unprepare (media);
2700 /* must be called with state-lock */
2702 finish_unprepare (GstRTSPMedia * media)
2704 GstRTSPMediaPrivate *priv = media->priv;
2708 GST_DEBUG ("shutting down");
2710 /* release the lock on shutdown, otherwise pad_added_cb might try to
2711 * acquire the lock and then we deadlock */
2712 g_rec_mutex_unlock (&priv->state_lock);
2713 set_state (media, GST_STATE_NULL);
2714 g_rec_mutex_lock (&priv->state_lock);
2715 remove_fakesink (priv);
2717 for (i = 0; i < priv->streams->len; i++) {
2718 GstRTSPStream *stream;
2720 GST_INFO ("Removing elements of stream %d from pipeline", i);
2722 stream = g_ptr_array_index (priv->streams, i);
2724 gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
2727 /* remove the pad signal handlers */
2728 for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
2729 GstElement *elem = walk->data;
2730 DynPaySignalHandlers *handlers;
2733 g_object_steal_data (G_OBJECT (elem), "gst-rtsp-dynpay-handlers");
2734 g_assert (handlers != NULL);
2736 g_signal_handler_disconnect (G_OBJECT (elem), handlers->pad_added_handler);
2737 g_signal_handler_disconnect (G_OBJECT (elem),
2738 handlers->pad_removed_handler);
2739 g_signal_handler_disconnect (G_OBJECT (elem),
2740 handlers->no_more_pads_handler);
2742 g_slice_free (DynPaySignalHandlers, handlers);
2745 gst_bin_remove (GST_BIN (priv->pipeline), priv->rtpbin);
2746 priv->rtpbin = NULL;
2749 gst_object_unref (priv->nettime);
2750 priv->nettime = NULL;
2752 priv->reused = TRUE;
2753 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_UNPREPARED);
2755 /* when the media is not reusable, this will effectively unref the media and
2757 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_UNPREPARED], 0, NULL);
2759 /* the source has the last ref to the media */
2761 GST_DEBUG ("destroy source");
2762 g_source_destroy (priv->source);
2763 g_source_unref (priv->source);
2766 GST_DEBUG ("stop thread");
2767 gst_rtsp_thread_stop (priv->thread);
2771 /* called with state-lock */
2773 default_unprepare (GstRTSPMedia * media)
2775 GstRTSPMediaPrivate *priv = media->priv;
2777 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_UNPREPARING);
2779 if (priv->eos_shutdown) {
2780 GST_DEBUG ("sending EOS for shutdown");
2781 /* ref so that we don't disappear */
2782 gst_element_send_event (priv->pipeline, gst_event_new_eos ());
2783 /* we need to go to playing again for the EOS to propagate, normally in this
2784 * state, nothing is receiving data from us anymore so this is ok. */
2785 set_state (media, GST_STATE_PLAYING);
2787 finish_unprepare (media);
2793 * gst_rtsp_media_unprepare:
2794 * @media: a #GstRTSPMedia
2796 * Unprepare @media. After this call, the media should be prepared again before
2797 * it can be used again. If the media is set to be non-reusable, a new instance
2800 * Returns: %TRUE on success.
2803 gst_rtsp_media_unprepare (GstRTSPMedia * media)
2805 GstRTSPMediaPrivate *priv;
2808 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2812 g_rec_mutex_lock (&priv->state_lock);
2813 if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARED)
2814 goto was_unprepared;
2816 priv->prepare_count--;
2817 if (priv->prepare_count > 0)
2820 GST_INFO ("unprepare media %p", media);
2822 media_streams_set_blocked (media, FALSE);
2823 set_target_state (media, GST_STATE_NULL, FALSE);
2826 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED) {
2827 GstRTSPMediaClass *klass;
2829 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2830 if (klass->unprepare)
2831 success = klass->unprepare (media);
2833 finish_unprepare (media);
2835 g_rec_mutex_unlock (&priv->state_lock);
2841 g_rec_mutex_unlock (&priv->state_lock);
2842 GST_INFO ("media %p was already unprepared", media);
2847 GST_INFO ("media %p still prepared %d times", media, priv->prepare_count);
2848 g_rec_mutex_unlock (&priv->state_lock);
2853 /* should be called with state-lock */
2855 get_clock_unlocked (GstRTSPMedia * media)
2857 if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED) {
2858 GST_DEBUG_OBJECT (media, "media was not prepared");
2861 return gst_pipeline_get_clock (GST_PIPELINE_CAST (media->priv->pipeline));
2865 * gst_rtsp_media_get_clock:
2866 * @media: a #GstRTSPMedia
2868 * Get the clock that is used by the pipeline in @media.
2870 * @media must be prepared before this method returns a valid clock object.
2872 * Returns: (transfer full): the #GstClock used by @media. unref after usage.
2875 gst_rtsp_media_get_clock (GstRTSPMedia * media)
2878 GstRTSPMediaPrivate *priv;
2880 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
2884 g_rec_mutex_lock (&priv->state_lock);
2885 clock = get_clock_unlocked (media);
2886 g_rec_mutex_unlock (&priv->state_lock);
2892 * gst_rtsp_media_get_base_time:
2893 * @media: a #GstRTSPMedia
2895 * Get the base_time that is used by the pipeline in @media.
2897 * @media must be prepared before this method returns a valid base_time.
2899 * Returns: the base_time used by @media.
2902 gst_rtsp_media_get_base_time (GstRTSPMedia * media)
2904 GstClockTime result;
2905 GstRTSPMediaPrivate *priv;
2907 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_CLOCK_TIME_NONE);
2911 g_rec_mutex_lock (&priv->state_lock);
2912 if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
2915 result = gst_element_get_base_time (media->priv->pipeline);
2916 g_rec_mutex_unlock (&priv->state_lock);
2923 g_rec_mutex_unlock (&priv->state_lock);
2924 GST_DEBUG_OBJECT (media, "media was not prepared");
2925 return GST_CLOCK_TIME_NONE;
2930 * gst_rtsp_media_get_time_provider:
2931 * @media: a #GstRTSPMedia
2932 * @address: (allow-none): an address or %NULL
2933 * @port: a port or 0
2935 * Get the #GstNetTimeProvider for the clock used by @media. The time provider
2936 * will listen on @address and @port for client time requests.
2938 * Returns: (transfer full): the #GstNetTimeProvider of @media.
2940 GstNetTimeProvider *
2941 gst_rtsp_media_get_time_provider (GstRTSPMedia * media, const gchar * address,
2944 GstRTSPMediaPrivate *priv;
2945 GstNetTimeProvider *provider = NULL;
2947 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
2951 g_rec_mutex_lock (&priv->state_lock);
2952 if (priv->time_provider) {
2953 if ((provider = priv->nettime) == NULL) {
2956 if (priv->time_provider && (clock = get_clock_unlocked (media))) {
2957 provider = gst_net_time_provider_new (clock, address, port);
2958 gst_object_unref (clock);
2960 priv->nettime = provider;
2964 g_rec_mutex_unlock (&priv->state_lock);
2967 gst_object_ref (provider);
2973 default_setup_sdp (GstRTSPMedia * media, GstSDPMessage * sdp, GstSDPInfo * info)
2975 return gst_rtsp_sdp_from_media (sdp, info, media);
2979 * gst_rtsp_media_setup_sdp:
2980 * @media: a #GstRTSPMedia
2981 * @sdp: (transfer none): a #GstSDPMessage
2982 * @info: (transfer none): a #GstSDPInfo
2984 * Add @media specific info to @sdp. @info is used to configure the connection
2985 * information in the SDP.
2987 * Returns: TRUE on success.
2990 gst_rtsp_media_setup_sdp (GstRTSPMedia * media, GstSDPMessage * sdp,
2993 GstRTSPMediaPrivate *priv;
2994 GstRTSPMediaClass *klass;
2997 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2998 g_return_val_if_fail (sdp != NULL, FALSE);
2999 g_return_val_if_fail (info != NULL, FALSE);
3003 g_rec_mutex_lock (&priv->state_lock);
3005 klass = GST_RTSP_MEDIA_GET_CLASS (media);
3007 if (!klass->setup_sdp)
3010 res = klass->setup_sdp (media, sdp, info);
3012 g_rec_mutex_unlock (&priv->state_lock);
3019 g_rec_mutex_unlock (&priv->state_lock);
3020 GST_ERROR ("no setup_sdp function");
3021 g_critical ("no setup_sdp vmethod function set");
3026 static const gchar *
3027 rtsp_get_attribute_for_pt (const GstSDPMedia * media, const gchar * name,
3036 if ((attr = gst_sdp_media_get_attribute_val_n (media, name, i)) == NULL)
3039 if (sscanf (attr, "%d ", &val) != 1)
3048 #define PARSE_INT(p, del, res) \
3051 p = strstr (p, del); \
3061 #define PARSE_STRING(p, del, res) \
3064 p = strstr (p, del); \
3076 #define SKIP_SPACES(p) \
3077 while (*p && g_ascii_isspace (*p)) \
3082 * <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
3085 parse_rtpmap (const gchar * rtpmap, gint * payload, gchar ** name,
3086 gint * rate, gchar ** params)
3090 p = (gchar *) rtpmap;
3092 PARSE_INT (p, " ", *payload);
3100 PARSE_STRING (p, "/", *name);
3101 if (*name == NULL) {
3102 GST_DEBUG ("no rate, name %s", p);
3103 /* no rate, assume -1 then, this is not supposed to happen but RealMedia
3104 * streams seem to omit the rate. */
3111 p = strstr (p, "/");
3129 * Mapping of caps to and from SDP fields:
3131 * a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
3132 * a=fmtp:<payload> <param>[=<value>];...
3135 media_to_caps (gint pt, const GstSDPMedia * media)
3138 const gchar *rtpmap;
3142 gchar *params = NULL;
3148 /* get and parse rtpmap */
3149 rtpmap = rtsp_get_attribute_for_pt (media, "rtpmap", pt);
3152 ret = parse_rtpmap (rtpmap, &payload, &name, &rate, ¶ms);
3154 g_warning ("error parsing rtpmap, ignoring");
3158 /* dynamic payloads need rtpmap or we fail */
3159 if (rtpmap == NULL && pt >= 96)
3162 /* check if we have a rate, if not, we need to look up the rate from the
3163 * default rates based on the payload types. */
3165 const GstRTPPayloadInfo *info;
3167 if (GST_RTP_PAYLOAD_IS_DYNAMIC (pt)) {
3168 /* dynamic types, use media and encoding_name */
3169 tmp = g_ascii_strdown (media->media, -1);
3170 info = gst_rtp_payload_info_for_name (tmp, name);
3173 /* static types, use payload type */
3174 info = gst_rtp_payload_info_for_pt (pt);
3178 if ((rate = info->clock_rate) == 0)
3181 /* we fail if we cannot find one */
3186 tmp = g_ascii_strdown (media->media, -1);
3187 caps = gst_caps_new_simple ("application/x-unknown",
3188 "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
3190 s = gst_caps_get_structure (caps, 0);
3192 gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);
3194 /* encoding name must be upper case */
3196 tmp = g_ascii_strup (name, -1);
3197 gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
3201 /* params must be lower case */
3202 if (params != NULL) {
3203 tmp = g_ascii_strdown (params, -1);
3204 gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
3208 /* parse optional fmtp: field */
3209 if ((fmtp = rtsp_get_attribute_for_pt (media, "fmtp", pt))) {
3215 /* p is now of the format <payload> <param>[=<value>];... */
3216 PARSE_INT (p, " ", payload);
3217 if (payload != -1 && payload == pt) {
3221 /* <param>[=<value>] are separated with ';' */
3222 pairs = g_strsplit (p, ";", 0);
3223 for (i = 0; pairs[i]; i++) {
3225 const gchar *val, *key;
3227 /* the key may not have a '=', the value can have other '='s */
3228 valpos = strstr (pairs[i], "=");
3230 /* we have a '=' and thus a value, remove the '=' with \0 */
3232 /* value is everything between '=' and ';'. We split the pairs at ;
3233 * boundaries so we can take the remainder of the value. Some servers
3234 * put spaces around the value which we strip off here. Alternatively
3235 * we could strip those spaces in the depayloaders should these spaces
3236 * actually carry any meaning in the future. */
3237 val = g_strstrip (valpos + 1);
3239 /* simple <param>;.. is translated into <param>=1;... */
3242 /* strip the key of spaces, convert key to lowercase but not the value. */
3243 key = g_strstrip (pairs[i]);
3244 if (strlen (key) > 1) {
3245 tmp = g_ascii_strdown (key, -1);
3246 gst_structure_set (s, tmp, G_TYPE_STRING, val, NULL);
3258 g_warning ("rtpmap type not given for dynamic payload %d", pt);
3263 g_warning ("rate unknown for payload type %d", pt);
3269 parse_keymgmt (const gchar * keymgmt, GstCaps * caps)
3271 gboolean res = FALSE;
3275 GstMIKEYMessage *msg;
3276 const GstMIKEYPayload *payload;
3277 const gchar *srtp_cipher;
3278 const gchar *srtp_auth;
3280 p = (gchar *) keymgmt;
3286 PARSE_STRING (p, " ", kmpid);
3287 if (!g_str_equal (kmpid, "mikey"))
3290 data = g_base64_decode (p, &size);
3294 msg = gst_mikey_message_new_from_data (data, size, NULL, NULL);
3299 srtp_cipher = "aes-128-icm";
3300 srtp_auth = "hmac-sha1-80";
3302 /* check the Security policy if any */
3303 if ((payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_SP, 0))) {
3304 GstMIKEYPayloadSP *p = (GstMIKEYPayloadSP *) payload;
3307 if (p->proto != GST_MIKEY_SEC_PROTO_SRTP)
3310 len = gst_mikey_payload_sp_get_n_params (payload);
3311 for (i = 0; i < len; i++) {
3312 const GstMIKEYPayloadSPParam *param =
3313 gst_mikey_payload_sp_get_param (payload, i);
3315 switch (param->type) {
3316 case GST_MIKEY_SP_SRTP_ENC_ALG:
3317 switch (param->val[0]) {
3319 srtp_cipher = "null";
3323 srtp_cipher = "aes-128-icm";
3329 case GST_MIKEY_SP_SRTP_ENC_KEY_LEN:
3330 switch (param->val[0]) {
3331 case AES_128_KEY_LEN:
3332 srtp_cipher = "aes-128-icm";
3334 case AES_256_KEY_LEN:
3335 srtp_cipher = "aes-256-icm";
3341 case GST_MIKEY_SP_SRTP_AUTH_ALG:
3342 switch (param->val[0]) {
3348 srtp_auth = "hmac-sha1-80";
3354 case GST_MIKEY_SP_SRTP_AUTH_KEY_LEN:
3355 switch (param->val[0]) {
3356 case HMAC_32_KEY_LEN:
3357 srtp_auth = "hmac-sha1-32";
3359 case HMAC_80_KEY_LEN:
3360 srtp_auth = "hmac-sha1-80";
3366 case GST_MIKEY_SP_SRTP_SRTP_ENC:
3368 case GST_MIKEY_SP_SRTP_SRTCP_ENC:
3376 if (!(payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_KEMAC, 0)))
3379 GstMIKEYPayloadKEMAC *p = (GstMIKEYPayloadKEMAC *) payload;
3380 const GstMIKEYPayload *sub;
3381 GstMIKEYPayloadKeyData *pkd;
3384 if (p->enc_alg != GST_MIKEY_ENC_NULL || p->mac_alg != GST_MIKEY_MAC_NULL)
3387 if (!(sub = gst_mikey_payload_kemac_get_sub (payload, 0)))
3390 if (sub->type != GST_MIKEY_PT_KEY_DATA)
3393 pkd = (GstMIKEYPayloadKeyData *) sub;
3395 gst_buffer_new_wrapped (g_memdup (pkd->key_data, pkd->key_len),
3397 gst_caps_set_simple (caps, "srtp-key", GST_TYPE_BUFFER, buf, NULL);
3400 gst_caps_set_simple (caps,
3401 "srtp-cipher", G_TYPE_STRING, srtp_cipher,
3402 "srtp-auth", G_TYPE_STRING, srtp_auth,
3403 "srtcp-cipher", G_TYPE_STRING, srtp_cipher,
3404 "srtcp-auth", G_TYPE_STRING, srtp_auth, NULL);
3408 gst_mikey_message_unref (msg);
3414 * Mapping SDP attributes to caps
3416 * prepend 'a-' to IANA registered sdp attributes names
3417 * (ie: not prefixed with 'x-') in order to avoid
3418 * collision with gstreamer standard caps properties names
3421 sdp_attributes_to_caps (GArray * attributes, GstCaps * caps)
3423 if (attributes->len > 0) {
3427 s = gst_caps_get_structure (caps, 0);
3429 for (i = 0; i < attributes->len; i++) {
3430 GstSDPAttribute *attr = &g_array_index (attributes, GstSDPAttribute, i);
3431 gchar *tofree, *key;
3435 /* skip some of the attribute we already handle */
3436 if (!strcmp (key, "fmtp"))
3438 if (!strcmp (key, "rtpmap"))
3440 if (!strcmp (key, "control"))
3442 if (!strcmp (key, "range"))
3444 if (g_str_equal (key, "key-mgmt")) {
3445 parse_keymgmt (attr->value, caps);
3449 /* string must be valid UTF8 */
3450 if (!g_utf8_validate (attr->value, -1, NULL))
3453 if (!g_str_has_prefix (key, "x-"))
3454 tofree = key = g_strdup_printf ("a-%s", key);
3458 GST_DEBUG ("adding caps: %s=%s", key, attr->value);
3459 gst_structure_set (s, key, G_TYPE_STRING, attr->value, NULL);
3466 default_handle_sdp (GstRTSPMedia * media, GstSDPMessage * sdp)
3468 GstRTSPMediaPrivate *priv = media->priv;
3471 medias_len = gst_sdp_message_medias_len (sdp);
3472 if (medias_len != priv->streams->len) {
3473 GST_ERROR ("%p: Media has more or less streams than SDP (%d /= %d)", media,
3474 priv->streams->len, medias_len);
3478 for (i = 0; i < medias_len; i++) {
3479 const gchar *proto, *media_type;
3480 const GstSDPMedia *sdp_media = gst_sdp_message_get_media (sdp, i);
3481 GstRTSPStream *stream;
3482 gint j, formats_len;
3483 const gchar *control;
3484 GstRTSPProfile profile, profiles;
3486 stream = g_ptr_array_index (priv->streams, i);
3488 /* TODO: Should we do something with the other SDP information? */
3491 proto = gst_sdp_media_get_proto (sdp_media);
3492 if (proto == NULL) {
3493 GST_ERROR ("%p: SDP media %d has no proto", media, i);
3497 if (g_str_equal (proto, "RTP/AVP")) {
3498 media_type = "application/x-rtp";
3499 profile = GST_RTSP_PROFILE_AVP;
3500 } else if (g_str_equal (proto, "RTP/SAVP")) {
3501 media_type = "application/x-srtp";
3502 profile = GST_RTSP_PROFILE_SAVP;
3503 } else if (g_str_equal (proto, "RTP/AVPF")) {
3504 media_type = "application/x-rtp";
3505 profile = GST_RTSP_PROFILE_AVPF;
3506 } else if (g_str_equal (proto, "RTP/SAVPF")) {
3507 media_type = "application/x-srtp";
3508 profile = GST_RTSP_PROFILE_SAVPF;
3510 GST_ERROR ("%p: unsupported profile '%s' for stream %d", media, proto, i);
3514 profiles = gst_rtsp_stream_get_profiles (stream);
3515 if ((profiles & profile) == 0) {
3516 GST_ERROR ("%p: unsupported profile '%s' for stream %d", media, proto, i);
3520 formats_len = gst_sdp_media_formats_len (sdp_media);
3521 for (j = 0; j < formats_len; j++) {
3526 pt = atoi (gst_sdp_media_get_format (sdp_media, j));
3528 GST_DEBUG (" looking at %d pt: %d", j, pt);
3531 caps = media_to_caps (pt, sdp_media);
3533 GST_WARNING (" skipping pt %d without caps", pt);
3537 /* do some tweaks */
3538 GST_DEBUG ("mapping sdp session level attributes to caps");
3539 sdp_attributes_to_caps (sdp->attributes, caps);
3540 GST_DEBUG ("mapping sdp media level attributes to caps");
3541 sdp_attributes_to_caps (sdp_media->attributes, caps);
3543 s = gst_caps_get_structure (caps, 0);
3544 gst_structure_set_name (s, media_type);
3546 gst_rtsp_stream_set_pt_map (stream, pt, caps);
3547 gst_caps_unref (caps);
3550 control = gst_sdp_media_get_attribute_val (sdp_media, "control");
3552 gst_rtsp_stream_set_control (stream, control);
3560 * gst_rtsp_media_handle_sdp:
3561 * @media: a #GstRTSPMedia
3562 * @sdp: (transfer none): a #GstSDPMessage
3564 * Configure an SDP on @media for receiving streams
3566 * Returns: TRUE on success.
3569 gst_rtsp_media_handle_sdp (GstRTSPMedia * media, GstSDPMessage * sdp)
3571 GstRTSPMediaPrivate *priv;
3572 GstRTSPMediaClass *klass;
3575 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
3576 g_return_val_if_fail (sdp != NULL, FALSE);
3580 g_rec_mutex_lock (&priv->state_lock);
3582 klass = GST_RTSP_MEDIA_GET_CLASS (media);
3584 if (!klass->handle_sdp)
3587 res = klass->handle_sdp (media, sdp);
3589 g_rec_mutex_unlock (&priv->state_lock);
3596 g_rec_mutex_unlock (&priv->state_lock);
3597 GST_ERROR ("no handle_sdp function");
3598 g_critical ("no handle_sdp vmethod function set");
3604 do_set_seqnum (GstRTSPStream * stream)
3607 seq_num = gst_rtsp_stream_get_current_seqnum (stream);
3608 gst_rtsp_stream_set_seqnum_offset (stream, seq_num + 1);
3611 /* call with state_lock */
3613 default_suspend (GstRTSPMedia * media)
3615 GstRTSPMediaPrivate *priv = media->priv;
3616 GstStateChangeReturn ret;
3618 switch (priv->suspend_mode) {
3619 case GST_RTSP_SUSPEND_MODE_NONE:
3620 GST_DEBUG ("media %p no suspend", media);
3622 case GST_RTSP_SUSPEND_MODE_PAUSE:
3623 GST_DEBUG ("media %p suspend to PAUSED", media);
3624 ret = set_target_state (media, GST_STATE_PAUSED, TRUE);
3625 if (ret == GST_STATE_CHANGE_FAILURE)
3628 case GST_RTSP_SUSPEND_MODE_RESET:
3629 GST_DEBUG ("media %p suspend to NULL", media);
3630 ret = set_target_state (media, GST_STATE_NULL, TRUE);
3631 if (ret == GST_STATE_CHANGE_FAILURE)
3633 /* Because payloader needs to set the sequence number as
3634 * monotonic, we need to preserve the sequence number
3635 * after pause. (otherwise going from pause to play, which
3636 * is actually from NULL to PLAY will create a new sequence
3638 g_ptr_array_foreach (priv->streams, (GFunc) do_set_seqnum, NULL);
3644 /* let the streams do the state changes freely, if any */
3645 media_streams_set_blocked (media, FALSE);
3652 GST_WARNING ("failed changing pipeline's state for media %p", media);
3658 * gst_rtsp_media_suspend:
3659 * @media: a #GstRTSPMedia
3661 * Suspend @media. The state of the pipeline managed by @media is set to
3662 * GST_STATE_NULL but all streams are kept. @media can be prepared again
3663 * with gst_rtsp_media_unsuspend()
3665 * @media must be prepared with gst_rtsp_media_prepare();
3667 * Returns: %TRUE on success.
3670 gst_rtsp_media_suspend (GstRTSPMedia * media)
3672 GstRTSPMediaPrivate *priv = media->priv;
3673 GstRTSPMediaClass *klass;
3675 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
3677 GST_FIXME ("suspend for dynamic pipelines needs fixing");
3679 g_rec_mutex_lock (&priv->state_lock);
3680 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
3683 /* don't attempt to suspend when something is busy */
3684 if (priv->n_active > 0)
3687 klass = GST_RTSP_MEDIA_GET_CLASS (media);
3688 if (klass->suspend) {
3689 if (!klass->suspend (media))
3690 goto suspend_failed;
3693 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_SUSPENDED);
3695 g_rec_mutex_unlock (&priv->state_lock);
3702 g_rec_mutex_unlock (&priv->state_lock);
3703 GST_WARNING ("media %p was not prepared", media);
3708 g_rec_mutex_unlock (&priv->state_lock);
3709 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
3710 GST_WARNING ("failed to suspend media %p", media);
3715 /* call with state_lock */
3717 default_unsuspend (GstRTSPMedia * media)
3719 GstRTSPMediaPrivate *priv = media->priv;
3721 switch (priv->suspend_mode) {
3722 case GST_RTSP_SUSPEND_MODE_NONE:
3723 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
3725 case GST_RTSP_SUSPEND_MODE_PAUSE:
3726 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
3728 case GST_RTSP_SUSPEND_MODE_RESET:
3730 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARING);
3731 if (!start_preroll (media))
3733 g_rec_mutex_unlock (&priv->state_lock);
3735 if (!wait_preroll (media))
3736 goto preroll_failed;
3738 g_rec_mutex_lock (&priv->state_lock);
3749 GST_WARNING ("failed to preroll pipeline");
3754 GST_WARNING ("failed to preroll pipeline");
3760 * gst_rtsp_media_unsuspend:
3761 * @media: a #GstRTSPMedia
3763 * Unsuspend @media if it was in a suspended state. This method does nothing
3764 * when the media was not in the suspended state.
3766 * Returns: %TRUE on success.
3769 gst_rtsp_media_unsuspend (GstRTSPMedia * media)
3771 GstRTSPMediaPrivate *priv = media->priv;
3772 GstRTSPMediaClass *klass;
3774 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
3776 g_rec_mutex_lock (&priv->state_lock);
3777 if (priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
3780 klass = GST_RTSP_MEDIA_GET_CLASS (media);
3781 if (klass->unsuspend) {
3782 if (!klass->unsuspend (media))
3783 goto unsuspend_failed;
3787 g_rec_mutex_unlock (&priv->state_lock);
3794 g_rec_mutex_unlock (&priv->state_lock);
3795 GST_WARNING ("failed to unsuspend media %p", media);
3796 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
3801 /* must be called with state-lock */
3803 media_set_pipeline_state_locked (GstRTSPMedia * media, GstState state)
3805 GstRTSPMediaPrivate *priv = media->priv;
3807 if (state == GST_STATE_NULL) {
3808 gst_rtsp_media_unprepare (media);
3810 GST_INFO ("state %s media %p", gst_element_state_get_name (state), media);
3811 set_target_state (media, state, FALSE);
3812 /* when we are buffering, don't update the state yet, this will be done
3813 * when buffering finishes */
3814 if (priv->buffering) {
3815 GST_INFO ("Buffering busy, delay state change");
3817 if (state == GST_STATE_PLAYING)
3818 /* make sure pads are not blocking anymore when going to PLAYING */
3819 media_streams_set_blocked (media, FALSE);
3821 set_state (media, state);
3823 /* and suspend after pause */
3824 if (state == GST_STATE_PAUSED)
3825 gst_rtsp_media_suspend (media);
3831 * gst_rtsp_media_set_pipeline_state:
3832 * @media: a #GstRTSPMedia
3833 * @state: the target state of the pipeline
3835 * Set the state of the pipeline managed by @media to @state
3838 gst_rtsp_media_set_pipeline_state (GstRTSPMedia * media, GstState state)
3840 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
3842 g_rec_mutex_lock (&media->priv->state_lock);
3843 media_set_pipeline_state_locked (media, state);
3844 g_rec_mutex_unlock (&media->priv->state_lock);
3848 * gst_rtsp_media_set_state:
3849 * @media: a #GstRTSPMedia
3850 * @state: the target state of the media
3851 * @transports: (transfer none) (element-type GstRtspServer.RTSPStreamTransport):
3852 * a #GPtrArray of #GstRTSPStreamTransport pointers
3854 * Set the state of @media to @state and for the transports in @transports.
3856 * @media must be prepared with gst_rtsp_media_prepare();
3858 * Returns: %TRUE on success.
3861 gst_rtsp_media_set_state (GstRTSPMedia * media, GstState state,
3862 GPtrArray * transports)
3864 GstRTSPMediaPrivate *priv;
3866 gboolean activate, deactivate, do_state;
3869 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
3870 g_return_val_if_fail (transports != NULL, FALSE);
3874 g_rec_mutex_lock (&priv->state_lock);
3875 if (priv->status == GST_RTSP_MEDIA_STATUS_ERROR)
3877 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
3878 priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
3881 /* NULL and READY are the same */
3882 if (state == GST_STATE_READY)
3883 state = GST_STATE_NULL;
3885 activate = deactivate = FALSE;
3887 GST_INFO ("going to state %s media %p, target state %s",
3888 gst_element_state_get_name (state), media,
3889 gst_element_state_get_name (priv->target_state));
3892 case GST_STATE_NULL:
3893 /* we're going from PLAYING or PAUSED to READY or NULL, deactivate */
3894 if (priv->target_state >= GST_STATE_PAUSED)
3897 case GST_STATE_PAUSED:
3898 /* we're going from PLAYING to PAUSED, READY or NULL, deactivate */
3899 if (priv->target_state == GST_STATE_PLAYING)
3902 case GST_STATE_PLAYING:
3903 /* we're going to PLAYING, activate */
3909 old_active = priv->n_active;
3911 GST_DEBUG ("%d transports, activate %d, deactivate %d", transports->len,
3912 activate, deactivate);
3913 for (i = 0; i < transports->len; i++) {
3914 GstRTSPStreamTransport *trans;
3916 /* we need a non-NULL entry in the array */
3917 trans = g_ptr_array_index (transports, i);
3922 if (gst_rtsp_stream_transport_set_active (trans, TRUE))
3924 } else if (deactivate) {
3925 if (gst_rtsp_stream_transport_set_active (trans, FALSE))
3930 /* we just activated the first media, do the playing state change */
3931 if (old_active == 0 && activate)
3933 /* if we have no more active media, do the downward state changes */
3934 else if (priv->n_active == 0)
3939 GST_INFO ("state %d active %d media %p do_state %d", state, priv->n_active,
3942 if (priv->target_state != state) {
3944 media_set_pipeline_state_locked (media, state);
3946 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STATE], 0, state,
3950 /* remember where we are */
3951 if (state != GST_STATE_NULL && (state == GST_STATE_PAUSED ||
3952 old_active != priv->n_active))
3953 collect_media_stats (media);
3955 g_rec_mutex_unlock (&priv->state_lock);
3962 GST_WARNING ("media %p was not prepared", media);
3963 g_rec_mutex_unlock (&priv->state_lock);
3968 GST_WARNING ("media %p in error status while changing to state %d",
3970 if (state == GST_STATE_NULL) {
3971 for (i = 0; i < transports->len; i++) {
3972 GstRTSPStreamTransport *trans;
3974 /* we need a non-NULL entry in the array */
3975 trans = g_ptr_array_index (transports, i);
3979 gst_rtsp_stream_transport_set_active (trans, FALSE);
3983 g_rec_mutex_unlock (&priv->state_lock);
3989 * gst_rtsp_media_set_transport_mode:
3990 * @media: a #GstRTSPMedia
3991 * @mode: the new value
3993 * Sets if the media pipeline can work in PLAY or RECORD mode
3996 gst_rtsp_media_set_transport_mode (GstRTSPMedia * media,
3997 GstRTSPTransportMode mode)
3999 GstRTSPMediaPrivate *priv;
4001 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
4005 g_mutex_lock (&priv->lock);
4006 priv->transport_mode = mode;
4007 g_mutex_unlock (&priv->lock);
4011 * gst_rtsp_media_get_transport_mode:
4012 * @media: a #GstRTSPMedia
4014 * Check if the pipeline for @media can be used for PLAY or RECORD methods.
4016 * Returns: The transport mode.
4018 GstRTSPTransportMode
4019 gst_rtsp_media_get_transport_mode (GstRTSPMedia * media)
4021 GstRTSPMediaPrivate *priv;
4022 GstRTSPTransportMode res;
4024 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
4028 g_mutex_lock (&priv->lock);
4029 res = priv->transport_mode;
4030 g_mutex_unlock (&priv->lock);